blob: 54b2bf91e5c2c876a6095ff1716ee2e9cf840775 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Łukasz Majewskidc930212012-11-13 03:22:01 +00002/*
3 * Copyright (C) 2012 Samsung Electronics
4 * Lukasz Majewski <l.majewski@samsung.com>
Łukasz Majewskidc930212012-11-13 03:22:01 +00005 */
6
7#include <common.h>
Simon Glassa73bda42015-11-08 23:47:45 -07008#include <console.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glassdbd79542020-05-10 11:40:11 -060010#include <linux/delay.h>
Łukasz Majewskidc930212012-11-13 03:22:01 +000011#include <power/pmic.h>
12#include <power/battery.h>
13#include <power/max8997_pmic.h>
14#include <errno.h>
15
16static struct battery battery_trats;
17
18static 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 Glass05400a62014-05-20 06:01:35 -060024 if (bat->chrg->chrg_state(p_bat->chrg, PMIC_CHARGER_ENABLE, 450))
Łukasz Majewskidc930212012-11-13 03:22:01 +000025 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 Majewskie1d772a2013-06-25 15:28:15 +020030 udelay(2000000);
31 if (!(k % 5))
32 puts(".");
Łukasz Majewskidc930212012-11-13 03:22:01 +000033 bat->fg->fg_battery_update(p_bat->fg, bat);
34
Łukasz Majewskie1d772a2013-06-25 15:28:15 +020035 if (k == 200) {
Łukasz Majewskidc930212012-11-13 03:22:01 +000036 debug(" %d [V]", battery->voltage_uV);
37 puts("\n");
38 k = 0;
39 }
40
Łukasz Majewskie1d772a2013-06-25 15:28:15 +020041 if (ctrlc()) {
42 printf("\nCharging disabled on request.\n");
43 goto exit;
44 }
Łukasz Majewskidc930212012-11-13 03:22:01 +000045 }
Łukasz Majewskie1d772a2013-06-25 15:28:15 +020046 exit:
Simon Glass05400a62014-05-20 06:01:35 -060047 bat->chrg->chrg_state(p_bat->chrg, PMIC_CHARGER_DISABLE, 0);
Łukasz Majewskidc930212012-11-13 03:22:01 +000048
49 return 0;
50}
51
52static 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
67static 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
73int 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}