Received: by 10.223.176.46 with SMTP id f43csp3374093wra; Mon, 22 Jan 2018 13:05:13 -0800 (PST) X-Google-Smtp-Source: AH8x227QrHDnLhmFdgsaJ8DctB1WqZq/U062vlAuw8cYbM9H9Hib+HUtUTBz2BqFTy9tFcqd1rLN X-Received: by 10.36.250.196 with SMTP id v187mr242097ith.1.1516655113555; Mon, 22 Jan 2018 13:05:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516655113; cv=none; d=google.com; s=arc-20160816; b=GQZg3K1Zd+ns40lCbkV4BB9YRVO4SIXa/3Oa0X87wnN+4TjmQ0rg9XOmkmr3oC/em3 2yoNHpPMufY5ERFZLmFTau4vDRBed3ttre3Vrjj3KrWNad1CkOqaR51cvwZekH49bxxh 7R6Of7+htjhU6Z5KvWd2Bj6b4vEe8jd8xPs1tdXyh1sXvKZLOv3avRNNsue+aP/KyW1U 1/2CSJZMpIxQbcq/pxcAcw7DdsAL3iA7VrmvKnkb+dlM1DBF1BEvTuCL6jb1yIWgZ9Sz Ffi7lMgNx2sJeO1DAxBXwEjdi10v/0mp7CtDC9ywqNNaQb6OHVi3aAekLF6V8axMMSMT R5Yg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=3TsUU6zZHaPweAKfNEc8bOHokqkMczAxbWgpLeUmMPA=; b=H50UmIDSZFQIAPSJkkrrh/kGnfYlsLz3XRa3hbgrY6RAzNvzgSTg0dGAPDxG8j/Q0d 7pPKJkS8VNvF6x3vyhY63FMUpOWf4mr73UQWflLGkUcwYqhQcJ3imSqARvaiUouTerUk Fhn6VelFAKLJrCMvh7Ofo3Ri04hucFxxlEZcd2aMLWvKsmBfPr/32qlM0WVDClVlCQd3 llUjwPphXVTehJBJZJ+XKy9nmgzrLtiyrQ1NzLb85chu/0SV6BLIuX+ruP6yMvbjyPlR hMgaP7dYd7RMMkkJGXyAiWVQ1mnxTN6aAdFx0orr+ZuX9mkxq5Mt2H0WiACCpOYE35NX jzBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=H6gCu6XZ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n70si7121713ith.153.2018.01.22.13.05.00; Mon, 22 Jan 2018 13:05:13 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=H6gCu6XZ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751170AbeAVVDF (ORCPT + 99 others); Mon, 22 Jan 2018 16:03:05 -0500 Received: from mail-pf0-f194.google.com ([209.85.192.194]:44510 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751021AbeAVVDD (ORCPT ); Mon, 22 Jan 2018 16:03:03 -0500 Received: by mail-pf0-f194.google.com with SMTP id m26so8021467pfj.11; Mon, 22 Jan 2018 13:03:03 -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=3TsUU6zZHaPweAKfNEc8bOHokqkMczAxbWgpLeUmMPA=; b=H6gCu6XZtrekNg466/7fZiIT8OntDBFizQqZ5zgfHCiBn8n+c3NuEf4nM/gp2iGJ2y X3b/aWzS9aZyT8XJwgQlu9F5YcKOEOqtvNftpEKwMda3WPjD5+QcB7rHYVusSYbqcHHk 2+Gi30yKy7oUYJPZKhy9YXKo/2wDMjDMhdVcF+JRfYIZXoTxaNwpH9O7+AD22KeEqKKR s9IIzJfzaeunmiOU+LmTlzuFKPv+sJLLfFlgQmLUCMXxgqo91nIjJR1DgVC7hDfdVWYb Q4VGvl0svUsxZMy0Z9CIrg+hq3+uXiktIQqlSDUl6kb3TLHzJGqwUAvJ+GRDFGURW0tn 73Rg== 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=3TsUU6zZHaPweAKfNEc8bOHokqkMczAxbWgpLeUmMPA=; b=uaiYZC1L6JXxc7H6BKQ/aiBOg2J+4gu09YxJqFFYIeKTgCJywUQs3IIUpoJAOIC578 +Bw1GBlBeHWSsmpoRrBF50BC7+wasNNwph8VYlbabAY5tMbl30bt567CeAY+oCPV/aRd RWRtYTTDSJ9go7zP+wrlNZ5bdhPjQ9ceIYLe9eJ9cStgB8pkg4lkakf+ZlRzGn98r2d0 6Li7+JpkHIBuZf2dxVIHJCMfmB3y3UmBEDOB0dtHfdYtN9zDMgqD4CadjSsfmT3fd8k5 R9GI9p3/jMg6GMkMBlRTGFGhXUVTa/299QLm89sDsL2PYrQLRUJ7xcvyepyaFTI9Yp7B DLew== X-Gm-Message-State: AKwxytfaiJ+AvFrVvhy7jqocXn75AA5pHakNWiG8AUYzxwpnyk4WXbrC s+qivSMxEFpsVKm/5pCD30M= X-Received: by 2002:a17:902:781:: with SMTP id 1-v6mr4037892plj.411.1516654982622; Mon, 22 Jan 2018 13:03:02 -0800 (PST) Received: from shreeya-VirtualBox ([103.212.140.129]) by smtp.gmail.com with ESMTPSA id f72sm2749484pff.145.2018.01.22.13.03.00 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 22 Jan 2018 13:03:01 -0800 (PST) From: Shreeya Patel To: lars@metafoo.de, Michael.Hennerich@analog.com, jic23@kernel.org, knaack.h@gmx.de, pmeerw@pmeerw.net, gregkh@linuxfoundation.org, linux-iio@vger.kernel.org, devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org Cc: Shreeya Patel Subject: [PATCH] Staging: iio: ade7758: Expand buf_lock to cover both buffer Date: Tue, 23 Jan 2018 02:32:53 +0530 Message-Id: <1516654973-6434-1-git-send-email-shreeya.patel23498@gmail.com> X-Mailer: git-send-email 2.7.4 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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' __ade7758_spi_write_reg_8 and __ade7758_spi_read_reg_8 functions to be used by ade7758_write_samp_freq and ade7758_read_samp_freq which avoids nested locks and maintains atomicity between bus and device frequency changes. Signed-off-by: Shreeya Patel --- drivers/staging/iio/meter/ade7758.h | 2 +- drivers/staging/iio/meter/ade7758_core.c | 49 +++++++++++++++++++++++--------- 2 files changed, 37 insertions(+), 14 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..7f8f8c4 100644 --- a/drivers/staging/iio/meter/ade7758_core.c +++ b/drivers/staging/iio/meter/ade7758_core.c @@ -24,17 +24,26 @@ #include "meter.h" #include "ade7758.h" -int ade7758_spi_write_reg_8(struct device *dev, u8 reg_address, u8 val) +/* Unlocked version of ade7758_spi_write_reg_8 function */ +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 +100,8 @@ 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) +/* Unlocked version of ade7758_spi_read_reg_16 function */ +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 +121,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 +133,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; } @@ -470,7 +491,7 @@ static int ade7758_read_samp_freq(struct device *dev, int *val) int ret; u8 t; - ret = ade7758_spi_read_reg_8(dev, ADE7758_WAVMODE, &t); + ret = __ade7758_spi_read_reg_8(dev, ADE7758_WAVMODE, &t); if (ret) return ret; @@ -503,14 +524,14 @@ static int ade7758_write_samp_freq(struct device *dev, int val) goto out; } - ret = ade7758_spi_read_reg_8(dev, ADE7758_WAVMODE, ®); + 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: return ret; @@ -523,12 +544,13 @@ static int ade7758_read_raw(struct iio_dev *indio_dev, long mask) { int ret; + struct ade7758_state *st = iio_priv(indio_dev); switch (mask) { case IIO_CHAN_INFO_SAMP_FREQ: - mutex_lock(&indio_dev->mlock); + mutex_lock(&st->buf_lock); ret = ade7758_read_samp_freq(&indio_dev->dev, val); - mutex_unlock(&indio_dev->mlock); + mutex_unlock(&st->buf_lock); return ret; default: return -EINVAL; @@ -542,14 +564,15 @@ static int ade7758_write_raw(struct iio_dev *indio_dev, int val, int val2, long mask) { int ret; + struct ade7758_state *st = iio_priv(indio_dev); switch (mask) { case IIO_CHAN_INFO_SAMP_FREQ: if (val2) return -EINVAL; - mutex_lock(&indio_dev->mlock); + mutex_lock(&st->buf_lock); ret = ade7758_write_samp_freq(&indio_dev->dev, val); - mutex_unlock(&indio_dev->mlock); + mutex_unlock(&st->buf_lock); return ret; default: return -EINVAL; -- 2.7.4