diff options
Diffstat (limited to 'drivers/power/bq2415x_charger.c')
-rw-r--r-- | drivers/power/bq2415x_charger.c | 268 |
1 files changed, 147 insertions, 121 deletions
diff --git a/drivers/power/bq2415x_charger.c b/drivers/power/bq2415x_charger.c index 017b3c27f769..ee842b37f462 100644 --- a/drivers/power/bq2415x_charger.c +++ b/drivers/power/bq2415x_charger.c | |||
@@ -1,31 +1,32 @@ | |||
1 | /* | 1 | /* |
2 | bq2415x_charger.c - bq2415x charger driver | 2 | * bq2415x charger driver |
3 | Copyright (C) 2011-2012 Pali Rohár <pali.rohar@gmail.com> | 3 | * |
4 | 4 | * Copyright (C) 2011-2012 Pali Rohár <pali.rohar@gmail.com> | |
5 | This program is free software; you can redistribute it and/or modify | 5 | * |
6 | it under the terms of the GNU General Public License as published by | 6 | * This program is free software; you can redistribute it and/or modify |
7 | the Free Software Foundation; either version 2 of the License, or | 7 | * it under the terms of the GNU General Public License as published by |
8 | (at your option) any later version. | 8 | * the Free Software Foundation; either version 2 of the License, or |
9 | 9 | * (at your option) any later version. | |
10 | This program is distributed in the hope that it will be useful, | 10 | * |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | * This program is distributed in the hope that it will be useful, |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | GNU General Public License for more details. | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | 14 | * GNU General Public License for more details. | |
15 | You should have received a copy of the GNU General Public License along | 15 | * |
16 | with this program; if not, write to the Free Software Foundation, Inc., | 16 | * You should have received a copy of the GNU General Public License along |
17 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
18 | */ | 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 | */ | ||
19 | 20 | ||
20 | /* | 21 | /* |
21 | Datasheets: | 22 | * Datasheets: |
22 | http://www.ti.com/product/bq24150 | 23 | * http://www.ti.com/product/bq24150 |
23 | http://www.ti.com/product/bq24150a | 24 | * http://www.ti.com/product/bq24150a |
24 | http://www.ti.com/product/bq24152 | 25 | * http://www.ti.com/product/bq24152 |
25 | http://www.ti.com/product/bq24153 | 26 | * http://www.ti.com/product/bq24153 |
26 | http://www.ti.com/product/bq24153a | 27 | * http://www.ti.com/product/bq24153a |
27 | http://www.ti.com/product/bq24155 | 28 | * http://www.ti.com/product/bq24155 |
28 | */ | 29 | */ |
29 | 30 | ||
30 | #include <linux/version.h> | 31 | #include <linux/version.h> |
31 | #include <linux/kernel.h> | 32 | #include <linux/kernel.h> |
@@ -222,7 +223,7 @@ static int bq2415x_i2c_read(struct bq2415x_device *bq, u8 reg) | |||
222 | 223 | ||
223 | /* read value from register, apply mask and right shift it */ | 224 | /* read value from register, apply mask and right shift it */ |
224 | static int bq2415x_i2c_read_mask(struct bq2415x_device *bq, u8 reg, | 225 | static int bq2415x_i2c_read_mask(struct bq2415x_device *bq, u8 reg, |
225 | u8 mask, u8 shift) | 226 | u8 mask, u8 shift) |
226 | { | 227 | { |
227 | int ret; | 228 | int ret; |
228 | 229 | ||
@@ -232,8 +233,7 @@ static int bq2415x_i2c_read_mask(struct bq2415x_device *bq, u8 reg, | |||
232 | ret = bq2415x_i2c_read(bq, reg); | 233 | ret = bq2415x_i2c_read(bq, reg); |
233 | if (ret < 0) | 234 | if (ret < 0) |
234 | return ret; | 235 | return ret; |
235 | else | 236 | return (ret & mask) >> shift; |
236 | return (ret & mask) >> shift; | ||
237 | } | 237 | } |
238 | 238 | ||
239 | /* read value from register and return one specified bit */ | 239 | /* read value from register and return one specified bit */ |
@@ -241,8 +241,7 @@ static int bq2415x_i2c_read_bit(struct bq2415x_device *bq, u8 reg, u8 bit) | |||
241 | { | 241 | { |
242 | if (bit > 8) | 242 | if (bit > 8) |
243 | return -EINVAL; | 243 | return -EINVAL; |
244 | else | 244 | return bq2415x_i2c_read_mask(bq, reg, BIT(bit), bit); |
245 | return bq2415x_i2c_read_mask(bq, reg, BIT(bit), bit); | ||
246 | } | 245 | } |
247 | 246 | ||
248 | /**** i2c write functions ****/ | 247 | /**** i2c write functions ****/ |
@@ -278,7 +277,7 @@ static int bq2415x_i2c_write(struct bq2415x_device *bq, u8 reg, u8 val) | |||
278 | 277 | ||
279 | /* read value from register, change it with mask left shifted and write back */ | 278 | /* 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, | 279 | static int bq2415x_i2c_write_mask(struct bq2415x_device *bq, u8 reg, u8 val, |
281 | u8 mask, u8 shift) | 280 | u8 mask, u8 shift) |
282 | { | 281 | { |
283 | int ret; | 282 | int ret; |
284 | 283 | ||
@@ -297,12 +296,11 @@ static int bq2415x_i2c_write_mask(struct bq2415x_device *bq, u8 reg, u8 val, | |||
297 | 296 | ||
298 | /* change only one bit in register */ | 297 | /* change only one bit in register */ |
299 | static int bq2415x_i2c_write_bit(struct bq2415x_device *bq, u8 reg, | 298 | static int bq2415x_i2c_write_bit(struct bq2415x_device *bq, u8 reg, |
300 | bool val, u8 bit) | 299 | bool val, u8 bit) |
301 | { | 300 | { |
302 | if (bit > 8) | 301 | if (bit > 8) |
303 | return -EINVAL; | 302 | return -EINVAL; |
304 | else | 303 | return bq2415x_i2c_write_mask(bq, reg, val, BIT(bit), bit); |
305 | return bq2415x_i2c_write_mask(bq, reg, val, BIT(bit), bit); | ||
306 | } | 304 | } |
307 | 305 | ||
308 | /**** global functions ****/ | 306 | /**** global functions ****/ |
@@ -312,6 +310,7 @@ static int bq2415x_exec_command(struct bq2415x_device *bq, | |||
312 | enum bq2415x_command command) | 310 | enum bq2415x_command command) |
313 | { | 311 | { |
314 | int ret; | 312 | int ret; |
313 | |||
315 | switch (command) { | 314 | switch (command) { |
316 | case BQ2415X_TIMER_RESET: | 315 | case BQ2415X_TIMER_RESET: |
317 | return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, | 316 | return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, |
@@ -407,10 +406,8 @@ static int bq2415x_exec_command(struct bq2415x_device *bq, | |||
407 | case BQ2415X_REVISION: | 406 | case BQ2415X_REVISION: |
408 | return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER, | 407 | return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER, |
409 | BQ2415X_MASK_REVISION, BQ2415X_SHIFT_REVISION); | 408 | BQ2415X_MASK_REVISION, BQ2415X_SHIFT_REVISION); |
410 | |||
411 | default: | ||
412 | return -EINVAL; | ||
413 | } | 409 | } |
410 | return -EINVAL; | ||
414 | } | 411 | } |
415 | 412 | ||
416 | /* detect chip type */ | 413 | /* detect chip type */ |
@@ -470,6 +467,7 @@ static int bq2415x_detect_revision(struct bq2415x_device *bq) | |||
470 | { | 467 | { |
471 | int ret = bq2415x_exec_command(bq, BQ2415X_REVISION); | 468 | int ret = bq2415x_exec_command(bq, BQ2415X_REVISION); |
472 | int chip = bq2415x_detect_chip(bq); | 469 | int chip = bq2415x_detect_chip(bq); |
470 | |||
473 | if (ret < 0 || chip < 0) | 471 | if (ret < 0 || chip < 0) |
474 | return -1; | 472 | return -1; |
475 | 473 | ||
@@ -483,7 +481,6 @@ static int bq2415x_detect_revision(struct bq2415x_device *bq) | |||
483 | return ret; | 481 | return ret; |
484 | else | 482 | else |
485 | return -1; | 483 | return -1; |
486 | |||
487 | case BQ24153: | 484 | case BQ24153: |
488 | case BQ24153A: | 485 | case BQ24153A: |
489 | case BQ24156: | 486 | case BQ24156: |
@@ -495,13 +492,11 @@ static int bq2415x_detect_revision(struct bq2415x_device *bq) | |||
495 | return 1; | 492 | return 1; |
496 | else | 493 | else |
497 | return -1; | 494 | return -1; |
498 | |||
499 | case BQ24155: | 495 | case BQ24155: |
500 | if (ret == 3) | 496 | if (ret == 3) |
501 | return 3; | 497 | return 3; |
502 | else | 498 | else |
503 | return -1; | 499 | return -1; |
504 | |||
505 | case BQUNKNOWN: | 500 | case BQUNKNOWN: |
506 | return -1; | 501 | return -1; |
507 | } | 502 | } |
@@ -512,13 +507,16 @@ static int bq2415x_detect_revision(struct bq2415x_device *bq) | |||
512 | /* return chip vender code */ | 507 | /* return chip vender code */ |
513 | static int bq2415x_get_vender_code(struct bq2415x_device *bq) | 508 | static int bq2415x_get_vender_code(struct bq2415x_device *bq) |
514 | { | 509 | { |
515 | int ret = bq2415x_exec_command(bq, BQ2415X_VENDER_CODE); | 510 | int ret; |
511 | |||
512 | ret = bq2415x_exec_command(bq, BQ2415X_VENDER_CODE); | ||
516 | if (ret < 0) | 513 | if (ret < 0) |
517 | return 0; | 514 | return 0; |
518 | else /* convert to binary */ | 515 | |
519 | return (ret & 0x1) + | 516 | /* convert to binary */ |
520 | ((ret >> 1) & 0x1) * 10 + | 517 | return (ret & 0x1) + |
521 | ((ret >> 2) & 0x1) * 100; | 518 | ((ret >> 1) & 0x1) * 10 + |
519 | ((ret >> 2) & 0x1) * 100; | ||
522 | } | 520 | } |
523 | 521 | ||
524 | /* reset all chip registers to default state */ | 522 | /* reset all chip registers to default state */ |
@@ -537,6 +535,7 @@ static void bq2415x_reset_chip(struct bq2415x_device *bq) | |||
537 | static int bq2415x_set_current_limit(struct bq2415x_device *bq, int mA) | 535 | static int bq2415x_set_current_limit(struct bq2415x_device *bq, int mA) |
538 | { | 536 | { |
539 | int val; | 537 | int val; |
538 | |||
540 | if (mA <= 100) | 539 | if (mA <= 100) |
541 | val = 0; | 540 | val = 0; |
542 | else if (mA <= 500) | 541 | else if (mA <= 500) |
@@ -545,6 +544,7 @@ static int bq2415x_set_current_limit(struct bq2415x_device *bq, int mA) | |||
545 | val = 2; | 544 | val = 2; |
546 | else | 545 | else |
547 | val = 3; | 546 | val = 3; |
547 | |||
548 | return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val, | 548 | return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val, |
549 | BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT); | 549 | BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT); |
550 | } | 550 | } |
@@ -552,7 +552,9 @@ static int bq2415x_set_current_limit(struct bq2415x_device *bq, int mA) | |||
552 | /* get current limit in mA */ | 552 | /* get current limit in mA */ |
553 | static int bq2415x_get_current_limit(struct bq2415x_device *bq) | 553 | static int bq2415x_get_current_limit(struct bq2415x_device *bq) |
554 | { | 554 | { |
555 | int ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL, | 555 | int ret; |
556 | |||
557 | ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL, | ||
556 | BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT); | 558 | BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT); |
557 | if (ret < 0) | 559 | if (ret < 0) |
558 | return ret; | 560 | return ret; |
@@ -564,15 +566,15 @@ static int bq2415x_get_current_limit(struct bq2415x_device *bq) | |||
564 | return 800; | 566 | return 800; |
565 | else if (ret == 3) | 567 | else if (ret == 3) |
566 | return 1800; | 568 | return 1800; |
567 | else | 569 | return -EINVAL; |
568 | return -EINVAL; | ||
569 | } | 570 | } |
570 | 571 | ||
571 | /* set weak battery voltage in mV */ | 572 | /* set weak battery voltage in mV */ |
572 | static int bq2415x_set_weak_battery_voltage(struct bq2415x_device *bq, int mV) | 573 | static int bq2415x_set_weak_battery_voltage(struct bq2415x_device *bq, int mV) |
573 | { | 574 | { |
574 | /* round to 100mV */ | ||
575 | int val; | 575 | int val; |
576 | |||
577 | /* round to 100mV */ | ||
576 | if (mV <= 3400 + 50) | 578 | if (mV <= 3400 + 50) |
577 | val = 0; | 579 | val = 0; |
578 | else if (mV <= 3500 + 50) | 580 | else if (mV <= 3500 + 50) |
@@ -581,6 +583,7 @@ static int bq2415x_set_weak_battery_voltage(struct bq2415x_device *bq, int mV) | |||
581 | val = 2; | 583 | val = 2; |
582 | else | 584 | else |
583 | val = 3; | 585 | val = 3; |
586 | |||
584 | return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val, | 587 | return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val, |
585 | BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV); | 588 | BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV); |
586 | } | 589 | } |
@@ -588,17 +591,18 @@ static int bq2415x_set_weak_battery_voltage(struct bq2415x_device *bq, int mV) | |||
588 | /* get weak battery voltage in mV */ | 591 | /* get weak battery voltage in mV */ |
589 | static int bq2415x_get_weak_battery_voltage(struct bq2415x_device *bq) | 592 | static int bq2415x_get_weak_battery_voltage(struct bq2415x_device *bq) |
590 | { | 593 | { |
591 | int ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL, | 594 | int ret; |
595 | |||
596 | ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL, | ||
592 | BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV); | 597 | BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV); |
593 | if (ret < 0) | 598 | if (ret < 0) |
594 | return ret; | 599 | return ret; |
595 | else | 600 | return 100 * (34 + ret); |
596 | return 100 * (34 + ret); | ||
597 | } | 601 | } |
598 | 602 | ||
599 | /* set battery regulation voltage in mV */ | 603 | /* set battery regulation voltage in mV */ |
600 | static int bq2415x_set_battery_regulation_voltage(struct bq2415x_device *bq, | 604 | static int bq2415x_set_battery_regulation_voltage(struct bq2415x_device *bq, |
601 | int mV) | 605 | int mV) |
602 | { | 606 | { |
603 | int val = (mV/10 - 350) / 2; | 607 | int val = (mV/10 - 350) / 2; |
604 | 608 | ||
@@ -616,21 +620,21 @@ static int bq2415x_get_battery_regulation_voltage(struct bq2415x_device *bq) | |||
616 | { | 620 | { |
617 | int ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_VOLTAGE, | 621 | int ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_VOLTAGE, |
618 | BQ2415X_MASK_VO, BQ2415X_SHIFT_VO); | 622 | BQ2415X_MASK_VO, BQ2415X_SHIFT_VO); |
623 | |||
619 | if (ret < 0) | 624 | if (ret < 0) |
620 | return ret; | 625 | return ret; |
621 | else | 626 | return 10 * (350 + 2*ret); |
622 | return 10 * (350 + 2*ret); | ||
623 | } | 627 | } |
624 | 628 | ||
625 | /* set charge current in mA (platform data must provide resistor sense) */ | 629 | /* set charge current in mA (platform data must provide resistor sense) */ |
626 | static int bq2415x_set_charge_current(struct bq2415x_device *bq, int mA) | 630 | static int bq2415x_set_charge_current(struct bq2415x_device *bq, int mA) |
627 | { | 631 | { |
628 | int val; | 632 | int val; |
633 | |||
629 | if (bq->init_data.resistor_sense <= 0) | 634 | if (bq->init_data.resistor_sense <= 0) |
630 | return -ENOSYS; | 635 | return -ENOSYS; |
631 | 636 | ||
632 | val = (mA * bq->init_data.resistor_sense - 37400) / 6800; | 637 | val = (mA * bq->init_data.resistor_sense - 37400) / 6800; |
633 | |||
634 | if (val < 0) | 638 | if (val < 0) |
635 | val = 0; | 639 | val = 0; |
636 | else if (val > 7) | 640 | else if (val > 7) |
@@ -645,6 +649,7 @@ static int bq2415x_set_charge_current(struct bq2415x_device *bq, int mA) | |||
645 | static int bq2415x_get_charge_current(struct bq2415x_device *bq) | 649 | static int bq2415x_get_charge_current(struct bq2415x_device *bq) |
646 | { | 650 | { |
647 | int ret; | 651 | int ret; |
652 | |||
648 | if (bq->init_data.resistor_sense <= 0) | 653 | if (bq->init_data.resistor_sense <= 0) |
649 | return -ENOSYS; | 654 | return -ENOSYS; |
650 | 655 | ||
@@ -652,19 +657,18 @@ static int bq2415x_get_charge_current(struct bq2415x_device *bq) | |||
652 | BQ2415X_MASK_VI_CHRG, BQ2415X_SHIFT_VI_CHRG); | 657 | BQ2415X_MASK_VI_CHRG, BQ2415X_SHIFT_VI_CHRG); |
653 | if (ret < 0) | 658 | if (ret < 0) |
654 | return ret; | 659 | return ret; |
655 | else | 660 | return (37400 + 6800*ret) / bq->init_data.resistor_sense; |
656 | return (37400 + 6800*ret) / bq->init_data.resistor_sense; | ||
657 | } | 661 | } |
658 | 662 | ||
659 | /* set termination current in mA (platform data must provide resistor sense) */ | 663 | /* set termination current in mA (platform data must provide resistor sense) */ |
660 | static int bq2415x_set_termination_current(struct bq2415x_device *bq, int mA) | 664 | static int bq2415x_set_termination_current(struct bq2415x_device *bq, int mA) |
661 | { | 665 | { |
662 | int val; | 666 | int val; |
667 | |||
663 | if (bq->init_data.resistor_sense <= 0) | 668 | if (bq->init_data.resistor_sense <= 0) |
664 | return -ENOSYS; | 669 | return -ENOSYS; |
665 | 670 | ||
666 | val = (mA * bq->init_data.resistor_sense - 3400) / 3400; | 671 | val = (mA * bq->init_data.resistor_sense - 3400) / 3400; |
667 | |||
668 | if (val < 0) | 672 | if (val < 0) |
669 | val = 0; | 673 | val = 0; |
670 | else if (val > 7) | 674 | else if (val > 7) |
@@ -679,6 +683,7 @@ static int bq2415x_set_termination_current(struct bq2415x_device *bq, int mA) | |||
679 | static int bq2415x_get_termination_current(struct bq2415x_device *bq) | 683 | static int bq2415x_get_termination_current(struct bq2415x_device *bq) |
680 | { | 684 | { |
681 | int ret; | 685 | int ret; |
686 | |||
682 | if (bq->init_data.resistor_sense <= 0) | 687 | if (bq->init_data.resistor_sense <= 0) |
683 | return -ENOSYS; | 688 | return -ENOSYS; |
684 | 689 | ||
@@ -686,8 +691,7 @@ static int bq2415x_get_termination_current(struct bq2415x_device *bq) | |||
686 | BQ2415X_MASK_VI_TERM, BQ2415X_SHIFT_VI_TERM); | 691 | BQ2415X_MASK_VI_TERM, BQ2415X_SHIFT_VI_TERM); |
687 | if (ret < 0) | 692 | if (ret < 0) |
688 | return ret; | 693 | return ret; |
689 | else | 694 | return (3400 + 3400*ret) / bq->init_data.resistor_sense; |
690 | return (3400 + 3400*ret) / bq->init_data.resistor_sense; | ||
691 | } | 695 | } |
692 | 696 | ||
693 | /* set default value of property */ | 697 | /* set default value of property */ |
@@ -706,14 +710,17 @@ static int bq2415x_set_defaults(struct bq2415x_device *bq) | |||
706 | bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_DISABLE); | 710 | bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_DISABLE); |
707 | bq2415x_exec_command(bq, BQ2415X_CHARGER_DISABLE); | 711 | bq2415x_exec_command(bq, BQ2415X_CHARGER_DISABLE); |
708 | bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_DISABLE); | 712 | bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_DISABLE); |
713 | |||
709 | bq2415x_set_default_value(bq, current_limit); | 714 | bq2415x_set_default_value(bq, current_limit); |
710 | bq2415x_set_default_value(bq, weak_battery_voltage); | 715 | bq2415x_set_default_value(bq, weak_battery_voltage); |
711 | bq2415x_set_default_value(bq, battery_regulation_voltage); | 716 | bq2415x_set_default_value(bq, battery_regulation_voltage); |
717 | |||
712 | if (bq->init_data.resistor_sense > 0) { | 718 | if (bq->init_data.resistor_sense > 0) { |
713 | bq2415x_set_default_value(bq, charge_current); | 719 | bq2415x_set_default_value(bq, charge_current); |
714 | bq2415x_set_default_value(bq, termination_current); | 720 | bq2415x_set_default_value(bq, termination_current); |
715 | bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_ENABLE); | 721 | bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_ENABLE); |
716 | } | 722 | } |
723 | |||
717 | bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE); | 724 | bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE); |
718 | return 0; | 725 | return 0; |
719 | } | 726 | } |
@@ -844,8 +851,10 @@ static void bq2415x_timer_error(struct bq2415x_device *bq, const char *msg) | |||
844 | static void bq2415x_timer_work(struct work_struct *work) | 851 | static void bq2415x_timer_work(struct work_struct *work) |
845 | { | 852 | { |
846 | struct bq2415x_device *bq = container_of(work, struct bq2415x_device, | 853 | struct bq2415x_device *bq = container_of(work, struct bq2415x_device, |
847 | work.work); | 854 | work.work); |
848 | int ret, error, boost; | 855 | int ret; |
856 | int error; | ||
857 | int boost; | ||
849 | 858 | ||
850 | if (!bq->autotimer) | 859 | if (!bq->autotimer) |
851 | return; | 860 | return; |
@@ -946,10 +955,11 @@ static enum power_supply_property bq2415x_power_supply_props[] = { | |||
946 | }; | 955 | }; |
947 | 956 | ||
948 | static int bq2415x_power_supply_get_property(struct power_supply *psy, | 957 | static int bq2415x_power_supply_get_property(struct power_supply *psy, |
949 | enum power_supply_property psp, union power_supply_propval *val) | 958 | enum power_supply_property psp, |
959 | union power_supply_propval *val) | ||
950 | { | 960 | { |
951 | struct bq2415x_device *bq = container_of(psy, struct bq2415x_device, | 961 | struct bq2415x_device *bq = container_of(psy, struct bq2415x_device, |
952 | charger); | 962 | charger); |
953 | int ret; | 963 | int ret; |
954 | 964 | ||
955 | switch (psp) { | 965 | switch (psp) { |
@@ -1031,7 +1041,8 @@ static void bq2415x_power_supply_exit(struct bq2415x_device *bq) | |||
1031 | 1041 | ||
1032 | /* show *_status entries */ | 1042 | /* show *_status entries */ |
1033 | static ssize_t bq2415x_sysfs_show_status(struct device *dev, | 1043 | static ssize_t bq2415x_sysfs_show_status(struct device *dev, |
1034 | struct device_attribute *attr, char *buf) | 1044 | struct device_attribute *attr, |
1045 | char *buf) | ||
1035 | { | 1046 | { |
1036 | struct power_supply *psy = dev_get_drvdata(dev); | 1047 | struct power_supply *psy = dev_get_drvdata(dev); |
1037 | struct bq2415x_device *bq = container_of(psy, struct bq2415x_device, | 1048 | struct bq2415x_device *bq = container_of(psy, struct bq2415x_device, |
@@ -1053,17 +1064,19 @@ static ssize_t bq2415x_sysfs_show_status(struct device *dev, | |||
1053 | ret = bq2415x_exec_command(bq, command); | 1064 | ret = bq2415x_exec_command(bq, command); |
1054 | if (ret < 0) | 1065 | if (ret < 0) |
1055 | return ret; | 1066 | return ret; |
1056 | else | 1067 | return sprintf(buf, "%d\n", ret); |
1057 | return sprintf(buf, "%d\n", ret); | ||
1058 | } | 1068 | } |
1059 | 1069 | ||
1060 | /* set timer entry: | 1070 | /* |
1061 | auto - enable auto mode | 1071 | * set timer entry: |
1062 | off - disable auto mode | 1072 | * auto - enable auto mode |
1063 | (other values) - reset chip timer | 1073 | * off - disable auto mode |
1064 | */ | 1074 | * (other values) - reset chip timer |
1075 | */ | ||
1065 | static ssize_t bq2415x_sysfs_set_timer(struct device *dev, | 1076 | static ssize_t bq2415x_sysfs_set_timer(struct device *dev, |
1066 | struct device_attribute *attr, const char *buf, size_t count) | 1077 | struct device_attribute *attr, |
1078 | const char *buf, | ||
1079 | size_t count) | ||
1067 | { | 1080 | { |
1068 | struct power_supply *psy = dev_get_drvdata(dev); | 1081 | struct power_supply *psy = dev_get_drvdata(dev); |
1069 | struct bq2415x_device *bq = container_of(psy, struct bq2415x_device, | 1082 | struct bq2415x_device *bq = container_of(psy, struct bq2415x_device, |
@@ -1079,40 +1092,42 @@ static ssize_t bq2415x_sysfs_set_timer(struct device *dev, | |||
1079 | 1092 | ||
1080 | if (ret < 0) | 1093 | if (ret < 0) |
1081 | return ret; | 1094 | return ret; |
1082 | else | 1095 | return count; |
1083 | return count; | ||
1084 | } | 1096 | } |
1085 | 1097 | ||
1086 | /* show timer entry (auto or off) */ | 1098 | /* show timer entry (auto or off) */ |
1087 | static ssize_t bq2415x_sysfs_show_timer(struct device *dev, | 1099 | static ssize_t bq2415x_sysfs_show_timer(struct device *dev, |
1088 | struct device_attribute *attr, char *buf) | 1100 | struct device_attribute *attr, |
1101 | char *buf) | ||
1089 | { | 1102 | { |
1090 | struct power_supply *psy = dev_get_drvdata(dev); | 1103 | struct power_supply *psy = dev_get_drvdata(dev); |
1091 | struct bq2415x_device *bq = container_of(psy, struct bq2415x_device, | 1104 | struct bq2415x_device *bq = container_of(psy, struct bq2415x_device, |
1092 | charger); | 1105 | charger); |
1093 | 1106 | ||
1094 | if (bq->timer_error) | 1107 | if (bq->timer_error) |
1095 | return sprintf(buf, "%s\n", bq->timer_error); | 1108 | return sprintf(buf, "%s\n", bq->timer_error); |
1096 | 1109 | ||
1097 | if (bq->autotimer) | 1110 | if (bq->autotimer) |
1098 | return sprintf(buf, "auto\n"); | 1111 | return sprintf(buf, "auto\n"); |
1099 | else | 1112 | return sprintf(buf, "off\n"); |
1100 | return sprintf(buf, "off\n"); | ||
1101 | } | 1113 | } |
1102 | 1114 | ||
1103 | /* set mode entry: | 1115 | /* |
1104 | auto - if automode is supported, enable it and set mode to reported | 1116 | * set mode entry: |
1105 | none - disable charger and boost mode | 1117 | * auto - if automode is supported, enable it and set mode to reported |
1106 | host - charging mode for host/hub chargers (current limit 500mA) | 1118 | * none - disable charger and boost mode |
1107 | dedicated - charging mode for dedicated chargers (unlimited current limit) | 1119 | * host - charging mode for host/hub chargers (current limit 500mA) |
1108 | boost - disable charger and enable boost mode | 1120 | * dedicated - charging mode for dedicated chargers (unlimited current limit) |
1109 | */ | 1121 | * boost - disable charger and enable boost mode |
1122 | */ | ||
1110 | static ssize_t bq2415x_sysfs_set_mode(struct device *dev, | 1123 | static ssize_t bq2415x_sysfs_set_mode(struct device *dev, |
1111 | struct device_attribute *attr, const char *buf, size_t count) | 1124 | struct device_attribute *attr, |
1125 | const char *buf, | ||
1126 | size_t count) | ||
1112 | { | 1127 | { |
1113 | struct power_supply *psy = dev_get_drvdata(dev); | 1128 | struct power_supply *psy = dev_get_drvdata(dev); |
1114 | struct bq2415x_device *bq = container_of(psy, struct bq2415x_device, | 1129 | struct bq2415x_device *bq = container_of(psy, struct bq2415x_device, |
1115 | charger); | 1130 | charger); |
1116 | enum bq2415x_mode mode; | 1131 | enum bq2415x_mode mode; |
1117 | int ret = 0; | 1132 | int ret = 0; |
1118 | 1133 | ||
@@ -1144,19 +1159,20 @@ static ssize_t bq2415x_sysfs_set_mode(struct device *dev, | |||
1144 | return count; | 1159 | return count; |
1145 | bq->automode = 1; | 1160 | bq->automode = 1; |
1146 | mode = bq->reported_mode; | 1161 | mode = bq->reported_mode; |
1147 | } else | 1162 | } else { |
1148 | return -EINVAL; | 1163 | return -EINVAL; |
1164 | } | ||
1149 | 1165 | ||
1150 | ret = bq2415x_set_mode(bq, mode); | 1166 | ret = bq2415x_set_mode(bq, mode); |
1151 | if (ret < 0) | 1167 | if (ret < 0) |
1152 | return ret; | 1168 | return ret; |
1153 | else | 1169 | return count; |
1154 | return count; | ||
1155 | } | 1170 | } |
1156 | 1171 | ||
1157 | /* show mode entry (auto, none, host, dedicated or boost) */ | 1172 | /* show mode entry (auto, none, host, dedicated or boost) */ |
1158 | static ssize_t bq2415x_sysfs_show_mode(struct device *dev, | 1173 | static ssize_t bq2415x_sysfs_show_mode(struct device *dev, |
1159 | struct device_attribute *attr, char *buf) | 1174 | struct device_attribute *attr, |
1175 | char *buf) | ||
1160 | { | 1176 | { |
1161 | struct power_supply *psy = dev_get_drvdata(dev); | 1177 | struct power_supply *psy = dev_get_drvdata(dev); |
1162 | struct bq2415x_device *bq = container_of(psy, struct bq2415x_device, | 1178 | struct bq2415x_device *bq = container_of(psy, struct bq2415x_device, |
@@ -1190,11 +1206,12 @@ static ssize_t bq2415x_sysfs_show_mode(struct device *dev, | |||
1190 | 1206 | ||
1191 | /* show reported_mode entry (none, host, dedicated or boost) */ | 1207 | /* show reported_mode entry (none, host, dedicated or boost) */ |
1192 | static ssize_t bq2415x_sysfs_show_reported_mode(struct device *dev, | 1208 | static ssize_t bq2415x_sysfs_show_reported_mode(struct device *dev, |
1193 | struct device_attribute *attr, char *buf) | 1209 | struct device_attribute *attr, |
1210 | char *buf) | ||
1194 | { | 1211 | { |
1195 | struct power_supply *psy = dev_get_drvdata(dev); | 1212 | struct power_supply *psy = dev_get_drvdata(dev); |
1196 | struct bq2415x_device *bq = container_of(psy, struct bq2415x_device, | 1213 | struct bq2415x_device *bq = container_of(psy, struct bq2415x_device, |
1197 | charger); | 1214 | charger); |
1198 | 1215 | ||
1199 | if (bq->automode < 0) | 1216 | if (bq->automode < 0) |
1200 | return -EINVAL; | 1217 | return -EINVAL; |
@@ -1215,11 +1232,13 @@ static ssize_t bq2415x_sysfs_show_reported_mode(struct device *dev, | |||
1215 | 1232 | ||
1216 | /* directly set raw value to chip register, format: 'register value' */ | 1233 | /* directly set raw value to chip register, format: 'register value' */ |
1217 | static ssize_t bq2415x_sysfs_set_registers(struct device *dev, | 1234 | static ssize_t bq2415x_sysfs_set_registers(struct device *dev, |
1218 | struct device_attribute *attr, const char *buf, size_t count) | 1235 | struct device_attribute *attr, |
1236 | const char *buf, | ||
1237 | size_t count) | ||
1219 | { | 1238 | { |
1220 | struct power_supply *psy = dev_get_drvdata(dev); | 1239 | struct power_supply *psy = dev_get_drvdata(dev); |
1221 | struct bq2415x_device *bq = container_of(psy, struct bq2415x_device, | 1240 | struct bq2415x_device *bq = container_of(psy, struct bq2415x_device, |
1222 | charger); | 1241 | charger); |
1223 | ssize_t ret = 0; | 1242 | ssize_t ret = 0; |
1224 | unsigned int reg; | 1243 | unsigned int reg; |
1225 | unsigned int val; | 1244 | unsigned int val; |
@@ -1233,28 +1252,29 @@ static ssize_t bq2415x_sysfs_set_registers(struct device *dev, | |||
1233 | ret = bq2415x_i2c_write(bq, reg, val); | 1252 | ret = bq2415x_i2c_write(bq, reg, val); |
1234 | if (ret < 0) | 1253 | if (ret < 0) |
1235 | return ret; | 1254 | return ret; |
1236 | else | 1255 | return count; |
1237 | return count; | ||
1238 | } | 1256 | } |
1239 | 1257 | ||
1240 | /* print value of chip register, format: 'register=value' */ | 1258 | /* print value of chip register, format: 'register=value' */ |
1241 | static ssize_t bq2415x_sysfs_print_reg(struct bq2415x_device *bq, | 1259 | static ssize_t bq2415x_sysfs_print_reg(struct bq2415x_device *bq, |
1242 | u8 reg, char *buf) | 1260 | u8 reg, |
1261 | char *buf) | ||
1243 | { | 1262 | { |
1244 | int ret = bq2415x_i2c_read(bq, reg); | 1263 | int ret = bq2415x_i2c_read(bq, reg); |
1264 | |||
1245 | if (ret < 0) | 1265 | if (ret < 0) |
1246 | return sprintf(buf, "%#.2x=error %d\n", reg, ret); | 1266 | return sprintf(buf, "%#.2x=error %d\n", reg, ret); |
1247 | else | 1267 | return sprintf(buf, "%#.2x=%#.2x\n", reg, ret); |
1248 | return sprintf(buf, "%#.2x=%#.2x\n", reg, ret); | ||
1249 | } | 1268 | } |
1250 | 1269 | ||
1251 | /* show all raw values of chip register, format per line: 'register=value' */ | 1270 | /* show all raw values of chip register, format per line: 'register=value' */ |
1252 | static ssize_t bq2415x_sysfs_show_registers(struct device *dev, | 1271 | static ssize_t bq2415x_sysfs_show_registers(struct device *dev, |
1253 | struct device_attribute *attr, char *buf) | 1272 | struct device_attribute *attr, |
1273 | char *buf) | ||
1254 | { | 1274 | { |
1255 | struct power_supply *psy = dev_get_drvdata(dev); | 1275 | struct power_supply *psy = dev_get_drvdata(dev); |
1256 | struct bq2415x_device *bq = container_of(psy, struct bq2415x_device, | 1276 | struct bq2415x_device *bq = container_of(psy, struct bq2415x_device, |
1257 | charger); | 1277 | charger); |
1258 | ssize_t ret = 0; | 1278 | ssize_t ret = 0; |
1259 | 1279 | ||
1260 | ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_STATUS, buf+ret); | 1280 | ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_STATUS, buf+ret); |
@@ -1267,11 +1287,13 @@ static ssize_t bq2415x_sysfs_show_registers(struct device *dev, | |||
1267 | 1287 | ||
1268 | /* set current and voltage limit entries (in mA or mV) */ | 1288 | /* set current and voltage limit entries (in mA or mV) */ |
1269 | static ssize_t bq2415x_sysfs_set_limit(struct device *dev, | 1289 | static ssize_t bq2415x_sysfs_set_limit(struct device *dev, |
1270 | struct device_attribute *attr, const char *buf, size_t count) | 1290 | struct device_attribute *attr, |
1291 | const char *buf, | ||
1292 | size_t count) | ||
1271 | { | 1293 | { |
1272 | struct power_supply *psy = dev_get_drvdata(dev); | 1294 | struct power_supply *psy = dev_get_drvdata(dev); |
1273 | struct bq2415x_device *bq = container_of(psy, struct bq2415x_device, | 1295 | struct bq2415x_device *bq = container_of(psy, struct bq2415x_device, |
1274 | charger); | 1296 | charger); |
1275 | long val; | 1297 | long val; |
1276 | int ret; | 1298 | int ret; |
1277 | 1299 | ||
@@ -1293,17 +1315,17 @@ static ssize_t bq2415x_sysfs_set_limit(struct device *dev, | |||
1293 | 1315 | ||
1294 | if (ret < 0) | 1316 | if (ret < 0) |
1295 | return ret; | 1317 | return ret; |
1296 | else | 1318 | return count; |
1297 | return count; | ||
1298 | } | 1319 | } |
1299 | 1320 | ||
1300 | /* show current and voltage limit entries (in mA or mV) */ | 1321 | /* show current and voltage limit entries (in mA or mV) */ |
1301 | static ssize_t bq2415x_sysfs_show_limit(struct device *dev, | 1322 | static ssize_t bq2415x_sysfs_show_limit(struct device *dev, |
1302 | struct device_attribute *attr, char *buf) | 1323 | struct device_attribute *attr, |
1324 | char *buf) | ||
1303 | { | 1325 | { |
1304 | struct power_supply *psy = dev_get_drvdata(dev); | 1326 | struct power_supply *psy = dev_get_drvdata(dev); |
1305 | struct bq2415x_device *bq = container_of(psy, struct bq2415x_device, | 1327 | struct bq2415x_device *bq = container_of(psy, struct bq2415x_device, |
1306 | charger); | 1328 | charger); |
1307 | int ret; | 1329 | int ret; |
1308 | 1330 | ||
1309 | if (strcmp(attr->attr.name, "current_limit") == 0) | 1331 | if (strcmp(attr->attr.name, "current_limit") == 0) |
@@ -1321,17 +1343,18 @@ static ssize_t bq2415x_sysfs_show_limit(struct device *dev, | |||
1321 | 1343 | ||
1322 | if (ret < 0) | 1344 | if (ret < 0) |
1323 | return ret; | 1345 | return ret; |
1324 | else | 1346 | return sprintf(buf, "%d\n", ret); |
1325 | return sprintf(buf, "%d\n", ret); | ||
1326 | } | 1347 | } |
1327 | 1348 | ||
1328 | /* set *_enable entries */ | 1349 | /* set *_enable entries */ |
1329 | static ssize_t bq2415x_sysfs_set_enable(struct device *dev, | 1350 | static ssize_t bq2415x_sysfs_set_enable(struct device *dev, |
1330 | struct device_attribute *attr, const char *buf, size_t count) | 1351 | struct device_attribute *attr, |
1352 | const char *buf, | ||
1353 | size_t count) | ||
1331 | { | 1354 | { |
1332 | struct power_supply *psy = dev_get_drvdata(dev); | 1355 | struct power_supply *psy = dev_get_drvdata(dev); |
1333 | struct bq2415x_device *bq = container_of(psy, struct bq2415x_device, | 1356 | struct bq2415x_device *bq = container_of(psy, struct bq2415x_device, |
1334 | charger); | 1357 | charger); |
1335 | enum bq2415x_command command; | 1358 | enum bq2415x_command command; |
1336 | long val; | 1359 | long val; |
1337 | int ret; | 1360 | int ret; |
@@ -1357,17 +1380,17 @@ static ssize_t bq2415x_sysfs_set_enable(struct device *dev, | |||
1357 | ret = bq2415x_exec_command(bq, command); | 1380 | ret = bq2415x_exec_command(bq, command); |
1358 | if (ret < 0) | 1381 | if (ret < 0) |
1359 | return ret; | 1382 | return ret; |
1360 | else | 1383 | return count; |
1361 | return count; | ||
1362 | } | 1384 | } |
1363 | 1385 | ||
1364 | /* show *_enable entries */ | 1386 | /* show *_enable entries */ |
1365 | static ssize_t bq2415x_sysfs_show_enable(struct device *dev, | 1387 | static ssize_t bq2415x_sysfs_show_enable(struct device *dev, |
1366 | struct device_attribute *attr, char *buf) | 1388 | struct device_attribute *attr, |
1389 | char *buf) | ||
1367 | { | 1390 | { |
1368 | struct power_supply *psy = dev_get_drvdata(dev); | 1391 | struct power_supply *psy = dev_get_drvdata(dev); |
1369 | struct bq2415x_device *bq = container_of(psy, struct bq2415x_device, | 1392 | struct bq2415x_device *bq = container_of(psy, struct bq2415x_device, |
1370 | charger); | 1393 | charger); |
1371 | enum bq2415x_command command; | 1394 | enum bq2415x_command command; |
1372 | int ret; | 1395 | int ret; |
1373 | 1396 | ||
@@ -1385,8 +1408,7 @@ static ssize_t bq2415x_sysfs_show_enable(struct device *dev, | |||
1385 | ret = bq2415x_exec_command(bq, command); | 1408 | ret = bq2415x_exec_command(bq, command); |
1386 | if (ret < 0) | 1409 | if (ret < 0) |
1387 | return ret; | 1410 | return ret; |
1388 | else | 1411 | return sprintf(buf, "%d\n", ret); |
1389 | return sprintf(buf, "%d\n", ret); | ||
1390 | } | 1412 | } |
1391 | 1413 | ||
1392 | static DEVICE_ATTR(current_limit, S_IWUSR | S_IRUGO, | 1414 | static DEVICE_ATTR(current_limit, S_IWUSR | S_IRUGO, |
@@ -1425,6 +1447,10 @@ static DEVICE_ATTR(boost_status, S_IRUGO, bq2415x_sysfs_show_status, NULL); | |||
1425 | static DEVICE_ATTR(fault_status, S_IRUGO, bq2415x_sysfs_show_status, NULL); | 1447 | static DEVICE_ATTR(fault_status, S_IRUGO, bq2415x_sysfs_show_status, NULL); |
1426 | 1448 | ||
1427 | static struct attribute *bq2415x_sysfs_attributes[] = { | 1449 | static struct attribute *bq2415x_sysfs_attributes[] = { |
1450 | /* | ||
1451 | * TODO: some (appropriate) of these attrs should be switched to | ||
1452 | * use power supply class props. | ||
1453 | */ | ||
1428 | &dev_attr_current_limit.attr, | 1454 | &dev_attr_current_limit.attr, |
1429 | &dev_attr_weak_battery_voltage.attr, | 1455 | &dev_attr_weak_battery_voltage.attr, |
1430 | &dev_attr_battery_regulation_voltage.attr, | 1456 | &dev_attr_battery_regulation_voltage.attr, |
@@ -1466,7 +1492,7 @@ static void bq2415x_sysfs_exit(struct bq2415x_device *bq) | |||
1466 | 1492 | ||
1467 | /* main bq2415x probe function */ | 1493 | /* main bq2415x probe function */ |
1468 | static int bq2415x_probe(struct i2c_client *client, | 1494 | static int bq2415x_probe(struct i2c_client *client, |
1469 | const struct i2c_device_id *id) | 1495 | const struct i2c_device_id *id) |
1470 | { | 1496 | { |
1471 | int ret; | 1497 | int ret; |
1472 | int num; | 1498 | int num; |