From patchwork Sun Mar 4 01:49:33 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brian Masney X-Patchwork-Id: 10257501 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 2357E6037F for ; Sun, 4 Mar 2018 01:54:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 06073287A1 for ; Sun, 4 Mar 2018 01:54:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EB98B287A8; Sun, 4 Mar 2018 01:54:56 +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_SIGNED, 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 4D89E287A1 for ; Sun, 4 Mar 2018 01:54:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751819AbeCDByz (ORCPT ); Sat, 3 Mar 2018 20:54:55 -0500 Received: from onstation.org ([52.200.56.107]:52846 "EHLO onstation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751340AbeCDByy (ORCPT ); Sat, 3 Mar 2018 20:54:54 -0500 X-Greylist: delayed 302 seconds by postgrey-1.27 at vger.kernel.org; Sat, 03 Mar 2018 20:54:53 EST Received: from xilitla.hsd1.wv.comcast.net (c-98-236-77-125.hsd1.wv.comcast.net [98.236.77.125]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: masneyb) by onstation.org (Postfix) with ESMTPSA id 9E49B1AE4; Sun, 4 Mar 2018 01:49:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=onstation.org; s=default; t=1520128191; bh=iDUcOMfAcJUdTSCXTaUpXFbArUPXedUJ1zpZjzTm8v8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=O8wCEJn3QBey2sXWLhZmIyTkyZtRjAKMqGKAS4wTwl27T9bTiXbE8vXWyuAQotIRr jcOgPJEHuue9apLhkr+pkDOSJPYE77YBew5VElOZjUo2t2+4uwiBLq3Hgar6GligR7 US7HqYfYL5rbt/N/v2qkxZqIghV9OZs1zgqUgcqg= From: Brian Masney To: jic23@kernel.org, linux-iio@vger.kernel.org Cc: gregkh@linuxfoundation.org, devel@driverdev.osuosl.org, knaack.h@gmx.de, lars@metafoo.de, pmeerw@pmeerw.net, linux-kernel@vger.kernel.org, Jon.Brenner@ams.com Subject: [PATCH 03/12] staging: iio: tsl2x7x: add common function for reading chip status Date: Sat, 3 Mar 2018 20:49:33 -0500 Message-Id: <20180304014942.18727-4-masneyb@onstation.org> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180304014942.18727-1-masneyb@onstation.org> References: <20180304014942.18727-1-masneyb@onstation.org> 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 There were three places where the same chunk of code was used to read the chip status. This patch creates a common function tsl2x7x_read_status() to reduce duplicate code. This patch also corrects tsl2x7x_event_handler() to properly check for an error after reading the chip status. Signed-off-by: Brian Masney --- drivers/staging/iio/light/tsl2x7x.c | 40 ++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/drivers/staging/iio/light/tsl2x7x.c b/drivers/staging/iio/light/tsl2x7x.c index c02db03ef369..6bb622816660 100644 --- a/drivers/staging/iio/light/tsl2x7x.c +++ b/drivers/staging/iio/light/tsl2x7x.c @@ -293,6 +293,20 @@ static int tsl2x7x_clear_interrupts(struct tsl2X7X_chip *chip, int reg) return ret; } +static int tsl2x7x_read_status(struct tsl2X7X_chip *chip) +{ + int ret; + + ret = i2c_smbus_read_byte_data(chip->client, + TSL2X7X_CMD_REG | TSL2X7X_STATUS); + if (ret < 0) + dev_err(&chip->client->dev, + "%s: failed to read STATUS register: %d\n", __func__, + ret); + + return ret; +} + /** * tsl2x7x_get_lux() - Reads and calculates current lux value. * @indio_dev: pointer to IIO device @@ -332,13 +346,10 @@ static int tsl2x7x_get_lux(struct iio_dev *indio_dev) goto out_unlock; } - ret = i2c_smbus_read_byte_data(chip->client, - TSL2X7X_CMD_REG | TSL2X7X_STATUS); - if (ret < 0) { - dev_err(&chip->client->dev, - "%s: Failed to read STATUS Reg\n", __func__); + ret = tsl2x7x_read_status(chip); + if (ret < 0) goto out_unlock; - } + /* is data new & valid */ if (!(ret & TSL2X7X_STA_ADC_VALID)) { dev_err(&chip->client->dev, @@ -458,12 +469,9 @@ static int tsl2x7x_get_prox(struct iio_dev *indio_dev) return -EBUSY; } - ret = i2c_smbus_read_byte_data(chip->client, - TSL2X7X_CMD_REG | TSL2X7X_STATUS); - if (ret < 0) { - dev_err(&chip->client->dev, "i2c err=%d\n", ret); + ret = tsl2x7x_read_status(chip); + if (ret < 0) goto prox_poll_err; - } switch (chip->id) { case tsl2571: @@ -1396,13 +1404,13 @@ static irqreturn_t tsl2x7x_event_handler(int irq, void *private) struct tsl2X7X_chip *chip = iio_priv(indio_dev); s64 timestamp = iio_get_time_ns(indio_dev); int ret; - u8 value; - value = i2c_smbus_read_byte_data(chip->client, - TSL2X7X_CMD_REG | TSL2X7X_STATUS); + ret = tsl2x7x_read_status(chip); + if (ret < 0) + return ret; /* What type of interrupt do we need to process */ - if (value & TSL2X7X_STA_PRX_INTR) { + if (ret & TSL2X7X_STA_PRX_INTR) { tsl2x7x_get_prox(indio_dev); /* freshen data for ABI */ iio_push_event(indio_dev, IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, @@ -1412,7 +1420,7 @@ static irqreturn_t tsl2x7x_event_handler(int irq, void *private) timestamp); } - if (value & TSL2X7X_STA_ALS_INTR) { + if (ret & TSL2X7X_STA_ALS_INTR) { tsl2x7x_get_lux(indio_dev); /* freshen data for ABI */ iio_push_event(indio_dev, IIO_UNMOD_EVENT_CODE(IIO_LIGHT,