From nobody Sun Feb 8 10:05:20 2026 Received: from mail-ej1-f50.google.com (mail-ej1-f50.google.com [209.85.218.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 E9AA91553BC for ; Mon, 19 Aug 2024 06:47:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724050057; cv=none; b=p791tJvJz82F9gJAMuYqBPa5OsvA530lc7Oe1E/L+IvKHVDb0PU/NDkiYvR5g20V2IOOina183Egg4KyT1eu4VS3nE9yQ7ykXbOJJ6dyM4pj1ENG1y8xqetMzyz+FyLOEwf3Bp09f561NcxIDg/goFfLSv07PqyqPYI8EHEeHg4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724050057; c=relaxed/simple; bh=4q8Vprd1dKQUEgOJBjHl6wrabEeAMYe6xi7dkIKlOgI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jBwWoSnQmPOWrRYogKGQ1TOhWyc0e9DIVm7ZzrB9XBBC/sww0CFgfR4MOa+4psM2w4WSZBSqMU8qKyKcYBDzsgOniQFqdDOEuXc1KcBqoRFR7ib8/mGhEXJefLXPdk3zbAmlAvWfu0OPjf7FP5B/RM4EwvGWGMoIJfCZeBYcmv0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=X7jwyivR; arc=none smtp.client-ip=209.85.218.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="X7jwyivR" Received: by mail-ej1-f50.google.com with SMTP id a640c23a62f3a-a7a920d2eb7so25860166b.3 for ; Sun, 18 Aug 2024 23:47:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1724050053; x=1724654853; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=uPp2QutB5fRwOoqtFquZn0XMYFXbyecUg+CSR3WijWE=; b=X7jwyivR1nzEfgz8NLlwPWBdNToMbtvHD4xVReqYC5yRMEJeoTO5ISJOGDPZAJcFdG zIAqvM6QcRfGNmG1zlt8H+d2FctuhpyDcoVXDMkKI8zaHrZ2+lE/TRNxaAiFDzHbMcPA OTO4VES6d5JFrJSbJOFZbkxYMBYorvHu8OOomeKtfUUFj64yMYOIfBi97aCMPbuq5IWd +AvZk2vBIjnL934mUcw4mUNXyLlzCvSYCJBB1eWM5bKn/Zqh1PUPZ9WJLDi6vIx7Qf2L om1GgvM+kd3FaDaWQ+9TLH0qQddBc/8hC0laknkW00apAYIU2GvRbEtRZs9WJpeOkY7z JiLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724050053; x=1724654853; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=uPp2QutB5fRwOoqtFquZn0XMYFXbyecUg+CSR3WijWE=; b=OPzQxvcLqnH24FTeP8e/jI1rXgzSpLQDcTeUk21ueEwXBXdrm5AVlU3uF7q2dFs533 Vmhk4V7o9wuD93nk1w+iMVyJvZqlM7H9Z7kRFtsMVr/ok1ezgTaGKAzYRn7M0AMwndKZ zCaY2D54ywKgO3VbS7s0j+iYidYidm8uej+eVlL/csaKm/hQbz2i6xttUV3LBBff8ddQ W9dcHshM2M80xMdQSzVgbT5tvktD7x6oUvtMKgo5t5s5w61W084BkqeS/W2xHA5STHYC 9czbI3waY2+7HkpeyOhVZePLzSbGqHQ3CJyOJ2J0ASgNs70xsvVPwRgS+IPMQ98cgMmT PNAw== X-Forwarded-Encrypted: i=1; AJvYcCXyE3kATQ5Y0UZJn9+LiN5Afp0qCZVKw6PuUgumfUgmUxOarM1jBLWZ5MAbR7R8jvho2J1tqu88Sx81os8p3mWp3d+d+lmFA6rzQLT7 X-Gm-Message-State: AOJu0YyF6gxGiP5+WXolkgvEXiulS1qzWC008sEEZF/0l/eJyKV6Rl8i RQ8T15UlrGtJ4SAE4zjst6p166x8yGyXtBecPzUhkJOAJaN5Hav8E3+T9lQFSjI= X-Google-Smtp-Source: AGHT+IGcitxgqNBRI2malkDy89mFKz+g+dQe1jnCQmiQ89T31eqOa0U6bESkdGc79IJA08lxuZveQg== X-Received: by 2002:a17:907:7e95:b0:a72:499a:e5ba with SMTP id a640c23a62f3a-a8392a5c6cfmr367581766b.7.1724050052839; Sun, 18 Aug 2024 23:47:32 -0700 (PDT) Received: from neptune.lan ([188.27.128.229]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a83838c6bfcsm599069966b.28.2024.08.18.23.47.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Aug 2024 23:47:32 -0700 (PDT) From: Alexandru Ardelean To: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org Cc: jic23@kernel.org, krzk+dt@kernel.org, robh@kernel.org, lars@metafoo.de, michael.hennerich@analog.com, gstols@baylibre.com, Alexandru Ardelean Subject: [PATCH 1/7] iio: adc: ad7606: add 'bits' parameter to channels macros Date: Mon, 19 Aug 2024 09:47:11 +0300 Message-ID: <20240819064721.91494-2-aardelean@baylibre.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240819064721.91494-1-aardelean@baylibre.com> References: <20240819064721.91494-1-aardelean@baylibre.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" There are some newer additions to the AD7606 family, which support 18 bit precision. Up until now, all chips were 16 bit. This change adds a 'bits' parameter to the AD760X_CHANNEL macro and renames 'ad7606_channels' -> 'ad7606_channels_16bit' for the current devices. The AD7606_CHANNEL_PER_CHAN_SCALE() macro is also introduced, as it will also require that the number of bits be correctly adjusted (for 18 bit parts). Signed-off-by: Alexandru Ardelean --- drivers/iio/adc/ad7606.c | 58 ++++++++++++++++++------------------ drivers/iio/adc/ad7606.h | 18 ++++++----- drivers/iio/adc/ad7606_spi.c | 16 +++++----- 3 files changed, 47 insertions(+), 45 deletions(-) diff --git a/drivers/iio/adc/ad7606.c b/drivers/iio/adc/ad7606.c index 539e4a8621fe..dba1f28782e4 100644 --- a/drivers/iio/adc/ad7606.c +++ b/drivers/iio/adc/ad7606.c @@ -333,16 +333,16 @@ static const struct iio_chan_spec ad7605_channels[] = =3D { AD7605_CHANNEL(3), }; =20 -static const struct iio_chan_spec ad7606_channels[] =3D { +static const struct iio_chan_spec ad7606_channels_16bit[] =3D { IIO_CHAN_SOFT_TIMESTAMP(8), - AD7606_CHANNEL(0), - AD7606_CHANNEL(1), - AD7606_CHANNEL(2), - AD7606_CHANNEL(3), - AD7606_CHANNEL(4), - AD7606_CHANNEL(5), - AD7606_CHANNEL(6), - AD7606_CHANNEL(7), + AD7606_CHANNEL(0, 16), + AD7606_CHANNEL(1, 16), + AD7606_CHANNEL(2, 16), + AD7606_CHANNEL(3, 16), + AD7606_CHANNEL(4, 16), + AD7606_CHANNEL(5, 16), + AD7606_CHANNEL(6, 16), + AD7606_CHANNEL(7, 16), }; =20 /* @@ -357,22 +357,22 @@ static const struct iio_chan_spec ad7606_channels[] = =3D { */ static const struct iio_chan_spec ad7616_channels[] =3D { IIO_CHAN_SOFT_TIMESTAMP(16), - AD7606_CHANNEL(0), - AD7606_CHANNEL(1), - AD7606_CHANNEL(2), - AD7606_CHANNEL(3), - AD7606_CHANNEL(4), - AD7606_CHANNEL(5), - AD7606_CHANNEL(6), - AD7606_CHANNEL(7), - AD7606_CHANNEL(8), - AD7606_CHANNEL(9), - AD7606_CHANNEL(10), - AD7606_CHANNEL(11), - AD7606_CHANNEL(12), - AD7606_CHANNEL(13), - AD7606_CHANNEL(14), - AD7606_CHANNEL(15), + AD7606_CHANNEL(0, 16), + AD7606_CHANNEL(1, 16), + AD7606_CHANNEL(2, 16), + AD7606_CHANNEL(3, 16), + AD7606_CHANNEL(4, 16), + AD7606_CHANNEL(5, 16), + AD7606_CHANNEL(6, 16), + AD7606_CHANNEL(7, 16), + AD7606_CHANNEL(8, 16), + AD7606_CHANNEL(9, 16), + AD7606_CHANNEL(10, 16), + AD7606_CHANNEL(11, 16), + AD7606_CHANNEL(12, 16), + AD7606_CHANNEL(13, 16), + AD7606_CHANNEL(14, 16), + AD7606_CHANNEL(15, 16), }; =20 static const struct ad7606_chip_info ad7606_chip_info_tbl[] =3D { @@ -382,25 +382,25 @@ static const struct ad7606_chip_info ad7606_chip_info= _tbl[] =3D { .num_channels =3D 5, }, [ID_AD7606_8] =3D { - .channels =3D ad7606_channels, + .channels =3D ad7606_channels_16bit, .num_channels =3D 9, .oversampling_avail =3D ad7606_oversampling_avail, .oversampling_num =3D ARRAY_SIZE(ad7606_oversampling_avail), }, [ID_AD7606_6] =3D { - .channels =3D ad7606_channels, + .channels =3D ad7606_channels_16bit, .num_channels =3D 7, .oversampling_avail =3D ad7606_oversampling_avail, .oversampling_num =3D ARRAY_SIZE(ad7606_oversampling_avail), }, [ID_AD7606_4] =3D { - .channels =3D ad7606_channels, + .channels =3D ad7606_channels_16bit, .num_channels =3D 5, .oversampling_avail =3D ad7606_oversampling_avail, .oversampling_num =3D ARRAY_SIZE(ad7606_oversampling_avail), }, [ID_AD7606B] =3D { - .channels =3D ad7606_channels, + .channels =3D ad7606_channels_16bit, .num_channels =3D 9, .oversampling_avail =3D ad7606_oversampling_avail, .oversampling_num =3D ARRAY_SIZE(ad7606_oversampling_avail), diff --git a/drivers/iio/adc/ad7606.h b/drivers/iio/adc/ad7606.h index 0c6a88cc4695..771121350f98 100644 --- a/drivers/iio/adc/ad7606.h +++ b/drivers/iio/adc/ad7606.h @@ -8,7 +8,7 @@ #ifndef IIO_ADC_AD7606_H_ #define IIO_ADC_AD7606_H_ =20 -#define AD760X_CHANNEL(num, mask_sep, mask_type, mask_all) { \ +#define AD760X_CHANNEL(num, mask_sep, mask_type, mask_all, bits) { \ .type =3D IIO_VOLTAGE, \ .indexed =3D 1, \ .channel =3D num, \ @@ -19,24 +19,26 @@ .scan_index =3D num, \ .scan_type =3D { \ .sign =3D 's', \ - .realbits =3D 16, \ - .storagebits =3D 16, \ + .realbits =3D (bits), \ + .storagebits =3D (bits), \ .endianness =3D IIO_CPU, \ }, \ } =20 #define AD7605_CHANNEL(num) \ AD760X_CHANNEL(num, BIT(IIO_CHAN_INFO_RAW), \ - BIT(IIO_CHAN_INFO_SCALE), 0) + BIT(IIO_CHAN_INFO_SCALE), 0, 16) =20 -#define AD7606_CHANNEL(num) \ +#define AD7606_CHANNEL(num, bits) \ AD760X_CHANNEL(num, BIT(IIO_CHAN_INFO_RAW), \ BIT(IIO_CHAN_INFO_SCALE), \ - BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO)) + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), bits) =20 -#define AD7616_CHANNEL(num) \ +#define AD7606_SW_CHANNEL(num, bits) \ AD760X_CHANNEL(num, BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),\ - 0, BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO)) + 0, BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), bits) + +#define AD7616_CHANNEL(num) AD7606_SW_CHANNEL(num, 16) =20 /** * struct ad7606_chip_info - chip specific information diff --git a/drivers/iio/adc/ad7606_spi.c b/drivers/iio/adc/ad7606_spi.c index 287a0591533b..dd0075c97c24 100644 --- a/drivers/iio/adc/ad7606_spi.c +++ b/drivers/iio/adc/ad7606_spi.c @@ -67,14 +67,14 @@ static const struct iio_chan_spec ad7616_sw_channels[] = =3D { =20 static const struct iio_chan_spec ad7606b_sw_channels[] =3D { IIO_CHAN_SOFT_TIMESTAMP(8), - AD7616_CHANNEL(0), - AD7616_CHANNEL(1), - AD7616_CHANNEL(2), - AD7616_CHANNEL(3), - AD7616_CHANNEL(4), - AD7616_CHANNEL(5), - AD7616_CHANNEL(6), - AD7616_CHANNEL(7), + AD7606_SW_CHANNEL(0, 16), + AD7606_SW_CHANNEL(1, 16), + AD7606_SW_CHANNEL(2, 16), + AD7606_SW_CHANNEL(3, 16), + AD7606_SW_CHANNEL(4, 16), + AD7606_SW_CHANNEL(5, 16), + AD7606_SW_CHANNEL(6, 16), + AD7606_SW_CHANNEL(7, 16), }; =20 static const unsigned int ad7606B_oversampling_avail[9] =3D { --=20 2.46.0 From nobody Sun Feb 8 10:05:20 2026 Received: from mail-lj1-f177.google.com (mail-lj1-f177.google.com [209.85.208.177]) (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 EF06315748F for ; Mon, 19 Aug 2024 06:47:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724050059; cv=none; b=qb4HT6xnCdOxa7SqHTm7vEicqCly4KjEEHlVd4k3/iSZYA58Zoqwxk90eu4vDPmYf0pDGr+ntTHaI8HbWrlnXYLe7mM8I+v380KrJ3brvb2XflFP4QAIfWQ9A4zpue5jFitmHH3ABrs+sC2T3tZJZ48PHAUTGkpreitIZGxAjJM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724050059; c=relaxed/simple; bh=IXqmhsQDt/RO+GElC8x8ywqYUr/3SWcgPZP7XLJrRH0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WDZbszdm/sI4gGDEfCAanfrvkbbpg/RbVIHiQO99wTemYO/lfFmp3d+hsY1oWKInBw9JMUtNW+9o8TNy7XjT7dwVxvtF1StHHht/cOeNgFj13cDElLG5DAWHPUdZU9ZCQyqHjvWcYTco7iWBBPHvCbMFKam9hOriorZ8tb7qnoY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=1jypdwtj; arc=none smtp.client-ip=209.85.208.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="1jypdwtj" Received: by mail-lj1-f177.google.com with SMTP id 38308e7fff4ca-2ef2c0f35f2so3349251fa.1 for ; Sun, 18 Aug 2024 23:47:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1724050056; x=1724654856; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Ui2lv4x9FTPWujQRhFdVeT4msHXJUFxc93RUhnH6Vdg=; b=1jypdwtjdxhqrXu/8RUedTInpQxj1PitmoA0S58gHDADay5bHn90jP+x0Xba6etA2l R+asHeWEazPHWt0I3Pqzzw7FoZQdKeL2AiOQPvkM3UyAa83XuHtOQmYI6yQpC16Cq4J/ rOWa/2D1EAaF4Q2g8y6aUalaSDyqdroeHcYNtK+3PlzhfV1LctlBsfLmwdCdfE3Rdahq BkeFf17pClzhxSBHb7KNY6jIb6sA6am8HUlPbhUURbepT7Gsp3E/8IP7b28Q57T2LWOr enPmXbacn302cg7IsCLBXywu1O7hoMMc5ofDjsL7WSJPLFH/zuxiCDmOSXd/DTpAb4Vz 2S4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724050056; x=1724654856; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ui2lv4x9FTPWujQRhFdVeT4msHXJUFxc93RUhnH6Vdg=; b=Cb24og4VdEg2K5vfixj98MzM/FcKPqaJshSlmb7YkDd/scfHoIErBBpMKTbciQESB/ 1qmP6OpLFaR+v8ypLA/VE9QlK4Y9Lr6hYh+++HBuAf1yT/AlB08Ejgma45xVrTLIwKrr rZaizTDS55FCht60JzzbH++AQ7fIm7BfGuzET/1LUMFc0wqDtHTvYdV3hIDgnST+EGUD Tlo12HkiVBfb/IDduJP8ZF3F7ELvMMbNmAjuGHLZeQKFYIEaJ0I3GV/F5yv27P8qWXT+ GxvWEQuPpvihYIHc75gWQfL0G0XqUc/yBKP2Njkwq5J/p8A7mGIyDr23wUXKR/XZlAC5 VP7Q== X-Forwarded-Encrypted: i=1; AJvYcCUHLCRPjYwkV1kzTZV2A02XL7FgcLC/rD8yVmQ58v9esEwbOrAb15mhy4TXil0bGRXEtYQLDyDqd8irfYNpWgLw1DB1VdSmgHj45Bsi X-Gm-Message-State: AOJu0YyUZnOKTOltZqDyqV4ChdOUAXGYKJDCHmn0dtaq0ZNr8NWH4mko Zes4PJehrH88FQJhd3uA1in4whcgX9oC0g4jQRLkr5SJdyjQBFO0pyk5AdXiVgeSx7eYXRatuje g8QA= X-Google-Smtp-Source: AGHT+IH4B1cciVB7OI8ow2IPgAdtaVxaiulvsVvuv81Mby5vIFPvQVAHDd0cmEg8ZBhdUoQ5hAgPsA== X-Received: by 2002:a05:651c:b10:b0:2ee:d55c:255f with SMTP id 38308e7fff4ca-2f3be778bacmr40875561fa.7.1724050055727; Sun, 18 Aug 2024 23:47:35 -0700 (PDT) Received: from neptune.lan ([188.27.128.229]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a83838c6bfcsm599069966b.28.2024.08.18.23.47.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Aug 2024 23:47:35 -0700 (PDT) From: Alexandru Ardelean To: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org Cc: jic23@kernel.org, krzk+dt@kernel.org, robh@kernel.org, lars@metafoo.de, michael.hennerich@analog.com, gstols@baylibre.com, Alexandru Ardelean Subject: [PATCH 2/7] iio: adc: ad7606: move 'val' pointer to ad7606_scan_direct() Date: Mon, 19 Aug 2024 09:47:12 +0300 Message-ID: <20240819064721.91494-3-aardelean@baylibre.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240819064721.91494-1-aardelean@baylibre.com> References: <20240819064721.91494-1-aardelean@baylibre.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The ad7606_scan_direct() function returns 'int', which is fine for 16-bit samples. But when going to 18-bit samples, these need to be implemented as 32-bit (or int) type. In that case when getting samples (which can be negative), we'd get random error codes. So, the easiest thing is to just move the 'val' pointer to 'ad7606_scan_direct()'. This doesn't qualify as a fix, it's just a preparation for 18-bit ADCs (of the AD7606 family). Signed-off-by: Alexandru Ardelean --- drivers/iio/adc/ad7606.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/iio/adc/ad7606.c b/drivers/iio/adc/ad7606.c index dba1f28782e4..5049e37f8393 100644 --- a/drivers/iio/adc/ad7606.c +++ b/drivers/iio/adc/ad7606.c @@ -138,7 +138,8 @@ static irqreturn_t ad7606_trigger_handler(int irq, void= *p) return IRQ_HANDLED; } =20 -static int ad7606_scan_direct(struct iio_dev *indio_dev, unsigned int ch) +static int ad7606_scan_direct(struct iio_dev *indio_dev, unsigned int ch, + int *val) { struct ad7606_state *st =3D iio_priv(indio_dev); int ret; @@ -153,7 +154,7 @@ static int ad7606_scan_direct(struct iio_dev *indio_dev= , unsigned int ch) =20 ret =3D ad7606_read_samples(st); if (ret =3D=3D 0) - ret =3D st->data[ch]; + *val =3D sign_extend32(st->data[ch], 15); =20 error_ret: gpiod_set_value(st->gpio_convst, 0); @@ -173,10 +174,9 @@ static int ad7606_read_raw(struct iio_dev *indio_dev, switch (m) { case IIO_CHAN_INFO_RAW: iio_device_claim_direct_scoped(return -EBUSY, indio_dev) { - ret =3D ad7606_scan_direct(indio_dev, chan->address); + ret =3D ad7606_scan_direct(indio_dev, chan->address, val); if (ret < 0) return ret; - *val =3D (short) ret; return IIO_VAL_INT; } unreachable(); --=20 2.46.0 From nobody Sun Feb 8 10:05:20 2026 Received: from mail-lj1-f169.google.com (mail-lj1-f169.google.com [209.85.208.169]) (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 4C1B91547E6 for ; Mon, 19 Aug 2024 06:47:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724050062; cv=none; b=APoNBGfCmXbBPesfm0FkPG4qoXL5YEJcLruvkqtJDZgfp8JWb/hYolUYsGN1f3uXv+OJnLyu+PHro8gkILFAEbA/9phCjb9hrLkSdzpSuZk0O8rgUjYVT5eH+1TBcWrSO96/JPqek04lAgYMs/XgNi0qu0v1PzCv/TPIaaRObdI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724050062; c=relaxed/simple; bh=9wWTe2RhHwvr6CPLWLyUmFQssffTJWZ48+UUgVNgMfA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Fp3YwAJlNdpQ6K08O2FTtgdSZMZLdynXGY0zeH/JYvTQPvRJYUg59r8DxPUYFf/XoEBXtyz2WBHATxgchwsZ6/JTTteFfYSJAtGJgpv/IJ2FZ0BwAFxE/+F0zY86HIk1ZUKnnFa8ce3JeEmkAih/jzXpg9P7PseHwSkNK+UGOCQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=c04hrCL8; arc=none smtp.client-ip=209.85.208.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="c04hrCL8" Received: by mail-lj1-f169.google.com with SMTP id 38308e7fff4ca-2ef302f5dbbso3443561fa.2 for ; Sun, 18 Aug 2024 23:47:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1724050058; x=1724654858; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=WRucj7sLZEUBkFzhvRqpr3JKNs6+16Ez8lks/iAJ3Xo=; b=c04hrCL8DS/AGHsbzeX6exIwZiBs3fyXre/EH724jOqgWdQrbsHJ41WLRMRWzfW0+9 v5n5TsCHUcsfarp5nX+O7dBaiG7owuXCDdXBGbqqRte7mGVVMK1KQzhPaUNLmTSubYUz I6+iNUO0VzRn+Rpa5XwRIo4WrPcCyI0QxREoT57WO/CCMu8UqdzXyL6MgJFNS0SiLxVg IOuH9RFaNSnEXm0JUxcZtaiRoUENbwcdbvnE929mexz0cI6CL12k5UfmAr/noXgLj3o4 pKd/zdo+hoXCYFrkShwi+aNFDqx75zn0cOrboTzTTJgnPj2cxDEp/FLJIHPm5N5DTH7/ OYew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724050058; x=1724654858; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WRucj7sLZEUBkFzhvRqpr3JKNs6+16Ez8lks/iAJ3Xo=; b=C1uishrNKvmjhsl7gjO2yVrA07PKjtmSSdkJJkoEfpUlzJiorc0qBwsn1LINBHbDX2 HJFOwjQoPbffyYjtHE6DqSoZ0+w7COmo5MN5yNAfNAYyWHnMFfalDaipTpu/5PGDQODh zi7mbYO0kTqegUjmMLHuQ0SXEAU5fJ5fbXF1fzsnxjmYvVsHz5pPBJFKWjyHVf5/fIyy TLttevyXZSNLqCCMUXBHkvXFbw3kQjpSEpZiJG42Osu1BC69k9Bnz/XPyz1r65pN8p9g 9ub9DCP/HX1Hu+tNIwkFp9Hf0Mo9iT4FCc+Pqg9WAnn/xUf8csDufHnnHlNf97vDyu4J PC9A== X-Forwarded-Encrypted: i=1; AJvYcCVgrYyGQ/llCP+b/VrUycRvchLnnP5rfVmAgD0TvJXcVfYQXN2nrDP8s9FyAw3v/jxEkeGP4Ltv7XD/31h+OW7v8U4QckJVsqBH4Xk5 X-Gm-Message-State: AOJu0YyEmAsMQccEff+vkACrUqxeqdhWFGAoKrctHXtz6UI4ajlx4mTE JEFdLdiiMpQX2KkLybk4/ZXirCm6Q/jhi7ApN5gB+yDYNA0rCrxrONgcH3IH9GF/bzG9U/qAWOL ooOE= X-Google-Smtp-Source: AGHT+IF+DEbik1uOC4GS0ALFsCh+5Y7rbXbhvxj5lhq3956byF8gYPkdxyO6ujzsaXjae8xATGu/eQ== X-Received: by 2002:a05:651c:19a9:b0:2ef:23af:f1f2 with SMTP id 38308e7fff4ca-2f3be78709cmr38344991fa.9.1724050058380; Sun, 18 Aug 2024 23:47:38 -0700 (PDT) Received: from neptune.lan ([188.27.128.229]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a83838c6bfcsm599069966b.28.2024.08.18.23.47.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Aug 2024 23:47:38 -0700 (PDT) From: Alexandru Ardelean To: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org Cc: jic23@kernel.org, krzk+dt@kernel.org, robh@kernel.org, lars@metafoo.de, michael.hennerich@analog.com, gstols@baylibre.com, Alexandru Ardelean Subject: [PATCH 3/7] iio: adc: ad7606: split a 'ad7606_sw_mode_setup()' from probe Date: Mon, 19 Aug 2024 09:47:13 +0300 Message-ID: <20240819064721.91494-4-aardelean@baylibre.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240819064721.91494-1-aardelean@baylibre.com> References: <20240819064721.91494-1-aardelean@baylibre.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-Transfer-Encoding: quoted-printable This change moves the logic for setting up SW mode (during probe) into it's own function. With the addition of some newer parts, the SW-mode part can get a little more complicated. So it's a bit better to have a separate function for this. Signed-off-by: Alexandru Ardelean --- drivers/iio/adc/ad7606.c | 43 ++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/drivers/iio/adc/ad7606.c b/drivers/iio/adc/ad7606.c index 5049e37f8393..b400c9b2519d 100644 --- a/drivers/iio/adc/ad7606.c +++ b/drivers/iio/adc/ad7606.c @@ -545,6 +545,29 @@ static const struct iio_trigger_ops ad7606_trigger_ops= =3D { .validate_device =3D iio_trigger_validate_own_device, }; =20 +static int ad7606_sw_mode_setup(struct iio_dev *indio_dev) +{ + struct ad7606_state *st =3D iio_priv(indio_dev); + + if (!st->bops->sw_mode_config) + return 0; + + st->sw_mode_en =3D device_property_present(st->dev, "adi,sw-mode"); + if (!st->sw_mode_en) + return 0; + + indio_dev->info =3D &ad7606_info_os_range_and_debug; + + /* Scale of 0.076293 is only available in sw mode */ + st->scale_avail =3D ad7616_sw_scale_avail; + st->num_scales =3D ARRAY_SIZE(ad7616_sw_scale_avail); + + /* After reset, in software mode, =C2=B110 V is set by default */ + memset32(st->range, 2, ARRAY_SIZE(st->range)); + + return st->bops->sw_mode_config(indio_dev); +} + int ad7606_probe(struct device *dev, int irq, void __iomem *base_address, const char *name, unsigned int id, const struct ad7606_bus_ops *bops) @@ -617,23 +640,9 @@ int ad7606_probe(struct device *dev, int irq, void __i= omem *base_address, st->write_scale =3D ad7606_write_scale_hw; st->write_os =3D ad7606_write_os_hw; =20 - if (st->bops->sw_mode_config) - st->sw_mode_en =3D device_property_present(st->dev, - "adi,sw-mode"); - - if (st->sw_mode_en) { - /* Scale of 0.076293 is only available in sw mode */ - st->scale_avail =3D ad7616_sw_scale_avail; - st->num_scales =3D ARRAY_SIZE(ad7616_sw_scale_avail); - - /* After reset, in software mode, =C2=B110 V is set by default */ - memset32(st->range, 2, ARRAY_SIZE(st->range)); - indio_dev->info =3D &ad7606_info_os_range_and_debug; - - ret =3D st->bops->sw_mode_config(indio_dev); - if (ret < 0) - return ret; - } + ret =3D ad7606_sw_mode_setup(indio_dev); + if (ret) + return ret; =20 st->trig =3D devm_iio_trigger_alloc(dev, "%s-dev%d", indio_dev->name, --=20 2.46.0 From nobody Sun Feb 8 10:05:20 2026 Received: from mail-ed1-f49.google.com (mail-ed1-f49.google.com [209.85.208.49]) (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 0D1B41586FE for ; Mon, 19 Aug 2024 06:47:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724050065; cv=none; b=LxjJ6YJDx/K2scXHSKFflAG4/bEnZu3cBTY8/SST+C0ZVP65esRLw9m1SEzV4H/uT4XJzTvVbcMmRJjaRuZrOcyn2WqRuIjspS9aNd+AgDoDLTq0GjRK1eNS7nFnfFK5wdoR4L1WJiKjz8K0nGBmJVqaDLTV88a3+kCoEGdtet4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724050065; c=relaxed/simple; bh=ZK9Z1/Q4filbKX1kKWRghKSgUBwXpAJI26eeyznAt94=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=cx6KPq5IlPrYTTYrKBuNkYVmrRltFfQX1stk9mFF2RqU2vIPV7zG5S2wQhDf17zzfhCTxe909z5jB9l4gEh/MeFIm2YzmbevCoKGkazamS5nEcoNto5y9RzZU3djy28FfZY2U8j6z29WyM6sqITmFZTrlFH+vOqoq7DO8Bms9Mo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=fijMUKEq; arc=none smtp.client-ip=209.85.208.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="fijMUKEq" Received: by mail-ed1-f49.google.com with SMTP id 4fb4d7f45d1cf-5bec50de782so600186a12.1 for ; Sun, 18 Aug 2024 23:47:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1724050061; x=1724654861; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=OMjsY+V8C+xpLeayM52S92PbmAJB3Osj5meZbpqVaZk=; b=fijMUKEq2XedXxUEHBQXwErQt0aic1z9yMHWtHcthEHpMXzuBl9O4jPcuBC5n00PIe pWNmfYP8Ghii0ITjcDXAul/p7R38/3EwBPTW7BicWjc1C7yKmajNVfzks6Gtcwu5PH9W saRupSTkU40FtGDdV6wLZdFobmPEigQEy4+MlxVCE3YBLF6ZwzmfJwa0kABbXeHmYYQn A4anFCjvbYKX7XtIVh3H73y8Ae/yy0JPuIJZVtzRuA0W7sPBOUV4D0botiRrjCYE9QgF M5IiXscjijH2igu7LXObxrSNCFo3CGn/bCMerXAZlgSD57j5nGZQR3Kt5jrsDSpiEFf+ szTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724050061; x=1724654861; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OMjsY+V8C+xpLeayM52S92PbmAJB3Osj5meZbpqVaZk=; b=qqCz34CNVd4phs74PAcj40j0IW1IS/++IYMtQ/ENp6qOVsBI3IH+vyYEBISM+VgHeI w3cPkYxlA584Hk2PWR0zo05Oq4cAWWvWmvZbpshyuZSnkc8ZxThN/wMaXe0YCQTp7AZO eW4+8BQS2iH710XobUkMJbrqWYZEnL9d/LQhEjwBXYcYhfyIJ2d4IC5g0oApTzz5Kdbs GXjx7g/F6wKPbP6dKXz/ig5BTM5bsI4zX03UxXPo0pIvN8GpArvJNPlc6tIukpffL6uj MrjXdEoUI6aU0gSvPU9AjmTbwkhZQdkwuQhJE8d2IvcTmn0KdkW6kxsa1rBNLsqYl2Be LJ6w== X-Forwarded-Encrypted: i=1; AJvYcCVk3Is/lL3A7dyYCWOqbcdTptfdzwH5BB5gwEfnISjKdsop+Krh5h9MDQbBggWCvIWQ0VwB7I2sInZsF0mJAhvhVbqCR4mgPGjzCzZh X-Gm-Message-State: AOJu0Yw4iXF47EGdrtqFRPcxP4rfoo/cJjALMNstKzh778wld+FyBrfw Sph/pK2+jcDHNi/QXCLhcVQNCZfFC/s4CY9nYpthbu1Q5FH9LoyjsR6E53wsRWM= X-Google-Smtp-Source: AGHT+IHJnJbkmdTrX7s12spzvoZRN74RsDx4TSyadgSkApcAiXcpxyKNu1VgqMgPz6xu/5Cvo/q8qw== X-Received: by 2002:a17:906:6a18:b0:a80:ed7e:8468 with SMTP id a640c23a62f3a-a8392a9c641mr414381466b.0.1724050061333; Sun, 18 Aug 2024 23:47:41 -0700 (PDT) Received: from neptune.lan ([188.27.128.229]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a83838c6bfcsm599069966b.28.2024.08.18.23.47.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Aug 2024 23:47:41 -0700 (PDT) From: Alexandru Ardelean To: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org Cc: jic23@kernel.org, krzk+dt@kernel.org, robh@kernel.org, lars@metafoo.de, michael.hennerich@analog.com, gstols@baylibre.com, Alexandru Ardelean Subject: [PATCH 4/7] iio: adc: ad7606: wrap channel ranges & scales into struct Date: Mon, 19 Aug 2024 09:47:14 +0300 Message-ID: <20240819064721.91494-5-aardelean@baylibre.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240819064721.91494-1-aardelean@baylibre.com> References: <20240819064721.91494-1-aardelean@baylibre.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-Transfer-Encoding: quoted-printable With the addition of AD7606C-16,18 which have differential & bipolar channels (and ranges), which can vary from channel to channel, we'll need to keep more information about each channel range. To do that, we'll add a 'struct ad7606_chan_scale' type to hold just configuration for each channel. This includes the scales per channel (which can be different with AD7606C-16,18), as well as the range for each channel. This driver was already keeping the range value for each channel before, and since this is couple with the scales, it also makes sense to put them in the same struct. Signed-off-by: Alexandru Ardelean --- drivers/iio/adc/ad7606.c | 37 +++++++++++++++++++++++++------------ drivers/iio/adc/ad7606.h | 22 ++++++++++++++++------ 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/drivers/iio/adc/ad7606.c b/drivers/iio/adc/ad7606.c index b400c9b2519d..2554a4a4a9c0 100644 --- a/drivers/iio/adc/ad7606.c +++ b/drivers/iio/adc/ad7606.c @@ -170,6 +170,7 @@ static int ad7606_read_raw(struct iio_dev *indio_dev, { int ret, ch =3D 0; struct ad7606_state *st =3D iio_priv(indio_dev); + struct ad7606_chan_scale *cs; =20 switch (m) { case IIO_CHAN_INFO_RAW: @@ -183,8 +184,9 @@ static int ad7606_read_raw(struct iio_dev *indio_dev, case IIO_CHAN_INFO_SCALE: if (st->sw_mode_en) ch =3D chan->address; + cs =3D &st->chan_scales[ch]; *val =3D 0; - *val2 =3D st->scale_avail[st->range[ch]]; + *val2 =3D cs->scale_avail[cs->range]; return IIO_VAL_INT_PLUS_MICRO; case IIO_CHAN_INFO_OVERSAMPLING_RATIO: *val =3D st->oversampling; @@ -214,8 +216,9 @@ static ssize_t in_voltage_scale_available_show(struct d= evice *dev, { struct iio_dev *indio_dev =3D dev_to_iio_dev(dev); struct ad7606_state *st =3D iio_priv(indio_dev); + struct ad7606_chan_scale *cs =3D &st->chan_scales[0]; =20 - return ad7606_show_avail(buf, st->scale_avail, st->num_scales, true); + return ad7606_show_avail(buf, cs->scale_avail, cs->num_scales, true); } =20 static IIO_DEVICE_ATTR_RO(in_voltage_scale_available, 0); @@ -253,19 +256,21 @@ static int ad7606_write_raw(struct iio_dev *indio_dev, long mask) { struct ad7606_state *st =3D iio_priv(indio_dev); + struct ad7606_chan_scale *cs; int i, ret, ch =3D 0; =20 guard(mutex)(&st->lock); =20 switch (mask) { case IIO_CHAN_INFO_SCALE: - i =3D find_closest(val2, st->scale_avail, st->num_scales); if (st->sw_mode_en) ch =3D chan->address; + cs =3D &st->chan_scales[ch]; + i =3D find_closest(val2, cs->scale_avail, cs->num_scales); ret =3D st->write_scale(indio_dev, ch, i); if (ret < 0) return ret; - st->range[ch] =3D i; + cs->range =3D i; =20 return 0; case IIO_CHAN_INFO_OVERSAMPLING_RATIO: @@ -547,7 +552,9 @@ static const struct iio_trigger_ops ad7606_trigger_ops = =3D { =20 static int ad7606_sw_mode_setup(struct iio_dev *indio_dev) { + unsigned int num_channels =3D indio_dev->num_channels - 1; struct ad7606_state *st =3D iio_priv(indio_dev); + int ch; =20 if (!st->bops->sw_mode_config) return 0; @@ -559,11 +566,14 @@ static int ad7606_sw_mode_setup(struct iio_dev *indio= _dev) indio_dev->info =3D &ad7606_info_os_range_and_debug; =20 /* Scale of 0.076293 is only available in sw mode */ - st->scale_avail =3D ad7616_sw_scale_avail; - st->num_scales =3D ARRAY_SIZE(ad7616_sw_scale_avail); - /* After reset, in software mode, =C2=B110 V is set by default */ - memset32(st->range, 2, ARRAY_SIZE(st->range)); + for (ch =3D 0; ch < num_channels; ch++) { + struct ad7606_chan_scale *cs =3D &st->chan_scales[ch]; + + cs->scale_avail =3D ad7616_sw_scale_avail; + cs->num_scales =3D ARRAY_SIZE(ad7616_sw_scale_avail); + cs->range =3D 2; + } =20 return st->bops->sw_mode_config(indio_dev); } @@ -572,6 +582,7 @@ int ad7606_probe(struct device *dev, int irq, void __io= mem *base_address, const char *name, unsigned int id, const struct ad7606_bus_ops *bops) { + struct ad7606_chan_scale *cs; struct ad7606_state *st; int ret; struct iio_dev *indio_dev; @@ -588,10 +599,12 @@ int ad7606_probe(struct device *dev, int irq, void __= iomem *base_address, st->bops =3D bops; st->base_address =3D base_address; /* tied to logic low, analog input range is +/- 5V */ - st->range[0] =3D 0; st->oversampling =3D 1; - st->scale_avail =3D ad7606_scale_avail; - st->num_scales =3D ARRAY_SIZE(ad7606_scale_avail); + + cs =3D &st->chan_scales[0]; + cs->range =3D 0; + cs->scale_avail =3D ad7606_scale_avail; + cs->num_scales =3D ARRAY_SIZE(ad7606_scale_avail); =20 ret =3D devm_regulator_get_enable(dev, "avcc"); if (ret) @@ -698,7 +711,7 @@ static int ad7606_resume(struct device *dev) struct ad7606_state *st =3D iio_priv(indio_dev); =20 if (st->gpio_standby) { - gpiod_set_value(st->gpio_range, st->range[0]); + gpiod_set_value(st->gpio_range, st->chan_scales[0].range); gpiod_set_value(st->gpio_standby, 1); ad7606_reset(st); } diff --git a/drivers/iio/adc/ad7606.h b/drivers/iio/adc/ad7606.h index 771121350f98..afe6a4030e0e 100644 --- a/drivers/iio/adc/ad7606.h +++ b/drivers/iio/adc/ad7606.h @@ -8,6 +8,8 @@ #ifndef IIO_ADC_AD7606_H_ #define IIO_ADC_AD7606_H_ =20 +#define AD760X_MAX_CHANNELS 16 + #define AD760X_CHANNEL(num, mask_sep, mask_type, mask_all, bits) { \ .type =3D IIO_VOLTAGE, \ .indexed =3D 1, \ @@ -60,17 +62,27 @@ struct ad7606_chip_info { unsigned long init_delay_ms; }; =20 +/** + * struct ad7606_chan_scale - channel scale configuration + * @scale_avail pointer to the array which stores the available scales + * @num_scales number of elements stored in the scale_avail array + * @range voltage range selection, selects which scale to apply + */ +struct ad7606_chan_scale { + const unsigned int *scale_avail; + unsigned int num_scales; + unsigned int range; +}; + /** * struct ad7606_state - driver instance specific data * @dev pointer to kernel device * @chip_info entry in the table of chips that describes this device * @bops bus operations (SPI or parallel) - * @range voltage range selection, selects which scale to apply + * @chan_scales scale configuration for channels * @oversampling oversampling selection * @base_address address from where to read data in parallel operation * @sw_mode_en software mode enabled - * @scale_avail pointer to the array which stores the available scales - * @num_scales number of elements stored in the scale_avail array * @oversampling_avail pointer to the array which stores the available * oversampling ratios. * @num_os_ratios number of elements stored in oversampling_avail array @@ -94,12 +106,10 @@ struct ad7606_state { struct device *dev; const struct ad7606_chip_info *chip_info; const struct ad7606_bus_ops *bops; - unsigned int range[16]; + struct ad7606_chan_scale chan_scales[AD760X_MAX_CHANNELS]; unsigned int oversampling; void __iomem *base_address; bool sw_mode_en; - const unsigned int *scale_avail; - unsigned int num_scales; const unsigned int *oversampling_avail; unsigned int num_os_ratios; int (*write_scale)(struct iio_dev *indio_dev, int ch, int val); --=20 2.46.0 From nobody Sun Feb 8 10:05:20 2026 Received: from mail-ed1-f52.google.com (mail-ed1-f52.google.com [209.85.208.52]) (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 C5E4A158D80 for ; Mon, 19 Aug 2024 06:47:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724050067; cv=none; b=bmk++HdWjEPu94AuFB8x1KPhHCeWQienBSpVBExehMYFgQ6uDyjM/y60gRabaaLs08POilHaKsMgx0z6kxbr+84mp+ZjkO/7aWc0cXVWC26Ifn/bzZvMWa8EF4sakaoASin8/xQm6NtgZlqweahAtfGpmP28ZMoz/pNNtY2GKVU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724050067; c=relaxed/simple; bh=pmtIgdhzWLa/ZKEbL2pBQv6vMBM4gTaKKLivfp+dDGM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=DyorHVDHMZNd23gw/z8yiUkMSD9SAJVTDyBjvXAzvheT/s2SDq687jTWQJqLbPBNFVZ/a0evSalCaBviq3eUhqwOBVOxsrO0RDEp55fFDrcLAXwmEa62DJdC/YA74HSKQs34LvhXW/4RYtUCnPm7nqAEvPFnIiE5VrgpUUpoK7s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=NSjML6X/; arc=none smtp.client-ip=209.85.208.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="NSjML6X/" Received: by mail-ed1-f52.google.com with SMTP id 4fb4d7f45d1cf-5beccb39961so549205a12.3 for ; Sun, 18 Aug 2024 23:47:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1724050064; x=1724654864; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=wH15hzmOmML1/BW9BysaySXYu+XiXmIpiL0tw7k3/FY=; b=NSjML6X/3Rrt3TVA7QvmEoDYV3IwvTyurKGHAZZ+TnkcfOhdBycJheULIuXaOSPvoO Kw6OTlz8SzV0f/rYCvUAdh5TO2p2Au94LFOJQZ4O3Q19tbVDH8MYk1DW9Q+xyWMXWF2y Sieqpwd4LORuXZAHVLZchsxGtBHAnJswhb7xjZqFWvaVogU5i9yTgaRQhdoXVTXYYpsF 4SzjIMGHdOg3z28jbF6UT2eBTq4ABqgO4fhQ4c30Kq930LcavEiXEK9dVlCVTYF5zDLV RgB0Sss1RzZL2BXQm0psD+/aWm+wf0bTYS+hJeAoslcNkA1+8goBqTpJIEzsQT/j9tZr L6KQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724050064; x=1724654864; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wH15hzmOmML1/BW9BysaySXYu+XiXmIpiL0tw7k3/FY=; b=gE0ZoBkAWptcdtoIwgcK1JOisoW9OgfHyCSQS43mLoeqFINB8EpTkuLdw+eU6C+QWF VA6lby+ByD9St5PUGrWXSPopsT8yOIKzKHqYg/bbKnJTyjmBKES7N+UGes983fRq1Ls5 gc+d4BrdcOOUEreWRHBuCZcDkjcO3D/ZLnHaB9bgwovd1qmvAENuM850dNl4AVE484Br i67CQM+CftsSit7TW+0kkWvyCT+HL5eQriD8wUKt8GD0VB4/DQQJwTjnE/2BLTWTfNuW Mpoc0iRHvjFdZvEmJ0XcUPe8x0Pkl8lma5AoiqElzu0WsdJxtIwYNE049+LVqu0nLeKR zcUg== X-Forwarded-Encrypted: i=1; AJvYcCV3cUV/nFIuHhmVoccYZKDVobgSLla8pwkkM7j4elCHmOjLC9b6/9G27QHstSFCpI0p5K0iSytENJY75P8=@vger.kernel.org X-Gm-Message-State: AOJu0YyenSIKw8So3jDMqSkcwkJEss64qXlYSFgUoKjh1oJjpaSR2Raz rwSxvd7uIjXfU/pb4r6DybeOVlJFlJE92KzOLeXejizIJvCm3OnJDBpIvME8pO0= X-Google-Smtp-Source: AGHT+IH68BFFWpDM5vWPFq3UiupHgzhwsbeUiVHxg9SktswkQ6m4w4j+2J2VbJumRgV6cICDuiY37Q== X-Received: by 2002:a17:907:96aa:b0:a7a:9f0f:ab1b with SMTP id a640c23a62f3a-a8392a41197mr396777266b.7.1724050064104; Sun, 18 Aug 2024 23:47:44 -0700 (PDT) Received: from neptune.lan ([188.27.128.229]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a83838c6bfcsm599069966b.28.2024.08.18.23.47.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Aug 2024 23:47:43 -0700 (PDT) From: Alexandru Ardelean To: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org Cc: jic23@kernel.org, krzk+dt@kernel.org, robh@kernel.org, lars@metafoo.de, michael.hennerich@analog.com, gstols@baylibre.com, Alexandru Ardelean Subject: [PATCH 5/7] iio: adc: ad7606: rework available attributes for SW channels Date: Mon, 19 Aug 2024 09:47:15 +0300 Message-ID: <20240819064721.91494-6-aardelean@baylibre.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240819064721.91494-1-aardelean@baylibre.com> References: <20240819064721.91494-1-aardelean@baylibre.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-Transfer-Encoding: quoted-printable For SW mode, the oversampling and scales attributes are always present. So, they can be implemented via a 'read_avail' hook in iio_info. For HW mode, it's a bit tricky, as these attributes get assigned based on GPIO definitions. So, for SW mode, we define a separate AD7606_SW_CHANNEL() macro, and use that for the SW channels. And 'ad7606_info_os_range_and_debug' can be renamed to 'ad7606_info_sw_mode' as it is only used for SW mode. For the 'read_avail' hook, we'll need to allocate the SW scales, so that they are just returned userspace without any extra processing. The oversampling available parameters don't need any extra processing. Signed-off-by: Alexandru Ardelean --- drivers/iio/adc/ad7606.c | 75 +++++++++++++++++++++++++++++++++++++--- drivers/iio/adc/ad7606.h | 30 +++++++++++++--- 2 files changed, 96 insertions(+), 9 deletions(-) diff --git a/drivers/iio/adc/ad7606.c b/drivers/iio/adc/ad7606.c index 2554a4a4a9c0..7533aab4b7c8 100644 --- a/drivers/iio/adc/ad7606.c +++ b/drivers/iio/adc/ad7606.c @@ -507,6 +507,37 @@ static int ad7606_buffer_predisable(struct iio_dev *in= dio_dev) return 0; } =20 +static int ad7606_read_avail(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + const int **vals, int *type, int *length, + long info) +{ + struct ad7606_state *st =3D iio_priv(indio_dev); + struct ad7606_chan_scale *cs; + unsigned int ch =3D 0; + + switch (info) { + case IIO_CHAN_INFO_OVERSAMPLING_RATIO: + *vals =3D st->oversampling_avail; + *length =3D st->num_os_ratios; + *type =3D IIO_VAL_INT; + + return IIO_AVAIL_LIST; + + case IIO_CHAN_INFO_SCALE: + if (st->sw_mode_en) + ch =3D chan->address; + + cs =3D &st->chan_scales[ch]; + *vals =3D cs->scale_avail_show; + *length =3D cs->num_scales * 2; + *type =3D IIO_VAL_INT_PLUS_MICRO; + + return IIO_AVAIL_LIST; + } + return -EINVAL; +} + static const struct iio_buffer_setup_ops ad7606_buffer_ops =3D { .postenable =3D &ad7606_buffer_postenable, .predisable =3D &ad7606_buffer_predisable, @@ -524,11 +555,11 @@ static const struct iio_info ad7606_info_os_and_range= =3D { .validate_trigger =3D &ad7606_validate_trigger, }; =20 -static const struct iio_info ad7606_info_os_range_and_debug =3D { +static const struct iio_info ad7606_info_sw_mode =3D { .read_raw =3D &ad7606_read_raw, .write_raw =3D &ad7606_write_raw, + .read_avail =3D &ad7606_read_avail, .debugfs_reg_access =3D &ad7606_reg_access, - .attrs =3D &ad7606_attribute_group_os_and_range, .validate_trigger =3D &ad7606_validate_trigger, }; =20 @@ -554,7 +585,8 @@ static int ad7606_sw_mode_setup(struct iio_dev *indio_d= ev) { unsigned int num_channels =3D indio_dev->num_channels - 1; struct ad7606_state *st =3D iio_priv(indio_dev); - int ch; + unsigned int *scale_avail_show, num_scales_avail_show; + int ret, ch; =20 if (!st->bops->sw_mode_config) return 0; @@ -563,7 +595,7 @@ static int ad7606_sw_mode_setup(struct iio_dev *indio_d= ev) if (!st->sw_mode_en) return 0; =20 - indio_dev->info =3D &ad7606_info_os_range_and_debug; + indio_dev->info =3D &ad7606_info_sw_mode; =20 /* Scale of 0.076293 is only available in sw mode */ /* After reset, in software mode, =C2=B110 V is set by default */ @@ -575,7 +607,40 @@ static int ad7606_sw_mode_setup(struct iio_dev *indio_= dev) cs->range =3D 2; } =20 - return st->bops->sw_mode_config(indio_dev); + ret =3D st->bops->sw_mode_config(indio_dev); + if (ret) + return ret; + + num_scales_avail_show =3D 1; + + for (ch =3D 0; ch < num_channels; ch++) { + struct ad7606_chan_scale *cs; + int i; + + /* AD7606C supports different scales per channel */ + cs =3D &st->chan_scales[ch]; + + if (num_scales_avail_show =3D=3D 1 && ch > 0) { + cs->scale_avail_show =3D scale_avail_show; + continue; + } + + scale_avail_show =3D devm_kcalloc(st->dev, cs->num_scales * 2, + sizeof(*scale_avail_show), + GFP_KERNEL); + if (!scale_avail_show) + return -ENOMEM; + + /* Generate a scale_avail list for showing to userspace */ + for (i =3D 0; i < cs->num_scales; i++) { + scale_avail_show[i * 2] =3D 0; + scale_avail_show[i * 2 + 1] =3D cs->scale_avail[i]; + } + + cs->scale_avail_show =3D scale_avail_show; + } + + return 0; } =20 int ad7606_probe(struct device *dev, int irq, void __iomem *base_address, diff --git a/drivers/iio/adc/ad7606.h b/drivers/iio/adc/ad7606.h index afe6a4030e0e..d71a843a5de5 100644 --- a/drivers/iio/adc/ad7606.h +++ b/drivers/iio/adc/ad7606.h @@ -27,6 +27,29 @@ }, \ } =20 +#define AD7606_SW_CHANNEL(num, bits) { \ + .type =3D IIO_VOLTAGE, \ + .indexed =3D 1, \ + .channel =3D num, \ + .address =3D num, \ + .info_mask_separate =3D \ + BIT(IIO_CHAN_INFO_RAW) | \ + BIT(IIO_CHAN_INFO_SCALE), \ + .info_mask_separate_available =3D \ + BIT(IIO_CHAN_INFO_SCALE), \ + .info_mask_shared_by_all =3D \ + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ + .info_mask_shared_by_all_available =3D \ + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ + .scan_index =3D num, \ + .scan_type =3D { \ + .sign =3D 's', \ + .realbits =3D (bits), \ + .storagebits =3D (bits), \ + .endianness =3D IIO_CPU, \ + }, \ +} + #define AD7605_CHANNEL(num) \ AD760X_CHANNEL(num, BIT(IIO_CHAN_INFO_RAW), \ BIT(IIO_CHAN_INFO_SCALE), 0, 16) @@ -36,10 +59,6 @@ BIT(IIO_CHAN_INFO_SCALE), \ BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), bits) =20 -#define AD7606_SW_CHANNEL(num, bits) \ - AD760X_CHANNEL(num, BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),\ - 0, BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), bits) - #define AD7616_CHANNEL(num) AD7606_SW_CHANNEL(num, 16) =20 /** @@ -65,11 +84,14 @@ struct ad7606_chip_info { /** * struct ad7606_chan_scale - channel scale configuration * @scale_avail pointer to the array which stores the available scales + * @scale_avail_show a duplicate of 'scale_avail' which is readily formatt= ed + * such that it can be read via the 'read_avail' hook * @num_scales number of elements stored in the scale_avail array * @range voltage range selection, selects which scale to apply */ struct ad7606_chan_scale { const unsigned int *scale_avail; + const unsigned int *scale_avail_show; unsigned int num_scales; unsigned int range; }; --=20 2.46.0 From nobody Sun Feb 8 10:05:20 2026 Received: from mail-ed1-f45.google.com (mail-ed1-f45.google.com [209.85.208.45]) (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 99AE2158DCD for ; Mon, 19 Aug 2024 06:47:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724050070; cv=none; b=oiN+Xtr0irPWl1j+eN3lKOFkaGsHEF1DFBUVJ/1kEuvIUI54d+EHNseq5SXkS43y6NTsP+1wW34GG4FiBj4noAL45u8+tETiJqrdcPKm2jlBIPiEjpBimpIKUck3/JPWKlUwuux/k/mEUejtnT8ZPyhny0J7GFkbozG4O2FEP1I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724050070; c=relaxed/simple; bh=zjkhjxugrpPcOzPM7cJwvWgeaXdH9eHWxMEf0cBLd6I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=i/5Yz2Ui9Rauh7RcePC73YRs2p/fydRTjflXugUyersDDKN/lr4AFMitFEjoRGoUhecUEx57PDj018oMVI55Sy0vOjwfkHdlIw1lmsxptQT4djulK+2YySn2GIjWP1cUqPUcOTBjjbz/XGG6wi7NzYGsMiyTdXHXMPSCD9pBFMs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=ISYAykmD; arc=none smtp.client-ip=209.85.208.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="ISYAykmD" Received: by mail-ed1-f45.google.com with SMTP id 4fb4d7f45d1cf-5beccb39961so549226a12.3 for ; Sun, 18 Aug 2024 23:47:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1724050067; x=1724654867; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=N5HQOz+OPivxBeIw9/E/M5AYwS74pv+AP5N6QBsRsGE=; b=ISYAykmDCc8rGxGOCLKYBKK5MgppA90pFD7mXNotcWCXBdh55zz4w61UcpqN/zyptJ Z5TNk+PjuNqJClap8KJUd/PzDQ/LhzxlUpFMgG0ESfhXbDjPGTBODiSDltaOYnezgUZR 0qBiAx8hM50RgkRqdol8PWR1nEYe1R+uy75N/24BPEtvrV6JOPt0u/qaOACTv7dwqWGU evzclpuSveHqeMclH5uRFmaWnVcJD6FV0Y1Do88jaT7zQFM7Yc5cbWP7a/K2wWErEfwG DkWTbc++p2Q8wAUv9pm2TjHFKr9O7SRXOsl75SOras9LAfUx2vba0jxiTuOW4+LoxX50 JlSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724050067; x=1724654867; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=N5HQOz+OPivxBeIw9/E/M5AYwS74pv+AP5N6QBsRsGE=; b=cpDTBFfGuVIrRAy1cxwDWbqg1Frj9zXgS7lujckCXtDusP4P/OXnuWSVx5WMr/imZY fnhFouK4HUGy1nTzqMT5Z9emwwkjS3isn2jy5GeHedHHqaNf7PiKUpkDXZfNAfM7M4w0 H74RNyiPeqqfB/Fz9phnBYcX3x7a1EJ57kbkaTrwlQ9IQ371suOE8I5ngZMU1ZDeoE7y xkP5AdtKSiN+P29WaImG78oHNCfrd5zRJN8avwMJg4N8frStgh8qo/fXOfn3wwOeLA3q mykaLCqmg6ip9LubJ/ZFf8BZxGiz5sXLuetUAh+G9aVSIcjxjc0/TIAnwhJcg11axX3c UxDg== X-Forwarded-Encrypted: i=1; AJvYcCUHN3tYj+bx4wB4nlBNgyrxz6jHIve+2C6BYrsc1ZgMidFrUc0H1sOPUg6+lo+4MzPfdEDaLHMklTPKPnA=@vger.kernel.org X-Gm-Message-State: AOJu0YyVwwpBLnf82261l+uP74jkAidMxcKJGh6gDcw8lQaVzn05fDnv FvcvWeyTkx9sfn+JgFNJ2iy+ELME7X97u0Dlltgluo3Qoc+99jT21T6zjfWZVeY= X-Google-Smtp-Source: AGHT+IF8k63D6kdG9ePEGXSjcSrgL/j5g6ct6jHlHHPqECtDZSi8DlPJDH0Gb7qX2rmHvBmucGwJnA== X-Received: by 2002:a17:907:9717:b0:a83:70d0:7a1e with SMTP id a640c23a62f3a-a8392a6b37dmr419257866b.9.1724050066979; Sun, 18 Aug 2024 23:47:46 -0700 (PDT) Received: from neptune.lan ([188.27.128.229]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a83838c6bfcsm599069966b.28.2024.08.18.23.47.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Aug 2024 23:47:46 -0700 (PDT) From: Alexandru Ardelean To: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org Cc: jic23@kernel.org, krzk+dt@kernel.org, robh@kernel.org, lars@metafoo.de, michael.hennerich@analog.com, gstols@baylibre.com, Alexandru Ardelean Subject: [PATCH 6/7] dt-bindings: iio: adc: add adi,ad7606c-{16,18} compatible strings Date: Mon, 19 Aug 2024 09:47:16 +0300 Message-ID: <20240819064721.91494-7-aardelean@baylibre.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240819064721.91494-1-aardelean@baylibre.com> References: <20240819064721.91494-1-aardelean@baylibre.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The driver will support the AD7606C-16 and AD7606C-18. This change adds the compatible strings for these devices. The AD7606C-16,18 channels also support these (individually configurable) types of channels: - bipolar single-ended - unipolar single-ended - bipolar differential This DT adds support for 'channel@X' nodes' Signed-off-by: Alexandru Ardelean --- .../bindings/iio/adc/adi,ad7606.yaml | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/Documentation/devicetree/bindings/iio/adc/adi,ad7606.yaml b/Do= cumentation/devicetree/bindings/iio/adc/adi,ad7606.yaml index 69408cae3db9..f9e177de3f8c 100644 --- a/Documentation/devicetree/bindings/iio/adc/adi,ad7606.yaml +++ b/Documentation/devicetree/bindings/iio/adc/adi,ad7606.yaml @@ -14,6 +14,8 @@ description: | https://www.analog.com/media/en/technical-documentation/data-sheets/AD76= 05-4.pdf https://www.analog.com/media/en/technical-documentation/data-sheets/ad76= 06_7606-6_7606-4.pdf https://www.analog.com/media/en/technical-documentation/data-sheets/AD76= 06B.pdf + https://www.analog.com/media/en/technical-documentation/data-sheets/ad76= 06c-16.pdf + https://www.analog.com/media/en/technical-documentation/data-sheets/ad76= 06c-18.pdf https://www.analog.com/media/en/technical-documentation/data-sheets/AD76= 16.pdf =20 properties: @@ -24,6 +26,8 @@ properties: - adi,ad7606-6 - adi,ad7606-8 # Referred to as AD7606 (without -8) in the datasheet - adi,ad7606b + - adi,ad7606c-16 + - adi,ad7606c-18 - adi,ad7616 =20 reg: @@ -114,6 +118,30 @@ properties: assumed that the pins are hardwired to VDD. type: boolean =20 +patternProperties: + "^channel@([0-9a-f])$": + type: object + $ref: adc.yaml + unevaluatedProperties: false + + properties: + reg: + description: The channel number. + minimum: 0 + maximum: 7 + + diff-channel: + description: Channel is bipolar differential. + type: boolean + + bipolar: + description: | + Channel is bipolar single-ended. If 'diff-channel' is set, then + the value of this property will be ignored. + type: boolean + required: + - reg + required: - compatible - reg @@ -170,6 +198,21 @@ allOf: adi,conversion-start-gpios: maxItems: 1 =20 + - if: + not: + properties: + compatible: + contains: + enum: + - adi,ad7606c-16 + - adi,ad7606c-18 + then: + patternProperties: + "^channel@([0-9a-f])$": + properties: + diff-channels: false + bipolar: true + unevaluatedProperties: false =20 examples: @@ -202,4 +245,44 @@ examples: standby-gpios =3D <&gpio 24 GPIO_ACTIVE_LOW>; }; }; + - | + #include + #include + spi { + #address-cells =3D <1>; + #size-cells =3D <0>; + + adc@0 { + compatible =3D "adi,ad7606-8"; + reg =3D <0>; + spi-max-frequency =3D <1000000>; + spi-cpol; + spi-cpha; + + avcc-supply =3D <&adc_vref>; + vdrive-supply =3D <&vdd_supply>; + + interrupts =3D <25 IRQ_TYPE_EDGE_FALLING>; + interrupt-parent =3D <&gpio>; + + adi,conversion-start-gpios =3D <&gpio 17 GPIO_ACTIVE_HIGH>; + + adi,conversion-start-gpios =3D <&gpio 17 GPIO_ACTIVE_HIGH>; + reset-gpios =3D <&gpio 27 GPIO_ACTIVE_HIGH>; + adi,first-data-gpios =3D <&gpio 22 GPIO_ACTIVE_HIGH>; + standby-gpios =3D <&gpio 24 GPIO_ACTIVE_LOW>; + + adi,sw-mode; + + channel@1 { + reg =3D <1>; + diff-channel; + }; + + channel@3 { + reg =3D <3>; + bipolar; + }; + }; + }; ... --=20 2.46.0 From nobody Sun Feb 8 10:05:20 2026 Received: from mail-ed1-f50.google.com (mail-ed1-f50.google.com [209.85.208.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 3176015A4B3 for ; Mon, 19 Aug 2024 06:47:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724050074; cv=none; b=Zw1SMoVNDhmifeS+nmLdRDacW1XOidcah7UW2kByiiXJuTMjG5/qH7T2lOsoHmlsd7aN1dHB35ZHQ9WKPfOEYfad93qPHhz+x/IeKbYvJKo4o63AUe+XPVMi7Ke9EvrVAGV1Y+TeK+W8szLS+WW1SHGzFkrM7T7xvwu7emt5GTw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724050074; c=relaxed/simple; bh=XXVymd2d9ZQoYqV5x0X+F5zZ9r3Uty1aQWWKL0LW7kA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=rqIt5Zo4IwN1g5E1VEcON4BWEBCqgGKQegJTqndLXhUdLjkYayivzIMzQUfxH7WCr9K86KXCsRcWnJCbac5MdzfpJ5EwUhExiMAWa5tA07ZOQ9SnlQ87CKa1uY1Rn8PE38lbD9LQA0uxK63qFHJMQAf3Ifguy9yZuPtjB7kAic0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=OCe83cQ/; arc=none smtp.client-ip=209.85.208.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="OCe83cQ/" Received: by mail-ed1-f50.google.com with SMTP id 4fb4d7f45d1cf-5becb24e227so406046a12.2 for ; Sun, 18 Aug 2024 23:47:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1724050070; x=1724654870; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=b3S6X4VoAXj5qYcKOoiYF3CVxTxMLh1pVsU+4wLTCGQ=; b=OCe83cQ/gQE2MoBwpRpj4gjr38DPKUCcWEBlxVOCTe4erSikulCLjbHQyw0GoZETL+ 7Lo+b/2L/mbUtTzFdV7ujv40xJaW4fMr9JVvTkM1RvibuiSzlEBEqOqmPlMEBeK29RGw i24ujnJETCWPel2C+AH89UJ27yAQCqxHB4vwDnBbV9BTXNK2zSmvfzFJexkQsp3TRLhA 3gPLqeCMeuLuGoGDi0303utXG8clTc3pg0Nw2CA2oPkbmG5s8QIb/QKGbOsgY6ggC+z1 yCXSyzLFQ967V3rcbFabfoLlt5fAjaIw82yCh9RsJV2GwJxnKJe3LNSlqAxjb+aja3w6 aP5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724050070; x=1724654870; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=b3S6X4VoAXj5qYcKOoiYF3CVxTxMLh1pVsU+4wLTCGQ=; b=WErBimEeYuEGh5uvZCwn9xadgnk5GOsKqgaI6EQCd3EnVLb7ZYvbRMLhNPwuFn7Ftt gkYNzpeZ8uX0qpcMFd+g8Xm/mhRbVv7mu+CM9hnzVIQI13cuLEtn3TAs2ayTvca09Q7A LCujfoDfSquBNJrttWW/IkRufZII3QVMq4Vv7Aj54zQWH0/NbNWUPSwrDfCUrVx391Ah EgTsHBIxHTdivzgWNaNJ/PjH2MygEmqF+0EiDUoXxucKb7ICvg5GBmJtrNwM/zOvkGEh wVxlafIvRimkGgEJA+L/WI9EuZWipIlSxFpu3w5tpqk8iFZaNFJsDRucDNfwx5Tfpnkp VTlQ== X-Forwarded-Encrypted: i=1; AJvYcCVKVv+zC9spWoIXtzG4sprwnZnrtsDnGpB/E3ceFubnM+oatSnob+rA5A49B5DWOpHhQitQPBLwlFXbL6E=@vger.kernel.org X-Gm-Message-State: AOJu0Yw85CZlZArWoWn17ixrPI8aiQ16q8WRkPpwVGb5O5F5EONB/24/ tdmxj9M3/GejgaFfLbDsH5fQb1qx8FSqyDD+uSyD0hI+Y5FxLcl6UpZrGREOtAU= X-Google-Smtp-Source: AGHT+IH4qvrSGlYAiMNgzXG0J4/feXQUFfFIMFIWtWu8957Gg0iqKfR09LSzDv22PwR0i5eVkGC39Q== X-Received: by 2002:a17:906:6a1c:b0:a80:f63e:159b with SMTP id a640c23a62f3a-a83925633ddmr429621766b.0.1724050070309; Sun, 18 Aug 2024 23:47:50 -0700 (PDT) Received: from neptune.lan ([188.27.128.229]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a83838c6bfcsm599069966b.28.2024.08.18.23.47.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Aug 2024 23:47:49 -0700 (PDT) From: Alexandru Ardelean To: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org Cc: jic23@kernel.org, krzk+dt@kernel.org, robh@kernel.org, lars@metafoo.de, michael.hennerich@analog.com, gstols@baylibre.com, Alexandru Ardelean Subject: [PATCH 7/7] iio: adc: ad7606: add support for AD7606C-{16,18} parts Date: Mon, 19 Aug 2024 09:47:17 +0300 Message-ID: <20240819064721.91494-8-aardelean@baylibre.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240819064721.91494-1-aardelean@baylibre.com> References: <20240819064721.91494-1-aardelean@baylibre.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-Transfer-Encoding: quoted-printable The AD7606C-16 and AD7606C-18 are pretty similar with the AD7606B. The main difference between AD7606C-16 & AD7606C-18 is the precision in bits (16 vs 18). Because of that, some scales need to be defined for the 18-bit variants, as they need to be computed against 2**18 (vs 2**16 for the 16 bit-variants). Because the AD7606C-16,18 also supports bipolar & differential channels, for SW-mode, the default range of 10 V or =C2=B110V should be set at probe. On reset, the default range (in the registers) is set to value 0x3 which corresponds to '=C2=B110 V single-ended range', regardless of bipolar or differential configuration. Aside from the scale/ranges, the AD7606C-16 is similar to the AD7606B. And the AD7606C-18 variant offers 18-bit precision. The unfortunate effect of this 18-bit sample size, is that there is no simple/neat way to get the samples into a 32-bit array without having to do a home-brewed bit-buffer. The ADC must read all samples (from all 8 channels) in order to get the N-th sample (this could be reworked to do up-to-N-th sample for scan-direct= ). There doesn't seem to be any quick-trick to be usable to pad the samples up to at least 24 bits. Even the optional status-header is 8-bits, which would mean 26-bits of data per sample. That means that when using a simple SPI controller (which can usually read 8 bit multiples) a simple bit-buffer trick is required. Datasheet links: https://www.analog.com/media/en/technical-documentation/data-sheets/ad760= 6c-16.pdf https://www.analog.com/media/en/technical-documentation/data-sheets/ad760= 6c-18.pdf Signed-off-by: Alexandru Ardelean --- drivers/iio/adc/ad7606.c | 274 ++++++++++++++++++++++++++++++++--- drivers/iio/adc/ad7606.h | 17 ++- drivers/iio/adc/ad7606_spi.c | 94 ++++++++++++ 3 files changed, 357 insertions(+), 28 deletions(-) diff --git a/drivers/iio/adc/ad7606.c b/drivers/iio/adc/ad7606.c index 7533aab4b7c8..55faab321092 100644 --- a/drivers/iio/adc/ad7606.c +++ b/drivers/iio/adc/ad7606.c @@ -28,14 +28,44 @@ =20 #include "ad7606.h" =20 +typedef void (*ad7606c_chan_setup_cb_t)(struct ad7606_state *st, int ch, + bool bipolar, bool differential); + /* * Scales are computed as 5000/32768 and 10000/32768 respectively, * so that when applied to the raw values they provide mV values */ -static const unsigned int ad7606_scale_avail[2] =3D { +static const unsigned int ad7606_16bit_hw_scale_avail[2] =3D { 152588, 305176 }; =20 +static const unsigned int ad7606_18bit_hw_scale_avail[2] =3D { + 38147, 76294 +}; + +static const unsigned int ad7606c_16_scale_single_ended_unipolar_avail[3] = =3D { + 76294, 152588, 190735, +}; + +static const unsigned int ad7606c_16_scale_single_ended_bipolar_avail[5] = =3D { + 76294, 152588, 190735, 305176, 381470 +}; + +static const unsigned int ad7606c_16_scale_differential_bipolar_avail[4] = =3D { + 152588, 305176, 381470, 610352 +}; + +static const unsigned int ad7606c_18_scale_single_ended_unipolar_avail[3] = =3D { + 19073, 38147, 47684 +}; + +static const unsigned int ad7606c_18_scale_single_ended_bipolar_avail[5] = =3D { + 19073, 38147, 47684, 76294, 95367 +}; + +static const unsigned int ad7606c_18_scale_differential_bipolar_avail[4] = =3D { + 38147, 76294, 95367, 152588 +}; =20 static const unsigned int ad7616_sw_scale_avail[3] =3D { 76293, 152588, 305176 @@ -84,10 +114,18 @@ static int ad7606_reg_access(struct iio_dev *indio_dev, =20 static int ad7606_read_samples(struct ad7606_state *st) { + unsigned int storagebits =3D st->chip_info->channels[1].scan_type.storage= bits; unsigned int num =3D st->chip_info->num_channels - 1; - u16 *data =3D st->data; + u32 *data32 =3D st->data.d32; + u16 *data16 =3D st->data.d16; + void *data; int ret; =20 + if (storagebits > 16) + data =3D data32; + else + data =3D data16; + /* * The frstdata signal is set to high while and after reading the sample * of the first channel and low for all other channels. This can be used @@ -108,7 +146,10 @@ static int ad7606_read_samples(struct ad7606_state *st) return -EIO; } =20 - data++; + if (storagebits > 16) + data32++; + else + data16++; num--; } =20 @@ -128,7 +169,7 @@ static irqreturn_t ad7606_trigger_handler(int irq, void= *p) if (ret) goto error_ret; =20 - iio_push_to_buffers_with_timestamp(indio_dev, st->data, + iio_push_to_buffers_with_timestamp(indio_dev, st->data.d16, iio_get_time_ns(indio_dev)); error_ret: iio_trigger_notify_done(indio_dev->trig); @@ -142,6 +183,7 @@ static int ad7606_scan_direct(struct iio_dev *indio_dev= , unsigned int ch, int *val) { struct ad7606_state *st =3D iio_priv(indio_dev); + unsigned int storagebits =3D st->chip_info->channels[1].scan_type.storage= bits; int ret; =20 gpiod_set_value(st->gpio_convst, 1); @@ -153,8 +195,12 @@ static int ad7606_scan_direct(struct iio_dev *indio_de= v, unsigned int ch, } =20 ret =3D ad7606_read_samples(st); - if (ret =3D=3D 0) - *val =3D sign_extend32(st->data[ch], 15); + if (ret =3D=3D 0) { + if (storagebits > 16) + *val =3D sign_extend32(st->data.d32[ch], 17); + else + *val =3D sign_extend32(st->data.d16[ch], 15); + } =20 error_ret: gpiod_set_value(st->gpio_convst, 0); @@ -267,7 +313,7 @@ static int ad7606_write_raw(struct iio_dev *indio_dev, ch =3D chan->address; cs =3D &st->chan_scales[ch]; i =3D find_closest(val2, cs->scale_avail, cs->num_scales); - ret =3D st->write_scale(indio_dev, ch, i); + ret =3D st->write_scale(indio_dev, ch, i + cs->reg_offset); if (ret < 0) return ret; cs->range =3D i; @@ -350,6 +396,18 @@ static const struct iio_chan_spec ad7606_channels_16bi= t[] =3D { AD7606_CHANNEL(7, 16), }; =20 +static const struct iio_chan_spec ad7606_channels_18bit[] =3D { + IIO_CHAN_SOFT_TIMESTAMP(8), + AD7606_CHANNEL(0, 18), + AD7606_CHANNEL(1, 18), + AD7606_CHANNEL(2, 18), + AD7606_CHANNEL(3, 18), + AD7606_CHANNEL(4, 18), + AD7606_CHANNEL(5, 18), + AD7606_CHANNEL(6, 18), + AD7606_CHANNEL(7, 18), +}; + /* * The current assumption that this driver makes for AD7616, is that it's * working in Hardware Mode with Serial, Burst and Sequencer modes activat= ed. @@ -410,6 +468,18 @@ static const struct ad7606_chip_info ad7606_chip_info_= tbl[] =3D { .oversampling_avail =3D ad7606_oversampling_avail, .oversampling_num =3D ARRAY_SIZE(ad7606_oversampling_avail), }, + [ID_AD7606C_16] =3D { + .channels =3D ad7606_channels_16bit, + .num_channels =3D 9, + .oversampling_avail =3D ad7606_oversampling_avail, + .oversampling_num =3D ARRAY_SIZE(ad7606_oversampling_avail), + }, + [ID_AD7606C_18] =3D { + .channels =3D ad7606_channels_18bit, + .num_channels =3D 9, + .oversampling_avail =3D ad7606_oversampling_avail, + .oversampling_num =3D ARRAY_SIZE(ad7606_oversampling_avail), + }, [ID_AD7616] =3D { .channels =3D ad7616_channels, .num_channels =3D 17, @@ -563,6 +633,14 @@ static const struct iio_info ad7606_info_sw_mode =3D { .validate_trigger =3D &ad7606_validate_trigger, }; =20 +static const struct iio_info ad7606c_info_sw_mode =3D { + .read_raw =3D &ad7606_read_raw, + .write_raw =3D &ad7606_write_raw, + .read_avail =3D &ad7606_read_avail, + .debugfs_reg_access =3D &ad7606_reg_access, + .validate_trigger =3D &ad7606_validate_trigger, +}; + static const struct iio_info ad7606_info_os =3D { .read_raw =3D &ad7606_read_raw, .write_raw =3D &ad7606_write_raw, @@ -581,7 +659,136 @@ static const struct iio_trigger_ops ad7606_trigger_op= s =3D { .validate_device =3D iio_trigger_validate_own_device, }; =20 -static int ad7606_sw_mode_setup(struct iio_dev *indio_dev) +static void ad7606c_18_chan_setup(struct ad7606_state *st, int ch, + bool bipolar, bool differential) +{ + struct ad7606_chan_scale *cs =3D &st->chan_scales[ch]; + + if (differential) { + cs->scale_avail =3D + ad7606c_18_scale_differential_bipolar_avail; + cs->num_scales =3D + ARRAY_SIZE(ad7606c_18_scale_differential_bipolar_avail); + /* Bipolar differential ranges start at 8 (b1000) */ + cs->reg_offset =3D 8; + cs->range =3D 1; + } else if (bipolar) { + cs->scale_avail =3D + ad7606c_18_scale_single_ended_bipolar_avail; + cs->num_scales =3D + ARRAY_SIZE(ad7606c_18_scale_single_ended_bipolar_avail); + cs->range =3D 3; + } else { + cs->scale_avail =3D + ad7606c_18_scale_single_ended_unipolar_avail; + cs->num_scales =3D + ARRAY_SIZE(ad7606c_18_scale_single_ended_unipolar_avail); + /* Unipolar single-ended ranges start at 5 (b0101) */ + cs->reg_offset =3D 5; + cs->range =3D 1; + } +} + +static void ad7606c_16_chan_setup(struct ad7606_state *st, int ch, + bool bipolar, bool differential) +{ + struct ad7606_chan_scale *cs =3D &st->chan_scales[ch]; + + if (differential) { + cs->scale_avail =3D + ad7606c_16_scale_differential_bipolar_avail; + cs->num_scales =3D + ARRAY_SIZE(ad7606c_16_scale_differential_bipolar_avail); + /* Bipolar differential ranges start at 8 (b1000) */ + cs->reg_offset =3D 8; + cs->range =3D 1; + } else if (bipolar) { + cs->scale_avail =3D + ad7606c_16_scale_single_ended_bipolar_avail; + cs->num_scales =3D + ARRAY_SIZE(ad7606c_16_scale_single_ended_bipolar_avail); + cs->range =3D 3; + } else { + cs->scale_avail =3D + ad7606c_16_scale_single_ended_unipolar_avail; + cs->num_scales =3D + ARRAY_SIZE(ad7606c_16_scale_single_ended_unipolar_avail); + /* Unipolar single-ended ranges start at 5 (b0101) */ + cs->reg_offset =3D 5; + cs->range =3D 1; + } +} + +static int ad7606c_sw_mode_setup_channels(struct iio_dev *indio_dev, + ad7606c_chan_setup_cb_t chan_setup_cb) +{ + unsigned int num_channels =3D indio_dev->num_channels - 1; + struct ad7606_state *st =3D iio_priv(indio_dev); + bool chan_configured[AD760X_MAX_CHANNELS]; + struct device *dev =3D st->dev; + int ret; + u32 ch; + + /* We need to hook this first */ + ret =3D st->bops->sw_mode_config(indio_dev); + if (ret) + return ret; + + indio_dev->info =3D &ad7606c_info_sw_mode; + + memset(chan_configured, 0, sizeof(chan_configured)); + + device_for_each_child_node_scoped(dev, child) { + bool bipolar, differential; + + ret =3D fwnode_property_read_u32(child, "reg", &ch); + if (ret) + continue; + + if (ch >=3D num_channels) { + dev_warn(st->dev, + "Invalid channel number (ignoring): %d\n", ch); + continue; + } + + bipolar =3D fwnode_property_present(child, "bipolar"); + differential =3D fwnode_property_present(child, "diff-channel"); + + chan_setup_cb(st, ch, bipolar, differential); + chan_configured[ch] =3D true; + } + + /* Apply default configuration to unconfigured (via DT) channels */ + for (ch =3D 0; ch < num_channels; ch++) { + struct ad7606_chan_scale *cs; + unsigned int *scale_avail_show; + int i; + + if (!chan_configured[ch]) + chan_setup_cb(st, ch, false, false); + + /* AD7606C supports different scales per channel */ + cs =3D &st->chan_scales[ch]; + + scale_avail_show =3D devm_kcalloc(st->dev, cs->num_scales * 2, + sizeof(*scale_avail_show), + GFP_KERNEL); + if (!scale_avail_show) + return -ENOMEM; + + /* Generate a scale_avail list for showing to userspace */ + for (i =3D 0; i < cs->num_scales; i++) { + scale_avail_show[i * 2] =3D 0; + scale_avail_show[i * 2 + 1] =3D cs->scale_avail[i]; + } + + cs->scale_avail_show =3D scale_avail_show; + } + + return 0; +} + +static int ad7606_sw_mode_setup(struct iio_dev *indio_dev, unsigned int id) { unsigned int num_channels =3D indio_dev->num_channels - 1; struct ad7606_state *st =3D iio_priv(indio_dev); @@ -597,21 +804,33 @@ static int ad7606_sw_mode_setup(struct iio_dev *indio= _dev) =20 indio_dev->info =3D &ad7606_info_sw_mode; =20 - /* Scale of 0.076293 is only available in sw mode */ - /* After reset, in software mode, =C2=B110 V is set by default */ - for (ch =3D 0; ch < num_channels; ch++) { - struct ad7606_chan_scale *cs =3D &st->chan_scales[ch]; + switch (id) { + case ID_AD7606C_18: + num_scales_avail_show =3D num_channels; + ret =3D ad7606c_sw_mode_setup_channels(indio_dev, + ad7606c_18_chan_setup); + break; + case ID_AD7606C_16: + num_scales_avail_show =3D num_channels; + ret =3D ad7606c_sw_mode_setup_channels(indio_dev, + ad7606c_16_chan_setup); + break; + default: + num_scales_avail_show =3D 1; =20 - cs->scale_avail =3D ad7616_sw_scale_avail; - cs->num_scales =3D ARRAY_SIZE(ad7616_sw_scale_avail); - cs->range =3D 2; - } + /* Scale of 0.076293 is only available in sw mode */ + /* After reset, in software mode, =C2=B110 V is set by default */ + for (ch =3D 0; ch < num_channels; ch++) { + struct ad7606_chan_scale *cs =3D &st->chan_scales[ch]; =20 - ret =3D st->bops->sw_mode_config(indio_dev); - if (ret) - return ret; + cs->scale_avail =3D ad7616_sw_scale_avail; + cs->num_scales =3D ARRAY_SIZE(ad7616_sw_scale_avail); + cs->range =3D 2; + } =20 - num_scales_avail_show =3D 1; + ret =3D st->bops->sw_mode_config(indio_dev); + break; + } =20 for (ch =3D 0; ch < num_channels; ch++) { struct ad7606_chan_scale *cs; @@ -667,9 +886,16 @@ int ad7606_probe(struct device *dev, int irq, void __i= omem *base_address, st->oversampling =3D 1; =20 cs =3D &st->chan_scales[0]; - cs->range =3D 0; - cs->scale_avail =3D ad7606_scale_avail; - cs->num_scales =3D ARRAY_SIZE(ad7606_scale_avail); + switch (id) { + case ID_AD7606C_18: + cs->scale_avail =3D ad7606_18bit_hw_scale_avail; + cs->num_scales =3D ARRAY_SIZE(ad7606_18bit_hw_scale_avail); + break; + default: + cs->scale_avail =3D ad7606_16bit_hw_scale_avail; + cs->num_scales =3D ARRAY_SIZE(ad7606_16bit_hw_scale_avail); + break; + } =20 ret =3D devm_regulator_get_enable(dev, "avcc"); if (ret) @@ -718,7 +944,7 @@ int ad7606_probe(struct device *dev, int irq, void __io= mem *base_address, st->write_scale =3D ad7606_write_scale_hw; st->write_os =3D ad7606_write_os_hw; =20 - ret =3D ad7606_sw_mode_setup(indio_dev); + ret =3D ad7606_sw_mode_setup(indio_dev, id); if (ret) return ret; =20 diff --git a/drivers/iio/adc/ad7606.h b/drivers/iio/adc/ad7606.h index d71a843a5de5..fa9305923a72 100644 --- a/drivers/iio/adc/ad7606.h +++ b/drivers/iio/adc/ad7606.h @@ -22,7 +22,7 @@ .scan_type =3D { \ .sign =3D 's', \ .realbits =3D (bits), \ - .storagebits =3D (bits), \ + .storagebits =3D (bits) > 16 ? 32 : 16, \ .endianness =3D IIO_CPU, \ }, \ } @@ -45,7 +45,7 @@ .scan_type =3D { \ .sign =3D 's', \ .realbits =3D (bits), \ - .storagebits =3D (bits), \ + .storagebits =3D (bits) > 16 ? 32 : 16, \ .endianness =3D IIO_CPU, \ }, \ } @@ -88,12 +88,15 @@ struct ad7606_chip_info { * such that it can be read via the 'read_avail' hook * @num_scales number of elements stored in the scale_avail array * @range voltage range selection, selects which scale to apply + * @reg_offset offset for the register value, to be applied when + * writing the value of 'range' to the register value */ struct ad7606_chan_scale { const unsigned int *scale_avail; const unsigned int *scale_avail_show; unsigned int num_scales; unsigned int range; + unsigned int reg_offset; }; =20 /** @@ -150,9 +153,13 @@ struct ad7606_state { /* * DMA (thus cache coherency maintenance) may require the * transfer buffers to live in their own cache lines. - * 16 * 16-bit samples + 64-bit timestamp + * 16 * 16-bit samples + 64-bit timestamp - for AD7616 + * 8 * 32-bit samples + 64-bit timestamp - for AD7616C-18 (and similar) */ - unsigned short data[20] __aligned(IIO_DMA_MINALIGN); + union { + unsigned short d16[20]; + unsigned int d32[10]; + } data __aligned(IIO_DMA_MINALIGN); __be16 d16[2]; }; =20 @@ -191,6 +198,8 @@ enum ad7606_supported_device_ids { ID_AD7606_6, ID_AD7606_4, ID_AD7606B, + ID_AD7606C_16, + ID_AD7606C_18, ID_AD7616, }; =20 diff --git a/drivers/iio/adc/ad7606_spi.c b/drivers/iio/adc/ad7606_spi.c index dd0075c97c24..73a7b0007bf8 100644 --- a/drivers/iio/adc/ad7606_spi.c +++ b/drivers/iio/adc/ad7606_spi.c @@ -45,6 +45,8 @@ #define AD7606_RANGE_CH_ADDR(ch) (0x03 + ((ch) >> 1)) #define AD7606_OS_MODE 0x08 =20 +#define AD7606C_18_SAMPLE_MASK GENMASK(17, 0) + static const struct iio_chan_spec ad7616_sw_channels[] =3D { IIO_CHAN_SOFT_TIMESTAMP(16), AD7616_CHANNEL(0), @@ -77,6 +79,18 @@ static const struct iio_chan_spec ad7606b_sw_channels[] = =3D { AD7606_SW_CHANNEL(7, 16), }; =20 +static const struct iio_chan_spec ad7606c_18_sw_channels[] =3D { + IIO_CHAN_SOFT_TIMESTAMP(8), + AD7606_SW_CHANNEL(0, 18), + AD7606_SW_CHANNEL(1, 18), + AD7606_SW_CHANNEL(2, 18), + AD7606_SW_CHANNEL(3, 18), + AD7606_SW_CHANNEL(4, 18), + AD7606_SW_CHANNEL(5, 18), + AD7606_SW_CHANNEL(6, 18), + AD7606_SW_CHANNEL(7, 18), +}; + static const unsigned int ad7606B_oversampling_avail[9] =3D { 1, 2, 4, 8, 16, 32, 64, 128, 256 }; @@ -120,6 +134,56 @@ static int ad7606_spi_read_block(struct device *dev, return 0; } =20 +static int ad7606_spi_read_block18to32(struct device *dev, + int count, void *buf) +{ + struct spi_device *spi =3D to_spi_device(dev); + u32 i, bit_buffer, buf_size, bit_buf_size; + u32 *data =3D buf; + u8 *bdata =3D buf; + int j, ret; + + /** + * With the 18 bit ADC variants (here) is that we can't assume that all + * SPI controllers will pad 18-bit sequences into 32-bit arrays, + * so we need to do a bit of buffer magic here. + * Alternatively, we can have a variant of this function that works + * for SPI controllers that can pad 18-bit samples into 32-bit arrays. + */ + + /* Write 'count' bytes to the right, to not overwrite samples */ + bdata +=3D count; + + /* Read 24 bits only, as we'll only get samples of 18 bits each */ + buf_size =3D count * 3; + ret =3D spi_read(spi, bdata, buf_size); + if (ret < 0) { + dev_err(&spi->dev, "SPI read error\n"); + return ret; + } + + bit_buffer =3D 0; + bit_buf_size =3D 0; + for (j =3D 0, i =3D 0; i < buf_size; i++) { + u32 sample; + + bit_buffer =3D (bit_buffer << 8) | bdata[i]; + bit_buf_size +=3D 8; + + if (bit_buf_size < 18) + continue; + + bit_buf_size -=3D 18; + sample =3D (bit_buffer >> bit_buf_size) & AD7606C_18_SAMPLE_MASK; + data[j++] =3D sign_extend32(sample, 17); + + if (j =3D=3D count) + break; + } + + return 0; +} + static int ad7606_spi_reg_read(struct ad7606_state *st, unsigned int addr) { struct spi_device *spi =3D to_spi_device(st->dev); @@ -283,6 +347,19 @@ static int ad7606B_sw_mode_config(struct iio_dev *indi= o_dev) return 0; } =20 +static int ad7606c_18_sw_mode_config(struct iio_dev *indio_dev) +{ + int ret; + + ret =3D ad7606B_sw_mode_config(indio_dev); + if (ret) + return ret; + + indio_dev->channels =3D ad7606c_18_sw_channels; + + return 0; +} + static const struct ad7606_bus_ops ad7606_spi_bops =3D { .read_block =3D ad7606_spi_read_block, }; @@ -305,6 +382,15 @@ static const struct ad7606_bus_ops ad7606B_spi_bops = =3D { .sw_mode_config =3D ad7606B_sw_mode_config, }; =20 +static const struct ad7606_bus_ops ad7606c_18_spi_bops =3D { + .read_block =3D ad7606_spi_read_block18to32, + .reg_read =3D ad7606_spi_reg_read, + .reg_write =3D ad7606_spi_reg_write, + .write_mask =3D ad7606_spi_write_mask, + .rd_wr_cmd =3D ad7606B_spi_rd_wr_cmd, + .sw_mode_config =3D ad7606c_18_sw_mode_config, +}; + static int ad7606_spi_probe(struct spi_device *spi) { const struct spi_device_id *id =3D spi_get_device_id(spi); @@ -315,8 +401,12 @@ static int ad7606_spi_probe(struct spi_device *spi) bops =3D &ad7616_spi_bops; break; case ID_AD7606B: + case ID_AD7606C_16: bops =3D &ad7606B_spi_bops; break; + case ID_AD7606C_18: + bops =3D &ad7606c_18_spi_bops; + break; default: bops =3D &ad7606_spi_bops; break; @@ -333,6 +423,8 @@ static const struct spi_device_id ad7606_id_table[] =3D= { { "ad7606-6", ID_AD7606_6 }, { "ad7606-8", ID_AD7606_8 }, { "ad7606b", ID_AD7606B }, + { "ad7606c-16", ID_AD7606C_16 }, + { "ad7606c-18", ID_AD7606C_18 }, { "ad7616", ID_AD7616 }, {} }; @@ -344,6 +436,8 @@ static const struct of_device_id ad7606_of_match[] =3D { { .compatible =3D "adi,ad7606-6" }, { .compatible =3D "adi,ad7606-8" }, { .compatible =3D "adi,ad7606b" }, + { .compatible =3D "adi,ad7606c-16" }, + { .compatible =3D "adi,ad7606c-18" }, { .compatible =3D "adi,ad7616" }, { }, }; --=20 2.46.0