Łukasz Majewski | 8504296 | 2012-03-29 01:29:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Samsung Electronics |
| 3 | * Lukasz Majewski <l.majewski@samsung.com> |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Łukasz Majewski | 8504296 | 2012-03-29 01:29:17 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Łukasz Majewski | c733681 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 9 | #include <power/pmic.h> |
| 10 | #include <power/max8997_pmic.h> |
Łukasz Majewski | 452329f | 2012-11-13 03:21:54 +0000 | [diff] [blame] | 11 | #include <i2c.h> |
Łukasz Majewski | c733681 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 12 | #include <errno.h> |
Łukasz Majewski | 8504296 | 2012-03-29 01:29:17 +0000 | [diff] [blame] | 13 | |
Łukasz Majewski | bf995a9 | 2012-11-13 03:22:04 +0000 | [diff] [blame] | 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 | |
Łukasz Majewski | d955c6d | 2012-11-13 03:22:09 +0000 | [diff] [blame] | 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 | |
Łukasz Majewski | c733681 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 86 | int pmic_init(unsigned char bus) |
Łukasz Majewski | 8504296 | 2012-03-29 01:29:17 +0000 | [diff] [blame] | 87 | { |
Łukasz Majewski | 8504296 | 2012-03-29 01:29:17 +0000 | [diff] [blame] | 88 | static const char name[] = "MAX8997_PMIC"; |
Łukasz Majewski | c733681 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 89 | struct pmic *p = pmic_alloc(); |
| 90 | |
| 91 | if (!p) { |
| 92 | printf("%s: POWER allocation error!\n", __func__); |
| 93 | return -ENOMEM; |
| 94 | } |
Łukasz Majewski | 8504296 | 2012-03-29 01:29:17 +0000 | [diff] [blame] | 95 | |
Łukasz Majewski | d955c6d | 2012-11-13 03:22:09 +0000 | [diff] [blame] | 96 | debug("Board PMIC init\n"); |
Łukasz Majewski | 8504296 | 2012-03-29 01:29:17 +0000 | [diff] [blame] | 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; |
Łukasz Majewski | c733681 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 103 | p->bus = bus; |
Łukasz Majewski | 8504296 | 2012-03-29 01:29:17 +0000 | [diff] [blame] | 104 | |
Łukasz Majewski | d955c6d | 2012-11-13 03:22:09 +0000 | [diff] [blame] | 105 | p->chrg = &power_chrg_pmic_ops; |
Łukasz Majewski | 8504296 | 2012-03-29 01:29:17 +0000 | [diff] [blame] | 106 | return 0; |
| 107 | } |