Skip to contentSkip to conversation

i2c: designware: Add helper to remove redundancy

PATCH In review Jan Dabros 2 participants 3 messages Mar 10, 2022

Subsystems (3)
lore
Share & tools
lore

Jan Dabros

author
#0 lore raw

PATCH 1/1 i2c: designware: Add helper to remove redundancy

Simplify code by adding an extra static function for sending I2C requests and verifying results.

---
 drivers/i2c/busses/i2c-designware-amdpsp.c | 44 ++++++++++++----------
 1 file changed, 24 insertions(+), 20 deletions(-)
drivers/i2c/busses/i2c-designware-amdpsp.c +24 -20
index c64e459afb5c..cc758792f150 100644
@@ -229,6 +229,26 @@ static int psp_send_i2c_req(enum psp_i2c_req_type i2c_req_type)
 	return ret;
 }
 
+static int psp_send_i2c_req_check_err(enum psp_i2c_req_type request)
+{
+	int status;
+
+	status = psp_send_i2c_req(request);
+	if (status) {
+		if (status == -ETIMEDOUT)
+			dev_err(psp_i2c_dev, "Timed out waiting for PSP to %s I2C bus\n",
+				(request == PSP_I2C_REQ_ACQUIRE) ?
+				"release" : "acquire");
+		else
+			dev_err(psp_i2c_dev, "PSP communication error\n");
+
+		dev_err(psp_i2c_dev, "Assume i2c bus is for exclusive host usage\n");
+		psp_i2c_mbox_fail = true;
+	}
+
+	return status;
+}
+
 static int psp_acquire_i2c_bus(void)
 {
 	int status;
@@ -248,17 +268,9 @@ static int psp_acquire_i2c_bus(void)
 		goto cleanup;
 	};
 
-	status = psp_send_i2c_req(PSP_I2C_REQ_ACQUIRE);
-	if (status) {
-		if (status == -ETIMEDOUT)
-			dev_err(psp_i2c_dev, "Timed out waiting for PSP to release I2C bus\n");
-		else
-			dev_err(psp_i2c_dev, "PSP communication error\n");
-
-		dev_err(psp_i2c_dev, "Assume i2c bus is for exclusive host usage\n");
-		psp_i2c_mbox_fail = true;
+	status = psp_send_i2c_req_check_err(PSP_I2C_REQ_ACQUIRE);
+	if (status)
 		goto cleanup;
-	}
 
 	psp_i2c_sem_acquired = jiffies;
 	psp_i2c_access_count++;
@@ -293,17 +305,9 @@ static void psp_release_i2c_bus(void)
 		goto cleanup;
 
 	/* Send a release command to PSP */
-	status = psp_send_i2c_req(PSP_I2C_REQ_RELEASE);
-	if (status) {
-		if (status == -ETIMEDOUT)
-			dev_err(psp_i2c_dev, "Timed out waiting for PSP to acquire I2C bus\n");
-		else
-			dev_err(psp_i2c_dev, "PSP communication error\n");
-
-		dev_err(psp_i2c_dev, "Assume i2c bus is for exclusive host usage\n");
-		psp_i2c_mbox_fail = true;
+	status = psp_send_i2c_req_check_err(PSP_I2C_REQ_RELEASE);
+	if (status)
 		goto cleanup;
-	}
 
 	dev_dbg(psp_i2c_dev, "PSP semaphore held for %ums\n",
 		jiffies_to_msecs(jiffies - psp_i2c_sem_acquired));
Signature · 1 line
2.35.1.616.g0bdcbb4464-goog
#1 lore raw
Quoted 8 lines · Jan Dabros

Simplify code by adding an extra static function for sending I2C requests and verifying results.

---
  drivers/i2c/busses/i2c-designware-amdpsp.c | 44 ++++++++++++----------
  1 file changed, 24 insertions(+), 20 deletions(-)

Do I remember correctly was this suggested by Andy? I.e. to give kudos to him if that was the case:

Quoted 15 lines
drivers/i2c/busses/i2c-designware-amdpsp.c +7 -0
index c64e459afb5c..cc758792f150 100644
@@ -229,6 +229,26 @@ static int psp_send_i2c_req(enum psp_i2c_req_type i2c_req_type)
  	return ret;
  }
 
+static int psp_send_i2c_req_check_err(enum psp_i2c_req_type request)
+{
+	int status;
+
+	status = psp_send_i2c_req(request);
+	if (status) {
+		if (status == -ETIMEDOUT)

drivers/i2c/busses/i2c-designware-amdpsp.c static int psp_send_i2c_req(enum psp_i2c_req_type i2c_req_type) · line 248

Quoted 10 lines
drivers/i2c/busses/i2c-designware-amdpsp.c +10 -0
+			dev_err(psp_i2c_dev, "Timed out waiting for PSP to %s I2C bus\n",
+				(request == PSP_I2C_REQ_ACQUIRE) ?
+				"release" : "acquire");
+		else
+			dev_err(psp_i2c_dev, "PSP communication error\n");
+
+		dev_err(psp_i2c_dev, "Assume i2c bus is for exclusive host usage\n");
+		psp_i2c_mbox_fail = true;
+	}
+

Does it make sense to have these inside the psp_send_i2c_req() and get rid of this new middle function? I mean psp_send_i2c_req() is called now only from here so can it do these common error prints and set "psp_i2c_mbox_fail = true"?

Jarkko

#2 lore raw

czw., 10 mar 2022 o 15:52 Jarkko Nikula
<[email protected]> napisał(a):

Quoted 10 lines · Jan Dabros
Quoted 8 lines

Simplify code by adding an extra static function for sending I2C requests and verifying results.

---
  drivers/i2c/busses/i2c-designware-amdpsp.c | 44 ++++++++++++----------
  1 file changed, 24 insertions(+), 20 deletions(-)
Quoted 4 lines

Do I remember correctly was this suggested by Andy? I.e. to give kudos to him if that was the case:

Definitely! Actually I wasn't aware of such a tag, will add this in v2.

Quoted 23 lines
Quoted 23 lines
drivers/i2c/busses/i2c-designware-amdpsp.c +14 -0
index c64e459afb5c..cc758792f150 100644
@@ -229,6 +229,26 @@ static int psp_send_i2c_req(enum psp_i2c_req_type i2c_req_type)
      return ret;
  }
 
+static int psp_send_i2c_req_check_err(enum psp_i2c_req_type request)
+{
+     int status;
+
+     status = psp_send_i2c_req(request);
+     if (status) {
+             if (status == -ETIMEDOUT)
+                     dev_err(psp_i2c_dev, "Timed out waiting for PSP to %s I2C bus\n",
+                             (request == PSP_I2C_REQ_ACQUIRE) ?
+                             "release" : "acquire");
+             else
+                     dev_err(psp_i2c_dev, "PSP communication error\n");
+
+             dev_err(psp_i2c_dev, "Assume i2c bus is for exclusive host usage\n");
Quoted 8 lines

drivers/i2c/busses/i2c-designware-amdpsp.c static int psp_send_i2c_req(enum psp_i2c_req_type i2c_req_type) · line 248

Quoted 3 lines
drivers/i2c/busses/i2c-designware-amdpsp.c +3 -0
+             psp_i2c_mbox_fail = true;
+     }
+

Does it make sense to have these inside the psp_send_i2c_req() and get rid of this new middle function? I mean psp_send_i2c_req() is called now only from here so can it do these common error prints and set "psp_i2c_mbox_fail = true"?

I like this idea, please take a look at v2.

Best Regards,
Jan