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