p9221_charger: stay in EPP when alignment is bad

The following conditions cannot enter the WLC-DC
1. Misalignment
2. The mfg code cannot be obtained within 3 seconds after WLC online

Bug: 260019242
Change-Id: I7b23830d18efe2141adeccfaebba79be8d4aab79
Signed-off-by: yihsiangpeng <[email protected]>
diff --git a/p9221_charger.c b/p9221_charger.c
index b0af8d1..bd691c7 100644
--- a/p9221_charger.c
+++ b/p9221_charger.c
@@ -27,7 +27,7 @@
 #include <linux/debugfs.h>
 
 #define P9221R5_OVER_CHECK_NUM		3
-
+#define MFG_CHK_COUNT_MAX		30
 #define OVC_LIMIT			1
 #define OVC_THRESHOLD			1400000
 #define OVC_BACKOFF_LIMIT		900000
@@ -1438,7 +1438,7 @@
 	 *  Check 10 times if alignment_capble is still 0.
 	 */
 
-	if ((charger->mfg_check_count < 10) ||
+	if ((charger->mfg_check_count < MFG_CHK_COUNT_MAX) ||
 	    (charger->alignment_capable == ALIGN_MFG_PASSED)) {
 
 		/* release the align_ws before return*/
@@ -1455,7 +1455,8 @@
 	/* release the align_ws */
 	__pm_relax(charger->align_ws);
 
-	dev_info(&charger->client->dev, "align_work ended\n");
+	dev_info(&charger->client->dev, "align_work ended(mfg_check_count=%d)\n",
+		 charger->mfg_check_count);
 }
 
 static const char *p9221_get_tx_id_str(struct p9221_charger_data *charger)
@@ -2268,6 +2269,23 @@
 	return ret;
 }
 
+static int p9xxx_check_alignment(struct p9221_charger_data *charger)
+{
+	int ret = 0;
+
+	if (charger->alignment == 100) {
+		dev_dbg(&charger->client->dev, "Alignment check OK\n");
+	} else if (charger->alignment == -1 && charger->mfg_check_count < MFG_CHK_COUNT_MAX) {
+		ret = -EAGAIN;
+		dev_dbg(&charger->client->dev, "Alignment checking\n");
+	} else {
+		ret = -EOPNOTSUPP;
+		dev_err(&charger->client->dev, "Misalignment!\n");
+	}
+
+	return ret;
+}
+
 /* < 0 error, 0 = no changes, > 1 changed */
 static int p9221_set_psy_online(struct p9221_charger_data *charger, int online)
 {
@@ -2346,15 +2364,20 @@
 			return -EOPNOTSUPP;
 		}
 
+		/* AUTH is passed remove the DC_ICL limit */
+		p9221_set_auth_dc_icl(charger, false);
+		mutex_unlock(&charger->auth_lock);
+
+		/* Check alignment before enabling proprietary mode */
+		ret = p9xxx_check_alignment(charger);
+		if (ret < 0)
+			return ret;
+
 		ret = p9221_set_hpp_dc_icl(charger, true);
 		if (ret < 0)
 			dev_warn(&charger->client->dev, "Cannot enable HPP_ICL (%d)\n", ret);
 		mdelay(10);
 
-		/* AUTH is passed remove the DC_ICL limit */
-		p9221_set_auth_dc_icl(charger, false);
-		mutex_unlock(&charger->auth_lock);
-
 		/*
 		 * run ->chip_prop_mode_en() if proprietary mode or cap divider
 		 * mode isn't enabled (i.e. with p9412_prop_mode_enable())