blob: e649d628f7344010b1a5e3916edc86785bf1d2a4 [file] [log] [blame]
Edward A. James7bd86372017-05-15 12:28:44 -05001From patchwork Wed May 10 15:52:40 2017
2Content-Type: text/plain; charset="utf-8"
3MIME-Version: 1.0
4Content-Transfer-Encoding: 7bit
5Subject: [linux, dev-4.10, v2,
6 4/6] drivers: i2c: Add I2C master locking to FSI algorithm
7From: eajames@linux.vnet.ibm.com
8X-Patchwork-Id: 760699
9Message-Id: <1494431562-25101-5-git-send-email-eajames@linux.vnet.ibm.com>
10To: openbmc@lists.ozlabs.org
11Cc: "Edward A. James" <eajames@us.ibm.com>, cbostic@linux.vnet.ibm.com
12Date: Wed, 10 May 2017 10:52:40 -0500
13
14From: "Edward A. James" <eajames@us.ibm.com>
15
16Since there are many ports per master, each with it's own adapter and
17chardev, we need some locking to prevent xfers from changing the master
18state while other xfers are in progress.
19
20Signed-off-by: Edward A. James <eajames@us.ibm.com>
21---
22 drivers/i2c/busses/i2c-fsi.c | 41 +++++++++++++++++++++++++++++++++++++----
23 1 file changed, 37 insertions(+), 4 deletions(-)
24
25diff --git a/drivers/i2c/busses/i2c-fsi.c b/drivers/i2c/busses/i2c-fsi.c
26index f690b16..d757aee 100644
27--- a/drivers/i2c/busses/i2c-fsi.c
28+++ b/drivers/i2c/busses/i2c-fsi.c
29@@ -136,6 +136,8 @@ struct fsi_i2c_master {
30 u8 fifo_size;
31 struct list_head ports;
32 struct ida ida;
33+ wait_queue_head_t wait;
34+ struct semaphore lock;
35 };
36
37 struct fsi_i2c_port {
38@@ -171,6 +173,29 @@ static int fsi_i2c_write_reg(struct fsi_device *fsi, unsigned int reg,
39 return fsi_device_write(fsi, reg, &raw_data, sizeof(raw_data));
40 }
41
42+static int fsi_i2c_lock_master(struct fsi_i2c_master *i2c, int timeout)
43+{
44+ int rc;
45+
46+ rc = down_trylock(&i2c->lock);
47+ if (!rc)
48+ return 0;
49+
50+ rc = wait_event_interruptible_timeout(i2c->wait,
51+ !down_trylock(&i2c->lock),
52+ timeout);
53+ if (rc > 0)
54+ return 0;
55+
56+ return -EBUSY;
57+}
58+
59+static void fsi_i2c_unlock_master(struct fsi_i2c_master *i2c)
60+{
61+ up(&i2c->lock);
62+ wake_up(&i2c->wait);
63+}
64+
65 static int fsi_i2c_dev_init(struct fsi_i2c_master *i2c)
66 {
67 int rc;
68@@ -410,25 +435,31 @@ static int fsi_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
69 struct fsi_i2c_port *port = adap->algo_data;
70 struct i2c_msg *msg;
71
72- rc = fsi_i2c_set_port(port);
73+ rc = fsi_i2c_lock_master(port->master, adap->timeout);
74 if (rc)
75 return rc;
76
77+ rc = fsi_i2c_set_port(port);
78+ if (rc)
79+ goto unlock;
80+
81 for (i = 0; i < num; ++i) {
82 msg = msgs + i;
83 start_time = jiffies;
84
85 rc = fsi_i2c_start(port, msg, i == num - 1);
86 if (rc)
87- return rc;
88+ goto unlock;
89
90 rc = fsi_i2c_wait(port, msg,
91 adap->timeout - (jiffies - start_time));
92 if (rc)
93- return rc;
94+ goto unlock;
95 }
96
97- return 0;
98+unlock:
99+ fsi_i2c_unlock_master(port->master);
100+ return rc;
101 }
102
103 static u32 fsi_i2c_functionality(struct i2c_adapter *adap)
104@@ -453,6 +484,8 @@ static int fsi_i2c_probe(struct device *dev)
105 if (!i2c)
106 return -ENOMEM;
107
108+ init_waitqueue_head(&i2c->wait);
109+ sema_init(&i2c->lock, 1);
110 i2c->fsi = to_fsi_dev(dev);
111 i2c->idx = ida_simple_get(&fsi_i2c_ida, 1, INT_MAX, GFP_KERNEL);
112 ida_init(&i2c->ida);