linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 00/13] iio: Use scan_type shift and realbits when processing raw data
@ 2021-11-04  8:24 Gwendal Grignou
  2021-11-04  8:24 ` [PATCH v2 01/13] iio: bma220: Use scan_type " Gwendal Grignou
                   ` (13 more replies)
  0 siblings, 14 replies; 18+ messages in thread
From: Gwendal Grignou @ 2021-11-04  8:24 UTC (permalink / raw)
  To: jic23, lars; +Cc: andy.shevchenko, linux-iio, Gwendal Grignou

Using scan_type has source of truth, use shift and realbits instead of
constants when processing reading sensor registers to produce raw sysfs
entries.
The same shit and realbits are already used by the libiio user-space
library to present channel information from device buffer.

Fix only a handful of drivers, where channel scan_type was accessible
in the function handling the raw data request.

In mpl3115, use a 16 bit big endian buffer when reading temperature
channel to improve readability.

Changes in v2:
- Split first CL, one CL for each driver
- Add realbit for sca3000 temperature channel
- Remove constant in bma220 driver.

Gwendal Grignou (13):
  iio: bma220: Use scan_type when processing raw data
  iio: kxcjk-1013: Use scan_type when processing raw data
  iio: mma7455: Use scan_type when processing raw data
  iio: sca3000: Use scan_type when processing raw data
  iio: stk8312: Use scan_type when processing raw data
  iio: stk8ba50: Use scan_type when processing raw data
  iio: ad7266: Use scan_type when processing raw data
  iio: at91-sama5d2: Use scan_type when processing raw data
  iio: ti-adc12138: Use scan_type when processing raw data
  iio: mag3110: Use scan_type when processing raw data
  iio: ti-ads1015: Remove shift variable ads1015_read_raw
  iio: xilinx-xadc-core: Use local variable in xadc_read_raw
  iio: mpl3115: Use scan_type.shift and realbit in mpl3115_read_raw

 drivers/iio/accel/bma220_spi.c     |  6 +++---
 drivers/iio/accel/kxcjk-1013.c     |  3 ++-
 drivers/iio/accel/mma7455_core.c   |  3 ++-
 drivers/iio/accel/sca3000.c        | 17 +++++++++++++----
 drivers/iio/accel/stk8312.c        |  2 +-
 drivers/iio/accel/stk8ba50.c       |  3 ++-
 drivers/iio/adc/ad7266.c           |  3 ++-
 drivers/iio/adc/at91-sama5d2_adc.c |  3 ++-
 drivers/iio/adc/ti-adc12138.c      |  3 ++-
 drivers/iio/adc/ti-ads1015.c       |  8 +++-----
 drivers/iio/adc/xilinx-xadc-core.c |  2 +-
 drivers/iio/magnetometer/mag3110.c |  6 ++++--
 drivers/iio/pressure/mpl3115.c     | 16 +++++++++++-----
 13 files changed, 48 insertions(+), 27 deletions(-)

-- 
2.33.1.1089.g2158813163f-goog


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [PATCH v2 01/13] iio: bma220: Use scan_type when processing raw data
  2021-11-04  8:24 [PATCH v2 00/13] iio: Use scan_type shift and realbits when processing raw data Gwendal Grignou
@ 2021-11-04  8:24 ` Gwendal Grignou
  2021-11-04  8:24 ` [PATCH v2 02/13] iio: kxcjk-1013: " Gwendal Grignou
                   ` (12 subsequent siblings)
  13 siblings, 0 replies; 18+ messages in thread
From: Gwendal Grignou @ 2021-11-04  8:24 UTC (permalink / raw)
  To: jic23, lars; +Cc: andy.shevchenko, linux-iio, Gwendal Grignou

Use channel definition as root of trust and replace constant
when reading elements directly using the raw sysfs attributes.

Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
---
 drivers/iio/accel/bma220_spi.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/accel/bma220_spi.c b/drivers/iio/accel/bma220_spi.c
index bc4c626e454d3..74024d7ce5ac2 100644
--- a/drivers/iio/accel/bma220_spi.c
+++ b/drivers/iio/accel/bma220_spi.c
@@ -27,7 +27,6 @@
 #define BMA220_CHIP_ID				0xDD
 #define BMA220_READ_MASK			BIT(7)
 #define BMA220_RANGE_MASK			GENMASK(1, 0)
-#define BMA220_DATA_SHIFT			2
 #define BMA220_SUSPEND_SLEEP			0xFF
 #define BMA220_SUSPEND_WAKE			0x00
 
@@ -45,7 +44,7 @@
 		.sign = 's',						\
 		.realbits = 6,						\
 		.storagebits = 8,					\
-		.shift = BMA220_DATA_SHIFT,				\
+		.shift = 2,						\
 		.endianness = IIO_CPU,					\
 	},								\
 }
@@ -125,7 +124,8 @@ static int bma220_read_raw(struct iio_dev *indio_dev,
 		ret = bma220_read_reg(data->spi_device, chan->address);
 		if (ret < 0)
 			return -EINVAL;
-		*val = sign_extend32(ret >> BMA220_DATA_SHIFT, 5);
+		*val = sign_extend32(ret >> chan->scan_type.shift,
+				     chan->scan_type.realbits - 1);
 		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_SCALE:
 		ret = bma220_read_reg(data->spi_device, BMA220_REG_RANGE);
-- 
2.33.1.1089.g2158813163f-goog


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v2 02/13] iio: kxcjk-1013: Use scan_type when processing raw data
  2021-11-04  8:24 [PATCH v2 00/13] iio: Use scan_type shift and realbits when processing raw data Gwendal Grignou
  2021-11-04  8:24 ` [PATCH v2 01/13] iio: bma220: Use scan_type " Gwendal Grignou
@ 2021-11-04  8:24 ` Gwendal Grignou
  2021-11-04  8:24 ` [PATCH v2 03/13] iio: mma7455: " Gwendal Grignou
                   ` (11 subsequent siblings)
  13 siblings, 0 replies; 18+ messages in thread
From: Gwendal Grignou @ 2021-11-04  8:24 UTC (permalink / raw)
  To: jic23, lars; +Cc: andy.shevchenko, linux-iio, Gwendal Grignou

Use channel definition as root of trust and replace constant
when reading elements directly using the raw sysfs attributes.

Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
---
 drivers/iio/accel/kxcjk-1013.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/accel/kxcjk-1013.c b/drivers/iio/accel/kxcjk-1013.c
index a51fdd3c9b5b5..88cf0c276893a 100644
--- a/drivers/iio/accel/kxcjk-1013.c
+++ b/drivers/iio/accel/kxcjk-1013.c
@@ -927,7 +927,8 @@ static int kxcjk1013_read_raw(struct iio_dev *indio_dev,
 				mutex_unlock(&data->mutex);
 				return ret;
 			}
-			*val = sign_extend32(ret >> 4, 11);
+			*val = sign_extend32(ret >> chan->scan_type.shift,
+					     chan->scan_type.realbits - 1);
 			ret = kxcjk1013_set_power_state(data, false);
 		}
 		mutex_unlock(&data->mutex);
-- 
2.33.1.1089.g2158813163f-goog


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v2 03/13] iio: mma7455: Use scan_type when processing raw data
  2021-11-04  8:24 [PATCH v2 00/13] iio: Use scan_type shift and realbits when processing raw data Gwendal Grignou
  2021-11-04  8:24 ` [PATCH v2 01/13] iio: bma220: Use scan_type " Gwendal Grignou
  2021-11-04  8:24 ` [PATCH v2 02/13] iio: kxcjk-1013: " Gwendal Grignou
@ 2021-11-04  8:24 ` Gwendal Grignou
  2021-11-04  8:24 ` [PATCH v2 04/13] iio: sca3000: " Gwendal Grignou
                   ` (10 subsequent siblings)
  13 siblings, 0 replies; 18+ messages in thread
From: Gwendal Grignou @ 2021-11-04  8:24 UTC (permalink / raw)
  To: jic23, lars; +Cc: andy.shevchenko, linux-iio, Gwendal Grignou

Use channel definition as root of trust and replace constant
when reading elements directly using the raw sysfs attributes.

Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
---
 drivers/iio/accel/mma7455_core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/accel/mma7455_core.c b/drivers/iio/accel/mma7455_core.c
index 777c6c384b09e..e6739ba74edfa 100644
--- a/drivers/iio/accel/mma7455_core.c
+++ b/drivers/iio/accel/mma7455_core.c
@@ -134,7 +134,8 @@ static int mma7455_read_raw(struct iio_dev *indio_dev,
 		if (ret)
 			return ret;
 
-		*val = sign_extend32(le16_to_cpu(data), 9);
+		*val = sign_extend32(le16_to_cpu(data),
+				     chan->scan_type.realbits - 1);
 
 		return IIO_VAL_INT;
 
-- 
2.33.1.1089.g2158813163f-goog


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v2 04/13] iio: sca3000: Use scan_type when processing raw data
  2021-11-04  8:24 [PATCH v2 00/13] iio: Use scan_type shift and realbits when processing raw data Gwendal Grignou
                   ` (2 preceding siblings ...)
  2021-11-04  8:24 ` [PATCH v2 03/13] iio: mma7455: " Gwendal Grignou
@ 2021-11-04  8:24 ` Gwendal Grignou
  2021-11-04  8:24 ` [PATCH v2 05/13] iio: stk8312: " Gwendal Grignou
                   ` (9 subsequent siblings)
  13 siblings, 0 replies; 18+ messages in thread
From: Gwendal Grignou @ 2021-11-04  8:24 UTC (permalink / raw)
  To: jic23, lars; +Cc: andy.shevchenko, linux-iio, Gwendal Grignou

Use channel definition as root of trust and replace constant
when reading elements directly using the raw sysfs attributes.

Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
---
 drivers/iio/accel/sca3000.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/drivers/iio/accel/sca3000.c b/drivers/iio/accel/sca3000.c
index c6b75308148aa..43ecacbdc95ae 100644
--- a/drivers/iio/accel/sca3000.c
+++ b/drivers/iio/accel/sca3000.c
@@ -534,6 +534,13 @@ static const struct iio_chan_spec sca3000_channels_with_temp[] = {
 			BIT(IIO_CHAN_INFO_OFFSET),
 		/* No buffer support */
 		.scan_index = -1,
+		.scan_type = {
+			.sign = 'u',
+			.realbits = 9,
+			.storagebits = 16,
+			.shift = 5,
+			.endianness = IIO_BE,
+		},
 	},
 	{
 		.type = IIO_ACCEL,
@@ -730,8 +737,9 @@ static int sca3000_read_raw(struct iio_dev *indio_dev,
 				mutex_unlock(&st->lock);
 				return ret;
 			}
-			*val = (be16_to_cpup((__be16 *)st->rx) >> 3) & 0x1FFF;
-			*val = sign_extend32(*val, 12);
+			*val = sign_extend32(be16_to_cpup((__be16 *)st->rx) >>
+					     chan->scan_type.shift,
+					     chan->scan_type.realbits - 1);
 		} else {
 			/* get the temperature when available */
 			ret = sca3000_read_data_short(st,
@@ -741,8 +749,9 @@ static int sca3000_read_raw(struct iio_dev *indio_dev,
 				mutex_unlock(&st->lock);
 				return ret;
 			}
-			*val = ((st->rx[0] & 0x3F) << 3) |
-			       ((st->rx[1] & 0xE0) >> 5);
+			*val = (be16_to_cpup((__be16 *)st->rx) >>
+				chan->scan_type.shift) &
+				GENMASK(chan->scan_type.realbits - 1, 0);
 		}
 		mutex_unlock(&st->lock);
 		return IIO_VAL_INT;
-- 
2.33.1.1089.g2158813163f-goog


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v2 05/13] iio: stk8312: Use scan_type when processing raw data
  2021-11-04  8:24 [PATCH v2 00/13] iio: Use scan_type shift and realbits when processing raw data Gwendal Grignou
                   ` (3 preceding siblings ...)
  2021-11-04  8:24 ` [PATCH v2 04/13] iio: sca3000: " Gwendal Grignou
@ 2021-11-04  8:24 ` Gwendal Grignou
  2021-11-04  8:24 ` [PATCH v2 06/13] iio: stk8ba50: " Gwendal Grignou
                   ` (8 subsequent siblings)
  13 siblings, 0 replies; 18+ messages in thread
From: Gwendal Grignou @ 2021-11-04  8:24 UTC (permalink / raw)
  To: jic23, lars; +Cc: andy.shevchenko, linux-iio, Gwendal Grignou

Use channel definition as root of trust and replace constant
when reading elements directly using the raw sysfs attributes.

Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
---
 drivers/iio/accel/stk8312.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/iio/accel/stk8312.c b/drivers/iio/accel/stk8312.c
index 43c621d0f11e4..de0cdf8c1f94c 100644
--- a/drivers/iio/accel/stk8312.c
+++ b/drivers/iio/accel/stk8312.c
@@ -355,7 +355,7 @@ static int stk8312_read_raw(struct iio_dev *indio_dev,
 			mutex_unlock(&data->lock);
 			return ret;
 		}
-		*val = sign_extend32(ret, 7);
+		*val = sign_extend32(ret, chan->scan_type.realbits - 1);
 		ret = stk8312_set_mode(data,
 				       data->mode & (~STK8312_MODE_ACTIVE));
 		mutex_unlock(&data->lock);
-- 
2.33.1.1089.g2158813163f-goog


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v2 06/13] iio: stk8ba50: Use scan_type when processing raw data
  2021-11-04  8:24 [PATCH v2 00/13] iio: Use scan_type shift and realbits when processing raw data Gwendal Grignou
                   ` (4 preceding siblings ...)
  2021-11-04  8:24 ` [PATCH v2 05/13] iio: stk8312: " Gwendal Grignou
@ 2021-11-04  8:24 ` Gwendal Grignou
  2021-11-04  8:24 ` [PATCH v2 07/13] iio: ad7266: " Gwendal Grignou
                   ` (7 subsequent siblings)
  13 siblings, 0 replies; 18+ messages in thread
From: Gwendal Grignou @ 2021-11-04  8:24 UTC (permalink / raw)
  To: jic23, lars; +Cc: andy.shevchenko, linux-iio, Gwendal Grignou

Use channel definition as root of trust and replace constant
when reading elements directly using the raw sysfs attributes.

Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
---
 drivers/iio/accel/stk8ba50.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/accel/stk8ba50.c b/drivers/iio/accel/stk8ba50.c
index e137a34b5c9a9..517c57ed9e949 100644
--- a/drivers/iio/accel/stk8ba50.c
+++ b/drivers/iio/accel/stk8ba50.c
@@ -227,7 +227,8 @@ static int stk8ba50_read_raw(struct iio_dev *indio_dev,
 			mutex_unlock(&data->lock);
 			return -EINVAL;
 		}
-		*val = sign_extend32(ret >> STK8BA50_DATA_SHIFT, 9);
+		*val = sign_extend32(ret >> chan->scan_type.shift,
+				     chan->scan_type.realbits - 1);
 		stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND);
 		mutex_unlock(&data->lock);
 		return IIO_VAL_INT;
-- 
2.33.1.1089.g2158813163f-goog


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v2 07/13] iio: ad7266: Use scan_type when processing raw data
  2021-11-04  8:24 [PATCH v2 00/13] iio: Use scan_type shift and realbits when processing raw data Gwendal Grignou
                   ` (5 preceding siblings ...)
  2021-11-04  8:24 ` [PATCH v2 06/13] iio: stk8ba50: " Gwendal Grignou
@ 2021-11-04  8:24 ` Gwendal Grignou
  2021-11-04  8:24 ` [PATCH v2 08/13] iio: at91-sama5d2: " Gwendal Grignou
                   ` (6 subsequent siblings)
  13 siblings, 0 replies; 18+ messages in thread
From: Gwendal Grignou @ 2021-11-04  8:24 UTC (permalink / raw)
  To: jic23, lars; +Cc: andy.shevchenko, linux-iio, Gwendal Grignou

Use channel definition as root of trust and replace constant
when reading elements directly using the raw sysfs attributes.

Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
---
 drivers/iio/adc/ad7266.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/adc/ad7266.c b/drivers/iio/adc/ad7266.c
index a8ec3efd659ed..1d345d66742d8 100644
--- a/drivers/iio/adc/ad7266.c
+++ b/drivers/iio/adc/ad7266.c
@@ -159,7 +159,8 @@ static int ad7266_read_raw(struct iio_dev *indio_dev,
 
 		*val = (*val >> 2) & 0xfff;
 		if (chan->scan_type.sign == 's')
-			*val = sign_extend32(*val, 11);
+			*val = sign_extend32(*val,
+					     chan->scan_type.realbits - 1);
 
 		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_SCALE:
-- 
2.33.1.1089.g2158813163f-goog


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v2 08/13] iio: at91-sama5d2: Use scan_type when processing raw data
  2021-11-04  8:24 [PATCH v2 00/13] iio: Use scan_type shift and realbits when processing raw data Gwendal Grignou
                   ` (6 preceding siblings ...)
  2021-11-04  8:24 ` [PATCH v2 07/13] iio: ad7266: " Gwendal Grignou
@ 2021-11-04  8:24 ` Gwendal Grignou
  2021-11-13 16:42   ` Jonathan Cameron
  2021-11-04  8:24 ` [PATCH v2 09/13] iio: ti-adc12138: " Gwendal Grignou
                   ` (5 subsequent siblings)
  13 siblings, 1 reply; 18+ messages in thread
From: Gwendal Grignou @ 2021-11-04  8:24 UTC (permalink / raw)
  To: jic23, lars; +Cc: andy.shevchenko, linux-iio, Gwendal Grignou

Use channel definition as root of trust and replace constant
when reading elements directly using the raw sysfs attributes.

Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
---
 drivers/iio/adc/at91-sama5d2_adc.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/adc/at91-sama5d2_adc.c b/drivers/iio/adc/at91-sama5d2_adc.c
index 4c922ef634f8e..92a57cf10fba4 100644
--- a/drivers/iio/adc/at91-sama5d2_adc.c
+++ b/drivers/iio/adc/at91-sama5d2_adc.c
@@ -1586,7 +1586,8 @@ static int at91_adc_read_info_raw(struct iio_dev *indio_dev,
 		*val = st->conversion_value;
 		ret = at91_adc_adjust_val_osr(st, val);
 		if (chan->scan_type.sign == 's')
-			*val = sign_extend32(*val, 11);
+			*val = sign_extend32(*val,
+					     chan->scan_type.realbits - 1);
 		st->conversion_done = false;
 	}
 
-- 
2.33.1.1089.g2158813163f-goog


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v2 09/13] iio: ti-adc12138: Use scan_type when processing raw data
  2021-11-04  8:24 [PATCH v2 00/13] iio: Use scan_type shift and realbits when processing raw data Gwendal Grignou
                   ` (7 preceding siblings ...)
  2021-11-04  8:24 ` [PATCH v2 08/13] iio: at91-sama5d2: " Gwendal Grignou
@ 2021-11-04  8:24 ` Gwendal Grignou
  2021-11-04  8:24 ` [PATCH v2 10/13] iio: mag3110: " Gwendal Grignou
                   ` (4 subsequent siblings)
  13 siblings, 0 replies; 18+ messages in thread
From: Gwendal Grignou @ 2021-11-04  8:24 UTC (permalink / raw)
  To: jic23, lars; +Cc: andy.shevchenko, linux-iio, Gwendal Grignou

Use channel definition as root of trust and replace constant
when reading elements directly using the raw sysfs attributes.

Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
---
 drivers/iio/adc/ti-adc12138.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/adc/ti-adc12138.c b/drivers/iio/adc/ti-adc12138.c
index fcd5d39dd03ea..5b5d452105393 100644
--- a/drivers/iio/adc/ti-adc12138.c
+++ b/drivers/iio/adc/ti-adc12138.c
@@ -239,7 +239,8 @@ static int adc12138_read_raw(struct iio_dev *iio,
 		if (ret)
 			return ret;
 
-		*value = sign_extend32(be16_to_cpu(data) >> 3, 12);
+		*value = sign_extend32(be16_to_cpu(data) >> channel->scan_type.shift,
+				       channel->scan_type.realbits - 1);
 
 		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_SCALE:
-- 
2.33.1.1089.g2158813163f-goog


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v2 10/13] iio: mag3110: Use scan_type when processing raw data
  2021-11-04  8:24 [PATCH v2 00/13] iio: Use scan_type shift and realbits when processing raw data Gwendal Grignou
                   ` (8 preceding siblings ...)
  2021-11-04  8:24 ` [PATCH v2 09/13] iio: ti-adc12138: " Gwendal Grignou
@ 2021-11-04  8:24 ` Gwendal Grignou
  2021-11-04  8:24 ` [PATCH v2 11/13] iio: ti-ads1015: Remove shift variable ads1015_read_raw Gwendal Grignou
                   ` (3 subsequent siblings)
  13 siblings, 0 replies; 18+ messages in thread
From: Gwendal Grignou @ 2021-11-04  8:24 UTC (permalink / raw)
  To: jic23, lars; +Cc: andy.shevchenko, linux-iio, Gwendal Grignou

Use channel definition as root of trust and replace constant
when reading elements directly using the raw sysfs attributes.

Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
---
 drivers/iio/magnetometer/mag3110.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/iio/magnetometer/mag3110.c b/drivers/iio/magnetometer/mag3110.c
index c96415a1aeadd..17c62d806218d 100644
--- a/drivers/iio/magnetometer/mag3110.c
+++ b/drivers/iio/magnetometer/mag3110.c
@@ -291,7 +291,8 @@ static int mag3110_read_raw(struct iio_dev *indio_dev,
 			if (ret < 0)
 				goto release;
 			*val = sign_extend32(
-				be16_to_cpu(buffer[chan->scan_index]), 15);
+				be16_to_cpu(buffer[chan->scan_index]),
+					    chan->scan_type.realbits - 1);
 			ret = IIO_VAL_INT;
 			break;
 		case IIO_TEMP: /* in 1 C / LSB */
@@ -306,7 +307,8 @@ static int mag3110_read_raw(struct iio_dev *indio_dev,
 			mutex_unlock(&data->lock);
 			if (ret < 0)
 				goto release;
-			*val = sign_extend32(ret, 7);
+			*val = sign_extend32(ret,
+					     chan->scan_type.realbits - 1);
 			ret = IIO_VAL_INT;
 			break;
 		default:
-- 
2.33.1.1089.g2158813163f-goog


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v2 11/13] iio: ti-ads1015: Remove shift variable ads1015_read_raw
  2021-11-04  8:24 [PATCH v2 00/13] iio: Use scan_type shift and realbits when processing raw data Gwendal Grignou
                   ` (9 preceding siblings ...)
  2021-11-04  8:24 ` [PATCH v2 10/13] iio: mag3110: " Gwendal Grignou
@ 2021-11-04  8:24 ` Gwendal Grignou
  2021-11-04  8:24 ` [PATCH v2 12/13] iio: xilinx-xadc-core: Use local variable in xadc_read_raw Gwendal Grignou
                   ` (2 subsequent siblings)
  13 siblings, 0 replies; 18+ messages in thread
From: Gwendal Grignou @ 2021-11-04  8:24 UTC (permalink / raw)
  To: jic23, lars; +Cc: andy.shevchenko, linux-iio, Gwendal Grignou

By using scan_type.realbits when processing raw data,
we use scan_type.shit only once, thus we don't need to define a local
variable for it anymore.

Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
---
 drivers/iio/adc/ti-ads1015.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/drivers/iio/adc/ti-ads1015.c b/drivers/iio/adc/ti-ads1015.c
index b0352e91ac165..b92d4cd1b8238 100644
--- a/drivers/iio/adc/ti-ads1015.c
+++ b/drivers/iio/adc/ti-ads1015.c
@@ -464,9 +464,7 @@ static int ads1015_read_raw(struct iio_dev *indio_dev,
 
 	mutex_lock(&data->lock);
 	switch (mask) {
-	case IIO_CHAN_INFO_RAW: {
-		int shift = chan->scan_type.shift;
-
+	case IIO_CHAN_INFO_RAW:
 		ret = iio_device_claim_direct_mode(indio_dev);
 		if (ret)
 			break;
@@ -487,7 +485,8 @@ static int ads1015_read_raw(struct iio_dev *indio_dev,
 			goto release_direct;
 		}
 
-		*val = sign_extend32(*val >> shift, 15 - shift);
+		*val = sign_extend32(*val >> chan->scan_type.shift,
+				     chan->scan_type.realbits - 1);
 
 		ret = ads1015_set_power_state(data, false);
 		if (ret < 0)
@@ -497,7 +496,6 @@ static int ads1015_read_raw(struct iio_dev *indio_dev,
 release_direct:
 		iio_device_release_direct_mode(indio_dev);
 		break;
-	}
 	case IIO_CHAN_INFO_SCALE:
 		idx = data->channel_data[chan->address].pga;
 		*val = ads1015_fullscale_range[idx];
-- 
2.33.1.1089.g2158813163f-goog


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v2 12/13] iio: xilinx-xadc-core: Use local variable in xadc_read_raw
  2021-11-04  8:24 [PATCH v2 00/13] iio: Use scan_type shift and realbits when processing raw data Gwendal Grignou
                   ` (10 preceding siblings ...)
  2021-11-04  8:24 ` [PATCH v2 11/13] iio: ti-ads1015: Remove shift variable ads1015_read_raw Gwendal Grignou
@ 2021-11-04  8:24 ` Gwendal Grignou
  2021-11-04  8:24 ` [PATCH v2 13/13] iio: mpl3115: Use scan_type.shift and realbit in mpl3115_read_raw Gwendal Grignou
  2021-11-13 16:43 ` [PATCH v2 00/13] iio: Use scan_type shift and realbits when processing raw data Jonathan Cameron
  13 siblings, 0 replies; 18+ messages in thread
From: Gwendal Grignou @ 2021-11-04  8:24 UTC (permalink / raw)
  To: jic23, lars; +Cc: andy.shevchenko, linux-iio, Gwendal Grignou

Minor cleanup: bit is already defined as chan->scan_type.realbits,
use bit when needed.

Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
---
 drivers/iio/adc/xilinx-xadc-core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/iio/adc/xilinx-xadc-core.c b/drivers/iio/adc/xilinx-xadc-core.c
index 2aa4278ecba78..823c8e5f9809b 100644
--- a/drivers/iio/adc/xilinx-xadc-core.c
+++ b/drivers/iio/adc/xilinx-xadc-core.c
@@ -944,7 +944,7 @@ static int xadc_read_raw(struct iio_dev *indio_dev,
 				*val = 1000;
 				break;
 			}
-			*val2 = chan->scan_type.realbits;
+			*val2 = bits;
 			return IIO_VAL_FRACTIONAL_LOG2;
 		case IIO_TEMP:
 			/* Temp in C = (val * 503.975) / 2**bits - 273.15 */
-- 
2.33.1.1089.g2158813163f-goog


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v2 13/13] iio: mpl3115: Use scan_type.shift and realbit in mpl3115_read_raw
  2021-11-04  8:24 [PATCH v2 00/13] iio: Use scan_type shift and realbits when processing raw data Gwendal Grignou
                   ` (11 preceding siblings ...)
  2021-11-04  8:24 ` [PATCH v2 12/13] iio: xilinx-xadc-core: Use local variable in xadc_read_raw Gwendal Grignou
@ 2021-11-04  8:24 ` Gwendal Grignou
  2021-11-13 16:43 ` [PATCH v2 00/13] iio: Use scan_type shift and realbits when processing raw data Jonathan Cameron
  13 siblings, 0 replies; 18+ messages in thread
From: Gwendal Grignou @ 2021-11-04  8:24 UTC (permalink / raw)
  To: jic23, lars; +Cc: andy.shevchenko, linux-iio, Gwendal Grignou

When processing raw data using channel scan_type.shift as source of
trust to shift data appropriately.
When processing the temperature channel, use a 16bit big endian variable
as buffer to increase conversion readability.

Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
---
 drivers/iio/pressure/mpl3115.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/drivers/iio/pressure/mpl3115.c b/drivers/iio/pressure/mpl3115.c
index 1eb9e7b29e050..e95b9a5475b4e 100644
--- a/drivers/iio/pressure/mpl3115.c
+++ b/drivers/iio/pressure/mpl3115.c
@@ -74,7 +74,6 @@ static int mpl3115_read_raw(struct iio_dev *indio_dev,
 			    int *val, int *val2, long mask)
 {
 	struct mpl3115_data *data = iio_priv(indio_dev);
-	__be32 tmp = 0;
 	int ret;
 
 	switch (mask) {
@@ -84,7 +83,9 @@ static int mpl3115_read_raw(struct iio_dev *indio_dev,
 			return ret;
 
 		switch (chan->type) {
-		case IIO_PRESSURE: /* in 0.25 pascal / LSB */
+		case IIO_PRESSURE: { /* in 0.25 pascal / LSB */
+			__be32 tmp = 0;
+
 			mutex_lock(&data->lock);
 			ret = mpl3115_request(data);
 			if (ret < 0) {
@@ -96,10 +97,13 @@ static int mpl3115_read_raw(struct iio_dev *indio_dev,
 			mutex_unlock(&data->lock);
 			if (ret < 0)
 				break;
-			*val = be32_to_cpu(tmp) >> 12;
+			*val = be32_to_cpu(tmp) >> chan->scan_type.shift;
 			ret = IIO_VAL_INT;
 			break;
-		case IIO_TEMP: /* in 0.0625 celsius / LSB */
+		}
+		case IIO_TEMP: { /* in 0.0625 celsius / LSB */
+			__be16 tmp;
+
 			mutex_lock(&data->lock);
 			ret = mpl3115_request(data);
 			if (ret < 0) {
@@ -111,9 +115,11 @@ static int mpl3115_read_raw(struct iio_dev *indio_dev,
 			mutex_unlock(&data->lock);
 			if (ret < 0)
 				break;
-			*val = sign_extend32(be32_to_cpu(tmp) >> 20, 11);
+			*val = sign_extend32(be16_to_cpu(tmp) >> chan->scan_type.shift,
+					     chan->scan_type.realbits - 1);
 			ret = IIO_VAL_INT;
 			break;
+		}
 		default:
 			ret = -EINVAL;
 			break;
-- 
2.33.1.1089.g2158813163f-goog


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 08/13] iio: at91-sama5d2: Use scan_type when processing raw data
  2021-11-04  8:24 ` [PATCH v2 08/13] iio: at91-sama5d2: " Gwendal Grignou
@ 2021-11-13 16:42   ` Jonathan Cameron
  2021-11-15  9:22     ` Eugen.Hristev
  0 siblings, 1 reply; 18+ messages in thread
From: Jonathan Cameron @ 2021-11-13 16:42 UTC (permalink / raw)
  To: Gwendal Grignou; +Cc: lars, andy.shevchenko, linux-iio, Eugen.Hristev

On Thu,  4 Nov 2021 01:24:08 -0700
Gwendal Grignou <gwendal@chromium.org> wrote:

> Use channel definition as root of trust and replace constant
> when reading elements directly using the raw sysfs attributes.
> 
> Signed-off-by: Gwendal Grignou <gwendal@chromium.org>

Hi Eugen,

Gwendal's v2 crossed with your comments on this fixing an issue in 
6794e23fa3fe ("iio: adc: at91-sama5d2_adc: add support for oversampling 
resolution")

You requested a separate fix to change the value to 13 then this on top
of that.  I don't see why we can't go directly to this with an appropriately
reworded message to say what is being fixed.  Am I missing something beyond
the fix being more obvious if we just change the value?

Whilst this is pending I've applied the rest of this series as it's only this
one with open questions.

Thanks,

Jonathan

> ---
>  drivers/iio/adc/at91-sama5d2_adc.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/iio/adc/at91-sama5d2_adc.c b/drivers/iio/adc/at91-sama5d2_adc.c
> index 4c922ef634f8e..92a57cf10fba4 100644
> --- a/drivers/iio/adc/at91-sama5d2_adc.c
> +++ b/drivers/iio/adc/at91-sama5d2_adc.c
> @@ -1586,7 +1586,8 @@ static int at91_adc_read_info_raw(struct iio_dev *indio_dev,
>  		*val = st->conversion_value;
>  		ret = at91_adc_adjust_val_osr(st, val);
>  		if (chan->scan_type.sign == 's')
> -			*val = sign_extend32(*val, 11);
> +			*val = sign_extend32(*val,
> +					     chan->scan_type.realbits - 1);
>  		st->conversion_done = false;
>  	}
>  


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 00/13] iio: Use scan_type shift and realbits when processing raw data
  2021-11-04  8:24 [PATCH v2 00/13] iio: Use scan_type shift and realbits when processing raw data Gwendal Grignou
                   ` (12 preceding siblings ...)
  2021-11-04  8:24 ` [PATCH v2 13/13] iio: mpl3115: Use scan_type.shift and realbit in mpl3115_read_raw Gwendal Grignou
@ 2021-11-13 16:43 ` Jonathan Cameron
  13 siblings, 0 replies; 18+ messages in thread
From: Jonathan Cameron @ 2021-11-13 16:43 UTC (permalink / raw)
  To: Gwendal Grignou; +Cc: lars, andy.shevchenko, linux-iio

On Thu,  4 Nov 2021 01:24:00 -0700
Gwendal Grignou <gwendal@chromium.org> wrote:

> Using scan_type has source of truth, use shift and realbits instead of
> constants when processing reading sensor registers to produce raw sysfs
> entries.
> The same shit and realbits are already used by the libiio user-space
> library to present channel information from device buffer.
> 
> Fix only a handful of drivers, where channel scan_type was accessible
> in the function handling the raw data request.
> 
> In mpl3115, use a 16 bit big endian buffer when reading temperature
> channel to improve readability.

All but patch 8 applied.  There are outstanding questions about how to
handle that one as it happens to also be fixing a bug.

Applied to the togreg branch of iio.git and pushed out as testing until
the merge window is over and I can rebase on rc1.

Thanks,

Jonathan

> 
> Changes in v2:
> - Split first CL, one CL for each driver
> - Add realbit for sca3000 temperature channel
> - Remove constant in bma220 driver.
> 
> Gwendal Grignou (13):
>   iio: bma220: Use scan_type when processing raw data
>   iio: kxcjk-1013: Use scan_type when processing raw data
>   iio: mma7455: Use scan_type when processing raw data
>   iio: sca3000: Use scan_type when processing raw data
>   iio: stk8312: Use scan_type when processing raw data
>   iio: stk8ba50: Use scan_type when processing raw data
>   iio: ad7266: Use scan_type when processing raw data
>   iio: at91-sama5d2: Use scan_type when processing raw data
>   iio: ti-adc12138: Use scan_type when processing raw data
>   iio: mag3110: Use scan_type when processing raw data
>   iio: ti-ads1015: Remove shift variable ads1015_read_raw
>   iio: xilinx-xadc-core: Use local variable in xadc_read_raw
>   iio: mpl3115: Use scan_type.shift and realbit in mpl3115_read_raw
> 
>  drivers/iio/accel/bma220_spi.c     |  6 +++---
>  drivers/iio/accel/kxcjk-1013.c     |  3 ++-
>  drivers/iio/accel/mma7455_core.c   |  3 ++-
>  drivers/iio/accel/sca3000.c        | 17 +++++++++++++----
>  drivers/iio/accel/stk8312.c        |  2 +-
>  drivers/iio/accel/stk8ba50.c       |  3 ++-
>  drivers/iio/adc/ad7266.c           |  3 ++-
>  drivers/iio/adc/at91-sama5d2_adc.c |  3 ++-
>  drivers/iio/adc/ti-adc12138.c      |  3 ++-
>  drivers/iio/adc/ti-ads1015.c       |  8 +++-----
>  drivers/iio/adc/xilinx-xadc-core.c |  2 +-
>  drivers/iio/magnetometer/mag3110.c |  6 ++++--
>  drivers/iio/pressure/mpl3115.c     | 16 +++++++++++-----
>  13 files changed, 48 insertions(+), 27 deletions(-)
> 


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 08/13] iio: at91-sama5d2: Use scan_type when processing raw data
  2021-11-13 16:42   ` Jonathan Cameron
@ 2021-11-15  9:22     ` Eugen.Hristev
  2021-11-21 13:45       ` Jonathan Cameron
  0 siblings, 1 reply; 18+ messages in thread
From: Eugen.Hristev @ 2021-11-15  9:22 UTC (permalink / raw)
  To: jic23, gwendal; +Cc: lars, andy.shevchenko, linux-iio

On 11/13/21 6:42 PM, Jonathan Cameron wrote:
> On Thu,  4 Nov 2021 01:24:08 -0700
> Gwendal Grignou <gwendal@chromium.org> wrote:
> 
>> Use channel definition as root of trust and replace constant
>> when reading elements directly using the raw sysfs attributes.
>>
>> Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
> 
> Hi Eugen,
> 
> Gwendal's v2 crossed with your comments on this fixing an issue in
> 6794e23fa3fe ("iio: adc: at91-sama5d2_adc: add support for oversampling
> resolution")
> 
> You requested a separate fix to change the value to 13 then this on top
> of that.  I don't see why we can't go directly to this with an appropriately
> reworded message to say what is being fixed.  Am I missing something beyond
> the fix being more obvious if we just change the value?
> 
> Whilst this is pending I've applied the rest of this series as it's only this
> one with open questions.

Hi Jonathan,

If you feel it's not worth fixing it in a separate commit , then feel 
free to apply this patch, I am happy with both ways.

you can add my

Reviewed-by: Eugen Hristev <eugen.hristev@microchip.com>

Thanks !
Eugen
> 
> Thanks,
> 
> Jonathan
> 
>> ---
>>   drivers/iio/adc/at91-sama5d2_adc.c | 3 ++-
>>   1 file changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/iio/adc/at91-sama5d2_adc.c b/drivers/iio/adc/at91-sama5d2_adc.c
>> index 4c922ef634f8e..92a57cf10fba4 100644
>> --- a/drivers/iio/adc/at91-sama5d2_adc.c
>> +++ b/drivers/iio/adc/at91-sama5d2_adc.c
>> @@ -1586,7 +1586,8 @@ static int at91_adc_read_info_raw(struct iio_dev *indio_dev,
>>                *val = st->conversion_value;
>>                ret = at91_adc_adjust_val_osr(st, val);
>>                if (chan->scan_type.sign == 's')
>> -                     *val = sign_extend32(*val, 11);
>> +                     *val = sign_extend32(*val,
>> +                                          chan->scan_type.realbits - 1);
>>                st->conversion_done = false;
>>        }
>>
> 


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 08/13] iio: at91-sama5d2: Use scan_type when processing raw data
  2021-11-15  9:22     ` Eugen.Hristev
@ 2021-11-21 13:45       ` Jonathan Cameron
  0 siblings, 0 replies; 18+ messages in thread
From: Jonathan Cameron @ 2021-11-21 13:45 UTC (permalink / raw)
  To: Eugen.Hristev; +Cc: gwendal, lars, andy.shevchenko, linux-iio

On Mon, 15 Nov 2021 09:22:58 +0000
<Eugen.Hristev@microchip.com> wrote:

> On 11/13/21 6:42 PM, Jonathan Cameron wrote:
> > On Thu,  4 Nov 2021 01:24:08 -0700
> > Gwendal Grignou <gwendal@chromium.org> wrote:
> >   
> >> Use channel definition as root of trust and replace constant
> >> when reading elements directly using the raw sysfs attributes.
> >>
> >> Signed-off-by: Gwendal Grignou <gwendal@chromium.org>  
> > 
> > Hi Eugen,
> > 
> > Gwendal's v2 crossed with your comments on this fixing an issue in
> > 6794e23fa3fe ("iio: adc: at91-sama5d2_adc: add support for oversampling
> > resolution")
> > 
> > You requested a separate fix to change the value to 13 then this on top
> > of that.  I don't see why we can't go directly to this with an appropriately
> > reworded message to say what is being fixed.  Am I missing something beyond
> > the fix being more obvious if we just change the value?
> > 
> > Whilst this is pending I've applied the rest of this series as it's only this
> > one with open questions.  
> 
> Hi Jonathan,
> 
> If you feel it's not worth fixing it in a separate commit , then feel 
> free to apply this patch, I am happy with both ways.
> 
> you can add my
> 
> Reviewed-by: Eugen Hristev <eugen.hristev@microchip.com>

Thanks,

Applied to the fixes-togreg branch of iio.git with the fixes tag and stable marking.

Jonathan

> 
> Thanks !
> Eugen
> > 
> > Thanks,
> > 
> > Jonathan
> >   
> >> ---
> >>   drivers/iio/adc/at91-sama5d2_adc.c | 3 ++-
> >>   1 file changed, 2 insertions(+), 1 deletion(-)
> >>
> >> diff --git a/drivers/iio/adc/at91-sama5d2_adc.c b/drivers/iio/adc/at91-sama5d2_adc.c
> >> index 4c922ef634f8e..92a57cf10fba4 100644
> >> --- a/drivers/iio/adc/at91-sama5d2_adc.c
> >> +++ b/drivers/iio/adc/at91-sama5d2_adc.c
> >> @@ -1586,7 +1586,8 @@ static int at91_adc_read_info_raw(struct iio_dev *indio_dev,
> >>                *val = st->conversion_value;
> >>                ret = at91_adc_adjust_val_osr(st, val);
> >>                if (chan->scan_type.sign == 's')
> >> -                     *val = sign_extend32(*val, 11);
> >> +                     *val = sign_extend32(*val,
> >> +                                          chan->scan_type.realbits - 1);
> >>                st->conversion_done = false;
> >>        }
> >>  
> >   
> 


^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2021-11-21 13:41 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-04  8:24 [PATCH v2 00/13] iio: Use scan_type shift and realbits when processing raw data Gwendal Grignou
2021-11-04  8:24 ` [PATCH v2 01/13] iio: bma220: Use scan_type " Gwendal Grignou
2021-11-04  8:24 ` [PATCH v2 02/13] iio: kxcjk-1013: " Gwendal Grignou
2021-11-04  8:24 ` [PATCH v2 03/13] iio: mma7455: " Gwendal Grignou
2021-11-04  8:24 ` [PATCH v2 04/13] iio: sca3000: " Gwendal Grignou
2021-11-04  8:24 ` [PATCH v2 05/13] iio: stk8312: " Gwendal Grignou
2021-11-04  8:24 ` [PATCH v2 06/13] iio: stk8ba50: " Gwendal Grignou
2021-11-04  8:24 ` [PATCH v2 07/13] iio: ad7266: " Gwendal Grignou
2021-11-04  8:24 ` [PATCH v2 08/13] iio: at91-sama5d2: " Gwendal Grignou
2021-11-13 16:42   ` Jonathan Cameron
2021-11-15  9:22     ` Eugen.Hristev
2021-11-21 13:45       ` Jonathan Cameron
2021-11-04  8:24 ` [PATCH v2 09/13] iio: ti-adc12138: " Gwendal Grignou
2021-11-04  8:24 ` [PATCH v2 10/13] iio: mag3110: " Gwendal Grignou
2021-11-04  8:24 ` [PATCH v2 11/13] iio: ti-ads1015: Remove shift variable ads1015_read_raw Gwendal Grignou
2021-11-04  8:24 ` [PATCH v2 12/13] iio: xilinx-xadc-core: Use local variable in xadc_read_raw Gwendal Grignou
2021-11-04  8:24 ` [PATCH v2 13/13] iio: mpl3115: Use scan_type.shift and realbit in mpl3115_read_raw Gwendal Grignou
2021-11-13 16:43 ` [PATCH v2 00/13] iio: Use scan_type shift and realbits when processing raw data Jonathan Cameron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).