/* * drivers/power/axp/axp22x/axp22x-charger.c * (C) Copyright 2010-2017 * Allwinner Technology Co., Ltd. * * 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 #include #include #include #include #include #include #include #include #include #include #include "../axp-core.h" #include "../axp-charger.h" #include "axp259-charger.h" static int axp259_get_ac_voltage(struct axp_charger_dev *cdev) { return 0; } static int axp259_get_ac_current(struct axp_charger_dev *cdev) { return 0; } static int axp259_set_ac_vhold(struct axp_charger_dev *cdev, int vol) { return 0; } static int axp259_get_ac_vhold(struct axp_charger_dev *cdev) { return 0; } static int axp259_set_ac_ihold(struct axp_charger_dev *cdev, int cur) { return 0; } static int axp259_get_ac_ihold(struct axp_charger_dev *cdev) { return 0; } static struct axp_ac_info axp259_ac_info = { .det_bit = 0, .det_offset = AXP259_CHARGE_STATUS, .get_ac_voltage = axp259_get_ac_voltage, .get_ac_current = axp259_get_ac_current, .set_ac_vhold = axp259_set_ac_vhold, .get_ac_vhold = axp259_get_ac_vhold, .set_ac_ihold = axp259_set_ac_ihold, .get_ac_ihold = axp259_get_ac_ihold, }; static int axp259_get_usb_voltage(struct axp_charger_dev *cdev) { return 0; } static int axp259_get_usb_current(struct axp_charger_dev *cdev) { return 0; } static int axp259_set_usb_vhold(struct axp_charger_dev *cdev, int vol) { return 0; } static int axp259_get_usb_vhold(struct axp_charger_dev *cdev) { return 0; } static int axp259_set_usb_ihold(struct axp_charger_dev *cdev, int cur) { return 0; } static int axp259_get_usb_ihold(struct axp_charger_dev *cdev) { return 0; } static struct axp_usb_info axp259_usb_info = { .get_usb_voltage = axp259_get_usb_voltage, .get_usb_current = axp259_get_usb_current, .set_usb_vhold = axp259_set_usb_vhold, .get_usb_vhold = axp259_get_usb_vhold, .set_usb_ihold = axp259_set_usb_ihold, .get_usb_ihold = axp259_get_usb_ihold, }; static int axp259_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; struct axp_regmap *map = cdev->chip->regmap; axp_regmap_read(map, AXP259_CAP, &val); if (!(val & 0x80)) return 0; rest_vol = (int) (val & 0x7F); axp_regmap_reads(map, 0xe2, 2, temp_val); coulumb_counter = (((temp_val[0] & 0x7f) << 8) + temp_val[1]) * 1456 / 1000; axp_regmap_reads(map, 0xe0, 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, 0xe2, 2, batt_max_cap_val); AXP_DEBUG(AXP_SPLY, cdev->chip->pmu_num, "Axp259 coulumb_counter = %d\n", batt_max_cap); } return rest_vol; } static int axp259_get_bat_health(struct axp_charger_dev *cdev) { return POWER_SUPPLY_HEALTH_GOOD; } static inline int axp259_vbat_to_mV(u32 reg) { return ((int)(((reg >> 8) << 4) | (reg & 0x000F))) * 1200 / 1000; } static int axp259_get_vbat(struct axp_charger_dev *cdev) { u8 tmp[2]; u32 res; struct axp_regmap *map = cdev->chip->regmap; axp_regmap_reads(map, AXP259_VBATH_RES, 2, tmp); res = (tmp[0] << 8) | tmp[1]; return axp259_vbat_to_mV(res); } static inline int axp259_ibat_to_mA(u32 reg) { return (int)(((reg >> 8) << 4) | (reg & 0x000F)); } static inline int axp259_icharge_to_mA(u32 reg) { return (int)(((reg >> 8) << 4) | (reg & 0x000F)); } static int axp259_get_ibat(struct axp_charger_dev *cdev) { u8 tmp[2]; u32 res; struct axp_regmap *map = cdev->chip->regmap; axp_regmap_reads(map, AXP259_IBATH_REG, 2, tmp); res = (tmp[0] << 8) | tmp[1]; return axp259_icharge_to_mA(res); } static int axp259_get_disibat(struct axp_charger_dev *cdev) { u8 tmp[2]; u32 dis_res; struct axp_regmap *map = cdev->chip->regmap; axp_regmap_reads(map, AXP259_DISIBATH_REG, 2, tmp); dis_res = (tmp[0] << 8) | tmp[1]; return axp259_ibat_to_mA(dis_res); } static int axp259_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, AXP259_CHARGE_CONTROL1, 0x80); else axp_regmap_set_bits(map, AXP259_CHARGE_CONTROL1, 0x80); if (cur >= 400 && cur <= 3400) { tmp = (cur - 400) / 200; axp_regmap_update(map, AXP259_CHARGE_CONTROL2, tmp, 0x1F); } else if (cur < 400) { axp_regmap_clr_bits(map, AXP259_CHARGE_CONTROL2, 0x1F); } else { axp_regmap_set_bits(map, AXP259_CHARGE_CONTROL2, 0x1F); } return 0; } static int axp259_set_chg_vol(struct axp_charger_dev *cdev, int vol) { uint8_t tmp = 0; struct axp_regmap *map = cdev->chip->regmap; if (vol < 4100) { axp_regmap_clr_bits(map, AXP259_CHARGE_CONTROL2, 0xe0); } else if (vol >= 4100 && vol <= 4250) { tmp = (vol - 4100) / 50; tmp <<= 5; axp_regmap_update(map, AXP259_CHARGE_CONTROL2, tmp, 0xe0); } else if (vol >= 4350 && vol <= 4500) { tmp = (vol - 4350) / 50; tmp |= (0x1 << 2); tmp <<= 5; axp_regmap_update(map, AXP259_CHARGE_CONTROL2, tmp, 0xe0); } else if (vol > 4500) { axp_regmap_set_bits(map, AXP259_CHARGE_CONTROL2, 0xe0); } else { pr_warn("unsupported voltage: %dmv, use default 4200mv\n", vol); axp_regmap_update(map, AXP259_CHARGE_CONTROL2, 0x2 << 5, 0xe0); } return 0; } static struct axp_battery_info axp259_batt_info = { .chgstat_bit = 6, .chgstat_offset = AXP259_CHARGE_STATUS, .det_bit = 4, .det_offset = AXP259_CHARGE_STATUS, .det_valid = 0, .cur_direction_bit = 5, .cur_direction_offset = AXP259_CHARGE_STATUS, .get_rest_cap = axp259_get_rest_cap, .get_bat_health = axp259_get_bat_health, .get_vbat = axp259_get_vbat, .get_ibat = axp259_get_ibat, .get_disibat = axp259_get_disibat, .set_chg_cur = axp259_set_chg_cur, .set_chg_vol = axp259_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, .use_for_apm = 1, }; static struct axp_supply_info axp259_spy_info = { .ac = &axp259_ac_info, .usb = &axp259_usb_info, .batt = &axp259_batt_info, }; static int axp259_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; if (axp259_config.pmu_init_chgend_rate == 10) val = 0; else if (axp259_config.pmu_init_chgend_rate == 20) val = 1; else if (axp259_config.pmu_init_chgend_rate == 100) val = 2; else if (axp259_config.pmu_init_chgend_rate == 200) val = 3; else if (axp259_config.pmu_init_chgend_rate == 300) val = 4; else if (axp259_config.pmu_init_chgend_rate == 400) val = 5; else if (axp259_config.pmu_init_chgend_rate == 500) val = 6; else if (axp259_config.pmu_init_chgend_rate == 600) val = 7; val <<= 4; axp_regmap_update(map, AXP259_CHARGE_CONTROL1, val, 0x70); if (axp259_config.pmu_init_chg_pretime < 40) axp259_config.pmu_init_chg_pretime = 40; if (axp259_config.pmu_init_chg_csttime < 360) axp259_config.pmu_init_chg_csttime = 360; val = ((((axp259_config.pmu_init_chg_pretime - 40) / 10) << 2) | ((axp259_config.pmu_init_chg_csttime - 360) / 120)); axp_regmap_update(map, AXP259_CHARGE_CONTROL1, val, 0x0f); /* adc set */ val = AXP259_ADC_BATVOL_ENABLE | AXP259_ADC_BATCUR_ENABLE; if (axp259_config.pmu_bat_temp_enable != 0) val = val | AXP259_ADC_TSVOL_ENABLE; axp_regmap_update(map, AXP259_ADC_CONTROL, val, AXP259_ADC_BATVOL_ENABLE | AXP259_ADC_BATCUR_ENABLE | AXP259_ADC_TSVOL_ENABLE); axp_regmap_read(map, AXP259_TS_PIN_CONTROL, &val); switch (axp259_config.pmu_init_adc_freq / 100) { case 1: val &= ~(3 << 3); break; case 2: val &= ~(3 << 3); val |= 1 << 3; break; case 4: val &= ~(3 << 3); val |= 2 << 3; break; case 8: val |= 3 << 3; break; default: break; } if (axp259_config.pmu_bat_temp_enable != 0) val &= (~(1 << 2)); axp_regmap_write(map, AXP259_TS_PIN_CONTROL, val); /* bat para */ axp_regmap_write(map, AXP259_WARNING_LEVEL, ((axp259_config.pmu_battery_warning_level1 - 5) << 4) + axp259_config.pmu_battery_warning_level2); /* set target voltage */ if ((axp259_config.pmu_init_chgvol >= 4100) && (axp259_config.pmu_init_chgvol <= 4250)) val = (axp259_config.pmu_init_chgvol - 4100) / 50; else if ((axp259_config.pmu_init_chgvol >= 4350) && (axp259_config.pmu_init_chgvol <= 4500)) val = (axp259_config.pmu_init_chgvol - 4150) / 50; else val = 2; val <<= 5; axp_regmap_update(map, AXP259_CHARGE_CONTROL2, val, 0xe0); ocv_cap[0] = axp259_config.pmu_bat_para1; ocv_cap[1] = axp259_config.pmu_bat_para2; ocv_cap[2] = axp259_config.pmu_bat_para3; ocv_cap[3] = axp259_config.pmu_bat_para4; ocv_cap[4] = axp259_config.pmu_bat_para5; ocv_cap[5] = axp259_config.pmu_bat_para6; ocv_cap[6] = axp259_config.pmu_bat_para7; ocv_cap[7] = axp259_config.pmu_bat_para8; ocv_cap[8] = axp259_config.pmu_bat_para9; ocv_cap[9] = axp259_config.pmu_bat_para10; ocv_cap[10] = axp259_config.pmu_bat_para11; ocv_cap[11] = axp259_config.pmu_bat_para12; ocv_cap[12] = axp259_config.pmu_bat_para13; ocv_cap[13] = axp259_config.pmu_bat_para14; ocv_cap[14] = axp259_config.pmu_bat_para15; ocv_cap[15] = axp259_config.pmu_bat_para16; ocv_cap[16] = axp259_config.pmu_bat_para17; ocv_cap[17] = axp259_config.pmu_bat_para18; ocv_cap[18] = axp259_config.pmu_bat_para19; ocv_cap[19] = axp259_config.pmu_bat_para20; ocv_cap[20] = axp259_config.pmu_bat_para21; ocv_cap[21] = axp259_config.pmu_bat_para22; ocv_cap[22] = axp259_config.pmu_bat_para23; ocv_cap[23] = axp259_config.pmu_bat_para24; ocv_cap[24] = axp259_config.pmu_bat_para25; ocv_cap[25] = axp259_config.pmu_bat_para26; ocv_cap[26] = axp259_config.pmu_bat_para27; ocv_cap[27] = axp259_config.pmu_bat_para28; ocv_cap[28] = axp259_config.pmu_bat_para29; ocv_cap[29] = axp259_config.pmu_bat_para30; ocv_cap[30] = axp259_config.pmu_bat_para31; ocv_cap[31] = axp259_config.pmu_bat_para32; axp_regmap_writes(map, 0xC0, 32, ocv_cap); /*Init CHGLED function*/ if (axp259_config.pmu_chgled_func) axp_regmap_set_bits(map, 0x90, 0x02); /* control by charger */ else axp_regmap_set_bits(map, 0x90, 0x07); /* drive MOTO */ /*set CHGLED Indication Type*/ if (axp259_config.pmu_chgled_type) axp_regmap_set_bits(map, 0x90, 0x01); /* Type B */ else axp_regmap_clr_bits(map, 0x90, 0x07); /* Type A */ /*Init battery capacity correct function*/ if (axp259_config.pmu_batt_cap_correct) axp_regmap_set_bits(map, 0xb8, 0x20); /* enable */ else axp_regmap_clr_bits(map, 0xb8, 0x20); /* disable */ /*battery detect enable*/ if (axp259_config.pmu_batdeten) axp_regmap_set_bits(map, 0x22, 0x80); else axp_regmap_clr_bits(map, 0x22, 0x80); /* RDC initial */ axp_regmap_read(map, AXP259_RDC0, &val); if ((axp259_config.pmu_battery_rdc) && (!(val & 0x40))) { rdc = (axp259_config.pmu_battery_rdc * 10000 + 5371) / 10742; axp_regmap_write(map, AXP259_RDC0, ((rdc >> 8) & 0x1F)|0x80); axp_regmap_write(map, AXP259_RDC1, rdc & 0x00FF); } axp_regmap_read(map, AXP259_BATCAP0, &val); if ((axp259_config.pmu_battery_cap) && (!(val & 0x80))) { cur_coulomb_counter = axp259_config.pmu_battery_cap * 1000 / 1456; axp_regmap_write(map, AXP259_BATCAP0, ((cur_coulomb_counter >> 8) | 0x80)); axp_regmap_write(map, AXP259_BATCAP1, cur_coulomb_counter & 0x00FF); } else if (!axp259_config.pmu_battery_cap) { axp_regmap_write(map, AXP259_BATCAP0, 0x00); axp_regmap_write(map, AXP259_BATCAP1, 0x00); } if (axp259_config.pmu_bat_unused == 1) axp259_spy_info.batt->det_unused = 1; else axp259_spy_info.batt->det_unused = 0; axp259_spy_info.usb->det_unused = 1; if (axp259_config.pmu_bat_temp_enable != 0) { axp_regmap_write(map, AXP259_VLTF_CHARGE, axp259_config.pmu_bat_charge_ltf * 10 / 128); axp_regmap_write(map, AXP259_VHTF_CHARGE, axp259_config.pmu_bat_charge_htf * 10 / 128); axp_regmap_write(map, AXP259_VLTF_WORK, axp259_config.pmu_bat_shutdown_ltf * 10 / 128); axp_regmap_write(map, AXP259_VHTF_WORK, axp259_config.pmu_bat_shutdown_htf * 10 / 128); } return 0; } static struct axp_interrupts axp259_charger_irq[] = { {"ac in", axp_ac_in_isr}, {"ac out", axp_ac_out_isr}, {"bat in", axp_capchange_isr}, {"bat out", axp_capchange_isr}, {"charging", axp_change_isr}, {"charge over", axp_change_isr}, {"low warning1", axp_low_warning1_isr}, {"low warning2", axp_low_warning2_isr}, }; static void axp259_private_debug(struct axp_charger_dev *cdev) { u8 tmp[2]; struct axp_regmap *map = cdev->chip->regmap; axp_regmap_reads(map, 0x5a, 2, tmp); AXP_DEBUG(AXP_SPLY, cdev->chip->pmu_num, "acin_vol = %d\n", ((tmp[0] << 4) | (tmp[1] & 0xF)) * 8); axp_regmap_reads(map, 0xbc, 2, tmp); AXP_DEBUG(AXP_SPLY, cdev->chip->pmu_num, "ocv_vol = %d\n", ((tmp[0] << 4) | (tmp[1] & 0xF)) * 1200 / 1000); axp_regmap_read(map, 0xe4, &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, 0xe5, &tmp[0]); if (tmp[0] & 0x80) AXP_DEBUG(AXP_SPLY, cdev->chip->pmu_num, "coulomb_percent = %d\n", tmp[0] & 0x7f); } static int axp259_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, &axp259_config); if (ret) { pr_err("%s parse device tree err\n", __func__); return -EINVAL; } } else { pr_err("axp22 charger device tree err!\n"); return -EBUSY; } axp259_ac_info.ac_vol = axp259_config.pmu_ac_vol; axp259_ac_info.ac_cur = axp259_config.pmu_ac_cur; axp259_batt_info.runtime_chgcur = axp259_config.pmu_runtime_chgcur; axp259_batt_info.suspend_chgcur = axp259_config.pmu_suspend_chgcur; axp259_batt_info.shutdown_chgcur = axp259_config.pmu_shutdown_chgcur; battery_data.voltage_max_design = axp259_config.pmu_init_chgvol * 1000; battery_data.voltage_min_design = axp259_config.pmu_pwroff_vol * 1000; battery_data.energy_full_design = axp259_config.pmu_battery_cap; axp259_charger_init(axp_dev); chg_dev = axp_power_supply_register(&pdev->dev, axp_dev, &battery_data, &axp259_spy_info); if (IS_ERR_OR_NULL(chg_dev)) goto fail; chg_dev->private_debug = axp259_private_debug; for (i = 0; i < ARRAY_SIZE(axp259_charger_irq); i++) { irq = platform_get_irq_byname(pdev, axp259_charger_irq[i].name); if (irq < 0) continue; ret = axp_request_irq(axp_dev, irq, axp259_charger_irq[i].isr, chg_dev); if (ret != 0) { dev_err(&pdev->dev, "failed to request %s IRQ %d: %d\n", axp259_charger_irq[i].name, irq, ret); goto out_irq; } dev_dbg(&pdev->dev, "Requested %s IRQ %d: %d\n", axp259_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, axp259_charger_irq[i].name); if (irq < 0) continue; axp_free_irq(axp_dev, irq); } fail: return -1; } static int axp259_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(axp259_charger_irq); i++) { irq = platform_get_irq_byname(pdev, axp259_charger_irq[i].name); if (irq < 0) continue; axp_free_irq(axp_dev, irq); } axp_power_supply_unregister(chg_dev); return 0; } static int axp259_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 axp259_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; #ifdef CONFIG_AXP_NMI_USED enable_nmi(); #endif } 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, 0x05, chg_dev->rest_vol | 0x80); } return 0; } static void axp259_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 axp259_charger_dt_ids[] = { { .compatible = "axp259-charger", }, {}, }; MODULE_DEVICE_TABLE(of, axp259_charger_dt_ids); static struct platform_driver axp259_charger_driver = { .driver = { .name = "axp259-charger", .of_match_table = axp259_charger_dt_ids, }, .probe = axp259_charger_probe, .remove = axp259_charger_remove, .suspend = axp259_charger_suspend, .resume = axp259_charger_resume, .shutdown = axp259_charger_shutdown, }; static int __init axp259_charger_initcall(void) { int ret; ret = platform_driver_register(&axp259_charger_driver); if (IS_ERR_VALUE(ret)) { pr_err("%s: failed, errno %d\n", __func__, ret); return -EINVAL; } return 0; } fs_initcall_sync(axp259_charger_initcall); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Qin "); MODULE_DESCRIPTION("Charger Driver for axp259 PMIC"); MODULE_ALIAS("platform:axp259-charger");