Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * TI BQ24257 charger driver
0004  *
0005  * Copyright (C) 2015 Intel Corporation
0006  *
0007  * Datasheets:
0008  * https://www.ti.com/product/bq24250
0009  * https://www.ti.com/product/bq24251
0010  * https://www.ti.com/product/bq24257
0011  */
0012 
0013 #include <linux/module.h>
0014 #include <linux/i2c.h>
0015 #include <linux/power_supply.h>
0016 #include <linux/regmap.h>
0017 #include <linux/types.h>
0018 #include <linux/gpio/consumer.h>
0019 #include <linux/interrupt.h>
0020 #include <linux/delay.h>
0021 
0022 #include <linux/acpi.h>
0023 #include <linux/of.h>
0024 
0025 #define BQ24257_REG_1           0x00
0026 #define BQ24257_REG_2           0x01
0027 #define BQ24257_REG_3           0x02
0028 #define BQ24257_REG_4           0x03
0029 #define BQ24257_REG_5           0x04
0030 #define BQ24257_REG_6           0x05
0031 #define BQ24257_REG_7           0x06
0032 
0033 #define BQ24257_MANUFACTURER        "Texas Instruments"
0034 #define BQ24257_PG_GPIO         "pg"
0035 
0036 #define BQ24257_ILIM_SET_DELAY      1000    /* msec */
0037 
0038 /*
0039  * When adding support for new devices make sure that enum bq2425x_chip and
0040  * bq2425x_chip_name[] always stay in sync!
0041  */
0042 enum bq2425x_chip {
0043     BQ24250,
0044     BQ24251,
0045     BQ24257,
0046 };
0047 
0048 static const char *const bq2425x_chip_name[] = {
0049     "bq24250",
0050     "bq24251",
0051     "bq24257",
0052 };
0053 
0054 enum bq24257_fields {
0055     F_WD_FAULT, F_WD_EN, F_STAT, F_FAULT,               /* REG 1 */
0056     F_RESET, F_IILIMIT, F_EN_STAT, F_EN_TERM, F_CE, F_HZ_MODE,  /* REG 2 */
0057     F_VBAT, F_USB_DET,                      /* REG 3 */
0058     F_ICHG, F_ITERM,                        /* REG 4 */
0059     F_LOOP_STATUS, F_LOW_CHG, F_DPDM_EN, F_CE_STATUS, F_VINDPM, /* REG 5 */
0060     F_X2_TMR_EN, F_TMR, F_SYSOFF, F_TS_EN, F_TS_STAT,       /* REG 6 */
0061     F_VOVP, F_CLR_VDP, F_FORCE_BATDET, F_FORCE_PTM,         /* REG 7 */
0062 
0063     F_MAX_FIELDS
0064 };
0065 
0066 /* initial field values, converted from uV/uA */
0067 struct bq24257_init_data {
0068     u8 ichg;    /* charge current      */
0069     u8 vbat;    /* regulation voltage  */
0070     u8 iterm;   /* termination current */
0071     u8 iilimit; /* input current limit */
0072     u8 vovp;    /* over voltage protection voltage */
0073     u8 vindpm;  /* VDMP input threshold voltage */
0074 };
0075 
0076 struct bq24257_state {
0077     u8 status;
0078     u8 fault;
0079     bool power_good;
0080 };
0081 
0082 struct bq24257_device {
0083     struct i2c_client *client;
0084     struct device *dev;
0085     struct power_supply *charger;
0086 
0087     enum bq2425x_chip chip;
0088 
0089     struct regmap *rmap;
0090     struct regmap_field *rmap_fields[F_MAX_FIELDS];
0091 
0092     struct gpio_desc *pg;
0093 
0094     struct delayed_work iilimit_setup_work;
0095 
0096     struct bq24257_init_data init_data;
0097     struct bq24257_state state;
0098 
0099     struct mutex lock; /* protect state data */
0100 
0101     bool iilimit_autoset_enable;
0102 };
0103 
0104 static bool bq24257_is_volatile_reg(struct device *dev, unsigned int reg)
0105 {
0106     switch (reg) {
0107     case BQ24257_REG_2:
0108     case BQ24257_REG_4:
0109         return false;
0110 
0111     default:
0112         return true;
0113     }
0114 }
0115 
0116 static const struct regmap_config bq24257_regmap_config = {
0117     .reg_bits = 8,
0118     .val_bits = 8,
0119 
0120     .max_register = BQ24257_REG_7,
0121     .cache_type = REGCACHE_RBTREE,
0122 
0123     .volatile_reg = bq24257_is_volatile_reg,
0124 };
0125 
0126 static const struct reg_field bq24257_reg_fields[] = {
0127     /* REG 1 */
0128     [F_WD_FAULT]        = REG_FIELD(BQ24257_REG_1, 7, 7),
0129     [F_WD_EN]       = REG_FIELD(BQ24257_REG_1, 6, 6),
0130     [F_STAT]        = REG_FIELD(BQ24257_REG_1, 4, 5),
0131     [F_FAULT]       = REG_FIELD(BQ24257_REG_1, 0, 3),
0132     /* REG 2 */
0133     [F_RESET]       = REG_FIELD(BQ24257_REG_2, 7, 7),
0134     [F_IILIMIT]     = REG_FIELD(BQ24257_REG_2, 4, 6),
0135     [F_EN_STAT]     = REG_FIELD(BQ24257_REG_2, 3, 3),
0136     [F_EN_TERM]     = REG_FIELD(BQ24257_REG_2, 2, 2),
0137     [F_CE]          = REG_FIELD(BQ24257_REG_2, 1, 1),
0138     [F_HZ_MODE]     = REG_FIELD(BQ24257_REG_2, 0, 0),
0139     /* REG 3 */
0140     [F_VBAT]        = REG_FIELD(BQ24257_REG_3, 2, 7),
0141     [F_USB_DET]     = REG_FIELD(BQ24257_REG_3, 0, 1),
0142     /* REG 4 */
0143     [F_ICHG]        = REG_FIELD(BQ24257_REG_4, 3, 7),
0144     [F_ITERM]       = REG_FIELD(BQ24257_REG_4, 0, 2),
0145     /* REG 5 */
0146     [F_LOOP_STATUS]     = REG_FIELD(BQ24257_REG_5, 6, 7),
0147     [F_LOW_CHG]     = REG_FIELD(BQ24257_REG_5, 5, 5),
0148     [F_DPDM_EN]     = REG_FIELD(BQ24257_REG_5, 4, 4),
0149     [F_CE_STATUS]       = REG_FIELD(BQ24257_REG_5, 3, 3),
0150     [F_VINDPM]      = REG_FIELD(BQ24257_REG_5, 0, 2),
0151     /* REG 6 */
0152     [F_X2_TMR_EN]       = REG_FIELD(BQ24257_REG_6, 7, 7),
0153     [F_TMR]         = REG_FIELD(BQ24257_REG_6, 5, 6),
0154     [F_SYSOFF]      = REG_FIELD(BQ24257_REG_6, 4, 4),
0155     [F_TS_EN]       = REG_FIELD(BQ24257_REG_6, 3, 3),
0156     [F_TS_STAT]     = REG_FIELD(BQ24257_REG_6, 0, 2),
0157     /* REG 7 */
0158     [F_VOVP]        = REG_FIELD(BQ24257_REG_7, 5, 7),
0159     [F_CLR_VDP]     = REG_FIELD(BQ24257_REG_7, 4, 4),
0160     [F_FORCE_BATDET]    = REG_FIELD(BQ24257_REG_7, 3, 3),
0161     [F_FORCE_PTM]       = REG_FIELD(BQ24257_REG_7, 2, 2)
0162 };
0163 
0164 static const u32 bq24257_vbat_map[] = {
0165     3500000, 3520000, 3540000, 3560000, 3580000, 3600000, 3620000, 3640000,
0166     3660000, 3680000, 3700000, 3720000, 3740000, 3760000, 3780000, 3800000,
0167     3820000, 3840000, 3860000, 3880000, 3900000, 3920000, 3940000, 3960000,
0168     3980000, 4000000, 4020000, 4040000, 4060000, 4080000, 4100000, 4120000,
0169     4140000, 4160000, 4180000, 4200000, 4220000, 4240000, 4260000, 4280000,
0170     4300000, 4320000, 4340000, 4360000, 4380000, 4400000, 4420000, 4440000
0171 };
0172 
0173 #define BQ24257_VBAT_MAP_SIZE       ARRAY_SIZE(bq24257_vbat_map)
0174 
0175 static const u32 bq24257_ichg_map[] = {
0176     500000, 550000, 600000, 650000, 700000, 750000, 800000, 850000, 900000,
0177     950000, 1000000, 1050000, 1100000, 1150000, 1200000, 1250000, 1300000,
0178     1350000, 1400000, 1450000, 1500000, 1550000, 1600000, 1650000, 1700000,
0179     1750000, 1800000, 1850000, 1900000, 1950000, 2000000
0180 };
0181 
0182 #define BQ24257_ICHG_MAP_SIZE       ARRAY_SIZE(bq24257_ichg_map)
0183 
0184 static const u32 bq24257_iterm_map[] = {
0185     50000, 75000, 100000, 125000, 150000, 175000, 200000, 225000
0186 };
0187 
0188 #define BQ24257_ITERM_MAP_SIZE      ARRAY_SIZE(bq24257_iterm_map)
0189 
0190 static const u32 bq24257_iilimit_map[] = {
0191     100000, 150000, 500000, 900000, 1500000, 2000000
0192 };
0193 
0194 #define BQ24257_IILIMIT_MAP_SIZE    ARRAY_SIZE(bq24257_iilimit_map)
0195 
0196 static const u32 bq24257_vovp_map[] = {
0197     6000000, 6500000, 7000000, 8000000, 9000000, 9500000, 10000000,
0198     10500000
0199 };
0200 
0201 #define BQ24257_VOVP_MAP_SIZE       ARRAY_SIZE(bq24257_vovp_map)
0202 
0203 static const u32 bq24257_vindpm_map[] = {
0204     4200000, 4280000, 4360000, 4440000, 4520000, 4600000, 4680000,
0205     4760000
0206 };
0207 
0208 #define BQ24257_VINDPM_MAP_SIZE     ARRAY_SIZE(bq24257_vindpm_map)
0209 
0210 static int bq24257_field_read(struct bq24257_device *bq,
0211                   enum bq24257_fields field_id)
0212 {
0213     int ret;
0214     int val;
0215 
0216     ret = regmap_field_read(bq->rmap_fields[field_id], &val);
0217     if (ret < 0)
0218         return ret;
0219 
0220     return val;
0221 }
0222 
0223 static int bq24257_field_write(struct bq24257_device *bq,
0224                    enum bq24257_fields field_id, u8 val)
0225 {
0226     return regmap_field_write(bq->rmap_fields[field_id], val);
0227 }
0228 
0229 static u8 bq24257_find_idx(u32 value, const u32 *map, u8 map_size)
0230 {
0231     u8 idx;
0232 
0233     for (idx = 1; idx < map_size; idx++)
0234         if (value < map[idx])
0235             break;
0236 
0237     return idx - 1;
0238 }
0239 
0240 enum bq24257_status {
0241     STATUS_READY,
0242     STATUS_CHARGE_IN_PROGRESS,
0243     STATUS_CHARGE_DONE,
0244     STATUS_FAULT,
0245 };
0246 
0247 enum bq24257_fault {
0248     FAULT_NORMAL,
0249     FAULT_INPUT_OVP,
0250     FAULT_INPUT_UVLO,
0251     FAULT_SLEEP,
0252     FAULT_BAT_TS,
0253     FAULT_BAT_OVP,
0254     FAULT_TS,
0255     FAULT_TIMER,
0256     FAULT_NO_BAT,
0257     FAULT_ISET,
0258     FAULT_INPUT_LDO_LOW,
0259 };
0260 
0261 static int bq24257_get_input_current_limit(struct bq24257_device *bq,
0262                        union power_supply_propval *val)
0263 {
0264     int ret;
0265 
0266     ret = bq24257_field_read(bq, F_IILIMIT);
0267     if (ret < 0)
0268         return ret;
0269 
0270     /*
0271      * The "External ILIM" and "Production & Test" modes are not exposed
0272      * through this driver and not being covered by the lookup table.
0273      * Should such a mode have become active let's return an error rather
0274      * than exceeding the bounds of the lookup table and returning
0275      * garbage.
0276      */
0277     if (ret >= BQ24257_IILIMIT_MAP_SIZE)
0278         return -ENODATA;
0279 
0280     val->intval = bq24257_iilimit_map[ret];
0281 
0282     return 0;
0283 }
0284 
0285 static int bq24257_set_input_current_limit(struct bq24257_device *bq,
0286                     const union power_supply_propval *val)
0287 {
0288     /*
0289      * Address the case where the user manually sets an input current limit
0290      * while the charger auto-detection mechanism is active. In this
0291      * case we want to abort and go straight to the user-specified value.
0292      */
0293     if (bq->iilimit_autoset_enable)
0294         cancel_delayed_work_sync(&bq->iilimit_setup_work);
0295 
0296     return bq24257_field_write(bq, F_IILIMIT,
0297                    bq24257_find_idx(val->intval,
0298                             bq24257_iilimit_map,
0299                             BQ24257_IILIMIT_MAP_SIZE));
0300 }
0301 
0302 static int bq24257_power_supply_get_property(struct power_supply *psy,
0303                          enum power_supply_property psp,
0304                          union power_supply_propval *val)
0305 {
0306     struct bq24257_device *bq = power_supply_get_drvdata(psy);
0307     struct bq24257_state state;
0308 
0309     mutex_lock(&bq->lock);
0310     state = bq->state;
0311     mutex_unlock(&bq->lock);
0312 
0313     switch (psp) {
0314     case POWER_SUPPLY_PROP_STATUS:
0315         if (!state.power_good)
0316             val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
0317         else if (state.status == STATUS_READY)
0318             val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
0319         else if (state.status == STATUS_CHARGE_IN_PROGRESS)
0320             val->intval = POWER_SUPPLY_STATUS_CHARGING;
0321         else if (state.status == STATUS_CHARGE_DONE)
0322             val->intval = POWER_SUPPLY_STATUS_FULL;
0323         else
0324             val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
0325         break;
0326 
0327     case POWER_SUPPLY_PROP_MANUFACTURER:
0328         val->strval = BQ24257_MANUFACTURER;
0329         break;
0330 
0331     case POWER_SUPPLY_PROP_MODEL_NAME:
0332         val->strval = bq2425x_chip_name[bq->chip];
0333         break;
0334 
0335     case POWER_SUPPLY_PROP_ONLINE:
0336         val->intval = state.power_good;
0337         break;
0338 
0339     case POWER_SUPPLY_PROP_HEALTH:
0340         switch (state.fault) {
0341         case FAULT_NORMAL:
0342             val->intval = POWER_SUPPLY_HEALTH_GOOD;
0343             break;
0344 
0345         case FAULT_INPUT_OVP:
0346         case FAULT_BAT_OVP:
0347             val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
0348             break;
0349 
0350         case FAULT_TS:
0351         case FAULT_BAT_TS:
0352             val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
0353             break;
0354 
0355         case FAULT_TIMER:
0356             val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
0357             break;
0358 
0359         default:
0360             val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
0361             break;
0362         }
0363 
0364         break;
0365 
0366     case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
0367         val->intval = bq24257_ichg_map[bq->init_data.ichg];
0368         break;
0369 
0370     case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
0371         val->intval = bq24257_ichg_map[BQ24257_ICHG_MAP_SIZE - 1];
0372         break;
0373 
0374     case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
0375         val->intval = bq24257_vbat_map[bq->init_data.vbat];
0376         break;
0377 
0378     case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
0379         val->intval = bq24257_vbat_map[BQ24257_VBAT_MAP_SIZE - 1];
0380         break;
0381 
0382     case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
0383         val->intval = bq24257_iterm_map[bq->init_data.iterm];
0384         break;
0385 
0386     case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
0387         return bq24257_get_input_current_limit(bq, val);
0388 
0389     default:
0390         return -EINVAL;
0391     }
0392 
0393     return 0;
0394 }
0395 
0396 static int bq24257_power_supply_set_property(struct power_supply *psy,
0397                     enum power_supply_property prop,
0398                     const union power_supply_propval *val)
0399 {
0400     struct bq24257_device *bq = power_supply_get_drvdata(psy);
0401 
0402     switch (prop) {
0403     case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
0404         return bq24257_set_input_current_limit(bq, val);
0405     default:
0406         return -EINVAL;
0407     }
0408 }
0409 
0410 static int bq24257_power_supply_property_is_writeable(struct power_supply *psy,
0411                     enum power_supply_property psp)
0412 {
0413     switch (psp) {
0414     case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
0415         return true;
0416     default:
0417         return false;
0418     }
0419 }
0420 
0421 static int bq24257_get_chip_state(struct bq24257_device *bq,
0422                   struct bq24257_state *state)
0423 {
0424     int ret;
0425 
0426     ret = bq24257_field_read(bq, F_STAT);
0427     if (ret < 0)
0428         return ret;
0429 
0430     state->status = ret;
0431 
0432     ret = bq24257_field_read(bq, F_FAULT);
0433     if (ret < 0)
0434         return ret;
0435 
0436     state->fault = ret;
0437 
0438     if (bq->pg)
0439         state->power_good = !gpiod_get_value_cansleep(bq->pg);
0440     else
0441         /*
0442          * If we have a chip without a dedicated power-good GPIO or
0443          * some other explicit bit that would provide this information
0444          * assume the power is good if there is no supply related
0445          * fault - and not good otherwise. There is a possibility for
0446          * other errors to mask that power in fact is not good but this
0447          * is probably the best we can do here.
0448          */
0449         switch (state->fault) {
0450         case FAULT_INPUT_OVP:
0451         case FAULT_INPUT_UVLO:
0452         case FAULT_INPUT_LDO_LOW:
0453             state->power_good = false;
0454             break;
0455         default:
0456             state->power_good = true;
0457         }
0458 
0459     return 0;
0460 }
0461 
0462 static bool bq24257_state_changed(struct bq24257_device *bq,
0463                   struct bq24257_state *new_state)
0464 {
0465     int ret;
0466 
0467     mutex_lock(&bq->lock);
0468     ret = (bq->state.status != new_state->status ||
0469            bq->state.fault != new_state->fault ||
0470            bq->state.power_good != new_state->power_good);
0471     mutex_unlock(&bq->lock);
0472 
0473     return ret;
0474 }
0475 
0476 enum bq24257_loop_status {
0477     LOOP_STATUS_NONE,
0478     LOOP_STATUS_IN_DPM,
0479     LOOP_STATUS_IN_CURRENT_LIMIT,
0480     LOOP_STATUS_THERMAL,
0481 };
0482 
0483 enum bq24257_in_ilimit {
0484     IILIMIT_100,
0485     IILIMIT_150,
0486     IILIMIT_500,
0487     IILIMIT_900,
0488     IILIMIT_1500,
0489     IILIMIT_2000,
0490     IILIMIT_EXT,
0491     IILIMIT_NONE,
0492 };
0493 
0494 enum bq24257_vovp {
0495     VOVP_6000,
0496     VOVP_6500,
0497     VOVP_7000,
0498     VOVP_8000,
0499     VOVP_9000,
0500     VOVP_9500,
0501     VOVP_10000,
0502     VOVP_10500
0503 };
0504 
0505 enum bq24257_vindpm {
0506     VINDPM_4200,
0507     VINDPM_4280,
0508     VINDPM_4360,
0509     VINDPM_4440,
0510     VINDPM_4520,
0511     VINDPM_4600,
0512     VINDPM_4680,
0513     VINDPM_4760
0514 };
0515 
0516 enum bq24257_port_type {
0517     PORT_TYPE_DCP,      /* Dedicated Charging Port */
0518     PORT_TYPE_CDP,      /* Charging Downstream Port */
0519     PORT_TYPE_SDP,      /* Standard Downstream Port */
0520     PORT_TYPE_NON_STANDARD,
0521 };
0522 
0523 enum bq24257_safety_timer {
0524     SAFETY_TIMER_45,
0525     SAFETY_TIMER_360,
0526     SAFETY_TIMER_540,
0527     SAFETY_TIMER_NONE,
0528 };
0529 
0530 static int bq24257_iilimit_autoset(struct bq24257_device *bq)
0531 {
0532     int loop_status;
0533     int iilimit;
0534     int port_type;
0535     int ret;
0536     const u8 new_iilimit[] = {
0537         [PORT_TYPE_DCP] = IILIMIT_2000,
0538         [PORT_TYPE_CDP] = IILIMIT_2000,
0539         [PORT_TYPE_SDP] = IILIMIT_500,
0540         [PORT_TYPE_NON_STANDARD] = IILIMIT_500
0541     };
0542 
0543     ret = bq24257_field_read(bq, F_LOOP_STATUS);
0544     if (ret < 0)
0545         goto error;
0546 
0547     loop_status = ret;
0548 
0549     ret = bq24257_field_read(bq, F_IILIMIT);
0550     if (ret < 0)
0551         goto error;
0552 
0553     iilimit = ret;
0554 
0555     /*
0556      * All USB ports should be able to handle 500mA. If not, DPM will lower
0557      * the charging current to accommodate the power source. No need to set
0558      * a lower IILIMIT value.
0559      */
0560     if (loop_status == LOOP_STATUS_IN_DPM && iilimit == IILIMIT_500)
0561         return 0;
0562 
0563     ret = bq24257_field_read(bq, F_USB_DET);
0564     if (ret < 0)
0565         goto error;
0566 
0567     port_type = ret;
0568 
0569     ret = bq24257_field_write(bq, F_IILIMIT, new_iilimit[port_type]);
0570     if (ret < 0)
0571         goto error;
0572 
0573     ret = bq24257_field_write(bq, F_TMR, SAFETY_TIMER_360);
0574     if (ret < 0)
0575         goto error;
0576 
0577     ret = bq24257_field_write(bq, F_CLR_VDP, 1);
0578     if (ret < 0)
0579         goto error;
0580 
0581     dev_dbg(bq->dev, "port/loop = %d/%d -> iilimit = %d\n",
0582         port_type, loop_status, new_iilimit[port_type]);
0583 
0584     return 0;
0585 
0586 error:
0587     dev_err(bq->dev, "%s: Error communicating with the chip.\n", __func__);
0588     return ret;
0589 }
0590 
0591 static void bq24257_iilimit_setup_work(struct work_struct *work)
0592 {
0593     struct bq24257_device *bq = container_of(work, struct bq24257_device,
0594                          iilimit_setup_work.work);
0595 
0596     bq24257_iilimit_autoset(bq);
0597 }
0598 
0599 static void bq24257_handle_state_change(struct bq24257_device *bq,
0600                     struct bq24257_state *new_state)
0601 {
0602     int ret;
0603     struct bq24257_state old_state;
0604 
0605     mutex_lock(&bq->lock);
0606     old_state = bq->state;
0607     mutex_unlock(&bq->lock);
0608 
0609     /*
0610      * Handle BQ2425x state changes observing whether the D+/D- based input
0611      * current limit autoset functionality is enabled.
0612      */
0613     if (!new_state->power_good) {
0614         dev_dbg(bq->dev, "Power removed\n");
0615         if (bq->iilimit_autoset_enable) {
0616             cancel_delayed_work_sync(&bq->iilimit_setup_work);
0617 
0618             /* activate D+/D- port detection algorithm */
0619             ret = bq24257_field_write(bq, F_DPDM_EN, 1);
0620             if (ret < 0)
0621                 goto error;
0622         }
0623         /*
0624          * When power is removed always return to the default input
0625          * current limit as configured during probe.
0626          */
0627         ret = bq24257_field_write(bq, F_IILIMIT, bq->init_data.iilimit);
0628         if (ret < 0)
0629             goto error;
0630     } else if (!old_state.power_good) {
0631         dev_dbg(bq->dev, "Power inserted\n");
0632 
0633         if (bq->iilimit_autoset_enable)
0634             /* configure input current limit */
0635             schedule_delayed_work(&bq->iilimit_setup_work,
0636                       msecs_to_jiffies(BQ24257_ILIM_SET_DELAY));
0637     } else if (new_state->fault == FAULT_NO_BAT) {
0638         dev_warn(bq->dev, "Battery removed\n");
0639     } else if (new_state->fault == FAULT_TIMER) {
0640         dev_err(bq->dev, "Safety timer expired! Battery dead?\n");
0641     }
0642 
0643     return;
0644 
0645 error:
0646     dev_err(bq->dev, "%s: Error communicating with the chip.\n", __func__);
0647 }
0648 
0649 static irqreturn_t bq24257_irq_handler_thread(int irq, void *private)
0650 {
0651     int ret;
0652     struct bq24257_device *bq = private;
0653     struct bq24257_state state;
0654 
0655     ret = bq24257_get_chip_state(bq, &state);
0656     if (ret < 0)
0657         return IRQ_HANDLED;
0658 
0659     if (!bq24257_state_changed(bq, &state))
0660         return IRQ_HANDLED;
0661 
0662     dev_dbg(bq->dev, "irq(state changed): status/fault/pg = %d/%d/%d\n",
0663         state.status, state.fault, state.power_good);
0664 
0665     bq24257_handle_state_change(bq, &state);
0666 
0667     mutex_lock(&bq->lock);
0668     bq->state = state;
0669     mutex_unlock(&bq->lock);
0670 
0671     power_supply_changed(bq->charger);
0672 
0673     return IRQ_HANDLED;
0674 }
0675 
0676 static int bq24257_hw_init(struct bq24257_device *bq)
0677 {
0678     int ret;
0679     int i;
0680     struct bq24257_state state;
0681 
0682     const struct {
0683         int field;
0684         u32 value;
0685     } init_data[] = {
0686         {F_ICHG, bq->init_data.ichg},
0687         {F_VBAT, bq->init_data.vbat},
0688         {F_ITERM, bq->init_data.iterm},
0689         {F_VOVP, bq->init_data.vovp},
0690         {F_VINDPM, bq->init_data.vindpm},
0691     };
0692 
0693     /*
0694      * Disable the watchdog timer to prevent the IC from going back to
0695      * default settings after 50 seconds of I2C inactivity.
0696      */
0697     ret = bq24257_field_write(bq, F_WD_EN, 0);
0698     if (ret < 0)
0699         return ret;
0700 
0701     /* configure the charge currents and voltages */
0702     for (i = 0; i < ARRAY_SIZE(init_data); i++) {
0703         ret = bq24257_field_write(bq, init_data[i].field,
0704                       init_data[i].value);
0705         if (ret < 0)
0706             return ret;
0707     }
0708 
0709     ret = bq24257_get_chip_state(bq, &state);
0710     if (ret < 0)
0711         return ret;
0712 
0713     mutex_lock(&bq->lock);
0714     bq->state = state;
0715     mutex_unlock(&bq->lock);
0716 
0717     if (!bq->iilimit_autoset_enable) {
0718         dev_dbg(bq->dev, "manually setting iilimit = %u\n",
0719             bq->init_data.iilimit);
0720 
0721         /* program fixed input current limit */
0722         ret = bq24257_field_write(bq, F_IILIMIT,
0723                       bq->init_data.iilimit);
0724         if (ret < 0)
0725             return ret;
0726     } else if (!state.power_good)
0727         /* activate D+/D- detection algorithm */
0728         ret = bq24257_field_write(bq, F_DPDM_EN, 1);
0729     else if (state.fault != FAULT_NO_BAT)
0730         ret = bq24257_iilimit_autoset(bq);
0731 
0732     return ret;
0733 }
0734 
0735 static enum power_supply_property bq24257_power_supply_props[] = {
0736     POWER_SUPPLY_PROP_MANUFACTURER,
0737     POWER_SUPPLY_PROP_MODEL_NAME,
0738     POWER_SUPPLY_PROP_STATUS,
0739     POWER_SUPPLY_PROP_ONLINE,
0740     POWER_SUPPLY_PROP_HEALTH,
0741     POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
0742     POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
0743     POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
0744     POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
0745     POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
0746     POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
0747 };
0748 
0749 static char *bq24257_charger_supplied_to[] = {
0750     "main-battery",
0751 };
0752 
0753 static const struct power_supply_desc bq24257_power_supply_desc = {
0754     .name = "bq24257-charger",
0755     .type = POWER_SUPPLY_TYPE_USB,
0756     .properties = bq24257_power_supply_props,
0757     .num_properties = ARRAY_SIZE(bq24257_power_supply_props),
0758     .get_property = bq24257_power_supply_get_property,
0759     .set_property = bq24257_power_supply_set_property,
0760     .property_is_writeable = bq24257_power_supply_property_is_writeable,
0761 };
0762 
0763 static ssize_t bq24257_show_ovp_voltage(struct device *dev,
0764                     struct device_attribute *attr,
0765                     char *buf)
0766 {
0767     struct power_supply *psy = dev_get_drvdata(dev);
0768     struct bq24257_device *bq = power_supply_get_drvdata(psy);
0769 
0770     return scnprintf(buf, PAGE_SIZE, "%u\n",
0771              bq24257_vovp_map[bq->init_data.vovp]);
0772 }
0773 
0774 static ssize_t bq24257_show_in_dpm_voltage(struct device *dev,
0775                        struct device_attribute *attr,
0776                        char *buf)
0777 {
0778     struct power_supply *psy = dev_get_drvdata(dev);
0779     struct bq24257_device *bq = power_supply_get_drvdata(psy);
0780 
0781     return scnprintf(buf, PAGE_SIZE, "%u\n",
0782              bq24257_vindpm_map[bq->init_data.vindpm]);
0783 }
0784 
0785 static ssize_t bq24257_sysfs_show_enable(struct device *dev,
0786                      struct device_attribute *attr,
0787                      char *buf)
0788 {
0789     struct power_supply *psy = dev_get_drvdata(dev);
0790     struct bq24257_device *bq = power_supply_get_drvdata(psy);
0791     int ret;
0792 
0793     if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
0794         ret = bq24257_field_read(bq, F_HZ_MODE);
0795     else if (strcmp(attr->attr.name, "sysoff_enable") == 0)
0796         ret = bq24257_field_read(bq, F_SYSOFF);
0797     else
0798         return -EINVAL;
0799 
0800     if (ret < 0)
0801         return ret;
0802 
0803     return scnprintf(buf, PAGE_SIZE, "%d\n", ret);
0804 }
0805 
0806 static ssize_t bq24257_sysfs_set_enable(struct device *dev,
0807                     struct device_attribute *attr,
0808                     const char *buf,
0809                     size_t count)
0810 {
0811     struct power_supply *psy = dev_get_drvdata(dev);
0812     struct bq24257_device *bq = power_supply_get_drvdata(psy);
0813     long val;
0814     int ret;
0815 
0816     if (kstrtol(buf, 10, &val) < 0)
0817         return -EINVAL;
0818 
0819     if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
0820         ret = bq24257_field_write(bq, F_HZ_MODE, (bool)val);
0821     else if (strcmp(attr->attr.name, "sysoff_enable") == 0)
0822         ret = bq24257_field_write(bq, F_SYSOFF, (bool)val);
0823     else
0824         return -EINVAL;
0825 
0826     if (ret < 0)
0827         return ret;
0828 
0829     return count;
0830 }
0831 
0832 static DEVICE_ATTR(ovp_voltage, S_IRUGO, bq24257_show_ovp_voltage, NULL);
0833 static DEVICE_ATTR(in_dpm_voltage, S_IRUGO, bq24257_show_in_dpm_voltage, NULL);
0834 static DEVICE_ATTR(high_impedance_enable, S_IWUSR | S_IRUGO,
0835            bq24257_sysfs_show_enable, bq24257_sysfs_set_enable);
0836 static DEVICE_ATTR(sysoff_enable, S_IWUSR | S_IRUGO,
0837            bq24257_sysfs_show_enable, bq24257_sysfs_set_enable);
0838 
0839 static struct attribute *bq24257_charger_sysfs_attrs[] = {
0840     &dev_attr_ovp_voltage.attr,
0841     &dev_attr_in_dpm_voltage.attr,
0842     &dev_attr_high_impedance_enable.attr,
0843     &dev_attr_sysoff_enable.attr,
0844     NULL,
0845 };
0846 
0847 ATTRIBUTE_GROUPS(bq24257_charger_sysfs);
0848 
0849 static int bq24257_power_supply_init(struct bq24257_device *bq)
0850 {
0851     struct power_supply_config psy_cfg = { .drv_data = bq, };
0852 
0853     psy_cfg.attr_grp = bq24257_charger_sysfs_groups;
0854     psy_cfg.supplied_to = bq24257_charger_supplied_to;
0855     psy_cfg.num_supplicants = ARRAY_SIZE(bq24257_charger_supplied_to);
0856 
0857     bq->charger = devm_power_supply_register(bq->dev,
0858                          &bq24257_power_supply_desc,
0859                          &psy_cfg);
0860 
0861     return PTR_ERR_OR_ZERO(bq->charger);
0862 }
0863 
0864 static void bq24257_pg_gpio_probe(struct bq24257_device *bq)
0865 {
0866     bq->pg = devm_gpiod_get_optional(bq->dev, BQ24257_PG_GPIO, GPIOD_IN);
0867 
0868     if (PTR_ERR(bq->pg) == -EPROBE_DEFER) {
0869         dev_info(bq->dev, "probe retry requested for PG pin\n");
0870         return;
0871     } else if (IS_ERR(bq->pg)) {
0872         dev_err(bq->dev, "error probing PG pin\n");
0873         bq->pg = NULL;
0874         return;
0875     }
0876 
0877     if (bq->pg)
0878         dev_dbg(bq->dev, "probed PG pin = %d\n", desc_to_gpio(bq->pg));
0879 }
0880 
0881 static int bq24257_fw_probe(struct bq24257_device *bq)
0882 {
0883     int ret;
0884     u32 property;
0885 
0886     /* Required properties */
0887     ret = device_property_read_u32(bq->dev, "ti,charge-current", &property);
0888     if (ret < 0)
0889         return ret;
0890 
0891     bq->init_data.ichg = bq24257_find_idx(property, bq24257_ichg_map,
0892                           BQ24257_ICHG_MAP_SIZE);
0893 
0894     ret = device_property_read_u32(bq->dev, "ti,battery-regulation-voltage",
0895                        &property);
0896     if (ret < 0)
0897         return ret;
0898 
0899     bq->init_data.vbat = bq24257_find_idx(property, bq24257_vbat_map,
0900                           BQ24257_VBAT_MAP_SIZE);
0901 
0902     ret = device_property_read_u32(bq->dev, "ti,termination-current",
0903                        &property);
0904     if (ret < 0)
0905         return ret;
0906 
0907     bq->init_data.iterm = bq24257_find_idx(property, bq24257_iterm_map,
0908                            BQ24257_ITERM_MAP_SIZE);
0909 
0910     /* Optional properties. If not provided use reasonable default. */
0911     ret = device_property_read_u32(bq->dev, "ti,current-limit",
0912                        &property);
0913     if (ret < 0) {
0914         bq->iilimit_autoset_enable = true;
0915 
0916         /*
0917          * Explicitly set a default value which will be needed for
0918          * devices that don't support the automatic setting of the input
0919          * current limit through the charger type detection mechanism.
0920          */
0921         bq->init_data.iilimit = IILIMIT_500;
0922     } else
0923         bq->init_data.iilimit =
0924                 bq24257_find_idx(property,
0925                          bq24257_iilimit_map,
0926                          BQ24257_IILIMIT_MAP_SIZE);
0927 
0928     ret = device_property_read_u32(bq->dev, "ti,ovp-voltage",
0929                        &property);
0930     if (ret < 0)
0931         bq->init_data.vovp = VOVP_6500;
0932     else
0933         bq->init_data.vovp = bq24257_find_idx(property,
0934                               bq24257_vovp_map,
0935                               BQ24257_VOVP_MAP_SIZE);
0936 
0937     ret = device_property_read_u32(bq->dev, "ti,in-dpm-voltage",
0938                        &property);
0939     if (ret < 0)
0940         bq->init_data.vindpm = VINDPM_4360;
0941     else
0942         bq->init_data.vindpm =
0943                 bq24257_find_idx(property,
0944                          bq24257_vindpm_map,
0945                          BQ24257_VINDPM_MAP_SIZE);
0946 
0947     return 0;
0948 }
0949 
0950 static int bq24257_probe(struct i2c_client *client,
0951              const struct i2c_device_id *id)
0952 {
0953     struct i2c_adapter *adapter = client->adapter;
0954     struct device *dev = &client->dev;
0955     const struct acpi_device_id *acpi_id;
0956     struct bq24257_device *bq;
0957     int ret;
0958     int i;
0959 
0960     if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
0961         dev_err(dev, "No support for SMBUS_BYTE_DATA\n");
0962         return -ENODEV;
0963     }
0964 
0965     bq = devm_kzalloc(dev, sizeof(*bq), GFP_KERNEL);
0966     if (!bq)
0967         return -ENOMEM;
0968 
0969     bq->client = client;
0970     bq->dev = dev;
0971 
0972     if (ACPI_HANDLE(dev)) {
0973         acpi_id = acpi_match_device(dev->driver->acpi_match_table,
0974                         &client->dev);
0975         if (!acpi_id) {
0976             dev_err(dev, "Failed to match ACPI device\n");
0977             return -ENODEV;
0978         }
0979         bq->chip = (enum bq2425x_chip)acpi_id->driver_data;
0980     } else {
0981         bq->chip = (enum bq2425x_chip)id->driver_data;
0982     }
0983 
0984     mutex_init(&bq->lock);
0985 
0986     bq->rmap = devm_regmap_init_i2c(client, &bq24257_regmap_config);
0987     if (IS_ERR(bq->rmap)) {
0988         dev_err(dev, "failed to allocate register map\n");
0989         return PTR_ERR(bq->rmap);
0990     }
0991 
0992     for (i = 0; i < ARRAY_SIZE(bq24257_reg_fields); i++) {
0993         const struct reg_field *reg_fields = bq24257_reg_fields;
0994 
0995         bq->rmap_fields[i] = devm_regmap_field_alloc(dev, bq->rmap,
0996                                  reg_fields[i]);
0997         if (IS_ERR(bq->rmap_fields[i])) {
0998             dev_err(dev, "cannot allocate regmap field\n");
0999             return PTR_ERR(bq->rmap_fields[i]);
1000         }
1001     }
1002 
1003     i2c_set_clientdata(client, bq);
1004 
1005     if (!dev->platform_data) {
1006         ret = bq24257_fw_probe(bq);
1007         if (ret < 0) {
1008             dev_err(dev, "Cannot read device properties.\n");
1009             return ret;
1010         }
1011     } else {
1012         return -ENODEV;
1013     }
1014 
1015     /*
1016      * The BQ24250 doesn't support the D+/D- based charger type detection
1017      * used for the automatic setting of the input current limit setting so
1018      * explicitly disable that feature.
1019      */
1020     if (bq->chip == BQ24250)
1021         bq->iilimit_autoset_enable = false;
1022 
1023     if (bq->iilimit_autoset_enable)
1024         INIT_DELAYED_WORK(&bq->iilimit_setup_work,
1025                   bq24257_iilimit_setup_work);
1026 
1027     /*
1028      * The BQ24250 doesn't have a dedicated Power Good (PG) pin so let's
1029      * not probe for it and instead use a SW-based approach to determine
1030      * the PG state. We also use a SW-based approach for all other devices
1031      * if the PG pin is either not defined or can't be probed.
1032      */
1033     if (bq->chip != BQ24250)
1034         bq24257_pg_gpio_probe(bq);
1035 
1036     if (PTR_ERR(bq->pg) == -EPROBE_DEFER)
1037         return PTR_ERR(bq->pg);
1038     else if (!bq->pg)
1039         dev_info(bq->dev, "using SW-based power-good detection\n");
1040 
1041     /* reset all registers to defaults */
1042     ret = bq24257_field_write(bq, F_RESET, 1);
1043     if (ret < 0)
1044         return ret;
1045 
1046     /*
1047      * Put the RESET bit back to 0, in cache. For some reason the HW always
1048      * returns 1 on this bit, so this is the only way to avoid resetting the
1049      * chip every time we update another field in this register.
1050      */
1051     ret = bq24257_field_write(bq, F_RESET, 0);
1052     if (ret < 0)
1053         return ret;
1054 
1055     ret = bq24257_hw_init(bq);
1056     if (ret < 0) {
1057         dev_err(dev, "Cannot initialize the chip.\n");
1058         return ret;
1059     }
1060 
1061     ret = bq24257_power_supply_init(bq);
1062     if (ret < 0) {
1063         dev_err(dev, "Failed to register power supply\n");
1064         return ret;
1065     }
1066 
1067     ret = devm_request_threaded_irq(dev, client->irq, NULL,
1068                     bq24257_irq_handler_thread,
1069                     IRQF_TRIGGER_FALLING |
1070                     IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1071                     bq2425x_chip_name[bq->chip], bq);
1072     if (ret) {
1073         dev_err(dev, "Failed to request IRQ #%d\n", client->irq);
1074         return ret;
1075     }
1076 
1077     return 0;
1078 }
1079 
1080 static int bq24257_remove(struct i2c_client *client)
1081 {
1082     struct bq24257_device *bq = i2c_get_clientdata(client);
1083 
1084     if (bq->iilimit_autoset_enable)
1085         cancel_delayed_work_sync(&bq->iilimit_setup_work);
1086 
1087     bq24257_field_write(bq, F_RESET, 1); /* reset to defaults */
1088 
1089     return 0;
1090 }
1091 
1092 #ifdef CONFIG_PM_SLEEP
1093 static int bq24257_suspend(struct device *dev)
1094 {
1095     struct bq24257_device *bq = dev_get_drvdata(dev);
1096     int ret = 0;
1097 
1098     if (bq->iilimit_autoset_enable)
1099         cancel_delayed_work_sync(&bq->iilimit_setup_work);
1100 
1101     /* reset all registers to default (and activate standalone mode) */
1102     ret = bq24257_field_write(bq, F_RESET, 1);
1103     if (ret < 0)
1104         dev_err(bq->dev, "Cannot reset chip to standalone mode.\n");
1105 
1106     return ret;
1107 }
1108 
1109 static int bq24257_resume(struct device *dev)
1110 {
1111     int ret;
1112     struct bq24257_device *bq = dev_get_drvdata(dev);
1113 
1114     ret = regcache_drop_region(bq->rmap, BQ24257_REG_1, BQ24257_REG_7);
1115     if (ret < 0)
1116         return ret;
1117 
1118     ret = bq24257_field_write(bq, F_RESET, 0);
1119     if (ret < 0)
1120         return ret;
1121 
1122     ret = bq24257_hw_init(bq);
1123     if (ret < 0) {
1124         dev_err(bq->dev, "Cannot init chip after resume.\n");
1125         return ret;
1126     }
1127 
1128     /* signal userspace, maybe state changed while suspended */
1129     power_supply_changed(bq->charger);
1130 
1131     return 0;
1132 }
1133 #endif
1134 
1135 static const struct dev_pm_ops bq24257_pm = {
1136     SET_SYSTEM_SLEEP_PM_OPS(bq24257_suspend, bq24257_resume)
1137 };
1138 
1139 static const struct i2c_device_id bq24257_i2c_ids[] = {
1140     { "bq24250", BQ24250 },
1141     { "bq24251", BQ24251 },
1142     { "bq24257", BQ24257 },
1143     {},
1144 };
1145 MODULE_DEVICE_TABLE(i2c, bq24257_i2c_ids);
1146 
1147 static const struct of_device_id bq24257_of_match[] = {
1148     { .compatible = "ti,bq24250", },
1149     { .compatible = "ti,bq24251", },
1150     { .compatible = "ti,bq24257", },
1151     { },
1152 };
1153 MODULE_DEVICE_TABLE(of, bq24257_of_match);
1154 
1155 #ifdef CONFIG_ACPI
1156 static const struct acpi_device_id bq24257_acpi_match[] = {
1157     { "BQ242500", BQ24250 },
1158     { "BQ242510", BQ24251 },
1159     { "BQ242570", BQ24257 },
1160     {},
1161 };
1162 MODULE_DEVICE_TABLE(acpi, bq24257_acpi_match);
1163 #endif
1164 
1165 static struct i2c_driver bq24257_driver = {
1166     .driver = {
1167         .name = "bq24257-charger",
1168         .of_match_table = of_match_ptr(bq24257_of_match),
1169         .acpi_match_table = ACPI_PTR(bq24257_acpi_match),
1170         .pm = &bq24257_pm,
1171     },
1172     .probe = bq24257_probe,
1173     .remove = bq24257_remove,
1174     .id_table = bq24257_i2c_ids,
1175 };
1176 module_i2c_driver(bq24257_driver);
1177 
1178 MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>");
1179 MODULE_DESCRIPTION("bq24257 charger driver");
1180 MODULE_LICENSE("GPL");