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