Skip to contentSkip to conversation

iio: accel: Add support for Kionix/ROHM KX132-1211 accelerometer

PATCH In review Mehdi Djait 2 participants 10 messages Sep 16, 2023 – Sep 17, 2023

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

v9 cover iio: accel: Add support for Kionix/ROHM KX132-1211 accelerometer

Hello everyone,

Version 9 for adding support for the kx132-1211 accelerometer

KX132-1211 accelerometer is a sensor which:
- supports G-ranges of (+/-) 2, 4, 8, and 16G
- can be connected to I2C or SPI
- has internal HW FIFO buffer
- supports various ODRs (output data rates)

The KX132-1211 accelerometer is very similar to the KX022A.
One key difference is number of bits to report the number of data bytes that have been stored in the buffer: 8 bits for KX022A vs 10 bits for KX132-1211.

Changes in v9:
- used i2c_get_match_data
- changed the name and description of the function to get available data in HW fifo buffer
- changed the description in the Kconfig file

Changes in v8:
- replaced min_t by min and kmalloc by kmalloc_array as suggested by Andy

Changes in v7:
- added a min_t in kx132_get_fifo_bytes to ensure that we don't that the fifo_bytes is never bigger than the
fifo_length * KX022A_FIFO_SAMPLES_SIZE_BYTES - no matter what we read from I2C as suggested by Matti

Changes in v6:
- check for availability of chip_info for the SPI case
- changed the order of elements in the kx022a_data struct to save memory

Changes in v5:
- moved the "kfree" call to match the reverse of what happens in kx022a_fifo_enable() as suggested by Matti and Jonathan
- used min_t, checked for availability of chip_info and moved the position of u16 buf_smp_lvl_mask as suggested by Andy
- introduced buf_smp_lvl_mask in Patch 7 as suggested by Jonathan

Changes in v4:
- moved the allocation of the fifo_buffer to kx022a_fifo_enable and kx022a_fifo_disable
- some fixes to the regmap ranges of kx132-1211

Changes in v3:
- added two new patches by separating the addition of the i2c_device_id table and the removal of blank lines from other unrelated changes
- fixes a warning detected by the kernel test robot
- made all the changes related the chip_info in one patch

Changes in v2:
- added a new patch for warning when the device_id match fails in the probe function
- added a new patch for the function that retrieves the number of bytes in the buffer
- added a change to the Kconfig file in the patch adding the support for the kx132-1211
- various fixes and modifications listed under each patch

Mehdi Djait (7):
dt-bindings: iio: Add KX132-1211 accelerometer
iio: accel: kionix-kx022a: Remove blank lines
iio: accel: kionix-kx022a: Warn on failed matches and assume
compatibility
iio: accel: kionix-kx022a: Add an i2c_device_id table
iio: accel: kionix-kx022a: Refactor driver and add chip_info structure
iio: accel: kionix-kx022a: Add a function to retrieve number of bytes
in buffer
iio: accel: Add support for Kionix/ROHM KX132-1211 accelerometer

 .../bindings/iio/accel/kionix,kx022a.yaml     |  12 +-
 drivers/iio/accel/Kconfig                     |  10 +-
 drivers/iio/accel/kionix-kx022a-i2c.c         |  20 +-
 drivers/iio/accel/kionix-kx022a-spi.c         |  15 +-
 drivers/iio/accel/kionix-kx022a.c             | 315 ++++++++++++++----
 drivers/iio/accel/kionix-kx022a.h             | 112 ++++++-
 6 files changed, 409 insertions(+), 75 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH v9 1/7 dt-bindings: iio: Add KX132-1211 accelerometer

Extend the kionix,kx022a.yaml file to support the kx132-1211 device

---
 .../devicetree/bindings/iio/accel/kionix,kx022a.yaml | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
Documentation/devicetree/bindings/iio/accel/kionix,kx022a.yaml +7 -5
index 986df1a6ff0a..034b69614416 100644
@@ -4,19 +4,21 @@
 $id: http://devicetree.org/schemas/iio/accel/kionix,kx022a.yaml#
 $schema: http://devicetree.org/meta-schemas/core.yaml#
 
-title: ROHM/Kionix KX022A Accelerometer
+title: ROHM/Kionix KX022A and KX132-1211 Accelerometers
 
 maintainers:
   - Matti Vaittinen <[email protected]>
 
 description: |
-  KX022A is a 3-axis accelerometer supporting +/- 2G, 4G, 8G and 16G ranges,
-  output data-rates from 0.78Hz to 1600Hz and a hardware-fifo buffering.
-  KX022A can be accessed either via I2C or SPI.
+  KX022A and KX132-1211 are 3-axis accelerometers supporting +/- 2G, 4G, 8G and
+  16G ranges, variable output data-rates and a hardware-fifo buffering.
+  KX022A and KX132-1211 can be accessed either via I2C or SPI.
 
 properties:
   compatible:
-    const: kionix,kx022a
+    enum:
+      - kionix,kx022a
+      - kionix,kx132-1211
 
   reg:
     maxItems: 1
Signature · 1 line
2.30.2
#2 lore raw

PATCH v9 2/7 iio: accel: kionix-kx022a: Remove blank lines

In reply to Mehdi Djait

Remove blank lines pointed out by the checkpatch script

---
 drivers/iio/accel/kionix-kx022a.c | 2 --
 1 file changed, 2 deletions(-)
drivers/iio/accel/kionix-kx022a.c +0 -2
index f164b09520c9..2f1e7da25460 100644
@@ -341,7 +341,6 @@ static int kx022a_turn_on_off_unlocked(struct kx022a_data *data, bool on)
 		dev_err(data->dev, "Turn %s fail %d\n", str_on_off(on), ret);
 
 	return ret;
-
 }
 
 static int kx022a_turn_off_lock(struct kx022a_data *data)
@@ -1110,7 +1109,6 @@ int kx022a_probe_internal(struct device *dev)
 	if (ret)
 		return dev_err_probe(data->dev, ret, "Could not request IRQ\n");
 
-
 	ret = devm_iio_trigger_register(dev, indio_trig);
 	if (ret)
 		return dev_err_probe(data->dev, ret,
Signature · 1 line
2.30.2
#3 lore raw

PATCH v9 3/7 iio: accel: kionix-kx022a: Warn on failed matches and assume compatibility

In reply to Mehdi Djait

Avoid error returns on a failure to match and instead just warn with assumption that we have a correct dt-binding telling us that some new device with a different ID is backwards compatible.

---
 drivers/iio/accel/kionix-kx022a.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/iio/accel/kionix-kx022a.c +2 -4
index 2f1e7da25460..5a144f86c634 100644
@@ -1025,10 +1025,8 @@ int kx022a_probe_internal(struct device *dev)
 	if (ret)
 		return dev_err_probe(dev, ret, "Failed to access sensor\n");
 
-	if (chip_id != KX022A_ID) {
-		dev_err(dev, "unsupported device 0x%x\n", chip_id);
-		return -EINVAL;
-	}
+	if (chip_id != KX022A_ID)
+		dev_warn(dev, "unknown device 0x%x\n", chip_id);
 
 	irq = fwnode_irq_get_byname(fwnode, "INT1");
 	if (irq > 0) {
Signature · 1 line
2.30.2
#4 lore raw

PATCH v9 4/7 iio: accel: kionix-kx022a: Add an i2c_device_id table

In reply to Mehdi Djait

Add the missing i2c device id.

---
 drivers/iio/accel/kionix-kx022a-i2c.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/iio/accel/kionix-kx022a-i2c.c +7 -0
index b0ac78e85dad..c0d0b69680f9 100644
@@ -30,6 +30,12 @@ static int kx022a_i2c_probe(struct i2c_client *i2c)
 	return kx022a_probe_internal(dev);
 }
 
+static const struct i2c_device_id kx022a_i2c_id[] = {
+	{ .name = "kx022a" },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, kx022a_i2c_id);
+
 static const struct of_device_id kx022a_of_match[] = {
 	{ .compatible = "kionix,kx022a", },
 	{ }
@@ -43,6 +49,7 @@ static struct i2c_driver kx022a_i2c_driver = {
 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
 	  },
 	.probe        = kx022a_i2c_probe,
+	.id_table     = kx022a_i2c_id,
 };
 module_i2c_driver(kx022a_i2c_driver);
 
Signature · 1 line
2.30.2
#5 lore raw

PATCH v9 5/7 iio: accel: kionix-kx022a: Refactor driver and add chip_info structure

In reply to Mehdi Djait

Add the chip_info structure to the driver's private data to hold all the device specific infos.
Refactor the kx022a driver implementation to make it more generic and extensible.

---
 drivers/iio/accel/kionix-kx022a-i2c.c |  13 ++-
 drivers/iio/accel/kionix-kx022a-spi.c |  13 ++-
 drivers/iio/accel/kionix-kx022a.c     | 117 +++++++++++++++++---------
 drivers/iio/accel/kionix-kx022a.h     |  52 +++++++++++-
 4 files changed, 143 insertions(+), 52 deletions(-)
drivers/iio/accel/kionix-kx022a-i2c.c +9 -4
index c0d0b69680f9..2fdc4c1ab93f 100644
@@ -15,6 +15,7 @@
 static int kx022a_i2c_probe(struct i2c_client *i2c)
 {
 	struct device *dev = &i2c->dev;
+	const struct kx022a_chip_info *chip_info;
 	struct regmap *regmap;
 
 	if (!i2c->irq) {
@@ -22,22 +23,26 @@ static int kx022a_i2c_probe(struct i2c_client *i2c)
 		return -EINVAL;
 	}
 
-	regmap = devm_regmap_init_i2c(i2c, &kx022a_regmap);
+	chip_info = i2c_get_match_data(i2c);
+	if (!chip_info)
+		return -EINVAL;
+
+	regmap = devm_regmap_init_i2c(i2c, chip_info->regmap_config);
 	if (IS_ERR(regmap))
 		return dev_err_probe(dev, PTR_ERR(regmap),
 				     "Failed to initialize Regmap\n");
 
-	return kx022a_probe_internal(dev);
+	return kx022a_probe_internal(dev, chip_info);
 }
 
 static const struct i2c_device_id kx022a_i2c_id[] = {
-	{ .name = "kx022a" },
+	{ .name = "kx022a", .driver_data = (kernel_ulong_t)&kx022a_chip_info },
 	{ }
 };
 MODULE_DEVICE_TABLE(i2c, kx022a_i2c_id);
 
 static const struct of_device_id kx022a_of_match[] = {
-	{ .compatible = "kionix,kx022a", },
+	{ .compatible = "kionix,kx022a", .data = &kx022a_chip_info },
 	{ }
 };
 MODULE_DEVICE_TABLE(of, kx022a_of_match);
drivers/iio/accel/kionix-kx022a-spi.c +9 -4
index f45a46899a5f..c21494ade157 100644
@@ -15,6 +15,7 @@
 static int kx022a_spi_probe(struct spi_device *spi)
 {
 	struct device *dev = &spi->dev;
+	const struct kx022a_chip_info *chip_info;
 	struct regmap *regmap;
 
 	if (!spi->irq) {
@@ -22,22 +23,26 @@ static int kx022a_spi_probe(struct spi_device *spi)
 		return -EINVAL;
 	}
 
-	regmap = devm_regmap_init_spi(spi, &kx022a_regmap);
+	chip_info = spi_get_device_match_data(spi);
+	if (!chip_info)
+		return -EINVAL;
+
+	regmap = devm_regmap_init_spi(spi, chip_info->regmap_config);
 	if (IS_ERR(regmap))
 		return dev_err_probe(dev, PTR_ERR(regmap),
 				     "Failed to initialize Regmap\n");
 
-	return kx022a_probe_internal(dev);
+	return kx022a_probe_internal(dev, chip_info);
 }
 
 static const struct spi_device_id kx022a_id[] = {
-	{ "kx022a" },
+	{ .name = "kx022a", .driver_data = (kernel_ulong_t)&kx022a_chip_info },
 	{ }
 };
 MODULE_DEVICE_TABLE(spi, kx022a_id);
 
 static const struct of_device_id kx022a_of_match[] = {
-	{ .compatible = "kionix,kx022a", },
+	{ .compatible = "kionix,kx022a", .data = &kx022a_chip_info },
 	{ }
 };
 MODULE_DEVICE_TABLE(of, kx022a_of_match);
drivers/iio/accel/kionix-kx022a.c +75 -42

332 lines not on this page · raw message

drivers/iio/accel/kionix-kx022a.h +50 -2

57 lines not on this page · raw message

Signature · 1 line
2.30.2
#6 lore raw

PATCH v9 6/7 iio: accel: kionix-kx022a: Add a function to retrieve number of bytes in buffer

In reply to Mehdi Djait

Since Kionix accelerometers use various numbers of bits to report data, a device-specific function is required.
Implement the function as a callback in the device-specific chip_info structure

---
 drivers/iio/accel/kionix-kx022a.c | 68 +++++++++++++++++--------------
 drivers/iio/accel/kionix-kx022a.h | 46 ++++++++++++---------
 2 files changed, 64 insertions(+), 50 deletions(-)
drivers/iio/accel/kionix-kx022a.c +38 -30
index ab3e5c8a19df..cc7134b1183b 100644
@@ -585,26 +585,33 @@ static int kx022a_drop_fifo_contents(struct kx022a_data *data)
 	return regmap_write(data->regmap, data->chip_info->buf_clear, 0x0);
 }
 
+static int kx022a_get_fifo_bytes_available(struct kx022a_data *data)
+{
+	int ret, fifo_bytes;
+
+	ret = regmap_read(data->regmap, KX022A_REG_BUF_STATUS_1, &fifo_bytes);
+	if (ret) {
+		dev_err(data->dev, "Error reading buffer status\n");
+		return ret;
+	}
+
+	if (fifo_bytes == KX022A_FIFO_FULL_VALUE)
+		return KX022A_FIFO_MAX_BYTES;
+
+	return fifo_bytes;
+}
+
 static int __kx022a_fifo_flush(struct iio_dev *idev, unsigned int samples,
 			       bool irq)
 {
 	struct kx022a_data *data = iio_priv(idev);
-	struct device *dev = regmap_get_device(data->regmap);
 	uint64_t sample_period;
 	int count, fifo_bytes;
 	bool renable = false;
 	int64_t tstamp;
 	int ret, i;
 
-	ret = regmap_read(data->regmap, KX022A_REG_BUF_STATUS_1, &fifo_bytes);
-	if (ret) {
-		dev_err(dev, "Error reading buffer status\n");
-		return ret;
-	}
-
-	/* Let's not overflow if we for some reason get bogus value from i2c */
-	if (fifo_bytes == KX022A_FIFO_FULL_VALUE)
-		fifo_bytes = KX022A_FIFO_MAX_BYTES;
+	fifo_bytes = data->chip_info->get_fifo_bytes_available(data);
 
 	if (fifo_bytes % KX022A_FIFO_SAMPLES_SIZE_BYTES)
 		dev_warn(data->dev, "Bad FIFO alignment. Data may be corrupt\n");
@@ -993,26 +1000,27 @@ static int kx022a_chip_init(struct kx022a_data *data)
 }
 
 const struct kx022a_chip_info kx022a_chip_info = {
-	.name		  = "kx022-accel",
-	.regmap_config	  = &kx022a_regmap_config,
-	.channels	  = kx022a_channels,
-	.num_channels	  = ARRAY_SIZE(kx022a_channels),
-	.fifo_length	  = KX022A_FIFO_LENGTH,
-	.who		  = KX022A_REG_WHO,
-	.id		  = KX022A_ID,
-	.cntl		  = KX022A_REG_CNTL,
-	.cntl2		  = KX022A_REG_CNTL2,
-	.odcntl		  = KX022A_REG_ODCNTL,
-	.buf_cntl1	  = KX022A_REG_BUF_CNTL1,
-	.buf_cntl2	  = KX022A_REG_BUF_CNTL2,
-	.buf_clear	  = KX022A_REG_BUF_CLEAR,
-	.buf_status1	  = KX022A_REG_BUF_STATUS_1,
-	.buf_read	  = KX022A_REG_BUF_READ,
-	.inc1		  = KX022A_REG_INC1,
-	.inc4		  = KX022A_REG_INC4,
-	.inc5		  = KX022A_REG_INC5,
-	.inc6		  = KX022A_REG_INC6,
-	.xout_l		  = KX022A_REG_XOUT_L,
+	.name				= "kx022-accel",
+	.regmap_config			= &kx022a_regmap_config,
+	.channels			= kx022a_channels,
+	.num_channels			= ARRAY_SIZE(kx022a_channels),
+	.fifo_length			= KX022A_FIFO_LENGTH,
+	.who				= KX022A_REG_WHO,
+	.id				= KX022A_ID,
+	.cntl				= KX022A_REG_CNTL,
+	.cntl2				= KX022A_REG_CNTL2,
+	.odcntl				= KX022A_REG_ODCNTL,
+	.buf_cntl1			= KX022A_REG_BUF_CNTL1,
+	.buf_cntl2			= KX022A_REG_BUF_CNTL2,
+	.buf_clear			= KX022A_REG_BUF_CLEAR,
+	.buf_status1			= KX022A_REG_BUF_STATUS_1,
+	.buf_read			= KX022A_REG_BUF_READ,
+	.inc1				= KX022A_REG_INC1,
+	.inc4				= KX022A_REG_INC4,
+	.inc5				= KX022A_REG_INC5,
+	.inc6				= KX022A_REG_INC6,
+	.xout_l				= KX022A_REG_XOUT_L,
+	.get_fifo_bytes_available	= kx022a_get_fifo_bytes_available,
 };
 EXPORT_SYMBOL_NS_GPL(kx022a_chip_info, IIO_KX022A);
 
drivers/iio/accel/kionix-kx022a.h +26 -20

61 lines not on this page · raw message

Signature · 1 line
2.30.2
maintainer
#7 lore raw

Re: [PATCH v9 6/7] iio: accel: kionix-kx022a: Add a function to retrieve number of bytes in buffer

Quoted 19 lines · On Sat, 16 Sep 2023 14:38:52 +0200 Mehdi Djait <[email protected]> wrote:

Since Kionix accelerometers use various numbers of bits to report data, a device-specific function is required.
Implement the function as a callback in the device-specific chip_info structure

const struct kx022a_chip_info kx022a_chip_info = {

drivers/iio/accel/kionix-kx022a.c +0 -10
-	.name		  = "kx022-accel",
-	.regmap_config	  = &kx022a_regmap_config,
-	.channels	  = kx022a_channels,
-	.num_channels	  = ARRAY_SIZE(kx022a_channels),
-	.fifo_length	  = KX022A_FIFO_LENGTH,
-	.who		  = KX022A_REG_WHO,
-	.id		  = KX022A_ID,
-	.cntl		  = KX022A_REG_CNTL,
-	.cntl2		  = KX022A_REG_CNTL2,
-	.odcntl		  = KX022A_REG_ODCNTL,

drivers/iio/accel/kionix-kx022a.c static int kx022a_chip_init(struct kx022a_data *data) · line 1015

Quoted 10 lines
drivers/iio/accel/kionix-kx022a.c +0 -10
-	.buf_cntl1	  = KX022A_REG_BUF_CNTL1,
-	.buf_cntl2	  = KX022A_REG_BUF_CNTL2,
-	.buf_clear	  = KX022A_REG_BUF_CLEAR,
-	.buf_status1	  = KX022A_REG_BUF_STATUS_1,
-	.buf_read	  = KX022A_REG_BUF_READ,
-	.inc1		  = KX022A_REG_INC1,
-	.inc4		  = KX022A_REG_INC4,
-	.inc5		  = KX022A_REG_INC5,
-	.inc6		  = KX022A_REG_INC6,
-	.xout_l		  = KX022A_REG_XOUT_L,

This is a very good illustration of why aligning value assignments is not (in my opinion) a good idea. I'll tweak the earlier patch whilst applying to add the extra indent.

However, in future look through your complete series for cases where code added in an earlier patch is simply reformatted in a later one.

Same applies to the structure kdoc below.

Quoted 91 lines
drivers/iio/accel/kionix-kx022a.c +21 -0
+	.name				= "kx022-accel",
+	.regmap_config			= &kx022a_regmap_config,
+	.channels			= kx022a_channels,
+	.num_channels			= ARRAY_SIZE(kx022a_channels),
+	.fifo_length			= KX022A_FIFO_LENGTH,
+	.who				= KX022A_REG_WHO,
+	.id				= KX022A_ID,
+	.cntl				= KX022A_REG_CNTL,
+	.cntl2				= KX022A_REG_CNTL2,
+	.odcntl				= KX022A_REG_ODCNTL,
+	.buf_cntl1			= KX022A_REG_BUF_CNTL1,
+	.buf_cntl2			= KX022A_REG_BUF_CNTL2,
+	.buf_clear			= KX022A_REG_BUF_CLEAR,
+	.buf_status1			= KX022A_REG_BUF_STATUS_1,
+	.buf_read			= KX022A_REG_BUF_READ,
+	.inc1				= KX022A_REG_INC1,
+	.inc4				= KX022A_REG_INC4,
+	.inc5				= KX022A_REG_INC5,
+	.inc6				= KX022A_REG_INC6,
+	.xout_l				= KX022A_REG_XOUT_L,
+	.get_fifo_bytes_available	= kx022a_get_fifo_bytes_available,
 };
 EXPORT_SYMBOL_NS_GPL(kx022a_chip_info, IIO_KX022A);
drivers/iio/accel/kionix-kx022a.h +26 -20
index 0e5026019213..7ca48e6f2c49 100644
@@ -76,29 +76,34 @@
 
 struct device;
 
+struct kx022a_data;
+
 /**
  * struct kx022a_chip_info - Kionix accelerometer chip specific information
  *
- * @name:		name of the device
- * @regmap_config:	pointer to register map configuration
- * @channels:		pointer to iio_chan_spec array
- * @num_channels:	number of iio_chan_spec channels
- * @fifo_length:	number of 16-bit samples in a full buffer
- * @who:		WHO_AM_I register
- * @id:			WHO_AM_I register value
- * @cntl:		control register 1
- * @cntl2:		control register 2
- * @odcntl:		output data control register
- * @buf_cntl1:		buffer control register 1
- * @buf_cntl2:		buffer control register 2
- * @buf_clear:		buffer clear register
- * @buf_status1:	buffer status register 1
- * @buf_read:		buffer read register
- * @inc1:		interrupt control register 1
- * @inc4:		interrupt control register 4
- * @inc5:		interrupt control register 5
- * @inc6:		interrupt control register 6
- * @xout_l:		x-axis output least significant byte
+ * @name:			name of the device
+ * @regmap_config:		pointer to register map configuration
+ * @channels:			pointer to iio_chan_spec array
+ * @num_channels:		number of iio_chan_spec channels
+ * @fifo_length:		number of 16-bit samples in a full buffer
+ * @who:			WHO_AM_I register
+ * @id:				WHO_AM_I register value
+ * @cntl:			control register 1
+ * @cntl2:			control register 2
+ * @odcntl:			output data control register
+ * @buf_cntl1:			buffer control register 1
+ * @buf_cntl2:			buffer control register 2
+ * @buf_clear:			buffer clear register
+ * @buf_status1:		buffer status register 1
+ * @buf_read:			buffer read register
+ * @inc1:			interrupt control register 1
+ * @inc4:			interrupt control register 4
+ * @inc5:			interrupt control register 5
+ * @inc6:			interrupt control register 6
+ * @xout_l:			x-axis output least significant byte
+ * @get_fifo_bytes_available:	function pointer to get amount of acceleration
+ *				data bytes currently stored in the sensor's FIFO
+ *				buffer
  */
 struct kx022a_chip_info {
 	const char *name;
@@ -121,6 +126,7 @@ struct kx022a_chip_info {
 	u8 inc5;
 	u8 inc6;
 	u8 xout_l;
+	int (*get_fifo_bytes_available)(struct kx022a_data *);
 };
 
 int kx022a_probe_internal(struct device *dev, const struct kx022a_chip_info *chip_info);
#8 lore raw

PATCH v9 7/7 iio: accel: Add support for Kionix/ROHM KX132-1211 accelerometer

In reply to Mehdi Djait

Kionix KX132-1211 is a tri-axis 16-bit accelerometer that can support ranges from ±2G to ±16G, digital output through I²C/SPI.
Add support for basic accelerometer features such as reading acceleration via IIO using raw reads, triggered buffer (data-ready), or the WMI IRQ.

Datasheet: https://kionixfs.azureedge.net/en/document/KX132-1211-Technical-Reference-Manual-Rev-5.0.pdf

---
 drivers/iio/accel/Kconfig             |  10 +-
 drivers/iio/accel/kionix-kx022a-i2c.c |   2 +
 drivers/iio/accel/kionix-kx022a-spi.c |   2 +
 drivers/iio/accel/kionix-kx022a.c     | 164 ++++++++++++++++++++++++++
 drivers/iio/accel/kionix-kx022a.h     |  54 +++++++++
 5 files changed, 228 insertions(+), 4 deletions(-)
drivers/iio/accel/Kconfig +6 -4
index b6b45d359f28..373257d64a7e 100644
@@ -418,8 +418,9 @@ config IIO_KX022A_SPI
 	select IIO_KX022A
 	select REGMAP_SPI
 	help
-	  Enable support for the Kionix KX022A digital tri-axis
-	  accelerometer connected to I2C interface.
+	  Enable support for the Kionix digital tri-axis accelerometers
+	  connected to SPI interface. Supported devices are:
+	    KX022A, KX132-1211
 
 config IIO_KX022A_I2C
 	tristate "Kionix KX022A tri-axis digital accelerometer I2C interface"
@@ -427,8 +428,9 @@ config IIO_KX022A_I2C
 	select IIO_KX022A
 	select REGMAP_I2C
 	help
-	  Enable support for the Kionix KX022A digital tri-axis
-	  accelerometer connected to I2C interface.
+	  Enable support for the Kionix digital tri-axis accelerometers
+	  connected to I2C interface. Supported devices are:
+	    KX022A, KX132-1211
 
 config KXSD9
 	tristate "Kionix KXSD9 Accelerometer Driver"
drivers/iio/accel/kionix-kx022a-i2c.c +2 -0
index 2fdc4c1ab93f..fc53e527cae0 100644
@@ -37,12 +37,14 @@ static int kx022a_i2c_probe(struct i2c_client *i2c)
 
 static const struct i2c_device_id kx022a_i2c_id[] = {
 	{ .name = "kx022a", .driver_data = (kernel_ulong_t)&kx022a_chip_info },
+	{ .name = "kx132-1211", .driver_data = (kernel_ulong_t)&kx132_chip_info },
 	{ }
 };
 MODULE_DEVICE_TABLE(i2c, kx022a_i2c_id);
 
 static const struct of_device_id kx022a_of_match[] = {
 	{ .compatible = "kionix,kx022a", .data = &kx022a_chip_info },
+	{ .compatible = "kionix,kx132-1211", .data = &kx132_chip_info },
 	{ }
 };
 MODULE_DEVICE_TABLE(of, kx022a_of_match);
drivers/iio/accel/kionix-kx022a-spi.c +2 -0
index c21494ade157..e7878ba67827 100644
@@ -37,12 +37,14 @@ static int kx022a_spi_probe(struct spi_device *spi)
 
 static const struct spi_device_id kx022a_id[] = {
 	{ .name = "kx022a", .driver_data = (kernel_ulong_t)&kx022a_chip_info },
+	{ .name = "kx132-1211", .driver_data = (kernel_ulong_t)&kx132_chip_info },
 	{ }
 };
 MODULE_DEVICE_TABLE(spi, kx022a_id);
 
 static const struct of_device_id kx022a_of_match[] = {
 	{ .compatible = "kionix,kx022a", .data = &kx022a_chip_info },
+	{ .compatible = "kionix,kx132-1211", .data = &kx132_chip_info },
 	{ }
 };
 MODULE_DEVICE_TABLE(of, kx022a_of_match);
drivers/iio/accel/kionix-kx022a.c +164 -0

188 lines not on this page · raw message

drivers/iio/accel/kionix-kx022a.h +54 -0

77 lines not on this page · raw message

Signature · 1 line
2.30.2
maintainer
#9 lore raw
In reply to Mehdi Djait
Quoted 10 lines · On Sat, 16 Sep 2023 14:38:46 +0200 Mehdi Djait <[email protected]> wrote:

Hello everyone,

Version 9 for adding support for the kx132-1211 accelerometer

KX132-1211 accelerometer is a sensor which:
- supports G-ranges of (+/-) 2, 4, 8, and 16G
- can be connected to I2C or SPI
- has internal HW FIFO buffer
- supports various ODRs (output data rates)

Quoted 4 lines

The KX132-1211 accelerometer is very similar to the KX022A.
One key difference is number of bits to report the number of data bytes that have been stored in the buffer: 8 bits for KX022A vs 10 bits for KX132-1211.

Series applied with some minor white space tweaks as called out for individual patches.

Applied to the togreg branch of iio.git and initially pushed out as testing for the autobuilders to poke at it and see what we've missed.

Thanks,

Jonathan

Quoted 67 lines

Changes in v9:
- used i2c_get_match_data
- changed the name and description of the function to get available data in HW fifo buffer
- changed the description in the Kconfig file

Changes in v8:
- replaced min_t by min and kmalloc by kmalloc_array as suggested by Andy

Changes in v7:
- added a min_t in kx132_get_fifo_bytes to ensure that we don't that the fifo_bytes is never bigger than the
fifo_length * KX022A_FIFO_SAMPLES_SIZE_BYTES - no matter what we read from I2C as suggested by Matti

Changes in v6:
- check for availability of chip_info for the SPI case
- changed the order of elements in the kx022a_data struct to save memory

Changes in v5:
- moved the "kfree" call to match the reverse of what happens in kx022a_fifo_enable() as suggested by Matti and Jonathan
- used min_t, checked for availability of chip_info and moved the position of u16 buf_smp_lvl_mask as suggested by Andy
- introduced buf_smp_lvl_mask in Patch 7 as suggested by Jonathan

Changes in v4:
- moved the allocation of the fifo_buffer to kx022a_fifo_enable and kx022a_fifo_disable
- some fixes to the regmap ranges of kx132-1211

Changes in v3:
- added two new patches by separating the addition of the i2c_device_id table and the removal of blank lines from other unrelated changes
- fixes a warning detected by the kernel test robot
- made all the changes related the chip_info in one patch

Changes in v2:
- added a new patch for warning when the device_id match fails in the probe function
- added a new patch for the function that retrieves the number of bytes in the buffer
- added a change to the Kconfig file in the patch adding the support for the kx132-1211
- various fixes and modifications listed under each patch

Mehdi Djait (7):
dt-bindings: iio: Add KX132-1211 accelerometer
iio: accel: kionix-kx022a: Remove blank lines
iio: accel: kionix-kx022a: Warn on failed matches and assume
compatibility
iio: accel: kionix-kx022a: Add an i2c_device_id table
iio: accel: kionix-kx022a: Refactor driver and add chip_info structure
iio: accel: kionix-kx022a: Add a function to retrieve number of bytes
in buffer
iio: accel: Add support for Kionix/ROHM KX132-1211 accelerometer

 .../bindings/iio/accel/kionix,kx022a.yaml     |  12 +-
 drivers/iio/accel/Kconfig                     |  10 +-
 drivers/iio/accel/kionix-kx022a-i2c.c         |  20 +-
 drivers/iio/accel/kionix-kx022a-spi.c         |  15 +-
 drivers/iio/accel/kionix-kx022a.c             | 315 ++++++++++++++----
 drivers/iio/accel/kionix-kx022a.h             | 112 ++++++-
 6 files changed, 409 insertions(+), 75 deletions(-)