From patchwork Fri Mar 16 22:49:24 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rodrigo Siqueira X-Patchwork-Id: 10290429 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 4768E602BD for ; Fri, 16 Mar 2018 22:49:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 36544290DA for ; Fri, 16 Mar 2018 22:49:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2AE37290DC; Fri, 16 Mar 2018 22:49:33 +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 A7AE9290DA for ; Fri, 16 Mar 2018 22:49:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751951AbeCPWtb (ORCPT ); Fri, 16 Mar 2018 18:49:31 -0400 Received: from mail-qk0-f194.google.com ([209.85.220.194]:37764 "EHLO mail-qk0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751348AbeCPWta (ORCPT ); Fri, 16 Mar 2018 18:49:30 -0400 Received: by mail-qk0-f194.google.com with SMTP id y137so12743640qka.4; Fri, 16 Mar 2018 15:49:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=1FasD6I2P9z550Nu9T95Btfij/5DSo6k+FHWo8GCjSc=; b=J7ngZpcitIKrTIbrH41m2pA+f5f95vuXWfk9SD/1AX2+G7TRWR6+dWMXANohyF16RT T8WAvBGnLOwVLK6KF92xM7Gtfaq9lY0NLGLdJ90DgEmWCjxiKzBFzMKQ0/3HubFfzBAw h2E6jsSQapSyD7iXCLrDdvIxj+jRagz9vOj4DwNK0ytbSTZIiZD6CxRLMk2iP/oFA6Uy Mc5zgJXpsU44gWSo2LEtBex8OaOm7Ueu0S9nNZrSRFFaUsOvaZs1N5auJV8yQ5Kk/jav Bp+XNfIvAee9znSH08Kk1DhoawvOVMDDV9HZP/ejSn3QVmaSCssSyqyKTHulJq3xZ/OY Cfxw== 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:references :mime-version:content-disposition:in-reply-to:user-agent; bh=1FasD6I2P9z550Nu9T95Btfij/5DSo6k+FHWo8GCjSc=; b=ajknwiq0J8fWer3XOwrJVwAkIg6SCfESmAr+txUe90jXEzyrPTZavqJagVw13eF7aA 0O7WP8NeJaOXsDwPkvIxEqKbnAoLkniU/LF1L8O3GNq/0jd4Hn52Kfg/kpSGwPRA1ZyE 0tnynsfR+6goLTLpL+6rv0iZk4zLqm+7QtrNo8rDP29R2T+V0M1yLatUXnq0k7cr6vwi mwDeSsXhhVogvJJtVQmM+Vj3ssTIljUsPiLU65g88XgrHqZsQVjwFarh3y6D66YqQoJo 4dbixwKSdMhdCW0LWmejA0oCZLQms0KXfxP1cv6bVPrpCQTVJcVDLNAEXxZtl7r8UhHQ 4ooQ== X-Gm-Message-State: AElRT7Fy9MfTVHJMUVk8x6HrKJ732cAOjb8mx5qiH0XCsNUvhR+fvd8S EA7+DDr4pNBjozFJb0cj+/8= X-Google-Smtp-Source: AG47ELsFN5tTN3nXG/9DY/YBznd+cof6DBTnTOe/OjwLHHpt5H/2oAwixraxiFYPuGDaIYGPatq7pg== X-Received: by 10.55.95.133 with SMTP id t127mr5162216qkb.273.1521240569903; Fri, 16 Mar 2018 15:49:29 -0700 (PDT) Received: from smtp.gmail.com ([143.107.45.1]) by smtp.gmail.com with ESMTPSA id c5sm5754508qkf.93.2018.03.16.15.49.26 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 16 Mar 2018 15:49:29 -0700 (PDT) Date: Fri, 16 Mar 2018 19:49:24 -0300 From: Rodrigo Siqueira To: John Syne , Jonathan Cameron , Hartmut Knaack , Lars-Peter Clausen , Peter Meerwald-Stadler , Greg Kroah-Hartman , linux-iio@vger.kernel.org, devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org, Barry Song <21cnbao@gmail.com> Cc: daniel.baluta@nxp.com, linux-iio@vger.kernel.org, devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 4/8] staging:iio:ade7854: Rework SPI write function Message-ID: <66ce9378178c994eb64d19ba3cccc5dbfc64e0ef.1521239766.git.rodrigosiqueiramelo@gmail.com> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20180223 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 The write operation using SPI has a many code duplications (similar to I2C) and four different interfaces per data size. This patch introduces a single function that centralizes the main task related to SPI. Signed-off-by: Rodrigo Siqueira --- drivers/staging/iio/meter/ade7854-spi.c | 108 ++++++++++++-------------------- 1 file changed, 41 insertions(+), 67 deletions(-) diff --git a/drivers/staging/iio/meter/ade7854-spi.c b/drivers/staging/iio/meter/ade7854-spi.c index 4419b8f06197..f21dc24194fb 100644 --- a/drivers/staging/iio/meter/ade7854-spi.c +++ b/drivers/staging/iio/meter/ade7854-spi.c @@ -15,9 +15,10 @@ #include #include "ade7854.h" -static int ade7854_spi_write_reg_8(struct device *dev, - u16 reg_address, - u8 val) +static int ade7854_spi_write_reg(struct device *dev, + u16 reg_address, + u32 val, + int bytes) { int ret; struct iio_dev *indio_dev = dev_to_iio_dev(dev); @@ -32,93 +33,66 @@ static int ade7854_spi_write_reg_8(struct device *dev, st->tx[0] = ADE7854_WRITE_REG; st->tx[1] = (reg_address >> 8) & 0xFF; st->tx[2] = reg_address & 0xFF; - st->tx[3] = val & 0xFF; + switch (bytes) { + case DATA_SIZE_8_BITS: + st->tx[3] = val & 0xFF; + break; + case DATA_SIZE_16_BITS: + xfer.len = 5; + st->tx[3] = (val >> 8) & 0xFF; + st->tx[4] = val & 0xFF; + break; + case DATA_SIZE_24_BITS: + xfer.len = 6; + st->tx[3] = (val >> 16) & 0xFF; + st->tx[4] = (val >> 8) & 0xFF; + st->tx[5] = val & 0xFF; + break; + case DATA_SIZE_32_BITS: + xfer.len = 7; + st->tx[3] = (val >> 24) & 0xFF; + st->tx[4] = (val >> 16) & 0xFF; + st->tx[5] = (val >> 8) & 0xFF; + st->tx[6] = val & 0xFF; + break; + default: + ret = -EINVAL; + goto unlock; + } ret = spi_sync_transfer(st->spi, &xfer, 1); +unlock: mutex_unlock(&st->buf_lock); return ret; } +static int ade7854_spi_write_reg_8(struct device *dev, + u16 reg_address, + u8 val) +{ + return ade7854_spi_write_reg(dev, reg_address, val, DATA_SIZE_8_BITS); +} + static int ade7854_spi_write_reg_16(struct device *dev, u16 reg_address, u16 val) { - int ret; - struct iio_dev *indio_dev = dev_to_iio_dev(dev); - struct ade7854_state *st = iio_priv(indio_dev); - struct spi_transfer xfer = { - .tx_buf = st->tx, - .bits_per_word = 8, - .len = 5, - }; - - mutex_lock(&st->buf_lock); - st->tx[0] = ADE7854_WRITE_REG; - st->tx[1] = (reg_address >> 8) & 0xFF; - st->tx[2] = reg_address & 0xFF; - st->tx[3] = (val >> 8) & 0xFF; - st->tx[4] = val & 0xFF; - - ret = spi_sync_transfer(st->spi, &xfer, 1); - mutex_unlock(&st->buf_lock); - - return ret; + return ade7854_spi_write_reg(dev, reg_address, val, DATA_SIZE_16_BITS); } static int ade7854_spi_write_reg_24(struct device *dev, u16 reg_address, u32 val) { - int ret; - struct iio_dev *indio_dev = dev_to_iio_dev(dev); - struct ade7854_state *st = iio_priv(indio_dev); - struct spi_transfer xfer = { - .tx_buf = st->tx, - .bits_per_word = 8, - .len = 6, - }; - - mutex_lock(&st->buf_lock); - st->tx[0] = ADE7854_WRITE_REG; - st->tx[1] = (reg_address >> 8) & 0xFF; - st->tx[2] = reg_address & 0xFF; - st->tx[3] = (val >> 16) & 0xFF; - st->tx[4] = (val >> 8) & 0xFF; - st->tx[5] = val & 0xFF; - - ret = spi_sync_transfer(st->spi, &xfer, 1); - mutex_unlock(&st->buf_lock); - - return ret; + return ade7854_spi_write_reg(dev, reg_address, val, DATA_SIZE_24_BITS); } static int ade7854_spi_write_reg_32(struct device *dev, u16 reg_address, u32 val) { - int ret; - struct iio_dev *indio_dev = dev_to_iio_dev(dev); - struct ade7854_state *st = iio_priv(indio_dev); - struct spi_transfer xfer = { - .tx_buf = st->tx, - .bits_per_word = 8, - .len = 7, - }; - - mutex_lock(&st->buf_lock); - st->tx[0] = ADE7854_WRITE_REG; - st->tx[1] = (reg_address >> 8) & 0xFF; - st->tx[2] = reg_address & 0xFF; - st->tx[3] = (val >> 24) & 0xFF; - st->tx[4] = (val >> 16) & 0xFF; - st->tx[5] = (val >> 8) & 0xFF; - st->tx[6] = val & 0xFF; - - ret = spi_sync_transfer(st->spi, &xfer, 1); - mutex_unlock(&st->buf_lock); - - return ret; + return ade7854_spi_write_reg(dev, reg_address, val, DATA_SIZE_32_BITS); } static int ade7854_spi_read_reg_8(struct device *dev,