Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Piotr Wilczek | ca7e574 | 2013-09-24 16:31:22 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 Samsung Electronics |
| 4 | * Piotr Wilczek <p.wilczek@samsung.com> |
Piotr Wilczek | ca7e574 | 2013-09-24 16:31:22 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <power/pmic.h> |
| 9 | #include <power/max77693_pmic.h> |
| 10 | #include <i2c.h> |
| 11 | #include <errno.h> |
| 12 | |
| 13 | static int max77693_charger_state(struct pmic *p, int state, int current) |
| 14 | { |
| 15 | unsigned int val; |
| 16 | |
| 17 | if (pmic_probe(p)) |
Jaehoon Chung | 10e80f3 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 18 | return -ENODEV; |
Piotr Wilczek | ca7e574 | 2013-09-24 16:31:22 +0200 | [diff] [blame] | 19 | |
| 20 | /* unlock write capability */ |
| 21 | val = MAX77693_CHG_UNLOCK; |
| 22 | pmic_reg_write(p, MAX77693_CHG_CNFG_06, val); |
| 23 | |
Simon Glass | 05400a6 | 2014-05-20 06:01:35 -0600 | [diff] [blame] | 24 | if (state == PMIC_CHARGER_DISABLE) { |
Piotr Wilczek | ca7e574 | 2013-09-24 16:31:22 +0200 | [diff] [blame] | 25 | puts("Disable the charger.\n"); |
| 26 | pmic_reg_read(p, MAX77693_CHG_CNFG_00, &val); |
| 27 | val &= ~0x01; |
| 28 | pmic_reg_write(p, MAX77693_CHG_CNFG_00, val); |
Jaehoon Chung | 10e80f3 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 29 | return -ENOTSUPP; |
Piotr Wilczek | ca7e574 | 2013-09-24 16:31:22 +0200 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | if (current < CHARGER_MIN_CURRENT || current > CHARGER_MAX_CURRENT) { |
| 33 | printf("%s: Wrong charge current: %d [mA]\n", |
| 34 | __func__, current); |
Jaehoon Chung | 10e80f3 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 35 | return -EINVAL; |
Piotr Wilczek | ca7e574 | 2013-09-24 16:31:22 +0200 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | /* set charging current */ |
| 39 | pmic_reg_read(p, MAX77693_CHG_CNFG_02, &val); |
| 40 | val &= ~MAX77693_CHG_CC; |
| 41 | val |= current * 10 / 333; /* 0.1A/3 steps */ |
| 42 | pmic_reg_write(p, MAX77693_CHG_CNFG_02, val); |
| 43 | |
| 44 | /* enable charging */ |
| 45 | val = MAX77693_CHG_MODE_ON; |
| 46 | pmic_reg_write(p, MAX77693_CHG_CNFG_00, val); |
| 47 | |
| 48 | /* check charging current */ |
| 49 | pmic_reg_read(p, MAX77693_CHG_CNFG_02, &val); |
| 50 | val &= 0x3f; |
| 51 | printf("Enable the charger @ %d [mA]\n", val * 333 / 10); |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static int max77693_charger_bat_present(struct pmic *p) |
| 57 | { |
| 58 | unsigned int val; |
| 59 | |
| 60 | if (pmic_probe(p)) |
Jaehoon Chung | 10e80f3 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 61 | return -ENODEV; |
Piotr Wilczek | ca7e574 | 2013-09-24 16:31:22 +0200 | [diff] [blame] | 62 | |
| 63 | pmic_reg_read(p, MAX77693_CHG_INT_OK, &val); |
| 64 | |
| 65 | return !(val & MAX77693_CHG_DETBAT); |
| 66 | } |
| 67 | |
| 68 | static struct power_chrg power_chrg_pmic_ops = { |
| 69 | .chrg_bat_present = max77693_charger_bat_present, |
| 70 | .chrg_state = max77693_charger_state, |
| 71 | }; |
| 72 | |
| 73 | int pmic_init_max77693(unsigned char bus) |
| 74 | { |
| 75 | static const char name[] = "MAX77693_PMIC"; |
| 76 | struct pmic *p = pmic_alloc(); |
| 77 | |
| 78 | if (!p) { |
| 79 | printf("%s: POWER allocation error!\n", __func__); |
| 80 | return -ENOMEM; |
| 81 | } |
| 82 | |
| 83 | debug("Board PMIC init\n"); |
| 84 | |
| 85 | p->name = name; |
| 86 | p->interface = PMIC_I2C; |
| 87 | p->number_of_regs = PMIC_NUM_OF_REGS; |
| 88 | p->hw.i2c.addr = MAX77693_PMIC_I2C_ADDR; |
| 89 | p->hw.i2c.tx_num = 1; |
| 90 | p->bus = bus; |
| 91 | |
| 92 | p->chrg = &power_chrg_pmic_ops; |
| 93 | |
| 94 | return 0; |
| 95 | } |