From patchwork Tue Apr 22 16:45:49 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Doug Anderson X-Patchwork-Id: 4033791 Return-Path: X-Original-To: patchwork-linux-samsung-soc@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id E4D76BFF02 for ; Tue, 22 Apr 2014 16:49:23 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 9BEA2201C8 for ; Tue, 22 Apr 2014 16:49:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 245D5201E7 for ; Tue, 22 Apr 2014 16:49:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933335AbaDVQry (ORCPT ); Tue, 22 Apr 2014 12:47:54 -0400 Received: from mail-vc0-f202.google.com ([209.85.220.202]:38055 "EHLO mail-vc0-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933280AbaDVQqW (ORCPT ); Tue, 22 Apr 2014 12:46:22 -0400 Received: by mail-vc0-f202.google.com with SMTP id lh14so370150vcb.1 for ; Tue, 22 Apr 2014 09:46:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=EXRPqpMGeYB+Sxz2n1NIq2bVHTLhUncAEO3h4+HbW00=; b=cSy+K/C99uYJCXzEKY102lGATKfSGSw6uYHuLyEcMQjYqfNLCq27LQqnY7FCAlPPFR foTYJzw1cqALDxHajgR7p0zLlSGyB2NdHnfihf4q8P1/yb3o6SXHpBjis595brJpUpxl rsQgBzicEhshhMKgSDNCM/fAn9qCf2LIBaLKoyaSPMgu7bECQLwMMRnrgTqvcMzDSnGV wa6d0yscuUyF5mR0fy8vtjJt/JbY8qB1UTpo+Zsv8tzjWtVJh6V3Hlf6obJ9yynkGuqz hiOsSrpuaZ5lqwWeuATsm/CUlRaVAYDWRiG7mHcbNPRS+KvepE71TwnXEPN1FMGaV2Sm 4kiQ== X-Gm-Message-State: ALoCoQk8pNnSq+drvTTNbmMAgNmGpB56rEYHpic84k2/cFjzMC6vD63w3Cgf0F4qGb1is5gLkZqoX/c4i4tCBJGD2mjkC9pNxItq7RWR6RFydT2mWU29lwrPgLUrVFTbcor1fgFqr4f7OOAe7/pVwVe2R1zm22Z3DqwqsT0YtyULZrJJlli6fSjWKfWYEA3aSH/VcIKsowq+PiKx7tmSWOiXINAGoHTy7w== X-Received: by 10.58.46.240 with SMTP id y16mr22057137vem.2.1398185180812; Tue, 22 Apr 2014 09:46:20 -0700 (PDT) Received: from corp2gmr1-1.hot.corp.google.com (corp2gmr1-1.hot.corp.google.com [172.24.189.92]) by gmr-mx.google.com with ESMTPS id g21si5666013yhe.3.2014.04.22.09.46.20 for (version=TLSv1.1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 22 Apr 2014 09:46:20 -0700 (PDT) Received: from tictac.mtv.corp.google.com (tictac.mtv.corp.google.com [172.22.72.141]) by corp2gmr1-1.hot.corp.google.com (Postfix) with ESMTP id 97D3931C269; Tue, 22 Apr 2014 09:46:20 -0700 (PDT) Received: by tictac.mtv.corp.google.com (Postfix, from userid 121310) id 4132580EFE; Tue, 22 Apr 2014 09:46:20 -0700 (PDT) From: Doug Anderson To: lee.jones@linaro.org, swarren@nvidia.com, wsa@the-dreams.de Cc: abrestic@chromium.org, dgreid@chromium.org, olof@lixom.net, sjg@chromium.org, linux-samsung-soc@vger.kernel.org, linux-tegra@vger.kernel.org, Doug Anderson , sameo@linux.intel.com, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/7] mfd: cros_ec: spi: Add mutex to cros_ec_spi Date: Tue, 22 Apr 2014 09:45:49 -0700 Message-Id: <1398185154-19404-3-git-send-email-dianders@chromium.org> X-Mailer: git-send-email 1.9.1.423.g4596e3a In-Reply-To: <1398185154-19404-1-git-send-email-dianders@chromium.org> References: <1398185154-19404-1-git-send-email-dianders@chromium.org> Sender: linux-samsung-soc-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-samsung-soc@vger.kernel.org X-Spam-Status: No, score=-7.5 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 Reviewed-by: Simon Glass Tested-by: Andrew Bresticker Tested-by: Stephen Warren --- Changes in v2: None 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;