1 /*
2  *  Copyright (C) 2012 Samsung Electronics
3  *  Lukasz Majewski <l.majewski@samsung.com>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <power/pmic.h>
10 #include <power/power_chrg.h>
11 #include <power/max8997_muic.h>
12 #include <i2c.h>
13 #include <errno.h>
14 
15 static int power_chrg_get_type(struct pmic *p)
16 {
17 	unsigned int val;
18 	unsigned char charge_type, charger;
19 
20 	if (pmic_probe(p))
21 		return CHARGER_NO;
22 
23 	pmic_reg_read(p, MAX8997_MUIC_STATUS2, &val);
24 	charge_type = val & MAX8997_MUIC_CHG_MASK;
25 
26 	switch (charge_type) {
27 	case MAX8997_MUIC_CHG_NO:
28 		charger = CHARGER_NO;
29 		break;
30 	case MAX8997_MUIC_CHG_USB:
31 	case MAX8997_MUIC_CHG_USB_D:
32 		charger = CHARGER_USB;
33 		break;
34 	case MAX8997_MUIC_CHG_TA:
35 	case MAX8997_MUIC_CHG_TA_1A:
36 		charger = CHARGER_TA;
37 		break;
38 	case MAX8997_MUIC_CHG_TA_500:
39 		charger = CHARGER_TA_500;
40 		break;
41 	default:
42 		charger = CHARGER_UNKNOWN;
43 		break;
44 	}
45 
46 	return charger;
47 }
48 
49 static struct power_chrg power_chrg_muic_ops = {
50 	.chrg_type = power_chrg_get_type,
51 };
52 
53 int power_muic_init(unsigned int bus)
54 {
55 	static const char name[] = "MAX8997_MUIC";
56 	struct pmic *p = pmic_alloc();
57 
58 	if (!p) {
59 		printf("%s: POWER allocation error!\n", __func__);
60 		return -ENOMEM;
61 	}
62 
63 	debug("Board Micro USB Interface Controller init\n");
64 
65 	p->name = name;
66 	p->interface = PMIC_I2C;
67 	p->number_of_regs = MUIC_NUM_OF_REGS;
68 	p->hw.i2c.addr = MAX8997_MUIC_I2C_ADDR;
69 	p->hw.i2c.tx_num = 1;
70 	p->bus = bus;
71 
72 	p->chrg = &power_chrg_muic_ops;
73 	return 0;
74 }
75