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