1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Driver for the TI bq24190 battery charger. 4 * 5 * Author: Mark A. Greer <mgreer@animalcreek.com> 6 */ 7 8 #include <linux/mod_devicetable.h> 9 #include <linux/module.h> 10 #include <linux/interrupt.h> 11 #include <linux/delay.h> 12 #include <linux/pm_runtime.h> 13 #include <linux/power_supply.h> 14 #include <linux/power/bq24190_charger.h> 15 #include <linux/regulator/driver.h> 16 #include <linux/regulator/machine.h> 17 #include <linux/workqueue.h> 18 #include <linux/i2c.h> 19 #include <linux/extcon-provider.h> 20 21 #define BQ24190_MANUFACTURER "Texas Instruments" 22 23 #define BQ24190_REG_ISC 0x00 /* Input Source Control */ 24 #define BQ24190_REG_ISC_EN_HIZ_MASK BIT(7) 25 #define BQ24190_REG_ISC_EN_HIZ_SHIFT 7 26 #define BQ24190_REG_ISC_VINDPM_MASK (BIT(6) | BIT(5) | BIT(4) | \ 27 BIT(3)) 28 #define BQ24190_REG_ISC_VINDPM_SHIFT 3 29 #define BQ24190_REG_ISC_IINLIM_MASK (BIT(2) | BIT(1) | BIT(0)) 30 #define BQ24190_REG_ISC_IINLIM_SHIFT 0 31 32 #define BQ24190_REG_POC 0x01 /* Power-On Configuration */ 33 #define BQ24190_REG_POC_RESET_MASK BIT(7) 34 #define BQ24190_REG_POC_RESET_SHIFT 7 35 #define BQ24190_REG_POC_WDT_RESET_MASK BIT(6) 36 #define BQ24190_REG_POC_WDT_RESET_SHIFT 6 37 #define BQ24190_REG_POC_CHG_CONFIG_MASK (BIT(5) | BIT(4)) 38 #define BQ24190_REG_POC_CHG_CONFIG_SHIFT 4 39 #define BQ24190_REG_POC_CHG_CONFIG_DISABLE 0x0 40 #define BQ24190_REG_POC_CHG_CONFIG_CHARGE 0x1 41 #define BQ24190_REG_POC_CHG_CONFIG_OTG 0x2 42 #define BQ24190_REG_POC_CHG_CONFIG_OTG_ALT 0x3 43 #define BQ24190_REG_POC_SYS_MIN_MASK (BIT(3) | BIT(2) | BIT(1)) 44 #define BQ24190_REG_POC_SYS_MIN_SHIFT 1 45 #define BQ24190_REG_POC_SYS_MIN_MIN 3000 46 #define BQ24190_REG_POC_SYS_MIN_MAX 3700 47 #define BQ24190_REG_POC_BOOST_LIM_MASK BIT(0) 48 #define BQ24190_REG_POC_BOOST_LIM_SHIFT 0 49 50 #define BQ24190_REG_CCC 0x02 /* Charge Current Control */ 51 #define BQ24190_REG_CCC_ICHG_MASK (BIT(7) | BIT(6) | BIT(5) | \ 52 BIT(4) | BIT(3) | BIT(2)) 53 #define BQ24190_REG_CCC_ICHG_SHIFT 2 54 #define BQ24190_REG_CCC_FORCE_20PCT_MASK BIT(0) 55 #define BQ24190_REG_CCC_FORCE_20PCT_SHIFT 0 56 57 #define BQ24190_REG_PCTCC 0x03 /* Pre-charge/Termination Current Cntl */ 58 #define BQ24190_REG_PCTCC_IPRECHG_MASK (BIT(7) | BIT(6) | BIT(5) | \ 59 BIT(4)) 60 #define BQ24190_REG_PCTCC_IPRECHG_SHIFT 4 61 #define BQ24190_REG_PCTCC_IPRECHG_MIN 128 62 #define BQ24190_REG_PCTCC_IPRECHG_MAX 2048 63 #define BQ24190_REG_PCTCC_ITERM_MASK (BIT(3) | BIT(2) | BIT(1) | \ 64 BIT(0)) 65 #define BQ24190_REG_PCTCC_ITERM_SHIFT 0 66 #define BQ24190_REG_PCTCC_ITERM_MIN 128 67 #define BQ24190_REG_PCTCC_ITERM_MAX 2048 68 69 #define BQ24190_REG_CVC 0x04 /* Charge Voltage Control */ 70 #define BQ24190_REG_CVC_VREG_MASK (BIT(7) | BIT(6) | BIT(5) | \ 71 BIT(4) | BIT(3) | BIT(2)) 72 #define BQ24190_REG_CVC_VREG_SHIFT 2 73 #define BQ24190_REG_CVC_BATLOWV_MASK BIT(1) 74 #define BQ24190_REG_CVC_BATLOWV_SHIFT 1 75 #define BQ24190_REG_CVC_VRECHG_MASK BIT(0) 76 #define BQ24190_REG_CVC_VRECHG_SHIFT 0 77 78 #define BQ24190_REG_CTTC 0x05 /* Charge Term/Timer Control */ 79 #define BQ24190_REG_CTTC_EN_TERM_MASK BIT(7) 80 #define BQ24190_REG_CTTC_EN_TERM_SHIFT 7 81 #define BQ24190_REG_CTTC_TERM_STAT_MASK BIT(6) 82 #define BQ24190_REG_CTTC_TERM_STAT_SHIFT 6 83 #define BQ24190_REG_CTTC_WATCHDOG_MASK (BIT(5) | BIT(4)) 84 #define BQ24190_REG_CTTC_WATCHDOG_SHIFT 4 85 #define BQ24190_REG_CTTC_EN_TIMER_MASK BIT(3) 86 #define BQ24190_REG_CTTC_EN_TIMER_SHIFT 3 87 #define BQ24190_REG_CTTC_CHG_TIMER_MASK (BIT(2) | BIT(1)) 88 #define BQ24190_REG_CTTC_CHG_TIMER_SHIFT 1 89 #define BQ24190_REG_CTTC_JEITA_ISET_MASK BIT(0) 90 #define BQ24190_REG_CTTC_JEITA_ISET_SHIFT 0 91 92 #define BQ24190_REG_ICTRC 0x06 /* IR Comp/Thermal Regulation Control */ 93 #define BQ24190_REG_ICTRC_BAT_COMP_MASK (BIT(7) | BIT(6) | BIT(5)) 94 #define BQ24190_REG_ICTRC_BAT_COMP_SHIFT 5 95 #define BQ24190_REG_ICTRC_VCLAMP_MASK (BIT(4) | BIT(3) | BIT(2)) 96 #define BQ24190_REG_ICTRC_VCLAMP_SHIFT 2 97 #define BQ24190_REG_ICTRC_TREG_MASK (BIT(1) | BIT(0)) 98 #define BQ24190_REG_ICTRC_TREG_SHIFT 0 99 100 #define BQ24190_REG_MOC 0x07 /* Misc. Operation Control */ 101 #define BQ24190_REG_MOC_DPDM_EN_MASK BIT(7) 102 #define BQ24190_REG_MOC_DPDM_EN_SHIFT 7 103 #define BQ24190_REG_MOC_TMR2X_EN_MASK BIT(6) 104 #define BQ24190_REG_MOC_TMR2X_EN_SHIFT 6 105 #define BQ24190_REG_MOC_BATFET_DISABLE_MASK BIT(5) 106 #define BQ24190_REG_MOC_BATFET_DISABLE_SHIFT 5 107 #define BQ24190_REG_MOC_JEITA_VSET_MASK BIT(4) 108 #define BQ24190_REG_MOC_JEITA_VSET_SHIFT 4 109 #define BQ24190_REG_MOC_INT_MASK_MASK (BIT(1) | BIT(0)) 110 #define BQ24190_REG_MOC_INT_MASK_SHIFT 0 111 112 #define BQ24190_REG_SS 0x08 /* System Status */ 113 #define BQ24190_REG_SS_VBUS_STAT_MASK (BIT(7) | BIT(6)) 114 #define BQ24190_REG_SS_VBUS_STAT_SHIFT 6 115 #define BQ24190_REG_SS_CHRG_STAT_MASK (BIT(5) | BIT(4)) 116 #define BQ24190_REG_SS_CHRG_STAT_SHIFT 4 117 #define BQ24190_REG_SS_DPM_STAT_MASK BIT(3) 118 #define BQ24190_REG_SS_DPM_STAT_SHIFT 3 119 #define BQ24190_REG_SS_PG_STAT_MASK BIT(2) 120 #define BQ24190_REG_SS_PG_STAT_SHIFT 2 121 #define BQ24190_REG_SS_THERM_STAT_MASK BIT(1) 122 #define BQ24190_REG_SS_THERM_STAT_SHIFT 1 123 #define BQ24190_REG_SS_VSYS_STAT_MASK BIT(0) 124 #define BQ24190_REG_SS_VSYS_STAT_SHIFT 0 125 126 #define BQ24190_REG_F 0x09 /* Fault */ 127 #define BQ24190_REG_F_WATCHDOG_FAULT_MASK BIT(7) 128 #define BQ24190_REG_F_WATCHDOG_FAULT_SHIFT 7 129 #define BQ24190_REG_F_BOOST_FAULT_MASK BIT(6) 130 #define BQ24190_REG_F_BOOST_FAULT_SHIFT 6 131 #define BQ24190_REG_F_CHRG_FAULT_MASK (BIT(5) | BIT(4)) 132 #define BQ24190_REG_F_CHRG_FAULT_SHIFT 4 133 #define BQ24190_REG_F_BAT_FAULT_MASK BIT(3) 134 #define BQ24190_REG_F_BAT_FAULT_SHIFT 3 135 #define BQ24190_REG_F_NTC_FAULT_MASK (BIT(2) | BIT(1) | BIT(0)) 136 #define BQ24190_REG_F_NTC_FAULT_SHIFT 0 137 138 #define BQ24190_REG_VPRS 0x0A /* Vendor/Part/Revision Status */ 139 #define BQ24190_REG_VPRS_PN_MASK (BIT(5) | BIT(4) | BIT(3)) 140 #define BQ24190_REG_VPRS_PN_SHIFT 3 141 #define BQ24190_REG_VPRS_PN_24190 0x4 142 #define BQ24190_REG_VPRS_PN_24192 0x5 /* Also 24193, 24196 */ 143 #define BQ24190_REG_VPRS_PN_24192I 0x3 144 #define BQ24190_REG_VPRS_TS_PROFILE_MASK BIT(2) 145 #define BQ24190_REG_VPRS_TS_PROFILE_SHIFT 2 146 #define BQ24190_REG_VPRS_DEV_REG_MASK (BIT(1) | BIT(0)) 147 #define BQ24190_REG_VPRS_DEV_REG_SHIFT 0 148 149 /* 150 * The FAULT register is latched by the bq24190 (except for NTC_FAULT) 151 * so the first read after a fault returns the latched value and subsequent 152 * reads return the current value. In order to return the fault status 153 * to the user, have the interrupt handler save the reg's value and retrieve 154 * it in the appropriate health/status routine. 155 */ 156 struct bq24190_dev_info { 157 struct i2c_client *client; 158 struct device *dev; 159 struct extcon_dev *edev; 160 struct power_supply *charger; 161 struct power_supply *battery; 162 struct delayed_work input_current_limit_work; 163 char model_name[I2C_NAME_SIZE]; 164 bool initialized; 165 bool irq_event; 166 bool otg_vbus_enabled; 167 int charge_type; 168 u16 sys_min; 169 u16 iprechg; 170 u16 iterm; 171 u32 ichg; 172 u32 ichg_max; 173 u32 vreg; 174 u32 vreg_max; 175 struct mutex f_reg_lock; 176 u8 f_reg; 177 u8 ss_reg; 178 u8 watchdog; 179 }; 180 181 static int bq24190_charger_set_charge_type(struct bq24190_dev_info *bdi, 182 const union power_supply_propval *val); 183 184 static const unsigned int bq24190_usb_extcon_cable[] = { 185 EXTCON_USB, 186 EXTCON_NONE, 187 }; 188 189 /* 190 * The tables below provide a 2-way mapping for the value that goes in 191 * the register field and the real-world value that it represents. 192 * The index of the array is the value that goes in the register; the 193 * number at that index in the array is the real-world value that it 194 * represents. 195 */ 196 197 /* REG00[2:0] (IINLIM) in uAh */ 198 static const int bq24190_isc_iinlim_values[] = { 199 100000, 150000, 500000, 900000, 1200000, 1500000, 2000000, 3000000 200 }; 201 202 /* REG02[7:2] (ICHG) in uAh */ 203 static const int bq24190_ccc_ichg_values[] = { 204 512000, 576000, 640000, 704000, 768000, 832000, 896000, 960000, 205 1024000, 1088000, 1152000, 1216000, 1280000, 1344000, 1408000, 1472000, 206 1536000, 1600000, 1664000, 1728000, 1792000, 1856000, 1920000, 1984000, 207 2048000, 2112000, 2176000, 2240000, 2304000, 2368000, 2432000, 2496000, 208 2560000, 2624000, 2688000, 2752000, 2816000, 2880000, 2944000, 3008000, 209 3072000, 3136000, 3200000, 3264000, 3328000, 3392000, 3456000, 3520000, 210 3584000, 3648000, 3712000, 3776000, 3840000, 3904000, 3968000, 4032000, 211 4096000, 4160000, 4224000, 4288000, 4352000, 4416000, 4480000, 4544000 212 }; 213 214 /* REG04[7:2] (VREG) in uV */ 215 static const int bq24190_cvc_vreg_values[] = { 216 3504000, 3520000, 3536000, 3552000, 3568000, 3584000, 3600000, 3616000, 217 3632000, 3648000, 3664000, 3680000, 3696000, 3712000, 3728000, 3744000, 218 3760000, 3776000, 3792000, 3808000, 3824000, 3840000, 3856000, 3872000, 219 3888000, 3904000, 3920000, 3936000, 3952000, 3968000, 3984000, 4000000, 220 4016000, 4032000, 4048000, 4064000, 4080000, 4096000, 4112000, 4128000, 221 4144000, 4160000, 4176000, 4192000, 4208000, 4224000, 4240000, 4256000, 222 4272000, 4288000, 4304000, 4320000, 4336000, 4352000, 4368000, 4384000, 223 4400000 224 }; 225 226 /* REG06[1:0] (TREG) in tenths of degrees Celsius */ 227 static const int bq24190_ictrc_treg_values[] = { 228 600, 800, 1000, 1200 229 }; 230 231 /* 232 * Return the index in 'tbl' of greatest value that is less than or equal to 233 * 'val'. The index range returned is 0 to 'tbl_size' - 1. Assumes that 234 * the values in 'tbl' are sorted from smallest to largest and 'tbl_size' 235 * is less than 2^8. 236 */ 237 static u8 bq24190_find_idx(const int tbl[], int tbl_size, int v) 238 { 239 int i; 240 241 for (i = 1; i < tbl_size; i++) 242 if (v < tbl[i]) 243 break; 244 245 return i - 1; 246 } 247 248 /* Basic driver I/O routines */ 249 250 static int bq24190_read(struct bq24190_dev_info *bdi, u8 reg, u8 *data) 251 { 252 int ret; 253 254 ret = i2c_smbus_read_byte_data(bdi->client, reg); 255 if (ret < 0) 256 return ret; 257 258 *data = ret; 259 return 0; 260 } 261 262 static int bq24190_write(struct bq24190_dev_info *bdi, u8 reg, u8 data) 263 { 264 return i2c_smbus_write_byte_data(bdi->client, reg, data); 265 } 266 267 static int bq24190_read_mask(struct bq24190_dev_info *bdi, u8 reg, 268 u8 mask, u8 shift, u8 *data) 269 { 270 u8 v; 271 int ret; 272 273 ret = bq24190_read(bdi, reg, &v); 274 if (ret < 0) 275 return ret; 276 277 v &= mask; 278 v >>= shift; 279 *data = v; 280 281 return 0; 282 } 283 284 static int bq24190_write_mask(struct bq24190_dev_info *bdi, u8 reg, 285 u8 mask, u8 shift, u8 data) 286 { 287 u8 v; 288 int ret; 289 290 ret = bq24190_read(bdi, reg, &v); 291 if (ret < 0) 292 return ret; 293 294 v &= ~mask; 295 v |= ((data << shift) & mask); 296 297 return bq24190_write(bdi, reg, v); 298 } 299 300 static int bq24190_get_field_val(struct bq24190_dev_info *bdi, 301 u8 reg, u8 mask, u8 shift, 302 const int tbl[], int tbl_size, 303 int *val) 304 { 305 u8 v; 306 int ret; 307 308 ret = bq24190_read_mask(bdi, reg, mask, shift, &v); 309 if (ret < 0) 310 return ret; 311 312 v = (v >= tbl_size) ? (tbl_size - 1) : v; 313 *val = tbl[v]; 314 315 return 0; 316 } 317 318 static int bq24190_set_field_val(struct bq24190_dev_info *bdi, 319 u8 reg, u8 mask, u8 shift, 320 const int tbl[], int tbl_size, 321 int val) 322 { 323 u8 idx; 324 325 idx = bq24190_find_idx(tbl, tbl_size, val); 326 327 return bq24190_write_mask(bdi, reg, mask, shift, idx); 328 } 329 330 #ifdef CONFIG_SYSFS 331 /* 332 * There are a numerous options that are configurable on the bq24190 333 * that go well beyond what the power_supply properties provide access to. 334 * Provide sysfs access to them so they can be examined and possibly modified 335 * on the fly. They will be provided for the charger power_supply object only 336 * and will be prefixed by 'f_' to make them easier to recognize. 337 */ 338 339 #define BQ24190_SYSFS_FIELD(_name, r, f, m, store) \ 340 { \ 341 .attr = __ATTR(f_##_name, m, bq24190_sysfs_show, store), \ 342 .reg = BQ24190_REG_##r, \ 343 .mask = BQ24190_REG_##r##_##f##_MASK, \ 344 .shift = BQ24190_REG_##r##_##f##_SHIFT, \ 345 } 346 347 #define BQ24190_SYSFS_FIELD_RW(_name, r, f) \ 348 BQ24190_SYSFS_FIELD(_name, r, f, S_IWUSR | S_IRUGO, \ 349 bq24190_sysfs_store) 350 351 #define BQ24190_SYSFS_FIELD_RO(_name, r, f) \ 352 BQ24190_SYSFS_FIELD(_name, r, f, S_IRUGO, NULL) 353 354 static ssize_t bq24190_sysfs_show(struct device *dev, 355 struct device_attribute *attr, char *buf); 356 static ssize_t bq24190_sysfs_store(struct device *dev, 357 struct device_attribute *attr, const char *buf, size_t count); 358 359 struct bq24190_sysfs_field_info { 360 struct device_attribute attr; 361 u8 reg; 362 u8 mask; 363 u8 shift; 364 }; 365 366 /* On i386 ptrace-abi.h defines SS that breaks the macro calls below. */ 367 #undef SS 368 369 static struct bq24190_sysfs_field_info bq24190_sysfs_field_tbl[] = { 370 /* sysfs name reg field in reg */ 371 BQ24190_SYSFS_FIELD_RW(en_hiz, ISC, EN_HIZ), 372 BQ24190_SYSFS_FIELD_RW(vindpm, ISC, VINDPM), 373 BQ24190_SYSFS_FIELD_RW(iinlim, ISC, IINLIM), 374 BQ24190_SYSFS_FIELD_RW(chg_config, POC, CHG_CONFIG), 375 BQ24190_SYSFS_FIELD_RW(sys_min, POC, SYS_MIN), 376 BQ24190_SYSFS_FIELD_RW(boost_lim, POC, BOOST_LIM), 377 BQ24190_SYSFS_FIELD_RW(ichg, CCC, ICHG), 378 BQ24190_SYSFS_FIELD_RW(force_20_pct, CCC, FORCE_20PCT), 379 BQ24190_SYSFS_FIELD_RW(iprechg, PCTCC, IPRECHG), 380 BQ24190_SYSFS_FIELD_RW(iterm, PCTCC, ITERM), 381 BQ24190_SYSFS_FIELD_RW(vreg, CVC, VREG), 382 BQ24190_SYSFS_FIELD_RW(batlowv, CVC, BATLOWV), 383 BQ24190_SYSFS_FIELD_RW(vrechg, CVC, VRECHG), 384 BQ24190_SYSFS_FIELD_RW(en_term, CTTC, EN_TERM), 385 BQ24190_SYSFS_FIELD_RW(term_stat, CTTC, TERM_STAT), 386 BQ24190_SYSFS_FIELD_RO(watchdog, CTTC, WATCHDOG), 387 BQ24190_SYSFS_FIELD_RW(en_timer, CTTC, EN_TIMER), 388 BQ24190_SYSFS_FIELD_RW(chg_timer, CTTC, CHG_TIMER), 389 BQ24190_SYSFS_FIELD_RW(jeta_iset, CTTC, JEITA_ISET), 390 BQ24190_SYSFS_FIELD_RW(bat_comp, ICTRC, BAT_COMP), 391 BQ24190_SYSFS_FIELD_RW(vclamp, ICTRC, VCLAMP), 392 BQ24190_SYSFS_FIELD_RW(treg, ICTRC, TREG), 393 BQ24190_SYSFS_FIELD_RW(dpdm_en, MOC, DPDM_EN), 394 BQ24190_SYSFS_FIELD_RW(tmr2x_en, MOC, TMR2X_EN), 395 BQ24190_SYSFS_FIELD_RW(batfet_disable, MOC, BATFET_DISABLE), 396 BQ24190_SYSFS_FIELD_RW(jeita_vset, MOC, JEITA_VSET), 397 BQ24190_SYSFS_FIELD_RO(int_mask, MOC, INT_MASK), 398 BQ24190_SYSFS_FIELD_RO(vbus_stat, SS, VBUS_STAT), 399 BQ24190_SYSFS_FIELD_RO(chrg_stat, SS, CHRG_STAT), 400 BQ24190_SYSFS_FIELD_RO(dpm_stat, SS, DPM_STAT), 401 BQ24190_SYSFS_FIELD_RO(pg_stat, SS, PG_STAT), 402 BQ24190_SYSFS_FIELD_RO(therm_stat, SS, THERM_STAT), 403 BQ24190_SYSFS_FIELD_RO(vsys_stat, SS, VSYS_STAT), 404 BQ24190_SYSFS_FIELD_RO(watchdog_fault, F, WATCHDOG_FAULT), 405 BQ24190_SYSFS_FIELD_RO(boost_fault, F, BOOST_FAULT), 406 BQ24190_SYSFS_FIELD_RO(chrg_fault, F, CHRG_FAULT), 407 BQ24190_SYSFS_FIELD_RO(bat_fault, F, BAT_FAULT), 408 BQ24190_SYSFS_FIELD_RO(ntc_fault, F, NTC_FAULT), 409 BQ24190_SYSFS_FIELD_RO(pn, VPRS, PN), 410 BQ24190_SYSFS_FIELD_RO(ts_profile, VPRS, TS_PROFILE), 411 BQ24190_SYSFS_FIELD_RO(dev_reg, VPRS, DEV_REG), 412 }; 413 414 static struct attribute * 415 bq24190_sysfs_attrs[ARRAY_SIZE(bq24190_sysfs_field_tbl) + 1]; 416 417 ATTRIBUTE_GROUPS(bq24190_sysfs); 418 419 static void bq24190_sysfs_init_attrs(void) 420 { 421 int i, limit = ARRAY_SIZE(bq24190_sysfs_field_tbl); 422 423 for (i = 0; i < limit; i++) 424 bq24190_sysfs_attrs[i] = &bq24190_sysfs_field_tbl[i].attr.attr; 425 426 bq24190_sysfs_attrs[limit] = NULL; /* Has additional entry for this */ 427 } 428 429 static struct bq24190_sysfs_field_info *bq24190_sysfs_field_lookup( 430 const char *name) 431 { 432 int i, limit = ARRAY_SIZE(bq24190_sysfs_field_tbl); 433 434 for (i = 0; i < limit; i++) 435 if (!strcmp(name, bq24190_sysfs_field_tbl[i].attr.attr.name)) 436 break; 437 438 if (i >= limit) 439 return NULL; 440 441 return &bq24190_sysfs_field_tbl[i]; 442 } 443 444 static ssize_t bq24190_sysfs_show(struct device *dev, 445 struct device_attribute *attr, char *buf) 446 { 447 struct power_supply *psy = dev_get_drvdata(dev); 448 struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy); 449 struct bq24190_sysfs_field_info *info; 450 ssize_t count; 451 int ret; 452 u8 v; 453 454 info = bq24190_sysfs_field_lookup(attr->attr.name); 455 if (!info) 456 return -EINVAL; 457 458 ret = pm_runtime_get_sync(bdi->dev); 459 if (ret < 0) { 460 pm_runtime_put_noidle(bdi->dev); 461 return ret; 462 } 463 464 ret = bq24190_read_mask(bdi, info->reg, info->mask, info->shift, &v); 465 if (ret) 466 count = ret; 467 else 468 count = scnprintf(buf, PAGE_SIZE, "%hhx\n", v); 469 470 pm_runtime_mark_last_busy(bdi->dev); 471 pm_runtime_put_autosuspend(bdi->dev); 472 473 return count; 474 } 475 476 static ssize_t bq24190_sysfs_store(struct device *dev, 477 struct device_attribute *attr, const char *buf, size_t count) 478 { 479 struct power_supply *psy = dev_get_drvdata(dev); 480 struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy); 481 struct bq24190_sysfs_field_info *info; 482 int ret; 483 u8 v; 484 485 info = bq24190_sysfs_field_lookup(attr->attr.name); 486 if (!info) 487 return -EINVAL; 488 489 ret = kstrtou8(buf, 0, &v); 490 if (ret < 0) 491 return ret; 492 493 ret = pm_runtime_get_sync(bdi->dev); 494 if (ret < 0) { 495 pm_runtime_put_noidle(bdi->dev); 496 return ret; 497 } 498 499 ret = bq24190_write_mask(bdi, info->reg, info->mask, info->shift, v); 500 if (ret) 501 count = ret; 502 503 pm_runtime_mark_last_busy(bdi->dev); 504 pm_runtime_put_autosuspend(bdi->dev); 505 506 return count; 507 } 508 #endif 509 510 static int bq24190_set_otg_vbus(struct bq24190_dev_info *bdi, bool enable) 511 { 512 union power_supply_propval val = { .intval = bdi->charge_type }; 513 int ret; 514 515 ret = pm_runtime_get_sync(bdi->dev); 516 if (ret < 0) { 517 dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", ret); 518 pm_runtime_put_noidle(bdi->dev); 519 return ret; 520 } 521 522 bdi->otg_vbus_enabled = enable; 523 if (enable) 524 ret = bq24190_write_mask(bdi, BQ24190_REG_POC, 525 BQ24190_REG_POC_CHG_CONFIG_MASK, 526 BQ24190_REG_POC_CHG_CONFIG_SHIFT, 527 BQ24190_REG_POC_CHG_CONFIG_OTG); 528 else 529 ret = bq24190_charger_set_charge_type(bdi, &val); 530 531 pm_runtime_mark_last_busy(bdi->dev); 532 pm_runtime_put_autosuspend(bdi->dev); 533 534 return ret; 535 } 536 537 #ifdef CONFIG_REGULATOR 538 static int bq24190_vbus_enable(struct regulator_dev *dev) 539 { 540 return bq24190_set_otg_vbus(rdev_get_drvdata(dev), true); 541 } 542 543 static int bq24190_vbus_disable(struct regulator_dev *dev) 544 { 545 return bq24190_set_otg_vbus(rdev_get_drvdata(dev), false); 546 } 547 548 static int bq24190_vbus_is_enabled(struct regulator_dev *dev) 549 { 550 struct bq24190_dev_info *bdi = rdev_get_drvdata(dev); 551 int ret; 552 u8 val; 553 554 ret = pm_runtime_get_sync(bdi->dev); 555 if (ret < 0) { 556 dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", ret); 557 pm_runtime_put_noidle(bdi->dev); 558 return ret; 559 } 560 561 ret = bq24190_read_mask(bdi, BQ24190_REG_POC, 562 BQ24190_REG_POC_CHG_CONFIG_MASK, 563 BQ24190_REG_POC_CHG_CONFIG_SHIFT, &val); 564 565 pm_runtime_mark_last_busy(bdi->dev); 566 pm_runtime_put_autosuspend(bdi->dev); 567 568 if (ret) 569 return ret; 570 571 bdi->otg_vbus_enabled = (val == BQ24190_REG_POC_CHG_CONFIG_OTG || 572 val == BQ24190_REG_POC_CHG_CONFIG_OTG_ALT); 573 return bdi->otg_vbus_enabled; 574 } 575 576 static const struct regulator_ops bq24190_vbus_ops = { 577 .enable = bq24190_vbus_enable, 578 .disable = bq24190_vbus_disable, 579 .is_enabled = bq24190_vbus_is_enabled, 580 }; 581 582 static const struct regulator_desc bq24190_vbus_desc = { 583 .name = "usb_otg_vbus", 584 .of_match = "usb-otg-vbus", 585 .type = REGULATOR_VOLTAGE, 586 .owner = THIS_MODULE, 587 .ops = &bq24190_vbus_ops, 588 .fixed_uV = 5000000, 589 .n_voltages = 1, 590 }; 591 592 static const struct regulator_init_data bq24190_vbus_init_data = { 593 .constraints = { 594 .valid_ops_mask = REGULATOR_CHANGE_STATUS, 595 }, 596 }; 597 598 static int bq24190_register_vbus_regulator(struct bq24190_dev_info *bdi) 599 { 600 struct bq24190_platform_data *pdata = bdi->dev->platform_data; 601 struct regulator_config cfg = { }; 602 struct regulator_dev *reg; 603 int ret = 0; 604 605 cfg.dev = bdi->dev; 606 if (pdata && pdata->regulator_init_data) 607 cfg.init_data = pdata->regulator_init_data; 608 else 609 cfg.init_data = &bq24190_vbus_init_data; 610 cfg.driver_data = bdi; 611 reg = devm_regulator_register(bdi->dev, &bq24190_vbus_desc, &cfg); 612 if (IS_ERR(reg)) { 613 ret = PTR_ERR(reg); 614 dev_err(bdi->dev, "Can't register regulator: %d\n", ret); 615 } 616 617 return ret; 618 } 619 #else 620 static int bq24190_register_vbus_regulator(struct bq24190_dev_info *bdi) 621 { 622 return 0; 623 } 624 #endif 625 626 static int bq24190_set_config(struct bq24190_dev_info *bdi) 627 { 628 int ret; 629 u8 v; 630 631 ret = bq24190_read(bdi, BQ24190_REG_CTTC, &v); 632 if (ret < 0) 633 return ret; 634 635 bdi->watchdog = ((v & BQ24190_REG_CTTC_WATCHDOG_MASK) >> 636 BQ24190_REG_CTTC_WATCHDOG_SHIFT); 637 638 /* 639 * According to the "Host Mode and default Mode" section of the 640 * manual, a write to any register causes the bq24190 to switch 641 * from default mode to host mode. It will switch back to default 642 * mode after a WDT timeout unless the WDT is turned off as well. 643 * So, by simply turning off the WDT, we accomplish both with the 644 * same write. 645 */ 646 v &= ~BQ24190_REG_CTTC_WATCHDOG_MASK; 647 648 ret = bq24190_write(bdi, BQ24190_REG_CTTC, v); 649 if (ret < 0) 650 return ret; 651 652 if (bdi->sys_min) { 653 v = bdi->sys_min / 100 - 30; // manual section 9.5.1.2, table 9 654 ret = bq24190_write_mask(bdi, BQ24190_REG_POC, 655 BQ24190_REG_POC_SYS_MIN_MASK, 656 BQ24190_REG_POC_SYS_MIN_SHIFT, 657 v); 658 if (ret < 0) 659 return ret; 660 } 661 662 if (bdi->iprechg) { 663 v = bdi->iprechg / 128 - 1; // manual section 9.5.1.4, table 11 664 ret = bq24190_write_mask(bdi, BQ24190_REG_PCTCC, 665 BQ24190_REG_PCTCC_IPRECHG_MASK, 666 BQ24190_REG_PCTCC_IPRECHG_SHIFT, 667 v); 668 if (ret < 0) 669 return ret; 670 } 671 672 if (bdi->iterm) { 673 v = bdi->iterm / 128 - 1; // manual section 9.5.1.4, table 11 674 ret = bq24190_write_mask(bdi, BQ24190_REG_PCTCC, 675 BQ24190_REG_PCTCC_ITERM_MASK, 676 BQ24190_REG_PCTCC_ITERM_SHIFT, 677 v); 678 if (ret < 0) 679 return ret; 680 } 681 682 if (bdi->ichg) { 683 ret = bq24190_set_field_val(bdi, BQ24190_REG_CCC, 684 BQ24190_REG_CCC_ICHG_MASK, 685 BQ24190_REG_CCC_ICHG_SHIFT, 686 bq24190_ccc_ichg_values, 687 ARRAY_SIZE(bq24190_ccc_ichg_values), 688 bdi->ichg); 689 if (ret < 0) 690 return ret; 691 } 692 693 if (bdi->vreg) { 694 ret = bq24190_set_field_val(bdi, BQ24190_REG_CVC, 695 BQ24190_REG_CVC_VREG_MASK, 696 BQ24190_REG_CVC_VREG_SHIFT, 697 bq24190_cvc_vreg_values, 698 ARRAY_SIZE(bq24190_cvc_vreg_values), 699 bdi->vreg); 700 if (ret < 0) 701 return ret; 702 } 703 704 return 0; 705 } 706 707 static int bq24190_register_reset(struct bq24190_dev_info *bdi) 708 { 709 int ret, limit = 100; 710 u8 v; 711 712 /* 713 * This prop. can be passed on device instantiation from platform code: 714 * struct property_entry pe[] = 715 * { PROPERTY_ENTRY_BOOL("disable-reset"), ... }; 716 * struct i2c_board_info bi = 717 * { .type = "bq24190", .addr = 0x6b, .properties = pe, .irq = irq }; 718 * struct i2c_adapter ad = { ... }; 719 * i2c_add_adapter(&ad); 720 * i2c_new_client_device(&ad, &bi); 721 */ 722 if (device_property_read_bool(bdi->dev, "disable-reset")) 723 return 0; 724 725 /* Reset the registers */ 726 ret = bq24190_write_mask(bdi, BQ24190_REG_POC, 727 BQ24190_REG_POC_RESET_MASK, 728 BQ24190_REG_POC_RESET_SHIFT, 729 0x1); 730 if (ret < 0) 731 return ret; 732 733 /* Reset bit will be cleared by hardware so poll until it is */ 734 do { 735 ret = bq24190_read_mask(bdi, BQ24190_REG_POC, 736 BQ24190_REG_POC_RESET_MASK, 737 BQ24190_REG_POC_RESET_SHIFT, 738 &v); 739 if (ret < 0) 740 return ret; 741 742 if (v == 0) 743 return 0; 744 745 usleep_range(100, 200); 746 } while (--limit); 747 748 return -EIO; 749 } 750 751 /* Charger power supply property routines */ 752 753 static int bq24190_charger_get_charge_type(struct bq24190_dev_info *bdi, 754 union power_supply_propval *val) 755 { 756 u8 v; 757 int type, ret; 758 759 ret = bq24190_read_mask(bdi, BQ24190_REG_POC, 760 BQ24190_REG_POC_CHG_CONFIG_MASK, 761 BQ24190_REG_POC_CHG_CONFIG_SHIFT, 762 &v); 763 if (ret < 0) 764 return ret; 765 766 /* If POC[CHG_CONFIG] (REG01[5:4]) == 0, charge is disabled */ 767 if (!v) { 768 type = POWER_SUPPLY_CHARGE_TYPE_NONE; 769 } else { 770 ret = bq24190_read_mask(bdi, BQ24190_REG_CCC, 771 BQ24190_REG_CCC_FORCE_20PCT_MASK, 772 BQ24190_REG_CCC_FORCE_20PCT_SHIFT, 773 &v); 774 if (ret < 0) 775 return ret; 776 777 type = (v) ? POWER_SUPPLY_CHARGE_TYPE_TRICKLE : 778 POWER_SUPPLY_CHARGE_TYPE_FAST; 779 } 780 781 val->intval = type; 782 783 return 0; 784 } 785 786 static int bq24190_charger_set_charge_type(struct bq24190_dev_info *bdi, 787 const union power_supply_propval *val) 788 { 789 u8 chg_config, force_20pct, en_term; 790 int ret; 791 792 /* 793 * According to the "Termination when REG02[0] = 1" section of 794 * the bq24190 manual, the trickle charge could be less than the 795 * termination current so it recommends turning off the termination 796 * function. 797 * 798 * Note: AFAICT from the datasheet, the user will have to manually 799 * turn off the charging when in 20% mode. If its not turned off, 800 * there could be battery damage. So, use this mode at your own risk. 801 */ 802 switch (val->intval) { 803 case POWER_SUPPLY_CHARGE_TYPE_NONE: 804 chg_config = 0x0; 805 break; 806 case POWER_SUPPLY_CHARGE_TYPE_TRICKLE: 807 chg_config = 0x1; 808 force_20pct = 0x1; 809 en_term = 0x0; 810 break; 811 case POWER_SUPPLY_CHARGE_TYPE_FAST: 812 chg_config = 0x1; 813 force_20pct = 0x0; 814 en_term = 0x1; 815 break; 816 default: 817 return -EINVAL; 818 } 819 820 bdi->charge_type = val->intval; 821 /* 822 * If the 5V Vbus boost regulator is enabled delay setting 823 * the charge-type until its gets disabled. 824 */ 825 if (bdi->otg_vbus_enabled) 826 return 0; 827 828 if (chg_config) { /* Enabling the charger */ 829 ret = bq24190_write_mask(bdi, BQ24190_REG_CCC, 830 BQ24190_REG_CCC_FORCE_20PCT_MASK, 831 BQ24190_REG_CCC_FORCE_20PCT_SHIFT, 832 force_20pct); 833 if (ret < 0) 834 return ret; 835 836 ret = bq24190_write_mask(bdi, BQ24190_REG_CTTC, 837 BQ24190_REG_CTTC_EN_TERM_MASK, 838 BQ24190_REG_CTTC_EN_TERM_SHIFT, 839 en_term); 840 if (ret < 0) 841 return ret; 842 } 843 844 return bq24190_write_mask(bdi, BQ24190_REG_POC, 845 BQ24190_REG_POC_CHG_CONFIG_MASK, 846 BQ24190_REG_POC_CHG_CONFIG_SHIFT, chg_config); 847 } 848 849 static int bq24190_charger_get_health(struct bq24190_dev_info *bdi, 850 union power_supply_propval *val) 851 { 852 u8 v; 853 int health; 854 855 mutex_lock(&bdi->f_reg_lock); 856 v = bdi->f_reg; 857 mutex_unlock(&bdi->f_reg_lock); 858 859 if (v & BQ24190_REG_F_NTC_FAULT_MASK) { 860 switch (v >> BQ24190_REG_F_NTC_FAULT_SHIFT & 0x7) { 861 case 0x1: /* TS1 Cold */ 862 case 0x3: /* TS2 Cold */ 863 case 0x5: /* Both Cold */ 864 health = POWER_SUPPLY_HEALTH_COLD; 865 break; 866 case 0x2: /* TS1 Hot */ 867 case 0x4: /* TS2 Hot */ 868 case 0x6: /* Both Hot */ 869 health = POWER_SUPPLY_HEALTH_OVERHEAT; 870 break; 871 default: 872 health = POWER_SUPPLY_HEALTH_UNKNOWN; 873 } 874 } else if (v & BQ24190_REG_F_BAT_FAULT_MASK) { 875 health = POWER_SUPPLY_HEALTH_OVERVOLTAGE; 876 } else if (v & BQ24190_REG_F_CHRG_FAULT_MASK) { 877 switch (v >> BQ24190_REG_F_CHRG_FAULT_SHIFT & 0x3) { 878 case 0x1: /* Input Fault (VBUS OVP or VBAT<VBUS<3.8V) */ 879 /* 880 * This could be over-voltage or under-voltage 881 * and there's no way to tell which. Instead 882 * of looking foolish and returning 'OVERVOLTAGE' 883 * when its really under-voltage, just return 884 * 'UNSPEC_FAILURE'. 885 */ 886 health = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; 887 break; 888 case 0x2: /* Thermal Shutdown */ 889 health = POWER_SUPPLY_HEALTH_OVERHEAT; 890 break; 891 case 0x3: /* Charge Safety Timer Expiration */ 892 health = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE; 893 break; 894 default: /* prevent compiler warning */ 895 health = -1; 896 } 897 } else if (v & BQ24190_REG_F_BOOST_FAULT_MASK) { 898 /* 899 * This could be over-current or over-voltage but there's 900 * no way to tell which. Return 'OVERVOLTAGE' since there 901 * isn't an 'OVERCURRENT' value defined that we can return 902 * even if it was over-current. 903 */ 904 health = POWER_SUPPLY_HEALTH_OVERVOLTAGE; 905 } else { 906 health = POWER_SUPPLY_HEALTH_GOOD; 907 } 908 909 val->intval = health; 910 911 return 0; 912 } 913 914 static int bq24190_charger_get_online(struct bq24190_dev_info *bdi, 915 union power_supply_propval *val) 916 { 917 u8 pg_stat, batfet_disable; 918 int ret; 919 920 ret = bq24190_read_mask(bdi, BQ24190_REG_SS, 921 BQ24190_REG_SS_PG_STAT_MASK, 922 BQ24190_REG_SS_PG_STAT_SHIFT, &pg_stat); 923 if (ret < 0) 924 return ret; 925 926 ret = bq24190_read_mask(bdi, BQ24190_REG_MOC, 927 BQ24190_REG_MOC_BATFET_DISABLE_MASK, 928 BQ24190_REG_MOC_BATFET_DISABLE_SHIFT, &batfet_disable); 929 if (ret < 0) 930 return ret; 931 932 val->intval = pg_stat && !batfet_disable; 933 934 return 0; 935 } 936 937 static int bq24190_battery_set_online(struct bq24190_dev_info *bdi, 938 const union power_supply_propval *val); 939 static int bq24190_battery_get_status(struct bq24190_dev_info *bdi, 940 union power_supply_propval *val); 941 static int bq24190_battery_get_temp_alert_max(struct bq24190_dev_info *bdi, 942 union power_supply_propval *val); 943 static int bq24190_battery_set_temp_alert_max(struct bq24190_dev_info *bdi, 944 const union power_supply_propval *val); 945 946 static int bq24190_charger_set_online(struct bq24190_dev_info *bdi, 947 const union power_supply_propval *val) 948 { 949 return bq24190_battery_set_online(bdi, val); 950 } 951 952 static int bq24190_charger_get_status(struct bq24190_dev_info *bdi, 953 union power_supply_propval *val) 954 { 955 return bq24190_battery_get_status(bdi, val); 956 } 957 958 static int bq24190_charger_get_temp_alert_max(struct bq24190_dev_info *bdi, 959 union power_supply_propval *val) 960 { 961 return bq24190_battery_get_temp_alert_max(bdi, val); 962 } 963 964 static int bq24190_charger_set_temp_alert_max(struct bq24190_dev_info *bdi, 965 const union power_supply_propval *val) 966 { 967 return bq24190_battery_set_temp_alert_max(bdi, val); 968 } 969 970 static int bq24190_charger_get_precharge(struct bq24190_dev_info *bdi, 971 union power_supply_propval *val) 972 { 973 u8 v; 974 int ret; 975 976 ret = bq24190_read_mask(bdi, BQ24190_REG_PCTCC, 977 BQ24190_REG_PCTCC_IPRECHG_MASK, 978 BQ24190_REG_PCTCC_IPRECHG_SHIFT, &v); 979 if (ret < 0) 980 return ret; 981 982 val->intval = ++v * 128 * 1000; 983 return 0; 984 } 985 986 static int bq24190_charger_get_charge_term(struct bq24190_dev_info *bdi, 987 union power_supply_propval *val) 988 { 989 u8 v; 990 int ret; 991 992 ret = bq24190_read_mask(bdi, BQ24190_REG_PCTCC, 993 BQ24190_REG_PCTCC_ITERM_MASK, 994 BQ24190_REG_PCTCC_ITERM_SHIFT, &v); 995 if (ret < 0) 996 return ret; 997 998 val->intval = ++v * 128 * 1000; 999 return 0; 1000 } 1001 1002 static int bq24190_charger_get_current(struct bq24190_dev_info *bdi, 1003 union power_supply_propval *val) 1004 { 1005 u8 v; 1006 int curr, ret; 1007 1008 ret = bq24190_get_field_val(bdi, BQ24190_REG_CCC, 1009 BQ24190_REG_CCC_ICHG_MASK, BQ24190_REG_CCC_ICHG_SHIFT, 1010 bq24190_ccc_ichg_values, 1011 ARRAY_SIZE(bq24190_ccc_ichg_values), &curr); 1012 if (ret < 0) 1013 return ret; 1014 1015 ret = bq24190_read_mask(bdi, BQ24190_REG_CCC, 1016 BQ24190_REG_CCC_FORCE_20PCT_MASK, 1017 BQ24190_REG_CCC_FORCE_20PCT_SHIFT, &v); 1018 if (ret < 0) 1019 return ret; 1020 1021 /* If FORCE_20PCT is enabled, then current is 20% of ICHG value */ 1022 if (v) 1023 curr /= 5; 1024 1025 val->intval = curr; 1026 return 0; 1027 } 1028 1029 static int bq24190_charger_set_current(struct bq24190_dev_info *bdi, 1030 const union power_supply_propval *val) 1031 { 1032 u8 v; 1033 int ret, curr = val->intval; 1034 1035 ret = bq24190_read_mask(bdi, BQ24190_REG_CCC, 1036 BQ24190_REG_CCC_FORCE_20PCT_MASK, 1037 BQ24190_REG_CCC_FORCE_20PCT_SHIFT, &v); 1038 if (ret < 0) 1039 return ret; 1040 1041 /* If FORCE_20PCT is enabled, have to multiply value passed in by 5 */ 1042 if (v) 1043 curr *= 5; 1044 1045 if (curr > bdi->ichg_max) 1046 return -EINVAL; 1047 1048 ret = bq24190_set_field_val(bdi, BQ24190_REG_CCC, 1049 BQ24190_REG_CCC_ICHG_MASK, BQ24190_REG_CCC_ICHG_SHIFT, 1050 bq24190_ccc_ichg_values, 1051 ARRAY_SIZE(bq24190_ccc_ichg_values), curr); 1052 if (ret < 0) 1053 return ret; 1054 1055 bdi->ichg = curr; 1056 1057 return 0; 1058 } 1059 1060 static int bq24190_charger_get_voltage(struct bq24190_dev_info *bdi, 1061 union power_supply_propval *val) 1062 { 1063 int voltage, ret; 1064 1065 ret = bq24190_get_field_val(bdi, BQ24190_REG_CVC, 1066 BQ24190_REG_CVC_VREG_MASK, BQ24190_REG_CVC_VREG_SHIFT, 1067 bq24190_cvc_vreg_values, 1068 ARRAY_SIZE(bq24190_cvc_vreg_values), &voltage); 1069 if (ret < 0) 1070 return ret; 1071 1072 val->intval = voltage; 1073 return 0; 1074 } 1075 1076 static int bq24190_charger_set_voltage(struct bq24190_dev_info *bdi, 1077 const union power_supply_propval *val) 1078 { 1079 int ret; 1080 1081 if (val->intval > bdi->vreg_max) 1082 return -EINVAL; 1083 1084 ret = bq24190_set_field_val(bdi, BQ24190_REG_CVC, 1085 BQ24190_REG_CVC_VREG_MASK, BQ24190_REG_CVC_VREG_SHIFT, 1086 bq24190_cvc_vreg_values, 1087 ARRAY_SIZE(bq24190_cvc_vreg_values), val->intval); 1088 if (ret < 0) 1089 return ret; 1090 1091 bdi->vreg = val->intval; 1092 1093 return 0; 1094 } 1095 1096 static int bq24190_charger_get_iinlimit(struct bq24190_dev_info *bdi, 1097 union power_supply_propval *val) 1098 { 1099 int iinlimit, ret; 1100 1101 ret = bq24190_get_field_val(bdi, BQ24190_REG_ISC, 1102 BQ24190_REG_ISC_IINLIM_MASK, 1103 BQ24190_REG_ISC_IINLIM_SHIFT, 1104 bq24190_isc_iinlim_values, 1105 ARRAY_SIZE(bq24190_isc_iinlim_values), &iinlimit); 1106 if (ret < 0) 1107 return ret; 1108 1109 val->intval = iinlimit; 1110 return 0; 1111 } 1112 1113 static int bq24190_charger_set_iinlimit(struct bq24190_dev_info *bdi, 1114 const union power_supply_propval *val) 1115 { 1116 return bq24190_set_field_val(bdi, BQ24190_REG_ISC, 1117 BQ24190_REG_ISC_IINLIM_MASK, 1118 BQ24190_REG_ISC_IINLIM_SHIFT, 1119 bq24190_isc_iinlim_values, 1120 ARRAY_SIZE(bq24190_isc_iinlim_values), val->intval); 1121 } 1122 1123 static int bq24190_charger_get_property(struct power_supply *psy, 1124 enum power_supply_property psp, union power_supply_propval *val) 1125 { 1126 struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy); 1127 int ret; 1128 1129 dev_dbg(bdi->dev, "prop: %d\n", psp); 1130 1131 ret = pm_runtime_get_sync(bdi->dev); 1132 if (ret < 0) { 1133 pm_runtime_put_noidle(bdi->dev); 1134 return ret; 1135 } 1136 1137 switch (psp) { 1138 case POWER_SUPPLY_PROP_CHARGE_TYPE: 1139 ret = bq24190_charger_get_charge_type(bdi, val); 1140 break; 1141 case POWER_SUPPLY_PROP_HEALTH: 1142 ret = bq24190_charger_get_health(bdi, val); 1143 break; 1144 case POWER_SUPPLY_PROP_ONLINE: 1145 ret = bq24190_charger_get_online(bdi, val); 1146 break; 1147 case POWER_SUPPLY_PROP_STATUS: 1148 ret = bq24190_charger_get_status(bdi, val); 1149 break; 1150 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: 1151 ret = bq24190_charger_get_temp_alert_max(bdi, val); 1152 break; 1153 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT: 1154 ret = bq24190_charger_get_precharge(bdi, val); 1155 break; 1156 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT: 1157 ret = bq24190_charger_get_charge_term(bdi, val); 1158 break; 1159 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: 1160 ret = bq24190_charger_get_current(bdi, val); 1161 break; 1162 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: 1163 val->intval = bdi->ichg_max; 1164 ret = 0; 1165 break; 1166 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: 1167 ret = bq24190_charger_get_voltage(bdi, val); 1168 break; 1169 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: 1170 val->intval = bdi->vreg_max; 1171 ret = 0; 1172 break; 1173 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: 1174 ret = bq24190_charger_get_iinlimit(bdi, val); 1175 break; 1176 case POWER_SUPPLY_PROP_SCOPE: 1177 val->intval = POWER_SUPPLY_SCOPE_SYSTEM; 1178 ret = 0; 1179 break; 1180 case POWER_SUPPLY_PROP_MODEL_NAME: 1181 val->strval = bdi->model_name; 1182 ret = 0; 1183 break; 1184 case POWER_SUPPLY_PROP_MANUFACTURER: 1185 val->strval = BQ24190_MANUFACTURER; 1186 ret = 0; 1187 break; 1188 default: 1189 ret = -ENODATA; 1190 } 1191 1192 pm_runtime_mark_last_busy(bdi->dev); 1193 pm_runtime_put_autosuspend(bdi->dev); 1194 1195 return ret; 1196 } 1197 1198 static int bq24190_charger_set_property(struct power_supply *psy, 1199 enum power_supply_property psp, 1200 const union power_supply_propval *val) 1201 { 1202 struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy); 1203 int ret; 1204 1205 dev_dbg(bdi->dev, "prop: %d\n", psp); 1206 1207 ret = pm_runtime_get_sync(bdi->dev); 1208 if (ret < 0) { 1209 pm_runtime_put_noidle(bdi->dev); 1210 return ret; 1211 } 1212 1213 switch (psp) { 1214 case POWER_SUPPLY_PROP_ONLINE: 1215 ret = bq24190_charger_set_online(bdi, val); 1216 break; 1217 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: 1218 ret = bq24190_charger_set_temp_alert_max(bdi, val); 1219 break; 1220 case POWER_SUPPLY_PROP_CHARGE_TYPE: 1221 ret = bq24190_charger_set_charge_type(bdi, val); 1222 break; 1223 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: 1224 ret = bq24190_charger_set_current(bdi, val); 1225 break; 1226 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: 1227 ret = bq24190_charger_set_voltage(bdi, val); 1228 break; 1229 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: 1230 ret = bq24190_charger_set_iinlimit(bdi, val); 1231 break; 1232 default: 1233 ret = -EINVAL; 1234 } 1235 1236 pm_runtime_mark_last_busy(bdi->dev); 1237 pm_runtime_put_autosuspend(bdi->dev); 1238 1239 return ret; 1240 } 1241 1242 static int bq24190_charger_property_is_writeable(struct power_supply *psy, 1243 enum power_supply_property psp) 1244 { 1245 switch (psp) { 1246 case POWER_SUPPLY_PROP_ONLINE: 1247 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: 1248 case POWER_SUPPLY_PROP_CHARGE_TYPE: 1249 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: 1250 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: 1251 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: 1252 return 1; 1253 default: 1254 return 0; 1255 } 1256 } 1257 1258 static void bq24190_input_current_limit_work(struct work_struct *work) 1259 { 1260 struct bq24190_dev_info *bdi = 1261 container_of(work, struct bq24190_dev_info, 1262 input_current_limit_work.work); 1263 union power_supply_propval val; 1264 int ret; 1265 1266 ret = power_supply_get_property_from_supplier(bdi->charger, 1267 POWER_SUPPLY_PROP_CURRENT_MAX, 1268 &val); 1269 if (ret) 1270 return; 1271 1272 bq24190_charger_set_property(bdi->charger, 1273 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, 1274 &val); 1275 } 1276 1277 /* Sync the input-current-limit with our parent supply (if we have one) */ 1278 static void bq24190_charger_external_power_changed(struct power_supply *psy) 1279 { 1280 struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy); 1281 1282 /* 1283 * The Power-Good detection may take up to 220ms, sometimes 1284 * the external charger detection is quicker, and the bq24190 will 1285 * reset to iinlim based on its own charger detection (which is not 1286 * hooked up when using external charger detection) resulting in a 1287 * too low default 500mA iinlim. Delay setting the input-current-limit 1288 * for 300ms to avoid this. 1289 */ 1290 queue_delayed_work(system_wq, &bdi->input_current_limit_work, 1291 msecs_to_jiffies(300)); 1292 } 1293 1294 static enum power_supply_property bq24190_charger_properties[] = { 1295 POWER_SUPPLY_PROP_CHARGE_TYPE, 1296 POWER_SUPPLY_PROP_HEALTH, 1297 POWER_SUPPLY_PROP_ONLINE, 1298 POWER_SUPPLY_PROP_STATUS, 1299 POWER_SUPPLY_PROP_TEMP_ALERT_MAX, 1300 POWER_SUPPLY_PROP_PRECHARGE_CURRENT, 1301 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT, 1302 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT, 1303 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, 1304 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, 1305 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, 1306 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, 1307 POWER_SUPPLY_PROP_SCOPE, 1308 POWER_SUPPLY_PROP_MODEL_NAME, 1309 POWER_SUPPLY_PROP_MANUFACTURER, 1310 }; 1311 1312 static char *bq24190_charger_supplied_to[] = { 1313 "main-battery", 1314 }; 1315 1316 static const struct power_supply_desc bq24190_charger_desc = { 1317 .name = "bq24190-charger", 1318 .type = POWER_SUPPLY_TYPE_USB, 1319 .properties = bq24190_charger_properties, 1320 .num_properties = ARRAY_SIZE(bq24190_charger_properties), 1321 .get_property = bq24190_charger_get_property, 1322 .set_property = bq24190_charger_set_property, 1323 .property_is_writeable = bq24190_charger_property_is_writeable, 1324 .external_power_changed = bq24190_charger_external_power_changed, 1325 }; 1326 1327 /* Battery power supply property routines */ 1328 1329 static int bq24190_battery_get_status(struct bq24190_dev_info *bdi, 1330 union power_supply_propval *val) 1331 { 1332 u8 ss_reg, chrg_fault; 1333 int status, ret; 1334 1335 mutex_lock(&bdi->f_reg_lock); 1336 chrg_fault = bdi->f_reg; 1337 mutex_unlock(&bdi->f_reg_lock); 1338 1339 chrg_fault &= BQ24190_REG_F_CHRG_FAULT_MASK; 1340 chrg_fault >>= BQ24190_REG_F_CHRG_FAULT_SHIFT; 1341 1342 ret = bq24190_read(bdi, BQ24190_REG_SS, &ss_reg); 1343 if (ret < 0) 1344 return ret; 1345 1346 /* 1347 * The battery must be discharging when any of these are true: 1348 * - there is no good power source; 1349 * - there is a charge fault. 1350 * Could also be discharging when in "supplement mode" but 1351 * there is no way to tell when its in that mode. 1352 */ 1353 if (!(ss_reg & BQ24190_REG_SS_PG_STAT_MASK) || chrg_fault) { 1354 status = POWER_SUPPLY_STATUS_DISCHARGING; 1355 } else { 1356 ss_reg &= BQ24190_REG_SS_CHRG_STAT_MASK; 1357 ss_reg >>= BQ24190_REG_SS_CHRG_STAT_SHIFT; 1358 1359 switch (ss_reg) { 1360 case 0x0: /* Not Charging */ 1361 status = POWER_SUPPLY_STATUS_NOT_CHARGING; 1362 break; 1363 case 0x1: /* Pre-charge */ 1364 case 0x2: /* Fast Charging */ 1365 status = POWER_SUPPLY_STATUS_CHARGING; 1366 break; 1367 case 0x3: /* Charge Termination Done */ 1368 status = POWER_SUPPLY_STATUS_FULL; 1369 break; 1370 default: 1371 ret = -EIO; 1372 } 1373 } 1374 1375 if (!ret) 1376 val->intval = status; 1377 1378 return ret; 1379 } 1380 1381 static int bq24190_battery_get_health(struct bq24190_dev_info *bdi, 1382 union power_supply_propval *val) 1383 { 1384 u8 v; 1385 int health; 1386 1387 mutex_lock(&bdi->f_reg_lock); 1388 v = bdi->f_reg; 1389 mutex_unlock(&bdi->f_reg_lock); 1390 1391 if (v & BQ24190_REG_F_BAT_FAULT_MASK) { 1392 health = POWER_SUPPLY_HEALTH_OVERVOLTAGE; 1393 } else { 1394 v &= BQ24190_REG_F_NTC_FAULT_MASK; 1395 v >>= BQ24190_REG_F_NTC_FAULT_SHIFT; 1396 1397 switch (v) { 1398 case 0x0: /* Normal */ 1399 health = POWER_SUPPLY_HEALTH_GOOD; 1400 break; 1401 case 0x1: /* TS1 Cold */ 1402 case 0x3: /* TS2 Cold */ 1403 case 0x5: /* Both Cold */ 1404 health = POWER_SUPPLY_HEALTH_COLD; 1405 break; 1406 case 0x2: /* TS1 Hot */ 1407 case 0x4: /* TS2 Hot */ 1408 case 0x6: /* Both Hot */ 1409 health = POWER_SUPPLY_HEALTH_OVERHEAT; 1410 break; 1411 default: 1412 health = POWER_SUPPLY_HEALTH_UNKNOWN; 1413 } 1414 } 1415 1416 val->intval = health; 1417 return 0; 1418 } 1419 1420 static int bq24190_battery_get_online(struct bq24190_dev_info *bdi, 1421 union power_supply_propval *val) 1422 { 1423 u8 batfet_disable; 1424 int ret; 1425 1426 ret = bq24190_read_mask(bdi, BQ24190_REG_MOC, 1427 BQ24190_REG_MOC_BATFET_DISABLE_MASK, 1428 BQ24190_REG_MOC_BATFET_DISABLE_SHIFT, &batfet_disable); 1429 if (ret < 0) 1430 return ret; 1431 1432 val->intval = !batfet_disable; 1433 return 0; 1434 } 1435 1436 static int bq24190_battery_set_online(struct bq24190_dev_info *bdi, 1437 const union power_supply_propval *val) 1438 { 1439 return bq24190_write_mask(bdi, BQ24190_REG_MOC, 1440 BQ24190_REG_MOC_BATFET_DISABLE_MASK, 1441 BQ24190_REG_MOC_BATFET_DISABLE_SHIFT, !val->intval); 1442 } 1443 1444 static int bq24190_battery_get_temp_alert_max(struct bq24190_dev_info *bdi, 1445 union power_supply_propval *val) 1446 { 1447 int temp, ret; 1448 1449 ret = bq24190_get_field_val(bdi, BQ24190_REG_ICTRC, 1450 BQ24190_REG_ICTRC_TREG_MASK, 1451 BQ24190_REG_ICTRC_TREG_SHIFT, 1452 bq24190_ictrc_treg_values, 1453 ARRAY_SIZE(bq24190_ictrc_treg_values), &temp); 1454 if (ret < 0) 1455 return ret; 1456 1457 val->intval = temp; 1458 return 0; 1459 } 1460 1461 static int bq24190_battery_set_temp_alert_max(struct bq24190_dev_info *bdi, 1462 const union power_supply_propval *val) 1463 { 1464 return bq24190_set_field_val(bdi, BQ24190_REG_ICTRC, 1465 BQ24190_REG_ICTRC_TREG_MASK, 1466 BQ24190_REG_ICTRC_TREG_SHIFT, 1467 bq24190_ictrc_treg_values, 1468 ARRAY_SIZE(bq24190_ictrc_treg_values), val->intval); 1469 } 1470 1471 static int bq24190_battery_get_property(struct power_supply *psy, 1472 enum power_supply_property psp, union power_supply_propval *val) 1473 { 1474 struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy); 1475 int ret; 1476 1477 dev_warn(bdi->dev, "warning: /sys/class/power_supply/bq24190-battery is deprecated\n"); 1478 dev_dbg(bdi->dev, "prop: %d\n", psp); 1479 1480 ret = pm_runtime_get_sync(bdi->dev); 1481 if (ret < 0) { 1482 pm_runtime_put_noidle(bdi->dev); 1483 return ret; 1484 } 1485 1486 switch (psp) { 1487 case POWER_SUPPLY_PROP_STATUS: 1488 ret = bq24190_battery_get_status(bdi, val); 1489 break; 1490 case POWER_SUPPLY_PROP_HEALTH: 1491 ret = bq24190_battery_get_health(bdi, val); 1492 break; 1493 case POWER_SUPPLY_PROP_ONLINE: 1494 ret = bq24190_battery_get_online(bdi, val); 1495 break; 1496 case POWER_SUPPLY_PROP_TECHNOLOGY: 1497 /* Could be Li-on or Li-polymer but no way to tell which */ 1498 val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN; 1499 ret = 0; 1500 break; 1501 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: 1502 ret = bq24190_battery_get_temp_alert_max(bdi, val); 1503 break; 1504 case POWER_SUPPLY_PROP_SCOPE: 1505 val->intval = POWER_SUPPLY_SCOPE_SYSTEM; 1506 ret = 0; 1507 break; 1508 default: 1509 ret = -ENODATA; 1510 } 1511 1512 pm_runtime_mark_last_busy(bdi->dev); 1513 pm_runtime_put_autosuspend(bdi->dev); 1514 1515 return ret; 1516 } 1517 1518 static int bq24190_battery_set_property(struct power_supply *psy, 1519 enum power_supply_property psp, 1520 const union power_supply_propval *val) 1521 { 1522 struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy); 1523 int ret; 1524 1525 dev_warn(bdi->dev, "warning: /sys/class/power_supply/bq24190-battery is deprecated\n"); 1526 dev_dbg(bdi->dev, "prop: %d\n", psp); 1527 1528 ret = pm_runtime_get_sync(bdi->dev); 1529 if (ret < 0) { 1530 pm_runtime_put_noidle(bdi->dev); 1531 return ret; 1532 } 1533 1534 switch (psp) { 1535 case POWER_SUPPLY_PROP_ONLINE: 1536 ret = bq24190_battery_set_online(bdi, val); 1537 break; 1538 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: 1539 ret = bq24190_battery_set_temp_alert_max(bdi, val); 1540 break; 1541 default: 1542 ret = -EINVAL; 1543 } 1544 1545 pm_runtime_mark_last_busy(bdi->dev); 1546 pm_runtime_put_autosuspend(bdi->dev); 1547 1548 return ret; 1549 } 1550 1551 static int bq24190_battery_property_is_writeable(struct power_supply *psy, 1552 enum power_supply_property psp) 1553 { 1554 int ret; 1555 1556 switch (psp) { 1557 case POWER_SUPPLY_PROP_ONLINE: 1558 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: 1559 ret = 1; 1560 break; 1561 default: 1562 ret = 0; 1563 } 1564 1565 return ret; 1566 } 1567 1568 static enum power_supply_property bq24190_battery_properties[] = { 1569 POWER_SUPPLY_PROP_STATUS, 1570 POWER_SUPPLY_PROP_HEALTH, 1571 POWER_SUPPLY_PROP_ONLINE, 1572 POWER_SUPPLY_PROP_TECHNOLOGY, 1573 POWER_SUPPLY_PROP_TEMP_ALERT_MAX, 1574 POWER_SUPPLY_PROP_SCOPE, 1575 }; 1576 1577 static const struct power_supply_desc bq24190_battery_desc = { 1578 .name = "bq24190-battery", 1579 .type = POWER_SUPPLY_TYPE_BATTERY, 1580 .properties = bq24190_battery_properties, 1581 .num_properties = ARRAY_SIZE(bq24190_battery_properties), 1582 .get_property = bq24190_battery_get_property, 1583 .set_property = bq24190_battery_set_property, 1584 .property_is_writeable = bq24190_battery_property_is_writeable, 1585 }; 1586 1587 static int bq24190_configure_usb_otg(struct bq24190_dev_info *bdi, u8 ss_reg) 1588 { 1589 bool otg_enabled; 1590 int ret; 1591 1592 otg_enabled = !!(ss_reg & BQ24190_REG_SS_VBUS_STAT_MASK); 1593 ret = extcon_set_state_sync(bdi->edev, EXTCON_USB, otg_enabled); 1594 if (ret < 0) 1595 dev_err(bdi->dev, "Can't set extcon state to %d: %d\n", 1596 otg_enabled, ret); 1597 1598 return ret; 1599 } 1600 1601 static void bq24190_check_status(struct bq24190_dev_info *bdi) 1602 { 1603 const u8 battery_mask_ss = BQ24190_REG_SS_CHRG_STAT_MASK; 1604 const u8 battery_mask_f = BQ24190_REG_F_BAT_FAULT_MASK 1605 | BQ24190_REG_F_NTC_FAULT_MASK; 1606 bool alert_charger = false, alert_battery = false; 1607 u8 ss_reg = 0, f_reg = 0; 1608 int i, ret; 1609 1610 ret = bq24190_read(bdi, BQ24190_REG_SS, &ss_reg); 1611 if (ret < 0) { 1612 dev_err(bdi->dev, "Can't read SS reg: %d\n", ret); 1613 return; 1614 } 1615 1616 i = 0; 1617 do { 1618 ret = bq24190_read(bdi, BQ24190_REG_F, &f_reg); 1619 if (ret < 0) { 1620 dev_err(bdi->dev, "Can't read F reg: %d\n", ret); 1621 return; 1622 } 1623 } while (f_reg && ++i < 2); 1624 1625 /* ignore over/under voltage fault after disconnect */ 1626 if (f_reg == (1 << BQ24190_REG_F_CHRG_FAULT_SHIFT) && 1627 !(ss_reg & BQ24190_REG_SS_PG_STAT_MASK)) 1628 f_reg = 0; 1629 1630 if (f_reg != bdi->f_reg) { 1631 dev_warn(bdi->dev, 1632 "Fault: boost %d, charge %d, battery %d, ntc %d\n", 1633 !!(f_reg & BQ24190_REG_F_BOOST_FAULT_MASK), 1634 !!(f_reg & BQ24190_REG_F_CHRG_FAULT_MASK), 1635 !!(f_reg & BQ24190_REG_F_BAT_FAULT_MASK), 1636 !!(f_reg & BQ24190_REG_F_NTC_FAULT_MASK)); 1637 1638 mutex_lock(&bdi->f_reg_lock); 1639 if ((bdi->f_reg & battery_mask_f) != (f_reg & battery_mask_f)) 1640 alert_battery = true; 1641 if ((bdi->f_reg & ~battery_mask_f) != (f_reg & ~battery_mask_f)) 1642 alert_charger = true; 1643 bdi->f_reg = f_reg; 1644 mutex_unlock(&bdi->f_reg_lock); 1645 } 1646 1647 if (ss_reg != bdi->ss_reg) { 1648 /* 1649 * The device is in host mode so when PG_STAT goes from 1->0 1650 * (i.e., power removed) HIZ needs to be disabled. 1651 */ 1652 if ((bdi->ss_reg & BQ24190_REG_SS_PG_STAT_MASK) && 1653 !(ss_reg & BQ24190_REG_SS_PG_STAT_MASK)) { 1654 ret = bq24190_write_mask(bdi, BQ24190_REG_ISC, 1655 BQ24190_REG_ISC_EN_HIZ_MASK, 1656 BQ24190_REG_ISC_EN_HIZ_SHIFT, 1657 0); 1658 if (ret < 0) 1659 dev_err(bdi->dev, "Can't access ISC reg: %d\n", 1660 ret); 1661 } 1662 1663 if ((bdi->ss_reg & battery_mask_ss) != (ss_reg & battery_mask_ss)) 1664 alert_battery = true; 1665 if ((bdi->ss_reg & ~battery_mask_ss) != (ss_reg & ~battery_mask_ss)) 1666 alert_charger = true; 1667 bdi->ss_reg = ss_reg; 1668 } 1669 1670 if (alert_charger || alert_battery) { 1671 power_supply_changed(bdi->charger); 1672 bq24190_configure_usb_otg(bdi, ss_reg); 1673 } 1674 if (alert_battery && bdi->battery) 1675 power_supply_changed(bdi->battery); 1676 1677 dev_dbg(bdi->dev, "ss_reg: 0x%02x, f_reg: 0x%02x\n", ss_reg, f_reg); 1678 } 1679 1680 static irqreturn_t bq24190_irq_handler_thread(int irq, void *data) 1681 { 1682 struct bq24190_dev_info *bdi = data; 1683 int error; 1684 1685 bdi->irq_event = true; 1686 error = pm_runtime_get_sync(bdi->dev); 1687 if (error < 0) { 1688 dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", error); 1689 pm_runtime_put_noidle(bdi->dev); 1690 return IRQ_NONE; 1691 } 1692 bq24190_check_status(bdi); 1693 pm_runtime_mark_last_busy(bdi->dev); 1694 pm_runtime_put_autosuspend(bdi->dev); 1695 bdi->irq_event = false; 1696 1697 return IRQ_HANDLED; 1698 } 1699 1700 static int bq24190_hw_init(struct bq24190_dev_info *bdi) 1701 { 1702 u8 v; 1703 int ret; 1704 1705 /* First check that the device really is what its supposed to be */ 1706 ret = bq24190_read_mask(bdi, BQ24190_REG_VPRS, 1707 BQ24190_REG_VPRS_PN_MASK, 1708 BQ24190_REG_VPRS_PN_SHIFT, 1709 &v); 1710 if (ret < 0) 1711 return ret; 1712 1713 switch (v) { 1714 case BQ24190_REG_VPRS_PN_24190: 1715 case BQ24190_REG_VPRS_PN_24192: 1716 case BQ24190_REG_VPRS_PN_24192I: 1717 break; 1718 default: 1719 dev_err(bdi->dev, "Error unknown model: 0x%02x\n", v); 1720 return -ENODEV; 1721 } 1722 1723 ret = bq24190_register_reset(bdi); 1724 if (ret < 0) 1725 return ret; 1726 1727 ret = bq24190_set_config(bdi); 1728 if (ret < 0) 1729 return ret; 1730 1731 return bq24190_read(bdi, BQ24190_REG_SS, &bdi->ss_reg); 1732 } 1733 1734 static int bq24190_get_config(struct bq24190_dev_info *bdi) 1735 { 1736 const char * const s = "ti,system-minimum-microvolt"; 1737 struct power_supply_battery_info *info; 1738 int v, idx; 1739 1740 idx = ARRAY_SIZE(bq24190_ccc_ichg_values) - 1; 1741 bdi->ichg_max = bq24190_ccc_ichg_values[idx]; 1742 1743 idx = ARRAY_SIZE(bq24190_cvc_vreg_values) - 1; 1744 bdi->vreg_max = bq24190_cvc_vreg_values[idx]; 1745 1746 if (device_property_read_u32(bdi->dev, s, &v) == 0) { 1747 v /= 1000; 1748 if (v >= BQ24190_REG_POC_SYS_MIN_MIN 1749 && v <= BQ24190_REG_POC_SYS_MIN_MAX) 1750 bdi->sys_min = v; 1751 else 1752 dev_warn(bdi->dev, "invalid value for %s: %u\n", s, v); 1753 } 1754 1755 if (!power_supply_get_battery_info(bdi->charger, &info)) { 1756 v = info->precharge_current_ua / 1000; 1757 if (v >= BQ24190_REG_PCTCC_IPRECHG_MIN 1758 && v <= BQ24190_REG_PCTCC_IPRECHG_MAX) 1759 bdi->iprechg = v; 1760 else 1761 dev_warn(bdi->dev, "invalid value for battery:precharge-current-microamp: %d\n", 1762 v); 1763 1764 v = info->charge_term_current_ua / 1000; 1765 if (v >= BQ24190_REG_PCTCC_ITERM_MIN 1766 && v <= BQ24190_REG_PCTCC_ITERM_MAX) 1767 bdi->iterm = v; 1768 else 1769 dev_warn(bdi->dev, "invalid value for battery:charge-term-current-microamp: %d\n", 1770 v); 1771 1772 /* These are optional, so no warning when not set */ 1773 v = info->constant_charge_current_max_ua; 1774 if (v >= bq24190_ccc_ichg_values[0] && v <= bdi->ichg_max) 1775 bdi->ichg = bdi->ichg_max = v; 1776 1777 v = info->constant_charge_voltage_max_uv; 1778 if (v >= bq24190_cvc_vreg_values[0] && v <= bdi->vreg_max) 1779 bdi->vreg = bdi->vreg_max = v; 1780 } 1781 1782 return 0; 1783 } 1784 1785 static int bq24190_probe(struct i2c_client *client, 1786 const struct i2c_device_id *id) 1787 { 1788 struct i2c_adapter *adapter = client->adapter; 1789 struct device *dev = &client->dev; 1790 struct power_supply_config charger_cfg = {}, battery_cfg = {}; 1791 struct bq24190_dev_info *bdi; 1792 int ret; 1793 1794 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { 1795 dev_err(dev, "No support for SMBUS_BYTE_DATA\n"); 1796 return -ENODEV; 1797 } 1798 1799 bdi = devm_kzalloc(dev, sizeof(*bdi), GFP_KERNEL); 1800 if (!bdi) { 1801 dev_err(dev, "Can't alloc bdi struct\n"); 1802 return -ENOMEM; 1803 } 1804 1805 bdi->client = client; 1806 bdi->dev = dev; 1807 strncpy(bdi->model_name, id->name, I2C_NAME_SIZE); 1808 mutex_init(&bdi->f_reg_lock); 1809 bdi->charge_type = POWER_SUPPLY_CHARGE_TYPE_FAST; 1810 bdi->f_reg = 0; 1811 bdi->ss_reg = BQ24190_REG_SS_VBUS_STAT_MASK; /* impossible state */ 1812 INIT_DELAYED_WORK(&bdi->input_current_limit_work, 1813 bq24190_input_current_limit_work); 1814 1815 i2c_set_clientdata(client, bdi); 1816 1817 if (client->irq <= 0) { 1818 dev_err(dev, "Can't get irq info\n"); 1819 return -EINVAL; 1820 } 1821 1822 bdi->edev = devm_extcon_dev_allocate(dev, bq24190_usb_extcon_cable); 1823 if (IS_ERR(bdi->edev)) 1824 return PTR_ERR(bdi->edev); 1825 1826 ret = devm_extcon_dev_register(dev, bdi->edev); 1827 if (ret < 0) 1828 return ret; 1829 1830 pm_runtime_enable(dev); 1831 pm_runtime_use_autosuspend(dev); 1832 pm_runtime_set_autosuspend_delay(dev, 600); 1833 ret = pm_runtime_get_sync(dev); 1834 if (ret < 0) { 1835 dev_err(dev, "pm_runtime_get failed: %i\n", ret); 1836 goto out_pmrt; 1837 } 1838 1839 #ifdef CONFIG_SYSFS 1840 bq24190_sysfs_init_attrs(); 1841 charger_cfg.attr_grp = bq24190_sysfs_groups; 1842 #endif 1843 1844 charger_cfg.drv_data = bdi; 1845 charger_cfg.of_node = dev->of_node; 1846 charger_cfg.supplied_to = bq24190_charger_supplied_to; 1847 charger_cfg.num_supplicants = ARRAY_SIZE(bq24190_charger_supplied_to); 1848 bdi->charger = power_supply_register(dev, &bq24190_charger_desc, 1849 &charger_cfg); 1850 if (IS_ERR(bdi->charger)) { 1851 dev_err(dev, "Can't register charger\n"); 1852 ret = PTR_ERR(bdi->charger); 1853 goto out_pmrt; 1854 } 1855 1856 /* the battery class is deprecated and will be removed. */ 1857 /* in the interim, this property hides it. */ 1858 if (!device_property_read_bool(dev, "omit-battery-class")) { 1859 battery_cfg.drv_data = bdi; 1860 bdi->battery = power_supply_register(dev, &bq24190_battery_desc, 1861 &battery_cfg); 1862 if (IS_ERR(bdi->battery)) { 1863 dev_err(dev, "Can't register battery\n"); 1864 ret = PTR_ERR(bdi->battery); 1865 goto out_charger; 1866 } 1867 } 1868 1869 ret = bq24190_get_config(bdi); 1870 if (ret < 0) { 1871 dev_err(dev, "Can't get devicetree config\n"); 1872 goto out_charger; 1873 } 1874 1875 ret = bq24190_hw_init(bdi); 1876 if (ret < 0) { 1877 dev_err(dev, "Hardware init failed\n"); 1878 goto out_charger; 1879 } 1880 1881 ret = bq24190_configure_usb_otg(bdi, bdi->ss_reg); 1882 if (ret < 0) 1883 goto out_charger; 1884 1885 bdi->initialized = true; 1886 1887 ret = devm_request_threaded_irq(dev, client->irq, NULL, 1888 bq24190_irq_handler_thread, 1889 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 1890 "bq24190-charger", bdi); 1891 if (ret < 0) { 1892 dev_err(dev, "Can't set up irq handler\n"); 1893 goto out_charger; 1894 } 1895 1896 ret = bq24190_register_vbus_regulator(bdi); 1897 if (ret < 0) 1898 goto out_charger; 1899 1900 enable_irq_wake(client->irq); 1901 1902 pm_runtime_mark_last_busy(dev); 1903 pm_runtime_put_autosuspend(dev); 1904 1905 return 0; 1906 1907 out_charger: 1908 if (!IS_ERR_OR_NULL(bdi->battery)) 1909 power_supply_unregister(bdi->battery); 1910 power_supply_unregister(bdi->charger); 1911 1912 out_pmrt: 1913 pm_runtime_put_sync(dev); 1914 pm_runtime_dont_use_autosuspend(dev); 1915 pm_runtime_disable(dev); 1916 return ret; 1917 } 1918 1919 static int bq24190_remove(struct i2c_client *client) 1920 { 1921 struct bq24190_dev_info *bdi = i2c_get_clientdata(client); 1922 int error; 1923 1924 error = pm_runtime_get_sync(bdi->dev); 1925 if (error < 0) { 1926 dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", error); 1927 pm_runtime_put_noidle(bdi->dev); 1928 } 1929 1930 bq24190_register_reset(bdi); 1931 if (bdi->battery) 1932 power_supply_unregister(bdi->battery); 1933 power_supply_unregister(bdi->charger); 1934 if (error >= 0) 1935 pm_runtime_put_sync(bdi->dev); 1936 pm_runtime_dont_use_autosuspend(bdi->dev); 1937 pm_runtime_disable(bdi->dev); 1938 1939 return 0; 1940 } 1941 1942 static void bq24190_shutdown(struct i2c_client *client) 1943 { 1944 struct bq24190_dev_info *bdi = i2c_get_clientdata(client); 1945 1946 /* Turn off 5V boost regulator on shutdown */ 1947 bq24190_set_otg_vbus(bdi, false); 1948 } 1949 1950 static __maybe_unused int bq24190_runtime_suspend(struct device *dev) 1951 { 1952 struct i2c_client *client = to_i2c_client(dev); 1953 struct bq24190_dev_info *bdi = i2c_get_clientdata(client); 1954 1955 if (!bdi->initialized) 1956 return 0; 1957 1958 dev_dbg(bdi->dev, "%s\n", __func__); 1959 1960 return 0; 1961 } 1962 1963 static __maybe_unused int bq24190_runtime_resume(struct device *dev) 1964 { 1965 struct i2c_client *client = to_i2c_client(dev); 1966 struct bq24190_dev_info *bdi = i2c_get_clientdata(client); 1967 1968 if (!bdi->initialized) 1969 return 0; 1970 1971 if (!bdi->irq_event) { 1972 dev_dbg(bdi->dev, "checking events on possible wakeirq\n"); 1973 bq24190_check_status(bdi); 1974 } 1975 1976 return 0; 1977 } 1978 1979 static __maybe_unused int bq24190_pm_suspend(struct device *dev) 1980 { 1981 struct i2c_client *client = to_i2c_client(dev); 1982 struct bq24190_dev_info *bdi = i2c_get_clientdata(client); 1983 int error; 1984 1985 error = pm_runtime_get_sync(bdi->dev); 1986 if (error < 0) { 1987 dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", error); 1988 pm_runtime_put_noidle(bdi->dev); 1989 } 1990 1991 bq24190_register_reset(bdi); 1992 1993 if (error >= 0) { 1994 pm_runtime_mark_last_busy(bdi->dev); 1995 pm_runtime_put_autosuspend(bdi->dev); 1996 } 1997 1998 return 0; 1999 } 2000 2001 static __maybe_unused int bq24190_pm_resume(struct device *dev) 2002 { 2003 struct i2c_client *client = to_i2c_client(dev); 2004 struct bq24190_dev_info *bdi = i2c_get_clientdata(client); 2005 int error; 2006 2007 bdi->f_reg = 0; 2008 bdi->ss_reg = BQ24190_REG_SS_VBUS_STAT_MASK; /* impossible state */ 2009 2010 error = pm_runtime_get_sync(bdi->dev); 2011 if (error < 0) { 2012 dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", error); 2013 pm_runtime_put_noidle(bdi->dev); 2014 } 2015 2016 bq24190_register_reset(bdi); 2017 bq24190_set_config(bdi); 2018 bq24190_read(bdi, BQ24190_REG_SS, &bdi->ss_reg); 2019 2020 if (error >= 0) { 2021 pm_runtime_mark_last_busy(bdi->dev); 2022 pm_runtime_put_autosuspend(bdi->dev); 2023 } 2024 2025 /* Things may have changed while suspended so alert upper layer */ 2026 power_supply_changed(bdi->charger); 2027 if (bdi->battery) 2028 power_supply_changed(bdi->battery); 2029 2030 return 0; 2031 } 2032 2033 static const struct dev_pm_ops bq24190_pm_ops = { 2034 SET_RUNTIME_PM_OPS(bq24190_runtime_suspend, bq24190_runtime_resume, 2035 NULL) 2036 SET_SYSTEM_SLEEP_PM_OPS(bq24190_pm_suspend, bq24190_pm_resume) 2037 }; 2038 2039 static const struct i2c_device_id bq24190_i2c_ids[] = { 2040 { "bq24190" }, 2041 { "bq24192" }, 2042 { "bq24192i" }, 2043 { "bq24196" }, 2044 { }, 2045 }; 2046 MODULE_DEVICE_TABLE(i2c, bq24190_i2c_ids); 2047 2048 static const struct of_device_id bq24190_of_match[] = { 2049 { .compatible = "ti,bq24190", }, 2050 { .compatible = "ti,bq24192", }, 2051 { .compatible = "ti,bq24192i", }, 2052 { .compatible = "ti,bq24196", }, 2053 { }, 2054 }; 2055 MODULE_DEVICE_TABLE(of, bq24190_of_match); 2056 2057 static struct i2c_driver bq24190_driver = { 2058 .probe = bq24190_probe, 2059 .remove = bq24190_remove, 2060 .shutdown = bq24190_shutdown, 2061 .id_table = bq24190_i2c_ids, 2062 .driver = { 2063 .name = "bq24190-charger", 2064 .pm = &bq24190_pm_ops, 2065 .of_match_table = bq24190_of_match, 2066 }, 2067 }; 2068 module_i2c_driver(bq24190_driver); 2069 2070 MODULE_LICENSE("GPL"); 2071 MODULE_AUTHOR("Mark A. Greer <mgreer@animalcreek.com>"); 2072 MODULE_DESCRIPTION("TI BQ24190 Charger Driver"); 2073