Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp508353ybl; Fri, 10 Jan 2020 02:17:57 -0800 (PST) X-Google-Smtp-Source: APXvYqwrBmDGoYRVKZrQFmduqAwNO/nM2tu2nXrFD1HGCQ5LlKsBdvZXY0SRibMmmaLKQuwgioMS X-Received: by 2002:a9d:6301:: with SMTP id q1mr1879270otk.70.1578651477171; Fri, 10 Jan 2020 02:17:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578651477; cv=none; d=google.com; s=arc-20160816; b=ei9cvUDo2drdpUHt469yIEkSjgAeyHI2pJDdHZ+quOKWGHTYRX1wBRuQyUNWWZzD0Y W+f5rq0BWfqaNJrdQLd1KFvSS8YSU1JFTC6/7JuRIYQNZr6OfgS2QXQ+KiGudkvY85qp zST9n/9x/eVVICw8mzXg8Ovkb68PuSoG0RALAYZHHb4L51MVvH5FfyPtVF1D9gZe47a4 hV9A2B8hIka/z/beDqBMQDyJdrjgFwlQQD4RhLOCh7RtypPJ5/gcwNytnV/OHGSh3wue dYPdE3Ny7gZakzzXq+ssRmtmPF+9EQnCFfNBvI98GhvsaUamtJR+4WyPjjU7N+r6V/ZB rBdg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:to:from:dkim-signature; bh=VNdKiXHTsnLlZQDsSxPKZhiScesYyaZdTCaN+plx4oE=; b=XakSn/2DTRRDHMGRLbhHGVZml0IqskJTfGRPmoiGFX7viWGVYzaPnk5dG6GLowkmfB sE0UPWxXXNM0DG3asKDDaX4usl3DoNr5xi9msoV217Z5Kja9Ag7PZuC4zZ3W57t2MBXU +e+93EcNF6Qj3cTdP5L/d+5DhgCBTQRGHweQJRHlJ4KfvtlWQ6tJMayfnbELSqXK+qgn I8sbylCTpz3KW00bqUZmxEd8wCuEf1GFw0f1b57PDkLXHyWCL31KCVeiPvRNwrwNIi23 wGdTuj6fN/BcXKKdE0EKacKronB3meiz1PtioM7S3o2CTeEUde9hMd8oXnWQP+nCmgyC +TMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@st.com header.s=STMicroelectronics header.b=JFEJPOQo; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=st.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i12si817945oik.171.2020.01.10.02.17.45; Fri, 10 Jan 2020 02:17:57 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@st.com header.s=STMicroelectronics header.b=JFEJPOQo; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=st.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727591AbgAJKQi (ORCPT + 99 others); Fri, 10 Jan 2020 05:16:38 -0500 Received: from mx07-00178001.pphosted.com ([62.209.51.94]:32734 "EHLO mx07-00178001.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727455AbgAJKQg (ORCPT ); Fri, 10 Jan 2020 05:16:36 -0500 Received: from pps.filterd (m0046037.ppops.net [127.0.0.1]) by mx07-00178001.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 00AADuo6001631; Fri, 10 Jan 2020 11:16:17 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=st.com; h=from : to : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=STMicroelectronics; bh=VNdKiXHTsnLlZQDsSxPKZhiScesYyaZdTCaN+plx4oE=; b=JFEJPOQopDcHcSjc9etUtzI/ToH9CnFieXDHR+CN92+cxP+BLIpsRF99UdhLRyg18qjB hw9e+cHiFU3pFFGmRAi9gbCPuoxwq36K2GGiYGvloBOj8mesYiDRkCuZS43B+DBGyMRo J5Tck6FLMeUOHmh5J4mmegQL8fHFqNJ6Pj1eWEByaeHPoJFh4UTRV1ZoIUqfFApAN3s3 qfjIzaVyDlkY0ftOhukRN/oPp0s7zSZbN5lKhMzL0FJBn+Q0ccdR7OwAvfOdet35AyPO 3i+Scd6RVIdmP95pub0lFiD661FfgPQC2GhQT6yBkqP3t0vNazQCF8YYtUJhl+3pKkAF Xw== Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx07-00178001.pphosted.com with ESMTP id 2xakur6mnt-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 10 Jan 2020 11:16:17 +0100 Received: from euls16034.sgp.st.com (euls16034.sgp.st.com [10.75.44.20]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id 11382100034; Fri, 10 Jan 2020 11:16:12 +0100 (CET) Received: from Webmail-eu.st.com (sfhdag6node2.st.com [10.75.127.17]) by euls16034.sgp.st.com (STMicroelectronics) with ESMTP id 014962A7906; Fri, 10 Jan 2020 11:16:12 +0100 (CET) Received: from localhost (10.75.127.46) by SFHDAG6NODE2.st.com (10.75.127.17) with Microsoft SMTP Server (TLS) id 15.0.1347.2; Fri, 10 Jan 2020 11:16:11 +0100 From: Pascal Paillet To: , , , , , , , , , , , , , Subject: [PATCH_V3 5/6] thermal: stm32: improve temperature computing Date: Fri, 10 Jan 2020 11:16:04 +0100 Message-ID: <20200110101605.24984-6-p.paillet@st.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200110101605.24984-1-p.paillet@st.com> References: <20200110101605.24984-1-p.paillet@st.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.75.127.46] X-ClientProxiedBy: SFHDAG8NODE2.st.com (10.75.127.23) To SFHDAG6NODE2.st.com (10.75.127.17) X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.138,18.0.572 definitions=2020-01-10_01:2020-01-10,2020-01-09 signatures=0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Change the way of computing to avoid rounds by 1 or 2 degrees. Also simplify the sampling time management that is hard-coded to maximum value during probe. Signed-off-by: Pascal Paillet --- drivers/thermal/st/stm_thermal.c | 58 ++++++++------------------------ 1 file changed, 14 insertions(+), 44 deletions(-) diff --git a/drivers/thermal/st/stm_thermal.c b/drivers/thermal/st/stm_thermal.c index 29e7ee89adf6..aaf25ca2f118 100644 --- a/drivers/thermal/st/stm_thermal.c +++ b/drivers/thermal/st/stm_thermal.c @@ -59,7 +59,6 @@ /* Less significant bit position definitions */ #define TS1_T0_POS 16 -#define TS1_SMP_TIME_POS 16 #define TS1_HITTHD_POS 16 #define TS1_LITTHD_POS 0 #define HSREF_CLK_DIV_POS 24 @@ -83,15 +82,10 @@ #define ONE_MHZ 1000000 #define POLL_TIMEOUT 5000 #define STARTUP_TIME 40 -#define TS1_T0_VAL0 30 -#define TS1_T0_VAL1 130 +#define TS1_T0_VAL0 30000 /* 30 celsius */ +#define TS1_T0_VAL1 130000 /* 130 celsius */ #define NO_HW_TRIG 0 - -/* The Thermal Framework expects millidegrees */ -#define mcelsius(temp) ((temp) * 1000) - -/* The Sensor expects oC degrees */ -#define celsius(temp) ((temp) / 1000) +#define SAMPLING_TIME 15 struct stm_thermal_sensor { struct device *dev; @@ -280,27 +274,17 @@ static int stm_thermal_calculate_threshold(struct stm_thermal_sensor *sensor, int temp, u32 *th) { int freqM; - u32 sampling_time; - - /* Retrieve the number of periods to sample */ - sampling_time = (readl_relaxed(sensor->base + DTS_CFGR1_OFFSET) & - TS1_SMP_TIME_MASK) >> TS1_SMP_TIME_POS; /* Figure out the CLK_PTAT frequency for a given temperature */ - freqM = ((temp - sensor->t0) * sensor->ramp_coeff) - + sensor->fmt0; - - dev_dbg(sensor->dev, "%s: freqM for threshold = %d Hz", - __func__, freqM); + freqM = ((temp - sensor->t0) * sensor->ramp_coeff) / 1000 + + sensor->fmt0; /* Figure out the threshold sample number */ - *th = clk_get_rate(sensor->clk); + *th = clk_get_rate(sensor->clk) * SAMPLING_TIME / freqM; if (!*th) return -EINVAL; - *th = *th / freqM; - - *th *= sampling_time; + dev_dbg(sensor->dev, "freqM=%d Hz, threshold=0x%x", freqM, *th); return 0; } @@ -368,40 +352,26 @@ static int stm_thermal_set_trips(void *data, int low, int high) static int stm_thermal_get_temp(void *data, int *temp) { struct stm_thermal_sensor *sensor = data; - u32 sampling_time; + u32 periods; int freqM, ret; if (sensor->mode != THERMAL_DEVICE_ENABLED) return -EAGAIN; - /* Retrieve the number of samples */ - ret = readl_poll_timeout(sensor->base + DTS_DR_OFFSET, freqM, - (freqM & TS1_MFREQ_MASK), STARTUP_TIME, - POLL_TIMEOUT); - + /* Retrieve the number of periods sampled */ + ret = readl_relaxed_poll_timeout(sensor->base + DTS_DR_OFFSET, periods, + (periods & TS1_MFREQ_MASK), + STARTUP_TIME, POLL_TIMEOUT); if (ret) return ret; - if (!freqM) - return -ENODATA; - - /* Retrieve the number of periods sampled */ - sampling_time = (readl_relaxed(sensor->base + DTS_CFGR1_OFFSET) & - TS1_SMP_TIME_MASK) >> TS1_SMP_TIME_POS; - - /* Figure out the number of samples per period */ - freqM /= sampling_time; - /* Figure out the CLK_PTAT frequency */ - freqM = clk_get_rate(sensor->clk) / freqM; + freqM = (clk_get_rate(sensor->clk) * SAMPLING_TIME) / periods; if (!freqM) return -EINVAL; - dev_dbg(sensor->dev, "%s: freqM=%d\n", __func__, freqM); - /* Figure out the temperature in mili celsius */ - *temp = mcelsius(sensor->t0 + ((freqM - sensor->fmt0) / - sensor->ramp_coeff)); + *temp = (freqM - sensor->fmt0) * 1000 / sensor->ramp_coeff + sensor->t0; return 0; } -- 2.17.1