From patchwork Wed Mar 21 10:29:05 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brian Masney X-Patchwork-Id: 10299095 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 67C8B602B3 for ; Wed, 21 Mar 2018 10:31:07 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 54E7729894 for ; Wed, 21 Mar 2018 10:31:07 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 52BE629937; Wed, 21 Mar 2018 10:31:07 +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 599642982E for ; Wed, 21 Mar 2018 10:30:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751668AbeCUKau (ORCPT ); Wed, 21 Mar 2018 06:30:50 -0400 Received: from onstation.org ([52.200.56.107]:47294 "EHLO onstation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751609AbeCUK30 (ORCPT ); Wed, 21 Mar 2018 06:29:26 -0400 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 D1E0564C; Wed, 21 Mar 2018 10:29:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=onstation.org; s=default; t=1521628165; bh=dSEmNpxkhIitvwEcJsJ5bJveXHJ+O2kod0RNhqzYTWA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AEK2Dq62p3jqCkcjFRQAsjxutdQRbPERA/XXitke2S0URhn7q8lS9IiynLXw6tSLk bOLLHelPGCQPtO5Vm9bsbr0Da2O/gAKAwVBxI+K8x+ikvBbrUfq9NvHgQEXQpOP7j0 +u/yk6WXMJ9ND897tJPv33l/8s0rEicQ60afglGQ= 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 04/11] staging: iio: tsl2x7x: simplify tsl2x7x_prox_cal() Date: Wed, 21 Mar 2018 06:29:05 -0400 Message-Id: <20180321102912.5130-5-masneyb@onstation.org> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180321102912.5130-1-masneyb@onstation.org> References: <20180321102912.5130-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 tsl2x7x_prox_cal() would set the interrupt flag, and reset the device to start doing the calibration routine. However, this did not actually affect the readings since they are polled. This patch drops the interrupt code. This patch also drops the function tsl2x7x_prox_calculate() and removes support for the standard deviation and min sample since those values were not used. Driver was tested using a TSL2772 hooked up to a Raspberry Pi 2. I performed the following testing at various distances: - Put hand in front of sensor and keep the sensor and hand stationary. - Perform calibration routine. - Run iio_event_monitor. - Verify that a proximity event is triggered when my hand comes anywhere between the sensor and where I performed the calibration routine. Signed-off-by: Brian Masney --- drivers/staging/iio/light/tsl2x7x.c | 107 +++++------------------------------- 1 file changed, 15 insertions(+), 92 deletions(-) diff --git a/drivers/staging/iio/light/tsl2x7x.c b/drivers/staging/iio/light/tsl2x7x.c index 9c929e273135..99230d9313e1 100644 --- a/drivers/staging/iio/light/tsl2x7x.c +++ b/drivers/staging/iio/light/tsl2x7x.c @@ -149,13 +149,6 @@ struct tsl2x7x_als_info { u16 lux; }; -struct tsl2x7x_prox_stat { - int min; - int max; - int mean; - unsigned long stddev; -}; - struct tsl2x7x_chip_info { int chan_table_elements; struct iio_chan_spec channel[4]; @@ -771,106 +764,36 @@ static int tsl2x7x_invoke_change(struct iio_dev *indio_dev) return ret; } -static void tsl2x7x_prox_calculate(int *data, int length, - struct tsl2x7x_prox_stat *stat) -{ - int i; - int sample_sum; - int tmp; - - if (!length) - length = 1; - - sample_sum = 0; - stat->min = INT_MAX; - stat->max = INT_MIN; - for (i = 0; i < length; i++) { - sample_sum += data[i]; - stat->min = min(stat->min, data[i]); - stat->max = max(stat->max, data[i]); - } - - stat->mean = sample_sum / length; - sample_sum = 0; - for (i = 0; i < length; i++) { - tmp = data[i] - stat->mean; - sample_sum += tmp * tmp; - } - stat->stddev = int_sqrt((long)sample_sum / length); -} - -/** - * tsl2x7x_prox_cal() - Calculates std. and sets thresholds. - * @indio_dev: pointer to IIO device - * - * Calculates a standard deviation based on the samples, - * and sets the threshold accordingly. - */ static int tsl2x7x_prox_cal(struct iio_dev *indio_dev) { - int prox_history[MAX_SAMPLES_CAL + 1]; - int i, ret; - struct tsl2x7x_prox_stat prox_stat_data[2]; - struct tsl2x7x_prox_stat *cal; struct tsl2X7X_chip *chip = iio_priv(indio_dev); - u8 tmp_irq_settings; - u8 current_state = chip->tsl2x7x_chip_status; - - if (chip->settings.prox_max_samples_cal > MAX_SAMPLES_CAL) { - dev_err(&chip->client->dev, - "max prox samples cal is too big: %d\n", - chip->settings.prox_max_samples_cal); - chip->settings.prox_max_samples_cal = MAX_SAMPLES_CAL; - } - - /* have to stop to change settings */ - ret = tsl2x7x_chip_off(indio_dev); - if (ret < 0) - return ret; - - /* Enable proximity detection save just in case prox not wanted yet*/ - tmp_irq_settings = chip->settings.interrupts_en; - chip->settings.interrupts_en |= TSL2X7X_CNTL_PROX_INT_ENBL; + int prox_history[MAX_SAMPLES_CAL + 1]; + int i, ret, mean, max, sample_sum; - /*turn on device if not already on*/ - ret = tsl2x7x_chip_on(indio_dev); - if (ret < 0) - return ret; + if (chip->settings.prox_max_samples_cal < 1 || + chip->settings.prox_max_samples_cal > MAX_SAMPLES_CAL) + return -EINVAL; - /*gather the samples*/ for (i = 0; i < chip->settings.prox_max_samples_cal; i++) { usleep_range(15000, 17500); ret = tsl2x7x_get_prox(indio_dev); if (ret < 0) return ret; + prox_history[i] = chip->prox_data; - dev_info(&chip->client->dev, "2 i=%d prox data= %d\n", - i, chip->prox_data); } - ret = tsl2x7x_chip_off(indio_dev); - if (ret < 0) - return ret; - cal = &prox_stat_data[PROX_STAT_CAL]; - tsl2x7x_prox_calculate(prox_history, - chip->settings.prox_max_samples_cal, cal); - chip->settings.prox_thres_high = (cal->max << 1) - cal->mean; - - dev_info(&chip->client->dev, " cal min=%d mean=%d max=%d\n", - cal->min, cal->mean, cal->max); - dev_info(&chip->client->dev, - "%s proximity threshold set to %d\n", - chip->client->name, chip->settings.prox_thres_high); - - /* back to the way they were */ - chip->settings.interrupts_en = tmp_irq_settings; - if (current_state == TSL2X7X_CHIP_WORKING) { - ret = tsl2x7x_chip_on(indio_dev); - if (ret < 0) - return ret; + sample_sum = 0; + max = INT_MIN; + for (i = 0; i < chip->settings.prox_max_samples_cal; i++) { + sample_sum += prox_history[i]; + max = max(max, prox_history[i]); } + mean = sample_sum / chip->settings.prox_max_samples_cal; - return 0; + chip->settings.prox_thres_high = (max << 1) - mean; + + return tsl2x7x_invoke_change(indio_dev); } static ssize_t