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/max8997_pmic.h>
11 #include <i2c.h>
12 #include <errno.h>
13 
14 unsigned char max8997_reg_ldo(int uV)
15 {
16 	unsigned char ret;
17 	if (uV <= 800000)
18 		return 0;
19 	if (uV >= 3950000)
20 		return MAX8997_LDO_MAX_VAL;
21 	ret = (uV - 800000) / 50000;
22 	if (ret > MAX8997_LDO_MAX_VAL) {
23 		printf("MAX8997 LDO SETTING ERROR (%duV) -> %u\n", uV, ret);
24 		ret = MAX8997_LDO_MAX_VAL;
25 	}
26 
27 	return ret;
28 }
29 
30 static int pmic_charger_state(struct pmic *p, int state, int current)
31 {
32 	unsigned char fc;
33 	u32 val = 0;
34 
35 	if (pmic_probe(p))
36 		return -1;
37 
38 	if (state == CHARGER_DISABLE) {
39 		puts("Disable the charger.\n");
40 		pmic_reg_read(p, MAX8997_REG_MBCCTRL2, &val);
41 		val &= ~(MBCHOSTEN | VCHGR_FC);
42 		pmic_reg_write(p, MAX8997_REG_MBCCTRL2, val);
43 
44 		return -1;
45 	}
46 
47 	if (current < CHARGER_MIN_CURRENT || current > CHARGER_MAX_CURRENT) {
48 		printf("%s: Wrong charge current: %d [mA]\n",
49 		       __func__, current);
50 		return -1;
51 	}
52 
53 	fc = (current - CHARGER_MIN_CURRENT) / CHARGER_CURRENT_RESOLUTION;
54 	fc = fc & 0xf; /* up to 950 mA */
55 
56 	printf("Enable the charger @ %d [mA]\n", fc * CHARGER_CURRENT_RESOLUTION
57 	       + CHARGER_MIN_CURRENT);
58 
59 	val = fc | MBCICHFCSET;
60 	pmic_reg_write(p, MAX8997_REG_MBCCTRL4, val);
61 
62 	pmic_reg_read(p, MAX8997_REG_MBCCTRL2, &val);
63 	val = MBCHOSTEN | VCHGR_FC; /* enable charger & fast charge */
64 	pmic_reg_write(p, MAX8997_REG_MBCCTRL2, val);
65 
66 	return 0;
67 }
68 
69 static int pmic_charger_bat_present(struct pmic *p)
70 {
71 	u32 val;
72 
73 	if (pmic_probe(p))
74 		return -1;
75 
76 	pmic_reg_read(p, MAX8997_REG_STATUS4, &val);
77 
78 	return !(val & DETBAT);
79 }
80 
81 static struct power_chrg power_chrg_pmic_ops = {
82 	.chrg_bat_present = pmic_charger_bat_present,
83 	.chrg_state = pmic_charger_state,
84 };
85 
86 int pmic_init(unsigned char bus)
87 {
88 	static const char name[] = "MAX8997_PMIC";
89 	struct pmic *p = pmic_alloc();
90 
91 	if (!p) {
92 		printf("%s: POWER allocation error!\n", __func__);
93 		return -ENOMEM;
94 	}
95 
96 	debug("Board PMIC init\n");
97 
98 	p->name = name;
99 	p->interface = PMIC_I2C;
100 	p->number_of_regs = PMIC_NUM_OF_REGS;
101 	p->hw.i2c.addr = MAX8997_I2C_ADDR;
102 	p->hw.i2c.tx_num = 1;
103 	p->bus = bus;
104 
105 	p->chrg = &power_chrg_pmic_ops;
106 	return 0;
107 }
108