779 lines
21 KiB
C
779 lines
21 KiB
C
/*
|
|
* drivers/power/axp/axp22x/axp22x-charger.c
|
|
* (C) Copyright 2010-2016
|
|
* Allwinner Technology Co., Ltd. <www.allwinnertech.com>
|
|
* Pannan <pannan@allwinnertech.com>
|
|
*
|
|
* charger driver of axp22x
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License as
|
|
* published by the Free Software Foundation; either version 2 of
|
|
* the License, or (at your option) any later version.
|
|
*
|
|
*/
|
|
|
|
#include <linux/kernel.h>
|
|
#include <linux/init.h>
|
|
#include <linux/delay.h>
|
|
#include <linux/device.h>
|
|
#include <linux/module.h>
|
|
#include <linux/platform_device.h>
|
|
#include <linux/workqueue.h>
|
|
#include <linux/types.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/power_supply.h>
|
|
#include "../axp-core.h"
|
|
#include "../axp-charger.h"
|
|
#include "axp22x-charger.h"
|
|
|
|
static int axp22x_get_ac_voltage(struct axp_charger_dev *cdev)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
static int axp22x_get_ac_current(struct axp_charger_dev *cdev)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
static int axp22x_set_ac_vhold(struct axp_charger_dev *cdev, int vol)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
static int axp22x_get_ac_vhold(struct axp_charger_dev *cdev)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
static int axp22x_set_ac_ihold(struct axp_charger_dev *cdev, int cur)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
static int axp22x_get_ac_ihold(struct axp_charger_dev *cdev)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
static struct axp_ac_info axp22x_ac_info = {
|
|
.det_bit = 7,
|
|
.det_offset = AXP22X_CHARGE_STATUS,
|
|
.valid_bit = 6,
|
|
.valid_offset = AXP22X_CHARGE_STATUS,
|
|
.in_short_bit = 1,
|
|
.in_short_offset = AXP22X_CHARGE_STATUS,
|
|
.get_ac_voltage = axp22x_get_ac_voltage,
|
|
.get_ac_current = axp22x_get_ac_current,
|
|
.set_ac_vhold = axp22x_set_ac_vhold,
|
|
.get_ac_vhold = axp22x_get_ac_vhold,
|
|
.set_ac_ihold = axp22x_set_ac_ihold,
|
|
.get_ac_ihold = axp22x_get_ac_ihold,
|
|
};
|
|
|
|
static int axp22x_get_usb_voltage(struct axp_charger_dev *cdev)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
static int axp22x_get_usb_current(struct axp_charger_dev *cdev)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
static int axp22x_set_usb_vhold(struct axp_charger_dev *cdev, int vol)
|
|
{
|
|
u8 tmp;
|
|
struct axp_regmap *map = cdev->chip->regmap;
|
|
|
|
if (vol) {
|
|
axp_regmap_set_bits(map, AXP22X_CHARGE_VBUS, 0x40);
|
|
if (vol >= 4000 && vol <= 4700) {
|
|
tmp = (vol - 4000) / 100;
|
|
axp_regmap_update(map, AXP22X_CHARGE_VBUS,
|
|
tmp << 3, 0x7 << 3);
|
|
} else {
|
|
pr_err("set usb limit voltage error, %d mV\n",
|
|
axp22x_config.pmu_usbpc_vol);
|
|
}
|
|
} else {
|
|
axp_regmap_clr_bits(map, AXP22X_CHARGE_VBUS, 0x40);
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
static int axp22x_get_usb_vhold(struct axp_charger_dev *cdev)
|
|
{
|
|
u8 tmp;
|
|
struct axp_regmap *map = cdev->chip->regmap;
|
|
|
|
axp_regmap_read(map, AXP22X_CHARGE_VBUS, &tmp);
|
|
tmp = (tmp >> 3) & 0x7;
|
|
|
|
return 4000 + tmp * 100;
|
|
}
|
|
|
|
static int axp22x_set_usb_ihold(struct axp_charger_dev *cdev, int cur)
|
|
{
|
|
struct axp_regmap *map = cdev->chip->regmap;
|
|
|
|
if (cur) {
|
|
if (cur == 900)
|
|
axp_regmap_clr_bits(map, AXP22X_CHARGE_VBUS, 0x3);
|
|
else if (cur == 500)
|
|
axp_regmap_update(map, AXP22X_CHARGE_VBUS, 0x1, 0x3);
|
|
} else {
|
|
axp_regmap_set_bits(map, AXP22X_CHARGE_VBUS, 0x3);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int axp22x_get_usb_ihold(struct axp_charger_dev *cdev)
|
|
{
|
|
u8 tmp;
|
|
struct axp_regmap *map = cdev->chip->regmap;
|
|
|
|
axp_regmap_read(map, AXP22X_CHARGE_VBUS, &tmp);
|
|
tmp = tmp & 0x3;
|
|
if (tmp == 0x1)
|
|
return 500;
|
|
else if (tmp == 0)
|
|
return 900;
|
|
else
|
|
return 0;
|
|
}
|
|
|
|
static struct axp_usb_info axp22x_usb_info = {
|
|
.det_bit = 5,
|
|
.det_offset = AXP22X_CHARGE_STATUS,
|
|
.valid_bit = 4,
|
|
.valid_offset = AXP22X_CHARGE_STATUS,
|
|
.get_usb_voltage = axp22x_get_usb_voltage,
|
|
.get_usb_current = axp22x_get_usb_current,
|
|
.set_usb_vhold = axp22x_set_usb_vhold,
|
|
.get_usb_vhold = axp22x_get_usb_vhold,
|
|
.set_usb_ihold = axp22x_set_usb_ihold,
|
|
.get_usb_ihold = axp22x_get_usb_ihold,
|
|
};
|
|
|
|
static int axp22x_get_rest_cap(struct axp_charger_dev *cdev)
|
|
{
|
|
u8 val, temp_val[2], batt_max_cap_val[2];
|
|
int batt_max_cap, coulumb_counter;
|
|
int rest_vol = 0;
|
|
struct axp_regmap *map = cdev->chip->regmap;
|
|
|
|
axp_regmap_read(map, AXP22X_CAP, &val);
|
|
if (val & 0x80)
|
|
rest_vol = (int) (val & 0x7F);
|
|
|
|
axp_regmap_reads(map, AXP22X_COUCNT0, 2, temp_val);
|
|
coulumb_counter = (((temp_val[0] & 0x7f) << 8) + temp_val[1])
|
|
* 1456 / 1000;
|
|
|
|
axp_regmap_reads(map, AXP22X_BATCAP0, 2, temp_val);
|
|
batt_max_cap = (((temp_val[0] & 0x7f) << 8) + temp_val[1])
|
|
* 1456 / 1000;
|
|
|
|
/* Avoid the power stay in 100% for a long time. */
|
|
if (coulumb_counter > batt_max_cap) {
|
|
batt_max_cap_val[0] = temp_val[0] | (0x1<<7);
|
|
batt_max_cap_val[1] = temp_val[1];
|
|
axp_regmap_writes(map, AXP22X_COUCNT0, 2, batt_max_cap_val);
|
|
AXP_DEBUG(AXP_SPLY, cdev->chip->pmu_num,
|
|
"axp22x coulumb_counter = %d\n", batt_max_cap);
|
|
}
|
|
|
|
return rest_vol;
|
|
}
|
|
|
|
static int axp22x_get_bat_health(struct axp_charger_dev *cdev)
|
|
{
|
|
u8 val;
|
|
struct axp_regmap *map = cdev->chip->regmap;
|
|
|
|
axp_regmap_read(map, AXP22X_FAULT_LOG1, &val);
|
|
if (val & AXP22X_FAULT_LOG_BATINACT)
|
|
return POWER_SUPPLY_HEALTH_DEAD;
|
|
else if (val & AXP22X_FAULT_LOG_OVER_TEMP)
|
|
return POWER_SUPPLY_HEALTH_OVERHEAT;
|
|
else if (val & AXP22X_FAULT_LOG_COLD)
|
|
return POWER_SUPPLY_HEALTH_COLD;
|
|
else
|
|
return POWER_SUPPLY_HEALTH_GOOD;
|
|
}
|
|
|
|
static inline int axp22x_vbat_to_mV(u32 reg)
|
|
{
|
|
return ((int)(((reg >> 8) << 4) | (reg & 0x000F))) * 1100 / 1000;
|
|
}
|
|
|
|
static int axp22x_get_vbat(struct axp_charger_dev *cdev)
|
|
{
|
|
u8 tmp[2];
|
|
u32 res;
|
|
struct axp_regmap *map = cdev->chip->regmap;
|
|
|
|
axp_regmap_reads(map, AXP22X_VBATH_RES, 2, tmp);
|
|
res = (tmp[0] << 8) | tmp[1];
|
|
|
|
return axp22x_vbat_to_mV(res);
|
|
}
|
|
|
|
static inline int axp22x_ibat_to_mA(u32 reg)
|
|
{
|
|
return (int)(((reg >> 8) << 4) | (reg & 0x000F));
|
|
}
|
|
|
|
static inline int axp22x_icharge_to_mA(u32 reg)
|
|
{
|
|
return (int)(((reg >> 8) << 4) | (reg & 0x000F));
|
|
}
|
|
|
|
static int axp22x_get_ibat(struct axp_charger_dev *cdev)
|
|
{
|
|
u8 tmp[2];
|
|
u32 res;
|
|
struct axp_regmap *map = cdev->chip->regmap;
|
|
|
|
axp_regmap_reads(map, AXP22X_IBATH_REG, 2, tmp);
|
|
res = (tmp[0] << 8) | tmp[1];
|
|
|
|
return axp22x_icharge_to_mA(res);
|
|
}
|
|
|
|
static int axp22x_get_disibat(struct axp_charger_dev *cdev)
|
|
{
|
|
u8 tmp[2];
|
|
u32 dis_res;
|
|
struct axp_regmap *map = cdev->chip->regmap;
|
|
|
|
axp_regmap_reads(map, AXP22X_DISIBATH_REG, 2, tmp);
|
|
dis_res = (tmp[0] << 8) | tmp[1];
|
|
|
|
return axp22x_ibat_to_mA(dis_res);
|
|
}
|
|
|
|
static int axp22x_set_chg_cur(struct axp_charger_dev *cdev, int cur)
|
|
{
|
|
uint8_t tmp = 0;
|
|
struct axp_regmap *map = cdev->chip->regmap;
|
|
|
|
if (cur == 0)
|
|
axp_regmap_clr_bits(map, AXP22X_CHARGE_CONTROL1, 0x80);
|
|
else
|
|
axp_regmap_set_bits(map, AXP22X_CHARGE_CONTROL1, 0x80);
|
|
|
|
if (cur >= 300 && cur <= 2550) {
|
|
tmp = (cur - 300) / 150;
|
|
axp_regmap_update(map, AXP22X_CHARGE_CONTROL1, tmp, 0x0F);
|
|
} else if (cur < 300) {
|
|
axp_regmap_clr_bits(map, AXP22X_CHARGE_CONTROL1, 0x0F);
|
|
} else {
|
|
axp_regmap_set_bits(map, AXP22X_CHARGE_CONTROL1, 0x0F);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int axp22x_set_chg_vol(struct axp_charger_dev *cdev, int vol)
|
|
{
|
|
uint8_t tmp = 0;
|
|
struct axp_regmap *map = cdev->chip->regmap;
|
|
|
|
#ifdef CONFIG_AW_AXP233
|
|
if (vol < 4200) {
|
|
tmp &= ~(3 << 5);
|
|
} else if (vol < 4240) {
|
|
tmp &= ~(3 << 5);
|
|
tmp |= 1 << 5;
|
|
} else if (vol < 4350) {
|
|
tmp &= ~(3 << 5);
|
|
tmp |= 1 << 6;
|
|
} else {
|
|
tmp |= 3 << 5;
|
|
}
|
|
#else
|
|
if (vol < 4200) {
|
|
tmp &= ~(3 << 5);
|
|
} else if (vol < 4220) {
|
|
tmp &= ~(3 << 5);
|
|
tmp |= 1 << 6;
|
|
} else if (vol < 4240) {
|
|
tmp &= ~(3 << 5);
|
|
tmp |= 1 << 5;
|
|
} else {
|
|
tmp |= 3 << 5;
|
|
}
|
|
#endif
|
|
|
|
axp_regmap_update(map, AXP22X_CHARGE_CONTROL1, tmp, 0x3<<5);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static struct axp_battery_info axp22x_batt_info = {
|
|
.acpresent_bit = 7,
|
|
.vbuspresent_bit = 5,
|
|
.pwrsrc_offset = AXP22X_STATUS,
|
|
.chgstat_bit = 6,
|
|
.chgstat_offset = AXP22X_MODE_CHGSTATUS,
|
|
.det_bit = 5,
|
|
.det_valid_bit = 4,
|
|
.det_valid = 1,
|
|
.det_offset = AXP22X_MODE_CHGSTATUS,
|
|
.cur_direction_bit = 2,
|
|
.cur_direction_offset = AXP22X_CHARGE_STATUS,
|
|
.get_rest_cap = axp22x_get_rest_cap,
|
|
.get_bat_health = axp22x_get_bat_health,
|
|
.get_vbat = axp22x_get_vbat,
|
|
.get_ibat = axp22x_get_ibat,
|
|
.get_disibat = axp22x_get_disibat,
|
|
.set_chg_cur = axp22x_set_chg_cur,
|
|
.set_chg_vol = axp22x_set_chg_vol,
|
|
};
|
|
|
|
static struct power_supply_info battery_data = {
|
|
.name = "PTI PL336078",
|
|
.technology = POWER_SUPPLY_TECHNOLOGY_LiFe,
|
|
.voltage_max_design = 4200000,
|
|
.voltage_min_design = 3500000,
|
|
};
|
|
|
|
static struct axp_supply_info axp22x_spy_info = {
|
|
.ac = &axp22x_ac_info,
|
|
.usb = &axp22x_usb_info,
|
|
.batt = &axp22x_batt_info,
|
|
};
|
|
|
|
static int axp22x_charger_init(struct axp_dev *axp_dev)
|
|
{
|
|
u8 ocv_cap[32];
|
|
u8 val = 0;
|
|
int cur_coulomb_counter, rdc;
|
|
struct axp_regmap *map = axp_dev->regmap;
|
|
int i, update_min_times[8] = {30, 60, 120, 164, 0, 5, 10, 20};
|
|
|
|
if (axp22x_config.pmu_init_chgend_rate == 10)
|
|
val &= ~(1 << 4);
|
|
else
|
|
val |= 1 << 4;
|
|
|
|
val &= 0x7F;
|
|
axp_regmap_write(map, AXP22X_CHARGE_CONTROL1, val);
|
|
|
|
if (axp22x_config.pmu_init_chg_pretime < 30)
|
|
axp22x_config.pmu_init_chg_pretime = 30;
|
|
|
|
if (axp22x_config.pmu_init_chg_csttime < 360)
|
|
axp22x_config.pmu_init_chg_csttime = 360;
|
|
|
|
val = ((((axp22x_config.pmu_init_chg_pretime - 40) / 10) << 6)
|
|
| ((axp22x_config.pmu_init_chg_csttime - 360) / 120));
|
|
axp_regmap_update(map, AXP22X_CHARGE_CONTROL2, val, 0xC2);
|
|
|
|
/* adc set */
|
|
val = AXP22X_ADC_BATVOL_ENABLE | AXP22X_ADC_BATCUR_ENABLE;
|
|
if (axp22x_config.pmu_bat_temp_enable != 0)
|
|
val = val | AXP22X_ADC_TSVOL_ENABLE;
|
|
axp_regmap_update(map, AXP22X_ADC_CONTROL, val,
|
|
AXP22X_ADC_BATVOL_ENABLE
|
|
| AXP22X_ADC_BATCUR_ENABLE
|
|
| AXP22X_ADC_TSVOL_ENABLE);
|
|
|
|
axp_regmap_read(map, AXP22X_ADC_CONTROL3, &val);
|
|
switch (axp22x_config.pmu_init_adc_freq / 100) {
|
|
case 1:
|
|
val &= ~(3 << 6);
|
|
break;
|
|
case 2:
|
|
val &= ~(3 << 6);
|
|
val |= 1 << 6;
|
|
break;
|
|
case 4:
|
|
val &= ~(3 << 6);
|
|
val |= 2 << 6;
|
|
break;
|
|
case 8:
|
|
val |= 3 << 6;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
if (axp22x_config.pmu_bat_temp_enable != 0)
|
|
val &= (~(1 << 2));
|
|
axp_regmap_write(map, AXP22X_ADC_CONTROL3, val);
|
|
|
|
/* bat para */
|
|
axp_regmap_write(map, AXP22X_WARNING_LEVEL,
|
|
((axp22x_config.pmu_battery_warning_level1 - 5) << 4)
|
|
+ axp22x_config.pmu_battery_warning_level2);
|
|
ocv_cap[0] = axp22x_config.pmu_bat_para1;
|
|
ocv_cap[1] = axp22x_config.pmu_bat_para2;
|
|
ocv_cap[2] = axp22x_config.pmu_bat_para3;
|
|
ocv_cap[3] = axp22x_config.pmu_bat_para4;
|
|
ocv_cap[4] = axp22x_config.pmu_bat_para5;
|
|
ocv_cap[5] = axp22x_config.pmu_bat_para6;
|
|
ocv_cap[6] = axp22x_config.pmu_bat_para7;
|
|
ocv_cap[7] = axp22x_config.pmu_bat_para8;
|
|
ocv_cap[8] = axp22x_config.pmu_bat_para9;
|
|
ocv_cap[9] = axp22x_config.pmu_bat_para10;
|
|
ocv_cap[10] = axp22x_config.pmu_bat_para11;
|
|
ocv_cap[11] = axp22x_config.pmu_bat_para12;
|
|
ocv_cap[12] = axp22x_config.pmu_bat_para13;
|
|
ocv_cap[13] = axp22x_config.pmu_bat_para14;
|
|
ocv_cap[14] = axp22x_config.pmu_bat_para15;
|
|
ocv_cap[15] = axp22x_config.pmu_bat_para16;
|
|
ocv_cap[16] = axp22x_config.pmu_bat_para17;
|
|
ocv_cap[17] = axp22x_config.pmu_bat_para18;
|
|
ocv_cap[18] = axp22x_config.pmu_bat_para19;
|
|
ocv_cap[19] = axp22x_config.pmu_bat_para20;
|
|
ocv_cap[20] = axp22x_config.pmu_bat_para21;
|
|
ocv_cap[21] = axp22x_config.pmu_bat_para22;
|
|
ocv_cap[22] = axp22x_config.pmu_bat_para23;
|
|
ocv_cap[23] = axp22x_config.pmu_bat_para24;
|
|
ocv_cap[24] = axp22x_config.pmu_bat_para25;
|
|
ocv_cap[25] = axp22x_config.pmu_bat_para26;
|
|
ocv_cap[26] = axp22x_config.pmu_bat_para27;
|
|
ocv_cap[27] = axp22x_config.pmu_bat_para28;
|
|
ocv_cap[28] = axp22x_config.pmu_bat_para29;
|
|
ocv_cap[29] = axp22x_config.pmu_bat_para30;
|
|
ocv_cap[30] = axp22x_config.pmu_bat_para31;
|
|
ocv_cap[31] = axp22x_config.pmu_bat_para32;
|
|
axp_regmap_writes(map, AXP22X_OCV_BASE, 32, ocv_cap);
|
|
|
|
/*Init 16's Reset PMU en */
|
|
if (axp22x_config.pmu_reset)
|
|
axp_regmap_set_bits(map, AXP22X_HOTOVER_CTL, 0x08);
|
|
else
|
|
axp_regmap_clr_bits(map, AXP22X_HOTOVER_CTL, 0x08);
|
|
|
|
/*Init IRQ wakeup en*/
|
|
if (axp22x_config.pmu_irq_wakeup)
|
|
axp_regmap_set_bits(map, AXP22X_HOTOVER_CTL, 0x80);
|
|
else
|
|
axp_regmap_clr_bits(map, AXP22X_HOTOVER_CTL, 0x80);
|
|
|
|
/*Init N_VBUSEN status*/
|
|
if (axp22x_config.pmu_vbusen_func)
|
|
axp_regmap_set_bits(map, AXP22X_HOTOVER_CTL, 0x10);
|
|
else
|
|
axp_regmap_clr_bits(map, AXP22X_HOTOVER_CTL, 0x10);
|
|
|
|
/*Init InShort status*/
|
|
if (axp22x_config.pmu_inshort)
|
|
axp_regmap_set_bits(map, AXP22X_HOTOVER_CTL, 0x60);
|
|
else
|
|
axp_regmap_clr_bits(map, AXP22X_HOTOVER_CTL, 0x60);
|
|
|
|
/*Init CHGLED function*/
|
|
if (axp22x_config.pmu_chgled_func)
|
|
axp_regmap_set_bits(map, AXP22X_OFF_CTL, 0x08);
|
|
else
|
|
axp_regmap_clr_bits(map, AXP22X_OFF_CTL, 0x08);
|
|
|
|
/*set CHGLED Indication Type*/
|
|
if (axp22x_config.pmu_chgled_type)
|
|
axp_regmap_set_bits(map, AXP22X_CHARGE2, 0x10);
|
|
else
|
|
axp_regmap_clr_bits(map, AXP22X_CHARGE2, 0x10);
|
|
|
|
/*Init PMU Over Temperature protection*/
|
|
if (axp22x_config.pmu_hot_shutdown)
|
|
axp_regmap_set_bits(map, AXP22X_HOTOVER_CTL, 0x04);
|
|
else
|
|
axp_regmap_clr_bits(map, AXP22X_HOTOVER_CTL, 0x04);
|
|
|
|
/*Init battery capacity correct function*/
|
|
if (axp22x_config.pmu_batt_cap_correct)
|
|
axp_regmap_set_bits(map, AXP22X_COULOMB_CTL, 0x20);
|
|
else
|
|
axp_regmap_clr_bits(map, AXP22X_COULOMB_CTL, 0x20);
|
|
|
|
/* Init battery regulator enable or not when charge finish*/
|
|
if (axp22x_config.pmu_chg_end_on_en)
|
|
axp_regmap_set_bits(map, AXP22X_CHARGE2, 0x20);
|
|
else
|
|
axp_regmap_clr_bits(map, AXP22X_CHARGE2, 0x20);
|
|
|
|
if (!axp22x_config.pmu_batdeten)
|
|
axp_regmap_clr_bits(map, AXP22X_PDBC, 0x40);
|
|
else
|
|
axp_regmap_set_bits(map, AXP22X_PDBC, 0x40);
|
|
|
|
/* RDC initial */
|
|
axp_regmap_read(map, AXP22X_RDC0, &val);
|
|
if ((axp22x_config.pmu_battery_rdc) && (!(val & 0x40))) {
|
|
rdc = (axp22x_config.pmu_battery_rdc * 10000 + 5371) / 10742;
|
|
axp_regmap_write(map, AXP22X_RDC0, ((rdc >> 8) & 0x1F)|0x80);
|
|
axp_regmap_write(map, AXP22X_RDC1, rdc & 0x00FF);
|
|
}
|
|
|
|
axp_regmap_read(map, AXP22X_BATCAP0, &val);
|
|
if ((axp22x_config.pmu_battery_cap) && (!(val & 0x80))) {
|
|
cur_coulomb_counter = axp22x_config.pmu_battery_cap
|
|
* 1000 / 1456;
|
|
axp_regmap_write(map, AXP22X_BATCAP0,
|
|
((cur_coulomb_counter >> 8) | 0x80));
|
|
axp_regmap_write(map, AXP22X_BATCAP1,
|
|
cur_coulomb_counter & 0x00FF);
|
|
} else if (!axp22x_config.pmu_battery_cap) {
|
|
axp_regmap_write(map, AXP22X_BATCAP0, 0x00);
|
|
axp_regmap_write(map, AXP22X_BATCAP1, 0x00);
|
|
}
|
|
|
|
if (axp22x_config.pmu_bat_unused == 1)
|
|
axp22x_spy_info.batt->det_unused = 1;
|
|
else
|
|
axp22x_spy_info.batt->det_unused = 0;
|
|
|
|
if (axp22x_config.pmu_ocv_en == 0) {
|
|
pr_warn("axp22x ocv must be enabled\n");
|
|
axp22x_config.pmu_ocv_en = 1;
|
|
}
|
|
|
|
if (axp22x_config.pmu_cou_en == 1) {
|
|
/* use ocv and cou */
|
|
axp_regmap_set_bits(map, AXP22X_COULOMB_CTL, 0x80);
|
|
axp_regmap_set_bits(map, AXP22X_COULOMB_CTL, 0x40);
|
|
} else if (axp22x_config.pmu_cou_en == 0) {
|
|
/* only use ocv */
|
|
axp_regmap_set_bits(map, AXP22X_COULOMB_CTL, 0x80);
|
|
axp_regmap_clr_bits(map, AXP22X_COULOMB_CTL, 0x40);
|
|
}
|
|
|
|
for (i = 0; i < ARRAY_SIZE(update_min_times); i++) {
|
|
if (update_min_times[i] == axp22x_config.pmu_update_min_time)
|
|
break;
|
|
}
|
|
axp_regmap_update(map, AXP22X_ADJUST_PARA, i, 0x7);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static struct axp_interrupts axp22x_charger_irq[] = {
|
|
{"usb in", axp_usb_in_isr},
|
|
{"usb out", axp_usb_out_isr},
|
|
{"ac in", axp_ac_in_isr},
|
|
{"ac out", axp_ac_out_isr},
|
|
{"bat in", axp_capchange_isr},
|
|
{"bat out", axp_capchange_isr},
|
|
#ifdef CONFIG_AW_AXP233
|
|
{"bat QWtemplow", axp_change_isr},
|
|
{"bat Wtemplow", axp_change_isr},
|
|
{"bat QWtempover", axp_change_isr},
|
|
{"bat Wtempover", axp_change_isr},
|
|
{"bat QCtemplow", axp_change_isr},
|
|
{"bat Ctemplow", axp_change_isr},
|
|
{"bat QCtempover", axp_change_isr},
|
|
{"bat Ctempover", axp_change_isr},
|
|
#else
|
|
{"bat templow", axp_change_isr},
|
|
{"bat tempover", axp_change_isr},
|
|
#endif
|
|
{"charging", axp_change_isr},
|
|
{"charge over", axp_change_isr},
|
|
{"low warning1", axp_low_warning1_isr},
|
|
{"low warning2", axp_low_warning2_isr},
|
|
};
|
|
|
|
static void axp22x_private_debug(struct axp_charger_dev *cdev)
|
|
{
|
|
u8 tmp[2];
|
|
struct axp_regmap *map = cdev->chip->regmap;
|
|
|
|
axp_regmap_reads(map, AXP22X_OCVBATH_RES, 2, tmp);
|
|
AXP_DEBUG(AXP_SPLY, cdev->chip->pmu_num,
|
|
"ocv_vol = %d\n", ((tmp[0] << 4) | (tmp[1] & 0xF))
|
|
* 1100 / 1000);
|
|
|
|
axp_regmap_read(map, AXP22X_OCV_PERCENT, &tmp[0]);
|
|
if (tmp[0] & 0x80)
|
|
AXP_DEBUG(AXP_SPLY, cdev->chip->pmu_num,
|
|
"ocv_percent = %d\n", tmp[0] & 0x7f);
|
|
|
|
axp_regmap_read(map, AXP22X_COU_PERCENT, &tmp[0]);
|
|
if (tmp[0] & 0x80)
|
|
AXP_DEBUG(AXP_SPLY, cdev->chip->pmu_num,
|
|
"coulomb_percent = %d\n", tmp[0] & 0x7f);
|
|
}
|
|
|
|
static int axp22x_charger_probe(struct platform_device *pdev)
|
|
{
|
|
int ret, i, irq;
|
|
struct axp_charger_dev *chg_dev;
|
|
struct axp_dev *axp_dev = dev_get_drvdata(pdev->dev.parent);
|
|
|
|
if (pdev->dev.of_node) {
|
|
/* get dt and sysconfig */
|
|
ret = axp_charger_dt_parse(pdev->dev.of_node, &axp22x_config);
|
|
if (ret) {
|
|
pr_err("%s parse device tree err\n", __func__);
|
|
return -EINVAL;
|
|
}
|
|
} else {
|
|
pr_err("axp22x charger device tree err!\n");
|
|
return -EBUSY;
|
|
}
|
|
|
|
axp22x_ac_info.ac_vol = axp22x_config.pmu_ac_vol;
|
|
axp22x_ac_info.ac_cur = axp22x_config.pmu_ac_cur;
|
|
axp22x_usb_info.usb_pc_vol = axp22x_config.pmu_usbpc_vol;
|
|
axp22x_usb_info.usb_pc_cur = axp22x_config.pmu_usbpc_cur;
|
|
axp22x_usb_info.usb_ad_vol = axp22x_config.pmu_ac_vol;
|
|
axp22x_usb_info.usb_ad_cur = axp22x_config.pmu_ac_cur;
|
|
axp22x_batt_info.runtime_chgcur = axp22x_config.pmu_runtime_chgcur;
|
|
axp22x_batt_info.suspend_chgcur = axp22x_config.pmu_suspend_chgcur;
|
|
axp22x_batt_info.shutdown_chgcur = axp22x_config.pmu_shutdown_chgcur;
|
|
battery_data.voltage_max_design = axp22x_config.pmu_init_chgvol
|
|
* 1000;
|
|
battery_data.voltage_min_design = axp22x_config.pmu_pwroff_vol
|
|
* 1000;
|
|
battery_data.energy_full_design = axp22x_config.pmu_battery_cap;
|
|
|
|
axp22x_charger_init(axp_dev);
|
|
|
|
chg_dev = axp_power_supply_register(&pdev->dev, axp_dev,
|
|
&battery_data, &axp22x_spy_info);
|
|
if (IS_ERR_OR_NULL(chg_dev))
|
|
goto fail;
|
|
chg_dev->private_debug = axp22x_private_debug;
|
|
chg_dev->pmic_temp_offset = AXP22X_INTTEMP;
|
|
chg_dev->spy_info->batt->bat_temp_offset = AXP22X_VTS_RES;
|
|
|
|
for (i = 0; i < ARRAY_SIZE(axp22x_charger_irq); i++) {
|
|
irq = platform_get_irq_byname(pdev, axp22x_charger_irq[i].name);
|
|
if (irq < 0)
|
|
continue;
|
|
|
|
ret = axp_request_irq(axp_dev, irq,
|
|
axp22x_charger_irq[i].isr, chg_dev);
|
|
if (ret != 0) {
|
|
dev_err(&pdev->dev, "failed to request %s IRQ %d: %d\n",
|
|
axp22x_charger_irq[i].name, irq, ret);
|
|
goto out_irq;
|
|
}
|
|
|
|
dev_dbg(&pdev->dev, "Requested %s IRQ %d: %d\n",
|
|
axp22x_charger_irq[i].name, irq, ret);
|
|
}
|
|
|
|
platform_set_drvdata(pdev, chg_dev);
|
|
|
|
return 0;
|
|
|
|
out_irq:
|
|
for (i = i - 1; i >= 0; i--) {
|
|
irq = platform_get_irq_byname(pdev, axp22x_charger_irq[i].name);
|
|
if (irq < 0)
|
|
continue;
|
|
axp_free_irq(axp_dev, irq);
|
|
}
|
|
fail:
|
|
return -1;
|
|
}
|
|
|
|
static int axp22x_charger_remove(struct platform_device *pdev)
|
|
{
|
|
int i, irq;
|
|
struct axp_charger_dev *chg_dev = platform_get_drvdata(pdev);
|
|
struct axp_dev *axp_dev = dev_get_drvdata(pdev->dev.parent);
|
|
|
|
for (i = 0; i < ARRAY_SIZE(axp22x_charger_irq); i++) {
|
|
irq = platform_get_irq_byname(pdev, axp22x_charger_irq[i].name);
|
|
if (irq < 0)
|
|
continue;
|
|
axp_free_irq(axp_dev, irq);
|
|
}
|
|
|
|
axp_power_supply_unregister(chg_dev);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int axp22x_charger_suspend(struct platform_device *dev,
|
|
pm_message_t state)
|
|
{
|
|
struct axp_charger_dev *chg_dev = platform_get_drvdata(dev);
|
|
|
|
axp_suspend_flag = AXP_WAS_SUSPEND;
|
|
axp_charger_suspend(chg_dev);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int axp22x_charger_resume(struct platform_device *dev)
|
|
{
|
|
struct axp_charger_dev *chg_dev = platform_get_drvdata(dev);
|
|
struct axp_regmap *map = chg_dev->chip->regmap;
|
|
int pre_rest_vol;
|
|
|
|
if (axp_suspend_flag == AXP_SUSPEND_WITH_IRQ) {
|
|
axp_suspend_flag = AXP_NOT_SUSPEND;
|
|
sunxi_nmi_enable();
|
|
} else {
|
|
axp_suspend_flag = AXP_NOT_SUSPEND;
|
|
}
|
|
|
|
pre_rest_vol = chg_dev->rest_vol;
|
|
axp_charger_resume(chg_dev);
|
|
|
|
if (chg_dev->rest_vol - pre_rest_vol) {
|
|
pr_info("battery vol change: %d->%d\n",
|
|
pre_rest_vol, chg_dev->rest_vol);
|
|
axp_regmap_write(map, AXP22X_DATA_BUFFER1,
|
|
chg_dev->rest_vol | 0x80);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void axp22x_charger_shutdown(struct platform_device *dev)
|
|
{
|
|
struct axp_charger_dev *chg_dev = platform_get_drvdata(dev);
|
|
axp_charger_shutdown(chg_dev);
|
|
}
|
|
|
|
static const struct of_device_id axp22x_charger_dt_ids[] = {
|
|
{ .compatible = "axp221s-charger", },
|
|
{ .compatible = "axp227-charger", },
|
|
{ .compatible = "axp233-charger", },
|
|
{},
|
|
};
|
|
MODULE_DEVICE_TABLE(of, axp22x_charger_dt_ids);
|
|
|
|
static struct platform_driver axp22x_charger_driver = {
|
|
.driver = {
|
|
.name = "axp22x-charger",
|
|
.of_match_table = axp22x_charger_dt_ids,
|
|
},
|
|
.probe = axp22x_charger_probe,
|
|
.remove = axp22x_charger_remove,
|
|
.suspend = axp22x_charger_suspend,
|
|
.resume = axp22x_charger_resume,
|
|
.shutdown = axp22x_charger_shutdown,
|
|
};
|
|
|
|
static int __init axp22x_charger_initcall(void)
|
|
{
|
|
int ret;
|
|
|
|
ret = platform_driver_register(&axp22x_charger_driver);
|
|
if (IS_ERR_VALUE(ret)) {
|
|
pr_err("%s: failed, errno %d\n", __func__, ret);
|
|
return -EINVAL;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
fs_initcall_sync(axp22x_charger_initcall);
|
|
|
|
MODULE_DESCRIPTION("Charger driver of axp22x");
|
|
MODULE_AUTHOR("pannan");
|
|
MODULE_LICENSE("GPL");
|