blob: 450b1dfa28f7be8970d9e42f863e03b4170cc707 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Łukasz Majewskieba42392012-11-13 03:22:01 +00002/*
3 * Copyright (C) 2012 Samsung Electronics
4 * Lukasz Majewski <l.majewski@samsung.com>
Łukasz Majewskieba42392012-11-13 03:22:01 +00005 */
6
7#include <common.h>
Simon Glass24b852a2015-11-08 23:47:45 -07008#include <console.h>
Łukasz Majewskieba42392012-11-13 03:22:01 +00009#include <power/pmic.h>
10#include <power/battery.h>
11#include <power/max8997_pmic.h>
12#include <errno.h>
13
14static struct battery battery_trats;
15
16static int power_battery_charge(struct pmic *bat)
17{
18 struct power_battery *p_bat = bat->pbat;
19 struct battery *battery = p_bat->bat;
20 int k;
21
Simon Glass78a36c32014-05-20 06:01:35 -060022 if (bat->chrg->chrg_state(p_bat->chrg, PMIC_CHARGER_ENABLE, 450))
Łukasz Majewskieba42392012-11-13 03:22:01 +000023 return -1;
24
25 for (k = 0; bat->chrg->chrg_bat_present(p_bat->chrg) &&
26 bat->chrg->chrg_type(p_bat->muic) &&
27 battery->state_of_chrg < 100; k++) {
Łukasz Majewski9dfa3d02013-06-25 15:28:15 +020028 udelay(2000000);
29 if (!(k % 5))
30 puts(".");
Łukasz Majewskieba42392012-11-13 03:22:01 +000031 bat->fg->fg_battery_update(p_bat->fg, bat);
32
Łukasz Majewski9dfa3d02013-06-25 15:28:15 +020033 if (k == 200) {
Łukasz Majewskieba42392012-11-13 03:22:01 +000034 debug(" %d [V]", battery->voltage_uV);
35 puts("\n");
36 k = 0;
37 }
38
Łukasz Majewski9dfa3d02013-06-25 15:28:15 +020039 if (ctrlc()) {
40 printf("\nCharging disabled on request.\n");
41 goto exit;
42 }
Łukasz Majewskieba42392012-11-13 03:22:01 +000043 }
Łukasz Majewski9dfa3d02013-06-25 15:28:15 +020044 exit:
Simon Glass78a36c32014-05-20 06:01:35 -060045 bat->chrg->chrg_state(p_bat->chrg, PMIC_CHARGER_DISABLE, 0);
Łukasz Majewskieba42392012-11-13 03:22:01 +000046
47 return 0;
48}
49
50static int power_battery_init_trats(struct pmic *bat_,
51 struct pmic *fg_,
52 struct pmic *chrg_,
53 struct pmic *muic_)
54{
55 bat_->pbat->fg = fg_;
56 bat_->pbat->chrg = chrg_;
57 bat_->pbat->muic = muic_;
58
59 bat_->fg = fg_->fg;
60 bat_->chrg = chrg_->chrg;
61 bat_->chrg->chrg_type = muic_->chrg->chrg_type;
62 return 0;
63}
64
65static struct power_battery power_bat_trats = {
66 .bat = &battery_trats,
67 .battery_init = power_battery_init_trats,
68 .battery_charge = power_battery_charge,
69};
70
71int power_bat_init(unsigned char bus)
72{
73 static const char name[] = "BAT_TRATS";
74 struct pmic *p = pmic_alloc();
75
76 if (!p) {
77 printf("%s: POWER allocation error!\n", __func__);
78 return -ENOMEM;
79 }
80
81 debug("Board BAT init\n");
82
83 p->interface = PMIC_NONE;
84 p->name = name;
85 p->bus = bus;
86
87 p->pbat = &power_bat_trats;
88 return 0;
89}