Skip to contentSkip to conversation

iio:pressure:ms5637: add ms5803 support

PATCH In review Alexandre Belloni 3 participants 10 messages Jan 9, 2021 – Jan 16, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover iio:pressure:ms5637: add ms5803 support

Hello,

This series adds support for the Measurement Specialities ms5803. It is very similar to the ms5805 but has a different PROM layout (which I suspect predates the ms5805 PROM layout). Also it supports less frequency sampling options.

After a bit of preparatory work in the ms5637 driver and its common library, mainly to handle the PROM layout and sample frequencies, adding support is trivial.

Changes in v2:
- Dropped "iio:pressure:ms5637: switch to probe_new" to keep the i2c_device_id table.
- Reorder trivial-devices.yaml

Alexandre Belloni (6):
dt-bindings: trivial-devices: reorder memsic devices iio:pressure:ms5637: introduce hardware differentiation iio:pressure:ms5637: limit available sample frequencies iio:common:ms_sensors:ms_sensors_i2c: rework CRC calculation helper iio:common:ms_sensors:ms_sensors_i2c: add support for alternative PROM
layout
iio:pressure:ms5637: add ms5803 support

 .../devicetree/bindings/trivial-devices.yaml  | 10 ++-
 .../iio/common/ms_sensors/ms_sensors_i2c.c    | 76 ++++++++++++++----
 .../iio/common/ms_sensors/ms_sensors_i2c.h    | 15 +++-
 drivers/iio/pressure/ms5637.c                 | 77 +++++++++++++++----
 4 files changed, 143 insertions(+), 35 deletions(-)
Signature · 1 line
2.29.2
#1 lore raw

PATCH v2 1/6 dt-bindings: trivial-devices: reorder memsic devices

Reorder memsic compatible strings alphabetically

---
 Documentation/devicetree/bindings/trivial-devices.yaml | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
Documentation/devicetree/bindings/trivial-devices.yaml +4 -4
index bdc2dc318178..e9b64be4b91e 100644
@@ -148,10 +148,6 @@ properties:
           - maxim,max31730
             # mCube 3-axis 8-bit digital accelerometer
           - mcube,mc3230
-            # MEMSIC magnetometer
-          - memsic,mmc35240
-            # MEMSIC 2-axis 8-bit digital accelerometer
-          - memsic,mxc6225
             # Measurement Specialities I2C temperature and humidity sensor
           - meas,htu21
             # Measurement Specialities I2C pressure and temperature sensor
@@ -166,6 +162,10 @@ properties:
           - meas,ms8607-temppressure
             # Measurement Specialties temperature sensor
           - meas,tsys01
+            # MEMSIC magnetometer
+          - memsic,mmc35240
+            # MEMSIC 2-axis 8-bit digital accelerometer
+          - memsic,mxc6225
             # Microchip differential I2C ADC, 1 Channel, 18 bit
           - microchip,mcp3421
             # Microchip differential I2C ADC, 2 Channel, 18 bit
Signature · 1 line
2.29.2
#3 lore raw

PATCH v2 2/6 iio:pressure:ms5637: introduce hardware differentiation

In reply to Alexandre Belloni

Some sensors in the ms58xx family have a different PROM length and a different number of available resolution. introduce struct ms_tp_hw_data to handle those differences.

---
Changes in v2:
- handle the i2c_device_table

 .../iio/common/ms_sensors/ms_sensors_i2c.h    | 11 ++++
 drivers/iio/pressure/ms5637.c                 | 50 +++++++++++++++----
 2 files changed, 51 insertions(+), 10 deletions(-)
drivers/iio/common/ms_sensors/ms_sensors_i2c.h +11 -0
index bad09c80e47a..f4a88148c113 100644
@@ -25,6 +25,16 @@ struct ms_ht_dev {
 	u8 res_index;
 };
 
+/**
+ * struct ms_hw_data - Temperature/Pressure sensor hardware data
+ * @prom_len:		number of words in the PROM
+ * @max_res_index:	maximum sensor resolution index
+ */
+struct ms_tp_hw_data {
+	u8 prom_len;
+	u8 max_res_index;
+};
+
 /**
  * struct ms_tp_dev - Temperature/Pressure sensor device structure
  * @client:	i2c client
@@ -36,6 +46,7 @@ struct ms_ht_dev {
 struct ms_tp_dev {
 	struct i2c_client *client;
 	struct mutex lock;
+	const struct ms_tp_hw_data *hw;
 	u16 prom[MS_SENSORS_TP_PROM_WORDS_NB + 1];
 	u8 res_index;
 };
drivers/iio/pressure/ms5637.c +40 -10

89 lines not on this page · raw message

Signature · 1 line
2.29.2
#4 lore raw

PATCH v2 3/6 iio:pressure:ms5637: limit available sample frequencies

In reply to Alexandre Belloni

Avoid exposing all the sampling frequencies for chip that only support a subset.

---
Changes in v2:
-use sysfs_emit_at

 drivers/iio/pressure/ms5637.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
drivers/iio/pressure/ms5637.c +15 -4
index fdd557ac71a3..0a6034342714 100644
@@ -36,8 +36,19 @@ struct ms_tp_data {
 };
 
 static const int ms5637_samp_freq[6] = { 960, 480, 240, 120, 60, 30 };
-/* String copy of the above const for readability purpose */
-static const char ms5637_show_samp_freq[] = "960 480 240 120 60 30";
+
+static ssize_t ms5637_show_samp_freq(struct device *dev, struct device_attribute *attr, char *buf)
+{
+	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
+	struct ms_tp_dev *dev_data = iio_priv(indio_dev);
+	int i, len = 0;
+
+	for (i = 0; i <= dev_data->hw->max_res_index; i++)
+		len += sysfs_emit_at(buf, len, "%u ", ms5637_samp_freq[i]);
+	sysfs_emit_at(buf, len - 1, "\n");
+
+	return len;
+}
 
 static int ms5637_read_raw(struct iio_dev *indio_dev,
 			   struct iio_chan_spec const *channel, int *val,
@@ -114,10 +125,10 @@ static const struct iio_chan_spec ms5637_channels[] = {
 	}
 };
 
-static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(ms5637_show_samp_freq);
+static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(ms5637_show_samp_freq);
 
 static struct attribute *ms5637_attributes[] = {
-	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
+	&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
 	NULL,
 };
 
Signature · 1 line
2.29.2
#5 lore raw

PATCH v2 4/6 iio:common:ms_sensors:ms_sensors_i2c: rework CRC calculation helper

In reply to Alexandre Belloni

The CRC calculation always happens on 8 words which is why there is an extra element in the prom array of struct ms_tp_dev. However, on ms5637 and similar, only 7 words are readable.

Then, set MS_SENSORS_TP_PROM_WORDS_NB to 8 and stop passing a len parameter to ms_sensors_tp_crc_valid as this simply hide the fact that it is hardcoded.

Finally, use the newly introduced hw->prom_len to know how many words can be read.

---
 drivers/iio/common/ms_sensors/ms_sensors_i2c.c | 12 +++++-------
 drivers/iio/common/ms_sensors/ms_sensors_i2c.h |  4 ++--
 2 files changed, 7 insertions(+), 9 deletions(-)
drivers/iio/common/ms_sensors/ms_sensors_i2c.c +5 -7
index b9e2038d05ef..872f90459e2e 100644
@@ -493,19 +493,18 @@ EXPORT_SYMBOL(ms_sensors_ht_read_humidity);
  *     This function is only used when reading PROM coefficients
  *
  * @prom:	pointer to PROM coefficients array
- * @len:	length of PROM coefficients array
  *
  * Return: True if CRC is ok.
  */
-static bool ms_sensors_tp_crc_valid(u16 *prom, u8 len)
+static bool ms_sensors_tp_crc_valid(u16 *prom)
 {
 	unsigned int cnt, n_bit;
 	u16 n_rem = 0x0000, crc_read = prom[0], crc = (*prom & 0xF000) >> 12;
 
-	prom[len - 1] = 0;
+	prom[MS_SENSORS_TP_PROM_WORDS_NB - 1] = 0;
 	prom[0] &= 0x0FFF;      /* Clear the CRC computation part */
 
-	for (cnt = 0; cnt < len * 2; cnt++) {
+	for (cnt = 0; cnt < MS_SENSORS_TP_PROM_WORDS_NB * 2; cnt++) {
 		if (cnt % 2 == 1)
 			n_rem ^= prom[cnt >> 1] & 0x00FF;
 		else
@@ -537,7 +536,7 @@ int ms_sensors_tp_read_prom(struct ms_tp_dev *dev_data)
 {
 	int i, ret;
 
-	for (i = 0; i < MS_SENSORS_TP_PROM_WORDS_NB; i++) {
+	for (i = 0; i < dev_data->hw->prom_len; i++) {
 		ret = ms_sensors_read_prom_word(
 			dev_data->client,
 			MS_SENSORS_TP_PROM_READ + (i << 1),
@@ -547,8 +546,7 @@ int ms_sensors_tp_read_prom(struct ms_tp_dev *dev_data)
 			return ret;
 	}
 
-	if (!ms_sensors_tp_crc_valid(dev_data->prom,
-				     MS_SENSORS_TP_PROM_WORDS_NB + 1)) {
+	if (!ms_sensors_tp_crc_valid(dev_data->prom)) {
 		dev_err(&dev_data->client->dev,
 			"Calibration coefficients crc check error\n");
 		return -ENODEV;
drivers/iio/common/ms_sensors/ms_sensors_i2c.h +2 -2
index f4a88148c113..f15b973f27c6 100644
@@ -11,7 +11,7 @@
 #include <linux/i2c.h>
 #include <linux/mutex.h>
 
-#define MS_SENSORS_TP_PROM_WORDS_NB		7
+#define MS_SENSORS_TP_PROM_WORDS_NB		8
 
 /**
  * struct ms_ht_dev - Humidity/Temperature sensor device structure
@@ -47,7 +47,7 @@ struct ms_tp_dev {
 	struct i2c_client *client;
 	struct mutex lock;
 	const struct ms_tp_hw_data *hw;
-	u16 prom[MS_SENSORS_TP_PROM_WORDS_NB + 1];
+	u16 prom[MS_SENSORS_TP_PROM_WORDS_NB];
 	u8 res_index;
 };
 
Signature · 1 line
2.29.2
#6 lore raw

PATCH v2 5/6 iio:common:ms_sensors:ms_sensors_i2c: add support for alternative PROM layout

In reply to Alexandre Belloni

Currently, only the 112bit PROM with 7 words is supported. However the ms58xx family also have devices with a 128bit PROM on 8 words. See AN520:
C-CODE EXAMPLE FOR MS56XX, MS57XX (EXCEPT ANALOG SENSOR), AND MS58XX SERIES PRESSURE SENSORS and the various device datasheets.

The difference is that the CRC is the 4 LSBs of word7 instead of being the 4 MSBs of word0.

---
Changes in v2:
- Fix return value documentation for ms_sensors_tp_crc_valid_112 and ms_sensors_tp_crc_valid_128

 .../iio/common/ms_sensors/ms_sensors_i2c.c    | 70 ++++++++++++++++---
 1 file changed, 59 insertions(+), 11 deletions(-)
drivers/iio/common/ms_sensors/ms_sensors_i2c.c +59 -11
index 872f90459e2e..16ea697e945c 100644
@@ -488,21 +488,18 @@ int ms_sensors_ht_read_humidity(struct ms_ht_dev *dev_data,
 EXPORT_SYMBOL(ms_sensors_ht_read_humidity);
 
 /**
- * ms_sensors_tp_crc_valid() - CRC check function for
+ * ms_sensors_tp_crc4() - Calculate PROM CRC for
  *     Temperature and pressure devices.
  *     This function is only used when reading PROM coefficients
  *
  * @prom:	pointer to PROM coefficients array
  *
- * Return: True if CRC is ok.
+ * Return: CRC.
  */
-static bool ms_sensors_tp_crc_valid(u16 *prom)
+static u8 ms_sensors_tp_crc4(u16 *prom)
 {
 	unsigned int cnt, n_bit;
-	u16 n_rem = 0x0000, crc_read = prom[0], crc = (*prom & 0xF000) >> 12;
-
-	prom[MS_SENSORS_TP_PROM_WORDS_NB - 1] = 0;
-	prom[0] &= 0x0FFF;      /* Clear the CRC computation part */
+	u16 n_rem = 0x0000;
 
 	for (cnt = 0; cnt < MS_SENSORS_TP_PROM_WORDS_NB * 2; cnt++) {
 		if (cnt % 2 == 1)
@@ -517,10 +514,55 @@ static bool ms_sensors_tp_crc_valid(u16 *prom)
 				n_rem <<= 1;
 		}
 	}
-	n_rem >>= 12;
-	prom[0] = crc_read;
 
-	return n_rem == crc;
+	return n_rem >> 12;
+}
+
+/**
+ * ms_sensors_tp_crc_valid_112() - CRC check function for
+ *     Temperature and pressure devices for 112bit PROM.
+ *     This function is only used when reading PROM coefficients
+ *
+ * @prom:	pointer to PROM coefficients array
+ *
+ * Return: True if CRC is ok.
+ */
+static bool ms_sensors_tp_crc_valid_112(u16 *prom)
+{
+	u16 w0 = prom[0], crc_read = (w0 & 0xF000) >> 12;
+	u8 crc;
+
+	prom[0] &= 0x0FFF;      /* Clear the CRC computation part */
+	prom[MS_SENSORS_TP_PROM_WORDS_NB - 1] = 0;
+
+	crc = ms_sensors_tp_crc4(prom);
+
+	prom[0] = w0;
+
+	return crc == crc_read;
+}
+
+/**
+ * ms_sensors_tp_crc_valid_128() - CRC check function for
+ *     Temperature and pressure devices for 128bit PROM.
+ *     This function is only used when reading PROM coefficients
+ *
+ * @prom:	pointer to PROM coefficients array
+ *
+ * Return: True if CRC is ok.
+ */
+static bool ms_sensors_tp_crc_valid_128(u16 *prom)
+{
+	u16 w7 = prom[7], crc_read = w7 & 0x000F;
+	u8 crc;
+
+	prom[7] &= 0xFF00;      /* Clear the CRC and LSB part */
+
+	crc = ms_sensors_tp_crc4(prom);
+
+	prom[7] = w7;
+
+	return crc == crc_read;
 }
 
 /**
@@ -535,6 +577,7 @@ static bool ms_sensors_tp_crc_valid(u16 *prom)
 int ms_sensors_tp_read_prom(struct ms_tp_dev *dev_data)
 {
 	int i, ret;
+	bool valid;
 
 	for (i = 0; i < dev_data->hw->prom_len; i++) {
 		ret = ms_sensors_read_prom_word(
@@ -546,7 +589,12 @@ int ms_sensors_tp_read_prom(struct ms_tp_dev *dev_data)
 			return ret;
 	}
 
-	if (!ms_sensors_tp_crc_valid(dev_data->prom)) {
+	if (dev_data->hw->prom_len == 8)
+		valid = ms_sensors_tp_crc_valid_128(dev_data->prom);
+	else
+		valid = ms_sensors_tp_crc_valid_112(dev_data->prom);
+
+	if (!valid) {
 		dev_err(&dev_data->client->dev,
 			"Calibration coefficients crc check error\n");
 		return -ENODEV;
Signature · 1 line
2.29.2
#7 lore raw

PATCH v2 6/6 iio:pressure:ms5637: add ms5803 support

In reply to Alexandre Belloni

The ms5803 is very similar to the ms5805 but has less resolution options and has the 128bit PROM layout.

---
 Documentation/devicetree/bindings/trivial-devices.yaml | 2 ++
 drivers/iio/pressure/ms5637.c                          | 8 ++++++++
 2 files changed, 10 insertions(+)
Documentation/devicetree/bindings/trivial-devices.yaml +2 -0
index e9b64be4b91e..a327130d1faa 100644
@@ -153,6 +153,8 @@ properties:
             # Measurement Specialities I2C pressure and temperature sensor
           - meas,ms5637
             # Measurement Specialities I2C pressure and temperature sensor
+          - meas,ms5803
+            # Measurement Specialities I2C pressure and temperature sensor
           - meas,ms5805
             # Measurement Specialities I2C pressure and temperature sensor
           - meas,ms5837
drivers/iio/pressure/ms5637.c +8 -0
index 0a6034342714..81f683321b23 100644
@@ -200,8 +200,15 @@ static const struct ms_tp_hw_data ms5637_hw_data  = {
 	.max_res_index = 5
 };
 
+static const struct ms_tp_hw_data ms5803_hw_data  = {
+	.prom_len = 8,
+	.max_res_index = 4
+};
+
 static const struct ms_tp_data ms5637_data = { .name = "ms5637", .hw = &ms5637_hw_data };
 
+static const struct ms_tp_data ms5803_data = { .name = "ms5803", .hw = &ms5803_hw_data };
+
 static const struct ms_tp_data ms5805_data = { .name = "ms5805", .hw = &ms5637_hw_data };
 
 static const struct ms_tp_data ms5837_data = { .name = "ms5837", .hw = &ms5637_hw_data };
@@ -222,6 +229,7 @@ MODULE_DEVICE_TABLE(i2c, ms5637_id);
 
 static const struct of_device_id ms5637_of_match[] = {
 	{ .compatible = "meas,ms5637", .data = &ms5637_data },
+	{ .compatible = "meas,ms5803", .data = &ms5803_data },
 	{ .compatible = "meas,ms5805", .data = &ms5805_data },
 	{ .compatible = "meas,ms5837", .data = &ms5837_data },
 	{ .compatible = "meas,ms8607-temppressure", .data = &ms8607_data },
Signature · 1 line
2.29.2
maintainer Acked-by on v2 6/6
#8 lore raw
Quoted 9 lines · Alexandre Belloni

The ms5803 is very similar to the ms5805 but has less resolution options and has the 128bit PROM layout.

---
 Documentation/devicetree/bindings/trivial-devices.yaml | 2 ++
 drivers/iio/pressure/ms5637.c                          | 8 ++++++++
 2 files changed, 10 insertions(+)
maintainer
#9 lore raw
In reply to Alexandre Belloni
Quoted 7 lines · On Sun, 10 Jan 2021 00:11:42 +0100 Alexandre Belloni…

Hello,

This series adds support for the Measurement Specialities ms5803. It is very similar to the ms5805 but has a different PROM layout (which I suspect predates the ms5805 PROM layout). Also it supports less frequency sampling options.

Quoted 8 lines

After a bit of preparatory work in the ms5637 driver and its common library, mainly to handle the PROM layout and sample frequencies, adding support is trivial.

Changes in v2:
- Dropped "iio:pressure:ms5637: switch to probe_new" to keep the i2c_device_id table.
- Reorder trivial-devices.yaml

LGTM. Series applied to the togreg branch of iio.git and pushed out as testing to see if the autobuilders can find anything we missed.

thanks,

Jonathan

Quoted 16 lines

Alexandre Belloni (6):
dt-bindings: trivial-devices: reorder memsic devices iio:pressure:ms5637: introduce hardware differentiation iio:pressure:ms5637: limit available sample frequencies iio:common:ms_sensors:ms_sensors_i2c: rework CRC calculation helper iio:common:ms_sensors:ms_sensors_i2c: add support for alternative PROM
layout
iio:pressure:ms5637: add ms5803 support

 .../devicetree/bindings/trivial-devices.yaml  | 10 ++-
 .../iio/common/ms_sensors/ms_sensors_i2c.c    | 76 ++++++++++++++----
 .../iio/common/ms_sensors/ms_sensors_i2c.h    | 15 +++-
 drivers/iio/pressure/ms5637.c                 | 77 +++++++++++++++----
 4 files changed, 143 insertions(+), 35 deletions(-)