Received: by 2002:ac8:156:0:b0:3e0:cd10:60c8 with SMTP id f22csp1208936qtg; Thu, 30 Mar 2023 09:53:21 -0700 (PDT) X-Google-Smtp-Source: AKy350Z0GtsUuDGToDAz4Mtl1+5xC4288kd/LdBSfec6qgSerCQGmAh9S9PJ6JKgHCOkUolOnMpK X-Received: by 2002:a17:902:720a:b0:1a1:b51b:4d3b with SMTP id ba10-20020a170902720a00b001a1b51b4d3bmr20389718plb.1.1680195201507; Thu, 30 Mar 2023 09:53:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680195201; cv=none; d=google.com; s=arc-20160816; b=bHaJnvNBezFbHQ8w2ml+hpZq2CAabCTaDM4QoairFNA/az/zg5ufR/KSzUVGjnRPfa mzhA0Js786ECJPtqHcX3GBjx7mq9SBzmgRGxbAF1Csr5sxvi1o0av9Z23szLBu5mCxeN 0CCmqhIYxo/9XciIaYk+x+sNzMW9rrXpY0E+MyWnkvqT0jlcyK80Bbf9QVVi3VRDMsIV MpKJq5csof00DXA7NsM9jACbGIV+BYPj25l4PkkSPanH6Eq+zkXT2gJQ5u5on/0VIHtg argLlFGQrE+7CYxHUYn2RsLrTIvI2m1BiTiXHwI3wqr35dWnr7/uNTPJaIgvotiPBr68 jtzQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:subject :from:content-language:references:cc:to:user-agent:mime-version:date :message-id:dkim-signature; bh=VeR6trew7HW2G4oYPc7uXwBQjgjSoJfWbtLdkhlcjW8=; b=AcCWoJ8u64VRSnbSTBKG8NphvP+krrAGWD1wRbf+difiNzwUpBL4RDIHuL8qaHoTMG EfRyjiYlZAmwBATviWqajmIM9JEufNff5A/zPkrXdk83YprlODIKC/WI71mbx4N8DHya pG0OkV3VatxUFCTvE1C3ZvvaOr0IwAjEc7tf6JyAcrAMidQSFQ0jp7gxUa9axiz6gpJ+ O8vRsX/PAKkOeZFb/kiSU+ABo1FuX0fM0/df0FZNXwcXwirSa0QG/L/EdDGOl1SMICGB H4wzvmPOrsvSMX1wzhVWpiZusrsKL7RWOWXXxnaxzCXrpygaV+X+vNyAtq+n99p/0jRw lUdg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="npY/2p1f"; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id lh8-20020a170903290800b00192d6fb649dsi33267051plb.242.2023.03.30.09.53.02; Thu, 30 Mar 2023 09:53:21 -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; dkim=pass header.i=@gmail.com header.s=20210112 header.b="npY/2p1f"; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229768AbjC3Qt5 (ORCPT + 99 others); Thu, 30 Mar 2023 12:49:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55662 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231439AbjC3QtL (ORCPT ); Thu, 30 Mar 2023 12:49:11 -0400 Received: from mail-lf1-x136.google.com (mail-lf1-x136.google.com [IPv6:2a00:1450:4864:20::136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C4D0886AB; Thu, 30 Mar 2023 09:48:13 -0700 (PDT) Received: by mail-lf1-x136.google.com with SMTP id g17so25341124lfv.4; Thu, 30 Mar 2023 09:48:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680194888; h=content-transfer-encoding:in-reply-to:subject:from:content-language :references:cc:to:user-agent:mime-version:date:message-id:from:to:cc :subject:date:message-id:reply-to; bh=VeR6trew7HW2G4oYPc7uXwBQjgjSoJfWbtLdkhlcjW8=; b=npY/2p1fFHW2+YSwPXEbpgc6u491kVScGeEwTgy3n50qecYYvzlQYMUYIe9ymXcLZJ 76NutjJY8PmVkWQgcvB5VmVkIUZZCKHSpWFQ8w9fH+OhkVsWGSgw+WhizddsYR2wUOnF OukSvcNxF6ecBzBIJ2MPZUr18ig6G6SSE8LqN6HGe+z2dHWINJT3mmku9q0sunayiFki XFQ6QrwvIwWw81Smz0zo/XhYoRtOnT1VVJc1USSYp0j9pSXkrnr4dvXLPwFFgbz4bBRG O+65H63G7og1NNaLO5sQFWeevJEH4ORrzlILDF2CYLsWho4IFYKyf3QJReNZN6f3OiY6 P4BQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680194888; h=content-transfer-encoding:in-reply-to:subject:from:content-language :references:cc:to:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=VeR6trew7HW2G4oYPc7uXwBQjgjSoJfWbtLdkhlcjW8=; b=wiRGBYz8EhIClywguPKa/FCBgRB9d+N8LBp5g677LL8NwZk5htBMSJOHE77pohAj1w 1j6ogEhtqsxqx22yg7NpKUIguMYr5EnORNdgVFvJDlSQfXSjv3kapbOgLmJ5ZB2UkIBa n0QOWknLK8HzA6/KGjD6lUkk1PauC87kcJP0wWa6ZohgraLRf9NxUNKWu7b/ID2G08mg /ELugrSj3DdIOcnodXhmxRzejHgC3yhDY1HFIxEmTNdEyK6CtgCUyylUKzAEARtPqTVP 4nOo+vGfLvU2+pDghmJYbpdZUUVB+dP+pvVn7jVCXfeLkz+yuP0k/kh4r6D7KqcH1go/ w6ww== X-Gm-Message-State: AAQBX9cCoyX2sFaMKjdT7T+rzmN9gJN9C8/4otTLuMdOSGl4ozqzBgyo J3CcsV6e64ve98RFCsYuMlN04FcLKxM= X-Received: by 2002:ac2:46f9:0:b0:4e9:d719:3603 with SMTP id q25-20020ac246f9000000b004e9d7193603mr7425573lfo.55.1680194888041; Thu, 30 Mar 2023 09:48:08 -0700 (PDT) Received: from ?IPV6:2001:14ba:16f3:4a00::1? (dc75zzyyyyyyyyyyyyyyt-3.rev.dnainternet.fi. [2001:14ba:16f3:4a00::1]) by smtp.gmail.com with ESMTPSA id b16-20020ac25630000000b004e846175329sm7749lff.202.2023.03.30.09.48.07 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 30 Mar 2023 09:48:07 -0700 (PDT) Message-ID: Date: Thu, 30 Mar 2023 19:48:06 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.0 To: Matti Vaittinen Cc: Jonathan Cameron , Lars-Peter Clausen , linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org References: <68179bcc8371ef9026d1179847fc9c73aa7460f4.1679915278.git.mazziesaccount@gmail.com> Content-Language: en-US, en-GB From: Matti Vaittinen Subject: Re: [PATCH v6 2/7] iio: light: Add gain-time-scale helpers In-Reply-To: <68179bcc8371ef9026d1179847fc9c73aa7460f4.1679915278.git.mazziesaccount@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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 3/27/23 14:28, Matti Vaittinen wrote: > Some light sensors can adjust both the HW-gain and integration time. > There are cases where adjusting the integration time has similar impact > to the scale of the reported values as gain setting has. > > IIO users do typically expect to handle scale by a single writable 'scale' > entry. Driver should then adjust the gain/time accordingly. > > It however is difficult for a driver to know whether it should change > gain or integration time to meet the requested scale. Usually it is > preferred to have longer integration time which usually improves > accuracy, but there may be use-cases where long measurement times can be > an issue. Thus it can be preferable to allow also changing the > integration time - but mitigate the scale impact by also changing the gain > underneath. Eg, if integration time change doubles the measured values, > the driver can reduce the HW-gain to half. > > The theory of the computations of gain-time-scale is simple. However, > some people (undersigned) got that implemented wrong for more than once. > > Add some gain-time-scale helpers in order to not dublicate errors in all > drivers needing these computations. > > Signed-off-by: Matti Vaittinen > > --- > Currently it is only BU27034 using these in this series. I am however working > with drivers for RGB sensors BU27008 and BU27010 which have similar > [gain - integration time - scale] - relation. I hope sending those > follows soon after the BU27034 is done. > > +/** > + * iio_gts_find_new_gain_sel_by_old_gain_time - compensate for time change > + * @gts: Gain time scale descriptor > + * @old_gain: Previously set gain > + * @old_time_sel: Selector corresponding previously set time > + * @new_time_sel: Selector corresponding new time to be set > + * @new_gain: Pointer to value where new gain is to be written > + * > + * We may want to mitigate the scale change caused by setting a new integration > + * time (for a light sensor) by also updating the (HW)gain. This helper computes > + * new gain value to maintain the scale with new integration time. > + * > + * Return: 0 on success. -EINVAL if gain matching the new time is not found. Here we need to document another return value denote whether the @new_gain was updated. > + */ > +int iio_gts_find_new_gain_sel_by_old_gain_time(struct iio_gts *gts, > + int old_gain, int old_time_sel, > + int new_time_sel, int *new_gain) > +{ > + const struct iio_itime_sel_mul *itime_old, *itime_new; > + u64 scale; > + int ret; > + > + itime_old = iio_gts_find_itime_by_sel(gts, old_time_sel); > + if (!itime_old) > + return -EINVAL; > + > + itime_new = iio_gts_find_itime_by_sel(gts, new_time_sel); > + if (!itime_new) > + return -EINVAL; > + > + ret = iio_gts_get_scale_linear(gts, old_gain, itime_old->time_us, > + &scale); > + if (ret) > + return ret; > + > + ret = gain_get_scale_fraction(gts->max_scale, scale, itime_new->mul, > + new_gain); > + if (ret) > + return ret; > + > + if (!iio_gts_valid_gain(gts, *new_gain)) > + return -EINVAL; I would change this to -ERANGE to differentiate the case where the new gain was computed but was not valid. The bu27034 (and not-yet-fully-finished bu27008) driver uses the computed gain to find closest matching gain the hardware supports. I am not super happy with the -ERANGE, as it is also possible the gain is in the "range" of supported gains but not _exactly_ supported one. In a sense -EINVAL would be more correct. The invalid time could in a sense be interpreted as an "time selector not found" - so maybe the -ENOENT could be somehow tolerated. Still, in my opinion the invalid integration time is very much more an -EINVAL than anything else... > + > + return 0; > +} I will fix this for v7.