From patchwork Fri Mar 23 14:27:12 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rodrigo Siqueira X-Patchwork-Id: 10304807 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 973CA60386 for ; Fri, 23 Mar 2018 14:27:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8641228D84 for ; Fri, 23 Mar 2018 14:27:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7A92028DE3; Fri, 23 Mar 2018 14:27:20 +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 F0A8E28D84 for ; Fri, 23 Mar 2018 14:27:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751840AbeCWO1T (ORCPT ); Fri, 23 Mar 2018 10:27:19 -0400 Received: from mail-qt0-f193.google.com ([209.85.216.193]:42839 "EHLO mail-qt0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751388AbeCWO1S (ORCPT ); Fri, 23 Mar 2018 10:27:18 -0400 Received: by mail-qt0-f193.google.com with SMTP id j3so1217473qtn.9; Fri, 23 Mar 2018 07:27:17 -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=g3u3/ZUAgfZendeqJ/3byeGGrhxUr4ZrJPcIzDE6sF4=; b=J5owWWiqkW5pyQBcLPAmk3z6Fw0QRwPikXA/fIkdZJUErRg2v7+mXi7pr9UyipqDW5 O5TfLVM/zj8hf1J0RGitUySKkXRT6dJOHl6PdHVYvtKGVJ+LtN/kKNRjkD+uQyjAQe/6 WJ9NAGk6gE6FVVEd49r6JnpFN4wqb8ZWBiCTQAnLuq7c2VuyXGP3T5dRB4mfx0zShY39 Lp4Z0+3WgT6gZQuuqrfstls8j4oUFf3jcva4GoibjfufKl95Lrzv9P1CvHknHgkVZJdg 302vwGihqadiMecJUfgYKFcmCCj4GaJs0hS1RAWkggZ0ismqn6LUfYGJSiaUE2Cozf4f GhEg== 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=g3u3/ZUAgfZendeqJ/3byeGGrhxUr4ZrJPcIzDE6sF4=; b=evRHyG2sew16IGZB/GmthC4BXhl+1VgoycIN/KV/MjnYfgY/WxiAsY9lGwmcwUxERt ppVFroRiMwtjcLxi2/Z8DpNG3jB3BsfHaWnq/UVuSbJBU5zkQHnfD9XHK9/ciPS0x5Mi R5/438tmFcPKDiMu4754hORSgsefoV+KIyzkrUfwN0+EZ3r2lbF3Y8g147p2YcpOBPiq J++ACIt3r5NyzZUY9kxie+46OC6EMA1aFcyl0xCqRv2lXjusb5B7IvYQpMLbCz9Fog6h qoi2p+b7eRwhxIExwKc72QPR4n6VaSXtIEceCRp18efNcsavJhLS/snmWyX5TITEjxCv nbMg== X-Gm-Message-State: AElRT7GgUdV42odjVYkgVP/CT4ONCrLgn8MjEuhlcJHIyyRzfGW6MeDi sfyMIC5+L8I18fFQmf0hqXM= X-Google-Smtp-Source: AG47ELvY1zdXGIEU8H+ldj90C1t9r0qAGZXP0DoOkqaeU2kCrVAEc/UGZtPZWX0Te6zGLcaZI2ggBg== X-Received: by 10.200.38.7 with SMTP id u7mr26744504qtu.269.1521815237174; Fri, 23 Mar 2018 07:27:17 -0700 (PDT) Received: from smtp.gmail.com ([143.107.45.1]) by smtp.gmail.com with ESMTPSA id r4sm6857131qki.0.2018.03.23.07.27.13 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 23 Mar 2018 07:27:16 -0700 (PDT) Date: Fri, 23 Mar 2018 11:27:12 -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 v3 7/8] staging:iio:ade7854: Rework SPI read function Message-ID: <960df20b2a51e6517e9ccef2b5486ee116f35ba4.1521813782.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 Rework read SPI function to reduce the code duplication and centralizes all the task in a single function. Signed-off-by: Rodrigo Siqueira --- Changes in v3: - Remove the use of defines that not improve the readability - Replace variable name "bytes" for "bits" - Update commit message drivers/staging/iio/meter/ade7854-spi.c | 136 ++++++++------------------------ 1 file changed, 33 insertions(+), 103 deletions(-) diff --git a/drivers/staging/iio/meter/ade7854-spi.c b/drivers/staging/iio/meter/ade7854-spi.c index be7397042850..ee6e4d166ece 100644 --- a/drivers/staging/iio/meter/ade7854-spi.c +++ b/drivers/staging/iio/meter/ade7854-spi.c @@ -67,9 +67,10 @@ static int ade7854_spi_write_reg(struct device *dev, return ret; } -static int ade7854_spi_read_reg_8(struct device *dev, - u16 reg_address, - u8 *val) +static int ade7854_spi_read_reg(struct device *dev, + u16 reg_address, + u32 *val, + int bits) { struct iio_dev *indio_dev = dev_to_iio_dev(dev); struct ade7854_state *st = iio_priv(indio_dev); @@ -82,7 +83,7 @@ static int ade7854_spi_read_reg_8(struct device *dev, }, { .rx_buf = st->rx, .bits_per_word = 8, - .len = 1, + .len = bits, } }; @@ -94,128 +95,57 @@ static int ade7854_spi_read_reg_8(struct device *dev, ret = spi_sync_transfer(st->spi, xfers, ARRAY_SIZE(xfers)); if (ret) { - dev_err(&st->spi->dev, "problem when reading 8 bit register 0x%02X", + dev_err(&st->spi->dev, "problem when reading register 0x%02X", reg_address); - goto error_ret; + goto unlock; + } + + switch (bits) { + case 8: + *val = st->rx[0]; + break; + case 16: + *val = be16_to_cpup((const __be16 *)st->rx); + break; + case 24: + *val = (st->rx[0] << 16) | (st->rx[1] << 8) | st->rx[2]; + break; + case 32: + *val = be32_to_cpup((const __be32 *)st->rx); + break; } - *val = st->rx[0]; -error_ret: +unlock: mutex_unlock(&st->buf_lock); return ret; } +static int ade7854_spi_read_reg_8(struct device *dev, + u16 reg_address, + u8 *val) +{ + return ade7854_spi_read_reg(dev, reg_address, (u32 *)val, 8); +} + static int ade7854_spi_read_reg_16(struct device *dev, u16 reg_address, u16 *val) { - struct iio_dev *indio_dev = dev_to_iio_dev(dev); - struct ade7854_state *st = iio_priv(indio_dev); - int ret; - struct spi_transfer xfers[] = { - { - .tx_buf = st->tx, - .bits_per_word = 8, - .len = 3, - }, { - .rx_buf = st->rx, - .bits_per_word = 8, - .len = 2, - } - }; - - mutex_lock(&st->buf_lock); - st->tx[0] = ADE7854_READ_REG; - st->tx[1] = (reg_address >> 8) & 0xFF; - st->tx[2] = reg_address & 0xFF; - - ret = spi_sync_transfer(st->spi, xfers, ARRAY_SIZE(xfers)); - if (ret) { - dev_err(&st->spi->dev, "problem when reading 16 bit register 0x%02X", - reg_address); - goto error_ret; - } - *val = be16_to_cpup((const __be16 *)st->rx); - -error_ret: - mutex_unlock(&st->buf_lock); - return ret; + return ade7854_spi_read_reg(dev, reg_address, (u32 *)val, 16); } static int ade7854_spi_read_reg_24(struct device *dev, u16 reg_address, u32 *val) { - struct iio_dev *indio_dev = dev_to_iio_dev(dev); - struct ade7854_state *st = iio_priv(indio_dev); - int ret; - struct spi_transfer xfers[] = { - { - .tx_buf = st->tx, - .bits_per_word = 8, - .len = 3, - }, { - .rx_buf = st->rx, - .bits_per_word = 8, - .len = 3, - } - }; - - mutex_lock(&st->buf_lock); - - st->tx[0] = ADE7854_READ_REG; - st->tx[1] = (reg_address >> 8) & 0xFF; - st->tx[2] = reg_address & 0xFF; - - ret = spi_sync_transfer(st->spi, xfers, ARRAY_SIZE(xfers)); - if (ret) { - dev_err(&st->spi->dev, "problem when reading 24 bit register 0x%02X", - reg_address); - goto error_ret; - } - *val = (st->rx[0] << 16) | (st->rx[1] << 8) | st->rx[2]; - -error_ret: - mutex_unlock(&st->buf_lock); - return ret; + return ade7854_spi_read_reg(dev, reg_address, (u32 *)val, 24); } static int ade7854_spi_read_reg_32(struct device *dev, u16 reg_address, u32 *val) { - struct iio_dev *indio_dev = dev_to_iio_dev(dev); - struct ade7854_state *st = iio_priv(indio_dev); - int ret; - struct spi_transfer xfers[] = { - { - .tx_buf = st->tx, - .bits_per_word = 8, - .len = 3, - }, { - .rx_buf = st->rx, - .bits_per_word = 8, - .len = 4, - } - }; - - mutex_lock(&st->buf_lock); - - st->tx[0] = ADE7854_READ_REG; - st->tx[1] = (reg_address >> 8) & 0xFF; - st->tx[2] = reg_address & 0xFF; - - ret = spi_sync_transfer(st->spi, xfers, ARRAY_SIZE(xfers)); - if (ret) { - dev_err(&st->spi->dev, "problem when reading 32 bit register 0x%02X", - reg_address); - goto error_ret; - } - *val = be32_to_cpup((const __be32 *)st->rx); - -error_ret: - mutex_unlock(&st->buf_lock); - return ret; + return ade7854_spi_read_reg(dev, reg_address, (u32 *)val, 32); } static int ade7854_spi_probe(struct spi_device *spi)