From patchwork Mon Sep 18 22:52:40 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Katie Dunne X-Patchwork-Id: 9957737 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 5F82B601E9 for ; Mon, 18 Sep 2017 22:52:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 50C3328BD7 for ; Mon, 18 Sep 2017 22:52:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 459B528DC5; Mon, 18 Sep 2017 22:52:45 +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.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, 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 900C128DB8 for ; Mon, 18 Sep 2017 22:52:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751431AbdIRWwn (ORCPT ); Mon, 18 Sep 2017 18:52:43 -0400 Received: from mail-pf0-f194.google.com ([209.85.192.194]:34976 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750815AbdIRWwn (ORCPT ); Mon, 18 Sep 2017 18:52:43 -0400 Received: by mail-pf0-f194.google.com with SMTP id i23so747772pfi.2 for ; Mon, 18 Sep 2017 15:52:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mail-ccsf-edu.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :user-agent; bh=Cbj6+YmN6552rHtXdcWvHJ2VkWxVMIFe6CbCxu6whqM=; b=FUJcP9R69eeDOCFciApj7LDBhbjZsBx1dKsP8a1x/aoJeSKD16N5kZvtsFaKHW/IRD pCGdXYDHmQvuRoyiuFR9txYSJvqS0Tufsmtvr5NqHPfefMAY7re13H7cVlQPBLZAcCIu 5y9/i8HVG4jEuQUHi2hObgO+zS3L69UqYHUKHSk9DfIVhA3+VI9O4vAyfSd7UarGZ7ZQ vx6H9NC58PrWcw515r5ygsExaAc899LrJLPdsnahP1jM1sh02mv3eHVz4HHkFUV1doKL LxtYz6aKGSv0FSuRUhk372BgFx39WUqGfokcvAHQNa7nlJDM8Nnae6aOFeUpEvGgVHBI J8uQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:user-agent; bh=Cbj6+YmN6552rHtXdcWvHJ2VkWxVMIFe6CbCxu6whqM=; b=j7DX5uQ9biKjQsM5JMVzkzONHyyTTuFFHYz6WqP1YODSJTauV0YxL1efKR29jVg/xn 7VpaY2PWi/k72oV8b53wrRKv03vr5TBZRKnOmeTwyKT4YO0WnW/bccMNzr/nlM0n3iEE VYL4pTIDi1QKqGLQZapUKCRF0mMrxHj1uMZt/afM9SbeIvHGExPyenVLVEctFdB8sVmm GXjf0q9lLL8fz963x4acQm/bbaJ5bVR+3ams5F4IdsiHUvMez9uKJdVygWv6QnbWh7zH zhRf2nSYoYytBbK+JwApk8szj0m7UwyZHvUsaghA4EpqNOpMppN7eT+70oPIxYDza5q8 u7lw== X-Gm-Message-State: AHPjjUgtAb9ivgfnRpVTEuPjR3NR+Ea7wzpWs1RiXmEMqHfo0OwxG++5 zYPwjZ6IVrQbkKje X-Google-Smtp-Source: AOwi7QCOvNzWfQybGUzHRZN0QhZcy16RzhG8ZFV7tuyFkCbCPyGpmAtZhrCJe9821gXmPilZxngASQ== X-Received: by 10.98.166.137 with SMTP id r9mr135932pfl.86.1505775162639; Mon, 18 Sep 2017 15:52:42 -0700 (PDT) Received: from katie-Inspiron-5748 (c-69-181-34-251.hsd1.ca.comcast.net. [69.181.34.251]) by smtp.gmail.com with ESMTPSA id t25sm535540pfe.96.2017.09.18.15.52.41 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 18 Sep 2017 15:52:41 -0700 (PDT) Date: Mon, 18 Sep 2017 15:52:40 -0700 From: Katie Dunne To: outreachy-kernel@googlegroups.com Cc: jic23@kernel.org, knaack.h@gmx.de, lars@metafoo.de, linux-iio@vger.kernel.org, pmeerw@pmeerw.net, 21cnbao@gmail.com Subject: [PATCH] Staging: iio: meter: ade7759: update locking method for write frequency Message-ID: <20170918225240.GA3085@katie-Inspiron-5748> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.24 (2015-08-30) 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 an 'unlocked' spi_write_reg_16 function to be used by ade7759_write_frequency avoids nested locks and maintains atomicity between bus and device frequency changes. Based on the solution found in ade7754 patch here: https://marc.info/?l=linux-iio&m=149086659008991&w=2 Signed-off-by: Katie Dunne --- drivers/staging/iio/meter/ade7759.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/drivers/staging/iio/meter/ade7759.c b/drivers/staging/iio/meter/ade7759.c index 1691760..b52d4d7 100644 --- a/drivers/staging/iio/meter/ade7759.c +++ b/drivers/staging/iio/meter/ade7759.c @@ -60,7 +60,7 @@ /** * struct ade7759_state - device instance specific data * @us: actual spi_device - * @buf_lock: mutex to protect tx and rx + * @buf_lock: mutex to protect tx and rx and write frequency * @tx: transmit buffer * @rx: receive buffer **/ @@ -89,19 +89,30 @@ static int ade7759_spi_write_reg_8(struct device *dev, return ret; } -static int ade7759_spi_write_reg_16(struct device *dev, +/*Unlocked version of ade7759_spi_write_reg_16 function */ +static int __ade7759_spi_write_reg_16(struct device *dev, u8 reg_address, u16 value) { - int ret; struct iio_dev *indio_dev = dev_to_iio_dev(dev); struct ade7759_state *st = iio_priv(indio_dev); - mutex_lock(&st->buf_lock); st->tx[0] = ADE7759_WRITE_REG(reg_address); st->tx[1] = (value >> 8) & 0xFF; st->tx[2] = value & 0xFF; - ret = spi_write(st->us, st->tx, 3); + return spi_write(st->us, st->tx, 3); +} + +static int ade7759_spi_write_reg_16(struct device *dev, + u8 reg_address, + u16 value) +{ + int ret; + struct iio_dev *indio_dev = dev_to_iio_dev(dev); + struct ade7759_state *st = iio_priv(indio_dev); + + mutex_lock(&st->buf_lock); + ret = __ade7759_spi_write_reg_16(dev, reg_address, value); mutex_unlock(&st->buf_lock); return ret; @@ -429,7 +440,7 @@ static ssize_t ade7759_write_frequency(struct device *dev, if (!val) return -EINVAL; - mutex_lock(&indio_dev->mlock); + mutex_lock(&st->buf_lock); t = 27900 / val; if (t > 0) @@ -447,10 +458,10 @@ static ssize_t ade7759_write_frequency(struct device *dev, reg &= ~(3 << 13); reg |= t << 13; - ret = ade7759_spi_write_reg_16(dev, ADE7759_MODE, reg); + ret = __ade7759_spi_write_reg_16(dev, ADE7759_MODE, reg); out: - mutex_unlock(&indio_dev->mlock); + mutex_unlock(&st->buf_lock); return ret ? ret : len; }