From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-lj1-f173.google.com (mail-lj1-f173.google.com [209.85.208.173]) (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 E224A8494; Mon, 7 Apr 2025 05:23:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.173 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744003393; cv=none; b=JEguFtxJeWo25n4rYbMZ2FAz3Cyb9OuVN+aEChJqmt2NjcFE7VMrlHFmwEzs8RdCYH8LEKOyXmddv/sSdOG1J1ds5Ev26n8Xs1wc3tj8SL4elLHEUbkp/OQt08+McusArUibCp9vMDIeENNmfTLpMkkq/hRzfDmOTSKEjIDDng0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744003393; c=relaxed/simple; bh=4Uw7pJFtBIeo6dVfZ8PPt8rulIb8ur1utK/lmAzQrQs=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=t7OWLkPyeFK7E+XKzHoqhmbgB+ubIXLLPFWieNkJ0GPxSO2o38xZ/FjNxiY+pHfbMTSRhCLwUSbefIVI3oCg9axWvlRFZQgaub4nB3oCcDdii+Nu1clRRSZ3/ozzwqcyhdjhc68VfPZWJUPTCU9nPk6KSx1Iu00gdghdx8NpEOg= 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=aargRNwS; arc=none smtp.client-ip=209.85.208.173 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="aargRNwS" Received: by mail-lj1-f173.google.com with SMTP id 38308e7fff4ca-30bd21f887aso32688611fa.1; Sun, 06 Apr 2025 22:23:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1744003390; x=1744608190; 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=KZANaSp06H/1zuBfOX6aMv95xWnp689wwAWAXkSh8Fo=; b=aargRNwSlXq7QA6rnvFI+g9lUhzDzuGuIYsPN7sAhkfZkAtYPXf21uncAXPdl93bKN o/13mpjiH3pFow/IL7dbzcG37Q+EmCfICsbUJK74Qw7q2U/oQDltDv4wfm91nVhwcGwv X9ph4iuNETo+jLDTN93S58CjxKgpNShjStIalD9XvEX+IDVj8fTGj95ssSFDqJfbXtJC RnodNnRiEeIxa7KmSmGX/a2MgqY0dKTzUeRenhoMzzj3bivURpVvfY7ye5vEayalb1jB kzlOPaRMSxw3hGbQ4GUR436NzOiRAFwFo3vSj6sX1bM63GHIgZ8ILBYYubcJ8gpuAYUp ZI1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744003390; x=1744608190; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=KZANaSp06H/1zuBfOX6aMv95xWnp689wwAWAXkSh8Fo=; b=io6mKg9XutKY7TCADHrqEcvhAn7f+MaSse199wLby7BjaB/1rpsd6fq7SGb3fFPfVz E+3FQxJs64Ez5m9Hd4n0UBhnnzZIpz3tSfQm89PKcWNNnSIi3x9H0MLhnl0LX9uSnXQO S00/6IhMrxVMAsl6T4dK1DiVPeznCEVRX/SheyXQnWobVXo5P5qj7N5RRmjDaoUmeSp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Fhg== X-Received: by 2002:a2e:bd88:0:b0:308:fbd5:9d2d with SMTP id 38308e7fff4ca-30f0c07084amr32615301fa.37.1744003389759; Sun, 06 Apr 2025 22:23:09 -0700 (PDT) Received: from ?IPV6:2a10:a5c0:800d:dd00:8fdf:935a:2c85:d703? ([2a10:a5c0:800d:dd00:8fdf:935a:2c85:d703]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-30f0313f374sm14219601fa.24.2025.04.06.22.23.07 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 06 Apr 2025 22:23:08 -0700 (PDT) Message-ID: <4f099833-5aa6-47cc-917c-7a466cb644b9@gmail.com> Date: Mon, 7 Apr 2025 08:23:07 +0300 Precedence: bulk X-Mailing-List: devicetree@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 2/7] iio: adc: ti-adc128s052: Simplify using be16_to_cpu() To: Jonathan Cameron Cc: David Lechner , Matti Vaittinen , Lars-Peter Clausen , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Nuno Sa , Javier Carrasco , linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org References: <4c3e0d23-2582-4acf-8e90-542c8f8c385f@baylibre.com> <1189b539-adb4-46f9-9783-c6577b57a304@gmail.com> <20250405182947.06d5e67f@jic23-huawei> Content-Language: en-US, en-AU, en-GB, en-BW From: Matti Vaittinen In-Reply-To: <20250405182947.06d5e67f@jic23-huawei> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 05/04/2025 20:29, Jonathan Cameron wrote: > On Thu, 3 Apr 2025 08:16:43 +0300 > Matti Vaittinen wrote: > >> On 03/04/2025 00:04, David Lechner wrote: >>> On 4/2/25 1:09 AM, Matti Vaittinen wrote: >>>> The register data is 12-bit big-endian data. Use be16_to_cpu() to do >>>> the conversion, and simple bitwise AND for masking to make it more >>>> obvious. >>>> >>>> Signed-off-by: Matti Vaittinen >>>> --- >>>> Revision history: >>>> v1 => v2: >>>> - Fix commit msg to reflect the fact there was no bug >>>> - Drop Fixes tag >>>> - Use union for rx / tx buffer to avoid casting >>>> - Keep the shared message protected by the mutex >>>> --- >>>> drivers/iio/adc/ti-adc128s052.c | 18 ++++++++++-------- >>>> 1 file changed, 10 insertions(+), 8 deletions(-) >>>> >>>> diff --git a/drivers/iio/adc/ti-adc128s052.c b/drivers/iio/adc/ti-adc128s052.c >>>> index a456ea78462f..3e69a5fce010 100644 >>>> --- a/drivers/iio/adc/ti-adc128s052.c >>>> +++ b/drivers/iio/adc/ti-adc128s052.c >>>> @@ -28,32 +28,34 @@ struct adc128 { >>>> struct regulator *reg; >>>> struct mutex lock; >>>> >>>> - u8 buffer[2] __aligned(IIO_DMA_MINALIGN); >>>> + union { >>>> + __be16 rx_buffer; >>>> + u8 tx_buffer[2]; > As below. Maybe > __be16 buffer16; > u8 buffer[2]; Ok. >>>> + } __aligned(IIO_DMA_MINALIGN); >>>> }; >>>> >>>> static int adc128_adc_conversion(struct adc128 *adc, u8 channel) >>>> { >>>> int ret; >>>> + char *msg = &adc->tx_buffer[0]; >>>> >>>> mutex_lock(&adc->lock); >>>> >>>> - adc->buffer[0] = channel << 3; >>>> - adc->buffer[1] = 0; >>>> + msg[0] = channel << 3; >>>> + msg[1] = 0; >>>> >>>> - ret = spi_write(adc->spi, &adc->buffer, 2); >>>> + ret = spi_write(adc->spi, msg, sizeof(adc->tx_buffer)); > > I'd get rid of msg as it's now just confusing given we are > using the sizeof() here. Ok. >>>> if (ret < 0) { >>>> mutex_unlock(&adc->lock); >>>> return ret; >>>> } >>>> >>>> - ret = spi_read(adc->spi, &adc->buffer, 2); >>>> - >>>> + ret = spi_read(adc->spi, &adc->rx_buffer, 2); > > sizeof(adc->rx_buffer) I was thinking of this but went with raw 2 - because we need to read exactly 2 bytes from the device. Sizeof buffer is matter of software where as the 2 bytes is dictated by the device. (Sure the size of buffer needs to be large enough). I don't care it that much though, so I can go with the sizeof() if that's what you prefer. Just explaining that the '2' here was a conscious choice :) >>>> mutex_unlock(&adc->lock); >>>> - >>>> if (ret < 0) >>>> return ret; >>>> >>>> - return ((adc->buffer[0] << 8 | adc->buffer[1]) & 0xFFF); >>>> + return be16_to_cpu(adc->rx_buffer) & 0xFFF; >>> >>> >>> The cast isn't exactly beautiful, but this would save a lot of >>> lines of diff and a few lines of code by avoiding the need for >>> the union and the local msg variable. >>> >>> return be16_to_cpup((__be16 *)adc->buffer) & 0xFFF; > > The cast only works because we have forced the alignment for DMA safety. > That to me is a little fragile. > > You could do get_unaligned_be16() which doesn't need the cast then carry > on using the original buffer. >> >> Thanks again for the review David :) >> >> I am unsure which way to go. I kind of like having the __be16 in the >> struct, as it immediately yells "data from device is big-endian". OTOH, >> I've never loved unions (and, it silences the above "yelling" quite a >> bit). I still think this might be the first time I really see a valid >> use-case for an union :) And, you're right this adds more lines, >> besides, the cast doesn't look that ugly to me. Yet, I originally had a >> cast probably as simple as this (and I also had the __be16 in the >> struct), and Jonathan suggested using union to avoid it... >> >> At the end of the day, I suppose I am Okay with any of these 3 >> approaches. Original cast, union or this cast you suggest. Jonathan, any >> preferences on your side? > > Majority of the diff is really about renaming buffer to tx_buffer. > Could just not bother doing that and instead have buffer and buffer16 > as the two union elements. With msg gone as suggested above, then the diff > becomes only a few lines and you get to keep the nicety of it being either > a pair of u8s or a __be16. I was tempted to try using the spi_write_then_read() - but I suppose this may be kind of a hot path. I'll go with (not)renaming the buffer and dropping the msg, to squeeze the diff. Yours, -- Matti