Skip to contentSkip to conversation

iio: accel: dmard09: Implement IIO_CHAN_INFO_SCALE

PATCH Superseded Mert Seftali 3 participants 3 messages Jul 3 IIO

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: accel: dmard09: Implement IIO_CHAN_INFO_SCALE

From: Mert Seftali <[email protected]>

in_accel_scale has returned -EINVAL ever since the driver was added.

The channels advertise scale via info_mask_shared_by_type, so the IIO core exposes in_accel_scale, but dmard09_read_raw() only handles IIO_CHAN_INFO_RAW; a SCALE read falls through to 'default: return -EINVAL':

    $ cat .../iio:deviceX/in_accel_scale
    cat: in_accel_scale: Invalid argument

leaving userspace with raw counts it cannot convert to m/s^2.

The driver was written from a vendor source [1] without a datasheet, and the scale was declared but never implemented. The vendor source carries the sensitivity: its conversion is

    acc = raw * GRAVITY_EARTH_1000 / sensitivity   (then / 1000 -> m/s^2)

with sensitivity = 32 and GRAVITY_EARTH_1000 = 9807 ("about (9.80665)*1000"), i.e. 32 counts correspond to 1 g.

That sensitivity applies to the value this driver already reports as raw. The vendor processes each 16-bit sample as 'data >>= 3; data &= 0x1ff' plus a sign-extend of bit 8 - a signed 9-bit quantity from register bits [11:3] - and dmard09_read_raw()'s 'accel <<= 4; accel >>= 7' yields the same value. It is self-consistent: 256 counts / 32 = 8 g full scale, matching the part's +/-8g range.

Implement the scale derived from that sensitivity using standard gravity:

    scale = 9.80665 / 32 = 0.3064578125 m/s^2 per LSB

[1] https://github.com/minstrelsy/mediatek/blob/1f49d8c87b839651bc89afc870277e8e0f2e2d55/custom/common/kernel/accelerometer/dmard09/dmard09.c

Fixes a4fa6509dda4 ("iio: accel: add support for the Domintech DMARD09 3-axis accelerometer")
---
 drivers/iio/accel/dmard09.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/iio/accel/dmard09.c +7 -0
index fe35a1270786..a61943120d07 100644
@@ -27,6 +27,9 @@
 #define DMARD09_AXIS_Y_OFFSET ((DMARD09_AXIS_Y + 1) * 2)
 #define DMARD09_AXIS_Z_OFFSET ((DMARD09_AXIS_Z + 1) * 2)
 
+/* Sensitivity is 32 LSB/g; scale = 9.80665 / 32 m/s^2 per LSB. */
+#define DMARD09_SCALE_NANO	306457813
+
 struct dmard09_data {
 	struct i2c_client *client;
 };
@@ -79,6 +82,10 @@ static int dmard09_read_raw(struct iio_dev *indio_dev,
 		*val = accel;
 
 		return IIO_VAL_INT;
+	case IIO_CHAN_INFO_SCALE:
+		*val = 0;
+		*val2 = DMARD09_SCALE_NANO;
+		return IIO_VAL_INT_PLUS_NANO;
 	default:
 		return -EINVAL;
 	}
Signature · 1 line
2.55.0
#1 lore raw
Quoted 1 line · Mert Seftali

in_accel_scale has returned -EINVAL ever since the driver was added.

You mean "Reading the in_accel_scale attribute ends up in returning..." ?

Quoted 6 lines

The channels advertise scale via info_mask_shared_by_type, so the IIO core exposes in_accel_scale, but dmard09_read_raw() only handles IIO_CHAN_INFO_RAW;

a SCALE read falls through to 'default: return
-EINVAL':

This part is repetition of the first paragraph. Drop one of them.

Quoted 9 lines
    $ cat .../iio:deviceX/in_accel_scale
    cat: in_accel_scale: Invalid argument

leaving userspace with raw counts it cannot convert to m/s^2.

The driver was written from a vendor source [1] without a datasheet, and the scale was declared but never implemented. The vendor source carries the sensitivity: its conversion is

Quoted 7 lines
    acc = raw * GRAVITY_EARTH_1000 / sensitivity   (then / 1000 -> m/s^2)

with sensitivity = 32 and GRAVITY_EARTH_1000 = 9807 ("about (9.80665)*1000"), i.e. 32 counts correspond to 1 g.

That sensitivity applies to the value this driver already reports as raw. The vendor processes each 16-bit sample as 'data >>= 3; data &= 0x1ff'

Try to write it more human and less programmish. Something like

"...16-bit sample as a signed 9-bit..." In other words, drop those technical details, people can check the code by the link.

Quoted 2 lines

plus a sign-extend of bit 8 - a signed 9-bit quantity from register bits [11:3] - and dmard09_read_raw()'s 'accel <<= 4; accel >>= 7' yields the

"...and in dmard09_read_raw() preparation yields the..."

Quoted 9 lines

same value. It is self-consistent: 256 counts / 32 = 8 g full scale, matching the part's +/-8g range.

Implement the scale derived from that sensitivity using standard gravity:

    scale = 9.80665 / 32 = 0.3064578125 m/s^2 per LSB

[1] https://github.com/minstrelsy/mediatek/blob/1f49d8c87b839651bc89afc870277e8e0f2e2d55/custom/common/kernel/accelerometer/dmard09/dmard09.c

Make this a Link tag

Link $URL [1]
Quoted 2 lines
Fixes a4fa6509dda4 ("iio: accel: add support for the Domintech DMARD09 3-axis accelerometer")

...

drivers/iio/accel/dmard09.c line 31

Quoted 2 lines
drivers/iio/accel/dmard09.c +2 -0
+/* Sensitivity is 32 LSB/g; scale = 9.80665 / 32 m/s^2 per LSB. */
+#define DMARD09_SCALE_NANO	306457813

I'm not sure how it's derived. Can we use more of calculations here instead of precalculated value?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
In reply to Mert Seftali

drivers/iio/accel/dmard09.c static int dmard09_read_raw(struct iio_dev *indio_dev, · line 87

Quoted 10 lines · On Fri, 3 Jul 2026 15:07:50 +0200 Mert Seftali <[email protected]> wrote:

struct dmard09_data {

drivers/iio/accel/dmard09.c +3 -0
 	struct i2c_client *client;
 };
@@ -79,6 +82,10 @@ static int dmard09_read_raw(struct iio_dev *indio_dev,
 		*val = accel;
 
 		return IIO_VAL_INT;
+	case IIO_CHAN_INFO_SCALE:
+		*val = 0;
+		*val2 = DMARD09_SCALE_NANO;

Add a blank line here - same as the previous case.

Quoted 4 lines
drivers/iio/accel/dmard09.c +1 -0
+		return IIO_VAL_INT_PLUS_NANO;
 	default:
 		return -EINVAL;
 	}
Signature · 3 lines
Kind regards

CJD