From patchwork Mon Jan 10 18:17:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Shevchenko X-Patchwork-Id: 12709023 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7EBB6C433EF for ; Mon, 10 Jan 2022 18:17:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238921AbiAJSRJ (ORCPT ); Mon, 10 Jan 2022 13:17:09 -0500 Received: from mga05.intel.com ([192.55.52.43]:29178 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238848AbiAJSRG (ORCPT ); Mon, 10 Jan 2022 13:17:06 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1641838626; x=1673374626; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=n2VU3KcLS5khnCS9/8zTHtmWmBczUON+c54f03UlPBw=; b=Ii9FVykU6s1pNH/x+8wQhwbOQPqWrBM+i9q+L2KLnXQwArib72EKq1oD 6g7FSJtzBpM9uW7X5T00fKPDAAQ0YYlbYCq163YIqZ0DtKWmioAxHntnC Fkyrf0J2+w5ZMxZRm991C/7R+Zs2NU4xqtiqSnT9KFbB4HXPe9V1DatYz EeYDU79N4tGoopcmCE/IHZv4Zdj8LXwOSpQ8wks2FhwcA8veFRScoWRtr 6nrJK08m+dzb+NC9gdt85k+24JtFO77xn1w0rkmaEJIplqqE7/vPxX058 gPOtjIWUt7QBsOFLkJApx1GNNQa4AKuS8If2KmMvD3wR5/GCE2hGAWvnC A==; X-IronPort-AV: E=McAfee;i="6200,9189,10222"; a="329630526" X-IronPort-AV: E=Sophos;i="5.88,277,1635231600"; d="scan'208";a="329630526" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2022 10:17:04 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.88,277,1635231600"; d="scan'208";a="622759584" Received: from black.fi.intel.com ([10.237.72.28]) by orsmga004.jf.intel.com with ESMTP; 10 Jan 2022 10:17:01 -0800 Received: by black.fi.intel.com (Postfix, from userid 1003) id C9ED3120; Mon, 10 Jan 2022 20:17:12 +0200 (EET) From: Andy Shevchenko To: Dmitry Baryshkov , Jonathan Cameron , Andreas Kemnade , Andy Shevchenko , linux-arm-msm@vger.kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Andy Gross , Bjorn Andersson , Jonathan Cameron , Lars-Peter Clausen , Peter Rosin Subject: [PATCH v1 1/5] math.h: Introduce data types for fractional numbers Date: Mon, 10 Jan 2022 20:17:07 +0200 Message-Id: <20220110181711.65054-1-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org Introduce a macro to produce data types like struct TYPE_fract { __TYPE numerator; __TYPE denominator; }; to be used in the code wherever it's needed. In the following changes convert some users to it. Signed-off-by: Andy Shevchenko --- May be pulled via IIO tree. include/linux/math.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/include/linux/math.h b/include/linux/math.h index 53674a327e39..439b8f0b9ebd 100644 --- a/include/linux/math.h +++ b/include/linux/math.h @@ -2,6 +2,7 @@ #ifndef _LINUX_MATH_H #define _LINUX_MATH_H +#include #include #include @@ -106,6 +107,17 @@ } \ ) +#define __STRUCT_FRACT(type) \ +struct type##_fract { \ + __##type numerator; \ + __##type denominator; \ +}; +__STRUCT_FRACT(s16) +__STRUCT_FRACT(u16) +__STRUCT_FRACT(s32) +__STRUCT_FRACT(u32) +#undef __STRUCT_FRACT + /* * Multiplies an integer by a fraction, while avoiding unnecessary * overflow or loss of precision. From patchwork Mon Jan 10 18:17:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Shevchenko X-Patchwork-Id: 12709025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3F52EC43217 for ; Mon, 10 Jan 2022 18:17:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238932AbiAJSRK (ORCPT ); Mon, 10 Jan 2022 13:17:10 -0500 Received: from mga09.intel.com ([134.134.136.24]:52096 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238905AbiAJSRG (ORCPT ); Mon, 10 Jan 2022 13:17:06 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1641838626; x=1673374626; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=A/zBlgpho5sICynBSgHlKGJaa3VuSdECcydKaQ/t4dA=; b=HF7fmspbg4hSY5nbN6ETjXz26GwW7WsmOZglH45OxZy8ErMvvlM0R0uV V072EJbzhMXfsbBN1Q7rqezyoiSAKFq6hM33TA1acF9lOBcqdIAAHD3TE 7Ju9ZlYL59jKX/uh3AfBxz0+jDpTMhTlyu4H+gfibtMqt6XPR1anKUVl9 9haV4yHCmuUSLZnzGelIGg3NZQq04ndgIBu8/BBXeqlFD2A/kjO5k0dAl OnCjvbNnxLS9MWsm6Wn7Kdk8yZSyp0i+mMsoaEm+M0Ty8/R1YBblUL+3s 1y+lAXu79952WBg0LyM71+ATc6HCPVklripET70MHkpdQTOimW2ndeKDY Q==; X-IronPort-AV: E=McAfee;i="6200,9189,10222"; a="243083582" X-IronPort-AV: E=Sophos;i="5.88,277,1635231600"; d="scan'208";a="243083582" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2022 10:17:05 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.88,277,1635231600"; d="scan'208";a="669528410" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga001.fm.intel.com with ESMTP; 10 Jan 2022 10:17:01 -0800 Received: by black.fi.intel.com (Postfix, from userid 1003) id D905ED2; Mon, 10 Jan 2022 20:17:12 +0200 (EET) From: Andy Shevchenko To: Dmitry Baryshkov , Jonathan Cameron , Andreas Kemnade , Andy Shevchenko , linux-arm-msm@vger.kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Andy Gross , Bjorn Andersson , Jonathan Cameron , Lars-Peter Clausen , Peter Rosin Subject: [PATCH v1 2/5] iio: adc: rn5t618: Re-use generic struct u16_fract Date: Mon, 10 Jan 2022 20:17:08 +0200 Message-Id: <20220110181711.65054-2-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220110181711.65054-1-andriy.shevchenko@linux.intel.com> References: <20220110181711.65054-1-andriy.shevchenko@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org Instead of custom data type re-use generic struct u16_fract. No changes intended. Signed-off-by: Andy Shevchenko --- drivers/iio/adc/rn5t618-adc.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/drivers/iio/adc/rn5t618-adc.c b/drivers/iio/adc/rn5t618-adc.c index 7d891b4ea461..6bf32907f01d 100644 --- a/drivers/iio/adc/rn5t618-adc.c +++ b/drivers/iio/adc/rn5t618-adc.c @@ -42,11 +42,6 @@ struct rn5t618_adc_data { int irq; }; -struct rn5t618_channel_ratios { - u16 numerator; - u16 denominator; -}; - enum rn5t618_channels { LIMMON = 0, VBAT, @@ -58,7 +53,7 @@ enum rn5t618_channels { AIN0 }; -static const struct rn5t618_channel_ratios rn5t618_ratios[8] = { +static const struct u16_fract rn5t618_ratios[8] = { [LIMMON] = {50, 32}, /* measured across 20mOhm, amplified by 32 */ [VBAT] = {2, 1}, [VADP] = {3, 1}, From patchwork Mon Jan 10 18:17:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Shevchenko X-Patchwork-Id: 12709024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CC1FDC4332F for ; Mon, 10 Jan 2022 18:17:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238907AbiAJSRJ (ORCPT ); Mon, 10 Jan 2022 13:17:09 -0500 Received: from mga04.intel.com ([192.55.52.120]:15381 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238903AbiAJSRG (ORCPT ); Mon, 10 Jan 2022 13:17:06 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1641838626; x=1673374626; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=rhbG96ZN/wAY6OGe8M6N4jiC1NAS2+lq1p40m3Bi1G0=; b=N5Gus2AGnYZpI6TsuhaojCslrO+y/985I80jiUH+tv0FccBMGDClioDK FjQmtbjsw6pd5HL/qtaTns9Jd0wBTuDdVzP0stVa3684SjOzeCeATEDxO xlt2G84LJK+4fVtzMlrfWExp+2VasrQhWQH04hqwuQ2TSmWdvUDbGQ3bw 0d6Ke2EHb+XcOiucKBcP1ni5qVfOm+W2pkHySgOSqWOm6auk5G446h0KA TfLpFFRkKc9+dtxHECv5/UoN2fnIik0Ug8B51VVYQA43LBquHsYQbMj2b q2aM2Z2oMmzNl7JX+wEElkjuYGosUnc6AL5SSnYlE1P3O1+rRiEVy70U4 w==; X-IronPort-AV: E=McAfee;i="6200,9189,10222"; a="242101549" X-IronPort-AV: E=Sophos;i="5.88,277,1635231600"; d="scan'208";a="242101549" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2022 10:17:04 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.88,277,1635231600"; d="scan'208";a="592430090" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga004.fm.intel.com with ESMTP; 10 Jan 2022 10:17:01 -0800 Received: by black.fi.intel.com (Postfix, from userid 1003) id DEC7E125; Mon, 10 Jan 2022 20:17:12 +0200 (EET) From: Andy Shevchenko To: Dmitry Baryshkov , Jonathan Cameron , Andreas Kemnade , Andy Shevchenko , linux-arm-msm@vger.kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Andy Gross , Bjorn Andersson , Jonathan Cameron , Lars-Peter Clausen , Peter Rosin Subject: [PATCH v1 3/5] iio: adc: twl4030-madc: Re-use generic struct s16_fract Date: Mon, 10 Jan 2022 20:17:09 +0200 Message-Id: <20220110181711.65054-3-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220110181711.65054-1-andriy.shevchenko@linux.intel.com> References: <20220110181711.65054-1-andriy.shevchenko@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org Instead of custom data type re-use generic struct s16_fract. No changes intended. Signed-off-by: Andy Shevchenko --- drivers/iio/adc/twl4030-madc.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/drivers/iio/adc/twl4030-madc.c b/drivers/iio/adc/twl4030-madc.c index 6ce40cc4568a..f8f8aea15612 100644 --- a/drivers/iio/adc/twl4030-madc.c +++ b/drivers/iio/adc/twl4030-madc.c @@ -231,13 +231,7 @@ static const struct iio_chan_spec twl4030_madc_iio_channels[] = { static struct twl4030_madc_data *twl4030_madc; -struct twl4030_prescale_divider_ratios { - s16 numerator; - s16 denominator; -}; - -static const struct twl4030_prescale_divider_ratios -twl4030_divider_ratios[16] = { +static const struct s16_fract twl4030_divider_ratios[16] = { {1, 1}, /* CHANNEL 0 No Prescaler */ {1, 1}, /* CHANNEL 1 No Prescaler */ {6, 10}, /* CHANNEL 2 */ @@ -256,7 +250,6 @@ twl4030_divider_ratios[16] = { {5, 11}, /* CHANNEL 15 */ }; - /* Conversion table from -3 to 55 degrees Celcius */ static int twl4030_therm_tbl[] = { 30800, 29500, 28300, 27100, From patchwork Mon Jan 10 18:17:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Shevchenko X-Patchwork-Id: 12709026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2F52AC4321E for ; Mon, 10 Jan 2022 18:17:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238916AbiAJSRL (ORCPT ); Mon, 10 Jan 2022 13:17:11 -0500 Received: from mga11.intel.com ([192.55.52.93]:39573 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238898AbiAJSRG (ORCPT ); Mon, 10 Jan 2022 13:17:06 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1641838626; x=1673374626; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Q9s5Du4f2yZLctsj3Xd+IrsaX7TqFktojLsiV/xqgnM=; b=aM2W94P2miigeQM/kda+DIg3lovQXilzLf1YCp25firx4Y/nSs+DIwfM 8ArXcma3gQeUTXj3T2urEFm//pm5YHwSmHn+EZCMyMoLNn4+OLJoAqNbb W7X91gP6w1g90fzRhsfQvRU7WRl6zpC46D0Oro2ULbl0peJH4+SnWgjSq FK3y3uZ5BR1cPDbYV7NCFUq3rsVQLmaeSCCVVBomgjrQrZym7h41Ofr+J M1HnPLIDbuThCB9L80zEzT5czt4WT+4HiQWC0h5sgfblXS2gfokrlIj5b i4LKrjcH5vdNJNVK9qu45pG7tpkfRJMYW5wHFW8Wdy9bZI1M3En0TtkP+ w==; X-IronPort-AV: E=McAfee;i="6200,9189,10222"; a="240839567" X-IronPort-AV: E=Sophos;i="5.88,277,1635231600"; d="scan'208";a="240839567" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2022 10:17:05 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.88,277,1635231600"; d="scan'208";a="514765365" Received: from black.fi.intel.com ([10.237.72.28]) by orsmga007.jf.intel.com with ESMTP; 10 Jan 2022 10:17:01 -0800 Received: by black.fi.intel.com (Postfix, from userid 1003) id EED36232; Mon, 10 Jan 2022 20:17:12 +0200 (EET) From: Andy Shevchenko To: Dmitry Baryshkov , Jonathan Cameron , Andreas Kemnade , Andy Shevchenko , linux-arm-msm@vger.kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Andy Gross , Bjorn Andersson , Jonathan Cameron , Lars-Peter Clausen , Peter Rosin Subject: [PATCH v1 4/5] iio: adc: qcom-vadc-common: Re-use generic struct u32_fract Date: Mon, 10 Jan 2022 20:17:10 +0200 Message-Id: <20220110181711.65054-4-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220110181711.65054-1-andriy.shevchenko@linux.intel.com> References: <20220110181711.65054-1-andriy.shevchenko@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org Instead of custom data type re-use generic struct u32_fract. No changes intended. Signed-off-by: Andy Shevchenko --- drivers/iio/adc/qcom-pm8xxx-xoadc.c | 2 +- drivers/iio/adc/qcom-spmi-vadc.c | 2 +- drivers/iio/adc/qcom-vadc-common.c | 48 ++++++++++++------------ include/linux/iio/adc/qcom-vadc-common.h | 15 ++------ 4 files changed, 29 insertions(+), 38 deletions(-) diff --git a/drivers/iio/adc/qcom-pm8xxx-xoadc.c b/drivers/iio/adc/qcom-pm8xxx-xoadc.c index 21d7eff645c3..ef249d745d85 100644 --- a/drivers/iio/adc/qcom-pm8xxx-xoadc.c +++ b/drivers/iio/adc/qcom-pm8xxx-xoadc.c @@ -175,7 +175,7 @@ struct xoadc_channel { const char *datasheet_name; u8 pre_scale_mux:2; u8 amux_channel:4; - const struct vadc_prescale_ratio prescale; + const struct u32_fract prescale; enum iio_chan_type type; enum vadc_scale_fn_type scale_fn_type; u8 amux_ip_rsv:3; diff --git a/drivers/iio/adc/qcom-spmi-vadc.c b/drivers/iio/adc/qcom-spmi-vadc.c index 07b1a99381d9..69733564fa42 100644 --- a/drivers/iio/adc/qcom-spmi-vadc.c +++ b/drivers/iio/adc/qcom-spmi-vadc.c @@ -122,7 +122,7 @@ struct vadc_priv { struct mutex lock; }; -static const struct vadc_prescale_ratio vadc_prescale_ratios[] = { +static const struct u32_fract vadc_prescale_ratios[] = { {.num = 1, .den = 1}, {.num = 1, .den = 3}, {.num = 1, .den = 4}, diff --git a/drivers/iio/adc/qcom-vadc-common.c b/drivers/iio/adc/qcom-vadc-common.c index 14723896aab2..ec177360a354 100644 --- a/drivers/iio/adc/qcom-vadc-common.c +++ b/drivers/iio/adc/qcom-vadc-common.c @@ -289,7 +289,7 @@ static const struct vadc_map_pt adcmap7_100k[] = { { 2420, 130048 } }; -static const struct vadc_prescale_ratio adc5_prescale_ratios[] = { +static const struct u32_fract adc5_prescale_ratios[] = { {.num = 1, .den = 1}, {.num = 1, .den = 3}, {.num = 1, .den = 4}, @@ -302,31 +302,31 @@ static const struct vadc_prescale_ratio adc5_prescale_ratios[] = { }; static int qcom_vadc_scale_hw_calib_volt( - const struct vadc_prescale_ratio *prescale, + const struct u32_fract *prescale, const struct adc5_data *data, u16 adc_code, int *result_uv); static int qcom_vadc_scale_hw_calib_therm( - const struct vadc_prescale_ratio *prescale, + const struct u32_fract *prescale, const struct adc5_data *data, u16 adc_code, int *result_mdec); static int qcom_vadc7_scale_hw_calib_therm( - const struct vadc_prescale_ratio *prescale, + const struct u32_fract *prescale, const struct adc5_data *data, u16 adc_code, int *result_mdec); static int qcom_vadc_scale_hw_smb_temp( - const struct vadc_prescale_ratio *prescale, + const struct u32_fract *prescale, const struct adc5_data *data, u16 adc_code, int *result_mdec); static int qcom_vadc_scale_hw_chg5_temp( - const struct vadc_prescale_ratio *prescale, + const struct u32_fract *prescale, const struct adc5_data *data, u16 adc_code, int *result_mdec); static int qcom_vadc_scale_hw_calib_die_temp( - const struct vadc_prescale_ratio *prescale, + const struct u32_fract *prescale, const struct adc5_data *data, u16 adc_code, int *result_mdec); static int qcom_vadc7_scale_hw_calib_die_temp( - const struct vadc_prescale_ratio *prescale, + const struct u32_fract *prescale, const struct adc5_data *data, u16 adc_code, int *result_mdec); @@ -406,7 +406,7 @@ static void qcom_vadc_scale_calib(const struct vadc_linear_graph *calib_graph, } static int qcom_vadc_scale_volt(const struct vadc_linear_graph *calib_graph, - const struct vadc_prescale_ratio *prescale, + const struct u32_fract *prescale, bool absolute, u16 adc_code, int *result_uv) { @@ -422,7 +422,7 @@ static int qcom_vadc_scale_volt(const struct vadc_linear_graph *calib_graph, } static int qcom_vadc_scale_therm(const struct vadc_linear_graph *calib_graph, - const struct vadc_prescale_ratio *prescale, + const struct u32_fract *prescale, bool absolute, u16 adc_code, int *result_mdec) { @@ -444,7 +444,7 @@ static int qcom_vadc_scale_therm(const struct vadc_linear_graph *calib_graph, } static int qcom_vadc_scale_die_temp(const struct vadc_linear_graph *calib_graph, - const struct vadc_prescale_ratio *prescale, + const struct u32_fract *prescale, bool absolute, u16 adc_code, int *result_mdec) { @@ -467,7 +467,7 @@ static int qcom_vadc_scale_die_temp(const struct vadc_linear_graph *calib_graph, } static int qcom_vadc_scale_chg_temp(const struct vadc_linear_graph *calib_graph, - const struct vadc_prescale_ratio *prescale, + const struct u32_fract *prescale, bool absolute, u16 adc_code, int *result_mdec) { @@ -487,7 +487,7 @@ static int qcom_vadc_scale_chg_temp(const struct vadc_linear_graph *calib_graph, /* convert voltage to ADC code, using 1.875V reference */ static u16 qcom_vadc_scale_voltage_code(s32 voltage, - const struct vadc_prescale_ratio *prescale, + const struct u32_fract *prescale, const u32 full_scale_code_volt, unsigned int factor) { @@ -501,7 +501,7 @@ static u16 qcom_vadc_scale_voltage_code(s32 voltage, } static int qcom_vadc_scale_code_voltage_factor(u16 adc_code, - const struct vadc_prescale_ratio *prescale, + const struct u32_fract *prescale, const struct adc5_data *data, unsigned int factor) { @@ -531,7 +531,7 @@ static int qcom_vadc_scale_code_voltage_factor(u16 adc_code, } static int qcom_vadc7_scale_hw_calib_therm( - const struct vadc_prescale_ratio *prescale, + const struct u32_fract *prescale, const struct adc5_data *data, u16 adc_code, int *result_mdec) { @@ -557,7 +557,7 @@ static int qcom_vadc7_scale_hw_calib_therm( } static int qcom_vadc_scale_hw_calib_volt( - const struct vadc_prescale_ratio *prescale, + const struct u32_fract *prescale, const struct adc5_data *data, u16 adc_code, int *result_uv) { @@ -568,7 +568,7 @@ static int qcom_vadc_scale_hw_calib_volt( } static int qcom_vadc_scale_hw_calib_therm( - const struct vadc_prescale_ratio *prescale, + const struct u32_fract *prescale, const struct adc5_data *data, u16 adc_code, int *result_mdec) { @@ -584,7 +584,7 @@ static int qcom_vadc_scale_hw_calib_therm( } static int qcom_vadc_scale_hw_calib_die_temp( - const struct vadc_prescale_ratio *prescale, + const struct u32_fract *prescale, const struct adc5_data *data, u16 adc_code, int *result_mdec) { @@ -596,7 +596,7 @@ static int qcom_vadc_scale_hw_calib_die_temp( } static int qcom_vadc7_scale_hw_calib_die_temp( - const struct vadc_prescale_ratio *prescale, + const struct u32_fract *prescale, const struct adc5_data *data, u16 adc_code, int *result_mdec) { @@ -611,7 +611,7 @@ static int qcom_vadc7_scale_hw_calib_die_temp( } static int qcom_vadc_scale_hw_smb_temp( - const struct vadc_prescale_ratio *prescale, + const struct u32_fract *prescale, const struct adc5_data *data, u16 adc_code, int *result_mdec) { @@ -623,7 +623,7 @@ static int qcom_vadc_scale_hw_smb_temp( } static int qcom_vadc_scale_hw_chg5_temp( - const struct vadc_prescale_ratio *prescale, + const struct u32_fract *prescale, const struct adc5_data *data, u16 adc_code, int *result_mdec) { @@ -636,7 +636,7 @@ static int qcom_vadc_scale_hw_chg5_temp( int qcom_vadc_scale(enum vadc_scale_fn_type scaletype, const struct vadc_linear_graph *calib_graph, - const struct vadc_prescale_ratio *prescale, + const struct u32_fract *prescale, bool absolute, u16 adc_code, int *result) { @@ -667,7 +667,7 @@ EXPORT_SYMBOL(qcom_vadc_scale); u16 qcom_adc_tm5_temp_volt_scale(unsigned int prescale_ratio, u32 full_scale_code_volt, int temp) { - const struct vadc_prescale_ratio *prescale = &adc5_prescale_ratios[prescale_ratio]; + const struct u32_fract *prescale = &adc5_prescale_ratios[prescale_ratio]; s32 voltage; voltage = qcom_vadc_map_temp_voltage(adcmap_100k_104ef_104fb_1875_vref, @@ -682,7 +682,7 @@ int qcom_adc5_hw_scale(enum vadc_scale_fn_type scaletype, const struct adc5_data *data, u16 adc_code, int *result) { - const struct vadc_prescale_ratio *prescale = &adc5_prescale_ratios[prescale_ratio]; + const struct u32_fract *prescale = &adc5_prescale_ratios[prescale_ratio]; if (!(scaletype >= SCALE_HW_CALIB_DEFAULT && scaletype < SCALE_HW_CALIB_INVALID)) { diff --git a/include/linux/iio/adc/qcom-vadc-common.h b/include/linux/iio/adc/qcom-vadc-common.h index 33f60f43e1aa..ce78d4804994 100644 --- a/include/linux/iio/adc/qcom-vadc-common.h +++ b/include/linux/iio/adc/qcom-vadc-common.h @@ -6,6 +6,7 @@ #ifndef QCOM_VADC_COMMON_H #define QCOM_VADC_COMMON_H +#include #include #define VADC_CONV_TIME_MIN_US 2000 @@ -79,16 +80,6 @@ struct vadc_linear_graph { s32 gnd; }; -/** - * struct vadc_prescale_ratio - Represent scaling ratio for ADC input. - * @num: the inverse numerator of the gain applied to the input channel. - * @den: the inverse denominator of the gain applied to the input channel. - */ -struct vadc_prescale_ratio { - u32 num; - u32 den; -}; - /** * enum vadc_scale_fn_type - Scaling function to convert ADC code to * physical scaled units for the channel. @@ -144,12 +135,12 @@ struct adc5_data { int qcom_vadc_scale(enum vadc_scale_fn_type scaletype, const struct vadc_linear_graph *calib_graph, - const struct vadc_prescale_ratio *prescale, + const struct u32_fract *prescale, bool absolute, u16 adc_code, int *result_mdec); struct qcom_adc5_scale_type { - int (*scale_fn)(const struct vadc_prescale_ratio *prescale, + int (*scale_fn)(const struct u32_fract *prescale, const struct adc5_data *data, u16 adc_code, int *result); }; From patchwork Mon Jan 10 18:17:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Shevchenko X-Patchwork-Id: 12709027 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EE3B8C433FE for ; Mon, 10 Jan 2022 18:17:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239019AbiAJSR2 (ORCPT ); Mon, 10 Jan 2022 13:17:28 -0500 Received: from mga14.intel.com ([192.55.52.115]:57695 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239000AbiAJSRX (ORCPT ); Mon, 10 Jan 2022 13:17:23 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1641838643; x=1673374643; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=PSwVGlkEI3FgSeZMt1RvAaY33EihOKIIX2W1GKytUds=; b=UjQI4GOgq1X4N1YoMZIxwzJIgPJ/s0rUmVEF8+f/dc4xmOzpKi3vpCpW 0hBIXhsRUlMA0QV+zb49H/Af+dglr3o7yHLh644MaFacQReUoi3pVnxo9 2ONvyEdbSMM8qiGtfb/meltO4Kqw7/z5EaSWmZsgHdD8xV8h/8eyV828l 4Csyr7HDqGIEfJw+Nhb/MX/m2EHII4aJNvkVQEb0wC5jjK3WfkD6cb+p6 eOcGOGK5cCpAuHJcYHk2tbGZr9FGe8FHiwQop7Av1+Ojvt7IbxYq3aRa+ cZt2VlNV0OP5/LB7iXDXGtKwD31XrlWZRd5sWLm/mE1n3o6A6pJFsDfUt A==; X-IronPort-AV: E=McAfee;i="6200,9189,10222"; a="243486380" X-IronPort-AV: E=Sophos;i="5.88,277,1635231600"; d="scan'208";a="243486380" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2022 10:17:05 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.88,277,1635231600"; d="scan'208";a="690665057" Received: from black.fi.intel.com ([10.237.72.28]) by orsmga005.jf.intel.com with ESMTP; 10 Jan 2022 10:17:01 -0800 Received: by black.fi.intel.com (Postfix, from userid 1003) id 06D952C7; Mon, 10 Jan 2022 20:17:12 +0200 (EET) From: Andy Shevchenko To: Dmitry Baryshkov , Jonathan Cameron , Andreas Kemnade , Andy Shevchenko , linux-arm-msm@vger.kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Andy Gross , Bjorn Andersson , Jonathan Cameron , Lars-Peter Clausen , Peter Rosin Subject: [PATCH v1 5/5] iio: afe: iio-rescale: Re-use generic struct s32_fract Date: Mon, 10 Jan 2022 20:17:11 +0200 Message-Id: <20220110181711.65054-5-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220110181711.65054-1-andriy.shevchenko@linux.intel.com> References: <20220110181711.65054-1-andriy.shevchenko@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org Instead of custom data type re-use generic struct s32_fract. No changes intended. The new member is put to be the first one to avoid additional pointer arithmetic. Besides that one may switch to use fract member to perform container_of(), which will be no-op in this case, to get struct rescale. Signed-off-by: Andy Shevchenko --- I found this better in order how code is structurally (re)organized. I may rebase this on top of ongoing AFE series. Also reveals possibility to switch to rational best approximation. But this is another story... drivers/iio/afe/iio-rescale.c | 74 +++++++++++++++++------------------ 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index 774eb3044edd..0368bca8a485 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -21,17 +22,16 @@ struct rescale; struct rescale_cfg { enum iio_chan_type type; - int (*props)(struct device *dev, struct rescale *rescale); + int (*props)(struct device *dev, struct s32_fract *fract); }; struct rescale { + struct s32_fract fract; const struct rescale_cfg *cfg; struct iio_channel *source; struct iio_chan_spec chan; struct iio_chan_spec_ext_info *ext_info; bool chan_processed; - s32 numerator; - s32 denominator; }; static int rescale_read_raw(struct iio_dev *indio_dev, @@ -39,6 +39,7 @@ static int rescale_read_raw(struct iio_dev *indio_dev, int *val, int *val2, long mask) { struct rescale *rescale = iio_priv(indio_dev); + struct s32_fract *fract = &rescale->fract; unsigned long long tmp; int ret; @@ -67,19 +68,19 @@ static int rescale_read_raw(struct iio_dev *indio_dev, } switch (ret) { case IIO_VAL_FRACTIONAL: - *val *= rescale->numerator; - *val2 *= rescale->denominator; + *val *= fract->numerator; + *val2 *= fract->denominator; return ret; case IIO_VAL_INT: - *val *= rescale->numerator; - if (rescale->denominator == 1) + *val *= fract->numerator; + if (fract->denominator == 1) return ret; - *val2 = rescale->denominator; + *val2 = fract->denominator; return IIO_VAL_FRACTIONAL; case IIO_VAL_FRACTIONAL_LOG2: tmp = *val * 1000000000LL; - do_div(tmp, rescale->denominator); - tmp *= rescale->numerator; + do_div(tmp, fract->denominator); + tmp *= fract->numerator; do_div(tmp, 1000000000LL); *val = tmp; return ret; @@ -175,7 +176,7 @@ static int rescale_configure_channel(struct device *dev, } static int rescale_current_sense_amplifier_props(struct device *dev, - struct rescale *rescale) + struct s32_fract *fract) { u32 sense; u32 gain_mult = 1; @@ -199,22 +200,22 @@ static int rescale_current_sense_amplifier_props(struct device *dev, * numerator/denominator from overflowing. */ factor = gcd(sense, 1000000); - rescale->numerator = 1000000 / factor; - rescale->denominator = sense / factor; + fract->numerator = 1000000 / factor; + fract->denominator = sense / factor; - factor = gcd(rescale->numerator, gain_mult); - rescale->numerator /= factor; - rescale->denominator *= gain_mult / factor; + factor = gcd(fract->numerator, gain_mult); + fract->numerator /= factor; + fract->denominator *= gain_mult / factor; - factor = gcd(rescale->denominator, gain_div); - rescale->numerator *= gain_div / factor; - rescale->denominator /= factor; + factor = gcd(fract->denominator, gain_div); + fract->numerator *= gain_div / factor; + fract->denominator /= factor; return 0; } static int rescale_current_sense_shunt_props(struct device *dev, - struct rescale *rescale) + struct s32_fract *fract) { u32 shunt; u32 factor; @@ -228,35 +229,33 @@ static int rescale_current_sense_shunt_props(struct device *dev, } factor = gcd(shunt, 1000000); - rescale->numerator = 1000000 / factor; - rescale->denominator = shunt / factor; + fract->numerator = 1000000 / factor; + fract->denominator = shunt / factor; return 0; } static int rescale_voltage_divider_props(struct device *dev, - struct rescale *rescale) + struct s32_fract *fract) { int ret; u32 factor; - ret = device_property_read_u32(dev, "output-ohms", - &rescale->denominator); + ret = device_property_read_u32(dev, "output-ohms", &fract->denominator); if (ret) { dev_err(dev, "failed to read output-ohms: %d\n", ret); return ret; } - ret = device_property_read_u32(dev, "full-ohms", - &rescale->numerator); + ret = device_property_read_u32(dev, "full-ohms", &fract->numerator); if (ret) { dev_err(dev, "failed to read full-ohms: %d\n", ret); return ret; } - factor = gcd(rescale->numerator, rescale->denominator); - rescale->numerator /= factor; - rescale->denominator /= factor; + factor = gcd(fract->numerator, fract->denominator); + fract->numerator /= factor; + fract->denominator /= factor; return 0; } @@ -299,6 +298,7 @@ static int rescale_probe(struct platform_device *pdev) struct iio_dev *indio_dev; struct iio_channel *source; struct rescale *rescale; + struct s32_fract *fract; int sizeof_ext_info; int sizeof_priv; int i; @@ -322,24 +322,24 @@ static int rescale_probe(struct platform_device *pdev) return -ENOMEM; rescale = iio_priv(indio_dev); - + rescale->source = source; rescale->cfg = of_device_get_match_data(dev); - rescale->numerator = 1; - rescale->denominator = 1; - ret = rescale->cfg->props(dev, rescale); + fract = &rescale->fract; + fract->numerator = 1; + fract->denominator = 1; + + ret = rescale->cfg->props(dev, fract); if (ret) return ret; - if (!rescale->numerator || !rescale->denominator) { + if (!fract->numerator || !fract->denominator) { dev_err(dev, "invalid scaling factor.\n"); return -EINVAL; } platform_set_drvdata(pdev, indio_dev); - rescale->source = source; - indio_dev->name = dev_name(dev); indio_dev->info = &rescale_info; indio_dev->modes = INDIO_DIRECT_MODE;