From patchwork Tue Dec 24 09:34:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julien Stephan X-Patchwork-Id: 13919779 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7054018FC74 for ; Tue, 24 Dec 2024 09:34:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735032880; cv=none; b=tfuYz7iOEI8nDnEH5AHy6ltL1kRgpzE915M9dSxmuK+DnpJb9eT0bnyq+NHNOGhQ1B+zT3LOFxGKMhV7vJUgFT/yqxjgR4v4ttNk5JcbW3nc+Skgc4kYyzjP14m6Cmo/fjtmzuVNnBK97DuCGJqihilcHPJWQb92j1cZqAGxQ1w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735032880; c=relaxed/simple; bh=VE1/sptLNJO9oRV/s4kfSNaNVaKhi9VwxXf2A7CoZaU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=QYMAQ9/hcI1fRnymANnu9t8IsAqmfjD5yfrmOtX4I4EdY5I3pnGkhfd0huLhs5yHWouACLs+vF1BIOwoxI4x3y+Fhkb0GBluwYyZzfshIk4s9slkiAlody0tzFoZxmtJ0iIKbpIj0eSJOX09F9mt+lWnTt8KDpEbtzMF6DfMVVE= 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=tiVJgpL8; arc=none smtp.client-ip=209.85.128.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="tiVJgpL8" Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-4361fe642ddso51902185e9.2 for ; Tue, 24 Dec 2024 01:34:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1735032877; x=1735637677; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=pM6GFa5ErxgDv1duEO96l/YGRIBmSTG/1V9JEZbHJNg=; b=tiVJgpL8PBePKmoD8zGPr0UJIMRjAhNnkPq6DXAVzhjZFalqigT4izOb1Hq31ibngI /FfR1psrFvXx6TZDWQcge3uhx5rEQxrK9frove+Jpn+7t/NPBrPQi1nl23qKZiYDZh9c CVgkfUWclJLO/TFKxXD/59IIET5ru5u32U8aqfbAnW0Pt/20rAk/hRKOSNfBcOJl+oLZ 7h2LLw7ysPBaRnGf7cnfpB3HZDve0DV6+5ii9TKizoDAyR4+sSyJhV0+1RzyZieLu6dq q7SJXn4DlT8HbAF4I5Y6tAeyOCJ6smjJ/DsVSLSodKGZe6/4evHKMI1cTFbIXwKMa3Yq zkCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735032877; x=1735637677; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=pM6GFa5ErxgDv1duEO96l/YGRIBmSTG/1V9JEZbHJNg=; b=w4rrqeem+0lEo3Q7YysBAUSGhaEd9wkqW1n5BBLErKky5VFBMzNL0FSybenGs/KcS1 5Kj1o8DUAcNzOm5qSPxh1lXDjf5DGdIRS+872T2tPs1FbAU+XeHFZDP+uM4YYcQ7Wjq4 C/UFZys2kG9er+zLf4YRIXA8cl1BhWbZkn8Vz0ShFytWo0APJEdXRc/om133whicLj5n /6uZOsl2PN+asNV44xeWlCa9IyS0fTc4fZIHKavd8KghnHFSjxNmP4ubNfMTY26WGvvx HAlcQ7jG3vR3MEJ4mEA4fcCn2fTjeBIoNtlLQLi+vRseSkFq373iY6qHQ9J24Mr57YHo Jf2A== X-Gm-Message-State: AOJu0YyKWp70k6rZgJiBS5C4MUUjG+vyLh+4dNW9n2f4hzR8dwJ6V0Gl mO6r8FsKNi1cYO9GxpfwU3UfAAahiTtu2/S5YrllxgW1Rxyew79VmCviv3Z8F4nRzKDMYZHzsv3 XtASmqw== X-Gm-Gg: ASbGncvg7AX7yc9R77oNUZuMgP83cWxUDf79eyyHFXRg6nNhNZCkELPpYM6pA/p4SKf flyJVJHxXux5TadqgkChYNZYnTcu/hFdL2C+lB3R1oNCOhd5Va2ly71nuKmJHh9BnqxKU3F7vyg SoP8wBWGN4BcjDBIqiRGuV13J67K6StmclZo4mk//9xH0V4xGD4qBBXoIFzj5KU8NTzqJOreXJV w0QMJKIZBbwDTd4TYWMUyvb/3CC7pr7b7a3nYzNfBKmAx4K+wCIq0arUmr2pXsQPoyDYbl8PnvR erbZnpb4ljT/col+04Oj9uzm9h2cQt/FobXAOnqMi2PQHpkLRQ== X-Google-Smtp-Source: AGHT+IGGwBexuuCsSb+2k165A9EG+BSDBn4ld60SJ41FUJ7s7F5ae+cT99SxaOIzBMBk0nh9sB6PGg== X-Received: by 2002:a05:600c:1388:b0:434:f871:1bbc with SMTP id 5b1f17b1804b1-43668643aaamr168725875e9.10.1735032876784; Tue, 24 Dec 2024 01:34:36 -0800 (PST) Received: from [192.168.1.62] (2a02-842a-d52e-6101-6fd0-06c4-5d68-f0a5.rev.sfr.net. [2a02:842a:d52e:6101:6fd0:6c4:5d68:f0a5]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43661219a7csm160932535e9.24.2024.12.24.01.34.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Dec 2024 01:34:36 -0800 (PST) From: Julien Stephan Date: Tue, 24 Dec 2024 10:34:30 +0100 Subject: [PATCH RFC v2 1/4] iio: adc: ad7380: do not use iio_device_claim_direct_scoped anymore Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241224-ad7380-add-alert-support-v2-1-7c89b2bf7cb3@baylibre.com> References: <20241224-ad7380-add-alert-support-v2-0-7c89b2bf7cb3@baylibre.com> In-Reply-To: <20241224-ad7380-add-alert-support-v2-0-7c89b2bf7cb3@baylibre.com> To: Lars-Peter Clausen , Michael Hennerich , =?utf-8?q?Nuno_S=C3=A1?= , David Lechner , Jonathan Cameron , Jonathan Corbet Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Julien Stephan X-Mailer: b4 0.14.2 Rollback and remove the scoped version of iio_dvice_claim_direct_mode. Signed-off-by: Julien Stephan --- drivers/iio/adc/ad7380.c | 89 ++++++++++++++++++++++++++++-------------------- 1 file changed, 53 insertions(+), 36 deletions(-) diff --git a/drivers/iio/adc/ad7380.c b/drivers/iio/adc/ad7380.c index 4f32cb22f140442b831dc9a4f275e88e4ab2388e..4e26e0e7ac1d5a1c4c67118dbc34f2921fc171a4 100644 --- a/drivers/iio/adc/ad7380.c +++ b/drivers/iio/adc/ad7380.c @@ -675,15 +675,21 @@ static const struct regmap_config ad7380_regmap_config = { static int ad7380_debugfs_reg_access(struct iio_dev *indio_dev, u32 reg, u32 writeval, u32 *readval) { - iio_device_claim_direct_scoped(return -EBUSY, indio_dev) { - struct ad7380_state *st = iio_priv(indio_dev); + struct ad7380_state *st = iio_priv(indio_dev); + int ret; - if (readval) - return regmap_read(st->regmap, reg, readval); - else - return regmap_write(st->regmap, reg, writeval); - } - unreachable(); + ret = iio_device_claim_direct_mode(indio_dev); + if (ret) + return ret; + + if (readval) + ret = regmap_read(st->regmap, reg, readval); + else + ret = regmap_write(st->regmap, reg, writeval); + + iio_device_release_direct_mode(indio_dev); + + return ret; } /* @@ -920,6 +926,7 @@ static int ad7380_read_raw(struct iio_dev *indio_dev, { struct ad7380_state *st = iio_priv(indio_dev); const struct iio_scan_type *scan_type; + int ret; scan_type = iio_get_current_scan_type(indio_dev, chan); @@ -928,11 +935,16 @@ static int ad7380_read_raw(struct iio_dev *indio_dev, switch (info) { case IIO_CHAN_INFO_RAW: - iio_device_claim_direct_scoped(return -EBUSY, indio_dev) { - return ad7380_read_direct(st, chan->scan_index, - scan_type, val); - } - unreachable(); + ret = iio_device_claim_direct_mode(indio_dev); + if (ret) + return ret; + + ret = ad7380_read_direct(st, chan->scan_index, + scan_type, val); + + iio_device_release_direct_mode(indio_dev); + + return ret; case IIO_CHAN_INFO_SCALE: /* * According to the datasheet, the LSB size is: @@ -1024,31 +1036,36 @@ static int ad7380_write_raw(struct iio_dev *indio_dev, /* always enable resolution boost when oversampling is enabled */ boost = osr > 0 ? 1 : 0; - iio_device_claim_direct_scoped(return -EBUSY, indio_dev) { - ret = regmap_update_bits(st->regmap, - AD7380_REG_ADDR_CONFIG1, - AD7380_CONFIG1_OSR | AD7380_CONFIG1_RES, - FIELD_PREP(AD7380_CONFIG1_OSR, osr) | - FIELD_PREP(AD7380_CONFIG1_RES, boost)); + ret = iio_device_claim_direct_mode(indio_dev); + if (ret) + return ret; - if (ret) - return ret; + ret = regmap_update_bits(st->regmap, + AD7380_REG_ADDR_CONFIG1, + AD7380_CONFIG1_OSR | AD7380_CONFIG1_RES, + FIELD_PREP(AD7380_CONFIG1_OSR, osr) | + FIELD_PREP(AD7380_CONFIG1_RES, boost)); - st->oversampling_ratio = val; - st->resolution_boost_enabled = boost; - - /* - * Perform a soft reset. This will flush the oversampling - * block and FIFO but will maintain the content of the - * configurable registers. - */ - return regmap_update_bits(st->regmap, - AD7380_REG_ADDR_CONFIG2, - AD7380_CONFIG2_RESET, - FIELD_PREP(AD7380_CONFIG2_RESET, - AD7380_CONFIG2_RESET_SOFT)); - } - unreachable(); + if (ret) + goto err; + + st->oversampling_ratio = val; + st->resolution_boost_enabled = boost; + + /* + * Perform a soft reset. This will flush the oversampling + * block and FIFO but will maintain the content of the + * configurable registers. + */ + ret = regmap_update_bits(st->regmap, + AD7380_REG_ADDR_CONFIG2, + AD7380_CONFIG2_RESET, + FIELD_PREP(AD7380_CONFIG2_RESET, + AD7380_CONFIG2_RESET_SOFT)); +err: + iio_device_release_direct_mode(indio_dev); + + return ret; default: return -EINVAL; } From patchwork Tue Dec 24 09:34:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julien Stephan X-Patchwork-Id: 13919781 Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.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 4F9A01940A1 for ; Tue, 24 Dec 2024 09:34:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735032881; cv=none; b=R4X0M/C1tm1xKdP5U2zijtlDYKcz+mSotc8JLIt/3wMTkHI4xb9QReCldatF0mBXglX0NyUmamRu1U67Gwgue325IktpOl4XwsgW2S2W+zTAjE1cVn6KkpML3weVsNJPFa9xmSWuqz04gNZ7AwHnGn48PWPI3qEaqNz90joKYqM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735032881; c=relaxed/simple; bh=yua/0zbTjMAmf2oFYpIk9LoTXBCtqmeHxVaLSwADFOI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=koAdzRraaOtd7maMPk37SIznhNgl6Bdi6wlwLwLTWslzEvWuJz+DiyN6VLGsrlMNd17qzwqzlq/TiSjYkWs+IPOYoqsyv07goTttSEsSRbNzrrAXNSvLyy2RKoGwfRupewc4KLzyddq/M9klsFKJyzd1yQ2ZTH1adwUDfKoLSDc= 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=TEyu7CRc; arc=none smtp.client-ip=209.85.221.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="TEyu7CRc" Received: by mail-wr1-f53.google.com with SMTP id ffacd0b85a97d-385e2880606so3740511f8f.3 for ; Tue, 24 Dec 2024 01:34:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1735032877; x=1735637677; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=T+u4Jy5PvOQf+0yU+yfEzQNHFYwkap+NBzqGZU1Srk8=; b=TEyu7CRcUSamaKAOBgKktJln5RZZJA3bLD19Qzu4x5yLdOW3ZNMvvhJXlDr5xV4sPo L8X1So0gEd56Asim7J1yFiRZWf7D7BTI5Jj/gvqO0y25Bhy0CWdMidHJGBUJFt9X3pyR 6YLxGxlisLiY2btzOlhvXhji7nB7sIyJ91HccKU6hMbIXGWDIg+VksLQB1nMIMHZB5Yl geh867C27o0bvC+HPcdi++JP0x3BH0itDXvUrhcv/sTlDlYDoUWyNdEcGIo70TrM6rIT Chb2TvCrL3eIlBXi7HMTdD9WA1aebO+LAD3ezC1Bejg9zCmPL4kcU6VZAqBC9W5LtLv1 vhmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735032877; x=1735637677; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=T+u4Jy5PvOQf+0yU+yfEzQNHFYwkap+NBzqGZU1Srk8=; b=efHSRsMj8FrVtsFISLM/AX0uAInwAH+vdGIJwVNADEO7zJduT1E+PKOCrRs8sE4Tfz GKBPxgBC8+DFNDZ2njLqYq1YV6MrnbousQWBI0KVKzJ3k7zvPyiM0tLb21vTkmZl5MpB gLU4S8q/LjIfO02IXIFBrNM/RLV6266Jaq6+2fIxg1j28vki9bZtpo6IJwyfN/8G8P2w jtCRfCgCn3xFnZsRhYqqvwoXPcHTAQvmXtZF6Cv41A/KrpIK2IyJyMfpbf4TZwsozshi /PT+lxlskIlPqsZE5OEVR+3jllVgzwzolUgX7zP+Ul4KgTU+t/5ZD1n9tMjx7jHJnFna Fg/w== X-Gm-Message-State: AOJu0YyFRTeCJVPfgjz6/tdNuFLYJOi3Dj+Hsw8btcxq+niufbWFQn6v /dZ02f3KY4/MVkgv0QAobBEM+gOa6NnuzbIqx0zNwMxhViqL1ptSnFbGooymNOI= X-Gm-Gg: ASbGnculJmjK6D3cN4DPIGmq75iDacOyARO13E5vBNPZfqD8NVsVOS4xdeAWUEXPBpY X7arUWQGNTklO6O6/Ed8puRu2tJ71Ufn0Id8qeapdcdNMJ24s56L/1HhvjzAoKRqQTKZoxVajC7 v79TLUlQjinmJuFAkDUpI8hdGyJtINQi8De73rc4nDHMl+PBQbsPItTRft68tvc8Ih0UovsyolJ kJU+nTaAqw5778Zb5kGHg9St75LVyq5Xvf1Q13v+lk52P5SzQdA4lm2ZsoNFYncNHkY3u/rnWW1 Omf56TwgP0nhxzwPDNNUt7fy23oKemizhLwaTk0TPcj/Xfy3ig== X-Google-Smtp-Source: AGHT+IHc62hSMXyBCQybh0DW1xl5UpUk+2FH16w/EZk5yE0yHwj0g8JiuD93nhp+FS97ASHVIN8/uQ== X-Received: by 2002:a05:6000:156f:b0:385:fd31:ca23 with SMTP id ffacd0b85a97d-38a223f8302mr16102917f8f.40.1735032877514; Tue, 24 Dec 2024 01:34:37 -0800 (PST) Received: from [192.168.1.62] (2a02-842a-d52e-6101-6fd0-06c4-5d68-f0a5.rev.sfr.net. [2a02:842a:d52e:6101:6fd0:6c4:5d68:f0a5]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43661219a7csm160932535e9.24.2024.12.24.01.34.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Dec 2024 01:34:37 -0800 (PST) From: Julien Stephan Date: Tue, 24 Dec 2024 10:34:31 +0100 Subject: [PATCH RFC v2 2/4] iio: adc: ad7380: enable regmap cache Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241224-ad7380-add-alert-support-v2-2-7c89b2bf7cb3@baylibre.com> References: <20241224-ad7380-add-alert-support-v2-0-7c89b2bf7cb3@baylibre.com> In-Reply-To: <20241224-ad7380-add-alert-support-v2-0-7c89b2bf7cb3@baylibre.com> To: Lars-Peter Clausen , Michael Hennerich , =?utf-8?q?Nuno_S=C3=A1?= , David Lechner , Jonathan Cameron , Jonathan Corbet Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Julien Stephan X-Mailer: b4 0.14.2 Enable regmap cache, to avoid useless access on spi bus. Don't store anymore the oversampling ratio in private data structure. Signed-off-by: Julien Stephan --- drivers/iio/adc/ad7380.c | 98 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 84 insertions(+), 14 deletions(-) diff --git a/drivers/iio/adc/ad7380.c b/drivers/iio/adc/ad7380.c index 4e26e0e7ac1d5a1c4c67118dbc34f2921fc171a4..b49067c36fdd1bb0e760faf71d7fa0c8c1f610e9 100644 --- a/drivers/iio/adc/ad7380.c +++ b/drivers/iio/adc/ad7380.c @@ -582,7 +582,6 @@ struct ad7380_state { const struct ad7380_chip_info *chip_info; struct spi_device *spi; struct regmap *regmap; - unsigned int oversampling_ratio; bool resolution_boost_enabled; unsigned int ch; bool seq; @@ -663,6 +662,20 @@ static int ad7380_regmap_reg_read(void *context, unsigned int reg, return 0; } +static const struct reg_default ad7380_reg_defaults[] = { + { AD7380_REG_ADDR_ALERT_LOW_TH, 0x800 }, + { AD7380_REG_ADDR_ALERT_HIGH_TH, 0x7FF }, +}; + +static const struct regmap_range ad7380_volatile_reg_ranges[] = { + regmap_reg_range(AD7380_REG_ADDR_CONFIG2, AD7380_REG_ADDR_ALERT), +}; + +static const struct regmap_access_table ad7380_volatile_regs = { + .yes_ranges = ad7380_volatile_reg_ranges, + .n_yes_ranges = ARRAY_SIZE(ad7380_volatile_reg_ranges), +}; + static const struct regmap_config ad7380_regmap_config = { .reg_bits = 3, .val_bits = 12, @@ -670,6 +683,10 @@ static const struct regmap_config ad7380_regmap_config = { .reg_write = ad7380_regmap_reg_write, .max_register = AD7380_REG_ADDR_ALERT_HIGH_TH, .can_sleep = true, + .reg_defaults = ad7380_reg_defaults, + .num_reg_defaults = ARRAY_SIZE(ad7380_reg_defaults), + .volatile_table = &ad7380_volatile_regs, + .cache_type = REGCACHE_MAPLE, }; static int ad7380_debugfs_reg_access(struct iio_dev *indio_dev, u32 reg, @@ -692,6 +709,37 @@ static int ad7380_debugfs_reg_access(struct iio_dev *indio_dev, u32 reg, return ret; } +/** + * ad7380_regval_to_osr - convert OSR register value to ratio + * @regval: register value to check + * + * Returns: the ratio corresponding to the OSR register. If regval is not in + * bound, return 1 (oversampling disabled) + * + */ +static int ad7380_regval_to_osr(int regval) +{ + if (regval < 0 || regval >= ARRAY_SIZE(ad7380_oversampling_ratios)) + return 1; + + return ad7380_oversampling_ratios[regval]; +} + +static int ad7380_get_osr(struct ad7380_state *st, int *val) +{ + int tmp; + int ret = 0; + + ret = regmap_read(st->regmap, AD7380_REG_ADDR_CONFIG1, &tmp); + if (ret) + return ret; + + *val = ad7380_regval_to_osr(FIELD_GET(AD7380_CONFIG1_OSR, tmp)); + + return 0; +} + + /* * When switching channel, the ADC require an additional settling time. * According to the datasheet, data is value on the third CS low. We already @@ -707,11 +755,15 @@ static int ad7380_set_ch(struct ad7380_state *st, unsigned int ch) .unit = SPI_DELAY_UNIT_NSECS, } }; - int ret; + int oversampling_ratio, ret; if (st->ch == ch) return 0; + ret = ad7380_get_osr(st, &oversampling_ratio); + if (ret) + return ret; + ret = regmap_update_bits(st->regmap, AD7380_REG_ADDR_CONFIG1, AD7380_CONFIG1_CH, @@ -722,9 +774,9 @@ static int ad7380_set_ch(struct ad7380_state *st, unsigned int ch) st->ch = ch; - if (st->oversampling_ratio > 1) + if (oversampling_ratio > 1) xfer.delay.value = T_CONVERT_0_NS + - T_CONVERT_X_NS * (st->oversampling_ratio - 1) * + T_CONVERT_X_NS * (oversampling_ratio - 1) * st->chip_info->num_simult_channels / AD7380_NUM_SDO_LINES; return spi_sync_transfer(st->spi, &xfer, 1); @@ -735,20 +787,25 @@ static int ad7380_set_ch(struct ad7380_state *st, unsigned int ch) * @st: device instance specific state * @scan_type: current scan type */ -static void ad7380_update_xfers(struct ad7380_state *st, +static int ad7380_update_xfers(struct ad7380_state *st, const struct iio_scan_type *scan_type) { struct spi_transfer *xfer = st->seq ? st->seq_xfer : st->normal_xfer; unsigned int t_convert = T_CONVERT_NS; + int oversampling_ratio, ret; /* * In the case of oversampling, conversion time is higher than in normal * mode. Technically T_CONVERT_X_NS is lower for some chips, but we use * the maximum value for simplicity for now. */ - if (st->oversampling_ratio > 1) + ret = ad7380_get_osr(st, &oversampling_ratio); + if (ret) + return ret; + + if (oversampling_ratio > 1) t_convert = T_CONVERT_0_NS + T_CONVERT_X_NS * - (st->oversampling_ratio - 1) * + (oversampling_ratio - 1) * st->chip_info->num_simult_channels / AD7380_NUM_SDO_LINES; if (st->seq) { @@ -761,7 +818,7 @@ static void ad7380_update_xfers(struct ad7380_state *st, st->chip_info->num_simult_channels; xfer[3].rx_buf = xfer[2].rx_buf + xfer[2].len; /* Additional delay required here when oversampling is enabled */ - if (st->oversampling_ratio > 1) + if (oversampling_ratio > 1) xfer[2].delay.value = t_convert; else xfer[2].delay.value = 0; @@ -773,6 +830,8 @@ static void ad7380_update_xfers(struct ad7380_state *st, xfer[1].len = BITS_TO_BYTES(scan_type->storagebits) * st->chip_info->num_simult_channels; } + + return 0; } static int ad7380_triggered_buffer_preenable(struct iio_dev *indio_dev) @@ -780,6 +839,7 @@ static int ad7380_triggered_buffer_preenable(struct iio_dev *indio_dev) struct ad7380_state *st = iio_priv(indio_dev); const struct iio_scan_type *scan_type; struct spi_message *msg = &st->normal_msg; + int ret; /* * Currently, we always read all channels at the same time. The scan_type @@ -791,7 +851,6 @@ static int ad7380_triggered_buffer_preenable(struct iio_dev *indio_dev) if (st->chip_info->has_mux) { unsigned int index; - int ret; /* * Depending on the requested scan_mask and current state, @@ -822,7 +881,9 @@ static int ad7380_triggered_buffer_preenable(struct iio_dev *indio_dev) } - ad7380_update_xfers(st, scan_type); + ret = ad7380_update_xfers(st, scan_type); + if (ret) + return ret; return spi_optimize_message(st->spi, msg); } @@ -895,7 +956,9 @@ static int ad7380_read_direct(struct ad7380_state *st, unsigned int scan_index, return ret; } - ad7380_update_xfers(st, scan_type); + ret = ad7380_update_xfers(st, scan_type); + if (ret) + return ret; ret = spi_sync(st->spi, &st->normal_msg); if (ret < 0) @@ -973,7 +1036,16 @@ static int ad7380_read_raw(struct iio_dev *indio_dev, return IIO_VAL_INT; case IIO_CHAN_INFO_OVERSAMPLING_RATIO: - *val = st->oversampling_ratio; + ret = iio_device_claim_direct_mode(indio_dev); + if (ret) + return ret; + + ret = ad7380_get_osr(st, val); + + iio_device_release_direct_mode(indio_dev); + + if (ret) + return ret; return IIO_VAL_INT; default: @@ -1049,7 +1121,6 @@ static int ad7380_write_raw(struct iio_dev *indio_dev, if (ret) goto err; - st->oversampling_ratio = val; st->resolution_boost_enabled = boost; /* @@ -1109,7 +1180,6 @@ static int ad7380_init(struct ad7380_state *st, bool external_ref_en) } /* This is the default value after reset. */ - st->oversampling_ratio = 1; st->ch = 0; st->seq = false; From patchwork Tue Dec 24 09:34:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julien Stephan X-Patchwork-Id: 13919782 Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) (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 59FD61AF0C3 for ; Tue, 24 Dec 2024 09:34:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735032882; cv=none; b=WwO0MU4lACc26c6oUBDOSzSPebj7w0MkkGnjQBf6tILnqvSvBUKMyEApwgAmjsRmokV9MoeT1OEEBwCtG2Afq9TRD+tpc103505oCAEJNKOutiDjHJMm4zWbvAEqIYkcZmqMn/pk0GETjbXhdxYGWREQ1fQTo5XTs+qULG2SbC8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735032882; c=relaxed/simple; bh=aweWmpL5g0zjtLjLvkjHfrZypMdsORngD8G/mYmjiw0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=HD8jldgNgfbH9euL2JdjwdSuf6/fMcZ2zA5Pq6FxwLmw2t33nSdNJTXaE1yo/2iSsKibNbzxeuzCJVISdLUTF7j3oKaKRf07UcOubEkKxnFC/lEKGwxxxoZ1ll+n0cnxB+d1fuhG2ZDbrcZTHhPj0eGaBZQYnq6SaviecNDbSgg= 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=fyC+1094; arc=none smtp.client-ip=209.85.128.48 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="fyC+1094" Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-4364a37a1d7so51887705e9.3 for ; Tue, 24 Dec 2024 01:34:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1735032879; x=1735637679; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=DTvD8FKKFGipdmXYj86+K+maZe38oNa1ur7/GExWl0U=; b=fyC+1094ytj0YtAW0nBSyaIVTS5EoYRnQDVFhi+1mMCUn0tgoywTKwd361oPfxOGWf nWDW9GmKXdMtu9mY6ILkbx9U3jcb3y2dmikyJXvBtck+8hMAb5Un0Gh/E+snIr4N4Grl e90vNRY11/ixe4ugIULm5puT9XwTM6HL+qdwPBweZJNAGmR98UYUX751UeZtTE2g3+8I KoWnX4XBZD/69vw1ZoZPYBZF3x0/ldxlz4cpdrB2KTB1L72JlG0IK2PGYtwfe2nBT0Fy /R2l0fS77yWkN2o/Jdr70tYXfpdGj5gbRSYViPODzEzi3Mlon1qFZUeAIKLgXV73iame NY+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735032879; x=1735637679; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DTvD8FKKFGipdmXYj86+K+maZe38oNa1ur7/GExWl0U=; b=j8IKhalPc9pz1+S1FBOWM2GqlXpJtblngrGPJiSZdkqbMbkR03UJRABc4oc/Gra1y7 FqP1JqmBn4jiINZTLIKIe04jwgN1KWoZvKTXbOlwMDjMMrWTHfBb6pH891D+dVCRYjym MQDY2Uu+PSWpccO9dvGCEAECpseeMMKYW1XP2vGWMWmxnfMyinlYfEt10twALvJuRSIi bI7B4ZhR0yTFG0wdavf4LMfMNRI9nWKYa7To1bH7NIiPes85tnfztMJ3lmptLgDC/eil TtE+hGpQ5l7q4efGhqge8pES/rLcXGuP9DPVxl3EqP2PYO2wiOsz64XRgCA93XX9QFOL d+MQ== X-Gm-Message-State: AOJu0Ywd3mHNpgCDy8c605EzkNfS1ol4FNU7avtrW8LerF7qZ8wxsA/x 4KtZvD0NevWulvcEaEATIfjIWbmLtb74mZBi0iNiXrQCwSFoUPg0p8yp+PW9Wcw= X-Gm-Gg: ASbGncuObpaDbJ0dJNoyQknEqn8OsGbMIlpgEerCncVvhzfjxQDzUyQOMha0TTIjy6g fVRr/Xhfkq0aYYuMtZke6JHQnASMbng5InNcEsjyjhQLq2YW8nhfSEFeJhosWL9pUepKcwC5GKY bdfdU6FXQ7kzw9eq5XH//vwTWPTeYhkOHvwUjnznz4HRsmnbR3VQVHEhDfJnmJbxS0dhk/abVss FMFkbHJ72CVAW7hpfgLbjijC65KTZmaWUB4cVEFi+K6eKaEycUcmVfWh4jfJXPv9Ij+rFeVqQeP MFD+AvgBJiEevum74IOfHVsRWuJBfJfH9jlPwYcy/32OiHPBuQ== X-Google-Smtp-Source: AGHT+IFjhPDiLwUmF70XszgatjCLcuNynUslsAihnwb+MKydOpr05jWuW/8UpaHK61eRTH6thifznA== X-Received: by 2002:a05:600c:1c22:b0:434:a10f:9b with SMTP id 5b1f17b1804b1-43668643b44mr134829815e9.14.1735032878428; Tue, 24 Dec 2024 01:34:38 -0800 (PST) Received: from [192.168.1.62] (2a02-842a-d52e-6101-6fd0-06c4-5d68-f0a5.rev.sfr.net. [2a02:842a:d52e:6101:6fd0:6c4:5d68:f0a5]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43661219a7csm160932535e9.24.2024.12.24.01.34.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Dec 2024 01:34:37 -0800 (PST) From: Julien Stephan Date: Tue, 24 Dec 2024 10:34:32 +0100 Subject: [PATCH RFC v2 3/4] iio: adc: ad7380: add alert support Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241224-ad7380-add-alert-support-v2-3-7c89b2bf7cb3@baylibre.com> References: <20241224-ad7380-add-alert-support-v2-0-7c89b2bf7cb3@baylibre.com> In-Reply-To: <20241224-ad7380-add-alert-support-v2-0-7c89b2bf7cb3@baylibre.com> To: Lars-Peter Clausen , Michael Hennerich , =?utf-8?q?Nuno_S=C3=A1?= , David Lechner , Jonathan Cameron , Jonathan Corbet Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Julien Stephan X-Mailer: b4 0.14.2 The alert functionality is an out of range indicator and can be used as an early indicator of an out of bounds conversion result. ALERT_LOW_THRESHOLD and ALERT_HIGH_THRESHOLD registers are common to all channels. When using 1 SDO line (only mode supported by the driver right now), i.e data outputs only on SDOA, SDOB (or SDOD for 4 channels variants) is used as an alert pin. The alert pin is updated at the end of the conversion (set to low if an alert occurs) and is cleared on a falling edge of CS. The ALERT register contains information about the exact alert status: channel and direction. Unfortunately we can't read this register because in buffered read we cannot claim for direct mode. User can set high/low thresholds and enable event detection using the regular iio events: events/in_thresh_falling_value events/in_thresh_rising_value events/thresh_either_en If the interrupt properties is present in the device tree, an IIO event will be generated for each interrupt received. Because we cannot read ALERT register, we can't determine the exact channel that triggers the alert, neither the direction (hight/low threshold violation), so we send and IIO_EV_DIR_EITHER event for all channels. In buffered reads, if input stays out of thresholds limit, an interrupt will be generated for each sample read, because the alert pin is cleared on a falling edge of CS (i.e when starting a new conversion). To avoid generating to much interrupt, we introduce a reset_timeout that can be used to disable interrupt for a given time (in ms) events/thresh_either_reset_timeout When an interrupt is received, interrupts are disabled and re-enabled after thresh_either_reset_timeout ms. If the reset timeout is set to 0, interrupt are re-enabled directly. Note: interrupts are always disabled at least during the handling of the previous interrupt, because each read triggers 2 transactions, that can lead to 2 interrupts for a single user read. IRQF_ONESHOT is not enough, because, it postpones the 2nd irq after the handling of the first one, which can still trigger 2 interrupts for a single user read. Signed-off-by: Julien Stephan --- drivers/iio/adc/ad7380.c | 354 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 350 insertions(+), 4 deletions(-) diff --git a/drivers/iio/adc/ad7380.c b/drivers/iio/adc/ad7380.c index b49067c36fdd1bb0e760faf71d7fa0c8c1f610e9..fb59c2c064f402ea797fc0e563bd535d0e0bf785 100644 --- a/drivers/iio/adc/ad7380.c +++ b/drivers/iio/adc/ad7380.c @@ -34,6 +34,7 @@ #include #include +#include #include #include #include @@ -112,6 +113,43 @@ struct ad7380_chip_info { const struct ad7380_timing_specs *timing_specs; }; +static const struct iio_event_spec ad7380_events[] = { + { + .type = IIO_EV_TYPE_THRESH, + .dir = IIO_EV_DIR_RISING, + .mask_shared_by_dir = BIT(IIO_EV_INFO_VALUE), + }, + { + .type = IIO_EV_TYPE_THRESH, + .dir = IIO_EV_DIR_FALLING, + .mask_shared_by_dir = BIT(IIO_EV_INFO_VALUE), + }, + { + .type = IIO_EV_TYPE_THRESH, + .dir = IIO_EV_DIR_EITHER, + .mask_shared_by_all = BIT(IIO_EV_INFO_ENABLE), + }, +}; + +static const struct iio_event_spec ad7380_events_irq[] = { + { + .type = IIO_EV_TYPE_THRESH, + .dir = IIO_EV_DIR_RISING, + .mask_shared_by_dir = BIT(IIO_EV_INFO_VALUE), + }, + { + .type = IIO_EV_TYPE_THRESH, + .dir = IIO_EV_DIR_FALLING, + .mask_shared_by_dir = BIT(IIO_EV_INFO_VALUE), + }, + { + .type = IIO_EV_TYPE_THRESH, + .dir = IIO_EV_DIR_EITHER, + .mask_shared_by_all = BIT(IIO_EV_INFO_ENABLE) | + BIT(IIO_EV_INFO_RESET_TIMEOUT), + }, +}; + enum { AD7380_SCAN_TYPE_NORMAL, AD7380_SCAN_TYPE_RESOLUTION_BOOST, @@ -214,6 +252,8 @@ static const struct iio_scan_type ad7380_scan_type_16_u[] = { .has_ext_scan_type = 1, \ .ext_scan_type = ad7380_scan_type_##bits##_##sign, \ .num_ext_scan_type = ARRAY_SIZE(ad7380_scan_type_##bits##_##sign), \ + .event_spec = ad7380_events, \ + .num_event_specs = ARRAY_SIZE(ad7380_events), \ } #define AD7380_CHANNEL(index, bits, diff, sign) \ @@ -585,6 +625,10 @@ struct ad7380_state { bool resolution_boost_enabled; unsigned int ch; bool seq; + struct timer_list alert_timer; + unsigned int alert_reset_timeout_ms; + atomic_t irq_enabled; + bool buffered_read_enabled; unsigned int vref_mv; unsigned int vcm_mv[MAX_NUM_CHANNELS]; unsigned int gain_milli[MAX_NUM_CHANNELS]; @@ -689,6 +733,26 @@ static const struct regmap_config ad7380_regmap_config = { .cache_type = REGCACHE_MAPLE, }; +static void ad7380_enable_irq(struct ad7380_state *st) +{ + if (st->spi->irq && !atomic_cmpxchg(&st->irq_enabled, 0, 1)) + enable_irq(st->spi->irq); +} + +static void ad7380_disable_irq(struct ad7380_state *st) +{ + if (st->spi->irq && atomic_cmpxchg(&st->irq_enabled, 1, 0)) { + disable_irq(st->spi->irq); + synchronize_irq(st->spi->irq); + } +} + +static void ad7380_disable_irq_nosync(struct ad7380_state *st) +{ + if (st->spi->irq && atomic_cmpxchg(&st->irq_enabled, 1, 0)) + disable_irq_nosync(st->spi->irq); +} + static int ad7380_debugfs_reg_access(struct iio_dev *indio_dev, u32 reg, u32 writeval, u32 *readval) { @@ -727,8 +791,8 @@ static int ad7380_regval_to_osr(int regval) static int ad7380_get_osr(struct ad7380_state *st, int *val) { - int tmp; - int ret = 0; + u32 tmp; + int ret; ret = regmap_read(st->regmap, AD7380_REG_ADDR_CONFIG1, &tmp); if (ret) @@ -834,6 +898,13 @@ static int ad7380_update_xfers(struct ad7380_state *st, return 0; } +static void ad7380_handle_event_reset_timeout(struct timer_list *t) +{ + struct ad7380_state *st = from_timer(st, t, alert_timer); + + ad7380_enable_irq(st); +} + static int ad7380_triggered_buffer_preenable(struct iio_dev *indio_dev) { struct ad7380_state *st = iio_priv(indio_dev); @@ -841,6 +912,12 @@ static int ad7380_triggered_buffer_preenable(struct iio_dev *indio_dev) struct spi_message *msg = &st->normal_msg; int ret; + timer_setup(&st->alert_timer, ad7380_handle_event_reset_timeout, 0); + + ad7380_enable_irq(st); + + st->buffered_read_enabled = true; + /* * Currently, we always read all channels at the same time. The scan_type * is the same for all channels, so we just pass the first channel. @@ -894,6 +971,8 @@ static int ad7380_triggered_buffer_postdisable(struct iio_dev *indio_dev) struct spi_message *msg = &st->normal_msg; int ret; + st->buffered_read_enabled = false; + if (st->seq) { ret = regmap_update_bits(st->regmap, AD7380_REG_ADDR_CONFIG1, @@ -908,6 +987,9 @@ static int ad7380_triggered_buffer_postdisable(struct iio_dev *indio_dev) spi_unoptimize_message(msg); + ad7380_disable_irq(st); + timer_shutdown_sync(&st->alert_timer); + return 0; } @@ -1002,8 +1084,11 @@ static int ad7380_read_raw(struct iio_dev *indio_dev, if (ret) return ret; - ret = ad7380_read_direct(st, chan->scan_index, - scan_type, val); + ad7380_enable_irq(st); + + ret = ad7380_read_direct(st, chan->scan_index, scan_type, val); + + ad7380_disable_irq(st); iio_device_release_direct_mode(indio_dev); @@ -1151,12 +1236,190 @@ static int ad7380_get_current_scan_type(const struct iio_dev *indio_dev, : AD7380_SCAN_TYPE_NORMAL; } +static int ad7380_read_event_config(struct iio_dev *indio_dev, + const struct iio_chan_spec *chan, + enum iio_event_type type, + enum iio_event_direction dir) +{ + struct ad7380_state *st = iio_priv(indio_dev); + int alert_en, tmp, ret; + + ret = iio_device_claim_direct_mode(indio_dev); + if (ret) + return ret; + + ret = regmap_read(st->regmap, AD7380_REG_ADDR_CONFIG1, &tmp); + + iio_device_release_direct_mode(indio_dev); + + if (ret) + return ret; + + alert_en = FIELD_GET(AD7380_CONFIG1_ALERTEN, tmp); + + return alert_en; +} + +static int ad7380_write_event_config(struct iio_dev *indio_dev, + const struct iio_chan_spec *chan, + enum iio_event_type type, + enum iio_event_direction dir, + bool state) +{ + struct ad7380_state *st = iio_priv(indio_dev); + int ret; + + ret = iio_device_claim_direct_mode(indio_dev); + if (ret) + return ret; + + ret = regmap_update_bits(st->regmap, + AD7380_REG_ADDR_CONFIG1, + AD7380_CONFIG1_ALERTEN, + FIELD_PREP(AD7380_CONFIG1_ALERTEN, state)); + + iio_device_release_direct_mode(indio_dev); + + if (ret) + return ret; + + return 0; +} + +static int ad7380_read_event_value(struct iio_dev *indio_dev, + const struct iio_chan_spec *chan, + enum iio_event_type type, + enum iio_event_direction dir, + enum iio_event_info info, + int *val, int *val2) +{ + struct ad7380_state *st = iio_priv(indio_dev); + int ret, tmp; + + switch (info) { + case IIO_EV_INFO_VALUE: + ret = iio_device_claim_direct_mode(indio_dev); + + switch (dir) { + case IIO_EV_DIR_RISING: + ret = regmap_read(st->regmap, + AD7380_REG_ADDR_ALERT_HIGH_TH, + &tmp); + if (ret) + return ret; + + *val = FIELD_GET(AD7380_ALERT_HIGH_TH, tmp); + ret = IIO_VAL_INT; + break; + case IIO_EV_DIR_FALLING: + ret = regmap_read(st->regmap, + AD7380_REG_ADDR_ALERT_LOW_TH, + &tmp); + if (ret) + return ret; + + FIELD_GET(AD7380_ALERT_LOW_TH, tmp); + ret = IIO_VAL_INT; + break; + default: + ret = -EINVAL; + break; + } + + iio_device_release_direct_mode(indio_dev); + return ret; + case IIO_EV_INFO_RESET_TIMEOUT: + *val = st->alert_reset_timeout_ms; + return IIO_VAL_INT; + default: + return -EINVAL; + } +} + +static int ad7380_write_event_value(struct iio_dev *indio_dev, + const struct iio_chan_spec *chan, + enum iio_event_type type, + enum iio_event_direction dir, + enum iio_event_info info, + int val, int val2) +{ + struct ad7380_state *st = iio_priv(indio_dev); + const struct iio_scan_type *scan_type; + int ret; + u16 th; + + switch (info) { + case IIO_EV_INFO_VALUE: + ret = iio_device_claim_direct_mode(indio_dev); + if (ret) + return ret; + + /* + * According to the datasheet, + * AD7380_REG_ADDR_ALERT_HIGH_TH[11:0] are the 12 MSB of the + * 16-bits internal alert high register. LSB are set to 0xf. + * AD7380_REG_ADDR_ALERT_LOW_TH[11:0] are the 12 MSB of the + * 16 bits internal alert low register. LSB are set to 0x0. + * + * When alert is enabled the conversion from the adc is compared + * immediately to the alert high/low thresholds, before any + * oversampling. This means that the thresholds are the same for + * normal mode and oversampling mode. + */ + + /* Extract the 12 MSB of val */ + scan_type = iio_get_current_scan_type(indio_dev, chan); + if (IS_ERR(scan_type)) + return PTR_ERR(scan_type); + + th = val >> (scan_type->realbits - 12); + + switch (dir) { + case IIO_EV_DIR_RISING: + ret = regmap_write(st->regmap, + AD7380_REG_ADDR_ALERT_HIGH_TH, + th); + if (ret) + return ret; + + break; + case IIO_EV_DIR_FALLING: + ret = regmap_write(st->regmap, + AD7380_REG_ADDR_ALERT_LOW_TH, + th); + if (ret) + return ret; + + break; + default: + ret = -EINVAL; + break; + } + + iio_device_release_direct_mode(indio_dev); + return ret; + case IIO_EV_INFO_RESET_TIMEOUT: + if (val < 0) + return -EINVAL; + st->alert_reset_timeout_ms = val; + timer_reduce(&st->alert_timer, jiffies + msecs_to_jiffies(val)); + + return 0; + default: + return -EINVAL; + } +} + static const struct iio_info ad7380_info = { .read_raw = &ad7380_read_raw, .read_avail = &ad7380_read_avail, .write_raw = &ad7380_write_raw, .get_current_scan_type = &ad7380_get_current_scan_type, .debugfs_reg_access = &ad7380_debugfs_reg_access, + .read_event_config = &ad7380_read_event_config, + .write_event_config = &ad7380_write_event_config, + .read_event_value = &ad7380_read_event_value, + .write_event_value = &ad7380_write_event_value, }; static int ad7380_init(struct ad7380_state *st, bool external_ref_en) @@ -1182,6 +1445,12 @@ static int ad7380_init(struct ad7380_state *st, bool external_ref_en) /* This is the default value after reset. */ st->ch = 0; st->seq = false; + st->buffered_read_enabled = false; + + /* + * Set a minimum default 1s delay between each alert in buffered reads + */ + st->alert_reset_timeout_ms = 1000; /* SPI 1-wire mode */ return regmap_update_bits(st->regmap, AD7380_REG_ADDR_CONFIG2, @@ -1190,6 +1459,50 @@ static int ad7380_init(struct ad7380_state *st, bool external_ref_en) AD7380_NUM_SDO_LINES)); } +static irqreturn_t ad7380_primary_event_handler(int irq, void *private) +{ + struct iio_dev *indio_dev = private; + struct ad7380_state *st = iio_priv(indio_dev); + + if (!atomic_read(&st->irq_enabled)) + return IRQ_NONE; + + ad7380_disable_irq_nosync(st); + + return IRQ_WAKE_THREAD; +} + +static irqreturn_t ad7380_event_handler(int irq, void *private) +{ + struct iio_dev *indio_dev = private; + s64 timestamp = iio_get_time_ns(indio_dev); + struct ad7380_state *st = iio_priv(indio_dev); + const struct iio_chan_spec *chan = &indio_dev->channels[0]; + unsigned int i; + + for (i = 0; i < st->chip_info->num_channels - 1; i++) { + iio_push_event(indio_dev, + chan->differential ? + IIO_DIFF_EVENT_CODE(IIO_VOLTAGE, + 2 * i, + 2 * i + 1, + IIO_EV_TYPE_THRESH, + IIO_EV_DIR_EITHER) : + IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, + i, + IIO_EV_TYPE_THRESH, + IIO_EV_DIR_EITHER), + timestamp); + } + + if (st->spi->irq && st->buffered_read_enabled) + mod_timer(&st->alert_timer, + jiffies + + msecs_to_jiffies(st->alert_reset_timeout_ms)); + + return IRQ_HANDLED; +} + static int ad7380_probe(struct spi_device *spi) { struct device *dev = &spi->dev; @@ -1361,6 +1674,39 @@ static int ad7380_probe(struct spi_device *spi) indio_dev->modes = INDIO_DIRECT_MODE; indio_dev->available_scan_masks = st->chip_info->available_scan_masks; + if (spi->irq) { + struct iio_chan_spec *chans; + size_t size; + int ret; + + ret = devm_request_threaded_irq(dev, spi->irq, + &ad7380_primary_event_handler, + &ad7380_event_handler, + IRQF_TRIGGER_FALLING | IRQF_ONESHOT + | IRQF_NO_AUTOEN, + indio_dev->name, + indio_dev); + if (ret) + return dev_err_probe(dev, ret, "Failed to request irq\n"); + + atomic_set(&st->irq_enabled, 0); + + /* + * Copy channels to be able to update the event_spec, since some + * attributes are visible only when irq are configured + */ + size = indio_dev->num_channels * sizeof(*indio_dev->channels); + chans = devm_kzalloc(dev, size, GFP_KERNEL); + if (!chans) + return -ENOMEM; + + memcpy(chans, indio_dev->channels, size); + chans->event_spec = ad7380_events_irq; + chans->num_event_specs = ARRAY_SIZE(ad7380_events_irq); + + indio_dev->channels = chans; + } + ret = devm_iio_triggered_buffer_setup(dev, indio_dev, iio_pollfunc_store_time, ad7380_trigger_handler, From patchwork Tue Dec 24 09:34:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julien Stephan X-Patchwork-Id: 13919783 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 079C08C1F for ; Tue, 24 Dec 2024 09:34:40 +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=1735032882; cv=none; b=dL/KWBzmnohIHRvlDiDYOYMSH3ZHgsBjXhjxxJOOWauFle2gyKOSplQQHDDjC83gf1BtVV8FSUq8I+hfODLGhysARFCCtaze/qOoe/ElaUtEzMwlnAJCkPbHqEvSyYYcl9jAHA7OKK2r8hn2hj4jILUJNGSAW967AdwmwWxzAVQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735032882; c=relaxed/simple; bh=kSfw31r1V5uQjSD98r12a1K9VX6sZXrUEVT0pOUqXUs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=BzsSNp22GDl3aCe6XukM62Hp+tawFgBlmKsks/xLEtYSqlZm8rPXM+WthZAnYCcifXfBW1FH4RUA3DP4q0bgkC7Pcdwq+Y8S2tQDm1/UqHqonTXQcW24CuXu9dm7mcJfn/a4t036TlowvnIzYEQgzTis+nXYsnpK6XrTUCdXPJc= 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=hMgg89VJ; 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="hMgg89VJ" Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-434e3953b65so33702765e9.1 for ; Tue, 24 Dec 2024 01:34:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1735032879; x=1735637679; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=GHxfnHco609cw6bEt34RvlmgEA4BmkTtw1PkuvrIB18=; b=hMgg89VJFfdSLoQ6EhKxa9tBhfG/nJbolXDU6sG+4/rs8h0RvKxSy7yYDgVnyINm8e 0hoteQZbF40WwtdxbJs9YqanMoUlB5t5CU7bSCNC6bvHgZY666WR2Zj5eD9Gtjv9vteF 6Huc98CxQaKh1K5dClTNZq9e3YteGNlv5FyomgGwjaS9WbF69NleKNuuZG784WRhw6kL ea0ZgJ1Z2Sq65YV22j3M/xWWnMLEDClKFR0+2PDMfRDSndemHGwuFuL8u+ODYGx0gOGY U620eWEMVABRr4y/0pvcuCFSHvAe74yAodKm+8x5uqrl8nqrxxe5GulAFaV8kJjRVcwg pnWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735032879; x=1735637679; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GHxfnHco609cw6bEt34RvlmgEA4BmkTtw1PkuvrIB18=; b=xJVhUbX7xR9ZlBSZopExyZ8p1nwdBirSvizu5D1T+nPkGpZ5q9w8XljHqWZTzETxFj 3DHH1dN1C/nLNMbdccccrIt0QpeGqMm2lOduTCUI6uPW8tu+rY+iSZ+rkjdKTJZv06pg CZiJf4SUrHczQnxY8o6IkI8czj6GPZrbQurBarEJdJ+ySKpORvHK87/5fwDkcNOEz8LM LeHylSNjHYF0rjfkAdlShcfI2iF3ppCFmyj+ToMBehLOJiE9keMyJlod+wc/hpZ9sVi+ /zhfb1K9mobczXx0U5EOYixAXGXdwFZfHwuYhSCOohF3uD4jquxJrWB3ofRRdIEoE9Nc hoSg== X-Gm-Message-State: AOJu0Yw8SRtMmZoNiKYamdxGvEAa8C5CLoCBbpXIafOL2oD2wRRPcbP/ +8jC9zQcZOzP3qFj1RBXkT8r+7oI+3UQJcfNW1kgW61UWfMaLvXNDMeOiM52OFw= X-Gm-Gg: ASbGncsI2QHzi47ncG+Vw8T/Z8PGQU+EbQZem6RUmBV2t2jCNU/W4LvCwuuaVrgzlpV 6VOO9uyWfbKXp/ZOwl623SeC5NBE/Y+ls4bSp36KkYG65cYKGSjLwg1Z2hgu0oEa6/CLYsoLJE5 KMdo9Wn/89LPeMjlsNvgvGRwYTUWL8Tm7lzX+MUHrG1gRCZHueiJ4nFFEAfH5rJTl2QnScudOmJ b16vG+DtyodI0BxsdQw/l+q0YhCAqkuaQXbPsIMCeyx+CL6wLFnjTqs72aHJNsB0nDgNko1Vty+ NRF8eU9pQ30gvVd5nwKTQX/kgvlSIMv1hmqc4SxicpK7QIP6ug== X-Google-Smtp-Source: AGHT+IGZX699OSrVyMHZfxCNhIMRgOtdkxq4kcRZIVIIzs0qAsHeLAPMigr0o64lDLP6J6HC9mM9hg== X-Received: by 2002:a05:600c:1f85:b0:434:9936:c823 with SMTP id 5b1f17b1804b1-43668646741mr142737805e9.18.1735032879107; Tue, 24 Dec 2024 01:34:39 -0800 (PST) Received: from [192.168.1.62] (2a02-842a-d52e-6101-6fd0-06c4-5d68-f0a5.rev.sfr.net. [2a02:842a:d52e:6101:6fd0:6c4:5d68:f0a5]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43661219a7csm160932535e9.24.2024.12.24.01.34.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Dec 2024 01:34:38 -0800 (PST) From: Julien Stephan Date: Tue, 24 Dec 2024 10:34:33 +0100 Subject: [PATCH RFC v2 4/4] docs: iio: ad7380: add alert support Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241224-ad7380-add-alert-support-v2-4-7c89b2bf7cb3@baylibre.com> References: <20241224-ad7380-add-alert-support-v2-0-7c89b2bf7cb3@baylibre.com> In-Reply-To: <20241224-ad7380-add-alert-support-v2-0-7c89b2bf7cb3@baylibre.com> To: Lars-Peter Clausen , Michael Hennerich , =?utf-8?q?Nuno_S=C3=A1?= , David Lechner , Jonathan Cameron , Jonathan Corbet Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Julien Stephan X-Mailer: b4 0.14.2 Add a section for alert support, explaining how user can use iio events attributes to enable alert and set thresholds, but also what kind of events will be generated. Signed-off-by: Julien Stephan --- Documentation/iio/ad7380.rst | 56 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/Documentation/iio/ad7380.rst b/Documentation/iio/ad7380.rst index c46127700e14ca9ec3cac0bd5776b6702f2659e2..7cca4a9ad42ae8b3fda2e063ffd864ffa9dac2f1 100644 --- a/Documentation/iio/ad7380.rst +++ b/Documentation/iio/ad7380.rst @@ -92,6 +92,61 @@ must restart iiod using the following command: root:~# systemctl restart iiod +Alert +----- + +When configured in 1 SDO line mode (see `SPI wiring modes`_), the SDOB or the +SDOD line (respectively for the 2 or 4 channels variants) can act as an alert +pin. + +At the end of a conversion the low-active alert pin gets asserted if the +conversion result exceeds the alert high limit or falls below the alert low +limit. It is cleared, on a falling edge of CS. The alert pin is common to all +channels. + +User can enable alert using the regular iio events attribute: + +.. code-block:: bash + + events/thresh_either_en + +The high and low thresholds are common to all channels and can also be set using +regular iio events attributes: + +.. code-block:: bash + + events/in_thresh_falling_value + events/in_thresh_rising_value + +User space IIO events +~~~~~~~~~~~~~~~~~~~~~ + +If the ``interrupts`` property is set in the device tree, IIO events will be +generated for alerts. A register identifies the faulty channel, and direction, +but during buffered reads, registers are inaccessible, making it impossible to +know the exact channel triggering the alert. A generic event is sent for each +channel, resulting in 2 or 4 events per alert, depending on the number of +channels: + +.. code-block:: bash + + Event: time: 1733501917162945723, type: voltage, channel: 0-1, evtype: thresh, direction: either + Event: time: 1733501917162945723, type: voltage, channel: 2-3, evtype: thresh, direction: either + + +Alert Reset timeout (buffered read only) +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +In buffered reads, if input stays out of thresholds limit, an interrupt will be +generated for each sample read, because the alert pin is cleared when CS get +active (i.e when starting a new conversion). As a result, excessive event +generation can occur. User can set a reset timeout in milliseconds, during +which interrupt will be disabled: + +.. code-block:: bash + + events/thresh_either_reset_timeout + Channel selection and sequencer (single-end chips only) ------------------------------------------------------- @@ -144,7 +199,6 @@ Unimplemented features - Rolling average oversampling - Power down mode - CRC indication -- Alert Device buffers