From b30a26cb07c5054bb4f8219e207c3be526b1e288 Mon Sep 17 00:00:00 2001 From: Laxman Dewangan Date: Thu, 9 Nov 2017 17:56:22 +0530 Subject: [PATCH 2/2] mfd: max77620: Add configurations of backup battery Add PMIC configurations for backup battery and low-battery monitoring. Bug 2020512 Change-Id: If70bfe375ec83668d30c6521148d0461fabc33c8 Signed-off-by: Laxman Dewangan Reviewed-on: https://git-master.nvidia.com/r/1595616 GVS: Gerrit_Virtual_Submit Reviewed-by: Pavan Kunapuli Reviewed-by: mobile promotions Tested-by: mobile promotions --- drivers/mfd/max77620.c | 164 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) diff --git a/drivers/mfd/max77620.c b/drivers/mfd/max77620.c index 4dd0518..1ae7160 100644 --- a/drivers/mfd/max77620.c +++ b/drivers/mfd/max77620.c @@ -328,6 +328,162 @@ skip_fps: return 0; } +static int max77620_init_backup_battery_charging(struct max77620_chip *chip) +{ + struct device *dev = chip->dev; + struct device_node *np; + u32 pval; + u8 config; + int charging_current; + int charging_voltage; + int resistor; + int ret; + + np = of_get_child_by_name(dev->of_node, "backup-battery"); + if (!np) { + dev_info(dev, "Backup battery charging support disabled\n"); + ret = regmap_update_bits(chip->rmap, MAX77620_REG_CNFGBBC, + MAX77620_CNFGBBC_ENABLE, 0); + if (ret < 0) + dev_err(dev, "Failed to update CNFGBBC: %d\n", ret); + return ret; + } + + ret = of_property_read_u32(np, + "backup-battery-charging-current", &pval); + if (ret < 0) + ret = of_property_read_u32(np, + "maxim,backup-battery-charging-current", &pval); + charging_current = (!ret) ? pval : 50; + + ret = of_property_read_u32(np, + "backup-battery-charging-voltage", &pval); + if (ret < 0) + ret = of_property_read_u32(np, + "maxim,backup-battery-charging-voltage", &pval); + charging_voltage = (!ret) ? pval : 2500000; + charging_voltage /= 1000; + + ret = of_property_read_u32(np, "backup-battery-output-resister", &pval); + if (ret < 0) + ret = of_property_read_u32(np, + "maxim,backup-battery-output-resister", &pval); + resistor = (!ret) ? pval : 1000; + + config = MAX77620_CNFGBBC_ENABLE; + if (charging_current <= 50) + config |= 0 << MAX77620_CNFGBBC_CURRENT_SHIFT; + else if (charging_current <= 100) + config |= 3 << MAX77620_CNFGBBC_CURRENT_SHIFT; + else if (charging_current <= 200) + config |= 0 << MAX77620_CNFGBBC_CURRENT_SHIFT; + else if (charging_current <= 400) + config |= 3 << MAX77620_CNFGBBC_CURRENT_SHIFT; + else if (charging_current <= 600) + config |= 1 << MAX77620_CNFGBBC_CURRENT_SHIFT; + else + config |= 2 << MAX77620_CNFGBBC_CURRENT_SHIFT; + + if (charging_current > 100) + config |= MAX77620_CNFGBBC_LOW_CURRENT_DISABLE; + + if (charging_voltage <= 2500) + config |= 0 << MAX77620_CNFGBBC_VOLTAGE_SHIFT; + else if (charging_voltage <= 3000) + config |= 1 << MAX77620_CNFGBBC_VOLTAGE_SHIFT; + else if (charging_voltage <= 3300) + config |= 2 << MAX77620_CNFGBBC_VOLTAGE_SHIFT; + else + config |= 3 << MAX77620_CNFGBBC_VOLTAGE_SHIFT; + + if (resistor <= 100) + config |= 0 << MAX77620_CNFGBBC_RESISTOR_SHIFT; + else if (resistor <= 1000) + config |= 1 << MAX77620_CNFGBBC_RESISTOR_SHIFT; + else if (resistor <= 3000) + config |= 2 << MAX77620_CNFGBBC_RESISTOR_SHIFT; + else if (resistor <= 6000) + config |= 3 << MAX77620_CNFGBBC_RESISTOR_SHIFT; + + ret = regmap_write(chip->rmap, MAX77620_REG_CNFGBBC, config); + if (ret < 0) { + dev_err(dev, "Reg 0x%02x write failed, %d\n", + MAX77620_REG_CNFGBBC, ret); + return ret; + } + return 0; +} + +static int max77620_init_low_battery_monitor(struct max77620_chip *chip) +{ + struct device *dev = chip->dev; + struct device_node *np; + bool pval; + u8 mask = 0; + u8 val = 0; + int ret; + + np = of_get_child_by_name(dev->of_node, "low-battery-monitor"); + if (!np) + return 0; + + pval = of_property_read_bool(np, "low-battery-dac-enable"); + if (!pval) + pval = of_property_read_bool(np, + "maxim,low-battery-dac-enable"); + if (pval) { + mask |= MAX77620_CNFGGLBL1_LBDAC_EN; + val |= MAX77620_CNFGGLBL1_LBDAC_EN; + } + + pval = of_property_read_bool(np, "low-battery-dac-disable"); + if (!pval) + pval = of_property_read_bool(np, + "maxim,low-battery-dac-disable"); + if (pval) + mask |= MAX77620_CNFGGLBL1_LBDAC_EN; + + pval = of_property_read_bool(np, "low-battery-shutdown-enable"); + if (!pval) + pval = of_property_read_bool(np, + "maxim,low-battery-shutdown-enable"); + if (pval) { + mask |= MAX77620_CNFGGLBL1_MPPLD; + val |= MAX77620_CNFGGLBL1_MPPLD; + } + + pval = of_property_read_bool(np, "low-battery-shutdown-disable"); + if (!pval) + pval = of_property_read_bool(np, + "maxim,low-battery-shutdown-disable"); + if (pval) + mask |= MAX77620_CNFGGLBL1_MPPLD; + + pval = of_property_read_bool(np, "low-battery-reset-enable"); + if (!pval) + pval = of_property_read_bool(np, + "maxim,low-battery-reset-enable"); + if (pval) { + mask |= MAX77620_CNFGGLBL1_LBRSTEN; + val |= MAX77620_CNFGGLBL1_LBRSTEN; + } + + pval = of_property_read_bool(np, "low-battery-reset-disable"); + if (!pval) + pval = of_property_read_bool(np, + "maxim,low-battery-reset-disable"); + if (pval) + mask |= MAX77620_CNFGGLBL1_LBRSTEN; + + ret = regmap_update_bits(chip->rmap, MAX77620_REG_CNFGGLBL1, mask, val); + if (ret < 0) { + dev_err(dev, "Reg CNFGGLBL1 update failed, %d\n", ret); + return ret; + } + + return 0; +} + static int max77620_read_es_version(struct max77620_chip *chip) { unsigned int val; @@ -415,6 +571,14 @@ static int max77620_probe(struct i2c_client *client, if (ret < 0) return ret; + ret = max77620_init_backup_battery_charging(chip); + if (ret < 0) + return ret; + + ret = max77620_init_low_battery_monitor(chip); + if (ret < 0) + return ret; + ret = mfd_add_devices(chip->dev, PLATFORM_DEVID_NONE, mfd_cells, n_mfd_cells, NULL, 0, regmap_irq_get_domain(chip->top_irq_data)); -- 1.9.1