1 /*
2  *  Copyright (C) 2012 Samsung Electronics
3  *  Lukasz Majewski <l.majewski@samsung.com>
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23 
24 #include <common.h>
25 #include <power/pmic.h>
26 #include <power/battery.h>
27 #include <power/max8997_pmic.h>
28 #include <errno.h>
29 
30 static struct battery battery_trats;
31 
32 static int power_battery_charge(struct pmic *bat)
33 {
34 	struct power_battery *p_bat = bat->pbat;
35 	struct battery *battery = p_bat->bat;
36 	int k;
37 
38 	if (bat->chrg->chrg_state(p_bat->chrg, CHARGER_ENABLE, 450))
39 		return -1;
40 
41 	for (k = 0; bat->chrg->chrg_bat_present(p_bat->chrg) &&
42 		     bat->chrg->chrg_type(p_bat->muic) &&
43 		     battery->state_of_chrg < 100; k++) {
44 		udelay(2000000);
45 		if (!(k % 5))
46 			puts(".");
47 		bat->fg->fg_battery_update(p_bat->fg, bat);
48 
49 		if (k == 200) {
50 			debug(" %d [V]", battery->voltage_uV);
51 			puts("\n");
52 			k = 0;
53 		}
54 
55 		if (ctrlc()) {
56 			printf("\nCharging disabled on request.\n");
57 			goto exit;
58 		}
59 	}
60  exit:
61 	bat->chrg->chrg_state(p_bat->chrg, CHARGER_DISABLE, 0);
62 
63 	return 0;
64 }
65 
66 static int power_battery_init_trats(struct pmic *bat_,
67 				    struct pmic *fg_,
68 				    struct pmic *chrg_,
69 				    struct pmic *muic_)
70 {
71 	bat_->pbat->fg = fg_;
72 	bat_->pbat->chrg = chrg_;
73 	bat_->pbat->muic = muic_;
74 
75 	bat_->fg = fg_->fg;
76 	bat_->chrg = chrg_->chrg;
77 	bat_->chrg->chrg_type = muic_->chrg->chrg_type;
78 	return 0;
79 }
80 
81 static struct power_battery power_bat_trats = {
82 	.bat = &battery_trats,
83 	.battery_init = power_battery_init_trats,
84 	.battery_charge = power_battery_charge,
85 };
86 
87 int power_bat_init(unsigned char bus)
88 {
89 	static const char name[] = "BAT_TRATS";
90 	struct pmic *p = pmic_alloc();
91 
92 	if (!p) {
93 		printf("%s: POWER allocation error!\n", __func__);
94 		return -ENOMEM;
95 	}
96 
97 	debug("Board BAT init\n");
98 
99 	p->interface = PMIC_NONE;
100 	p->name = name;
101 	p->bus = bus;
102 
103 	p->pbat = &power_bat_trats;
104 	return 0;
105 }
106