1 // SPDX-License-Identifier: GPL-2.0 2 // BQ256XX Battery Charger Driver 3 // Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com/ 4 5 #include <linux/err.h> 6 #include <linux/i2c.h> 7 #include <linux/init.h> 8 #include <linux/interrupt.h> 9 #include <linux/kernel.h> 10 #include <linux/module.h> 11 #include <linux/gpio/consumer.h> 12 #include <linux/power_supply.h> 13 #include <linux/regmap.h> 14 #include <linux/types.h> 15 #include <linux/usb/phy.h> 16 #include <linux/device.h> 17 #include <linux/moduleparam.h> 18 #include <linux/slab.h> 19 #include <linux/acpi.h> 20 21 #define BQ256XX_MANUFACTURER "Texas Instruments" 22 23 #define BQ256XX_INPUT_CURRENT_LIMIT 0x00 24 #define BQ256XX_CHARGER_CONTROL_0 0x01 25 #define BQ256XX_CHARGE_CURRENT_LIMIT 0x02 26 #define BQ256XX_PRECHG_AND_TERM_CURR_LIM 0x03 27 #define BQ256XX_BATTERY_VOLTAGE_LIMIT 0x04 28 #define BQ256XX_CHARGER_CONTROL_1 0x05 29 #define BQ256XX_CHARGER_CONTROL_2 0x06 30 #define BQ256XX_CHARGER_CONTROL_3 0x07 31 #define BQ256XX_CHARGER_STATUS_0 0x08 32 #define BQ256XX_CHARGER_STATUS_1 0x09 33 #define BQ256XX_CHARGER_STATUS_2 0x0a 34 #define BQ256XX_PART_INFORMATION 0x0b 35 #define BQ256XX_CHARGER_CONTROL_4 0x0c 36 37 #define BQ256XX_IINDPM_MASK GENMASK(4, 0) 38 #define BQ256XX_IINDPM_STEP_uA 100000 39 #define BQ256XX_IINDPM_OFFSET_uA 100000 40 #define BQ256XX_IINDPM_MIN_uA 100000 41 #define BQ256XX_IINDPM_MAX_uA 3200000 42 #define BQ256XX_IINDPM_DEF_uA 2400000 43 44 #define BQ256XX_VINDPM_MASK GENMASK(3, 0) 45 #define BQ256XX_VINDPM_STEP_uV 100000 46 #define BQ256XX_VINDPM_OFFSET_uV 3900000 47 #define BQ256XX_VINDPM_MIN_uV 3900000 48 #define BQ256XX_VINDPM_MAX_uV 5400000 49 #define BQ256XX_VINDPM_DEF_uV 4500000 50 51 #define BQ256XX_VBATREG_MASK GENMASK(7, 3) 52 #define BQ2560X_VBATREG_STEP_uV 32000 53 #define BQ2560X_VBATREG_OFFSET_uV 3856000 54 #define BQ2560X_VBATREG_MIN_uV 3856000 55 #define BQ2560X_VBATREG_MAX_uV 4624000 56 #define BQ2560X_VBATREG_DEF_uV 4208000 57 #define BQ25601D_VBATREG_OFFSET_uV 3847000 58 #define BQ25601D_VBATREG_MIN_uV 3847000 59 #define BQ25601D_VBATREG_MAX_uV 4615000 60 #define BQ25601D_VBATREG_DEF_uV 4199000 61 #define BQ2561X_VBATREG_STEP_uV 10000 62 #define BQ25611D_VBATREG_MIN_uV 3494000 63 #define BQ25611D_VBATREG_MAX_uV 4510000 64 #define BQ25611D_VBATREG_DEF_uV 4190000 65 #define BQ25618_VBATREG_MIN_uV 3504000 66 #define BQ25618_VBATREG_MAX_uV 4500000 67 #define BQ25618_VBATREG_DEF_uV 4200000 68 #define BQ256XX_VBATREG_BIT_SHIFT 3 69 #define BQ2561X_VBATREG_THRESH 0x8 70 #define BQ25611D_VBATREG_THRESH_uV 4290000 71 #define BQ25618_VBATREG_THRESH_uV 4300000 72 73 #define BQ256XX_CHG_CONFIG_MASK BIT(4) 74 #define BQ256XX_CHG_CONFIG_BIT_SHIFT 4 75 76 #define BQ256XX_ITERM_MASK GENMASK(3, 0) 77 #define BQ256XX_ITERM_STEP_uA 60000 78 #define BQ256XX_ITERM_OFFSET_uA 60000 79 #define BQ256XX_ITERM_MIN_uA 60000 80 #define BQ256XX_ITERM_MAX_uA 780000 81 #define BQ256XX_ITERM_DEF_uA 180000 82 #define BQ25618_ITERM_STEP_uA 20000 83 #define BQ25618_ITERM_OFFSET_uA 20000 84 #define BQ25618_ITERM_MIN_uA 20000 85 #define BQ25618_ITERM_MAX_uA 260000 86 #define BQ25618_ITERM_DEF_uA 60000 87 88 #define BQ256XX_IPRECHG_MASK GENMASK(7, 4) 89 #define BQ256XX_IPRECHG_STEP_uA 60000 90 #define BQ256XX_IPRECHG_OFFSET_uA 60000 91 #define BQ256XX_IPRECHG_MIN_uA 60000 92 #define BQ256XX_IPRECHG_MAX_uA 780000 93 #define BQ256XX_IPRECHG_DEF_uA 180000 94 #define BQ25618_IPRECHG_STEP_uA 20000 95 #define BQ25618_IPRECHG_OFFSET_uA 20000 96 #define BQ25618_IPRECHG_MIN_uA 20000 97 #define BQ25618_IPRECHG_MAX_uA 260000 98 #define BQ25618_IPRECHG_DEF_uA 40000 99 #define BQ256XX_IPRECHG_BIT_SHIFT 4 100 101 #define BQ256XX_ICHG_MASK GENMASK(5, 0) 102 #define BQ256XX_ICHG_STEP_uA 60000 103 #define BQ256XX_ICHG_MIN_uA 0 104 #define BQ256XX_ICHG_MAX_uA 3000000 105 #define BQ2560X_ICHG_DEF_uA 2040000 106 #define BQ25611D_ICHG_DEF_uA 1020000 107 #define BQ25618_ICHG_STEP_uA 20000 108 #define BQ25618_ICHG_MIN_uA 0 109 #define BQ25618_ICHG_MAX_uA 1500000 110 #define BQ25618_ICHG_DEF_uA 340000 111 #define BQ25618_ICHG_THRESH 0x3c 112 #define BQ25618_ICHG_THRESH_uA 1180000 113 114 #define BQ256XX_VBUS_STAT_MASK GENMASK(7, 5) 115 #define BQ256XX_VBUS_STAT_NO_INPUT 0 116 #define BQ256XX_VBUS_STAT_USB_SDP BIT(5) 117 #define BQ256XX_VBUS_STAT_USB_CDP BIT(6) 118 #define BQ256XX_VBUS_STAT_USB_DCP (BIT(6) | BIT(5)) 119 #define BQ256XX_VBUS_STAT_USB_OTG (BIT(7) | BIT(6) | BIT(5)) 120 121 #define BQ256XX_CHRG_STAT_MASK GENMASK(4, 3) 122 #define BQ256XX_CHRG_STAT_NOT_CHRGING 0 123 #define BQ256XX_CHRG_STAT_PRECHRGING BIT(3) 124 #define BQ256XX_CHRG_STAT_FAST_CHRGING BIT(4) 125 #define BQ256XX_CHRG_STAT_CHRG_TERM (BIT(4) | BIT(3)) 126 127 #define BQ256XX_PG_STAT_MASK BIT(2) 128 #define BQ256XX_WDT_FAULT_MASK BIT(7) 129 #define BQ256XX_CHRG_FAULT_MASK GENMASK(5, 4) 130 #define BQ256XX_CHRG_FAULT_NORMAL 0 131 #define BQ256XX_CHRG_FAULT_INPUT BIT(4) 132 #define BQ256XX_CHRG_FAULT_THERM BIT(5) 133 #define BQ256XX_CHRG_FAULT_CST_EXPIRE (BIT(5) | BIT(4)) 134 #define BQ256XX_BAT_FAULT_MASK BIT(3) 135 #define BQ256XX_NTC_FAULT_MASK GENMASK(2, 0) 136 #define BQ256XX_NTC_FAULT_WARM BIT(1) 137 #define BQ256XX_NTC_FAULT_COOL (BIT(1) | BIT(0)) 138 #define BQ256XX_NTC_FAULT_COLD (BIT(2) | BIT(0)) 139 #define BQ256XX_NTC_FAULT_HOT (BIT(2) | BIT(1)) 140 141 #define BQ256XX_NUM_WD_VAL 4 142 #define BQ256XX_WATCHDOG_MASK GENMASK(5, 4) 143 #define BQ256XX_WATCHDOG_MAX 1600000 144 #define BQ256XX_WATCHDOG_DIS 0 145 #define BQ256XX_WDT_BIT_SHIFT 4 146 147 #define BQ256XX_REG_RST BIT(7) 148 149 /** 150 * struct bq256xx_init_data - 151 * @ichg: fast charge current 152 * @iindpm: input current limit 153 * @vbatreg: charge voltage 154 * @iterm: termination current 155 * @iprechg: precharge current 156 * @vindpm: input voltage limit 157 * @ichg_max: maximum fast charge current 158 * @vbatreg_max: maximum charge voltage 159 */ 160 struct bq256xx_init_data { 161 u32 ichg; 162 u32 iindpm; 163 u32 vbatreg; 164 u32 iterm; 165 u32 iprechg; 166 u32 vindpm; 167 u32 ichg_max; 168 u32 vbatreg_max; 169 }; 170 171 /** 172 * struct bq256xx_state - 173 * @vbus_stat: VBUS status according to BQ256XX_CHARGER_STATUS_0 174 * @chrg_stat: charging status according to BQ256XX_CHARGER_STATUS_0 175 * @online: PG status according to BQ256XX_CHARGER_STATUS_0 176 * 177 * @wdt_fault: watchdog fault according to BQ256XX_CHARGER_STATUS_1 178 * @bat_fault: battery fault according to BQ256XX_CHARGER_STATUS_1 179 * @chrg_fault: charging fault according to BQ256XX_CHARGER_STATUS_1 180 * @ntc_fault: TS fault according to BQ256XX_CHARGER_STATUS_1 181 */ 182 struct bq256xx_state { 183 u8 vbus_stat; 184 u8 chrg_stat; 185 bool online; 186 187 u8 wdt_fault; 188 u8 bat_fault; 189 u8 chrg_fault; 190 u8 ntc_fault; 191 }; 192 193 enum bq256xx_id { 194 BQ25600, 195 BQ25600D, 196 BQ25601, 197 BQ25601D, 198 BQ25618, 199 BQ25619, 200 BQ25611D, 201 }; 202 203 /** 204 * struct bq256xx_device - 205 * @client: i2c client structure 206 * @regmap: register map structure 207 * @dev: device structure 208 * @charger: power supply registered for the charger 209 * @battery: power supply registered for the battery 210 * @lock: mutex lock structure 211 * 212 * @usb2_phy: usb_phy identifier 213 * @usb3_phy: usb_phy identifier 214 * @usb_nb: notifier block 215 * @usb_work: usb work queue 216 * @usb_event: usb_event code 217 * 218 * @model_name: i2c name string 219 * 220 * @init_data: initialization data 221 * @chip_info: device variant information 222 * @state: device status and faults 223 * @watchdog_timer: watchdog timer value in milliseconds 224 */ 225 struct bq256xx_device { 226 struct i2c_client *client; 227 struct device *dev; 228 struct power_supply *charger; 229 struct power_supply *battery; 230 struct mutex lock; 231 struct regmap *regmap; 232 233 struct usb_phy *usb2_phy; 234 struct usb_phy *usb3_phy; 235 struct notifier_block usb_nb; 236 struct work_struct usb_work; 237 unsigned long usb_event; 238 239 char model_name[I2C_NAME_SIZE]; 240 241 struct bq256xx_init_data init_data; 242 const struct bq256xx_chip_info *chip_info; 243 struct bq256xx_state state; 244 int watchdog_timer; 245 }; 246 247 /** 248 * struct bq256xx_chip_info - 249 * @model_id: device instance 250 * 251 * @bq256xx_regmap_config: regmap configuration struct 252 * @bq256xx_get_ichg: pointer to instance specific get_ichg function 253 * @bq256xx_get_iindpm: pointer to instance specific get_iindpm function 254 * @bq256xx_get_vbatreg: pointer to instance specific get_vbatreg function 255 * @bq256xx_get_iterm: pointer to instance specific get_iterm function 256 * @bq256xx_get_iprechg: pointer to instance specific get_iprechg function 257 * @bq256xx_get_vindpm: pointer to instance specific get_vindpm function 258 * 259 * @bq256xx_set_ichg: pointer to instance specific set_ichg function 260 * @bq256xx_set_iindpm: pointer to instance specific set_iindpm function 261 * @bq256xx_set_vbatreg: pointer to instance specific set_vbatreg function 262 * @bq256xx_set_iterm: pointer to instance specific set_iterm function 263 * @bq256xx_set_iprechg: pointer to instance specific set_iprechg function 264 * @bq256xx_set_vindpm: pointer to instance specific set_vindpm function 265 * @bq256xx_set_charge_type: pointer to instance specific set_charge_type function 266 * 267 * @bq256xx_def_ichg: default ichg value in microamps 268 * @bq256xx_def_iindpm: default iindpm value in microamps 269 * @bq256xx_def_vbatreg: default vbatreg value in microvolts 270 * @bq256xx_def_iterm: default iterm value in microamps 271 * @bq256xx_def_iprechg: default iprechg value in microamps 272 * @bq256xx_def_vindpm: default vindpm value in microvolts 273 * 274 * @bq256xx_max_ichg: maximum charge current in microamps 275 * @bq256xx_max_vbatreg: maximum battery regulation voltage in microvolts 276 * 277 * @has_usb_detect: indicates whether device has BC1.2 detection 278 */ 279 struct bq256xx_chip_info { 280 int model_id; 281 282 const struct regmap_config *bq256xx_regmap_config; 283 284 int (*bq256xx_get_ichg)(struct bq256xx_device *bq); 285 int (*bq256xx_get_iindpm)(struct bq256xx_device *bq); 286 int (*bq256xx_get_vbatreg)(struct bq256xx_device *bq); 287 int (*bq256xx_get_iterm)(struct bq256xx_device *bq); 288 int (*bq256xx_get_iprechg)(struct bq256xx_device *bq); 289 int (*bq256xx_get_vindpm)(struct bq256xx_device *bq); 290 291 int (*bq256xx_set_ichg)(struct bq256xx_device *bq, int ichg); 292 int (*bq256xx_set_iindpm)(struct bq256xx_device *bq, int iindpm); 293 int (*bq256xx_set_vbatreg)(struct bq256xx_device *bq, int vbatreg); 294 int (*bq256xx_set_iterm)(struct bq256xx_device *bq, int iterm); 295 int (*bq256xx_set_iprechg)(struct bq256xx_device *bq, int iprechg); 296 int (*bq256xx_set_vindpm)(struct bq256xx_device *bq, int vindpm); 297 int (*bq256xx_set_charge_type)(struct bq256xx_device *bq, int type); 298 299 int bq256xx_def_ichg; 300 int bq256xx_def_iindpm; 301 int bq256xx_def_vbatreg; 302 int bq256xx_def_iterm; 303 int bq256xx_def_iprechg; 304 int bq256xx_def_vindpm; 305 306 int bq256xx_max_ichg; 307 int bq256xx_max_vbatreg; 308 309 bool has_usb_detect; 310 }; 311 312 static int bq256xx_watchdog_time[BQ256XX_NUM_WD_VAL] = { 313 0, 40000, 80000, 1600000 314 }; 315 316 static const int bq25611d_vbatreg_values[] = { 317 3494000, 3590000, 3686000, 3790000, 3894000, 3990000, 4090000, 4140000, 318 4190000 319 }; 320 321 static const int bq25618_619_vbatreg_values[] = { 322 3504000, 3600000, 3696000, 3800000, 3904000, 4000000, 4100000, 4150000, 323 4200000 324 }; 325 326 static const int bq25618_619_ichg_values[] = { 327 1290000, 1360000, 1430000, 1500000 328 }; 329 330 static enum power_supply_usb_type bq256xx_usb_type[] = { 331 POWER_SUPPLY_USB_TYPE_SDP, 332 POWER_SUPPLY_USB_TYPE_CDP, 333 POWER_SUPPLY_USB_TYPE_DCP, 334 POWER_SUPPLY_USB_TYPE_UNKNOWN, 335 POWER_SUPPLY_USB_TYPE_ACA, 336 }; 337 338 static int bq256xx_array_parse(int array_size, int val, const int array[]) 339 { 340 int i = 0; 341 342 if (val < array[i]) 343 return i - 1; 344 345 if (val >= array[array_size - 1]) 346 return array_size - 1; 347 348 for (i = 1; i < array_size; i++) { 349 if (val == array[i]) 350 return i; 351 352 if (val > array[i - 1] && val < array[i]) { 353 if (val < array[i]) 354 return i - 1; 355 else 356 return i; 357 } 358 } 359 return -EINVAL; 360 } 361 362 static int bq256xx_usb_notifier(struct notifier_block *nb, unsigned long val, 363 void *priv) 364 { 365 struct bq256xx_device *bq = 366 container_of(nb, struct bq256xx_device, usb_nb); 367 368 bq->usb_event = val; 369 queue_work(system_power_efficient_wq, &bq->usb_work); 370 371 return NOTIFY_OK; 372 } 373 374 static void bq256xx_usb_work(struct work_struct *data) 375 { 376 struct bq256xx_device *bq = 377 container_of(data, struct bq256xx_device, usb_work); 378 379 switch (bq->usb_event) { 380 case USB_EVENT_ID: 381 break; 382 case USB_EVENT_NONE: 383 power_supply_changed(bq->charger); 384 break; 385 default: 386 dev_err(bq->dev, "Error switching to charger mode.\n"); 387 break; 388 } 389 } 390 391 static struct reg_default bq2560x_reg_defs[] = { 392 {BQ256XX_INPUT_CURRENT_LIMIT, 0x17}, 393 {BQ256XX_CHARGER_CONTROL_0, 0x1a}, 394 {BQ256XX_CHARGE_CURRENT_LIMIT, 0xa2}, 395 {BQ256XX_PRECHG_AND_TERM_CURR_LIM, 0x22}, 396 {BQ256XX_BATTERY_VOLTAGE_LIMIT, 0x58}, 397 {BQ256XX_CHARGER_CONTROL_1, 0x9f}, 398 {BQ256XX_CHARGER_CONTROL_2, 0x66}, 399 {BQ256XX_CHARGER_CONTROL_3, 0x4c}, 400 }; 401 402 static struct reg_default bq25611d_reg_defs[] = { 403 {BQ256XX_INPUT_CURRENT_LIMIT, 0x17}, 404 {BQ256XX_CHARGER_CONTROL_0, 0x1a}, 405 {BQ256XX_CHARGE_CURRENT_LIMIT, 0x91}, 406 {BQ256XX_PRECHG_AND_TERM_CURR_LIM, 0x12}, 407 {BQ256XX_BATTERY_VOLTAGE_LIMIT, 0x40}, 408 {BQ256XX_CHARGER_CONTROL_1, 0x9e}, 409 {BQ256XX_CHARGER_CONTROL_2, 0xe6}, 410 {BQ256XX_CHARGER_CONTROL_3, 0x4c}, 411 {BQ256XX_PART_INFORMATION, 0x54}, 412 {BQ256XX_CHARGER_CONTROL_4, 0x75}, 413 }; 414 415 static struct reg_default bq25618_619_reg_defs[] = { 416 {BQ256XX_INPUT_CURRENT_LIMIT, 0x17}, 417 {BQ256XX_CHARGER_CONTROL_0, 0x1a}, 418 {BQ256XX_CHARGE_CURRENT_LIMIT, 0x91}, 419 {BQ256XX_PRECHG_AND_TERM_CURR_LIM, 0x12}, 420 {BQ256XX_BATTERY_VOLTAGE_LIMIT, 0x40}, 421 {BQ256XX_CHARGER_CONTROL_1, 0x9e}, 422 {BQ256XX_CHARGER_CONTROL_2, 0xe6}, 423 {BQ256XX_CHARGER_CONTROL_3, 0x4c}, 424 {BQ256XX_PART_INFORMATION, 0x2c}, 425 {BQ256XX_CHARGER_CONTROL_4, 0x75}, 426 }; 427 428 static int bq256xx_get_state(struct bq256xx_device *bq, 429 struct bq256xx_state *state) 430 { 431 unsigned int charger_status_0; 432 unsigned int charger_status_1; 433 int ret; 434 435 ret = regmap_read(bq->regmap, BQ256XX_CHARGER_STATUS_0, 436 &charger_status_0); 437 if (ret) 438 return ret; 439 440 ret = regmap_read(bq->regmap, BQ256XX_CHARGER_STATUS_1, 441 &charger_status_1); 442 if (ret) 443 return ret; 444 445 state->vbus_stat = charger_status_0 & BQ256XX_VBUS_STAT_MASK; 446 state->chrg_stat = charger_status_0 & BQ256XX_CHRG_STAT_MASK; 447 state->online = charger_status_0 & BQ256XX_PG_STAT_MASK; 448 449 state->wdt_fault = charger_status_1 & BQ256XX_WDT_FAULT_MASK; 450 state->bat_fault = charger_status_1 & BQ256XX_BAT_FAULT_MASK; 451 state->chrg_fault = charger_status_1 & BQ256XX_CHRG_FAULT_MASK; 452 state->ntc_fault = charger_status_1 & BQ256XX_NTC_FAULT_MASK; 453 454 return 0; 455 } 456 457 static int bq256xx_set_charge_type(struct bq256xx_device *bq, int type) 458 { 459 int chg_config = 0; 460 461 switch (type) { 462 case POWER_SUPPLY_CHARGE_TYPE_NONE: 463 chg_config = 0x0; 464 break; 465 case POWER_SUPPLY_CHARGE_TYPE_TRICKLE: 466 case POWER_SUPPLY_CHARGE_TYPE_FAST: 467 chg_config = 0x1; 468 break; 469 default: 470 return -EINVAL; 471 } 472 473 return regmap_update_bits(bq->regmap, BQ256XX_CHARGER_CONTROL_0, 474 BQ256XX_CHG_CONFIG_MASK, 475 (chg_config ? 1 : 0) << BQ256XX_CHG_CONFIG_BIT_SHIFT); 476 } 477 478 static int bq256xx_get_ichg_curr(struct bq256xx_device *bq) 479 { 480 unsigned int charge_current_limit; 481 unsigned int ichg_reg_code; 482 int ret; 483 484 ret = regmap_read(bq->regmap, BQ256XX_CHARGE_CURRENT_LIMIT, 485 &charge_current_limit); 486 if (ret) 487 return ret; 488 489 ichg_reg_code = charge_current_limit & BQ256XX_ICHG_MASK; 490 491 return ichg_reg_code * BQ256XX_ICHG_STEP_uA; 492 } 493 494 static int bq25618_619_get_ichg_curr(struct bq256xx_device *bq) 495 { 496 unsigned int charge_current_limit; 497 unsigned int ichg_reg_code; 498 int ret; 499 500 ret = regmap_read(bq->regmap, BQ256XX_CHARGE_CURRENT_LIMIT, 501 &charge_current_limit); 502 if (ret) 503 return ret; 504 505 ichg_reg_code = charge_current_limit & BQ256XX_ICHG_MASK; 506 507 if (ichg_reg_code < BQ25618_ICHG_THRESH) 508 return ichg_reg_code * BQ25618_ICHG_STEP_uA; 509 510 return bq25618_619_ichg_values[ichg_reg_code - BQ25618_ICHG_THRESH]; 511 } 512 513 static int bq256xx_set_ichg_curr(struct bq256xx_device *bq, int ichg) 514 { 515 unsigned int ichg_reg_code; 516 int ichg_max = bq->init_data.ichg_max; 517 518 ichg = clamp(ichg, BQ256XX_ICHG_MIN_uA, ichg_max); 519 ichg_reg_code = ichg / BQ256XX_ICHG_STEP_uA; 520 521 return regmap_update_bits(bq->regmap, BQ256XX_CHARGE_CURRENT_LIMIT, 522 BQ256XX_ICHG_MASK, ichg_reg_code); 523 } 524 525 static int bq25618_619_set_ichg_curr(struct bq256xx_device *bq, int ichg) 526 { 527 int array_size = ARRAY_SIZE(bq25618_619_ichg_values); 528 unsigned int ichg_reg_code; 529 int ichg_max = bq->init_data.ichg_max; 530 531 ichg = clamp(ichg, BQ25618_ICHG_MIN_uA, ichg_max); 532 533 if (ichg <= BQ25618_ICHG_THRESH_uA) { 534 ichg_reg_code = ichg / BQ25618_ICHG_STEP_uA; 535 } else { 536 ichg_reg_code = bq256xx_array_parse(array_size, ichg, 537 bq25618_619_ichg_values) + BQ25618_ICHG_THRESH; 538 } 539 540 return regmap_update_bits(bq->regmap, BQ256XX_CHARGE_CURRENT_LIMIT, 541 BQ256XX_ICHG_MASK, ichg_reg_code); 542 } 543 544 static int bq25618_619_get_chrg_volt(struct bq256xx_device *bq) 545 { 546 unsigned int battery_volt_lim; 547 unsigned int vbatreg_reg_code; 548 int ret; 549 550 ret = regmap_read(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT, 551 &battery_volt_lim); 552 553 if (ret) 554 return ret; 555 556 vbatreg_reg_code = (battery_volt_lim & BQ256XX_VBATREG_MASK) >> 557 BQ256XX_VBATREG_BIT_SHIFT; 558 559 if (vbatreg_reg_code > BQ2561X_VBATREG_THRESH) 560 return ((vbatreg_reg_code - BQ2561X_VBATREG_THRESH) * 561 BQ2561X_VBATREG_STEP_uV) + 562 BQ25618_VBATREG_THRESH_uV; 563 564 return bq25618_619_vbatreg_values[vbatreg_reg_code]; 565 } 566 567 static int bq25611d_get_chrg_volt(struct bq256xx_device *bq) 568 { 569 unsigned int battery_volt_lim; 570 unsigned int vbatreg_reg_code; 571 int ret; 572 573 ret = regmap_read(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT, 574 &battery_volt_lim); 575 if (ret) 576 return ret; 577 578 vbatreg_reg_code = (battery_volt_lim & BQ256XX_VBATREG_MASK) >> 579 BQ256XX_VBATREG_BIT_SHIFT; 580 581 if (vbatreg_reg_code > BQ2561X_VBATREG_THRESH) 582 return ((vbatreg_reg_code - BQ2561X_VBATREG_THRESH) * 583 BQ2561X_VBATREG_STEP_uV) + 584 BQ25611D_VBATREG_THRESH_uV; 585 586 return bq25611d_vbatreg_values[vbatreg_reg_code]; 587 } 588 589 static int bq2560x_get_chrg_volt(struct bq256xx_device *bq) 590 { 591 unsigned int battery_volt_lim; 592 unsigned int vbatreg_reg_code; 593 int ret; 594 595 ret = regmap_read(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT, 596 &battery_volt_lim); 597 if (ret) 598 return ret; 599 600 vbatreg_reg_code = (battery_volt_lim & BQ256XX_VBATREG_MASK) >> 601 BQ256XX_VBATREG_BIT_SHIFT; 602 603 return (vbatreg_reg_code * BQ2560X_VBATREG_STEP_uV) 604 + BQ2560X_VBATREG_OFFSET_uV; 605 } 606 607 static int bq25601d_get_chrg_volt(struct bq256xx_device *bq) 608 { 609 unsigned int battery_volt_lim; 610 unsigned int vbatreg_reg_code; 611 int ret; 612 613 ret = regmap_read(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT, 614 &battery_volt_lim); 615 if (ret) 616 return ret; 617 618 vbatreg_reg_code = (battery_volt_lim & BQ256XX_VBATREG_MASK) >> 619 BQ256XX_VBATREG_BIT_SHIFT; 620 621 return (vbatreg_reg_code * BQ2560X_VBATREG_STEP_uV) 622 + BQ25601D_VBATREG_OFFSET_uV; 623 } 624 625 static int bq25618_619_set_chrg_volt(struct bq256xx_device *bq, int vbatreg) 626 { 627 int array_size = ARRAY_SIZE(bq25618_619_vbatreg_values); 628 unsigned int vbatreg_reg_code; 629 int vbatreg_max = bq->init_data.vbatreg_max; 630 631 vbatreg = clamp(vbatreg, BQ25618_VBATREG_MIN_uV, vbatreg_max); 632 633 if (vbatreg > BQ25618_VBATREG_THRESH_uV) 634 vbatreg_reg_code = ((vbatreg - 635 BQ25618_VBATREG_THRESH_uV) / 636 (BQ2561X_VBATREG_STEP_uV)) + BQ2561X_VBATREG_THRESH; 637 else { 638 vbatreg_reg_code = bq256xx_array_parse(array_size, vbatreg, 639 bq25618_619_vbatreg_values); 640 } 641 642 return regmap_update_bits(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT, 643 BQ256XX_VBATREG_MASK, vbatreg_reg_code << 644 BQ256XX_VBATREG_BIT_SHIFT); 645 } 646 647 static int bq25611d_set_chrg_volt(struct bq256xx_device *bq, int vbatreg) 648 { 649 int array_size = ARRAY_SIZE(bq25611d_vbatreg_values); 650 unsigned int vbatreg_reg_code; 651 int vbatreg_max = bq->init_data.vbatreg_max; 652 653 vbatreg = clamp(vbatreg, BQ25611D_VBATREG_MIN_uV, vbatreg_max); 654 655 if (vbatreg > BQ25611D_VBATREG_THRESH_uV) 656 vbatreg_reg_code = ((vbatreg - 657 BQ25611D_VBATREG_THRESH_uV) / 658 (BQ2561X_VBATREG_STEP_uV)) + BQ2561X_VBATREG_THRESH; 659 else { 660 vbatreg_reg_code = bq256xx_array_parse(array_size, vbatreg, 661 bq25611d_vbatreg_values); 662 } 663 664 return regmap_update_bits(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT, 665 BQ256XX_VBATREG_MASK, vbatreg_reg_code << 666 BQ256XX_VBATREG_BIT_SHIFT); 667 } 668 669 static int bq2560x_set_chrg_volt(struct bq256xx_device *bq, int vbatreg) 670 { 671 unsigned int vbatreg_reg_code; 672 int vbatreg_max = bq->init_data.vbatreg_max; 673 674 vbatreg = clamp(vbatreg, BQ2560X_VBATREG_MIN_uV, vbatreg_max); 675 676 vbatreg_reg_code = (vbatreg - BQ2560X_VBATREG_OFFSET_uV) / 677 BQ2560X_VBATREG_STEP_uV; 678 679 return regmap_update_bits(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT, 680 BQ256XX_VBATREG_MASK, vbatreg_reg_code << 681 BQ256XX_VBATREG_BIT_SHIFT); 682 } 683 684 static int bq25601d_set_chrg_volt(struct bq256xx_device *bq, int vbatreg) 685 { 686 unsigned int vbatreg_reg_code; 687 int vbatreg_max = bq->init_data.vbatreg_max; 688 689 vbatreg = clamp(vbatreg, BQ25601D_VBATREG_MIN_uV, vbatreg_max); 690 691 vbatreg_reg_code = (vbatreg - BQ25601D_VBATREG_OFFSET_uV) / 692 BQ2560X_VBATREG_STEP_uV; 693 694 return regmap_update_bits(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT, 695 BQ256XX_VBATREG_MASK, vbatreg_reg_code << 696 BQ256XX_VBATREG_BIT_SHIFT); 697 } 698 699 static int bq256xx_get_prechrg_curr(struct bq256xx_device *bq) 700 { 701 unsigned int prechg_and_term_curr_lim; 702 unsigned int iprechg_reg_code; 703 int ret; 704 705 ret = regmap_read(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM, 706 &prechg_and_term_curr_lim); 707 if (ret) 708 return ret; 709 710 iprechg_reg_code = (prechg_and_term_curr_lim & BQ256XX_IPRECHG_MASK) 711 >> BQ256XX_IPRECHG_BIT_SHIFT; 712 713 return (iprechg_reg_code * BQ256XX_IPRECHG_STEP_uA) + 714 BQ256XX_IPRECHG_OFFSET_uA; 715 } 716 717 static int bq256xx_set_prechrg_curr(struct bq256xx_device *bq, int iprechg) 718 { 719 unsigned int iprechg_reg_code; 720 721 iprechg = clamp(iprechg, BQ256XX_IPRECHG_MIN_uA, 722 BQ256XX_IPRECHG_MAX_uA); 723 724 iprechg_reg_code = ((iprechg - BQ256XX_IPRECHG_OFFSET_uA) / 725 BQ256XX_IPRECHG_STEP_uA) << BQ256XX_IPRECHG_BIT_SHIFT; 726 727 return regmap_update_bits(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM, 728 BQ256XX_IPRECHG_MASK, iprechg_reg_code); 729 } 730 731 static int bq25618_619_get_prechrg_curr(struct bq256xx_device *bq) 732 { 733 unsigned int prechg_and_term_curr_lim; 734 unsigned int iprechg_reg_code; 735 int ret; 736 737 ret = regmap_read(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM, 738 &prechg_and_term_curr_lim); 739 if (ret) 740 return ret; 741 742 iprechg_reg_code = (prechg_and_term_curr_lim & BQ256XX_IPRECHG_MASK) 743 >> BQ256XX_IPRECHG_BIT_SHIFT; 744 745 return (iprechg_reg_code * BQ25618_IPRECHG_STEP_uA) + 746 BQ25618_IPRECHG_OFFSET_uA; 747 } 748 749 static int bq25618_619_set_prechrg_curr(struct bq256xx_device *bq, int iprechg) 750 { 751 unsigned int iprechg_reg_code; 752 753 iprechg = clamp(iprechg, BQ25618_IPRECHG_MIN_uA, 754 BQ25618_IPRECHG_MAX_uA); 755 756 iprechg_reg_code = ((iprechg - BQ25618_IPRECHG_OFFSET_uA) / 757 BQ25618_IPRECHG_STEP_uA) << BQ256XX_IPRECHG_BIT_SHIFT; 758 759 return regmap_update_bits(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM, 760 BQ256XX_IPRECHG_MASK, iprechg_reg_code); 761 } 762 763 static int bq256xx_get_term_curr(struct bq256xx_device *bq) 764 { 765 unsigned int prechg_and_term_curr_lim; 766 unsigned int iterm_reg_code; 767 int ret; 768 769 ret = regmap_read(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM, 770 &prechg_and_term_curr_lim); 771 if (ret) 772 return ret; 773 774 iterm_reg_code = prechg_and_term_curr_lim & BQ256XX_ITERM_MASK; 775 776 return (iterm_reg_code * BQ256XX_ITERM_STEP_uA) + 777 BQ256XX_ITERM_OFFSET_uA; 778 } 779 780 static int bq256xx_set_term_curr(struct bq256xx_device *bq, int iterm) 781 { 782 unsigned int iterm_reg_code; 783 784 iterm = clamp(iterm, BQ256XX_ITERM_MIN_uA, BQ256XX_ITERM_MAX_uA); 785 786 iterm_reg_code = (iterm - BQ256XX_ITERM_OFFSET_uA) / 787 BQ256XX_ITERM_STEP_uA; 788 789 return regmap_update_bits(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM, 790 BQ256XX_ITERM_MASK, iterm_reg_code); 791 } 792 793 static int bq25618_619_get_term_curr(struct bq256xx_device *bq) 794 { 795 unsigned int prechg_and_term_curr_lim; 796 unsigned int iterm_reg_code; 797 int ret; 798 799 ret = regmap_read(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM, 800 &prechg_and_term_curr_lim); 801 if (ret) 802 return ret; 803 804 iterm_reg_code = prechg_and_term_curr_lim & BQ256XX_ITERM_MASK; 805 806 return (iterm_reg_code * BQ25618_ITERM_STEP_uA) + 807 BQ25618_ITERM_OFFSET_uA; 808 } 809 810 static int bq25618_619_set_term_curr(struct bq256xx_device *bq, int iterm) 811 { 812 unsigned int iterm_reg_code; 813 814 iterm = clamp(iterm, BQ25618_ITERM_MIN_uA, BQ25618_ITERM_MAX_uA); 815 816 iterm_reg_code = (iterm - BQ25618_ITERM_OFFSET_uA) / 817 BQ25618_ITERM_STEP_uA; 818 819 return regmap_update_bits(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM, 820 BQ256XX_ITERM_MASK, iterm_reg_code); 821 } 822 823 static int bq256xx_get_input_volt_lim(struct bq256xx_device *bq) 824 { 825 unsigned int charger_control_2; 826 unsigned int vindpm_reg_code; 827 int ret; 828 829 ret = regmap_read(bq->regmap, BQ256XX_CHARGER_CONTROL_2, 830 &charger_control_2); 831 if (ret) 832 return ret; 833 834 vindpm_reg_code = charger_control_2 & BQ256XX_VINDPM_MASK; 835 836 return (vindpm_reg_code * BQ256XX_VINDPM_STEP_uV) + 837 BQ256XX_VINDPM_OFFSET_uV; 838 } 839 840 static int bq256xx_set_input_volt_lim(struct bq256xx_device *bq, int vindpm) 841 { 842 unsigned int vindpm_reg_code; 843 844 vindpm = clamp(vindpm, BQ256XX_VINDPM_MIN_uV, BQ256XX_VINDPM_MAX_uV); 845 846 vindpm_reg_code = (vindpm - BQ256XX_VINDPM_OFFSET_uV) / 847 BQ256XX_VINDPM_STEP_uV; 848 849 return regmap_update_bits(bq->regmap, BQ256XX_CHARGER_CONTROL_2, 850 BQ256XX_VINDPM_MASK, vindpm_reg_code); 851 } 852 853 static int bq256xx_get_input_curr_lim(struct bq256xx_device *bq) 854 { 855 unsigned int input_current_limit; 856 unsigned int iindpm_reg_code; 857 int ret; 858 859 ret = regmap_read(bq->regmap, BQ256XX_INPUT_CURRENT_LIMIT, 860 &input_current_limit); 861 if (ret) 862 return ret; 863 864 iindpm_reg_code = input_current_limit & BQ256XX_IINDPM_MASK; 865 866 return (iindpm_reg_code * BQ256XX_IINDPM_STEP_uA) + 867 BQ256XX_IINDPM_OFFSET_uA; 868 } 869 870 static int bq256xx_set_input_curr_lim(struct bq256xx_device *bq, int iindpm) 871 { 872 unsigned int iindpm_reg_code; 873 874 iindpm = clamp(iindpm, BQ256XX_IINDPM_MIN_uA, BQ256XX_IINDPM_MAX_uA); 875 876 iindpm_reg_code = (iindpm - BQ256XX_IINDPM_OFFSET_uA) / 877 BQ256XX_IINDPM_STEP_uA; 878 879 return regmap_update_bits(bq->regmap, BQ256XX_INPUT_CURRENT_LIMIT, 880 BQ256XX_IINDPM_MASK, iindpm_reg_code); 881 } 882 883 static void bq256xx_charger_reset(void *data) 884 { 885 struct bq256xx_device *bq = data; 886 887 regmap_update_bits(bq->regmap, BQ256XX_PART_INFORMATION, 888 BQ256XX_REG_RST, BQ256XX_REG_RST); 889 890 if (!IS_ERR_OR_NULL(bq->usb2_phy)) 891 usb_unregister_notifier(bq->usb2_phy, &bq->usb_nb); 892 893 if (!IS_ERR_OR_NULL(bq->usb3_phy)) 894 usb_unregister_notifier(bq->usb3_phy, &bq->usb_nb); 895 } 896 897 static int bq256xx_set_charger_property(struct power_supply *psy, 898 enum power_supply_property prop, 899 const union power_supply_propval *val) 900 { 901 struct bq256xx_device *bq = power_supply_get_drvdata(psy); 902 int ret = -EINVAL; 903 904 switch (prop) { 905 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: 906 ret = bq->chip_info->bq256xx_set_iindpm(bq, val->intval); 907 if (ret) 908 return ret; 909 break; 910 911 case POWER_SUPPLY_PROP_STATUS: 912 break; 913 914 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: 915 ret = bq->chip_info->bq256xx_set_vbatreg(bq, val->intval); 916 if (ret) 917 return ret; 918 break; 919 920 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: 921 ret = bq->chip_info->bq256xx_set_ichg(bq, val->intval); 922 if (ret) 923 return ret; 924 break; 925 926 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT: 927 ret = bq->chip_info->bq256xx_set_iprechg(bq, val->intval); 928 if (ret) 929 return ret; 930 break; 931 932 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT: 933 ret = bq->chip_info->bq256xx_set_iterm(bq, val->intval); 934 if (ret) 935 return ret; 936 break; 937 938 case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT: 939 ret = bq->chip_info->bq256xx_set_vindpm(bq, val->intval); 940 if (ret) 941 return ret; 942 break; 943 944 case POWER_SUPPLY_PROP_CHARGE_TYPE: 945 ret = bq->chip_info->bq256xx_set_charge_type(bq, val->intval); 946 if (ret) 947 return ret; 948 break; 949 950 default: 951 break; 952 } 953 954 return ret; 955 } 956 957 958 static int bq256xx_get_battery_property(struct power_supply *psy, 959 enum power_supply_property psp, 960 union power_supply_propval *val) 961 { 962 struct bq256xx_device *bq = power_supply_get_drvdata(psy); 963 964 switch (psp) { 965 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: 966 val->intval = bq->init_data.ichg_max; 967 break; 968 969 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: 970 val->intval = bq->init_data.vbatreg_max; 971 break; 972 973 default: 974 return -EINVAL; 975 } 976 977 return 0; 978 } 979 980 static int bq256xx_get_charger_property(struct power_supply *psy, 981 enum power_supply_property psp, 982 union power_supply_propval *val) 983 { 984 struct bq256xx_device *bq = power_supply_get_drvdata(psy); 985 struct bq256xx_state state; 986 int ret = 0; 987 988 mutex_lock(&bq->lock); 989 ret = bq256xx_get_state(bq, &state); 990 mutex_unlock(&bq->lock); 991 if (ret) 992 return ret; 993 994 switch (psp) { 995 case POWER_SUPPLY_PROP_STATUS: 996 if (state.vbus_stat == BQ256XX_VBUS_STAT_NO_INPUT || 997 state.vbus_stat == BQ256XX_VBUS_STAT_USB_OTG) 998 val->intval = POWER_SUPPLY_STATUS_DISCHARGING; 999 else if (state.chrg_stat == BQ256XX_CHRG_STAT_NOT_CHRGING) 1000 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; 1001 else if (state.chrg_stat == BQ256XX_CHRG_STAT_CHRG_TERM) 1002 val->intval = POWER_SUPPLY_STATUS_FULL; 1003 else 1004 val->intval = POWER_SUPPLY_STATUS_CHARGING; 1005 break; 1006 1007 case POWER_SUPPLY_PROP_HEALTH: 1008 val->intval = POWER_SUPPLY_HEALTH_UNKNOWN; 1009 if (state.wdt_fault) { 1010 val->intval = 1011 POWER_SUPPLY_HEALTH_WATCHDOG_TIMER_EXPIRE; 1012 } else if (state.bat_fault) { 1013 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE; 1014 } else { 1015 switch (state.chrg_stat) { 1016 case BQ256XX_CHRG_FAULT_INPUT: 1017 val->intval = 1018 POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; 1019 break; 1020 case BQ256XX_CHRG_FAULT_THERM: 1021 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; 1022 break; 1023 case BQ256XX_CHRG_FAULT_CST_EXPIRE: 1024 val->intval = 1025 POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE; 1026 break; 1027 default: 1028 break; 1029 } 1030 1031 switch (state.ntc_fault) { 1032 case BQ256XX_NTC_FAULT_WARM: 1033 val->intval = POWER_SUPPLY_HEALTH_WARM; 1034 break; 1035 case BQ256XX_NTC_FAULT_COOL: 1036 val->intval = POWER_SUPPLY_HEALTH_COOL; 1037 break; 1038 case BQ256XX_NTC_FAULT_COLD: 1039 val->intval = POWER_SUPPLY_HEALTH_COLD; 1040 break; 1041 case BQ256XX_NTC_FAULT_HOT: 1042 val->intval = POWER_SUPPLY_HEALTH_HOT; 1043 break; 1044 default: 1045 val->intval = POWER_SUPPLY_HEALTH_GOOD; 1046 break; 1047 } 1048 } 1049 break; 1050 1051 case POWER_SUPPLY_PROP_USB_TYPE: 1052 if (bq->chip_info->has_usb_detect) { 1053 switch (state.vbus_stat) { 1054 case BQ256XX_VBUS_STAT_USB_SDP: 1055 val->intval = POWER_SUPPLY_USB_TYPE_SDP; 1056 break; 1057 case BQ256XX_VBUS_STAT_USB_CDP: 1058 val->intval = POWER_SUPPLY_USB_TYPE_CDP; 1059 break; 1060 case BQ256XX_VBUS_STAT_USB_DCP: 1061 val->intval = POWER_SUPPLY_USB_TYPE_DCP; 1062 break; 1063 case BQ256XX_VBUS_STAT_USB_OTG: 1064 val->intval = POWER_SUPPLY_USB_TYPE_ACA; 1065 break; 1066 default: 1067 val->intval = POWER_SUPPLY_USB_TYPE_UNKNOWN; 1068 break; 1069 } 1070 } else { 1071 switch (state.vbus_stat) { 1072 case BQ256XX_VBUS_STAT_USB_SDP: 1073 val->intval = POWER_SUPPLY_USB_TYPE_SDP; 1074 break; 1075 case BQ256XX_VBUS_STAT_USB_OTG: 1076 val->intval = POWER_SUPPLY_USB_TYPE_ACA; 1077 break; 1078 default: 1079 val->intval = POWER_SUPPLY_USB_TYPE_UNKNOWN; 1080 break; 1081 } 1082 } 1083 break; 1084 1085 case POWER_SUPPLY_PROP_CHARGE_TYPE: 1086 switch (state.chrg_stat) { 1087 case BQ256XX_CHRG_STAT_NOT_CHRGING: 1088 val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE; 1089 break; 1090 case BQ256XX_CHRG_STAT_PRECHRGING: 1091 val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE; 1092 break; 1093 case BQ256XX_CHRG_STAT_FAST_CHRGING: 1094 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST; 1095 break; 1096 case BQ256XX_CHRG_STAT_CHRG_TERM: 1097 val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE; 1098 break; 1099 default: 1100 val->intval = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN; 1101 } 1102 break; 1103 1104 case POWER_SUPPLY_PROP_MANUFACTURER: 1105 val->strval = BQ256XX_MANUFACTURER; 1106 break; 1107 1108 case POWER_SUPPLY_PROP_MODEL_NAME: 1109 val->strval = bq->model_name; 1110 break; 1111 1112 case POWER_SUPPLY_PROP_ONLINE: 1113 val->intval = state.online; 1114 break; 1115 1116 case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT: 1117 ret = bq->chip_info->bq256xx_get_vindpm(bq); 1118 if (ret < 0) 1119 return ret; 1120 val->intval = ret; 1121 break; 1122 1123 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: 1124 ret = bq->chip_info->bq256xx_get_iindpm(bq); 1125 if (ret < 0) 1126 return ret; 1127 val->intval = ret; 1128 break; 1129 1130 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: 1131 ret = bq->chip_info->bq256xx_get_vbatreg(bq); 1132 if (ret < 0) 1133 return ret; 1134 val->intval = ret; 1135 break; 1136 1137 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: 1138 ret = bq->chip_info->bq256xx_get_ichg(bq); 1139 if (ret < 0) 1140 return ret; 1141 val->intval = ret; 1142 break; 1143 1144 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT: 1145 ret = bq->chip_info->bq256xx_get_iprechg(bq); 1146 if (ret < 0) 1147 return ret; 1148 val->intval = ret; 1149 break; 1150 1151 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT: 1152 ret = bq->chip_info->bq256xx_get_iterm(bq); 1153 if (ret < 0) 1154 return ret; 1155 val->intval = ret; 1156 break; 1157 1158 default: 1159 return -EINVAL; 1160 } 1161 1162 return ret; 1163 } 1164 1165 static bool bq256xx_state_changed(struct bq256xx_device *bq, 1166 struct bq256xx_state *new_state) 1167 { 1168 struct bq256xx_state old_state; 1169 1170 mutex_lock(&bq->lock); 1171 old_state = bq->state; 1172 mutex_unlock(&bq->lock); 1173 1174 return memcmp(&old_state, new_state, sizeof(struct bq256xx_state)) != 0; 1175 } 1176 1177 static irqreturn_t bq256xx_irq_handler_thread(int irq, void *private) 1178 { 1179 struct bq256xx_device *bq = private; 1180 struct bq256xx_state state; 1181 int ret; 1182 1183 ret = bq256xx_get_state(bq, &state); 1184 if (ret < 0) 1185 goto irq_out; 1186 1187 if (!bq256xx_state_changed(bq, &state)) 1188 goto irq_out; 1189 1190 mutex_lock(&bq->lock); 1191 bq->state = state; 1192 mutex_unlock(&bq->lock); 1193 1194 power_supply_changed(bq->charger); 1195 1196 irq_out: 1197 return IRQ_HANDLED; 1198 } 1199 1200 static enum power_supply_property bq256xx_power_supply_props[] = { 1201 POWER_SUPPLY_PROP_MANUFACTURER, 1202 POWER_SUPPLY_PROP_MODEL_NAME, 1203 POWER_SUPPLY_PROP_STATUS, 1204 POWER_SUPPLY_PROP_ONLINE, 1205 POWER_SUPPLY_PROP_HEALTH, 1206 POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT, 1207 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, 1208 POWER_SUPPLY_PROP_CHARGE_TYPE, 1209 POWER_SUPPLY_PROP_USB_TYPE, 1210 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT, 1211 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, 1212 POWER_SUPPLY_PROP_PRECHARGE_CURRENT, 1213 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT, 1214 }; 1215 1216 static enum power_supply_property bq256xx_battery_props[] = { 1217 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, 1218 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, 1219 }; 1220 1221 static int bq256xx_property_is_writeable(struct power_supply *psy, 1222 enum power_supply_property prop) 1223 { 1224 switch (prop) { 1225 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: 1226 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: 1227 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: 1228 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT: 1229 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT: 1230 case POWER_SUPPLY_PROP_STATUS: 1231 case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT: 1232 case POWER_SUPPLY_PROP_CHARGE_TYPE: 1233 return true; 1234 default: 1235 return false; 1236 } 1237 } 1238 1239 static const struct power_supply_desc bq256xx_power_supply_desc = { 1240 .name = "bq256xx-charger", 1241 .type = POWER_SUPPLY_TYPE_USB, 1242 .usb_types = bq256xx_usb_type, 1243 .num_usb_types = ARRAY_SIZE(bq256xx_usb_type), 1244 .properties = bq256xx_power_supply_props, 1245 .num_properties = ARRAY_SIZE(bq256xx_power_supply_props), 1246 .get_property = bq256xx_get_charger_property, 1247 .set_property = bq256xx_set_charger_property, 1248 .property_is_writeable = bq256xx_property_is_writeable, 1249 }; 1250 1251 static struct power_supply_desc bq256xx_battery_desc = { 1252 .name = "bq256xx-battery", 1253 .type = POWER_SUPPLY_TYPE_BATTERY, 1254 .get_property = bq256xx_get_battery_property, 1255 .properties = bq256xx_battery_props, 1256 .num_properties = ARRAY_SIZE(bq256xx_battery_props), 1257 .property_is_writeable = bq256xx_property_is_writeable, 1258 }; 1259 1260 1261 static bool bq256xx_is_volatile_reg(struct device *dev, unsigned int reg) 1262 { 1263 switch (reg) { 1264 case BQ256XX_INPUT_CURRENT_LIMIT: 1265 case BQ256XX_CHARGER_STATUS_0...BQ256XX_CHARGER_STATUS_2: 1266 return true; 1267 default: 1268 return false; 1269 } 1270 } 1271 1272 static const struct regmap_config bq25600_regmap_config = { 1273 .reg_bits = 8, 1274 .val_bits = 8, 1275 1276 .max_register = BQ256XX_PART_INFORMATION, 1277 .reg_defaults = bq2560x_reg_defs, 1278 .num_reg_defaults = ARRAY_SIZE(bq2560x_reg_defs), 1279 .cache_type = REGCACHE_FLAT, 1280 .volatile_reg = bq256xx_is_volatile_reg, 1281 }; 1282 1283 static const struct regmap_config bq25611d_regmap_config = { 1284 .reg_bits = 8, 1285 .val_bits = 8, 1286 1287 .max_register = BQ256XX_CHARGER_CONTROL_4, 1288 .reg_defaults = bq25611d_reg_defs, 1289 .num_reg_defaults = ARRAY_SIZE(bq25611d_reg_defs), 1290 .cache_type = REGCACHE_FLAT, 1291 .volatile_reg = bq256xx_is_volatile_reg, 1292 }; 1293 1294 static const struct regmap_config bq25618_619_regmap_config = { 1295 .reg_bits = 8, 1296 .val_bits = 8, 1297 1298 .max_register = BQ256XX_CHARGER_CONTROL_4, 1299 .reg_defaults = bq25618_619_reg_defs, 1300 .num_reg_defaults = ARRAY_SIZE(bq25618_619_reg_defs), 1301 .cache_type = REGCACHE_FLAT, 1302 .volatile_reg = bq256xx_is_volatile_reg, 1303 }; 1304 1305 static const struct bq256xx_chip_info bq256xx_chip_info_tbl[] = { 1306 [BQ25600] = { 1307 .model_id = BQ25600, 1308 .bq256xx_regmap_config = &bq25600_regmap_config, 1309 .bq256xx_get_ichg = bq256xx_get_ichg_curr, 1310 .bq256xx_get_iindpm = bq256xx_get_input_curr_lim, 1311 .bq256xx_get_vbatreg = bq2560x_get_chrg_volt, 1312 .bq256xx_get_iterm = bq256xx_get_term_curr, 1313 .bq256xx_get_iprechg = bq256xx_get_prechrg_curr, 1314 .bq256xx_get_vindpm = bq256xx_get_input_volt_lim, 1315 1316 .bq256xx_set_ichg = bq256xx_set_ichg_curr, 1317 .bq256xx_set_iindpm = bq256xx_set_input_curr_lim, 1318 .bq256xx_set_vbatreg = bq2560x_set_chrg_volt, 1319 .bq256xx_set_iterm = bq256xx_set_term_curr, 1320 .bq256xx_set_iprechg = bq256xx_set_prechrg_curr, 1321 .bq256xx_set_vindpm = bq256xx_set_input_volt_lim, 1322 .bq256xx_set_charge_type = bq256xx_set_charge_type, 1323 1324 .bq256xx_def_ichg = BQ2560X_ICHG_DEF_uA, 1325 .bq256xx_def_iindpm = BQ256XX_IINDPM_DEF_uA, 1326 .bq256xx_def_vbatreg = BQ2560X_VBATREG_DEF_uV, 1327 .bq256xx_def_iterm = BQ256XX_ITERM_DEF_uA, 1328 .bq256xx_def_iprechg = BQ256XX_IPRECHG_DEF_uA, 1329 .bq256xx_def_vindpm = BQ256XX_VINDPM_DEF_uV, 1330 1331 .bq256xx_max_ichg = BQ256XX_ICHG_MAX_uA, 1332 .bq256xx_max_vbatreg = BQ2560X_VBATREG_MAX_uV, 1333 1334 .has_usb_detect = false, 1335 }, 1336 1337 [BQ25600D] = { 1338 .model_id = BQ25600D, 1339 .bq256xx_regmap_config = &bq25600_regmap_config, 1340 .bq256xx_get_ichg = bq256xx_get_ichg_curr, 1341 .bq256xx_get_iindpm = bq256xx_get_input_curr_lim, 1342 .bq256xx_get_vbatreg = bq2560x_get_chrg_volt, 1343 .bq256xx_get_iterm = bq256xx_get_term_curr, 1344 .bq256xx_get_iprechg = bq256xx_get_prechrg_curr, 1345 .bq256xx_get_vindpm = bq256xx_get_input_volt_lim, 1346 1347 .bq256xx_set_ichg = bq256xx_set_ichg_curr, 1348 .bq256xx_set_iindpm = bq256xx_set_input_curr_lim, 1349 .bq256xx_set_vbatreg = bq2560x_set_chrg_volt, 1350 .bq256xx_set_iterm = bq256xx_set_term_curr, 1351 .bq256xx_set_iprechg = bq256xx_set_prechrg_curr, 1352 .bq256xx_set_vindpm = bq256xx_set_input_volt_lim, 1353 .bq256xx_set_charge_type = bq256xx_set_charge_type, 1354 1355 .bq256xx_def_ichg = BQ2560X_ICHG_DEF_uA, 1356 .bq256xx_def_iindpm = BQ256XX_IINDPM_DEF_uA, 1357 .bq256xx_def_vbatreg = BQ2560X_VBATREG_DEF_uV, 1358 .bq256xx_def_iterm = BQ256XX_ITERM_DEF_uA, 1359 .bq256xx_def_iprechg = BQ256XX_IPRECHG_DEF_uA, 1360 .bq256xx_def_vindpm = BQ256XX_VINDPM_DEF_uV, 1361 1362 .bq256xx_max_ichg = BQ256XX_ICHG_MAX_uA, 1363 .bq256xx_max_vbatreg = BQ2560X_VBATREG_MAX_uV, 1364 1365 .has_usb_detect = true, 1366 }, 1367 1368 [BQ25601] = { 1369 .model_id = BQ25601, 1370 .bq256xx_regmap_config = &bq25600_regmap_config, 1371 .bq256xx_get_ichg = bq256xx_get_ichg_curr, 1372 .bq256xx_get_iindpm = bq256xx_get_input_curr_lim, 1373 .bq256xx_get_vbatreg = bq2560x_get_chrg_volt, 1374 .bq256xx_get_iterm = bq256xx_get_term_curr, 1375 .bq256xx_get_iprechg = bq256xx_get_prechrg_curr, 1376 .bq256xx_get_vindpm = bq256xx_get_input_volt_lim, 1377 1378 .bq256xx_set_ichg = bq256xx_set_ichg_curr, 1379 .bq256xx_set_iindpm = bq256xx_set_input_curr_lim, 1380 .bq256xx_set_vbatreg = bq2560x_set_chrg_volt, 1381 .bq256xx_set_iterm = bq256xx_set_term_curr, 1382 .bq256xx_set_iprechg = bq256xx_set_prechrg_curr, 1383 .bq256xx_set_vindpm = bq256xx_set_input_volt_lim, 1384 .bq256xx_set_charge_type = bq256xx_set_charge_type, 1385 1386 .bq256xx_def_ichg = BQ2560X_ICHG_DEF_uA, 1387 .bq256xx_def_iindpm = BQ256XX_IINDPM_DEF_uA, 1388 .bq256xx_def_vbatreg = BQ2560X_VBATREG_DEF_uV, 1389 .bq256xx_def_iterm = BQ256XX_ITERM_DEF_uA, 1390 .bq256xx_def_iprechg = BQ256XX_IPRECHG_DEF_uA, 1391 .bq256xx_def_vindpm = BQ256XX_VINDPM_DEF_uV, 1392 1393 .bq256xx_max_ichg = BQ256XX_ICHG_MAX_uA, 1394 .bq256xx_max_vbatreg = BQ2560X_VBATREG_MAX_uV, 1395 1396 .has_usb_detect = false, 1397 }, 1398 1399 [BQ25601D] = { 1400 .model_id = BQ25601D, 1401 .bq256xx_regmap_config = &bq25600_regmap_config, 1402 .bq256xx_get_ichg = bq256xx_get_ichg_curr, 1403 .bq256xx_get_iindpm = bq256xx_get_input_curr_lim, 1404 .bq256xx_get_vbatreg = bq25601d_get_chrg_volt, 1405 .bq256xx_get_iterm = bq256xx_get_term_curr, 1406 .bq256xx_get_iprechg = bq256xx_get_prechrg_curr, 1407 .bq256xx_get_vindpm = bq256xx_get_input_volt_lim, 1408 1409 .bq256xx_set_ichg = bq256xx_set_ichg_curr, 1410 .bq256xx_set_iindpm = bq256xx_set_input_curr_lim, 1411 .bq256xx_set_vbatreg = bq25601d_set_chrg_volt, 1412 .bq256xx_set_iterm = bq256xx_set_term_curr, 1413 .bq256xx_set_iprechg = bq256xx_set_prechrg_curr, 1414 .bq256xx_set_vindpm = bq256xx_set_input_volt_lim, 1415 .bq256xx_set_charge_type = bq256xx_set_charge_type, 1416 1417 .bq256xx_def_ichg = BQ2560X_ICHG_DEF_uA, 1418 .bq256xx_def_iindpm = BQ256XX_IINDPM_DEF_uA, 1419 .bq256xx_def_vbatreg = BQ2560X_VBATREG_DEF_uV, 1420 .bq256xx_def_iterm = BQ256XX_ITERM_DEF_uA, 1421 .bq256xx_def_iprechg = BQ256XX_IPRECHG_DEF_uA, 1422 .bq256xx_def_vindpm = BQ256XX_VINDPM_DEF_uV, 1423 1424 .bq256xx_max_ichg = BQ256XX_ICHG_MAX_uA, 1425 .bq256xx_max_vbatreg = BQ2560X_VBATREG_MAX_uV, 1426 1427 .has_usb_detect = true, 1428 }, 1429 1430 [BQ25611D] = { 1431 .model_id = BQ25611D, 1432 .bq256xx_regmap_config = &bq25611d_regmap_config, 1433 .bq256xx_get_ichg = bq256xx_get_ichg_curr, 1434 .bq256xx_get_iindpm = bq256xx_get_input_curr_lim, 1435 .bq256xx_get_vbatreg = bq25611d_get_chrg_volt, 1436 .bq256xx_get_iterm = bq256xx_get_term_curr, 1437 .bq256xx_get_iprechg = bq256xx_get_prechrg_curr, 1438 .bq256xx_get_vindpm = bq256xx_get_input_volt_lim, 1439 1440 .bq256xx_set_ichg = bq256xx_set_ichg_curr, 1441 .bq256xx_set_iindpm = bq256xx_set_input_curr_lim, 1442 .bq256xx_set_vbatreg = bq25611d_set_chrg_volt, 1443 .bq256xx_set_iterm = bq256xx_set_term_curr, 1444 .bq256xx_set_iprechg = bq256xx_set_prechrg_curr, 1445 .bq256xx_set_vindpm = bq256xx_set_input_volt_lim, 1446 .bq256xx_set_charge_type = bq256xx_set_charge_type, 1447 1448 .bq256xx_def_ichg = BQ25611D_ICHG_DEF_uA, 1449 .bq256xx_def_iindpm = BQ256XX_IINDPM_DEF_uA, 1450 .bq256xx_def_vbatreg = BQ25611D_VBATREG_DEF_uV, 1451 .bq256xx_def_iterm = BQ256XX_ITERM_DEF_uA, 1452 .bq256xx_def_iprechg = BQ256XX_IPRECHG_DEF_uA, 1453 .bq256xx_def_vindpm = BQ256XX_VINDPM_DEF_uV, 1454 1455 .bq256xx_max_ichg = BQ256XX_ICHG_MAX_uA, 1456 .bq256xx_max_vbatreg = BQ25611D_VBATREG_MAX_uV, 1457 1458 .has_usb_detect = true, 1459 }, 1460 1461 [BQ25618] = { 1462 .model_id = BQ25618, 1463 .bq256xx_regmap_config = &bq25618_619_regmap_config, 1464 .bq256xx_get_ichg = bq25618_619_get_ichg_curr, 1465 .bq256xx_get_iindpm = bq256xx_get_input_curr_lim, 1466 .bq256xx_get_vbatreg = bq25618_619_get_chrg_volt, 1467 .bq256xx_get_iterm = bq25618_619_get_term_curr, 1468 .bq256xx_get_iprechg = bq25618_619_get_prechrg_curr, 1469 .bq256xx_get_vindpm = bq256xx_get_input_volt_lim, 1470 1471 .bq256xx_set_ichg = bq25618_619_set_ichg_curr, 1472 .bq256xx_set_iindpm = bq256xx_set_input_curr_lim, 1473 .bq256xx_set_vbatreg = bq25618_619_set_chrg_volt, 1474 .bq256xx_set_iterm = bq25618_619_set_term_curr, 1475 .bq256xx_set_iprechg = bq25618_619_set_prechrg_curr, 1476 .bq256xx_set_vindpm = bq256xx_set_input_volt_lim, 1477 .bq256xx_set_charge_type = bq256xx_set_charge_type, 1478 1479 .bq256xx_def_ichg = BQ25618_ICHG_DEF_uA, 1480 .bq256xx_def_iindpm = BQ256XX_IINDPM_DEF_uA, 1481 .bq256xx_def_vbatreg = BQ25618_VBATREG_DEF_uV, 1482 .bq256xx_def_iterm = BQ25618_ITERM_DEF_uA, 1483 .bq256xx_def_iprechg = BQ25618_IPRECHG_DEF_uA, 1484 .bq256xx_def_vindpm = BQ256XX_VINDPM_DEF_uV, 1485 1486 .bq256xx_max_ichg = BQ25618_ICHG_MAX_uA, 1487 .bq256xx_max_vbatreg = BQ25618_VBATREG_MAX_uV, 1488 1489 .has_usb_detect = false, 1490 }, 1491 1492 [BQ25619] = { 1493 .model_id = BQ25619, 1494 .bq256xx_regmap_config = &bq25618_619_regmap_config, 1495 .bq256xx_get_ichg = bq25618_619_get_ichg_curr, 1496 .bq256xx_get_iindpm = bq256xx_get_input_curr_lim, 1497 .bq256xx_get_vbatreg = bq25618_619_get_chrg_volt, 1498 .bq256xx_get_iterm = bq25618_619_get_term_curr, 1499 .bq256xx_get_iprechg = bq25618_619_get_prechrg_curr, 1500 .bq256xx_get_vindpm = bq256xx_get_input_volt_lim, 1501 1502 .bq256xx_set_ichg = bq25618_619_set_ichg_curr, 1503 .bq256xx_set_iindpm = bq256xx_set_input_curr_lim, 1504 .bq256xx_set_vbatreg = bq25618_619_set_chrg_volt, 1505 .bq256xx_set_iterm = bq25618_619_set_term_curr, 1506 .bq256xx_set_iprechg = bq25618_619_set_prechrg_curr, 1507 .bq256xx_set_vindpm = bq256xx_set_input_volt_lim, 1508 .bq256xx_set_charge_type = bq256xx_set_charge_type, 1509 1510 .bq256xx_def_ichg = BQ25618_ICHG_DEF_uA, 1511 .bq256xx_def_iindpm = BQ256XX_IINDPM_DEF_uA, 1512 .bq256xx_def_vbatreg = BQ25618_VBATREG_DEF_uV, 1513 .bq256xx_def_iterm = BQ25618_ITERM_DEF_uA, 1514 .bq256xx_def_iprechg = BQ25618_IPRECHG_DEF_uA, 1515 .bq256xx_def_vindpm = BQ256XX_VINDPM_DEF_uV, 1516 1517 .bq256xx_max_ichg = BQ25618_ICHG_MAX_uA, 1518 .bq256xx_max_vbatreg = BQ25618_VBATREG_MAX_uV, 1519 1520 .has_usb_detect = false, 1521 }, 1522 }; 1523 1524 static int bq256xx_power_supply_init(struct bq256xx_device *bq, 1525 struct power_supply_config *psy_cfg, struct device *dev) 1526 { 1527 bq->charger = devm_power_supply_register(bq->dev, 1528 &bq256xx_power_supply_desc, 1529 psy_cfg); 1530 if (IS_ERR(bq->charger)) { 1531 dev_err(dev, "power supply register charger failed\n"); 1532 return PTR_ERR(bq->charger); 1533 } 1534 1535 bq->battery = devm_power_supply_register(bq->dev, 1536 &bq256xx_battery_desc, 1537 psy_cfg); 1538 if (IS_ERR(bq->battery)) { 1539 dev_err(dev, "power supply register battery failed\n"); 1540 return PTR_ERR(bq->battery); 1541 } 1542 return 0; 1543 } 1544 1545 static int bq256xx_hw_init(struct bq256xx_device *bq) 1546 { 1547 struct power_supply_battery_info *bat_info; 1548 int wd_reg_val = BQ256XX_WATCHDOG_DIS; 1549 int ret = 0; 1550 int i; 1551 1552 for (i = 0; i < BQ256XX_NUM_WD_VAL; i++) { 1553 if (bq->watchdog_timer == bq256xx_watchdog_time[i]) { 1554 wd_reg_val = i; 1555 break; 1556 } 1557 if (bq->watchdog_timer > bq256xx_watchdog_time[i] && 1558 bq->watchdog_timer < bq256xx_watchdog_time[i + 1]) 1559 wd_reg_val = i; 1560 } 1561 ret = regmap_update_bits(bq->regmap, BQ256XX_CHARGER_CONTROL_1, 1562 BQ256XX_WATCHDOG_MASK, wd_reg_val << 1563 BQ256XX_WDT_BIT_SHIFT); 1564 1565 ret = power_supply_get_battery_info(bq->charger, &bat_info); 1566 if (ret == -ENOMEM) 1567 return ret; 1568 1569 if (ret) { 1570 dev_warn(bq->dev, "battery info missing, default values will be applied\n"); 1571 1572 bat_info->constant_charge_current_max_ua = 1573 bq->chip_info->bq256xx_def_ichg; 1574 1575 bat_info->constant_charge_voltage_max_uv = 1576 bq->chip_info->bq256xx_def_vbatreg; 1577 1578 bat_info->precharge_current_ua = 1579 bq->chip_info->bq256xx_def_iprechg; 1580 1581 bat_info->charge_term_current_ua = 1582 bq->chip_info->bq256xx_def_iterm; 1583 1584 bq->init_data.ichg_max = 1585 bq->chip_info->bq256xx_max_ichg; 1586 1587 bq->init_data.vbatreg_max = 1588 bq->chip_info->bq256xx_max_vbatreg; 1589 } else { 1590 bq->init_data.ichg_max = 1591 bat_info->constant_charge_current_max_ua; 1592 1593 bq->init_data.vbatreg_max = 1594 bat_info->constant_charge_voltage_max_uv; 1595 } 1596 1597 ret = bq->chip_info->bq256xx_set_vindpm(bq, bq->init_data.vindpm); 1598 if (ret) 1599 return ret; 1600 1601 ret = bq->chip_info->bq256xx_set_iindpm(bq, bq->init_data.iindpm); 1602 if (ret) 1603 return ret; 1604 1605 ret = bq->chip_info->bq256xx_set_ichg(bq, 1606 bq->chip_info->bq256xx_def_ichg); 1607 if (ret) 1608 return ret; 1609 1610 ret = bq->chip_info->bq256xx_set_iprechg(bq, 1611 bat_info->precharge_current_ua); 1612 if (ret) 1613 return ret; 1614 1615 ret = bq->chip_info->bq256xx_set_vbatreg(bq, 1616 bq->chip_info->bq256xx_def_vbatreg); 1617 if (ret) 1618 return ret; 1619 1620 ret = bq->chip_info->bq256xx_set_iterm(bq, 1621 bat_info->charge_term_current_ua); 1622 if (ret) 1623 return ret; 1624 1625 power_supply_put_battery_info(bq->charger, bat_info); 1626 1627 return 0; 1628 } 1629 1630 static int bq256xx_parse_dt(struct bq256xx_device *bq, 1631 struct power_supply_config *psy_cfg, struct device *dev) 1632 { 1633 int ret = 0; 1634 1635 psy_cfg->drv_data = bq; 1636 psy_cfg->of_node = dev->of_node; 1637 1638 ret = device_property_read_u32(bq->dev, "ti,watchdog-timeout-ms", 1639 &bq->watchdog_timer); 1640 if (ret) 1641 bq->watchdog_timer = BQ256XX_WATCHDOG_DIS; 1642 1643 if (bq->watchdog_timer > BQ256XX_WATCHDOG_MAX || 1644 bq->watchdog_timer < BQ256XX_WATCHDOG_DIS) 1645 return -EINVAL; 1646 1647 ret = device_property_read_u32(bq->dev, 1648 "input-voltage-limit-microvolt", 1649 &bq->init_data.vindpm); 1650 if (ret) 1651 bq->init_data.vindpm = bq->chip_info->bq256xx_def_vindpm; 1652 1653 ret = device_property_read_u32(bq->dev, 1654 "input-current-limit-microamp", 1655 &bq->init_data.iindpm); 1656 if (ret) 1657 bq->init_data.iindpm = bq->chip_info->bq256xx_def_iindpm; 1658 1659 return 0; 1660 } 1661 1662 static int bq256xx_probe(struct i2c_client *client) 1663 { 1664 const struct i2c_device_id *id = i2c_client_get_device_id(client); 1665 struct device *dev = &client->dev; 1666 struct bq256xx_device *bq; 1667 struct power_supply_config psy_cfg = { }; 1668 1669 int ret; 1670 1671 bq = devm_kzalloc(dev, sizeof(*bq), GFP_KERNEL); 1672 if (!bq) 1673 return -ENOMEM; 1674 1675 bq->client = client; 1676 bq->dev = dev; 1677 bq->chip_info = &bq256xx_chip_info_tbl[id->driver_data]; 1678 1679 mutex_init(&bq->lock); 1680 1681 strncpy(bq->model_name, id->name, I2C_NAME_SIZE); 1682 1683 bq->regmap = devm_regmap_init_i2c(client, 1684 bq->chip_info->bq256xx_regmap_config); 1685 1686 if (IS_ERR(bq->regmap)) { 1687 dev_err(dev, "Failed to allocate register map\n"); 1688 return PTR_ERR(bq->regmap); 1689 } 1690 1691 i2c_set_clientdata(client, bq); 1692 1693 ret = bq256xx_parse_dt(bq, &psy_cfg, dev); 1694 if (ret) { 1695 dev_err(dev, "Failed to read device tree properties%d\n", ret); 1696 return ret; 1697 } 1698 1699 ret = devm_add_action_or_reset(dev, bq256xx_charger_reset, bq); 1700 if (ret) 1701 return ret; 1702 1703 /* OTG reporting */ 1704 bq->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2); 1705 if (!IS_ERR_OR_NULL(bq->usb2_phy)) { 1706 INIT_WORK(&bq->usb_work, bq256xx_usb_work); 1707 bq->usb_nb.notifier_call = bq256xx_usb_notifier; 1708 usb_register_notifier(bq->usb2_phy, &bq->usb_nb); 1709 } 1710 1711 bq->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3); 1712 if (!IS_ERR_OR_NULL(bq->usb3_phy)) { 1713 INIT_WORK(&bq->usb_work, bq256xx_usb_work); 1714 bq->usb_nb.notifier_call = bq256xx_usb_notifier; 1715 usb_register_notifier(bq->usb3_phy, &bq->usb_nb); 1716 } 1717 1718 if (client->irq) { 1719 ret = devm_request_threaded_irq(dev, client->irq, NULL, 1720 bq256xx_irq_handler_thread, 1721 IRQF_TRIGGER_FALLING | 1722 IRQF_ONESHOT, 1723 dev_name(&client->dev), bq); 1724 if (ret < 0) { 1725 dev_err(dev, "get irq fail: %d\n", ret); 1726 return ret; 1727 } 1728 } 1729 1730 ret = bq256xx_power_supply_init(bq, &psy_cfg, dev); 1731 if (ret) { 1732 dev_err(dev, "Failed to register power supply\n"); 1733 return ret; 1734 } 1735 1736 ret = bq256xx_hw_init(bq); 1737 if (ret) { 1738 dev_err(dev, "Cannot initialize the chip.\n"); 1739 return ret; 1740 } 1741 1742 return ret; 1743 } 1744 1745 static const struct i2c_device_id bq256xx_i2c_ids[] = { 1746 { "bq25600", BQ25600 }, 1747 { "bq25600d", BQ25600D }, 1748 { "bq25601", BQ25601 }, 1749 { "bq25601d", BQ25601D }, 1750 { "bq25611d", BQ25611D }, 1751 { "bq25618", BQ25618 }, 1752 { "bq25619", BQ25619 }, 1753 {}, 1754 }; 1755 MODULE_DEVICE_TABLE(i2c, bq256xx_i2c_ids); 1756 1757 static const struct of_device_id bq256xx_of_match[] = { 1758 { .compatible = "ti,bq25600", .data = (void *)BQ25600 }, 1759 { .compatible = "ti,bq25600d", .data = (void *)BQ25600D }, 1760 { .compatible = "ti,bq25601", .data = (void *)BQ25601 }, 1761 { .compatible = "ti,bq25601d", .data = (void *)BQ25601D }, 1762 { .compatible = "ti,bq25611d", .data = (void *)BQ25611D }, 1763 { .compatible = "ti,bq25618", .data = (void *)BQ25618 }, 1764 { .compatible = "ti,bq25619", .data = (void *)BQ25619 }, 1765 { }, 1766 }; 1767 MODULE_DEVICE_TABLE(of, bq256xx_of_match); 1768 1769 static const struct acpi_device_id bq256xx_acpi_match[] = { 1770 { "bq25600", BQ25600 }, 1771 { "bq25600d", BQ25600D }, 1772 { "bq25601", BQ25601 }, 1773 { "bq25601d", BQ25601D }, 1774 { "bq25611d", BQ25611D }, 1775 { "bq25618", BQ25618 }, 1776 { "bq25619", BQ25619 }, 1777 {}, 1778 }; 1779 MODULE_DEVICE_TABLE(acpi, bq256xx_acpi_match); 1780 1781 static struct i2c_driver bq256xx_driver = { 1782 .driver = { 1783 .name = "bq256xx-charger", 1784 .of_match_table = bq256xx_of_match, 1785 .acpi_match_table = bq256xx_acpi_match, 1786 }, 1787 .probe_new = bq256xx_probe, 1788 .id_table = bq256xx_i2c_ids, 1789 }; 1790 module_i2c_driver(bq256xx_driver); 1791 1792 MODULE_AUTHOR("Ricardo Rivera-Matos <r-rivera-matos@ti.com>"); 1793 MODULE_DESCRIPTION("bq256xx charger driver"); 1794 MODULE_LICENSE("GPL v2"); 1795