Skip to contentSkip to conversation

st_lsm6dsx: do not access active-low/open-drain if not supported

PATCH In review Lorenzo Bianconi 3 participants 8 messages Sep 22, 2019 – Sep 24, 2019

Subsystems (2)
lore
Share & tools
lore

Lorenzo Bianconi

author maintainer
#0 lore raw

cover st_lsm6dsx: do not access active-low/open-drain if not supported

Move active low and open drain register definitions in hw_settings register map in order to avoid to access them if the sensor does not support them.
Group irq related definition in irq_config structure in st_lsm6dsx_settings.

@Jonathan: I post this series now since in this way Bobby can test it and maybe you want to apply it on top of togreg branch. If you prefer to add it on testing branch I can rebase it ontop of Sean's series.

Lorenzo Bianconi (3):
iio: imu: st_lsm6dsx: move irq related definitions in irq_config
iio: imu: st_lsm6dsx: do not access active-low/open-drain regs if not
supported
iio: imu: st_lsm6dsx: add sanity check for read_fifo pointer

 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h       |  17 +-
 .../iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c    |  35 ++-
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c  | 243 ++++++++++++------
 3 files changed, 203 insertions(+), 92 deletions(-)
Signature · 1 line
2.21.0
author maintainer
#1 lore raw

PATCH 1/3 iio: imu: st_lsm6dsx: move irq related definitions in irq_config

Group irq related definition in irq_config structure in st_lsm6dsx_settings. This is a preliminary patch to check OpenDrain/Active low registers before accessing them

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h      |  15 +-
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 195 ++++++++++++-------
 2 files changed, 130 insertions(+), 80 deletions(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h +7 -8
index 21d14072d1c6..b2c568aadd4c 100644
@@ -205,26 +205,21 @@ struct st_lsm6dsx_ext_dev_settings {
 /**
  * struct st_lsm6dsx_settings - ST IMU sensor settings
  * @wai: Sensor WhoAmI default value.
- * @int1_addr: Control Register address for INT1
- * @int2_addr: Control Register address for INT2
  * @reset_addr: register address for reset/reboot
  * @max_fifo_size: Sensor max fifo length in FIFO words.
  * @id: List of hw id/device name supported by the driver configuration.
  * @channels: IIO channels supported by the device.
+ * @irq_config: interrupts related registers.
  * @odr_table: Hw sensors odr table (Hz + val).
  * @fs_table: Hw sensors gain table (gain + val).
  * @decimator: List of decimator register info (addr + mask).
  * @batch: List of FIFO batching register info (addr + mask).
- * @lir: Latched interrupt register info (addr + mask).
- * @clear_on_read: Clear on read register info (addr + mask).
  * @fifo_ops: Sensor hw FIFO parameters.
  * @ts_settings: Hw timer related settings.
  * @shub_settings: i2c controller related settings.
  */
 struct st_lsm6dsx_settings {
 	u8 wai;
-	u8 int1_addr;
-	u8 int2_addr;
 	u8 reset_addr;
 	u16 max_fifo_size;
 	struct {
@@ -235,12 +230,16 @@ struct st_lsm6dsx_settings {
 		const struct iio_chan_spec *chan;
 		int len;
 	} channels[2];
+	struct {
+		struct st_lsm6dsx_reg irq1;
+		struct st_lsm6dsx_reg irq2;
+		struct st_lsm6dsx_reg lir;
+		struct st_lsm6dsx_reg clear_on_read;
+	} irq_config;
 	struct st_lsm6dsx_odr_table_entry odr_table[2];
 	struct st_lsm6dsx_fs_table_entry fs_table[2];
 	struct st_lsm6dsx_reg decimator[ST_LSM6DSX_MAX_ID];
 	struct st_lsm6dsx_reg batch[ST_LSM6DSX_MAX_ID];
-	struct st_lsm6dsx_reg lir;
-	struct st_lsm6dsx_reg clear_on_read;
 	struct st_lsm6dsx_fifo_ops fifo_ops;
 	struct st_lsm6dsx_hw_ts_settings ts_settings;
 	struct st_lsm6dsx_shub_settings shub_settings;
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +123 -72

356 lines not on this page · raw message

Signature · 1 line
2.21.0
author maintainer
#3 lore raw

PATCH 2/3 iio: imu: st_lsm6dsx: do not access active-low/open-drain regs if not supported

In reply to Lorenzo Bianconi

Move active low and open drain register definitions in hw_settings register map in order to avoid to access them if the sensor does not support them

Fixes 52f4b1f19679 ("iio: imu: st_lsm6dsx: add support for accel/gyro unit of lsm9ds1")
---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h       |  2 +
 .../iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c    | 29 ++++++-----
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c  | 48 +++++++++++++++++++
 3 files changed, 67 insertions(+), 12 deletions(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h +2 -0
index b2c568aadd4c..c213ead31083 100644
@@ -235,6 +235,8 @@ struct st_lsm6dsx_settings {
 		struct st_lsm6dsx_reg irq2;
 		struct st_lsm6dsx_reg lir;
 		struct st_lsm6dsx_reg clear_on_read;
+		struct st_lsm6dsx_reg hla;
+		struct st_lsm6dsx_reg od;
 	} irq_config;
 	struct st_lsm6dsx_odr_table_entry odr_table[2];
 	struct st_lsm6dsx_fs_table_entry fs_table[2];
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +17 -12
index b0f3da1976e4..21c2aad8c56a 100644
@@ -42,10 +42,6 @@
 
 #include "st_lsm6dsx.h"
 
-#define ST_LSM6DSX_REG_HLACTIVE_ADDR		0x12
-#define ST_LSM6DSX_REG_HLACTIVE_MASK		BIT(5)
-#define ST_LSM6DSX_REG_PP_OD_ADDR		0x12
-#define ST_LSM6DSX_REG_PP_OD_MASK		BIT(4)
 #define ST_LSM6DSX_REG_FIFO_MODE_ADDR		0x0a
 #define ST_LSM6DSX_FIFO_MODE_MASK		GENMASK(2, 0)
 #define ST_LSM6DSX_FIFO_ODR_MASK		GENMASK(6, 3)
@@ -704,6 +700,7 @@ int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw)
 {
 	struct device_node *np = hw->dev->of_node;
 	struct st_sensors_platform_data *pdata;
+	const struct st_lsm6dsx_reg *reg;
 	struct iio_buffer *buffer;
 	unsigned long irq_type;
 	bool irq_active_low;
@@ -711,6 +708,7 @@ int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw)
 
 	irq_type = irqd_get_trigger_type(irq_get_irq_data(hw->irq));
 
+	reg = &hw->settings->irq_config.hla;
 	switch (irq_type) {
 	case IRQF_TRIGGER_HIGH:
 	case IRQF_TRIGGER_RISING:
@@ -718,6 +716,10 @@ int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw)
 		break;
 	case IRQF_TRIGGER_LOW:
 	case IRQF_TRIGGER_FALLING:
+		if (!reg->addr) {
+			dev_info(hw->dev, "mode %lx unsupported\n", irq_type);
+			return -EINVAL;
+		}
 		irq_active_low = true;
 		break;
 	default:
@@ -725,20 +727,23 @@ int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw)
 		return -EINVAL;
 	}
 
-	err = regmap_update_bits(hw->regmap, ST_LSM6DSX_REG_HLACTIVE_ADDR,
-				 ST_LSM6DSX_REG_HLACTIVE_MASK,
-				 FIELD_PREP(ST_LSM6DSX_REG_HLACTIVE_MASK,
-					    irq_active_low));
+	err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
+				 ST_LSM6DSX_SHIFT_VAL(irq_active_low,
+						      reg->mask));
 	if (err < 0)
 		return err;
 
 	pdata = (struct st_sensors_platform_data *)hw->dev->platform_data;
 	if ((np && of_property_read_bool(np, "drive-open-drain")) ||
 	    (pdata && pdata->open_drain)) {
-		err = regmap_update_bits(hw->regmap, ST_LSM6DSX_REG_PP_OD_ADDR,
-					 ST_LSM6DSX_REG_PP_OD_MASK,
-					 FIELD_PREP(ST_LSM6DSX_REG_PP_OD_MASK,
-						    1));
+		reg = &hw->settings->irq_config.od;
+		if (!reg->addr) {
+			dev_info(hw->dev, "open drain mode unsupported\n");
+			return -EINVAL;
+		}
+
+		err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
+					 ST_LSM6DSX_SHIFT_VAL(1, reg->mask));
 		if (err < 0)
 			return err;
 
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +48 -0

84 lines not on this page · raw message

Signature · 1 line
2.21.0
author maintainer
#6 lore raw

PATCH 3/3 iio: imu: st_lsm6dsx: add sanity check for read_fifo pointer

In reply to Lorenzo Bianconi

Check read_fifo pointer before using it since we can't assume it is always set adding new sensors

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +6 -0
index 21c2aad8c56a..f80ce77a4154 100644
@@ -588,6 +588,9 @@ int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw *hw)
 {
 	int err;
 
+	if (!hw->settings->fifo_ops.read_fifo)
+		return -ENOTSUPP;
+
 	mutex_lock(&hw->fifo_lock);
 
 	hw->settings->fifo_ops.read_fifo(hw);
@@ -662,6 +665,9 @@ static irqreturn_t st_lsm6dsx_handler_thread(int irq, void *private)
 	struct st_lsm6dsx_hw *hw = private;
 	int count;
 
+	if (!hw->settings->fifo_ops.read_fifo)
+		return IRQ_NONE;
+
 	mutex_lock(&hw->fifo_lock);
 	count = hw->settings->fifo_ops.read_fifo(hw);
 	mutex_unlock(&hw->fifo_lock);
Signature · 1 line
2.21.0
Tested-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] iio: imu: st_lsm6dsx: do not access active-low/open-drain regs if not supported

In reply to Lorenzo Bianconi
Quoted 4 lines · Lorenzo Bianconi

Move active low and open drain register definitions in hw_settings register map in order to avoid to access them if the sensor does not support them

Quoted 8 lines
Fixes 52f4b1f19679 ("iio: imu: st_lsm6dsx: add support for accel/gyro unit of lsm9ds1")
---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h       |  2 +
 .../iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c    | 29 ++++++-----
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c  | 48 +++++++++++++++++++
 3 files changed, 67 insertions(+), 12 deletions(-)
Tested-by on v1 3/3
#7 lore raw

Re: [PATCH 3/3] iio: imu: st_lsm6dsx: add sanity check for read_fifo pointer

In reply to Lorenzo Bianconi
Quoted 8 lines · Lorenzo Bianconi

Check read_fifo pointer before using it since we can't assume it is always set adding new sensors

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c | 6 ++++++
 1 file changed, 6 insertions(+)
#5 lore raw

Re: [PATCH 2/3] iio: imu: st_lsm6dsx: do not access active-low/open-drain regs if not supported

In reply to Martin Kepplinger
Quoted 15 lines · Lorenzo Bianconi
Quoted 4 lines

Move active low and open drain register definitions in hw_settings register map in order to avoid to access them if the sensor does not support them

Quoted 8 lines
Fixes 52f4b1f19679 ("iio: imu: st_lsm6dsx: add support for accel/gyro unit of lsm9ds1")
---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h       |  2 +
 .../iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c    | 29 ++++++-----
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c  | 48 +++++++++++++++++++
 3 files changed, 67 insertions(+), 12 deletions(-)
Quoted 1 line

Hi Martin,

thx for testing the series. I will rework this patch in v2 since even LSM9DS1 supports Open Drain/Active Low.

Regards,
Lorenzo

Quoted 2 lines