From patchwork Fri Mar 23 14:26:06 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rodrigo Siqueira X-Patchwork-Id: 10304791 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 3F1DE60384 for ; Fri, 23 Mar 2018 14:26:15 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2EC9128D84 for ; Fri, 23 Mar 2018 14:26:15 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 235E028DE3; Fri, 23 Mar 2018 14:26:15 +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 A674B28D84 for ; Fri, 23 Mar 2018 14:26:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751551AbeCWO0N (ORCPT ); Fri, 23 Mar 2018 10:26:13 -0400 Received: from mail-qk0-f196.google.com ([209.85.220.196]:45394 "EHLO mail-qk0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751504AbeCWO0M (ORCPT ); Fri, 23 Mar 2018 10:26:12 -0400 Received: by mail-qk0-f196.google.com with SMTP id s9so13036290qke.12; Fri, 23 Mar 2018 07:26:12 -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=/btVlYTCSK6s/Q0cJqH/65H1HcP7LAyoaMqDXwBEMnY=; b=apqdGONTQtRqqOKkB2A2uPDQCGILuciHPL8fgS6i3OOl370Cr51f1c9WCNgMZa4biO Gz0KaU71+za1jooCxUIu53edRGtHC+jELOlaRbaPJpQtYn8Zlz0ZFhu8+WY4rHn05+mF APY69/d6Bal/ZiuEqe888VOVSeZF8qMAU93RfVL1XeERC/4gHqyhPnNdBcPuOs92nvIK 85pYDPT1pPG9wZCmroeuwmV9Qd2mZx4tfl3d+cWVC/8fPApE9KoyzESnEFIDOx1A/9p9 Wkmnms8dFllmD5p3RbtgFibSUod54WeCaOUkoo4KHjI+OEjga0byY2NqF24WMyscZI9W zCTg== 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=/btVlYTCSK6s/Q0cJqH/65H1HcP7LAyoaMqDXwBEMnY=; b=AYPnqtVjk9mImdS2YdfkufBUP/z4hIJHZH28WlGhjntPPzJAyvBFncINL8gcKyCrcx E7XJ4NXqg9c2Xsp6MFeFOkjfP2NtDF+mR/ylMCIQfhFpym2yqfwy3SOmL2QU4msm/12D pRcUl0Yye25SkWQU9CXZiAZW3NprKZE7bTrdJ4z9mPhqL6x+gT1zQ304BqGKmn+zN4K4 +8NBR6lprVXlK2liAeQWSXnm0OPC8PlK+o7G7tDf8oNzxyxZFwGWLEVYjAUqzYxsPBbd Zk3l1hegOy+iurZUkEbuqWwdkI9txU+/93FSFh+FcngNqDqErkHnpqHi/XyDZlskFcAj s6QQ== X-Gm-Message-State: AElRT7HD5QQbETCStAG+PgamqxnRnlI2dqeJkHmyCOZfJeeIAMoqXntn 8erUT8qjGzc3Fv3U79bGshc= X-Google-Smtp-Source: AG47ELsYp+APu8FOG/nbmocjTEkyM0yPdwzYP7pRMQPkBU/1M44GT1s5JqMoezNHvHNCbgWvs+ztbg== X-Received: by 10.55.9.211 with SMTP id 202mr40401522qkj.183.1521815172080; Fri, 23 Mar 2018 07:26:12 -0700 (PDT) Received: from smtp.gmail.com ([143.107.45.1]) by smtp.gmail.com with ESMTPSA id v57sm6619711qtv.48.2018.03.23.07.26.08 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 23 Mar 2018 07:26:11 -0700 (PDT) Date: Fri, 23 Mar 2018 11:26:06 -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 3/8] staging:iio:ade7854: Rework I2C write function Message-ID: 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 I2C has many code duplications and four different interfaces per data size. This patch introduces a single function that centralizes the main tasks. The central function inserted by this patch can easily replace all the four functions related to the data size. However, this patch does not remove any code signature for keeping the meter module work and make easier to review this patch. Signed-off-by: Rodrigo Siqueira --- Changes in v3: - Remove the use of defines that not improve the readability - Replace variable name "bytes" for "bits" drivers/staging/iio/meter/ade7854-i2c.c | 96 ++++++++++++++++----------------- 1 file changed, 46 insertions(+), 50 deletions(-) diff --git a/drivers/staging/iio/meter/ade7854-i2c.c b/drivers/staging/iio/meter/ade7854-i2c.c index 37c957482493..c9f46d26b752 100644 --- a/drivers/staging/iio/meter/ade7854-i2c.c +++ b/drivers/staging/iio/meter/ade7854-i2c.c @@ -15,86 +15,82 @@ #include #include "ade7854.h" -static int ade7854_i2c_write_reg_8(struct device *dev, - u16 reg_address, - u8 val) +static int ade7854_i2c_write_reg(struct device *dev, + u16 reg_address, + u32 val, + int bits) { int ret; + int count; struct iio_dev *indio_dev = dev_to_iio_dev(dev); struct ade7854_state *st = iio_priv(indio_dev); mutex_lock(&st->buf_lock); st->tx[0] = (reg_address >> 8) & 0xFF; st->tx[1] = reg_address & 0xFF; - st->tx[2] = val; - ret = i2c_master_send(st->i2c, st->tx, 3); + switch (bits) { + case 8: + st->tx[2] = val & 0xFF; + count = 3; + break; + case 16: + st->tx[2] = (val >> 8) & 0xFF; + st->tx[3] = val & 0xFF; + count = 4; + break; + case 24: + st->tx[2] = (val >> 16) & 0xFF; + st->tx[3] = (val >> 8) & 0xFF; + st->tx[4] = val & 0xFF; + count = 5; + break; + case 32: + st->tx[2] = (val >> 24) & 0xFF; + st->tx[3] = (val >> 16) & 0xFF; + st->tx[4] = (val >> 8) & 0xFF; + st->tx[5] = val & 0xFF; + count = 6; + break; + default: + ret = -EINVAL; + goto unlock; + } + + ret = i2c_master_send(st->i2c, st->tx, count); + +unlock: mutex_unlock(&st->buf_lock); return ret < 0 ? ret : 0; } +static int ade7854_i2c_write_reg_8(struct device *dev, + u16 reg_address, + u8 val) +{ + return ade7854_i2c_write_reg(dev, reg_address, val, 8); +} + static int ade7854_i2c_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); - - mutex_lock(&st->buf_lock); - st->tx[0] = (reg_address >> 8) & 0xFF; - st->tx[1] = reg_address & 0xFF; - st->tx[2] = (val >> 8) & 0xFF; - st->tx[3] = val & 0xFF; - - ret = i2c_master_send(st->i2c, st->tx, 4); - mutex_unlock(&st->buf_lock); - - return ret < 0 ? ret : 0; + return ade7854_i2c_write_reg(dev, reg_address, val, 16); } static int ade7854_i2c_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); - - mutex_lock(&st->buf_lock); - st->tx[0] = (reg_address >> 8) & 0xFF; - st->tx[1] = reg_address & 0xFF; - st->tx[2] = (val >> 16) & 0xFF; - st->tx[3] = (val >> 8) & 0xFF; - st->tx[4] = val & 0xFF; - - ret = i2c_master_send(st->i2c, st->tx, 5); - mutex_unlock(&st->buf_lock); - - return ret < 0 ? ret : 0; + return ade7854_i2c_write_reg(dev, reg_address, val, 24); } static int ade7854_i2c_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); - - mutex_lock(&st->buf_lock); - st->tx[0] = (reg_address >> 8) & 0xFF; - st->tx[1] = reg_address & 0xFF; - st->tx[2] = (val >> 24) & 0xFF; - st->tx[3] = (val >> 16) & 0xFF; - st->tx[4] = (val >> 8) & 0xFF; - st->tx[5] = val & 0xFF; - - ret = i2c_master_send(st->i2c, st->tx, 6); - mutex_unlock(&st->buf_lock); - - return ret < 0 ? ret : 0; + return ade7854_i2c_write_reg(dev, reg_address, val, 32); } static int ade7854_i2c_read_reg_8(struct device *dev,