From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) (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 0C350363085 for ; Thu, 30 Apr 2026 16:00:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.168.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777564845; cv=none; b=EpVxI+1cNjRrozGTWDdaVp3zWB0GoESrrLu5br0XctD5u7EJjn30ShLJZqZMm4u5BfibZzbmhggJxwOchcNDXlq9Tv4ZUNJSD+pORXynoeqiCsXG5P7ncijpE1wNcexpEKzZc9tZ2sO0zSryVxcC9JmNGk1o4hBRFZtn9n1NEvo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777564845; c=relaxed/simple; bh=xDFMbAQ+d50N8pgekZjLKMPaEp6MjAtl9P7svWAapA0=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=O4tJp7OwKg36MhRmi9kjsNP4++P3vgXj7QPWhbinimYCKsH6G26LAEZtg01/Bl3pfNVj4NZl5DAJiQhjHN2117HkjPtpZPnm7QaqwIS84/KKkfRC4dTsZ49d42HASRyDxLi7278CfiV5muC8yTh2eE0SToan0dNNihAfp1XhQus= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b=JdZSDabQ; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b=PgxBw6pA; arc=none smtp.client-ip=205.220.168.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b="JdZSDabQ"; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b="PgxBw6pA" Received: from pps.filterd (m0279865.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 63UBkDRa1820572 for ; Thu, 30 Apr 2026 16:00:43 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= bo0XjYVyRoPqt/BSUYkXnHD+Dx9SB7n3/A/Bv4qMvAM=; b=JdZSDabQSVy2HSk4 jA0PlWQN3BWTa+veoBngTSg7G7w6oleXdt3AQAmfWPbosTR8Ctqmgh9oNB/4QA9m KBu9L+O2El47HP8RN/Jszb48ukvPBdteA8c7fTS7lTNufK5RhE2mrK8zQPcMyVoX BO28UVgwsKqlh3c1f/j7277/Sa3INrJISGAAufVpNg2+XlgTILwxpgjMezO1UfFj qcZfZDKXVLwx44WGviebfOflDpBjW79wtlYyfxEZENj8k8ch3pgNBdAzTxLLBCP1 8lV7DUxYwmGpzb6KIMIxVcIzqSBMpRO4XXUn5PY63CvYl/f6IBpNw66bPy4y3psq eQ8yZw== Received: from mail-vs1-f71.google.com (mail-vs1-f71.google.com [209.85.217.71]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4dv6ges6rj-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Thu, 30 Apr 2026 16:00:43 +0000 (GMT) Received: by mail-vs1-f71.google.com with SMTP id ada2fe7eead31-6058daaee1fso102339137.1 for ; Thu, 30 Apr 2026 09:00:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1777564842; x=1778169642; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=bo0XjYVyRoPqt/BSUYkXnHD+Dx9SB7n3/A/Bv4qMvAM=; b=PgxBw6pAof6RrpMqtXXkAkP8F5Sz+ETfMszR7nHi6hXMUyYUmoLfIzciXQpsxj1fBo yUxiO//kkomz3V7ax/CuzL0T2eAbYgEbV6bAmDxFKYtju9/rTwCiCTEUIB/aU/xDxmRJ RmBgR1e39QYUfHiphoDmhC9AB86SQv4tJFCfNhByQYnzDr0OTfxYqx005Nnago2I5fVk aqjeze/MTBg10GlReK+dsLXPy6BuPpL1v+66Uq790KU1pDlVS6EPXNYKksYeaB2t8SG+ ZdzCu/PmIs9z+pWZgdcwsOW7i83fZ5hCbWTE4BzJL5KbifJTdomGVIWh3qtolsdk60yT ic8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777564842; x=1778169642; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=bo0XjYVyRoPqt/BSUYkXnHD+Dx9SB7n3/A/Bv4qMvAM=; b=CEjvKGHPSkMcZDvaCo6DKIEV4vxL8fdT+b9HrLN+tNRIpWU0Fe1ByYgUfgV5rXK4lT I7yXsUdqhLNlDM1b7OiCRhD9JN8XNqAOXgJ/cYGzfAP7816LaZK4w2TA+PBRlC9o3yBe uAsoff8W1ZymvXNHtywEpZPzEIBWw6n63236JkQir2SmffRXTFxqRuoECkMZzlwJtstp trzOIff6fnHJucBxLxsNX6ve9UoA1YrhH/d+gAf24FKDA6tYGGSKtIv2/V9vzp6MrKOr YWM5gPL2o52WU553VFZwR34BJfXFB3IEQuyRptJyLI46Qz2yuCcDSxxwA5adpBQMpEn9 iQXg== X-Forwarded-Encrypted: i=1; AFNElJ9QLJjl1fiXtPZ81XMXvvK4KtmWXcSU2tbCOkqSaZFl6uME4Bjz8B0vIEhzbMG752eBZX5tjwNM1+CeHXE=@vger.kernel.org X-Gm-Message-State: AOJu0YwS2qYClOI1JD2KBkGbg6ywunlybIsXYfGP1Hcx/mrnMcplYAOZ 5DwlVX7Z76DVasGnyh0SIhobXdfDkw4yQCSUYHOKIirc2iQDrK6TxjMCA0GjEpjA7UG6mvlZnvw GQScZ8Y7ecPUWtrXq2kWebyrCwbjhSCR/B1nt09tGN+F8O1btEsQgg+YIBWf+QLGwzEI= X-Gm-Gg: AeBDietTco6jYQ1yt16yYqr+SuUg0jhvPzi2vNOsMBSd3ysaIp7vdgQQ2TF/zOFaDoU 63pAB49oowBr/pXj91BoeUamHK/k6zPivoyDEXe7fddZ84KAsUkiS/zoNu1VxckgrzBruSR+pvR M5+6zbWaulj380y66rPhE592mTh6E7qrcAeRl9MzM/CmGt/po4Zf9DqqO6qtS0atS/ZshQu7U4m 0tVO80T4C3XKdDEnB3OUbQGvYdN3sIzBO/ICSUbXPd+scYK7Uu9V4EvFbFyu3ij442mhqvlSskS eVpIHITzzhNkVG3cANXmX/B+QLY+Bmcf550cgWkeeR9u4ZT0DTKPtAUy/IPsRc3DavlKGLEvQhk 62pSo9AFSoBbKOrV2NVw6phyr/D0Fm1viNM+zVX5DvvIPpNqfStw6p2IB2EShddIPXO731otv3j HGEgdT5Cm8ZMksXw== X-Received: by 2002:a05:6102:3a09:b0:610:2c3b:7d99 with SMTP id ada2fe7eead31-62ad52ccd30mr926079137.6.1777564842130; Thu, 30 Apr 2026 09:00:42 -0700 (PDT) X-Received: by 2002:a05:6102:3a09:b0:610:2c3b:7d99 with SMTP id ada2fe7eead31-62ad52ccd30mr926058137.6.1777564841500; Thu, 30 Apr 2026 09:00:41 -0700 (PDT) Received: from [192.168.119.254] (078088045245.garwolin.vectranet.pl. [78.88.45.245]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-bbe69f6d641sm13842966b.13.2026.04.30.09.00.39 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 30 Apr 2026 09:00:40 -0700 (PDT) Message-ID: Date: Thu, 30 Apr 2026 18:00:38 +0200 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 1/2] thermal: qcom: tsens: atomic temperature read with hardware-guided retries To: Priyansh Jain , Amit Kucheria , Thara Gopinath , "Rafael J . Wysocki" , Daniel Lezcano , Zhang Rui , Lukasz Luba Cc: linux-pm@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org, manaf.pallikunhi@oss.qualcomm.com References: <20260430054422.2461150-1-priyansh.jain@oss.qualcomm.com> <20260430054422.2461150-2-priyansh.jain@oss.qualcomm.com> Content-Language: en-US From: Konrad Dybcio In-Reply-To: <20260430054422.2461150-2-priyansh.jain@oss.qualcomm.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Authority-Analysis: v=2.4 cv=f6V4wuyM c=1 sm=1 tr=0 ts=69f37cab cx=c_pps a=P2rfLEam3zuxRRdjJWA2cw==:117 a=FpWmc02/iXfjRdCD7H54yg==:17 a=IkcTkHD0fZMA:10 a=A5OVakUREuEA:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=Um2Pa8k9VHT-vaBCBUpS:22 a=EUspDBNiAAAA:8 a=bdp5ZZDYhz2jdzQ5nI4A:9 a=QEXdDO2ut3YA:10 a=ODZdjJIeia2B_SHc_B0f:22 X-Proofpoint-GUID: -7C46V6WJcZ9vnSQMdKycY2kLa9YiS2V X-Proofpoint-ORIG-GUID: -7C46V6WJcZ9vnSQMdKycY2kLa9YiS2V X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwNDMwMDE2NSBTYWx0ZWRfXwaF1I545uz1V yQM576vLq2FEBPuT3L2GrmwI3OPq2s+ihHsK+YuVtrf8ArWT0xCDZJrU2gtWY7bWhZcmx9epQ0P cnvGjonIHQYMKBanqd5vC4StfnjAQXNk0MFtzweP6zp4M7+14UPAmeOOgsgGZYeHbwBzQQftmaa F9Tg2K6PnGT4n7ibIFFeIXyPun+TdUemlhxWYl9MwvS0alooN8oQciaqDcv5u0wKDMu5/ZMz1bE 13b7zI5AVFj8o2hh2WGVHAvSJCmliJL2QNot+7Z9PX/WfS4uwd20XznJJMrjgwFjMUvgN/UxCG1 dszRzdzZQJ5djE/+gAqraHQUsg+F9R7IJ0rJ2rQ4d724LJ4GLrv6XrLrC6UvuEEVuxLJpA2XXEz O4UjbXMVh+rhXDNcLsNF7Ur3HFpzaLPcs9wHUC11XkGldS9sKHyXUWt7ajdLYw0xxw4Wk9qiUHs ZZBiDXWch7UZDW3BgRw== X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.51,FMLib:17.12.100.49 definitions=2026-04-30_04,2026-04-30_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 impostorscore=0 suspectscore=0 adultscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 malwarescore=0 priorityscore=1501 phishscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2604200000 definitions=main-2604300165 On 4/30/26 7:44 AM, Priyansh Jain wrote: > The existing TSENS temperature read logic polls the valid bit and then > reads the temperature register. When temperature reads are triggered > at very short intervals, this can race with hardware updates and allow > the temperature field to be read while it is still being updated. > > In this case, the valid bit may already be asserted even though the > temperature value is transitioning, resulting in an incorrect reading. > > Hardware programming guidelines require the temperature value and the > valid bit to be sampled atomically in the same read transaction. A > reading is considered valid only if the valid bit is observed set in > that same sample. > > The guidelines further specify that software should attempt the > temperature read up to three times to account for transient update > windows. If none of the attempts observe a valid sample, a stable > fallback value must be returned: if the first and second samples match, > the second value is returned; otherwise, if the second and third > samples match, the third value is returned. > > Update the TSENS sensor read logic to implement atomic sampling along > with the recommended retry-and-compare fallback behavior. This removes > the race window and ensures deterministic temperature values in > accordance with hardware requirements. > > Signed-off-by: Priyansh Jain > --- [...] > +/** > + * tsens_read_temp - To read temperature from hw in deciCelsius. "Retrieve temperature readings from the hardware" > + * @s: Pointer to sensor struct > + * @field: Index into regmap_field array pointing to temperature data > + * @temp: temperature in deciCelsius to be read from hardware > + * > + * This function handles temperature returned in ADC code or deciCelsius > + * depending on IP version. > + * > + * Return: 0 on success, a negative errno will be returned in error cases > + */ > +static int tsens_read_temp(const struct tsens_sensor *s, int field, int *temp) > +{ > + struct tsens_priv *priv = s->priv; > + int temp_val[3] = {0}; > + unsigned int status = 0; This should be a u32 to reflect this is a known-size register > + int ret = 0, i; You can declare the iterator inside the loop 'ret' doesn't need to be initialized, as it's immediately overwritten > + int max_retry = 3; This should be a constant, perhaps a #defined one, reused for the size of temp_val > + > + ret = regmap_field_read(priv->rf[field], &status); > + if (ret) > + return ret; > + > + /* VER_0 doesn't have VALID bit */ > + if (tsens_version(priv) == VER_0) { > + *temp = status; > + return ret; Ret will always be a '0' here (and in below cases) > + } > + > + for (i = 0; i < max_retry; i++) { > + temp_val[i] = status & priv->feat->last_temp_mask; > + if (status & priv->feat->valid_bit) { > + *temp = temp_val[i]; > + return ret; > + } > + ret = regmap_field_read(priv->rf[field], &status); Please add a \n above for readability> + if (ret) > + return ret; > + } > + > + if (temp_val[0] == temp_val[1]) > + *temp = temp_val[1]; > + else if (temp_val[1] == temp_val[2]) > + *temp = temp_val[2]; > + else > + return -EAGAIN; This deserves a comment. Your description in the commit message is good, but the reasoning behind this logic is not obvious and will get lost in time, as 'git blame' will be bloated by cleanups Konrad