iio: magnetometer: add support for Melexis MLX90393
Subsystems (2)
cover iio: magnetometer: add support for Melexis MLX90393
Hi,
This series adds initial Industrial I/O subsystem support for the Melexis MLX90393 3-axis magnetometer and temperature sensor.
The MLX90393 supports both I2C and SPI interfaces. This RFC implements support for the I2C interface while keeping the driver structure transport-independent to simplify future SPI support.
Currently supported features:
raw magnetic field measurements for X/Y/Z axes
raw temperature measurements
configurable gain/scale selection
configurable oversampling ratio
direct mode operation through the IIO subsystem
initially I2C support for this driver
The driver has been tested on Raspberry Pi 5 hardware using an actual MLX90393 sensor connected over I2C. Raw magnetic field and temperature values were verified through the IIO sysfs interface.
A custom transport abstraction was used instead of regmap due to the MLX90393 command/response protocol semantics and repeated-start transfer behavior. This approach was discussed previously on the mailing list and seemed preferable for this device.
The implementation intentionally focuses on a minimal and reviewable feature set for the initial RFC submission. Support for SPI, buffered capture, triggers, interrupts and runtime PM can be added incrementally in follow-up work.
The DT binding schema has been validated using dt_binding_check and the driver has been checked using checkpatch.pl --strict.
Feedback on the overall driver structure, transport abstraction, IIO ABI usage and DT binding would be appreciated.
Thanks,
Nikhil Gautam
Nikhil Gautam (2):
dt-bindings: iio: magnetometer: add Melexis MLX90393
iio: magnetometer: add support for Melexis MLX90393
.../iio/magnetometer/melexis,mlx90393.yaml | 39 + drivers/iio/magnetometer/Kconfig | 10 + drivers/iio/magnetometer/Makefile | 2 + drivers/iio/magnetometer/mlx90393.h | 76 ++ drivers/iio/magnetometer/mlx90393_core.c | 724 ++++++++++++++++++ drivers/iio/magnetometer/mlx90393_i2c.c | 71 ++ 6 files changed, 922 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/magnetometer/melexis,mlx90393.yaml create mode 100644 drivers/iio/magnetometer/mlx90393.h create mode 100644 drivers/iio/magnetometer/mlx90393_core.c create mode 100644 drivers/iio/magnetometer/mlx90393_i2c.c
Signature · 1 line
2.39.5
RFC PATCH 1/2 dt-bindings: iio: magnetometer: add Melexis MLX90393
Add devicetree bindings for the Melexis MLX90393
3-axis magnetometer and temperature sensor.
The device supports magnetic field and temperature measurements over I2C and SPI interfaces.
This initial binding documents the I2C interface.
--- .../iio/magnetometer/melexis,mlx90393.yaml | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/magnetometer/melexis,mlx90393.yaml
Documentation/devicetree/bindings/iio/magnetometer/melexis,mlx90393.yaml new file +39 -0
@@ -0,0 +1,39 @@ +# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/magnetometer/melexis,mlx90393.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Melexis MLX90393 magnetometer sensor + +maintainers: + - Nikhil Gautam <[email protected]> + +description: + Melexis MLX90393 3-axis magnetometer and temperature sensor. + +properties: + compatible: + enum: + - melexis,mlx90393 + + reg: + maxItems: 1 + +required: + - compatible + - reg + +additionalProperties: false + +examples: + - | + i2c { + #address-cells = <1>; + #size-cells = <0>; + + magnetometer@c { + compatible = "melexis,mlx90393"; + reg = <0x0c>; + }; + };
Signature · 1 line
2.39.5
Jonathan Cameron
maintainerRe: [RFC PATCH 1/2] dt-bindings: iio: magnetometer: add Melexis MLX90393
Quoted 9 lines · On Mon, 11 May 2026 00:40:09 +0530 Nikhil Gautam <[email protected]> wrote:
Add devicetree bindings for the Melexis MLX90393
3-axis magnetometer and temperature sensor.The device supports magnetic field and temperature measurements over I2C and SPI interfaces.
This initial binding documents the I2C interface.
Signed-off-by Nikhil Gautam <[email protected]>
Hi Nikhil,
Add a MAINTAINERS entry as part of this patch and expand it in patch 2 to cover the driver. That's much cleaner than doing it after the fact as a separate patch.
Quoted 26 lines
--- .../iio/magnetometer/melexis,mlx90393.yaml | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/magnetometer/melexis,mlx90393.yamlDocumentation/devicetree/bindings/iio/magnetometer/melexis,mlx90393.yaml new file +15
-0@@ -0,0 +1,39 @@ +# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/magnetometer/melexis,mlx90393.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Melexis MLX90393 magnetometer sensor + +maintainers: + - Nikhil Gautam <[email protected]> + +description: + Melexis MLX90393 3-axis magnetometer and temperature sensor. + +properties:
Documentation/devicetree/bindings/iio/magnetometer/melexis,mlx90393.yaml line 25
Quoted 10 lines
Documentation/devicetree/bindings/iio/magnetometer/melexis,mlx90393.yaml new file +10
-0+ compatible: + enum: + - melexis,mlx90393 + + reg: + maxItems: 1 + +required: + - compatible + - reg
Definitely require some power... Looks like vdd-supply and vddio-supply
Also binding in general should be as complete as possible, even if the driver you are sending it with only supports some stuff to start with. So looks like there is an interrupt and a trigger pin that can possibly be used with a GPIO. Seems they may be the same pin from the pinout so maybe don't bother with trig.
Quoted 14 lines
Documentation/devicetree/bindings/iio/magnetometer/melexis,mlx90393.yaml new file +14
-0+ +additionalProperties: false + +examples: + - | + i2c { + #address-cells = <1>; + #size-cells = <0>; + + magnetometer@c { + compatible = "melexis,mlx90393"; + reg = <0x0c>; + }; + };
[email protected]
AI reviewRe: [RFC PATCH 1/2] dt-bindings: iio: magnetometer: add Melexis MLX90393
In reply to Nikhil GautamThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] The devicetree binding documents only the I2C interface for a device that supports both I2C and…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The devicetree binding documents only the I2C interface for a device that supports both I2C and SPI, artificially restricting valid hardware descriptions.
- [Medium] Essential physical hardware connections, such as power supplies and interrupts, are omitted from the schema and actively forbidden by additionalProperties: false. --
commit 8e952d4236837623faf2f1c5e7d285f977fcfe5a
Author: Nikhil Gautam <[email protected]>
dt-bindings: iio: magnetometer: add Melexis MLX90393
This commit adds devicetree bindings for the Melexis MLX90393 3-axis magnetometer and temperature sensor. It currently documents the I2C interface for the device.
Quoted 5 lines
Documentation/devicetree/bindings/iio/magnetometer/melexis,mlx90393.yaml new file +0
-0
[ ... ]
Documentation/devicetree/bindings/iio/magnetometer/melexis,mlx90393.yaml line 21
Quoted 7 lines
Documentation/devicetree/bindings/iio/magnetometer/melexis,mlx90393.yaml new file +7
-0+properties: + compatible: + enum: + - melexis,mlx90393 + + reg: + maxItems: 1
Are essential physical hardware connections, such as power supplies (vdd-supply, vddio-supply) and optional interrupt pins missing here?
Because the schema enforces additionalProperties: false, any downstream board devicetree that accurately models these physical connections to regulators or interrupt controllers will fail schema validation. Could these properties be explicitly listed to permit complete hardware descriptions, even if the current driver does not yet use them?
Documentation/devicetree/bindings/iio/magnetometer/melexis,mlx90393.yaml line 27
Quoted 6 lines
Documentation/devicetree/bindings/iio/magnetometer/melexis,mlx90393.yaml new file +6
-0+ +required: + - compatible + - reg + +additionalProperties: false
Does this artificially restrict valid hardware descriptions by omitting the SPI interface?
The commit message and subsequent driver commit (iio: magnetometer: add support for Melexis MLX90393) explicitly state that the MLX90393 hardware supports both I2C and SPI interfaces. However, the schema enforces
additionalProperties: false without referencing standard SPI properties like spi-max-frequency.
Consequently, any valid board devicetree that connects the device via an SPI bus will fail dtbs_check validation. Should the binding describe the hardware component's full capabilities rather than just the limits of the initial driver implementation?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
RFC PATCH 2/2 iio: magnetometer: add support for Melexis MLX90393
In reply to Nikhil GautamAdd Industrial I/O subsystem support for the Melexis MLX90393 3-axis magnetometer and temperature sensor.
The driver currently supports:
raw magnetic field measurements
raw temperature measurements
configurable gain/scale selection
configurable oversampling ratio
direct mode operation
The MLX90393 supports both I2C and SPI interfaces. This initial implementation adds support for the I2C interface.
The driver is structured around a shared sensor core with a small transport abstraction layer to simplify future SPI support without duplicating sensor logic.
--- drivers/iio/magnetometer/Kconfig | 10 + drivers/iio/magnetometer/Makefile | 2 + drivers/iio/magnetometer/mlx90393.h | 76 +++ drivers/iio/magnetometer/mlx90393_core.c | 724 +++++++++++++++++++++++ drivers/iio/magnetometer/mlx90393_i2c.c | 71 +++ 5 files changed, 883 insertions(+) create mode 100644 drivers/iio/magnetometer/mlx90393.h create mode 100644 drivers/iio/magnetometer/mlx90393_core.c create mode 100644 drivers/iio/magnetometer/mlx90393_i2c.c
drivers/iio/magnetometer/Kconfig +10 -0
@@ -128,6 +128,16 @@ config HID_SENSOR_MAGNETOMETER_3D Say yes here to build support for the HID SENSOR Magnetometer 3D. +config MLX90393 + tristate "MELEXIS MLX90393 3-axis magnetometer sensor" + depends on I2C + help + Say yes here to build support for the MELEXIS MLX90393 3-axis + magnetometer. + + To compile this driver as a module, choose M here: the module + will be called mlx90393. + config MMC35240 tristate "MEMSIC MMC35240 3-axis magnetic sensor" select REGMAP_I2C
drivers/iio/magnetometer/Makefile +2 -0
@@ -14,6 +14,8 @@ obj-$(CONFIG_BMC150_MAGN_SPI) += bmc150_magn_spi.o obj-$(CONFIG_MAG3110) += mag3110.o obj-$(CONFIG_HID_SENSOR_MAGNETOMETER_3D) += hid-sensor-magn-3d.o +obj-$(CONFIG_MLX90393) += mlx90393_core.o +obj-$(CONFIG_MLX90393) += mlx90393_i2c.o obj-$(CONFIG_MMC35240) += mmc35240.o obj-$(CONFIG_IIO_ST_MAGN_3AXIS) += st_magn.o
drivers/iio/magnetometer/mlx90393.h new file +76 -0
76 lines not on this page · raw message
drivers/iio/magnetometer/mlx90393_core.c new file +724 -0
724 lines not on this page · raw message
drivers/iio/magnetometer/mlx90393_i2c.c new file +71 -0
71 lines not on this page · raw message
Signature · 1 line
2.39.5
Jonathan Cameron
maintainerQuoted 11 lines · On Mon, 11 May 2026 00:40:10 +0530 Nikhil Gautam <[email protected]> wrote:
Add Industrial I/O subsystem support for the Melexis MLX90393 3-axis magnetometer and temperature sensor.
The driver currently supports:
raw magnetic field measurements
raw temperature measurements
configurable gain/scale selection
configurable oversampling ratio
direct mode operation
Quoted 8 lines
The MLX90393 supports both I2C and SPI interfaces. This initial implementation adds support for the I2C interface.
The driver is structured around a shared sensor core with a small transport abstraction layer to simplify future SPI support without duplicating sensor logic.
Signed-off-by Nikhil Gautam <[email protected]>
Hi Nikhil
Quick process reminder - even though I've reviewed this the day you sent it out (coincidence of when I am catching up with backlog!), wait to send a v2 for at least a week so there is plenty of time for others to review.
Various comments inline,
Thanks,
Jonathan
Quoted 9 lines
drivers/iio/magnetometer/mlx90393.h new file +3
-0@@ -0,0 +1,76 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* MLX90393 magnetometer & temperature sensor driver
drivers/iio/magnetometer/mlx90393.h line 13
Quoted 10 lines
drivers/iio/magnetometer/mlx90393.h new file +10
-0+* +* Copyright (c) 2026 Nikhil Gautam <[email protected]> +*/ + +#ifndef MLX90393_H +#define MLX90393_H + +#include <linux/bitops.h> +#include <linux/bits.h> +#include <linux/device.h>
As below - generally don't want to see device.h included in a new driver.
Quoted 11 lines
drivers/iio/magnetometer/mlx90393.h new file +11
-0+#include <linux/types.h> + +#define MLX90393_AXIS_MAX 2 +#define MLX90393_GAIN_MAX 8 +#define MLX90393_RES_MAX 4 +#define MLX90393_OSR2_MAX 4 +#define MLX90393_OSR_MAX 4 + +#define MLX90393_CMD_MASK GENMASK(7, 4) + +/* Commands */
drivers/iio/magnetometer/mlx90393.h line 34
Quoted 10 lines
drivers/iio/magnetometer/mlx90393.h new file +10
-0+#define MLX90393_CMD_SB 0x10 +#define MLX90393_CMD_SW 0x20 +#define MLX90393_CMD_SM 0x30 +#define MLX90393_CMD_RM 0x40 +#define MLX90393_CMD_RR 0x50 +#define MLX90393_CMD_WR 0x60 +#define MLX90393_CMD_EX 0x80 +#define MLX90393_CMD_HR 0xD0 +#define MLX90393_CMD_HS 0xE0 +#define MLX90393_CMD_RT 0xF0
Maybe use a few more characters as those are pretty opaque acronyms! Fair enough if they are straight from the datasheet but in that case can we have some comments.
Quoted 6 lines
drivers/iio/magnetometer/mlx90393_core.c new file +0
-0
drivers/iio/magnetometer/mlx90393_core.c line 9
Quoted 9 lines
drivers/iio/magnetometer/mlx90393_core.c new file +9
-0@@ -0,0 +1,724 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* MLX90393 magnetometer & temperature sensor driver +* +* Copyright (c) 2026 Nikhil Gautam <[email protected]> +*/ + +#include <linux/delay.h> +#include <linux/device.h>
Generally not needed - try and find more specific relevant includes.
Check to see if you need more - this is a very short set for a driver and we generally try to follow include what you use principles in IIO.
drivers/iio/magnetometer/mlx90393_core.c line 21
Quoted 12 lines
drivers/iio/magnetometer/mlx90393_core.c new file +12
-0+#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/unaligned.h> +#include <linux/units.h> + +#include <linux/iio/iio.h> + +#include "mlx90393.h" + +struct mlx90393_data { +struct device *dev; +struct mutex lock;
Locks should always have a comment saying what data they are protecting.
Quoted 11 lines
drivers/iio/magnetometer/mlx90393_core.c new file +11
-0+void *bus_context; +const struct mlx90393_transfer_ops *ops; +u8 gain_sel; +u8 hallconf; + +u8 res_xy; +u8 res_z; + +u8 dig_filt; +u8 osr; +u8 osr2;
drivers/iio/magnetometer/mlx90393_core.c line 53
Quoted 10 lines
drivers/iio/magnetometer/mlx90393_core.c new file +9
-0+}; + +/* Datasheet: Table no.17 */ +static const int mlx90393_scale_table[MLX90393_AXIS_MAX ] + [MLX90393_GAIN_MAX ] + [MLX90393_RES_MAX ] = { +/* XY axis */ + { + {751 ,1502 ,3004 ,6009 },
{ 751, 1502, 3004, 6009 },etc
As noted below I'd prefer this reformatted so you don't have to copy it into that local static array. That way we can greatly simplify avoiding some race conditions.
Quoted 56 lines
drivers/iio/magnetometer/mlx90393_core.c new file +55
-0+ {601, 1202, 2403, 4840}, + {451, 901, 1803, 3605}, + {376, 751, 1502, 3004}, + {300, 601, 1202, 2403}, + {250, 501, 1001, 2003}, + {200, 401, 801, 1602}, + {150, 300, 601, 1202}, + }, +/* Z axis */ + { + {1210, 2420, 4840, 9680}, + {968, 1936, 3872, 7744}, + {726, 1452, 2904, 5808}, + {605, 1210, 2420, 4840}, + {484, 968, 1936, 3872}, + {403, 807, 1613, 3227}, + {323, 645, 1291, 2581}, + {242, 484, 968, 1936}, + } +}; +/* +* Calculate total conversion time in microseconds. +* +* Formula derived from datasheet timing equations. +*/ + +static int mlx90393_get_tconv_us (struct mlx90393_data *data) +{ +const int osr = data->osr; +const int osr2 = data->osr2; +const int df = data->dig_filt; + +int tconvm; +int tconvt; + +int m =3 ;/* X,Y,Z */ + +/* +* Datasheet: +* +* TCONVM = +* 67 + 64 * 2^OSR * (2 + 2^DIG_FILT) +*/ + tconvm =67 + + (64 *BIT (osr) * + (2 +BIT (df))); + +/* +* Datasheet: +* +* TCONVT = +* 67 + 192 * 2^OSR2 +*/ + tconvt =67 + + (192 *BIT (osr2));
drivers/iio/magnetometer/mlx90393_core.c line 158
Quoted 10 lines
drivers/iio/magnetometer/mlx90393_core.c new file +10
-0+/* +* Total conversion time: +* TSTBY + TACTIVE + m * TCONVM + +* TCONVT + TCONV_END +*/ +return 220 + +360 + + (m * tconvm) + + tconvt + +100 ;
If you are going to comment on the maths - match that layout in the code rather than having so many line breaks.
Quoted 8 lines
drivers/iio/magnetometer/mlx90393_core.c new file +7
-0+} + +static int mlx90393_check_status (u8 cmd,u8 status) +{ +/* Always validate error bit */ +if (status &MLX90393_STATUS_ERROR ) +return -EIO ;
drivers/iio/magnetometer/mlx90393_core.c line 184
Quoted 10 lines
drivers/iio/magnetometer/mlx90393_core.c new file +10
-0+ +switch (cmd &MLX90393_CMD_MASK ) { +case MLX90393_CMD_RM : +/* +* D1:D0 indicates response availability +* 00 means invalid/no measurement +*/ +if ((status &MLX90393_STATUS_RESP ) ==0 ) +return -EIO ; +break ;
return 0;
similar reasoning to comment below.
Quoted 20 lines
drivers/iio/magnetometer/mlx90393_core.c new file +19
-0+ +case MLX90393_CMD_RT : +/* Reset acknowledge */ +if (!(status &MLX90393_STATUS_RT )) +return -EIO ; +break ; + +default : +break ; + } + +return 0 ; +} + +static int mlx90393_write_reg (struct mlx90393_data *data, +u8 reg,u16 val) +{ +u8 tx[4 ]; +u8 status;
drivers/iio/magnetometer/mlx90393_core.c line 267
Quoted 10 lines
drivers/iio/magnetometer/mlx90393_core.c new file +10
-0+int ret; + + tx[0 ] =MLX90393_CMD_WR ; +put_unaligned_be16 (val, &tx[1 ]); +/* Register address is encoded in bits [7:2] */ + tx[3 ] = reg <<2 ; + + ret =mlx90393_xfer (data, + tx,sizeof (tx), + &status,1 );
Wrap much less!
Quoted 14 lines
drivers/iio/magnetometer/mlx90393_core.c new file +13
-0+if (ret) +return ret; + +return mlx90393_check_status (tx[0 ], status); +} + +static int mlx90393_read_measurement (struct mlx90393_data *data, +enum mlx90393_channels chan,int *val) +{ +u8 cmd; +u8 rx[9 ]; +int ret; +int tconv_us =mlx90393_get_tconv_us (data);
drivers/iio/magnetometer/mlx90393_core.c line 307
Quoted 10 lines
drivers/iio/magnetometer/mlx90393_core.c new file +10
-0+ +/* Start measurement */ + cmd =MLX90393_CMD_SM |MLX90393_MEASURE_ALL ; + + ret =mlx90393_write_cmd (data, cmd); +if (ret) +return ret; + +/* Wait conversion */ +usleep_range (tconv_us, tconv_us +1000 );
fsleep() for all 'fuzzy' ranges like this.
drivers/iio/magnetometer/mlx90393_core.c line 310
Quoted 3 lines
drivers/iio/magnetometer/mlx90393_core.c new file +3
-0+ +/* Read measurement */ + cmd =MLX90393_CMD_RM |MLX90393_MEASURE_ALL ;
Even here I'd put that value inline in the call rather than having a local variable for it.
Quoted 21 lines
drivers/iio/magnetometer/mlx90393_core.c new file +21
-0+ + ret =mlx90393_read_cmd (data, cmd, rx,sizeof (rx)); +if (ret) +return ret; +/* +* Measurement response layout: +* [status][temp][x][y][z] +*/ + +switch (chan) { +case MLX90393_CHAN_TEMP : + *val =get_unaligned_be16 (&rx[1 ]); +break ; + +case MLX90393_CHAN_X : + *val =sign_extend32 (get_unaligned_be16 (&rx[3 ]),15 ); +break ; + +case MLX90393_CHAN_Y : + *val =sign_extend32 (get_unaligned_be16 (&rx[5 ]),15 ); +break ;
drivers/iio/magnetometer/mlx90393_core.c line 341
Quoted 10 lines
drivers/iio/magnetometer/mlx90393_core.c new file +10
-0+ +case MLX90393_CHAN_Z : + *val =sign_extend32 (get_unaligned_be16 (&rx[7 ]),15 ); +break ; + +default : +return -EINVAL ; + } + +return 0 ;
Maybe just return early instead of break. Saves the reader scrolling down to see what else is to be done.
drivers/iio/magnetometer/mlx90393_core.c line 353
Quoted 12 lines
drivers/iio/magnetometer/mlx90393_core.c new file +12
-0+} + +static int mlx90393_get_scale (struct mlx90393_data *data, +const struct iio_chan_spec *chan, +int *val,int *val2) +{ +enum mlx90393_axis_type axis; +u8 res; + +if (chan->channel2 ==IIO_MOD_Z ) { + axis =MLX90393_AXIS_TYPE_Z ; + res = data->res_z;
Be consistent - here you use an if / else, below to ternary operators. I don't much mind which (slightly preference for this) but mixing this sort of thing up just makes for harder code to review.
Quoted 16 lines
drivers/iio/magnetometer/mlx90393_core.c new file +16
-0+ }else { + axis =MLX90393_AXIS_TYPE_XY ; + res = data->res_xy; + } + +/* +* Convert: +* µT × 1000 → nT +*/ + *val =0 ; + *val2 = mlx90393_scale_table[axis][data->gain_sel][res]; + +return IIO_VAL_INT_PLUS_NANO ; +} + +static int mlx90393_find_scale(struct mlx90393_data *data,
drivers/iio/magnetometer/mlx90393_core.c line 379
Quoted 10 lines
drivers/iio/magnetometer/mlx90393_core.c new file +10
-0+bool z_axis, + int val, int val2, + int *gain) +{ + int i; +u8 res; +enum mlx90393_axis_type axis; + + axis = z_axis ?MLX90393_AXIS_TYPE_Z : +MLX90393_AXIS_TYPE_XY ;
Go long if it helps readability and here I think it does + it's under 80 chars anyway!
axis = z_axis ? MLX90393_AXIS_TYPE_Z : MLX90393_AXIS_TYPE_XY;
drivers/iio/magnetometer/mlx90393_core.c line 388
Quoted 9 lines
drivers/iio/magnetometer/mlx90393_core.c new file +9
-0+ + res = z_axis ? data->res_z : data->res_xy; + +if (val !=0 ) +return -EINVAL ; + +for (i =0 ; i <ARRAY_SIZE (mlx90393_scale_table[0 ]); i++) { +if (mlx90393_scale_table[axis][i][res] + == val2) {
Here readability is hurt and it fits on one line under 80 chars anyway!
if (mlx90393_scale_table[axis][i][res] == val2) {Quoted 18 lines
drivers/iio/magnetometer/mlx90393_core.c new file +18
-0+ *gain = i; +return 0 ; + } + } + +return -EINVAL ; +} + +static int mlx90393_set_scale (struct mlx90393_data *data, +const struct iio_chan_spec *chan, +int val,int val2) +{ +bool z_axis; +int gain; +int ret; + + z_axis = chan->channel2 ==IIO_MOD_Z ; +
drivers/iio/magnetometer/mlx90393_core.c line 416
Quoted 10 lines
drivers/iio/magnetometer/mlx90393_core.c new file +10
-0+ ret =mlx90393_find_scale (data, z_axis, + val, val2, + &gain); +if (ret) +return ret; + + ret =mlx90393_update_bits (data, +MLX90393_REG_CONF1 , +MLX90393_CONF1_GAIN_SEL , + gain);
I'd wrap these less
ret = mlx90393_update_bits(data, MLX90393_REG_CONF1,
drivers/iio/magnetometer/mlx90393_core.c
Quoted 1 line
drivers/iio/magnetometer/mlx90393_core.c new file +1
-0+ MLX90393_CONF1_GAIN_SEL, gain);
For example. Same for other similar cases.
drivers/iio/magnetometer/mlx90393_core.c line 436
Quoted 13 lines
drivers/iio/magnetometer/mlx90393_core.c new file +12
-0+if (ret) +return ret; + + data->gain_sel = gain; + +return 0 ; +} +static int mlx90393_find_osr (int val,int *osr) +{ +int i; + +for (i =0 ; i <MLX90393_OSR_MAX ; i++) {
For modern kernel code it's acceptable to do
for (unsigned int i = 0; i < MLX....)
and avoid needing to define the local variable separately above.
Quoted 24 lines
drivers/iio/magnetometer/mlx90393_core.c new file +23
-0+if (mlx90393_osr_avail[i] == val) { + *osr = i; +return 0 ; + } + } + +return -EINVAL ; +} +static int mlx90393_write_raw_get_fmt (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan, +long mask) +{ +switch (mask) { +case IIO_CHAN_INFO_SCALE : +return IIO_VAL_INT_PLUS_NANO ; +case IIO_CHAN_INFO_OVERSAMPLING_RATIO : +return IIO_VAL_INT ; +default : +return -EINVAL ; + } +} + +static int mlx90393_write_raw(struct iio_dev *indio_dev,
drivers/iio/magnetometer/mlx90393_core.c line 519
Quoted 10 lines
drivers/iio/magnetometer/mlx90393_core.c new file +10
-0+const struct iio_chan_spec *chan, + int val, int val2, + long mask) +{ + struct mlx90393_data *data = iio_priv(indio_dev); +int ret; + +switch (mask) { +case IIO_CHAN_INFO_SCALE : +mutex_lock (&data->lock);
Less useful but I'd use a guard() here as well just for consistency.
drivers/iio/magnetometer/mlx90393_core.c line 524
Quoted 5 lines
drivers/iio/magnetometer/mlx90393_core.c new file +5
-0+ ret =mlx90393_set_scale (data, chan, val, val2); +mutex_unlock (&data->lock); +return ret; + +case IIO_CHAN_INFO_OVERSAMPLING_RATIO :
Similar use of guard() + {} to described below. Then you can return directly for each leg.
Quoted 13 lines
drivers/iio/magnetometer/mlx90393_core.c new file +13
-0+mutex_lock (&data->lock); +if (chan->type ==IIO_TEMP ) + ret =mlx90393_set_temp_osr2 (data, val); +else if (chan->type ==IIO_MAGN ) + ret =mlx90393_set_osr (data, val); +else + ret = -EINVAL ; +mutex_unlock (&data->lock); +return ret; +default : +return -EINVAL ; + } +}
drivers/iio/magnetometer/mlx90393_core.c line 547
Quoted 10 lines
drivers/iio/magnetometer/mlx90393_core.c new file +10
-0+ +static int mlx90393_read_raw (struct iio_dev *indio_dev, +const struct iio_chan_spec *chan, +int *val,int *val2,long mask) +{ +struct mlx90393_data *data =iio_priv (indio_dev); +int ret; + +switch (mask) { +case IIO_CHAN_INFO_RAW :
This is fine but maybe slightly nicer as:
case IIO_CHAN_INFO_RAW: {
guard(mutex)(&data->lock);
ret = mlx90393_read_measurement(data, chan->addrss, val)
if (ret)
return ret;
return IIO_VAL_INT;
}Quoted 20 lines
drivers/iio/magnetometer/mlx90393_core.c new file +20
-0+mutex_lock (&data->lock); + ret =mlx90393_read_measurement (data, chan->address, val); +mutex_unlock (&data->lock); +if (ret) +return ret; + +return IIO_VAL_INT ; + +case IIO_CHAN_INFO_SCALE : +switch (chan->type) { +case IIO_MAGN : +return mlx90393_get_scale (data, chan, val, val2); + +case IIO_TEMP : +/* Datasheet: 22124 millidegC/LSB */ + *val =0 ; + *val2 =22124 ; +return IIO_VAL_INT_PLUS_MICRO ; + +default :
drivers/iio/magnetometer/mlx90393_core.c line 577
Quoted 10 lines
drivers/iio/magnetometer/mlx90393_core.c new file +10
-0+return -EINVAL ; + } + +case IIO_CHAN_INFO_OFFSET : +if (chan->type ==IIO_TEMP ) { +/* Datasheet: temperature offset */ + *val =-45114 ; +return IIO_VAL_INT ; + } +return -EINVAL ;
I'd flip the logic .
if (chan->type != IIO_TEMP) return -EINVAL;
drivers/iio/magnetometer/mlx90393_core.c line 526
Quoted 3 lines
drivers/iio/magnetometer/mlx90393_core.c new file +3
-0+ +case IIO_CHAN_INFO_OVERSAMPLING_RATIO : +if (chan->type ==IIO_TEMP )
Maybe a switch statement would be nicer and consistent at leasts with scale above.
Quoted 22 lines
drivers/iio/magnetometer/mlx90393_core.c new file +22
-0+return mlx90393_get_temp_osr2 (data, val); +if (chan->type ==IIO_MAGN ) +return mlx90393_get_osr (data, val); +return -EINVAL ; +default : +return -EINVAL ; + } +} + +static int mlx90393_read_avail (struct iio_dev *indio_dev, +const struct iio_chan_spec *chan, +const int **vals, +int *type, +int *length, +long mask) +{ +struct mlx90393_data *data =iio_priv (indio_dev); + +static int scale_avail[MLX90393_GAIN_MAX ][MLX90393_AXIS_MAX ]; +enum mlx90393_axis_type axis; +int i; +u8 res;
drivers/iio/magnetometer/mlx90393_core.c line 612
Quoted 10 lines
drivers/iio/magnetometer/mlx90393_core.c new file +10
-0+ +switch (mask) { +case IIO_CHAN_INFO_SCALE : + axis = chan->channel2 ==IIO_MOD_Z ; + res = axis ? data->res_z : data->res_xy; + +for (i =0 ; i <MLX90393_GAIN_MAX ; i++) { + scale_avail[i][0 ] =0 ; + scale_avail[i][1 ] = + mlx90393_scale_table[axis][i][res];
If you support changing rest this can tear mid table write and you get half of one table and half of the other. Use a lock.
However, can you just make that mlx90393_scale_table have a datalayout that lets you pick between parts of that rather than having to copy it in here?
Quoted 23 lines
drivers/iio/magnetometer/mlx90393_core.c new file +23
-0+ } + + *vals = &scale_avail[0 ][0 ]; + *type =IIO_VAL_INT_PLUS_NANO ; + *length =MLX90393_GAIN_MAX *MLX90393_AXIS_MAX ; +break ; +case IIO_CHAN_INFO_OVERSAMPLING_RATIO : +if (chan->type ==IIO_TEMP ) { + *vals = mlx90393_osr2_avail; + *type =IIO_VAL_INT ; + *length =MLX90393_OSR2_MAX ; + }else { + *vals = mlx90393_osr_avail; + *type =IIO_VAL_INT ; + *length =MLX90393_OSR_MAX ; + } + +break ; +default : +return -EINVAL ; + } +return IIO_AVAIL_LIST ; +}
drivers/iio/magnetometer/mlx90393_core.c line 651
Quoted 10 lines
drivers/iio/magnetometer/mlx90393_core.c new file +9
-0+ +static int mlx90393_init (struct mlx90393_data *data) +{ +int ret; +u8 cmd; +u16 reg; + +/* Exit mode */ + cmd =MLX90393_CMD_EX ;
Seems little benefit in the local variable use over
ret = mlx90393_write_cmd(data, MLX90393_CMD_EX);
drivers/iio/magnetometer/mlx90393_core.c line 656
Quoted 5 lines
drivers/iio/magnetometer/mlx90393_core.c new file +5
-0+ ret =mlx90393_write_cmd (data, cmd); +if (ret) +return ret; + +usleep_range (1000 ,1500 );
fsleep() + a comment on why this particular sleep time.
drivers/iio/magnetometer/mlx90393_core.c line 660
Quoted 4 lines
drivers/iio/magnetometer/mlx90393_core.c new file +4
-0+ +/* Reset device */ + cmd =MLX90393_CMD_RT ; + ret =mlx90393_write_cmd (data, cmd);
Similar to above.
drivers/iio/magnetometer/mlx90393_core.c line 665
Quoted 5 lines
drivers/iio/magnetometer/mlx90393_core.c new file +5
-0+if (ret) +return ret; + +/* Wait for device to reset */ +usleep_range (5000 ,6000 );
and similar to above.
Quoted 25 lines
drivers/iio/magnetometer/mlx90393_core.c new file +25
-0+ + ret =mlx90393_read_reg (data,MLX90393_REG_CONF1 , ®); +if (ret) +return ret; + + data->gain_sel =FIELD_GET (MLX90393_CONF1_GAIN_SEL , reg); + data->hallconf =FIELD_GET (MLX90393_CONF1_HALLCONF , reg); + + ret =mlx90393_read_reg (data,MLX90393_REG_CONF3 , ®); +if (ret) +return ret; + + data->res_xy =FIELD_GET (MLX90393_CONF3_RES_X , reg); + data->res_z =FIELD_GET (MLX90393_CONF3_RES_Z , reg); + data->dig_filt =FIELD_GET (MLX90393_CONF3_DIG_FILT , reg); + data->osr =FIELD_GET (MLX90393_CONF3_OSR , reg); + data->osr2 =FIELD_GET (MLX90393_CONF3_OSR2 , reg); + +return 0 ; +} + +int mlx90393_core_probe (struct device *dev, +const struct mlx90393_transfer_ops *ops, +void *context) +{
drivers/iio/magnetometer/mlx90393_core.c line 700
Quoted 10 lines
drivers/iio/magnetometer/mlx90393_core.c new file +10
-0+struct iio_dev *indio_dev; +struct mlx90393_data *data; +int ret; + + indio_dev =devm_iio_device_alloc (dev,sizeof (*data)); +if (!indio_dev) +return -ENOMEM ; + + data =iio_priv (indio_dev); +mutex_init (&data->lock);
ret = devm_mutex_init(&data->lock); if (ret) return ret;
Small advantage for lock debugging but now it's near free with that helper it's nicer to do the version that actually marks the lock as disabled on exit.
Quoted 5 lines
drivers/iio/magnetometer/mlx90393_core.c new file +5
-0+ + data->dev = dev; + data->ops = ops; + data->bus_context = context; +
drivers/iio/magnetometer/mlx90393_core.c line 715
Quoted 10 lines
drivers/iio/magnetometer/mlx90393_core.c new file +10
-0+ indio_dev->name ="mlx90393" ; + indio_dev->info = &mlx90393_info; + indio_dev->modes =INDIO_DIRECT_MODE ; + indio_dev->channels = mlx90393_channels; + indio_dev->num_channels =ARRAY_SIZE (mlx90393_channels); + + ret =mlx90393_init (data); +if (ret) { +dev_err_probe (dev, ret,"failed to initialize device\n" ); +return ret;
return dev_err_probe();
Quoted 11 lines
drivers/iio/magnetometer/mlx90393_core.c new file +4
-0+ } + +return devm_iio_device_register (dev, indio_dev); +}drivers/iio/magnetometer/mlx90393_i2c.c new file +0
-0
drivers/iio/magnetometer/mlx90393_i2c.c line 3
Quoted 3 lines
drivers/iio/magnetometer/mlx90393_i2c.c new file +3
-0@@ -0,0 +1,71 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <linux/module.h> +#include <linux/i2c.h>
mod_devicetable.h for the id table structure definitions.
Quoted 14 lines
drivers/iio/magnetometer/mlx90393_i2c.c new file +14
-0+ +#include "mlx90393.h" + +/* +* MLX90393 commands use repeated-start transfers where +* every command is followed by a status/data response. +*/ +static int mlx90393_i2c_xfer (void *context, +const u8 *tx,int tx_len, +u8 *rx,int rx_len) +{ +struct i2c_client *client = context; +int ret; +struct i2c_msg msgs[2 ];
drivers/iio/magnetometer/mlx90393_i2c.c line 27
Quoted 10 lines
drivers/iio/magnetometer/mlx90393_i2c.c new file +10
-0+ + msgs[0 ].addr = client->addr; + msgs[0 ].flags =0 ; + msgs[0 ].len = tx_len; + msgs[0 ].buf = (u8 *)tx; + + msgs[1 ].addr = client->addr; + msgs[1 ].flags =I2C_M_RD ; + msgs[1 ].len = rx_len; + msgs[1 ].buf = rx;
struct i2c_msg msgs[2] = {
[0] = {
.addr = client->addr,
.len = tx_len,
.buf = (u8 *)tx,
},
[1] = {
.addr = client->addr,
.flags = I2C_M_RD,
.len = rx_len,
.buf = rx,
},
};drivers/iio/magnetometer/mlx90393_i2c.c line 29
Quoted 2 lines
drivers/iio/magnetometer/mlx90393_i2c.c new file +2
-0+ + ret =i2c_transfer (client->adapter, msgs,2 );
sizeof(msgs)
drivers/iio/magnetometer/mlx90393_i2c.c line 30
Quoted 1 line
drivers/iio/magnetometer/mlx90393_i2c.c new file +1
-0+if (ret !=2 )
sizeof(msgs)
drivers/iio/magnetometer/mlx90393_i2c.c line 44
Quoted 10 lines
drivers/iio/magnetometer/mlx90393_i2c.c new file +9
-0+return ret <0 ? ret : -EIO ; + +return 0 ; +} +static int mlx90393_i2c_probe (struct i2c_client *client) +{ +return mlx90393_core_probe (&client->dev, + &mlx90393_i2c_ops, + client);
return mlx90393_core_probe(&client->dev, &mlx90393_i2c_ops, client);
Definitely wrap less than you have and I'd probably just go a bit long on this one and have it all on one line.
drivers/iio/magnetometer/mlx90393_i2c.c line 48
Quoted 4 lines
drivers/iio/magnetometer/mlx90393_i2c.c new file +4
-0+} + +static const struct i2c_device_id mlx90393_id[] = { + {"mlx90393" ,0 },
{ "mlx90393" }The , 0 doesn't add anything as if we do ever need to add data it will be a pointer and not 0 anyway.
Quoted 3 lines
drivers/iio/magnetometer/mlx90393_i2c.c new file +3
-0+ { } +}; +MODULE_DEVICE_TABLE (i2c, mlx90393_id);
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider: - [High] Data race on static `scale_avail` array in `mlx90393_read_avail` - [High] `mlx90393_check_status`…
Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider:
- [High] Data race on static scale_avail array in mlx90393_read_avail
- [High] mlx90393_check_status unconditionally rejects valid full-sensor measurements
- [High] Stack-allocated buffers are passed to I2C transfer, violating DMA safety rules
- [High] Unprotected state reads in mlx90393_read_raw lead to data races
- [Medium] IIO ABI violation: Magnetometer and temperature scales use incorrect units
- [Medium] mlx90393_write_raw improperly allows modifying the magnetometer gain via the temperature scale attribute
- [Medium] Magnetometer scale is exported as separate attributes despite utilizing a global hardware register
- [Low] Temperature channel exposes internal hardware register values for oversampling ratios
- [Low] Incorrect use of usleep_range for delays exceeding 200ms
--
commit b098ee066ea285eea196aa30f9837e81df63b980
Author: Nikhil Gautam <[email protected]>
iio: magnetometer: add support for Melexis MLX90393
This commit adds Industrial I/O subsystem support for the Melexis MLX90393 3-axis magnetometer and temperature sensor.
Quoted 4 lines
drivers/iio/magnetometer/mlx90393_core.c new file +0
-0
[ ... ]
drivers/iio/magnetometer/mlx90393_core.c line 77
Quoted 3 lines
drivers/iio/magnetometer/mlx90393_core.c new file +3
-0+static const int mlx90393_osr2_avail[MLX90393_OSR2_MAX ] = { +0 ,1 ,2 ,3 , +};
Is it intentional to expose internal hardware register index limits directly to userspace instead of the actual physical oversampling ratios?
[ ... ]
drivers/iio/magnetometer/mlx90393_core.c line 90
Quoted 8 lines
drivers/iio/magnetometer/mlx90393_core.c new file +8
-0+#define MLX90393_CHAN (idx, axis, addr) { \ + .type = IIO_MAGN, \ + .modified = 1, \ + .channel = idx, \ + .address = addr, \ + .channel2 = IIO_MOD_##axis, \ + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ + BIT(IIO_CHAN_INFO_SCALE), \
Does using info_mask_separate for IIO_CHAN_INFO_SCALE correctly represent the hardware capabilities? Writing to the scale of one axis will alter the global GAIN_SEL hardware register in mlx90393_set_scale(), altering the scale for all axes.
[ ... ]
Quoted 4 lines
drivers/iio/magnetometer/mlx90393_core.c new file +4
-0+static int mlx90393_check_status (u8 cmd,u8 status) +{ +/* Always validate error bit */ +if (status &MLX90393_STATUS_ERROR )
drivers/iio/magnetometer/mlx90393_core.c line 183
Quoted 10 lines
drivers/iio/magnetometer/mlx90393_core.c new file +10
-0+return -EIO ; + +switch (cmd &MLX90393_CMD_MASK ) { +case MLX90393_CMD_RM : +/* +* D1:D0 indicates response availability +* 00 means invalid/no measurement +*/ +if ((status &MLX90393_STATUS_RESP ) ==0 ) +return -EIO ;
If MLX90393_MEASURE_ALL requests all 4 channels (T, X, Y, Z), resulting in 4 words, does the 2-bit D[1:0] field truncate this to 00? This check might unconditionally reject valid full-sensor measurements and return -EIO.
[ ... ]
Quoted 4 lines
drivers/iio/magnetometer/mlx90393_core.c new file +4
-0+static int mlx90393_read_reg (struct mlx90393_data *data, +u8 reg,u16 *val) +{ +u8 tx[2 ];
drivers/iio/magnetometer/mlx90393_core.c line 240
Quoted 10 lines
drivers/iio/magnetometer/mlx90393_core.c new file +10
-0+u8 rx[3 ]; +int ret; + + tx[0 ] =MLX90393_CMD_RR ; +/* Register address is encoded in bits [7:2] */ + tx[1 ] = reg <<2 ; + + ret =mlx90393_xfer (data, + tx,sizeof (tx), + rx,sizeof (rx));
Are these stack-allocated buffers DMA-safe?
When passed to ops->xfer which calls i2c_transfer(), this can lead to memory corruption or panics if the host controller maps buffers for DMA (especially with CONFIG_VMAP_STACK=y).
[ ... ]
drivers/iio/magnetometer/mlx90393_core.c line 295
Quoted 5 lines
drivers/iio/magnetometer/mlx90393_core.c new file +5
-0+static int mlx90393_read_measurement (struct mlx90393_data *data, +enum mlx90393_channels chan,int *val) +{ +u8 cmd; +u8 rx[9 ];
Can this stack allocated buffer also cause DMA safety issues?
drivers/iio/magnetometer/mlx90393_core.c line 307
Quoted 12 lines
drivers/iio/magnetometer/mlx90393_core.c new file +12
-0+int ret; +int tconv_us =mlx90393_get_tconv_us (data); + +/* Start measurement */ + cmd =MLX90393_CMD_SM |MLX90393_MEASURE_ALL ; + + ret =mlx90393_write_cmd (data, cmd); +if (ret) +return ret; + +/* Wait conversion */ +usleep_range (tconv_us, tconv_us +1000 );
Depending on the configured oversampling ratio and digital filters, tconv_us can exceed 200,000 microseconds. Is usleep_range() appropriate for delays exceeding 20ms, or should msleep() be used here instead to avoid unnecessarily taxing the hrtimer subsystem?
[ ... ]
Quoted 13 lines
drivers/iio/magnetometer/mlx90393_core.c new file +13
-0+static int mlx90393_get_scale (struct mlx90393_data *data, +const struct iio_chan_spec *chan, +int *val,int *val2) +{ +enum mlx90393_axis_type axis; +u8 res; + +if (chan->channel2 ==IIO_MOD_Z ) { + axis =MLX90393_AXIS_TYPE_Z ; + res = data->res_z; + }else { + axis =MLX90393_AXIS_TYPE_XY ; + res = data->res_xy;
drivers/iio/magnetometer/mlx90393_core.c line 366
Quoted 10 lines
drivers/iio/magnetometer/mlx90393_core.c new file +10
-0+ } + +/* +* Convert: +* µT × 1000 → nT +*/ + *val =0 ; + *val2 = mlx90393_scale_table[axis][data->gain_sel][res]; + +return IIO_VAL_INT_PLUS_NANO ;
Does this scale comply with the IIO ABI requirements? Returning IIO_VAL_INT_PLUS_NANO with these values produces Tesla rather than the Gauss units required for magnetometers.
[ ... ]
Quoted 4 lines
drivers/iio/magnetometer/mlx90393_core.c new file +4
-0+static int mlx90393_write_raw (struct iio_dev *indio_dev, +const struct iio_chan_spec *chan, +int val,int val2, +long mask)
drivers/iio/magnetometer/mlx90393_core.c line 522
Quoted 10 lines
drivers/iio/magnetometer/mlx90393_core.c new file +10
-0+{ +struct mlx90393_data *data =iio_priv (indio_dev); +int ret; + +switch (mask) { +case IIO_CHAN_INFO_SCALE : +mutex_lock (&data->lock); + ret =mlx90393_set_scale (data, chan, val, val2); +mutex_unlock (&data->lock); +return ret;
Does this safely handle writes to the temperature scale?
The temperature channel exposes IIO_CHAN_INFO_SCALE, but mlx90393_set_scale() modifies the GAIN_SEL hardware register, which might inadvertently corrupt the magnetometer's sensitivity when a user writes to in_temp_scale.
[ ... ]
Quoted 11 lines
drivers/iio/magnetometer/mlx90393_core.c new file +11
-0+static int mlx90393_read_raw (struct iio_dev *indio_dev, +const struct iio_chan_spec *chan, +int *val,int *val2,long mask) +{ +struct mlx90393_data *data =iio_priv (indio_dev); +int ret; + +switch (mask) { +case IIO_CHAN_INFO_RAW : +mutex_lock (&data->lock); + ret =mlx90393_read_measurement (data, chan->address, val);
drivers/iio/magnetometer/mlx90393_core.c line 559
Quoted 10 lines
drivers/iio/magnetometer/mlx90393_core.c new file +10
-0+mutex_unlock (&data->lock); +if (ret) +return ret; + +return IIO_VAL_INT ; + +case IIO_CHAN_INFO_SCALE : +switch (chan->type) { +case IIO_MAGN : +return mlx90393_get_scale (data, chan, val, val2);
Can this read race with concurrent sysfs writes?
mlx90393_get_scale() reads shared state like data->gain_sel without acquiring data->lock, while mlx90393_write_raw() modifies these fields while holding the lock.
drivers/iio/magnetometer/mlx90393_core.c line 565
Quoted 6 lines
drivers/iio/magnetometer/mlx90393_core.c new file +6
-0+ +case IIO_TEMP : +/* Datasheet: 22124 millidegC/LSB */ + *val =0 ; + *val2 =22124 ; +return IIO_VAL_INT_PLUS_MICRO ;
Does returning IIO_VAL_INT_PLUS_MICRO with 22124 yield Celsius rather than the milli-Celsius required for temperature channels by the IIO ABI?
Quoted 8 lines
drivers/iio/magnetometer/mlx90393_core.c new file +8
-0+ +default : +return -EINVAL ; + } + +case IIO_CHAN_INFO_OFFSET : +if (chan->type ==IIO_TEMP ) { +/* Datasheet: temperature offset */
drivers/iio/magnetometer/mlx90393_core.c line 583
Quoted 10 lines
drivers/iio/magnetometer/mlx90393_core.c new file +10
-0+ *val =-45114 ; +return IIO_VAL_INT ; + } +return -EINVAL ; + +case IIO_CHAN_INFO_OVERSAMPLING_RATIO : +if (chan->type ==IIO_TEMP ) +return mlx90393_get_temp_osr2 (data, val); +if (chan->type ==IIO_MAGN ) +return mlx90393_get_osr (data, val);
Can these reads of data->osr and data->osr2 also race with concurrent sysfs writes, as data->lock is not acquired here?
[ ... ]
Quoted 16 lines
drivers/iio/magnetometer/mlx90393_core.c new file +16
-0+static int mlx90393_read_avail (struct iio_dev *indio_dev, +const struct iio_chan_spec *chan, +const int **vals, +int *type, +int *length, +long mask) +{ +struct mlx90393_data *data =iio_priv (indio_dev); + +static int scale_avail[MLX90393_GAIN_MAX ][MLX90393_AXIS_MAX ]; +enum mlx90393_axis_type axis; +int i; +u8 res; + +switch (mask) { +case IIO_CHAN_INFO_SCALE :
drivers/iio/magnetometer/mlx90393_core.c line 615
Quoted 10 lines
drivers/iio/magnetometer/mlx90393_core.c new file +10
-0+ axis = chan->channel2 ==IIO_MOD_Z ; + res = axis ? data->res_z : data->res_xy; + +for (i =0 ; i <MLX90393_GAIN_MAX ; i++) { + scale_avail[i][0 ] =0 ; + scale_avail[i][1 ] = + mlx90393_scale_table[axis][i][res]; + } + + *vals = &scale_avail[0 ][0 ];
Is it safe to use a global static array here?
Since scale_avail is dynamically populated based on the requested channel axis, concurrent sysfs reads across different axes or device instances could overwrite the array mid-operation, causing a data race and corrupted scale values to be returned.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Jonathan Cameron
maintainerQuoted 1 line · On Mon, 11 May 2026 00:40:08 +0530 Nikhil Gautam <[email protected]> wrote:
Hi,
Hi Nikhil.
Thanks for your driver
Before I look at it, a quick comment on use of RFC. Generally we only use that for drivers that either have a series of specific open questions (driver structure doesn't count for IIO as we have a 'lot' of examples!) and those questions should be at the top of the RFC.
The other exception is where you know something is not ready for merge due to some dependency or similar on other code. Occasionally done as a quick hint on how we might solve a problem that is raised.
Anyhow, just send this without RFC. You'll get comments anyway!
Probably more of them as many reviewers are short on time so will only briefly glance an an RFC if at all!
Jonathan
Quoted 57 lines
This series adds initial Industrial I/O subsystem support for the Melexis MLX90393 3-axis magnetometer and temperature sensor.
The MLX90393 supports both I2C and SPI interfaces. This RFC implements support for the I2C interface while keeping the driver structure transport-independent to simplify future SPI support.
Currently supported features:
raw magnetic field measurements for X/Y/Z axes
raw temperature measurements
configurable gain/scale selection
configurable oversampling ratio
direct mode operation through the IIO subsystem
initially I2C support for this driverThe driver has been tested on Raspberry Pi 5 hardware using an actual MLX90393 sensor connected over I2C. Raw magnetic field and temperature values were verified through the IIO sysfs interface.
A custom transport abstraction was used instead of regmap due to the MLX90393 command/response protocol semantics and repeated-start transfer behavior. This approach was discussed previously on the mailing list and seemed preferable for this device.
The implementation intentionally focuses on a minimal and reviewable feature set for the initial RFC submission. Support for SPI, buffered capture, triggers, interrupts and runtime PM can be added incrementally in follow-up work.
The DT binding schema has been validated using
dt_binding_checkand the driver has been checked using checkpatch.pl --strict.Feedback on the overall driver structure, transport abstraction, IIO ABI usage and DT binding would be appreciated.
Thanks,
Nikhil GautamNikhil Gautam (2):
dt-bindings: iio: magnetometer: add Melexis MLX90393
iio: magnetometer: add support for Melexis MLX90393.../iio/magnetometer/melexis,mlx90393.yaml | 39 + drivers/iio/magnetometer/Kconfig | 10 + drivers/iio/magnetometer/Makefile | 2 + drivers/iio/magnetometer/mlx90393.h | 76 ++ drivers/iio/magnetometer/mlx90393_core.c | 724 ++++++++++++++++++ drivers/iio/magnetometer/mlx90393_i2c.c | 71 ++ 6 files changed, 922 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/magnetometer/melexis,mlx90393.yaml create mode 100644 drivers/iio/magnetometer/mlx90393.h create mode 100644 drivers/iio/magnetometer/mlx90393_core.c create mode 100644 drivers/iio/magnetometer/mlx90393_i2c.c