blob: a36a9a08bf48b821bc3c47076f50fd8c561d7b54 [file] [log] [blame]
Łukasz Majewski85042962012-03-29 01:29:17 +00001/*
2 * Copyright (C) 2012 Samsung Electronics
3 * Lukasz Majewski <l.majewski@samsung.com>
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Łukasz Majewski85042962012-03-29 01:29:17 +00006 */
7
8#include <common.h>
Łukasz Majewskic7336812012-11-13 03:21:55 +00009#include <power/pmic.h>
10#include <power/max8997_pmic.h>
Łukasz Majewski452329f2012-11-13 03:21:54 +000011#include <i2c.h>
Łukasz Majewskic7336812012-11-13 03:21:55 +000012#include <errno.h>
Łukasz Majewski85042962012-03-29 01:29:17 +000013
Łukasz Majewskibf995a92012-11-13 03:22:04 +000014unsigned 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
Łukasz Majewskid955c6d2012-11-13 03:22:09 +000030static 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
Simon Glass78a36c32014-05-20 06:01:35 -060038 if (state == PMIC_CHARGER_DISABLE) {
Łukasz Majewskid955c6d2012-11-13 03:22:09 +000039 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
69static 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
81static struct power_chrg power_chrg_pmic_ops = {
82 .chrg_bat_present = pmic_charger_bat_present,
83 .chrg_state = pmic_charger_state,
84};
85
Łukasz Majewskic7336812012-11-13 03:21:55 +000086int pmic_init(unsigned char bus)
Łukasz Majewski85042962012-03-29 01:29:17 +000087{
Łukasz Majewski85042962012-03-29 01:29:17 +000088 static const char name[] = "MAX8997_PMIC";
Łukasz Majewskic7336812012-11-13 03:21:55 +000089 struct pmic *p = pmic_alloc();
90
91 if (!p) {
92 printf("%s: POWER allocation error!\n", __func__);
93 return -ENOMEM;
94 }
Łukasz Majewski85042962012-03-29 01:29:17 +000095
Łukasz Majewskid955c6d2012-11-13 03:22:09 +000096 debug("Board PMIC init\n");
Łukasz Majewski85042962012-03-29 01:29:17 +000097
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;
Łukasz Majewskic7336812012-11-13 03:21:55 +0000103 p->bus = bus;
Łukasz Majewski85042962012-03-29 01:29:17 +0000104
Łukasz Majewskid955c6d2012-11-13 03:22:09 +0000105 p->chrg = &power_chrg_pmic_ops;
Łukasz Majewski85042962012-03-29 01:29:17 +0000106 return 0;
107}