blob: 32afb3f7d0a39aa3d74342dca2aec19d04106fb2 [file] [log] [blame]
Łukasz Majewskie2ec82c2012-03-29 01:29:17 +00001/*
2 * Copyright (C) 2012 Samsung Electronics
3 * Lukasz Majewski <l.majewski@samsung.com>
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Łukasz Majewskie2ec82c2012-03-29 01:29:17 +00006 */
7
8#include <common.h>
Łukasz Majewski1c6dba12012-11-13 03:21:55 +00009#include <power/pmic.h>
10#include <power/max8997_pmic.h>
Łukasz Majewski72ac7942012-11-13 03:21:54 +000011#include <i2c.h>
Łukasz Majewski1c6dba12012-11-13 03:21:55 +000012#include <errno.h>
Łukasz Majewskie2ec82c2012-03-29 01:29:17 +000013
Łukasz Majewski57e91112012-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 Majewski77313a62012-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))
Jaehoon Chung10e80f32016-12-15 20:49:50 +090036 return -ENODEV;
Łukasz Majewski77313a62012-11-13 03:22:09 +000037
Simon Glass05400a62014-05-20 06:01:35 -060038 if (state == PMIC_CHARGER_DISABLE) {
Łukasz Majewski77313a62012-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
Jaehoon Chung10e80f32016-12-15 20:49:50 +090044 return -ENOTSUPP;
Łukasz Majewski77313a62012-11-13 03:22:09 +000045 }
46
47 if (current < CHARGER_MIN_CURRENT || current > CHARGER_MAX_CURRENT) {
48 printf("%s: Wrong charge current: %d [mA]\n",
49 __func__, current);
Jaehoon Chung10e80f32016-12-15 20:49:50 +090050 return -EINVAL;
Łukasz Majewski77313a62012-11-13 03:22:09 +000051 }
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))
Jaehoon Chung10e80f32016-12-15 20:49:50 +090074 return -ENODEV;
Łukasz Majewski77313a62012-11-13 03:22:09 +000075
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 Majewski1c6dba12012-11-13 03:21:55 +000086int pmic_init(unsigned char bus)
Łukasz Majewskie2ec82c2012-03-29 01:29:17 +000087{
Łukasz Majewskie2ec82c2012-03-29 01:29:17 +000088 static const char name[] = "MAX8997_PMIC";
Łukasz Majewski1c6dba12012-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 Majewskie2ec82c2012-03-29 01:29:17 +000095
Łukasz Majewski77313a62012-11-13 03:22:09 +000096 debug("Board PMIC init\n");
Łukasz Majewskie2ec82c2012-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 Majewski1c6dba12012-11-13 03:21:55 +0000103 p->bus = bus;
Łukasz Majewskie2ec82c2012-03-29 01:29:17 +0000104
Łukasz Majewski77313a62012-11-13 03:22:09 +0000105 p->chrg = &power_chrg_pmic_ops;
Łukasz Majewskie2ec82c2012-03-29 01:29:17 +0000106 return 0;
107}