Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp2234207rwl; Thu, 13 Apr 2023 03:44:01 -0700 (PDT) X-Google-Smtp-Source: AKy350bVEfWFg1wCL8Ynkrbl7wl3oCXyPV6b+dSpMyvXGH/GxKlCZt1WcY2+OpZVSXW11ra1SaT5 X-Received: by 2002:a17:90a:9a9:b0:246:b9e3:aab5 with SMTP id 38-20020a17090a09a900b00246b9e3aab5mr1599488pjo.21.1681382641112; Thu, 13 Apr 2023 03:44:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681382641; cv=none; d=google.com; s=arc-20160816; b=xrpgxZXqdlyD/6/P49xr/nEff7vFjiJmVF8vjm5MC3pg0uHvkIgatBQDulcPDlA0uv psvS789/TJg4ErepaJDNMBCwDGNvcWfYoI+UNMGigCVi3d8H66s877KEyib9uIA+PMZl XPpTIQaLPdiTqLBAJ4o5C/yImZdxP+S9QHyzzH0HyE0sVuvk1czpvsvJE1yr6rN6iB+1 eP7YZC606Bu1FoYSu3S0c42/LVlXAjAuh0vQ3w5k9jqJ5GQz12WxbmZcHgEF2Fa7nOxk FzK5823Qsu8ijyLvEAxrdZ7v6hMn/9wEPGTabCtf9KPGdibBczzCNlrqtPoTD8WpFwji FKdw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :organization:references:in-reply-to:message-id:subject:cc:to:from :date; bh=PrxqxRjyxZWsRVP9xHRXBH4kSVRAE+3kVpe7iyrYVqk=; b=CGQ9x1AmzqrtxOP52dZCbmw61Yb5HwNqSST4Y8o3WOuGNSrBiaPuMdbAlhbivSXyeE r3EXzoutowViA6ES0M99QeQtDaldubfO5hFlhlHNa1u7Sb7i21hHIcOgFQuBjQSUyfKt W28aC+KVLo/4B98DFu59LAAeMMBfA37KlSpmsMbEl7VqZrnmiI+tfi0YZhCDUSLjk5fl 7ZL+AInT9feXkjfzyuLB8gU16GPoZ5jgmh5XI0V0QsjMHanPP0eVERi3R8osujDP5I+9 fDhRBgVmj4Md1HEwonhl4xehjA77a357UkMD1byizkng4+EnZYCJKE8vXg1HIaNhtl9v +vJg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gn23-20020a17090ac79700b00233ebd4c537si1689446pjb.22.2023.04.13.03.43.49; Thu, 13 Apr 2023 03:44:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230134AbjDMK2k convert rfc822-to-8bit (ORCPT + 99 others); Thu, 13 Apr 2023 06:28:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45218 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229479AbjDMK2g (ORCPT ); Thu, 13 Apr 2023 06:28:36 -0400 Received: from frasgout.his.huawei.com (frasgout.his.huawei.com [185.176.79.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 246002737; Thu, 13 Apr 2023 03:28:34 -0700 (PDT) Received: from lhrpeml500005.china.huawei.com (unknown [172.18.147.201]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4PxwkY5Tgbz67QKJ; Thu, 13 Apr 2023 18:27:33 +0800 (CST) Received: from localhost (10.202.227.76) by lhrpeml500005.china.huawei.com (7.191.163.240) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Thu, 13 Apr 2023 11:28:32 +0100 Date: Thu, 13 Apr 2023 11:28:30 +0100 From: Jonathan Cameron To: Patrik =?ISO-8859-1?Q?Dahlstr=F6m?= CC: , , , , , , , , Subject: Re: [PATCH v4 8/9] iio: adc: palmas: add support for iio threshold events Message-ID: <20230413112830.00006279@Huawei.com> In-Reply-To: <20230408114825.824505-9-risca@dalakolonin.se> References: <20230408114825.824505-1-risca@dalakolonin.se> <20230408114825.824505-9-risca@dalakolonin.se> Organization: Huawei Technologies Research and Development (UK) Ltd. X-Mailer: Claws Mail 4.1.0 (GTK 3.24.33; x86_64-w64-mingw32) MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8BIT X-Originating-IP: [10.202.227.76] X-ClientProxiedBy: lhrpeml500003.china.huawei.com (7.191.162.67) To lhrpeml500005.china.huawei.com (7.191.163.240) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, 8 Apr 2023 13:48:24 +0200 Patrik Dahlstr?m wrote: > The palmas gpadc block has support for monitoring up to 2 ADC channels > and issue an interrupt if they reach past a set threshold. This change > hooks into the IIO events system and exposes to userspace the ability to > configure these threshold values for each channel, but only allow up to > 2 such thresholds to be enabled at any given time. Trying to enable a > third channel will result in an error. > > Userspace is expected to input calibrated, as opposed to raw, values as > threshold. However, it is not enough to do the opposite of what is done > when converting the other way around. To account for tolerances in the > ADC, the calculated raw threshold should be adjusted based on the ADC > specifications for the device. These specifications include the integral > nonlinearity (INL), offset, and gain error. To adjust the high > threshold, use the following equation: > > (calibrated value + INL) * Gain error + offset = maximum value [1] > > Likewise, use the following equation for the low threshold: > > (calibrated value - INL) * Gain error - offset = minimum value > > The gain error is a combination of gain error, as listed in the > datasheet, and gain error drift due to temperature and supply. The exact > values for these specifications vary between palmas devices. This patch > sets the values found in TWL6035, TWL6037 datasheet. > > [1] TI Application Report, SLIA087A, Guide to Using the GPADC in > TPS65903x, TPS65917-Q1, TPS65919-Q1, and TPS65916 Devices. > > Signed-off-by: Patrik Dahlstr?m 0-day found some stuff we'd missed in here. I've fixed it up and pushed out again. > --- ... > +static int palmas_gpadc_write_event_value(struct iio_dev *indio_dev, > + const struct iio_chan_spec *chan, > + enum iio_event_type type, > + enum iio_event_direction dir, > + enum iio_event_info info, > + int val, int val2) > +{ > + struct palmas_gpadc *adc = iio_priv(indio_dev); > + int adc_chan = chan->channel; > + int old; > + int ret; > + > + if (adc_chan > PALMAS_ADC_CH_MAX || type != IIO_EV_TYPE_THRESH) > + return -EINVAL; > + > + mutex_lock(&adc->lock); > + switch (info) { > + case IIO_EV_INFO_VALUE: > + if (val < 0 || val > 0xFFF) { > + ret = -EINVAL; > + break; Should be goto out_unlock; Found because old is undefined. > + } > + if (dir == IIO_EV_DIR_RISING) { > + old = adc->thresholds[adc_chan].high; > + adc->thresholds[adc_chan].high = val; > + } Whilst here should be } else { Tidied both up force an update on the testing branch. > + else { > + old = adc->thresholds[adc_chan].low; > + adc->thresholds[adc_chan].low = val; > + } > + ret = 0; > + break; > + default: > + ret = -EINVAL; > + goto out_unlock; > + } > + > + if (val != old && palmas_gpadc_get_event(adc, adc_chan, dir)) > + ret = palmas_gpadc_reconfigure_event_channels(adc); > + > +out_unlock: > + mutex_unlock(&adc->lock); > + > + return ret; > +} > +