Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Łukasz Majewski | 8504296 | 2012-03-29 01:29:17 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2012 Samsung Electronics |
| 4 | * Lukasz Majewski <l.majewski@samsung.com> |
Łukasz Majewski | 8504296 | 2012-03-29 01:29:17 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Łukasz Majewski | c733681 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 8 | #include <power/pmic.h> |
| 9 | #include <power/max8997_pmic.h> |
Łukasz Majewski | 452329f | 2012-11-13 03:21:54 +0000 | [diff] [blame] | 10 | #include <i2c.h> |
Łukasz Majewski | c733681 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 11 | #include <errno.h> |
Łukasz Majewski | 8504296 | 2012-03-29 01:29:17 +0000 | [diff] [blame] | 12 | |
Łukasz Majewski | bf995a9 | 2012-11-13 03:22:04 +0000 | [diff] [blame] | 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 | |
Łukasz Majewski | d955c6d | 2012-11-13 03:22:09 +0000 | [diff] [blame] | 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)) |
Jaehoon Chung | 505cf47 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 35 | return -ENODEV; |
Łukasz Majewski | d955c6d | 2012-11-13 03:22:09 +0000 | [diff] [blame] | 36 | |
Simon Glass | 78a36c3 | 2014-05-20 06:01:35 -0600 | [diff] [blame] | 37 | if (state == PMIC_CHARGER_DISABLE) { |
Łukasz Majewski | d955c6d | 2012-11-13 03:22:09 +0000 | [diff] [blame] | 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 | |
Jaehoon Chung | 505cf47 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 43 | return -ENOTSUPP; |
Łukasz Majewski | d955c6d | 2012-11-13 03:22:09 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | if (current < CHARGER_MIN_CURRENT || current > CHARGER_MAX_CURRENT) { |
| 47 | printf("%s: Wrong charge current: %d [mA]\n", |
| 48 | __func__, current); |
Jaehoon Chung | 505cf47 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 49 | return -EINVAL; |
Łukasz Majewski | d955c6d | 2012-11-13 03:22:09 +0000 | [diff] [blame] | 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 | |
| 68 | static int pmic_charger_bat_present(struct pmic *p) |
| 69 | { |
| 70 | u32 val; |
| 71 | |
| 72 | if (pmic_probe(p)) |
Jaehoon Chung | 505cf47 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 73 | return -ENODEV; |
Łukasz Majewski | d955c6d | 2012-11-13 03:22:09 +0000 | [diff] [blame] | 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 | |
Łukasz Majewski | c733681 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 85 | int pmic_init(unsigned char bus) |
Łukasz Majewski | 8504296 | 2012-03-29 01:29:17 +0000 | [diff] [blame] | 86 | { |
Łukasz Majewski | 8504296 | 2012-03-29 01:29:17 +0000 | [diff] [blame] | 87 | static const char name[] = "MAX8997_PMIC"; |
Łukasz Majewski | c733681 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 88 | struct pmic *p = pmic_alloc(); |
| 89 | |
| 90 | if (!p) { |
| 91 | printf("%s: POWER allocation error!\n", __func__); |
| 92 | return -ENOMEM; |
| 93 | } |
Łukasz Majewski | 8504296 | 2012-03-29 01:29:17 +0000 | [diff] [blame] | 94 | |
Łukasz Majewski | d955c6d | 2012-11-13 03:22:09 +0000 | [diff] [blame] | 95 | debug("Board PMIC init\n"); |
Łukasz Majewski | 8504296 | 2012-03-29 01:29:17 +0000 | [diff] [blame] | 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; |
Łukasz Majewski | c733681 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 102 | p->bus = bus; |
Łukasz Majewski | 8504296 | 2012-03-29 01:29:17 +0000 | [diff] [blame] | 103 | |
Łukasz Majewski | d955c6d | 2012-11-13 03:22:09 +0000 | [diff] [blame] | 104 | p->chrg = &power_chrg_pmic_ops; |
Łukasz Majewski | 8504296 | 2012-03-29 01:29:17 +0000 | [diff] [blame] | 105 | return 0; |
| 106 | } |