Skip to contentSkip to conversation

Add support for magnetometer Yamaha YAS537

PATCH Superseded Jakob Hauser 3 participants 23 messages Jun 8, 2022 – Jun 11, 2022 1 message not in the archive IIO

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

cover Add support for magnetometer Yamaha YAS537

This patchset adds YAS537 variant to the already existing driver for Yamaha YAS magnetometers.

Patches 1-4 applies corrections that popped up when working on this. Patches 5-6 are preparations for adding YAS537.
Patch 7 finally adds the YAS537 variant.

Jakob Hauser (7):
iio: magnetometer: yas530: Change range of data in volatile register
iio: magnetometer: yas530: Correct scaling of magnetic axes
iio: magnetometer: yas530: Correct temperature handling
iio: magnetometer: yas530: Change data type of hard_offsets to signed
iio: magnetometer: yas530: Change data type of calibration
coefficients
iio: magnetometer: yas530: Remove redundant defaults on switch devid
iio: magnetometer: yas530: Add YAS537 variant

 drivers/iio/magnetometer/Kconfig         |   4 +-
 drivers/iio/magnetometer/yamaha-yas530.c | 814 +++++++++++++++++++----
 2 files changed, 698 insertions(+), 120 deletions(-)
Signature · 1 line
2.35.1
#2 lore raw

PATCH 1/7 iio: magnetometer: yas530: Change range of data in volatile register

In function yas5xx_volatile_reg(), the range for measure data should end at "YAS5XX_MEASURE_DATA + 7" instead of "+ 8" as we count from 0 to 7 here.

This change is of low importance as the "+ 8" register isn't called.

---
 drivers/iio/magnetometer/yamaha-yas530.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/magnetometer/yamaha-yas530.c +1 -1
index 9ff7b0e56cf6..425281401324 100644
@@ -527,7 +527,7 @@ static bool yas5xx_volatile_reg(struct device *dev, unsigned int reg)
 {
 	return reg == YAS5XX_ACTUATE_INIT_COIL ||
 		reg == YAS5XX_MEASURE ||
-		(reg >= YAS5XX_MEASURE_DATA && reg <= YAS5XX_MEASURE_DATA + 8);
+		(reg >= YAS5XX_MEASURE_DATA && reg <= YAS5XX_MEASURE_DATA + 7);
 }
 
 /* TODO: enable regmap cache, using mark dirty and sync at runtime resume */
Signature · 1 line
2.35.1
Reviewed-by on v1 1/7
#3 lore raw

Re: [PATCH 1/7] iio: magnetometer: yas530: Change range of data in volatile register

Quoted 7 lines · Jakob Hauser

In function yas5xx_volatile_reg(), the range for measure data should end at "YAS5XX_MEASURE_DATA + 7" instead of "+ 8" as we count from 0 to 7 here.

This change is of low importance as the "+ 8" register isn't called.

Yours,
Linus Walleij

#4 lore raw

PATCH 2/7 iio: magnetometer: yas530: Correct scaling of magnetic axes

In reply to Jakob Hauser

Looks like YAS530 raw values return picotesla and YAS532 nanotesla. Adapt comments and scaling.

---
 drivers/iio/magnetometer/yamaha-yas530.c | 28 +++++++++++++++---------
 1 file changed, 18 insertions(+), 10 deletions(-)
drivers/iio/magnetometer/yamaha-yas530.c +18 -10
index 425281401324..5c7d0ac533ac 100644
@@ -310,8 +310,6 @@ static s32 yas5xx_linearize(struct yas5xx *yas5xx, u16 val, int axis)
  * @yo: Y axis out
  * @zo: Z axis out
  * @return: 0 on success or error code
- *
- * Returned values are in nanotesla according to some code.
  */
 static int yas5xx_get_measure(struct yas5xx *yas5xx, s32 *to, s32 *xo, s32 *yo, s32 *zo)
 {
@@ -417,14 +415,24 @@ static int yas5xx_read_raw(struct iio_dev *indio_dev,
 			*val = 1;
 			return IIO_VAL_INT;
 		}
-		/*
-		 * The axis values are in nanotesla according to the vendor
-		 * drivers, but is clearly in microtesla according to
-		 * experiments. Since 1 uT = 0.01 Gauss, we need to divide
-		 * by 100000000 (10^8) to get to Gauss from the raw value.
-		 */
-		*val = 1;
-		*val2 = 100000000;
+		switch (yas5xx->devid) {
+		case YAS530_DEVICE_ID:
+			/*
+			 * Raw values of YAS530 are in picotesla. Devide by
+			 * 100000000 (10^8) to get Gauss.
+			 */
+			*val = 1;
+			*val2 = 100000000;
+			break;
+		case YAS532_DEVICE_ID:
+			/*
+			 * Raw values of YAS532 are in nanotesla. Devide by
+			 * 100000 (10^5) to get Gauss.
+			 */
+			*val = 1;
+			*val2 = 100000;
+			break;
+		}
 		return IIO_VAL_FRACTIONAL;
 	default:
 		/* Unknown request */
Signature · 1 line
2.35.1
Reviewed-by on v1 2/7
#5 lore raw

Re: [PATCH 2/7] iio: magnetometer: yas530: Correct scaling of magnetic axes

Quoted 5 lines · Jakob Hauser

Looks like YAS530 raw values return picotesla and YAS532 nanotesla. Adapt comments and scaling.

Ooops. In-kernel device trees only use YAS530 so the fix does not seem urgent, but it's up to Jonathan to decide whether this should even go to stable.

Yours,
Linus Walleij

#6 lore raw

PATCH 3/7 iio: magnetometer: yas530: Correct temperature handling

In reply to Jakob Hauser

The raw temperature value is a number of counts from a certain starting point. The resolution of the temperature counts is different for the YAS variants.

Temperature compensation for YAS532 version AC seems to be handled differently. It uses the deviation from 20 degree Celsius [1] whereas YAS530 and older versions of YAS532 apply solely the t value as a multiplier [2][3].

In funtion yas5xx_read_raw(), add case IIO_CHAN_INFO_PROCESSED. Remove scale of temperature as this isn't applied.

Additionally correct sign of temperature channel in iio_chan_spec. It's already defined that way in yas5xx_get_measure() function.

[1] https://github.com/msm8916-mainline/android_kernel_qcom_msm8916/blob/GT-I9195I/drivers/iio/magnetometer/yas_mag_drv-yas532.c#L442
[2] https://github.com/NovaFusion/android_kernel_samsung_golden/blob/cm-12.1/drivers/sensor/compass/yas_mag_driver-yas530.c#L881-L883
[3] https://github.com/LineageOS/android_kernel_samsung_msm8930-common/blob/lineage-18.1/drivers/sensors/geomagnetic/yas_mag_driver-yas53x.c#L856-L858

---
 drivers/iio/magnetometer/yamaha-yas530.c | 93 ++++++++++++++++++------
 1 file changed, 70 insertions(+), 23 deletions(-)
drivers/iio/magnetometer/yamaha-yas530.c +70 -23
index 5c7d0ac533ac..2e8d20b05217 100644
@@ -77,6 +77,7 @@
 #define YAS530_DATA_BITS		12
 #define YAS530_DATA_CENTER		BIT(YAS530_DATA_BITS - 1)
 #define YAS530_DATA_OVERFLOW		(BIT(YAS530_DATA_BITS) - 1)
+#define YAS530_20DEGREES		182 /* Counts starting at -62 °C */
 
 #define YAS532_DEVICE_ID		0x02 /* YAS532/YAS533 (MS-3R/F) */
 #define YAS532_VERSION_AB		0 /* YAS532/533 AB (MS-3R/F AB) */
@@ -88,7 +89,7 @@
 #define YAS532_DATA_BITS		13
 #define YAS532_DATA_CENTER		BIT(YAS532_DATA_BITS - 1)
 #define YAS532_DATA_OVERFLOW		(BIT(YAS532_DATA_BITS) - 1)
-#define YAS532_20DEGREES		390 /* Looks like Kelvin */
+#define YAS532_20DEGREES		390 /* Counts starting at -50 °C */
 
 /* These variant IDs are known from code dumps */
 #define YAS537_DEVICE_ID		0x07 /* YAS537 (MS-3T) */
@@ -314,7 +315,7 @@ static s32 yas5xx_linearize(struct yas5xx *yas5xx, u16 val, int axis)
 static int yas5xx_get_measure(struct yas5xx *yas5xx, s32 *to, s32 *xo, s32 *yo, s32 *zo)
 {
 	struct yas5xx_calibration *c = &yas5xx->calibration;
-	u16 t, x, y1, y2;
+	u16 t_ref, t, x, y1, y2;
 	/* These are "signed x, signed y1 etc */
 	s32 sx, sy1, sy2, sy, sz;
 	int ret;
@@ -329,16 +330,43 @@ static int yas5xx_get_measure(struct yas5xx *yas5xx, s32 *to, s32 *xo, s32 *yo,
 	sy1 = yas5xx_linearize(yas5xx, y1, 1);
 	sy2 = yas5xx_linearize(yas5xx, y2, 2);
 
-	/*
-	 * Temperature compensation for x, y1, y2 respectively:
-	 *
-	 *          Cx * t
-	 * x' = x - ------
-	 *           100
-	 */
-	sx = sx - (c->Cx * t) / 100;
-	sy1 = sy1 - (c->Cy1 * t) / 100;
-	sy2 = sy2 - (c->Cy2 * t) / 100;
+	/* Set the temperature reference value (unit: counts) */
+	switch (yas5xx->devid) {
+	case YAS530_DEVICE_ID:
+		t_ref = YAS530_20DEGREES;
+		break;
+	case YAS532_DEVICE_ID:
+		t_ref = YAS532_20DEGREES;
+		break;
+	}
+
+	/* Temperature compensation for x, y1, y2 respectively */
+	if (yas5xx->devid == YAS532_DEVICE_ID &&
+	    yas5xx->version == YAS532_VERSION_AC) {
+		/*
+		 * YAS532 version AC uses the temperature deviation as a
+		 * multiplier.
+		 *
+		 *          Cx * (t - t_ref)
+		 * x' = x - ----------------
+		 *                100
+		 */
+		sx = sx - (c->Cx * (t - t_ref)) / 100;
+		sy1 = sy1 - (c->Cy1 * (t - t_ref)) / 100;
+		sy2 = sy2 - (c->Cy2 * (t - t_ref)) / 100;
+	} else {
+		/*
+		 * YAS530 and YAS532 version AB use solely the t value as a
+		 * multiplier.
+		 *
+		 *          Cx * t
+		 * x' = x - ------
+		 *           100
+		 */
+		sx = sx - (c->Cx * t) / 100;
+		sy1 = sy1 - (c->Cy1 * t) / 100;
+		sy2 = sy2 - (c->Cy2 * t) / 100;
+	}
 
 	/*
 	 * Break y1 and y2 into y and z, y1 and y2 are apparently encoding
@@ -347,11 +375,34 @@ static int yas5xx_get_measure(struct yas5xx *yas5xx, s32 *to, s32 *xo, s32 *yo,
 	sy = sy1 - sy2;
 	sz = -sy1 - sy2;
 
-	/*
-	 * FIXME: convert to Celsius? Just guessing this is given
-	 * as 1/10:s of degrees so multiply by 100 to get millicentigrades.
-	 */
-	*to = t * 100;
+	/* Process temperature readout */
+	switch (yas5xx->devid) {
+	case YAS530_DEVICE_ID:
+		/*
+		 * Raw temperature value t is number of counts starting at
+		 * -62 °C. Reference value t_ref is the number of counts
+		 * between -62 °C and 20 °C (82 °C range).
+		 *
+		 * Temperature in °C would be (82 / t_ref * t) - 62.
+		 *
+		 * Contrary to this, perform multiplication first and division
+		 * second due to calculating with integers.
+		 *
+		 * To get a nicer result, calculate with 1/10:s degrees Celsius
+		 * and finally multiply by 100 to return milli degrees Celsius.
+		 */
+		*to = ((820 * t / t_ref) - 620) * 100;
+		break;
+	case YAS532_DEVICE_ID:
+		/*
+		 * Actually same procedure for YAS532 but the starting point is
+		 * at -50 °C. Reference value t_ref is the number of counts
+		 * between -50 °C and 20 °C (70 °C range).
+		 */
+		*to = ((700 * t / t_ref) - 500) * 100;
+		break;
+	}
+
 	/*
 	 * Calibrate [x,y,z] with some formulas like this:
 	 *
@@ -384,6 +435,7 @@ static int yas5xx_read_raw(struct iio_dev *indio_dev,
 	int ret;
 
 	switch (mask) {
+	case IIO_CHAN_INFO_PROCESSED:
 	case IIO_CHAN_INFO_RAW:
 		pm_runtime_get_sync(yas5xx->dev);
 		ret = yas5xx_get_measure(yas5xx, &t, &x, &y, &z);
@@ -410,11 +462,6 @@ static int yas5xx_read_raw(struct iio_dev *indio_dev,
 		}
 		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_SCALE:
-		if (chan->address == 0) {
-			/* Temperature is unscaled */
-			*val = 1;
-			return IIO_VAL_INT;
-		}
 		switch (yas5xx->devid) {
 		case YAS530_DEVICE_ID:
 			/*
@@ -513,7 +560,7 @@ static const struct iio_chan_spec yas5xx_channels[] = {
 		.address = 0,
 		.scan_index = 0,
 		.scan_type = {
-			.sign = 'u',
+			.sign = 's',
 			.realbits = 32,
 			.storagebits = 32,
 			.endianness = IIO_CPU,
Signature · 1 line
2.35.1
Reviewed-by on v1 3/7
#7 lore raw

Re: [PATCH 3/7] iio: magnetometer: yas530: Correct temperature handling

Quoted 14 lines · Jakob Hauser

The raw temperature value is a number of counts from a certain starting point. The resolution of the temperature counts is different for the YAS variants.

Temperature compensation for YAS532 version AC seems to be handled differently. It uses the deviation from 20 degree Celsius [1] whereas YAS530 and older versions of YAS532 apply solely the t value as a multiplier [2][3].

In funtion yas5xx_read_raw(), add case IIO_CHAN_INFO_PROCESSED. Remove scale of temperature as this isn't applied.

Additionally correct sign of temperature channel in iio_chan_spec. It's already defined that way in yas5xx_get_measure() function.

Quoted 6 lines

[1] https://github.com/msm8916-mainline/android_kernel_qcom_msm8916/blob/GT-I9195I/drivers/iio/magnetometer/yas_mag_drv-yas532.c#L442
[2] https://github.com/NovaFusion/android_kernel_samsung_golden/blob/cm-12.1/drivers/sensor/compass/yas_mag_driver-yas530.c#L881-L883
[3] https://github.com/LineageOS/android_kernel_samsung_msm8930-common/blob/lineage-18.1/drivers/sensors/geomagnetic/yas_mag_driver-yas53x.c#L856-L858

Thanks, my temperatures seem more accurate after this on YAS530. Patch does not seem urgent as the temperature does not see much usage outside of the device itself.

Yours,
Linus Walleij

#8 lore raw

PATCH 4/7 iio: magnetometer: yas530: Change data type of hard_offsets to signed

In reply to Jakob Hauser

The "hard_offsets" are currently unsigned u8 but they should be signed as they can get negative. They are signed in function yas5xx_meaure_offsets() and in the Yamaha drivers [1][2].

[1] https://github.com/NovaFusion/android_kernel_samsung_golden/blob/cm-12.1/drivers/sensor/compass/yas.h#L156
[2] https://github.com/msm8916-mainline/android_kernel_qcom_msm8916/blob/GT-I9195I/drivers/iio/magnetometer/yas_mag_drv-yas532.c#L91

Fixes de8860b1ed47 ("iio: magnetometer: Add driver for Yamaha YAS530")
Cc Linus Walleij <[email protected]>
---
 drivers/iio/magnetometer/yamaha-yas530.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/magnetometer/yamaha-yas530.c +1 -1
index 2e8d20b05217..9bfb3b573907 100644
@@ -133,7 +133,7 @@ struct yas5xx {
 	unsigned int version;
 	char name[16];
 	struct yas5xx_calibration calibration;
-	u8 hard_offsets[3];
+	s8 hard_offsets[3];
 	struct iio_mount_matrix orientation;
 	struct regmap *map;
 	struct regulator_bulk_data regs[2];
Signature · 1 line
2.35.1
Reviewed-by on v1 4/7
#9 lore raw

Re: [PATCH 4/7] iio: magnetometer: yas530: Change data type of hard_offsets to signed

Quoted 10 lines · Jakob Hauser

The "hard_offsets" are currently unsigned u8 but they should be signed as they can get negative. They are signed in function yas5xx_meaure_offsets() and in the Yamaha drivers [1][2].

[1] https://github.com/NovaFusion/android_kernel_samsung_golden/blob/cm-12.1/drivers/sensor/compass/yas.h#L156
[2] https://github.com/msm8916-mainline/android_kernel_qcom_msm8916/blob/GT-I9195I/drivers/iio/magnetometer/yas_mag_drv-yas532.c#L91

Fixes de8860b1ed47 ("iio: magnetometer: Add driver for Yamaha YAS530")
Cc Linus Walleij <[email protected]>

Yours,
Linus Walleij

Reviewed-by on v1 4/7
#10 lore raw

Re: [PATCH 4/7] iio: magnetometer: yas530: Change data type of hard_offsets to signed

In reply to Jakob Hauser
Quoted 9 lines · Jakob Hauser

The "hard_offsets" are currently unsigned u8 but they should be signed as they can get negative. They are signed in function yas5xx_meaure_offsets() and in the Yamaha drivers [1][2].

[1] https://github.com/NovaFusion/android_kernel_samsung_golden/blob/cm-12.1/drivers/sensor/compass/yas.h#L156
[2] https://github.com/msm8916-mainline/android_kernel_qcom_msm8916/blob/GT-I9195I/drivers/iio/magnetometer/yas_mag_drv-yas532.c#L91

Fixes de8860b1ed47 ("iio: magnetometer: Add driver for Yamaha YAS530")

The series should be organized like this:
1) definite fixes;
2) cleanups / refactoring;
3) new features / documentation.

It will give a maintainer easier times to handle it.

Code looks good to me.

Quoted 22 lines
---
 drivers/iio/magnetometer/yamaha-yas530.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/magnetometer/yamaha-yas530.c +1 -2
index 2e8d20b05217..9bfb3b573907 100644
@@ -133,7 +133,7 @@ struct yas5xx {
        unsigned int version;
        char name[16];
        struct yas5xx_calibration calibration;
-       u8 hard_offsets[3];
+       s8 hard_offsets[3];
        struct iio_mount_matrix orientation;
        struct regmap *map;
        struct regulator_bulk_data regs[2];
--

2.35.1

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#11 lore raw

PATCH 5/7 iio: magnetometer: yas530: Change data type of calibration coefficients

In reply to Jakob Hauser

This is a preparation for adding YAS537 variant.

YAS537 uses other data types on the calibration coefficients [1] than YAS530 [2] and YAS532 [3].

On YAS537, at least for a4 and a7 this could matter because 8-bit unsigned data from the register gets stored into a signed data type, therefore this should be 8-bit as well.

For YAS530/532, on the other hand, it doesn't seem to matter. The size of a2-a9 and k is smaller than 8-bit at extraction, also the applied math is low. And Cx/Cy1/Cy2, now being defined as signed 16-bit, are extracted as unsigned 8-bit and undergo only minor math.

[1] https://github.com/msm8916-mainline/android_kernel_qcom_msm8916/blob/GT-I9195I/drivers/iio/magnetometer/yas_mag_drv-yas537.c#L76-L78
[2] https://github.com/NovaFusion/android_kernel_samsung_golden/blob/cm-12.1/drivers/sensor/compass/yas_mag_driver-yas530.c#L526-L527
[3] https://github.com/msm8916-mainline/android_kernel_qcom_msm8916/blob/GT-I9195I/drivers/iio/magnetometer/yas_mag_drv-yas532.c#L76-L77

---
 drivers/iio/magnetometer/yamaha-yas530.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/iio/magnetometer/yamaha-yas530.c +4 -2
index 9bfb3b573907..31637a890b7f 100644
@@ -103,9 +103,11 @@ struct yas5xx_calibration {
 	s32 r[3];
 	u32 f[3];
 	/* Temperature compensation calibration */
-	s32 Cx, Cy1, Cy2;
+	s16 Cx, Cy1, Cy2;
 	/* Misc calibration coefficients */
-	s32 a2, a3, a4, a5, a6, a7, a8, a9, k;
+	s8  a2, a3, a4, a6, a7, a8;
+	s16 a5, a9;
+	u8  k;
 	/* clock divider */
 	u8 dck;
 };
Signature · 1 line
2.35.1
Reviewed-by on v1 5/7
#12 lore raw

Re: [PATCH 5/7] iio: magnetometer: yas530: Change data type of calibration coefficients

Quoted 14 lines · Jakob Hauser

This is a preparation for adding YAS537 variant.

YAS537 uses other data types on the calibration coefficients [1] than YAS530 [2] and YAS532 [3].

On YAS537, at least for a4 and a7 this could matter because 8-bit unsigned data from the register gets stored into a signed data type, therefore this should be 8-bit as well.

For YAS530/532, on the other hand, it doesn't seem to matter. The size of a2-a9 and k is smaller than 8-bit at extraction, also the applied math is low. And Cx/Cy1/Cy2, now being defined as signed 16-bit, are extracted as unsigned 8-bit and undergo only minor math.

Quoted 6 lines

[1] https://github.com/msm8916-mainline/android_kernel_qcom_msm8916/blob/GT-I9195I/drivers/iio/magnetometer/yas_mag_drv-yas537.c#L76-L78
[2] https://github.com/NovaFusion/android_kernel_samsung_golden/blob/cm-12.1/drivers/sensor/compass/yas_mag_driver-yas530.c#L526-L527
[3] https://github.com/msm8916-mainline/android_kernel_qcom_msm8916/blob/GT-I9195I/drivers/iio/magnetometer/yas_mag_drv-yas532.c#L76-L77

Yours,
Linus Walleij

#13 lore raw

PATCH 6/7 iio: magnetometer: yas530: Remove redundant defaults on switch devid

In reply to Jakob Hauser

This is a preparation for adding YAS537 variant.

In function yas5xx_probe(), there is a switch statement checking for device IDs. If the ID is unknown, it exits with a device error. In later functions, it's not neccessary to check the validity of the device IDs again.

When adding YAS537 in a later patch, several of such switch statements will be added. To make it more uniform, the redundant ones in YAS530/532 get herby removed. This is done in a separate patch for better history control.

---
 drivers/iio/magnetometer/yamaha-yas530.c | 12 +-----------
 1 file changed, 1 insertion(+), 11 deletions(-)
drivers/iio/magnetometer/yamaha-yas530.c +1 -11
index 31637a890b7f..59844e1b794c 100644
@@ -251,13 +251,9 @@ static int yas5xx_measure(struct yas5xx *yas5xx, u16 *t, u16 *x, u16 *y1, u16 *y
 		*y1 = yas532_extract_axis(&data[4]);
 		*y2 = yas532_extract_axis(&data[6]);
 		break;
-	default:
-		dev_err(yas5xx->dev, "unknown data format\n");
-		ret = -EINVAL;
-		break;
 	}
 
-	return ret;
+	return 0;
 
 out_unlock:
 	mutex_unlock(&yas5xx->lock);
@@ -289,9 +285,6 @@ static s32 yas5xx_linearize(struct yas5xx *yas5xx, u16 val, int axis)
 			/* Elaborate coefficients */
 			coef = yas532ac_coef[axis];
 		break;
-	default:
-		dev_err(yas5xx->dev, "unknown device type\n");
-		return val;
 	}
 	/*
 	 * Linearization formula:
@@ -798,9 +791,6 @@ static int yas5xx_meaure_offsets(struct yas5xx *yas5xx)
 	case YAS532_DEVICE_ID:
 		center = YAS532_DATA_CENTER;
 		break;
-	default:
-		dev_err(yas5xx->dev, "unknown device type\n");
-		return -EINVAL;
 	}
 
 	/*
Signature · 1 line
2.35.1
Reviewed-by on v1 6/7
#14 lore raw

Re: [PATCH 6/7] iio: magnetometer: yas530: Remove redundant defaults on switch devid

Quoted 6 lines · Jakob Hauser

This is a preparation for adding YAS537 variant.

In function yas5xx_probe(), there is a switch statement checking for device IDs. If the ID is unknown, it exits with a device error. In later functions, it's not neccessary to check the validity of the device IDs again.

Quoted 6 lines

When adding YAS537 in a later patch, several of such switch statements will be added. To make it more uniform, the redundant ones in YAS530/532 get herby removed. This is done in a separate patch for better history control.

No big deal for me, this works fine.

Yours,
Linus Walleij

#15 lore raw

Re: [PATCH 6/7] iio: magnetometer: yas530: Remove redundant defaults on switch devid

In reply to Jakob Hauser
Quoted 10 lines · Jakob Hauser

This is a preparation for adding YAS537 variant.

In function yas5xx_probe(), there is a switch statement checking for device IDs. If the ID is unknown, it exits with a device error. In later functions, it's not neccessary to check the validity of the device IDs again.

When adding YAS537 in a later patch, several of such switch statements will be added. To make it more uniform, the redundant ones in YAS530/532 get herby removed. This is done in a separate patch for better history control.

...

Quoted 1 line

- default:

Please, leave it. Some static analyzers or weird compiler versions may complain on this.

drivers/iio/magnetometer/yamaha-yas530.c static int yas5xx_measure(struct yas5xx *yas5xx, u16 *t, u16 *x, u16 *y1, u16 *y

Quoted 4 lines
drivers/iio/magnetometer/yamaha-yas530.c +0 -3
-               dev_err(yas5xx->dev, "unknown data format\n");
-               ret = -EINVAL;
 
-               break;

So leave this.

Quoted 1 line
        }

Ditto for the rest.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#16 lore raw

PATCH 7/7 iio: magnetometer: yas530: Add YAS537 variant

In reply to Jakob Hauser

This adds support for the magnetometer Yamaha YAS537. The additions are based on comparison of Yamaha Android kernel drivers for YAS532 [1] and YAS537 [2].

Functions used by YAS530 & YAS532 only were renamed from yas5xx to yas530_532. Registers were renamed accordingly.

In the Yamaha YAS537 Android driver, there is an overflow/underflow control implemented. For regular usage, this seems not necessary. A similar overflow/ underflow control of Yamaha YAS530/532 Android driver isn't integrated in the mainline driver. It is therefore skipped for YAS537 in mainline too.

Also in the Yamaha YAS537 Android driver, at the end of the reset_yas537() function, a measurement is saved in "last_after_rcoil". Later on, this is compared to current measurements. If the difference gets too big, a new reset is intialized. The difference in measurements needs to be quite big, it's hard to say if this is necessary for regular operation. Therefore this isn't integrated in the mainline driver either.

[1] https://github.com/msm8916-mainline/android_kernel_qcom_msm8916/blob/GT-I9195I/drivers/iio/magnetometer/yas_mag_drv-yas532.c
[2] https://github.com/msm8916-mainline/android_kernel_qcom_msm8916/blob/GT-I9195I/drivers/iio/magnetometer/yas_mag_drv-yas537.c

---
 drivers/iio/magnetometer/Kconfig         |   4 +-
 drivers/iio/magnetometer/yamaha-yas530.c | 683 ++++++++++++++++++++---
 2 files changed, 609 insertions(+), 78 deletions(-)
drivers/iio/magnetometer/Kconfig +2 -2
index 07eb619bcfe8..868128cee835 100644
@@ -216,8 +216,8 @@ config YAMAHA_YAS530
 	select IIO_TRIGGERED_BUFFER
 	help
 	  Say Y here to add support for the Yamaha YAS530 series of
-	  3-Axis Magnetometers. Right now YAS530, YAS532 and YAS533 are
-	  fully supported.
+	  3-Axis Magnetometers. Right now YAS530, YAS532, YAS533 and
+	  YAS537 are fully supported.
 
 	  This driver can also be compiled as a module.
 	  To compile this driver as a module, choose M here: the module
drivers/iio/magnetometer/yamaha-yas530.c +607 -76

964 lines not on this page · raw message

Signature · 1 line
2.35.1
Reviewed-by on v1 7/7
#17 lore raw

Re: [PATCH 7/7] iio: magnetometer: yas530: Add YAS537 variant

Quoted 18 lines · Jakob Hauser

This adds support for the magnetometer Yamaha YAS537. The additions are based on comparison of Yamaha Android kernel drivers for YAS532 [1] and YAS537 [2].

Functions used by YAS530 & YAS532 only were renamed from yas5xx to yas530_532. Registers were renamed accordingly.

In the Yamaha YAS537 Android driver, there is an overflow/underflow control implemented. For regular usage, this seems not necessary. A similar overflow/ underflow control of Yamaha YAS530/532 Android driver isn't integrated in the mainline driver. It is therefore skipped for YAS537 in mainline too.

Also in the Yamaha YAS537 Android driver, at the end of the reset_yas537() function, a measurement is saved in "last_after_rcoil". Later on, this is compared to current measurements. If the difference gets too big, a new reset is intialized. The difference in measurements needs to be quite big, it's hard to say if this is necessary for regular operation. Therefore this isn't integrated in the mainline driver either.

Quoted 5 lines

[1] https://github.com/msm8916-mainline/android_kernel_qcom_msm8916/blob/GT-I9195I/drivers/iio/magnetometer/yas_mag_drv-yas532.c
[2] https://github.com/msm8916-mainline/android_kernel_qcom_msm8916/blob/GT-I9195I/drivers/iio/magnetometer/yas_mag_drv-yas537.c

Nice work! I have reviewed the drafts before in private and can't see any remaining issues with the final version so:

Yours,
Linus Walleij

#18 lore raw

Re: [PATCH 7/7] iio: magnetometer: yas530: Add YAS537 variant

In reply to Jakob Hauser
Quoted 12 lines · Jakob Hauser

This adds support for the magnetometer Yamaha YAS537. The additions are based on comparison of Yamaha Android kernel drivers for YAS532 [1] and YAS537 [2].

Functions used by YAS530 & YAS532 only were renamed from yas5xx to yas530_532. Registers were renamed accordingly.

In the Yamaha YAS537 Android driver, there is an overflow/underflow control implemented. For regular usage, this seems not necessary. A similar overflow/ underflow control of Yamaha YAS530/532 Android driver isn't integrated in the mainline driver. It is therefore skipped for YAS537 in mainline too.

Quoted 4 lines

Also in the Yamaha YAS537 Android driver, at the end of the reset_yas537() function, a measurement is saved in "last_after_rcoil". Later on, this is compared to current measurements. If the difference gets too big, a new reset is intialized. The difference in measurements needs to be quite big,

initialized

Quoted 2 lines

it's hard to say if this is necessary for regular operation. Therefore this isn't integrated in the mainline driver either.

I understand that Linus knows well this code and may review this, but can you please split register renaming (at least, maybe something else can be split as well as preparatory change) to the separate patch?

...

drivers/iio/magnetometer/yamaha-yas530.c static void yas5xx_dump_calibration(struct yas5xx *yas5xx) · line 1180

Quoted 3 lines
drivers/iio/magnetometer/yamaha-yas530.c +3 -0
+                               regmap_read(yas5xx->map, i, &val);
+                               dev_dbg(yas5xx->dev, "register 0x%02x: %u\n",
+                                       i, val);

Please, drop all these value reads/writes debug messages, they are quite expensive (by resource consuming), noisy (may spam logs), and most important duplicative. regmap API has tracepoints, use them!

Perhaps it would require an additional patch to clean this up, if anything like this is present in the current code base..

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#19 lore raw

Re: [PATCH 7/7] iio: magnetometer: yas530: Add YAS537 variant

Hi Andy,

Quoted 3 lines · Andy Shevchenko

I understand that Linus knows well this code and may review this, but can you please split register renaming (at least, maybe something else can be split as well as preparatory change) to the separate patch?

That makes sense, I'll move the renaming into a separate patch.

Quoted 10 lines

drivers/iio/magnetometer/yamaha-yas530.c static void yas5xx_dump_calibration(struct yas5xx *yas5xx) · line 1180

Quoted 3 lines
drivers/iio/magnetometer/yamaha-yas530.c +3 -0
+                               regmap_read(yas5xx->map, i, &val);
+                               dev_dbg(yas5xx->dev, "register 0x%02x: %u\n",
+                                       i, val);

Please, drop all these value reads/writes debug messages, they are quite expensive (by resource consuming), noisy (may spam logs), and most important duplicative. regmap API has tracepoints, use them!

Perhaps it would require an additional patch to clean this up, if anything like this is present in the current code base..

Ok, I'll remove those direct regmap reads in yas537_dump_calibration().

However, I'd like to keep the others. The calibration data is dumped before [1] and after [2] being processed by the driver. This is helpful to check if it was processed correctly. Dumping the data is done only once at probing.

In yas537_dump_calibration(), I'd also like to keep dumping the "hard_offsets". Currently there is no linearization formula known for YAS537. Providing the "hard_offsets" may help to find a way.

[1]
https://github.com/torvalds/linux/blob/v5.18/drivers/iio/magnetometer/yamaha-yas530.c#L592
[2]
https://github.com/torvalds/linux/blob/v5.18/drivers/iio/magnetometer/yamaha-yas530.c#L678-L699

Kind regards,
Jakob

#20 lore raw

Re: [PATCH 7/7] iio: magnetometer: yas530: Add YAS537 variant

Quoted 1 line · Jakob Hauser

On 10.06.22 16:31, Andy Shevchenko wrote:

...

Quoted 11 lines
Quoted 9 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+                               dev_dbg(yas5xx->dev, "register 0x%02x: %u\n",
+                                       i, val);

Please, drop all these value reads/writes debug messages, they are quite expensive (by resource consuming), noisy (may spam logs), and most important duplicative. regmap API has tracepoints, use them!

Perhaps it would require an additional patch to clean this up, if anything like this is present in the current code base..

Ok, I'll remove those direct regmap reads in yas537_dump_calibration().

I'm not sure I understand what you are going to drop. I was talking about debug messages, the regmap reads are fine. Or you are talking about them as they are tightened together and one makes no sense without the other?

Quoted 4 lines

However, I'd like to keep the others. The calibration data is dumped before [1] and after [2] being processed by the driver. This is helpful to check if it was processed correctly. Dumping the data is done only once at probing.

Then it should be probably dev_info() in such cases.

Quoted 3 lines

In yas537_dump_calibration(), I'd also like to keep dumping the "hard_offsets". Currently there is no linearization formula known for YAS537. Providing the "hard_offsets" may help to find a way.

I understand that, but per se this is not for production esp. taking into account that regmap has a tracepoint mechanism.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#21 lore raw

Re: [PATCH 7/7] iio: magnetometer: yas530: Add YAS537 variant

Hi Andy,

(Side note: There is something wrong with the Cc list in your e-mail, the list of addresses isn't handled correctly.)

Quoted 7 lines · Andy Shevchenko
Quoted 1 line · Jakob Hauser

Ok, I'll remove those direct regmap reads in yas537_dump_calibration().

I'm not sure I understand what you are going to drop. I was talking about debug messages, the regmap reads are fine. Or you are talking about them as they are tightened together and one makes no sense without the other?

Yes on your question. In the new function yas537_dump_calibration() I added some regmap reads that are performed only because of debug messages. I'll remove those regmap reads incl. the corresponding debug messages. However, I'd like to keep the "other" debug messages.

I'll explain more detailed.

Generally speaking on YAS530/532:
At driver probe, calibration "raw" data is read from the calibration register (currently YAS5XX_CAL). Within the driver, this data gets processed into several calibration coefficients like e.g. c->a2. These are later on applied on the measure data.

Calibration debug on YAS530/532:
After reading the calibration "raw" data from the register (for further processing), we dump that "raw" data to debug. After having "calculated" the calibration coefficients like c->a2 within the driver, we dump them to debug too.

Generally speaking on YAS537 version 0:
There are two versions of YAS537. Version 0 reads calibration data from the register (YAS537_CAL) and writes it directly back into some other registers. The driver doesn't touch anything. This is done in new function yas537_get_calibration_data() after "case YAS537_VERSION_0:".

Generally speaking on YAS537 version 1:
Version 1 of YAS537 is a mixture. Some of the data being read from the calibration register (YAS537_CAL) is directly written back to some other registers. But additionally, some calibration coefficients like c->a2 need to be "calculated". This happens in new function yas537_get_calibration_data() after "case YAS537_VERSION_1:". The calibration coefficients will be applied on the measure data later on.

Calibration debug on YAS537:
In the new function yas537_dump_calibration(), I implemented that mixture also. Firstly, I added some regmap reads and debug dumps of the registers where some of the calibration data was written into. Secondly, for YAS537 version 1, I dumped the "calculated" calibration coefficients like c->a2.

What I intend to change for patchset v2:
In the new function yas537_dump_calibration(), I'll remove the two "for loops" (they contain the "unnecessary" regmap reads and debug dumps). But I'd like to keep the debug dump of the "calculated" calibration coefficients after the "for loops".

Quoted 6 lines
Quoted 4 lines

However, I'd like to keep the others. The calibration data is dumped before [1] and after [2] being processed by the driver. This is helpful to check if it was processed correctly. Dumping the data is done only once at probing.

Then it should be probably dev_info() in such cases.

To my understanding, in this case it's rather debug. If at some point it turns out that the driver doesn't work correctly, it allows a deeper insight on the intermediate steps the driver is doing internally.

Quoted 6 lines
Quoted 3 lines

In yas537_dump_calibration(), I'd also like to keep dumping the "hard_offsets". Currently there is no linearization formula known for YAS537. Providing the "hard_offsets" may help to find a way.

I understand that, but per se this is not for production esp. taking into account that regmap has a tracepoint mechanism.

Well, ok, I'll drop the "hard_offsets" debug dump.

Kind regards,
Jakob

#22 lore raw

Re: [PATCH 7/7] iio: magnetometer: yas530: Add YAS537 variant

Quoted 5 lines · Jakob Hauser

Hi Andy,

(Side note: There is something wrong with the Cc list in your e-mail, the list of addresses isn't handled correctly.)

The list is the same as in your mail. Dunno what exactly is the problem you are referring to?

Quoted 49 lines · Andy Shevchenko
Quoted 7 lines · Jakob Hauser
Quoted 1 line

Ok, I'll remove those direct regmap reads in yas537_dump_calibration().

I'm not sure I understand what you are going to drop. I was talking about debug messages, the regmap reads are fine. Or you are talking about them as they are tightened together and one makes no sense without the other?

Yes on your question. In the new function yas537_dump_calibration() I added some regmap reads that are performed only because of debug messages. I'll remove those regmap reads incl. the corresponding debug messages. However, I'd like to keep the "other" debug messages.

I'll explain more detailed.

Generally speaking on YAS530/532:
At driver probe, calibration "raw" data is read from the calibration register (currently YAS5XX_CAL). Within the driver, this data gets processed into several calibration coefficients like e.g. c->a2. These are later on applied on the measure data.

Calibration debug on YAS530/532:
After reading the calibration "raw" data from the register (for further processing), we dump that "raw" data to debug. After having "calculated" the calibration coefficients like c->a2 within the driver, we dump them to debug too.

Generally speaking on YAS537 version 0:
There are two versions of YAS537. Version 0 reads calibration data from the register (YAS537_CAL) and writes it directly back into some other registers. The driver doesn't touch anything. This is done in new function yas537_get_calibration_data() after "case YAS537_VERSION_0:".

Generally speaking on YAS537 version 1:
Version 1 of YAS537 is a mixture. Some of the data being read from the calibration register (YAS537_CAL) is directly written back to some other registers. But additionally, some calibration coefficients like c->a2 need to be "calculated". This happens in new function yas537_get_calibration_data() after "case YAS537_VERSION_1:". The calibration coefficients will be applied on the measure data later on.

Calibration debug on YAS537:
In the new function yas537_dump_calibration(), I implemented that mixture also. Firstly, I added some regmap reads and debug dumps of the registers where some of the calibration data was written into. Secondly, for YAS537 version 1, I dumped the "calculated" calibration coefficients like c->a2.

Quoted 5 lines

What I intend to change for patchset v2:
In the new function yas537_dump_calibration(), I'll remove the two "for loops" (they contain the "unnecessary" regmap reads and debug dumps). But I'd like to keep the debug dump of the "calculated" calibration coefficients after the "for loops".

Sounds reasonable to me.

Quoted 10 lines
Quoted 6 lines
Quoted 4 lines

However, I'd like to keep the others. The calibration data is dumped before [1] and after [2] being processed by the driver. This is helpful to check if it was processed correctly. Dumping the data is done only once at probing.

Then it should be probably dev_info() in such cases.

To my understanding, in this case it's rather debug. If at some point it turns out that the driver doesn't work correctly, it allows a deeper insight on the intermediate steps the driver is doing internally.

OK!

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

In yas537_dump_calibration(), I'd also like to keep dumping the "hard_offsets". Currently there is no linearization formula known for YAS537. Providing the "hard_offsets" may help to find a way.

I understand that, but per se this is not for production esp. taking into account that regmap has a tracepoint mechanism.

Well, ok, I'll drop the "hard_offsets" debug dump.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#23 lore raw

Re: [PATCH 7/7] iio: magnetometer: yas530: Add YAS537 variant

Hi Andy,

Quoted 8 lines · Andy Shevchenko
Quoted 3 lines · Jakob Hauser

(Side note: There is something wrong with the Cc list in your e-mail, the list of addresses isn't handled correctly.)

The list is the same as in your mail. Dunno what exactly is the problem you are referring to?

It somehow got confused by the colon after "open list" and the tilde before "postmarketos". Everything in between got interpreted as the name/description of the postmarketos mailing list. Your first reply at 2022-06-10 14:31 didn't make it to the devicetree list and likely not to Hans. Since then, these addresses show up twice in the thread, once for real and once still interpreted as name/description of the postmarketos mailing list.

The thread in the devicetree list:
https://lore.kernel.org/linux-devicetree/CAHp75VfV1E1WaLVDG3V9460AsVxahb9Epod-63T35eDV+h8Ubg@mail.gmail.com/T/#t

On the patchset, I'll send v2 probably at Sunday or Monday.

Kind regards,
Jakob