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