1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  Copyright (C) 2012 Samsung Electronics
4  *  Lukasz Majewski <l.majewski@samsung.com>
5  */
6 
7 #include <common.h>
8 #include <console.h>
9 #include <log.h>
10 #include <linux/delay.h>
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 
power_battery_charge(struct pmic * bat)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 
24 	if (bat->chrg->chrg_state(p_bat->chrg, PMIC_CHARGER_ENABLE, 450))
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++) {
30 		udelay(2000000);
31 		if (!(k % 5))
32 			puts(".");
33 		bat->fg->fg_battery_update(p_bat->fg, bat);
34 
35 		if (k == 200) {
36 			debug(" %d [V]", battery->voltage_uV);
37 			puts("\n");
38 			k = 0;
39 		}
40 
41 		if (ctrlc()) {
42 			printf("\nCharging disabled on request.\n");
43 			goto exit;
44 		}
45 	}
46  exit:
47 	bat->chrg->chrg_state(p_bat->chrg, PMIC_CHARGER_DISABLE, 0);
48 
49 	return 0;
50 }
51 
power_battery_init_trats(struct pmic * bat_,struct pmic * fg_,struct pmic * chrg_,struct pmic * muic_)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 
power_bat_init(unsigned char bus)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 }
92