From nobody Fri Feb 13 15:06:39 2026 Received: from mail-oa1-f51.google.com (mail-oa1-f51.google.com [209.85.160.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 5078457333 for ; Fri, 24 May 2024 20:18:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716581902; cv=none; b=LLHipP5vXnWFKL03hfjzVMc2ibaurcR4vksUoxbmltfR+I4+0hRY1VH4xPVGllsp1eARECwCYNr4QcrYuE6X5YUxklvUwiOQ8A6lt+9jQBIeYW2FF+140um5zVw5egPkqStjFmybSGl3yT+riKo9i/CTCrCPTOYXijZ6+eHJzKI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716581902; c=relaxed/simple; bh=bYYuHzRG311cDXeGndQWVZB69Of4dQS40MzsRUiyPbI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=lqX14q7S2S8mPytnBrzRiRLtseiQupuU3jiTdxQPmhf9nyy0RuxIRMIWIjr3ESoCTPGeAkxe2Hj3gRFZq8uFmfk62vqo9UCtsCtKx8qAvuKkmmAxqypHS/uZlcY6fWhpUNmU+M23GfVQYxCfABTOhl++cpQuEN4cPW3stqqYdAI= 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=WPhpa23q; arc=none smtp.client-ip=209.85.160.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="WPhpa23q" Received: by mail-oa1-f51.google.com with SMTP id 586e51a60fabf-24c0dbd2866so2789724fac.0 for ; Fri, 24 May 2024 13:18:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1716581899; x=1717186699; 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=sykLvYr/gOGSRUZAM2N3tbLtBfP+F9YMWbHwRYeKemw=; b=WPhpa23qwfMo7teBUAK0BdEbd/JFQDNGGFLfqEr8Fe8KDlPC14qG136uvZpSY0UQYG 74Z1cSfY2RkgY67qmjN95JRzto6EJ6sKkSAEOM1DCI9DHLETZvi+5fCRZiqDDb+Oiu/B KoKOD2Fm5gCofLJ91XEP01JZDQOBoWmAANz/uN6LEWYJxNREdIfHEgcSOsIuNQzxdw87 GyESwK0KNvwzYARkdzSThA0BpFLk3sn5mtFZtzo4tLnEsDi0XiH23jQOpv+XHJLalfVJ 0YCaJdKN2Y/6j6drQs4rQCSn6b48ajyoHmQuu8KG87SmNMYQIpLbSxiVRX2/TfNUopQV Mjqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716581899; x=1717186699; 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=sykLvYr/gOGSRUZAM2N3tbLtBfP+F9YMWbHwRYeKemw=; b=KdxiTOYpv3S7BTWvR49LwD+jxj9AYvpAEr2G5uWQYJAE/Fu7BM34PD30ZHRuK2T3LQ Ux6FaK7ZVkl1pa3NjfvncqwU/12ow/apVq7Tkyf1t8JN9BF/BYUrAm2vCa9LfpvPazeg 5iCbVahwqn4zv2QOgUxwTSiocj2K8HxTyAKKPSDfgggqHUWoQt2m9gmn12m67du+5EX/ 94EGjl8T3JyVMcDDVfV8X6pem8PXqXfHhirxNj0rDGONI1gbwPRrcHZ4tV2NXhEwq5nn QCmYyJcAx0zYqdLjPwTDAH85j3kgkrx0qpxHO5vAb33E5h//eTbxc730sWZghJ7+1SBg +bsg== X-Forwarded-Encrypted: i=1; AJvYcCWDnugUKf3m1BYbbgR8a8B6RpyV9gln7wlXwdD1mgoHwfS6vD3MQvQ/lhyLb9Nx+fXH0zBdozGFvkhZ4LlpWyNzS7XqnhUzgF27XkLg X-Gm-Message-State: AOJu0Ywv5QI7UeOsY762JlGmEiZLxcFeQ5Q0vjBF7VoDTZvEWxdTT7hb T0Sa1SXUVBC4wDg8IEs3LxUTohkWD2/5YT5TMda2bBWdvF8dui9ebWyNlPkZ7yOJb+DH8KW4YxV vekQ= X-Google-Smtp-Source: AGHT+IHAevnZuRCsFSPJzWXtRe81dz5ChfteCBc9cIC38XtQdOL6ERojL4L2hC72yg+K4kj9dkbqAg== X-Received: by 2002:a05:6870:168e:b0:23d:1a0f:f034 with SMTP id 586e51a60fabf-24ca13dd501mr3512549fac.46.1716581898265; Fri, 24 May 2024 13:18: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 586e51a60fabf-24ca2840481sm516737fac.57.2024.05.24.13.18.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 13:18: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 v2 1/3] iio: introduce struct iio_scan_type Date: Fri, 24 May 2024 15:18:07 -0500 Message-ID: <20240524-iio-add-support-for-multiple-scan-types-v2-1-a6c328fdfab7@baylibre.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240524-iio-add-support-for-multiple-scan-types-v2-0-a6c328fdfab7@baylibre.com> References: <20240524-iio-add-support-for-multiple-scan-types-v2-0-a6c328fdfab7@baylibre.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" X-Mailer: b4 0.12.4 Content-Transfer-Encoding: quoted-printable 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 --- v2 changes: none --- 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; }; =20 +/** + * 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 specifi= c 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; --=20 2.45.1 From nobody Fri Feb 13 15:06:39 2026 Received: from mail-oo1-f44.google.com (mail-oo1-f44.google.com [209.85.161.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 25D18446AC for ; Fri, 24 May 2024 20:18:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716581903; cv=none; b=FO61A/J7ziULZILVe2kEF0HItFAVVAH95aH+sB9XF391nm9FwVKp7WJZfFFTe8PonGJmHWmOn7gthxUuM3PXm22W3KsJBAnVB6IdoowT5vWKXHW4FgagTwMwsuRVMl4NR1Y73EXIl3WCzpHHXF/VdQ3tY4Ez2MNe1kLMw0UjJSI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716581903; c=relaxed/simple; bh=eU0+2iBlqKN5TWgM1ZRRGbn4eWxUd48oagKUQKm0a2g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=t1tbOsJKRLsN9F5cppXvVThEA/MX3m+iDRp6UTg/348kPZHoZILCydKbjjOEQOuG5+Zt/onew2AwURuPg6G5XUvcs2p04prHoVAf5JHgJFq513PRBEElxbYeiYRMu5RbmFr6sOUgSUf2VqwpBNh9aBOLu0qYRVViWHgf/XFK3DA= 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=vV1Xlu3p; arc=none smtp.client-ip=209.85.161.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="vV1Xlu3p" Received: by mail-oo1-f44.google.com with SMTP id 006d021491bc7-5b970e90ab8so660688eaf.3 for ; Fri, 24 May 2024 13:18:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1716581899; x=1717186699; 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=pupJ0zX8Gz4rIa61ZbX42+dQviT6t2wR9g3cWXQmpmY=; b=vV1Xlu3plTJ7qyvbis7eF6SGYrHyEI/C4crImH5d1nhQ84EoOOpHuaOivkTxjDxH74 gOQRQZ+Jkea3Uhp3tINEZ5JzTrkcGW8yppjhjnA11XW/hwx5qcf15KMQsRjtEOX6quIU y6fRPyEA5u/FBYPVqtyXnrvr2KG6+TvNrmOoL2JAmNP+vo302btab9NBKCcMHoCLh5jz ox5/ry4hslr/ZolDGdAK0V/3qD/4sWvj7DNb8lxalbRzFcfWMtOq1nhukPV2fLnXdQv4 OuNFMozkbur7gT6hAmx9QX/x6QN6WsRaeZOgpL/i4fVvzVoX4KJGDV2PjRP0h3ZHAE8V H8cA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716581899; x=1717186699; 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=pupJ0zX8Gz4rIa61ZbX42+dQviT6t2wR9g3cWXQmpmY=; b=fHmdmTzmj3U0M0qNXH9DiRJ37O9ssPw0KCHv5RqyBnz8XWRDnUN+AyA92aXAV4vxiw mwCnuiXDQc10SSaeBm/9R+mTrdlbyHXMCSQGVILhAQUEUPiwWipRUCW2r3h7rEXhg17X 0JCl3BDE473YXg+f/kp+bCHn5hnWH3d/IKdOCcAWFLUAa6iOxkn3Oz3fRSHy2em9nNv9 WuG7n3gxINsUJQuceAnVhOAc5duyPoGxr81bIDgAP2mcx+ABrkuuRnOReQ2dJ1ydRjb6 pec0hCRokeLVa8gZLT7tZ2DRjVo4m6uSbOV4VOaKae6V89DpMdg3hBPjAZzK8qoqattv oYvQ== X-Forwarded-Encrypted: i=1; AJvYcCXO6LXdgKE3Gi9or96PqoLLKcoxf6x6Tz2BdOx/cASZzTFxIIi9WcbHmwNsoOK6wtvxkJNbPJ/zKy/x53wewbk+YbTBMPeDFsMCvd7U X-Gm-Message-State: AOJu0YxBee97eOFjgzoxpiCxi4G4UPosS9Fe0TU8A7JSlGEpq1tfKIJq BY8CYtL741sLHf/It0q760NPD77N4/6cKHMQ8bXbqNgMYYh6oNign8lp2eDH+3Qw3B8ElhDEIk3 R8P8= X-Google-Smtp-Source: AGHT+IHSCklJR+QvpB+nBZa7D5RN+L/XEcvrpg4e2IPoWqNA5iezNgR3a6t19KbxWGvWM5hZCpJ8vQ== X-Received: by 2002:a05:6871:5810:b0:24e:6d33:9e26 with SMTP id 586e51a60fabf-24e6d33a0eemr1842132fac.15.1716581899115; Fri, 24 May 2024 13:18: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 586e51a60fabf-24ca2840481sm516737fac.57.2024.05.24.13.18.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 13:18: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 v2 2/3] iio: buffer: use struct iio_scan_type to simplify code Date: Fri, 24 May 2024 15:18:08 -0500 Message-ID: <20240524-iio-add-support-for-multiple-scan-types-v2-2-a6c328fdfab7@baylibre.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240524-iio-add-support-for-multiple-scan-types-v2-0-a6c328fdfab7@baylibre.com> References: <20240524-iio-add-support-for-multiple-scan-types-v2-0-a6c328fdfab7@baylibre.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" X-Mailer: b4 0.12.4 Content-Transfer-Encoding: quoted-printable By using struct iio_scan_type, we can simplify the code by removing lots of duplicate pointer dereferences. 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 --- v2 changes: * fixed spelling of dereferences in commit message --- 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-b= uffer.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 =3D to_iio_dev_attr(attr); - u8 type =3D this_attr->c->scan_type.endianness; + const struct iio_scan_type *scan_type =3D &this_attr->c->scan_type; + u8 type =3D scan_type->endianness; =20 if (type =3D=3D IIO_CPU) { #ifdef __LITTLE_ENDIAN @@ -375,21 +376,21 @@ static ssize_t iio_show_fixed_type(struct device *dev, type =3D 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); } =20 static ssize_t iio_scan_el_show(struct device *dev, @@ -694,12 +695,16 @@ static unsigned int iio_storage_bytes_for_si(struct i= io_dev *indio_dev, unsigned int scan_index) { const struct iio_chan_spec *ch; + const struct iio_scan_type *scan_type; unsigned int bytes; =20 ch =3D iio_find_channel_from_si(indio_dev, scan_index); - bytes =3D ch->scan_type.storagebits / 8; - if (ch->scan_type.repeat > 1) - bytes *=3D ch->scan_type.repeat; + scan_type =3D &ch->scan_type; + bytes =3D scan_type->storagebits / 8; + + if (scan_type->repeat > 1) + bytes *=3D scan_type->repeat; + return bytes; } =20 @@ -1616,18 +1621,21 @@ static int __iio_buffer_alloc_sysfs_and_mask(struct= iio_buffer *buffer, if (channels) { /* new magic */ for (i =3D 0; i < indio_dev->num_channels; i++) { + const struct iio_scan_type *scan_type; + if (channels[i].scan_index < 0) continue; =20 + scan_type =3D &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 =3D -EINVAL; goto error_cleanup_dynamic; } --=20 2.45.1 From nobody Fri Feb 13 15:06:39 2026 Received: from mail-oa1-f43.google.com (mail-oa1-f43.google.com [209.85.160.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E5D326F06D for ; Fri, 24 May 2024 20:18:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716581902; cv=none; b=UCdOad0RXXbuGAgV+FxwrI/RoEBTe9AP3VeiueGCpX0DP4NuNrjjmFBfXBProrRWhd0A0SaIH+qbz4uE5hUoNNhIe3yfxdfvaWeTWKOIlQ8CZ9uXbCa8oiNb41IZL2AUIdomk+aYYcDFaiS+NicXVPPER/nUN2ACVqPfYRUofeA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716581902; c=relaxed/simple; bh=k1DKLc8pxxgg1AYnYH88mtuOhL38TUJJ1mKFETHH1fA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=gqNOUutQhG1Uo8FsMMtF2UoBEE9PVWHN82/gbXvYWeKJFWTXN5dOGAXbpJyQReOQt9vH7nyJvOGc4g5oF4HSiFTcPgSWycsi3jZhrJm313QB56zUS2XvR/TPyi1FqLTIkpj6zp0YYIxdjibg8ykhk6iUps9/mUi5rEDdixKRMGE= 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=zDACARjf; arc=none smtp.client-ip=209.85.160.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="zDACARjf" Received: by mail-oa1-f43.google.com with SMTP id 586e51a60fabf-24c9f628e71so658691fac.1 for ; Fri, 24 May 2024 13:18:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1716581900; x=1717186700; 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=VtPQmXhI59y3Nbnw6w8KYqVFNmGB3yypLsi3HdguYNI=; b=zDACARjfdqjOjcRVrgEr4LPCyzSWbiho95LWAlQFQdSZ5qPAEdiddcBihwh5PtBwdo CaQ3PtDCGveRP4g5NsPo4TpoVwrm9j30B+IVIToMrFyYI608sP5YpB8G8zhdwDFVslCK juHorLRb/CquMHAWGyGLeW3SESyOMiqKBQ8Crib0PDBAY03vSaVSWq42Dut2T7sqTx4i nkvOsrRhgsmkEtv3AWT+7Cd0FmqxjHgoaVm//BC7uM2KMHE6A7CaLZcYxmPGznGXQkpz 8C4vuUT50OLWLX2yQI8QFAGx4mWWpPXj+iYWGkbQgrqJXt22rZ9o5tt9dsjady1Ntj1z ygPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716581900; x=1717186700; 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=VtPQmXhI59y3Nbnw6w8KYqVFNmGB3yypLsi3HdguYNI=; b=TS6teuYlg+2kZKmZgrOEcN/XazZKzDX+p6xAMT/BI4i2ThtzggyutikHJUHUXkGmMZ 7g9T46mdYD/+PVDOcgrvil4E7ziCGaRSOKb6eWCM2AFq7gDO0W9N1o6Gnrmt7dCB5fdW ULDE2KluSRj3Cu64CxraTKdC769STZh7L+XDDSbHOn16ksXdPpF9pkMJnRRdT8ri2yCp mEIV9v0ewl6dZ3EcLbW/x4ETMjbqy7DqO0XKsaegmhMVwYmX94VPWStFA3CGlXAsKtLM fszf5HWuF/XjtluomyhGb2hHutIpjDQCaH7jT73MaxoxPK1gQIJN2gpkQwIVJ8JfD7ct 8GDA== X-Forwarded-Encrypted: i=1; AJvYcCWOTJXoQmEtqjV8dSF5HwkcEBpeWoW+YCLB76AzlyR3qjP0rpWAVZvcx/vHcSDUjbSYHmlgXDXKHwq91cytIwPCRkYH4muUfmFxkROp X-Gm-Message-State: AOJu0Ywa99TaPDBNU6PRWYBTgsJxLH+rLtnT0VHzVVlR0HMgubrfNj36 utnwbnZP8a5KIZL2KfBFCVPCn8WSwG1rhfDdGkKhB223cQ6bG2j/YLQriGCtI8o1nWj1URQAlif rQKw= X-Google-Smtp-Source: AGHT+IGLXY4Yk/y2rk2JX11F/FtbKuoZfvyir6vr6iXmAR717RZMaePwXVeT2QDqGKFN5iQ7RHolsg== X-Received: by 2002:a05:6870:8dcf:b0:23a:2ddc:30d4 with SMTP id 586e51a60fabf-24ca129cb9dmr3711786fac.23.1716581899941; Fri, 24 May 2024 13:18: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 586e51a60fabf-24ca2840481sm516737fac.57.2024.05.24.13.18.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 13:18: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 v2 3/3] iio: add support for multiple scan types per channel Date: Fri, 24 May 2024 15:18:09 -0500 Message-ID: <20240524-iio-add-support-for-multiple-scan-types-v2-3-a6c328fdfab7@baylibre.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240524-iio-add-support-for-multiple-scan-types-v2-0-a6c328fdfab7@baylibre.com> References: <20240524-iio-add-support-for-multiple-scan-types-v2-0-a6c328fdfab7@baylibre.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" X-Mailer: b4 0.12.4 Content-Transfer-Encoding: quoted-printable 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 need to implement the new callback get_current_scan_type() to resolve the scan type for a given channel based on the current state of the driver. There is a new scan_type_ext field in the iio_channel structure that should be used to store the scan types for any channel that has more than one. There is also a new flag has_ext_scan_type that acts as a type discriminator for the scan_type/ext_scan_type union. A union is used so that we don't grow the size of the iio_channel structure and also makes it clear that scan_type and ext_scan_type are mutually exclusive. 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 --- v2 changes: * use union for scan_type and ext_scan_type/num_ext_scan_types * add has_ext_scan_type flag for type discrimination * add extra validation check for get_current_scan_type when has_ext_scan_type is set --- drivers/iio/industrialio-buffer.c | 61 ++++++++++++++++++++++++++++++-----= ---- include/linux/iio/iio.h | 46 +++++++++++++++++++++++++++-- 2 files changed, 91 insertions(+), 16 deletions(-) diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-b= uffer.c index 08103a9e77f7..0c6ee5610987 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 =3D dev_to_iio_dev(dev); struct iio_dev_attr *this_attr =3D to_iio_dev_attr(attr); - const struct iio_scan_type *scan_type =3D &this_attr->c->scan_type; + const struct iio_scan_type *scan_type =3D + iio_get_current_scan_type(indio_dev, this_attr->c); u8 type =3D scan_type->endianness; =20 if (type =3D=3D IIO_CPU) { @@ -699,7 +701,7 @@ static unsigned int iio_storage_bytes_for_si(struct iio= _dev *indio_dev, unsigned int bytes; =20 ch =3D iio_find_channel_from_si(indio_dev, scan_index); - scan_type =3D &ch->scan_type; + scan_type =3D iio_get_current_scan_type(indio_dev, ch); bytes =3D scan_type->storagebits / 8; =20 if (scan_type->repeat > 1) @@ -1597,6 +1599,22 @@ static long iio_device_buffer_ioctl(struct iio_dev *= indio_dev, struct file *filp } } =20 +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) @@ -1626,18 +1644,33 @@ static int __iio_buffer_alloc_sysfs_and_mask(struct= iio_buffer *buffer, if (channels[i].scan_index < 0) continue; =20 - scan_type =3D &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 =3D -EINVAL; - goto error_cleanup_dynamic; + if (channels[i].has_ext_scan_type) { + int j; + + /* + * get_current_scan_type is required when using + * extended scan types. + */ + if (!indio_dev->info->get_current_scan_type) { + ret =3D -EINVAL; + goto error_cleanup_dynamic; + } + + for (j =3D 0; j < channels[i].num_ext_scan_type; j++) { + scan_type =3D &channels[i].ext_scan_type[j]; + + ret =3D iio_channel_validate_scan_type( + &indio_dev->dev, i, scan_type); + if (ret) + goto error_cleanup_dynamic; + } + } else { + scan_type =3D &channels[i].scan_type; + + ret =3D iio_channel_validate_scan_type( + &indio_dev->dev, i, scan_type); + if (ret) + goto error_cleanup_dynamic; } =20 ret =3D iio_buffer_add_channel_sysfs(indio_dev, buffer, diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h index 19de573a944a..bbcc36abc3ad 100644 --- a/include/linux/iio/iio.h +++ b/include/linux/iio/iio.h @@ -204,7 +204,13 @@ struct iio_scan_type { * @address: Driver specific identifier. * @scan_index: Monotonic index to give ordering in scans when read * from a buffer. - * @scan_type: struct describing the scan type + * @scan_type: struct describing the scan type - mutually exclusive + * with ext_scan_type. + * @ext_scan_type: Used in rare cases where there is more than one scan + * format for a channel. When this is used, the flag + * has_ext_scan_type must be set and the driver must + * implement get_current_scan_type in struct iio_info. + * @num_ext_scan_type: Number of elements in ext_scan_type. * @info_mask_separate: What information is to be exported that is specifi= c to * this channel. * @info_mask_separate_available: What availability information is to be @@ -248,6 +254,7 @@ struct iio_scan_type { * attributes but not for event codes. * @output: Channel is output. * @differential: Channel is differential. + * @has_ext_scan_type: True if ext_scan_type is used instead of scan_type. */ struct iio_chan_spec { enum iio_chan_type type; @@ -255,7 +262,13 @@ struct iio_chan_spec { int channel2; unsigned long address; int scan_index; - struct iio_scan_type scan_type; + union { + struct iio_scan_type scan_type; + struct { + 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; @@ -273,6 +286,7 @@ struct iio_chan_spec { unsigned indexed:1; unsigned output:1; unsigned differential:1; + unsigned has_ext_scan_type:1; }; =20 =20 @@ -435,6 +449,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_sca= n_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 +536,9 @@ struct iio_info { =20 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 +824,28 @@ static inline bool iio_read_acpi_mount_matrix(struct d= evice *dev, } #endif =20 +/** + * 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 (chan->has_ext_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= ); =20 int iio_str_to_fixpoint(const char *str, int fract_mult, int *integer, --=20 2.45.1