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