Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751835AbdGQOgk (ORCPT ); Mon, 17 Jul 2017 10:36:40 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:58986 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751508AbdGQOgi (ORCPT ); Mon, 17 Jul 2017 10:36:38 -0400 From: Eddie James To: linux-kernel@vger.kernel.org Cc: linux-i2c@vger.kernel.org, devicetree@vger.kernel.org, wsa@the-dreams.de, robh+dt@kernel.org, gregkh@linuxfoundation.org, jk@ozlabs.org, joel@jms.id.au, andrew@aj.id.au, eajames@linux.vnet.ibm.com, cbostic@linux.vnet.ibm.com, "Edward A. James" Subject: [PATCH v4 4/6] drivers/i2c: Add I2C master locking to FSI algorithm Date: Mon, 17 Jul 2017 09:35:55 -0500 X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1500302157-14670-1-git-send-email-eajames@linux.vnet.ibm.com> References: <1500302157-14670-1-git-send-email-eajames@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 17071714-0012-0000-0000-000014AF6711 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00007378; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000214; SDB=6.00888884; UDB=6.00443946; IPR=6.00669057; BA=6.00005475; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00016250; XFM=3.00000015; UTC=2017-07-17 14:36:15 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17071714-0013-0000-0000-00004EA464C4 Message-Id: <1500302157-14670-5-git-send-email-eajames@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2017-07-17_11:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=1 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1706020000 definitions=main-1707170231 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2637 Lines: 110 From: "Edward A. James" Since there are many ports per master, each with it's own adapter and chardev, we need some locking to prevent transfers from changing the\ master state while other transfers are in progress. Signed-off-by: Edward A. James --- drivers/i2c/busses/i2c-fsi.c | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/drivers/i2c/busses/i2c-fsi.c b/drivers/i2c/busses/i2c-fsi.c index f86595b..1c7fcae 100644 --- a/drivers/i2c/busses/i2c-fsi.c +++ b/drivers/i2c/busses/i2c-fsi.c @@ -19,6 +19,8 @@ #include #include #include +#include +#include #define FSI_ENGID_I2C 0x7 @@ -135,6 +137,8 @@ struct fsi_i2c_master { struct fsi_device *fsi; u8 fifo_size; struct list_head ports; + wait_queue_head_t wait; + struct semaphore lock; }; struct fsi_i2c_port { @@ -168,6 +172,29 @@ 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; @@ -409,25 +436,31 @@ static int fsi_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, struct fsi_i2c_port *port = adap->algo_data; struct i2c_msg *msg; - rc = fsi_i2c_set_port(port); + rc = fsi_i2c_lock_master(port->master, adap->timeout); if (rc) return rc; + rc = fsi_i2c_set_port(port); + if (rc) + goto unlock; + for (i = 0; i < num; ++i) { msg = msgs + i; start_time = jiffies; rc = fsi_i2c_start(port, msg, i == num - 1); if (rc) - return rc; + goto unlock; rc = fsi_i2c_wait(port, msg, adap->timeout - (jiffies - start_time)); if (rc) - return rc; + goto unlock; } - return 0; +unlock: + fsi_i2c_unlock_master(port->master); + return rc; } static u32 fsi_i2c_functionality(struct i2c_adapter *adap) @@ -452,6 +485,8 @@ static int fsi_i2c_probe(struct device *dev) if (!i2c) return -ENOMEM; + init_waitqueue_head(&i2c->wait); + sema_init(&i2c->lock, 1); i2c->fsi = to_fsi_dev(dev); INIT_LIST_HEAD(&i2c->ports); -- 1.8.3.1