Łukasz Majewski | eba4239 | 2012-11-13 03:22:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Samsung Electronics |
| 3 | * Lukasz Majewski <l.majewski@samsung.com> |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Łukasz Majewski | eba4239 | 2012-11-13 03:22:01 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Simon Glass | 24b852a | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 9 | #include <console.h> |
Łukasz Majewski | eba4239 | 2012-11-13 03:22:01 +0000 | [diff] [blame] | 10 | #include <power/pmic.h> |
| 11 | #include <power/battery.h> |
| 12 | #include <power/max8997_pmic.h> |
| 13 | #include <errno.h> |
| 14 | |
| 15 | static struct battery battery_trats; |
| 16 | |
| 17 | static int power_battery_charge(struct pmic *bat) |
| 18 | { |
| 19 | struct power_battery *p_bat = bat->pbat; |
| 20 | struct battery *battery = p_bat->bat; |
| 21 | int k; |
| 22 | |
Simon Glass | 78a36c3 | 2014-05-20 06:01:35 -0600 | [diff] [blame] | 23 | if (bat->chrg->chrg_state(p_bat->chrg, PMIC_CHARGER_ENABLE, 450)) |
Łukasz Majewski | eba4239 | 2012-11-13 03:22:01 +0000 | [diff] [blame] | 24 | return -1; |
| 25 | |
| 26 | for (k = 0; bat->chrg->chrg_bat_present(p_bat->chrg) && |
| 27 | bat->chrg->chrg_type(p_bat->muic) && |
| 28 | battery->state_of_chrg < 100; k++) { |
Łukasz Majewski | 9dfa3d0 | 2013-06-25 15:28:15 +0200 | [diff] [blame] | 29 | udelay(2000000); |
| 30 | if (!(k % 5)) |
| 31 | puts("."); |
Łukasz Majewski | eba4239 | 2012-11-13 03:22:01 +0000 | [diff] [blame] | 32 | bat->fg->fg_battery_update(p_bat->fg, bat); |
| 33 | |
Łukasz Majewski | 9dfa3d0 | 2013-06-25 15:28:15 +0200 | [diff] [blame] | 34 | if (k == 200) { |
Łukasz Majewski | eba4239 | 2012-11-13 03:22:01 +0000 | [diff] [blame] | 35 | debug(" %d [V]", battery->voltage_uV); |
| 36 | puts("\n"); |
| 37 | k = 0; |
| 38 | } |
| 39 | |
Łukasz Majewski | 9dfa3d0 | 2013-06-25 15:28:15 +0200 | [diff] [blame] | 40 | if (ctrlc()) { |
| 41 | printf("\nCharging disabled on request.\n"); |
| 42 | goto exit; |
| 43 | } |
Łukasz Majewski | eba4239 | 2012-11-13 03:22:01 +0000 | [diff] [blame] | 44 | } |
Łukasz Majewski | 9dfa3d0 | 2013-06-25 15:28:15 +0200 | [diff] [blame] | 45 | exit: |
Simon Glass | 78a36c3 | 2014-05-20 06:01:35 -0600 | [diff] [blame] | 46 | bat->chrg->chrg_state(p_bat->chrg, PMIC_CHARGER_DISABLE, 0); |
Łukasz Majewski | eba4239 | 2012-11-13 03:22:01 +0000 | [diff] [blame] | 47 | |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | static int power_battery_init_trats(struct pmic *bat_, |
| 52 | struct pmic *fg_, |
| 53 | struct pmic *chrg_, |
| 54 | struct pmic *muic_) |
| 55 | { |
| 56 | bat_->pbat->fg = fg_; |
| 57 | bat_->pbat->chrg = chrg_; |
| 58 | bat_->pbat->muic = muic_; |
| 59 | |
| 60 | bat_->fg = fg_->fg; |
| 61 | bat_->chrg = chrg_->chrg; |
| 62 | bat_->chrg->chrg_type = muic_->chrg->chrg_type; |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | static struct power_battery power_bat_trats = { |
| 67 | .bat = &battery_trats, |
| 68 | .battery_init = power_battery_init_trats, |
| 69 | .battery_charge = power_battery_charge, |
| 70 | }; |
| 71 | |
| 72 | int power_bat_init(unsigned char bus) |
| 73 | { |
| 74 | static const char name[] = "BAT_TRATS"; |
| 75 | struct pmic *p = pmic_alloc(); |
| 76 | |
| 77 | if (!p) { |
| 78 | printf("%s: POWER allocation error!\n", __func__); |
| 79 | return -ENOMEM; |
| 80 | } |
| 81 | |
| 82 | debug("Board BAT init\n"); |
| 83 | |
| 84 | p->interface = PMIC_NONE; |
| 85 | p->name = name; |
| 86 | p->bus = bus; |
| 87 | |
| 88 | p->pbat = &power_bat_trats; |
| 89 | return 0; |
| 90 | } |