1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * bq2415x charger driver 4 * 5 * Copyright (C) 2011-2013 Pali Rohár <pali@kernel.org> 6 * 7 * Datasheets: 8 * https://www.ti.com/product/bq24150 9 * https://www.ti.com/product/bq24150a 10 * https://www.ti.com/product/bq24152 11 * https://www.ti.com/product/bq24153 12 * https://www.ti.com/product/bq24153a 13 * https://www.ti.com/product/bq24155 14 * https://www.ti.com/product/bq24157s 15 * https://www.ti.com/product/bq24158 16 */ 17 18 #include <linux/kernel.h> 19 #include <linux/module.h> 20 #include <linux/param.h> 21 #include <linux/err.h> 22 #include <linux/workqueue.h> 23 #include <linux/sysfs.h> 24 #include <linux/platform_device.h> 25 #include <linux/power_supply.h> 26 #include <linux/idr.h> 27 #include <linux/i2c.h> 28 #include <linux/slab.h> 29 #include <linux/acpi.h> 30 31 #include <linux/power/bq2415x_charger.h> 32 33 /* timeout for resetting chip timer */ 34 #define BQ2415X_TIMER_TIMEOUT 10 35 36 #define BQ2415X_REG_STATUS 0x00 37 #define BQ2415X_REG_CONTROL 0x01 38 #define BQ2415X_REG_VOLTAGE 0x02 39 #define BQ2415X_REG_VENDER 0x03 40 #define BQ2415X_REG_CURRENT 0x04 41 42 /* reset state for all registers */ 43 #define BQ2415X_RESET_STATUS BIT(6) 44 #define BQ2415X_RESET_CONTROL (BIT(4)|BIT(5)) 45 #define BQ2415X_RESET_VOLTAGE (BIT(1)|BIT(3)) 46 #define BQ2415X_RESET_CURRENT (BIT(0)|BIT(3)|BIT(7)) 47 48 /* status register */ 49 #define BQ2415X_BIT_TMR_RST 7 50 #define BQ2415X_BIT_OTG 7 51 #define BQ2415X_BIT_EN_STAT 6 52 #define BQ2415X_MASK_STAT (BIT(4)|BIT(5)) 53 #define BQ2415X_SHIFT_STAT 4 54 #define BQ2415X_BIT_BOOST 3 55 #define BQ2415X_MASK_FAULT (BIT(0)|BIT(1)|BIT(2)) 56 #define BQ2415X_SHIFT_FAULT 0 57 58 /* control register */ 59 #define BQ2415X_MASK_LIMIT (BIT(6)|BIT(7)) 60 #define BQ2415X_SHIFT_LIMIT 6 61 #define BQ2415X_MASK_VLOWV (BIT(4)|BIT(5)) 62 #define BQ2415X_SHIFT_VLOWV 4 63 #define BQ2415X_BIT_TE 3 64 #define BQ2415X_BIT_CE 2 65 #define BQ2415X_BIT_HZ_MODE 1 66 #define BQ2415X_BIT_OPA_MODE 0 67 68 /* voltage register */ 69 #define BQ2415X_MASK_VO (BIT(2)|BIT(3)|BIT(4)|BIT(5)|BIT(6)|BIT(7)) 70 #define BQ2415X_SHIFT_VO 2 71 #define BQ2415X_BIT_OTG_PL 1 72 #define BQ2415X_BIT_OTG_EN 0 73 74 /* vender register */ 75 #define BQ2415X_MASK_VENDER (BIT(5)|BIT(6)|BIT(7)) 76 #define BQ2415X_SHIFT_VENDER 5 77 #define BQ2415X_MASK_PN (BIT(3)|BIT(4)) 78 #define BQ2415X_SHIFT_PN 3 79 #define BQ2415X_MASK_REVISION (BIT(0)|BIT(1)|BIT(2)) 80 #define BQ2415X_SHIFT_REVISION 0 81 82 /* current register */ 83 #define BQ2415X_MASK_RESET BIT(7) 84 #define BQ2415X_MASK_VI_CHRG (BIT(4)|BIT(5)|BIT(6)) 85 #define BQ2415X_SHIFT_VI_CHRG 4 86 /* N/A BIT(3) */ 87 #define BQ2415X_MASK_VI_TERM (BIT(0)|BIT(1)|BIT(2)) 88 #define BQ2415X_SHIFT_VI_TERM 0 89 90 91 enum bq2415x_command { 92 BQ2415X_TIMER_RESET, 93 BQ2415X_OTG_STATUS, 94 BQ2415X_STAT_PIN_STATUS, 95 BQ2415X_STAT_PIN_ENABLE, 96 BQ2415X_STAT_PIN_DISABLE, 97 BQ2415X_CHARGE_STATUS, 98 BQ2415X_BOOST_STATUS, 99 BQ2415X_FAULT_STATUS, 100 101 BQ2415X_CHARGE_TERMINATION_STATUS, 102 BQ2415X_CHARGE_TERMINATION_ENABLE, 103 BQ2415X_CHARGE_TERMINATION_DISABLE, 104 BQ2415X_CHARGER_STATUS, 105 BQ2415X_CHARGER_ENABLE, 106 BQ2415X_CHARGER_DISABLE, 107 BQ2415X_HIGH_IMPEDANCE_STATUS, 108 BQ2415X_HIGH_IMPEDANCE_ENABLE, 109 BQ2415X_HIGH_IMPEDANCE_DISABLE, 110 BQ2415X_BOOST_MODE_STATUS, 111 BQ2415X_BOOST_MODE_ENABLE, 112 BQ2415X_BOOST_MODE_DISABLE, 113 114 BQ2415X_OTG_LEVEL, 115 BQ2415X_OTG_ACTIVATE_HIGH, 116 BQ2415X_OTG_ACTIVATE_LOW, 117 BQ2415X_OTG_PIN_STATUS, 118 BQ2415X_OTG_PIN_ENABLE, 119 BQ2415X_OTG_PIN_DISABLE, 120 121 BQ2415X_VENDER_CODE, 122 BQ2415X_PART_NUMBER, 123 BQ2415X_REVISION, 124 }; 125 126 enum bq2415x_chip { 127 BQUNKNOWN, 128 BQ24150, 129 BQ24150A, 130 BQ24151, 131 BQ24151A, 132 BQ24152, 133 BQ24153, 134 BQ24153A, 135 BQ24155, 136 BQ24156, 137 BQ24156A, 138 BQ24157S, 139 BQ24158, 140 }; 141 142 static char *bq2415x_chip_name[] = { 143 "unknown", 144 "bq24150", 145 "bq24150a", 146 "bq24151", 147 "bq24151a", 148 "bq24152", 149 "bq24153", 150 "bq24153a", 151 "bq24155", 152 "bq24156", 153 "bq24156a", 154 "bq24157s", 155 "bq24158", 156 }; 157 158 struct bq2415x_device { 159 struct device *dev; 160 struct bq2415x_platform_data init_data; 161 struct power_supply *charger; 162 struct power_supply_desc charger_desc; 163 struct delayed_work work; 164 struct device_node *notify_node; 165 struct notifier_block nb; 166 enum bq2415x_mode reported_mode;/* mode reported by hook function */ 167 enum bq2415x_mode mode; /* currently configured mode */ 168 enum bq2415x_chip chip; 169 const char *timer_error; 170 char *model; 171 char *name; 172 int autotimer; /* 1 - if driver automatically reset timer, 0 - not */ 173 int automode; /* 1 - enabled, 0 - disabled; -1 - not supported */ 174 int id; 175 }; 176 177 /* each registered chip must have unique id */ 178 static DEFINE_IDR(bq2415x_id); 179 180 static DEFINE_MUTEX(bq2415x_id_mutex); 181 static DEFINE_MUTEX(bq2415x_timer_mutex); 182 static DEFINE_MUTEX(bq2415x_i2c_mutex); 183 184 /**** i2c read functions ****/ 185 186 /* read value from register */ 187 static int bq2415x_i2c_read(struct bq2415x_device *bq, u8 reg) 188 { 189 struct i2c_client *client = to_i2c_client(bq->dev); 190 struct i2c_msg msg[2]; 191 u8 val; 192 int ret; 193 194 if (!client->adapter) 195 return -ENODEV; 196 197 msg[0].addr = client->addr; 198 msg[0].flags = 0; 199 msg[0].buf = ® 200 msg[0].len = sizeof(reg); 201 msg[1].addr = client->addr; 202 msg[1].flags = I2C_M_RD; 203 msg[1].buf = &val; 204 msg[1].len = sizeof(val); 205 206 mutex_lock(&bq2415x_i2c_mutex); 207 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)); 208 mutex_unlock(&bq2415x_i2c_mutex); 209 210 if (ret < 0) 211 return ret; 212 213 return val; 214 } 215 216 /* read value from register, apply mask and right shift it */ 217 static int bq2415x_i2c_read_mask(struct bq2415x_device *bq, u8 reg, 218 u8 mask, u8 shift) 219 { 220 int ret; 221 222 if (shift > 8) 223 return -EINVAL; 224 225 ret = bq2415x_i2c_read(bq, reg); 226 if (ret < 0) 227 return ret; 228 return (ret & mask) >> shift; 229 } 230 231 /* read value from register and return one specified bit */ 232 static int bq2415x_i2c_read_bit(struct bq2415x_device *bq, u8 reg, u8 bit) 233 { 234 if (bit > 8) 235 return -EINVAL; 236 return bq2415x_i2c_read_mask(bq, reg, BIT(bit), bit); 237 } 238 239 /**** i2c write functions ****/ 240 241 /* write value to register */ 242 static int bq2415x_i2c_write(struct bq2415x_device *bq, u8 reg, u8 val) 243 { 244 struct i2c_client *client = to_i2c_client(bq->dev); 245 struct i2c_msg msg[1]; 246 u8 data[2]; 247 int ret; 248 249 data[0] = reg; 250 data[1] = val; 251 252 msg[0].addr = client->addr; 253 msg[0].flags = 0; 254 msg[0].buf = data; 255 msg[0].len = ARRAY_SIZE(data); 256 257 mutex_lock(&bq2415x_i2c_mutex); 258 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)); 259 mutex_unlock(&bq2415x_i2c_mutex); 260 261 /* i2c_transfer returns number of messages transferred */ 262 if (ret < 0) 263 return ret; 264 else if (ret != 1) 265 return -EIO; 266 267 return 0; 268 } 269 270 /* read value from register, change it with mask left shifted and write back */ 271 static int bq2415x_i2c_write_mask(struct bq2415x_device *bq, u8 reg, u8 val, 272 u8 mask, u8 shift) 273 { 274 int ret; 275 276 if (shift > 8) 277 return -EINVAL; 278 279 ret = bq2415x_i2c_read(bq, reg); 280 if (ret < 0) 281 return ret; 282 283 ret &= ~mask; 284 ret |= val << shift; 285 286 return bq2415x_i2c_write(bq, reg, ret); 287 } 288 289 /* change only one bit in register */ 290 static int bq2415x_i2c_write_bit(struct bq2415x_device *bq, u8 reg, 291 bool val, u8 bit) 292 { 293 if (bit > 8) 294 return -EINVAL; 295 return bq2415x_i2c_write_mask(bq, reg, val, BIT(bit), bit); 296 } 297 298 /**** global functions ****/ 299 300 /* exec command function */ 301 static int bq2415x_exec_command(struct bq2415x_device *bq, 302 enum bq2415x_command command) 303 { 304 int ret; 305 306 switch (command) { 307 case BQ2415X_TIMER_RESET: 308 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, 309 1, BQ2415X_BIT_TMR_RST); 310 case BQ2415X_OTG_STATUS: 311 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS, 312 BQ2415X_BIT_OTG); 313 case BQ2415X_STAT_PIN_STATUS: 314 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS, 315 BQ2415X_BIT_EN_STAT); 316 case BQ2415X_STAT_PIN_ENABLE: 317 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, 1, 318 BQ2415X_BIT_EN_STAT); 319 case BQ2415X_STAT_PIN_DISABLE: 320 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, 0, 321 BQ2415X_BIT_EN_STAT); 322 case BQ2415X_CHARGE_STATUS: 323 return bq2415x_i2c_read_mask(bq, BQ2415X_REG_STATUS, 324 BQ2415X_MASK_STAT, BQ2415X_SHIFT_STAT); 325 case BQ2415X_BOOST_STATUS: 326 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS, 327 BQ2415X_BIT_BOOST); 328 case BQ2415X_FAULT_STATUS: 329 return bq2415x_i2c_read_mask(bq, BQ2415X_REG_STATUS, 330 BQ2415X_MASK_FAULT, BQ2415X_SHIFT_FAULT); 331 332 case BQ2415X_CHARGE_TERMINATION_STATUS: 333 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL, 334 BQ2415X_BIT_TE); 335 case BQ2415X_CHARGE_TERMINATION_ENABLE: 336 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL, 337 1, BQ2415X_BIT_TE); 338 case BQ2415X_CHARGE_TERMINATION_DISABLE: 339 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL, 340 0, BQ2415X_BIT_TE); 341 case BQ2415X_CHARGER_STATUS: 342 ret = bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL, 343 BQ2415X_BIT_CE); 344 if (ret < 0) 345 return ret; 346 return ret > 0 ? 0 : 1; 347 case BQ2415X_CHARGER_ENABLE: 348 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL, 349 0, BQ2415X_BIT_CE); 350 case BQ2415X_CHARGER_DISABLE: 351 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL, 352 1, BQ2415X_BIT_CE); 353 case BQ2415X_HIGH_IMPEDANCE_STATUS: 354 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL, 355 BQ2415X_BIT_HZ_MODE); 356 case BQ2415X_HIGH_IMPEDANCE_ENABLE: 357 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL, 358 1, BQ2415X_BIT_HZ_MODE); 359 case BQ2415X_HIGH_IMPEDANCE_DISABLE: 360 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL, 361 0, BQ2415X_BIT_HZ_MODE); 362 case BQ2415X_BOOST_MODE_STATUS: 363 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL, 364 BQ2415X_BIT_OPA_MODE); 365 case BQ2415X_BOOST_MODE_ENABLE: 366 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL, 367 1, BQ2415X_BIT_OPA_MODE); 368 case BQ2415X_BOOST_MODE_DISABLE: 369 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL, 370 0, BQ2415X_BIT_OPA_MODE); 371 372 case BQ2415X_OTG_LEVEL: 373 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_VOLTAGE, 374 BQ2415X_BIT_OTG_PL); 375 case BQ2415X_OTG_ACTIVATE_HIGH: 376 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE, 377 1, BQ2415X_BIT_OTG_PL); 378 case BQ2415X_OTG_ACTIVATE_LOW: 379 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE, 380 0, BQ2415X_BIT_OTG_PL); 381 case BQ2415X_OTG_PIN_STATUS: 382 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_VOLTAGE, 383 BQ2415X_BIT_OTG_EN); 384 case BQ2415X_OTG_PIN_ENABLE: 385 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE, 386 1, BQ2415X_BIT_OTG_EN); 387 case BQ2415X_OTG_PIN_DISABLE: 388 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE, 389 0, BQ2415X_BIT_OTG_EN); 390 391 case BQ2415X_VENDER_CODE: 392 return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER, 393 BQ2415X_MASK_VENDER, BQ2415X_SHIFT_VENDER); 394 case BQ2415X_PART_NUMBER: 395 return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER, 396 BQ2415X_MASK_PN, BQ2415X_SHIFT_PN); 397 case BQ2415X_REVISION: 398 return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER, 399 BQ2415X_MASK_REVISION, BQ2415X_SHIFT_REVISION); 400 } 401 return -EINVAL; 402 } 403 404 /* detect chip type */ 405 static enum bq2415x_chip bq2415x_detect_chip(struct bq2415x_device *bq) 406 { 407 struct i2c_client *client = to_i2c_client(bq->dev); 408 int ret = bq2415x_exec_command(bq, BQ2415X_PART_NUMBER); 409 410 if (ret < 0) 411 return ret; 412 413 switch (client->addr) { 414 case 0x6b: 415 switch (ret) { 416 case 0: 417 if (bq->chip == BQ24151A) 418 return bq->chip; 419 return BQ24151; 420 case 1: 421 if (bq->chip == BQ24150A || 422 bq->chip == BQ24152 || 423 bq->chip == BQ24155) 424 return bq->chip; 425 return BQ24150; 426 case 2: 427 if (bq->chip == BQ24153A) 428 return bq->chip; 429 return BQ24153; 430 default: 431 return BQUNKNOWN; 432 } 433 break; 434 435 case 0x6a: 436 switch (ret) { 437 case 0: 438 if (bq->chip == BQ24156A) 439 return bq->chip; 440 return BQ24156; 441 case 2: 442 if (bq->chip == BQ24157S) 443 return bq->chip; 444 return BQ24158; 445 default: 446 return BQUNKNOWN; 447 } 448 break; 449 } 450 451 return BQUNKNOWN; 452 } 453 454 /* detect chip revision */ 455 static int bq2415x_detect_revision(struct bq2415x_device *bq) 456 { 457 int ret = bq2415x_exec_command(bq, BQ2415X_REVISION); 458 int chip = bq2415x_detect_chip(bq); 459 460 if (ret < 0 || chip < 0) 461 return -1; 462 463 switch (chip) { 464 case BQ24150: 465 case BQ24150A: 466 case BQ24151: 467 case BQ24151A: 468 case BQ24152: 469 if (ret >= 0 && ret <= 3) 470 return ret; 471 return -1; 472 case BQ24153: 473 case BQ24153A: 474 case BQ24156: 475 case BQ24156A: 476 case BQ24157S: 477 case BQ24158: 478 if (ret == 3) 479 return 0; 480 else if (ret == 1) 481 return 1; 482 return -1; 483 case BQ24155: 484 if (ret == 3) 485 return 3; 486 return -1; 487 case BQUNKNOWN: 488 return -1; 489 } 490 491 return -1; 492 } 493 494 /* return chip vender code */ 495 static int bq2415x_get_vender_code(struct bq2415x_device *bq) 496 { 497 int ret; 498 499 ret = bq2415x_exec_command(bq, BQ2415X_VENDER_CODE); 500 if (ret < 0) 501 return 0; 502 503 /* convert to binary */ 504 return (ret & 0x1) + 505 ((ret >> 1) & 0x1) * 10 + 506 ((ret >> 2) & 0x1) * 100; 507 } 508 509 /* reset all chip registers to default state */ 510 static void bq2415x_reset_chip(struct bq2415x_device *bq) 511 { 512 bq2415x_i2c_write(bq, BQ2415X_REG_CURRENT, BQ2415X_RESET_CURRENT); 513 bq2415x_i2c_write(bq, BQ2415X_REG_VOLTAGE, BQ2415X_RESET_VOLTAGE); 514 bq2415x_i2c_write(bq, BQ2415X_REG_CONTROL, BQ2415X_RESET_CONTROL); 515 bq2415x_i2c_write(bq, BQ2415X_REG_STATUS, BQ2415X_RESET_STATUS); 516 bq->timer_error = NULL; 517 } 518 519 /**** properties functions ****/ 520 521 /* set current limit in mA */ 522 static int bq2415x_set_current_limit(struct bq2415x_device *bq, int mA) 523 { 524 int val; 525 526 if (mA <= 100) 527 val = 0; 528 else if (mA <= 500) 529 val = 1; 530 else if (mA <= 800) 531 val = 2; 532 else 533 val = 3; 534 535 return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val, 536 BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT); 537 } 538 539 /* get current limit in mA */ 540 static int bq2415x_get_current_limit(struct bq2415x_device *bq) 541 { 542 int ret; 543 544 ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL, 545 BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT); 546 if (ret < 0) 547 return ret; 548 else if (ret == 0) 549 return 100; 550 else if (ret == 1) 551 return 500; 552 else if (ret == 2) 553 return 800; 554 else if (ret == 3) 555 return 1800; 556 return -EINVAL; 557 } 558 559 /* set weak battery voltage in mV */ 560 static int bq2415x_set_weak_battery_voltage(struct bq2415x_device *bq, int mV) 561 { 562 int val; 563 564 /* round to 100mV */ 565 if (mV <= 3400 + 50) 566 val = 0; 567 else if (mV <= 3500 + 50) 568 val = 1; 569 else if (mV <= 3600 + 50) 570 val = 2; 571 else 572 val = 3; 573 574 return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val, 575 BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV); 576 } 577 578 /* get weak battery voltage in mV */ 579 static int bq2415x_get_weak_battery_voltage(struct bq2415x_device *bq) 580 { 581 int ret; 582 583 ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL, 584 BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV); 585 if (ret < 0) 586 return ret; 587 return 100 * (34 + ret); 588 } 589 590 /* set battery regulation voltage in mV */ 591 static int bq2415x_set_battery_regulation_voltage(struct bq2415x_device *bq, 592 int mV) 593 { 594 int val = (mV/10 - 350) / 2; 595 596 /* 597 * According to datasheet, maximum battery regulation voltage is 598 * 4440mV which is b101111 = 47. 599 */ 600 if (val < 0) 601 val = 0; 602 else if (val > 47) 603 return -EINVAL; 604 605 return bq2415x_i2c_write_mask(bq, BQ2415X_REG_VOLTAGE, val, 606 BQ2415X_MASK_VO, BQ2415X_SHIFT_VO); 607 } 608 609 /* get battery regulation voltage in mV */ 610 static int bq2415x_get_battery_regulation_voltage(struct bq2415x_device *bq) 611 { 612 int ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_VOLTAGE, 613 BQ2415X_MASK_VO, BQ2415X_SHIFT_VO); 614 615 if (ret < 0) 616 return ret; 617 return 10 * (350 + 2*ret); 618 } 619 620 /* set charge current in mA (platform data must provide resistor sense) */ 621 static int bq2415x_set_charge_current(struct bq2415x_device *bq, int mA) 622 { 623 int val; 624 625 if (bq->init_data.resistor_sense <= 0) 626 return -EINVAL; 627 628 val = (mA * bq->init_data.resistor_sense - 37400) / 6800; 629 if (val < 0) 630 val = 0; 631 else if (val > 7) 632 val = 7; 633 634 return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CURRENT, val, 635 BQ2415X_MASK_VI_CHRG | BQ2415X_MASK_RESET, 636 BQ2415X_SHIFT_VI_CHRG); 637 } 638 639 /* get charge current in mA (platform data must provide resistor sense) */ 640 static int bq2415x_get_charge_current(struct bq2415x_device *bq) 641 { 642 int ret; 643 644 if (bq->init_data.resistor_sense <= 0) 645 return -EINVAL; 646 647 ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CURRENT, 648 BQ2415X_MASK_VI_CHRG, BQ2415X_SHIFT_VI_CHRG); 649 if (ret < 0) 650 return ret; 651 return (37400 + 6800*ret) / bq->init_data.resistor_sense; 652 } 653 654 /* set termination current in mA (platform data must provide resistor sense) */ 655 static int bq2415x_set_termination_current(struct bq2415x_device *bq, int mA) 656 { 657 int val; 658 659 if (bq->init_data.resistor_sense <= 0) 660 return -EINVAL; 661 662 val = (mA * bq->init_data.resistor_sense - 3400) / 3400; 663 if (val < 0) 664 val = 0; 665 else if (val > 7) 666 val = 7; 667 668 return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CURRENT, val, 669 BQ2415X_MASK_VI_TERM | BQ2415X_MASK_RESET, 670 BQ2415X_SHIFT_VI_TERM); 671 } 672 673 /* get termination current in mA (platform data must provide resistor sense) */ 674 static int bq2415x_get_termination_current(struct bq2415x_device *bq) 675 { 676 int ret; 677 678 if (bq->init_data.resistor_sense <= 0) 679 return -EINVAL; 680 681 ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CURRENT, 682 BQ2415X_MASK_VI_TERM, BQ2415X_SHIFT_VI_TERM); 683 if (ret < 0) 684 return ret; 685 return (3400 + 3400*ret) / bq->init_data.resistor_sense; 686 } 687 688 /* set default value of property */ 689 #define bq2415x_set_default_value(bq, prop) \ 690 do { \ 691 int ret = 0; \ 692 if (bq->init_data.prop != -1) \ 693 ret = bq2415x_set_##prop(bq, bq->init_data.prop); \ 694 if (ret < 0) \ 695 return ret; \ 696 } while (0) 697 698 /* set default values of all properties */ 699 static int bq2415x_set_defaults(struct bq2415x_device *bq) 700 { 701 bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_DISABLE); 702 bq2415x_exec_command(bq, BQ2415X_CHARGER_DISABLE); 703 bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_DISABLE); 704 705 bq2415x_set_default_value(bq, current_limit); 706 bq2415x_set_default_value(bq, weak_battery_voltage); 707 bq2415x_set_default_value(bq, battery_regulation_voltage); 708 709 if (bq->init_data.resistor_sense > 0) { 710 bq2415x_set_default_value(bq, charge_current); 711 bq2415x_set_default_value(bq, termination_current); 712 bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_ENABLE); 713 } 714 715 bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE); 716 return 0; 717 } 718 719 /**** charger mode functions ****/ 720 721 /* set charger mode */ 722 static int bq2415x_set_mode(struct bq2415x_device *bq, enum bq2415x_mode mode) 723 { 724 int ret = 0; 725 int charger = 0; 726 int boost = 0; 727 728 if (mode == BQ2415X_MODE_BOOST) 729 boost = 1; 730 else if (mode != BQ2415X_MODE_OFF) 731 charger = 1; 732 733 if (!charger) 734 ret = bq2415x_exec_command(bq, BQ2415X_CHARGER_DISABLE); 735 736 if (!boost) 737 ret = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_DISABLE); 738 739 if (ret < 0) 740 return ret; 741 742 switch (mode) { 743 case BQ2415X_MODE_OFF: 744 dev_dbg(bq->dev, "changing mode to: Offline\n"); 745 ret = bq2415x_set_current_limit(bq, 100); 746 break; 747 case BQ2415X_MODE_NONE: 748 dev_dbg(bq->dev, "changing mode to: N/A\n"); 749 ret = bq2415x_set_current_limit(bq, 100); 750 break; 751 case BQ2415X_MODE_HOST_CHARGER: 752 dev_dbg(bq->dev, "changing mode to: Host/HUB charger\n"); 753 ret = bq2415x_set_current_limit(bq, 500); 754 break; 755 case BQ2415X_MODE_DEDICATED_CHARGER: 756 dev_dbg(bq->dev, "changing mode to: Dedicated charger\n"); 757 ret = bq2415x_set_current_limit(bq, 1800); 758 break; 759 case BQ2415X_MODE_BOOST: /* Boost mode */ 760 dev_dbg(bq->dev, "changing mode to: Boost\n"); 761 ret = bq2415x_set_current_limit(bq, 100); 762 break; 763 } 764 765 if (ret < 0) 766 return ret; 767 768 if (charger) 769 ret = bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE); 770 else if (boost) 771 ret = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_ENABLE); 772 773 if (ret < 0) 774 return ret; 775 776 bq2415x_set_default_value(bq, weak_battery_voltage); 777 bq2415x_set_default_value(bq, battery_regulation_voltage); 778 779 bq->mode = mode; 780 sysfs_notify(&bq->charger->dev.kobj, NULL, "mode"); 781 782 return 0; 783 784 } 785 786 static bool bq2415x_update_reported_mode(struct bq2415x_device *bq, int mA) 787 { 788 enum bq2415x_mode mode; 789 790 if (mA == 0) 791 mode = BQ2415X_MODE_OFF; 792 else if (mA < 500) 793 mode = BQ2415X_MODE_NONE; 794 else if (mA < 1800) 795 mode = BQ2415X_MODE_HOST_CHARGER; 796 else 797 mode = BQ2415X_MODE_DEDICATED_CHARGER; 798 799 if (bq->reported_mode == mode) 800 return false; 801 802 bq->reported_mode = mode; 803 return true; 804 } 805 806 static int bq2415x_notifier_call(struct notifier_block *nb, 807 unsigned long val, void *v) 808 { 809 struct bq2415x_device *bq = 810 container_of(nb, struct bq2415x_device, nb); 811 struct power_supply *psy = v; 812 union power_supply_propval prop; 813 int ret; 814 815 if (val != PSY_EVENT_PROP_CHANGED) 816 return NOTIFY_OK; 817 818 /* Ignore event if it was not send by notify_node/notify_device */ 819 if (bq->notify_node) { 820 if (!psy->dev.parent || 821 psy->dev.parent->of_node != bq->notify_node) 822 return NOTIFY_OK; 823 } else if (bq->init_data.notify_device) { 824 if (strcmp(psy->desc->name, bq->init_data.notify_device) != 0) 825 return NOTIFY_OK; 826 } 827 828 dev_dbg(bq->dev, "notifier call was called\n"); 829 830 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_CURRENT_MAX, 831 &prop); 832 if (ret != 0) 833 return NOTIFY_OK; 834 835 if (!bq2415x_update_reported_mode(bq, prop.intval)) 836 return NOTIFY_OK; 837 838 /* if automode is not enabled do not tell about reported_mode */ 839 if (bq->automode < 1) 840 return NOTIFY_OK; 841 842 schedule_delayed_work(&bq->work, 0); 843 844 return NOTIFY_OK; 845 } 846 847 /**** timer functions ****/ 848 849 /* enable/disable auto resetting chip timer */ 850 static void bq2415x_set_autotimer(struct bq2415x_device *bq, int state) 851 { 852 mutex_lock(&bq2415x_timer_mutex); 853 854 if (bq->autotimer == state) { 855 mutex_unlock(&bq2415x_timer_mutex); 856 return; 857 } 858 859 bq->autotimer = state; 860 861 if (state) { 862 schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ); 863 bq2415x_exec_command(bq, BQ2415X_TIMER_RESET); 864 bq->timer_error = NULL; 865 } else { 866 cancel_delayed_work_sync(&bq->work); 867 } 868 869 mutex_unlock(&bq2415x_timer_mutex); 870 } 871 872 /* called by bq2415x_timer_work on timer error */ 873 static void bq2415x_timer_error(struct bq2415x_device *bq, const char *msg) 874 { 875 bq->timer_error = msg; 876 sysfs_notify(&bq->charger->dev.kobj, NULL, "timer"); 877 dev_err(bq->dev, "%s\n", msg); 878 if (bq->automode > 0) 879 bq->automode = 0; 880 bq2415x_set_mode(bq, BQ2415X_MODE_OFF); 881 bq2415x_set_autotimer(bq, 0); 882 } 883 884 /* delayed work function for auto resetting chip timer */ 885 static void bq2415x_timer_work(struct work_struct *work) 886 { 887 struct bq2415x_device *bq = container_of(work, struct bq2415x_device, 888 work.work); 889 int ret; 890 int error; 891 int boost; 892 893 if (bq->automode > 0 && (bq->reported_mode != bq->mode)) { 894 sysfs_notify(&bq->charger->dev.kobj, NULL, "reported_mode"); 895 bq2415x_set_mode(bq, bq->reported_mode); 896 } 897 898 if (!bq->autotimer) 899 return; 900 901 ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET); 902 if (ret < 0) { 903 bq2415x_timer_error(bq, "Resetting timer failed"); 904 return; 905 } 906 907 boost = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_STATUS); 908 if (boost < 0) { 909 bq2415x_timer_error(bq, "Unknown error"); 910 return; 911 } 912 913 error = bq2415x_exec_command(bq, BQ2415X_FAULT_STATUS); 914 if (error < 0) { 915 bq2415x_timer_error(bq, "Unknown error"); 916 return; 917 } 918 919 if (boost) { 920 switch (error) { 921 /* Non fatal errors, chip is OK */ 922 case 0: /* No error */ 923 break; 924 case 6: /* Timer expired */ 925 dev_err(bq->dev, "Timer expired\n"); 926 break; 927 case 3: /* Battery voltage too low */ 928 dev_err(bq->dev, "Battery voltage to low\n"); 929 break; 930 931 /* Fatal errors, disable and reset chip */ 932 case 1: /* Overvoltage protection (chip fried) */ 933 bq2415x_timer_error(bq, 934 "Overvoltage protection (chip fried)"); 935 return; 936 case 2: /* Overload */ 937 bq2415x_timer_error(bq, "Overload"); 938 return; 939 case 4: /* Battery overvoltage protection */ 940 bq2415x_timer_error(bq, 941 "Battery overvoltage protection"); 942 return; 943 case 5: /* Thermal shutdown (too hot) */ 944 bq2415x_timer_error(bq, 945 "Thermal shutdown (too hot)"); 946 return; 947 case 7: /* N/A */ 948 bq2415x_timer_error(bq, "Unknown error"); 949 return; 950 } 951 } else { 952 switch (error) { 953 /* Non fatal errors, chip is OK */ 954 case 0: /* No error */ 955 break; 956 case 2: /* Sleep mode */ 957 dev_err(bq->dev, "Sleep mode\n"); 958 break; 959 case 3: /* Poor input source */ 960 dev_err(bq->dev, "Poor input source\n"); 961 break; 962 case 6: /* Timer expired */ 963 dev_err(bq->dev, "Timer expired\n"); 964 break; 965 case 7: /* No battery */ 966 dev_err(bq->dev, "No battery\n"); 967 break; 968 969 /* Fatal errors, disable and reset chip */ 970 case 1: /* Overvoltage protection (chip fried) */ 971 bq2415x_timer_error(bq, 972 "Overvoltage protection (chip fried)"); 973 return; 974 case 4: /* Battery overvoltage protection */ 975 bq2415x_timer_error(bq, 976 "Battery overvoltage protection"); 977 return; 978 case 5: /* Thermal shutdown (too hot) */ 979 bq2415x_timer_error(bq, 980 "Thermal shutdown (too hot)"); 981 return; 982 } 983 } 984 985 schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ); 986 } 987 988 /**** power supply interface code ****/ 989 990 static enum power_supply_property bq2415x_power_supply_props[] = { 991 /* TODO: maybe add more power supply properties */ 992 POWER_SUPPLY_PROP_STATUS, 993 POWER_SUPPLY_PROP_MODEL_NAME, 994 POWER_SUPPLY_PROP_ONLINE, 995 }; 996 997 static int bq2415x_power_supply_get_property(struct power_supply *psy, 998 enum power_supply_property psp, 999 union power_supply_propval *val) 1000 { 1001 struct bq2415x_device *bq = power_supply_get_drvdata(psy); 1002 int ret; 1003 1004 switch (psp) { 1005 case POWER_SUPPLY_PROP_STATUS: 1006 ret = bq2415x_exec_command(bq, BQ2415X_CHARGE_STATUS); 1007 if (ret < 0) 1008 return ret; 1009 else if (ret == 0) /* Ready */ 1010 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; 1011 else if (ret == 1) /* Charge in progress */ 1012 val->intval = POWER_SUPPLY_STATUS_CHARGING; 1013 else if (ret == 2) /* Charge done */ 1014 val->intval = POWER_SUPPLY_STATUS_FULL; 1015 else 1016 val->intval = POWER_SUPPLY_STATUS_UNKNOWN; 1017 break; 1018 case POWER_SUPPLY_PROP_MODEL_NAME: 1019 val->strval = bq->model; 1020 break; 1021 case POWER_SUPPLY_PROP_ONLINE: 1022 /* VBUS is present for all charging and fault states, 1023 * except the 'Ready' state. 1024 */ 1025 ret = bq2415x_exec_command(bq, BQ2415X_CHARGE_STATUS); 1026 if (ret < 0) 1027 return ret; 1028 val->intval = ret > 0; 1029 break; 1030 default: 1031 return -EINVAL; 1032 } 1033 return 0; 1034 } 1035 1036 static void bq2415x_power_supply_exit(struct bq2415x_device *bq) 1037 { 1038 bq->autotimer = 0; 1039 if (bq->automode > 0) 1040 bq->automode = 0; 1041 cancel_delayed_work_sync(&bq->work); 1042 power_supply_unregister(bq->charger); 1043 kfree(bq->model); 1044 } 1045 1046 /**** additional sysfs entries for power supply interface ****/ 1047 1048 /* show *_status entries */ 1049 static ssize_t bq2415x_sysfs_show_status(struct device *dev, 1050 struct device_attribute *attr, 1051 char *buf) 1052 { 1053 struct power_supply *psy = dev_get_drvdata(dev); 1054 struct bq2415x_device *bq = power_supply_get_drvdata(psy); 1055 enum bq2415x_command command; 1056 int ret; 1057 1058 if (strcmp(attr->attr.name, "otg_status") == 0) 1059 command = BQ2415X_OTG_STATUS; 1060 else if (strcmp(attr->attr.name, "charge_status") == 0) 1061 command = BQ2415X_CHARGE_STATUS; 1062 else if (strcmp(attr->attr.name, "boost_status") == 0) 1063 command = BQ2415X_BOOST_STATUS; 1064 else if (strcmp(attr->attr.name, "fault_status") == 0) 1065 command = BQ2415X_FAULT_STATUS; 1066 else 1067 return -EINVAL; 1068 1069 ret = bq2415x_exec_command(bq, command); 1070 if (ret < 0) 1071 return ret; 1072 return sysfs_emit(buf, "%d\n", ret); 1073 } 1074 1075 /* 1076 * set timer entry: 1077 * auto - enable auto mode 1078 * off - disable auto mode 1079 * (other values) - reset chip timer 1080 */ 1081 static ssize_t bq2415x_sysfs_set_timer(struct device *dev, 1082 struct device_attribute *attr, 1083 const char *buf, 1084 size_t count) 1085 { 1086 struct power_supply *psy = dev_get_drvdata(dev); 1087 struct bq2415x_device *bq = power_supply_get_drvdata(psy); 1088 int ret = 0; 1089 1090 if (strncmp(buf, "auto", 4) == 0) 1091 bq2415x_set_autotimer(bq, 1); 1092 else if (strncmp(buf, "off", 3) == 0) 1093 bq2415x_set_autotimer(bq, 0); 1094 else 1095 ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET); 1096 1097 if (ret < 0) 1098 return ret; 1099 return count; 1100 } 1101 1102 /* show timer entry (auto or off) */ 1103 static ssize_t bq2415x_sysfs_show_timer(struct device *dev, 1104 struct device_attribute *attr, 1105 char *buf) 1106 { 1107 struct power_supply *psy = dev_get_drvdata(dev); 1108 struct bq2415x_device *bq = power_supply_get_drvdata(psy); 1109 1110 if (bq->timer_error) 1111 return sysfs_emit(buf, "%s\n", bq->timer_error); 1112 1113 if (bq->autotimer) 1114 return sysfs_emit(buf, "auto\n"); 1115 return sysfs_emit(buf, "off\n"); 1116 } 1117 1118 /* 1119 * set mode entry: 1120 * auto - if automode is supported, enable it and set mode to reported 1121 * none - disable charger and boost mode 1122 * host - charging mode for host/hub chargers (current limit 500mA) 1123 * dedicated - charging mode for dedicated chargers (unlimited current limit) 1124 * boost - disable charger and enable boost mode 1125 */ 1126 static ssize_t bq2415x_sysfs_set_mode(struct device *dev, 1127 struct device_attribute *attr, 1128 const char *buf, 1129 size_t count) 1130 { 1131 struct power_supply *psy = dev_get_drvdata(dev); 1132 struct bq2415x_device *bq = power_supply_get_drvdata(psy); 1133 enum bq2415x_mode mode; 1134 int ret = 0; 1135 1136 if (strncmp(buf, "auto", 4) == 0) { 1137 if (bq->automode < 0) 1138 return -EINVAL; 1139 bq->automode = 1; 1140 mode = bq->reported_mode; 1141 } else if (strncmp(buf, "off", 3) == 0) { 1142 if (bq->automode > 0) 1143 bq->automode = 0; 1144 mode = BQ2415X_MODE_OFF; 1145 } else if (strncmp(buf, "none", 4) == 0) { 1146 if (bq->automode > 0) 1147 bq->automode = 0; 1148 mode = BQ2415X_MODE_NONE; 1149 } else if (strncmp(buf, "host", 4) == 0) { 1150 if (bq->automode > 0) 1151 bq->automode = 0; 1152 mode = BQ2415X_MODE_HOST_CHARGER; 1153 } else if (strncmp(buf, "dedicated", 9) == 0) { 1154 if (bq->automode > 0) 1155 bq->automode = 0; 1156 mode = BQ2415X_MODE_DEDICATED_CHARGER; 1157 } else if (strncmp(buf, "boost", 5) == 0) { 1158 if (bq->automode > 0) 1159 bq->automode = 0; 1160 mode = BQ2415X_MODE_BOOST; 1161 } else if (strncmp(buf, "reset", 5) == 0) { 1162 bq2415x_reset_chip(bq); 1163 bq2415x_set_defaults(bq); 1164 if (bq->automode <= 0) 1165 return count; 1166 bq->automode = 1; 1167 mode = bq->reported_mode; 1168 } else { 1169 return -EINVAL; 1170 } 1171 1172 ret = bq2415x_set_mode(bq, mode); 1173 if (ret < 0) 1174 return ret; 1175 return count; 1176 } 1177 1178 /* show mode entry (auto, none, host, dedicated or boost) */ 1179 static ssize_t bq2415x_sysfs_show_mode(struct device *dev, 1180 struct device_attribute *attr, 1181 char *buf) 1182 { 1183 struct power_supply *psy = dev_get_drvdata(dev); 1184 struct bq2415x_device *bq = power_supply_get_drvdata(psy); 1185 ssize_t ret = 0; 1186 1187 if (bq->automode > 0) 1188 ret += sysfs_emit_at(buf, ret, "auto ("); 1189 1190 switch (bq->mode) { 1191 case BQ2415X_MODE_OFF: 1192 ret += sysfs_emit_at(buf, ret, "off"); 1193 break; 1194 case BQ2415X_MODE_NONE: 1195 ret += sysfs_emit_at(buf, ret, "none"); 1196 break; 1197 case BQ2415X_MODE_HOST_CHARGER: 1198 ret += sysfs_emit_at(buf, ret, "host"); 1199 break; 1200 case BQ2415X_MODE_DEDICATED_CHARGER: 1201 ret += sysfs_emit_at(buf, ret, "dedicated"); 1202 break; 1203 case BQ2415X_MODE_BOOST: 1204 ret += sysfs_emit_at(buf, ret, "boost"); 1205 break; 1206 } 1207 1208 if (bq->automode > 0) 1209 ret += sysfs_emit_at(buf, ret, ")"); 1210 1211 ret += sysfs_emit_at(buf, ret, "\n"); 1212 return ret; 1213 } 1214 1215 /* show reported_mode entry (none, host, dedicated or boost) */ 1216 static ssize_t bq2415x_sysfs_show_reported_mode(struct device *dev, 1217 struct device_attribute *attr, 1218 char *buf) 1219 { 1220 struct power_supply *psy = dev_get_drvdata(dev); 1221 struct bq2415x_device *bq = power_supply_get_drvdata(psy); 1222 1223 if (bq->automode < 0) 1224 return -EINVAL; 1225 1226 switch (bq->reported_mode) { 1227 case BQ2415X_MODE_OFF: 1228 return sysfs_emit(buf, "off\n"); 1229 case BQ2415X_MODE_NONE: 1230 return sysfs_emit(buf, "none\n"); 1231 case BQ2415X_MODE_HOST_CHARGER: 1232 return sysfs_emit(buf, "host\n"); 1233 case BQ2415X_MODE_DEDICATED_CHARGER: 1234 return sysfs_emit(buf, "dedicated\n"); 1235 case BQ2415X_MODE_BOOST: 1236 return sysfs_emit(buf, "boost\n"); 1237 } 1238 1239 return -EINVAL; 1240 } 1241 1242 /* directly set raw value to chip register, format: 'register value' */ 1243 static ssize_t bq2415x_sysfs_set_registers(struct device *dev, 1244 struct device_attribute *attr, 1245 const char *buf, 1246 size_t count) 1247 { 1248 struct power_supply *psy = dev_get_drvdata(dev); 1249 struct bq2415x_device *bq = power_supply_get_drvdata(psy); 1250 ssize_t ret = 0; 1251 unsigned int reg; 1252 unsigned int val; 1253 1254 if (sscanf(buf, "%x %x", ®, &val) != 2) 1255 return -EINVAL; 1256 1257 if (reg > 4 || val > 255) 1258 return -EINVAL; 1259 1260 ret = bq2415x_i2c_write(bq, reg, val); 1261 if (ret < 0) 1262 return ret; 1263 return count; 1264 } 1265 1266 /* print value of chip register, format: 'register=value' */ 1267 static ssize_t bq2415x_sysfs_print_reg(struct bq2415x_device *bq, 1268 u8 reg, 1269 char *buf) 1270 { 1271 int ret = bq2415x_i2c_read(bq, reg); 1272 1273 if (ret < 0) 1274 return sysfs_emit(buf, "%#.2x=error %d\n", reg, ret); 1275 return sysfs_emit(buf, "%#.2x=%#.2x\n", reg, ret); 1276 } 1277 1278 /* show all raw values of chip register, format per line: 'register=value' */ 1279 static ssize_t bq2415x_sysfs_show_registers(struct device *dev, 1280 struct device_attribute *attr, 1281 char *buf) 1282 { 1283 struct power_supply *psy = dev_get_drvdata(dev); 1284 struct bq2415x_device *bq = power_supply_get_drvdata(psy); 1285 ssize_t ret = 0; 1286 1287 ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_STATUS, buf+ret); 1288 ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_CONTROL, buf+ret); 1289 ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_VOLTAGE, buf+ret); 1290 ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_VENDER, buf+ret); 1291 ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_CURRENT, buf+ret); 1292 return ret; 1293 } 1294 1295 /* set current and voltage limit entries (in mA or mV) */ 1296 static ssize_t bq2415x_sysfs_set_limit(struct device *dev, 1297 struct device_attribute *attr, 1298 const char *buf, 1299 size_t count) 1300 { 1301 struct power_supply *psy = dev_get_drvdata(dev); 1302 struct bq2415x_device *bq = power_supply_get_drvdata(psy); 1303 long val; 1304 int ret; 1305 1306 if (kstrtol(buf, 10, &val) < 0) 1307 return -EINVAL; 1308 1309 if (strcmp(attr->attr.name, "current_limit") == 0) 1310 ret = bq2415x_set_current_limit(bq, val); 1311 else if (strcmp(attr->attr.name, "weak_battery_voltage") == 0) 1312 ret = bq2415x_set_weak_battery_voltage(bq, val); 1313 else if (strcmp(attr->attr.name, "battery_regulation_voltage") == 0) 1314 ret = bq2415x_set_battery_regulation_voltage(bq, val); 1315 else if (strcmp(attr->attr.name, "charge_current") == 0) 1316 ret = bq2415x_set_charge_current(bq, val); 1317 else if (strcmp(attr->attr.name, "termination_current") == 0) 1318 ret = bq2415x_set_termination_current(bq, val); 1319 else 1320 return -EINVAL; 1321 1322 if (ret < 0) 1323 return ret; 1324 return count; 1325 } 1326 1327 /* show current and voltage limit entries (in mA or mV) */ 1328 static ssize_t bq2415x_sysfs_show_limit(struct device *dev, 1329 struct device_attribute *attr, 1330 char *buf) 1331 { 1332 struct power_supply *psy = dev_get_drvdata(dev); 1333 struct bq2415x_device *bq = power_supply_get_drvdata(psy); 1334 int ret; 1335 1336 if (strcmp(attr->attr.name, "current_limit") == 0) 1337 ret = bq2415x_get_current_limit(bq); 1338 else if (strcmp(attr->attr.name, "weak_battery_voltage") == 0) 1339 ret = bq2415x_get_weak_battery_voltage(bq); 1340 else if (strcmp(attr->attr.name, "battery_regulation_voltage") == 0) 1341 ret = bq2415x_get_battery_regulation_voltage(bq); 1342 else if (strcmp(attr->attr.name, "charge_current") == 0) 1343 ret = bq2415x_get_charge_current(bq); 1344 else if (strcmp(attr->attr.name, "termination_current") == 0) 1345 ret = bq2415x_get_termination_current(bq); 1346 else 1347 return -EINVAL; 1348 1349 if (ret < 0) 1350 return ret; 1351 return sysfs_emit(buf, "%d\n", ret); 1352 } 1353 1354 /* set *_enable entries */ 1355 static ssize_t bq2415x_sysfs_set_enable(struct device *dev, 1356 struct device_attribute *attr, 1357 const char *buf, 1358 size_t count) 1359 { 1360 struct power_supply *psy = dev_get_drvdata(dev); 1361 struct bq2415x_device *bq = power_supply_get_drvdata(psy); 1362 enum bq2415x_command command; 1363 long val; 1364 int ret; 1365 1366 if (kstrtol(buf, 10, &val) < 0) 1367 return -EINVAL; 1368 1369 if (strcmp(attr->attr.name, "charge_termination_enable") == 0) 1370 command = val ? BQ2415X_CHARGE_TERMINATION_ENABLE : 1371 BQ2415X_CHARGE_TERMINATION_DISABLE; 1372 else if (strcmp(attr->attr.name, "high_impedance_enable") == 0) 1373 command = val ? BQ2415X_HIGH_IMPEDANCE_ENABLE : 1374 BQ2415X_HIGH_IMPEDANCE_DISABLE; 1375 else if (strcmp(attr->attr.name, "otg_pin_enable") == 0) 1376 command = val ? BQ2415X_OTG_PIN_ENABLE : 1377 BQ2415X_OTG_PIN_DISABLE; 1378 else if (strcmp(attr->attr.name, "stat_pin_enable") == 0) 1379 command = val ? BQ2415X_STAT_PIN_ENABLE : 1380 BQ2415X_STAT_PIN_DISABLE; 1381 else 1382 return -EINVAL; 1383 1384 ret = bq2415x_exec_command(bq, command); 1385 if (ret < 0) 1386 return ret; 1387 return count; 1388 } 1389 1390 /* show *_enable entries */ 1391 static ssize_t bq2415x_sysfs_show_enable(struct device *dev, 1392 struct device_attribute *attr, 1393 char *buf) 1394 { 1395 struct power_supply *psy = dev_get_drvdata(dev); 1396 struct bq2415x_device *bq = power_supply_get_drvdata(psy); 1397 enum bq2415x_command command; 1398 int ret; 1399 1400 if (strcmp(attr->attr.name, "charge_termination_enable") == 0) 1401 command = BQ2415X_CHARGE_TERMINATION_STATUS; 1402 else if (strcmp(attr->attr.name, "high_impedance_enable") == 0) 1403 command = BQ2415X_HIGH_IMPEDANCE_STATUS; 1404 else if (strcmp(attr->attr.name, "otg_pin_enable") == 0) 1405 command = BQ2415X_OTG_PIN_STATUS; 1406 else if (strcmp(attr->attr.name, "stat_pin_enable") == 0) 1407 command = BQ2415X_STAT_PIN_STATUS; 1408 else 1409 return -EINVAL; 1410 1411 ret = bq2415x_exec_command(bq, command); 1412 if (ret < 0) 1413 return ret; 1414 return sysfs_emit(buf, "%d\n", ret); 1415 } 1416 1417 static DEVICE_ATTR(current_limit, S_IWUSR | S_IRUGO, 1418 bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit); 1419 static DEVICE_ATTR(weak_battery_voltage, S_IWUSR | S_IRUGO, 1420 bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit); 1421 static DEVICE_ATTR(battery_regulation_voltage, S_IWUSR | S_IRUGO, 1422 bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit); 1423 static DEVICE_ATTR(charge_current, S_IWUSR | S_IRUGO, 1424 bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit); 1425 static DEVICE_ATTR(termination_current, S_IWUSR | S_IRUGO, 1426 bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit); 1427 1428 static DEVICE_ATTR(charge_termination_enable, S_IWUSR | S_IRUGO, 1429 bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable); 1430 static DEVICE_ATTR(high_impedance_enable, S_IWUSR | S_IRUGO, 1431 bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable); 1432 static DEVICE_ATTR(otg_pin_enable, S_IWUSR | S_IRUGO, 1433 bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable); 1434 static DEVICE_ATTR(stat_pin_enable, S_IWUSR | S_IRUGO, 1435 bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable); 1436 1437 static DEVICE_ATTR(reported_mode, S_IRUGO, 1438 bq2415x_sysfs_show_reported_mode, NULL); 1439 static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO, 1440 bq2415x_sysfs_show_mode, bq2415x_sysfs_set_mode); 1441 static DEVICE_ATTR(timer, S_IWUSR | S_IRUGO, 1442 bq2415x_sysfs_show_timer, bq2415x_sysfs_set_timer); 1443 1444 static DEVICE_ATTR(registers, S_IWUSR | S_IRUGO, 1445 bq2415x_sysfs_show_registers, bq2415x_sysfs_set_registers); 1446 1447 static DEVICE_ATTR(otg_status, S_IRUGO, bq2415x_sysfs_show_status, NULL); 1448 static DEVICE_ATTR(charge_status, S_IRUGO, bq2415x_sysfs_show_status, NULL); 1449 static DEVICE_ATTR(boost_status, S_IRUGO, bq2415x_sysfs_show_status, NULL); 1450 static DEVICE_ATTR(fault_status, S_IRUGO, bq2415x_sysfs_show_status, NULL); 1451 1452 static struct attribute *bq2415x_sysfs_attrs[] = { 1453 /* 1454 * TODO: some (appropriate) of these attrs should be switched to 1455 * use power supply class props. 1456 */ 1457 &dev_attr_current_limit.attr, 1458 &dev_attr_weak_battery_voltage.attr, 1459 &dev_attr_battery_regulation_voltage.attr, 1460 &dev_attr_charge_current.attr, 1461 &dev_attr_termination_current.attr, 1462 1463 &dev_attr_charge_termination_enable.attr, 1464 &dev_attr_high_impedance_enable.attr, 1465 &dev_attr_otg_pin_enable.attr, 1466 &dev_attr_stat_pin_enable.attr, 1467 1468 &dev_attr_reported_mode.attr, 1469 &dev_attr_mode.attr, 1470 &dev_attr_timer.attr, 1471 1472 &dev_attr_registers.attr, 1473 1474 &dev_attr_otg_status.attr, 1475 &dev_attr_charge_status.attr, 1476 &dev_attr_boost_status.attr, 1477 &dev_attr_fault_status.attr, 1478 NULL, 1479 }; 1480 1481 ATTRIBUTE_GROUPS(bq2415x_sysfs); 1482 1483 static int bq2415x_power_supply_init(struct bq2415x_device *bq) 1484 { 1485 int ret; 1486 int chip; 1487 char revstr[8]; 1488 struct power_supply_config psy_cfg = { 1489 .drv_data = bq, 1490 .of_node = bq->dev->of_node, 1491 .attr_grp = bq2415x_sysfs_groups, 1492 }; 1493 1494 bq->charger_desc.name = bq->name; 1495 bq->charger_desc.type = POWER_SUPPLY_TYPE_USB; 1496 bq->charger_desc.properties = bq2415x_power_supply_props; 1497 bq->charger_desc.num_properties = 1498 ARRAY_SIZE(bq2415x_power_supply_props); 1499 bq->charger_desc.get_property = bq2415x_power_supply_get_property; 1500 1501 ret = bq2415x_detect_chip(bq); 1502 if (ret < 0) 1503 chip = BQUNKNOWN; 1504 else 1505 chip = ret; 1506 1507 ret = bq2415x_detect_revision(bq); 1508 if (ret < 0) 1509 strcpy(revstr, "unknown"); 1510 else 1511 sprintf(revstr, "1.%d", ret); 1512 1513 bq->model = kasprintf(GFP_KERNEL, 1514 "chip %s, revision %s, vender code %.3d", 1515 bq2415x_chip_name[chip], revstr, 1516 bq2415x_get_vender_code(bq)); 1517 if (!bq->model) { 1518 dev_err(bq->dev, "failed to allocate model name\n"); 1519 return -ENOMEM; 1520 } 1521 1522 bq->charger = power_supply_register(bq->dev, &bq->charger_desc, 1523 &psy_cfg); 1524 if (IS_ERR(bq->charger)) { 1525 kfree(bq->model); 1526 return PTR_ERR(bq->charger); 1527 } 1528 1529 return 0; 1530 } 1531 1532 /* main bq2415x probe function */ 1533 static int bq2415x_probe(struct i2c_client *client) 1534 { 1535 const struct i2c_device_id *id = i2c_client_get_device_id(client); 1536 int ret; 1537 int num; 1538 char *name = NULL; 1539 struct bq2415x_device *bq; 1540 struct device_node *np = client->dev.of_node; 1541 struct bq2415x_platform_data *pdata = client->dev.platform_data; 1542 const struct acpi_device_id *acpi_id = NULL; 1543 struct power_supply *notify_psy = NULL; 1544 union power_supply_propval prop; 1545 1546 if (!np && !pdata && !ACPI_HANDLE(&client->dev)) { 1547 dev_err(&client->dev, "Neither devicetree, nor platform data, nor ACPI support\n"); 1548 return -ENODEV; 1549 } 1550 1551 /* Get new ID for the new device */ 1552 mutex_lock(&bq2415x_id_mutex); 1553 num = idr_alloc(&bq2415x_id, client, 0, 0, GFP_KERNEL); 1554 mutex_unlock(&bq2415x_id_mutex); 1555 if (num < 0) 1556 return num; 1557 1558 if (id) { 1559 name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num); 1560 } else if (ACPI_HANDLE(&client->dev)) { 1561 acpi_id = 1562 acpi_match_device(client->dev.driver->acpi_match_table, 1563 &client->dev); 1564 if (!acpi_id) { 1565 dev_err(&client->dev, "failed to match device name\n"); 1566 ret = -ENODEV; 1567 goto error_1; 1568 } 1569 name = kasprintf(GFP_KERNEL, "%s-%d", acpi_id->id, num); 1570 } 1571 if (!name) { 1572 dev_err(&client->dev, "failed to allocate device name\n"); 1573 ret = -ENOMEM; 1574 goto error_1; 1575 } 1576 1577 bq = devm_kzalloc(&client->dev, sizeof(*bq), GFP_KERNEL); 1578 if (!bq) { 1579 ret = -ENOMEM; 1580 goto error_2; 1581 } 1582 1583 i2c_set_clientdata(client, bq); 1584 1585 bq->id = num; 1586 bq->dev = &client->dev; 1587 if (id) 1588 bq->chip = id->driver_data; 1589 else if (ACPI_HANDLE(bq->dev)) 1590 bq->chip = acpi_id->driver_data; 1591 bq->name = name; 1592 bq->mode = BQ2415X_MODE_OFF; 1593 bq->reported_mode = BQ2415X_MODE_OFF; 1594 bq->autotimer = 0; 1595 bq->automode = 0; 1596 1597 if (np || ACPI_HANDLE(bq->dev)) { 1598 ret = device_property_read_u32(bq->dev, 1599 "ti,current-limit", 1600 &bq->init_data.current_limit); 1601 if (ret) 1602 goto error_2; 1603 ret = device_property_read_u32(bq->dev, 1604 "ti,weak-battery-voltage", 1605 &bq->init_data.weak_battery_voltage); 1606 if (ret) 1607 goto error_2; 1608 ret = device_property_read_u32(bq->dev, 1609 "ti,battery-regulation-voltage", 1610 &bq->init_data.battery_regulation_voltage); 1611 if (ret) 1612 goto error_2; 1613 ret = device_property_read_u32(bq->dev, 1614 "ti,charge-current", 1615 &bq->init_data.charge_current); 1616 if (ret) 1617 goto error_2; 1618 ret = device_property_read_u32(bq->dev, 1619 "ti,termination-current", 1620 &bq->init_data.termination_current); 1621 if (ret) 1622 goto error_2; 1623 ret = device_property_read_u32(bq->dev, 1624 "ti,resistor-sense", 1625 &bq->init_data.resistor_sense); 1626 if (ret) 1627 goto error_2; 1628 if (np) 1629 bq->notify_node = of_parse_phandle(np, 1630 "ti,usb-charger-detection", 0); 1631 } else { 1632 memcpy(&bq->init_data, pdata, sizeof(bq->init_data)); 1633 } 1634 1635 bq2415x_reset_chip(bq); 1636 1637 ret = bq2415x_power_supply_init(bq); 1638 if (ret) { 1639 dev_err(bq->dev, "failed to register power supply: %d\n", ret); 1640 goto error_2; 1641 } 1642 1643 ret = bq2415x_set_defaults(bq); 1644 if (ret) { 1645 dev_err(bq->dev, "failed to set default values: %d\n", ret); 1646 goto error_3; 1647 } 1648 1649 if (bq->notify_node || bq->init_data.notify_device) { 1650 bq->nb.notifier_call = bq2415x_notifier_call; 1651 ret = power_supply_reg_notifier(&bq->nb); 1652 if (ret) { 1653 dev_err(bq->dev, "failed to reg notifier: %d\n", ret); 1654 goto error_3; 1655 } 1656 1657 bq->automode = 1; 1658 dev_info(bq->dev, "automode supported, waiting for events\n"); 1659 } else { 1660 bq->automode = -1; 1661 dev_info(bq->dev, "automode not supported\n"); 1662 } 1663 1664 /* Query for initial reported_mode and set it */ 1665 if (bq->nb.notifier_call) { 1666 if (np) { 1667 notify_psy = power_supply_get_by_phandle(np, 1668 "ti,usb-charger-detection"); 1669 if (IS_ERR(notify_psy)) 1670 notify_psy = NULL; 1671 } else if (bq->init_data.notify_device) { 1672 notify_psy = power_supply_get_by_name( 1673 bq->init_data.notify_device); 1674 } 1675 } 1676 if (notify_psy) { 1677 ret = power_supply_get_property(notify_psy, 1678 POWER_SUPPLY_PROP_CURRENT_MAX, &prop); 1679 power_supply_put(notify_psy); 1680 1681 if (ret == 0) { 1682 bq2415x_update_reported_mode(bq, prop.intval); 1683 bq2415x_set_mode(bq, bq->reported_mode); 1684 } 1685 } 1686 1687 INIT_DELAYED_WORK(&bq->work, bq2415x_timer_work); 1688 bq2415x_set_autotimer(bq, 1); 1689 1690 dev_info(bq->dev, "driver registered\n"); 1691 return 0; 1692 1693 error_3: 1694 bq2415x_power_supply_exit(bq); 1695 error_2: 1696 if (bq) 1697 of_node_put(bq->notify_node); 1698 kfree(name); 1699 error_1: 1700 mutex_lock(&bq2415x_id_mutex); 1701 idr_remove(&bq2415x_id, num); 1702 mutex_unlock(&bq2415x_id_mutex); 1703 1704 return ret; 1705 } 1706 1707 /* main bq2415x remove function */ 1708 1709 static void bq2415x_remove(struct i2c_client *client) 1710 { 1711 struct bq2415x_device *bq = i2c_get_clientdata(client); 1712 1713 if (bq->nb.notifier_call) 1714 power_supply_unreg_notifier(&bq->nb); 1715 1716 of_node_put(bq->notify_node); 1717 bq2415x_power_supply_exit(bq); 1718 1719 bq2415x_reset_chip(bq); 1720 1721 mutex_lock(&bq2415x_id_mutex); 1722 idr_remove(&bq2415x_id, bq->id); 1723 mutex_unlock(&bq2415x_id_mutex); 1724 1725 dev_info(bq->dev, "driver unregistered\n"); 1726 1727 kfree(bq->name); 1728 } 1729 1730 static const struct i2c_device_id bq2415x_i2c_id_table[] = { 1731 { "bq2415x", BQUNKNOWN }, 1732 { "bq24150", BQ24150 }, 1733 { "bq24150a", BQ24150A }, 1734 { "bq24151", BQ24151 }, 1735 { "bq24151a", BQ24151A }, 1736 { "bq24152", BQ24152 }, 1737 { "bq24153", BQ24153 }, 1738 { "bq24153a", BQ24153A }, 1739 { "bq24155", BQ24155 }, 1740 { "bq24156", BQ24156 }, 1741 { "bq24156a", BQ24156A }, 1742 { "bq24157s", BQ24157S }, 1743 { "bq24158", BQ24158 }, 1744 {}, 1745 }; 1746 MODULE_DEVICE_TABLE(i2c, bq2415x_i2c_id_table); 1747 1748 #ifdef CONFIG_ACPI 1749 static const struct acpi_device_id bq2415x_i2c_acpi_match[] = { 1750 { "BQ2415X", BQUNKNOWN }, 1751 { "BQ241500", BQ24150 }, 1752 { "BQA24150", BQ24150A }, 1753 { "BQ241510", BQ24151 }, 1754 { "BQA24151", BQ24151A }, 1755 { "BQ241520", BQ24152 }, 1756 { "BQ241530", BQ24153 }, 1757 { "BQA24153", BQ24153A }, 1758 { "BQ241550", BQ24155 }, 1759 { "BQ241560", BQ24156 }, 1760 { "BQA24156", BQ24156A }, 1761 { "BQS24157", BQ24157S }, 1762 { "BQ241580", BQ24158 }, 1763 {}, 1764 }; 1765 MODULE_DEVICE_TABLE(acpi, bq2415x_i2c_acpi_match); 1766 #endif 1767 1768 #ifdef CONFIG_OF 1769 static const struct of_device_id bq2415x_of_match_table[] = { 1770 { .compatible = "ti,bq24150" }, 1771 { .compatible = "ti,bq24150a" }, 1772 { .compatible = "ti,bq24151" }, 1773 { .compatible = "ti,bq24151a" }, 1774 { .compatible = "ti,bq24152" }, 1775 { .compatible = "ti,bq24153" }, 1776 { .compatible = "ti,bq24153a" }, 1777 { .compatible = "ti,bq24155" }, 1778 { .compatible = "ti,bq24156" }, 1779 { .compatible = "ti,bq24156a" }, 1780 { .compatible = "ti,bq24157s" }, 1781 { .compatible = "ti,bq24158" }, 1782 {}, 1783 }; 1784 MODULE_DEVICE_TABLE(of, bq2415x_of_match_table); 1785 #endif 1786 1787 static struct i2c_driver bq2415x_driver = { 1788 .driver = { 1789 .name = "bq2415x-charger", 1790 .of_match_table = of_match_ptr(bq2415x_of_match_table), 1791 .acpi_match_table = ACPI_PTR(bq2415x_i2c_acpi_match), 1792 }, 1793 .probe = bq2415x_probe, 1794 .remove = bq2415x_remove, 1795 .id_table = bq2415x_i2c_id_table, 1796 }; 1797 module_i2c_driver(bq2415x_driver); 1798 1799 MODULE_AUTHOR("Pali Rohár <pali@kernel.org>"); 1800 MODULE_DESCRIPTION("bq2415x charger driver"); 1801 MODULE_LICENSE("GPL"); 1802