Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * ROHM BD99954 charger driver
0004  *
0005  * Copyright (C) 2020 Rohm Semiconductors
0006  *  Originally written by:
0007  *      Mikko Mutanen <mikko.mutanen@fi.rohmeurope.com>
0008  *      Markus Laine <markus.laine@fi.rohmeurope.com>
0009  *  Bugs added by:
0010  *      Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>
0011  */
0012 
0013 /*
0014  *   The battery charging profile of BD99954.
0015  *
0016  *   Curve (1) represents charging current.
0017  *   Curve (2) represents battery voltage.
0018  *
0019  *   The BD99954 data sheet divides charging to three phases.
0020  *   a) Trickle-charge with constant current (8).
0021  *   b) pre-charge with constant current (6)
0022  *   c) fast-charge, first with constant current (5) phase. After
0023  *      the battery voltage has reached target level (4) we have constant
0024  *      voltage phase until charging current has dropped to termination
0025  *      level (7)
0026  *
0027  *    V ^                                                        ^ I
0028  *      .                                                        .
0029  *      .                                                        .
0030  *(4)` `.` ` ` ` ` ` ` ` ` ` ` ` ` ` ----------------------------.
0031  *      .                           :/                           .
0032  *      .                     o----+/:/ ` ` ` ` ` ` ` ` ` ` ` ` `.` ` (5)
0033  *      .                     +   ::  +                          .
0034  *      .                     +  /-   --                         .
0035  *      .                     +`/-     +                         .
0036  *      .                     o/-      -:                        .
0037  *      .                    .s.        +`                       .
0038  *      .                  .--+         `/                       .
0039  *      .               ..``  +          .:                      .
0040  *      .             -`      +           --                     .
0041  *      .    (2)  ...``       +            :-                    .
0042  *      .    ...``            +             -:                   .
0043  *(3)` `.`.""  ` ` ` `+-------- ` ` ` ` ` ` `.:` ` ` ` ` ` ` ` ` .` ` (6)
0044  *      .             +                       `:.                .
0045  *      .             +                         -:               .
0046  *      .             +                           -:.            .
0047  *      .             +                             .--.         .
0048  *      .   (1)       +                                `.+` ` ` `.` ` (7)
0049  *      -..............` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` + ` ` ` .` ` (8)
0050  *      .                                                +       -
0051  *      -------------------------------------------------+++++++++-->
0052  *      |   trickle   |  pre  |          fast            |
0053  *
0054  * Details of DT properties for different limits can be found from BD99954
0055  * device tree binding documentation.
0056  */
0057 
0058 #include <linux/delay.h>
0059 #include <linux/gpio/consumer.h>
0060 #include <linux/interrupt.h>
0061 #include <linux/i2c.h>
0062 #include <linux/kernel.h>
0063 #include <linux/linear_range.h>
0064 #include <linux/module.h>
0065 #include <linux/mod_devicetable.h>
0066 #include <linux/power_supply.h>
0067 #include <linux/property.h>
0068 #include <linux/regmap.h>
0069 #include <linux/types.h>
0070 
0071 #include "bd99954-charger.h"
0072 
0073 struct battery_data {
0074     u16 precharge_current;  /* Trickle-charge Current */
0075     u16 fc_reg_voltage; /* Fast Charging Regulation Voltage */
0076     u16 voltage_min;
0077     u16 voltage_max;
0078 };
0079 
0080 /* Initial field values, converted to initial register values */
0081 struct bd9995x_init_data {
0082     u16 vsysreg_set;    /* VSYS Regulation Setting */
0083     u16 ibus_lim_set;   /* VBUS input current limitation */
0084     u16 icc_lim_set;    /* VCC/VACP Input Current Limit Setting */
0085     u16 itrich_set;     /* Trickle-charge Current Setting */
0086     u16 iprech_set;     /* Pre-Charge Current Setting */
0087     u16 ichg_set;       /* Fast-Charge constant current */
0088     u16 vfastchg_reg_set1;  /* Fast Charging Regulation Voltage */
0089     u16 vprechg_th_set; /* Pre-charge Voltage Threshold Setting */
0090     u16 vrechg_set;     /* Re-charge Battery Voltage Setting */
0091     u16 vbatovp_set;    /* Battery Over Voltage Threshold Setting */
0092     u16 iterm_set;      /* Charging termination current */
0093 };
0094 
0095 struct bd9995x_state {
0096     u8 online;
0097     u16 chgstm_status;
0098     u16 vbat_vsys_status;
0099     u16 vbus_vcc_status;
0100 };
0101 
0102 struct bd9995x_device {
0103     struct i2c_client *client;
0104     struct device *dev;
0105     struct power_supply *charger;
0106 
0107     struct regmap *rmap;
0108     struct regmap_field *rmap_fields[F_MAX_FIELDS];
0109 
0110     int chip_id;
0111     int chip_rev;
0112     struct bd9995x_init_data init_data;
0113     struct bd9995x_state state;
0114 
0115     struct mutex lock; /* Protect state data */
0116 };
0117 
0118 static const struct regmap_range bd9995x_readonly_reg_ranges[] = {
0119     regmap_reg_range(CHGSTM_STATUS, SEL_ILIM_VAL),
0120     regmap_reg_range(IOUT_DACIN_VAL, IOUT_DACIN_VAL),
0121     regmap_reg_range(VCC_UCD_STATUS, VCC_IDD_STATUS),
0122     regmap_reg_range(VBUS_UCD_STATUS, VBUS_IDD_STATUS),
0123     regmap_reg_range(CHIP_ID, CHIP_REV),
0124     regmap_reg_range(SYSTEM_STATUS, SYSTEM_STATUS),
0125     regmap_reg_range(IBATP_VAL, VBAT_AVE_VAL),
0126     regmap_reg_range(VTH_VAL, EXTIADP_AVE_VAL),
0127 };
0128 
0129 static const struct regmap_access_table bd9995x_writeable_regs = {
0130     .no_ranges = bd9995x_readonly_reg_ranges,
0131     .n_no_ranges = ARRAY_SIZE(bd9995x_readonly_reg_ranges),
0132 };
0133 
0134 static const struct regmap_range bd9995x_volatile_reg_ranges[] = {
0135     regmap_reg_range(CHGSTM_STATUS, WDT_STATUS),
0136     regmap_reg_range(VCC_UCD_STATUS, VCC_IDD_STATUS),
0137     regmap_reg_range(VBUS_UCD_STATUS, VBUS_IDD_STATUS),
0138     regmap_reg_range(INT0_STATUS, INT7_STATUS),
0139     regmap_reg_range(SYSTEM_STATUS, SYSTEM_CTRL_SET),
0140     regmap_reg_range(IBATP_VAL, EXTIADP_AVE_VAL), /* Measurement regs */
0141 };
0142 
0143 static const struct regmap_access_table bd9995x_volatile_regs = {
0144     .yes_ranges = bd9995x_volatile_reg_ranges,
0145     .n_yes_ranges = ARRAY_SIZE(bd9995x_volatile_reg_ranges),
0146 };
0147 
0148 static const struct regmap_range_cfg regmap_range_cfg[] = {
0149     {
0150     .selector_reg     = MAP_SET,
0151     .selector_mask    = 0xFFFF,
0152     .selector_shift   = 0,
0153     .window_start     = 0,
0154     .window_len       = 0x100,
0155     .range_min        = 0 * 0x100,
0156     .range_max        = 3 * 0x100,
0157     },
0158 };
0159 
0160 static const struct regmap_config bd9995x_regmap_config = {
0161     .reg_bits = 8,
0162     .val_bits = 16,
0163     .reg_stride = 1,
0164 
0165     .max_register = 3 * 0x100,
0166     .cache_type = REGCACHE_RBTREE,
0167 
0168     .ranges = regmap_range_cfg,
0169     .num_ranges = ARRAY_SIZE(regmap_range_cfg),
0170     .val_format_endian = REGMAP_ENDIAN_LITTLE,
0171     .wr_table = &bd9995x_writeable_regs,
0172     .volatile_table = &bd9995x_volatile_regs,
0173 };
0174 
0175 enum bd9995x_chrg_fault {
0176     CHRG_FAULT_NORMAL,
0177     CHRG_FAULT_INPUT,
0178     CHRG_FAULT_THERMAL_SHUTDOWN,
0179     CHRG_FAULT_TIMER_EXPIRED,
0180 };
0181 
0182 static int bd9995x_get_prop_batt_health(struct bd9995x_device *bd)
0183 {
0184     int ret, tmp;
0185 
0186     ret = regmap_field_read(bd->rmap_fields[F_BATTEMP], &tmp);
0187     if (ret)
0188         return POWER_SUPPLY_HEALTH_UNKNOWN;
0189 
0190     /* TODO: Check these against datasheet page 34 */
0191 
0192     switch (tmp) {
0193     case ROOM:
0194         return POWER_SUPPLY_HEALTH_GOOD;
0195     case HOT1:
0196     case HOT2:
0197     case HOT3:
0198         return POWER_SUPPLY_HEALTH_OVERHEAT;
0199     case COLD1:
0200     case COLD2:
0201         return POWER_SUPPLY_HEALTH_COLD;
0202     case TEMP_DIS:
0203     case BATT_OPEN:
0204     default:
0205         return POWER_SUPPLY_HEALTH_UNKNOWN;
0206     }
0207 }
0208 
0209 static int bd9995x_get_prop_charge_type(struct bd9995x_device *bd)
0210 {
0211     int ret, tmp;
0212 
0213     ret = regmap_field_read(bd->rmap_fields[F_CHGSTM_STATE], &tmp);
0214     if (ret)
0215         return POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
0216 
0217     switch (tmp) {
0218     case CHGSTM_TRICKLE_CHARGE:
0219     case CHGSTM_PRE_CHARGE:
0220         return POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
0221     case CHGSTM_FAST_CHARGE:
0222         return POWER_SUPPLY_CHARGE_TYPE_FAST;
0223     case CHGSTM_TOP_OFF:
0224     case CHGSTM_DONE:
0225     case CHGSTM_SUSPEND:
0226         return POWER_SUPPLY_CHARGE_TYPE_NONE;
0227     default: /* Rest of the states are error related, no charging */
0228         return POWER_SUPPLY_CHARGE_TYPE_NONE;
0229     }
0230 }
0231 
0232 static bool bd9995x_get_prop_batt_present(struct bd9995x_device *bd)
0233 {
0234     int ret, tmp;
0235 
0236     ret = regmap_field_read(bd->rmap_fields[F_BATTEMP], &tmp);
0237     if (ret)
0238         return false;
0239 
0240     return tmp != BATT_OPEN;
0241 }
0242 
0243 static int bd9995x_get_prop_batt_voltage(struct bd9995x_device *bd)
0244 {
0245     int ret, tmp;
0246 
0247     ret = regmap_field_read(bd->rmap_fields[F_VBAT_VAL], &tmp);
0248     if (ret)
0249         return 0;
0250 
0251     tmp = min(tmp, 19200);
0252 
0253     return tmp * 1000;
0254 }
0255 
0256 static int bd9995x_get_prop_batt_current(struct bd9995x_device *bd)
0257 {
0258     int ret, tmp;
0259 
0260     ret = regmap_field_read(bd->rmap_fields[F_IBATP_VAL], &tmp);
0261     if (ret)
0262         return 0;
0263 
0264     return tmp * 1000;
0265 }
0266 
0267 #define DEFAULT_BATTERY_TEMPERATURE 250
0268 
0269 static int bd9995x_get_prop_batt_temp(struct bd9995x_device *bd)
0270 {
0271     int ret, tmp;
0272 
0273     ret = regmap_field_read(bd->rmap_fields[F_THERM_VAL], &tmp);
0274     if (ret)
0275         return DEFAULT_BATTERY_TEMPERATURE;
0276 
0277     return (200 - tmp) * 10;
0278 }
0279 
0280 static int bd9995x_power_supply_get_property(struct power_supply *psy,
0281                          enum power_supply_property psp,
0282                          union power_supply_propval *val)
0283 {
0284     int ret, tmp;
0285     struct bd9995x_device *bd = power_supply_get_drvdata(psy);
0286     struct bd9995x_state state;
0287 
0288     mutex_lock(&bd->lock);
0289     state = bd->state;
0290     mutex_unlock(&bd->lock);
0291 
0292     switch (psp) {
0293     case POWER_SUPPLY_PROP_STATUS:
0294         switch (state.chgstm_status) {
0295         case CHGSTM_TRICKLE_CHARGE:
0296         case CHGSTM_PRE_CHARGE:
0297         case CHGSTM_FAST_CHARGE:
0298         case CHGSTM_TOP_OFF:
0299             val->intval = POWER_SUPPLY_STATUS_CHARGING;
0300             break;
0301 
0302         case CHGSTM_DONE:
0303             val->intval = POWER_SUPPLY_STATUS_FULL;
0304             break;
0305 
0306         case CHGSTM_SUSPEND:
0307         case CHGSTM_TEMPERATURE_ERROR_1:
0308         case CHGSTM_TEMPERATURE_ERROR_2:
0309         case CHGSTM_TEMPERATURE_ERROR_3:
0310         case CHGSTM_TEMPERATURE_ERROR_4:
0311         case CHGSTM_TEMPERATURE_ERROR_5:
0312         case CHGSTM_TEMPERATURE_ERROR_6:
0313         case CHGSTM_TEMPERATURE_ERROR_7:
0314         case CHGSTM_THERMAL_SHUT_DOWN_1:
0315         case CHGSTM_THERMAL_SHUT_DOWN_2:
0316         case CHGSTM_THERMAL_SHUT_DOWN_3:
0317         case CHGSTM_THERMAL_SHUT_DOWN_4:
0318         case CHGSTM_THERMAL_SHUT_DOWN_5:
0319         case CHGSTM_THERMAL_SHUT_DOWN_6:
0320         case CHGSTM_THERMAL_SHUT_DOWN_7:
0321         case CHGSTM_BATTERY_ERROR:
0322             val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
0323             break;
0324 
0325         default:
0326             val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
0327             break;
0328         }
0329         break;
0330 
0331     case POWER_SUPPLY_PROP_MANUFACTURER:
0332         val->strval = BD9995X_MANUFACTURER;
0333         break;
0334 
0335     case POWER_SUPPLY_PROP_ONLINE:
0336         val->intval = state.online;
0337         break;
0338 
0339     case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
0340         ret = regmap_field_read(bd->rmap_fields[F_IBATP_VAL], &tmp);
0341         if (ret)
0342             return ret;
0343         val->intval = tmp * 1000;
0344         break;
0345 
0346     case POWER_SUPPLY_PROP_CHARGE_AVG:
0347         ret = regmap_field_read(bd->rmap_fields[F_IBATP_AVE_VAL], &tmp);
0348         if (ret)
0349             return ret;
0350         val->intval = tmp * 1000;
0351         break;
0352 
0353     case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
0354         /*
0355          * Currently the DT uses this property to give the
0356          * target current for fast-charging constant current phase.
0357          * I think it is correct in a sense.
0358          *
0359          * Yet, this prop we read and return here is the programmed
0360          * safety limit for combined input currents. This feels
0361          * also correct in a sense.
0362          *
0363          * However, this results a mismatch to DT value and value
0364          * read from sysfs.
0365          */
0366         ret = regmap_field_read(bd->rmap_fields[F_SEL_ILIM_VAL], &tmp);
0367         if (ret)
0368             return ret;
0369         val->intval = tmp * 1000;
0370         break;
0371 
0372     case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
0373         if (!state.online) {
0374             val->intval = 0;
0375             break;
0376         }
0377 
0378         ret = regmap_field_read(bd->rmap_fields[F_VFASTCHG_REG_SET1],
0379                     &tmp);
0380         if (ret)
0381             return ret;
0382 
0383         /*
0384          * The actual range : 2560 to 19200 mV. No matter what the
0385          * register says
0386          */
0387         val->intval = clamp_val(tmp << 4, 2560, 19200);
0388         val->intval *= 1000;
0389         break;
0390 
0391     case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
0392         ret = regmap_field_read(bd->rmap_fields[F_ITERM_SET], &tmp);
0393         if (ret)
0394             return ret;
0395         /* Start step is 64 mA */
0396         val->intval = tmp << 6;
0397         /* Maximum is 1024 mA - no matter what register says */
0398         val->intval = min(val->intval, 1024);
0399         val->intval *= 1000;
0400         break;
0401 
0402     /* Battery properties which we access through charger */
0403     case POWER_SUPPLY_PROP_PRESENT:
0404         val->intval = bd9995x_get_prop_batt_present(bd);
0405         break;
0406 
0407     case POWER_SUPPLY_PROP_VOLTAGE_NOW:
0408         val->intval = bd9995x_get_prop_batt_voltage(bd);
0409         break;
0410 
0411     case POWER_SUPPLY_PROP_CURRENT_NOW:
0412         val->intval = bd9995x_get_prop_batt_current(bd);
0413         break;
0414 
0415     case POWER_SUPPLY_PROP_CHARGE_TYPE:
0416         val->intval = bd9995x_get_prop_charge_type(bd);
0417         break;
0418 
0419     case POWER_SUPPLY_PROP_HEALTH:
0420         val->intval = bd9995x_get_prop_batt_health(bd);
0421         break;
0422 
0423     case POWER_SUPPLY_PROP_TEMP:
0424         val->intval = bd9995x_get_prop_batt_temp(bd);
0425         break;
0426 
0427     case POWER_SUPPLY_PROP_TECHNOLOGY:
0428         val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
0429         break;
0430 
0431     case POWER_SUPPLY_PROP_MODEL_NAME:
0432         val->strval = "bd99954";
0433         break;
0434 
0435     default:
0436         return -EINVAL;
0437 
0438     }
0439 
0440     return 0;
0441 }
0442 
0443 static int bd9995x_get_chip_state(struct bd9995x_device *bd,
0444                   struct bd9995x_state *state)
0445 {
0446     int i, ret, tmp;
0447     struct {
0448         struct regmap_field *id;
0449         u16 *data;
0450     } state_fields[] = {
0451         {
0452             bd->rmap_fields[F_CHGSTM_STATE], &state->chgstm_status,
0453         }, {
0454             bd->rmap_fields[F_VBAT_VSYS_STATUS],
0455             &state->vbat_vsys_status,
0456         }, {
0457             bd->rmap_fields[F_VBUS_VCC_STATUS],
0458             &state->vbus_vcc_status,
0459         },
0460     };
0461 
0462 
0463     for (i = 0; i < ARRAY_SIZE(state_fields); i++) {
0464         ret = regmap_field_read(state_fields[i].id, &tmp);
0465         if (ret)
0466             return ret;
0467 
0468         *state_fields[i].data = tmp;
0469     }
0470 
0471     if (state->vbus_vcc_status & STATUS_VCC_DET ||
0472         state->vbus_vcc_status & STATUS_VBUS_DET)
0473         state->online = 1;
0474     else
0475         state->online = 0;
0476 
0477     return 0;
0478 }
0479 
0480 static irqreturn_t bd9995x_irq_handler_thread(int irq, void *private)
0481 {
0482     struct bd9995x_device *bd = private;
0483     int ret, status, mask, i;
0484     unsigned long tmp;
0485     struct bd9995x_state state;
0486 
0487     /*
0488      * The bd9995x does not seem to generate big amount of interrupts.
0489      * The logic regarding which interrupts can cause relevant
0490      * status changes seem to be pretty complex.
0491      *
0492      * So lets implement really simple and hopefully bullet-proof handler:
0493      * It does not really matter which IRQ we handle, we just go and
0494      * re-read all interesting statuses + give the framework a nudge.
0495      *
0496      * Other option would be building a _complex_ and error prone logic
0497      * trying to decide what could have been changed (resulting this IRQ
0498      * we are now handling). During the normal operation the BD99954 does
0499      * not seem to be generating much of interrupts so benefit from such
0500      * logic would probably be minimal.
0501      */
0502 
0503     ret = regmap_read(bd->rmap, INT0_STATUS, &status);
0504     if (ret) {
0505         dev_err(bd->dev, "Failed to read IRQ status\n");
0506         return IRQ_NONE;
0507     }
0508 
0509     ret = regmap_field_read(bd->rmap_fields[F_INT0_SET], &mask);
0510     if (ret) {
0511         dev_err(bd->dev, "Failed to read IRQ mask\n");
0512         return IRQ_NONE;
0513     }
0514 
0515     /* Handle only IRQs that are not masked */
0516     status &= mask;
0517     tmp = status;
0518 
0519     /* Lowest bit does not represent any sub-registers */
0520     tmp >>= 1;
0521 
0522     /*
0523      * Mask and ack IRQs we will handle (+ the idiot bit)
0524      */
0525     ret = regmap_field_write(bd->rmap_fields[F_INT0_SET], 0);
0526     if (ret) {
0527         dev_err(bd->dev, "Failed to mask F_INT0\n");
0528         return IRQ_NONE;
0529     }
0530 
0531     ret = regmap_write(bd->rmap, INT0_STATUS, status);
0532     if (ret) {
0533         dev_err(bd->dev, "Failed to ack F_INT0\n");
0534         goto err_umask;
0535     }
0536 
0537     for_each_set_bit(i, &tmp, 7) {
0538         int sub_status, sub_mask;
0539         int sub_status_reg[] = {
0540             INT1_STATUS, INT2_STATUS, INT3_STATUS, INT4_STATUS,
0541             INT5_STATUS, INT6_STATUS, INT7_STATUS,
0542         };
0543         struct regmap_field *sub_mask_f[] = {
0544             bd->rmap_fields[F_INT1_SET],
0545             bd->rmap_fields[F_INT2_SET],
0546             bd->rmap_fields[F_INT3_SET],
0547             bd->rmap_fields[F_INT4_SET],
0548             bd->rmap_fields[F_INT5_SET],
0549             bd->rmap_fields[F_INT6_SET],
0550             bd->rmap_fields[F_INT7_SET],
0551         };
0552 
0553         /* Clear sub IRQs */
0554         ret = regmap_read(bd->rmap, sub_status_reg[i], &sub_status);
0555         if (ret) {
0556             dev_err(bd->dev, "Failed to read IRQ sub-status\n");
0557             goto err_umask;
0558         }
0559 
0560         ret = regmap_field_read(sub_mask_f[i], &sub_mask);
0561         if (ret) {
0562             dev_err(bd->dev, "Failed to read IRQ sub-mask\n");
0563             goto err_umask;
0564         }
0565 
0566         /* Ack active sub-statuses */
0567         sub_status &= sub_mask;
0568 
0569         ret = regmap_write(bd->rmap, sub_status_reg[i], sub_status);
0570         if (ret) {
0571             dev_err(bd->dev, "Failed to ack sub-IRQ\n");
0572             goto err_umask;
0573         }
0574     }
0575 
0576     ret = regmap_field_write(bd->rmap_fields[F_INT0_SET], mask);
0577     if (ret)
0578         /* May as well retry once */
0579         goto err_umask;
0580 
0581     /* Read whole chip state */
0582     ret = bd9995x_get_chip_state(bd, &state);
0583     if (ret < 0) {
0584         dev_err(bd->dev, "Failed to read chip state\n");
0585     } else {
0586         mutex_lock(&bd->lock);
0587         bd->state = state;
0588         mutex_unlock(&bd->lock);
0589 
0590         power_supply_changed(bd->charger);
0591     }
0592 
0593     return IRQ_HANDLED;
0594 
0595 err_umask:
0596     ret = regmap_field_write(bd->rmap_fields[F_INT0_SET], mask);
0597     if (ret)
0598         dev_err(bd->dev,
0599         "Failed to un-mask F_INT0 - IRQ permanently disabled\n");
0600 
0601     return IRQ_NONE;
0602 }
0603 
0604 static int __bd9995x_chip_reset(struct bd9995x_device *bd)
0605 {
0606     int ret, state;
0607     int rst_check_counter = 10;
0608     u16 tmp = ALLRST | OTPLD;
0609 
0610     ret = regmap_raw_write(bd->rmap, SYSTEM_CTRL_SET, &tmp, 2);
0611     if (ret < 0)
0612         return ret;
0613 
0614     do {
0615         ret = regmap_field_read(bd->rmap_fields[F_OTPLD_STATE], &state);
0616         if (ret)
0617             return ret;
0618 
0619         msleep(10);
0620     } while (state == 0 && --rst_check_counter);
0621 
0622     if (!rst_check_counter) {
0623         dev_err(bd->dev, "chip reset not completed\n");
0624         return -ETIMEDOUT;
0625     }
0626 
0627     tmp = 0;
0628     ret = regmap_raw_write(bd->rmap, SYSTEM_CTRL_SET, &tmp, 2);
0629 
0630     return ret;
0631 }
0632 
0633 static int bd9995x_hw_init(struct bd9995x_device *bd)
0634 {
0635     int ret;
0636     int i;
0637     struct bd9995x_state state;
0638     struct bd9995x_init_data *id = &bd->init_data;
0639 
0640     const struct {
0641         enum bd9995x_fields id;
0642         u16 value;
0643     } init_data[] = {
0644         /* Enable the charging trigger after SDP charger attached */
0645         {F_SDP_CHG_TRIG_EN, 1},
0646         /* Enable charging trigger after SDP charger attached */
0647         {F_SDP_CHG_TRIG,    1},
0648         /* Disable charging trigger by BC1.2 detection */
0649         {F_VBUS_BC_DISEN,   1},
0650         /* Disable charging trigger by BC1.2 detection */
0651         {F_VCC_BC_DISEN,    1},
0652         /* Disable automatic limitation of the input current */
0653         {F_ILIM_AUTO_DISEN, 1},
0654         /* Select current limitation when SDP charger attached*/
0655         {F_SDP_500_SEL,     1},
0656         /* Select current limitation when DCP charger attached */
0657         {F_DCP_2500_SEL,    1},
0658         {F_VSYSREG_SET,     id->vsysreg_set},
0659         /* Activate USB charging and DC/DC converter */
0660         {F_USB_SUS,     0},
0661         /* DCDC clock: 1200 kHz*/
0662         {F_DCDC_CLK_SEL,    3},
0663         /* Enable charging */
0664         {F_CHG_EN,      1},
0665         /* Disable Input current Limit setting voltage measurement */
0666         {F_EXTIADPEN,       0},
0667         /* Disable input current limiting */
0668         {F_VSYS_PRIORITY,   1},
0669         {F_IBUS_LIM_SET,    id->ibus_lim_set},
0670         {F_ICC_LIM_SET,     id->icc_lim_set},
0671         /* Charge Termination Current Setting to 0*/
0672         {F_ITERM_SET,       id->iterm_set},
0673         /* Trickle-charge Current Setting */
0674         {F_ITRICH_SET,      id->itrich_set},
0675         /* Pre-charge Current setting */
0676         {F_IPRECH_SET,      id->iprech_set},
0677         /* Fast Charge Current for constant current phase */
0678         {F_ICHG_SET,        id->ichg_set},
0679         /* Fast Charge Voltage Regulation Setting */
0680         {F_VFASTCHG_REG_SET1,   id->vfastchg_reg_set1},
0681         /* Set Pre-charge Voltage Threshold for trickle charging. */
0682         {F_VPRECHG_TH_SET,  id->vprechg_th_set},
0683         {F_VRECHG_SET,      id->vrechg_set},
0684         {F_VBATOVP_SET,     id->vbatovp_set},
0685         /* Reverse buck boost voltage Setting */
0686         {F_VRBOOST_SET,     0},
0687         /* Disable fast-charging watchdog */
0688         {F_WDT_FST,     0},
0689         /* Disable pre-charging watchdog */
0690         {F_WDT_PRE,     0},
0691         /* Power save off */
0692         {F_POWER_SAVE_MODE, 0},
0693         {F_INT1_SET,        INT1_ALL},
0694         {F_INT2_SET,        INT2_ALL},
0695         {F_INT3_SET,        INT3_ALL},
0696         {F_INT4_SET,        INT4_ALL},
0697         {F_INT5_SET,        INT5_ALL},
0698         {F_INT6_SET,        INT6_ALL},
0699         {F_INT7_SET,        INT7_ALL},
0700     };
0701 
0702     /*
0703      * Currently we initialize charger to a known state at startup.
0704      * If we want to allow for example the boot code to initialize
0705      * charger we should get rid of this.
0706      */
0707     ret = __bd9995x_chip_reset(bd);
0708     if (ret < 0)
0709         return ret;
0710 
0711     /* Initialize currents/voltages and other parameters */
0712     for (i = 0; i < ARRAY_SIZE(init_data); i++) {
0713         ret = regmap_field_write(bd->rmap_fields[init_data[i].id],
0714                      init_data[i].value);
0715         if (ret) {
0716             dev_err(bd->dev, "failed to initialize charger (%d)\n",
0717                 ret);
0718             return ret;
0719         }
0720     }
0721 
0722     ret = bd9995x_get_chip_state(bd, &state);
0723     if (ret < 0)
0724         return ret;
0725 
0726     mutex_lock(&bd->lock);
0727     bd->state = state;
0728     mutex_unlock(&bd->lock);
0729 
0730     return 0;
0731 }
0732 
0733 static enum power_supply_property bd9995x_power_supply_props[] = {
0734     POWER_SUPPLY_PROP_MANUFACTURER,
0735     POWER_SUPPLY_PROP_STATUS,
0736     POWER_SUPPLY_PROP_ONLINE,
0737     POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
0738     POWER_SUPPLY_PROP_CHARGE_AVG,
0739     POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
0740     POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
0741     POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
0742     /* Battery props we access through charger */
0743     POWER_SUPPLY_PROP_PRESENT,
0744     POWER_SUPPLY_PROP_VOLTAGE_NOW,
0745     POWER_SUPPLY_PROP_CURRENT_NOW,
0746     POWER_SUPPLY_PROP_CHARGE_TYPE,
0747     POWER_SUPPLY_PROP_HEALTH,
0748     POWER_SUPPLY_PROP_TEMP,
0749     POWER_SUPPLY_PROP_TECHNOLOGY,
0750     POWER_SUPPLY_PROP_MODEL_NAME,
0751 };
0752 
0753 static const struct power_supply_desc bd9995x_power_supply_desc = {
0754     .name = "bd9995x-charger",
0755     .type = POWER_SUPPLY_TYPE_USB,
0756     .properties = bd9995x_power_supply_props,
0757     .num_properties = ARRAY_SIZE(bd9995x_power_supply_props),
0758     .get_property = bd9995x_power_supply_get_property,
0759 };
0760 
0761 /*
0762  * Limit configurations for vbus-input-current and vcc-vacp-input-current
0763  * Minimum limit is 0 uA. Max is 511 * 32000 uA = 16352000 uA. This is
0764  * configured by writing a register so that each increment in register
0765  * value equals to 32000 uA limit increment.
0766  *
0767  * Eg, value 0x0 is limit 0, value 0x1 is limit 32000, ...
0768  * Describe the setting in linear_range table.
0769  */
0770 static const struct linear_range input_current_limit_ranges[] = {
0771     {
0772         .min = 0,
0773         .step = 32000,
0774         .min_sel = 0x0,
0775         .max_sel = 0x1ff,
0776     },
0777 };
0778 
0779 /* Possible trickle, pre-charging and termination current values */
0780 static const struct linear_range charging_current_ranges[] = {
0781     {
0782         .min = 0,
0783         .step = 64000,
0784         .min_sel = 0x0,
0785         .max_sel = 0x10,
0786     }, {
0787         .min = 1024000,
0788         .step = 0,
0789         .min_sel = 0x11,
0790         .max_sel = 0x1f,
0791     },
0792 };
0793 
0794 /*
0795  * Fast charging voltage regulation, starting re-charging limit
0796  * and battery over voltage protection have same possible values
0797  */
0798 static const struct linear_range charge_voltage_regulation_ranges[] = {
0799     {
0800         .min = 2560000,
0801         .step = 0,
0802         .min_sel = 0,
0803         .max_sel = 0xA0,
0804     }, {
0805         .min = 2560000,
0806         .step = 16000,
0807         .min_sel = 0xA0,
0808         .max_sel = 0x4B0,
0809     }, {
0810         .min = 19200000,
0811         .step = 0,
0812         .min_sel = 0x4B0,
0813         .max_sel = 0x7FF,
0814     },
0815 };
0816 
0817 /* Possible VSYS voltage regulation values */
0818 static const struct linear_range vsys_voltage_regulation_ranges[] = {
0819     {
0820         .min = 2560000,
0821         .step = 0,
0822         .min_sel = 0,
0823         .max_sel = 0x28,
0824     }, {
0825         .min = 2560000,
0826         .step = 64000,
0827         .min_sel = 0x28,
0828         .max_sel = 0x12C,
0829     }, {
0830         .min = 19200000,
0831         .step = 0,
0832         .min_sel = 0x12C,
0833         .max_sel = 0x1FF,
0834     },
0835 };
0836 
0837 /* Possible settings for switching from trickle to pre-charging limits */
0838 static const struct linear_range trickle_to_pre_threshold_ranges[] = {
0839     {
0840         .min = 2048000,
0841         .step = 0,
0842         .min_sel = 0,
0843         .max_sel = 0x20,
0844     }, {
0845         .min = 2048000,
0846         .step = 64000,
0847         .min_sel = 0x20,
0848         .max_sel = 0x12C,
0849     }, {
0850         .min = 19200000,
0851         .step = 0,
0852         .min_sel = 0x12C,
0853         .max_sel = 0x1FF
0854     }
0855 };
0856 
0857 /* Possible current values for fast-charging constant current phase */
0858 static const struct linear_range fast_charge_current_ranges[] = {
0859     {
0860         .min = 0,
0861         .step = 64000,
0862         .min_sel = 0,
0863         .max_sel = 0xFF,
0864     }
0865 };
0866 
0867 struct battery_init {
0868     const char *name;
0869     int *info_data;
0870     const struct linear_range *range;
0871     int ranges;
0872     u16 *data;
0873 };
0874 
0875 struct dt_init {
0876     char *prop;
0877     const struct linear_range *range;
0878     int ranges;
0879     u16 *data;
0880 };
0881 
0882 static int bd9995x_fw_probe(struct bd9995x_device *bd)
0883 {
0884     int ret;
0885     struct power_supply_battery_info *info;
0886     u32 property;
0887     int i;
0888     int regval;
0889     bool found;
0890     struct bd9995x_init_data *init = &bd->init_data;
0891     struct battery_init battery_inits[] = {
0892         {
0893             .name = "trickle-charging current",
0894             .range = &charging_current_ranges[0],
0895             .ranges = 2,
0896             .data = &init->itrich_set,
0897         }, {
0898             .name = "pre-charging current",
0899             .range = &charging_current_ranges[0],
0900             .ranges = 2,
0901             .data = &init->iprech_set,
0902         }, {
0903             .name = "pre-to-trickle charge voltage threshold",
0904             .range = &trickle_to_pre_threshold_ranges[0],
0905             .ranges = 2,
0906             .data = &init->vprechg_th_set,
0907         }, {
0908             .name = "charging termination current",
0909             .range = &charging_current_ranges[0],
0910             .ranges = 2,
0911             .data = &init->iterm_set,
0912         }, {
0913             .name = "charging re-start voltage",
0914             .range = &charge_voltage_regulation_ranges[0],
0915             .ranges = 2,
0916             .data = &init->vrechg_set,
0917         }, {
0918             .name = "battery overvoltage limit",
0919             .range = &charge_voltage_regulation_ranges[0],
0920             .ranges = 2,
0921             .data = &init->vbatovp_set,
0922         }, {
0923             .name = "fast-charging max current",
0924             .range = &fast_charge_current_ranges[0],
0925             .ranges = 1,
0926             .data = &init->ichg_set,
0927         }, {
0928             .name = "fast-charging voltage",
0929             .range = &charge_voltage_regulation_ranges[0],
0930             .ranges = 2,
0931             .data = &init->vfastchg_reg_set1,
0932         },
0933     };
0934     struct dt_init props[] = {
0935         {
0936             .prop = "rohm,vsys-regulation-microvolt",
0937             .range = &vsys_voltage_regulation_ranges[0],
0938             .ranges = 2,
0939             .data = &init->vsysreg_set,
0940         }, {
0941             .prop = "rohm,vbus-input-current-limit-microamp",
0942             .range = &input_current_limit_ranges[0],
0943             .ranges = 1,
0944             .data = &init->ibus_lim_set,
0945         }, {
0946             .prop = "rohm,vcc-input-current-limit-microamp",
0947             .range = &input_current_limit_ranges[0],
0948             .ranges = 1,
0949             .data = &init->icc_lim_set,
0950         },
0951     };
0952 
0953     /*
0954      * The power_supply_get_battery_info() does not support getting values
0955      * from ACPI. Let's fix it if ACPI is required here.
0956      */
0957     ret = power_supply_get_battery_info(bd->charger, &info);
0958     if (ret < 0)
0959         return ret;
0960 
0961     /* Put pointers to the generic battery info */
0962     battery_inits[0].info_data = &info->tricklecharge_current_ua;
0963     battery_inits[1].info_data = &info->precharge_current_ua;
0964     battery_inits[2].info_data = &info->precharge_voltage_max_uv;
0965     battery_inits[3].info_data = &info->charge_term_current_ua;
0966     battery_inits[4].info_data = &info->charge_restart_voltage_uv;
0967     battery_inits[5].info_data = &info->overvoltage_limit_uv;
0968     battery_inits[6].info_data = &info->constant_charge_current_max_ua;
0969     battery_inits[7].info_data = &info->constant_charge_voltage_max_uv;
0970 
0971     for (i = 0; i < ARRAY_SIZE(battery_inits); i++) {
0972         int val = *battery_inits[i].info_data;
0973         const struct linear_range *range = battery_inits[i].range;
0974         int ranges = battery_inits[i].ranges;
0975 
0976         if (val == -EINVAL)
0977             continue;
0978 
0979         ret = linear_range_get_selector_low_array(range, ranges, val,
0980                               &regval, &found);
0981         if (ret) {
0982             dev_err(bd->dev, "Unsupported value for %s\n",
0983                 battery_inits[i].name);
0984 
0985             power_supply_put_battery_info(bd->charger, info);
0986             return -EINVAL;
0987         }
0988         if (!found) {
0989             dev_warn(bd->dev,
0990                  "Unsupported value for %s - using smaller\n",
0991                  battery_inits[i].name);
0992         }
0993         *(battery_inits[i].data) = regval;
0994     }
0995 
0996     power_supply_put_battery_info(bd->charger, info);
0997 
0998     for (i = 0; i < ARRAY_SIZE(props); i++) {
0999         ret = device_property_read_u32(bd->dev, props[i].prop,
1000                            &property);
1001         if (ret < 0) {
1002             dev_err(bd->dev, "failed to read %s", props[i].prop);
1003 
1004             return ret;
1005         }
1006 
1007         ret = linear_range_get_selector_low_array(props[i].range,
1008                               props[i].ranges,
1009                               property, &regval,
1010                               &found);
1011         if (ret) {
1012             dev_err(bd->dev, "Unsupported value for '%s'\n",
1013                 props[i].prop);
1014 
1015             return -EINVAL;
1016         }
1017 
1018         if (!found) {
1019             dev_warn(bd->dev,
1020                  "Unsupported value for '%s' - using smaller\n",
1021                  props[i].prop);
1022         }
1023 
1024         *(props[i].data) = regval;
1025     }
1026 
1027     return 0;
1028 }
1029 
1030 static void bd9995x_chip_reset(void *bd)
1031 {
1032     __bd9995x_chip_reset(bd);
1033 }
1034 
1035 static int bd9995x_probe(struct i2c_client *client)
1036 {
1037     struct device *dev = &client->dev;
1038     struct bd9995x_device *bd;
1039     struct power_supply_config psy_cfg = {};
1040     int ret;
1041     int i;
1042 
1043     bd = devm_kzalloc(dev, sizeof(*bd), GFP_KERNEL);
1044     if (!bd)
1045         return -ENOMEM;
1046 
1047     bd->client = client;
1048     bd->dev = dev;
1049     psy_cfg.drv_data = bd;
1050     psy_cfg.of_node = dev->of_node;
1051 
1052     mutex_init(&bd->lock);
1053 
1054     bd->rmap = devm_regmap_init_i2c(client, &bd9995x_regmap_config);
1055     if (IS_ERR(bd->rmap)) {
1056         dev_err(dev, "Failed to setup register access via i2c\n");
1057         return PTR_ERR(bd->rmap);
1058     }
1059 
1060     for (i = 0; i < ARRAY_SIZE(bd9995x_reg_fields); i++) {
1061         const struct reg_field *reg_fields = bd9995x_reg_fields;
1062 
1063         bd->rmap_fields[i] = devm_regmap_field_alloc(dev, bd->rmap,
1064                                  reg_fields[i]);
1065         if (IS_ERR(bd->rmap_fields[i])) {
1066             dev_err(dev, "cannot allocate regmap field\n");
1067             return PTR_ERR(bd->rmap_fields[i]);
1068         }
1069     }
1070 
1071     i2c_set_clientdata(client, bd);
1072 
1073     ret = regmap_field_read(bd->rmap_fields[F_CHIP_ID], &bd->chip_id);
1074     if (ret) {
1075         dev_err(dev, "Cannot read chip ID.\n");
1076         return ret;
1077     }
1078 
1079     if (bd->chip_id != BD99954_ID) {
1080         dev_err(dev, "Chip with ID=0x%x, not supported!\n",
1081             bd->chip_id);
1082         return -ENODEV;
1083     }
1084 
1085     ret = regmap_field_read(bd->rmap_fields[F_CHIP_REV], &bd->chip_rev);
1086     if (ret) {
1087         dev_err(dev, "Cannot read revision.\n");
1088         return ret;
1089     }
1090 
1091     dev_info(bd->dev, "Found BD99954 chip rev %d\n", bd->chip_rev);
1092 
1093     /*
1094      * We need to init the psy before we can call
1095      * power_supply_get_battery_info() for it
1096      */
1097     bd->charger = devm_power_supply_register(bd->dev,
1098                          &bd9995x_power_supply_desc,
1099                         &psy_cfg);
1100     if (IS_ERR(bd->charger)) {
1101         dev_err(dev, "Failed to register power supply\n");
1102         return PTR_ERR(bd->charger);
1103     }
1104 
1105     ret = bd9995x_fw_probe(bd);
1106     if (ret < 0) {
1107         dev_err(dev, "Cannot read device properties.\n");
1108         return ret;
1109     }
1110 
1111     ret = bd9995x_hw_init(bd);
1112     if (ret < 0) {
1113         dev_err(dev, "Cannot initialize the chip.\n");
1114         return ret;
1115     }
1116 
1117     ret = devm_add_action_or_reset(dev, bd9995x_chip_reset, bd);
1118     if (ret)
1119         return ret;
1120 
1121     return devm_request_threaded_irq(dev, client->irq, NULL,
1122                      bd9995x_irq_handler_thread,
1123                      IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1124                      BD9995X_IRQ_PIN, bd);
1125 }
1126 
1127 static const struct of_device_id bd9995x_of_match[] = {
1128     { .compatible = "rohm,bd99954", },
1129     { }
1130 };
1131 MODULE_DEVICE_TABLE(of, bd9995x_of_match);
1132 
1133 static struct i2c_driver bd9995x_driver = {
1134     .driver = {
1135         .name = "bd9995x-charger",
1136         .of_match_table = bd9995x_of_match,
1137     },
1138     .probe_new = bd9995x_probe,
1139 };
1140 module_i2c_driver(bd9995x_driver);
1141 
1142 MODULE_AUTHOR("Laine Markus <markus.laine@fi.rohmeurope.com>");
1143 MODULE_DESCRIPTION("ROHM BD99954 charger driver");
1144 MODULE_LICENSE("GPL");