From mboxrd@z Thu Jan 1 00:00:00 1970 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A7F12CD98F2 for ; Tue, 23 Jun 2026 11:00:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type: Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date :Subject:CC:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=pxFM0lTsEtFYr6BI0qi5LbG4nGtsOYz3f1RawXZ8iD8=; b=1SehHXFa2W8ab96GmR5TL0kZNv xjh3uyjrfjoE+bCNLOlxxVPaeX4beXKoDE2W6hdMT4QpgYytOFshWQgofqMMm0oHJaVAeLNp+l1od VjwxSDNBg+GbF7122D9e6Kxpg+fl8VO8+tDHcoPpVg5d4NxLPp4cXvu7W87/3eF27kS1pIK3gIhDk QhUgbF5hzt6jFPqo4277vk0q6rZSN+RUwzj8XGAxKXWjoByLBS3785HcXLe8OgTVz9+uVPEHKO4vn a8D72EddAfiMPaMFhgCO9P3ujCCmon5FkDAhC40AlRwrC6N1uey16PsaXhpTo7AgDQg4OprCiXyRG NdwyEvCg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.99.1 #2 (Red Hat Linux)) id 1wbyrW-000000067qi-03Xl; Tue, 23 Jun 2026 11:00:14 +0000 Received: from esa.microchip.iphmx.com ([68.232.153.233]) by bombadil.infradead.org with esmtps (Exim 4.99.1 #2 (Red Hat Linux)) id 1wbyrR-000000067oO-1C3c for linux-arm-kernel@lists.infradead.org; Tue, 23 Jun 2026 11:00:10 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=microchip.com; i=@microchip.com; q=dns/txt; s=mchp; t=1782212409; x=1813748409; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ze5tZw3w4M56ltt1q/sTy28a3TwaZwr09+r3Mhm+2F4=; b=aPiETp4wAMAMZnLUGPr3+hKy+GbHZVprzoXk/0qI9s7NPPx3sFDv+ckY Dt/r4F1hDujopRmdwV9MSc+/04cL5VAe0nLq1FN+P856lfQiPoj5GJFE0 1DLMouxfKFKLh7OA9QW/HoWwM7XyD3RqT/IzABOvPkIgzOylalXWgLY3B OKQd0dwBWwpi46ULNbhwp22O4qzgnKZ2KJTYuB3KQdj4icF9Y2eta5jod Nv6JZN5eqDh83Gdy+sfzrehO2ZBrPv0K0yQpXFi2w1vVJ4Fll34jTpw8A ar9QuSQvNBD9QwLL+lSQ2sUy/YRjbNHMCm+06KBHwdvbX0/bPly1Pridf w==; X-CSE-ConnectionGUID: XNTOYn36RUSid1JgeJkA0g== X-CSE-MsgGUID: CiLUD8LsRL+AWq/xqvsyWg== X-IronPort-AV: E=Sophos;i="6.24,220,1774335600"; d="scan'208";a="68690062" X-Amp-Result: SKIPPED(no attachment in message) Received: from unknown (HELO email.microchip.com) ([170.129.1.10]) by esa1.microchip.iphmx.com with ESMTP/TLS/ECDHE-RSA-AES128-GCM-SHA256; 23 Jun 2026 04:00:08 -0700 Received: from chn-vm-ex03.mchp-main.com (10.10.85.151) by chn-vm-ex04.mchp-main.com (10.10.85.152) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.58; Tue, 23 Jun 2026 04:00:08 -0700 Received: from che-ll-i67070.microchip.com (10.10.85.11) by chn-vm-ex03.mchp-main.com (10.10.85.151) with Microsoft SMTP Server id 15.1.2507.58 via Frontend Transport; Tue, 23 Jun 2026 04:00:02 -0700 From: Varshini Rajendran To: , , , , , , , , , , , , , , , CC: Subject: [PATCH v2 02/12] iio: adc: at91-sama5d2_adc: rework temp calibration layout handling Date: Tue, 23 Jun 2026 16:29:34 +0530 Message-ID: <20260623105944.128840-3-varshini.rajendran@microchip.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260623105944.128840-1-varshini.rajendran@microchip.com> References: <20260623105944.128840-1-varshini.rajendran@microchip.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20260623_040009_332966_BACD5963 X-CRM114-Status: GOOD ( 21.53 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Extend support to handle different temperature calibration layouts. Add a temperature calibration data layout structure to describe indexes of the factors P1, P4, P6, tag, minimum length of the packet and the scaling factors for P1 (mul, div) which are SoC-specific instead of the older non scalable id structure. This helps handle the differences in the same function flow and prepare the calibration data to be applied. Add additional condition to validate the calibration data read from the NVMEM cell using the TAG of the packet. Use cleanup helpers for NVMEM data buffer wherever applicable. Signed-off-by: Varshini Rajendran --- drivers/iio/adc/at91-sama5d2_adc.c | 85 ++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 27 deletions(-) diff --git a/drivers/iio/adc/at91-sama5d2_adc.c b/drivers/iio/adc/at91-sama5d2_adc.c index 255970b2e747..b569d175f4c3 100644 --- a/drivers/iio/adc/at91-sama5d2_adc.c +++ b/drivers/iio/adc/at91-sama5d2_adc.c @@ -445,6 +445,28 @@ static const struct at91_adc_reg_layout sama7g5_layout = { #define at91_adc_writel(st, reg, val) \ writel_relaxed(val, (st)->base + (st)->soc_info.platform->layout->reg) +#define AT91_TEMP_CALIB_TAG_ACST 0x41435354 + +/** + * struct at91_adc_temp_calib_layout - temperature calibration packet layout + * @tag_idx: index of Packet tag in the NVMEM cell buffer + * @p1_idx: index of FT1_TEMP, equivalent to P1 in the NVMEM cell buffer + * @p4_idx: index of FT1_VPAT, equivalent to P4 in the NVMEM cell buffer + * @p6_idx: index of FT2_VBG, equivalent to P6 in the NVMEM cell buffer + * @min_len: minimum number of u32 words expected in the NVMEM cell buffer + * @p1_mul: multiplier applied to P1 to convert to millicelcius + * @p1_div: divider applied to P1 to convert to millicelcius + */ +struct at91_adc_temp_calib_layout { + unsigned int tag_idx; + unsigned int p1_idx; + unsigned int p4_idx; + unsigned int p6_idx; + unsigned int min_len; + unsigned int p1_mul; + unsigned int p1_div; +}; + /** * struct at91_adc_platform - at91-sama5d2 platform information struct * @layout: pointer to the reg layout struct @@ -464,6 +486,7 @@ static const struct at91_adc_reg_layout sama7g5_layout = { * @chan_realbits: realbits for registered channels * @temp_chan: temperature channel index * @temp_sensor: temperature sensor supported + * @temp_calib_layout: temperature calibration packet layout */ struct at91_adc_platform { const struct at91_adc_reg_layout *layout; @@ -481,6 +504,7 @@ struct at91_adc_platform { unsigned int chan_realbits; unsigned int temp_chan; bool temp_sensor; + const struct at91_adc_temp_calib_layout *temp_calib_layout; }; /** @@ -496,18 +520,14 @@ struct at91_adc_temp_sensor_clb { u32 p6; }; -/** - * enum at91_adc_ts_clb_idx - calibration indexes in NVMEM buffer - * @AT91_ADC_TS_CLB_IDX_P1: index for P1 - * @AT91_ADC_TS_CLB_IDX_P4: index for P4 - * @AT91_ADC_TS_CLB_IDX_P6: index for P6 - * @AT91_ADC_TS_CLB_IDX_MAX: max index for temperature calibration packet in OTP - */ -enum at91_adc_ts_clb_idx { - AT91_ADC_TS_CLB_IDX_P1 = 2, - AT91_ADC_TS_CLB_IDX_P4 = 5, - AT91_ADC_TS_CLB_IDX_P6 = 7, - AT91_ADC_TS_CLB_IDX_MAX = 19, +static const struct at91_adc_temp_calib_layout sama7g5_temp_calib = { + .tag_idx = 1, + .p1_idx = 2, + .p4_idx = 5, + .p6_idx = 7, + .min_len = 19, + .p1_mul = 1000, + .p1_div = 1, }; /* Temperature sensor calibration - Vtemp voltage sensitivity to temperature. */ @@ -745,6 +765,7 @@ static const struct at91_adc_platform sama7g5_platform = { .chan_realbits = 16, .temp_sensor = true, .temp_chan = AT91_SAMA7G5_ADC_TEMP_CHANNEL, + .temp_calib_layout = &sama7g5_temp_calib, }; static int at91_adc_chan_xlate(struct iio_dev *indio_dev, int chan) @@ -2251,13 +2272,19 @@ static int at91_adc_temp_sensor_init(struct at91_adc_state *st, { struct at91_adc_temp_sensor_clb *clb = &st->soc_info.temp_sensor_clb; struct nvmem_cell *temp_calib; - u32 *buf; + const struct at91_adc_temp_calib_layout *layout; + void *cell_data; + u32 *buf __free(kfree) = NULL; size_t len; int ret = 0; if (!st->soc_info.platform->temp_sensor) return 0; + layout = st->soc_info.platform->temp_calib_layout; + if (!layout || !layout->p1_div) + return -EINVAL; + /* Get the calibration data from NVMEM. */ temp_calib = nvmem_cell_get(dev, "temperature_calib"); if (IS_ERR(temp_calib)) { @@ -2267,31 +2294,35 @@ static int at91_adc_temp_sensor_init(struct at91_adc_state *st, return ret; } - buf = nvmem_cell_read(temp_calib, &len); + cell_data = nvmem_cell_read(temp_calib, &len); nvmem_cell_put(temp_calib); - if (IS_ERR(buf)) { + if (IS_ERR(cell_data)) { dev_err(dev, "Failed to read calibration data!\n"); - return PTR_ERR(buf); + return PTR_ERR(cell_data); } - if (len < AT91_ADC_TS_CLB_IDX_MAX * 4) { + + buf = cell_data; + + if (len < layout->min_len * sizeof(*buf) || + buf[layout->tag_idx] != AT91_TEMP_CALIB_TAG_ACST) { dev_err(dev, "Invalid calibration data!\n"); - ret = -EINVAL; - goto free_buf; + return -EINVAL; } /* Store calibration data for later use. */ - clb->p1 = buf[AT91_ADC_TS_CLB_IDX_P1]; - clb->p4 = buf[AT91_ADC_TS_CLB_IDX_P4]; - clb->p6 = buf[AT91_ADC_TS_CLB_IDX_P6]; + clb->p1 = buf[layout->p1_idx]; + clb->p4 = buf[layout->p4_idx]; + clb->p6 = buf[layout->p6_idx]; /* - * We prepare here the conversion to milli to avoid doing it on hotpath. + * Here we prepare the conversion to milli to avoid doing it on hotpath. + * The p1 value is multiplied and divided with a scaling factor as per + * the SoC storage format described by per-platform calibration layout. */ - clb->p1 = clb->p1 * 1000; + clb->p1 *= layout->p1_mul; + clb->p1 /= layout->p1_div; -free_buf: - kfree(buf); - return ret; + return 0; } static int at91_adc_probe(struct platform_device *pdev) -- 2.34.1