Received: by 2002:ab2:1689:0:b0:1f7:5705:b850 with SMTP id d9csp544323lqa; Sat, 27 Apr 2024 12:58:17 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV+X8+ex9d/EAvKJatowUk0H2PD98wP381NL4BZgawuZMBrCYPErVlG5GQbzrH7tXfCpU+4QonVcWHyS+46nngvcPjUgIydi6vgfEfyiA== X-Google-Smtp-Source: AGHT+IERSL4JUmpU+ezg2ggdWIYjXQyYc0hKAAQMOt5/IJs/moiG1XaBnJPDAWbAerWvKK9JGKgo X-Received: by 2002:a05:6a21:3d84:b0:1a9:5b1e:4f06 with SMTP id bj4-20020a056a213d8400b001a95b1e4f06mr6699309pzc.52.1714247897426; Sat, 27 Apr 2024 12:58:17 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714247897; cv=pass; d=google.com; s=arc-20160816; b=CQ9xrT+KWNcLAQe+x2Z5W7eliKoiiWMjKvKm7/rfDi/EAiAAPUVCgvryu4Q5c+SK+1 GjMBNcWi0+CWkk3S1YD0c+YXbcRwdYTSEhZv9kF3HGuVrhfE+fm86DulH6amY73leT2F kmAe5+0BeZBcvIlgbMH0ow+INaUD46NFV+xe3+aHTV3E00decWLy6ZMGE9wgrXbxHI/3 KoJGEjWPC+fv1Cy2yKcYxL91+htOEkUbsFsrYDqy1C1951WIMmOAwl8hAZij+7pvcPjC V5yqCDErFLjh7WhBj/ya11nbeN3gWJAfghYT/ymIGLuu4EtpFbwAM4mhALakRq8E0snV 3bNQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:message-id:subject:cc:to:from:date:dkim-signature; bh=6sLeaB1FlBRZ70pzjbPaLXI6rhtbdR12bImC9yVAPfs=; fh=4XjBsOBB0FVj8TjDECdB/JY6kgc3g01gvu+y2GXoEJA=; b=rDpytuiRMHa5FIwPhlYNd2wecJ5GOzalbi+TOwC7819rrva654t5ejp5T2TgCaY1jZ d2Usbb6UnZnIQXswrL5dgXYEz/QyUogn3OpNEtt8XKM0roKqDxsOhgbYzUYJIzWwifv1 ENuMx+gg+qpodnmKclcZAYG8z5lmkmpcahpkx09h1eZXOBn/4/6cDXKhMhHSL0qpx798 4cjRd5C7Amlj4WInc2QOw/u5pivkLA87MLLq/sOhhOX/QVBi3e2pX2H/VqEvq4uyUChJ 7yIPlICLwNu7XV9AxnNyJ0pw/ZjF8zewEdojvTz5xh4VlPw2kEExzOMuYXKT3f+6qt/k Alag==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=TgAZY5dv; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-161155-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-161155-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id gp6-20020a056a003b8600b006ed03fd6ea8si16997165pfb.140.2024.04.27.12.58.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 27 Apr 2024 12:58:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-161155-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=TgAZY5dv; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-161155-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-161155-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 16C70B212BA for ; Sat, 27 Apr 2024 19:58:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AC27913C83D; Sat, 27 Apr 2024 19:58:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="TgAZY5dv" Received: from mail-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.50]) (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 4CC847E58D; Sat, 27 Apr 2024 19:58:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714247884; cv=none; b=aM0pca/o1jEjmcNm1ZTz9ZCkF20afZyy2oXVW5eKGWspj1Z31Ue2xQJBf5wsHy+9b3ZderrjB7eOcj4jEiwgzoQaFMMvETVgsTx8JN+vbNeYD0VFYhqvAduwmn4THP0wSY6MpWeH6DPfV6hBd6oxElZipL8/np1s4VOIplu4Luw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714247884; c=relaxed/simple; bh=Ui61Kqd3H/BG3qyUqZGqtcVNhOr6wTkCK+eT94OQAKo=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=ncB6kYdhbStIrO714DuYKSRPy2buGhGmaw4A6FDKeXhTqli+CctVWW8fEfet561zqvVWJ/KGC0LuUppody3ccsxo/2sAk24j7/8CuwmczXKrpFKYyRxfE0aLhG09fHyWl6iQUU/lSi2IhsWvTjHsTjwv9pdvOkvjlcvEM2hs6f4= 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=TgAZY5dv; arc=none smtp.client-ip=209.85.221.50 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 Received: by mail-wr1-f50.google.com with SMTP id ffacd0b85a97d-343b7c015a8so2510898f8f.1; Sat, 27 Apr 2024 12:58:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714247881; x=1714852681; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=6sLeaB1FlBRZ70pzjbPaLXI6rhtbdR12bImC9yVAPfs=; b=TgAZY5dvMK9iS+wRFxPHsUMDwaMa6YtHrHBV908+UpE2anJKGzuJbqmUEyK3l7dQUE nBY+1QmkHCJS1gAP0IoBXr4HTHL/G/vCNZ3zzYplcPghsu7v/Q4Aey7q5s1iqgLLtWJP CJ9q/G8MZ0nxsGhq/PtAGUeNGig1xCMNhFzJZPJyi2RCoonEVoFyTyYL0S1+OdTnqqld 3DEfwWvogNXwjaJwyx8PV+sWc1FH++Q5OBhkG2ATXjW47iBBy0rso4+lfQYbAQaBY60l CjvPR/5sK63YWateSuRtc5XeGp7hwi/rEsDSd1EfXX+6b4qpWETGsyyy7/EPqBJe+j2m h9eA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714247881; x=1714852681; h=in-reply-to:content-transfer-encoding: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=6sLeaB1FlBRZ70pzjbPaLXI6rhtbdR12bImC9yVAPfs=; b=QGhjr0MMNXnICJ45zRKrtpSyyPoBMYk8v2WZw4BllSEit7XcZ5i5cxc//nX5EdlARu xAbIFXpm5zqTOrXpl0K9Pu0RHPwPYcR1pJnLYiu67cOJdLfj2Vjz9wPbSOjKgJVwYGMs KHLb/f2DYeoqwKQIhTtDJ2rVUF4ZizNQLhIyXhimIp8T1zu/DzJ3j0+mtQ30wLpXCwEd siYCxnbwCddrhH9F4vbI5TCWG3/CsdAArOTXb5pNLdKUM+WK1o89Ukhsd5swsZzvDbDg gzVf/P0wMGw4k+RE8veBaJcef+35qvxoX0AzWNJzTpHJzryig3a+Zo9KNCXE87OHd/wF vMwA== X-Forwarded-Encrypted: i=1; AJvYcCWHczEWqvVgk7uSwjx4iXYqkPBI+iLSxLFsDgLuMFoMJ0hqYLJV/4ceH/fhmUjdGL7JUt5S/ZBZ5NKyCa/NpYqd5Syugf4OfU+zWNZqTz/ZWfxm5/P1Wizkw4FDhFgOAm6IL9V8z6DV X-Gm-Message-State: AOJu0Yz9BoZLofs7F+yKQd8dYLmy6pSwrIpN1Pd8JGem3/sDdFd6lLqe dpw57tLC2qNkSAkYOJaa/8KARs8YibsNZwGytAsBMHZCOmjvCL92 X-Received: by 2002:a05:600c:358c:b0:41b:13a3:6183 with SMTP id p12-20020a05600c358c00b0041b13a36183mr4430923wmq.24.1714247881124; Sat, 27 Apr 2024 12:58:01 -0700 (PDT) Received: from debian ([93.184.186.109]) by smtp.gmail.com with ESMTPSA id ay24-20020a05600c1e1800b0041bf29ab003sm1870718wmb.30.2024.04.27.12.58.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 27 Apr 2024 12:58:00 -0700 (PDT) Date: Sat, 27 Apr 2024 21:57:58 +0200 From: Dimitri Fedrau To: Marcelo Schmitt Cc: Jonathan Cameron , Lars-Peter Clausen , Andrew Hepp , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] iio: temperature: mcp9600: Fix temperature reading for negative values Message-ID: <20240427195758.GA3350@debian> References: <20240424185913.1177127-1-dima.fedrau@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: Am Sat, Apr 27, 2024 at 05:49:18AM -0300 schrieb Marcelo Schmitt: > Hi Dimitri, > > Interesting patch this one. > I think this does apply, although, the cold junction register has for sign bits > so I think we could also have a mask to clear those out. > Some code suggestions inline. > Hi Marcelo, the temperature bits are in two’s complement format for hot and cold junction. Equations to calculate the temperature are also the same in the datasheet. There should be no difference when handling them. I don't think we need to do anything more with the value except sign_extend it to the appropriate data type. If the sign bits aren't right, there is a bug in the chip, until then futher processing of it is unneeded. I could add a comment here if it helps. > On 04/24, Dimitri Fedrau wrote: > > Temperature is stored as 16bit value in two's complement format. Current > > implementation ignores the sign bit. Make it aware of the sign bit by > > using sign_extend32. > > > > Fixes: 3f6b9598b6df ("iio: temperature: Add MCP9600 thermocouple EMF converter") > > Signed-off-by: Dimitri Fedrau > > --- > > drivers/iio/temperature/mcp9600.c | 3 ++- > > 1 file changed, 2 insertions(+), 1 deletion(-) > > > > diff --git a/drivers/iio/temperature/mcp9600.c b/drivers/iio/temperature/mcp9600.c > > index 46845804292b..7a3eef5d5e75 100644 > > --- a/drivers/iio/temperature/mcp9600.c > > +++ b/drivers/iio/temperature/mcp9600.c > > #define MCP9600_COLD_JUNCTION_SIGN_MSK GENMASK(15,12) > ... > > > @@ -52,7 +52,8 @@ static int mcp9600_read(struct mcp9600_data *data, > > > > if (ret < 0) > > return ret; > > - *val = ret; > > + > > + *val = sign_extend32(ret, 15); > if (chan->address == MCP9600_COLD_JUNCTION) > *val &= ~MCP9600_COLD_JUNCTION_SIGN_MSK; > This won't work. Assuming int is 32-bit ret = 0xfffe and *val = -2 after sign_extends32, this would result in *val = -61442 which is wrong. > > > > return 0; > > } > > -- > > 2.39.2 > > > > Best regards, Dimitri Fedrau