Received: by 2002:a05:7412:ba23:b0:fa:4c10:6cad with SMTP id jp35csp2142188rdb; Sun, 21 Jan 2024 09:08:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IFNPv4Kd9aKmkMOkemf1T8LL+RBs+7A1pg/wLA38DV0hFrDVUHs5jvc6Z5g6zOXgoWjtMPS X-Received: by 2002:a05:6358:ee41:b0:175:f2c1:d649 with SMTP id ik1-20020a056358ee4100b00175f2c1d649mr1642408rwb.25.1705856910015; Sun, 21 Jan 2024 09:08:30 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705856909; cv=pass; d=google.com; s=arc-20160816; b=fGhTr8sfgBdnRcHA9P8P27U19uaqwoZxks2ZZa/r5H0HJ4EZUfxcJkb62B3W2vlefO oWQP7avj7O/I6gb1uRTGEP1zb7vnClXws5sE5vSqvmmW6DGLJck5igj39TLY4Ay5E6ja S/QdR3aIQfm4aMgTTBVF7TU11FwOs1cvYeIjOabBX574cJ/cwBaDFILvvrep5Kmf4FpU uJJslG1hVCLXr8slgMEFMZk8nbmPZPuJvThYZiJjO4+S5TsBcLXr7aWUrZL1vSVQcs6M jMcHvBTxK3X2AQS4uGqwtTMXrD9VUkuDVDWhhF3+eK4uh1ryPixg3Gw1CduV24XHq2f3 FDeQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :subject:cc:to:from:date:dkim-signature; bh=V8ERNKNAbo15fdCUo3luLYzYrCOmqhfBzJSA8DLp68A=; fh=i8N0Rer/wssDtgyvQcXngvWu7etAV7tVof4DOWYU3g0=; b=xh2z8kZgKWXaGqfgdK26/ixYhHtrtvj4uufrncQujniFr6P76EE58M4/oj7kjtV4ga xKzr9g+PpCqS+Xvpk2N96cOQ/IXp+G7K7O6fdPsDw2o8UpoVqwtOyfpU9JBfMXehz4Ma 4mmQ/ald5TwdlDEJ2MiG0VSI7/5fvne8ERV5C9H7fE9SSSWIAsaWkamWkcCSIQEiaVrz 1E70He1MNzsmjQawa6iLn7F9vhEnKpd8F0eIMhQn26FG1vZ/QiN4VFrZDYQgYSQrsg8q pvDqoNcK57nRzx8OINToXupMw/b98x13c0FifvgbxdT2O2swsK1JbcT99Ezj2yS3DG3Y K97A== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=APOKnkrz; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-32138-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-32138-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id z12-20020a17090abd8c00b0028c86e12f01si6803216pjr.41.2024.01.21.09.08.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Jan 2024 09:08:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-32138-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=APOKnkrz; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-32138-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-32138-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id A96CCB20CC6 for ; Sun, 21 Jan 2024 17:08:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1BAE3381B5; Sun, 21 Jan 2024 17:08:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="APOKnkrz" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 369BB12E4C; Sun, 21 Jan 2024 17:08:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705856895; cv=none; b=Lio+MrUwzNnDgGzsWo/DH6h6zGB1JbFJxGbAC/UYlfWSfXIOuzJzaCPpXWbUMfFxREM+GdGR3X7m7oLn1X1lYo5kYm9u4pJlV7V9l3dKIq8hge/sq8MxW7t/ECsFQZ2BKfP2JdxgQyM7bEuwnaB4Teg3QWHPvoisbKuo8iFY60s= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705856895; c=relaxed/simple; bh=u5ZvBZF1pXrSm3zSNAXlVdGjYuhQd8XRplQ92G8rl4Y=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=NYfitejLQoQ05LeIu+HrKK+plPi11Wsgz7GNJLQ5wykcwzMmIOC7RQZSIL/qu83BUT/Pu7K++f+M6f+IXKbWn/HY8TF1XvEG8/Ssr4JRmGFXEhwPJaA1HWfhIdGWNoDuHBj142USEwfiXBbKj6A+2akuvlGwH6s90oPXRGfGj4M= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=APOKnkrz; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 22202C433F1; Sun, 21 Jan 2024 17:08:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1705856894; bh=u5ZvBZF1pXrSm3zSNAXlVdGjYuhQd8XRplQ92G8rl4Y=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=APOKnkrz1DiOLwSBPul7WIjqj3auIgLuDNDmCshFkurIqLuMvuIIoPdvhHuEUUC68 w+WqhMUDP4vYHtNVs1Ic7pIdVGIvq6G0FqLtTSjuyb3b7xoGwi2r5PEDkuDmoVH5kh +NJjaB53WjUOXCIbDgus3JzHZXt/Hl+BDIpcQo8iMKDi5p+OisXatKD6LqniUfoPXr C/YPSQkm8kssCo1hCbdNOYo47Ej61Wcf98/D+1CXf02KxB0FRwHm5yyn2IiejsDlXP zwKKAzN6PoA6DFVYPcFgAP8ffd9WvxakuTmSUC01j2eCtFAZj2j/L9683O2zp+Y3Xr DoKtWuOjGmkEQ== Date: Sun, 21 Jan 2024 17:07:59 +0000 From: Jonathan Cameron To: Dumitru Ceclan Cc: Lars-Peter Clausen , Rob Herring , Krzysztof Kozlowski , Conor Dooley , linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Ceclan Dumitru Subject: Re: [PATCH v4 3/5] iio: amplifiers: hmc425a: move conversion logic Message-ID: <20240121170759.7e4488dd@jic23-huawei> In-Reply-To: <20240117125124.8326-4-mitrutzceclan@gmail.com> References: <20240117125124.8326-1-mitrutzceclan@gmail.com> <20240117125124.8326-4-mitrutzceclan@gmail.com> X-Mailer: Claws Mail 4.2.0 (GTK 3.24.40; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Wed, 17 Jan 2024 14:51:12 +0200 Dumitru Ceclan wrote: > Move gain-dB<->code conversion logic from read_raw and write_raw to > hmc425a_gain_dB_to_code() and hmc425a_code_to_gain_dB(). > > Signed-off-by: Dumitru Ceclan Some comments inline Jonathan > --- > drivers/iio/amplifiers/hmc425a.c | 102 ++++++++++++++++++------------- > 1 file changed, 59 insertions(+), 43 deletions(-) > > diff --git a/drivers/iio/amplifiers/hmc425a.c b/drivers/iio/amplifiers/hmc425a.c > index ed4d72922696..e1162a500daf 100644 > --- a/drivers/iio/amplifiers/hmc425a.c > +++ b/drivers/iio/amplifiers/hmc425a.c > @@ -56,35 +56,72 @@ static int hmc425a_write(struct iio_dev *indio_dev, u32 value) > return 0; > } > > +static int hmc425a_gain_dB_to_code(struct hmc425a_state *st, int val, int val2, int *code) > +{ > + struct hmc425a_chip_info *inf = st->chip_info; > + int gain, temp; > + > + if (val < 0) > + gain = (val * 1000) - (val2 / 1000); > + else > + gain = (val * 1000) + (val2 / 1000); > + > + if (gain > inf->gain_max || gain < inf->gain_min) > + return -EINVAL; > + > + switch (st->type) { > + case ID_HMC425A: > + *code = ~((abs(gain) / 500) & 0x3F); In the next patch I point out that this should be data or callbacks in in the st->chip_info structure, not encoded in code here based on st->type (which I want you to get rid of!) > + return 0; > + case ID_HMC540S: > + *code = ~((abs(gain) / 1000) & 0xF); > + return 0; > + case ID_ADRF5740: > + temp = (abs(gain) / 2000) & 0xF; > + *code = temp & BIT(3) ? temp | BIT(2) : temp; Given you are moving the code, a comment here might be nice as it's unusual (bits are 2DB, 4DB, 8DB and another 8DB) > + return 0; > + default: > + return -EINVAL; > + } > +} > + > +static int hmc425a_code_to_gain_dB(struct hmc425a_state *st, int *val, int *val2) > +{ > + int code, gain; > + > + code = st->gain; > + switch (st->type) { > + case ID_HMC425A: > + gain = ~code * -500; > + break; > + case ID_HMC540S: > + gain = ~code * -1000; > + break; > + case ID_ADRF5740: > + code = code & BIT(3) ? code & ~BIT(2) : code; > + gain = code * -2000; > + break; > + } > + > + *val = gain / 1000; > + *val2 = (gain % 1000) * 1000; > + > + return 0; > +} > + > static int hmc425a_read_raw(struct iio_dev *indio_dev, > struct iio_chan_spec const *chan, int *val, > int *val2, long m) > { > struct hmc425a_state *st = iio_priv(indio_dev); > - int code, gain = 0; > int ret; > > mutex_lock(&st->lock); > switch (m) { > case IIO_CHAN_INFO_HARDWAREGAIN: > - code = st->gain; > - > - switch (st->type) { > - case ID_HMC425A: > - gain = ~code * -500; > - break; > - case ID_HMC540S: > - gain = ~code * -1000; > + ret = hmc425a_code_to_gain_dB(st, val, val2); > + if (ret) > break; > - case ID_ADRF5740: > - code = code & BIT(3) ? code & ~BIT(2) : code; > - gain = code * -2000; > - break; > - } > - > - *val = gain / 1000; > - *val2 = (gain % 1000) * 1000; > - > ret = IIO_VAL_INT_PLUS_MICRO_DB; > break; > default: > @@ -100,36 +137,15 @@ static int hmc425a_write_raw(struct iio_dev *indio_dev, > int val2, long mask) > { > struct hmc425a_state *st = iio_priv(indio_dev); > - struct hmc425a_chip_info *inf = st->chip_info; > - int code = 0, gain; > - int ret; > - > - if (val < 0) > - gain = (val * 1000) - (val2 / 1000); > - else > - gain = (val * 1000) + (val2 / 1000); > - > - if (gain > inf->gain_max || gain < inf->gain_min) > - return -EINVAL; > - > - switch (st->type) { > - case ID_HMC425A: > - code = ~((abs(gain) / 500) & 0x3F); > - break; > - case ID_HMC540S: > - code = ~((abs(gain) / 1000) & 0xF); > - break; > - case ID_ADRF5740: > - code = (abs(gain) / 2000) & 0xF; > - code = code & BIT(3) ? code | BIT(2) : code; > - break; > - } > + int code = 0, ret; > > mutex_lock(&st->lock); > switch (mask) { > case IIO_CHAN_INFO_HARDWAREGAIN: > + ret = hmc425a_gain_dB_to_code(st, val, val2, &code); > + if (ret) > + break; > st->gain = code; > - > ret = hmc425a_write(indio_dev, st->gain); > break; > default: