Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Summit Microelectronics SMB347 Battery Charger Driver
0004  *
0005  * Copyright (C) 2011, Intel Corporation
0006  *
0007  * Authors: Bruce E. Robertson <bruce.e.robertson@intel.com>
0008  *          Mika Westerberg <mika.westerberg@linux.intel.com>
0009  */
0010 
0011 #include <linux/delay.h>
0012 #include <linux/err.h>
0013 #include <linux/kernel.h>
0014 #include <linux/module.h>
0015 #include <linux/init.h>
0016 #include <linux/interrupt.h>
0017 #include <linux/i2c.h>
0018 #include <linux/power_supply.h>
0019 #include <linux/property.h>
0020 #include <linux/regmap.h>
0021 #include <linux/regulator/driver.h>
0022 
0023 #include <dt-bindings/power/summit,smb347-charger.h>
0024 
0025 /* Use the default compensation method */
0026 #define SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT -1
0027 
0028 /* Use default factory programmed value for hard/soft temperature limit */
0029 #define SMB3XX_TEMP_USE_DEFAULT     -273
0030 
0031 /*
0032  * Configuration registers. These are mirrored to volatile RAM and can be
0033  * written once %CMD_A_ALLOW_WRITE is set in %CMD_A register. They will be
0034  * reloaded from non-volatile registers after POR.
0035  */
0036 #define CFG_CHARGE_CURRENT          0x00
0037 #define CFG_CHARGE_CURRENT_FCC_MASK     0xe0
0038 #define CFG_CHARGE_CURRENT_FCC_SHIFT        5
0039 #define CFG_CHARGE_CURRENT_PCC_MASK     0x18
0040 #define CFG_CHARGE_CURRENT_PCC_SHIFT        3
0041 #define CFG_CHARGE_CURRENT_TC_MASK      0x07
0042 #define CFG_CURRENT_LIMIT           0x01
0043 #define CFG_CURRENT_LIMIT_DC_MASK       0xf0
0044 #define CFG_CURRENT_LIMIT_DC_SHIFT      4
0045 #define CFG_CURRENT_LIMIT_USB_MASK      0x0f
0046 #define CFG_FLOAT_VOLTAGE           0x03
0047 #define CFG_FLOAT_VOLTAGE_FLOAT_MASK        0x3f
0048 #define CFG_FLOAT_VOLTAGE_THRESHOLD_MASK    0xc0
0049 #define CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT   6
0050 #define CFG_STAT                0x05
0051 #define CFG_STAT_DISABLED           BIT(5)
0052 #define CFG_STAT_ACTIVE_HIGH            BIT(7)
0053 #define CFG_PIN                 0x06
0054 #define CFG_PIN_EN_CTRL_MASK            0x60
0055 #define CFG_PIN_EN_CTRL_ACTIVE_HIGH     0x40
0056 #define CFG_PIN_EN_CTRL_ACTIVE_LOW      0x60
0057 #define CFG_PIN_EN_APSD_IRQ         BIT(1)
0058 #define CFG_PIN_EN_CHARGER_ERROR        BIT(2)
0059 #define CFG_PIN_EN_CTRL             BIT(4)
0060 #define CFG_THERM               0x07
0061 #define CFG_THERM_SOFT_HOT_COMPENSATION_MASK    0x03
0062 #define CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT   0
0063 #define CFG_THERM_SOFT_COLD_COMPENSATION_MASK   0x0c
0064 #define CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT  2
0065 #define CFG_THERM_MONITOR_DISABLED      BIT(4)
0066 #define CFG_SYSOK               0x08
0067 #define CFG_SYSOK_INOK_ACTIVE_HIGH      BIT(0)
0068 #define CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED   BIT(2)
0069 #define CFG_OTHER               0x09
0070 #define CFG_OTHER_RID_MASK          0xc0
0071 #define CFG_OTHER_RID_ENABLED_AUTO_OTG      0xc0
0072 #define CFG_OTG                 0x0a
0073 #define CFG_OTG_TEMP_THRESHOLD_MASK     0x30
0074 #define CFG_OTG_CURRENT_LIMIT_250mA     BIT(2)
0075 #define CFG_OTG_CURRENT_LIMIT_750mA     BIT(3)
0076 #define CFG_OTG_TEMP_THRESHOLD_SHIFT        4
0077 #define CFG_OTG_CC_COMPENSATION_MASK        0xc0
0078 #define CFG_OTG_CC_COMPENSATION_SHIFT       6
0079 #define CFG_TEMP_LIMIT              0x0b
0080 #define CFG_TEMP_LIMIT_SOFT_HOT_MASK        0x03
0081 #define CFG_TEMP_LIMIT_SOFT_HOT_SHIFT       0
0082 #define CFG_TEMP_LIMIT_SOFT_COLD_MASK       0x0c
0083 #define CFG_TEMP_LIMIT_SOFT_COLD_SHIFT      2
0084 #define CFG_TEMP_LIMIT_HARD_HOT_MASK        0x30
0085 #define CFG_TEMP_LIMIT_HARD_HOT_SHIFT       4
0086 #define CFG_TEMP_LIMIT_HARD_COLD_MASK       0xc0
0087 #define CFG_TEMP_LIMIT_HARD_COLD_SHIFT      6
0088 #define CFG_FAULT_IRQ               0x0c
0089 #define CFG_FAULT_IRQ_DCIN_UV           BIT(2)
0090 #define CFG_STATUS_IRQ              0x0d
0091 #define CFG_STATUS_IRQ_TERMINATION_OR_TAPER BIT(4)
0092 #define CFG_STATUS_IRQ_CHARGE_TIMEOUT       BIT(7)
0093 #define CFG_ADDRESS             0x0e
0094 
0095 /* Command registers */
0096 #define CMD_A                   0x30
0097 #define CMD_A_CHG_ENABLED           BIT(1)
0098 #define CMD_A_SUSPEND_ENABLED           BIT(2)
0099 #define CMD_A_OTG_ENABLED           BIT(4)
0100 #define CMD_A_ALLOW_WRITE           BIT(7)
0101 #define CMD_B                   0x31
0102 #define CMD_C                   0x33
0103 
0104 /* Interrupt Status registers */
0105 #define IRQSTAT_A               0x35
0106 #define IRQSTAT_C               0x37
0107 #define IRQSTAT_C_TERMINATION_STAT      BIT(0)
0108 #define IRQSTAT_C_TERMINATION_IRQ       BIT(1)
0109 #define IRQSTAT_C_TAPER_IRQ         BIT(3)
0110 #define IRQSTAT_D               0x38
0111 #define IRQSTAT_D_CHARGE_TIMEOUT_STAT       BIT(2)
0112 #define IRQSTAT_D_CHARGE_TIMEOUT_IRQ        BIT(3)
0113 #define IRQSTAT_E               0x39
0114 #define IRQSTAT_E_USBIN_UV_STAT         BIT(0)
0115 #define IRQSTAT_E_USBIN_UV_IRQ          BIT(1)
0116 #define IRQSTAT_E_DCIN_UV_STAT          BIT(4)
0117 #define IRQSTAT_E_DCIN_UV_IRQ           BIT(5)
0118 #define IRQSTAT_F               0x3a
0119 
0120 /* Status registers */
0121 #define STAT_A                  0x3b
0122 #define STAT_A_FLOAT_VOLTAGE_MASK       0x3f
0123 #define STAT_B                  0x3c
0124 #define STAT_C                  0x3d
0125 #define STAT_C_CHG_ENABLED          BIT(0)
0126 #define STAT_C_HOLDOFF_STAT         BIT(3)
0127 #define STAT_C_CHG_MASK             0x06
0128 #define STAT_C_CHG_SHIFT            1
0129 #define STAT_C_CHG_TERM             BIT(5)
0130 #define STAT_C_CHARGER_ERROR            BIT(6)
0131 #define STAT_E                  0x3f
0132 
0133 #define SMB347_MAX_REGISTER         0x3f
0134 
0135 /**
0136  * struct smb347_charger - smb347 charger instance
0137  * @dev: pointer to device
0138  * @regmap: pointer to driver regmap
0139  * @mains: power_supply instance for AC/DC power
0140  * @usb: power_supply instance for USB power
0141  * @usb_rdev: USB VBUS regulator device
0142  * @id: SMB charger ID
0143  * @mains_online: is AC/DC input connected
0144  * @usb_online: is USB input connected
0145  * @irq_unsupported: is interrupt unsupported by SMB hardware
0146  * @usb_vbus_enabled: is USB VBUS powered by SMB charger
0147  * @max_charge_current: maximum current (in uA) the battery can be charged
0148  * @max_charge_voltage: maximum voltage (in uV) the battery can be charged
0149  * @pre_charge_current: current (in uA) to use in pre-charging phase
0150  * @termination_current: current (in uA) used to determine when the
0151  *           charging cycle terminates
0152  * @pre_to_fast_voltage: voltage (in uV) treshold used for transitioning to
0153  *           pre-charge to fast charge mode
0154  * @mains_current_limit: maximum input current drawn from AC/DC input (in uA)
0155  * @usb_hc_current_limit: maximum input high current (in uA) drawn from USB
0156  *            input
0157  * @chip_temp_threshold: die temperature where device starts limiting charge
0158  *           current [%100 - %130] (in degree C)
0159  * @soft_cold_temp_limit: soft cold temperature limit [%0 - %15] (in degree C),
0160  *            granularity is 5 deg C.
0161  * @soft_hot_temp_limit: soft hot temperature limit [%40 - %55] (in degree  C),
0162  *           granularity is 5 deg C.
0163  * @hard_cold_temp_limit: hard cold temperature limit [%-5 - %10] (in degree C),
0164  *            granularity is 5 deg C.
0165  * @hard_hot_temp_limit: hard hot temperature limit [%50 - %65] (in degree C),
0166  *           granularity is 5 deg C.
0167  * @suspend_on_hard_temp_limit: suspend charging when hard limit is hit
0168  * @soft_temp_limit_compensation: compensation method when soft temperature
0169  *                limit is hit
0170  * @charge_current_compensation: current (in uA) for charging compensation
0171  *               current when temperature hits soft limits
0172  * @use_mains: AC/DC input can be used
0173  * @use_usb: USB input can be used
0174  * @use_usb_otg: USB OTG output can be used (not implemented yet)
0175  * @enable_control: how charging enable/disable is controlled
0176  *          (driver/pin controls)
0177  * @inok_polarity: polarity of INOK signal which denotes presence of external
0178  *         power supply
0179  *
0180  * @use_main, @use_usb, and @use_usb_otg are means to enable/disable
0181  * hardware support for these. This is useful when we want to have for
0182  * example OTG charging controlled via OTG transceiver driver and not by
0183  * the SMB347 hardware.
0184  *
0185  * Hard and soft temperature limit values are given as described in the
0186  * device data sheet and assuming NTC beta value is %3750. Even if this is
0187  * not the case, these values should be used. They can be mapped to the
0188  * corresponding NTC beta values with the help of table %2 in the data
0189  * sheet. So for example if NTC beta is %3375 and we want to program hard
0190  * hot limit to be %53 deg C, @hard_hot_temp_limit should be set to %50.
0191  *
0192  * If zero value is given in any of the current and voltage values, the
0193  * factory programmed default will be used. For soft/hard temperature
0194  * values, pass in %SMB3XX_TEMP_USE_DEFAULT instead.
0195  */
0196 struct smb347_charger {
0197     struct device       *dev;
0198     struct regmap       *regmap;
0199     struct power_supply *mains;
0200     struct power_supply *usb;
0201     struct regulator_dev    *usb_rdev;
0202     unsigned int        id;
0203     bool            mains_online;
0204     bool            usb_online;
0205     bool            irq_unsupported;
0206     bool            usb_vbus_enabled;
0207 
0208     unsigned int        max_charge_current;
0209     unsigned int        max_charge_voltage;
0210     unsigned int        pre_charge_current;
0211     unsigned int        termination_current;
0212     unsigned int        pre_to_fast_voltage;
0213     unsigned int        mains_current_limit;
0214     unsigned int        usb_hc_current_limit;
0215     unsigned int        chip_temp_threshold;
0216     int         soft_cold_temp_limit;
0217     int         soft_hot_temp_limit;
0218     int         hard_cold_temp_limit;
0219     int         hard_hot_temp_limit;
0220     bool            suspend_on_hard_temp_limit;
0221     unsigned int        soft_temp_limit_compensation;
0222     unsigned int        charge_current_compensation;
0223     bool            use_mains;
0224     bool            use_usb;
0225     bool            use_usb_otg;
0226     unsigned int        enable_control;
0227     unsigned int        inok_polarity;
0228 };
0229 
0230 enum smb_charger_chipid {
0231     SMB345,
0232     SMB347,
0233     SMB358,
0234     NUM_CHIP_TYPES,
0235 };
0236 
0237 /* Fast charge current in uA */
0238 static const unsigned int fcc_tbl[NUM_CHIP_TYPES][8] = {
0239     [SMB345] = {  200000,  450000,  600000,  900000,
0240              1300000, 1500000, 1800000, 2000000 },
0241     [SMB347] = {  700000,  900000, 1200000, 1500000,
0242              1800000, 2000000, 2200000, 2500000 },
0243     [SMB358] = {  200000,  450000,  600000,  900000,
0244              1300000, 1500000, 1800000, 2000000 },
0245 };
0246 /* Pre-charge current in uA */
0247 static const unsigned int pcc_tbl[NUM_CHIP_TYPES][4] = {
0248     [SMB345] = { 150000, 250000, 350000, 450000 },
0249     [SMB347] = { 100000, 150000, 200000, 250000 },
0250     [SMB358] = { 150000, 250000, 350000, 450000 },
0251 };
0252 
0253 /* Termination current in uA */
0254 static const unsigned int tc_tbl[NUM_CHIP_TYPES][8] = {
0255     [SMB345] = {  30000,  40000,  60000,  80000,
0256              100000, 125000, 150000, 200000 },
0257     [SMB347] = {  37500,  50000, 100000, 150000,
0258              200000, 250000, 500000, 600000 },
0259     [SMB358] = {  30000,  40000,  60000,  80000,
0260              100000, 125000, 150000, 200000 },
0261 };
0262 
0263 /* Input current limit in uA */
0264 static const unsigned int icl_tbl[NUM_CHIP_TYPES][10] = {
0265     [SMB345] = {  300000,  500000,  700000, 1000000, 1500000,
0266              1800000, 2000000, 2000000, 2000000, 2000000 },
0267     [SMB347] = {  300000,  500000,  700000,  900000, 1200000,
0268              1500000, 1800000, 2000000, 2200000, 2500000 },
0269     [SMB358] = {  300000,  500000,  700000, 1000000, 1500000,
0270              1800000, 2000000, 2000000, 2000000, 2000000 },
0271 };
0272 
0273 /* Charge current compensation in uA */
0274 static const unsigned int ccc_tbl[NUM_CHIP_TYPES][4] = {
0275     [SMB345] = {  200000,  450000,  600000,  900000 },
0276     [SMB347] = {  250000,  700000,  900000, 1200000 },
0277     [SMB358] = {  200000,  450000,  600000,  900000 },
0278 };
0279 
0280 /* Convert register value to current using lookup table */
0281 static int hw_to_current(const unsigned int *tbl, size_t size, unsigned int val)
0282 {
0283     if (val >= size)
0284         return -EINVAL;
0285     return tbl[val];
0286 }
0287 
0288 /* Convert current to register value using lookup table */
0289 static int current_to_hw(const unsigned int *tbl, size_t size, unsigned int val)
0290 {
0291     size_t i;
0292 
0293     for (i = 0; i < size; i++)
0294         if (val < tbl[i])
0295             break;
0296     return i > 0 ? i - 1 : -EINVAL;
0297 }
0298 
0299 /**
0300  * smb347_update_ps_status - refreshes the power source status
0301  * @smb: pointer to smb347 charger instance
0302  *
0303  * Function checks whether any power source is connected to the charger and
0304  * updates internal state accordingly. If there is a change to previous state
0305  * function returns %1, otherwise %0 and negative errno in case of errror.
0306  */
0307 static int smb347_update_ps_status(struct smb347_charger *smb)
0308 {
0309     bool usb = false;
0310     bool dc = false;
0311     unsigned int val;
0312     int ret;
0313 
0314     ret = regmap_read(smb->regmap, IRQSTAT_E, &val);
0315     if (ret < 0)
0316         return ret;
0317 
0318     /*
0319      * Dc and usb are set depending on whether they are enabled in
0320      * platform data _and_ whether corresponding undervoltage is set.
0321      */
0322     if (smb->use_mains)
0323         dc = !(val & IRQSTAT_E_DCIN_UV_STAT);
0324     if (smb->use_usb)
0325         usb = !(val & IRQSTAT_E_USBIN_UV_STAT);
0326 
0327     ret = smb->mains_online != dc || smb->usb_online != usb;
0328     smb->mains_online = dc;
0329     smb->usb_online = usb;
0330 
0331     return ret;
0332 }
0333 
0334 /*
0335  * smb347_is_ps_online - returns whether input power source is connected
0336  * @smb: pointer to smb347 charger instance
0337  *
0338  * Returns %true if input power source is connected. Note that this is
0339  * dependent on what platform has configured for usable power sources. For
0340  * example if USB is disabled, this will return %false even if the USB cable
0341  * is connected.
0342  */
0343 static bool smb347_is_ps_online(struct smb347_charger *smb)
0344 {
0345     return smb->usb_online || smb->mains_online;
0346 }
0347 
0348 /**
0349  * smb347_charging_status - returns status of charging
0350  * @smb: pointer to smb347 charger instance
0351  *
0352  * Function returns charging status. %0 means no charging is in progress,
0353  * %1 means pre-charging, %2 fast-charging and %3 taper-charging.
0354  */
0355 static int smb347_charging_status(struct smb347_charger *smb)
0356 {
0357     unsigned int val;
0358     int ret;
0359 
0360     if (!smb347_is_ps_online(smb))
0361         return 0;
0362 
0363     ret = regmap_read(smb->regmap, STAT_C, &val);
0364     if (ret < 0)
0365         return 0;
0366 
0367     return (val & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT;
0368 }
0369 
0370 static int smb347_charging_set(struct smb347_charger *smb, bool enable)
0371 {
0372     if (smb->enable_control != SMB3XX_CHG_ENABLE_SW) {
0373         dev_dbg(smb->dev, "charging enable/disable in SW disabled\n");
0374         return 0;
0375     }
0376 
0377     if (enable && smb->usb_vbus_enabled) {
0378         dev_dbg(smb->dev, "charging not enabled because USB is in host mode\n");
0379         return 0;
0380     }
0381 
0382     return regmap_update_bits(smb->regmap, CMD_A, CMD_A_CHG_ENABLED,
0383                   enable ? CMD_A_CHG_ENABLED : 0);
0384 }
0385 
0386 static inline int smb347_charging_enable(struct smb347_charger *smb)
0387 {
0388     return smb347_charging_set(smb, true);
0389 }
0390 
0391 static inline int smb347_charging_disable(struct smb347_charger *smb)
0392 {
0393     return smb347_charging_set(smb, false);
0394 }
0395 
0396 static int smb347_start_stop_charging(struct smb347_charger *smb)
0397 {
0398     int ret;
0399 
0400     /*
0401      * Depending on whether valid power source is connected or not, we
0402      * disable or enable the charging. We do it manually because it
0403      * depends on how the platform has configured the valid inputs.
0404      */
0405     if (smb347_is_ps_online(smb)) {
0406         ret = smb347_charging_enable(smb);
0407         if (ret < 0)
0408             dev_err(smb->dev, "failed to enable charging\n");
0409     } else {
0410         ret = smb347_charging_disable(smb);
0411         if (ret < 0)
0412             dev_err(smb->dev, "failed to disable charging\n");
0413     }
0414 
0415     return ret;
0416 }
0417 
0418 static int smb347_set_charge_current(struct smb347_charger *smb)
0419 {
0420     unsigned int id = smb->id;
0421     int ret;
0422 
0423     if (smb->max_charge_current) {
0424         ret = current_to_hw(fcc_tbl[id], ARRAY_SIZE(fcc_tbl[id]),
0425                     smb->max_charge_current);
0426         if (ret < 0)
0427             return ret;
0428 
0429         ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
0430                      CFG_CHARGE_CURRENT_FCC_MASK,
0431                      ret << CFG_CHARGE_CURRENT_FCC_SHIFT);
0432         if (ret < 0)
0433             return ret;
0434     }
0435 
0436     if (smb->pre_charge_current) {
0437         ret = current_to_hw(pcc_tbl[id], ARRAY_SIZE(pcc_tbl[id]),
0438                     smb->pre_charge_current);
0439         if (ret < 0)
0440             return ret;
0441 
0442         ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
0443                      CFG_CHARGE_CURRENT_PCC_MASK,
0444                      ret << CFG_CHARGE_CURRENT_PCC_SHIFT);
0445         if (ret < 0)
0446             return ret;
0447     }
0448 
0449     if (smb->termination_current) {
0450         ret = current_to_hw(tc_tbl[id], ARRAY_SIZE(tc_tbl[id]),
0451                     smb->termination_current);
0452         if (ret < 0)
0453             return ret;
0454 
0455         ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
0456                      CFG_CHARGE_CURRENT_TC_MASK, ret);
0457         if (ret < 0)
0458             return ret;
0459     }
0460 
0461     return 0;
0462 }
0463 
0464 static int smb347_set_current_limits(struct smb347_charger *smb)
0465 {
0466     unsigned int id = smb->id;
0467     int ret;
0468 
0469     if (smb->mains_current_limit) {
0470         ret = current_to_hw(icl_tbl[id], ARRAY_SIZE(icl_tbl[id]),
0471                     smb->mains_current_limit);
0472         if (ret < 0)
0473             return ret;
0474 
0475         ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT,
0476                      CFG_CURRENT_LIMIT_DC_MASK,
0477                      ret << CFG_CURRENT_LIMIT_DC_SHIFT);
0478         if (ret < 0)
0479             return ret;
0480     }
0481 
0482     if (smb->usb_hc_current_limit) {
0483         ret = current_to_hw(icl_tbl[id], ARRAY_SIZE(icl_tbl[id]),
0484                     smb->usb_hc_current_limit);
0485         if (ret < 0)
0486             return ret;
0487 
0488         ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT,
0489                      CFG_CURRENT_LIMIT_USB_MASK, ret);
0490         if (ret < 0)
0491             return ret;
0492     }
0493 
0494     return 0;
0495 }
0496 
0497 static int smb347_set_voltage_limits(struct smb347_charger *smb)
0498 {
0499     int ret;
0500 
0501     if (smb->pre_to_fast_voltage) {
0502         ret = smb->pre_to_fast_voltage;
0503 
0504         /* uV */
0505         ret = clamp_val(ret, 2400000, 3000000) - 2400000;
0506         ret /= 200000;
0507 
0508         ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE,
0509                 CFG_FLOAT_VOLTAGE_THRESHOLD_MASK,
0510                 ret << CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT);
0511         if (ret < 0)
0512             return ret;
0513     }
0514 
0515     if (smb->max_charge_voltage) {
0516         ret = smb->max_charge_voltage;
0517 
0518         /* uV */
0519         ret = clamp_val(ret, 3500000, 4500000) - 3500000;
0520         ret /= 20000;
0521 
0522         ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE,
0523                      CFG_FLOAT_VOLTAGE_FLOAT_MASK, ret);
0524         if (ret < 0)
0525             return ret;
0526     }
0527 
0528     return 0;
0529 }
0530 
0531 static int smb347_set_temp_limits(struct smb347_charger *smb)
0532 {
0533     unsigned int id = smb->id;
0534     bool enable_therm_monitor = false;
0535     int ret = 0;
0536     int val;
0537 
0538     if (smb->chip_temp_threshold) {
0539         val = smb->chip_temp_threshold;
0540 
0541         /* degree C */
0542         val = clamp_val(val, 100, 130) - 100;
0543         val /= 10;
0544 
0545         ret = regmap_update_bits(smb->regmap, CFG_OTG,
0546                      CFG_OTG_TEMP_THRESHOLD_MASK,
0547                      val << CFG_OTG_TEMP_THRESHOLD_SHIFT);
0548         if (ret < 0)
0549             return ret;
0550     }
0551 
0552     if (smb->soft_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT) {
0553         val = smb->soft_cold_temp_limit;
0554 
0555         val = clamp_val(val, 0, 15);
0556         val /= 5;
0557         /* this goes from higher to lower so invert the value */
0558         val = ~val & 0x3;
0559 
0560         ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
0561                      CFG_TEMP_LIMIT_SOFT_COLD_MASK,
0562                      val << CFG_TEMP_LIMIT_SOFT_COLD_SHIFT);
0563         if (ret < 0)
0564             return ret;
0565 
0566         enable_therm_monitor = true;
0567     }
0568 
0569     if (smb->soft_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT) {
0570         val = smb->soft_hot_temp_limit;
0571 
0572         val = clamp_val(val, 40, 55) - 40;
0573         val /= 5;
0574 
0575         ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
0576                      CFG_TEMP_LIMIT_SOFT_HOT_MASK,
0577                      val << CFG_TEMP_LIMIT_SOFT_HOT_SHIFT);
0578         if (ret < 0)
0579             return ret;
0580 
0581         enable_therm_monitor = true;
0582     }
0583 
0584     if (smb->hard_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT) {
0585         val = smb->hard_cold_temp_limit;
0586 
0587         val = clamp_val(val, -5, 10) + 5;
0588         val /= 5;
0589         /* this goes from higher to lower so invert the value */
0590         val = ~val & 0x3;
0591 
0592         ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
0593                      CFG_TEMP_LIMIT_HARD_COLD_MASK,
0594                      val << CFG_TEMP_LIMIT_HARD_COLD_SHIFT);
0595         if (ret < 0)
0596             return ret;
0597 
0598         enable_therm_monitor = true;
0599     }
0600 
0601     if (smb->hard_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT) {
0602         val = smb->hard_hot_temp_limit;
0603 
0604         val = clamp_val(val, 50, 65) - 50;
0605         val /= 5;
0606 
0607         ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
0608                      CFG_TEMP_LIMIT_HARD_HOT_MASK,
0609                      val << CFG_TEMP_LIMIT_HARD_HOT_SHIFT);
0610         if (ret < 0)
0611             return ret;
0612 
0613         enable_therm_monitor = true;
0614     }
0615 
0616     /*
0617      * If any of the temperature limits are set, we also enable the
0618      * thermistor monitoring.
0619      *
0620      * When soft limits are hit, the device will start to compensate
0621      * current and/or voltage depending on the configuration.
0622      *
0623      * When hard limit is hit, the device will suspend charging
0624      * depending on the configuration.
0625      */
0626     if (enable_therm_monitor) {
0627         ret = regmap_update_bits(smb->regmap, CFG_THERM,
0628                      CFG_THERM_MONITOR_DISABLED, 0);
0629         if (ret < 0)
0630             return ret;
0631     }
0632 
0633     if (smb->suspend_on_hard_temp_limit) {
0634         ret = regmap_update_bits(smb->regmap, CFG_SYSOK,
0635                  CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED, 0);
0636         if (ret < 0)
0637             return ret;
0638     }
0639 
0640     if (smb->soft_temp_limit_compensation !=
0641         SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT) {
0642         val = smb->soft_temp_limit_compensation & 0x3;
0643 
0644         ret = regmap_update_bits(smb->regmap, CFG_THERM,
0645                  CFG_THERM_SOFT_HOT_COMPENSATION_MASK,
0646                  val << CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT);
0647         if (ret < 0)
0648             return ret;
0649 
0650         ret = regmap_update_bits(smb->regmap, CFG_THERM,
0651                  CFG_THERM_SOFT_COLD_COMPENSATION_MASK,
0652                  val << CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT);
0653         if (ret < 0)
0654             return ret;
0655     }
0656 
0657     if (smb->charge_current_compensation) {
0658         val = current_to_hw(ccc_tbl[id], ARRAY_SIZE(ccc_tbl[id]),
0659                     smb->charge_current_compensation);
0660         if (val < 0)
0661             return val;
0662 
0663         ret = regmap_update_bits(smb->regmap, CFG_OTG,
0664                 CFG_OTG_CC_COMPENSATION_MASK,
0665                 (val & 0x3) << CFG_OTG_CC_COMPENSATION_SHIFT);
0666         if (ret < 0)
0667             return ret;
0668     }
0669 
0670     return ret;
0671 }
0672 
0673 /*
0674  * smb347_set_writable - enables/disables writing to non-volatile registers
0675  * @smb: pointer to smb347 charger instance
0676  *
0677  * You can enable/disable writing to the non-volatile configuration
0678  * registers by calling this function.
0679  *
0680  * Returns %0 on success and negative errno in case of failure.
0681  */
0682 static int smb347_set_writable(struct smb347_charger *smb, bool writable,
0683                    bool irq_toggle)
0684 {
0685     struct i2c_client *client = to_i2c_client(smb->dev);
0686     int ret;
0687 
0688     if (writable && irq_toggle && !smb->irq_unsupported)
0689         disable_irq(client->irq);
0690 
0691     ret = regmap_update_bits(smb->regmap, CMD_A, CMD_A_ALLOW_WRITE,
0692                  writable ? CMD_A_ALLOW_WRITE : 0);
0693 
0694     if ((!writable || ret) && irq_toggle && !smb->irq_unsupported)
0695         enable_irq(client->irq);
0696 
0697     return ret;
0698 }
0699 
0700 static int smb347_hw_init(struct smb347_charger *smb)
0701 {
0702     unsigned int val;
0703     int ret;
0704 
0705     ret = smb347_set_writable(smb, true, false);
0706     if (ret < 0)
0707         return ret;
0708 
0709     /*
0710      * Program the platform specific configuration values to the device
0711      * first.
0712      */
0713     ret = smb347_set_charge_current(smb);
0714     if (ret < 0)
0715         goto fail;
0716 
0717     ret = smb347_set_current_limits(smb);
0718     if (ret < 0)
0719         goto fail;
0720 
0721     ret = smb347_set_voltage_limits(smb);
0722     if (ret < 0)
0723         goto fail;
0724 
0725     ret = smb347_set_temp_limits(smb);
0726     if (ret < 0)
0727         goto fail;
0728 
0729     /* If USB charging is disabled we put the USB in suspend mode */
0730     if (!smb->use_usb) {
0731         ret = regmap_update_bits(smb->regmap, CMD_A,
0732                      CMD_A_SUSPEND_ENABLED,
0733                      CMD_A_SUSPEND_ENABLED);
0734         if (ret < 0)
0735             goto fail;
0736     }
0737 
0738     /*
0739      * If configured by platform data, we enable hardware Auto-OTG
0740      * support for driving VBUS. Otherwise we disable it.
0741      */
0742     ret = regmap_update_bits(smb->regmap, CFG_OTHER, CFG_OTHER_RID_MASK,
0743         smb->use_usb_otg ? CFG_OTHER_RID_ENABLED_AUTO_OTG : 0);
0744     if (ret < 0)
0745         goto fail;
0746 
0747     /* Activate pin control, making it writable. */
0748     switch (smb->enable_control) {
0749     case SMB3XX_CHG_ENABLE_PIN_ACTIVE_LOW:
0750     case SMB3XX_CHG_ENABLE_PIN_ACTIVE_HIGH:
0751         ret = regmap_set_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CTRL);
0752         if (ret < 0)
0753             goto fail;
0754     }
0755 
0756     /*
0757      * Make the charging functionality controllable by a write to the
0758      * command register unless pin control is specified in the platform
0759      * data.
0760      */
0761     switch (smb->enable_control) {
0762     case SMB3XX_CHG_ENABLE_PIN_ACTIVE_LOW:
0763         val = CFG_PIN_EN_CTRL_ACTIVE_LOW;
0764         break;
0765     case SMB3XX_CHG_ENABLE_PIN_ACTIVE_HIGH:
0766         val = CFG_PIN_EN_CTRL_ACTIVE_HIGH;
0767         break;
0768     default:
0769         val = 0;
0770         break;
0771     }
0772 
0773     ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CTRL_MASK,
0774                  val);
0775     if (ret < 0)
0776         goto fail;
0777 
0778     /* Disable Automatic Power Source Detection (APSD) interrupt. */
0779     ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_APSD_IRQ, 0);
0780     if (ret < 0)
0781         goto fail;
0782 
0783     ret = smb347_update_ps_status(smb);
0784     if (ret < 0)
0785         goto fail;
0786 
0787     ret = smb347_start_stop_charging(smb);
0788 
0789 fail:
0790     smb347_set_writable(smb, false, false);
0791     return ret;
0792 }
0793 
0794 static irqreturn_t smb347_interrupt(int irq, void *data)
0795 {
0796     struct smb347_charger *smb = data;
0797     unsigned int stat_c, irqstat_c, irqstat_d, irqstat_e;
0798     bool handled = false;
0799     int ret;
0800 
0801     /* SMB347 it needs at least 20ms for setting IRQSTAT_E_*IN_UV_IRQ */
0802     usleep_range(25000, 35000);
0803 
0804     ret = regmap_read(smb->regmap, STAT_C, &stat_c);
0805     if (ret < 0) {
0806         dev_warn(smb->dev, "reading STAT_C failed\n");
0807         return IRQ_NONE;
0808     }
0809 
0810     ret = regmap_read(smb->regmap, IRQSTAT_C, &irqstat_c);
0811     if (ret < 0) {
0812         dev_warn(smb->dev, "reading IRQSTAT_C failed\n");
0813         return IRQ_NONE;
0814     }
0815 
0816     ret = regmap_read(smb->regmap, IRQSTAT_D, &irqstat_d);
0817     if (ret < 0) {
0818         dev_warn(smb->dev, "reading IRQSTAT_D failed\n");
0819         return IRQ_NONE;
0820     }
0821 
0822     ret = regmap_read(smb->regmap, IRQSTAT_E, &irqstat_e);
0823     if (ret < 0) {
0824         dev_warn(smb->dev, "reading IRQSTAT_E failed\n");
0825         return IRQ_NONE;
0826     }
0827 
0828     /*
0829      * If we get charger error we report the error back to user.
0830      * If the error is recovered charging will resume again.
0831      */
0832     if (stat_c & STAT_C_CHARGER_ERROR) {
0833         dev_err(smb->dev, "charging stopped due to charger error\n");
0834         if (smb->use_mains)
0835             power_supply_changed(smb->mains);
0836         if (smb->use_usb)
0837             power_supply_changed(smb->usb);
0838         handled = true;
0839     }
0840 
0841     /*
0842      * If we reached the termination current the battery is charged and
0843      * we can update the status now. Charging is automatically
0844      * disabled by the hardware.
0845      */
0846     if (irqstat_c & (IRQSTAT_C_TERMINATION_IRQ | IRQSTAT_C_TAPER_IRQ)) {
0847         if (irqstat_c & IRQSTAT_C_TERMINATION_STAT) {
0848             if (smb->use_mains)
0849                 power_supply_changed(smb->mains);
0850             if (smb->use_usb)
0851                 power_supply_changed(smb->usb);
0852         }
0853         dev_dbg(smb->dev, "going to HW maintenance mode\n");
0854         handled = true;
0855     }
0856 
0857     /*
0858      * If we got a charger timeout INT that means the charge
0859      * full is not detected with in charge timeout value.
0860      */
0861     if (irqstat_d & IRQSTAT_D_CHARGE_TIMEOUT_IRQ) {
0862         dev_dbg(smb->dev, "total Charge Timeout INT received\n");
0863 
0864         if (irqstat_d & IRQSTAT_D_CHARGE_TIMEOUT_STAT)
0865             dev_warn(smb->dev, "charging stopped due to timeout\n");
0866         if (smb->use_mains)
0867             power_supply_changed(smb->mains);
0868         if (smb->use_usb)
0869             power_supply_changed(smb->usb);
0870         handled = true;
0871     }
0872 
0873     /*
0874      * If we got an under voltage interrupt it means that AC/USB input
0875      * was connected or disconnected.
0876      */
0877     if (irqstat_e & (IRQSTAT_E_USBIN_UV_IRQ | IRQSTAT_E_DCIN_UV_IRQ)) {
0878         if (smb347_update_ps_status(smb) > 0) {
0879             smb347_start_stop_charging(smb);
0880             if (smb->use_mains)
0881                 power_supply_changed(smb->mains);
0882             if (smb->use_usb)
0883                 power_supply_changed(smb->usb);
0884         }
0885         handled = true;
0886     }
0887 
0888     return handled ? IRQ_HANDLED : IRQ_NONE;
0889 }
0890 
0891 static int smb347_irq_set(struct smb347_charger *smb, bool enable)
0892 {
0893     int ret;
0894 
0895     if (smb->irq_unsupported)
0896         return 0;
0897 
0898     ret = smb347_set_writable(smb, true, true);
0899     if (ret < 0)
0900         return ret;
0901 
0902     /*
0903      * Enable/disable interrupts for:
0904      *  - under voltage
0905      *  - termination current reached
0906      *  - charger timeout
0907      *  - charger error
0908      */
0909     ret = regmap_update_bits(smb->regmap, CFG_FAULT_IRQ, 0xff,
0910                  enable ? CFG_FAULT_IRQ_DCIN_UV : 0);
0911     if (ret < 0)
0912         goto fail;
0913 
0914     ret = regmap_update_bits(smb->regmap, CFG_STATUS_IRQ, 0xff,
0915             enable ? (CFG_STATUS_IRQ_TERMINATION_OR_TAPER |
0916                     CFG_STATUS_IRQ_CHARGE_TIMEOUT) : 0);
0917     if (ret < 0)
0918         goto fail;
0919 
0920     ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CHARGER_ERROR,
0921                  enable ? CFG_PIN_EN_CHARGER_ERROR : 0);
0922 fail:
0923     smb347_set_writable(smb, false, true);
0924     return ret;
0925 }
0926 
0927 static inline int smb347_irq_enable(struct smb347_charger *smb)
0928 {
0929     return smb347_irq_set(smb, true);
0930 }
0931 
0932 static inline int smb347_irq_disable(struct smb347_charger *smb)
0933 {
0934     return smb347_irq_set(smb, false);
0935 }
0936 
0937 static int smb347_irq_init(struct smb347_charger *smb,
0938                struct i2c_client *client)
0939 {
0940     int ret;
0941 
0942     smb->irq_unsupported = true;
0943 
0944     /*
0945      * Interrupt pin is optional. If it is connected, we setup the
0946      * interrupt support here.
0947      */
0948     if (!client->irq)
0949         return 0;
0950 
0951     ret = smb347_set_writable(smb, true, false);
0952     if (ret < 0)
0953         return ret;
0954 
0955     /*
0956      * Configure the STAT output to be suitable for interrupts: disable
0957      * all other output (except interrupts) and make it active low.
0958      */
0959     ret = regmap_update_bits(smb->regmap, CFG_STAT,
0960                  CFG_STAT_ACTIVE_HIGH | CFG_STAT_DISABLED,
0961                  CFG_STAT_DISABLED);
0962 
0963     smb347_set_writable(smb, false, false);
0964 
0965     if (ret < 0) {
0966         dev_warn(smb->dev, "failed to initialize IRQ: %d\n", ret);
0967         dev_warn(smb->dev, "disabling IRQ support\n");
0968         return 0;
0969     }
0970 
0971     ret = devm_request_threaded_irq(smb->dev, client->irq, NULL,
0972                     smb347_interrupt, IRQF_ONESHOT,
0973                     client->name, smb);
0974     if (ret)
0975         return ret;
0976 
0977     smb->irq_unsupported = false;
0978 
0979     ret = smb347_irq_enable(smb);
0980     if (ret < 0)
0981         return ret;
0982 
0983     return 0;
0984 }
0985 
0986 /*
0987  * Returns the constant charge current programmed
0988  * into the charger in uA.
0989  */
0990 static int get_const_charge_current(struct smb347_charger *smb)
0991 {
0992     unsigned int id = smb->id;
0993     int ret, intval;
0994     unsigned int v;
0995 
0996     if (!smb347_is_ps_online(smb))
0997         return -ENODATA;
0998 
0999     ret = regmap_read(smb->regmap, STAT_B, &v);
1000     if (ret < 0)
1001         return ret;
1002 
1003     /*
1004      * The current value is composition of FCC and PCC values
1005      * and we can detect which table to use from bit 5.
1006      */
1007     if (v & 0x20) {
1008         intval = hw_to_current(fcc_tbl[id],
1009                        ARRAY_SIZE(fcc_tbl[id]), v & 7);
1010     } else {
1011         v >>= 3;
1012         intval = hw_to_current(pcc_tbl[id],
1013                        ARRAY_SIZE(pcc_tbl[id]), v & 7);
1014     }
1015 
1016     return intval;
1017 }
1018 
1019 /*
1020  * Returns the constant charge voltage programmed
1021  * into the charger in uV.
1022  */
1023 static int get_const_charge_voltage(struct smb347_charger *smb)
1024 {
1025     int ret, intval;
1026     unsigned int v;
1027 
1028     if (!smb347_is_ps_online(smb))
1029         return -ENODATA;
1030 
1031     ret = regmap_read(smb->regmap, STAT_A, &v);
1032     if (ret < 0)
1033         return ret;
1034 
1035     v &= STAT_A_FLOAT_VOLTAGE_MASK;
1036     if (v > 0x3d)
1037         v = 0x3d;
1038 
1039     intval = 3500000 + v * 20000;
1040 
1041     return intval;
1042 }
1043 
1044 static int smb347_get_charging_status(struct smb347_charger *smb,
1045                       struct power_supply *psy)
1046 {
1047     int ret, status;
1048     unsigned int val;
1049 
1050     if (psy->desc->type == POWER_SUPPLY_TYPE_USB) {
1051         if (!smb->usb_online)
1052             return POWER_SUPPLY_STATUS_DISCHARGING;
1053     } else {
1054         if (!smb->mains_online)
1055             return POWER_SUPPLY_STATUS_DISCHARGING;
1056     }
1057 
1058     ret = regmap_read(smb->regmap, STAT_C, &val);
1059     if (ret < 0)
1060         return ret;
1061 
1062     if ((val & STAT_C_CHARGER_ERROR) ||
1063             (val & STAT_C_HOLDOFF_STAT)) {
1064         /*
1065          * set to NOT CHARGING upon charger error
1066          * or charging has stopped.
1067          */
1068         status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1069     } else {
1070         if ((val & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT) {
1071             /*
1072              * set to charging if battery is in pre-charge,
1073              * fast charge or taper charging mode.
1074              */
1075             status = POWER_SUPPLY_STATUS_CHARGING;
1076         } else if (val & STAT_C_CHG_TERM) {
1077             /*
1078              * set the status to FULL if battery is not in pre
1079              * charge, fast charge or taper charging mode AND
1080              * charging is terminated at least once.
1081              */
1082             status = POWER_SUPPLY_STATUS_FULL;
1083         } else {
1084             /*
1085              * in this case no charger error or termination
1086              * occured but charging is not in progress!!!
1087              */
1088             status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1089         }
1090     }
1091 
1092     return status;
1093 }
1094 
1095 static int smb347_get_property_locked(struct power_supply *psy,
1096                       enum power_supply_property prop,
1097                       union power_supply_propval *val)
1098 {
1099     struct smb347_charger *smb = power_supply_get_drvdata(psy);
1100     int ret;
1101 
1102     switch (prop) {
1103     case POWER_SUPPLY_PROP_STATUS:
1104         ret = smb347_get_charging_status(smb, psy);
1105         if (ret < 0)
1106             return ret;
1107         val->intval = ret;
1108         break;
1109 
1110     case POWER_SUPPLY_PROP_CHARGE_TYPE:
1111         if (psy->desc->type == POWER_SUPPLY_TYPE_USB) {
1112             if (!smb->usb_online)
1113                 return -ENODATA;
1114         } else {
1115             if (!smb->mains_online)
1116                 return -ENODATA;
1117         }
1118 
1119         /*
1120          * We handle trickle and pre-charging the same, and taper
1121          * and none the same.
1122          */
1123         switch (smb347_charging_status(smb)) {
1124         case 1:
1125             val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1126             break;
1127         case 2:
1128             val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
1129             break;
1130         default:
1131             val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
1132             break;
1133         }
1134         break;
1135 
1136     case POWER_SUPPLY_PROP_ONLINE:
1137         if (psy->desc->type == POWER_SUPPLY_TYPE_USB)
1138             val->intval = smb->usb_online;
1139         else
1140             val->intval = smb->mains_online;
1141         break;
1142 
1143     case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
1144         ret = get_const_charge_voltage(smb);
1145         if (ret < 0)
1146             return ret;
1147         val->intval = ret;
1148         break;
1149 
1150     case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
1151         ret = get_const_charge_current(smb);
1152         if (ret < 0)
1153             return ret;
1154         val->intval = ret;
1155         break;
1156 
1157     default:
1158         return -EINVAL;
1159     }
1160 
1161     return 0;
1162 }
1163 
1164 static int smb347_get_property(struct power_supply *psy,
1165                    enum power_supply_property prop,
1166                    union power_supply_propval *val)
1167 {
1168     struct smb347_charger *smb = power_supply_get_drvdata(psy);
1169     struct i2c_client *client = to_i2c_client(smb->dev);
1170     int ret;
1171 
1172     if (!smb->irq_unsupported)
1173         disable_irq(client->irq);
1174 
1175     ret = smb347_get_property_locked(psy, prop, val);
1176 
1177     if (!smb->irq_unsupported)
1178         enable_irq(client->irq);
1179 
1180     return ret;
1181 }
1182 
1183 static enum power_supply_property smb347_properties[] = {
1184     POWER_SUPPLY_PROP_STATUS,
1185     POWER_SUPPLY_PROP_CHARGE_TYPE,
1186     POWER_SUPPLY_PROP_ONLINE,
1187     POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
1188     POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
1189 };
1190 
1191 static bool smb347_volatile_reg(struct device *dev, unsigned int reg)
1192 {
1193     switch (reg) {
1194     case IRQSTAT_A:
1195     case IRQSTAT_C:
1196     case IRQSTAT_D:
1197     case IRQSTAT_E:
1198     case IRQSTAT_F:
1199     case STAT_A:
1200     case STAT_B:
1201     case STAT_C:
1202     case STAT_E:
1203         return true;
1204     }
1205 
1206     return false;
1207 }
1208 
1209 static bool smb347_readable_reg(struct device *dev, unsigned int reg)
1210 {
1211     switch (reg) {
1212     case CFG_CHARGE_CURRENT:
1213     case CFG_CURRENT_LIMIT:
1214     case CFG_FLOAT_VOLTAGE:
1215     case CFG_STAT:
1216     case CFG_PIN:
1217     case CFG_THERM:
1218     case CFG_SYSOK:
1219     case CFG_OTHER:
1220     case CFG_OTG:
1221     case CFG_TEMP_LIMIT:
1222     case CFG_FAULT_IRQ:
1223     case CFG_STATUS_IRQ:
1224     case CFG_ADDRESS:
1225     case CMD_A:
1226     case CMD_B:
1227     case CMD_C:
1228         return true;
1229     }
1230 
1231     return smb347_volatile_reg(dev, reg);
1232 }
1233 
1234 static void smb347_dt_parse_dev_info(struct smb347_charger *smb)
1235 {
1236     struct device *dev = smb->dev;
1237 
1238     smb->soft_temp_limit_compensation =
1239                     SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT;
1240     /*
1241      * These properties come from the battery info, still we need to
1242      * pre-initialize the values. See smb347_get_battery_info() below.
1243      */
1244     smb->soft_cold_temp_limit = SMB3XX_TEMP_USE_DEFAULT;
1245     smb->hard_cold_temp_limit = SMB3XX_TEMP_USE_DEFAULT;
1246     smb->soft_hot_temp_limit  = SMB3XX_TEMP_USE_DEFAULT;
1247     smb->hard_hot_temp_limit  = SMB3XX_TEMP_USE_DEFAULT;
1248 
1249     /* Charging constraints */
1250     device_property_read_u32(dev, "summit,fast-voltage-threshold-microvolt",
1251                  &smb->pre_to_fast_voltage);
1252     device_property_read_u32(dev, "summit,mains-current-limit-microamp",
1253                  &smb->mains_current_limit);
1254     device_property_read_u32(dev, "summit,usb-current-limit-microamp",
1255                  &smb->usb_hc_current_limit);
1256 
1257     /* For thermometer monitoring */
1258     device_property_read_u32(dev, "summit,chip-temperature-threshold-celsius",
1259                  &smb->chip_temp_threshold);
1260     device_property_read_u32(dev, "summit,soft-compensation-method",
1261                  &smb->soft_temp_limit_compensation);
1262     device_property_read_u32(dev, "summit,charge-current-compensation-microamp",
1263                  &smb->charge_current_compensation);
1264 
1265     /* Supported charging mode */
1266     smb->use_mains = device_property_read_bool(dev, "summit,enable-mains-charging");
1267     smb->use_usb = device_property_read_bool(dev, "summit,enable-usb-charging");
1268     smb->use_usb_otg = device_property_read_bool(dev, "summit,enable-otg-charging");
1269 
1270     /* Select charging control */
1271     device_property_read_u32(dev, "summit,enable-charge-control",
1272                  &smb->enable_control);
1273 
1274     /*
1275      * Polarity of INOK signal indicating presence of external power
1276      * supply connected to the charger.
1277      */
1278     device_property_read_u32(dev, "summit,inok-polarity",
1279                  &smb->inok_polarity);
1280 }
1281 
1282 static int smb347_get_battery_info(struct smb347_charger *smb)
1283 {
1284     struct power_supply_battery_info *info;
1285     struct power_supply *supply;
1286     int err;
1287 
1288     if (smb->mains)
1289         supply = smb->mains;
1290     else
1291         supply = smb->usb;
1292 
1293     err = power_supply_get_battery_info(supply, &info);
1294     if (err == -ENXIO || err == -ENODEV)
1295         return 0;
1296     if (err)
1297         return err;
1298 
1299     if (info->constant_charge_current_max_ua != -EINVAL)
1300         smb->max_charge_current = info->constant_charge_current_max_ua;
1301 
1302     if (info->constant_charge_voltage_max_uv != -EINVAL)
1303         smb->max_charge_voltage = info->constant_charge_voltage_max_uv;
1304 
1305     if (info->precharge_current_ua != -EINVAL)
1306         smb->pre_charge_current = info->precharge_current_ua;
1307 
1308     if (info->charge_term_current_ua != -EINVAL)
1309         smb->termination_current = info->charge_term_current_ua;
1310 
1311     if (info->temp_alert_min != INT_MIN)
1312         smb->soft_cold_temp_limit = info->temp_alert_min;
1313 
1314     if (info->temp_alert_max != INT_MAX)
1315         smb->soft_hot_temp_limit = info->temp_alert_max;
1316 
1317     if (info->temp_min != INT_MIN)
1318         smb->hard_cold_temp_limit = info->temp_min;
1319 
1320     if (info->temp_max != INT_MAX)
1321         smb->hard_hot_temp_limit = info->temp_max;
1322 
1323     /* Suspend when battery temperature is outside hard limits */
1324     if (smb->hard_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT ||
1325         smb->hard_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT)
1326         smb->suspend_on_hard_temp_limit = true;
1327 
1328     return 0;
1329 }
1330 
1331 static int smb347_usb_vbus_get_current_limit(struct regulator_dev *rdev)
1332 {
1333     struct smb347_charger *smb = rdev_get_drvdata(rdev);
1334     unsigned int val;
1335     int ret;
1336 
1337     ret = regmap_read(smb->regmap, CFG_OTG, &val);
1338     if (ret < 0)
1339         return ret;
1340 
1341     /*
1342      * It's unknown what happens if this bit is unset due to lack of
1343      * access to the datasheet, assume it's limit-enable.
1344      */
1345     if (!(val & CFG_OTG_CURRENT_LIMIT_250mA))
1346         return 0;
1347 
1348     return val & CFG_OTG_CURRENT_LIMIT_750mA ? 750000 : 250000;
1349 }
1350 
1351 static int smb347_usb_vbus_set_new_current_limit(struct smb347_charger *smb,
1352                          int max_uA)
1353 {
1354     const unsigned int mask = CFG_OTG_CURRENT_LIMIT_750mA |
1355                   CFG_OTG_CURRENT_LIMIT_250mA;
1356     unsigned int val = CFG_OTG_CURRENT_LIMIT_250mA;
1357     int ret;
1358 
1359     if (max_uA >= 750000)
1360         val |= CFG_OTG_CURRENT_LIMIT_750mA;
1361 
1362     ret = regmap_update_bits(smb->regmap, CFG_OTG, mask, val);
1363     if (ret < 0)
1364         dev_err(smb->dev, "failed to change USB current limit\n");
1365 
1366     return ret;
1367 }
1368 
1369 static int smb347_usb_vbus_set_current_limit(struct regulator_dev *rdev,
1370                          int min_uA, int max_uA)
1371 {
1372     struct smb347_charger *smb = rdev_get_drvdata(rdev);
1373     int ret;
1374 
1375     ret = smb347_set_writable(smb, true, true);
1376     if (ret < 0)
1377         return ret;
1378 
1379     ret = smb347_usb_vbus_set_new_current_limit(smb, max_uA);
1380     smb347_set_writable(smb, false, true);
1381 
1382     return ret;
1383 }
1384 
1385 static int smb347_usb_vbus_regulator_enable(struct regulator_dev *rdev)
1386 {
1387     struct smb347_charger *smb = rdev_get_drvdata(rdev);
1388     int ret, max_uA;
1389 
1390     ret = smb347_set_writable(smb, true, true);
1391     if (ret < 0)
1392         return ret;
1393 
1394     smb347_charging_disable(smb);
1395 
1396     if (device_property_read_bool(&rdev->dev, "summit,needs-inok-toggle")) {
1397         unsigned int sysok = 0;
1398 
1399         if (smb->inok_polarity == SMB3XX_SYSOK_INOK_ACTIVE_LOW)
1400             sysok = CFG_SYSOK_INOK_ACTIVE_HIGH;
1401 
1402         /*
1403          * VBUS won't be powered if INOK is active, so we need to
1404          * manually disable INOK on some platforms.
1405          */
1406         ret = regmap_update_bits(smb->regmap, CFG_SYSOK,
1407                      CFG_SYSOK_INOK_ACTIVE_HIGH, sysok);
1408         if (ret < 0) {
1409             dev_err(smb->dev, "failed to disable INOK\n");
1410             goto done;
1411         }
1412     }
1413 
1414     ret = smb347_usb_vbus_get_current_limit(rdev);
1415     if (ret < 0) {
1416         dev_err(smb->dev, "failed to get USB VBUS current limit\n");
1417         goto done;
1418     }
1419 
1420     max_uA = ret;
1421 
1422     ret = smb347_usb_vbus_set_new_current_limit(smb, 250000);
1423     if (ret < 0) {
1424         dev_err(smb->dev, "failed to preset USB VBUS current limit\n");
1425         goto done;
1426     }
1427 
1428     ret = regmap_set_bits(smb->regmap, CMD_A, CMD_A_OTG_ENABLED);
1429     if (ret < 0) {
1430         dev_err(smb->dev, "failed to enable USB VBUS\n");
1431         goto done;
1432     }
1433 
1434     smb->usb_vbus_enabled = true;
1435 
1436     ret = smb347_usb_vbus_set_new_current_limit(smb, max_uA);
1437     if (ret < 0) {
1438         dev_err(smb->dev, "failed to restore USB VBUS current limit\n");
1439         goto done;
1440     }
1441 done:
1442     smb347_set_writable(smb, false, true);
1443 
1444     return ret;
1445 }
1446 
1447 static int smb347_usb_vbus_regulator_disable(struct regulator_dev *rdev)
1448 {
1449     struct smb347_charger *smb = rdev_get_drvdata(rdev);
1450     int ret;
1451 
1452     ret = smb347_set_writable(smb, true, true);
1453     if (ret < 0)
1454         return ret;
1455 
1456     ret = regmap_clear_bits(smb->regmap, CMD_A, CMD_A_OTG_ENABLED);
1457     if (ret < 0) {
1458         dev_err(smb->dev, "failed to disable USB VBUS\n");
1459         goto done;
1460     }
1461 
1462     smb->usb_vbus_enabled = false;
1463 
1464     if (device_property_read_bool(&rdev->dev, "summit,needs-inok-toggle")) {
1465         unsigned int sysok = 0;
1466 
1467         if (smb->inok_polarity == SMB3XX_SYSOK_INOK_ACTIVE_HIGH)
1468             sysok = CFG_SYSOK_INOK_ACTIVE_HIGH;
1469 
1470         ret = regmap_update_bits(smb->regmap, CFG_SYSOK,
1471                      CFG_SYSOK_INOK_ACTIVE_HIGH, sysok);
1472         if (ret < 0) {
1473             dev_err(smb->dev, "failed to enable INOK\n");
1474             goto done;
1475         }
1476     }
1477 
1478     smb347_start_stop_charging(smb);
1479 done:
1480     smb347_set_writable(smb, false, true);
1481 
1482     return ret;
1483 }
1484 
1485 static const struct regmap_config smb347_regmap = {
1486     .reg_bits   = 8,
1487     .val_bits   = 8,
1488     .max_register   = SMB347_MAX_REGISTER,
1489     .volatile_reg   = smb347_volatile_reg,
1490     .readable_reg   = smb347_readable_reg,
1491     .cache_type = REGCACHE_RBTREE,
1492 };
1493 
1494 static const struct regulator_ops smb347_usb_vbus_regulator_ops = {
1495     .is_enabled = regulator_is_enabled_regmap,
1496     .enable     = smb347_usb_vbus_regulator_enable,
1497     .disable    = smb347_usb_vbus_regulator_disable,
1498     .get_current_limit = smb347_usb_vbus_get_current_limit,
1499     .set_current_limit = smb347_usb_vbus_set_current_limit,
1500 };
1501 
1502 static const struct power_supply_desc smb347_mains_desc = {
1503     .name       = "smb347-mains",
1504     .type       = POWER_SUPPLY_TYPE_MAINS,
1505     .get_property   = smb347_get_property,
1506     .properties = smb347_properties,
1507     .num_properties = ARRAY_SIZE(smb347_properties),
1508 };
1509 
1510 static const struct power_supply_desc smb347_usb_desc = {
1511     .name       = "smb347-usb",
1512     .type       = POWER_SUPPLY_TYPE_USB,
1513     .get_property   = smb347_get_property,
1514     .properties = smb347_properties,
1515     .num_properties = ARRAY_SIZE(smb347_properties),
1516 };
1517 
1518 static const struct regulator_desc smb347_usb_vbus_regulator_desc = {
1519     .name       = "smb347-usb-vbus",
1520     .of_match   = of_match_ptr("usb-vbus"),
1521     .ops        = &smb347_usb_vbus_regulator_ops,
1522     .type       = REGULATOR_VOLTAGE,
1523     .owner      = THIS_MODULE,
1524     .enable_reg = CMD_A,
1525     .enable_mask    = CMD_A_OTG_ENABLED,
1526     .enable_val = CMD_A_OTG_ENABLED,
1527     .fixed_uV   = 5000000,
1528     .n_voltages = 1,
1529 };
1530 
1531 static int smb347_probe(struct i2c_client *client,
1532             const struct i2c_device_id *id)
1533 {
1534     struct power_supply_config mains_usb_cfg = {};
1535     struct regulator_config usb_rdev_cfg = {};
1536     struct device *dev = &client->dev;
1537     struct smb347_charger *smb;
1538     int ret;
1539 
1540     smb = devm_kzalloc(dev, sizeof(*smb), GFP_KERNEL);
1541     if (!smb)
1542         return -ENOMEM;
1543     smb->dev = &client->dev;
1544     smb->id = id->driver_data;
1545     i2c_set_clientdata(client, smb);
1546 
1547     smb347_dt_parse_dev_info(smb);
1548     if (!smb->use_mains && !smb->use_usb)
1549         return -EINVAL;
1550 
1551     smb->regmap = devm_regmap_init_i2c(client, &smb347_regmap);
1552     if (IS_ERR(smb->regmap))
1553         return PTR_ERR(smb->regmap);
1554 
1555     mains_usb_cfg.drv_data = smb;
1556     mains_usb_cfg.of_node = dev->of_node;
1557     if (smb->use_mains) {
1558         smb->mains = devm_power_supply_register(dev, &smb347_mains_desc,
1559                             &mains_usb_cfg);
1560         if (IS_ERR(smb->mains))
1561             return PTR_ERR(smb->mains);
1562     }
1563 
1564     if (smb->use_usb) {
1565         smb->usb = devm_power_supply_register(dev, &smb347_usb_desc,
1566                               &mains_usb_cfg);
1567         if (IS_ERR(smb->usb))
1568             return PTR_ERR(smb->usb);
1569     }
1570 
1571     ret = smb347_get_battery_info(smb);
1572     if (ret)
1573         return ret;
1574 
1575     ret = smb347_hw_init(smb);
1576     if (ret < 0)
1577         return ret;
1578 
1579     ret = smb347_irq_init(smb, client);
1580     if (ret)
1581         return ret;
1582 
1583     usb_rdev_cfg.dev = dev;
1584     usb_rdev_cfg.driver_data = smb;
1585     usb_rdev_cfg.regmap = smb->regmap;
1586 
1587     smb->usb_rdev = devm_regulator_register(dev,
1588                         &smb347_usb_vbus_regulator_desc,
1589                         &usb_rdev_cfg);
1590     if (IS_ERR(smb->usb_rdev)) {
1591         smb347_irq_disable(smb);
1592         return PTR_ERR(smb->usb_rdev);
1593     }
1594 
1595     return 0;
1596 }
1597 
1598 static int smb347_remove(struct i2c_client *client)
1599 {
1600     struct smb347_charger *smb = i2c_get_clientdata(client);
1601 
1602     smb347_usb_vbus_regulator_disable(smb->usb_rdev);
1603     smb347_irq_disable(smb);
1604 
1605     return 0;
1606 }
1607 
1608 static void smb347_shutdown(struct i2c_client *client)
1609 {
1610     smb347_remove(client);
1611 }
1612 
1613 static const struct i2c_device_id smb347_id[] = {
1614     { "smb345", SMB345 },
1615     { "smb347", SMB347 },
1616     { "smb358", SMB358 },
1617     { },
1618 };
1619 MODULE_DEVICE_TABLE(i2c, smb347_id);
1620 
1621 static const struct of_device_id smb3xx_of_match[] = {
1622     { .compatible = "summit,smb345" },
1623     { .compatible = "summit,smb347" },
1624     { .compatible = "summit,smb358" },
1625     { },
1626 };
1627 MODULE_DEVICE_TABLE(of, smb3xx_of_match);
1628 
1629 static struct i2c_driver smb347_driver = {
1630     .driver = {
1631         .name = "smb347",
1632         .of_match_table = smb3xx_of_match,
1633     },
1634     .probe = smb347_probe,
1635     .remove = smb347_remove,
1636     .shutdown = smb347_shutdown,
1637     .id_table = smb347_id,
1638 };
1639 module_i2c_driver(smb347_driver);
1640 
1641 MODULE_AUTHOR("Bruce E. Robertson <bruce.e.robertson@intel.com>");
1642 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1643 MODULE_DESCRIPTION("SMB347 battery charger driver");
1644 MODULE_LICENSE("GPL");