Skip to contentSkip to conversation

iio: Support ROHM/Kionix kx022a

PATCH Applied Matti Vaittinen 6 participants 14 messages Oct 24, 2022 – Oct 19, 2023

Subsystems (3)
lore
Share & tools
lore

Matti Vaittinen

author maintainer
#0 lore raw

v5 cover iio: Support ROHM/Kionix kx022a

Add initial support for ROHM/Kionix kx022a accelerometer

About the HW:
KX022A 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)
- support detecting special events like double tap or motion
- can be configured to wake-up system when events are detected.

About the series:

This series adds support for only getting the accelerometer data and configuring the G-range / ODR via IIO. Motion detection or double-tap detection are not supported by the series. The other quite important but still missing piece is the runtime PM. Nevertheless, the driver should be usable and brings the basic support for getting accelerometer data.

Changelog v5:
- KX022A: styling/print fixes.
- KX022A: fix race for requesting IRQ/populating user-space entries.
- KX022A: fix few variable types
- KX022A: drop unnecessary udelay()
- MAINTAINERS: Change myself to be a maintainer.

Changelog v4: (More detailed log in individual patches)
- KX022A: mostly styling/print fixes.
- KX022A: fix return value from IRQ thread handler.

Changelog v3: (More detailed log in individual patches)
- dt-bindings: fixed as suggested by Krzysztof
- KX022A: multiple fixes as suggested by Andy
- Fair to mention that couple of suggested changes are not done (Some styling, boilerplate text in KConfig) as there has been no consensus whether the suggested changes improve situation. Ultimately the decision whether these changes are mandatory lay on the shoulders of the maintainers... Sorry for that.
- Enable the FIFO without module param now that the "FIFO stuck" issue can be prevented.
- Fix potential race with time-stamp calculation when user-space invokes the fifo flush.
- Drop the already merged regulator patches.

Changelog v2: (More detailed log in individual patches)
- dt-bindings: fixed as suggested by Krzysztof
- dt-bindings: support both INT1 and INT2
- KX022A: multiple fixes as suggested by Jonathan
- fixed SPI driver after some testing

---

Matti Vaittinen (3):
dt-bindings: iio: Add KX022A accelerometer
iio: accel: Support Kionix/ROHM KX022A accelerometer
MAINTAINERS: Add KX022A maintainer entry

 .../bindings/iio/accel/kionix,kx022a.yaml     |   65 +
 MAINTAINERS                                   |    5 +
 drivers/iio/accel/Kconfig                     |   21 +
 drivers/iio/accel/Makefile                    |    3 +
 drivers/iio/accel/kionix-kx022a-i2c.c         |   51 +
 drivers/iio/accel/kionix-kx022a-spi.c         |   58 +
 drivers/iio/accel/kionix-kx022a.c             | 1142 +++++++++++++++++
 drivers/iio/accel/kionix-kx022a.h             |   82 ++
 8 files changed, 1427 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/accel/kionix,kx022a.yaml
 create mode 100644 drivers/iio/accel/kionix-kx022a-i2c.c
 create mode 100644 drivers/iio/accel/kionix-kx022a-spi.c
 create mode 100644 drivers/iio/accel/kionix-kx022a.c
 create mode 100644 drivers/iio/accel/kionix-kx022a.h

base-commit: 9abf2313adc1ca1b6180c508c25f22f9395cc780

Signature · 14 lines
2.37.3


-- 
Matti Vaittinen, Linux device drivers
ROHM Semiconductors, Finland SWDC
Kiviharjunlenkki 1E
90220 OULU
FINLAND

~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~
Simon says - in Latin please.
~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~
Thanks to Simon Glass for the translation =] 
author maintainer
#1 lore raw

PATCH v5 1/3 dt-bindings: iio: Add KX022A accelerometer

KX022A is a 3-axis Accelerometer from ROHM/Kionix. The sensor features include variable ODRs, I2C and SPI control, FIFO/LIFO with watermark IRQ, tap/motion detection, wake-up & back-to-sleep events, four acceleration ranges (2, 4, 8 and 16g) and probably some other cool features.

Add the basic device tree description for the accelerometer. Only basic accelerometer features are considered as of now - new properties may or may not be needed in the future when rest of the features are supported.

---
v3 => No changes.

v2 => v3 as suggested by Krzysztof:
- better check for the interrupt-names

RFCv1 => v2:
Based on a review by Krzysztof:
- fix a typo from commit message
- const compatible
- drop unnecessary descriptions/words
- io_vdd-supply => io-vdd-supply
- fix the binding example indentiation
Also,
- change my email address
- support both INT pins

---
 .../bindings/iio/accel/kionix,kx022a.yaml     | 65 +++++++++++++++++++
 1 file changed, 65 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/accel/kionix,kx022a.yaml
Documentation/devicetree/bindings/iio/accel/kionix,kx022a.yaml new file +65 -0
new file mode 100644
index 000000000000..986df1a6ff0a
@@ -0,0 +1,65 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/accel/kionix,kx022a.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: ROHM/Kionix KX022A Accelerometer
+
+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.
+
+properties:
+  compatible:
+    const: kionix,kx022a
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    minItems: 1
+    maxItems: 2
+
+  interrupt-names:
+    minItems: 1
+    items:
+      - enum: [INT1, INT2]
+      - const: INT2
+
+  vdd-supply: true
+  io-vdd-supply: true
+
+  mount-matrix:
+    description: |
+      an optional 3x3 mounting rotation matrix.
+
+required:
+  - compatible
+  - reg
+  - interrupts
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+        accel@1f {
+            compatible = "kionix,kx022a";
+            reg = <0x1f>;
+
+            interrupt-parent = <&gpio1>;
+            interrupts = <29 IRQ_TYPE_LEVEL_LOW>;
+            interrupt-names = "INT1";
+
+            io-vdd-supply = <&iovdd>;
+            vdd-supply = <&vdd>;
+        };
+    };
Signature · 14 lines
2.37.3


-- 
Matti Vaittinen, Linux device drivers
ROHM Semiconductors, Finland SWDC
Kiviharjunlenkki 1E
90220 OULU
FINLAND

~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~
Simon says - in Latin please.
~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~
Thanks to Simon Glass for the translation =] 
author maintainer
#2 lore raw

PATCH v5 2/3 iio: accel: Support Kionix/ROHM KX022A accelerometer

In reply to Matti Vaittinen

KX022A is a 3-axis accelerometer from ROHM/Kionix. The sensor features include variable ODRs, I2C and SPI control, FIFO/LIFO with watermark IRQ, tap/motion detection, wake-up & back-to-sleep events, four acceleration ranges (2, 4, 8 and 16g), and probably some other cool features.

Add support for the basic accelerometer features such as getting the acceleration data via IIO. (raw reads, triggered buffer [data-ready] or using the WMI IRQ).

Important things to be added include the double-tap, motion detection and wake-up as well as the runtime power management.

---
v4 => v5: Fixes suggested by Andy and Jonathan
- use namespace IIO_KX022A
- styling fixes
- use correct types and assignment instead of memcpy()
- fix variable type 'int' => 'irqreturn_t'
- remove unnecessary udelay() - was a leftover
- request IRQ before allowing user-space to acces it.
- drop warning about unsupported dual IRQs in DT

v3 => v4 fixes suggested by Andy:
- styling changes
- use str_on_off()
- drop check for !dev
- drop adding return value to print printed using dev_err_probe()
- use dev_err_probe() in SPI/I2C drivers too
- fix IRQ thread return value

v2 => v3 Mostly fixes suggested by Andy
- styling issues
- spell-checks
- use namespaces for exported symbols
- drop module param
- document the lock
- change value written when clearing fifo
- drop WARN_ON()
- correctly check the return value from fwnode_irq_get_byname()
- do not emphasize config Y over M
- reorder struct kx022a_data from potential optimization
- IIO_DEVICE_ATTR_RO instead of IIO_DEVICE_ATTR where applicable
- directly include bits.h for BIT()
- use sysfs_emit() for sysfs
- use unique name for IRQ
- convert read_raw() values to CPU endianess
- fix HW-fifo size to 258 bytes
- kx022a-spi, Fix kconfig dependency
- disable irq (to protect timestamp / sample amount calculation) when the fifo flush is iniriated by user-space

RFCv1 => v2 (mostly based on feedback from Jonathan):
- Fix bunch of typos from the commit message.
- Add missing break; to the kx022a_write_raw()
- Fix SPI driver to use of_match_table
- Fix indentiation in I2C driver
- Drop struct kx022a_trigger
- Drop cross references from Kconfig
- Use /* */ also in file header comments
- Misc minor styling
- Do sensor-reset at probe
- Support both IRQ pins
- Implement read_avail callback
- Use dma aligned buffers for bulk-reads
- Use iio_trigger_poll_chained()
- Use devm consistently
- Drop inclusion of device.h
- Add SPI device ID for module loading
- Add module param for hw fifo / watermark IRQ usage
- Fix io-vdd-supply name to match one in the bindings

---
 drivers/iio/accel/Kconfig             |   21 +
 drivers/iio/accel/Makefile            |    3 +
 drivers/iio/accel/kionix-kx022a-i2c.c |   51 ++
 drivers/iio/accel/kionix-kx022a-spi.c |   58 ++
 drivers/iio/accel/kionix-kx022a.c     | 1142 +++++++++++++++++++++++++
 drivers/iio/accel/kionix-kx022a.h     |   82 ++
 6 files changed, 1357 insertions(+)
 create mode 100644 drivers/iio/accel/kionix-kx022a-i2c.c
 create mode 100644 drivers/iio/accel/kionix-kx022a-spi.c
 create mode 100644 drivers/iio/accel/kionix-kx022a.c
 create mode 100644 drivers/iio/accel/kionix-kx022a.h
drivers/iio/accel/Kconfig +21 -0
index ffac66db7ac9..b7fd054819d2 100644
@@ -409,6 +409,27 @@ config IIO_ST_ACCEL_SPI_3AXIS
 	  To compile this driver as a module, choose M here. The module
 	  will be called st_accel_spi.
 
+config IIO_KX022A
+	tristate
+
+config IIO_KX022A_SPI
+	tristate "Kionix KX022A tri-axis digital accelerometer"
+	depends on SPI
+	select IIO_KX022A
+	select REGMAP_SPI
+	help
+	  Enable support for the Kionix KX022A digital tri-axis
+	  accelerometer connected to I2C interface.
+
+config IIO_KX022A_I2C
+	tristate "Kionix KX022A tri-axis digital accelerometer"
+	depends on I2C
+	select IIO_KX022A
+	select REGMAP_I2C
+	help
+	  Enable support for the Kionix KX022A digital tri-axis
+	  accelerometer connected to I2C interface.
+
 config KXSD9
 	tristate "Kionix KXSD9 Accelerometer Driver"
 	select IIO_BUFFER
drivers/iio/accel/Makefile +3 -0
index 5e45b5fa5ab5..311ead9c3ef1 100644
@@ -40,6 +40,9 @@ obj-$(CONFIG_FXLS8962AF)	+= fxls8962af-core.o
 obj-$(CONFIG_FXLS8962AF_I2C)	+= fxls8962af-i2c.o
 obj-$(CONFIG_FXLS8962AF_SPI)	+= fxls8962af-spi.o
 obj-$(CONFIG_HID_SENSOR_ACCEL_3D) += hid-sensor-accel-3d.o
+obj-$(CONFIG_IIO_KX022A)	+= kionix-kx022a.o
+obj-$(CONFIG_IIO_KX022A_I2C)	+= kionix-kx022a-i2c.o
+obj-$(CONFIG_IIO_KX022A_SPI)	+= kionix-kx022a-spi.o
 obj-$(CONFIG_KXCJK1013) += kxcjk-1013.o
 obj-$(CONFIG_KXSD9)	+= kxsd9.o
 obj-$(CONFIG_KXSD9_SPI)	+= kxsd9-spi.o
drivers/iio/accel/kionix-kx022a-i2c.c new file +51 -0

51 lines not on this page · raw message

drivers/iio/accel/kionix-kx022a-spi.c new file +58 -0

58 lines not on this page · raw message

drivers/iio/accel/kionix-kx022a.c new file +1142 -0

1,142 lines not on this page · raw message

drivers/iio/accel/kionix-kx022a.h new file +82 -0

82 lines not on this page · raw message

Signature · 14 lines
2.37.3


-- 
Matti Vaittinen, Linux device drivers
ROHM Semiconductors, Finland SWDC
Kiviharjunlenkki 1E
90220 OULU
FINLAND

~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~
Simon says - in Latin please.
~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~
Thanks to Simon Glass for the translation =] 
#3 lore raw

Re: [PATCH v5 2/3] iio: accel: Support Kionix/ROHM KX022A accelerometer

Quoted 11 lines · Matti Vaittinen

KX022A is a 3-axis accelerometer from ROHM/Kionix. The sensor features include variable ODRs, I2C and SPI control, FIFO/LIFO with watermark IRQ, tap/motion detection, wake-up & back-to-sleep events, four acceleration ranges (2, 4, 8 and 16g), and probably some other cool features.

Add support for the basic accelerometer features such as getting the acceleration data via IIO. (raw reads, triggered buffer [data-ready] or using the WMI IRQ).

Important things to be added include the double-tap, motion detection and wake-up as well as the runtime power management.

I think I gave you my tag, no?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#4 lore raw

Re: [PATCH v5 2/3] iio: accel: Support Kionix/ROHM KX022A accelerometer

Quoted 6 lines · Andy Shevchenko
Quoted 5 lines · Matti Vaittinen

KX022A is a 3-axis accelerometer from ROHM/Kionix. The sensor features include variable ODRs, I2C and SPI control, FIFO/LIFO with watermark IRQ, tap/motion detection, wake-up & back-to-sleep events, four acceleration ranges (2, 4, 8 and 16g), and probably some other cool features.

Quoted 9 lines
Quoted 6 lines

Add support for the basic accelerometer features such as getting the acceleration data via IIO. (raw reads, triggered buffer [data-ready] or using the WMI IRQ).

Important things to be added include the double-tap, motion detection and wake-up as well as the runtime power management.

I think I gave you my tag, no?

Sorry Andy. Yes you did. I forgot to add it - completely my bad.
If I need to respin I'll add it - if I don't need to respin, Jonathan, can you please add RBT from Andy if you merge this version?

Sorry for the hassle!

Yours,
-- Matti

Signature · 5 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~
maintainer
#5 lore raw

Re: [PATCH v5 2/3] iio: accel: Support Kionix/ROHM KX022A accelerometer

In reply to Matti Vaittinen
Quoted 14 lines · On Mon, 24 Oct 2022 15:40:29 +0300 Matti Vaittinen <[email protected]>…

KX022A is a 3-axis accelerometer from ROHM/Kionix. The sensor features include variable ODRs, I2C and SPI control, FIFO/LIFO with watermark IRQ, tap/motion detection, wake-up & back-to-sleep events, four acceleration ranges (2, 4, 8 and 16g), and probably some other cool features.

Add support for the basic accelerometer features such as getting the acceleration data via IIO. (raw reads, triggered buffer [data-ready] or using the WMI IRQ).

Important things to be added include the double-tap, motion detection and wake-up as well as the runtime power management.

...

Quoted 18 lines
---
 drivers/iio/accel/Kconfig             |   21 +
 drivers/iio/accel/Makefile            |    3 +
 drivers/iio/accel/kionix-kx022a-i2c.c |   51 ++
 drivers/iio/accel/kionix-kx022a-spi.c |   58 ++
 drivers/iio/accel/kionix-kx022a.c     | 1142 +++++++++++++++++++++++++
 drivers/iio/accel/kionix-kx022a.h     |   82 ++
 6 files changed, 1357 insertions(+)
 create mode 100644 drivers/iio/accel/kionix-kx022a-i2c.c
 create mode 100644 drivers/iio/accel/kionix-kx022a-spi.c
 create mode 100644 drivers/iio/accel/kionix-kx022a.c
 create mode 100644 drivers/iio/accel/kionix-kx022a.h
drivers/iio/accel/Kconfig +0 -0
index ffac66db7ac9..b7fd054819d2 100644

drivers/iio/accel/Kconfig config IIO_ST_ACCEL_SPI_3AXIS · line 416

Quoted 8 lines
drivers/iio/accel/Kconfig +5 -0
@@ -409,6 +409,27 @@ config IIO_ST_ACCEL_SPI_3AXIS
 	  To compile this driver as a module, choose M here. The module
 	  will be called st_accel_spi.
 
+config IIO_KX022A
+	tristate
+
+config IIO_KX022A_SPI
+	tristate "Kionix KX022A tri-axis digital accelerometer"

Entry should mention SPI / I2C or we have two menu options that are identical. I'll fix that if nothing else comes up.

Quoted 6 lines
drivers/iio/accel/Kconfig +6 -0
+	depends on SPI
+	select IIO_KX022A
+	select REGMAP_SPI
+	help
+	  Enable support for the Kionix KX022A digital tri-axis
+	  accelerometer connected to I2C interface.
#6 lore raw

Re: [PATCH v5 2/3] iio: accel: Support Kionix/ROHM KX022A accelerometer

In reply to Matti Vaittinen

Hi Matti,

Quoted 5 lines · Matti Vaittinen

KX022A is a 3-axis accelerometer from ROHM/Kionix. The sensor features include variable ODRs, I2C and SPI control, FIFO/LIFO with watermark IRQ, tap/motion detection, wake-up & back-to-sleep events, four acceleration ranges (2, 4, 8 and 16g), and probably some other cool features.

This is a nice driver, and I found it very helpful as a reference. One question regarding scale please see below.

Quoted 11 lines

+ * range is typically +-2G/4G/8G/16G, distributed over the amount of bits.
+ * The scale table can be calculated using

+ *     (range / 2^bits) * g = (range / 2^bits) * 9.80665 m/s^2
+ *     => KX022A uses 16 bit (HiRes mode - assume the low 8 bits are zeroed
+ *     in low-power mode(?) )
+ *     => +/-2G  => 4 / 2^16 * 9,80665 * 10^6 (to scale to micro)
+ *     => +/-2G  - 598.550415
+ *        +/-4G  - 1197.10083
+ *        +/-8G  - 2394.20166
+ *        +/-16G - 4788.40332

+ */

drivers/iio/accel/kionix-kx022a.c line 288

Quoted 6 lines
drivers/iio/accel/kionix-kx022a.c new file +6 -0
+static const int kx022a_scale_table[][2] = {
+       { 598, 550415 },
+       { 1197, 100830 },
+       { 2394, 201660 },
+       { 4788, 403320 },
+};

Given that the integer part is non-zero, and IIO_VAL_INT_PLUS_MICRO is returned for read_scale, As raw value will never be fractional how does this correspond to a reading of 9.8 m/s² for the Z-axis?

Quoted 13 lines
drivers/iio/accel/kionix-kx022a.c new file +13 -0
+
+static int kx022a_read_avail(struct iio_dev *indio_dev,
+                            struct iio_chan_spec const *chan,
+                            const int **vals, int *type, int *length,
+                            long mask)
+{
+       switch (mask) {
+       case IIO_CHAN_INFO_SAMP_FREQ:
+               *vals = (const int *)kx022a_accel_samp_freq_table;
+               *length = ARRAY_SIZE(kx022a_accel_samp_freq_table) *
+                         ARRAY_SIZE(kx022a_accel_samp_freq_table[0]);
+               *type = IIO_VAL_INT_PLUS_MICRO;
+               return IIO_AVAIL_LIST;

drivers/iio/accel/kionix-kx022a.c line 311

Quoted 10 lines
drivers/iio/accel/kionix-kx022a.c new file +10 -0
+       case IIO_CHAN_INFO_SCALE:
+               *vals = (const int *)kx022a_scale_table;
+               *length = ARRAY_SIZE(kx022a_scale_table) *
+                         ARRAY_SIZE(kx022a_scale_table[0]);
+               *type = IIO_VAL_INT_PLUS_MICRO;
+               return IIO_AVAIL_LIST;
+       default:
+               return -EINVAL;
+       }
+}

Regards
Jagath

maintainer
#7 lore raw

Re: [PATCH v5 2/3] iio: accel: Support Kionix/ROHM KX022A accelerometer

Quoted 27 lines · On Wed, 18 Oct 2023 01:37:12 +0530 Jagath Jog J <[email protected]> wrote:

Hi Matti,

Quoted 5 lines · Matti Vaittinen

KX022A is a 3-axis accelerometer from ROHM/Kionix. The sensor features include variable ODRs, I2C and SPI control, FIFO/LIFO with watermark IRQ, tap/motion detection, wake-up & back-to-sleep events, four acceleration ranges (2, 4, 8 and 16g), and probably some other cool features.

This is a nice driver, and I found it very helpful as a reference. One question regarding scale please see below.

Quoted 14 lines

+ * range is typically +-2G/4G/8G/16G, distributed over the amount of bits.
+ * The scale table can be calculated using

+ *     (range / 2^bits) * g = (range / 2^bits) * 9.80665 m/s^2
+ *     => KX022A uses 16 bit (HiRes mode - assume the low 8 bits are zeroed
+ *     in low-power mode(?) )
+ *     => +/-2G  => 4 / 2^16 * 9,80665 * 10^6 (to scale to micro)
+ *     => +/-2G  - 598.550415
+ *        +/-4G  - 1197.10083
+ *        +/-8G  - 2394.20166
+ *        +/-16G - 4788.40332

+ */

drivers/iio/accel/kionix-kx022a.c new file +3 -0
+static const int kx022a_scale_table[][2] = {
+       { 598, 550415 },
+       { 1197, 100830 },
Quoted 8 lines

drivers/iio/accel/kionix-kx022a.c line 288

Quoted 3 lines
drivers/iio/accel/kionix-kx022a.c new file +3 -0
+       { 2394, 201660 },
+       { 4788, 403320 },
+};  

Given that the integer part is non-zero, and IIO_VAL_INT_PLUS_MICRO is returned for read_scale, As raw value will never be fractional how does this correspond to a reading of 9.8 m/s² for the Z-axis?

Definitely suspicious as should be in m/s^2 for an acceleration and it should be

9.8*16/2^bits

So I think these are out by a factor of 10^6

Quoted 27 lines
Quoted 14 lines
drivers/iio/accel/kionix-kx022a.c new file +13 -0
+
+static int kx022a_read_avail(struct iio_dev *indio_dev,
+                            struct iio_chan_spec const *chan,
+                            const int **vals, int *type, int *length,
+                            long mask)
+{
+       switch (mask) {
+       case IIO_CHAN_INFO_SAMP_FREQ:
+               *vals = (const int *)kx022a_accel_samp_freq_table;
+               *length = ARRAY_SIZE(kx022a_accel_samp_freq_table) *
+                         ARRAY_SIZE(kx022a_accel_samp_freq_table[0]);
+               *type = IIO_VAL_INT_PLUS_MICRO;
+               return IIO_AVAIL_LIST;

drivers/iio/accel/kionix-kx022a.c line 311

Quoted 10 lines
drivers/iio/accel/kionix-kx022a.c new file +10 -0
+       case IIO_CHAN_INFO_SCALE:
+               *vals = (const int *)kx022a_scale_table;
+               *length = ARRAY_SIZE(kx022a_scale_table) *
+                         ARRAY_SIZE(kx022a_scale_table[0]);
+               *type = IIO_VAL_INT_PLUS_MICRO;
+               return IIO_AVAIL_LIST;
+       default:
+               return -EINVAL;
+       }
+}  

Regards
Jagath

author maintainer
#8 lore raw

Re: [PATCH v5 2/3] iio: accel: Support Kionix/ROHM KX022A accelerometer

Quoted 2 lines · Jonathan Cameron

On Wed, 18 Oct 2023 01:37:12 +0530
Jagath Jog J <[email protected]> wrote:

Hi Jagath - and thanks!

Quoted 36 lines
Quoted 27 lines

Hi Matti,

Quoted 5 lines · Matti Vaittinen

KX022A is a 3-axis accelerometer from ROHM/Kionix. The sensor features include variable ODRs, I2C and SPI control, FIFO/LIFO with watermark IRQ, tap/motion detection, wake-up & back-to-sleep events, four acceleration ranges (2, 4, 8 and 16g), and probably some other cool features.

This is a nice driver, and I found it very helpful as a reference. One question regarding scale please see below.

Quoted 14 lines

+ * range is typically +-2G/4G/8G/16G, distributed over the amount of bits.
+ * The scale table can be calculated using

+ *     (range / 2^bits) * g = (range / 2^bits) * 9.80665 m/s^2
+ *     => KX022A uses 16 bit (HiRes mode - assume the low 8 bits are zeroed
+ *     in low-power mode(?) )
+ *     => +/-2G  => 4 / 2^16 * 9,80665 * 10^6 (to scale to micro)
+ *     => +/-2G  - 598.550415
+ *        +/-4G  - 1197.10083
+ *        +/-8G  - 2394.20166
+ *        +/-16G - 4788.40332

+ */

drivers/iio/accel/kionix-kx022a.c new file +3 -0
+static const int kx022a_scale_table[][2] = {
+       { 598, 550415 },
+       { 1197, 100830 },
Quoted 8 lines

drivers/iio/accel/kionix-kx022a.c line 288

Quoted 3 lines
drivers/iio/accel/kionix-kx022a.c new file +3 -0
+       { 2394, 201660 },
+       { 4788, 403320 },
+};

Given that the integer part is non-zero, and IIO_VAL_INT_PLUS_MICRO is returned for read_scale, As raw value will never be fractional how does this correspond to a reading of 9.8 m/s² for the Z-axis?

Quoted 6 lines

Definitely suspicious as should be in m/s^2 for an acceleration and it should be

9.8*16/2^bits

So I think these are out by a factor of 10^6

I think you are right. Looks like I misinterpreted the meaning of IIO_VAL_INT_PLUS_MICRO when I took my first tour in the IIO with this driver. The comment above the scale table does support that assumption ... 10^6 would match such a brainfart. (This is my first thought. I will take better look at this later today and see if I can come up with a fix if no-one else has sent a patch already).

I CC'd Mehdi who has also been working on this driver.

Regarding the KX022A - I am not aware of upstream users of this IC (yet). May be you're the first lucky one :) Hence, I am tempted to just fixing the driver - but it's Jonathan who will take the splatters when **** hits the fan - so it's his call to decide whether we can still fix this. _If_ there are users who have adapted to this buggy scale (users I am not aware of) then fix will break their apps. Mehdi, do you know any users of this upstream driver?

I will ping the HQ guy who has contacts to those who might be using the driver in a downstream repository and ask him to inform potential users.

It'd be very nice to get this fixed.

Sorry and thanks!

Yours,
-- Matti

Signature · 5 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~
#9 lore raw

Re: [PATCH v5 2/3] iio: accel: Support Kionix/ROHM KX022A accelerometer

Quoted 57 lines · On Thu, 19 Oct 2023 08:53:31 +0300 Matti Vaittinen <[email protected]>…
Quoted 2 lines · Jonathan Cameron

On Wed, 18 Oct 2023 01:37:12 +0530
Jagath Jog J <[email protected]> wrote:

Hi Jagath - and thanks!

Quoted 36 lines
Quoted 27 lines

Hi Matti,

Quoted 5 lines · Matti Vaittinen

KX022A is a 3-axis accelerometer from ROHM/Kionix. The sensor features include variable ODRs, I2C and SPI control, FIFO/LIFO with watermark IRQ, tap/motion detection, wake-up & back-to-sleep events, four acceleration ranges (2, 4, 8 and 16g), and probably some other cool features.

This is a nice driver, and I found it very helpful as a reference. One question regarding scale please see below.

Quoted 14 lines

+ * range is typically +-2G/4G/8G/16G, distributed over the amount of bits.
+ * The scale table can be calculated using

+ *     (range / 2^bits) * g = (range / 2^bits) * 9.80665 m/s^2
+ *     => KX022A uses 16 bit (HiRes mode - assume the low 8 bits are zeroed
+ *     in low-power mode(?) )
+ *     => +/-2G  => 4 / 2^16 * 9,80665 * 10^6 (to scale to micro)
+ *     => +/-2G  - 598.550415
+ *        +/-4G  - 1197.10083
+ *        +/-8G  - 2394.20166
+ *        +/-16G - 4788.40332

+ */

drivers/iio/accel/kionix-kx022a.c new file +3 -0
+static const int kx022a_scale_table[][2] = {
+       { 598, 550415 },
+       { 1197, 100830 },
Quoted 8 lines

drivers/iio/accel/kionix-kx022a.c line 288

Quoted 3 lines
drivers/iio/accel/kionix-kx022a.c new file +3 -0
+       { 2394, 201660 },
+       { 4788, 403320 },
+};  

Given that the integer part is non-zero, and IIO_VAL_INT_PLUS_MICRO is returned for read_scale, As raw value will never be fractional how does this correspond to a reading of 9.8 m/s² for the Z-axis?

Quoted 6 lines

Definitely suspicious as should be in m/s^2 for an acceleration and it should be

9.8*16/2^bits

So I think these are out by a factor of 10^6

I think you are right. Looks like I misinterpreted the meaning of IIO_VAL_INT_PLUS_MICRO when I took my first tour in the IIO with this driver. The comment above the scale table does support that assumption ... 10^6 would match such a brainfart. (This is my first thought. I will take better look at this later today and see if I can come up with a fix if no-one else has sent a patch already).

I CC'd Mehdi who has also been working on this driver.

Quoted 7 lines

Regarding the KX022A - I am not aware of upstream users of this IC (yet). May be you're the first lucky one :) Hence, I am tempted to just fixing the driver - but it's Jonathan who will take the splatters when **** hits the fan - so it's his call to decide whether we can still fix this. _If_ there are users who have adapted to this buggy scale (users I am not aware of) then fix will break their apps. Mehdi, do you know any users of this upstream driver?

It's an ABI usage bug so allowed fix even if it has impacts... Hopefully those aren't too painful for people to fix :(

So we fix and get it into stable asap.

Quoted 11 lines

I will ping the HQ guy who has contacts to those who might be using the driver in a downstream repository and ask him to inform potential users.

It'd be very nice to get this fixed.

Sorry and thanks!

Yours,
-- Matti

author maintainer
#10 lore raw

PATCH v5 3/3 MAINTAINERS: Add KX022A maintainer entry

In reply to Matti Vaittinen

Add maintainer entry for ROHM/Kionix KX022A accelerometer sensor driver.

---
v4 => v5: After discussion with Joe Perches
- change myself to a maintainer

---
 MAINTAINERS | 5 +++++
 1 file changed, 5 insertions(+)
MAINTAINERS +5 -0
index cf0f18502372..63cae24a74a5 100644
@@ -11435,6 +11435,11 @@ F:	drivers/mfd/khadas-mcu.c
 F:	include/linux/mfd/khadas-mcu.h
 F:	drivers/thermal/khadas_mcu_fan.c
 
+KIONIX/ROHM KX022A ACCELEROMETER
+M:	Matti Vaittinen <[email protected]>
+S:	Supported
+F:	drivers/iio/accel/kionix-kx022a*
+
 KMEMLEAK
 M:	Catalin Marinas <[email protected]>
 S:	Maintained
Signature · 14 lines
2.37.3


-- 
Matti Vaittinen, Linux device drivers
ROHM Semiconductors, Finland SWDC
Kiviharjunlenkki 1E
90220 OULU
FINLAND

~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~
Simon says - in Latin please.
~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~
Thanks to Simon Glass for the translation =] 
maintainer
#11 lore raw

Re: [PATCH v5 3/3] MAINTAINERS: Add KX022A maintainer entry

Quoted 3 lines · On Mon, 24 Oct 2022 15:40:58 +0300 Matti Vaittinen <[email protected]>…

Add maintainer entry for ROHM/Kionix KX022A accelerometer sensor driver.

Whilst scripts will pick up the subsystem entry, I think it is also good to have an L: entry for [email protected] for anyone reading MAINTAINERS directly to figure out who to email.

As such I've added one whilst applying.

Jonathan

Quoted 24 lines

---
v4 => v5: After discussion with Joe Perches
- change myself to a maintainer

---
 MAINTAINERS | 5 +++++
 1 file changed, 5 insertions(+)
MAINTAINERS +5 -0
index cf0f18502372..63cae24a74a5 100644
@@ -11435,6 +11435,11 @@ F:	drivers/mfd/khadas-mcu.c
 F:	include/linux/mfd/khadas-mcu.h
 F:	drivers/thermal/khadas_mcu_fan.c
 
+KIONIX/ROHM KX022A ACCELEROMETER
+M:	Matti Vaittinen <[email protected]>
+S:	Supported
+F:	drivers/iio/accel/kionix-kx022a*
+
 KMEMLEAK
 M:	Catalin Marinas <[email protected]>
 S:	Maintained
#12 lore raw

Re: [PATCH v5 3/3] MAINTAINERS: Add KX022A maintainer entry

Quoted 11 lines · Jonathan Cameron
Quoted 3 lines · On Mon, 24 Oct 2022 15:40:58 +0300 Matti Vaittinen <[email protected]>…

Add maintainer entry for ROHM/Kionix KX022A accelerometer sensor driver.

Whilst scripts will pick up the subsystem entry, I think it is also good to have an L: entry for [email protected] for anyone reading MAINTAINERS directly to figure out who to email.

As such I've added one whilst applying.

That makes sense. Thanks!

Yours,
-- Matti

Signature · 5 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~
maintainer Applied
#13 lore raw
In reply to Matti Vaittinen
Quoted 1 line · On Mon, 24 Oct 2022 15:37:57 +0300 Matti Vaittinen <[email protected]>…

Add initial support for ROHM/Kionix kx022a accelerometer

Series applied with minor tweaks as per individual replies + Andy's RB found and added.

Applied to the togreg branch of iio.git and pushed out initially as testing to see if 0-day can find anything we missed before I make a mess of linux-next.

Thanks,

Jonathan

Quoted 75 lines

About the HW:
KX022A 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)
- support detecting special events like double tap or motion
- can be configured to wake-up system when events are detected.

About the series:

This series adds support for only getting the accelerometer data and configuring the G-range / ODR via IIO. Motion detection or double-tap detection are not supported by the series. The other quite important but still missing piece is the runtime PM. Nevertheless, the driver should be usable and brings the basic support for getting accelerometer data.

Changelog v5:
- KX022A: styling/print fixes.
- KX022A: fix race for requesting IRQ/populating user-space entries.
- KX022A: fix few variable types
- KX022A: drop unnecessary udelay()
- MAINTAINERS: Change myself to be a maintainer.

Changelog v4: (More detailed log in individual patches)
- KX022A: mostly styling/print fixes.
- KX022A: fix return value from IRQ thread handler.

Changelog v3: (More detailed log in individual patches)
- dt-bindings: fixed as suggested by Krzysztof
- KX022A: multiple fixes as suggested by Andy
- Fair to mention that couple of suggested changes are not done (Some styling, boilerplate text in KConfig) as there has been no consensus whether the suggested changes improve situation. Ultimately the decision whether these changes are mandatory lay on the shoulders of the maintainers... Sorry for that.
- Enable the FIFO without module param now that the "FIFO stuck" issue can be prevented.
- Fix potential race with time-stamp calculation when user-space invokes the fifo flush.
- Drop the already merged regulator patches.

Changelog v2: (More detailed log in individual patches)
- dt-bindings: fixed as suggested by Krzysztof
- dt-bindings: support both INT1 and INT2
- KX022A: multiple fixes as suggested by Jonathan
- fixed SPI driver after some testing

---

Matti Vaittinen (3):
dt-bindings: iio: Add KX022A accelerometer
iio: accel: Support Kionix/ROHM KX022A accelerometer
MAINTAINERS: Add KX022A maintainer entry

 .../bindings/iio/accel/kionix,kx022a.yaml     |   65 +
 MAINTAINERS                                   |    5 +
 drivers/iio/accel/Kconfig                     |   21 +
 drivers/iio/accel/Makefile                    |    3 +
 drivers/iio/accel/kionix-kx022a-i2c.c         |   51 +
 drivers/iio/accel/kionix-kx022a-spi.c         |   58 +
 drivers/iio/accel/kionix-kx022a.c             | 1142 +++++++++++++++++
 drivers/iio/accel/kionix-kx022a.h             |   82 ++
 8 files changed, 1427 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/accel/kionix,kx022a.yaml
 create mode 100644 drivers/iio/accel/kionix-kx022a-i2c.c
 create mode 100644 drivers/iio/accel/kionix-kx022a-spi.c
 create mode 100644 drivers/iio/accel/kionix-kx022a.c
 create mode 100644 drivers/iio/accel/kionix-kx022a.h

base-commit: 9abf2313adc1ca1b6180c508c25f22f9395cc780