From patchwork Wed Feb 12 10:53:22 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Uwe Kleine-Konig X-Patchwork-Id: 13971448 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 EEA6020896D for ; Wed, 12 Feb 2025 10:53:44 +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=1739357627; cv=none; b=Evut27MaWJvhrMMb5QZEX7R2A/SaJxhH6lssSnS/ibx12+HKVwCOSe0sGnRDCEVJSugSs04VjJEYaGkAnvy5SnS5GwpUUDmqHiJQXUfVPzFw3SttZuqU1eN3vBuqzS6CIOwogS0WerATyCS1syS79dzp8utTcEFYJ2jJK6Zwig4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739357627; c=relaxed/simple; bh=8aGSG1R2/KZiNqMahgYpcUrsHqZlV2GQyxwhFjRne34=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Z67HrFsSiTUBcHkh13GsZ+24rckjNe7EcwLj3d9l26gsFjvYctiecErXL3hc386T6dCHdw4b3ipZlBGzmgi/Tu6gyXBmIh9F/0K7Q7618/rAR7NYotvwxN+gDp2IzPEXSPO87ZlHImS2GzoIgPb6BpT08D8CH9kl9FPa7tFp7q4= 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=B/Nqd+mN; 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="B/Nqd+mN" Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-43934d6b155so4372285e9.1 for ; Wed, 12 Feb 2025 02:53:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1739357623; x=1739962423; 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=AizAf84+FV40x1qFg8hqPNRrmZc6lShv3yme/L08mEs=; b=B/Nqd+mNgwWgY9rehlkzchAy+dX88BXxxKJk03Dq1Qt9CNSImqEUtry3cS6N52jNUE vKKHbgHt0PZ78I3zx87GDlgA4Ii3syb4od42W0NnT0gPbiGwX4x3ielFYHs9C595ZqEY WEc3Kx8znNTPksHcqq4MLyTzFV4zrWy039i2HV/AxppJEZ+TfkJQoGoYTt+CaE4nfcry NkUTBreVAhaGoclEH/2U2P9IYDGSkmxH7GLzNZqkFhYB0sE7CHWqKdR3QbL9j4iwIdwN gwxEIVx2hJGRlDBJbQDgeyDdSgbqwgceMX61sTQw2rNChKlOkeZtS8t+Jr7eQLl6sxob DE3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739357623; x=1739962423; 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=AizAf84+FV40x1qFg8hqPNRrmZc6lShv3yme/L08mEs=; b=BHtGwFP0a33QwkjzhXlWuUqC54HObMMXVbF7a9o2FyC9luque1W5SL+3jrHYEFZTsv rO4r7hwO8GWeQQchYuQNUOK5ccnisNR+BNnZMNHkdYhWXEuHfdgZWKJm3R2eBQ1ocUjZ kxe+67u0p9ciW4QuDOejb/B/UZ6zHzPllnwMlXaU+JyFgSbzDVr76EpPPTtEDhT/JsmT rApTf4S0GlUn2tHb1ZuTpCRt2dWdrNgLzBIaQmgpwlUccxiNjiRdjkY/lXjcqdji9So7 uJC0eO0M8CWgMh6wpfyJQN55ce4uxmwLsaoKDXK+xNrDy/2xVQ74oFNhr82m0ceug/Gw UzaQ== X-Gm-Message-State: AOJu0YyiqeQ0b+HKZ+mJtnnvpIusyu10qGzX/ppYI9xYtpNkGZyPi3SD dNxH9u52SIuq00AVLPIQ+h+WsC0JzpjjtFk7mpWzF34f5qS7kyrDCkneRLxlb3s= X-Gm-Gg: ASbGncsUubI7J7Ys9q1xwet3w07Nuyv7iGcez1ltBVNjWx6AZnjQbehjTBBuG8cfcd5 ZM+8RW3iariLlyMEHzdXh3mPvoov9UIc/X/wv+E8K52RH20d0L9yVanYtfQctVWI0zg9tfBw5X6 q3bDJ5TkXXJud2VFumfcCbNtucASGVaVE+PibdjnxeNgQVmttzncsJXpIzSxanTebNJUxS4rR1v E/TySN9fr51lIlWcJkOV3MQT5xigzTo2EtQvDmHJ7stGB2IXQZAqdR+ORPH/8ndinWrCkM7UP4l jHghzlSj4hCwImFhH8GhpglQBm4yBjce/o8IC5kLCOf9ZCcvkrimkEFstMXkTtuSNAGEqWu+h3o 9Xg== X-Google-Smtp-Source: AGHT+IFIbsKmcWZz+y+kYlL0bxCohZJQJKjbcWIUx5kzQah2pbNSyVkIBMNqG6C8hrQNGe5N/QQIoQ== X-Received: by 2002:a05:600d:e:b0:439:3e90:c54b with SMTP id 5b1f17b1804b1-4394ce622dcmr58532325e9.0.1739357623024; Wed, 12 Feb 2025 02:53:43 -0800 (PST) Received: from localhost (p200300f65f083b04d1ced5eed56c82e8.dip0.t-ipconnect.de. [2003:f6:5f08:3b04:d1ce:d5ee:d56c:82e8]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4395a04f217sm15964605e9.1.2025.02.12.02.53.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Feb 2025 02:53:42 -0800 (PST) From: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= To: Lars-Peter Clausen , Michael Hennerich , Jonathan Cameron Cc: linux-iio@vger.kernel.org Subject: [PATCH v1 1/3] iio: adc: ad7124: Fix comparison of channel configs Date: Wed, 12 Feb 2025 11:53:22 +0100 Message-ID: <20250212105322.10243-6-u.kleine-koenig@baylibre.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250212105322.10243-5-u.kleine-koenig@baylibre.com> References: <20250212105322.10243-5-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=2807; i=u.kleine-koenig@baylibre.com; h=from:subject; bh=8aGSG1R2/KZiNqMahgYpcUrsHqZlV2GQyxwhFjRne34=; b=owEBbQGS/pANAwAKAY+A+1h9Ev5OAcsmYgBnrH2lTmdjBcVT1t0qY4QVi1tYu/xXLx/wE/sId +nLY3s1MXyJATMEAAEKAB0WIQQ/gaxpOnoeWYmt/tOPgPtYfRL+TgUCZ6x9pQAKCRCPgPtYfRL+ Tm5iCACyrUPO18oOSHV8TMsSeSzDqcWOUPCOV4tT7sPXvOq1RbieDyIORyLFZLZTZw/HE8aGsAF 9HL7IAZIuAipkri13U4CXiRGVWF0Q5VAJ4oPYXL+rRopfR8kEbR03tgMbyBeARdouDzRF6mq4oE CR77QmRVDvtKdZCXFzuWr2b2+ey+vSBHtGgu9BaQGi91X0sY+Mxg311Nf58889P8FYWJ8mbVcy6 NRGc7X55AaD+jDvtadXW1T734x+arIXzgHq+c5g0bwCNsCb6hev0nD4RKGMubu1HaiY+2TvoxXI 5Nto/IQxlzPK6azDY2Zb0AGqsGw/9U2dRW9BGgmiP4vJqV7y X-Developer-Key: i=u.kleine-koenig@baylibre.com; a=openpgp; fpr=0D2511F322BFAB1C1580266BE2DCDD9132669BD6 Checking the binary representation of two structs (of the same type) for equality doesn't have the same semantic as comparing all members for equality. The former might find a difference where the latter doesn't in the presence of padding or when ambiguous types like float or bool are involved. (Floats typically have different representations for single values, like -0.0 vs +0.0, or 0.5 * 2² vs 0.25 * 2³. The type bool has at least 8 bits and the raw values 1 and 2 (probably) both evaluate to true, but memcmp finds a difference.) When searching for a channel that already has the configuration we need, the comparison by member is the one that is needed. Convert the comparison accordingly to compare the members one after another. Also add a BUILD_BUG guard to (somewhat) ensure that when struct ad7124_channel_config::config_props is expanded, the comparison is adapted, too. Fixes: 7b8d045e497a ("iio: adc: ad7124: allow more than 8 channels") Signed-off-by: Uwe Kleine-König --- drivers/iio/adc/ad7124.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/drivers/iio/adc/ad7124.c b/drivers/iio/adc/ad7124.c index 6bc418d38820..a664179918fc 100644 --- a/drivers/iio/adc/ad7124.c +++ b/drivers/iio/adc/ad7124.c @@ -338,15 +338,38 @@ static struct ad7124_channel_config *ad7124_find_similar_live_cfg(struct ad7124_ struct ad7124_channel_config *cfg) { struct ad7124_channel_config *cfg_aux; - ptrdiff_t cmp_size; int i; - cmp_size = sizeof_field(struct ad7124_channel_config, config_props); + /* + * This is just to make sure that the comparison is adapted after + * struct ad7124_channel_config was changed. + */ + BUILD_BUG_ON(sizeof_field(struct ad7124_channel_config, config_props) != + sizeof(struct { + enum ad7124_ref_sel refsel; + bool bipolar; + bool buf_positive; + bool buf_negative; + unsigned int vref_mv; + unsigned int pga_bits; + unsigned int odr; + unsigned int odr_sel_bits; + unsigned int filter_type; + })); + for (i = 0; i < st->num_channels; i++) { cfg_aux = &st->channels[i].cfg; if (cfg_aux->live && - !memcmp(&cfg->config_props, &cfg_aux->config_props, cmp_size)) + cfg->refsel == cfg_aux->refsel && + cfg->bipolar == cfg_aux->bipolar && + cfg->buf_positive == cfg_aux->buf_positive && + cfg->buf_negative == cfg_aux->buf_negative && + cfg->vref_mv == cfg_aux->vref_mv && + cfg->pga_bits == cfg_aux->pga_bits && + cfg->odr == cfg_aux->odr && + cfg->odr_sel_bits == cfg_aux->odr_sel_bits && + cfg->filter_type == cfg_aux->filter_type) return cfg_aux; } From patchwork Wed Feb 12 10:53:23 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Uwe Kleine-Konig X-Patchwork-Id: 13971449 Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.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 3949A1FECD2 for ; Wed, 12 Feb 2025 10:53:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739357628; cv=none; b=QvUFgB/NbjLxc3Gjs6qZJ6OEvUarX4keEP1IpklDCLh4i851pX7lwawsAfetnx6VUU9YAoXvy2Oytserij0ic4nW3ISJmuwcwjhdvhznegfB3UMN5V4vxkGnCGjQTb7m0SgcC2JYpCRInTZd/NUWApCjp2wrq1IEisMayIq9NSo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739357628; c=relaxed/simple; bh=FaCcjfN7tsH4OS3ZlgvormgYVFIHIhRR3WXL4LQvN/g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Ra+/YVD7pTpsPjQWAc2PVlvv8h2D7Oiw1TZOIFdFcbMSJjLpQ4/JOmbKAjzXPDtFHLSjRjA8ioKl+yi2aKrdNmx8IL2X1F9D5gnvoNBlLoTSISCyM5LcrztOCHoexdaDMpGBV5DWHFjOegf7fyjnStrUaJnwincgHBvDNpO6jvU= 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=EoB0Qjj4; arc=none smtp.client-ip=209.85.221.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="EoB0Qjj4" Received: by mail-wr1-f46.google.com with SMTP id ffacd0b85a97d-38dd14c9a66so2012222f8f.0 for ; Wed, 12 Feb 2025 02:53:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1739357624; x=1739962424; 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=Qua5MIP1F2+Br2o7DhnfriDyRSWtUQzguoJZmEBlsHk=; b=EoB0Qjj4KyePU139rDobsdabhf2rWmUSzIEN5f6A6CjrLR6cQ29OlDUraAPo4T+UQY 4pRl9ujCbE3tKc8smyLlVgfsIA3DrfPRFOIPJ3PcZMU13Fxt6jt6ehPGP1cMgGNBsDGh TvFLckPsMPUc9cOUH9O3K/DW0OFRk64yWbWRNz1rQ5kd30K3Dhn0WEGwHmcj8uG0iJM/ hARs67YI6RqI9GJzUp9jaYI9KQe6bq9h0GTKmqhCDCF/SBXooQ7+6czAlUTii+ea31QS a5o3FQU6kPX5lJOJmxIGMP/XHDex/xorRwbeP6IpyPP9Bf2qKl6KJd8qHHp6zehNNvCH Eh4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739357624; x=1739962424; 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=Qua5MIP1F2+Br2o7DhnfriDyRSWtUQzguoJZmEBlsHk=; b=VvY5mF9gsRCTLiBZBzmM2qsvGD2A5q2Oa+qU3zCXCwq8pWUlCXPIuMF5VtygaSxrlI IYi8aS4WsAklzKdo0/NYMUTIjoGpE39kIn4TaUsnGR0VX8QwPzS2ztH07f7FF7l0n/6Z gAWPDSD5JlD5fA3LW2nST4sg3s0/Pt9tYNBdDi2QooA3r9LC3mTx1fgGrPEx86anGq3c bR3qFTFCwzadSZuTPZr4kltUFeWuZalvgVbkbWMyUi78JKc2FArq7GCyMQ6f3EuxBfQW sd1lg8HQA+XrgwNIDDF5p1pO6P5ZcCthPd+2C915aDO0ReuRbcUiNPsV6+h7FSUC1DFX OqoQ== X-Gm-Message-State: AOJu0Yyahi4phtZW3TdcKhsL3zqRZl5TlysDDpa1IJKr3FyzOy2eqkS9 XGflikba0Jr8YROks8mrrk4CSbquMXrzrPjqIhdXO7UTGyyuObfs9/odj2Z4xYY= X-Gm-Gg: ASbGncvrfNC9gIgmDrweiE3wokCoZ9G73zKsy6yYcrZBE7Znx40ITYU8LxBFCEwwTfP Uehz8KFNpDuZWOF2B4MekOafDaiKPlcEJ2G/Zid1ouikwUy6lFgR2MaLJIfar3bmLojGuPud4O9 iTPOX5r3+l4821NJEHw4z3AsNEne3brEhxjYoa40+MYRQsybFbDtQvy4e/FdcKvf/UMaPZ21Sio RaURtWrwKGH1vIpDojjSgayI0sNb9zuZsUzucFCuKlFZD0L1+cB4KGdRgLqcwzuPrLGTl67J1pN rKNaVR0N6i+VggQgIrteRo0xH1zUaeoUYdj7yNImkiq8zD0yrg/iS1VD8ixvKgaGFIwQgfueNSv WNA== X-Google-Smtp-Source: AGHT+IFWPugSZjah0wRf2O2mkPiV3cBZZ++fO1SA6jVTzyEaEzkOGozJBdnn/r2UERW+T2mlydaoQQ== X-Received: by 2002:a5d:5986:0:b0:38f:2224:738e with SMTP id ffacd0b85a97d-38f22247752mr87839f8f.1.1739357624456; Wed, 12 Feb 2025 02:53:44 -0800 (PST) Received: from localhost (p200300f65f083b04d1ced5eed56c82e8.dip0.t-ipconnect.de. [2003:f6:5f08:3b04:d1ce:d5ee:d56c:82e8]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38dc6c80df2sm15470586f8f.18.2025.02.12.02.53.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Feb 2025 02:53:44 -0800 (PST) From: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= To: Lars-Peter Clausen , Michael Hennerich , Jonathan Cameron Cc: linux-iio@vger.kernel.org Subject: [PATCH v1 2/3] iio: adc: ad_sigma_delta: Disable channel after calibration Date: Wed, 12 Feb 2025 11:53:23 +0100 Message-ID: <20250212105322.10243-7-u.kleine-koenig@baylibre.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250212105322.10243-5-u.kleine-koenig@baylibre.com> References: <20250212105322.10243-5-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=1168; i=u.kleine-koenig@baylibre.com; h=from:subject; bh=FaCcjfN7tsH4OS3ZlgvormgYVFIHIhRR3WXL4LQvN/g=; b=owEBbQGS/pANAwAKAY+A+1h9Ev5OAcsmYgBnrH2nATxNncXDFFinO2K3f8zT+p00P7RAhUkOT 2ucwJV5w5KJATMEAAEKAB0WIQQ/gaxpOnoeWYmt/tOPgPtYfRL+TgUCZ6x9pwAKCRCPgPtYfRL+ TiYvB/9Q+K0ReIOfQK//Lxcs+BUrpXAd+6h7Hfy8Qus2kAtwcJC87XQez3MbgZ0acgm9seXMqIi bEziGRAlhg0VqTqGHbLn/cSi2S+2SlqP90HDCqZYrS7MVKe8lU5UeEEQvggP8H2M85Tbip2Zo8u CqnKplZUSyUkTjPXkwTGzBS5jDZTf6Uxx4cbksjtEYW41+2tqsvhS7vYKclrvjM+gU3ZXg9CmDZ N6+iDchTJLnXIbFvAAnMM+FsyRvTWPsIUs/oTbWgX09JmRV4JNDTaPkwTemnKxnnIc6P6rvTpho Bt2wpWAyVx8BTrzjqYQuIjJ6aucksV398LWCdRaZYw1z3rh3 X-Developer-Key: i=u.kleine-koenig@baylibre.com; a=openpgp; fpr=0D2511F322BFAB1C1580266BE2DCDD9132669BD6 The function ad_sd_calibrate() enables the channel to calibrate at function entry but doesn't disable it on exit. This is problematic because if two (or more) channels are calibrated in a row, the second calibration isn't executed as intended as the first (still enabled) channel is recalibrated and after the first irq (i.e. when the calibration of the first channel completed) the calibration is aborted. To fix this, disable the calibrated channel after calibration. Signed-off-by: Uwe Kleine-König --- drivers/iio/adc/ad_sigma_delta.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/iio/adc/ad_sigma_delta.c b/drivers/iio/adc/ad_sigma_delta.c index 10e635fc4fa4..fbe241b90f37 100644 --- a/drivers/iio/adc/ad_sigma_delta.c +++ b/drivers/iio/adc/ad_sigma_delta.c @@ -339,6 +339,7 @@ int ad_sd_calibrate(struct ad_sigma_delta *sigma_delta, out: sigma_delta->keep_cs_asserted = false; ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE); + ad_sigma_delta_disable_one(sigma_delta, channel); sigma_delta->bus_locked = false; spi_bus_unlock(sigma_delta->spi->controller); From patchwork Wed Feb 12 10:53:24 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Uwe Kleine-Konig X-Patchwork-Id: 13971450 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.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 74BB8209F2A for ; Wed, 12 Feb 2025 10:53:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739357630; cv=none; b=s6gP11ZV4hcyavZn2aq97kKQZJ8ORYxJOlxskCJudvA77xx4osFlSLfAu0rqaxZJAgpHBzchueDsL5IUuddOb4NzbZMCpN+EYdLlAO+DQWdcRkGeACZ1fHLERQrLxzTPvTz7e6mbDDkAQHde6H0ZadH4F1uTWK9b3zH3toqUlNI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739357630; c=relaxed/simple; bh=VdjIqxgwm2NnVzZkk+QuTGP3mNdn3OgigNCGuWhN5JQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=H0NfVDRfGZZUYzYdOcwK75KTIJIna8duE0xvrNSxgbm149VuU3svx2RsDcOVQw2tDH2f0YKDxWAEqSnOqsbLXp/BxqXvHgmtgJNtJUTg0HSOSpiJzPrHy/DAq998nF11x1J84E0dSJy++JenhdUs7J0y3FwEQbCSOJFch+2XMls= 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=Ais2qnQg; arc=none smtp.client-ip=209.85.128.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="Ais2qnQg" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-4394a823036so25347655e9.0 for ; Wed, 12 Feb 2025 02:53:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1739357627; x=1739962427; 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=5db5caX2vtPe/GnhY7FuqwYgcu6w/biIkrUinpv0GKg=; b=Ais2qnQgcufZZYvjUMMD+3SwMH7JMc23MQGVQVexjr+pLgxYuIPkfCh+ovkMePZiuR B93iabblbW2Ra+//mWs3w1BhH4q/PgxY7Ci4AoQBjV2UqcyEkZyc/dadPgFSw4vChfpF ASUdzSwoUl8HdyuicKhEx1OwzC36weem00FeZuwzKqsg3uDg4I2uhuBD2WyqXVdBMZeD V7EixLKetA/I/cSFYQI0Sp+7UYGYGLJM+7HjB/+auygGl6bcYlQcN2hkBAkGI+cepJgN hJt7qEZpQtFZmSJeA/Wqt3ys+6MWHwEwnGcC5gBy5LYYVKu1kIKiz34VxturZ2xqq9pN f9qw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739357627; x=1739962427; 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=5db5caX2vtPe/GnhY7FuqwYgcu6w/biIkrUinpv0GKg=; b=GCJcPn1zBJdMqczqUx+YujDL7E1BJO+tgK+gceFwQsDS9pMX+La8tgQWoQ3umggZse 3Sv9JaZSKtrQhbPK01O2bUUmKUBB9tpY7HiDA+jZFJ8WGeL0ls7cA2wEw4QaBb2wsVxx pBNIqNkRVnZ8ED2650QIlyOtXM3r30WfaVfa18usGRt/p6b2lA60om2NWP57fM+rX11G y+Xc7H1e33R5t+9nh7dqi4sr2cul4/LXif/kxQhDJY+u1RU/HkfdXYRNC2lJSHsnOc4x xBVH+izan5qD/uAtyEDLVhIN28SD0FR4vMaiGGd6IGPGbQPrAaye+Q1XaqKItcZuqViS s7Cw== X-Gm-Message-State: AOJu0YxqSP1/etaS6bBy/MV0h3jy99p/1cWKtAX+4vDpipgg+oavcXn6 kGSt3UdASdAvxbKcl72N0HSC9DiGLOAdZ0sPWrGCXc+ZrCVMgHbQUJtm9ehr9hY= X-Gm-Gg: ASbGncslyAE0wJnIyPUiGY0NW4fzAp59n7+yQ8f45U2w1FQHhTYdqwWqgrfc8Y+UgNj IhWp/X3sU4Oxkg66YZZfnUJSTyoS6VWHtpPFpEk/gO/xU4Gg1bXgZDcRV7F6cFPsf3UdqNd4Z+R c6sV5LYmA7kSoeT+Ex9KtV6b7gtaWYLhuqPfFbke8b7T4Mk0P1bv/4mM8yGTtw6qncI/7RKpWXI TwuI53uwKLL4s8EvmT6dKbKfFmI3k5RSa0JYgrJvF3TnMsVuclrzjDZZYIrcqQOc9XOGbPQhX2h GsnY6/xV4JDN4AdauRGTG5tGR4fRGoS21L444LMXomSXkEq7cBoqtCUko9K+ChCpNcZjIbNNSe1 sqw== X-Google-Smtp-Source: AGHT+IGpZK3qoR1JVQfpnMXX8Uy4rlbwm4nyScv8FnSa/xdr5cVycdWv/RHCH6CsvlHCa8tGg8vPFA== X-Received: by 2002:a05:600c:5487:b0:439:38a1:e49 with SMTP id 5b1f17b1804b1-439581605dbmr27605365e9.6.1739357626630; Wed, 12 Feb 2025 02:53:46 -0800 (PST) Received: from localhost (p200300f65f083b04d1ced5eed56c82e8.dip0.t-ipconnect.de. [2003:f6:5f08:3b04:d1ce:d5ee:d56c:82e8]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4395a1b8443sm16006395e9.35.2025.02.12.02.53.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Feb 2025 02:53:46 -0800 (PST) From: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= To: Lars-Peter Clausen , Michael Hennerich , Jonathan Cameron Cc: linux-iio@vger.kernel.org Subject: [PATCH v1 3/3] iio: adc: ad7124: Implement calibration at probe time Date: Wed, 12 Feb 2025 11:53:24 +0100 Message-ID: <20250212105322.10243-8-u.kleine-koenig@baylibre.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250212105322.10243-5-u.kleine-koenig@baylibre.com> References: <20250212105322.10243-5-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=6632; i=u.kleine-koenig@baylibre.com; h=from:subject; bh=VdjIqxgwm2NnVzZkk+QuTGP3mNdn3OgigNCGuWhN5JQ=; b=owEBbQGS/pANAwAKAY+A+1h9Ev5OAcsmYgBnrH2qecNJ6C87PmjnIwhCGH3rZoWsbtpY2qb3d sboYVsU2J6JATMEAAEKAB0WIQQ/gaxpOnoeWYmt/tOPgPtYfRL+TgUCZ6x9qgAKCRCPgPtYfRL+ TiTaB/0c5rM1wv93b68uvHHrH32hrED8hiBb9fSMzkUtzDqt7pKJbpDs2TlRil0JJlb8fU8lt2s iafgcEEFt8tGiAj51QZB8bzGE/tjWo5x8LTTh2sKKPWLHB/MQNLnKScr/GnwM4sz111jmGqHy9n HqJ+7d/sL80J3/bdF7mScPoSttF/xRXECBOC4PpNzMaRWDXvdKcECX6JcXENp7fBrhdWql/AfMD 3k91KJQnKESNRBoxc/TAo9YLrjopC2DWuZAPv+OO8YWOz4JVUyG5BqzYIsRTHT3sImVuVldy9im znIuiwkZt9ePft5KsU3jNy7JhbNrt3iWY4in8Us0/jVtKkg1 X-Developer-Key: i=u.kleine-koenig@baylibre.com; a=openpgp; fpr=0D2511F322BFAB1C1580266BE2DCDD9132669BD6 Use the calibration function provided by the ad_sigma_delta shim to calibrate all channels at probe time. For measurements with gain 1 (i.e. if CONFIG_x.PGA = 0) full-scale calibrations are not supported and the reset default value of the GAIN register is supposed to be used then. Signed-off-by: Uwe Kleine-König --- drivers/iio/adc/ad7124.c | 99 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 96 insertions(+), 3 deletions(-) diff --git a/drivers/iio/adc/ad7124.c b/drivers/iio/adc/ad7124.c index a664179918fc..6b460407d2d2 100644 --- a/drivers/iio/adc/ad7124.c +++ b/drivers/iio/adc/ad7124.c @@ -53,6 +53,11 @@ #define AD7124_ADC_CTRL_MODE_MSK GENMASK(5, 2) #define AD7124_ADC_CTRL_MODE(x) FIELD_PREP(AD7124_ADC_CTRL_MODE_MSK, x) +#define AD7124_MODE_CAL_INT_ZERO 0x5 /* Internal Zero-Scale Calibration */ +#define AD7124_MODE_CAL_INT_FULL 0x6 /* Internal Full-Scale Calibration */ +#define AD7124_MODE_CAL_SYS_ZERO 0x7 /* System Zero-Scale Calibration */ +#define AD7124_MODE_CAL_SYS_FULL 0x8 /* System Full-Scale Calibration */ + /* AD7124 ID */ #define AD7124_DEVICE_ID_MSK GENMASK(7, 4) #define AD7124_DEVICE_ID_GET(x) FIELD_GET(AD7124_DEVICE_ID_MSK, x) @@ -162,6 +167,8 @@ struct ad7124_channel_config { unsigned int odr; unsigned int odr_sel_bits; unsigned int filter_type; + unsigned int calibration_offset; + unsigned int calibration_gain; ); }; @@ -182,6 +189,12 @@ struct ad7124_state { unsigned int num_channels; struct mutex cfgs_lock; /* lock for configs access */ unsigned long cfg_slots_status; /* bitmap with slot status (1 means it is used) */ + + /* + * Stores the power-on reset value for the GAIN(x) registers which are + * needed for measurements at gain 1 (i.e. CONFIG(x).PGA == 0) + */ + unsigned int gain_default; DECLARE_KFIFO(live_cfgs_fifo, struct ad7124_channel_config *, AD7124_MAX_CONFIGS); }; @@ -355,6 +368,8 @@ static struct ad7124_channel_config *ad7124_find_similar_live_cfg(struct ad7124_ unsigned int odr; unsigned int odr_sel_bits; unsigned int filter_type; + unsigned int calibration_offset; + unsigned int calibration_gain; })); for (i = 0; i < st->num_channels; i++) { @@ -369,7 +384,9 @@ static struct ad7124_channel_config *ad7124_find_similar_live_cfg(struct ad7124_ cfg->pga_bits == cfg_aux->pga_bits && cfg->odr == cfg_aux->odr && cfg->odr_sel_bits == cfg_aux->odr_sel_bits && - cfg->filter_type == cfg_aux->filter_type) + cfg->filter_type == cfg_aux->filter_type && + cfg->calibration_offset == cfg_aux->calibration_offset && + cfg->calibration_gain == cfg_aux->calibration_gain) return cfg_aux; } @@ -425,6 +442,14 @@ static int ad7124_write_config(struct ad7124_state *st, struct ad7124_channel_co cfg->cfg_slot = cfg_slot; + ret = ad_sd_write_reg(&st->sd, AD7124_OFFSET(ch->cfg.cfg_slot), 3, ch->cfg.calibration_offset); + if (ret) + return ret; + + ret = ad_sd_write_reg(&st->sd, AD7124_GAIN(ch->cfg.cfg_slot), 3, ch->cfg.calibration_gain); + if (ret) + return ret; + tmp = (cfg->buf_positive << 1) + cfg->buf_negative; val = AD7124_CONFIG_BIPOLAR(cfg->bipolar) | AD7124_CONFIG_REF_SEL(cfg->refsel) | AD7124_CONFIG_IN_BUFF(tmp) | AD7124_CONFIG_PGA(cfg->pga_bits); @@ -831,13 +856,22 @@ static int ad7124_soft_reset(struct ad7124_state *st) return dev_err_probe(dev, ret, "Error reading status register\n"); if (!(readval & AD7124_STATUS_POR_FLAG_MSK)) - return 0; + break; /* The AD7124 requires typically 2ms to power up and settle */ usleep_range(100, 2000); } while (--timeout); - return dev_err_probe(dev, -EIO, "Soft reset failed\n"); + if (readval & AD7124_STATUS_POR_FLAG_MSK) + return dev_err_probe(dev, -EIO, "Soft reset failed\n"); + + ret = ad_sd_read_reg(&st->sd, AD7124_GAIN(0), 3, &st->gain_default); + if (ret < 0) + return dev_err_probe(dev, ret, "Error reading gain register\n"); + + dev_dbg(dev, "Reset value of GAIN register is 0x%x\n", st->gain_default); + + return 0; } static int ad7124_check_chip_id(struct ad7124_state *st) @@ -1050,6 +1084,61 @@ static int ad7124_setup(struct ad7124_state *st) return ret; } +static int ad7124_calibrate_all(struct ad7124_state *st, struct iio_dev *indio_dev) +{ + struct device *dev = &st->sd.spi->dev; + int ret, i; + + for (i = 0; i < st->num_channels; i++) { + + if (indio_dev->channels[i].type != IIO_VOLTAGE) + continue; + + /* + * For calibration the OFFSET register should hold its reset default + * value. For the GAIN register there is no such requirement but + * for gain 1 it should hold the reset default value, too. So to + * simplify matters use the reset default value for both. + */ + st->channels[i].cfg.calibration_offset = 0x800000; + st->channels[i].cfg.calibration_gain = st->gain_default; + + /* + * Full scale calibration isn't supported at gain 1, so skip in + * that case. + */ + if (st->channels[i].cfg.pga_bits > 0) { + ret = ad_sd_calibrate(&st->sd, AD7124_MODE_CAL_INT_FULL, i); + if (ret < 0) + return ret; + + /* + * read out the resulting value of GAIN + * after full-scale calibration because the next + * ad_sd_calibrate() call overwrites this via + * ad_sigma_delta_set_channel() -> ad7124_set_channel() + * ... -> ad7124_enable_channel(). + */ + ret = ad_sd_read_reg(&st->sd, AD7124_GAIN(st->channels[i].cfg.cfg_slot), 3, &st->channels[i].cfg.calibration_gain); + if (ret < 0) + return ret; + } + + ret = ad_sd_calibrate(&st->sd, AD7124_MODE_CAL_INT_ZERO, i); + if (ret < 0) + return ret; + + ret = ad_sd_read_reg(&st->sd, AD7124_OFFSET(st->channels[i].cfg.cfg_slot), 3, &st->channels[i].cfg.calibration_offset); + if (ret < 0) + return ret; + + dev_dbg(dev, "offset and gain for channel %d = 0x%x + 0x%x\n", + i, st->channels[i].cfg.calibration_offset, st->channels[i].cfg.calibration_gain); + } + + return 0; +} + static void ad7124_reg_disable(void *r) { regulator_disable(r); @@ -1128,6 +1217,10 @@ static int ad7124_probe(struct spi_device *spi) if (ret < 0) return dev_err_probe(dev, ret, "Failed to setup triggers\n"); + ret = ad7124_calibrate_all(st, indio_dev); + if (ret) + return ret; + ret = devm_iio_device_register(&spi->dev, indio_dev); if (ret < 0) return dev_err_probe(dev, ret, "Failed to register iio device\n");