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_resume_and_get(bdi->dev); 459 if (ret < 0) 460 return ret; 461 462 ret = bq24190_read_mask(bdi, info->reg, info->mask, info->shift, &v); 463 if (ret) 464 count = ret; 465 else 466 count = sysfs_emit(buf, "%hhx\n", v); 467 468 pm_runtime_mark_last_busy(bdi->dev); 469 pm_runtime_put_autosuspend(bdi->dev); 470 471 return count; 472 } 473 474 static ssize_t bq24190_sysfs_store(struct device *dev, 475 struct device_attribute *attr, const char *buf, size_t count) 476 { 477 struct power_supply *psy = dev_get_drvdata(dev); 478 struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy); 479 struct bq24190_sysfs_field_info *info; 480 int ret; 481 u8 v; 482 483 info = bq24190_sysfs_field_lookup(attr->attr.name); 484 if (!info) 485 return -EINVAL; 486 487 ret = kstrtou8(buf, 0, &v); 488 if (ret < 0) 489 return ret; 490 491 ret = pm_runtime_resume_and_get(bdi->dev); 492 if (ret < 0) 493 return ret; 494 495 ret = bq24190_write_mask(bdi, info->reg, info->mask, info->shift, v); 496 if (ret) 497 count = ret; 498 499 pm_runtime_mark_last_busy(bdi->dev); 500 pm_runtime_put_autosuspend(bdi->dev); 501 502 return count; 503 } 504 #endif 505 506 static int bq24190_set_otg_vbus(struct bq24190_dev_info *bdi, bool enable) 507 { 508 union power_supply_propval val = { .intval = bdi->charge_type }; 509 int ret; 510 511 ret = pm_runtime_resume_and_get(bdi->dev); 512 if (ret < 0) { 513 dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", ret); 514 return ret; 515 } 516 517 bdi->otg_vbus_enabled = enable; 518 if (enable) 519 ret = bq24190_write_mask(bdi, BQ24190_REG_POC, 520 BQ24190_REG_POC_CHG_CONFIG_MASK, 521 BQ24190_REG_POC_CHG_CONFIG_SHIFT, 522 BQ24190_REG_POC_CHG_CONFIG_OTG); 523 else 524 ret = bq24190_charger_set_charge_type(bdi, &val); 525 526 pm_runtime_mark_last_busy(bdi->dev); 527 pm_runtime_put_autosuspend(bdi->dev); 528 529 return ret; 530 } 531 532 #ifdef CONFIG_REGULATOR 533 static int bq24190_vbus_enable(struct regulator_dev *dev) 534 { 535 return bq24190_set_otg_vbus(rdev_get_drvdata(dev), true); 536 } 537 538 static int bq24190_vbus_disable(struct regulator_dev *dev) 539 { 540 return bq24190_set_otg_vbus(rdev_get_drvdata(dev), false); 541 } 542 543 static int bq24190_vbus_is_enabled(struct regulator_dev *dev) 544 { 545 struct bq24190_dev_info *bdi = rdev_get_drvdata(dev); 546 int ret; 547 u8 val; 548 549 ret = pm_runtime_resume_and_get(bdi->dev); 550 if (ret < 0) { 551 dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", ret); 552 return ret; 553 } 554 555 ret = bq24190_read_mask(bdi, BQ24190_REG_POC, 556 BQ24190_REG_POC_CHG_CONFIG_MASK, 557 BQ24190_REG_POC_CHG_CONFIG_SHIFT, &val); 558 559 pm_runtime_mark_last_busy(bdi->dev); 560 pm_runtime_put_autosuspend(bdi->dev); 561 562 if (ret) 563 return ret; 564 565 bdi->otg_vbus_enabled = (val == BQ24190_REG_POC_CHG_CONFIG_OTG || 566 val == BQ24190_REG_POC_CHG_CONFIG_OTG_ALT); 567 return bdi->otg_vbus_enabled; 568 } 569 570 static const struct regulator_ops bq24190_vbus_ops = { 571 .enable = bq24190_vbus_enable, 572 .disable = bq24190_vbus_disable, 573 .is_enabled = bq24190_vbus_is_enabled, 574 }; 575 576 static const struct regulator_desc bq24190_vbus_desc = { 577 .name = "usb_otg_vbus", 578 .of_match = "usb-otg-vbus", 579 .type = REGULATOR_VOLTAGE, 580 .owner = THIS_MODULE, 581 .ops = &bq24190_vbus_ops, 582 .fixed_uV = 5000000, 583 .n_voltages = 1, 584 }; 585 586 static const struct regulator_init_data bq24190_vbus_init_data = { 587 .constraints = { 588 .valid_ops_mask = REGULATOR_CHANGE_STATUS, 589 }, 590 }; 591 592 static int bq24190_register_vbus_regulator(struct bq24190_dev_info *bdi) 593 { 594 struct bq24190_platform_data *pdata = bdi->dev->platform_data; 595 struct regulator_config cfg = { }; 596 struct regulator_dev *reg; 597 int ret = 0; 598 599 cfg.dev = bdi->dev; 600 if (pdata && pdata->regulator_init_data) 601 cfg.init_data = pdata->regulator_init_data; 602 else 603 cfg.init_data = &bq24190_vbus_init_data; 604 cfg.driver_data = bdi; 605 reg = devm_regulator_register(bdi->dev, &bq24190_vbus_desc, &cfg); 606 if (IS_ERR(reg)) { 607 ret = PTR_ERR(reg); 608 dev_err(bdi->dev, "Can't register regulator: %d\n", ret); 609 } 610 611 return ret; 612 } 613 #else 614 static int bq24190_register_vbus_regulator(struct bq24190_dev_info *bdi) 615 { 616 return 0; 617 } 618 #endif 619 620 static int bq24190_set_config(struct bq24190_dev_info *bdi) 621 { 622 int ret; 623 u8 v; 624 625 ret = bq24190_read(bdi, BQ24190_REG_CTTC, &v); 626 if (ret < 0) 627 return ret; 628 629 bdi->watchdog = ((v & BQ24190_REG_CTTC_WATCHDOG_MASK) >> 630 BQ24190_REG_CTTC_WATCHDOG_SHIFT); 631 632 /* 633 * According to the "Host Mode and default Mode" section of the 634 * manual, a write to any register causes the bq24190 to switch 635 * from default mode to host mode. It will switch back to default 636 * mode after a WDT timeout unless the WDT is turned off as well. 637 * So, by simply turning off the WDT, we accomplish both with the 638 * same write. 639 */ 640 v &= ~BQ24190_REG_CTTC_WATCHDOG_MASK; 641 642 ret = bq24190_write(bdi, BQ24190_REG_CTTC, v); 643 if (ret < 0) 644 return ret; 645 646 if (bdi->sys_min) { 647 v = bdi->sys_min / 100 - 30; // manual section 9.5.1.2, table 9 648 ret = bq24190_write_mask(bdi, BQ24190_REG_POC, 649 BQ24190_REG_POC_SYS_MIN_MASK, 650 BQ24190_REG_POC_SYS_MIN_SHIFT, 651 v); 652 if (ret < 0) 653 return ret; 654 } 655 656 if (bdi->iprechg) { 657 v = bdi->iprechg / 128 - 1; // manual section 9.5.1.4, table 11 658 ret = bq24190_write_mask(bdi, BQ24190_REG_PCTCC, 659 BQ24190_REG_PCTCC_IPRECHG_MASK, 660 BQ24190_REG_PCTCC_IPRECHG_SHIFT, 661 v); 662 if (ret < 0) 663 return ret; 664 } 665 666 if (bdi->iterm) { 667 v = bdi->iterm / 128 - 1; // manual section 9.5.1.4, table 11 668 ret = bq24190_write_mask(bdi, BQ24190_REG_PCTCC, 669 BQ24190_REG_PCTCC_ITERM_MASK, 670 BQ24190_REG_PCTCC_ITERM_SHIFT, 671 v); 672 if (ret < 0) 673 return ret; 674 } 675 676 if (bdi->ichg) { 677 ret = bq24190_set_field_val(bdi, BQ24190_REG_CCC, 678 BQ24190_REG_CCC_ICHG_MASK, 679 BQ24190_REG_CCC_ICHG_SHIFT, 680 bq24190_ccc_ichg_values, 681 ARRAY_SIZE(bq24190_ccc_ichg_values), 682 bdi->ichg); 683 if (ret < 0) 684 return ret; 685 } 686 687 if (bdi->vreg) { 688 ret = bq24190_set_field_val(bdi, BQ24190_REG_CVC, 689 BQ24190_REG_CVC_VREG_MASK, 690 BQ24190_REG_CVC_VREG_SHIFT, 691 bq24190_cvc_vreg_values, 692 ARRAY_SIZE(bq24190_cvc_vreg_values), 693 bdi->vreg); 694 if (ret < 0) 695 return ret; 696 } 697 698 return 0; 699 } 700 701 static int bq24190_register_reset(struct bq24190_dev_info *bdi) 702 { 703 int ret, limit = 100; 704 u8 v; 705 706 /* 707 * This prop. can be passed on device instantiation from platform code: 708 * struct property_entry pe[] = 709 * { PROPERTY_ENTRY_BOOL("disable-reset"), ... }; 710 * struct i2c_board_info bi = 711 * { .type = "bq24190", .addr = 0x6b, .properties = pe, .irq = irq }; 712 * struct i2c_adapter ad = { ... }; 713 * i2c_add_adapter(&ad); 714 * i2c_new_client_device(&ad, &bi); 715 */ 716 if (device_property_read_bool(bdi->dev, "disable-reset")) 717 return 0; 718 719 /* Reset the registers */ 720 ret = bq24190_write_mask(bdi, BQ24190_REG_POC, 721 BQ24190_REG_POC_RESET_MASK, 722 BQ24190_REG_POC_RESET_SHIFT, 723 0x1); 724 if (ret < 0) 725 return ret; 726 727 /* Reset bit will be cleared by hardware so poll until it is */ 728 do { 729 ret = bq24190_read_mask(bdi, BQ24190_REG_POC, 730 BQ24190_REG_POC_RESET_MASK, 731 BQ24190_REG_POC_RESET_SHIFT, 732 &v); 733 if (ret < 0) 734 return ret; 735 736 if (v == 0) 737 return 0; 738 739 usleep_range(100, 200); 740 } while (--limit); 741 742 return -EIO; 743 } 744 745 /* Charger power supply property routines */ 746 747 static int bq24190_charger_get_charge_type(struct bq24190_dev_info *bdi, 748 union power_supply_propval *val) 749 { 750 u8 v; 751 int type, ret; 752 753 ret = bq24190_read_mask(bdi, BQ24190_REG_POC, 754 BQ24190_REG_POC_CHG_CONFIG_MASK, 755 BQ24190_REG_POC_CHG_CONFIG_SHIFT, 756 &v); 757 if (ret < 0) 758 return ret; 759 760 /* If POC[CHG_CONFIG] (REG01[5:4]) == 0, charge is disabled */ 761 if (!v) { 762 type = POWER_SUPPLY_CHARGE_TYPE_NONE; 763 } else { 764 ret = bq24190_read_mask(bdi, BQ24190_REG_CCC, 765 BQ24190_REG_CCC_FORCE_20PCT_MASK, 766 BQ24190_REG_CCC_FORCE_20PCT_SHIFT, 767 &v); 768 if (ret < 0) 769 return ret; 770 771 type = (v) ? POWER_SUPPLY_CHARGE_TYPE_TRICKLE : 772 POWER_SUPPLY_CHARGE_TYPE_FAST; 773 } 774 775 val->intval = type; 776 777 return 0; 778 } 779 780 static int bq24190_charger_set_charge_type(struct bq24190_dev_info *bdi, 781 const union power_supply_propval *val) 782 { 783 u8 chg_config, force_20pct, en_term; 784 int ret; 785 786 /* 787 * According to the "Termination when REG02[0] = 1" section of 788 * the bq24190 manual, the trickle charge could be less than the 789 * termination current so it recommends turning off the termination 790 * function. 791 * 792 * Note: AFAICT from the datasheet, the user will have to manually 793 * turn off the charging when in 20% mode. If its not turned off, 794 * there could be battery damage. So, use this mode at your own risk. 795 */ 796 switch (val->intval) { 797 case POWER_SUPPLY_CHARGE_TYPE_NONE: 798 chg_config = 0x0; 799 break; 800 case POWER_SUPPLY_CHARGE_TYPE_TRICKLE: 801 chg_config = 0x1; 802 force_20pct = 0x1; 803 en_term = 0x0; 804 break; 805 case POWER_SUPPLY_CHARGE_TYPE_FAST: 806 chg_config = 0x1; 807 force_20pct = 0x0; 808 en_term = 0x1; 809 break; 810 default: 811 return -EINVAL; 812 } 813 814 bdi->charge_type = val->intval; 815 /* 816 * If the 5V Vbus boost regulator is enabled delay setting 817 * the charge-type until its gets disabled. 818 */ 819 if (bdi->otg_vbus_enabled) 820 return 0; 821 822 if (chg_config) { /* Enabling the charger */ 823 ret = bq24190_write_mask(bdi, BQ24190_REG_CCC, 824 BQ24190_REG_CCC_FORCE_20PCT_MASK, 825 BQ24190_REG_CCC_FORCE_20PCT_SHIFT, 826 force_20pct); 827 if (ret < 0) 828 return ret; 829 830 ret = bq24190_write_mask(bdi, BQ24190_REG_CTTC, 831 BQ24190_REG_CTTC_EN_TERM_MASK, 832 BQ24190_REG_CTTC_EN_TERM_SHIFT, 833 en_term); 834 if (ret < 0) 835 return ret; 836 } 837 838 return bq24190_write_mask(bdi, BQ24190_REG_POC, 839 BQ24190_REG_POC_CHG_CONFIG_MASK, 840 BQ24190_REG_POC_CHG_CONFIG_SHIFT, chg_config); 841 } 842 843 static int bq24190_charger_get_health(struct bq24190_dev_info *bdi, 844 union power_supply_propval *val) 845 { 846 u8 v; 847 int health; 848 849 mutex_lock(&bdi->f_reg_lock); 850 v = bdi->f_reg; 851 mutex_unlock(&bdi->f_reg_lock); 852 853 if (v & BQ24190_REG_F_NTC_FAULT_MASK) { 854 switch (v >> BQ24190_REG_F_NTC_FAULT_SHIFT & 0x7) { 855 case 0x1: /* TS1 Cold */ 856 case 0x3: /* TS2 Cold */ 857 case 0x5: /* Both Cold */ 858 health = POWER_SUPPLY_HEALTH_COLD; 859 break; 860 case 0x2: /* TS1 Hot */ 861 case 0x4: /* TS2 Hot */ 862 case 0x6: /* Both Hot */ 863 health = POWER_SUPPLY_HEALTH_OVERHEAT; 864 break; 865 default: 866 health = POWER_SUPPLY_HEALTH_UNKNOWN; 867 } 868 } else if (v & BQ24190_REG_F_BAT_FAULT_MASK) { 869 health = POWER_SUPPLY_HEALTH_OVERVOLTAGE; 870 } else if (v & BQ24190_REG_F_CHRG_FAULT_MASK) { 871 switch (v >> BQ24190_REG_F_CHRG_FAULT_SHIFT & 0x3) { 872 case 0x1: /* Input Fault (VBUS OVP or VBAT<VBUS<3.8V) */ 873 /* 874 * This could be over-voltage or under-voltage 875 * and there's no way to tell which. Instead 876 * of looking foolish and returning 'OVERVOLTAGE' 877 * when its really under-voltage, just return 878 * 'UNSPEC_FAILURE'. 879 */ 880 health = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; 881 break; 882 case 0x2: /* Thermal Shutdown */ 883 health = POWER_SUPPLY_HEALTH_OVERHEAT; 884 break; 885 case 0x3: /* Charge Safety Timer Expiration */ 886 health = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE; 887 break; 888 default: /* prevent compiler warning */ 889 health = -1; 890 } 891 } else if (v & BQ24190_REG_F_BOOST_FAULT_MASK) { 892 /* 893 * This could be over-current or over-voltage but there's 894 * no way to tell which. Return 'OVERVOLTAGE' since there 895 * isn't an 'OVERCURRENT' value defined that we can return 896 * even if it was over-current. 897 */ 898 health = POWER_SUPPLY_HEALTH_OVERVOLTAGE; 899 } else { 900 health = POWER_SUPPLY_HEALTH_GOOD; 901 } 902 903 val->intval = health; 904 905 return 0; 906 } 907 908 static int bq24190_charger_get_online(struct bq24190_dev_info *bdi, 909 union power_supply_propval *val) 910 { 911 u8 pg_stat, batfet_disable; 912 int ret; 913 914 ret = bq24190_read_mask(bdi, BQ24190_REG_SS, 915 BQ24190_REG_SS_PG_STAT_MASK, 916 BQ24190_REG_SS_PG_STAT_SHIFT, &pg_stat); 917 if (ret < 0) 918 return ret; 919 920 ret = bq24190_read_mask(bdi, BQ24190_REG_MOC, 921 BQ24190_REG_MOC_BATFET_DISABLE_MASK, 922 BQ24190_REG_MOC_BATFET_DISABLE_SHIFT, &batfet_disable); 923 if (ret < 0) 924 return ret; 925 926 val->intval = pg_stat && !batfet_disable; 927 928 return 0; 929 } 930 931 static int bq24190_battery_set_online(struct bq24190_dev_info *bdi, 932 const union power_supply_propval *val); 933 static int bq24190_battery_get_status(struct bq24190_dev_info *bdi, 934 union power_supply_propval *val); 935 static int bq24190_battery_get_temp_alert_max(struct bq24190_dev_info *bdi, 936 union power_supply_propval *val); 937 static int bq24190_battery_set_temp_alert_max(struct bq24190_dev_info *bdi, 938 const union power_supply_propval *val); 939 940 static int bq24190_charger_set_online(struct bq24190_dev_info *bdi, 941 const union power_supply_propval *val) 942 { 943 return bq24190_battery_set_online(bdi, val); 944 } 945 946 static int bq24190_charger_get_status(struct bq24190_dev_info *bdi, 947 union power_supply_propval *val) 948 { 949 return bq24190_battery_get_status(bdi, val); 950 } 951 952 static int bq24190_charger_get_temp_alert_max(struct bq24190_dev_info *bdi, 953 union power_supply_propval *val) 954 { 955 return bq24190_battery_get_temp_alert_max(bdi, val); 956 } 957 958 static int bq24190_charger_set_temp_alert_max(struct bq24190_dev_info *bdi, 959 const union power_supply_propval *val) 960 { 961 return bq24190_battery_set_temp_alert_max(bdi, val); 962 } 963 964 static int bq24190_charger_get_precharge(struct bq24190_dev_info *bdi, 965 union power_supply_propval *val) 966 { 967 u8 v; 968 int ret; 969 970 ret = bq24190_read_mask(bdi, BQ24190_REG_PCTCC, 971 BQ24190_REG_PCTCC_IPRECHG_MASK, 972 BQ24190_REG_PCTCC_IPRECHG_SHIFT, &v); 973 if (ret < 0) 974 return ret; 975 976 val->intval = ++v * 128 * 1000; 977 return 0; 978 } 979 980 static int bq24190_charger_get_charge_term(struct bq24190_dev_info *bdi, 981 union power_supply_propval *val) 982 { 983 u8 v; 984 int ret; 985 986 ret = bq24190_read_mask(bdi, BQ24190_REG_PCTCC, 987 BQ24190_REG_PCTCC_ITERM_MASK, 988 BQ24190_REG_PCTCC_ITERM_SHIFT, &v); 989 if (ret < 0) 990 return ret; 991 992 val->intval = ++v * 128 * 1000; 993 return 0; 994 } 995 996 static int bq24190_charger_get_current(struct bq24190_dev_info *bdi, 997 union power_supply_propval *val) 998 { 999 u8 v; 1000 int curr, ret; 1001 1002 ret = bq24190_get_field_val(bdi, BQ24190_REG_CCC, 1003 BQ24190_REG_CCC_ICHG_MASK, BQ24190_REG_CCC_ICHG_SHIFT, 1004 bq24190_ccc_ichg_values, 1005 ARRAY_SIZE(bq24190_ccc_ichg_values), &curr); 1006 if (ret < 0) 1007 return ret; 1008 1009 ret = bq24190_read_mask(bdi, BQ24190_REG_CCC, 1010 BQ24190_REG_CCC_FORCE_20PCT_MASK, 1011 BQ24190_REG_CCC_FORCE_20PCT_SHIFT, &v); 1012 if (ret < 0) 1013 return ret; 1014 1015 /* If FORCE_20PCT is enabled, then current is 20% of ICHG value */ 1016 if (v) 1017 curr /= 5; 1018 1019 val->intval = curr; 1020 return 0; 1021 } 1022 1023 static int bq24190_charger_set_current(struct bq24190_dev_info *bdi, 1024 const union power_supply_propval *val) 1025 { 1026 u8 v; 1027 int ret, curr = val->intval; 1028 1029 ret = bq24190_read_mask(bdi, BQ24190_REG_CCC, 1030 BQ24190_REG_CCC_FORCE_20PCT_MASK, 1031 BQ24190_REG_CCC_FORCE_20PCT_SHIFT, &v); 1032 if (ret < 0) 1033 return ret; 1034 1035 /* If FORCE_20PCT is enabled, have to multiply value passed in by 5 */ 1036 if (v) 1037 curr *= 5; 1038 1039 if (curr > bdi->ichg_max) 1040 return -EINVAL; 1041 1042 ret = bq24190_set_field_val(bdi, BQ24190_REG_CCC, 1043 BQ24190_REG_CCC_ICHG_MASK, BQ24190_REG_CCC_ICHG_SHIFT, 1044 bq24190_ccc_ichg_values, 1045 ARRAY_SIZE(bq24190_ccc_ichg_values), curr); 1046 if (ret < 0) 1047 return ret; 1048 1049 bdi->ichg = curr; 1050 1051 return 0; 1052 } 1053 1054 static int bq24190_charger_get_voltage(struct bq24190_dev_info *bdi, 1055 union power_supply_propval *val) 1056 { 1057 int voltage, ret; 1058 1059 ret = bq24190_get_field_val(bdi, BQ24190_REG_CVC, 1060 BQ24190_REG_CVC_VREG_MASK, BQ24190_REG_CVC_VREG_SHIFT, 1061 bq24190_cvc_vreg_values, 1062 ARRAY_SIZE(bq24190_cvc_vreg_values), &voltage); 1063 if (ret < 0) 1064 return ret; 1065 1066 val->intval = voltage; 1067 return 0; 1068 } 1069 1070 static int bq24190_charger_set_voltage(struct bq24190_dev_info *bdi, 1071 const union power_supply_propval *val) 1072 { 1073 int ret; 1074 1075 if (val->intval > bdi->vreg_max) 1076 return -EINVAL; 1077 1078 ret = bq24190_set_field_val(bdi, BQ24190_REG_CVC, 1079 BQ24190_REG_CVC_VREG_MASK, BQ24190_REG_CVC_VREG_SHIFT, 1080 bq24190_cvc_vreg_values, 1081 ARRAY_SIZE(bq24190_cvc_vreg_values), val->intval); 1082 if (ret < 0) 1083 return ret; 1084 1085 bdi->vreg = val->intval; 1086 1087 return 0; 1088 } 1089 1090 static int bq24190_charger_get_iinlimit(struct bq24190_dev_info *bdi, 1091 union power_supply_propval *val) 1092 { 1093 int iinlimit, ret; 1094 1095 ret = bq24190_get_field_val(bdi, BQ24190_REG_ISC, 1096 BQ24190_REG_ISC_IINLIM_MASK, 1097 BQ24190_REG_ISC_IINLIM_SHIFT, 1098 bq24190_isc_iinlim_values, 1099 ARRAY_SIZE(bq24190_isc_iinlim_values), &iinlimit); 1100 if (ret < 0) 1101 return ret; 1102 1103 val->intval = iinlimit; 1104 return 0; 1105 } 1106 1107 static int bq24190_charger_set_iinlimit(struct bq24190_dev_info *bdi, 1108 const union power_supply_propval *val) 1109 { 1110 return bq24190_set_field_val(bdi, BQ24190_REG_ISC, 1111 BQ24190_REG_ISC_IINLIM_MASK, 1112 BQ24190_REG_ISC_IINLIM_SHIFT, 1113 bq24190_isc_iinlim_values, 1114 ARRAY_SIZE(bq24190_isc_iinlim_values), val->intval); 1115 } 1116 1117 static int bq24190_charger_get_property(struct power_supply *psy, 1118 enum power_supply_property psp, union power_supply_propval *val) 1119 { 1120 struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy); 1121 int ret; 1122 1123 dev_dbg(bdi->dev, "prop: %d\n", psp); 1124 1125 ret = pm_runtime_resume_and_get(bdi->dev); 1126 if (ret < 0) 1127 return ret; 1128 1129 switch (psp) { 1130 case POWER_SUPPLY_PROP_CHARGE_TYPE: 1131 ret = bq24190_charger_get_charge_type(bdi, val); 1132 break; 1133 case POWER_SUPPLY_PROP_HEALTH: 1134 ret = bq24190_charger_get_health(bdi, val); 1135 break; 1136 case POWER_SUPPLY_PROP_ONLINE: 1137 ret = bq24190_charger_get_online(bdi, val); 1138 break; 1139 case POWER_SUPPLY_PROP_STATUS: 1140 ret = bq24190_charger_get_status(bdi, val); 1141 break; 1142 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: 1143 ret = bq24190_charger_get_temp_alert_max(bdi, val); 1144 break; 1145 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT: 1146 ret = bq24190_charger_get_precharge(bdi, val); 1147 break; 1148 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT: 1149 ret = bq24190_charger_get_charge_term(bdi, val); 1150 break; 1151 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: 1152 ret = bq24190_charger_get_current(bdi, val); 1153 break; 1154 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: 1155 val->intval = bdi->ichg_max; 1156 ret = 0; 1157 break; 1158 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: 1159 ret = bq24190_charger_get_voltage(bdi, val); 1160 break; 1161 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: 1162 val->intval = bdi->vreg_max; 1163 ret = 0; 1164 break; 1165 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: 1166 ret = bq24190_charger_get_iinlimit(bdi, val); 1167 break; 1168 case POWER_SUPPLY_PROP_SCOPE: 1169 val->intval = POWER_SUPPLY_SCOPE_SYSTEM; 1170 ret = 0; 1171 break; 1172 case POWER_SUPPLY_PROP_MODEL_NAME: 1173 val->strval = bdi->model_name; 1174 ret = 0; 1175 break; 1176 case POWER_SUPPLY_PROP_MANUFACTURER: 1177 val->strval = BQ24190_MANUFACTURER; 1178 ret = 0; 1179 break; 1180 default: 1181 ret = -ENODATA; 1182 } 1183 1184 pm_runtime_mark_last_busy(bdi->dev); 1185 pm_runtime_put_autosuspend(bdi->dev); 1186 1187 return ret; 1188 } 1189 1190 static int bq24190_charger_set_property(struct power_supply *psy, 1191 enum power_supply_property psp, 1192 const union power_supply_propval *val) 1193 { 1194 struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy); 1195 int ret; 1196 1197 dev_dbg(bdi->dev, "prop: %d\n", psp); 1198 1199 ret = pm_runtime_resume_and_get(bdi->dev); 1200 if (ret < 0) 1201 return ret; 1202 1203 switch (psp) { 1204 case POWER_SUPPLY_PROP_ONLINE: 1205 ret = bq24190_charger_set_online(bdi, val); 1206 break; 1207 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: 1208 ret = bq24190_charger_set_temp_alert_max(bdi, val); 1209 break; 1210 case POWER_SUPPLY_PROP_CHARGE_TYPE: 1211 ret = bq24190_charger_set_charge_type(bdi, val); 1212 break; 1213 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: 1214 ret = bq24190_charger_set_current(bdi, val); 1215 break; 1216 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: 1217 ret = bq24190_charger_set_voltage(bdi, val); 1218 break; 1219 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: 1220 ret = bq24190_charger_set_iinlimit(bdi, val); 1221 break; 1222 default: 1223 ret = -EINVAL; 1224 } 1225 1226 pm_runtime_mark_last_busy(bdi->dev); 1227 pm_runtime_put_autosuspend(bdi->dev); 1228 1229 return ret; 1230 } 1231 1232 static int bq24190_charger_property_is_writeable(struct power_supply *psy, 1233 enum power_supply_property psp) 1234 { 1235 switch (psp) { 1236 case POWER_SUPPLY_PROP_ONLINE: 1237 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: 1238 case POWER_SUPPLY_PROP_CHARGE_TYPE: 1239 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: 1240 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: 1241 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: 1242 return 1; 1243 default: 1244 return 0; 1245 } 1246 } 1247 1248 static void bq24190_input_current_limit_work(struct work_struct *work) 1249 { 1250 struct bq24190_dev_info *bdi = 1251 container_of(work, struct bq24190_dev_info, 1252 input_current_limit_work.work); 1253 union power_supply_propval val; 1254 int ret; 1255 1256 ret = power_supply_get_property_from_supplier(bdi->charger, 1257 POWER_SUPPLY_PROP_CURRENT_MAX, 1258 &val); 1259 if (ret) 1260 return; 1261 1262 bq24190_charger_set_property(bdi->charger, 1263 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, 1264 &val); 1265 power_supply_changed(bdi->charger); 1266 } 1267 1268 /* Sync the input-current-limit with our parent supply (if we have one) */ 1269 static void bq24190_charger_external_power_changed(struct power_supply *psy) 1270 { 1271 struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy); 1272 1273 /* 1274 * The Power-Good detection may take up to 220ms, sometimes 1275 * the external charger detection is quicker, and the bq24190 will 1276 * reset to iinlim based on its own charger detection (which is not 1277 * hooked up when using external charger detection) resulting in a 1278 * too low default 500mA iinlim. Delay setting the input-current-limit 1279 * for 300ms to avoid this. 1280 */ 1281 queue_delayed_work(system_wq, &bdi->input_current_limit_work, 1282 msecs_to_jiffies(300)); 1283 } 1284 1285 static enum power_supply_property bq24190_charger_properties[] = { 1286 POWER_SUPPLY_PROP_CHARGE_TYPE, 1287 POWER_SUPPLY_PROP_HEALTH, 1288 POWER_SUPPLY_PROP_ONLINE, 1289 POWER_SUPPLY_PROP_STATUS, 1290 POWER_SUPPLY_PROP_TEMP_ALERT_MAX, 1291 POWER_SUPPLY_PROP_PRECHARGE_CURRENT, 1292 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT, 1293 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT, 1294 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, 1295 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, 1296 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, 1297 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, 1298 POWER_SUPPLY_PROP_SCOPE, 1299 POWER_SUPPLY_PROP_MODEL_NAME, 1300 POWER_SUPPLY_PROP_MANUFACTURER, 1301 }; 1302 1303 static char *bq24190_charger_supplied_to[] = { 1304 "main-battery", 1305 }; 1306 1307 static const struct power_supply_desc bq24190_charger_desc = { 1308 .name = "bq24190-charger", 1309 .type = POWER_SUPPLY_TYPE_USB, 1310 .properties = bq24190_charger_properties, 1311 .num_properties = ARRAY_SIZE(bq24190_charger_properties), 1312 .get_property = bq24190_charger_get_property, 1313 .set_property = bq24190_charger_set_property, 1314 .property_is_writeable = bq24190_charger_property_is_writeable, 1315 .external_power_changed = bq24190_charger_external_power_changed, 1316 }; 1317 1318 /* Battery power supply property routines */ 1319 1320 static int bq24190_battery_get_status(struct bq24190_dev_info *bdi, 1321 union power_supply_propval *val) 1322 { 1323 u8 ss_reg, chrg_fault; 1324 int status, ret; 1325 1326 mutex_lock(&bdi->f_reg_lock); 1327 chrg_fault = bdi->f_reg; 1328 mutex_unlock(&bdi->f_reg_lock); 1329 1330 chrg_fault &= BQ24190_REG_F_CHRG_FAULT_MASK; 1331 chrg_fault >>= BQ24190_REG_F_CHRG_FAULT_SHIFT; 1332 1333 ret = bq24190_read(bdi, BQ24190_REG_SS, &ss_reg); 1334 if (ret < 0) 1335 return ret; 1336 1337 /* 1338 * The battery must be discharging when any of these are true: 1339 * - there is no good power source; 1340 * - there is a charge fault. 1341 * Could also be discharging when in "supplement mode" but 1342 * there is no way to tell when its in that mode. 1343 */ 1344 if (!(ss_reg & BQ24190_REG_SS_PG_STAT_MASK) || chrg_fault) { 1345 status = POWER_SUPPLY_STATUS_DISCHARGING; 1346 } else { 1347 ss_reg &= BQ24190_REG_SS_CHRG_STAT_MASK; 1348 ss_reg >>= BQ24190_REG_SS_CHRG_STAT_SHIFT; 1349 1350 switch (ss_reg) { 1351 case 0x0: /* Not Charging */ 1352 status = POWER_SUPPLY_STATUS_NOT_CHARGING; 1353 break; 1354 case 0x1: /* Pre-charge */ 1355 case 0x2: /* Fast Charging */ 1356 status = POWER_SUPPLY_STATUS_CHARGING; 1357 break; 1358 case 0x3: /* Charge Termination Done */ 1359 status = POWER_SUPPLY_STATUS_FULL; 1360 break; 1361 default: 1362 ret = -EIO; 1363 } 1364 } 1365 1366 if (!ret) 1367 val->intval = status; 1368 1369 return ret; 1370 } 1371 1372 static int bq24190_battery_get_health(struct bq24190_dev_info *bdi, 1373 union power_supply_propval *val) 1374 { 1375 u8 v; 1376 int health; 1377 1378 mutex_lock(&bdi->f_reg_lock); 1379 v = bdi->f_reg; 1380 mutex_unlock(&bdi->f_reg_lock); 1381 1382 if (v & BQ24190_REG_F_BAT_FAULT_MASK) { 1383 health = POWER_SUPPLY_HEALTH_OVERVOLTAGE; 1384 } else { 1385 v &= BQ24190_REG_F_NTC_FAULT_MASK; 1386 v >>= BQ24190_REG_F_NTC_FAULT_SHIFT; 1387 1388 switch (v) { 1389 case 0x0: /* Normal */ 1390 health = POWER_SUPPLY_HEALTH_GOOD; 1391 break; 1392 case 0x1: /* TS1 Cold */ 1393 case 0x3: /* TS2 Cold */ 1394 case 0x5: /* Both Cold */ 1395 health = POWER_SUPPLY_HEALTH_COLD; 1396 break; 1397 case 0x2: /* TS1 Hot */ 1398 case 0x4: /* TS2 Hot */ 1399 case 0x6: /* Both Hot */ 1400 health = POWER_SUPPLY_HEALTH_OVERHEAT; 1401 break; 1402 default: 1403 health = POWER_SUPPLY_HEALTH_UNKNOWN; 1404 } 1405 } 1406 1407 val->intval = health; 1408 return 0; 1409 } 1410 1411 static int bq24190_battery_get_online(struct bq24190_dev_info *bdi, 1412 union power_supply_propval *val) 1413 { 1414 u8 batfet_disable; 1415 int ret; 1416 1417 ret = bq24190_read_mask(bdi, BQ24190_REG_MOC, 1418 BQ24190_REG_MOC_BATFET_DISABLE_MASK, 1419 BQ24190_REG_MOC_BATFET_DISABLE_SHIFT, &batfet_disable); 1420 if (ret < 0) 1421 return ret; 1422 1423 val->intval = !batfet_disable; 1424 return 0; 1425 } 1426 1427 static int bq24190_battery_set_online(struct bq24190_dev_info *bdi, 1428 const union power_supply_propval *val) 1429 { 1430 return bq24190_write_mask(bdi, BQ24190_REG_MOC, 1431 BQ24190_REG_MOC_BATFET_DISABLE_MASK, 1432 BQ24190_REG_MOC_BATFET_DISABLE_SHIFT, !val->intval); 1433 } 1434 1435 static int bq24190_battery_get_temp_alert_max(struct bq24190_dev_info *bdi, 1436 union power_supply_propval *val) 1437 { 1438 int temp, ret; 1439 1440 ret = bq24190_get_field_val(bdi, BQ24190_REG_ICTRC, 1441 BQ24190_REG_ICTRC_TREG_MASK, 1442 BQ24190_REG_ICTRC_TREG_SHIFT, 1443 bq24190_ictrc_treg_values, 1444 ARRAY_SIZE(bq24190_ictrc_treg_values), &temp); 1445 if (ret < 0) 1446 return ret; 1447 1448 val->intval = temp; 1449 return 0; 1450 } 1451 1452 static int bq24190_battery_set_temp_alert_max(struct bq24190_dev_info *bdi, 1453 const union power_supply_propval *val) 1454 { 1455 return bq24190_set_field_val(bdi, BQ24190_REG_ICTRC, 1456 BQ24190_REG_ICTRC_TREG_MASK, 1457 BQ24190_REG_ICTRC_TREG_SHIFT, 1458 bq24190_ictrc_treg_values, 1459 ARRAY_SIZE(bq24190_ictrc_treg_values), val->intval); 1460 } 1461 1462 static int bq24190_battery_get_property(struct power_supply *psy, 1463 enum power_supply_property psp, union power_supply_propval *val) 1464 { 1465 struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy); 1466 int ret; 1467 1468 dev_warn(bdi->dev, "warning: /sys/class/power_supply/bq24190-battery is deprecated\n"); 1469 dev_dbg(bdi->dev, "prop: %d\n", psp); 1470 1471 ret = pm_runtime_resume_and_get(bdi->dev); 1472 if (ret < 0) 1473 return ret; 1474 1475 switch (psp) { 1476 case POWER_SUPPLY_PROP_STATUS: 1477 ret = bq24190_battery_get_status(bdi, val); 1478 break; 1479 case POWER_SUPPLY_PROP_HEALTH: 1480 ret = bq24190_battery_get_health(bdi, val); 1481 break; 1482 case POWER_SUPPLY_PROP_ONLINE: 1483 ret = bq24190_battery_get_online(bdi, val); 1484 break; 1485 case POWER_SUPPLY_PROP_TECHNOLOGY: 1486 /* Could be Li-on or Li-polymer but no way to tell which */ 1487 val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN; 1488 ret = 0; 1489 break; 1490 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: 1491 ret = bq24190_battery_get_temp_alert_max(bdi, val); 1492 break; 1493 case POWER_SUPPLY_PROP_SCOPE: 1494 val->intval = POWER_SUPPLY_SCOPE_SYSTEM; 1495 ret = 0; 1496 break; 1497 default: 1498 ret = -ENODATA; 1499 } 1500 1501 pm_runtime_mark_last_busy(bdi->dev); 1502 pm_runtime_put_autosuspend(bdi->dev); 1503 1504 return ret; 1505 } 1506 1507 static int bq24190_battery_set_property(struct power_supply *psy, 1508 enum power_supply_property psp, 1509 const union power_supply_propval *val) 1510 { 1511 struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy); 1512 int ret; 1513 1514 dev_warn(bdi->dev, "warning: /sys/class/power_supply/bq24190-battery is deprecated\n"); 1515 dev_dbg(bdi->dev, "prop: %d\n", psp); 1516 1517 ret = pm_runtime_resume_and_get(bdi->dev); 1518 if (ret < 0) 1519 return ret; 1520 1521 switch (psp) { 1522 case POWER_SUPPLY_PROP_ONLINE: 1523 ret = bq24190_battery_set_online(bdi, val); 1524 break; 1525 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: 1526 ret = bq24190_battery_set_temp_alert_max(bdi, val); 1527 break; 1528 default: 1529 ret = -EINVAL; 1530 } 1531 1532 pm_runtime_mark_last_busy(bdi->dev); 1533 pm_runtime_put_autosuspend(bdi->dev); 1534 1535 return ret; 1536 } 1537 1538 static int bq24190_battery_property_is_writeable(struct power_supply *psy, 1539 enum power_supply_property psp) 1540 { 1541 int ret; 1542 1543 switch (psp) { 1544 case POWER_SUPPLY_PROP_ONLINE: 1545 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: 1546 ret = 1; 1547 break; 1548 default: 1549 ret = 0; 1550 } 1551 1552 return ret; 1553 } 1554 1555 static enum power_supply_property bq24190_battery_properties[] = { 1556 POWER_SUPPLY_PROP_STATUS, 1557 POWER_SUPPLY_PROP_HEALTH, 1558 POWER_SUPPLY_PROP_ONLINE, 1559 POWER_SUPPLY_PROP_TECHNOLOGY, 1560 POWER_SUPPLY_PROP_TEMP_ALERT_MAX, 1561 POWER_SUPPLY_PROP_SCOPE, 1562 }; 1563 1564 static const struct power_supply_desc bq24190_battery_desc = { 1565 .name = "bq24190-battery", 1566 .type = POWER_SUPPLY_TYPE_BATTERY, 1567 .properties = bq24190_battery_properties, 1568 .num_properties = ARRAY_SIZE(bq24190_battery_properties), 1569 .get_property = bq24190_battery_get_property, 1570 .set_property = bq24190_battery_set_property, 1571 .property_is_writeable = bq24190_battery_property_is_writeable, 1572 }; 1573 1574 static int bq24190_configure_usb_otg(struct bq24190_dev_info *bdi, u8 ss_reg) 1575 { 1576 bool otg_enabled; 1577 int ret; 1578 1579 otg_enabled = !!(ss_reg & BQ24190_REG_SS_VBUS_STAT_MASK); 1580 ret = extcon_set_state_sync(bdi->edev, EXTCON_USB, otg_enabled); 1581 if (ret < 0) 1582 dev_err(bdi->dev, "Can't set extcon state to %d: %d\n", 1583 otg_enabled, ret); 1584 1585 return ret; 1586 } 1587 1588 static void bq24190_check_status(struct bq24190_dev_info *bdi) 1589 { 1590 const u8 battery_mask_ss = BQ24190_REG_SS_CHRG_STAT_MASK; 1591 const u8 battery_mask_f = BQ24190_REG_F_BAT_FAULT_MASK 1592 | BQ24190_REG_F_NTC_FAULT_MASK; 1593 bool alert_charger = false, alert_battery = false; 1594 u8 ss_reg = 0, f_reg = 0; 1595 int i, ret; 1596 1597 ret = bq24190_read(bdi, BQ24190_REG_SS, &ss_reg); 1598 if (ret < 0) { 1599 dev_err(bdi->dev, "Can't read SS reg: %d\n", ret); 1600 return; 1601 } 1602 1603 i = 0; 1604 do { 1605 ret = bq24190_read(bdi, BQ24190_REG_F, &f_reg); 1606 if (ret < 0) { 1607 dev_err(bdi->dev, "Can't read F reg: %d\n", ret); 1608 return; 1609 } 1610 } while (f_reg && ++i < 2); 1611 1612 /* ignore over/under voltage fault after disconnect */ 1613 if (f_reg == (1 << BQ24190_REG_F_CHRG_FAULT_SHIFT) && 1614 !(ss_reg & BQ24190_REG_SS_PG_STAT_MASK)) 1615 f_reg = 0; 1616 1617 if (f_reg != bdi->f_reg) { 1618 dev_warn(bdi->dev, 1619 "Fault: boost %d, charge %d, battery %d, ntc %d\n", 1620 !!(f_reg & BQ24190_REG_F_BOOST_FAULT_MASK), 1621 !!(f_reg & BQ24190_REG_F_CHRG_FAULT_MASK), 1622 !!(f_reg & BQ24190_REG_F_BAT_FAULT_MASK), 1623 !!(f_reg & BQ24190_REG_F_NTC_FAULT_MASK)); 1624 1625 mutex_lock(&bdi->f_reg_lock); 1626 if ((bdi->f_reg & battery_mask_f) != (f_reg & battery_mask_f)) 1627 alert_battery = true; 1628 if ((bdi->f_reg & ~battery_mask_f) != (f_reg & ~battery_mask_f)) 1629 alert_charger = true; 1630 bdi->f_reg = f_reg; 1631 mutex_unlock(&bdi->f_reg_lock); 1632 } 1633 1634 if (ss_reg != bdi->ss_reg) { 1635 /* 1636 * The device is in host mode so when PG_STAT goes from 1->0 1637 * (i.e., power removed) HIZ needs to be disabled. 1638 */ 1639 if ((bdi->ss_reg & BQ24190_REG_SS_PG_STAT_MASK) && 1640 !(ss_reg & BQ24190_REG_SS_PG_STAT_MASK)) { 1641 ret = bq24190_write_mask(bdi, BQ24190_REG_ISC, 1642 BQ24190_REG_ISC_EN_HIZ_MASK, 1643 BQ24190_REG_ISC_EN_HIZ_SHIFT, 1644 0); 1645 if (ret < 0) 1646 dev_err(bdi->dev, "Can't access ISC reg: %d\n", 1647 ret); 1648 } 1649 1650 if ((bdi->ss_reg & battery_mask_ss) != (ss_reg & battery_mask_ss)) 1651 alert_battery = true; 1652 if ((bdi->ss_reg & ~battery_mask_ss) != (ss_reg & ~battery_mask_ss)) 1653 alert_charger = true; 1654 bdi->ss_reg = ss_reg; 1655 } 1656 1657 if (alert_charger || alert_battery) { 1658 power_supply_changed(bdi->charger); 1659 bq24190_configure_usb_otg(bdi, ss_reg); 1660 } 1661 if (alert_battery && bdi->battery) 1662 power_supply_changed(bdi->battery); 1663 1664 dev_dbg(bdi->dev, "ss_reg: 0x%02x, f_reg: 0x%02x\n", ss_reg, f_reg); 1665 } 1666 1667 static irqreturn_t bq24190_irq_handler_thread(int irq, void *data) 1668 { 1669 struct bq24190_dev_info *bdi = data; 1670 int error; 1671 1672 bdi->irq_event = true; 1673 error = pm_runtime_resume_and_get(bdi->dev); 1674 if (error < 0) { 1675 dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", error); 1676 return IRQ_NONE; 1677 } 1678 bq24190_check_status(bdi); 1679 pm_runtime_mark_last_busy(bdi->dev); 1680 pm_runtime_put_autosuspend(bdi->dev); 1681 bdi->irq_event = false; 1682 1683 return IRQ_HANDLED; 1684 } 1685 1686 static int bq24190_hw_init(struct bq24190_dev_info *bdi) 1687 { 1688 u8 v; 1689 int ret; 1690 1691 /* First check that the device really is what its supposed to be */ 1692 ret = bq24190_read_mask(bdi, BQ24190_REG_VPRS, 1693 BQ24190_REG_VPRS_PN_MASK, 1694 BQ24190_REG_VPRS_PN_SHIFT, 1695 &v); 1696 if (ret < 0) 1697 return ret; 1698 1699 switch (v) { 1700 case BQ24190_REG_VPRS_PN_24190: 1701 case BQ24190_REG_VPRS_PN_24192: 1702 case BQ24190_REG_VPRS_PN_24192I: 1703 break; 1704 default: 1705 dev_err(bdi->dev, "Error unknown model: 0x%02x\n", v); 1706 return -ENODEV; 1707 } 1708 1709 ret = bq24190_register_reset(bdi); 1710 if (ret < 0) 1711 return ret; 1712 1713 ret = bq24190_set_config(bdi); 1714 if (ret < 0) 1715 return ret; 1716 1717 return bq24190_read(bdi, BQ24190_REG_SS, &bdi->ss_reg); 1718 } 1719 1720 static int bq24190_get_config(struct bq24190_dev_info *bdi) 1721 { 1722 const char * const s = "ti,system-minimum-microvolt"; 1723 struct power_supply_battery_info *info; 1724 int v, idx; 1725 1726 idx = ARRAY_SIZE(bq24190_ccc_ichg_values) - 1; 1727 bdi->ichg_max = bq24190_ccc_ichg_values[idx]; 1728 1729 idx = ARRAY_SIZE(bq24190_cvc_vreg_values) - 1; 1730 bdi->vreg_max = bq24190_cvc_vreg_values[idx]; 1731 1732 if (device_property_read_u32(bdi->dev, s, &v) == 0) { 1733 v /= 1000; 1734 if (v >= BQ24190_REG_POC_SYS_MIN_MIN 1735 && v <= BQ24190_REG_POC_SYS_MIN_MAX) 1736 bdi->sys_min = v; 1737 else 1738 dev_warn(bdi->dev, "invalid value for %s: %u\n", s, v); 1739 } 1740 1741 if (!power_supply_get_battery_info(bdi->charger, &info)) { 1742 v = info->precharge_current_ua / 1000; 1743 if (v >= BQ24190_REG_PCTCC_IPRECHG_MIN 1744 && v <= BQ24190_REG_PCTCC_IPRECHG_MAX) 1745 bdi->iprechg = v; 1746 else 1747 dev_warn(bdi->dev, "invalid value for battery:precharge-current-microamp: %d\n", 1748 v); 1749 1750 v = info->charge_term_current_ua / 1000; 1751 if (v >= BQ24190_REG_PCTCC_ITERM_MIN 1752 && v <= BQ24190_REG_PCTCC_ITERM_MAX) 1753 bdi->iterm = v; 1754 else 1755 dev_warn(bdi->dev, "invalid value for battery:charge-term-current-microamp: %d\n", 1756 v); 1757 1758 /* These are optional, so no warning when not set */ 1759 v = info->constant_charge_current_max_ua; 1760 if (v >= bq24190_ccc_ichg_values[0] && v <= bdi->ichg_max) 1761 bdi->ichg = bdi->ichg_max = v; 1762 1763 v = info->constant_charge_voltage_max_uv; 1764 if (v >= bq24190_cvc_vreg_values[0] && v <= bdi->vreg_max) 1765 bdi->vreg = bdi->vreg_max = v; 1766 } 1767 1768 return 0; 1769 } 1770 1771 static int bq24190_probe(struct i2c_client *client) 1772 { 1773 const struct i2c_device_id *id = i2c_client_get_device_id(client); 1774 struct i2c_adapter *adapter = client->adapter; 1775 struct device *dev = &client->dev; 1776 struct power_supply_config charger_cfg = {}, battery_cfg = {}; 1777 struct bq24190_dev_info *bdi; 1778 int ret; 1779 1780 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { 1781 dev_err(dev, "No support for SMBUS_BYTE_DATA\n"); 1782 return -ENODEV; 1783 } 1784 1785 bdi = devm_kzalloc(dev, sizeof(*bdi), GFP_KERNEL); 1786 if (!bdi) { 1787 dev_err(dev, "Can't alloc bdi struct\n"); 1788 return -ENOMEM; 1789 } 1790 1791 bdi->client = client; 1792 bdi->dev = dev; 1793 strncpy(bdi->model_name, id->name, I2C_NAME_SIZE); 1794 mutex_init(&bdi->f_reg_lock); 1795 bdi->charge_type = POWER_SUPPLY_CHARGE_TYPE_FAST; 1796 bdi->f_reg = 0; 1797 bdi->ss_reg = BQ24190_REG_SS_VBUS_STAT_MASK; /* impossible state */ 1798 INIT_DELAYED_WORK(&bdi->input_current_limit_work, 1799 bq24190_input_current_limit_work); 1800 1801 i2c_set_clientdata(client, bdi); 1802 1803 if (client->irq <= 0) { 1804 dev_err(dev, "Can't get irq info\n"); 1805 return -EINVAL; 1806 } 1807 1808 bdi->edev = devm_extcon_dev_allocate(dev, bq24190_usb_extcon_cable); 1809 if (IS_ERR(bdi->edev)) 1810 return PTR_ERR(bdi->edev); 1811 1812 ret = devm_extcon_dev_register(dev, bdi->edev); 1813 if (ret < 0) 1814 return ret; 1815 1816 pm_runtime_enable(dev); 1817 pm_runtime_use_autosuspend(dev); 1818 pm_runtime_set_autosuspend_delay(dev, 600); 1819 ret = pm_runtime_get_sync(dev); 1820 if (ret < 0) { 1821 dev_err(dev, "pm_runtime_get failed: %i\n", ret); 1822 goto out_pmrt; 1823 } 1824 1825 #ifdef CONFIG_SYSFS 1826 bq24190_sysfs_init_attrs(); 1827 charger_cfg.attr_grp = bq24190_sysfs_groups; 1828 #endif 1829 1830 charger_cfg.drv_data = bdi; 1831 charger_cfg.of_node = dev->of_node; 1832 charger_cfg.supplied_to = bq24190_charger_supplied_to; 1833 charger_cfg.num_supplicants = ARRAY_SIZE(bq24190_charger_supplied_to); 1834 bdi->charger = power_supply_register(dev, &bq24190_charger_desc, 1835 &charger_cfg); 1836 if (IS_ERR(bdi->charger)) { 1837 dev_err(dev, "Can't register charger\n"); 1838 ret = PTR_ERR(bdi->charger); 1839 goto out_pmrt; 1840 } 1841 1842 /* the battery class is deprecated and will be removed. */ 1843 /* in the interim, this property hides it. */ 1844 if (!device_property_read_bool(dev, "omit-battery-class")) { 1845 battery_cfg.drv_data = bdi; 1846 bdi->battery = power_supply_register(dev, &bq24190_battery_desc, 1847 &battery_cfg); 1848 if (IS_ERR(bdi->battery)) { 1849 dev_err(dev, "Can't register battery\n"); 1850 ret = PTR_ERR(bdi->battery); 1851 goto out_charger; 1852 } 1853 } 1854 1855 ret = bq24190_get_config(bdi); 1856 if (ret < 0) { 1857 dev_err(dev, "Can't get devicetree config\n"); 1858 goto out_charger; 1859 } 1860 1861 ret = bq24190_hw_init(bdi); 1862 if (ret < 0) { 1863 dev_err(dev, "Hardware init failed\n"); 1864 goto out_charger; 1865 } 1866 1867 ret = bq24190_configure_usb_otg(bdi, bdi->ss_reg); 1868 if (ret < 0) 1869 goto out_charger; 1870 1871 bdi->initialized = true; 1872 1873 ret = devm_request_threaded_irq(dev, client->irq, NULL, 1874 bq24190_irq_handler_thread, 1875 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 1876 "bq24190-charger", bdi); 1877 if (ret < 0) { 1878 dev_err(dev, "Can't set up irq handler\n"); 1879 goto out_charger; 1880 } 1881 1882 ret = bq24190_register_vbus_regulator(bdi); 1883 if (ret < 0) 1884 goto out_charger; 1885 1886 enable_irq_wake(client->irq); 1887 1888 pm_runtime_mark_last_busy(dev); 1889 pm_runtime_put_autosuspend(dev); 1890 1891 return 0; 1892 1893 out_charger: 1894 if (!IS_ERR_OR_NULL(bdi->battery)) 1895 power_supply_unregister(bdi->battery); 1896 power_supply_unregister(bdi->charger); 1897 1898 out_pmrt: 1899 pm_runtime_put_sync(dev); 1900 pm_runtime_dont_use_autosuspend(dev); 1901 pm_runtime_disable(dev); 1902 return ret; 1903 } 1904 1905 static void bq24190_remove(struct i2c_client *client) 1906 { 1907 struct bq24190_dev_info *bdi = i2c_get_clientdata(client); 1908 int error; 1909 1910 cancel_delayed_work_sync(&bdi->input_current_limit_work); 1911 error = pm_runtime_resume_and_get(bdi->dev); 1912 if (error < 0) 1913 dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", error); 1914 1915 bq24190_register_reset(bdi); 1916 if (bdi->battery) 1917 power_supply_unregister(bdi->battery); 1918 power_supply_unregister(bdi->charger); 1919 if (error >= 0) 1920 pm_runtime_put_sync(bdi->dev); 1921 pm_runtime_dont_use_autosuspend(bdi->dev); 1922 pm_runtime_disable(bdi->dev); 1923 } 1924 1925 static void bq24190_shutdown(struct i2c_client *client) 1926 { 1927 struct bq24190_dev_info *bdi = i2c_get_clientdata(client); 1928 1929 /* Turn off 5V boost regulator on shutdown */ 1930 bq24190_set_otg_vbus(bdi, false); 1931 } 1932 1933 static __maybe_unused int bq24190_runtime_suspend(struct device *dev) 1934 { 1935 struct i2c_client *client = to_i2c_client(dev); 1936 struct bq24190_dev_info *bdi = i2c_get_clientdata(client); 1937 1938 if (!bdi->initialized) 1939 return 0; 1940 1941 dev_dbg(bdi->dev, "%s\n", __func__); 1942 1943 return 0; 1944 } 1945 1946 static __maybe_unused int bq24190_runtime_resume(struct device *dev) 1947 { 1948 struct i2c_client *client = to_i2c_client(dev); 1949 struct bq24190_dev_info *bdi = i2c_get_clientdata(client); 1950 1951 if (!bdi->initialized) 1952 return 0; 1953 1954 if (!bdi->irq_event) { 1955 dev_dbg(bdi->dev, "checking events on possible wakeirq\n"); 1956 bq24190_check_status(bdi); 1957 } 1958 1959 return 0; 1960 } 1961 1962 static __maybe_unused int bq24190_pm_suspend(struct device *dev) 1963 { 1964 struct i2c_client *client = to_i2c_client(dev); 1965 struct bq24190_dev_info *bdi = i2c_get_clientdata(client); 1966 int error; 1967 1968 error = pm_runtime_resume_and_get(bdi->dev); 1969 if (error < 0) 1970 dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", error); 1971 1972 bq24190_register_reset(bdi); 1973 1974 if (error >= 0) { 1975 pm_runtime_mark_last_busy(bdi->dev); 1976 pm_runtime_put_autosuspend(bdi->dev); 1977 } 1978 1979 return 0; 1980 } 1981 1982 static __maybe_unused int bq24190_pm_resume(struct device *dev) 1983 { 1984 struct i2c_client *client = to_i2c_client(dev); 1985 struct bq24190_dev_info *bdi = i2c_get_clientdata(client); 1986 int error; 1987 1988 bdi->f_reg = 0; 1989 bdi->ss_reg = BQ24190_REG_SS_VBUS_STAT_MASK; /* impossible state */ 1990 1991 error = pm_runtime_resume_and_get(bdi->dev); 1992 if (error < 0) 1993 dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", error); 1994 1995 bq24190_register_reset(bdi); 1996 bq24190_set_config(bdi); 1997 bq24190_read(bdi, BQ24190_REG_SS, &bdi->ss_reg); 1998 1999 if (error >= 0) { 2000 pm_runtime_mark_last_busy(bdi->dev); 2001 pm_runtime_put_autosuspend(bdi->dev); 2002 } 2003 2004 /* Things may have changed while suspended so alert upper layer */ 2005 power_supply_changed(bdi->charger); 2006 if (bdi->battery) 2007 power_supply_changed(bdi->battery); 2008 2009 return 0; 2010 } 2011 2012 static const struct dev_pm_ops bq24190_pm_ops = { 2013 SET_RUNTIME_PM_OPS(bq24190_runtime_suspend, bq24190_runtime_resume, 2014 NULL) 2015 SET_SYSTEM_SLEEP_PM_OPS(bq24190_pm_suspend, bq24190_pm_resume) 2016 }; 2017 2018 static const struct i2c_device_id bq24190_i2c_ids[] = { 2019 { "bq24190" }, 2020 { "bq24192" }, 2021 { "bq24192i" }, 2022 { "bq24196" }, 2023 { }, 2024 }; 2025 MODULE_DEVICE_TABLE(i2c, bq24190_i2c_ids); 2026 2027 static const struct of_device_id bq24190_of_match[] = { 2028 { .compatible = "ti,bq24190", }, 2029 { .compatible = "ti,bq24192", }, 2030 { .compatible = "ti,bq24192i", }, 2031 { .compatible = "ti,bq24196", }, 2032 { }, 2033 }; 2034 MODULE_DEVICE_TABLE(of, bq24190_of_match); 2035 2036 static struct i2c_driver bq24190_driver = { 2037 .probe = bq24190_probe, 2038 .remove = bq24190_remove, 2039 .shutdown = bq24190_shutdown, 2040 .id_table = bq24190_i2c_ids, 2041 .driver = { 2042 .name = "bq24190-charger", 2043 .pm = &bq24190_pm_ops, 2044 .of_match_table = bq24190_of_match, 2045 }, 2046 }; 2047 module_i2c_driver(bq24190_driver); 2048 2049 MODULE_LICENSE("GPL"); 2050 MODULE_AUTHOR("Mark A. Greer <mgreer@animalcreek.com>"); 2051 MODULE_DESCRIPTION("TI BQ24190 Charger Driver"); 2052