From patchwork Wed Nov 27 14:59:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= X-Patchwork-Id: 13887110 Received: from mail-wr1-f44.google.com (mail-wr1-f44.google.com [209.85.221.44]) (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 B05461FF7C4 for ; Wed, 27 Nov 2024 15:06:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732720015; cv=none; b=Qd/IzWnOmcWS94btyxoJQPdtjZWdZd8I0YxgocPR3ijTsidYrEhiLUB4a1WjmW24LUdPMr3e3+cUi7e50YYUlcZm1xq4be4G1LtX8e73dicrDizNzjqMlwqmwCfrkTrLJmsCd89mq4I0o/a0qweOUqDqgUJYuGiheui6ov+MoPs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732720015; c=relaxed/simple; bh=zn8zvV9Mo4O2rpc8Z3avU2Iyc7Z00tacRFBEl6WqSmQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=VUq21bLpMpscFEx4g6sG7rbuCx2GH4zdq+kQNLYQXWnpSwEnRitEOS1Z6TtR3NvNArjFRMK1HBu7WQ3Cjo0PRaMQ1fBY+pM8WHJNgptsG4rLKmP90TdhunYHUySO/sPIQeQuGmFsrOwv5hjiNMzV3nem5aDffTnxyW2OhEe/uuI= 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=XPWMgxrT; arc=none smtp.client-ip=209.85.221.44 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="XPWMgxrT" Received: by mail-wr1-f44.google.com with SMTP id ffacd0b85a97d-3823e45339bso4959168f8f.0 for ; Wed, 27 Nov 2024 07:06:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1732720011; x=1733324811; 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=EoPQYGtC/ppQEZ9aVkh2TLcv9C9qeXCGvY95zPdobik=; b=XPWMgxrTvJpRrHr7L5Ts9VTHkLIPkK9UZPL1NnWhNGtyx83l14Bfpg6vAW8deu6Th3 gW7ywCikWAT1ipmrnHRWW0I9gJTKo0UfIz5g7etJhUCKnjtUPzm7UcI3kqSyrDdBY/ki hxFf0RcF2530QYCp7HNFDayMLi+x/3fyfWlh2xIGsvfIL7tfYTYQOgjtDMjHkMpnSVG6 IHmwpCfDM0zcBMfVb70qkCytSAr8xO+P5GK8mCiDGDpNplzRVkBu8AvQAjP/haD8dyll V40POdkoen/EVhg5UwhewrfY39AVT7TW2dhw27EbEiYuhVQP8EfkiTUsPcnJ9hYBJLVU WXKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732720011; x=1733324811; 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=EoPQYGtC/ppQEZ9aVkh2TLcv9C9qeXCGvY95zPdobik=; b=rKG6+QdN4cjdL8qK6n6UiEX69lh9d8uEq55KZc4P5ieDhLv4N6C5S94N6PJlW/xmyt fq0D/LLPVSxDZWV7A6cpnrGh4o3P1KrfVc5Szcuaw5MKqmtZOnzFbuPAdnfwD8rzzXBy 8R7AigZ0dETjhyYa1rq5sFOxSbI79hvQOuYFRuq22LURwCZkARPF3p1MRWZ2ZsdlQOQ1 kMYHZEC/8JUOVtuz4GlV2MzBxJd1OhRP+QsydpqOuDQ/LGBcz7W1bKZSZwNvn3FEJuvh YdlqJOa6x0y4KaYdGDF+/yz6S/KfYddco11XgCQ9Bxx4T9urYhkZL9ECb+aiMecOthmp qBOg== X-Forwarded-Encrypted: i=1; AJvYcCWPU/C8tqd/ufq8cp/G7q5bgVUQfps8g7Mls7unGKf6uwx51tibXauYgHAu1QYW2eIkOknAcs7SQAs=@vger.kernel.org X-Gm-Message-State: AOJu0Yx+4mOSlKnpsYxtOtBOrPYfpS2ShZ8wYbkKC1Ox7MONhXe2FNv6 3tEc+yAkJlrcZBg6BJUoKrNUU9ei2QLwNgA56GCrN7aB5kVtomrv1C5E5G4BfhOFFXJKqwdI40n h5Zw= X-Gm-Gg: ASbGncsHAbVoD7Mj8Hd7SagZbYpUc6dS7258l+X470bHJCOB++JSpt0Ej4Trxw/kF46 lLtI2eoV5SNdw2LeczlucXghj/KD1L0Tf5g684lC29+9/kAa5WPj5DXhIzNwz+/QDNeOCt9Kt7y oG5+eccvx1RCYgV6O1Zlj1byRQcKlTSMyyjx46IgYpC5oazOl07lne3qykD6SjKh3oBMqqwEKrL tqIP4jw16oEpuTJioOugGuPg3btCy1gN3SDKPhXxF/rKVc4Rf4CodPEoq7Gb66VAj9DOyuGX0sD k5tC X-Google-Smtp-Source: AGHT+IEdVagKnN7ScxTCCHc6botfDUQwjo5t5vCUD3sywf9pEq2gwZDQdLE7YiLyra6lsD8bi8tdtw== X-Received: by 2002:a05:600c:5488:b0:434:a91e:c709 with SMTP id 5b1f17b1804b1-434a9e0c1e8mr24638375e9.28.1732719607272; Wed, 27 Nov 2024 07:00:07 -0800 (PST) Received: from localhost (p5dc6838f.dip0.t-ipconnect.de. [93.198.131.143]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-434aa74f1e6sm23943565e9.9.2024.11.27.07.00.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Nov 2024 07:00:06 -0800 (PST) From: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= To: Jonathan Cameron Cc: Lars-Peter Clausen , Michael Hennerich , linux-iio@vger.kernel.org, Andy Shevchenko , David Lechner , Trevor Gamblin , Nuno Sa Subject: [PATCH v4 01/10] iio: adc: ad7124: Don't create more channels than the driver can handle Date: Wed, 27 Nov 2024 15:59:30 +0100 Message-ID: <20241127145929.679408-13-u.kleine-koenig@baylibre.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241127145929.679408-12-u.kleine-koenig@baylibre.com> References: <20241127145929.679408-12-u.kleine-koenig@baylibre.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1454; i=u.kleine-koenig@baylibre.com; h=from:subject; bh=zn8zvV9Mo4O2rpc8Z3avU2Iyc7Z00tacRFBEl6WqSmQ=; b=owEBbQGS/pANAwAKAY+A+1h9Ev5OAcsmYgBnRzPUdZucAX2c0f4j3lwLFQfEx4Ynwm9Zsma60 WqCqNIV1u+JATMEAAEKAB0WIQQ/gaxpOnoeWYmt/tOPgPtYfRL+TgUCZ0cz1AAKCRCPgPtYfRL+ TghLB/9IMmwbVTOfyNgsOPDFtvxK+HTtiZ69EdisfS2BhnNupqAGgb67yFfVMO67L4H9wsy4dik j+cGYPFCKJEUNwoYHVtbkn5OwhbG/4yN847Qp+LHbnPXRyV4tBztHDuBZnKp3u4e5NwHlQyYAm5 q+cKni4lCp9dKZizknTYzErUr96/hBIHr1xsUOFp8f17EVBI/WkLUf0zV/DabNvWLv9ZNILyUG3 BAzIcz8RZvrpiQ0Oe0J42MRW5Xunrr+rkxELDO2xt0X2B5XbwEEYKKZ5STHuYZde/Z1m0LdvCQm edmPZEsFMAg6QFu3V/rRPQMPZyKo5PeyqQUgNu1mgOPXoO6g X-Developer-Key: i=u.kleine-koenig@baylibre.com; a=openpgp; fpr=0D2511F322BFAB1C1580266BE2DCDD9132669BD6 The ad7124-4 and ad7124-8 both support 16 channel registers and assigns each channel defined in dt statically such a register. While the driver could be a bit more clever about this, it currently isn't and specifying more than 16 channels yields broken behaviour. So just refuse to bind in this situation. Fixes: b3af341bbd96 ("iio: adc: Add ad7124 support") Signed-off-by: Uwe Kleine-König --- drivers/iio/adc/ad7124.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/drivers/iio/adc/ad7124.c b/drivers/iio/adc/ad7124.c index 8d94bc2b1cac..5352b26bb391 100644 --- a/drivers/iio/adc/ad7124.c +++ b/drivers/iio/adc/ad7124.c @@ -821,6 +821,16 @@ static int ad7124_parse_channel_config(struct iio_dev *indio_dev, if (!st->num_channels) return dev_err_probe(dev, -ENODEV, "no channel children\n"); + /* + * The driver assigns each logical channel defined in the device tree + * statically one channel register. So only accept 16 such logical + * channels to not treat CONFIG_0 (i.e. the register following + * CHANNEL_15) as an additional channel register. The driver could be + * improved to lift this limitation. + */ + if (st->num_channels > AD7124_MAX_CHANNELS) + return dev_err_probe(dev, -EINVAL, "Too many channels defined\n"); + chan = devm_kcalloc(indio_dev->dev.parent, st->num_channels, sizeof(*chan), GFP_KERNEL); if (!chan) From patchwork Wed Nov 27 14:59:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= X-Patchwork-Id: 13887102 Received: from mail-wr1-f42.google.com (mail-wr1-f42.google.com [209.85.221.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 81FED2110E for ; Wed, 27 Nov 2024 15:00:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732719612; cv=none; b=NzBNvgBvTveI0dKkp2CGX+l8fQPTCeP438QmDG1iyEjjVk3dGmnN17UphpFewyaQKlIW10v4YxT7a/Ntlh2ZRQkavM4tJFZyc9a8XnB33STO36tELSuH9+tK4lK6my4tkf7Bhig3w/eBu2hF3W2jKxZmX8/COHsM6XeZHn65kcc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732719612; c=relaxed/simple; bh=UpCDFbHiLoBrphP0ZQ6Ni+CRtV5VYT4LknAceCYNRfw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=fu0TchTWerBuYWtHNtFbqOq5jAVkaVbpi+lAJsUSCXhuAzR2PBqfjHm66R38ze6KdPsRFLfcX6pSJAtq7HM9af5E25NtmftpF0kY3qfxKoBG3Q5kdh9/MTvzEntfkPXttMCVE/R8Q/2FKGWDGDP9I71jCa6hBJK0JuRnPmpUbto= 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=N5JTYIF9; arc=none smtp.client-ip=209.85.221.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="N5JTYIF9" Received: by mail-wr1-f42.google.com with SMTP id ffacd0b85a97d-38232cebb0cso5191218f8f.1 for ; Wed, 27 Nov 2024 07:00:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1732719609; x=1733324409; 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=RITSuX/h//gaahWszdW0fpwFRnABn01F981JW4yCXNU=; b=N5JTYIF960rX+xKrmdU+030IVgiZ2LCEEePiGfpyZvWElc5mW8X0p16V2/zLOesPSo HpLzf8JVPbsgkkU02SBlYxiYyERk5zHZ04VNxi9OUwKvka9ntjI/wj8PKpbe4W2RijLv 9SAuIDZcpjEyFy0CP17wnEB3bKUljGla2y2L/Ai+XOtuV836bnPPdkw7QQpKQmSc9pzt UsiuJpMYFcthDcnIOLYiiZ/mIcNh3+jSBy1FrprCXdYMt7X6EzxUbyNQhmJIm9DlAgao RHCM3PRMCJhw0UZXACI+Rqx1vTPWGZPfFG7ViYyoI0cx3i56kqZRVHIy3vL3LyBJc+7F +jcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732719609; x=1733324409; 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=RITSuX/h//gaahWszdW0fpwFRnABn01F981JW4yCXNU=; b=loQtzxAEE/4l/sp0bYPwrYaJEd9fI6ftn4ZSdGD8hJP+bNGjA0fagVMZqaE/gFSzsY TT99IlUuUqA74Jl/eUWcMjtkeA67jXZ+rEOrrRn08lFuzjhi8UkccdyskpS6/svuli3m VEOWi7WjUi0A1gRPsbk8N0LMfbzkAiAft25+zmupP6i26cx+BdIgfyOi6LfeBrZiQzDt 1TFhCPHfC8KsZG1RsjA7xV6om037kJaEI3CTKOxcdM2RsrGbsY30ukS1yTntFVQjnUYN 9xcb7MvEDGxd96aunTSGlaNJl3hjX6TiXQgmwWorXV6I1wYVQhiWwJqkMHivawEICs3P NrQw== X-Forwarded-Encrypted: i=1; AJvYcCULfJR8WhtjviEuQqsp3ZviJfjcu4hcAzNR0IiZ5KA95tmjg7HlyRLWdWq8hk5eIGMjDM85qVPX7Kw=@vger.kernel.org X-Gm-Message-State: AOJu0YyMGopm5SJdZuwa8fN/ZSUDLhP9nRNZCotv7kpyQR/bYm6MjFk+ /5IsbPs4meq3npUUi5nYh/OpDG0h7ecXzG87cYKid3vcO++/SdLc2tBvwIW9Cao= X-Gm-Gg: ASbGnctkxiZK7OjxcL2qytEWX8FleMUpViE5f62NSgcx3DHEAUzX/w4ST8u/Y+e1Jaz 3LfoYEh+COvmNaKb6OxsNjXuB3O9N6aVUtZ8tzoJV3o8Ljcs6VCdLCmiE9i7yUPteL0acvpW8Kz 0fDliNnzTsSBaiJseq8TwMCvF6eJJYIeQfIr2JVmGz1caPbWWudUGQvIhvnwa49QUjNeXsOyLj4 F1kIYKcf9US+oWJ13605Wy+oYU8Qc+LWzXdvubb4dAYIKjIE+7a1wXieUgiyMmwaOoArQoqJ4TP fl5i X-Google-Smtp-Source: AGHT+IGORLPQpRh7vv2CdjslyQ02zjIg2bkEYo1NdQ+gj7PUhBJVCXY3Snm/nz9BkHcfEvGaz13+SQ== X-Received: by 2002:a5d:5f53:0:b0:382:3be6:5502 with SMTP id ffacd0b85a97d-385c6ee11dcmr2568850f8f.51.1732719608768; Wed, 27 Nov 2024 07:00:08 -0800 (PST) Received: from localhost (p5dc6838f.dip0.t-ipconnect.de. [93.198.131.143]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-434aa7d25c5sm23413055e9.28.2024.11.27.07.00.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Nov 2024 07:00:08 -0800 (PST) From: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= To: Jonathan Cameron Cc: Lars-Peter Clausen , Michael Hennerich , linux-iio@vger.kernel.org, Andy Shevchenko , David Lechner , Trevor Gamblin , Nuno Sa Subject: [PATCH v4 02/10] iio: adc: ad7124: Refuse invalid input specifiers Date: Wed, 27 Nov 2024 15:59:31 +0100 Message-ID: <20241127145929.679408-14-u.kleine-koenig@baylibre.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241127145929.679408-12-u.kleine-koenig@baylibre.com> References: <20241127145929.679408-12-u.kleine-koenig@baylibre.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2144; i=u.kleine-koenig@baylibre.com; h=from:subject; bh=UpCDFbHiLoBrphP0ZQ6Ni+CRtV5VYT4LknAceCYNRfw=; b=owEBbQGS/pANAwAKAY+A+1h9Ev5OAcsmYgBnRzPX+1KZ5UiwNQ58Q3K97bSFf9BrVCEAt6t2k PM5wp0FvtGJATMEAAEKAB0WIQQ/gaxpOnoeWYmt/tOPgPtYfRL+TgUCZ0cz1wAKCRCPgPtYfRL+ Tjz4CACjHHVo+l9/VcYRQDnKfORqFYvMP8nD+d0iKLFNSwqCExEkt/LmCnUeIfU6zMDuNtVl4nZ lXq3uhWDF158WbuxaVkSeIdOqD8duPscvrCkqugupgL582DUiiqkEu273nRScnELACt7fvgUP+r GAT07eCS8KfRiZcBiP0DNZXI0mPKnC6T8zIMRNSkuMNv6hLrz+/e/MQuzAXTgTE8JDLmsOuyxGU YS75a3KtbiIOupj0/zvQ1XmVhG2RwHpcresJijijDCbN/y3vt/VtJUGxsjxHZo4+49eq4iM0YH2 3etfzBDSYA8AEt2jfY+Ovqe/eNE0bxUh7QojrTe5FoiNkoXz X-Developer-Key: i=u.kleine-koenig@baylibre.com; a=openpgp; fpr=0D2511F322BFAB1C1580266BE2DCDD9132669BD6 The ad7124-4 has 8 analog inputs; the input select values 8 to 15 are reserved and not to be used. These are fine for ad7124-8. For both ad7124-4 and ad7124-8 values bigger than 15 are internal channels that might appear as inputs in the channels specified in the device description according to the description of commit f1794fd7bdf7 ("iio: adc: ad7124: Remove input number limitation"), values bigger than 31 don't fit into the respective register bit field and the driver masked them to smaller values. Check for these invalid input specifiers and fail to probe if one is found. Fixes: f1794fd7bdf7 ("iio: adc: ad7124: Remove input number limitation") Signed-off-by: Uwe Kleine-König --- drivers/iio/adc/ad7124.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/drivers/iio/adc/ad7124.c b/drivers/iio/adc/ad7124.c index 5352b26bb391..1f3342373f1c 100644 --- a/drivers/iio/adc/ad7124.c +++ b/drivers/iio/adc/ad7124.c @@ -807,6 +807,19 @@ static int ad7124_check_chip_id(struct ad7124_state *st) return 0; } +/* + * Input specifiers 8 - 15 are explicitly reserved for ad7124-4 + * while they are fine for ad7124-8. Values above 31 don't fit + * into the register field and so are invalid for sure. + */ +static bool ad7124_valid_input_select(unsigned int ain, const struct ad7124_chip_info *info) +{ + if (ain >= info->num_inputs && ain < 16) + return false; + + return ain <= FIELD_MAX(AD7124_CHANNEL_AINM_MSK); +} + static int ad7124_parse_channel_config(struct iio_dev *indio_dev, struct device *dev) { @@ -859,6 +872,11 @@ static int ad7124_parse_channel_config(struct iio_dev *indio_dev, if (ret) return ret; + if (!ad7124_valid_input_select(ain[0], st->chip_info) || + !ad7124_valid_input_select(ain[1], st->chip_info)) + return dev_err_probe(dev, -EINVAL, + "diff-channels property of %pfwP contains invalid data\n", child); + st->channels[channel].nr = channel; st->channels[channel].ain = AD7124_CHANNEL_AINP(ain[0]) | AD7124_CHANNEL_AINM(ain[1]); From patchwork Wed Nov 27 14:59:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= X-Patchwork-Id: 13887103 Received: from mail-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DDD19192B82 for ; Wed, 27 Nov 2024 15:00:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732719615; cv=none; b=rH2Us18NgEffV2j1A43tnzrUQiSeXfl/FF8pEraiG8llZ7yakEJmJ3QDjN6fJsjbyO35ch/NDu+crjYtBGqO/snsuOQSDa1gCllfbfSZrvDEqYRYqn86SNtv9vxvDm/k30QCSSWwmnb3Nz9/jOBHA0djq5OILT6eUhhzqsA1fDA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732719615; c=relaxed/simple; bh=c2chI2fPb2+IVOeXoz9ZQxtBUVAWnKXiOkqBr41MPhg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=HUoDzx958VCaOz8n6c0Bj89+Q4vXYgOvIWCBbw3TpMEsMQDkyQRMqSS/j1xtSvcAJak1wTkLjSD7H/Jdg8Sy72nKK/0Y3fEs/pHoub2moNZpzs1neBJYSou0gW41IXF60+4Wy1k+fSuHbfxOyxcPlYAAeuje/oFGJMXVHFJ3yfE= 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=MsH9BkWt; arc=none smtp.client-ip=209.85.221.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="MsH9BkWt" Received: by mail-wr1-f50.google.com with SMTP id ffacd0b85a97d-38231f84dccso4772263f8f.1 for ; Wed, 27 Nov 2024 07:00:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1732719610; x=1733324410; 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=k0lFn9ttFUfc23iC9zAXiCY9LVgl7Q1TQYlmWddMFg4=; b=MsH9BkWt7mEQUHK3fIz9QjthVQ8IwmFxTLV7jVXtDAQRReGpjG/+16uG1/4P/7Yj7a nlOJBwc/nYzNBVk9x9um+rkCTfATUenGBjk8xkAIsQkZBb/lIUMUZ5E5eCTe+JtpEbIe 1ZkV1vTdWoW+AfblGYy44M35XiC4p+b2QZWchAfHnkHN+QIK6lEP0+TLqc7PV5woUPJ8 zUY918CiVvMc/ebqBWDjwrLsOtMiHHxBfq6BGxTxl3Wk6vP6sDF2KlD53V3iQatTw2Tv ZfXyzWtvgBUT4U9pkeOZOhGj+AmOyy4b2bGGirnw1URnYOJXIVlVQGNo5XNe0gZ2nTdv lynA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732719610; x=1733324410; 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=k0lFn9ttFUfc23iC9zAXiCY9LVgl7Q1TQYlmWddMFg4=; b=U7oFVnCs3rqPW/8FV3Rienw+oZZaqyqm8bP2ccyOnOVji5LhoVQkh8h3bkX4XlakwT 9y22iWvYgAvH4/rKvdOYDdRJMhNyhklheluFXDrslQEQ3EEwwBfQ/sI9kGzczQLJ97pb oFQPRkD0EH03/zfTTIqnOWXbr9AQ5e87cua3BqNM7dIL5EK+x2DPLr1HdEU0cB4tjxx1 Qy1T4qzaNusmQdnnVK8JoQ1Rd52yG0K+B1bpQGhOGATMRaRMwtymigduTijJGBRFZ/Qq UlHZVFpABJJFG+b/ZaZkz1Y+hOY7RT/h8eDcGIFS/VLmpgFicghLutXrLifhiXcBhvjz Fg3Q== X-Forwarded-Encrypted: i=1; AJvYcCURtqRYHIm37m2zIv5vsVb0hPEeWM4DZ0RHyUB3DBZhRi4OZboez45I1KvjiyvC6PK7U4jcqe1UaKs=@vger.kernel.org X-Gm-Message-State: AOJu0YzJ0VvnVXP4JwdMkQ6eXUS7e8Rf4RvHR9wzFtXsXuA+W1ovXayV G0MN2MnU49V1ABXNn/QrXjqh+V5XqtXsGU4T6/qUM/fexQ/UVyzpNlllInbON/U= X-Gm-Gg: ASbGncv8L2Qy46q23VoKqqTyiE0LvT/dZJjf90m35vLubSV3QRQA5uF22dABVZ/gGwf /HQAG+ODMr16b0SDlzK9JZfpE0KlnuEdiJ2OeKtdy7VpXvjKmNdVAxS//f+mDyGGAEAdp9a2lOR BzHxlV+CXeRAH6RtgCkIC+5u0WMd+VqVp6HrUpWm51A8y29qD9PAINqIXlZJl/kVvDVHpmEVrDj Hx99xdVD2ZHpLwLFYN2CAKgyAcTG57gJbOyzEiYpQ9CWXsv40amuwIh//JWtZutp4JscniJBD9u B/0A X-Google-Smtp-Source: AGHT+IEHULXerSP+r+yOAEyZw83Nm59b2Y30xx4nztgFcNfqFf979iBcJXlHTwjZpUxMDllbcwGq3w== X-Received: by 2002:a05:6000:2d04:b0:382:4a27:132a with SMTP id ffacd0b85a97d-385c6ebcbdemr2027807f8f.16.1732719610204; Wed, 27 Nov 2024 07:00:10 -0800 (PST) Received: from localhost (p5dc6838f.dip0.t-ipconnect.de. [93.198.131.143]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-434aa763a59sm23577435e9.11.2024.11.27.07.00.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Nov 2024 07:00:09 -0800 (PST) From: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= To: Jonathan Cameron Cc: Lars-Peter Clausen , Michael Hennerich , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Alisa-Dariana Roman , Renato Lui Geh , Ceclan Dumitru , linux-iio@vger.kernel.org, devicetree@vger.kernel.org, Andy Shevchenko , David Lechner , Trevor Gamblin , Nuno Sa Subject: [PATCH v4 03/10] dt-bindings: iio: adc: adi,ad7{124,173,192,780}: Allow specifications of a gpio for irq line Date: Wed, 27 Nov 2024 15:59:32 +0100 Message-ID: <20241127145929.679408-15-u.kleine-koenig@baylibre.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241127145929.679408-12-u.kleine-koenig@baylibre.com> References: <20241127145929.679408-12-u.kleine-koenig@baylibre.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=6977; i=u.kleine-koenig@baylibre.com; h=from:subject; bh=c2chI2fPb2+IVOeXoz9ZQxtBUVAWnKXiOkqBr41MPhg=; b=owEBbQGS/pANAwAKAY+A+1h9Ev5OAcsmYgBnRzPZBWQdAg1OhqqsYO5AHj5Et9uJ2+Ghb70hc IP1Mig6PCCJATMEAAEKAB0WIQQ/gaxpOnoeWYmt/tOPgPtYfRL+TgUCZ0cz2QAKCRCPgPtYfRL+ TqB8B/92m1qv5vDKwe8mGHUTgKPjkCSocGGUxzEPY5vyRexd+hWt9KH9P0ciCMwffCdOBeDzQVq VaNglcpnajGr4mydMmHqlVZhKNR5Ae+FTeZ9FaUHWDy4AwPHYVCWndE1ykE8FpPjKRkP5W1LTI4 yfvy4B2HaJ1xtzwXbZB+s0rdN3wPKWXzUHupX6JcDrpw6x+z/IvQJEx6fNc8xhvbUdXEmVkqg59 bOk0yKVdZobocaj44nobbMVCH8m6zKKUQNOV9yVfDbS+AWh84HDeg6OGUicJNDVdFCkcGVCRZQX 80CjgSyVbjq8/Vz12xeilAEsW8b953GQ/9i/2pKxqpP4UzvF X-Developer-Key: i=u.kleine-koenig@baylibre.com; a=openpgp; fpr=0D2511F322BFAB1C1580266BE2DCDD9132669BD6 For the AD7124 chip and some of its cousins the logical irq line (̅R̅D̅Y) is physically on the same pin as the spi MISO output (DOUT) and so reading a register might trigger an interrupt. For correct operation it's critical that the actual state of the pin can be read to judge if an interrupt event is a real one or just a spurious one triggered by toggling the line in its MISO mode. Allow specification of an "rdy-gpios" property that references a GPIO that can be used for that purpose. While this is typically the same GPIO also used (implicitly) as interrupt source, it is still supposed that the interrupt is specified as before and usual. Signed-off-by: Uwe Kleine-König --- .../devicetree/bindings/iio/adc/adi,ad7124.yaml | 13 +++++++++++++ .../devicetree/bindings/iio/adc/adi,ad7173.yaml | 12 ++++++++++++ .../devicetree/bindings/iio/adc/adi,ad7192.yaml | 15 +++++++++++++++ .../devicetree/bindings/iio/adc/adi,ad7780.yaml | 11 +++++++++++ 4 files changed, 51 insertions(+) diff --git a/Documentation/devicetree/bindings/iio/adc/adi,ad7124.yaml b/Documentation/devicetree/bindings/iio/adc/adi,ad7124.yaml index 35ed04350e28..9f34a055fdf1 100644 --- a/Documentation/devicetree/bindings/iio/adc/adi,ad7124.yaml +++ b/Documentation/devicetree/bindings/iio/adc/adi,ad7124.yaml @@ -37,6 +37,17 @@ properties: description: IRQ line for the ADC maxItems: 1 + rdy-gpios: + description: | + GPIO reading the ̅̅R̅D̅Y line. Having such a GPIO is technically optional but + highly recommended because DOUT/̅R̅D̅Y toggles during SPI transfers (in its + DOUT aka MISO role) and so usually triggers a spurious interrupt. The + distinction between such a spurious event and a real one can only be done + by reading such a GPIO. (There is a register telling the same + information, but accessing that one needs a SPI transfer which then + triggers another interrupt event.) + maxItems: 1 + '#address-cells': const: 1 @@ -111,6 +122,7 @@ unevaluatedProperties: false examples: - | + #include spi { #address-cells = <1>; #size-cells = <0>; @@ -121,6 +133,7 @@ examples: spi-max-frequency = <5000000>; interrupts = <25 2>; interrupt-parent = <&gpio>; + rdy-gpios = <&gpio 25 GPIO_ACTIVE_LOW>; refin1-supply = <&adc_vref>; clocks = <&ad7124_mclk>; clock-names = "mclk"; diff --git a/Documentation/devicetree/bindings/iio/adc/adi,ad7173.yaml b/Documentation/devicetree/bindings/iio/adc/adi,ad7173.yaml index 17c5d39cc2c1..c70eb75c6a65 100644 --- a/Documentation/devicetree/bindings/iio/adc/adi,ad7173.yaml +++ b/Documentation/devicetree/bindings/iio/adc/adi,ad7173.yaml @@ -133,6 +133,17 @@ properties: '#clock-cells': const: 0 + rdy-gpios: + description: | + GPIO reading the ̅̅R̅D̅Y line. Having such a GPIO is technically optional but + highly recommended because DOUT/̅R̅D̅Y toggles during SPI transfers (in its + DOUT aka MISO role) and so usually triggers a spurious interrupt. The + distinction between such a spurious event and a real one can only be done + by reading such a GPIO. (There is a register telling the same + information, but accessing that one needs a SPI transfer which then + triggers another interrupt event.) + maxItems: 1 + patternProperties: "^channel@[0-9a-f]$": type: object @@ -440,6 +451,7 @@ examples: interrupts = <25 IRQ_TYPE_EDGE_FALLING>; interrupt-names = "rdy"; interrupt-parent = <&gpio>; + rdy-gpios = <&gpio 25 GPIO_ACTIVE_LOW>; spi-max-frequency = <5000000>; gpio-controller; #gpio-cells = <2>; diff --git a/Documentation/devicetree/bindings/iio/adc/adi,ad7192.yaml b/Documentation/devicetree/bindings/iio/adc/adi,ad7192.yaml index 66dd1c549bd3..204766de1e01 100644 --- a/Documentation/devicetree/bindings/iio/adc/adi,ad7192.yaml +++ b/Documentation/devicetree/bindings/iio/adc/adi,ad7192.yaml @@ -106,6 +106,17 @@ properties: description: see Documentation/devicetree/bindings/iio/adc/adc.yaml type: boolean + rdy-gpios: + description: | + GPIO reading the ̅̅R̅D̅Y line. Having such a GPIO is technically optional but + highly recommended because DOUT/̅R̅D̅Y toggles during SPI transfers (in its + DOUT aka MISO role) and so usually triggers a spurious interrupt. The + distinction between such a spurious event and a real one can only be done + by reading such a GPIO. (There is a register telling the same + information, but accessing that one needs a SPI transfer which then + triggers another interrupt event.) + maxItems: 1 + patternProperties: "^channel@[0-9a-f]+$": type: object @@ -181,6 +192,7 @@ unevaluatedProperties: false examples: - | + #include spi { #address-cells = <1>; #size-cells = <0>; @@ -195,6 +207,7 @@ examples: clock-names = "mclk"; interrupts = <25 0x2>; interrupt-parent = <&gpio>; + rdy-gpios = <&gpio 25 GPIO_ACTIVE_LOW>; aincom-supply = <&aincom>; dvdd-supply = <&dvdd>; avdd-supply = <&avdd>; @@ -207,6 +220,7 @@ examples: }; }; - | + #include spi { #address-cells = <1>; #size-cells = <0>; @@ -224,6 +238,7 @@ examples: #clock-cells = <0>; interrupts = <25 0x2>; interrupt-parent = <&gpio>; + rdy-gpios = <&gpio 25 GPIO_ACTIVE_LOW>; aincom-supply = <&aincom>; dvdd-supply = <&dvdd>; avdd-supply = <&avdd>; diff --git a/Documentation/devicetree/bindings/iio/adc/adi,ad7780.yaml b/Documentation/devicetree/bindings/iio/adc/adi,ad7780.yaml index be2616ff9af6..5f8f59e4e336 100644 --- a/Documentation/devicetree/bindings/iio/adc/adi,ad7780.yaml +++ b/Documentation/devicetree/bindings/iio/adc/adi,ad7780.yaml @@ -63,6 +63,17 @@ properties: marked GPIO_ACTIVE_LOW. maxItems: 1 + rdy-gpios: + description: | + GPIO reading the ̅̅R̅D̅Y line. Having such a GPIO is technically optional but + highly recommended because DOUT/̅R̅D̅Y toggles during SPI transfers (in its + DOUT aka MISO role) and so usually triggers a spurious interrupt. The + distinction between such a spurious event and a real one can only be done + by reading such a GPIO. (There is a register telling the same + information, but accessing that one needs a SPI transfer which then + triggers another interrupt event.) + maxItems: 1 + required: - compatible - reg From patchwork Wed Nov 27 14:59:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= X-Patchwork-Id: 13887104 Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (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 8D14246447 for ; Wed, 27 Nov 2024 15:00:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732719615; cv=none; b=ampQBqRyGoNXw3vTxaIj6QeyxnWWLfD0pFaeCpzGrXv7t9m3129+CErJhnBP7/mS/uK7B9BTACX/qH89JsnBF7/UF7qI2VaGReoHRUxf/3DWq3O+0Qdikg0fKlU5qRY3NzwQDMOQenN+sIYU+fXakAJceQgd1CvNSFiw/9CSUTs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732719615; c=relaxed/simple; bh=3VYK2lqcXHXpyUFZjq+zgESMLPVepCyq2JNbLTqaCCM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=M/R7EGG9JHKV5/lZA5KMMhltYpRHGn2V7JK8peFB/11NzlSAFCKNbMeNN7cvFcNNtKyibYyUbE/ur1xTJJD9ymPIpcw9fAw8oRSVWsVLDxghEC5QZONE/r4UkjZw544oZLU2phRgeyLyC+s9TEAbfnlS8iS5P4zx3DgZcEgzbmw= 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=p1q/tJuL; arc=none smtp.client-ip=209.85.128.43 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="p1q/tJuL" Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-434a90fed23so12365185e9.1 for ; Wed, 27 Nov 2024 07:00:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1732719612; x=1733324412; 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=lXqiwyOCn9KgPUbNo99Xcjj0+MMaaSIu7O76gHJWUsY=; b=p1q/tJuLgJQFu4uVsozk4JJL9+8foHLcAyQMXwDQ5wRoxjMrZdnmHeK6NCeA8AbtBJ C3sc0CbOlUK4CP4duW6NYem4c+1/N+xJ+EsfLKATu+9xH684GEGd5lhtbVInjwNYcvIp qH+LzFkG5nGRKcYJfDnZhKDMovV7PGv+EEH36Hp+VfMaloDBXn3Sn2NhS7NmFC4yMRsI 7g1Vk9MU0GwHQtCxSjZ0tWBVJ0F481Fa4GBvHsna6l3Arrzaei89hy1pa+XjqSFd4lQw FOKP7nM10vvOiwW6lkhWXgTEVm9t9Hsz/bhw6nZr+ud/CUR5AdQtmDGEQnUF6hFAS+sp Q4Ew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732719612; x=1733324412; 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=lXqiwyOCn9KgPUbNo99Xcjj0+MMaaSIu7O76gHJWUsY=; b=qN0kq8fO8wowFXrxMdNB++JCszCOiWWQYhFiuRjHEaLpDn6yEVU17+mSYPGb8QjtiV qucDxWCDXX8O/BmNb5IIYfkXmfHkkyAcf1s6Kqjmtz9dQTakAmqYoRA7AkR2RxDE8Rw5 2yfLzMA/WywTIEvOR3yuKIXu5SYK/8xzpe6XJglMkZkkkeOb3PBKNOjH5q3QlS5M4pZu hXLHdUn3qo7AarrArhpbR43KfFmWC4NhBnX0xsKYRUWUiRrvH+XJPAwRTeWVGbXfhR7t GBEwDJKzr+eWWtNQo4Hncdr7b4K2vZUQx20gGJI/E429Zmssu/u6FRP2n+3urj03CPp9 SblQ== X-Forwarded-Encrypted: i=1; AJvYcCV78oxBLhLmOQnFvVCQ2w69ctFpE0kIcrhI1u8y43fzhtSLe8uG5nsW/w0WMsrSAk3RydnA9+nsmaw=@vger.kernel.org X-Gm-Message-State: AOJu0Yxpx1CghtMQvrTvG4M1ST6q/Fm4+cZi8PUtHQ+8mfVaeGwcj3/x Wi8Wv2Lg2Yp1Pdm6akHzipjcctzCiQTIQAJrgOEiqnHs0DkE1A27aUTmchzob8s= X-Gm-Gg: ASbGnctCHjX2HuuDnHuKc4vENiZDZWTnXGdhS4D4lX+Y9RuvJrEyIpCZl5+uxD2gbs/ 3L1Psfb8X/yVAsj00dYRpMZ7oKmg+Gg6HxDhh5IscMXxbOYt91QmT5iCei7Kkmjlg5Nd5ps1R5t kEL1iBHo9ZTTu78fYDBkJS4wh46vAYfty0CWPGsfYeNnCl/wB/Wt6ROzWf2bppPIbDVDtPSoyZW VFwDYi5YLrF624XgKsRgEtX6xarw4Nw1sJfbv4GjAC7UsbMmEXEen+pAmBE/CwieuTxc52eWTdp i0Mv X-Google-Smtp-Source: AGHT+IHxtcDZV6SnZr/qyz0FcDhgA32vf0vMVOU08X8jSxOUDKH6eZqwNjDsSH7GoRtVumnfeIoW4w== X-Received: by 2002:a05:600c:4fc9:b0:434:a315:19c with SMTP id 5b1f17b1804b1-434a9dbc455mr35225795e9.3.1732719611855; Wed, 27 Nov 2024 07:00:11 -0800 (PST) Received: from localhost (p5dc6838f.dip0.t-ipconnect.de. [93.198.131.143]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-434a9728460sm20689275e9.1.2024.11.27.07.00.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Nov 2024 07:00:11 -0800 (PST) From: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= To: Jonathan Cameron Cc: Lars-Peter Clausen , Michael Hennerich , Nuno Sa , Dumitru Ceclan , David Lechner , linux-iio@vger.kernel.org, Andy Shevchenko , Trevor Gamblin Subject: [PATCH v4 04/10] iio: adc: ad_sigma_delta: Add support for reading irq status using a GPIO Date: Wed, 27 Nov 2024 15:59:33 +0100 Message-ID: <20241127145929.679408-16-u.kleine-koenig@baylibre.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241127145929.679408-12-u.kleine-koenig@baylibre.com> References: <20241127145929.679408-12-u.kleine-koenig@baylibre.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4052; i=u.kleine-koenig@baylibre.com; h=from:subject; bh=3VYK2lqcXHXpyUFZjq+zgESMLPVepCyq2JNbLTqaCCM=; b=owEBbQGS/pANAwAKAY+A+1h9Ev5OAcsmYgBnRzPbPIMxf/kANGRxUASya61ZjKGNpN0xlaFan tfW8FmkCwmJATMEAAEKAB0WIQQ/gaxpOnoeWYmt/tOPgPtYfRL+TgUCZ0cz2wAKCRCPgPtYfRL+ TiMDCACmImK7KoDWgUAjlqgrZp9d+6bB4TxTG9Ad2I87B3VAo32g4xT7je5VATJLOovjUW9J0d3 x8ZSmzN7QvndvPxM9HDc7fwPHtWqNEuQnNOTyI9DDM7IMeZJ7yVDM/4civu7gSfC3xXP0zCb2/8 qx1DML5LfXECZq/GTIZYQV00nyubcC+mdyQpzC0ZNaDt4PZU75WqCI6cB5HD5AuIsZyeV/u2hn7 vBf+AK035oBIxqVtXzi+UrmraqH1YNjaxHpFz/dsnTzpK6hoJ1PJD/APiz2XZ5FoRwjLWeW0rV4 z0FDx+rB7kJJ0qLWKqeu8MJjYk3Y4nUASscnTF6E5kQUf97C X-Developer-Key: i=u.kleine-koenig@baylibre.com; a=openpgp; fpr=0D2511F322BFAB1C1580266BE2DCDD9132669BD6 Some of the ADCs by Analog signal their irq condition on the MISO line. So typically that line is connected to an SPI controller and a GPIO. The GPIO is used as input and the respective interrupt is enabled when the last SPI transfer is completed. Depending on the GPIO controller the toggling MISO line might make the interrupt pending even while it's masked. In that case the irq handler is called immediately after irq_enable() and so before the device actually pulls that line low which results in non-sense values being reported to the upper layers. The only way to find out if the line was actually pulled low is to read the GPIO. (There is a flag in AD7124's status register that also signals if an interrupt was asserted, but reading that register toggles the MISO line and so might trigger another spurious interrupt.) Add the possibility to specify an interrupt GPIO in the machine description in addition to the plain interrupt. This GPIO is used then to check if the irq line is actually active in the irq handler. Signed-off-by: Uwe Kleine-König --- drivers/iio/adc/ad_sigma_delta.c | 38 ++++++++++++++++++++++---- include/linux/iio/adc/ad_sigma_delta.h | 2 ++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/drivers/iio/adc/ad_sigma_delta.c b/drivers/iio/adc/ad_sigma_delta.c index ea4aabd3960a..7f4eae5244dc 100644 --- a/drivers/iio/adc/ad_sigma_delta.c +++ b/drivers/iio/adc/ad_sigma_delta.c @@ -539,12 +539,29 @@ static irqreturn_t ad_sd_data_rdy_trig_poll(int irq, void *private) { struct ad_sigma_delta *sigma_delta = private; - complete(&sigma_delta->completion); - disable_irq_nosync(irq); - sigma_delta->irq_dis = true; - iio_trigger_poll(sigma_delta->trig); + /* + * AD7124 and a few others use the same physical line for interrupt + * reporting (nRDY) and MISO. + * As MISO toggles when reading a register, this likely results in a + * pending interrupt. This has two consequences: a) The irq might + * trigger immediately after it's enabled even though the conversion + * isn't done yet; and b) checking the STATUS register's nRDY flag is + * off-limits as reading that would trigger another irq event. + * + * So read the MOSI line as GPIO (if available) and only trigger the irq + * if the line is active. Without such a GPIO assume this is a valid + * interrupt. + */ + if (!sigma_delta->rdy_gpiod || gpiod_get_value(sigma_delta->rdy_gpiod)) { + complete(&sigma_delta->completion); + disable_irq_nosync(irq); + sigma_delta->irq_dis = true; + iio_trigger_poll(sigma_delta->trig); - return IRQ_HANDLED; + return IRQ_HANDLED; + } + + return IRQ_NONE; } /** @@ -679,6 +696,17 @@ int ad_sd_init(struct ad_sigma_delta *sigma_delta, struct iio_dev *indio_dev, else sigma_delta->irq_line = spi->irq; + sigma_delta->rdy_gpiod = devm_gpiod_get_optional(&spi->dev, "rdy", GPIOD_IN); + if (IS_ERR(sigma_delta->rdy_gpiod)) + return dev_err_probe(&spi->dev, PTR_ERR(sigma_delta->rdy_gpiod), + "Failed to find rdy gpio\n"); + + if (sigma_delta->rdy_gpiod && !sigma_delta->irq_line) { + sigma_delta->irq_line = gpiod_to_irq(sigma_delta->rdy_gpiod); + if (sigma_delta->irq_line < 0) + return sigma_delta->irq_line; + } + iio_device_set_drvdata(indio_dev, sigma_delta); return 0; diff --git a/include/linux/iio/adc/ad_sigma_delta.h b/include/linux/iio/adc/ad_sigma_delta.h index f8c1d2505940..126b187d70e9 100644 --- a/include/linux/iio/adc/ad_sigma_delta.h +++ b/include/linux/iio/adc/ad_sigma_delta.h @@ -29,6 +29,7 @@ struct ad_sd_calib_data { struct ad_sigma_delta; struct device; +struct gpio_desc; struct iio_dev; /** @@ -96,6 +97,7 @@ struct ad_sigma_delta { unsigned int active_slots; unsigned int current_slot; unsigned int num_slots; + struct gpio_desc *rdy_gpiod; int irq_line; bool status_appended; /* map slots to channels in order to know what to expect from devices */ From patchwork Wed Nov 27 14:59:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= X-Patchwork-Id: 13887111 Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) (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 4EEC41F9EC7 for ; Wed, 27 Nov 2024 15:08:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732720083; cv=none; b=oRh/By3mCmIK3VqeoOm+F1ZflWzJ7Y+xrjcKWjtlWzGCv3CxBOx3nEZp/P79hEDtZiM1pH4iXjOkJiDdS7qgKm1pE+qKg3c0DGSycjF5gB4hLp72kLSQ7EyD+eYeW1046ICTs+0YS49C9BjV/fTIHD30BILUmpM4KsrnByKzT7s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732720083; c=relaxed/simple; bh=dkTnukD2rrkruTwfH798eaeGxe3q8221q3Q+tAj6IYI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=nrUslP7AcL9XABGZWgWcfufUcGL3njQa8SEb0xze4nUwHpleEppZJcK5wM4JToi25k+XJEwnRvzMKb6/AraftGOP5/wLslfUXD6eYHAYz6FhV6/H2hY5P7gNrHnCtr3d6wwv0CQwA5CSxotB6mXrDCKKiqOP72jOzATNwpPSTRE= 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=cIwtw8y/; arc=none smtp.client-ip=209.85.221.51 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="cIwtw8y/" Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-382442b7d9aso5230460f8f.1 for ; Wed, 27 Nov 2024 07:08:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1732720079; x=1733324879; 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=L8sEiFdmMWi2hzthhwzHkLTong+QNvgFETpYvC/h2tk=; b=cIwtw8y/zkOlxUMWTDQFGRphIspEGJJch+wCZSXMMCsBacB609zMMQKQpJCv+jQX+s v1VtdthMcHFcxL4qel9doQXN6AumNAKv13AN/1Kwo0qigMoY30VGkT+Krcwx/bgHtRH+ k1Z9qgiLW0bSpXNekuj4P1OBlHb2cA1qqPOu4hiig649sGQUP71gmoFy2XpHOAXZEcBv ADP2oXqdese7Amb/59gb3LOwG/KXa9FxROs11bo/roykGPzAFOgerDdiUGA0AVy7Qd1m Ky55yjGLxnZLA1XKUQ97IwcAa8U8vpMY6k/sYScrmmkj+pQ97LiYGM42GuKK1t5c4fu0 XmWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732720079; x=1733324879; 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=L8sEiFdmMWi2hzthhwzHkLTong+QNvgFETpYvC/h2tk=; b=sCF24ge6EAPkx6CyMJxbOC85AVfrQdWeRbNXKaxpgA1F08ADU1umEV7aj11fzICbA1 2m0mWpCVx4XCPSARHLQAE0mOTjVdxX+Qi60fg/Dp7NJcsKTcWZhbq6LRrRyQNmOoQ2Y6 k278eVLhq7FsCPg3p0KOc2zCwpmv7vM7ZPDTERyYSXBG1uJX4JwQgSXDqr6VAlx7L+mC Bn80FQ6EI3Ua1SGG88N5ukyyTLE0q0eQbYGYZBGH4zCO7fz88u7yadrCVxpKn3pzqLnC pK5mwTpZ3Hsn6MAS2vlUr3ayZlaDPC8iI03O857vmqLPI32/9YqgLp/27ejIN63iYkhI nBpA== X-Forwarded-Encrypted: i=1; AJvYcCWw16WyXs0C/VWTK9EhsAfDkHsYY3w2wT/uFBDDk0/ujKGDent+Sr191Lc8UrTP+Aoc1PGBjp/8Ae4=@vger.kernel.org X-Gm-Message-State: AOJu0Yw0tIHttbS01il0HYisygtDE5TRcdki6zOJ9DpJsMJj2Bc2bwcA nJsIcFGLkgCEvUF5TawVvCAsClhYoKVxzn/pGHsq+jVW9M+XSeMbSNHZnlQB/buOP/CndBnyxB+ Ag2M= X-Gm-Gg: ASbGnctAKMENMBRc6kXMmOg2ngoI8kH6gtk3N+nsqdsEkPVH1b+WyilfoLBjl4iYjn+ AlZgXJVgwaOmcGewufFobkBLMVt/OAS7sk0dd973sHNmbVo94IToVxoyrx7p6DyXSdNYmdm94e6 58+tEUYU/DjWF/h2t2kij4kfoLydgUHomIjKlUfPuditWMFjWhsVhytR1N8ikUW40ozBjNMTTpy U+OVmEZiWlKolKzYOh2mdHVz3Q5Z7EJAR/ZY+9Uy88wOU/TNoQeBsfIqre1+psn4Pz2hnBbpRAm n3Gx X-Google-Smtp-Source: AGHT+IEQrX5uqS4EOwz/UfuI3dVm5HPlvFdXAk/LsUp4rW3OcjjVUXNq7Kc3Ko0v9nnQlqxBIAWZHg== X-Received: by 2002:a5d:47a2:0:b0:382:3816:f50e with SMTP id ffacd0b85a97d-385c6ec0c20mr2502982f8f.34.1732719613277; Wed, 27 Nov 2024 07:00:13 -0800 (PST) Received: from localhost (p5dc6838f.dip0.t-ipconnect.de. [93.198.131.143]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3825fad6270sm16432660f8f.14.2024.11.27.07.00.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Nov 2024 07:00:12 -0800 (PST) From: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= To: Jonathan Cameron Cc: Lars-Peter Clausen , Michael Hennerich , Alexandru Ardelean , linux-iio@vger.kernel.org, Andy Shevchenko , David Lechner , Trevor Gamblin , Nuno Sa Subject: [PATCH v4 05/10] iio: adc: ad_sigma_delta: Handle CS assertion as intended in ad_sd_read_reg_raw() Date: Wed, 27 Nov 2024 15:59:34 +0100 Message-ID: <20241127145929.679408-17-u.kleine-koenig@baylibre.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241127145929.679408-12-u.kleine-koenig@baylibre.com> References: <20241127145929.679408-12-u.kleine-koenig@baylibre.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=953; i=u.kleine-koenig@baylibre.com; h=from:subject; bh=dkTnukD2rrkruTwfH798eaeGxe3q8221q3Q+tAj6IYI=; b=owEBbQGS/pANAwAKAY+A+1h9Ev5OAcsmYgBnRzPerHUffRNjnKdY3og9fWY46C7ZE5BO0lhvf IA8qsTdgOqJATMEAAEKAB0WIQQ/gaxpOnoeWYmt/tOPgPtYfRL+TgUCZ0cz3gAKCRCPgPtYfRL+ ToLWB/9qRLATUVF9QLKhnDVq/Fqgs8YANLiyhskJLCuYqV8wZ6DL2YGBntNzUx8MF5RERMlie2d mINrjNIYs7mLZHSHl1h34P4PV3m8a/p4IdDmJu77F0+ze7feFaNZNvNvU6zp+bDo3EXiMfWU3WH rT2uq3VY2d/UGiRESNtbDo+LH7p67OlhSaoW9BLcoz+61IlIvjlKSfXF+r6gh4T3+MiCjUr2GAU aykf4rYZNvk4hGPVNeFU2bFDLSc4gphW2mGN4Jdbq9OdImjzvVqnnnoHPUeAAWj8LssE6Q33FSN x5Jhmsit0lhMT/2jU960cnLgUYG7MV+i8HSB2MBlK6eBzi+c X-Developer-Key: i=u.kleine-koenig@baylibre.com; a=openpgp; fpr=0D2511F322BFAB1C1580266BE2DCDD9132669BD6 When struct ad_sigma_delta::keep_cs_asserted was introduced only register writing was adapted to honor this new flag. Also respect it when reading a register. Fixes: df1d80aee963 ("iio: ad_sigma_delta: Properly handle SPI bus locking vs CS assertion") Reviewed-by: Trevor Gamblin Signed-off-by: Uwe Kleine-König --- drivers/iio/adc/ad_sigma_delta.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/iio/adc/ad_sigma_delta.c b/drivers/iio/adc/ad_sigma_delta.c index 7f4eae5244dc..a2efd2145373 100644 --- a/drivers/iio/adc/ad_sigma_delta.c +++ b/drivers/iio/adc/ad_sigma_delta.c @@ -109,7 +109,7 @@ static int ad_sd_read_reg_raw(struct ad_sigma_delta *sigma_delta, }, { .rx_buf = val, .len = size, - .cs_change = sigma_delta->bus_locked, + .cs_change = sigma_delta->keep_cs_asserted, }, }; struct spi_message m; From patchwork Wed Nov 27 14:59:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= X-Patchwork-Id: 13887105 Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.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 A517E2110E for ; Wed, 27 Nov 2024 15:00:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732719618; cv=none; b=lRy0PajDdw91dkE6QyN5AjskbDcANV6TMbdx0qJVco6gk3ILH1DUuc6s5/pEnzbmY/3rIb7umXM0fcunrVoAgCWoFn3WEHhrNoRIGTaxBdRTi0PnfONKvpLOIxxhZ2roTaOqgmuGIcTM9XNPV/Eqgd0rhI5WU1uGTRsNkQo+7O0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732719618; c=relaxed/simple; bh=/UtxUNcu7FlfgGbbbYqeI92ekworOfOuWxMulK9vmm4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=m+UT11o1PdNcMWqOhxCN4knUU7zVHbzUERaRhDrq8K8HbatO3KK16Xf8ZuTNpwvbL4FUMnX5M7Ovo/Y+YHGWCiJtCpCegI+r+Rs3GSmFuQp56YgHdpWrkbCtTsaEDjo2rQLdAaxNmOiEY7brwZZqqVO5Znqj9LXoQY0HL6tPWX0= 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=yDXZ6Wb2; arc=none smtp.client-ip=209.85.128.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="yDXZ6Wb2" Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-434a7ee3d60so5548325e9.1 for ; Wed, 27 Nov 2024 07:00:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1732719615; x=1733324415; 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=Aig5K0sssQgvPkFPKhN2zssbw+/Mz4FBnMCldVaVbqU=; b=yDXZ6Wb2AvXPXWqsNaJZIc8lRxCLPPPksKjKCf/6Cu4RD8lSItjW03Y9WKeVXaGoO0 lQ5xBCvgmvT50vxrP0n8kvbMWKhN35xERZQHMekYIUuLnZKuIUVuHYW6hC9X2GgzRjS4 xAvSJ0hTCNknmHf8YyIKMyr6TvhduJVJtXIr2pA3ISy5jOHn8fBi06bhYyM7+07Mrq5n M9q+Jj1iYiHCPIQ+6v1Skq5ilXMxMAArcRMOBJ+giyPfMevxzgvaUOk0jKmbmC0urVJh XMH31PWMU3e7VAvK0MtdvT5zKfCsEPY5s294BiG3eaBhd95RDIRo8bJzdr25w+PbMXFB Mh8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732719615; x=1733324415; 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=Aig5K0sssQgvPkFPKhN2zssbw+/Mz4FBnMCldVaVbqU=; b=Nr4O2GKyN7pdtxHuQZARcnWL78kfSjrpsJ8/11Jb+k1Z7df2tWtQI28EBoG28MATck yrJmP+GLmMbpnihurN6GCWlcNgMym3ypQvHnyaAoIrnR8wzcILe6zV5CcaLg6zawoJES y1bKnU4RBfp6QyRb9yKcaXRNmJuofE0IUobOZo4zMZ8bH8LpZA461ysU/BEYVnMJ/UYw SZ1lY5eryMdCisT4/DBygr+ZbNzLQIANEEAKBBG825QEjndanbVk389PGAYjBa1PYwiN EXraCea/pmuzsGo+9WRf3YTQUkXceiiWHSN66n2QMTBlAjS1QWOHOr0I3lnYrGynXGz4 s6Uw== X-Forwarded-Encrypted: i=1; AJvYcCWQCtja13+WIk0YYtMTecmLGbvlldgVMuS+9P18EvBJDt7Cc2FTFW5bBsO8hM/dqnOwhbhP7IiXoXE=@vger.kernel.org X-Gm-Message-State: AOJu0YwDP0GjuxSnA4O8hR8efmI/NdULXEBqr4yXZ3xu1I3/o9zCfLjD PoxiVP9JsNafniEFGLY47Me9iLtDE9cRgg44UTFBZ095R3kd+A8Ud313gFG0RG8= X-Gm-Gg: ASbGncv9Eww4w5mz/qftzTz3GTHmyn/ECO+IQ5Bknj+L7CnHgZVtTNN7cQ7tIGnw7j5 fFSrIt2fiwHFqcxfXRIXcZ2Fr58npyG30OUydWr5TQQdCDydR86SEliOR6KHgLkZDERgYdjw5ls 5SbRl22PUjURsXJJ+CzfrFBT2sAbf5NW6vfDwykxxM7uG9/glQaJb09tqU0yUqsnCiviOWjFEwo u7n6ABomnEfsCmy+CdStZqX3IPqRMBbA5OzPqq+emtzw0Iqu93RvBPT4hPV6EnDEUzmbjieLBEE iU7M X-Google-Smtp-Source: AGHT+IHVcyXgbDQ3wUIT3KKZlTmiXMDUie/dJYYTuxVbAakk4X+OJkfWnAIjanW3q4aIcou11G4e2g== X-Received: by 2002:a7b:c85a:0:b0:431:15f1:421d with SMTP id 5b1f17b1804b1-434a4eb786dmr64613835e9.16.1732719614880; Wed, 27 Nov 2024 07:00:14 -0800 (PST) Received: from localhost (p5dc6838f.dip0.t-ipconnect.de. [93.198.131.143]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-434aa7e4df1sm23289425e9.39.2024.11.27.07.00.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Nov 2024 07:00:14 -0800 (PST) From: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= To: Jonathan Cameron Cc: Lars-Peter Clausen , Michael Hennerich , Nuno Sa , Dumitru Ceclan , David Lechner , linux-iio@vger.kernel.org, Andy Shevchenko , Trevor Gamblin Subject: [PATCH v4 06/10] iio: adc: ad_sigma_delta: Fix a race condition Date: Wed, 27 Nov 2024 15:59:35 +0100 Message-ID: <20241127145929.679408-18-u.kleine-koenig@baylibre.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241127145929.679408-12-u.kleine-koenig@baylibre.com> References: <20241127145929.679408-12-u.kleine-koenig@baylibre.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=5630; i=u.kleine-koenig@baylibre.com; h=from:subject; bh=/UtxUNcu7FlfgGbbbYqeI92ekworOfOuWxMulK9vmm4=; b=owEBbQGS/pANAwAKAY+A+1h9Ev5OAcsmYgBnRzPhZ1jctnT359wUzbJNDXuiikKyGWIfPGgx0 Z+4vPzq9DCJATMEAAEKAB0WIQQ/gaxpOnoeWYmt/tOPgPtYfRL+TgUCZ0cz4QAKCRCPgPtYfRL+ Tlc0CACSa3E0ocQsqmH0c2ExJYQfJWBXyAjIKxvyC3tGUO5zz0RBmJkE+tZtbQMhnfxKVnoPh// 4nMdVkUiQjyV2lEgi4fBOuWt9xS2r6j/IYj4Ay0jdzuWSSEx/oLm/64HQSOYBL16MZhmBKPOBhB VZ6gdwgJKIz+2bhAv78aiFw6zdE6Ie8FpSlESYYptEu4gNJcDSuWPyex/uH3SqPK9Khmf/ZPkjU dxHeeYickCTap4j80mQ08AF4o73PLBPjbmsE7gdhnOv0caa15drAJa7Zs2wgZ+cJyungP5Lm3+U /gVanmnqYcuYxwLZHytZtksxuvMN5tfrPuQxK5iiYD63n8j3 X-Developer-Key: i=u.kleine-koenig@baylibre.com; a=openpgp; fpr=0D2511F322BFAB1C1580266BE2DCDD9132669BD6 The ad_sigma_delta driver helper uses irq_disable_nosync(). With that one it is possible that the irq handler still runs after the irq_disable_nosync() function call returns. Also to properly synchronize irq disabling in the different threads proper locking is needed and because it's unclear if the irq handler's irq_disable_nosync() call comes first or the one in the enabler's error path, all code locations that disable the irq must check for .irq_dis first to ensure there is exactly one disable call per enable call. So add a spinlock to the struct ad_sigma_delta and use it to synchronize irq enabling and disabling. Also only act in the irq handler if the irq is still enabled. Fixes: af3008485ea0 ("iio:adc: Add common code for ADI Sigma Delta devices") Signed-off-by: Uwe Kleine-König --- drivers/iio/adc/ad_sigma_delta.c | 56 ++++++++++++++++---------- include/linux/iio/adc/ad_sigma_delta.h | 1 + 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/drivers/iio/adc/ad_sigma_delta.c b/drivers/iio/adc/ad_sigma_delta.c index a2efd2145373..9abde276cd17 100644 --- a/drivers/iio/adc/ad_sigma_delta.c +++ b/drivers/iio/adc/ad_sigma_delta.c @@ -202,6 +202,27 @@ int ad_sd_reset(struct ad_sigma_delta *sigma_delta, } EXPORT_SYMBOL_NS_GPL(ad_sd_reset, IIO_AD_SIGMA_DELTA); +static bool ad_sd_disable_irq(struct ad_sigma_delta *sigma_delta) +{ + guard(spinlock_irqsave)(&sigma_delta->irq_lock); + + /* It's already off, return false to indicate nothing was changed */ + if (sigma_delta->irq_dis) + return false; + + sigma_delta->irq_dis = true; + disable_irq_nosync(sigma_delta->irq_line); + return true; +} + +static void ad_sd_enable_irq(struct ad_sigma_delta *sigma_delta) +{ + guard(spinlock_irqsave)(&sigma_delta->irq_lock); + + sigma_delta->irq_dis = false; + enable_irq(sigma_delta->irq_line); +} + int ad_sd_calibrate(struct ad_sigma_delta *sigma_delta, unsigned int mode, unsigned int channel) { @@ -221,12 +242,10 @@ int ad_sd_calibrate(struct ad_sigma_delta *sigma_delta, if (ret < 0) goto out; - sigma_delta->irq_dis = false; - enable_irq(sigma_delta->irq_line); + ad_sd_enable_irq(sigma_delta); time_left = wait_for_completion_timeout(&sigma_delta->completion, 2 * HZ); if (time_left == 0) { - sigma_delta->irq_dis = true; - disable_irq_nosync(sigma_delta->irq_line); + ad_sd_disable_irq(sigma_delta); ret = -EIO; } else { ret = 0; @@ -294,8 +313,7 @@ int ad_sigma_delta_single_conversion(struct iio_dev *indio_dev, ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_SINGLE); - sigma_delta->irq_dis = false; - enable_irq(sigma_delta->irq_line); + ad_sd_enable_irq(sigma_delta); ret = wait_for_completion_interruptible_timeout( &sigma_delta->completion, HZ); @@ -314,10 +332,7 @@ int ad_sigma_delta_single_conversion(struct iio_dev *indio_dev, &raw_sample); out: - if (!sigma_delta->irq_dis) { - disable_irq_nosync(sigma_delta->irq_line); - sigma_delta->irq_dis = true; - } + ad_sd_disable_irq(sigma_delta); sigma_delta->keep_cs_asserted = false; ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE); @@ -396,8 +411,7 @@ static int ad_sd_buffer_postenable(struct iio_dev *indio_dev) if (ret) goto err_unlock; - sigma_delta->irq_dis = false; - enable_irq(sigma_delta->irq_line); + ad_sd_enable_irq(sigma_delta); return 0; @@ -414,10 +428,7 @@ static int ad_sd_buffer_postdisable(struct iio_dev *indio_dev) reinit_completion(&sigma_delta->completion); wait_for_completion_timeout(&sigma_delta->completion, HZ); - if (!sigma_delta->irq_dis) { - disable_irq_nosync(sigma_delta->irq_line); - sigma_delta->irq_dis = true; - } + ad_sd_disable_irq(sigma_delta); sigma_delta->keep_cs_asserted = false; ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE); @@ -516,8 +527,7 @@ static irqreturn_t ad_sd_trigger_handler(int irq, void *p) irq_handled: iio_trigger_notify_done(indio_dev->trig); - sigma_delta->irq_dis = false; - enable_irq(sigma_delta->irq_line); + ad_sd_enable_irq(sigma_delta); return IRQ_HANDLED; } @@ -551,11 +561,13 @@ static irqreturn_t ad_sd_data_rdy_trig_poll(int irq, void *private) * So read the MOSI line as GPIO (if available) and only trigger the irq * if the line is active. Without such a GPIO assume this is a valid * interrupt. + * + * Also as disable_irq_nosync() is used to disable the irq, only act if + * the irq wasn't disabled before. */ - if (!sigma_delta->rdy_gpiod || gpiod_get_value(sigma_delta->rdy_gpiod)) { + if ((!sigma_delta->rdy_gpiod || gpiod_get_value(sigma_delta->rdy_gpiod)) && + ad_sd_disable_irq(sigma_delta)) { complete(&sigma_delta->completion); - disable_irq_nosync(irq); - sigma_delta->irq_dis = true; iio_trigger_poll(sigma_delta->trig); return IRQ_HANDLED; @@ -691,6 +703,8 @@ int ad_sd_init(struct ad_sigma_delta *sigma_delta, struct iio_dev *indio_dev, } } + spin_lock_init(&sigma_delta->irq_lock); + if (info->irq_line) sigma_delta->irq_line = info->irq_line; else diff --git a/include/linux/iio/adc/ad_sigma_delta.h b/include/linux/iio/adc/ad_sigma_delta.h index 126b187d70e9..f86eca6126b4 100644 --- a/include/linux/iio/adc/ad_sigma_delta.h +++ b/include/linux/iio/adc/ad_sigma_delta.h @@ -86,6 +86,7 @@ struct ad_sigma_delta { /* private: */ struct completion completion; + spinlock_t irq_lock; /* protects .irq_dis and irq en/disable state */ bool irq_dis; bool bus_locked; From patchwork Wed Nov 27 14:59:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= X-Patchwork-Id: 13887106 Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (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 F29AC22F19 for ; Wed, 27 Nov 2024 15:00:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732719620; cv=none; b=q+hhnZPEpjW2GrmIBBpKpEgcSYnzoRHUVhP46j7bSDZ5iSNUBYcpHXk2x09dYrmPI1BOkCoDJMzjLEjx8Vnnznbo51pCvLD6kG0C8kEjozJJHUW7ujpxjhzvXRa/XDBV+B7nchhKzwjwCh7AbeLFEyjpxL/7A/ehC5q9393IW8M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732719620; c=relaxed/simple; bh=2HKcFo7TQCrQ4num0VNgfMdjg/saRUqyt2yVyJlKlYY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=eKWwqzEWL++Nv78bgSVlyjoxgF/iwvsnzbrN9FH9iWxY2C/Nug/x7YR+K2JuvyJ3g5qfvC8MmwrTXtfqXbmju+CQ4/GzGkk+Z0jQIrkVZ+QLG33nCAX4FvLhtqG21dEg5wvGuypTch+l1PZMg8nqSg/NO3W4JbZ5L+ufRTnmziU= 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=xlimx/87; arc=none smtp.client-ip=209.85.128.43 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="xlimx/87" Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-434aa222d96so9239035e9.0 for ; Wed, 27 Nov 2024 07:00:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1732719616; x=1733324416; 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=PaSD2JbxY7n+JgrJBd0cmajEwrXt3LIZm98N5Js46JA=; b=xlimx/87jEeY09N9ryhjUiCaZBUGKc9iS4oaMfzWzZZRE4oqTGqHxmv4lErEUzUcb3 Lzmb6+OiLg3Y9ZJZHsMZH52IjBWX5/aN0W0WfirboCng44NRdfA4G64LANuYuyFCbgiH O6aCtMoj1+81TPJ6mg4KzgV6tODSRW3ah8qL9045qS/gId8nQflHfkNJgjhcu93XvoZC ioi/xYKOtqz6WIT7M2cwXg9My3VUfizrJZXgGigWRCGezY3oQJegMJyJYvEYtxFXTU8F QS9IjtwsUOkNf9I2hu+uaBJr1jZIfAcJRMfGUStvup6fu8GA2U8u0aEPZHPTRadaRwtg vJBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732719616; x=1733324416; 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=PaSD2JbxY7n+JgrJBd0cmajEwrXt3LIZm98N5Js46JA=; b=QBvm7+MmgKUOxttKB50vlNGwiwYwbjwBWtg36mWcKilIZtCdkYHrVif71Az2VWFMLp 9w5bXvQDrM8R6I+ITN/n9CjYPud56S/Gy3DrIIkc7p2qM2SDkNjRmJ4SrhEf+j3rlJmd 2xxdlTs9Grt9ERQvj5pinojtFApixZq6q5XKvW6B1Bj8APwjEBWVjRD/A/89fIBIGhqK Jk+B2dXW/Iq7QtBp37TnulFMNlp2ACU12Pk393hLKP4HFNqTpjx2pPZS/A81lcBfDnus IH1qTdgQBXCQYI67iY/Ly8/76/RYz8tBuRKebYa9nCuyNh22tMTENhJkCKPyiHnM/ycQ vw5Q== X-Forwarded-Encrypted: i=1; AJvYcCUlcw/9LvnkoqySQbdtcKnxEIjAFzn1mQAplYT7kFxRjmkLraRFPQPyasdt/G/MjVwk6mNFS9Pt0Ek=@vger.kernel.org X-Gm-Message-State: AOJu0YzHTYWUhNxC1SnK4Ig/0fGV2vDY7cgmM2uPvniyDc4vnBgN9WoF hz+6meldKl+VY6AdvQoSs90U7p2q2L5k3moY4CD4DozyzYmW1jC/9m3yn+kuIuY= X-Gm-Gg: ASbGncsmF3oyweUg0YCJmuume6uulSp+f9pDGFQylYt8P+sVTq0bTR2hux/2deMLztC JE0xAUIFckC+vHy7I3+/gvN8InrJuL/ZpROghFf4QEs5N76MgzXNOS5+6S5N7FfAo7DiBtHiYGV 6KFsfep69dEe0TEB6KuFGtM5CWJeTxL26PJduFuME39i2qoiyIiLW1kzMAcz+FEi7QlC/IPgPh9 lIv5YFpZAn0bUMxLqaJJ81kGO+I21+3DjLblStbnrmhILNXilc+8JFEdtEqO1GQ2zJG9wtIUFgh nc/F X-Google-Smtp-Source: AGHT+IEDmnMfYAIh7Erf5LSYpx0Oeb2Nr8EJZ9NqrpieQDGM1uY5046kpUVWn2+Zb1Jbq6fEyDiRQw== X-Received: by 2002:a05:600c:2244:b0:434:a8ef:442e with SMTP id 5b1f17b1804b1-434a9dfbafamr36710345e9.31.1732719616347; Wed, 27 Nov 2024 07:00:16 -0800 (PST) Received: from localhost (p5dc6838f.dip0.t-ipconnect.de. [93.198.131.143]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-434aa7a45d9sm23337335e9.2.2024.11.27.07.00.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Nov 2024 07:00:15 -0800 (PST) From: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= To: Jonathan Cameron Cc: Lars-Peter Clausen , Michael Hennerich , Alisa-Dariana Roman , Nuno Sa , Dumitru Ceclan , David Lechner , linux-iio@vger.kernel.org, Andy Shevchenko , Trevor Gamblin Subject: [PATCH v4 07/10] iio: adc: ad_sigma_delta: Store information about reset sequence length Date: Wed, 27 Nov 2024 15:59:36 +0100 Message-ID: <20241127145929.679408-19-u.kleine-koenig@baylibre.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241127145929.679408-12-u.kleine-koenig@baylibre.com> References: <20241127145929.679408-12-u.kleine-koenig@baylibre.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=6415; i=u.kleine-koenig@baylibre.com; h=from:subject; bh=2HKcFo7TQCrQ4num0VNgfMdjg/saRUqyt2yVyJlKlYY=; b=owEBbQGS/pANAwAKAY+A+1h9Ev5OAcsmYgBnRzPlfPehvuD7NanA5CU+zAVsKAUhDZ6URvb5T rJan46pg8iJATMEAAEKAB0WIQQ/gaxpOnoeWYmt/tOPgPtYfRL+TgUCZ0cz5QAKCRCPgPtYfRL+ TkpHB/9nd3oOYYrO7ZvheRWJP4Xjc01u355/UhXbreM+7UqilBdpuDa1VwNPD28Ci6NXhjzvuhJ V1FI1yPlP2L1vqCDtdbraMsFIDp6qYCNYzDL2EWyU4gpYVh6yLw/icYQWNO92SEK1mVqtMFeBse hRPaUEjhm45ez2scPUxFG1J22uUc2jIEbJ96kbeSRdJujYGx99ZpW//b34AOLPdTZ83BWw/Ais2 gg6fDEjg03lGYOe6lLnVuW0oq0/+vecyINwHSWZRbAQfZqqJV18FEoanhpjoy+xJu5OUlb+0k35 JrV/z3yjPoQOFhOoGdeIeRXVR8+QjvUC+biB4TUPFSvv+kFJ X-Developer-Key: i=u.kleine-koenig@baylibre.com; a=openpgp; fpr=0D2511F322BFAB1C1580266BE2DCDD9132669BD6 The various chips can be reset using a sequence of SPI transfers with MOSI = 1. The length of such a sequence varies from chip to chip. Store that length in struct ad_sigma_delta_info and replace the respective parameter to ad_sd_reset() with it. Note the ad7192 used to pass 48 as length but the documentation specifies 40 as the required length. Assuming the latter is right. (Using a too long sequence doesn't hurt apart from using a longer spi transfer than necessary, so this is no relevant fix.) The motivation for storing this information is that this is useful to clear a pending RDY signal in the next change. Signed-off-by: Uwe Kleine-König --- drivers/iio/adc/ad7124.c | 3 ++- drivers/iio/adc/ad7173.c | 1 + drivers/iio/adc/ad7192.c | 4 +++- drivers/iio/adc/ad7791.c | 1 + drivers/iio/adc/ad7793.c | 3 ++- drivers/iio/adc/ad_sigma_delta.c | 5 ++--- include/linux/iio/adc/ad_sigma_delta.h | 5 +++-- 7 files changed, 14 insertions(+), 8 deletions(-) diff --git a/drivers/iio/adc/ad7124.c b/drivers/iio/adc/ad7124.c index 1f3342373f1c..b17c3dbeaeba 100644 --- a/drivers/iio/adc/ad7124.c +++ b/drivers/iio/adc/ad7124.c @@ -571,6 +571,7 @@ static const struct ad_sigma_delta_info ad7124_sigma_delta_info = { .data_reg = AD7124_DATA, .num_slots = 8, .irq_flags = IRQF_TRIGGER_FALLING, + .num_resetclks = 64, }; static int ad7124_read_raw(struct iio_dev *indio_dev, @@ -756,7 +757,7 @@ static int ad7124_soft_reset(struct ad7124_state *st) unsigned int readval, timeout; int ret; - ret = ad_sd_reset(&st->sd, 64); + ret = ad_sd_reset(&st->sd); if (ret < 0) return ret; diff --git a/drivers/iio/adc/ad7173.c b/drivers/iio/adc/ad7173.c index 0702ec71aa29..2550194efee8 100644 --- a/drivers/iio/adc/ad7173.c +++ b/drivers/iio/adc/ad7173.c @@ -744,6 +744,7 @@ static struct ad_sigma_delta_info ad7173_sigma_delta_info = { .read_mask = BIT(6), .status_ch_mask = GENMASK(3, 0), .data_reg = AD7173_REG_DATA, + .num_resetclks = 64, }; static int ad7173_setup(struct iio_dev *indio_dev) diff --git a/drivers/iio/adc/ad7192.c b/drivers/iio/adc/ad7192.c index 7042ddfdfc03..c4dd48edd8d9 100644 --- a/drivers/iio/adc/ad7192.c +++ b/drivers/iio/adc/ad7192.c @@ -361,6 +361,7 @@ static const struct ad_sigma_delta_info ad7192_sigma_delta_info = { .status_ch_mask = GENMASK(3, 0), .num_slots = 4, .irq_flags = IRQF_TRIGGER_FALLING, + .num_resetclks = 40, }; static const struct ad_sigma_delta_info ad7194_sigma_delta_info = { @@ -373,6 +374,7 @@ static const struct ad_sigma_delta_info ad7194_sigma_delta_info = { .read_mask = BIT(6), .status_ch_mask = GENMASK(3, 0), .irq_flags = IRQF_TRIGGER_FALLING, + .num_resetclks = 40, }; static const struct ad_sd_calib_data ad7192_calib_arr[8] = { @@ -565,7 +567,7 @@ static int ad7192_setup(struct iio_dev *indio_dev, struct device *dev) int i, ret, id; /* reset the serial interface */ - ret = ad_sd_reset(&st->sd, 48); + ret = ad_sd_reset(&st->sd); if (ret < 0) return ret; usleep_range(500, 1000); /* Wait for at least 500us */ diff --git a/drivers/iio/adc/ad7791.c b/drivers/iio/adc/ad7791.c index 86effe8501b4..c7509b911835 100644 --- a/drivers/iio/adc/ad7791.c +++ b/drivers/iio/adc/ad7791.c @@ -254,6 +254,7 @@ static const struct ad_sigma_delta_info ad7791_sigma_delta_info = { .addr_shift = 4, .read_mask = BIT(3), .irq_flags = IRQF_TRIGGER_FALLING, + .num_resetclks = 32, }; static int ad7791_read_raw(struct iio_dev *indio_dev, diff --git a/drivers/iio/adc/ad7793.c b/drivers/iio/adc/ad7793.c index abebd519cafa..0767c56bb442 100644 --- a/drivers/iio/adc/ad7793.c +++ b/drivers/iio/adc/ad7793.c @@ -206,6 +206,7 @@ static const struct ad_sigma_delta_info ad7793_sigma_delta_info = { .addr_shift = 3, .read_mask = BIT(6), .irq_flags = IRQF_TRIGGER_FALLING, + .num_resetclks = 32, }; static const struct ad_sd_calib_data ad7793_calib_arr[6] = { @@ -265,7 +266,7 @@ static int ad7793_setup(struct iio_dev *indio_dev, return ret; /* reset the serial interface */ - ret = ad_sd_reset(&st->sd, 32); + ret = ad_sd_reset(&st->sd); if (ret < 0) goto out; usleep_range(500, 2000); /* Wait for at least 500us */ diff --git a/drivers/iio/adc/ad_sigma_delta.c b/drivers/iio/adc/ad_sigma_delta.c index 9abde276cd17..9891346c2d73 100644 --- a/drivers/iio/adc/ad_sigma_delta.c +++ b/drivers/iio/adc/ad_sigma_delta.c @@ -178,13 +178,12 @@ EXPORT_SYMBOL_NS_GPL(ad_sd_read_reg, IIO_AD_SIGMA_DELTA); * ad_sd_reset() - Reset the serial interface * * @sigma_delta: The sigma delta device - * @reset_length: Number of SCLKs with DIN = 1 * * Returns 0 on success, an error code otherwise. **/ -int ad_sd_reset(struct ad_sigma_delta *sigma_delta, - unsigned int reset_length) +int ad_sd_reset(struct ad_sigma_delta *sigma_delta) { + unsigned int reset_length = sigma_delta->info->num_resetclks; uint8_t *buf; unsigned int size; int ret; diff --git a/include/linux/iio/adc/ad_sigma_delta.h b/include/linux/iio/adc/ad_sigma_delta.h index f86eca6126b4..eef87d04eb6b 100644 --- a/include/linux/iio/adc/ad_sigma_delta.h +++ b/include/linux/iio/adc/ad_sigma_delta.h @@ -54,6 +54,7 @@ struct iio_dev; * @irq_flags: flags for the interrupt used by the triggered buffer * @num_slots: Number of sequencer slots * @irq_line: IRQ for reading conversions. If 0, spi->irq will be used + * @num_resetclks: Number of SPI clk cycles with MOSI=1 to reset the chip. */ struct ad_sigma_delta_info { int (*set_channel)(struct ad_sigma_delta *, unsigned int channel); @@ -70,6 +71,7 @@ struct ad_sigma_delta_info { unsigned long irq_flags; unsigned int num_slots; int irq_line; + unsigned int num_resetclks; }; /** @@ -181,8 +183,7 @@ int ad_sd_write_reg(struct ad_sigma_delta *sigma_delta, unsigned int reg, int ad_sd_read_reg(struct ad_sigma_delta *sigma_delta, unsigned int reg, unsigned int size, unsigned int *val); -int ad_sd_reset(struct ad_sigma_delta *sigma_delta, - unsigned int reset_length); +int ad_sd_reset(struct ad_sigma_delta *sigma_delta); int ad_sigma_delta_single_conversion(struct iio_dev *indio_dev, const struct iio_chan_spec *chan, int *val); From patchwork Wed Nov 27 14:59:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= X-Patchwork-Id: 13887107 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.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 70A7D1E1041 for ; Wed, 27 Nov 2024 15:00:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732719621; cv=none; b=RYJ03/uM8SpUnD07TdjG8Mx+mkR67jKyI9j38vdWBfEhc2ABm1/wxk929fu+DgYVyQ69l3iaHwMY9o/3eV+sRHLpWfMj9a6gEPaOAubussI0naKCbF/sqhLdiinOw0zob7agltKbNeEVtWmiE3mh7aVxZCfd2EGo/mO/BiFcDbQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732719621; c=relaxed/simple; bh=XOnp+zMNl2112dcr6t0bRq9UBNGqYsZvMxbCUkajCRE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=bvQlYbLJOUPgBA16uLa/+G54kUbk9YCQ4woUCUFjnTd48u4UH9NVBpBNEWdXleVorm8wDlzQfcCh337iISGB/CspTvWaiwoctVIQaZd8bTZIstNpfAaEVed/iuZB/HBuk4AJPV4rrgDO1r/MSKr8JzFaPHJeUZRI60qb0gc5+Ew= 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=HFsMx7CP; arc=none smtp.client-ip=209.85.128.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="HFsMx7CP" Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-434a14d6bf4so26804185e9.1 for ; Wed, 27 Nov 2024 07:00:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1732719618; x=1733324418; 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=8xjbyqC0IEz1wor4H0WJlxb93JVQsScZqDo+jLntdmQ=; b=HFsMx7CPhVxOcR97xYlvOR9n4iHiU5lGKB1RxWwr1wB4oy7Djpbvb1e4jzpe4UIh9O tyT5LrzXxoxpNqgm3cGYbVqETzFZKABnb09NWA2GbkdiY4TZ1oY83OZwAG6SA1nlqGcs b/Cm+GUfjwgMB1zNs9dQy8xfJ89kCFbpwH5oN9hqsyF00uNDv8ap90AbQ3srH/5NJGqF dMovr9Tiym7oDZ6z4BlmbjB2lf+HmlzzTwQ28GFwEoGAodMgR1obJHsY1XsYp3Abq/Ee VbbGVRdLa+L/vJZnTPknDpaBgRL7xud++08h12+h8/eRaDvLy2NyFD93ICegz+ilPBxe s3XA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732719618; x=1733324418; 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=8xjbyqC0IEz1wor4H0WJlxb93JVQsScZqDo+jLntdmQ=; b=XddCzwEugEfD0F9saXZL5QDhTIITieM6vXlXZBTD47Uy434yY1eAW8iLAk5OOHHMb9 zjlRpWYHwjkzEnuFyjnpAf0iMIyjOhMP9e3f/TFSP8P7lqARN/MiwjWXwwe6NGhT7Ag+ uekCOLn4WcIBHrRb4CvJmvqf+M1Jek6DRd32s/hl5YF0Pi8vy+qqrBHV1Ku20zN3zo2N P06yfY9yRd56J1IRhAzIgc7fPlnNHwLMqmex9IsGq18bQ6iJdEkwzA03RsWKlG+gL2iw 8iPDIB1geN12C5ngLfoOniNbIdVYLHr58b+PzKNirqk0H9fp8KeKIWhn2XBC4///Yn9I 9FhA== X-Forwarded-Encrypted: i=1; AJvYcCXI8waKMst0ARhH6YTdjHV3zKf1ITGUs4mQNuO/cwGoQ7U+NvXC9V8Yfgt90UtUsFKsr53knln3zns=@vger.kernel.org X-Gm-Message-State: AOJu0YxAavGFiSKUyx9tSK91Syc7Rhe1zbze57LIoTWpvl5fKpMcg7eg OAWA0muXP0BVxK8c6BJoy/sJ502TFNoeJoq/IMHhZK+C4CyC5UcCgaYnXd5kwTM= X-Gm-Gg: ASbGncs6OApLFI6G1lBtbc9UeyAHK2FGipV55gVC4mXwAIFel6MoiPkJ/25kwxytySj W7dCkJi853/4N5gtJaKcV5qapar0MmH+XXf7crsyuP1/qAQo9RjMjGSbKOQpwSoBgW9247XF2ye /+wwYSh0vF3iHVhl9imS9/Cb/zQDExRz2bdBubiUz7X7NiQ4t7Ri2DCSTRVtJPIFJTEEufnkDYk tKZyDEvXdcGT6MD6o5xjgH/8q26fZbVmG+To8xTNDrLQod8KfAVovnqNd5G4xpA6rTTREVUAa2S 5iBM X-Google-Smtp-Source: AGHT+IHJRDTrOjqwpoS5LZ7sWn9/h0f3B3JZFqZGpHqNIUxHGaVVSGM+/UgcLO46GTxOsXzdTPP9Pg== X-Received: by 2002:a05:600c:4e8b:b0:431:93d8:e1a1 with SMTP id 5b1f17b1804b1-434a9df1f34mr30459855e9.27.1732719617868; Wed, 27 Nov 2024 07:00:17 -0800 (PST) Received: from localhost (p5dc6838f.dip0.t-ipconnect.de. [93.198.131.143]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3825fafe98asm16384441f8f.39.2024.11.27.07.00.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Nov 2024 07:00:17 -0800 (PST) From: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= To: Jonathan Cameron Cc: Lars-Peter Clausen , Michael Hennerich , linux-iio@vger.kernel.org, Andy Shevchenko , David Lechner , Trevor Gamblin , Nuno Sa Subject: [PATCH v4 08/10] iio: adc: ad_sigma_delta: Check for previous ready signals Date: Wed, 27 Nov 2024 15:59:37 +0100 Message-ID: <20241127145929.679408-20-u.kleine-koenig@baylibre.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241127145929.679408-12-u.kleine-koenig@baylibre.com> References: <20241127145929.679408-12-u.kleine-koenig@baylibre.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4634; i=u.kleine-koenig@baylibre.com; h=from:subject; bh=XOnp+zMNl2112dcr6t0bRq9UBNGqYsZvMxbCUkajCRE=; b=owEBbQGS/pANAwAKAY+A+1h9Ev5OAcsmYgBnRzPneMJMUFU7EF1rUfVxKnzsAm/GrW45ts0nD xRMoS6/5XKJATMEAAEKAB0WIQQ/gaxpOnoeWYmt/tOPgPtYfRL+TgUCZ0cz5wAKCRCPgPtYfRL+ TrOtCACZ0QKMUI9R6nRj3E5ETGz2mqilAV14a7aA2NdorFLJ2tFdbfPO19aKaqhLgWZpzNw2iBg 37aNgtGzLK5OIgclmPc7XcMbhiOsMI+BRdmzxBkYbL2XiCxpus6cFZU92SfXhoNXiDr9L29ZeGP UkNtNXs0bas4CMZVMx4nS3+B5hmVVjlP4CTX3RsvwJSMnR7pILVt9zmf1zNRTo9e1MFYkM7nKMa 4Drz67+UcAxHiBSKa+YbaNk/owE0SNyQaTT4YTiBcotdgvw3DQN7cHFWgPDIpBW+inG8/gbv/lI +ptgk9LNpM6m7y4XIkHCaHUo8vWMCMiJ2nA1+GwWFSriFZaL X-Developer-Key: i=u.kleine-koenig@baylibre.com; a=openpgp; fpr=0D2511F322BFAB1C1580266BE2DCDD9132669BD6 It can happen if a previous conversion was aborted the ADC pulls down the ̅R̅D̅Y line but the event wasn't handled before. In that case enabling the irq might immediately fire (depending on the irq controller's capabilities) and even with a rdy-gpio isn't identified as an unrelated one. To cure that problem check for a pending event before the measurement is started and clear it if needed. Signed-off-by: Uwe Kleine-König --- drivers/iio/adc/ad_sigma_delta.c | 89 ++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/drivers/iio/adc/ad_sigma_delta.c b/drivers/iio/adc/ad_sigma_delta.c index 9891346c2d73..164a4d0b571d 100644 --- a/drivers/iio/adc/ad_sigma_delta.c +++ b/drivers/iio/adc/ad_sigma_delta.c @@ -29,8 +29,11 @@ #define AD_SD_COMM_CHAN_MASK 0x3 #define AD_SD_REG_COMM 0x00 +#define AD_SD_REG_STATUS 0x00 #define AD_SD_REG_DATA 0x03 +#define AD_SD_REG_STATUS_RDY 0x80 + /** * ad_sd_set_comm() - Set communications register * @@ -222,6 +225,80 @@ static void ad_sd_enable_irq(struct ad_sigma_delta *sigma_delta) enable_irq(sigma_delta->irq_line); } +/* Called with `sigma_delta->bus_locked == true` only. */ +static int ad_sigma_delta_clear_pending_event(struct ad_sigma_delta *sigma_delta) +{ + bool pending_event; + unsigned int data_read_len = BITS_TO_BYTES(sigma_delta->info->num_resetclks); + u8 data[9]; + struct spi_transfer t[] = { + { + .tx_buf = data, + .len = 1, + }, { + .tx_buf = data + 1, + .len = data_read_len, + } + }; + struct spi_message m; + + /* + * read RDY pin (if possible) or status register to check if there is an + * old event. + */ + if (sigma_delta->rdy_gpiod) { + pending_event = gpiod_get_value(sigma_delta->rdy_gpiod); + } else { + int ret; + unsigned status_reg; + + ret = ad_sd_read_reg(sigma_delta, AD_SD_REG_STATUS, 1, &status_reg); + if (ret) + return ret; + + pending_event = !(status_reg & AD_SD_REG_STATUS_RDY); + } + + if (!pending_event) + return 0; + + /* + * In general the size of the data register is unknown. It varies from + * device to device, might be one byte longer if CONTROL.DATA_STATUS is + * set and even varies on some devices depending on which input is + * selected. So send one byte to start reading the data register and + * then just clock for some bytes with DIN (aka MOSI) high to not + * confuse the register access state machine after the data register was + * completely read. Note however that the sequence length must be + * shorter than the reset procedure. + */ + + /* Oh, back out instead of overflowing data[] */ + if (data_read_len > sizeof(data) - 1) + return -EINVAL; + + spi_message_init(&m); + if (sigma_delta->info->has_registers) { + unsigned int data_reg = sigma_delta->info->data_reg ?: AD_SD_REG_DATA; + + data[0] = data_reg << sigma_delta->info->addr_shift; + data[0] |= sigma_delta->info->read_mask; + data[0] |= sigma_delta->comm; + spi_message_add_tail(&t[0], &m); + } + + /* + * The first transferred byte is part of the real data register, + * so this doesn't need to be 0xff. In the remaining + * `data_read_len - 1` bytes are less than $num_resetclks ones. + */ + data[1] = 0x00; + memset(data + 2, 0xff, data_read_len - 1); + spi_message_add_tail(&t[1], &m); + + return spi_sync_locked(sigma_delta->spi, &m); +} + int ad_sd_calibrate(struct ad_sigma_delta *sigma_delta, unsigned int mode, unsigned int channel) { @@ -237,6 +314,10 @@ int ad_sd_calibrate(struct ad_sigma_delta *sigma_delta, sigma_delta->keep_cs_asserted = true; reinit_completion(&sigma_delta->completion); + ret = ad_sigma_delta_clear_pending_event(sigma_delta); + if (ret) + return ret; + ret = ad_sigma_delta_set_mode(sigma_delta, mode); if (ret < 0) goto out; @@ -310,6 +391,10 @@ int ad_sigma_delta_single_conversion(struct iio_dev *indio_dev, sigma_delta->keep_cs_asserted = true; reinit_completion(&sigma_delta->completion); + ret = ad_sigma_delta_clear_pending_event(sigma_delta); + if (ret) + return ret; + ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_SINGLE); ad_sd_enable_irq(sigma_delta); @@ -406,6 +491,10 @@ static int ad_sd_buffer_postenable(struct iio_dev *indio_dev) sigma_delta->bus_locked = true; sigma_delta->keep_cs_asserted = true; + ret = ad_sigma_delta_clear_pending_event(sigma_delta); + if (ret) + return ret; + ret = ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_CONTINUOUS); if (ret) goto err_unlock; From patchwork Wed Nov 27 14:59:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= X-Patchwork-Id: 13887108 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.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 F083322F19 for ; Wed, 27 Nov 2024 15:00:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732719623; cv=none; b=cbnWu/z5tWho9vzDYQIomT9dmvhHl4kNOQN/9UtkdU/vbKPn+sAF8nR3+Lt5n50H5gEaJ/iEt39pAQjmyjB00v0LlLysSOTFUge5mc3khCpO4xvVVzrTBh6nqkE9D39eJ8wHvlA1X766DKL3WfXY0qyQAryXsTYOWymYAOe9Xjs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732719623; c=relaxed/simple; bh=yMC8oQsqpS+1AeWzQh+HkzdplhBqgZBlb2Nm13CGO5E=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=bURYC0qtMyyb2LjXvkQiuV4YRFBQ3fs3vg2q9WpBNmCC7IUh10byCm8ITnIakmJ9sZ4lqBo/MQXnqtIWiBmCuaSHH/cri2dv76mHkQnO8znOfTaIQNcFPzrF0k37bgWWE/V5QkxzRp7k6Rae152iz4Lldc6VgWxt7OhaRiIyjsw= 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=ccsCnZGw; arc=none smtp.client-ip=209.85.128.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="ccsCnZGw" Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-434a0fd9778so27711435e9.0 for ; Wed, 27 Nov 2024 07:00:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1732719619; x=1733324419; 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=Srg/Ufkkj8rWTSfunjRyWG6zErlFNDfrLKeOeqz39H4=; b=ccsCnZGwqT4VqbkGmVXvx3/OsAS4F6tD8SXZkQ2avjdPCyxgHA2M+VFn+1YyRaFH0v dGYQ7TIV8YymRdbB22hJ7mGbvKMMa2gIKFiCSCT3p1+/ciXf21DRcgOXzR83VZu5kEbd 98N/ALn6lXXDDleyXWxwCnYghzsBENR5DbvZ2KOV4NAxewfh6DfZJo2DiK0BruAAUtr2 Z0EsorKdLw6H6C6t5OZK4QGxDlbH85lXo9jLK67OUOkw6oeN3Igx3/tp7sLKmmiIJOuj T/k5hziN9RdD6dkRSiv79xnOlvrYFaNd7KVHYpj9tHAvg+t0fW1aSLCqgM6op5mOh+iy 4b+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732719619; x=1733324419; 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=Srg/Ufkkj8rWTSfunjRyWG6zErlFNDfrLKeOeqz39H4=; b=HxOclbibMp0rt7kzLGe/xTSh/OQydCnZ1G6yXpE1s2O6HMZSLdPAAZHKdskRsdl/CI qq6tUsdgEojlrzEj7VU9wPKKGg2PpWxYYR7iTZrYMBWeyCd8m0RJIrbVV6sh0NZlzduI IXosgYRC3kKejym6yWAyYpZqFqRxG1Z3yzxFU+hYbye0qsxpP+pBJ04l6qyyXo2/n+Z4 SPrnxrWXbLqEdFGtqFGImTxinFSmlRWND0T2nJdolB2cx+2WSatQ3mp2kqDcndIC8OLT qFj7y69ePO6F7m3YOoN1rncwZ9H4383r5X9g7eB2vHrKAKOYeFpmVd6QGWyzPMghbkLD fHIQ== X-Forwarded-Encrypted: i=1; AJvYcCU5sJu7DUKzlU0Yi18+SQVLWhfx6Dk+KoFjbsP45POH9cNykvOlO1U6KJYK4LJ5pnQqPfSj2Q+3RpE=@vger.kernel.org X-Gm-Message-State: AOJu0Yx/FJ5IhjNyFKLSHvIZnuWRCgRlGZ/Zb2ROIY9Ky1i4H7JbPoRu /2KfrVZWVB6IPcWL+5GFz0APxE1Q6Qo/UoI68SVAHr0puAF9btf9A04qntDnsXY= X-Gm-Gg: ASbGncsPFRylkX228/Hq+3sHZRKV/j87oV2IgDcHzZ0yxHyMZaiZ0xQSjnI7PAVRjPv GUZ0s/c8j9pFdlOT9CDjyg8dNLwuC6APRrsBKiWK5nndkg8Kx3m8Fh9muSO7xLBY+ZsBaeCZMOX uZsz4b+NdxM2WVQFrZ+xiLnWs7ruDesdoXXAcyBEELjho7Zq5fWbVHNEeQWnySuebKftpTNZKJh 6mdYqBkEmhIDEXzyRftaFgJR+0+GNoqjWzs8uirhz8xkVfx9flXvKPEdlSxMsAdzms95c+im8Bp XqXG X-Google-Smtp-Source: AGHT+IF5axebCRwBuEQ3JhIYMmZij6PxuI3EWl9eu3ZvHctGZaWrtYzGuI0jHdZz/5RjtpH3aCwB1g== X-Received: by 2002:a05:600c:3596:b0:434:a386:6cf with SMTP id 5b1f17b1804b1-434a9dbbc00mr30559655e9.2.1732719619323; Wed, 27 Nov 2024 07:00:19 -0800 (PST) Received: from localhost (p5dc6838f.dip0.t-ipconnect.de. [93.198.131.143]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-434aa7e4d42sm23279815e9.37.2024.11.27.07.00.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Nov 2024 07:00:18 -0800 (PST) From: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= To: Jonathan Cameron Cc: Lars-Peter Clausen , Michael Hennerich , linux-iio@vger.kernel.org, Andy Shevchenko , David Lechner , Trevor Gamblin , Nuno Sa Subject: [PATCH v4 09/10] iio: adc: ad7124: Add error reporting during probe Date: Wed, 27 Nov 2024 15:59:38 +0100 Message-ID: <20241127145929.679408-21-u.kleine-koenig@baylibre.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241127145929.679408-12-u.kleine-koenig@baylibre.com> References: <20241127145929.679408-12-u.kleine-koenig@baylibre.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=7836; i=u.kleine-koenig@baylibre.com; h=from:subject; bh=yMC8oQsqpS+1AeWzQh+HkzdplhBqgZBlb2Nm13CGO5E=; b=owEBbQGS/pANAwAKAY+A+1h9Ev5OAcsmYgBnRzPqde8OlZgEfcrMj39TnJDm/UoufDLQ7LYBv iTRi7FUkoCJATMEAAEKAB0WIQQ/gaxpOnoeWYmt/tOPgPtYfRL+TgUCZ0cz6gAKCRCPgPtYfRL+ Th8kB/9lArYNHBOknpasZVAJAYa1RWdgeoD8lz+SykBAfD/66Iwa98G0qcc4W7Y3RYcAKhGjxgT OS0neu1ajv/bwMSGvYodT5uHpLz64k10uJ/rbUqfDIfmatcoThbp0wDuE2lZyQ2uOSsSWXWKMFC l/rjG9r30tcTHogHIaThIqnca3NDpxyKXnVZNSXUdIVTpZIx/pAJSrQKIAFaGZl4e3B7FirGGcd CCj2EdVOGjtmF4K3RcBxbcFEVMLC0RN0odQ631JmDs47c/WcvCKwPjOmDiMjGbdq8sCKi8gROcO H1MirgowmMHff7r7oT2OzuQDlHwd5VSM1KOf8jJzpeNwJc0u X-Developer-Key: i=u.kleine-koenig@baylibre.com; a=openpgp; fpr=0D2511F322BFAB1C1580266BE2DCDD9132669BD6 A driver that silently fails to probe is annoying and hard to debug. So add messages in the error paths of the probe function. Signed-off-by: Uwe Kleine-König --- drivers/iio/adc/ad7124.c | 79 ++++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 36 deletions(-) diff --git a/drivers/iio/adc/ad7124.c b/drivers/iio/adc/ad7124.c index b17c3dbeaeba..50b8ffa2dbe5 100644 --- a/drivers/iio/adc/ad7124.c +++ b/drivers/iio/adc/ad7124.c @@ -360,20 +360,21 @@ static int ad7124_find_free_config_slot(struct ad7124_state *st) return free_cfg_slot; } +/* Only called during probe, so dev_err_probe() can be used */ static int ad7124_init_config_vref(struct ad7124_state *st, struct ad7124_channel_config *cfg) { + struct device *dev = &st->sd.spi->dev; unsigned int refsel = cfg->refsel; switch (refsel) { case AD7124_REFIN1: case AD7124_REFIN2: case AD7124_AVDD_REF: - if (IS_ERR(st->vref[refsel])) { - dev_err(&st->sd.spi->dev, - "Error, trying to use external voltage reference without a %s regulator.\n", - ad7124_ref_names[refsel]); - return PTR_ERR(st->vref[refsel]); - } + if (IS_ERR(st->vref[refsel])) + return dev_err_probe(dev, PTR_ERR(st->vref[refsel]), + "Error, trying to use external voltage reference without a %s regulator.\n", + ad7124_ref_names[refsel]); + cfg->vref_mv = regulator_get_voltage(st->vref[refsel]); /* Conversion from uV to mV */ cfg->vref_mv /= 1000; @@ -384,8 +385,7 @@ static int ad7124_init_config_vref(struct ad7124_state *st, struct ad7124_channe st->adc_control |= AD7124_ADC_CTRL_REF_EN(1); return 0; default: - dev_err(&st->sd.spi->dev, "Invalid reference %d\n", refsel); - return -EINVAL; + return dev_err_probe(dev, -EINVAL, "Invalid reference %d\n", refsel); } } @@ -752,8 +752,10 @@ static const struct iio_info ad7124_info = { .attrs = &ad7124_attrs_group, }; +/* Only called during probe, so dev_err_probe() can be used */ static int ad7124_soft_reset(struct ad7124_state *st) { + struct device *dev = &st->sd.spi->dev; unsigned int readval, timeout; int ret; @@ -766,7 +768,7 @@ static int ad7124_soft_reset(struct ad7124_state *st) do { ret = ad_sd_read_reg(&st->sd, AD7124_STATUS, 1, &readval); if (ret < 0) - return ret; + return dev_err_probe(dev, ret, "Error reading status register\n"); if (!(readval & AD7124_STATUS_POR_FLAG_MSK)) return 0; @@ -775,35 +777,30 @@ static int ad7124_soft_reset(struct ad7124_state *st) usleep_range(100, 2000); } while (--timeout); - dev_err(&st->sd.spi->dev, "Soft reset failed\n"); - - return -EIO; + return dev_err_probe(dev, -EIO, "Soft reset failed\n"); } static int ad7124_check_chip_id(struct ad7124_state *st) { + struct device *dev = &st->sd.spi->dev; unsigned int readval, chip_id, silicon_rev; int ret; ret = ad_sd_read_reg(&st->sd, AD7124_ID, 1, &readval); if (ret < 0) - return ret; + return dev_err_probe(dev, ret, "Failure to read ID register\n"); chip_id = AD7124_DEVICE_ID_GET(readval); silicon_rev = AD7124_SILICON_REV_GET(readval); - if (chip_id != st->chip_info->chip_id) { - dev_err(&st->sd.spi->dev, - "Chip ID mismatch: expected %u, got %u\n", - st->chip_info->chip_id, chip_id); - return -ENODEV; - } + if (chip_id != st->chip_info->chip_id) + return dev_err_probe(dev, -ENODEV, + "Chip ID mismatch: expected %u, got %u\n", + st->chip_info->chip_id, chip_id); - if (silicon_rev == 0) { - dev_err(&st->sd.spi->dev, - "Silicon revision empty. Chip may not be present\n"); - return -ENODEV; - } + if (silicon_rev == 0) + return dev_err_probe(dev, -ENODEV, + "Silicon revision empty. Chip may not be present\n"); return 0; } @@ -862,16 +859,18 @@ static int ad7124_parse_channel_config(struct iio_dev *indio_dev, device_for_each_child_node_scoped(dev, child) { ret = fwnode_property_read_u32(child, "reg", &channel); if (ret) - return ret; + return dev_err_probe(dev, ret, + "Failed to parse reg property of %pfwP\n", child); if (channel >= indio_dev->num_channels) return dev_err_probe(dev, -EINVAL, - "Channel index >= number of channels\n"); + "Channel index >= number of channels in %pfwP\n", child); ret = fwnode_property_read_u32_array(child, "diff-channels", ain, 2); if (ret) - return ret; + return dev_err_probe(dev, ret, + "Failed to parse diff-channels property of %pfwP\n", child); if (!ad7124_valid_input_select(ain[0], st->chip_info) || !ad7124_valid_input_select(ain[1], st->chip_info)) @@ -908,12 +907,13 @@ static int ad7124_parse_channel_config(struct iio_dev *indio_dev, static int ad7124_setup(struct ad7124_state *st) { + struct device *dev = &st->sd.spi->dev; unsigned int fclk, power_mode; int i, ret; fclk = clk_get_rate(st->mclk); if (!fclk) - return -EINVAL; + return dev_err_probe(dev, -EINVAL, "Failed to get mclk rate\n"); /* The power mode changes the master clock frequency */ power_mode = ad7124_find_closest_match(ad7124_master_clk_freq_hz, @@ -922,7 +922,7 @@ static int ad7124_setup(struct ad7124_state *st) if (fclk != ad7124_master_clk_freq_hz[power_mode]) { ret = clk_set_rate(st->mclk, fclk); if (ret) - return ret; + return dev_err_probe(dev, ret, "Failed to set mclk rate\n"); } /* Set the power mode */ @@ -953,7 +953,7 @@ static int ad7124_setup(struct ad7124_state *st) ret = ad_sd_write_reg(&st->sd, AD7124_ADC_CONTROL, 2, st->adc_control); if (ret < 0) - return ret; + return dev_err_probe(dev, ret, "Failed to setup CONTROL register\n"); return ret; } @@ -966,13 +966,14 @@ static void ad7124_reg_disable(void *r) static int ad7124_probe(struct spi_device *spi) { const struct ad7124_chip_info *info; + struct device *dev = &spi->dev; struct ad7124_state *st; struct iio_dev *indio_dev; int i, ret; info = spi_get_device_match_data(spi); if (!info) - return -ENODEV; + return dev_err_probe(dev, -ENODEV, "Failed to get match data\n"); indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); if (!indio_dev) @@ -1007,36 +1008,42 @@ static int ad7124_probe(struct spi_device *spi) ret = regulator_enable(st->vref[i]); if (ret) - return ret; + return dev_err_probe(dev, ret, "Failed to enable regulator #%d\n", i); ret = devm_add_action_or_reset(&spi->dev, ad7124_reg_disable, st->vref[i]); if (ret) - return ret; + return dev_err_probe(dev, ret, "Failed to register disable handler for regulator #%d\n", i); } st->mclk = devm_clk_get_enabled(&spi->dev, "mclk"); if (IS_ERR(st->mclk)) - return PTR_ERR(st->mclk); + return dev_err_probe(dev, PTR_ERR(st->mclk), "Failed to get mclk\n"); ret = ad7124_soft_reset(st); if (ret < 0) + /* ad7124_soft_reset() already emitted an error message */ return ret; ret = ad7124_check_chip_id(st); if (ret) + /* ad7124_check_chip_id() already emitted an error message */ return ret; ret = ad7124_setup(st); if (ret < 0) + /* ad7124_setup() already emitted an error message */ return ret; ret = devm_ad_sd_setup_buffer_and_trigger(&spi->dev, indio_dev); if (ret < 0) - return ret; + return dev_err_probe(dev, ret, "Failed to setup triggers\n"); - return devm_iio_device_register(&spi->dev, indio_dev); + ret = devm_iio_device_register(&spi->dev, indio_dev); + if (ret < 0) + return dev_err_probe(dev, ret, "Failed to register iio device\n"); + return 0; } static const struct of_device_id ad7124_of_match[] = { From patchwork Wed Nov 27 14:59:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= X-Patchwork-Id: 13887109 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (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 960361442E8 for ; Wed, 27 Nov 2024 15:00:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732719624; cv=none; b=SpUD0Rqu6zTGxfjCVKaJEQiNV9Xe7GSvNqywlDx8d5e9NblUPQA7stL6GSMSDux4VPJpF0IT0SY/2ZwAs4WTBObTQSiFyuKzrl0LfNUid0nrOXraDd9W7tm7dYGtxUlLbcktjW5ESI0MROg0N5k06CQgBmnQT7Nl3ZlDjGFLKN0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732719624; c=relaxed/simple; bh=FybxynUJ1Yba5Z5OIHqiV3cKww2mtZhnPNftNTVUYCk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=fHYOAm3KvdsqTYPa8u6NvlRJj9deXfQQG27myfn1WCCy5w3n+0wX4ksqZ27J6PlFsKeqXvpyo0UYolT4PIyvKbjs756weAlw4f1MnNG+zak5Rlqvy9XL3XhMXf9hPUw1JjscOjOYHuGi1u0aD2P0TNG7DGuHgCwxHQSREtnPH4w= 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=T5BF6rFo; arc=none smtp.client-ip=209.85.128.47 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="T5BF6rFo" Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-434a752140eso15744835e9.3 for ; Wed, 27 Nov 2024 07:00:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1732719621; x=1733324421; 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=91eRYaFTdN1PNWbZ3zglyGnbkpzfISIP1MPsAUMjxGw=; b=T5BF6rFoTXoi80szurEsPTKa76GKvaFn+FhRIiAAZ1tCdjRk0ylwzO/YK/40h5juzb DfhyIsjkEgPs5/9WT5F5kr586bZ76q5uWTJnvhhTRkoJxpIzS+158qFnra2tsuZ+Z+ty uTGtWlNt3WEhLBnufplplczw2U+768P4mpEDSA6Zgn9dcNzV/jo/do2tnFARpABtE6Qh HimniABAdW7JLlwdt28A4A+lzUg80nkghIQfu5/SVxerxNIVN7jVW/hLlGlAXHLKVtG8 abFyJK9IAojrPY4fFHCh5idcrpgEwAn8jhCuxvTAdlkHp0xA+wOIs/y7ZKn0gbYq2VDZ Wtww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732719621; x=1733324421; 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=91eRYaFTdN1PNWbZ3zglyGnbkpzfISIP1MPsAUMjxGw=; b=b0dFDGaYrKuRV6ctIDcp4VVVqECct46f0BDybTqd2kNYUWKzjAroHGAphlpfNNCQEp XSYAVPW0N9Xoddo9JsxWFCAs5qftXdAtFM4bGNSt1AL7DLXOyoq9ewM0nRRRFc2wui5b xlzJZ2FLXox9DU95P32gUrAGe5gVEAFLJ6VK+uJbveVmk/u18iC3PnBSbkbBpUmb8v3X stSCcYJnGIGZdfl6JCFQK5wYxzgIuSj5zHf8vINpLORsuTdQyHEZL2Fp3aBSKp7+L+Ey QClMTtRRX4eO7M7CfuBKBSIMQ62fLTMc2SHxg6dUM9pzZfttCEZEu5DJcT4R3kFHVUuq S57g== X-Forwarded-Encrypted: i=1; AJvYcCV6KyEwrNgEYN6S5ENhaXlaMYZqnM4nbYdXk+HmD55bjcIrNbpTvQVqiR92huJu/hQ/eQapZ5apQFU=@vger.kernel.org X-Gm-Message-State: AOJu0YzJIeXKBkFNRnDhXQiD+4xAUT/jf9znXSmKsigLvdyHZPLTz066 uTP+t4U9HhnSbghHicdRBJ5WvOq+w4/VUce4uFBtHaqk4TOai17xHQagmGfyMxY= X-Gm-Gg: ASbGnctIWwwx+7HnO7bNF/1JxYC2ayfSDj7oRn8pJc6oFyfmESmRqz1cmflKrFxaDfT DWMDSnKnrLDPv0AGDzo5rhfK3gtiuXcgmTY/c2Ean6Zj6jHKrUfuLRczDuVu9p58rcHDLdvfTNa DtzD6cWmT1D0vMA/yl7O3vc3az9BOulwIw/6RDCN0I9Rap0D0WWMIZwkF3D9Nu2v38JktV2flxt lfHoz8E/5zhOXK92r0uNezVvpDEtIXGR43EVy/HA0jkdqSZnWmBSBmxfcpXGv6a7T5Aqqbmrmha I1o6 X-Google-Smtp-Source: AGHT+IEon23hwGon37Cs26pApOor+Kj7RGM+9zcYxjFXNvz1Qgsn/I1bC/HGKwvjRCGfevQzKXiHlQ== X-Received: by 2002:a05:600c:5012:b0:434:a396:9474 with SMTP id 5b1f17b1804b1-434a9dd0586mr32853085e9.18.1732719620846; Wed, 27 Nov 2024 07:00:20 -0800 (PST) Received: from localhost (p5dc6838f.dip0.t-ipconnect.de. [93.198.131.143]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-434a5d64d28sm39762705e9.0.2024.11.27.07.00.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Nov 2024 07:00:20 -0800 (PST) From: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= To: Jonathan Cameron Cc: Lars-Peter Clausen , Michael Hennerich , linux-iio@vger.kernel.org, Andy Shevchenko , David Lechner , Trevor Gamblin , Nuno Sa Subject: [PATCH v4 10/10] iio: adc: ad7124: Implement temperature measurement Date: Wed, 27 Nov 2024 15:59:39 +0100 Message-ID: <20241127145929.679408-22-u.kleine-koenig@baylibre.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241127145929.679408-12-u.kleine-koenig@baylibre.com> References: <20241127145929.679408-12-u.kleine-koenig@baylibre.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=5420; i=u.kleine-koenig@baylibre.com; h=from:subject; bh=FybxynUJ1Yba5Z5OIHqiV3cKww2mtZhnPNftNTVUYCk=; b=owEBbQGS/pANAwAKAY+A+1h9Ev5OAcsmYgBnRzPscGpDvRPiqpsUIoTHWFah0zv2tvCLpVYIQ LJJqqepVvOJATMEAAEKAB0WIQQ/gaxpOnoeWYmt/tOPgPtYfRL+TgUCZ0cz7AAKCRCPgPtYfRL+ TjWGCACLBPyFFaGtq1NkOPlVCWF2b5QeUHyVHVnDAU0cQNMunlAcWMZfY1m2/NAydhHrejLohUq yLCbIskwKweT92dwHk9DGkoaQlBc5uQYabFRL4urnCVjTWopYxvt7M08CDqC5PGAhWmls+EDSOD NOhX787as6eJCSc4+7Quyn3VVNk/sErFafb72VpKG/wMVpG9S7f8Pa3W7UUdcLUqPQPhdZt7Taq Ct6WhBfHT6wFTTIwjWUZQ09AA5m8ZO0XlhBPocXZ9grctIRb04e6ynVAMMWSz1LLbKmwyKbYv4M nJEbGETZSESdcZlIBw2rLzE1HIWER7r17ak1sUVJT6JQatqr X-Developer-Key: i=u.kleine-koenig@baylibre.com; a=openpgp; fpr=0D2511F322BFAB1C1580266BE2DCDD9132669BD6 If the maximal count of channels the driver supports isn't fully utilized, add an attribute providing the internal temperature. Signed-off-by: Uwe Kleine-König --- drivers/iio/adc/ad7124.c | 112 +++++++++++++++++++++++++++++++-------- 1 file changed, 91 insertions(+), 21 deletions(-) diff --git a/drivers/iio/adc/ad7124.c b/drivers/iio/adc/ad7124.c index 50b8ffa2dbe5..3b95e5a22473 100644 --- a/drivers/iio/adc/ad7124.c +++ b/drivers/iio/adc/ad7124.c @@ -95,6 +95,10 @@ #define AD7124_MAX_CONFIGS 8 #define AD7124_MAX_CHANNELS 16 +/* AD7124 input sources */ +#define AD7124_INPUT_TEMPSENSOR 16 +#define AD7124_INPUT_AVSS 17 + enum ad7124_ids { ID_AD7124_4, ID_AD7124_8, @@ -589,26 +593,59 @@ static int ad7124_read_raw(struct iio_dev *indio_dev, return IIO_VAL_INT; case IIO_CHAN_INFO_SCALE: - mutex_lock(&st->cfgs_lock); + switch (chan->type) { + case IIO_VOLTAGE: + mutex_lock(&st->cfgs_lock); - idx = st->channels[chan->address].cfg.pga_bits; - *val = st->channels[chan->address].cfg.vref_mv; - if (st->channels[chan->address].cfg.bipolar) - *val2 = chan->scan_type.realbits - 1 + idx; - else - *val2 = chan->scan_type.realbits + idx; + idx = st->channels[chan->address].cfg.pga_bits; + *val = st->channels[chan->address].cfg.vref_mv; + if (st->channels[chan->address].cfg.bipolar) + *val2 = chan->scan_type.realbits - 1 + idx; + else + *val2 = chan->scan_type.realbits + idx; + + mutex_unlock(&st->cfgs_lock); + return IIO_VAL_FRACTIONAL_LOG2; + + case IIO_TEMP: + /* + * According to the data sheet + * Temperature (°C) + * = ((Conversion − 0x800000)/13584) − 272.5 + * = (Conversion − 0x800000 - 13584 * 272.5) / 13584 + * = (Conversion − 12090248) / 13584 + * So scale with 1000/13584 to yield °mC. Reduce by 8 to + * 125/1698. + */ + *val = 125; + *val2 = 1698; + return IIO_VAL_FRACTIONAL; + + default: + return -EINVAL; + } - mutex_unlock(&st->cfgs_lock); - return IIO_VAL_FRACTIONAL_LOG2; case IIO_CHAN_INFO_OFFSET: - mutex_lock(&st->cfgs_lock); - if (st->channels[chan->address].cfg.bipolar) - *val = -(1 << (chan->scan_type.realbits - 1)); - else - *val = 0; + switch (chan->type) { + case IIO_VOLTAGE: + mutex_lock(&st->cfgs_lock); + if (st->channels[chan->address].cfg.bipolar) + *val = -(1 << (chan->scan_type.realbits - 1)); + else + *val = 0; + + mutex_unlock(&st->cfgs_lock); + return IIO_VAL_INT; + + case IIO_TEMP: + /* see calculation above */ + *val = -12090248; + return IIO_VAL_INT; + + default: + return -EINVAL; + } - mutex_unlock(&st->cfgs_lock); - return IIO_VAL_INT; case IIO_CHAN_INFO_SAMP_FREQ: mutex_lock(&st->cfgs_lock); *val = st->channels[chan->address].cfg.odr; @@ -826,11 +863,10 @@ static int ad7124_parse_channel_config(struct iio_dev *indio_dev, struct ad7124_channel *channels; struct iio_chan_spec *chan; unsigned int ain[2], channel = 0, tmp; + unsigned int num_channels; int ret; - st->num_channels = device_get_child_node_count(dev); - if (!st->num_channels) - return dev_err_probe(dev, -ENODEV, "no channel children\n"); + num_channels = device_get_child_node_count(dev); /* * The driver assigns each logical channel defined in the device tree @@ -839,9 +875,12 @@ static int ad7124_parse_channel_config(struct iio_dev *indio_dev, * CHANNEL_15) as an additional channel register. The driver could be * improved to lift this limitation. */ - if (st->num_channels > AD7124_MAX_CHANNELS) + if (num_channels > AD7124_MAX_CHANNELS) return dev_err_probe(dev, -EINVAL, "Too many channels defined\n"); + /* Add one for temperature */ + st->num_channels = min(num_channels + 1, AD7124_MAX_CHANNELS); + chan = devm_kcalloc(indio_dev->dev.parent, st->num_channels, sizeof(*chan), GFP_KERNEL); if (!chan) @@ -862,7 +901,7 @@ static int ad7124_parse_channel_config(struct iio_dev *indio_dev, return dev_err_probe(dev, ret, "Failed to parse reg property of %pfwP\n", child); - if (channel >= indio_dev->num_channels) + if (channel >= num_channels) return dev_err_probe(dev, -EINVAL, "Channel index >= number of channels in %pfwP\n", child); @@ -902,6 +941,37 @@ static int ad7124_parse_channel_config(struct iio_dev *indio_dev, chan[channel].channel2 = ain[1]; } + if (num_channels < AD7124_MAX_CHANNELS) { + st->channels[num_channels] = (struct ad7124_channel) { + .nr = num_channels, + .ain = AD7124_CHANNEL_AINP(AD7124_INPUT_TEMPSENSOR) | + AD7124_CHANNEL_AINM(AD7124_INPUT_AVSS), + .cfg = { + .bipolar = true, + }, + }; + + chan[num_channels] = (struct iio_chan_spec) { + .type = IIO_TEMP, + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | + BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET) | + BIT(IIO_CHAN_INFO_SAMP_FREQ), + .scan_type = { + /* + * You might find it strange that a bipolar + * measurement yields an unsigned value, but + * this matches the device's manual. + */ + .sign = 'u', + .realbits = 24, + .storagebits = 32, + .endianness = IIO_BE, + }, + .address = num_channels, + .scan_index = num_channels, + }; + }; + return 0; }