| /* |
| * Copyright (C) 2014 Marvell International Ltd. |
| * Fenghang Yin <yinfh@marvell.com> |
| * |
| * SPDX-License-Identifier: GPL-2.0+ |
| */ |
| |
| #include <common.h> |
| #include <power/pmic.h> |
| #include <power/eta6005.h> |
| #include <i2c.h> |
| #include <errno.h> |
| #include <power/power_chrg.h> |
| #include <power/battery.h> |
| |
| /*depend on board design */ |
| #define PM803_BAT_PRESENT_UP_THRESH (1250) |
| #define PM803_BAT_PRESENT_DOWN_TRESH (150) |
| |
| #define PM802_BAT_PRESENT_UP_THRESH (1250) |
| #define PM802_BAT_PRESENT_DOWN_TRESH (50) |
| |
| |
| __weak int get_usb_charger_type(void) {return NULL_CHARGER;} |
| |
| static int eta6005_charger_type(struct pmic *p) |
| { |
| int charger = get_usb_charger_type(); |
| |
| debug("%s: charger=%d\n", __func__, charger); |
| return charger; |
| } |
| |
| int eta6005_charger_full(void) |
| { |
| int full = 0; |
| |
| full = gpio_get_value(CHARGER_STATUS_GPIO); |
| return full; |
| } |
| |
| static int eta6005_charger_state(struct pmic *p, int state, int current) |
| { |
| if (state == CHARGER_ENABLE) |
| gpio_set_value(CHARGER_EN_GPIO, 0); |
| else |
| gpio_set_value(CHARGER_EN_GPIO, 1); |
| |
| return 0; |
| } |
| |
| static int eta6005_charger_bat_present(struct pmic *p) |
| { |
| int volt; |
| struct pmic *p_fg; |
| |
| if (p->parent && p->parent->pbat && p->parent->pbat->fg) { |
| p_fg = p->parent->pbat->fg; |
| } else { |
| printf("%s: can't get pmic_fg\n", __func__); |
| return 0; |
| } |
| #ifdef CONFIG_FG_PM803 |
| if (pmic_is_pm803()) { |
| volt = pmic_get_gpadc_bias_volt(p_fg, BAT_NTC_GPADC, BAT_PRESENT_CURRENT_SOC); |
| } else { |
| volt = pmic_get_gpadc_bias_volt(p_fg, BAT_NTC_GPADC, BAT_PRESENT_CURRENT); |
| } |
| #else |
| volt = pmic_get_gpadc_bias_volt(p_fg, BAT_NTC_GPADC, BAT_PRESENT_CURRENT); |
| #endif |
| printf("%s: bias mvolt = %dmV\n", __func__, volt); |
| |
| #ifdef CONFIG_FG_PM803 |
| if (pmic_is_pm803() && (volt <= PM803_BAT_PRESENT_UP_THRESH) |
| && (volt >= PM803_BAT_PRESENT_DOWN_TRESH)) { |
| return 1; |
| } else if ((volt <= PM802_BAT_PRESENT_UP_THRESH) |
| && (volt >= PM802_BAT_PRESENT_DOWN_TRESH)) { |
| return 1; |
| } else { |
| return 0; |
| } |
| #else |
| if ((volt <= PM802_BAT_PRESENT_UP_THRESH) |
| && (volt >= PM802_BAT_PRESENT_DOWN_TRESH)) { |
| return 1; |
| } else { |
| return 0; |
| } |
| #endif |
| } |
| |
| static struct power_chrg power_chrg_ops = { |
| .chrg_type = eta6005_charger_type, |
| .chrg_bat_present = eta6005_charger_bat_present, |
| .chrg_state = eta6005_charger_state, |
| }; |
| |
| int power_chrg_init(unsigned char bus) |
| { |
| static const char name[] = MARVELL_PMIC_CHARGE; |
| struct pmic *p = pmic_alloc(); |
| unsigned int buff; |
| int ret; |
| |
| if (!p) { |
| printf("%s: POWER allocation error!\n", __func__); |
| return -ENOMEM; |
| } |
| |
| printf("Charger ETA6005 init\n"); |
| gpio_direction_output(CHARGER_EN_GPIO, 0); |
| p->name = name; |
| p->interface = PMIC_I2C; |
| p->number_of_regs = 0; |
| p->hw.i2c.addr = 0; |
| p->hw.i2c.tx_num = 1; |
| p->bus = bus; |
| p->chrg = &power_chrg_ops; |
| |
| return 0; |
| } |