target: msm8952: Add dead battery charging support for SDM429W

Move dead battery charging (partially) from SBL to LK.
LK supports display and hence moving the charging to LK will
keep the user informed that the device is up and charging.
LK charges the battery from SBL exit threshold (3.2V) to
HLOS entry threshold - configurable (3.4V).

Change-Id: I537bd5e56d49b098b1bd8b8613651f9e102d26d8
Signed-off-by: Umang Chheda <uchheda@codeaurora.org>
diff --git a/dev/pmic/pm8x41/include/qpnp-fg-gen3.h b/dev/pmic/pm8x41/include/qpnp-fg-gen3.h
new file mode 100644
index 0000000..baae8e3
--- /dev/null
+++ b/dev/pmic/pm8x41/include/qpnp-fg-gen3.h
@@ -0,0 +1,38 @@
+/* Copyright (c) 2015, 2019, The Linux Foundation. All rights reserved.
+
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *   * Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   * Redistributions in binary form must reproduce the above
+ *     copyright notice, this list of conditions and the following
+ *     disclaimer in the documentation and/or other materials provided
+ *     with the distribution.
+ *   * Neither the name of The Linux Foundation nor the names of its
+ *     contributors may be used to endorse or promote products derived
+ *     from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _QPNP_FG_GEN3_H_
+#define _QPNP_FG_GEN3_H_
+
+#include <stdint.h>
+#include <sys/types.h>
+
+uint32_t fg_gen3_get_battery_voltage(void);
+int fg_gen3_get_battery_current(void);
+
+#endif
diff --git a/dev/pmic/pm8x41/include/qpnp-smb2.h b/dev/pmic/pm8x41/include/qpnp-smb2.h
new file mode 100644
index 0000000..fb905b9
--- /dev/null
+++ b/dev/pmic/pm8x41/include/qpnp-smb2.h
@@ -0,0 +1,34 @@
+/* Copyright (c) 2015, 2019, The Linux Foundation. All rights reserved.
+
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *   * Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   * Redistributions in binary form must reproduce the above
+ *     copyright notice, this list of conditions and the following
+ *     disclaimer in the documentation and/or other materials provided
+ *     with the distribution.
+ *   * Neither the name of The Linux Foundation nor the names of its
+ *     contributors may be used to endorse or promote products derived
+ *     from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _QPNP_SMB2_H_
+#define _QPNP_SMB2_H_
+
+void weak_battery_charging(void);
+
+#endif
diff --git a/dev/pmic/pm8x41/qpnp-fg-gen3.c b/dev/pmic/pm8x41/qpnp-fg-gen3.c
new file mode 100644
index 0000000..39d5457
--- /dev/null
+++ b/dev/pmic/pm8x41/qpnp-fg-gen3.c
@@ -0,0 +1,101 @@
+/* Copyright (c) 2015, 2019, The Linux Foundation. All rights reserved.
+
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *   * Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   * Redistributions in binary form must reproduce the above
+ *     copyright notice, this list of conditions and the following
+ *     disclaimer in the documentation and/or other materials provided
+ *     with the distribution.
+ *   * Neither the name of The Linux Foundation nor the names of its
+ *     contributors may be used to endorse or promote products derived
+ *     from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <bits.h>
+#include <debug.h>
+#include <stdlib.h>
+#include <pm8x41.h>
+#include <pm8x41_hw.h>
+#include <qpnp-fg-gen3.h>
+#include <reg.h>
+
+#define BATT_INFO_VBATT_LSB	0x41A0
+#define BATT_INFO_VBATT_MSB	0x41A1
+#define BATT_INFO_CURR_LSB	0x41A2
+#define BATT_INFO_CURR_MSB	0x41A3
+
+#define BATT_VOLTAGE_NUMR	122070
+#define BATT_VOLTAGE_DENR	1000
+#define BATT_CURRENT_NUMR	488281
+#define BATT_CURRENT_DENR	1000
+
+#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
+
+static int64_t twos_compliment_extend(int64_t val, int sign_bit_pos)
+{
+	int i, nbytes = DIV_ROUND_UP(sign_bit_pos, 8);
+	int64_t mask, val_out;
+
+	val_out = val;
+	mask = 1 << sign_bit_pos;
+	if (val & mask) {
+		for (i = 8; i > nbytes; i--) {
+			mask = 0xFFLL << ((i - 1) * 8);
+			val_out |= mask;
+		}
+
+		if ((nbytes * 8) - 1 > sign_bit_pos) {
+			mask = 1 << sign_bit_pos;
+			for (i = 1; i <= (nbytes * 8) - sign_bit_pos; i++)
+				val_out |= mask << i;
+		}
+	}
+
+	return val_out;
+}
+
+uint32_t fg_gen3_get_battery_voltage(void)
+{
+	uint8_t buff[2];
+	uint16_t temp;
+	uint32_t vbat = 0;
+
+	buff[0] = REG_READ(BATT_INFO_VBATT_LSB);
+	buff[1] = REG_READ(BATT_INFO_VBATT_MSB);
+	temp = buff[1] << 8 | buff[0];
+	/* {MSB,LSB} to decode the voltage level, refer register description. */
+	vbat = (((uint64_t)temp)*BATT_VOLTAGE_NUMR/BATT_VOLTAGE_DENR);
+
+	return vbat;
+}
+
+int fg_gen3_get_battery_current(void)
+{
+	uint8_t buff[2];
+	int current = 0;
+	int64_t temp = 0;
+
+	buff[0] = REG_READ(BATT_INFO_CURR_LSB);
+	buff[1] = REG_READ(BATT_INFO_CURR_MSB);
+	temp = buff[1] << 8 | buff[0];
+	/* Sign bit is bit 15 */
+	temp = twos_compliment_extend(temp, 15);
+	current = (((int64_t)temp)*BATT_CURRENT_NUMR/BATT_CURRENT_DENR);
+
+	return current;
+}
diff --git a/dev/pmic/pm8x41/qpnp-smb2.c b/dev/pmic/pm8x41/qpnp-smb2.c
new file mode 100644
index 0000000..07f5265
--- /dev/null
+++ b/dev/pmic/pm8x41/qpnp-smb2.c
@@ -0,0 +1,133 @@
+/* Copyright (c) 2015, 2019, The Linux Foundation. All rights reserved.
+
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *   * Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   * Redistributions in binary form must reproduce the above
+ *     copyright notice, this list of conditions and the following
+ *     disclaimer in the documentation and/or other materials provided
+ *     with the distribution.
+ *   * Neither the name of The Linux Foundation nor the names of its
+ *     contributors may be used to endorse or promote products derived
+ *     from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <bits.h>
+#include <debug.h>
+#include <pm8x41.h>
+#include <pm8x41_hw.h>
+#include <qpnp-smb2.h>
+#include <qpnp-fg-gen3.h>
+#include <qtimer.h>
+#include <reboot.h>
+#include <target.h>
+
+#define BATT_CHARGER_STATUS_1_REG	0x1006
+#define BATT_CHARGER_STATUS_2_REG	0x1007
+#define BATT_IF_INT_RT_STS	0x1210
+#define USBIN_PLUGIN_RT_STS_REG	0x1310
+
+#define BATT_STATUS_MASK	0x07
+#define DISABLE_CHARGE	0x07
+
+static bool is_battery_present(void)
+{
+	uint8_t val = 0;
+
+	val = REG_READ(BATT_IF_INT_RT_STS);
+
+	/*
+	 * If BATT_TERMINAL_MISSING_RT_STS BIT(5) or BATT_THERM_OR_ID_MISSING_RT_STS BIT(4)
+	 * are set, battery is not present.
+	 */
+	if ((val & BIT(5)) | (val & BIT(4)))
+		return false;
+
+	return true;
+}
+
+static void dump_jeita_status(void)
+{
+	uint8_t val = 0;
+
+	val = REG_READ(BATT_CHARGER_STATUS_2_REG);
+	dprintf(INFO,"BATT_CHARGER_STATUS_2_REG value : %d\n",val);
+}
+
+static bool is_usb_present(void)
+{
+	uint8_t val = 0;
+
+	val = REG_READ(USBIN_PLUGIN_RT_STS_REG);
+
+	if (val & BIT(4))
+		return true;
+
+	return false;
+}
+
+static bool is_battery_charging(void)
+{
+	uint8_t val = 0;
+
+	val = REG_READ(BATT_CHARGER_STATUS_1_REG);
+	dprintf(INFO,"BATT_CHARGER_STATUS_1_REG value : %d\n",val);
+	val = val & BATT_STATUS_MASK;
+
+	if (val == DISABLE_CHARGE)
+		return false;
+
+	return true;
+}
+
+void weak_battery_charging(void)
+{
+	uint32_t current_vbat = 0;
+	int batt_curr = 0;
+
+	current_vbat = fg_gen3_get_battery_voltage();
+	batt_curr = fg_gen3_get_battery_current();
+	dprintf(INFO,"Entering LK charging loop: Battery current=%d mA voltage=%d mV\n",
+								batt_curr,current_vbat);
+
+	while (current_vbat < LK_BATT_VOLT_THRESHOLD)
+	{
+		if (!is_battery_present()) {
+			dprintf(CRITICAL, "Battery is not present.Exiting weak battery charging\n");
+			break;
+		}
+
+		if (!is_usb_present()) {
+			dprintf(CRITICAL, "Shutting down because USB is not present\n");
+			shutdown_device();
+		}
+
+		if (!is_battery_charging()) {
+			dump_jeita_status();
+			dprintf(CRITICAL, "Shutting down because battery is not charging\n");
+			shutdown_device();
+		}
+
+		current_vbat = fg_gen3_get_battery_voltage();
+		batt_curr = fg_gen3_get_battery_current();
+		dprintf(INFO, "Battery Charging: current=%d mA voltage=%d mV\n",
+							batt_curr,current_vbat);
+		mdelay(1000);
+	}
+
+	dprintf(INFO,"Battery Charging is completed Booting to HLOS\n");
+}