blob: 87c438ea0476fe8305c763b4ae2eb14569a4ddf9 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Łukasz Majewskie2ec82c2012-03-29 01:29:17 +00002/*
3 * Copyright (C) 2012 Samsung Electronics
4 * Lukasz Majewski <l.majewski@samsung.com>
Łukasz Majewskie2ec82c2012-03-29 01:29:17 +00005 */
6
7#include <common.h>
Łukasz Majewski1c6dba12012-11-13 03:21:55 +00008#include <power/pmic.h>
9#include <power/max8997_pmic.h>
Łukasz Majewski72ac7942012-11-13 03:21:54 +000010#include <i2c.h>
Łukasz Majewski1c6dba12012-11-13 03:21:55 +000011#include <errno.h>
Łukasz Majewskie2ec82c2012-03-29 01:29:17 +000012
Łukasz Majewski57e91112012-11-13 03:22:04 +000013unsigned 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
Łukasz Majewski77313a62012-11-13 03:22:09 +000029static 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))
Jaehoon Chung10e80f32016-12-15 20:49:50 +090035 return -ENODEV;
Łukasz Majewski77313a62012-11-13 03:22:09 +000036
Simon Glass05400a62014-05-20 06:01:35 -060037 if (state == PMIC_CHARGER_DISABLE) {
Łukasz Majewski77313a62012-11-13 03:22:09 +000038 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
Jaehoon Chung10e80f32016-12-15 20:49:50 +090043 return -ENOTSUPP;
Łukasz Majewski77313a62012-11-13 03:22:09 +000044 }
45
46 if (current < CHARGER_MIN_CURRENT || current > CHARGER_MAX_CURRENT) {
47 printf("%s: Wrong charge current: %d [mA]\n",
48 __func__, current);
Jaehoon Chung10e80f32016-12-15 20:49:50 +090049 return -EINVAL;
Łukasz Majewski77313a62012-11-13 03:22:09 +000050 }
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
68static int pmic_charger_bat_present(struct pmic *p)
69{
70 u32 val;
71
72 if (pmic_probe(p))
Jaehoon Chung10e80f32016-12-15 20:49:50 +090073 return -ENODEV;
Łukasz Majewski77313a62012-11-13 03:22:09 +000074
75 pmic_reg_read(p, MAX8997_REG_STATUS4, &val);
76
77 return !(val & DETBAT);
78}
79
80static struct power_chrg power_chrg_pmic_ops = {
81 .chrg_bat_present = pmic_charger_bat_present,
82 .chrg_state = pmic_charger_state,
83};
84
Łukasz Majewski1c6dba12012-11-13 03:21:55 +000085int pmic_init(unsigned char bus)
Łukasz Majewskie2ec82c2012-03-29 01:29:17 +000086{
Łukasz Majewskie2ec82c2012-03-29 01:29:17 +000087 static const char name[] = "MAX8997_PMIC";
Łukasz Majewski1c6dba12012-11-13 03:21:55 +000088 struct pmic *p = pmic_alloc();
89
90 if (!p) {
91 printf("%s: POWER allocation error!\n", __func__);
92 return -ENOMEM;
93 }
Łukasz Majewskie2ec82c2012-03-29 01:29:17 +000094
Łukasz Majewski77313a62012-11-13 03:22:09 +000095 debug("Board PMIC init\n");
Łukasz Majewskie2ec82c2012-03-29 01:29:17 +000096
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;
Łukasz Majewski1c6dba12012-11-13 03:21:55 +0000102 p->bus = bus;
Łukasz Majewskie2ec82c2012-03-29 01:29:17 +0000103
Łukasz Majewski77313a62012-11-13 03:22:09 +0000104 p->chrg = &power_chrg_pmic_ops;
Łukasz Majewskie2ec82c2012-03-29 01:29:17 +0000105 return 0;
106}