[PATCH linux dev-4.13 2/3] i2c: fsi: Use mutex lock instead of semaphore

Eddie James eajames at linux.vnet.ibm.com
Wed May 30 05:16:37 AEST 2018


Simplify the FSI I2C driver by using a mutex lock instead of a
semaphore. This removes the wait until timeout on the semaphore.

Signed-off-by: Eddie James <eajames at linux.vnet.ibm.com>
---
 drivers/i2c/busses/i2c-fsi.c | 46 ++++++++------------------------------------
 1 file changed, 8 insertions(+), 38 deletions(-)

diff --git a/drivers/i2c/busses/i2c-fsi.c b/drivers/i2c/busses/i2c-fsi.c
index 3fcc14e..adc2e38 100644
--- a/drivers/i2c/busses/i2c-fsi.c
+++ b/drivers/i2c/busses/i2c-fsi.c
@@ -18,10 +18,9 @@
 #include <linux/kernel.h>
 #include <linux/list.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/of.h>
 #include <linux/sched.h>
-#include <linux/semaphore.h>
-#include <linux/wait.h>
 
 #define FSI_ENGID_I2C		0x7
 
@@ -145,8 +144,7 @@ struct fsi_i2c_master {
 	struct fsi_device	*fsi;
 	u8			fifo_size;
 	struct list_head	ports;
-	wait_queue_head_t	wait;
-	struct semaphore	lock;
+	struct mutex		lock;
 };
 
 struct fsi_i2c_port {
@@ -180,29 +178,6 @@ static int fsi_i2c_write_reg(struct fsi_device *fsi, unsigned int reg,
 	return fsi_device_write(fsi, reg, &data_be, sizeof(data_be));
 }
 
-static int fsi_i2c_lock_master(struct fsi_i2c_master *i2c, int timeout)
-{
-	int rc;
-
-	rc = down_trylock(&i2c->lock);
-	if (!rc)
-		return 0;
-
-	rc = wait_event_interruptible_timeout(i2c->wait,
-					      !down_trylock(&i2c->lock),
-					      timeout);
-	if (rc > 0)
-		return 0;
-
-	return -EBUSY;
-}
-
-static void fsi_i2c_unlock_master(struct fsi_i2c_master *i2c)
-{
-	up(&i2c->lock);
-	wake_up(&i2c->wait);
-}
-
 static int fsi_i2c_dev_init(struct fsi_i2c_master *i2c)
 {
 	int rc;
@@ -626,11 +601,10 @@ static int fsi_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
 	int i, rc;
 	unsigned long start_time;
 	struct fsi_i2c_port *port = adap->algo_data;
+	struct fsi_i2c_master *master = port->master;
 	struct i2c_msg *msg;
 
-	rc = fsi_i2c_lock_master(port->master, adap->timeout);
-	if (rc)
-		return rc;
+	mutex_lock(&master->lock);
 
 	rc = fsi_i2c_set_port(port);
 	if (rc)
@@ -651,7 +625,7 @@ static int fsi_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
 	}
 
 unlock:
-	fsi_i2c_unlock_master(port->master);
+	mutex_unlock(&master->lock);
 	return rc;
 }
 
@@ -667,14 +641,11 @@ static int fsi_i2c_recover_bus(struct i2c_adapter *adap)
 	struct fsi_i2c_port *port = adap->algo_data;
 	struct fsi_i2c_master *master = port->master;
 
-	rc = fsi_i2c_lock_master(master, adap->timeout);
-	if (rc)
-		return rc;
+	mutex_lock(&master->lock);
 
 	rc = fsi_i2c_reset(master, port->port);
 
-	fsi_i2c_unlock_master(master);
-
+	mutex_unlock(&master->lock);
 	return rc;
 }
 
@@ -699,8 +670,7 @@ static int fsi_i2c_probe(struct device *dev)
 	if (!i2c)
 		return -ENOMEM;
 
-	init_waitqueue_head(&i2c->wait);
-	sema_init(&i2c->lock, 1);
+	mutex_init(&i2c->lock);
 	i2c->fsi = to_fsi_dev(dev);
 	INIT_LIST_HEAD(&i2c->ports);
 
-- 
1.8.3.1



More information about the openbmc mailing list