Łukasz Majewski | e2ec82c | 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 | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Łukasz Majewski | e2ec82c | 2012-03-29 01:29:17 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 9 | #include <power/pmic.h> |
| 10 | #include <power/max8997_pmic.h> |
Łukasz Majewski | 72ac794 | 2012-11-13 03:21:54 +0000 | [diff] [blame] | 11 | #include <i2c.h> |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 12 | #include <errno.h> |
Łukasz Majewski | e2ec82c | 2012-03-29 01:29:17 +0000 | [diff] [blame] | 13 | |
Łukasz Majewski | 57e9111 | 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 | 77313a6 | 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)) |
Jaehoon Chung | 10e80f3 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 36 | return -ENODEV; |
Łukasz Majewski | 77313a6 | 2012-11-13 03:22:09 +0000 | [diff] [blame] | 37 | |
Simon Glass | 05400a6 | 2014-05-20 06:01:35 -0600 | [diff] [blame] | 38 | if (state == PMIC_CHARGER_DISABLE) { |
Łukasz Majewski | 77313a6 | 2012-11-13 03:22:09 +0000 | [diff] [blame] | 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 | |
Jaehoon Chung | 10e80f3 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 44 | return -ENOTSUPP; |
Łukasz Majewski | 77313a6 | 2012-11-13 03:22:09 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | if (current < CHARGER_MIN_CURRENT || current > CHARGER_MAX_CURRENT) { |
| 48 | printf("%s: Wrong charge current: %d [mA]\n", |
| 49 | __func__, current); |
Jaehoon Chung | 10e80f3 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 50 | return -EINVAL; |
Łukasz Majewski | 77313a6 | 2012-11-13 03:22:09 +0000 | [diff] [blame] | 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)) |
Jaehoon Chung | 10e80f3 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 74 | return -ENODEV; |
Łukasz Majewski | 77313a6 | 2012-11-13 03:22:09 +0000 | [diff] [blame] | 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 | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 86 | int pmic_init(unsigned char bus) |
Łukasz Majewski | e2ec82c | 2012-03-29 01:29:17 +0000 | [diff] [blame] | 87 | { |
Łukasz Majewski | e2ec82c | 2012-03-29 01:29:17 +0000 | [diff] [blame] | 88 | static const char name[] = "MAX8997_PMIC"; |
Łukasz Majewski | 1c6dba1 | 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 | e2ec82c | 2012-03-29 01:29:17 +0000 | [diff] [blame] | 95 | |
Łukasz Majewski | 77313a6 | 2012-11-13 03:22:09 +0000 | [diff] [blame] | 96 | debug("Board PMIC init\n"); |
Łukasz Majewski | e2ec82c | 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 | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 103 | p->bus = bus; |
Łukasz Majewski | e2ec82c | 2012-03-29 01:29:17 +0000 | [diff] [blame] | 104 | |
Łukasz Majewski | 77313a6 | 2012-11-13 03:22:09 +0000 | [diff] [blame] | 105 | p->chrg = &power_chrg_pmic_ops; |
Łukasz Majewski | e2ec82c | 2012-03-29 01:29:17 +0000 | [diff] [blame] | 106 | return 0; |
| 107 | } |