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