From: Doug Anderson <dianders@chromium.org>
To: Lee Jones <lee.jones@linaro.org>,
Stephen Warren <swarren@nvidia.com>,
Wolfram Sang <wsa@the-dreams.de>
Cc: abrestic@chromium.org, dgreid@chromium.org,
Olof Johansson <olof@lixom.net>, Simon Glass <sjg@chromium.org>,
linux-samsung-soc@vger.kernel.org, linux-tegra@vger.kernel.org,
Doug Anderson <dianders@chromium.org>,
Samuel Ortiz <sameo@linux.intel.com>,
linux-kernel@vger.kernel.org
Subject: [PATCH 2/7] mfd: cros_ec: spi: Add mutex to cros_ec_spi
Date: Thu, 17 Apr 2014 10:59:25 -0700 [thread overview]
Message-ID: <1397757570-19750-3-git-send-email-dianders@chromium.org> (raw)
In-Reply-To: <1397757570-19750-1-git-send-email-dianders@chromium.org>
The main transfer function for cros_ec_spi can be called by more than
one client at a time. Make sure that those clients don't stomp on
each other by locking the bus for the duration of the transfer
function.
Signed-off-by: Doug Anderson <dianders@chromium.org>
---
drivers/mfd/cros_ec_spi.c | 26 +++++++++++++++++++++-----
1 file changed, 21 insertions(+), 5 deletions(-)
diff --git a/drivers/mfd/cros_ec_spi.c b/drivers/mfd/cros_ec_spi.c
index c185eb6..a2a605d 100644
--- a/drivers/mfd/cros_ec_spi.c
+++ b/drivers/mfd/cros_ec_spi.c
@@ -65,11 +65,13 @@
* if no record
* @end_of_msg_delay: used to set the delay_usecs on the spi_transfer that
* is sent when we want to turn off CS at the end of a transaction.
+ * @lock: mutex to ensure only one user of cros_ec_command_spi_xfer at a time
*/
struct cros_ec_spi {
struct spi_device *spi;
s64 last_transfer_ns;
unsigned int end_of_msg_delay;
+ struct mutex lock;
};
static void debug_packet(struct device *dev, const char *name, u8 *ptr,
@@ -208,6 +210,13 @@ static int cros_ec_command_spi_xfer(struct cros_ec_device *ec_dev,
int ret = 0, final_ret;
struct timespec ts;
+ /*
+ * We have the shared ec_dev buffer plus we do lots of separate spi_sync
+ * calls, so we need to make sure only one person is using this at a
+ * time.
+ */
+ mutex_lock(&ec_spi->lock);
+
len = cros_ec_prepare_tx(ec_dev, ec_msg);
dev_dbg(ec_dev->dev, "prepared, len=%d\n", len);
@@ -260,7 +269,7 @@ static int cros_ec_command_spi_xfer(struct cros_ec_device *ec_dev,
ret = final_ret;
if (ret < 0) {
dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
- return ret;
+ goto exit;
}
/* check response error code */
@@ -269,14 +278,16 @@ static int cros_ec_command_spi_xfer(struct cros_ec_device *ec_dev,
dev_warn(ec_dev->dev, "command 0x%02x returned an error %d\n",
ec_msg->cmd, ptr[0]);
debug_packet(ec_dev->dev, "in_err", ptr, len);
- return -EINVAL;
+ ret = -EINVAL;
+ goto exit;
}
len = ptr[1];
sum = ptr[0] + ptr[1];
if (len > ec_msg->in_len) {
dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)",
len, ec_msg->in_len);
- return -ENOSPC;
+ ret = -ENOSPC;
+ goto exit;
}
/* copy response packet payload and compute checksum */
@@ -293,10 +304,14 @@ static int cros_ec_command_spi_xfer(struct cros_ec_device *ec_dev,
dev_err(ec_dev->dev,
"bad packet checksum, expected %02x, got %02x\n",
sum, ptr[len + 2]);
- return -EBADMSG;
+ ret = -EBADMSG;
+ goto exit;
}
- return 0;
+ ret = 0;
+exit:
+ mutex_unlock(&ec_spi->lock);
+ return ret;
}
static void cros_ec_spi_dt_probe(struct cros_ec_spi *ec_spi, struct device *dev)
@@ -327,6 +342,7 @@ static int cros_ec_spi_probe(struct spi_device *spi)
if (ec_spi == NULL)
return -ENOMEM;
ec_spi->spi = spi;
+ mutex_init(&ec_spi->lock);
ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
if (!ec_dev)
return -ENOMEM;
--
1.9.1.423.g4596e3a
next prev parent reply other threads:[~2014-04-17 18:01 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <1397757570-19750-1-git-send-email-dianders@chromium.org>
2014-04-17 17:59 ` [PATCH 1/7] mfd: cros_ec: spi: calculate delay between transfers correctly Doug Anderson
2014-04-18 17:27 ` Simon Glass
2014-04-17 17:59 ` Doug Anderson [this message]
2014-04-18 17:28 ` [PATCH 2/7] mfd: cros_ec: spi: Add mutex to cros_ec_spi Simon Glass
2014-04-18 21:15 ` Doug Anderson
2014-04-18 21:43 ` Simon Glass
2014-04-17 17:59 ` [PATCH 3/7] mfd: cros_ec: spi: Make the cros_ec_spi timeout more reliable Doug Anderson
2014-04-18 17:26 ` Simon Glass
2014-04-17 17:59 ` [PATCH 4/7] mfd: cros_ec: spi: Increase cros_ec_spi deadline from 5ms to 100ms Doug Anderson
2014-04-18 17:30 ` Simon Glass
2014-04-17 17:59 ` [PATCH 5/7] mfd: cros_ec: Sync to the latest cros_ec_commands.h from EC sources Doug Anderson
2014-04-18 17:29 ` Simon Glass
2014-04-17 17:59 ` [PATCH 7/7] ARM: tegra: Add the EC i2c tunnel to tegra124-venice2 Doug Anderson
2014-04-21 18:18 ` Stephen Warren
2014-04-21 19:35 ` Doug Anderson
2014-04-21 20:03 ` Stephen Warren
2014-05-15 18:42 ` Stephen Warren
2014-05-15 18:55 ` Doug Anderson
2014-05-15 20:12 ` Olof Johansson
2014-05-19 9:18 ` Lee Jones
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1397757570-19750-3-git-send-email-dianders@chromium.org \
--to=dianders@chromium.org \
--cc=abrestic@chromium.org \
--cc=dgreid@chromium.org \
--cc=lee.jones@linaro.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-samsung-soc@vger.kernel.org \
--cc=linux-tegra@vger.kernel.org \
--cc=olof@lixom.net \
--cc=sameo@linux.intel.com \
--cc=sjg@chromium.org \
--cc=swarren@nvidia.com \
--cc=wsa@the-dreams.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).