From patchwork Tue Feb 6 17:01:57 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shreeya Patel X-Patchwork-Id: 10203543 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 04FB36020F for ; Tue, 6 Feb 2018 17:02:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DBBB5287EF for ; Tue, 6 Feb 2018 17:02:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CEC8028A51; Tue, 6 Feb 2018 17:02:13 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 451CD287EF for ; Tue, 6 Feb 2018 17:02:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752650AbeBFRCM (ORCPT ); Tue, 6 Feb 2018 12:02:12 -0500 Received: from mail-pg0-f68.google.com ([74.125.83.68]:42551 "EHLO mail-pg0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752644AbeBFRCH (ORCPT ); Tue, 6 Feb 2018 12:02:07 -0500 Received: by mail-pg0-f68.google.com with SMTP id m28so1323223pgc.9 for ; Tue, 06 Feb 2018 09:02:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=URJtbEaQw6b02gIJZWV2Q0luwikwOChT0rP01qDlo/g=; b=WH5S09Mko5LiIpfe/7rngxUU6xfo0Yp1ie09HqK7RyhJFPfySJ3cElcacaMCkxYK8G cEsJ3tDAsW7PNUF+HlUiDd5XzAyLWeRKWM39G1dWw1AsiXXZkr7EHm4SbbVX2lSypHxv MceVUXQlX0Bh38Xxc5E7UhIWRP5p2rJm2ci8t4QIKl36Ra4KU7FcycyRTQYCrx6Cbg00 hkhXMA57uk+NHvNak4eAumDJch51ZkymrVOYLkiOT8UMUB60yC4B0cwEQjDhrF4kNCTU L4LwX63/IERpYq+V2PYUrhBzHOfghB3S8CUKNH9JTihgOvts8LjmDMnVMjMTvCacXi4O 9MTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=URJtbEaQw6b02gIJZWV2Q0luwikwOChT0rP01qDlo/g=; b=sdP6qyrK+IaBOlvITuuK8BwbfXdvuQPbXtCfw0En/YS2dGQT6JUNzhEpcYTmT2J87D OKQQOoKBHXYJ8ttInQ+3NCnABpjSSv9KPsQNWXxev+XYQ0vdSkFjai8UFk9CXkWJTAo8 KPpbKd7DoodFm8Dy23vCtTdU3UeHZEihdTzgrrXRwdGkXUq4e613NZsAyRKuPeTee9QW ODoCLMEEH2upivTHivIFKhBo5I2nDD1WRC3beaFf3hcGAyWhIvkAvKKl63VjnvDOs4fj OuVvhiSxb8aPbfnsvQ3PJWPXIEDcXJ+oPxIKn3pd63TQFE7HYQpXFLt7PcwEVcLt4Q9Y XltA== X-Gm-Message-State: APf1xPBpVcMkNYofzy29rPiBXP/CFYAQ2cl8nvjyWcaTo9sgpKPN2+BE Eu/1p3bBOL6gN326Ljb3hOg= X-Google-Smtp-Source: AH8x2252dwKAxkVjpaW1DEfSlZvFC9dik5jC5JZMXha9x9pSWmdbj9jBCtHtr3LQl2oLdP4dPXTUZw== X-Received: by 10.101.100.8 with SMTP id a8mr2482913pgv.102.1517936526157; Tue, 06 Feb 2018 09:02:06 -0800 (PST) Received: from shreeya-VirtualBox ([103.212.140.128]) by smtp.gmail.com with ESMTPSA id b81sm13855514pfm.89.2018.02.06.09.02.04 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 06 Feb 2018 09:02:04 -0800 (PST) From: Shreeya Patel To: lars@metafoo.de, Michael.Hennerich@analog.com, jic23@kernel.org, knaack.h@gmx.de, pmeerw@pmeerw.net, linux-iio@vger.kernel.org, devel@driverdev.osuosl.org Cc: Shreeya Patel Subject: [PATCH v6] Staging: iio: ade7758: Expand buf_lock to cover both buffer and state protection Date: Tue, 6 Feb 2018 22:31:57 +0530 Message-Id: <1517936517-5223-1-git-send-email-shreeya.patel23498@gmail.com> X-Mailer: git-send-email 2.7.4 Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP iio_dev->mlock is to be used only by the IIO core for protecting device mode changes between INDIO_DIRECT and INDIO_BUFFER. This patch replaces the use of mlock with the already established buf_lock mutex. Introducing 'unlocked' forms of read and write registers. The read/write frequency functions now require buf_lock to be held. That's not obvious so avoid this but moving the locking inside the functions where it is then clear that they are taking the unlocked forms of the register read/write. It isn't readily apparent that write frequency function requires the locks to be taken, so move it inside the function to where it is required to protect. Also, the read raw does not require iio_dev->mlock for reads. It can run concurrently as resource protection is handled by buf_lock in read register. Signed-off-by: Shreeya Patel --- Changes in v2 -Add static keyword to newly introduced functions and remove some added comments which are not required. Changes in v3 -Remove some useless mlocks and send it as another patch. Also make the necessary change in the current patch associated with the new patch with commit id 88eba33. Make commit message more appropriate. Changes in v4 -Write frequency function do not require lock so move it inside the function to where it is required to protect. Changes in v5 -Remove goto statement and make the code to return -EINVAL there itself. Changes in v6 -Merge patch with commit id 88eba33 with this patch. Also make the changed function definition same as before. drivers/staging/iio/meter/ade7758.h | 2 +- drivers/staging/iio/meter/ade7758_core.c | 52 +++++++++++++++++++++++--------- 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/drivers/staging/iio/meter/ade7758.h b/drivers/staging/iio/meter/ade7758.h index 6ae78d8..2de81b5 100644 --- a/drivers/staging/iio/meter/ade7758.h +++ b/drivers/staging/iio/meter/ade7758.h @@ -111,7 +111,7 @@ * @trig: data ready trigger registered with iio * @tx: transmit buffer * @rx: receive buffer - * @buf_lock: mutex to protect tx and rx + * @buf_lock: mutex to protect tx, rx, read and write frequency **/ struct ade7758_state { struct spi_device *us; diff --git a/drivers/staging/iio/meter/ade7758_core.c b/drivers/staging/iio/meter/ade7758_core.c index 7b7ffe5..4e0dbf5 100644 --- a/drivers/staging/iio/meter/ade7758_core.c +++ b/drivers/staging/iio/meter/ade7758_core.c @@ -24,17 +24,25 @@ #include "meter.h" #include "ade7758.h" -int ade7758_spi_write_reg_8(struct device *dev, u8 reg_address, u8 val) +static int __ade7758_spi_write_reg_8(struct device *dev, u8 reg_address, u8 val) { - int ret; struct iio_dev *indio_dev = dev_to_iio_dev(dev); struct ade7758_state *st = iio_priv(indio_dev); - mutex_lock(&st->buf_lock); st->tx[0] = ADE7758_WRITE_REG(reg_address); st->tx[1] = val; - ret = spi_write(st->us, st->tx, 2); + return spi_write(st->us, st->tx, 2); +} + +int ade7758_spi_write_reg_8(struct device *dev, u8 reg_address, u8 val) +{ + int ret; + struct iio_dev *indio_dev = dev_to_iio_dev(dev); + struct ade7758_state *st = iio_priv(indio_dev); + + mutex_lock(&st->buf_lock); + ret = __ade7758_spi_write_reg_8(dev, reg_address, val); mutex_unlock(&st->buf_lock); return ret; @@ -91,7 +99,7 @@ static int ade7758_spi_write_reg_24(struct device *dev, u8 reg_address, return ret; } -int ade7758_spi_read_reg_8(struct device *dev, u8 reg_address, u8 *val) +static int __ade7758_spi_read_reg_8(struct device *dev, u8 reg_address, u8 *val) { struct iio_dev *indio_dev = dev_to_iio_dev(dev); struct ade7758_state *st = iio_priv(indio_dev); @@ -111,7 +119,6 @@ int ade7758_spi_read_reg_8(struct device *dev, u8 reg_address, u8 *val) }, }; - mutex_lock(&st->buf_lock); st->tx[0] = ADE7758_READ_REG(reg_address); st->tx[1] = 0; @@ -124,7 +131,19 @@ int ade7758_spi_read_reg_8(struct device *dev, u8 reg_address, u8 *val) *val = st->rx[0]; error_ret: + return ret; +} + +int ade7758_spi_read_reg_8(struct device *dev, u8 reg_address, u8 *val) +{ + struct iio_dev *indio_dev = dev_to_iio_dev(dev); + struct ade7758_state *st = iio_priv(indio_dev); + int ret; + + mutex_lock(&st->buf_lock); + ret = __ade7758_spi_read_reg_8(dev, reg_address, val); mutex_unlock(&st->buf_lock); + return ret; } @@ -484,6 +503,8 @@ static int ade7758_write_samp_freq(struct device *dev, int val) { int ret; u8 reg, t; + struct iio_dev *indio_dev = dev_to_iio_dev(dev); + struct ade7758_state *st = iio_priv(indio_dev); switch (val) { case 26040: @@ -499,20 +520,23 @@ static int ade7758_write_samp_freq(struct device *dev, int val) t = 3; break; default: - ret = -EINVAL; - goto out; + return -EINVAL; } - ret = ade7758_spi_read_reg_8(dev, ADE7758_WAVMODE, ®); + mutex_lock(&st->buf_lock); + + ret = __ade7758_spi_read_reg_8(dev, ADE7758_WAVMODE, ®); if (ret) goto out; reg &= ~(5 << 3); reg |= t << 5; - ret = ade7758_spi_write_reg_8(dev, ADE7758_WAVMODE, reg); + ret = __ade7758_spi_write_reg_8(dev, ADE7758_WAVMODE, reg); out: + mutex_unlock(&st->buf_lock); + return ret; } @@ -526,9 +550,9 @@ static int ade7758_read_raw(struct iio_dev *indio_dev, switch (mask) { case IIO_CHAN_INFO_SAMP_FREQ: - mutex_lock(&indio_dev->mlock); + ret = ade7758_read_samp_freq(&indio_dev->dev, val); - mutex_unlock(&indio_dev->mlock); + return ret; default: return -EINVAL; @@ -547,9 +571,9 @@ static int ade7758_write_raw(struct iio_dev *indio_dev, case IIO_CHAN_INFO_SAMP_FREQ: if (val2) return -EINVAL; - mutex_lock(&indio_dev->mlock); + ret = ade7758_write_samp_freq(&indio_dev->dev, val); - mutex_unlock(&indio_dev->mlock); + return ret; default: return -EINVAL;