From patchwork Tue May 7 19:02:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Lechner X-Patchwork-Id: 13657619 Received: from mail-oi1-f175.google.com (mail-oi1-f175.google.com [209.85.167.175]) (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 DC2BB16D9AF for ; Tue, 7 May 2024 19:02:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715108540; cv=none; b=q/4WAWvAru+LxDThqC/yLYSmQkslRQVfvXz3oxFAns/H4sBkFe1v4SQWh5kVpFdy/lQpW8njvp4v16oDabSiPjIuFnxHxZNcrtH1vibOivKbuhrGqyMYnyIlHwhf/+lyMQ5F1uOF9mTcQxF0deCvsrau+PIY5fh8mMLsmeutV78= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715108540; c=relaxed/simple; bh=ZuQNDZ6FvEykzl/xun3pMRTdOGRALArKnmySgTgs6W4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=oovuoA6hdPJzANEasmQFsO+aR3ws/Kpe8aocJG00VVUE3FpL9XCTtjGQwzaVJ6ot2lvjK2Lne0gZMLv4QTkolq39muR6yRNoEKj32NCZHEL13pTXWovNkjzEZ4uvLTyUEkCLrMpP0RGf7I7+IYfFTjrnUEEHYfvLTPsmofAj9g8= 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=ZulPEtYj; arc=none smtp.client-ip=209.85.167.175 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="ZulPEtYj" Received: by mail-oi1-f175.google.com with SMTP id 5614622812f47-3c96be1ef20so1446440b6e.1 for ; Tue, 07 May 2024 12:02:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1715108538; x=1715713338; 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=wCGWhLT+GW7C3GUbTFxtCVYF67mxCVX333qfGkLE2u0=; b=ZulPEtYjD6TRCmGJEN9vUxlJw61b7U4ADye04UWDC4ICwuwbP55/RXw5MLQf6iZgt/ HHTk5+7jDM01vtJwPcNtg2C6CxuvhJA7STyhgBNwHux5GD4wVILcn0aYvwf3NmJ+a808 OnWfyP5mjY1mqmckXDDN0OmSIaLn/GK50SiT5rNC12t3voZNwImadJs3I9MGJJYmkjPU QR9NuIi22/8f1D3fpcctQNxFcD1Xcec8gPLK628yTBojYBygPCbDapT/v5v0MgxF9Xvr plt/cHrrTtNtvZPMaXoR12rqIC3WPeFxfXFy7PHewaKTUGVeOPcfwxGQZGFexd2sRDta T+Aw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715108538; x=1715713338; 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=wCGWhLT+GW7C3GUbTFxtCVYF67mxCVX333qfGkLE2u0=; b=RGp5cX4dlZRXaw6zolBTaQtwAtzD0AEr5qFDPxWMob+TeQlj6z23DHqAdAa+kJ8LCs TktoZFiIQmIU5uARvOkxmd1jV2YJoZlVG04w6rvWO8TfXZc8skmpk0TO1SOJ5pYwd9S9 GcjyJGJTRfxFB5xRQrzfIHm1QtG4hqzz63eetcJe49cA7g/9BdAwzlieasp6H1qWnUxH C5KpWQjMFNPRs32u7maA9PBZkxa9dWV8XL+JMOU6BkQwTNDfTuayZk3zuCY7lnHFUj5Q 0bVmox/isPgvE5vTBJ1kZAlUZVcCbkpFM+eeo4jnC6ID/wSvX0yrIaBniZM0BdrveYA8 j9lw== X-Forwarded-Encrypted: i=1; AJvYcCWZErwuaKTwMjca68A/YTIK+KngQwab3uOtbMLK16DJm5ynlQ26sQnsY7Pdlzl7KEzx58FMmftHxWs4Jps2oU0hTfM/VeEzpgRD X-Gm-Message-State: AOJu0YzGeaLZqY7pWgaKs3G8oA7I+xt6QXbeeGJTQPy7VuMntVRqJ8Fb 3BdUGmsxx4nUjGC2hfTfsixC6Al6rpJb1mhkfx18ZKbtSaE9UbXr3n5X/zapIIk= X-Google-Smtp-Source: AGHT+IFL6ERS1/RYKsFupGgA1xBg56o5QNJtLC1lE9gU+5b2z3/U/CF3gHCNHmI4JJsZ2InfuQGgaw== X-Received: by 2002:a05:6808:1b21:b0:3c9:6d1c:81db with SMTP id 5614622812f47-3c98532c341mr532151b6e.56.1715108537916; Tue, 07 May 2024 12:02:17 -0700 (PDT) Received: from freyr.lechnology.com (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id j14-20020a54480e000000b003c96bbe0e79sm909652oij.13.2024.05.07.12.02.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 12:02:17 -0700 (PDT) From: David Lechner To: Jonathan Cameron Cc: David Lechner , Michael Hennerich , =?utf-8?q?Nuno_S=C3=A1?= , Julien Stephan , Esteban Blanc , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH RFC 1/4] iio: introduce struct iio_scan_type Date: Tue, 7 May 2024 14:02:05 -0500 Message-ID: <20240507-iio-add-support-for-multiple-scan-types-v1-1-95ac33ee51e9@baylibre.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240507-iio-add-support-for-multiple-scan-types-v1-0-95ac33ee51e9@baylibre.com> References: <20240507-iio-add-support-for-multiple-scan-types-v1-0-95ac33ee51e9@baylibre.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Mailer: b4 0.12.4 This gives the channel scan_type a named type so that it can be used to simplify code in later commits. Signed-off-by: David Lechner --- include/linux/iio/iio.h | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h index 55e2b22086a1..19de573a944a 100644 --- a/include/linux/iio/iio.h +++ b/include/linux/iio/iio.h @@ -173,6 +173,27 @@ struct iio_event_spec { unsigned long mask_shared_by_all; }; +/** + * struct iio_scan_type - specification for channel data format in buffer + * @sign: 's' or 'u' to specify signed or unsigned + * @realbits: Number of valid bits of data + * @storagebits: Realbits + padding + * @shift: Shift right by this before masking out realbits. + * @repeat: Number of times real/storage bits repeats. When the + * repeat element is more than 1, then the type element in + * sysfs will show a repeat value. Otherwise, the number + * of repetitions is omitted. + * @endianness: little or big endian + */ +struct iio_scan_type { + char sign; + u8 realbits; + u8 storagebits; + u8 shift; + u8 repeat; + enum iio_endian endianness; +}; + /** * struct iio_chan_spec - specification of a single channel * @type: What type of measurement is the channel making. @@ -184,17 +205,6 @@ struct iio_event_spec { * @scan_index: Monotonic index to give ordering in scans when read * from a buffer. * @scan_type: struct describing the scan type - * @scan_type.sign: 's' or 'u' to specify signed or unsigned - * @scan_type.realbits: Number of valid bits of data - * @scan_type.storagebits: Realbits + padding - * @scan_type.shift: Shift right by this before masking out - * realbits. - * @scan_type.repeat: Number of times real/storage bits repeats. - * When the repeat element is more than 1, then - * the type element in sysfs will show a repeat - * value. Otherwise, the number of repetitions - * is omitted. - * @scan_type.endianness: little or big endian * @info_mask_separate: What information is to be exported that is specific to * this channel. * @info_mask_separate_available: What availability information is to be @@ -245,14 +255,7 @@ struct iio_chan_spec { int channel2; unsigned long address; int scan_index; - struct { - char sign; - u8 realbits; - u8 storagebits; - u8 shift; - u8 repeat; - enum iio_endian endianness; - } scan_type; + struct iio_scan_type scan_type; long info_mask_separate; long info_mask_separate_available; long info_mask_shared_by_type; From patchwork Tue May 7 19:02:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Lechner X-Patchwork-Id: 13657620 Received: from mail-oi1-f175.google.com (mail-oi1-f175.google.com [209.85.167.175]) (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 84A3216D9B5 for ; Tue, 7 May 2024 19:02:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715108541; cv=none; b=LclM6GBD2I3NJb4iHu+AhszUmACeF4Uh/I9gP3JcDwvSU4D+l9n1dutODpACV3LZi2wixxkuqWTcbc91jmMfROqvhdaADXTp4yy2uagYD+kylqkh0nTFCa7Fl2tQnYhkiNxvfD2pAcnEY+u5XFnytnGM5BmLpc29rI1EEzgGcdg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715108541; c=relaxed/simple; bh=lPUQfi00hz3EZzfmxjqGDaeJ2LGzL0z8Fh1NDCHiCqI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=LT/WlILyT+QGJGnJhKFuKKphTB056KdVNlgg9fQhV76bGCwZdWsXbE74GsEMtXMXbDsLeECsxdQYmSpBcBs/jOtaUt05INjyqGy60eOSqDwTkGt65JX1B3M4BG2mIHPgk6Ei1wfbUdv8HeSkA+utMjcxdAG6hbdYxXi9lkkf7kY= 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=1qfWtfeW; arc=none smtp.client-ip=209.85.167.175 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="1qfWtfeW" Received: by mail-oi1-f175.google.com with SMTP id 5614622812f47-3c968f64cdeso884561b6e.0 for ; Tue, 07 May 2024 12:02:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1715108539; x=1715713339; 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=gusPUkvJw+HUxHjWbJgthYj3tkXrP9EigZmtbqwV49I=; b=1qfWtfeWufUUgUDqjw6SEulZOWdeKJ+rTduwhcuw7lLk50AKcN9IhNYkXgiXpIcogS Umxm3QY+i8+eVvYL5NhBoIJF/Xr4FZ2fqFX2w9NbXFDYdCl1CvnTZwtWk8svzZYN/yDu ucFLajGzaHMl/IXl55S88WOTzNydkfounRXn7sweAW10DAxhbVwgTcMCfErfoxZ1BNCP c5B7kPZ/QWpRXD75W9hEzNYi5s10IhBRgDxkh1lFRxf6TW5DMzAWoz05mr56s94b6VvA /XeaOcTt0dfcYnG8E4vmic30+K6/zKSI9Hhj6motWbA8l8VBummhEkbPERjqAAiAheMq TLvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715108539; x=1715713339; 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=gusPUkvJw+HUxHjWbJgthYj3tkXrP9EigZmtbqwV49I=; b=pOlAwQGI8aPniZiY6kkqTW6e/SD62t8YK0clHU0Qx4XTljl7E+udrF0WTMQBaFxZaZ PIpTnn1h5RyNyzNkEvCKhi4EVbwk30IA/2OjbfNYbp6H0CB4kDCMHesj4NfDb4ZXhbPS BHfqBNZ5PA3JYkWrzbQxdAJbYDflqXIs8cEj+K7tqCS6Q93yHnSFixCS4mhrmNY0KxaY OsinqIXNI6WAtCXYBUEFy2mI3sApfTE4rRE5nPmQtQXP/wuMPOJsNfogXTlGTKdj4ycK VPL2kzel3G2bkQ1EfToNmJP88Xi8FyFtYq42KSd5ZAPpGy/ZcXs8gjVQgeul0o7KEng4 xvAQ== X-Forwarded-Encrypted: i=1; AJvYcCVvhtfRamQoeych8p81cLs1Sg1f8sAlNLK+ym9rgbdDZJ6hlmqBHmIq8zTRczJ0BN56cJEb7xbP1YajD6oI0yTvhaH1M2n5ko3L X-Gm-Message-State: AOJu0Yz1GBIg1mpuHp0yQJzjcYeJFsBzF83LbL8qpoIkfuFOxovEHIWu RKOQ7+TL8jh9PNKZgs1WJc/viwTDKzocLzG7HZPCMRiR3KXYdQGHTRy5OxtGaxs= X-Google-Smtp-Source: AGHT+IFHtu+tsKrhcPKjEbahD4o9LGQo0jYOer1kDmQ9GUynkWoVDP2V0354NOUJT5/NuXaQabwhyw== X-Received: by 2002:a05:6808:202:b0:3c9:7013:85c3 with SMTP id 5614622812f47-3c9852aac11mr598484b6e.9.1715108538793; Tue, 07 May 2024 12:02:18 -0700 (PDT) Received: from freyr.lechnology.com (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id j14-20020a54480e000000b003c96bbe0e79sm909652oij.13.2024.05.07.12.02.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 12:02:18 -0700 (PDT) From: David Lechner To: Jonathan Cameron Cc: David Lechner , Michael Hennerich , =?utf-8?q?Nuno_S=C3=A1?= , Julien Stephan , Esteban Blanc , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH RFC 2/4] iio: buffer: use struct iio_scan_type to simplify code Date: Tue, 7 May 2024 14:02:06 -0500 Message-ID: <20240507-iio-add-support-for-multiple-scan-types-v1-2-95ac33ee51e9@baylibre.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240507-iio-add-support-for-multiple-scan-types-v1-0-95ac33ee51e9@baylibre.com> References: <20240507-iio-add-support-for-multiple-scan-types-v1-0-95ac33ee51e9@baylibre.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Mailer: b4 0.12.4 By using struct iio_scan_type, we can simplify the code by removing lots of duplicate pointer deferences. This make the code a bit easier to read. This also prepares for a future where channels may have more than one scan_type. Signed-off-by: David Lechner --- drivers/iio/industrialio-buffer.c | 48 +++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c index cec58a604d73..08103a9e77f7 100644 --- a/drivers/iio/industrialio-buffer.c +++ b/drivers/iio/industrialio-buffer.c @@ -366,7 +366,8 @@ static ssize_t iio_show_fixed_type(struct device *dev, char *buf) { struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); - u8 type = this_attr->c->scan_type.endianness; + const struct iio_scan_type *scan_type = &this_attr->c->scan_type; + u8 type = scan_type->endianness; if (type == IIO_CPU) { #ifdef __LITTLE_ENDIAN @@ -375,21 +376,21 @@ static ssize_t iio_show_fixed_type(struct device *dev, type = IIO_BE; #endif } - if (this_attr->c->scan_type.repeat > 1) + if (scan_type->repeat > 1) return sysfs_emit(buf, "%s:%c%d/%dX%d>>%u\n", iio_endian_prefix[type], - this_attr->c->scan_type.sign, - this_attr->c->scan_type.realbits, - this_attr->c->scan_type.storagebits, - this_attr->c->scan_type.repeat, - this_attr->c->scan_type.shift); + scan_type->sign, + scan_type->realbits, + scan_type->storagebits, + scan_type->repeat, + scan_type->shift); else return sysfs_emit(buf, "%s:%c%d/%d>>%u\n", iio_endian_prefix[type], - this_attr->c->scan_type.sign, - this_attr->c->scan_type.realbits, - this_attr->c->scan_type.storagebits, - this_attr->c->scan_type.shift); + scan_type->sign, + scan_type->realbits, + scan_type->storagebits, + scan_type->shift); } static ssize_t iio_scan_el_show(struct device *dev, @@ -694,12 +695,16 @@ static unsigned int iio_storage_bytes_for_si(struct iio_dev *indio_dev, unsigned int scan_index) { const struct iio_chan_spec *ch; + const struct iio_scan_type *scan_type; unsigned int bytes; ch = iio_find_channel_from_si(indio_dev, scan_index); - bytes = ch->scan_type.storagebits / 8; - if (ch->scan_type.repeat > 1) - bytes *= ch->scan_type.repeat; + scan_type = &ch->scan_type; + bytes = scan_type->storagebits / 8; + + if (scan_type->repeat > 1) + bytes *= scan_type->repeat; + return bytes; } @@ -1616,18 +1621,21 @@ static int __iio_buffer_alloc_sysfs_and_mask(struct iio_buffer *buffer, if (channels) { /* new magic */ for (i = 0; i < indio_dev->num_channels; i++) { + const struct iio_scan_type *scan_type; + if (channels[i].scan_index < 0) continue; + scan_type = &channels[i].scan_type; + /* Verify that sample bits fit into storage */ - if (channels[i].scan_type.storagebits < - channels[i].scan_type.realbits + - channels[i].scan_type.shift) { + if (scan_type->storagebits < + scan_type->realbits + scan_type->shift) { dev_err(&indio_dev->dev, "Channel %d storagebits (%d) < shifted realbits (%d + %d)\n", - i, channels[i].scan_type.storagebits, - channels[i].scan_type.realbits, - channels[i].scan_type.shift); + i, scan_type->storagebits, + scan_type->realbits, + scan_type->shift); ret = -EINVAL; goto error_cleanup_dynamic; } From patchwork Tue May 7 19:02:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Lechner X-Patchwork-Id: 13657622 Received: from mail-oi1-f181.google.com (mail-oi1-f181.google.com [209.85.167.181]) (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 A7DA516D9B7 for ; Tue, 7 May 2024 19:02:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715108542; cv=none; b=XLb+hZBO1pahcjUkX9gzL+UqiIY2Jy/XK6Bvj+CY7unNBwilPF2Pt3XDWkdsX6XBQqr0Kmwyv0VbPrp0aQkaFNW/PMLnif4cX7O9sSgBqeygHW672y1sdXS+IDAT1xLwz2JAq9e2baa5yoTokVOsds8yJ8+mY7g+bTXbMDkG9x8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715108542; c=relaxed/simple; bh=Fn1xbpXsu4PAcmCL4wCw7+fLXWr43hwXXVU0yqQAbdw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=RkuYMAhzTeg3MRG/2yf8S8GniA7HYG40SA9KCy7HqBFGvSTedHqzEkw+n/NtHRGKwpeMUg9WfzUXb2Wd0RkFm16ADZkySeiRrXnRS7kcr/jitzo4gPXfOWItHP5PqrgYSbipxftGrPMjfybpl2DSHMkj49xugGSazW6xuec4Pfk= 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=GHvNY/NQ; arc=none smtp.client-ip=209.85.167.181 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="GHvNY/NQ" Received: by mail-oi1-f181.google.com with SMTP id 5614622812f47-3c86f066256so1990690b6e.2 for ; Tue, 07 May 2024 12:02:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1715108539; x=1715713339; 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=Zva8gT55IALq82V9YRVJAnVfhuhJYt5YJF4Qc5Cxi6Y=; b=GHvNY/NQZo+CQQgda26XuIvNNy6w6ux4LR1BizIemG/ioiNlpQUMAAngs6ljWPgmIg oRrWbsOAB6SPA2kQJvoMIV1lTEvzd7zOjYEhYY65IjmnvrPtxAA7KgZNuTkS5+mvTQPd QuRMOVOJlXjMYm5bsjGkYgzqoeT3veGT2QrM++SBFUqzwJE8IbvF8jZCHRV/qud+7hbw ssZ9zgOlOk+7FFItAFygJ7/axZ3R4U4MnY1hDIg1LYsvUU620s0Xu7KPs9xB7JR2JvhQ lpb/AOYvxghbScH0MBNS8NGqNESZ2Ju7shsqccLNknyMITH3TSpzQoICpsF4nOlb5XTe 6YBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715108539; x=1715713339; 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=Zva8gT55IALq82V9YRVJAnVfhuhJYt5YJF4Qc5Cxi6Y=; b=Di8Zs9Y5+fU9s0QvOcfLIBPGyEbv0s2/kLuEtXMS+MnDEodLk11aFxSEZ/IOCqnhjg DWXaEwT/j1lplxbbOmRyuIkQ+6yOUNO8FeeM2nNN18LxTBW7CvMa6FT5iqwtaiScXaCT wTkNxI6UiqOdGNARkTZKHEbccikHTJ6rhuvIrGHQlccK/LCNoyoS2qHp0aa0b8QQcAm6 dAs7/7SQT3D9KdO3975PJfWvMNUrKXpGYfO2F5+nw0lSI0zuWnbFoP4uxnImeLzLc1/R WszPELuuMvltNMehdoigVUjpMihj83VRTkvH247MNFn3s8bvvxSnx/x6ge1H48rp+riu 7Hzw== X-Forwarded-Encrypted: i=1; AJvYcCXs9o6xCSkbET+xNdO3Vr9Lo6vBMQhXNTq1seKS0btBSqvRxUOCeGuJiMfBVwNsRm1a7Y+Ld0Ce3H6pfXPEqkFQMfPNZu/S7qDw X-Gm-Message-State: AOJu0YxzDoUE8/nGzKZeFwUj+6+g749eShtHwFi+ucU0/SlH/s8nU7Af 3DwdzveaJh1cTT89WneXYdDLy6Wywc5G6UHopLwXpVm+yXnf+b+y+Oy++EMqrok= X-Google-Smtp-Source: AGHT+IH9wjghj4HrhmVD/ukOy+0Q7pKFtsk6AEg9jWoiV/hwRi2mH9xji406zXVARSpFy9wOTkJbuQ== X-Received: by 2002:aca:1e06:0:b0:3c5:f4b3:e0e3 with SMTP id 5614622812f47-3c985305afdmr446220b6e.41.1715108539681; Tue, 07 May 2024 12:02:19 -0700 (PDT) Received: from freyr.lechnology.com (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id j14-20020a54480e000000b003c96bbe0e79sm909652oij.13.2024.05.07.12.02.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 12:02:19 -0700 (PDT) From: David Lechner To: Jonathan Cameron Cc: David Lechner , Michael Hennerich , =?utf-8?q?Nuno_S=C3=A1?= , Julien Stephan , Esteban Blanc , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH RFC 3/4] iio: add support for multiple scan types per channel Date: Tue, 7 May 2024 14:02:07 -0500 Message-ID: <20240507-iio-add-support-for-multiple-scan-types-v1-3-95ac33ee51e9@baylibre.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240507-iio-add-support-for-multiple-scan-types-v1-0-95ac33ee51e9@baylibre.com> References: <20240507-iio-add-support-for-multiple-scan-types-v1-0-95ac33ee51e9@baylibre.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Mailer: b4 0.12.4 This adds new fields to the iio_channel structure to support multiple scan types per channel. This is useful for devices that support multiple resolution modes or other modes that require different data formats of the raw data. To make use of this, drivers can still use the old scan_type field for the "default" scan type and use the new scan_type_ext field for any additional scan types. And they must implement the new callback get_current_scan_type() to return the current scan type based on the current state of the device. The buffer code is the only code in the IIO core code that is using the scan_type field. This patch updates the buffer code to use the new iio_channel_validate_scan_type() function to ensure it is returning the correct scan type for the current state of the device when reading the sysfs attributes. The buffer validation code is also update to validate any additional scan types that are set in the scan_type_ext field. Part of that code is refactored to a new function to avoid duplication. Signed-off-by: David Lechner --- drivers/iio/industrialio-buffer.c | 43 +++++++++++++++++++++++++++++---------- include/linux/iio/iio.h | 33 ++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 11 deletions(-) diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c index 08103a9e77f7..ef27ce71ec25 100644 --- a/drivers/iio/industrialio-buffer.c +++ b/drivers/iio/industrialio-buffer.c @@ -365,8 +365,10 @@ static ssize_t iio_show_fixed_type(struct device *dev, struct device_attribute *attr, char *buf) { + struct iio_dev *indio_dev = dev_to_iio_dev(dev); struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); - const struct iio_scan_type *scan_type = &this_attr->c->scan_type; + const struct iio_scan_type *scan_type = + iio_get_current_scan_type(indio_dev, this_attr->c); u8 type = scan_type->endianness; if (type == IIO_CPU) { @@ -699,7 +701,7 @@ static unsigned int iio_storage_bytes_for_si(struct iio_dev *indio_dev, unsigned int bytes; ch = iio_find_channel_from_si(indio_dev, scan_index); - scan_type = &ch->scan_type; + scan_type = iio_get_current_scan_type(indio_dev, ch); bytes = scan_type->storagebits / 8; if (scan_type->repeat > 1) @@ -1597,6 +1599,22 @@ static long iio_device_buffer_ioctl(struct iio_dev *indio_dev, struct file *filp } } +static int iio_channel_validate_scan_type(struct device *dev, int ch, + const struct iio_scan_type *scan_type) +{ + /* Verify that sample bits fit into storage */ + if (scan_type->storagebits < scan_type->realbits + scan_type->shift) { + dev_err(dev, + "Channel %d storagebits (%d) < shifted realbits (%d + %d)\n", + ch, scan_type->storagebits, + scan_type->realbits, + scan_type->shift); + return -EINVAL; + } + + return 0; +} + static int __iio_buffer_alloc_sysfs_and_mask(struct iio_buffer *buffer, struct iio_dev *indio_dev, int index) @@ -1622,22 +1640,25 @@ static int __iio_buffer_alloc_sysfs_and_mask(struct iio_buffer *buffer, /* new magic */ for (i = 0; i < indio_dev->num_channels; i++) { const struct iio_scan_type *scan_type; + int j; if (channels[i].scan_index < 0) continue; scan_type = &channels[i].scan_type; - /* Verify that sample bits fit into storage */ - if (scan_type->storagebits < - scan_type->realbits + scan_type->shift) { - dev_err(&indio_dev->dev, - "Channel %d storagebits (%d) < shifted realbits (%d + %d)\n", - i, scan_type->storagebits, - scan_type->realbits, - scan_type->shift); - ret = -EINVAL; + ret = iio_channel_validate_scan_type(&indio_dev->dev, + i, scan_type); + if (ret) goto error_cleanup_dynamic; + + for (j = 0; j < channels[i].num_ext_scan_type; j++) { + scan_type = &channels[i].ext_scan_type[j]; + + ret = iio_channel_validate_scan_type( + &indio_dev->dev, i, scan_type); + if (ret) + goto error_cleanup_dynamic; } ret = iio_buffer_add_channel_sysfs(indio_dev, buffer, diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h index 19de573a944a..66f0b4c68f53 100644 --- a/include/linux/iio/iio.h +++ b/include/linux/iio/iio.h @@ -205,6 +205,9 @@ struct iio_scan_type { * @scan_index: Monotonic index to give ordering in scans when read * from a buffer. * @scan_type: struct describing the scan type + * @ext_scan_type: Used in rare cases where there is more than one scan + * format for a channel. When this is used, omit scan_type. + * @num_ext_scan_type: Number of elements in ext_scan_type. * @info_mask_separate: What information is to be exported that is specific to * this channel. * @info_mask_separate_available: What availability information is to be @@ -256,6 +259,8 @@ struct iio_chan_spec { unsigned long address; int scan_index; struct iio_scan_type scan_type; + const struct iio_scan_type *ext_scan_type; + unsigned int num_ext_scan_type; long info_mask_separate; long info_mask_separate_available; long info_mask_shared_by_type; @@ -435,6 +440,9 @@ struct iio_trigger; /* forward declaration */ * for better event identification. * @validate_trigger: function to validate the trigger when the * current trigger gets changed. + * @get_current_scan_type: must be implemented by drivers that use ext_scan_type + * in the channel spec to return the currently active scan + * type based on the current state of the device. * @update_scan_mode: function to configure device and scan buffer when * channels have changed * @debugfs_reg_access: function to read or write register value of device @@ -519,6 +527,9 @@ struct iio_info { int (*validate_trigger)(struct iio_dev *indio_dev, struct iio_trigger *trig); + const struct iio_scan_type *(*get_current_scan_type)( + const struct iio_dev *indio_dev, + const struct iio_chan_spec *chan); int (*update_scan_mode)(struct iio_dev *indio_dev, const unsigned long *scan_mask); int (*debugfs_reg_access)(struct iio_dev *indio_dev, @@ -804,6 +815,28 @@ static inline bool iio_read_acpi_mount_matrix(struct device *dev, } #endif +/** + * iio_get_current_scan_type - Get the current scan type for a channel + * @indio_dev: the IIO device to get the scan type for + * @chan: the channel to get the scan type for + * + * Most devices only have one scan type per channel and can just access it + * directly without calling this function. Core IIO code and drivers that + * implement ext_scan_type in the channel spec should use this function to + * get the current scan type for a channel. + * + * Returns: the current scan type for the channel + */ +static inline const struct iio_scan_type *iio_get_current_scan_type( + const struct iio_dev *indio_dev, + const struct iio_chan_spec *chan) +{ + if (indio_dev->info->get_current_scan_type) + return indio_dev->info->get_current_scan_type(indio_dev, chan); + + return &chan->scan_type; +} + ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals); int iio_str_to_fixpoint(const char *str, int fract_mult, int *integer, From patchwork Tue May 7 19:02:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Lechner X-Patchwork-Id: 13657623 Received: from mail-oi1-f180.google.com (mail-oi1-f180.google.com [209.85.167.180]) (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 7FB8016D9BA for ; Tue, 7 May 2024 19:02:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715108543; cv=none; b=luZ2TMae+FleDQJxMtCgMXZNcV4sXx8gCjdFhs39GqwjdcEW7GvzqPYs+WXyh0yLOdH3WhRU2WGNmxAzeNMuPvx+hW2A5EE5UkeZOYXZh//qZit4HRrjaORkA+IygKt6im0WL/rsN65yyqcJl8fqqTqmR5BpxAjBzvu951THe+g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715108543; c=relaxed/simple; bh=EvBNYEvN8j9ghLH+OpWG+7kKxj7ybV0QTyNby5uwafc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=mMWr6AkdeqzduQo2e875xwvyAP5xSqb7th+YYKP45RDtT5L8ThKwQN45D3GH4IvNANx/F8cK+VMmor9pSTdPvNuu/U9i9DD3gOeGaPhBfNAQvVJ+HEPYkCclJUZ3KAC0r2C2vLj/bmehUjyT8BE2hgI0iySZF70YU83sjgpt9eg= 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=STpyEbxS; arc=none smtp.client-ip=209.85.167.180 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="STpyEbxS" Received: by mail-oi1-f180.google.com with SMTP id 5614622812f47-3c7513a991cso2380785b6e.1 for ; Tue, 07 May 2024 12:02:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1715108540; x=1715713340; 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=a6XxGU1FlTtbSR4iab/xAALxEuyhck2Pa/6k2+UASj0=; b=STpyEbxScFOnzzYRHs7pw5GJxWtY6RlLlsBsBBbegyFGBgKdDOAKjXJpe/Swfh0eXU 0FUpcD3QglCO5DRqdK3dqJKqW5uJHDTvaNdPl2w7HTw3VF9WjmzoN8tKt7sY7Zv+S2aO iOJgu5X6nCR+ZjCxmlU5hR76yWYwDrX6sWMcoVwzhAOsAbIDWbJzcruj0ZBbLasT+XyJ xfEl8y8RH/5wj3jNQVQ8td2YwFW8v7E32F9ljrUx9ZNM1tRV0OcfUMUShm+cZEZ9N02e BejfDFaLyIN+fuOyDYEsBUPOqqkUbEHoM5ZuPaeSRbOLiPpvHk3/AbvhekBSOgCKewOv U0hw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715108540; x=1715713340; 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=a6XxGU1FlTtbSR4iab/xAALxEuyhck2Pa/6k2+UASj0=; b=RrCz8b7+J5BBg+l/G5Jnh2hgfE/84Aq9i2/WFPfnVIAyhNHGD/YGLW8cnVeTfkGYDe aqttajHzwGmwg42y6pI4p8EVcj0nftrEZ9PkVDlA8SUesaSWu2tXzcukMK1ToKVp3hdO O3a7r+w/Sf+zFZN5h5g0kUXa32y3obolTxR+uCwEWLpYsqT4Y+mqVdKu5BB6ChtcYyTj qTZDyUhV2+vzhLWZlakxVPQ2lQoYbD4gGpMwasnPM26AU8gMVsfO/gjitlOTz2tqWv+v 2W8cpB0BsPDCeMt5p+hr+eRRxcfwL/MP+qtu2/FTCwOzaFkXh24iEFJzTHJPxDavzgFo 8znA== X-Forwarded-Encrypted: i=1; AJvYcCVDbN41X4AsycNcrfTO6e1N8MnGjp9lRrXbUUUvCJ/N6+wxfYRbRmklSskaYX5XcHGRaDEW363NG6gHTsAO7CKMgm98C2RjDXpP X-Gm-Message-State: AOJu0YyrvdLGDhGvbDR6zx45MbE35F2tZfHTkkBjMIHHR0XhSN0cDzYi hgwkpT8ZhD03vUyluj2Temk5ZZVBupy3kBFv60NIjB6PkfoVwBDNHvhz0aU+RcqQS5lmD8lLcYu Jd1s= X-Google-Smtp-Source: AGHT+IHv02i3bUWe97W+AMQ6Wm/DpdvU7UL0PD2+AVRJMmCdz/EkTQYkuN2YVA4GHU0hKqTEA6xPtQ== X-Received: by 2002:a05:6808:6292:b0:3c9:6bf7:bb7f with SMTP id 5614622812f47-3c985309de7mr415150b6e.48.1715108540566; Tue, 07 May 2024 12:02:20 -0700 (PDT) Received: from freyr.lechnology.com (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id j14-20020a54480e000000b003c96bbe0e79sm909652oij.13.2024.05.07.12.02.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 12:02:20 -0700 (PDT) From: David Lechner To: Jonathan Cameron Cc: David Lechner , Michael Hennerich , =?utf-8?q?Nuno_S=C3=A1?= , Julien Stephan , Esteban Blanc , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH RFC 4/4] iio: adc: ad7380: add support for multiple scan type Date: Tue, 7 May 2024 14:02:08 -0500 Message-ID: <20240507-iio-add-support-for-multiple-scan-types-v1-4-95ac33ee51e9@baylibre.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240507-iio-add-support-for-multiple-scan-types-v1-0-95ac33ee51e9@baylibre.com> References: <20240507-iio-add-support-for-multiple-scan-types-v1-0-95ac33ee51e9@baylibre.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Mailer: b4 0.12.4 The AD783x chips have a resolution boost feature that allows for 2 extra bits of resolution. Previously, we had to choose a scan type to fit the largest resolution and manipulate the raw data to fit when the resolution was lower. This patch adds support for multiple scan types for the voltage input channels so that we can support both resolutions without having to manipulate the raw data. Signed-off-by: David Lechner --- drivers/iio/adc/ad7380.c | 185 ++++++++++++++++++++++------------------------- 1 file changed, 86 insertions(+), 99 deletions(-) diff --git a/drivers/iio/adc/ad7380.c b/drivers/iio/adc/ad7380.c index e240098708e9..ca317e3a72d9 100644 --- a/drivers/iio/adc/ad7380.c +++ b/drivers/iio/adc/ad7380.c @@ -89,14 +89,22 @@ struct ad7380_chip_info { const struct ad7380_timing_specs *timing_specs; }; -/* - * realbits/storagebits cannot be dynamically changed, so in order to - * support the resolution boost (additional 2 bits of resolution) - * we need to set realbits/storagebits to the maximum value i.e : - * - realbits = 16 + 2 = 18, and storagebits = 32 for 16-bits chips - * - realbits = 14 + 2 = 16, and storagebits = 16 for 14-bits chips - * We need to adjust the scale depending on resolution boost status - */ +/** scan type for 14-bit chips with resolution boost enabled. */ +static const struct iio_scan_type ad7380_scan_type_14_boost = { + .sign = 's', + .realbits = 16, + .storagebits = 16, + .endianness = IIO_CPU, +}; + +/** scan type for 16-bit chips with resolution boost enabled. */ +static const struct iio_scan_type ad7380_scan_type_16_boost = { + .sign = 's', + .realbits = 18, + .storagebits = 32, + .endianness = IIO_CPU, +}; + #define AD7380_CHANNEL(index, bits, diff) { \ .type = IIO_VOLTAGE, \ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ @@ -113,10 +121,12 @@ struct ad7380_chip_info { .scan_index = (index), \ .scan_type = { \ .sign = 's', \ - .realbits = (bits) + 2, \ - .storagebits = ((bits) + 2 > 16) ? 32 : 16, \ + .realbits = (bits), \ + .storagebits = ((bits) > 16) ? 32 : 16, \ .endianness = IIO_CPU, \ }, \ + .ext_scan_type = &ad7380_scan_type_##bits##_boost, \ + .num_ext_scan_type = 1, \ } #define DEFINE_AD7380_2_CHANNEL(name, bits, diff) \ @@ -376,67 +386,62 @@ static int ad7380_debugfs_reg_access(struct iio_dev *indio_dev, u32 reg, unreachable(); } -static int ad7380_prepare_spi_xfer(struct ad7380_state *st, struct spi_transfer *xfer) +/** + * Reads one set of samples from the device. This is a simultaneous sampling + * chip, so all channels are always read at the same time. + * + * On successful return, the raw data is stored in st->scan_data.raw. + */ +static int ad7380_read_one_sample(struct ad7380_state *st, + const struct iio_scan_type *scan_type) { - int bits_per_word; - - memset(xfer, 0, sizeof(*xfer)); - - xfer->rx_buf = &st->scan_data.raw; - - if (st->resolution_boost_enable == RESOLUTION_BOOST_ENABLE) - bits_per_word = st->chip_info->channels[0].scan_type.realbits; - else - bits_per_word = st->chip_info->channels[0].scan_type.realbits - 2; - - xfer->bits_per_word = bits_per_word; + struct spi_transfer xfers[2] = { + /* toggle CS (no data xfer) to trigger a conversion */ + { + .speed_hz = AD7380_REG_WR_SPEED_HZ, + .bits_per_word = scan_type->realbits, + .delay = { + .value = T_CONVERT_NS, + .unit = SPI_DELAY_UNIT_NSECS, + }, + .cs_change = 1, + .cs_change_delay = { + .value = st->chip_info->timing_specs->t_csh_ns, + .unit = SPI_DELAY_UNIT_NSECS, + }, + }, + { + .rx_buf = &st->scan_data.raw, + .len = BITS_TO_BYTES(scan_type->storagebits) * + (st->chip_info->num_channels - 1), + .bits_per_word = scan_type->realbits, + }, + }; - xfer->len = (st->chip_info->num_channels - 1) * BITS_TO_BYTES(bits_per_word); + /* + * In normal average oversampling we need to wait for multiple conversions to be done + */ + if (st->oversampling_mode == OS_MODE_NORMAL_AVERAGE && st->oversampling_ratio > 1) + xfers[0].delay.value = T_CONVERT_NS + 500 * st->oversampling_ratio; - return bits_per_word; + return spi_sync_transfer(st->spi, xfers, ARRAY_SIZE(xfers)); } static irqreturn_t ad7380_trigger_handler(int irq, void *p) { struct iio_poll_func *pf = p; struct iio_dev *indio_dev = pf->indio_dev; + const struct iio_chan_spec *chan = &indio_dev->channels[0]; + const struct iio_scan_type *scan_type = iio_get_current_scan_type( + indio_dev, chan); struct ad7380_state *st = iio_priv(indio_dev); - struct spi_transfer xfer; - int bits_per_word, realbits, i, ret; + int ret; - realbits = st->chip_info->channels[0].scan_type.realbits; - bits_per_word = ad7380_prepare_spi_xfer(st, &xfer); - ret = spi_sync_transfer(st->spi, &xfer, 1); + ret = ad7380_read_one_sample(st, scan_type); if (ret) goto out; - /* - * If bits_per_word == realbits (resolution boost enabled), we don't - * need to manipulate the raw data, otherwise we may need to fix things - * up a bit to fit the scan_type specs - */ - if (bits_per_word < realbits) { - if (realbits > 16 && bits_per_word <= 16) { - /* - * Here realbits > 16 so storagebits is 32 and bits_per_word is <= 16 - * so we need to sign extend u16 to u32 using reverse order to - * avoid writing over union data - */ - for (i = st->chip_info->num_channels - 2; i >= 0; i--) - st->scan_data.raw.u32[i] = sign_extend32(st->scan_data.raw.u16[i], - bits_per_word - 1); - } else if (bits_per_word < 16) { - /* - * Here realbits <= 16 so storagebits is 16. - * We only need to sign extend only if bits_per_word is < 16 - */ - for (i = 0; i < st->chip_info->num_channels - 1; i++) - st->scan_data.raw.u16[i] = sign_extend32(st->scan_data.raw.u16[i], - bits_per_word - 1); - } - } - iio_push_to_buffers_with_timestamp(indio_dev, &st->scan_data, pf->timestamp); @@ -447,47 +452,21 @@ static irqreturn_t ad7380_trigger_handler(int irq, void *p) } static int ad7380_read_direct(struct ad7380_state *st, - struct iio_chan_spec const *chan, int *val) + struct iio_chan_spec const *chan, + const struct iio_scan_type *scan_type, + int *val) { - struct spi_transfer xfers[2] = { - /* toggle CS (no data xfer) to trigger a conversion */ - { - .speed_hz = AD7380_REG_WR_SPEED_HZ, - .bits_per_word = chan->scan_type.realbits, - .delay = { - .value = T_CONVERT_NS, - .unit = SPI_DELAY_UNIT_NSECS, - }, - .cs_change = 1, - .cs_change_delay = { - .value = st->chip_info->timing_specs->t_csh_ns, - .unit = SPI_DELAY_UNIT_NSECS, - }, - }, - /* then read all channels, it will be filled by ad7380_prepare_spi_xfer */ - { - }, - }; - int bits_per_word, ret; - - /* - * In normal average oversampling we need to wait for multiple conversions to be done - */ - if (st->oversampling_mode == OS_MODE_NORMAL_AVERAGE && st->oversampling_ratio > 1) - xfers[0].delay.value = T_CONVERT_NS + 500 * st->oversampling_ratio; - - bits_per_word = ad7380_prepare_spi_xfer(st, &xfers[1]); + int ret; - ret = spi_sync_transfer(st->spi, xfers, ARRAY_SIZE(xfers)); - if (ret < 0) + ret = ad7380_read_one_sample(st, scan_type); + if (ret) return ret; - - if (bits_per_word > 16) + if (scan_type->storagebits > 16) *val = sign_extend32(st->scan_data.raw.u32[chan->scan_index], - bits_per_word - 1); + scan_type->realbits - 1); else *val = sign_extend32(st->scan_data.raw.u16[chan->scan_index], - bits_per_word - 1); + scan_type->realbits - 1); return IIO_VAL_INT; } @@ -496,18 +475,14 @@ static int ad7380_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int *val, int *val2, long info) { + const struct iio_scan_type *scan_type = iio_get_current_scan_type( + indio_dev, chan); struct ad7380_state *st = iio_priv(indio_dev); - int realbits; - - if (st->resolution_boost_enable == RESOLUTION_BOOST_ENABLE) - realbits = chan->scan_type.realbits; - else - realbits = chan->scan_type.realbits - 2; switch (info) { case IIO_CHAN_INFO_RAW: iio_device_claim_direct_scoped(return -EBUSY, indio_dev) { - return ad7380_read_direct(st, chan, val); + return ad7380_read_direct(st, chan, scan_type, val); } unreachable(); case IIO_CHAN_INFO_SCALE: @@ -520,7 +495,7 @@ static int ad7380_read_raw(struct iio_dev *indio_dev, * According to IIO ABI, offset is applied before scale, * so offset is: vcm_mv / scale */ - *val = st->vcm_mv[chan->channel] * (1 << realbits) + *val = st->vcm_mv[chan->channel] * (1 << scan_type->realbits) / st->vref_mv; return IIO_VAL_INT; @@ -700,6 +675,17 @@ static int ad7380_write_raw(struct iio_dev *indio_dev, } } +static const struct iio_scan_type *ad7380_get_current_scan_type( + const struct iio_dev *indio_dev, struct iio_chan_spec const *chan) +{ + struct ad7380_state *st = iio_priv(indio_dev); + + if (st->resolution_boost_enable && chan->num_ext_scan_type) + return chan->ext_scan_type; + + return &chan->scan_type; +} + static ssize_t oversampling_mode_show(struct device *dev, struct device_attribute *attr, char *buf) { @@ -796,6 +782,7 @@ 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, };