From patchwork Thu Sep 5 08:23:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Ardelean X-Patchwork-Id: 13791943 Received: from mail-ej1-f53.google.com (mail-ej1-f53.google.com [209.85.218.53]) (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 EDAB819006E for ; Thu, 5 Sep 2024 08:24:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725524655; cv=none; b=CbA2UD4disA9v8aewLzh55B0cjEsAXmSJ0JBUW4nnVyO9UGOJUWiKIITfb4EaXJEkfQTPGxzR9omBVizBe2UaIJgDvOjOedoJ35qdCw4P+9KXmaUjovNGiKQJteoLBgwlz+6AG9i4Ex+nOY+MuYHSp9weP0AFgZfOTMUiTVuuQ8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725524655; c=relaxed/simple; bh=m+JDUAFze61TKUmqgjZi4kJQBJPi7r7Nl0ZvyJY2pOo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aj5sIyEdBtOPYtP7JNEaCmQ2UHc1OS9Yj/78LDWFvKEAu0IgQuaO7s9A13bG71i/N1I04pQPgOiToy23LwlIrhQgLVDyIzFkeGWRzq0qDaEppdCNIZ2YD6trzFMmOxourCrKoX0za9LUTivea2iNwWCDh9jz7/U7l5ixvDJ+y78= 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=g3E33VzH; arc=none smtp.client-ip=209.85.218.53 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="g3E33VzH" Received: by mail-ej1-f53.google.com with SMTP id a640c23a62f3a-a7a81bd549eso56867766b.3 for ; Thu, 05 Sep 2024 01:24:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1725524651; x=1726129451; 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=zwFKX15TuB+lP2XuqgSDKuPw5N2dTeJWzE8YjtBx2Ck=; b=g3E33VzHzhbbD7jExnMOV2NP756Geve0GMk1Zty81+0+2H7Qgp4xjyAWmU2HAHEky0 Kv22J5k+I9GWjC3QYwwAEbyherzivdtDAlDjG5Yith082zQz4NVkdsStZ5FtqgyoMco/ Yaf62tLUYAAYZAMkrfBTNOf2JawiCK3NPWq1K5jOtJYg9wpdZVOClScXj82H/y1U6UJG FQFXKhqY7TCe884tUeq3mgADmiuO3Rc26zTm2zC//3YTCe09UAO8+VeVgrkjZkgQ3Ulk ZCXNFvqrgVu59gZnz7JmTWWj6pXqlVEclzpUotXKah2JxL9ZWFF+HmX+2koY/MEcIGrz 4tlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725524651; x=1726129451; 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=zwFKX15TuB+lP2XuqgSDKuPw5N2dTeJWzE8YjtBx2Ck=; b=ocJLHymZTnHGCcclAAhauJ/yQIZPqs9ZsQ/XiMP2SMDwA8xTQW7YdGGnQjn6fyjGbM BKJ2y6KuBBi1OYPiU/k6NGU/jHW1m8HdhJApL+dGD9wA3llPSFodrppms/6GdT8qJwGd XLvHh2h+Q+URn6JFkzXyuszGHLEY3DZz+TWBnJNmxm3G0WWU/TN0IEOqRa/GvJ53pJ+A evjCWqKu/SktfaefSSQyTVI3jloYVferiGgIrF0RaWo0nFzxeUjqi7KDXaTHIwMhqviN rGoJ/dn0EMXSXgPaVAtVUOSRwNAMhcoQT9JdDfbtyoiwDLOOg/ngYqqCJmgARZsuAmX3 3NzA== X-Gm-Message-State: AOJu0YwT2c+9vs4EJ14Wscam/ZmGxzA5xnN7kwFrK9eWZIEsbh+sh9Jc kBwgGj4V7lnNH3o0SMf2rUfgjCUDStrqfDH4QmoDkpEMvKt9zfSc3irvsu9e/2MikrYJ6Dguk4a W6YM= X-Google-Smtp-Source: AGHT+IFtitYuRZTUOW9czyZovn6Ss5VJ1XOnQFXqGurB7hkDMfVYYYtanDt3+TNDH8s7SXGorJPUjQ== X-Received: by 2002:a17:907:7ea3:b0:a86:82da:2c3c with SMTP id a640c23a62f3a-a8a1d32e2femr761128766b.40.1725524650265; Thu, 05 Sep 2024 01:24:10 -0700 (PDT) Received: from localhost.localdomain ([188.27.130.242]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a8a623a6c80sm102300366b.174.2024.09.05.01.24.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Sep 2024 01:24:09 -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 v4 1/8] iio: adc: ad7606: add 'bits' parameter to channels macros Date: Thu, 5 Sep 2024 11:23:55 +0300 Message-ID: <20240905082404.119022-2-aardelean@baylibre.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240905082404.119022-1-aardelean@baylibre.com> References: <20240905082404.119022-1-aardelean@baylibre.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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_SW_CHANNEL() macro is also introduced, as a short-hand for IIO channels in SW mode. 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[] = { AD7605_CHANNEL(3), }; -static const struct iio_chan_spec ad7606_channels[] = { +static const struct iio_chan_spec ad7606_channels_16bit[] = { 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), }; /* @@ -357,22 +357,22 @@ static const struct iio_chan_spec ad7606_channels[] = { */ static const struct iio_chan_spec ad7616_channels[] = { 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), }; static const struct ad7606_chip_info ad7606_chip_info_tbl[] = { @@ -382,25 +382,25 @@ static const struct ad7606_chip_info ad7606_chip_info_tbl[] = { .num_channels = 5, }, [ID_AD7606_8] = { - .channels = ad7606_channels, + .channels = ad7606_channels_16bit, .num_channels = 9, .oversampling_avail = ad7606_oversampling_avail, .oversampling_num = ARRAY_SIZE(ad7606_oversampling_avail), }, [ID_AD7606_6] = { - .channels = ad7606_channels, + .channels = ad7606_channels_16bit, .num_channels = 7, .oversampling_avail = ad7606_oversampling_avail, .oversampling_num = ARRAY_SIZE(ad7606_oversampling_avail), }, [ID_AD7606_4] = { - .channels = ad7606_channels, + .channels = ad7606_channels_16bit, .num_channels = 5, .oversampling_avail = ad7606_oversampling_avail, .oversampling_num = ARRAY_SIZE(ad7606_oversampling_avail), }, [ID_AD7606B] = { - .channels = ad7606_channels, + .channels = ad7606_channels_16bit, .num_channels = 9, .oversampling_avail = ad7606_oversampling_avail, .oversampling_num = 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_ -#define AD760X_CHANNEL(num, mask_sep, mask_type, mask_all) { \ +#define AD760X_CHANNEL(num, mask_sep, mask_type, mask_all, bits) { \ .type = IIO_VOLTAGE, \ .indexed = 1, \ .channel = num, \ @@ -19,24 +19,26 @@ .scan_index = num, \ .scan_type = { \ .sign = 's', \ - .realbits = 16, \ - .storagebits = 16, \ + .realbits = (bits), \ + .storagebits = (bits), \ .endianness = IIO_CPU, \ }, \ } #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) -#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) -#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) /** * struct ad7606_chip_info - chip specific information diff --git a/drivers/iio/adc/ad7606_spi.c b/drivers/iio/adc/ad7606_spi.c index 62ec12195307..e00f58a6a0e9 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[] = { static const struct iio_chan_spec ad7606b_sw_channels[] = { 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), }; static const unsigned int ad7606B_oversampling_avail[9] = { From patchwork Thu Sep 5 08:23:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Ardelean X-Patchwork-Id: 13791944 Received: from mail-ej1-f46.google.com (mail-ej1-f46.google.com [209.85.218.46]) (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 5A82B19342F for ; Thu, 5 Sep 2024 08:24:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725524656; cv=none; b=G8BfIfaBRCWWLbLuT0alYwmvKUzq5OhYwcjD+OmvJ4Jm8v4A0TNYz8pwI6nQt1rxOGbSk626JBuA+VkQ79FIBD/ZyOgKUjjKuQsxiVC7TLdt8olIWYs4VPubnut49MRkpQWYqmL5KCOvddfUpJstilWY/zjk96TLTQcWvvtgNCA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725524656; c=relaxed/simple; bh=IXqmhsQDt/RO+GElC8x8ywqYUr/3SWcgPZP7XLJrRH0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jcBwtHCNx6DrXM727KURqdiijx3v2GTDBSIieJVJAAu/IjBi+9wsiGY6xvvBvrk7P2HQRAuy47TRHpT9B6iz3SCGVs0x5l8J/ei6tOdTSXiCoW3Xet3VrxhEdmM06pUyYeBEF7/WCk1d7hfzlhEo1O/b7Um1dlJVT43NnqDU8Dk= 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=JwPGn4g8; arc=none smtp.client-ip=209.85.218.46 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="JwPGn4g8" Received: by mail-ej1-f46.google.com with SMTP id a640c23a62f3a-a8a7596b7dfso40778266b.0 for ; Thu, 05 Sep 2024 01:24:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1725524652; x=1726129452; 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=JwPGn4g8GeYDVKv4b9DldZ9DFTgVI+yaQ8IiL2Awq4e429XdqnSApH+1z+5Yl9yHBJ gNgSwD+tVZMvuwXgUePP55VFBuUyX8ZwpjWDGbh9+GKMjEVU5h9m45we+pQdMTtoVF6d YoJLlRq+fD1NslByFHgQ74eanGKPUeExnxrSX2o+i4KZX5Clh5ggKgnv6WB8U6jihynt dKzSGwf3pfcnUQpdEESR1WgUb1GwRI0H89yQH1/in3yiGPkbsov+6O9eSLmltuV7uqzj N6Ps3UuhSFyDcJe/6zQzwdbUyuUZcO8wA4amCJISJPcBj4QoVFuwc+YI4SiIb8aZG9K+ wf4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725524652; x=1726129452; 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=GsZw9PiyfFRIQVhOHX2Dk4v6NqIO9L5KlgYIphzPtaWxG5RiNpSvol1ZaclbKo4NDK 8UAAGXQU+DxhmI6Bz7ONiOEhv79u4f1zlgDN5DIC0EczZlf+0Pq12ROJzYecrWfjNSIQ FZ7k8F75Anvucs61r4HXfh9kKAvYWlVyru9sDDuM0y7z8jp7wBlba6vzPg4JV7AH1EOj 94mtJTyNmP+ssZua/4bmJzF7Wg8iKAesXNIgJlvq85eFvAW2uMqHyKrOYU5wF0i6b74B 3p/zAiVkuaKgDIvRwgy3dY7NY3CJrexe9Q/h60K77OH5iEHQlXOyTRkLplVHvq0kuVt/ ZqYw== X-Gm-Message-State: AOJu0Yzi7Mp2PTNez9baT7j0m9KC6ob32ZAJOKOgUZNq9Fy8YVdIvvVj aA+9+F3dlW786w5opE5jPwS2pCjcrWMSFn8Z+9WLVl8PZ9SGTo5eGtM4Qw5MT49SGIk94wxZIhs FRl8= X-Google-Smtp-Source: AGHT+IEg9smD3/79iAKydqGIqzyJ9DE03yBVXkcC3TkTHttLotdZFlY5siEzBv07VVQlDR9b14GcJg== X-Received: by 2002:a17:907:c1e:b0:a86:a30f:4b00 with SMTP id a640c23a62f3a-a8a43209886mr500823866b.27.1725524652223; Thu, 05 Sep 2024 01:24:12 -0700 (PDT) Received: from localhost.localdomain ([188.27.130.242]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a8a623a6c80sm102300366b.174.2024.09.05.01.24.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Sep 2024 01:24:11 -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 v4 2/8] iio: adc: ad7606: move 'val' pointer to ad7606_scan_direct() Date: Thu, 5 Sep 2024 11:23:56 +0300 Message-ID: <20240905082404.119022-3-aardelean@baylibre.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240905082404.119022-1-aardelean@baylibre.com> References: <20240905082404.119022-1-aardelean@baylibre.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 Reviewed-by: David Lechner --- 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; } -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 = iio_priv(indio_dev); int ret; @@ -153,7 +154,7 @@ static int ad7606_scan_direct(struct iio_dev *indio_dev, unsigned int ch) ret = ad7606_read_samples(st); if (ret == 0) - ret = st->data[ch]; + *val = sign_extend32(st->data[ch], 15); 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 = ad7606_scan_direct(indio_dev, chan->address); + ret = ad7606_scan_direct(indio_dev, chan->address, val); if (ret < 0) return ret; - *val = (short) ret; return IIO_VAL_INT; } unreachable(); From patchwork Thu Sep 5 08:23:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alexandru Ardelean X-Patchwork-Id: 13791945 Received: from mail-ej1-f46.google.com (mail-ej1-f46.google.com [209.85.218.46]) (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 04700194A65 for ; Thu, 5 Sep 2024 08:24:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725524658; cv=none; b=fsmhv/imohfLEOLZuJKWMXw/Ob5CfXcXs4U2+gkTvGf7sdGg41rjz2BKcTllDyU1KcorupyUQ/ig+hCv8WBtW7X22tdnzWPqA+ewP02FmVrP70FWVSNcfV6Xz4IRV3l0G6pITeuSokn6WuSqC9vMayadR4oAb4Wx5OIg4/UBk00= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725524658; c=relaxed/simple; bh=9wWTe2RhHwvr6CPLWLyUmFQssffTJWZ48+UUgVNgMfA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=a7soazxpbyvVs4Zz077nBcKcVO1M8dULnI17cE+nupdPdGPz9xxD2ICk/2otTlgrZBkBzhOVi59AO47gfBvUM2hwFL4KXlM2M9Dd6wpzRZ/q/cPCqG8cD9TB0czISsK5K30wertk7gROHbbol7casMhm4w6FcHD/U0xF1wYwztM= 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=vR0KxDxd; arc=none smtp.client-ip=209.85.218.46 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="vR0KxDxd" Received: by mail-ej1-f46.google.com with SMTP id a640c23a62f3a-a80eab3945eso60537766b.1 for ; Thu, 05 Sep 2024 01:24:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1725524654; x=1726129454; 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=vR0KxDxdQ77x0HcgiwxAV6eJwsx97UxYlez2UKMSNBEIoQTZU6sq/t0u4cX/uVdr23 Dz2pg2B5opjkyW5cvvv4SBuw3QtVo7eWu3v9npQN492SzUf/WkJudObmBxAc3oyPiOJ1 /FNSZHr81CoTKsGIWRNzRTY7qxj35lNyZbKaLGejx/rx3k+Uko/zIxfRW/38RAImye/p MSbysA3Glsn47VUJ9tPvD9/y55X+r3IcsWow2YcNVZ828T/O/0+fENT4Ag4slh/KKMve evst4mE8EmsvVD+1Sj0ALwGPSht0iJdLNw0s763Aml4kEaTJd2lab//babVn5clbiCyl VcVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725524654; x=1726129454; 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=Tcu4axgQ4Z6iS0qid6xM748kxaEV8/BVbZmNWVmNrTKwLaU3rfP3RDJ+vm3ZiquA9i UVesMX4xwv2UOAcGjYBGRrTiZAhdq2MA6L7pqIxPxLxWYHCnwrNuVlTldVS644ac7DOV hLHGZxDda9fky4Pri9T8QNLf3JLnD6ziyGqAR7HD6RlfTDU+9U+GwdN5yyxwBUlz0sOt 8apDxjPRe6/N+m68ZF3sV7jdyNxttL9eeAtLA0NVVPjUWuZy6m01RLPsYDc7VNS9sgYR iyqMF/ECKsMa/LktanP+WiBrkC8vp8M6ye1REn9+ELF3GStjST/hsGyuNQHyJ3GxT1x+ ez8Q== X-Gm-Message-State: AOJu0YwOYuBHZdOqN0HtXN4flbJTL5AGEyVWhcOTinZZni8ZS1DcoF8b Y5Yul20LvTRBa9m7Co/oBiXQHGETOI8GwyHadg7nbhFlooIgeA80KYYibcQv0evwLeI2QvVNU7M 4eLQ= X-Google-Smtp-Source: AGHT+IGysHzpaI/D+IHLYDFtgbkl4iiw0+CnPgXwvIIR+LDXrAkqivGsmmQIUYkaxNsMKt0GYadSBA== X-Received: by 2002:a17:907:72d6:b0:a80:788e:176c with SMTP id a640c23a62f3a-a89a35ceef6mr1707691766b.23.1725524653974; Thu, 05 Sep 2024 01:24:13 -0700 (PDT) Received: from localhost.localdomain ([188.27.130.242]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a8a623a6c80sm102300366b.174.2024.09.05.01.24.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Sep 2024 01:24:13 -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 v4 3/8] iio: adc: ad7606: split a 'ad7606_sw_mode_setup()' from probe Date: Thu, 5 Sep 2024 11:23:57 +0300 Message-ID: <20240905082404.119022-4-aardelean@baylibre.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240905082404.119022-1-aardelean@baylibre.com> References: <20240905082404.119022-1-aardelean@baylibre.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 = { .validate_device = iio_trigger_validate_own_device, }; +static int ad7606_sw_mode_setup(struct iio_dev *indio_dev) +{ + struct ad7606_state *st = iio_priv(indio_dev); + + if (!st->bops->sw_mode_config) + return 0; + + st->sw_mode_en = device_property_present(st->dev, "adi,sw-mode"); + if (!st->sw_mode_en) + return 0; + + indio_dev->info = &ad7606_info_os_range_and_debug; + + /* Scale of 0.076293 is only available in sw mode */ + st->scale_avail = ad7616_sw_scale_avail; + st->num_scales = ARRAY_SIZE(ad7616_sw_scale_avail); + + /* After reset, in software mode, ±10 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 __iomem *base_address, st->write_scale = ad7606_write_scale_hw; st->write_os = ad7606_write_os_hw; - if (st->bops->sw_mode_config) - st->sw_mode_en = 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 = ad7616_sw_scale_avail; - st->num_scales = ARRAY_SIZE(ad7616_sw_scale_avail); - - /* After reset, in software mode, ±10 V is set by default */ - memset32(st->range, 2, ARRAY_SIZE(st->range)); - indio_dev->info = &ad7606_info_os_range_and_debug; - - ret = st->bops->sw_mode_config(indio_dev); - if (ret < 0) - return ret; - } + ret = ad7606_sw_mode_setup(indio_dev); + if (ret) + return ret; st->trig = devm_iio_trigger_alloc(dev, "%s-dev%d", indio_dev->name, From patchwork Thu Sep 5 08:23:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alexandru Ardelean X-Patchwork-Id: 13791946 Received: from mail-ej1-f41.google.com (mail-ej1-f41.google.com [209.85.218.41]) (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 D80D51953A2 for ; Thu, 5 Sep 2024 08:24:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725524659; cv=none; b=LdF8KT57JrgdoDNlv5Be8FNRPKeoyd8DBJc1c7zPdmraD+ZTFBgn6vAh58KHNWy0UirJ+vWYpGLfX7iVbaSFQUTk6zoFM1ToMEmaDl2kg2xRd1yxMoqapz7lk7/de2JQL1SSI1933V5l48a88U27SMRGaK0IbAmPDInFOliwj+A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725524659; c=relaxed/simple; bh=ZK9Z1/Q4filbKX1kKWRghKSgUBwXpAJI26eeyznAt94=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=qNp98EPKdGEGuPj8hbXFBnwVw0O3+XCnenX+QkJlsQU+qqRRxiYkixYTsYmPDQ/h3BlUmYXGxfoddHr7cJ+4Ovh8j/kmHSPUFs1rfSc+ljhW5OqrW4d3wCtEby0S5N863WKqOpALKFJwHzimBauRMaEIkn2Vz2T+vkjUd1kfwV0= 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=mlozzLxg; arc=none smtp.client-ip=209.85.218.41 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="mlozzLxg" Received: by mail-ej1-f41.google.com with SMTP id a640c23a62f3a-a8684c31c60so78369466b.3 for ; Thu, 05 Sep 2024 01:24:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1725524656; x=1726129456; 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=mlozzLxg/WO/yqOo9AkgO7x+HZy2vJHBqZ9sX2gyfjWiUlIzFVQkXinqXGSLomQ8IH Jt9CBKa8S1W8v4HhO+2Oc/Fq6EPiR4JKS+3ix4D532ege8jZYEbY2LRaiRwo4O0ax8Qe iUDEUAeYxS/oBg4eRwDp6tnIBBy9ytjM/cI+3vgUyKKh//NCNWvNytiji3Lj0aTQpq7+ 5QJCdCWRrot5KVbOReyU6KbtjA5SNPfVsdk4tQekdYCiIkzLByB2atMWpZy7aAfCfKTx 5nGk6CpABlLE2512WvOR0fL+yxdVmpw+rh6kUbWo/7HKdFxa6Bo2K8AEzlD21lLfUWUo IRHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725524656; x=1726129456; 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=mKhnc6u3+imRvvE+oJFm1UB8BlSffeSRaf8zRMPyL7CnozgjE6VzjY6PDr/yI5pPkX P6XH2FFY3wKbaGub90xG/A8RuVNqAqYBxiqeMzS/K99f8m9YDIlyLcAN2ZQnbQUybD5F KCN9OfoCI9X7CG7YYxqN6cjanphP2TmHnqMEWFbYwzw7bKnMXBsIqgvBza+KVki0ijRI RGOplXdPOkcAdhI+yID/pWnlwDQZ2yhO0spgMJ9IzxL1KuCSze+InYB1WNwtUrME6f7W mi5BwfKyJmPOx43WBBlTd3gNk/EzHMcjqLpMbSeUE8X/X2Oo0+HqK3glCxUgduFWl8h6 M9xQ== X-Gm-Message-State: AOJu0YwdQPyD9hlrXzf/Uw65Lz00wKlCIeFbsqkiyQlB55OAKevvk7hy KfOFOp9qdaEqVCuTHx/CxcXGnfUf/KakqNo5KyHIi2/iVssuQEbD3TMlEHPlg1KbDZZuh/FEMma lYCM= X-Google-Smtp-Source: AGHT+IHoCqOCL74ppbS/1Wf7oaHt9Td74TK/ayaJ7yQiB+XjDAXmmXLXT6yqIaQV9Ts+xV16haOFDw== X-Received: by 2002:a17:907:3d8a:b0:a86:a90f:3025 with SMTP id a640c23a62f3a-a897fad45cdmr1612932766b.59.1725524655831; Thu, 05 Sep 2024 01:24:15 -0700 (PDT) Received: from localhost.localdomain ([188.27.130.242]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a8a623a6c80sm102300366b.174.2024.09.05.01.24.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Sep 2024 01:24:15 -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 v4 4/8] iio: adc: ad7606: wrap channel ranges & scales into struct Date: Thu, 5 Sep 2024 11:23:58 +0300 Message-ID: <20240905082404.119022-5-aardelean@baylibre.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240905082404.119022-1-aardelean@baylibre.com> References: <20240905082404.119022-1-aardelean@baylibre.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 = 0; struct ad7606_state *st = iio_priv(indio_dev); + struct ad7606_chan_scale *cs; 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 = chan->address; + cs = &st->chan_scales[ch]; *val = 0; - *val2 = st->scale_avail[st->range[ch]]; + *val2 = cs->scale_avail[cs->range]; return IIO_VAL_INT_PLUS_MICRO; case IIO_CHAN_INFO_OVERSAMPLING_RATIO: *val = st->oversampling; @@ -214,8 +216,9 @@ static ssize_t in_voltage_scale_available_show(struct device *dev, { struct iio_dev *indio_dev = dev_to_iio_dev(dev); struct ad7606_state *st = iio_priv(indio_dev); + struct ad7606_chan_scale *cs = &st->chan_scales[0]; - return ad7606_show_avail(buf, st->scale_avail, st->num_scales, true); + return ad7606_show_avail(buf, cs->scale_avail, cs->num_scales, true); } 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 = iio_priv(indio_dev); + struct ad7606_chan_scale *cs; int i, ret, ch = 0; guard(mutex)(&st->lock); switch (mask) { case IIO_CHAN_INFO_SCALE: - i = find_closest(val2, st->scale_avail, st->num_scales); if (st->sw_mode_en) ch = chan->address; + cs = &st->chan_scales[ch]; + i = find_closest(val2, cs->scale_avail, cs->num_scales); ret = st->write_scale(indio_dev, ch, i); if (ret < 0) return ret; - st->range[ch] = i; + cs->range = i; return 0; case IIO_CHAN_INFO_OVERSAMPLING_RATIO: @@ -547,7 +552,9 @@ static const struct iio_trigger_ops ad7606_trigger_ops = { static int ad7606_sw_mode_setup(struct iio_dev *indio_dev) { + unsigned int num_channels = indio_dev->num_channels - 1; struct ad7606_state *st = iio_priv(indio_dev); + int ch; 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 = &ad7606_info_os_range_and_debug; /* Scale of 0.076293 is only available in sw mode */ - st->scale_avail = ad7616_sw_scale_avail; - st->num_scales = ARRAY_SIZE(ad7616_sw_scale_avail); - /* After reset, in software mode, ±10 V is set by default */ - memset32(st->range, 2, ARRAY_SIZE(st->range)); + for (ch = 0; ch < num_channels; ch++) { + struct ad7606_chan_scale *cs = &st->chan_scales[ch]; + + cs->scale_avail = ad7616_sw_scale_avail; + cs->num_scales = ARRAY_SIZE(ad7616_sw_scale_avail); + cs->range = 2; + } return st->bops->sw_mode_config(indio_dev); } @@ -572,6 +582,7 @@ int ad7606_probe(struct device *dev, int irq, void __iomem *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 = bops; st->base_address = base_address; /* tied to logic low, analog input range is +/- 5V */ - st->range[0] = 0; st->oversampling = 1; - st->scale_avail = ad7606_scale_avail; - st->num_scales = ARRAY_SIZE(ad7606_scale_avail); + + cs = &st->chan_scales[0]; + cs->range = 0; + cs->scale_avail = ad7606_scale_avail; + cs->num_scales = ARRAY_SIZE(ad7606_scale_avail); ret = devm_regulator_get_enable(dev, "avcc"); if (ret) @@ -698,7 +711,7 @@ static int ad7606_resume(struct device *dev) struct ad7606_state *st = iio_priv(indio_dev); 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_ +#define AD760X_MAX_CHANNELS 16 + #define AD760X_CHANNEL(num, mask_sep, mask_type, mask_all, bits) { \ .type = IIO_VOLTAGE, \ .indexed = 1, \ @@ -60,17 +62,27 @@ struct ad7606_chip_info { unsigned long init_delay_ms; }; +/** + * 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); From patchwork Thu Sep 5 08:23:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alexandru Ardelean X-Patchwork-Id: 13791947 Received: from mail-lj1-f178.google.com (mail-lj1-f178.google.com [209.85.208.178]) (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 D9CFF194139 for ; Thu, 5 Sep 2024 08:24:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725524662; cv=none; b=I67iSU9QdAc41a/BHIlVRp4oxHtwOA/GG7h9tW2VipLO0FOaeCODlZ3TUCE2ZdkBs5kQccoPR72beLLd9YetzqSbBStgMVPDi2gm7x28wNs9nPaGSZa2wLJd8lXtCQCnWsTvpLf6Y/x7/HO+SOc8KS7Cp8fRbf6RGHEFYQbfBls= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725524662; c=relaxed/simple; bh=nVbw190QbFvntpv2F3+c7kcWkCwggr81hIv8yKcYO0Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=MydZIoZFUyvQPChEHhKZPNQjokfcEw9hJCjAsIBiNwfEYws9e4vGPBTTFv4N+12Kuy2Ft3ESm7EdA+W2aUELDYtP0LcP+yvb2tn6BtgThplVUYiFtNa8kHwSlScKFZ+sXJBkPrR6GDubANPcqm+hGBmDyNQGXgUCsYpvu9rs/E8= 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=FSsfrIon; arc=none smtp.client-ip=209.85.208.178 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="FSsfrIon" Received: by mail-lj1-f178.google.com with SMTP id 38308e7fff4ca-2f74e468aa8so1291261fa.1 for ; Thu, 05 Sep 2024 01:24:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1725524658; x=1726129458; 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=VP40VYCR8trduWJE1JYza3M6qtnB5tUGujhHJKROTmg=; b=FSsfrIonIRx+lqW6+oxXa+/Jr4yI5a8yee8pp8M4Qp8xeI88CGL1zy67e4p74mNhWH JXTk6Fn8wQYzkwMXvv4Hv2xOMf8fX/8+3RP6ejavapNvseLWYO77FtQ8SrKxmDkKk8Z3 wukKn/YiOzzezCY8ymsjdhxrOCBjmAx1+H5e6xzTjCMEoVS8fy3e0XoSjIhTryBe2My1 qy9xiETuNqBhj01KgitmLo2SV4utSA/AbVOSg6kZPTUwPy7BHk+HbSbA/WCZTNV2uV9r 97kjHU+80JP3taxbLAXMKMDIEB/L0MSFSY9d2BtHuk3w1jfUz/i4E2iQCHCH6qvsmIi1 f6UA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725524658; x=1726129458; 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=VP40VYCR8trduWJE1JYza3M6qtnB5tUGujhHJKROTmg=; b=XyvPcky6Yah6FaFzvho+rgkVWU9aA66XKnGXG7J7ZCErJ7gEj6dG8ouRQOGBow4yG6 OdiWjwpNBW0W+uRluydIlg7Objm7LHFEPcSW36lARMFXBJ47WAFLq124BNIGC9MvnNy5 9bddifSJEgygYCsQ/KYJzPeKCbRDLvGFwAysdR40TNl7NznWdeojZI+2WWkPHFdDXZWu 0qbFAsjpI/96cDgUi8Gk3fPq6qVtgYLGsB7xC7goReNpyNF9BX8PmvC7KdYwhBwm4J7q +ZhxFkGm+3ZLOv3x3Q9BACFZ7f7Jmezfertm3D3WiKp+Zk7VMtpfr+cjNw4xFG2k2KbC dJEg== X-Gm-Message-State: AOJu0YyM4MK5TS1ip0VXjBHJdptk60QCOyGfFqgGkPiBzJWTzf7TaUus Oc2sEClSiidpOSJl8FK+hRG436uwbRBcjKBy+BvtUjiAs4ehVpRgeV1Twsu+r+Xg1zjm2Sh+UdX 3YJI= X-Google-Smtp-Source: AGHT+IHmi5jxxopFnmutzL8/6MWeq0rpUDvbCBpGqbzVbF0c/vORPSOhCkrOwQdegUJ8Za4qK584fw== X-Received: by 2002:a05:6512:33cb:b0:52c:f3fa:86c with SMTP id 2adb3069b0e04-53546b25743mr14493703e87.18.1725524657493; Thu, 05 Sep 2024 01:24:17 -0700 (PDT) Received: from localhost.localdomain ([188.27.130.242]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a8a623a6c80sm102300366b.174.2024.09.05.01.24.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Sep 2024 01:24:17 -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 v4 5/8] iio: adc: ad7606: rework available attributes for SW channels Date: Thu, 5 Sep 2024 11:23:59 +0300 Message-ID: <20240905082404.119022-6-aardelean@baylibre.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240905082404.119022-1-aardelean@baylibre.com> References: <20240905082404.119022-1-aardelean@baylibre.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 allocation will happen when then ad7606_state struct is allocated. The oversampling available parameters don't need any extra processing; they can just be passed back to userspace (as they are). Signed-off-by: Alexandru Ardelean --- drivers/iio/adc/ad7606.c | 63 ++++++++++++++++++++++++++++++++++++---- drivers/iio/adc/ad7606.h | 31 +++++++++++++++++--- 2 files changed, 85 insertions(+), 9 deletions(-) diff --git a/drivers/iio/adc/ad7606.c b/drivers/iio/adc/ad7606.c index 2554a4a4a9c0..4c3fbb28f790 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 *indio_dev) return 0; } +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 = iio_priv(indio_dev); + struct ad7606_chan_scale *cs; + unsigned int ch = 0; + + switch (info) { + case IIO_CHAN_INFO_OVERSAMPLING_RATIO: + *vals = st->oversampling_avail; + *length = st->num_os_ratios; + *type = IIO_VAL_INT; + + return IIO_AVAIL_LIST; + + case IIO_CHAN_INFO_SCALE: + if (st->sw_mode_en) + ch = chan->address; + + cs = &st->chan_scales[ch]; + *vals = cs->scale_avail_show; + *length = cs->num_scales * 2; + *type = IIO_VAL_INT_PLUS_MICRO; + + return IIO_AVAIL_LIST; + } + return -EINVAL; +} + static const struct iio_buffer_setup_ops ad7606_buffer_ops = { .postenable = &ad7606_buffer_postenable, .predisable = &ad7606_buffer_predisable, @@ -524,11 +555,11 @@ static const struct iio_info ad7606_info_os_and_range = { .validate_trigger = &ad7606_validate_trigger, }; -static const struct iio_info ad7606_info_os_range_and_debug = { +static const struct iio_info ad7606_info_sw_mode = { .read_raw = &ad7606_read_raw, .write_raw = &ad7606_write_raw, + .read_avail = &ad7606_read_avail, .debugfs_reg_access = &ad7606_reg_access, - .attrs = &ad7606_attribute_group_os_and_range, .validate_trigger = &ad7606_validate_trigger, }; @@ -554,7 +585,7 @@ static int ad7606_sw_mode_setup(struct iio_dev *indio_dev) { unsigned int num_channels = indio_dev->num_channels - 1; struct ad7606_state *st = iio_priv(indio_dev); - int ch; + int ret, ch; if (!st->bops->sw_mode_config) return 0; @@ -563,7 +594,7 @@ static int ad7606_sw_mode_setup(struct iio_dev *indio_dev) if (!st->sw_mode_en) return 0; - indio_dev->info = &ad7606_info_os_range_and_debug; + indio_dev->info = &ad7606_info_sw_mode; /* Scale of 0.076293 is only available in sw mode */ /* After reset, in software mode, ±10 V is set by default */ @@ -575,7 +606,29 @@ static int ad7606_sw_mode_setup(struct iio_dev *indio_dev) cs->range = 2; } - return st->bops->sw_mode_config(indio_dev); + ret = st->bops->sw_mode_config(indio_dev); + if (ret) + return ret; + + for (ch = 0; ch < num_channels; ch++) { + struct ad7606_chan_scale *cs = &st->chan_scales[ch]; + int i; + + cs = &st->chan_scales[ch]; + + if (cs->num_scales * 2 > AD760X_MAX_SCALE_SHOW) { + dev_err(st->dev, "Driver error: scale range too big"); + return -ERANGE; + } + + /* Generate a scale_avail list for showing to userspace */ + for (i = 0; i < cs->num_scales; i++) { + cs->scale_avail_show[i * 2] = 0; + cs->scale_avail_show[i * 2 + 1] = cs->scale_avail[i]; + } + } + + return 0; } 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..2113ad460c0f 100644 --- a/drivers/iio/adc/ad7606.h +++ b/drivers/iio/adc/ad7606.h @@ -27,6 +27,29 @@ }, \ } +#define AD7606_SW_CHANNEL(num, bits) { \ + .type = IIO_VOLTAGE, \ + .indexed = 1, \ + .channel = num, \ + .address = num, \ + .info_mask_separate = \ + BIT(IIO_CHAN_INFO_RAW) | \ + BIT(IIO_CHAN_INFO_SCALE), \ + .info_mask_separate_available = \ + BIT(IIO_CHAN_INFO_SCALE), \ + .info_mask_shared_by_all = \ + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ + .info_mask_shared_by_all_available = \ + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ + .scan_index = num, \ + .scan_type = { \ + .sign = 's', \ + .realbits = (bits), \ + .storagebits = (bits), \ + .endianness = 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) -#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) /** @@ -65,11 +84,15 @@ 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 formatted + * 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 { +#define AD760X_MAX_SCALE_SHOW (AD760X_MAX_CHANNELS * 2) const unsigned int *scale_avail; + int scale_avail_show[AD760X_MAX_SCALE_SHOW]; unsigned int num_scales; unsigned int range; }; From patchwork Thu Sep 5 08:24:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Ardelean X-Patchwork-Id: 13791948 Received: from mail-ej1-f46.google.com (mail-ej1-f46.google.com [209.85.218.46]) (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 0D6DA197A67 for ; Thu, 5 Sep 2024 08:24:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725524663; cv=none; b=nKaI1HjZizPirb4t+zlKIzvRjA/sLDwYXwnRcs3ENPGZjM3SZ6TUlPoMfJSQEWMs3k1BwAGq3ADdREv29uagGnIT0nweKaf+BFlD7vcHygYGnMzIc2/anGXL4V1fV7/DLjfa5T6DWVBOpC9YSEKj+poPGkIXaRHSpFu4rlW/rYY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725524663; c=relaxed/simple; bh=/ovmFlVD/7j+ipJY0VPXyzdNP1zhe/wqKiyGk4ajqgM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YRUCz1XJaGAiI++F8hAvtUWWc1VSmnKUN++ZcMsM9QC1aPgQhNotj1a4CMKRYO2snzcXcIj5baiFkxiOdciIk+Wvgrq6Mdyy49Et6AYP1UnOa1fEoOn93Cerbw0XCMd6OfZHgvU1UFKz6be9uTs00EEyGAd02CJhVw7THtafQis= 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=i6RoRD6E; arc=none smtp.client-ip=209.85.218.46 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="i6RoRD6E" Received: by mail-ej1-f46.google.com with SMTP id a640c23a62f3a-a86e9db75b9so74391066b.1 for ; Thu, 05 Sep 2024 01:24:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1725524660; x=1726129460; 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=g46xgvcRuzluvD9uFfVgOC6tB6SvIHIhrOwQLDjP3Ko=; b=i6RoRD6EATH5Lxqi4OewSSRiaGl6NNGWHB6hGKsBJ6jcAUqcRFDTCL3IJPJ9Ckv4LV dEk6OJx/odAMKCi7gUEYz1nssTvKre15RqTsrlhq5L3CN7pkkGeHDa2j+xltYfoyOyMw Z4x7WmMK69KJoT6u0wNi7QDTilMIx8I97RQGcWe2XqqhU1wiCkZgYGRn7aZwcze/An2c uJZT0QPW+xVhCGPMkIj8G4hBSuJQWQFhXRlOgiB7ma2qXqZORPDwzLMASKhk969YXeQW VXkCvLIBVvUs2TNMSE90UP1Zw54v4aSkIBHqJUGq3CHnajYOtgM5xeIfggmks2vfrUp9 /UyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725524660; x=1726129460; 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=g46xgvcRuzluvD9uFfVgOC6tB6SvIHIhrOwQLDjP3Ko=; b=NMfE0DABNhKO19XfwRBjRnWL7AduEDa9BnJ2m0IsFeqQ9uwHaNY20yRSw89dqnHSix RXBso2GskpK/bRWWLBA3ri1YbCDXYSLxJbHT7aZ3MFYcQQLlly2/fxuc1Oll+11owIAH 06js04GHC8Rm8eM+PPbRXVgQm+V4crVae+Nup3Pg1Wo5xRt0NT34waAAJj34IeES1tNJ aaDWdtt3ILmzLo9ImOd1eow+1hMplvGozaTHlzZV8CCrpmyEL766NAy70iw014p5AFxS 3vIin7QUAaEz5msuM5+uQ2dgMZMOj7Vv1T6aqlr9epzUNR9v9DVOfnzc+vszIB9H5aeB /EdA== X-Gm-Message-State: AOJu0Yyqzs/Jmn8Jl/t8d9OQwjlmpYHTiv+FVmYaE96BMZBDyxafeDnW EeBpoglysFz9W+TGGNBoSluhGqmUcYeXRNPqCSZLsYqWkcSnPnKq3YSFxP5QJ6IUEEJuToxWEYn MYr0= X-Google-Smtp-Source: AGHT+IEPcapgT5c8vyHV3LQwJ4MKYkMzKLUd0c6or4eTsc0LfeUFrkNG8rK61o5FzDWPYLQrgG6jYQ== X-Received: by 2002:a17:907:7206:b0:a86:a5a0:7613 with SMTP id a640c23a62f3a-a8a1d4c2db0mr918986766b.55.1725524659644; Thu, 05 Sep 2024 01:24:19 -0700 (PDT) Received: from localhost.localdomain ([188.27.130.242]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a8a623a6c80sm102300366b.174.2024.09.05.01.24.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Sep 2024 01:24:18 -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 , Krzysztof Kozlowski Subject: [PATCH v4 6/8] dt-bindings: iio: adc: document diff-channels corner case for some ADCs Date: Thu, 5 Sep 2024 11:24:00 +0300 Message-ID: <20240905082404.119022-7-aardelean@baylibre.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240905082404.119022-1-aardelean@baylibre.com> References: <20240905082404.119022-1-aardelean@baylibre.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Some ADCs have channels with negative and positive inputs, which can be used to measure differential voltage levels. These inputs/pins are dedicated (to the given channel) and cannot be muxed as with other ADCs. For those types of setups, the 'diff-channels' property can be specified to be used with the channel number (or reg property) for both negative and positive inputs/pins. Reviewed-by: Krzysztof Kozlowski Signed-off-by: Alexandru Ardelean --- Documentation/devicetree/bindings/iio/adc/adc.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Documentation/devicetree/bindings/iio/adc/adc.yaml b/Documentation/devicetree/bindings/iio/adc/adc.yaml index 8e7835cf36fd..b9bc02b5b07a 100644 --- a/Documentation/devicetree/bindings/iio/adc/adc.yaml +++ b/Documentation/devicetree/bindings/iio/adc/adc.yaml @@ -37,6 +37,10 @@ properties: to both the positive and negative inputs of a differential ADC. The first value specifies the positive input pin, the second specifies the negative input pin. + There are also some ADCs, where the differential channel has dedicated + positive and negative inputs which can be used to measure differential + voltage levels. For those setups, this property can be configured with + the 'reg' property for both inputs (i.e. diff-channels = ). single-channel: $ref: /schemas/types.yaml#/definitions/uint32 From patchwork Thu Sep 5 08:24:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Ardelean X-Patchwork-Id: 13791949 Received: from mail-ej1-f42.google.com (mail-ej1-f42.google.com [209.85.218.42]) (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 3E4C9198822 for ; Thu, 5 Sep 2024 08:24:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725524665; cv=none; b=UVNlmr7yo2t1WjsqJo0KxhR9lJaY2KoiaqZIZ4wbMlC2r3sAD+Ip5UhFA5Dz01HkZvqJot16sdVTrfpUI09swNdrlPokXJeo2y6q2YuBR3VW7+beKtt5bSM+tTNatvdHxFHbwtuDIh4T3crbVso7sRF1axGYiu0Y4B4I5k6nGWg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725524665; c=relaxed/simple; bh=8FC73oa/MEVkefisa/HgJYpYyiCAGwvKNzN6qaTlHSQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=q4h6JwVgVGhJmlKCJTQYz573o23UCKcCYT2aYmCV2LicXk3YnGXYZFuOuP5w7IfMVlq+ggTyn19cB8bVwS1rqkfMD8B0VDXD4x2vqRTSmlHI1dhNf56HJx1VBcv7/udT8PSV7cwO1HsGz08qJ0c2r1EDcgsn7ZJWqf+q66Gnxpg= 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=FgC2of+e; arc=none smtp.client-ip=209.85.218.42 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="FgC2of+e" Received: by mail-ej1-f42.google.com with SMTP id a640c23a62f3a-a8684c31c60so78379866b.3 for ; Thu, 05 Sep 2024 01:24:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1725524661; x=1726129461; 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=6E91591mSBml3TNwKKH/V0e8w+Xe8oeQd2rUoP5TY7c=; b=FgC2of+eXcyHWqeJ6B14BGgkFf4hdFQrGXVPtQEIObKR8/j8ahjBvQS3J5Uff1V9Yp uu+hJZ34Gfs2O6EP9dB1ajL8xEU1TJ8eVDHeoH9r9qo8TDa1y6dR2D0hD31WamkywiMD Y07VWlSEBf9S8asLORbcLcrZw/SybL9cPP05T6JkNzRqdqJbjsoBe0Uc0lRzPyAXdSom MMNWXJQ0+gWahap+m0hgTHH+8KjJe9eH5GyH+9MOXq01ICcTCAduHPDhspawBE0I3KLv T8dBcaoXqVw8gHrLAkm+syUepu5fVicePccYccgC52lPl/20sR9lb2uWGSxnheMZh9Je eEAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725524661; x=1726129461; 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=6E91591mSBml3TNwKKH/V0e8w+Xe8oeQd2rUoP5TY7c=; b=poEEnW944IMgjFN1ivxlw00HaXaUYNYY9nJ6vbBhpnlxBBKmxVHSUYPguiDjB33dVM nUQRnkbYEBRcV/eFNvA2mXNW1zCFTM2C5OBPpGMJJRYS/6gvPGZnix+EQE/smGH1aYf4 /XIi8ZyZgu8l1vwcQn+Qks0VjQ097x9HrysKRSyjY9TDXBNdzqSknQRT8v+nspNa/8rm vVO3dUOej+PXkyCkwA8WhT0OAGAApj5nchIHnjd3KUpthkJ9W+/r43IeLsMHMFYuRGoD lt51n0P5SJckJu9/hVn3ww+OAQC40BCi/jkM3k85/BJSM9G+sH8yKdsdPr0dAfXsLNfj 8mzA== X-Gm-Message-State: AOJu0YxYbyF5VDivroba6eMuNKXIZVUew82valHrPfDAfwJ/mZsi0pgd v3d4J1NQm7gAKD0SeH55q5UDBR3cO0EbYHgczrsN29XVrogUT6bKz4sxb8WuxhFBgzH3tIccPdo Mwco= X-Google-Smtp-Source: AGHT+IEue6KQB0cxF3glXoNKRtA7TVZTKr6ANWpW3YsE7FVumztajZG9HSuommv6910riT26/HM/MQ== X-Received: by 2002:a17:907:9725:b0:a88:b90a:ff30 with SMTP id a640c23a62f3a-a897fa74dbcmr1786881366b.50.1725524661282; Thu, 05 Sep 2024 01:24:21 -0700 (PDT) Received: from localhost.localdomain ([188.27.130.242]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a8a623a6c80sm102300366b.174.2024.09.05.01.24.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Sep 2024 01:24:20 -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 v4 7/8] dt-bindings: iio: adc: add docs for AD7606C-{16,18} parts Date: Thu, 5 Sep 2024 11:24:01 +0300 Message-ID: <20240905082404.119022-8-aardelean@baylibre.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240905082404.119022-1-aardelean@baylibre.com> References: <20240905082404.119022-1-aardelean@baylibre.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 Signed-off-by: Alexandru Ardelean Reviewed-by: Krzysztof Kozlowski --- .../bindings/iio/adc/adi,ad7606.yaml | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/Documentation/devicetree/bindings/iio/adc/adi,ad7606.yaml b/Documentation/devicetree/bindings/iio/adc/adi,ad7606.yaml index 69408cae3db9..57537ab0ec82 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/AD7605-4.pdf https://www.analog.com/media/en/technical-documentation/data-sheets/ad7606_7606-6_7606-4.pdf https://www.analog.com/media/en/technical-documentation/data-sheets/AD7606B.pdf + https://www.analog.com/media/en/technical-documentation/data-sheets/ad7606c-16.pdf + https://www.analog.com/media/en/technical-documentation/data-sheets/ad7606c-18.pdf https://www.analog.com/media/en/technical-documentation/data-sheets/AD7616.pdf properties: @@ -24,11 +26,19 @@ 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 reg: maxItems: 1 + "#address-cells": + const: 1 + + "#size-cells": + const: 0 + spi-cpha: true spi-cpol: true @@ -114,6 +124,46 @@ properties: assumed that the pins are hardwired to VDD. type: boolean +patternProperties: + "^channel@[1-8]$": + type: object + $ref: adc.yaml + unevaluatedProperties: false + + properties: + reg: + description: + The channel number, as specified in the datasheet (from 1 to 8). + minimum: 1 + maximum: 8 + + diff-channels: + description: + Each channel can be configured as a differential bipolar channel. + The ADC uses the same positive and negative inputs for this. + This property must be specified as 'reg' (or the channel number) for + both positive and negative inputs (i.e. diff-channels = ). + items: + minimum: 1 + maximum: 8 + + bipolar: + description: + Each channel can be configured as a unipolar or bipolar single-ended. + When this property is not specified, it's unipolar, so the ADC will + have only the positive input wired. + For this ADC the 'diff-channels' & 'bipolar' properties are mutually + exclusive. + + required: + - reg + + oneOf: + - required: + - diff-channels + - required: + - bipolar + required: - compatible - reg @@ -170,6 +220,17 @@ allOf: adi,conversion-start-gpios: maxItems: 1 + - if: + not: + properties: + compatible: + enum: + - adi,ad7606c-16 + - adi,ad7606c-18 + then: + patternProperties: + "^channel@[1-8]$": false + unevaluatedProperties: false examples: @@ -202,4 +263,52 @@ examples: standby-gpios = <&gpio 24 GPIO_ACTIVE_LOW>; }; }; + - | + #include + #include + spi { + #address-cells = <1>; + #size-cells = <0>; + + adc@0 { + compatible = "adi,ad7606c-18"; + reg = <0>; + + #address-cells = <1>; + #size-cells = <0>; + + spi-max-frequency = <1000000>; + spi-cpol; + spi-cpha; + + avcc-supply = <&adc_vref>; + vdrive-supply = <&vdd_supply>; + + interrupts = <25 IRQ_TYPE_EDGE_FALLING>; + interrupt-parent = <&gpio>; + + adi,conversion-start-gpios = <&gpio 17 GPIO_ACTIVE_HIGH>; + reset-gpios = <&gpio 27 GPIO_ACTIVE_HIGH>; + adi,first-data-gpios = <&gpio 22 GPIO_ACTIVE_HIGH>; + standby-gpios = <&gpio 24 GPIO_ACTIVE_LOW>; + + adi,sw-mode; + + channel@1 { + reg = <1>; + diff-channels = <1 1>; + }; + + channel@3 { + reg = <3>; + bipolar; + }; + + channel@8 { + reg = <8>; + diff-channels = <8 8>; + }; + + }; + }; ... From patchwork Thu Sep 5 08:24:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alexandru Ardelean X-Patchwork-Id: 13791950 Received: from mail-ej1-f45.google.com (mail-ej1-f45.google.com [209.85.218.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 3EA41198A10 for ; Thu, 5 Sep 2024 08:24:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725524667; cv=none; b=FW483r6h+6gFBbLRVMI8k1NizQLxXz91wd4essioUDTmpl0NOQIUxVhLxBXm9m22e4qqo4GOM8LEefpHjeNDPelSYekJaS7xL68qODjKTq4UqoL6sUTKpwPHjffKxMQ1kSbe7GyYqRXh0YdTRZEK3VOiieaNUFHEn5XMfxa1vZI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725524667; c=relaxed/simple; bh=FCEF3DQauLwbuelWiyzYnUIiAJ4YoV06Rw2ecOgc+g4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=uXInxzCsebiCkm/JNE2fbnVvXaNx+onOgUY93frinMyGt2riN+vpd8UU6MaXxc4Q6qcHN2ljenZO1i3HZ+lijD0Ot5WReghvjoaClMMhjkBQ9lJiuDJNErawXQkkQy5EtA+RC+ZXuxc0hlTI9m8n8WVmP9XYGL3MM/tretjjD+w= 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=tWf2gGID; arc=none smtp.client-ip=209.85.218.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="tWf2gGID" Received: by mail-ej1-f45.google.com with SMTP id a640c23a62f3a-a8692bbec79so74965666b.3 for ; Thu, 05 Sep 2024 01:24:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1725524663; x=1726129463; 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=lkYpWjHYFCY/21i91aWFXpsfKLY9KsXjMcsmRZHcUxA=; b=tWf2gGIDPbct2XKoT7opMVbMBo+huJus5UQqis9ASv+mJrKyG0ZobmWdy0qXeO0+ZG Rwm7c1HANNCSD2EX73E2aRZdDPSEE/guIHG5Mrc1EW91GFu9TKbCyxtkATsYjGDMSIjS sBvxoEGUbm8U5a1uCAvtPOgrEPt4dZ94vRdWayxVWfKfz5mkagORYPw46bCoD48IVQ7C B+pi1L/qIL3ajZ2o9y3H4S29oY5mOykQd+homlv1ZxV9bSVnp+m0MiXoywPNCjdk/++O GmFmiBnQg9Z2zEwUanwq99a5xV9YcRQdSf8yTob/rb3uRMkDK1n9ZwKWIcdQ7XI5zwIj kKFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725524663; x=1726129463; 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=lkYpWjHYFCY/21i91aWFXpsfKLY9KsXjMcsmRZHcUxA=; b=Zejel2iwMtwzCv7bQiz1ccam82UoSSiuEuA8+HHbGP7M2Fn2TgLP2qLfTkqWnpiqEU n5XghhCGatesFEzhEIt4Y8g2rLoqkgWWTtr0f/rG5A7T69ApiiJpFQKng/0MkC/Vl9uq fz7l8G9o/HF9ErpnDMywgxcP39nV6koSxT8vm11s/yhrDhv1efwPxhK5GMSyBeqgZg/S phfDoNnKwGarZBa2wJgedXlHHj8v5RLJmPyg5cjIpgHpxIh7Eh+gZdfm4R/sYl9FNLci q1hO0LrLzygsYMVWLc0swv3yFKnqkphNIhW6sxvdeUBhAgdVqWZ52vaZ2h8nJY/+KeAJ cveA== X-Gm-Message-State: AOJu0Yx+BhJi3bjHt1i4obnEcdxoDEDT6vo9/l7bazTfg4a2ERQrQRF0 dghuoOWHksPTqrU8UW6D8swQAqosS5P3H5je/dVSOFVBX71fNdS1MxXfVBL/OqARIsMK3jKmeuT jWDk= X-Google-Smtp-Source: AGHT+IHPqd9Oj1mm1dbsYxKnV5HJe5a4GQ+gK3S6UIn5nyBK5FuRBUpafx4HB9U3s5QEEIhNQV2aCQ== X-Received: by 2002:a17:907:7f04:b0:a8a:780f:4faf with SMTP id a640c23a62f3a-a8a780f5111mr48018566b.47.1725524663131; Thu, 05 Sep 2024 01:24:23 -0700 (PDT) Received: from localhost.localdomain ([188.27.130.242]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a8a623a6c80sm102300366b.174.2024.09.05.01.24.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Sep 2024 01:24:22 -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 v4 8/8] iio: adc: ad7606: add support for AD7606C-{16,18} parts Date: Thu, 5 Sep 2024 11:24:02 +0300 Message-ID: <20240905082404.119022-9-aardelean@baylibre.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240905082404.119022-1-aardelean@baylibre.com> References: <20240905082404.119022-1-aardelean@baylibre.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 ±10V should be set at probe. On reset, the default range (in the registers) is set to value 0x3 which corresponds to '±10 V single-ended range', regardless of bipolar or differential configuration. Aside from the scale/ranges, the AD7606C-16 is similar to the AD7606B. The AD7606C-18 variant offers 18-bit precision. Because of this, the requirement to use this chip is that the SPI controller supports padding of 18-bit sequences to 32-bit arrays. Datasheet links: https://www.analog.com/media/en/technical-documentation/data-sheets/ad7606c-16.pdf https://www.analog.com/media/en/technical-documentation/data-sheets/ad7606c-18.pdf Signed-off-by: Alexandru Ardelean --- drivers/iio/adc/ad7606.c | 266 +++++++++++++++++++++++++++++++---- drivers/iio/adc/ad7606.h | 17 ++- drivers/iio/adc/ad7606_spi.c | 55 ++++++++ 3 files changed, 309 insertions(+), 29 deletions(-) diff --git a/drivers/iio/adc/ad7606.c b/drivers/iio/adc/ad7606.c index 4c3fbb28f790..999c4411859e 100644 --- a/drivers/iio/adc/ad7606.c +++ b/drivers/iio/adc/ad7606.c @@ -28,14 +28,44 @@ #include "ad7606.h" +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] = { +static const unsigned int ad7606_16bit_hw_scale_avail[2] = { 152588, 305176 }; +static const unsigned int ad7606_18bit_hw_scale_avail[2] = { + 38147, 76294 +}; + +static const unsigned int ad7606c_16_scale_single_ended_unipolar_avail[3] = { + 76294, 152588, 190735, +}; + +static const unsigned int ad7606c_16_scale_single_ended_bipolar_avail[5] = { + 76294, 152588, 190735, 305176, 381470 +}; + +static const unsigned int ad7606c_16_scale_differential_bipolar_avail[4] = { + 152588, 305176, 381470, 610352 +}; + +static const unsigned int ad7606c_18_scale_single_ended_unipolar_avail[3] = { + 19073, 38147, 47684 +}; + +static const unsigned int ad7606c_18_scale_single_ended_bipolar_avail[5] = { + 19073, 38147, 47684, 76294, 95367 +}; + +static const unsigned int ad7606c_18_scale_differential_bipolar_avail[4] = { + 38147, 76294, 95367, 152588 +}; static const unsigned int ad7616_sw_scale_avail[3] = { 76293, 152588, 305176 @@ -82,11 +112,19 @@ static int ad7606_reg_access(struct iio_dev *indio_dev, } } -static int ad7606_read_samples(struct ad7606_state *st) +static int ad7606_read_samples(struct ad7606_state *st, bool sign_extend_samples) { + unsigned int storagebits = st->chip_info->channels[1].scan_type.storagebits; unsigned int num = st->chip_info->num_channels - 1; - u16 *data = st->data; - int ret; + u32 *data32 = st->data.d32; + u16 *data16 = st->data.d16; + void *data; + int i, ret; + + if (storagebits > 16) + data = data32; + else + data = data16; /* * The frstdata signal is set to high while and after reading the sample @@ -108,11 +146,25 @@ static int ad7606_read_samples(struct ad7606_state *st) return -EIO; } - data++; + if (storagebits > 16) + data32++; + else + data16++; num--; } - return st->bops->read_block(st->dev, num, data); + ret = st->bops->read_block(st->dev, num, data); + if (ret) + return ret; + + if (storagebits == 16 || !sign_extend_samples) + return 0; + + /* For 18 bit samples, we need to sign-extend samples to 32 bits */ + for (i = 0; i < num; i++) + data32[i] = sign_extend32(data32[i], 17); + + return 0; } static irqreturn_t ad7606_trigger_handler(int irq, void *p) @@ -124,11 +176,11 @@ static irqreturn_t ad7606_trigger_handler(int irq, void *p) guard(mutex)(&st->lock); - ret = ad7606_read_samples(st); + ret = ad7606_read_samples(st, true); if (ret) goto error_ret; - 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 +194,7 @@ static int ad7606_scan_direct(struct iio_dev *indio_dev, unsigned int ch, int *val) { struct ad7606_state *st = iio_priv(indio_dev); + unsigned int storagebits = st->chip_info->channels[1].scan_type.storagebits; int ret; gpiod_set_value(st->gpio_convst, 1); @@ -152,9 +205,13 @@ static int ad7606_scan_direct(struct iio_dev *indio_dev, unsigned int ch, goto error_ret; } - ret = ad7606_read_samples(st); - if (ret == 0) - *val = sign_extend32(st->data[ch], 15); + ret = ad7606_read_samples(st, false); + if (ret == 0) { + if (storagebits > 16) + *val = sign_extend32(st->data.d32[ch], 17); + else + *val = sign_extend32(st->data.d16[ch], 15); + } error_ret: gpiod_set_value(st->gpio_convst, 0); @@ -267,7 +324,7 @@ static int ad7606_write_raw(struct iio_dev *indio_dev, ch = chan->address; cs = &st->chan_scales[ch]; i = find_closest(val2, cs->scale_avail, cs->num_scales); - ret = st->write_scale(indio_dev, ch, i); + ret = st->write_scale(indio_dev, ch, i + cs->reg_offset); if (ret < 0) return ret; cs->range = i; @@ -350,6 +407,18 @@ static const struct iio_chan_spec ad7606_channels_16bit[] = { AD7606_CHANNEL(7, 16), }; +static const struct iio_chan_spec ad7606_channels_18bit[] = { + 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 activated. @@ -410,6 +479,18 @@ static const struct ad7606_chip_info ad7606_chip_info_tbl[] = { .oversampling_avail = ad7606_oversampling_avail, .oversampling_num = ARRAY_SIZE(ad7606_oversampling_avail), }, + [ID_AD7606C_16] = { + .channels = ad7606_channels_16bit, + .num_channels = 9, + .oversampling_avail = ad7606_oversampling_avail, + .oversampling_num = ARRAY_SIZE(ad7606_oversampling_avail), + }, + [ID_AD7606C_18] = { + .channels = ad7606_channels_18bit, + .num_channels = 9, + .oversampling_avail = ad7606_oversampling_avail, + .oversampling_num = ARRAY_SIZE(ad7606_oversampling_avail), + }, [ID_AD7616] = { .channels = ad7616_channels, .num_channels = 17, @@ -581,7 +662,122 @@ static const struct iio_trigger_ops ad7606_trigger_ops = { .validate_device = iio_trigger_validate_own_device, }; -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 = &st->chan_scales[ch]; + + if (differential) { + cs->scale_avail = + ad7606c_18_scale_differential_bipolar_avail; + cs->num_scales = + ARRAY_SIZE(ad7606c_18_scale_differential_bipolar_avail); + /* Bipolar differential ranges start at 8 (b1000) */ + cs->reg_offset = 8; + cs->range = 1; + } else if (bipolar) { + cs->scale_avail = + ad7606c_18_scale_single_ended_bipolar_avail; + cs->num_scales = + ARRAY_SIZE(ad7606c_18_scale_single_ended_bipolar_avail); + cs->range = 3; + } else { + cs->scale_avail = + ad7606c_18_scale_single_ended_unipolar_avail; + cs->num_scales = + ARRAY_SIZE(ad7606c_18_scale_single_ended_unipolar_avail); + /* Unipolar single-ended ranges start at 5 (b0101) */ + cs->reg_offset = 5; + cs->range = 1; + } +} + +static void ad7606c_16_chan_setup(struct ad7606_state *st, int ch, + bool bipolar, bool differential) +{ + struct ad7606_chan_scale *cs = &st->chan_scales[ch]; + + if (differential) { + cs->scale_avail = + ad7606c_16_scale_differential_bipolar_avail; + cs->num_scales = + ARRAY_SIZE(ad7606c_16_scale_differential_bipolar_avail); + /* Bipolar differential ranges start at 8 (b1000) */ + cs->reg_offset = 8; + cs->range = 1; + } else if (bipolar) { + cs->scale_avail = + ad7606c_16_scale_single_ended_bipolar_avail; + cs->num_scales = + ARRAY_SIZE(ad7606c_16_scale_single_ended_bipolar_avail); + cs->range = 3; + } else { + cs->scale_avail = + ad7606c_16_scale_single_ended_unipolar_avail; + cs->num_scales = + ARRAY_SIZE(ad7606c_16_scale_single_ended_unipolar_avail); + /* Unipolar single-ended ranges start at 5 (b0101) */ + cs->reg_offset = 5; + cs->range = 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 = indio_dev->num_channels - 1; + struct ad7606_state *st = iio_priv(indio_dev); + bool chan_configured[AD760X_MAX_CHANNELS] = {}; + struct device *dev = st->dev; + int ret; + u32 ch; + + /* We need to hook this first */ + ret = st->bops->sw_mode_config(indio_dev); + if (ret) + return ret; + + device_for_each_child_node_scoped(dev, child) { + bool bipolar, differential; + u32 pins[2]; + + ret = fwnode_property_read_u32(child, "reg", &ch); + if (ret) + continue; + + /* channel number (here) is from 1 to num_channels */ + if (ch == 0 || ch > num_channels) { + dev_warn(st->dev, + "Invalid channel number (ignoring): %d\n", ch); + continue; + } + + bipolar = fwnode_property_present(child, "bipolar"); + + ret = fwnode_property_read_u32_array(child, "diff-channels", + pins, ARRAY_SIZE(pins)); + /* Channel is differential, if pins are the same as 'reg' */ + if (ret == 0 && pins[0] == ch && pins[1] == ch) + differential = true; + else + differential = false; + + ch--; + + chan_setup_cb(st, ch, bipolar, differential); + chan_configured[ch] = true; + } + + /* Apply default configuration to unconfigured (via DT) channels */ + for (ch = 0; ch < num_channels; ch++) { + if (!chan_configured[ch]) + chan_setup_cb(st, ch, false, false); + } + + return 0; +} + +static int ad7606_sw_mode_setup(struct iio_dev *indio_dev, unsigned int id) { unsigned int num_channels = indio_dev->num_channels - 1; struct ad7606_state *st = iio_priv(indio_dev); @@ -596,17 +792,30 @@ static int ad7606_sw_mode_setup(struct iio_dev *indio_dev) indio_dev->info = &ad7606_info_sw_mode; - /* Scale of 0.076293 is only available in sw mode */ - /* After reset, in software mode, ±10 V is set by default */ - for (ch = 0; ch < num_channels; ch++) { - struct ad7606_chan_scale *cs = &st->chan_scales[ch]; + switch (id) { + case ID_AD7606C_18: + ret = ad7606c_sw_mode_setup_channels(indio_dev, + ad7606c_18_chan_setup); + break; + case ID_AD7606C_16: + ret = ad7606c_sw_mode_setup_channels(indio_dev, + ad7606c_16_chan_setup); + break; + default: + /* Scale of 0.076293 is only available in sw mode */ + /* After reset, in software mode, ±10 V is set by default */ + for (ch = 0; ch < num_channels; ch++) { + struct ad7606_chan_scale *cs = &st->chan_scales[ch]; + + cs->scale_avail = ad7616_sw_scale_avail; + cs->num_scales = ARRAY_SIZE(ad7616_sw_scale_avail); + cs->range = 2; + } - cs->scale_avail = ad7616_sw_scale_avail; - cs->num_scales = ARRAY_SIZE(ad7616_sw_scale_avail); - cs->range = 2; + ret = st->bops->sw_mode_config(indio_dev); + break; } - ret = st->bops->sw_mode_config(indio_dev); if (ret) return ret; @@ -655,9 +864,16 @@ int ad7606_probe(struct device *dev, int irq, void __iomem *base_address, st->oversampling = 1; cs = &st->chan_scales[0]; - cs->range = 0; - cs->scale_avail = ad7606_scale_avail; - cs->num_scales = ARRAY_SIZE(ad7606_scale_avail); + switch (id) { + case ID_AD7606C_18: + cs->scale_avail = ad7606_18bit_hw_scale_avail; + cs->num_scales = ARRAY_SIZE(ad7606_18bit_hw_scale_avail); + break; + default: + cs->scale_avail = ad7606_16bit_hw_scale_avail; + cs->num_scales = ARRAY_SIZE(ad7606_16bit_hw_scale_avail); + break; + } ret = devm_regulator_get_enable(dev, "avcc"); if (ret) @@ -706,7 +922,7 @@ int ad7606_probe(struct device *dev, int irq, void __iomem *base_address, st->write_scale = ad7606_write_scale_hw; st->write_os = ad7606_write_os_hw; - ret = ad7606_sw_mode_setup(indio_dev); + ret = ad7606_sw_mode_setup(indio_dev, id); if (ret) return ret; diff --git a/drivers/iio/adc/ad7606.h b/drivers/iio/adc/ad7606.h index 2113ad460c0f..6b0897aa2dc7 100644 --- a/drivers/iio/adc/ad7606.h +++ b/drivers/iio/adc/ad7606.h @@ -22,7 +22,7 @@ .scan_type = { \ .sign = 's', \ .realbits = (bits), \ - .storagebits = (bits), \ + .storagebits = (bits) > 16 ? 32 : 16, \ .endianness = IIO_CPU, \ }, \ } @@ -45,7 +45,7 @@ .scan_type = { \ .sign = 's', \ .realbits = (bits), \ - .storagebits = (bits), \ + .storagebits = (bits) > 16 ? 32 : 16, \ .endianness = IIO_CPU, \ }, \ } @@ -88,6 +88,8 @@ 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 { #define AD760X_MAX_SCALE_SHOW (AD760X_MAX_CHANNELS * 2) @@ -95,6 +97,7 @@ struct ad7606_chan_scale { int scale_avail_show[AD760X_MAX_SCALE_SHOW]; unsigned int num_scales; unsigned int range; + unsigned int reg_offset; }; /** @@ -151,9 +154,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]; }; @@ -192,6 +199,8 @@ enum ad7606_supported_device_ids { ID_AD7606_6, ID_AD7606_4, ID_AD7606B, + ID_AD7606C_16, + ID_AD7606C_18, ID_AD7616, }; diff --git a/drivers/iio/adc/ad7606_spi.c b/drivers/iio/adc/ad7606_spi.c index e00f58a6a0e9..b8d630ad156d 100644 --- a/drivers/iio/adc/ad7606_spi.c +++ b/drivers/iio/adc/ad7606_spi.c @@ -77,6 +77,18 @@ static const struct iio_chan_spec ad7606b_sw_channels[] = { AD7606_SW_CHANNEL(7, 16), }; +static const struct iio_chan_spec ad7606c_18_sw_channels[] = { + 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] = { 1, 2, 4, 8, 16, 32, 64, 128, 256 }; @@ -120,6 +132,19 @@ static int ad7606_spi_read_block(struct device *dev, return 0; } +static int ad7606_spi_read_block18to32(struct device *dev, + int count, void *buf) +{ + struct spi_device *spi = to_spi_device(dev); + struct spi_transfer xfer = { + .bits_per_word = 18, + .len = count, + .rx_buf = buf, + }; + + return spi_sync_transfer(spi, &xfer, 1); +} + static int ad7606_spi_reg_read(struct ad7606_state *st, unsigned int addr) { struct spi_device *spi = to_spi_device(st->dev); @@ -283,6 +308,19 @@ static int ad7606B_sw_mode_config(struct iio_dev *indio_dev) return 0; } +static int ad7606c_18_sw_mode_config(struct iio_dev *indio_dev) +{ + int ret; + + ret = ad7606B_sw_mode_config(indio_dev); + if (ret) + return ret; + + indio_dev->channels = ad7606c_18_sw_channels; + + return 0; +} + static const struct ad7606_bus_ops ad7606_spi_bops = { .read_block = ad7606_spi_read_block, }; @@ -305,6 +343,15 @@ static const struct ad7606_bus_ops ad7606B_spi_bops = { .sw_mode_config = ad7606B_sw_mode_config, }; +static const struct ad7606_bus_ops ad7606c_18_spi_bops = { + .read_block = ad7606_spi_read_block18to32, + .reg_read = ad7606_spi_reg_read, + .reg_write = ad7606_spi_reg_write, + .write_mask = ad7606_spi_write_mask, + .rd_wr_cmd = ad7606B_spi_rd_wr_cmd, + .sw_mode_config = ad7606c_18_sw_mode_config, +}; + static int ad7606_spi_probe(struct spi_device *spi) { const struct spi_device_id *id = spi_get_device_id(spi); @@ -315,8 +362,12 @@ static int ad7606_spi_probe(struct spi_device *spi) bops = &ad7616_spi_bops; break; case ID_AD7606B: + case ID_AD7606C_16: bops = &ad7606B_spi_bops; break; + case ID_AD7606C_18: + bops = &ad7606c_18_spi_bops; + break; default: bops = &ad7606_spi_bops; break; @@ -333,6 +384,8 @@ static const struct spi_device_id ad7606_id_table[] = { { "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 +397,8 @@ static const struct of_device_id ad7606_of_match[] = { { .compatible = "adi,ad7606-6" }, { .compatible = "adi,ad7606-8" }, { .compatible = "adi,ad7606b" }, + { .compatible = "adi,ad7606c-16" }, + { .compatible = "adi,ad7606c-18" }, { .compatible = "adi,ad7616" }, { } };