From patchwork Thu Nov 28 16:50:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matti Vaittinen X-Patchwork-Id: 13888252 Received: from mail-lj1-f175.google.com (mail-lj1-f175.google.com [209.85.208.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0B6EB1AA1E4; Thu, 28 Nov 2024 16:50:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732812635; cv=none; b=V+DnM0FIQGaQAesIQTTNtbSwY6TGZG2MIKlssTb0HUuGs3PHXaysKbIVvMFRAosgVyiZesP7FAYgOacla04IP4ukPBIPmd+PTmp7eiM1vOBbbZE0pnKVteeI57hpO2cNxlTWvq3HRCm6xxwaJbZqc8g78jERs6pVKglNl8+gDyE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732812635; c=relaxed/simple; bh=okAaqW4wJNybkQoDzBAMqbD3/IoNTo/QwUvQMJFxovg=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=iSKtomqw1M8HFzoS31fiMv90+4jellruQRegq6CbEalZJi3HRO2dg1X+vlAl+Ykm9RlarM79Gh9AOEbuyYg6ZLtzfTrPh6j1/GDIUSc8Pu+3FQx95XFLX45oMybMfGpTbSbYFN76xwo8OvhE9Io+pp1K2l/vGEwPHx29JEIq6u8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Qr9TeyA+; arc=none smtp.client-ip=209.85.208.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Qr9TeyA+" Received: by mail-lj1-f175.google.com with SMTP id 38308e7fff4ca-2ffe4569fbeso4295691fa.1; Thu, 28 Nov 2024 08:50:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1732812632; x=1733417432; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=JMhyJcHOTwOTZlj4QlGUDoHdLCNeq1FU2ehiF3dhMzs=; b=Qr9TeyA+1vo4pHuYcgkJ9iTJjVau/85nl6g0tUwyccQjmEA6vl3l9xB1yIl1kuPxXD HseO9O2S8JXwDXzlu6yxXMM/vy2L0ir+yHlrr4ASToHYKe2ZP+k8tRWxlAOt5qujRjrX /mc/b852+5R0K7qwHywOOiFc7BA7zhH96s4lRlEdEhn98TMJi73hCU6Cmyo4BA3Xdai2 39iYqXyI90ECNFyAiAG1pwvwZdQJ7a/reOJmHS5Ftq8336AWT1legDCFLEBZy2ds5rlT ttLwKPJLc78J5FhfWpQhPWPPwblv9xVXpHVVqtwIS4Eg5Td9Nd7rw8rw4NUe8dHlYfNC TorQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732812632; x=1733417432; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=JMhyJcHOTwOTZlj4QlGUDoHdLCNeq1FU2ehiF3dhMzs=; b=OqVumOMww0KcAVaUwswD9QBBHulTIslTcnORZVPMp2Pd5pfOlO6qeRLij/NerNqnbS GsGZJNuFsmxxBAAObQL+LUZCuh1uFPkxD8cjYDd5q1igBmGFp+um5y8rJ+74IN4hJKWs 4Sft6BU+CdX6YyPa/3tbQJqQuHoxoLhEQTXQns1pbmpfVsYImrrm63YIkGe/tMfJW01D C+X8zqVRE8NOj5x9XOT4t/kfFA0egF7skwv/1I0PG6bRkz2bq0O41kMQGeCzCjybbYKj 1DfdSkxCUNOFAW1QzIsqPT6Vg25Fh8GW2RWxn3gjRYTCla5hEd820tliQ+GBrGXzbSN7 9COQ== X-Forwarded-Encrypted: i=1; AJvYcCU0velPZZOoI+BWH/AyHxLx4S0YlpxdhESpNeCHrONhwaWX0d8PHMx6Xe5W2FV7Unj584AspuG/VEY=@vger.kernel.org, AJvYcCVsN3/PKQlxG9V0Am8rYX4HstVx2x64ZAa3xuS1DztMlieW0z9lgkJjYq3K7vDbrs3TyfaOMwVBOYJI/Yvl@vger.kernel.org X-Gm-Message-State: AOJu0Yyw3CgQrroY1Z1WQkyDI9uzwvjAmAVIMGrDV2FmS96IySeIauDy 36/5s05Pd55U6KW7l6YcfW51uiaBdD2dIiG8m703Xwy2bEqk2xjSK551tw== X-Gm-Gg: ASbGncu9t1wbSE3WWOzvCRQQdk3paS1WkRmPQZ4D7VmRTmNSQEKE/wkyFv2jMOgrOJ+ P7mTP0gh5RI2znVnTDxYl++Bg9iEX99RM8XT7j7x+24LISmh80nYkMCcd2sMU3UGqczBj/iZAdx bsO5O2RrpSy4LOfk1ZeMj0b9Cp9oIr6fB7SqRTa74X4XPHqBfwA7iDekhg2X8FHInJf+G3BJDHA wgGu/SZvjn5WqxOZt7OsTZHd4j0aczcK9BYFQxJl0jo6c8ZblV6/EtA X-Google-Smtp-Source: AGHT+IGFHETRuNIz0UOtcyPEOkLL+enzqYEyP2wRO3yn4NsL5OPcH/n8W8tWPcLrElcJ6ueDeAlTyQ== X-Received: by 2002:a2e:9687:0:b0:2ff:e54a:cd08 with SMTP id 38308e7fff4ca-2ffe54acea4mr3133751fa.3.1732812631758; Thu, 28 Nov 2024 08:50:31 -0800 (PST) Received: from mva-rohm ([2a10:a5c0:800d:dd00:8fdf:935a:2c85:d703]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-2ffdfca0ee9sm2315511fa.95.2024.11.28.08.50.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Nov 2024 08:50:29 -0800 (PST) Date: Thu, 28 Nov 2024 18:50:24 +0200 From: Matti Vaittinen To: Matti Vaittinen , Matti Vaittinen Cc: Matti Vaittinen , Jonathan Cameron , Lars-Peter Clausen , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/3] iio: gts: Simplify using __free Message-ID: <1f8e1388b69df8a5a1a87748e9c748d2a3aa0533.1732811829.git.mazziesaccount@gmail.com> References: Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: The error path in the gain_to_scaletables() uses goto for unwinding an allocation on failure. This can be slightly simplified by using the automated free when exiting the scope. Use __free(kfree) and drop the goto based error handling. Signed-off-by: Matti Vaittinen --- This is derived from the: https://lore.kernel.org/lkml/5efc30d832275778d1f48d7e2c75b1ecc63511d5.1732105157.git.mazziesaccount@gmail.com/ --- drivers/iio/industrialio-gts-helper.c | 108 +++++++++++++++----------- 1 file changed, 63 insertions(+), 45 deletions(-) diff --git a/drivers/iio/industrialio-gts-helper.c b/drivers/iio/industrialio-gts-helper.c index 291c0fc332c9..e15d0112e9e3 100644 --- a/drivers/iio/industrialio-gts-helper.c +++ b/drivers/iio/industrialio-gts-helper.c @@ -4,6 +4,7 @@ * Copyright (c) 2023 Matti Vaittinen */ +#include #include #include #include @@ -165,11 +166,9 @@ static int iio_gts_gain_cmp(const void *a, const void *b) return *(int *)a - *(int *)b; } -static int gain_to_scaletables(struct iio_gts *gts, int **gains, int **scales) +static int fill_and_sort_scaletables(struct iio_gts *gts, int **gains, int **scales) { - int i, j, new_idx, time_idx, ret = 0; - int *all_gains; - size_t gain_bytes; + int i, j, ret; for (i = 0; i < gts->num_itime; i++) { /* @@ -189,8 +188,15 @@ static int gain_to_scaletables(struct iio_gts *gts, int **gains, int **scales) } } - gain_bytes = array_size(gts->num_hwgain, sizeof(int)); - all_gains = kcalloc(gts->num_itime, gain_bytes, GFP_KERNEL); + return 0; +} + +static int build_combined_table(struct iio_gts *gts, int **gains, size_t gain_bytes) +{ + int ret, i, j, new_idx, time_idx; + int *all_gains __free(kfree) = kcalloc(gts->num_itime, gain_bytes, + GFP_KERNEL); + if (!all_gains) return -ENOMEM; @@ -232,10 +238,9 @@ static int gain_to_scaletables(struct iio_gts *gts, int **gains, int **scales) gts->avail_all_scales_table = kcalloc(new_idx, 2 * sizeof(int), GFP_KERNEL); - if (!gts->avail_all_scales_table) { - ret = -ENOMEM; - goto free_out; - } + if (!gts->avail_all_scales_table) + return -ENOMEM; + gts->num_avail_all_scales = new_idx; for (i = 0; i < gts->num_avail_all_scales; i++) { @@ -246,14 +251,25 @@ static int gain_to_scaletables(struct iio_gts *gts, int **gains, int **scales) if (ret) { kfree(gts->avail_all_scales_table); gts->num_avail_all_scales = 0; - goto free_out; + return ret; } } -free_out: - kfree(all_gains); + return 0; +} - return ret; +static int gain_to_scaletables(struct iio_gts *gts, int **gains, int **scales) +{ + int ret; + size_t gain_bytes; + + ret = fill_and_sort_scaletables(gts, gains, scales); + if (ret) + return ret; + + gain_bytes = array_size(gts->num_hwgain, sizeof(int)); + + return build_combined_table(gts, gains, gain_bytes); } /** @@ -337,26 +353,11 @@ static void iio_gts_us_to_int_micro(int *time_us, int *int_micro_times, } } -/** - * iio_gts_build_avail_time_table - build table of available integration times - * @gts: Gain time scale descriptor - * - * Build the table which can represent the available times to be returned - * to users using the read_avail-callback. - * - * NOTE: Space allocated for the tables must be freed using - * iio_gts_purge_avail_time_table() when the tables are no longer needed. - * - * Return: 0 on success. - */ -static int iio_gts_build_avail_time_table(struct iio_gts *gts) +static int __iio_gts_build_avail_time_table(struct iio_gts *gts) { - int *times, i, j, idx = 0, *int_micro_times; - - if (!gts->num_itime) - return 0; + int i, j, idx = 0, *int_micro_times; + int *times __free(kfree) = kcalloc(gts->num_itime, sizeof(int), GFP_KERNEL); - times = kcalloc(gts->num_itime, sizeof(int), GFP_KERNEL); if (!times) return -ENOMEM; @@ -384,25 +385,42 @@ static int iio_gts_build_avail_time_table(struct iio_gts *gts) /* create a list of times formatted as list of IIO_VAL_INT_PLUS_MICRO */ int_micro_times = kcalloc(idx, sizeof(int) * 2, GFP_KERNEL); - if (int_micro_times) { - /* - * This is just to survive a unlikely corner-case where times in - * the given time table were not unique. Else we could just - * trust the gts->num_itime. - */ - gts->num_avail_time_tables = idx; - iio_gts_us_to_int_micro(times, int_micro_times, idx); - } - - gts->avail_time_tables = int_micro_times; - kfree(times); - if (!int_micro_times) return -ENOMEM; + /* + * This is just to survive a unlikely corner-case where times in + * the given time table were not unique. Else we could just + * trust the gts->num_itime. + */ + gts->num_avail_time_tables = idx; + iio_gts_us_to_int_micro(times, int_micro_times, idx); + + gts->avail_time_tables = int_micro_times; + return 0; } +/** + * iio_gts_build_avail_time_table - build table of available integration times + * @gts: Gain time scale descriptor + * + * Build the table which can represent the available times to be returned + * to users using the read_avail-callback. + * + * NOTE: Space allocated for the tables must be freed using + * iio_gts_purge_avail_time_table() when the tables are no longer needed. + * + * Return: 0 on success. + */ +static int iio_gts_build_avail_time_table(struct iio_gts *gts) +{ + if (!gts->num_itime) + return 0; + + return __iio_gts_build_avail_time_table(gts); +} + /** * iio_gts_purge_avail_time_table - free-up the available integration time table * @gts: Gain time scale descriptor From patchwork Thu Nov 28 16:50:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matti Vaittinen X-Patchwork-Id: 13888253 Received: from mail-lj1-f181.google.com (mail-lj1-f181.google.com [209.85.208.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4150019923C; Thu, 28 Nov 2024 16:50:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732812655; cv=none; b=jHG6TYvzXD+5yPnOp0HZ8g0QOY0Fulk8XRYWpReYWMLf/Ip+SP5OunKAXkzk0IKu5ER+2SiZq9uT2EXrwpc1VFm7x224Gz/sd42/Se1p4EG2TRw61nmEOc7GeRDLYQWEUDxu6UGsQWZgc3XGNOHuYb9cOkvvpJNyCA7iebl0Rjw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732812655; c=relaxed/simple; bh=jVAKUz6MEw83QiOzWclac1mRqxhA/+eXfFUwUwzTwFY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Sul8BWdUc8vsmFOuqlmdTHEqwkHQLJQ+mdH14AbcwsFqTI9NyN9q0zvaAHWU9O1OrIidRt4HcgnYSZkW8Rd8ObytcXQY6YPCspVqSEhNyADlGoLdt0oZfjDsUKj+s2KbRl+sIpDLVazpTpUbbJ8hH8rlkNa778BmKh/si1MBe6k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=f9KKmNiV; arc=none smtp.client-ip=209.85.208.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="f9KKmNiV" Received: by mail-lj1-f181.google.com with SMTP id 38308e7fff4ca-2ffbea0acc2so11627171fa.1; Thu, 28 Nov 2024 08:50:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1732812652; x=1733417452; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=jLTYx/5BrDI+gA+YBI5Z0MbzuciNuAtO+o+043dPYSA=; b=f9KKmNiVkMJUGGJmli4sPrvEQKu3INYaG9Dmu9g6mGLyKPFyPuu2QWbdvharVxwmce zx1+N4d+JcaWBY29N5E9AGZ8IcaeykW2HMVrmfhrlFtyasBaUXj4rW98T1TqToIX07aN vWhvPaTm4pihKBlheLIzGwZVsIoPJHppBiv9evG0l3AVUISuQG3bQJUG6AgZNO+sTUdz 6Qu5Oa3tgvoeGPlvl9BkOqThTSO6rx2tx2w7O61Bw0sXnSv8UppBbHR81eTUaZvW0sSD YEtJh0AVQjrREv0CcBnrJf44sZfIiUKAh655AOri6tmxhbwXbZAhhb13wT8z4Ab2Pr6P FAIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732812652; x=1733417452; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=jLTYx/5BrDI+gA+YBI5Z0MbzuciNuAtO+o+043dPYSA=; b=uszOwMGRveUIuuIKh1ZqyhlNeq+5g/6vdOY2obwaSGPMAq0lFCsd76KgrAzQxqKMFe mpViV3rHOJpM4mvDfUnaDyKdsB7/3CrZD3B2uQ4tumZOoPKaUVFE9TaMnMvtcyZQI69s iW+YKJsjgfygjCkJH3G3qLOIfjvhGWxFshrSKrJuvtbqASqzZW6twcwE+ENmUzcsn7ni OZJ2cNcjeDF27MuzdEcmEOlS/FD7nj/BTaFUNgSdg9t8gOz9RvRDTEvgD69twPJ72dCF mJvW8BzMHCqydhe2zUXKTKj9KqzqGyVl8CY24xdu5mWYfPDpx/dMCHFUrHwkX/zGbr8y Zb3A== X-Forwarded-Encrypted: i=1; AJvYcCU/xn3QiSSO5SeU8yYBjqs90GyJbd1t6+I+QZqbAgfLxCNhsaIZpQFE3zSGRBaYDlAcXNKUeDfwBEg=@vger.kernel.org, AJvYcCXHbf5cJ0msSBOYYFK2OGrNca+jIQybzo3uGI+RtqHc2WIevI2xYpyXrPOPyrR/ZQYn0H2SaSJt8TPok3XF@vger.kernel.org X-Gm-Message-State: AOJu0YxdLipzy5AawusGLXVNCn56sHeQ7Fd+lXsmgxbxlyiD5bRefoMF 28rJ0Y2QrtpPKLR14J5E4SkqZtWvt7OuG6GegWfa7knu/1YfdFkmsjC06w== X-Gm-Gg: ASbGnct6uvrb1hvYNyNgq+/p68LWbsQdlT39GRIC8TivNNUq76+g4PbzFtBWzmwSHlt 9Aqv0P4GNBhyjw6JEgOhYdcggzDbnt+qus/PRatcWnSpw0UQySC3ss1uHYR2kHYV3NSeyl9G5vz LYhiIB8WJ4y3OMNxtl+M6hLUlDre1ERnz7eorgh7E4a6UwVAsNwYdjLp49y//iXdE8i1DAJUcYV pyMwXdG0NHz3h0NdTsXdult3KuBb3xwkgaoc2wvhupHW4cfzJtRJ/DL X-Google-Smtp-Source: AGHT+IEPpSpgm9OntrkxTix1kkPmk1INj6kIXBsZT5Lx3X4ybxdCrzQvdc/n1j7QM9iph6MZO1QHsw== X-Received: by 2002:a2e:b8c7:0:b0:2ff:8d3b:46d3 with SMTP id 38308e7fff4ca-2ffd6096835mr40391371fa.16.1732812652243; Thu, 28 Nov 2024 08:50:52 -0800 (PST) Received: from mva-rohm ([2a10:a5c0:800d:dd00:8fdf:935a:2c85:d703]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-2ffdfccda9dsm2277301fa.123.2024.11.28.08.50.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Nov 2024 08:50:50 -0800 (PST) Date: Thu, 28 Nov 2024 18:50:44 +0200 From: Matti Vaittinen To: Matti Vaittinen , Matti Vaittinen Cc: Matti Vaittinen , Jonathan Cameron , Lars-Peter Clausen , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/3] iio: gts: split table-building function Message-ID: <678a3859bd9feeedc10eda04b801f37fa0444d25.1732811829.git.mazziesaccount@gmail.com> References: Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: The function which builds the available scales tables for the GTS helpers is hard to follow. Split it up so it is more obvious for occasional reader to see what is done and why. Signed-off-by: Matti Vaittinen --- drivers/iio/industrialio-gts-helper.c | 66 +++++++++++++++++---------- 1 file changed, 42 insertions(+), 24 deletions(-) diff --git a/drivers/iio/industrialio-gts-helper.c b/drivers/iio/industrialio-gts-helper.c index e15d0112e9e3..7f900f578f1d 100644 --- a/drivers/iio/industrialio-gts-helper.c +++ b/drivers/iio/industrialio-gts-helper.c @@ -191,14 +191,10 @@ static int fill_and_sort_scaletables(struct iio_gts *gts, int **gains, int **sca return 0; } -static int build_combined_table(struct iio_gts *gts, int **gains, size_t gain_bytes) +static int combine_gain_tables(struct iio_gts *gts, int **gains, + int *all_gains, size_t gain_bytes) { - int ret, i, j, new_idx, time_idx; - int *all_gains __free(kfree) = kcalloc(gts->num_itime, gain_bytes, - GFP_KERNEL); - - if (!all_gains) - return -ENOMEM; + int i, new_idx, time_idx; /* * We assume all the gains for same integration time were unique. @@ -212,8 +208,8 @@ static int build_combined_table(struct iio_gts *gts, int **gains, size_t gain_by new_idx = gts->num_hwgain; while (time_idx-- > 0) { - for (j = 0; j < gts->num_hwgain; j++) { - int candidate = gains[time_idx][j]; + for (i = 0; i < gts->num_hwgain; i++) { + int candidate = gains[time_idx][i]; int chk; if (candidate > all_gains[new_idx - 1]) { @@ -236,25 +232,47 @@ static int build_combined_table(struct iio_gts *gts, int **gains, size_t gain_by } } - gts->avail_all_scales_table = kcalloc(new_idx, 2 * sizeof(int), - GFP_KERNEL); - if (!gts->avail_all_scales_table) - return -ENOMEM; + return new_idx; +} - gts->num_avail_all_scales = new_idx; +static int *build_all_scales_table(struct iio_gts *gts, int *all_gains, int num_scales) +{ + int i, ret; + int *all_scales __free(kfree) = kcalloc(num_scales, 2 * sizeof(int), + GFP_KERNEL); - for (i = 0; i < gts->num_avail_all_scales; i++) { - ret = iio_gts_total_gain_to_scale(gts, all_gains[i], - >s->avail_all_scales_table[i * 2], - >s->avail_all_scales_table[i * 2 + 1]); + if (!all_scales) + return ERR_PTR(-ENOMEM); - if (ret) { - kfree(gts->avail_all_scales_table); - gts->num_avail_all_scales = 0; - return ret; - } + for (i = 0; i < num_scales; i++) { + ret = iio_gts_total_gain_to_scale(gts, all_gains[i], &all_scales[i * 2], + &all_scales[i * 2 + 1]); + if (ret) + return ERR_PTR(ret); } + return_ptr(all_scales); +} + +static int build_combined_tables(struct iio_gts *gts, + int **gains, size_t gain_bytes) +{ + int *all_scales, num_gains; + int *all_gains __free(kfree) = kcalloc(gts->num_itime, gain_bytes, + GFP_KERNEL); + + if (!all_gains) + return -ENOMEM; + + num_gains = combine_gain_tables(gts, gains, all_gains, gain_bytes); + + all_scales = build_all_scales_table(gts, all_gains, num_gains); + if (IS_ERR(all_scales)) + return PTR_ERR(all_scales); + + gts->num_avail_all_scales = num_gains; + gts->avail_all_scales_table = all_scales; + return 0; } @@ -269,7 +287,7 @@ static int gain_to_scaletables(struct iio_gts *gts, int **gains, int **scales) gain_bytes = array_size(gts->num_hwgain, sizeof(int)); - return build_combined_table(gts, gains, gain_bytes); + return build_combined_tables(gts, gains, gain_bytes); } /** From patchwork Thu Nov 28 16:51:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matti Vaittinen X-Patchwork-Id: 13888254 Received: from mail-lj1-f175.google.com (mail-lj1-f175.google.com [209.85.208.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 216391B85FA; Thu, 28 Nov 2024 16:51:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732812671; cv=none; b=qywKts/qVTsdSdlQPQ+ged2msCC4rUat+E+1FHV9GlOSCYczbno0AnSsk1EwTpuiVw1NJD3B1U3MN8ksVBlc9bMxxid2BeZ5a8XZEO6PDWfJm1QnGgQXzKPnNr8YTA8jAIKiKjhXC1uz2FIrlzQZuQ+pOb+DsMwDNl2FmBOHtHc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732812671; c=relaxed/simple; bh=HRu6qMThfqN+xQWI++pVUu9DTreaS3FHP8Mm5pJtibY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=rpMnwAjQEWn0x3lwaKbnREnX2jB3dVeRkMGACx89/rUqOdJXvTsS5Vm0MtS7IZEGVKdK2/eliPkarJpV3Z+hE4DmfO9uRKQI2w7E1ikl4NsOPuGrWqk+bsFViPGpKPbu/pGeyEVn1SVdXZA1pTRUtu5Vpg+cULGz5V9UCmJCPcQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=RmMRt/8c; arc=none smtp.client-ip=209.85.208.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="RmMRt/8c" Received: by mail-lj1-f175.google.com with SMTP id 38308e7fff4ca-2ffced84ba8so10618011fa.2; Thu, 28 Nov 2024 08:51:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1732812667; x=1733417467; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=0ndFM14y62Wb33fCpKZ8TUwn/mnlk0DqbbpdktEjptc=; b=RmMRt/8cAW3XWWNJRgPyqusIFrC8FEW06xmCSDs+exie//CDqt7aK47+OVjeG1byR+ RJ3pyvm07zrpm8s/Wb9lqvHvbAd27KeFp5YTT1UjYMinHrhwYk0grwgMmc0bUqwImYtr jKbsAxiPBO4Pxy5I7lnO2Q9CFbDzs44rQ4Ritv2okul5A/ql2H+W53lTbbA9Dz+6/yNH hU3Vw3wbyTGwrC0/pUR3I2M227NPMJXLe76F4GQODzlFLthPq//Ip6Hv7dqkFviFZS7z Kx8IAm0zdXYEs3s/kIoFax8CDlQS9HT509Hlg6U/yMaivBmqnf3PrPqx4FvF9QfDKxsX Ubew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732812667; x=1733417467; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=0ndFM14y62Wb33fCpKZ8TUwn/mnlk0DqbbpdktEjptc=; b=C2NZWpiDL3/kgHJ0jqAn70mGjOCYbZAEf+75H88A77dwGd8/1Zt2f+6iRhVtpumAZT /W9GxTUKaX2pPHFyNlLVMAelvrgK+32ghNbg1a7I261a1zA4fLXP3WI2Zxv0ZfFSr/G0 YarnmrYwaneQaX9YhqBDgyS7vy5rknnDgF+z/YFSFSLZ+mvQiE32+vdb+2UPi8t3zpC/ jPtDN38lWO7AsdJLBo1rOLh6vtOb5wk1LDxJoA70MN4w4swdTmfAftWWTqGhiXg63S65 NbDUQh8TCKoWrRNKaqtF0mxpBP2Rzzs26IGGDNieRc+ROLqZE49D4TWY985gnoMDHG1g SWqg== X-Forwarded-Encrypted: i=1; AJvYcCW5XWc7+FPiWCXuHRemxl29B0uvXTrgh0kf0eeZnj2kHslqTafJ/XimdZ78u+tx4kD6aRS++Sq4OrA=@vger.kernel.org, AJvYcCWnMtBNw7maJp2yP/fIfLjqhqVZjFr8qtOqS4TEPp5VtIdizDAtf30V5nPle2fuMMEj1tpyYu/ocvyIPwyw@vger.kernel.org X-Gm-Message-State: AOJu0Yw9W3ndv5n/pxHKFuaOzrRWOFokjZIHoIjnvtCWlZdKWRjuICk1 wfsDY1Fa1Qb8ZMe6xE+B89cJdHH/VEQp8jEEKgZ0X8AqgmNTEkc0 X-Gm-Gg: ASbGncscGHoNyb7sBZTgAVf+9P71+zsR/yHBCj59aX1EVmpfjMyTU2AUTGUdzfw7WtI +KEQeEdrBdnq/my5mh9cl8iNjSOX7/l5Kt5n7vjQxrMF1hMxXRtvlaM6Sl/DmHrqpLwdVzZuHIG ef/++/MRpemxwhrH69AF/5+CQWfMY6+VvmmC5nGcM3u4mbTqZmsyT2ux6yW6EG2BwJcy3p8STaw cVYxBAqTvVaRVcnXTgKV6OBSqF6nfLeIBk+yP5Hwnr4A2/HXEzp4Nya X-Google-Smtp-Source: AGHT+IHzTYzz902yDmwlDMHKjfLhepUqjiHHItwAowmu45zVAvKGfeRxa1/PtTpp9SscJz+lBJxJxg== X-Received: by 2002:a2e:bd06:0:b0:2ff:ca1a:14f5 with SMTP id 38308e7fff4ca-2ffd6020153mr48784121fa.17.1732812666993; Thu, 28 Nov 2024 08:51:06 -0800 (PST) Received: from mva-rohm ([2a10:a5c0:800d:dd00:8fdf:935a:2c85:d703]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-2ffdfca1249sm2321851fa.108.2024.11.28.08.51.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Nov 2024 08:51:04 -0800 (PST) Date: Thu, 28 Nov 2024 18:51:00 +0200 From: Matti Vaittinen To: Matti Vaittinen , Matti Vaittinen Cc: Matti Vaittinen , Jonathan Cameron , Lars-Peter Clausen , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 3/3] iio: gts: simplify scale table build Message-ID: <4b05448b65969f9f433f7ac3aa234c33025ad262.1732811829.git.mazziesaccount@gmail.com> References: Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: The GTS helpers offer two different set of "available scales" -tables. Drivers can choose to advertice the scales which are available on a currently selected integration time (by just changing the hwgain). Another option is to list all scales which can be supported using any of the integration times. This is useful for drivers which allow scale setting to also change the integration time to meet the scale user prefers. The helper function which build these tables for the GTS did firstbuild the "time specific" scale arrays for all the times. This is done by calculating the scales based on the integration time specific "total gain" arrays (gain contributed by both the integration time and hw-gain). After this the helper code calculates an array for all available scales. This is done combining all the time specific total-gains into one sorted array, removing dublicate gains and finally converting the gains to scales as above. This can be somewhat simplified by changing the logic for calculating the 'all available scales' -array to directly use the time specific scale arrays instead of time specific total-gain arrays. Code can directly just add all the already computed time specific scales to one big 'all scales'-array, keep it sorted and remove duplicates. Signed-off-by: Matti Vaittinen --- This has been tested by IIO-gts kunit tests only. All testing is appreciated. Comparing the scales is not as pretty as comparing the gains was, as scales are in two ints where the gains were in one. This makes the code slightly more hairy. I however believe that the logic is now more obvious. This might be more important for one reading this later... --- drivers/iio/industrialio-gts-helper.c | 109 ++++++++++---------------- 1 file changed, 42 insertions(+), 67 deletions(-) diff --git a/drivers/iio/industrialio-gts-helper.c b/drivers/iio/industrialio-gts-helper.c index 7f900f578f1d..31101848b194 100644 --- a/drivers/iio/industrialio-gts-helper.c +++ b/drivers/iio/industrialio-gts-helper.c @@ -191,86 +191,61 @@ static int fill_and_sort_scaletables(struct iio_gts *gts, int **gains, int **sca return 0; } -static int combine_gain_tables(struct iio_gts *gts, int **gains, - int *all_gains, size_t gain_bytes) +static int scale_eq(int *sc1, int *sc2) { - int i, new_idx, time_idx; + return *sc1 == *sc2 && *(sc1 + 1) == *(sc2 + 1); +} - /* - * We assume all the gains for same integration time were unique. - * It is likely the first time table had greatest time multiplier as - * the times are in the order of preference and greater times are - * usually preferred. Hence we start from the last table which is likely - * to have the smallest total gains. - */ - time_idx = gts->num_itime - 1; - memcpy(all_gains, gains[time_idx], gain_bytes); - new_idx = gts->num_hwgain; +static int scale_smaller(int *sc1, int *sc2) +{ + if (*sc1 != *sc2) + return *sc1 < *sc2; + + /* If integer parts are equal, fixp parts */ + return *(sc1 + 1) < *(sc2 + 1); +} + +static int do_combined_scaletable(struct iio_gts *gts, int **scales, size_t scale_bytes) +{ + int t_idx, i, new_idx; + int *all_scales = kcalloc(gts->num_itime, scale_bytes, GFP_KERNEL); - while (time_idx-- > 0) { - for (i = 0; i < gts->num_hwgain; i++) { - int candidate = gains[time_idx][i]; + if (!all_scales) + return -ENOMEM; + + t_idx = gts->num_itime - 1; + memcpy(all_scales, scales[t_idx], scale_bytes); + new_idx = gts->num_hwgain * 2; + + while (t_idx-- > 0) { + for (i = 0; i < gts->num_hwgain ; i++) { + int *candidate = &scales[t_idx][i * 2]; int chk; - if (candidate > all_gains[new_idx - 1]) { - all_gains[new_idx] = candidate; - new_idx++; + if (scale_smaller(candidate, &all_scales[new_idx - 2])) { + all_scales[new_idx] = *candidate; + all_scales[new_idx + 1] = *(candidate + 1); + new_idx += 2; continue; } - for (chk = 0; chk < new_idx; chk++) - if (candidate <= all_gains[chk]) + for (chk = 0; chk < new_idx; chk += 2) + if (!scale_smaller(candidate, &all_scales[chk])) break; - if (candidate == all_gains[chk]) + + if (scale_eq(candidate, &all_scales[chk])) continue; - memmove(&all_gains[chk + 1], &all_gains[chk], + memmove(&all_scales[chk + 2], &all_scales[chk], (new_idx - chk) * sizeof(int)); - all_gains[chk] = candidate; - new_idx++; + all_scales[chk] = *candidate; + all_scales[chk + 1] = *(candidate + 1); + new_idx += 2; } } - return new_idx; -} - -static int *build_all_scales_table(struct iio_gts *gts, int *all_gains, int num_scales) -{ - int i, ret; - int *all_scales __free(kfree) = kcalloc(num_scales, 2 * sizeof(int), - GFP_KERNEL); - - if (!all_scales) - return ERR_PTR(-ENOMEM); - - for (i = 0; i < num_scales; i++) { - ret = iio_gts_total_gain_to_scale(gts, all_gains[i], &all_scales[i * 2], - &all_scales[i * 2 + 1]); - if (ret) - return ERR_PTR(ret); - } - - return_ptr(all_scales); -} - -static int build_combined_tables(struct iio_gts *gts, - int **gains, size_t gain_bytes) -{ - int *all_scales, num_gains; - int *all_gains __free(kfree) = kcalloc(gts->num_itime, gain_bytes, - GFP_KERNEL); - - if (!all_gains) - return -ENOMEM; - - num_gains = combine_gain_tables(gts, gains, all_gains, gain_bytes); - - all_scales = build_all_scales_table(gts, all_gains, num_gains); - if (IS_ERR(all_scales)) - return PTR_ERR(all_scales); - - gts->num_avail_all_scales = num_gains; + gts->num_avail_all_scales = new_idx / 2; gts->avail_all_scales_table = all_scales; return 0; @@ -279,15 +254,15 @@ static int build_combined_tables(struct iio_gts *gts, static int gain_to_scaletables(struct iio_gts *gts, int **gains, int **scales) { int ret; - size_t gain_bytes; + size_t scale_bytes; ret = fill_and_sort_scaletables(gts, gains, scales); if (ret) return ret; - gain_bytes = array_size(gts->num_hwgain, sizeof(int)); + scale_bytes = array_size(gts->num_hwgain, 2 * sizeof(int)); - return build_combined_tables(gts, gains, gain_bytes); + return do_combined_scaletable(gts, scales, scale_bytes); } /**