iio: magnetometer: add driver for mmc5603 sensor
PATCH 1/1 iio: magnetometer: add driver for mmc5603 sensor
This adds support for the MMC5603 magnetometer.
Features:
- Raw X/Y/Z readout in milligauss
- Raw temp readout in millicelsius
--- drivers/iio/magnetometer/Kconfig | 11 + drivers/iio/magnetometer/Makefile | 1 + drivers/iio/magnetometer/mmc5603.c | 629 +++++++++++++++++++++++++++++ 3 files changed, 641 insertions(+) create mode 100644 drivers/iio/magnetometer/mmc5603.c
drivers/iio/magnetometer/Kconfig +11 -0
@@ -139,6 +139,17 @@ config MMC35240 To compile this driver as a module, choose M here: the module will be called mmc35240. +config MMC5603 + tristate "MEMSIC MMC5603NJ 3-axis magnetic sensor" + select REGMAP_I2C + depends on I2C + help + Say yes here to build support for the MEMSIC MMC5603NJ 3-axis + magnetic sensor. + + To compile this driver as a module, choose M here: the module + will be called mmc5603. + config IIO_ST_MAGN_3AXIS tristate "STMicroelectronics magnetometers 3-Axis Driver" depends on (I2C || SPI_MASTER) && SYSFS
drivers/iio/magnetometer/Makefile +1 -0
@@ -15,6 +15,7 @@ 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_MMC35240) += mmc35240.o +obj-$(CONFIG_MMC5603) += mmc5603.o obj-$(CONFIG_IIO_ST_MAGN_3AXIS) += st_magn.o st_magn-y := st_magn_core.o
drivers/iio/magnetometer/mmc5603.c new file +629 -0
629 lines not on this page · raw message
Signature · 1 line
2.48.1
Quoted 6 lines · On Fri, 3 Oct 2025 02:07:31 +0200 Adrian Fluturel <[email protected]>…
This adds support for the MMC5603 magnetometer.
Features:
- Raw X/Y/Z readout in milligauss
- Raw temp readout in millicelsiusSigned-off-by Adrian Fluturel <[email protected]>
Hi Adrian
I see you had a discussion with Frank on whether this could be integrated with the mmc5633 driver. What was conclusion of that?
I'll give this a quick review anyway.
I'd expect to see a DT binding patch for any new magnetometer driver even if very simple. Or an addition to trivial devices. If it is already documented, then add a note to this patch description to say that.
Thanks,
Jonathan
Quoted 12 lines
--- drivers/iio/magnetometer/Kconfig | 11 + drivers/iio/magnetometer/Makefile | 1 + drivers/iio/magnetometer/mmc5603.c | 629 +++++++++++++++++++++++++++++ 3 files changed, 641 insertions(+) create mode 100644 drivers/iio/magnetometer/mmc5603.cdrivers/iio/magnetometer/Kconfig +0
-0
drivers/iio/magnetometer/Kconfig config MMC35240 · line 148
Quoted 10 lines
drivers/iio/magnetometer/Kconfig +7
-0@@ -139,6 +139,17 @@ config MMC35240 To compile this driver as a module, choose M here: the module will be called mmc35240. +config MMC5603 + tristate "MEMSIC MMC5603NJ 3-axis magnetic sensor" + select REGMAP_I2C + depends on I2C + help + Say yes here to build support for the MEMSIC MMC5603NJ 3-axis + magnetic sensor.
Formatting seems to have gone wrong. Help text should be a tab and 2 spaces in. Everything else one tab.
Quoted 15 lines
drivers/iio/magnetometer/Kconfig +4
-0+ + To compile this driver as a module, choose M here: the module + will be called mmc5603. + config IIO_ST_MAGN_3AXIS tristate "STMicroelectronics magnetometers 3-Axis Driver" depends on (I2C || SPI_MASTER) && SYSFSdrivers/iio/magnetometer/mmc5603.c new file +1
-0@@ -0,0 +1,629 @@ +// SPDX-License-Identifier: GPL-2.0-only
drivers/iio/magnetometer/mmc5603.c line 11
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+/* +* MMC5603NJ - MEMSIC 3-axis magnetic sensor +* +* Copyright (c) 2025, Adrian Fluturel. +* +* IIO driver for MMC5603 (7-bit I2C slave address 0x30). +*/ + +#include <linux/module.h> +#include <linux/mod_devicetable.h>
Follow include what you use principles a little closer. Should be mutex.h and probably various others.
drivers/iio/magnetometer/mmc5603.c line 19
Quoted 8 lines
drivers/iio/magnetometer/mmc5603.c new file +8
-0+#include <linux/init.h> +#include <linux/i2c.h> +#include <linux/delay.h> +#include <linux/regmap.h> +#include <linux/pm.h> + +#include <linux/iio/iio.h> +#include <linux/iio/sysfs.h>
This should go with the changes suggested below.
drivers/iio/magnetometer/mmc5603.c line 22
Quoted 3 lines
drivers/iio/magnetometer/mmc5603.c new file +3
-0+ +#define MMC5603_DRV_NAME "mmc5603" +#define MMC5603_REGMAP_NAME "mmc5603_regmap"
Just put that inline. The define doesn't benefit anyone as we look for the name where it is used. Same for drv name.
Quoted 12 lines
drivers/iio/magnetometer/mmc5603.c new file +12
-0+ +/* Registers */ +#define MMC5603_REG_XOUT_0 0x00 +#define MMC5603_REG_XOUT_1 0x01 +#define MMC5603_REG_XOUT_2 0x06 +#define MMC5603_REG_YOUT_0 0x02 +#define MMC5603_REG_YOUT_1 0x03 +#define MMC5603_REG_YOUT_2 0x07 +#define MMC5603_REG_ZOUT_0 0x04 +#define MMC5603_REG_ZOUT_1 0x05 +#define MMC5603_REG_ZOUT_2 0x08 +#define MMC5603_REG_TOUT 0x09
drivers/iio/magnetometer/mmc5603.c line 44
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+ +#define MMC5603_REG_STATUS 0x18 +#define MMC5603_REG_ODR 0x1A +#define MMC5603_REG_CTRL0 0x1B +#define MMC5603_REG_CTRL1 0x1C +#define MMC5603_REG_CTRL2 0x1D + +#define MMC5603_REG_ID 0x39 + +/* STATUS bits */
The naming makes that clear (which is great). So drop the comments.
Quoted 5 lines
drivers/iio/magnetometer/mmc5603.c new file +5
-0+#define MMC5603_STATUS_MEAS_M_DONE_BIT BIT(6) +#define MMC5603_STATUS_MEAS_T_DONE_BIT BIT(7) + +/* CONTROL0 bits */ +#define MMC5603_CTRL0_TM_M_BIT BIT(0)
drivers/iio/magnetometer/mmc5603.c line 59
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+#define MMC5603_CTRL0_TM_T_BIT BIT(1) +#define MMC5603_CTRL0_SET_BIT BIT(3) +#define MMC5603_CTRL0_RESET_BIT BIT(4) +#define MMC5603_CTRL0_AUTO_SR_EN BIT(5) + +/* CONTROL1 bits */ +#define MMC5603_CTRL1_BW0_BIT BIT(0) +#define MMC5603_CTRL1_BW1_BIT BIT(1) +#define MMC5603_CTRL1_BW_MASK (MMC5603_CTRL1_BW0_BIT | MMC5603_CTRL1_BW1_BIT) +#define MMC5603_CTRL1_BW_SHIFT 0
Seems like a 2 bit field. Use GENMASK(1, 0) and no need for shift or individual bit definitions that I can spot.
drivers/iio/magnetometer/mmc5603.c line 65
Quoted 6 lines
drivers/iio/magnetometer/mmc5603.c new file +6
-0+#define MMC5603_CTRL1_SW_RESET_BIT BIT(7) + +/* WAIT TIMES */ +#define MMC5603_POWERUP_WAIT_TIME 20000/* us */ +#define MMC5603_SR_WAIT_TIME 1000/* us */ +#define MMC5603_MEAS_WAIT_TIME 6600/* us */
rename the define to include the units. Then don't need the comments.
WAIT_USECS then you don't need to call out its a time as kind of
obvious.
drivers/iio/magnetometer/mmc5603.c line 77
Quoted 12 lines
drivers/iio/magnetometer/mmc5603.c new file +12
-0+ +enum mmc5603_resolution { +MMC5603_16_BITS =0 , +MMC5603_18_BITS , +MMC5603_20_BITS , +}; + +enum mmc5603_channels { +AXIS_X =0 , +AXIS_Y , +AXIS_Z , +TEMPERATURE ,
Very generic names that are likely to clash in future. Prefix with device name.
Quoted 18 lines
drivers/iio/magnetometer/mmc5603.c new file +18
-0+}; + +static const struct { +int sens; +int nfo; +} mmc5603_props_table[] = { +/* 16 bits operation */ + { +1024 , +32768 , + }, +/* 18 bits operation */ + { +4096 , +131072 , + }, +/* 20 bits operation */ + {
drivers/iio/magnetometer/mmc5603.c line 105
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+16384 , + 524288, + } +}; + +static const struct { +int val; +int val2; +} mmc5603_samp_freq[] = { + {1, 200000},
{ 1, 200000 },etc preferred for IIO. We are slowly standardizing on this.
drivers/iio/magnetometer/mmc5603.c line 111
Quoted 6 lines
drivers/iio/magnetometer/mmc5603.c new file +6
-0+ {2, 0}, + {3, 500000}, + {6, 600000}, +}; + +static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("1.2 2 3.5 6.6");
As below, use the callbacks instead of custom attr.
Quoted 16 lines
drivers/iio/magnetometer/mmc5603.c new file +16
-0+ +#define MMC5603_CHANNEL (_axis) { \ + .type = IIO_MAGN, \ + .modified = 1, \ + .channel2 = IIO_MOD_ ## _axis, \ + .address = AXIS_ ## _axis, \ + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \ + BIT(IIO_CHAN_INFO_SCALE), \ +} + +static const struct iio_chan_spec mmc5603_channels[] = { +MMC5603_CHANNEL (X ), +MMC5603_CHANNEL (Y ), +MMC5603_CHANNEL (Z ), + {
drivers/iio/magnetometer/mmc5603.c line 137
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+ .type = IIO_TEMP, + .info_mask_separate =BIT (IIO_CHAN_INFO_RAW ) | +BIT (IIO_CHAN_INFO_SCALE ) | + BIT(IIO_CHAN_INFO_OFFSET), + .address = TEMPERATURE, + }, +}; + +static struct attribute *mmc5603_attributes[] = { + &iio_const_attr_sampling_frequency_available.dev_attr.attr,
Should be able to use the read_avail() callback and the relevant masks to implement this. Eventually I plan to remove the macros that make this approach too easy to do. The reason being that they don't allow in kernel accesses.
drivers/iio/magnetometer/mmc5603.c line 145
Quoted 8 lines
drivers/iio/magnetometer/mmc5603.c new file +8
-0+ NULL +}; + +static const struct attribute_group mmc5603_attribute_group = { + .attrs = mmc5603_attributes, +}; + +// Private data
Comment not useful, also not C style.
drivers/iio/magnetometer/mmc5603.c line 148
Quoted 3 lines
drivers/iio/magnetometer/mmc5603.c new file +3
-0+struct mmc5603_data { +struct i2c_client *client; +struct mutex mutex;
Locks always need a comment to say what data they serialize access to. Often that's either a RMW cycle or some case where we need a bunch of accesses to occur atomically wrt to other accesses. Otherwise the regmap internal locks are sufficent.
Quoted 8 lines
drivers/iio/magnetometer/mmc5603.c new file +7
-0+struct regmap *regmap; + +enum mmc5603_resolution res; +}; +static int mmc5603_sw_reset (struct mmc5603_data *data) +{ +int ret;
drivers/iio/magnetometer/mmc5603.c line 191
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+ + ret =regmap_set_bits (data->regmap,MMC5603_REG_CTRL1 , +MMC5603_CTRL1_SW_RESET_BIT ); + +if (ret <0 ) +return ret; + +/* The datasheet says to wait 20ms after a software reset */ +usleep_range (MMC5603_POWERUP_WAIT_TIME , +MMC5603_POWERUP_WAIT_TIME +100 );
fsleep() for all sleeps where we don't need precision on when they finish. That sets standard range values for us so we don't need to justify choices at each call.
drivers/iio/magnetometer/mmc5603.c line 202
Quoted 11 lines
drivers/iio/magnetometer/mmc5603.c new file +11
-0+ +return 0 ; +} + +static int mmc5603_init (struct mmc5603_data *data) +{ +int ret; +unsigned int reg_id; + +usleep_range (MMC5603_POWERUP_WAIT_TIME , +MMC5603_POWERUP_WAIT_TIME +100 );
fsleep(). Normally we put this with the regulator enable that's related to this rather than in init.
drivers/iio/magnetometer/mmc5603.c line 210
Quoted 8 lines
drivers/iio/magnetometer/mmc5603.c new file +8
-0+ + ret =regmap_read (data->regmap,MMC5603_REG_ID , ®_id); +if (ret <0 ) { +dev_err (&data->client->dev,"Error reading product id\n" ); +return ret; + } + +dev_info (&data->client->dev,"MMC5603 chip id %x\n" , reg_id);
Probably too noisy. Use dev_dbg() and rely on dynamic debug if anyone needs to know this.
drivers/iio/magnetometer/mmc5603.c
Quoted 2 lines
drivers/iio/magnetometer/mmc5603.c new file +2
-0+ +/* Do a software reset */
Function name makes that obvious so drop the comment.
drivers/iio/magnetometer/mmc5603.c line 222
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+ ret =mmc5603_sw_reset (data); +if (ret <0 ) +return ret; + +/* Do a SET-RESET cycle to get more accurate readings */ + ret =mmc5603_set (data, true); +if (ret <0 ) +return ret; + +/* Wait 1 millisecond after a SET or RESET */
fsleep()
Quoted 7 lines
drivers/iio/magnetometer/mmc5603.c new file +7
-0+usleep_range (MMC5603_SR_WAIT_TIME ,MMC5603_SR_WAIT_TIME +1 ); + + ret =mmc5603_set (data, false); +if (ret <0 ) +return ret; + +usleep_range (MMC5603_SR_WAIT_TIME ,MMC5603_SR_WAIT_TIME +1 );
drivers/iio/magnetometer/mmc5603.c line 239
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+ +/* Set default sampling frequency */ + ret =regmap_update_bits (data->regmap,MMC5603_REG_CTRL1 , +MMC5603_CTRL1_BW_MASK , data->res); +if (ret <0 ) +return ret; + +/* Enable automatic periodic SET-RESET */ + ret =regmap_set_bits (data->regmap,MMC5603_REG_CTRL0 , +MMC5603_CTRL0_AUTO_SR_EN );
Not checking ret?
Quoted 5 lines
drivers/iio/magnetometer/mmc5603.c new file +5
-0+ +return 0 ; +} + +static int mmc5603_take_magn_measurement (struct mmc5603_data *data)
drivers/iio/magnetometer/mmc5603.c line 254
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+{ +int ret, tries =100 ; +unsigned int reg_status; + + ret =regmap_write (data->regmap,MMC5603_REG_CTRL0 , +MMC5603_CTRL0_TM_M_BIT ); +if (ret <0 ) +return ret; + +while (tries-- >0 ) {
Similar comments to below.
Quoted 21 lines
drivers/iio/magnetometer/mmc5603.c new file +21
-0+ ret =regmap_read (data->regmap,MMC5603_REG_STATUS , + ®_status); +if (ret <0 ) +return ret; +if (reg_status &MMC5603_STATUS_MEAS_M_DONE_BIT ) +break ; + +/* Minimum time between measurements */ +usleep_range (MMC5603_MEAS_WAIT_TIME , +MMC5603_MEAS_WAIT_TIME +1 ); + } + +if (tries <0 ) { +dev_err (&data->client->dev,"data not ready" ); +return -EIO ; + } + +return 0 ; +} + +static int mmc5603_take_temp_measurement (struct mmc5603_data *data)
drivers/iio/magnetometer/mmc5603.c line 285
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+{ +int ret, tries =100 ; +unsigned int reg_status; + + ret =regmap_write (data->regmap,MMC5603_REG_CTRL0 , +MMC5603_CTRL0_TM_T_BIT ); +if (ret <0 ) +return ret; + +while (tries-- >0 ) {
Add a comment on why retry and why that many times. Ideally reference the datasheet.
drivers/iio/magnetometer/mmc5603.c line 291
Quoted 6 lines
drivers/iio/magnetometer/mmc5603.c new file +6
-0+ ret =regmap_read (data->regmap,MMC5603_REG_STATUS , + ®_status); +if (ret <0 ) +return ret; +if (reg_status &MMC5603_STATUS_MEAS_T_DONE_BIT ) { +printk (KERN_INFO "Measurement T done bit is 1\n" );
dev_info() if it makes sense. Probably dev_dbg().
Quoted 20 lines
drivers/iio/magnetometer/mmc5603.c new file +20
-0+break ; + } + +/* Minimum time between measurements */ +usleep_range (MMC5603_MEAS_WAIT_TIME , +MMC5603_MEAS_WAIT_TIME +1 ); + } + +if (tries <0 ) { +dev_err (&data->client->dev,"data not ready" ); +return -EIO ; + } + +return 0 ; +} + +static int mmc5603_read_magn_measurement (struct mmc5603_data *data, +u32 ret_data[3 ]) +{ +int ret;
drivers/iio/magnetometer/mmc5603.c line 321
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+u8 buf[9 ]; + + ret =mmc5603_take_magn_measurement (data); +if (ret <0 ) +return ret; + + ret =regmap_bulk_read (data->regmap,MMC5603_REG_XOUT_0 , buf, +9 *sizeof (u8)); + +// Extract X (registers 0x00, 0x01, 0x06)
Can see that, so the comment isn't that helpful.
Looks like you could use a get_unaligned_be16() or similar to help a little. I think I commented on this in Frank's driver.
Quoted 14 lines
drivers/iio/magnetometer/mmc5603.c new file +13
-0+ ret_data[AXIS_X ] = (buf[0 ] <<12 ) | (buf[1 ] <<4 ) | (buf[6 ] >>4 ); + +// Extract Y (registers 0x02, 0x03, 0x07) + ret_data[AXIS_Y ] = (buf[2 ] <<12 ) | (buf[3 ] <<4 ) | (buf[7 ] >>4 ); + +// Extract Z (registers 0x04, 0x05, 0x08) + ret_data[AXIS_Z ] = (buf[4 ] <<12 ) | (buf[5 ] <<4 ) | (buf[8 ] >>4 ); + +return ret; +} +static int mmc5603_read_temperature (struct mmc5603_data *data,int *ret_data) +{ +int ret, raw_temp;
drivers/iio/magnetometer/mmc5603.c line 357
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+ +mutex_lock (&data->mutex); + ret =mmc5603_read_temp_measurement (data, &raw_temp); +mutex_unlock (&data->mutex); + +if (ret <0 ) +return ret; + +/* 0.8 degrees Celsius/LSB, 0x00 is -75 degrees Celsius */ + *ret_data = raw_temp *800 -75000 ;
Why are you doing some maths to convert in driver and leaving some scaling to userspace? IIO convention is push everything to userspace if possible. There we have floating point to help do the maths nicely.
drivers/iio/magnetometer/mmc5603.c line 368
Quoted 11 lines
drivers/iio/magnetometer/mmc5603.c new file +11
-0+return 0 ; +} + +static int mmc5603_raw_to_mgauss (struct mmc5603_data *data,int index, +u32 raw[],int *val) +{ +int sens; +int nfo; + + sens = mmc5603_props_table[data->res].sens; + nfo = mmc5603_props_table[data->res].nfo;
Might as well combine assignment and declaration here.
int sens = ...
etc
Quoted 20 lines
drivers/iio/magnetometer/mmc5603.c new file +20
-0+ +switch (index) { +case AXIS_X : + *val = ((int )raw[AXIS_X ] - nfo) *1000 / sens; +break ; +case AXIS_Y : + *val = ((int )raw[AXIS_Y ] - nfo) *1000 / sens; +break ; +case AXIS_Z : + *val = ((int )raw[AXIS_Z ] - nfo) *1000 / sens; +break ; +default : +return -EINVAL ; + } + +return 0 ; +} + +static int mmc5603_read_mgauss (struct mmc5603_data *data,int index,int *val) +{
drivers/iio/magnetometer/mmc5603.c line 398
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+int ret; +u32 raw[3 ]; + +mutex_lock (&data->mutex); + ret =mmc5603_read_magn_measurement (data, raw); +mutex_unlock (&data->mutex); +if (ret <0 ) +return ret; + ret =mmc5603_raw_to_mgauss (data, index, raw, val); +return ret;
return mmc...
Quoted 21 lines
drivers/iio/magnetometer/mmc5603.c new file +21
-0+} + +static int mmc5603_read_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan,int *val, +int *val2,long mask) +{ +struct mmc5603_data *data =iio_priv (indio_dev); +int ret, i; +unsigned int reg; + +switch (mask) { +case IIO_CHAN_INFO_RAW : +switch (chan->address) { +case AXIS_X : +case AXIS_Y : +case AXIS_Z : + ret =mmc5603_read_mgauss (data, chan->address, val); +if (ret <0 ) +return ret; +return IIO_VAL_INT ; +case TEMPERATURE :
drivers/iio/magnetometer/mmc5603.c line 429
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+ ret =mmc5603_read_temperature (data, val); +if (ret <0 ) +return ret; +return IIO_VAL_INT ; +default : +return -EINVAL ; + } +case IIO_CHAN_INFO_SCALE : +/* For IIO_TEMP and IIO_MAGN, the result is in milligauss +and millicelcius so the scale is the same. */
Comment slightly confuses me. Is the point that raw values for bother are in micro units so we need to divide by 1000 to correct that?
If so a fraction type might be easier to read.
Multiline comment syntax is.
/* * For IIO_TEM... * and milice... */
Quoted 33 lines
drivers/iio/magnetometer/mmc5603.c new file +33
-0+switch (chan->type) { +case IIO_MAGN : +case IIO_TEMP : + *val =0 ; + *val2 =1000 ; +return IIO_VAL_INT_PLUS_MICRO ; +default : +return -EINVAL ; + } + +case IIO_CHAN_INFO_SAMP_FREQ : +mutex_lock (&data->mutex); + ret =regmap_read (data->regmap,MMC5603_REG_CTRL1 , ®); +mutex_unlock (&data->mutex); + +if (ret <0 ) +return ret; + + i = (reg &MMC5603_CTRL1_BW_MASK ); +if (i <0 || i >=ARRAY_SIZE (mmc5603_samp_freq)) +return -EINVAL ; + + *val = mmc5603_samp_freq[i].val; + *val2 = mmc5603_samp_freq[i].val2; +return IIO_VAL_INT_PLUS_MICRO ; +default : +return -EINVAL ; + } +} + +static int mmc5603_write_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan,int val, +int val2,long mask)
drivers/iio/magnetometer/mmc5603.c line 472
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+{ +struct mmc5603_data *data =iio_priv (indio_dev); +int i, ret; + +switch (mask) { +case IIO_CHAN_INFO_SAMP_FREQ : + i =mmc5603_get_samp_freq_index (data, val, val2); +if (i <0 ) +return -EINVAL ; +mutex_lock (&data->mutex);
I'd add scope with {} around the whole case block and then use
guard(mutex)(&data->mutex);
make sure to include cleanuph if you aren't already.
The advantage here is that you can then do
return regmap_update_bits...
Quoted 15 lines
drivers/iio/magnetometer/mmc5603.c new file +15
-0+ ret =regmap_update_bits (data->regmap,MMC5603_REG_CTRL1 , +MMC5603_CTRL1_BW_MASK , + i <<MMC5603_CTRL1_BW_SHIFT ); +mutex_unlock (&data->mutex); +return ret; +default : +return -EINVAL ; + } +} + +static const struct iio_info mmc5603_info = { + .read_raw = mmc5603_read_raw, + .write_raw = mmc5603_write_raw, + .attrs = &mmc5603_attribute_group +};
drivers/iio/magnetometer/mmc5603.c line 546
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +9
-0+ +static const struct regmap_config mmc5603_regmap_config = { + .name =MMC5603_REGMAP_NAME , + + .reg_bits =8 , + .val_bits =8 , + + .max_register =MMC5603_REG_ID , + .cache_type =REGCACHE_FLAT ,
Generally just use the maple tree one unless very strong reason to do otherwise. It's pretty efficient event for dense cases like I guess you have here.
Quoted 12 lines
drivers/iio/magnetometer/mmc5603.c new file +12
-0+ + .writeable_reg = mmc5603_is_writeable_reg, + .readable_reg = mmc5603_is_readable_reg, + .volatile_reg = mmc5603_is_volatile_reg, + + .reg_defaults = mmc5603_reg_defaults, + .num_reg_defaults = ARRAY_SIZE(mmc5603_reg_defaults), +}; + +static int mmc5603_probe (struct i2c_client *client) { +struct mmc5603_data *data; +struct iio_dev *indio_dev;
drivers/iio/magnetometer/mmc5603.c line 568
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+struct regmap *regmap; +int ret; + + indio_dev =devm_iio_device_alloc (&client->dev,sizeof (*data)); +if (!indio_dev) +return -ENOMEM ; + + regmap =devm_regmap_init_i2c (client, &mmc5603_regmap_config); +if (IS_ERR (regmap)) { +dev_err (&client->dev,"regmap initialization failed\n" );
For all error messages in probe and functions called from probe only use
return dev_err_probe(...)
Might be worth
struct device *dev = &client->dev;
just to shorten a few lines.
drivers/iio/magnetometer/mmc5603.c line 573
Quoted 5 lines
drivers/iio/magnetometer/mmc5603.c new file +5
-0+return PTR_ERR (regmap); + } + + data =iio_priv (indio_dev); +i2c_set_clientdata (client, indio_dev);
Is this used? Normally only needed if we have some pm callbacks around.
drivers/iio/magnetometer/mmc5603.c line 578
Quoted 5 lines
drivers/iio/magnetometer/mmc5603.c new file +5
-0+ data->client = client; + data->regmap = regmap; + data->res =MMC5603_20_BITS ; + +mutex_init (&data->mutex);
For new code I prefer
ret = devm_mutex_init(&data->mutex); if (ret) return ret;
Brings only a minor advantage for debugging some use after free conditions but it's cheap so why not?
drivers/iio/magnetometer/mmc5603.c line 588
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+ + indio_dev->info = &mmc5603_info; + indio_dev->name =MMC5603_DRV_NAME ; + indio_dev->channels = mmc5603_channels; + indio_dev->num_channels =ARRAY_SIZE (mmc5603_channels); + indio_dev->modes =INDIO_DIRECT_MODE ; + + ret =mmc5603_init (data); +if (ret <0 ) { +dev_err (&client->dev,"mmc5603 chip init failed\n" );
Another return dev_err_probe()
drivers/iio/magnetometer/mmc5603.c line 594
Quoted 6 lines
drivers/iio/magnetometer/mmc5603.c new file +6
-0+return ret; + } +return devm_iio_device_register (&client->dev, indio_dev); +} + +// Device tree matching
As below.
drivers/iio/magnetometer/mmc5603.c line 601
Quoted 7 lines
drivers/iio/magnetometer/mmc5603.c new file +7
-0+static const struct of_device_id mmc5603_of_match[] = { + { .compatible ="memsic,mmc5603" , }, + { } +}; +MODULE_DEVICE_TABLE (of, mmc5603_of_match); + +// ACPI matching
As below.
drivers/iio/magnetometer/mmc5603.c line 608
Quoted 7 lines
drivers/iio/magnetometer/mmc5603.c new file +7
-0+static const struct acpi_device_id mmc5603_acpi_match[] = { + {"MMC5603" ,0 }, + { } +}; +MODULE_DEVICE_TABLE (acpi, mmc5603_acpi_match); + +// Raw I2C matching
Comment unnecessary and for IIO at least stick to traditional C comments except where it's an SPDX tag and slightly different rules apply
/* Raw I2C matching */
Quoted 21 lines
drivers/iio/magnetometer/mmc5603.c new file +21
-0+static const struct i2c_device_id mmc5603_id[] = { + {"mmc5603" }, + { } +}; +MODULE_DEVICE_TABLE (i2c, mmc5603_id); + +static struct i2c_driver mmc5603_driver = { + .driver = { + .name =MMC5603_DRV_NAME , + .of_match_table = mmc5603_of_match, + .acpi_match_table = mmc5603_acpi_match, + }, + .probe = mmc5603_probe, + .id_table = mmc5603_id, +}; + +module_i2c_driver (mmc5603_driver); + +MODULE_AUTHOR ("Adrian Fluturel <[email protected]>" ); +MODULE_DESCRIPTION ("MEMSIC MMC5603 magnetic sensor driver" ); +MODULE_LICENSE ("GPL v2" );
Quoted 8 lines · Jonathan Cameron
Quoted 5 lines · On Fri, 3 Oct 2025 02:07:31 +0200 Adrian Fluturel <[email protected]>…
This adds support for the MMC5603 magnetometer.
Features:
- Raw X/Y/Z readout in milligauss
- Raw temp readout in millicelsius
Quoted 5 lines
Quoted 1 line
Signed-off-by Adrian Fluturel <[email protected]>Hi Adrian
I see you had a discussion with Frank on whether this could be integrated with the mmc5633 driver. What was conclusion of that?
It is quite similar with mmc5633. Let me merge to one.
Frank
Quoted 736 lines
I'll give this a quick review anyway.
I'd expect to see a DT binding patch for any new magnetometer driver even if very simple. Or an addition to trivial devices. If it is already documented, then add a note to this patch description to say that.
Thanks,
Jonathan
Quoted 12 lines
--- drivers/iio/magnetometer/Kconfig | 11 + drivers/iio/magnetometer/Makefile | 1 + drivers/iio/magnetometer/mmc5603.c | 629 +++++++++++++++++++++++++++++ 3 files changed, 641 insertions(+) create mode 100644 drivers/iio/magnetometer/mmc5603.cdrivers/iio/magnetometer/Kconfig +0
-0drivers/iio/magnetometer/Kconfig config MMC35240 · line 148
Quoted 10 lines
drivers/iio/magnetometer/Kconfig +7
-0@@ -139,6 +139,17 @@ config MMC35240 To compile this driver as a module, choose M here: the module will be called mmc35240. +config MMC5603 + tristate "MEMSIC MMC5603NJ 3-axis magnetic sensor" + select REGMAP_I2C + depends on I2C + help + Say yes here to build support for the MEMSIC MMC5603NJ 3-axis + magnetic sensor.Formatting seems to have gone wrong. Help text should be a tab and 2 spaces in. Everything else one tab.
Quoted 15 lines
drivers/iio/magnetometer/Kconfig +4
-0+ + To compile this driver as a module, choose M here: the module + will be called mmc5603. + config IIO_ST_MAGN_3AXIS tristate "STMicroelectronics magnetometers 3-Axis Driver" depends on (I2C || SPI_MASTER) && SYSFSdrivers/iio/magnetometer/mmc5603.c new file +1
-0@@ -0,0 +1,629 @@ +// SPDX-License-Identifier: GPL-2.0-only drivers/iio/magnetometer/mmc5603.c line 11
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+/* +* MMC5603NJ - MEMSIC 3-axis magnetic sensor +* +* Copyright (c) 2025, Adrian Fluturel. +* +* IIO driver for MMC5603 (7-bit I2C slave address 0x30). +*/ + +#include <linux/module.h> +#include <linux/mod_devicetable.h> Follow include what you use principles a little closer. Should be
mutex.hand probably various others.drivers/iio/magnetometer/mmc5603.c line 19
Quoted 8 lines
drivers/iio/magnetometer/mmc5603.c new file +8
-0+#include <linux/init.h> +#include <linux/i2c.h> +#include <linux/delay.h> +#include <linux/regmap.h> +#include <linux/pm.h> + +#include <linux/iio/iio.h> +#include <linux/iio/sysfs.h> This should go with the changes suggested below.
drivers/iio/magnetometer/mmc5603.c line 22
Quoted 3 lines
drivers/iio/magnetometer/mmc5603.c new file +3
-0+ +#define MMC5603_DRV_NAME "mmc5603" +#define MMC5603_REGMAP_NAME "mmc5603_regmap"Just put that inline. The define doesn't benefit anyone as we look for the name where it is used. Same for drv name.
Quoted 12 lines
drivers/iio/magnetometer/mmc5603.c new file +12
-0+ +/* Registers */ +#define MMC5603_REG_XOUT_0 0x00 +#define MMC5603_REG_XOUT_1 0x01 +#define MMC5603_REG_XOUT_2 0x06 +#define MMC5603_REG_YOUT_0 0x02 +#define MMC5603_REG_YOUT_1 0x03 +#define MMC5603_REG_YOUT_2 0x07 +#define MMC5603_REG_ZOUT_0 0x04 +#define MMC5603_REG_ZOUT_1 0x05 +#define MMC5603_REG_ZOUT_2 0x08 +#define MMC5603_REG_TOUT 0x09drivers/iio/magnetometer/mmc5603.c line 44
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+ +#define MMC5603_REG_STATUS 0x18 +#define MMC5603_REG_ODR 0x1A +#define MMC5603_REG_CTRL0 0x1B +#define MMC5603_REG_CTRL1 0x1C +#define MMC5603_REG_CTRL2 0x1D + +#define MMC5603_REG_ID 0x39 + +/* STATUS bits */ The naming makes that clear (which is great). So drop the comments.
Quoted 5 lines
drivers/iio/magnetometer/mmc5603.c new file +5
-0+#define MMC5603_STATUS_MEAS_M_DONE_BIT BIT(6) +#define MMC5603_STATUS_MEAS_T_DONE_BIT BIT(7) + +/* CONTROL0 bits */ +#define MMC5603_CTRL0_TM_M_BIT BIT(0)drivers/iio/magnetometer/mmc5603.c line 59
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+#define MMC5603_CTRL0_TM_T_BIT BIT(1) +#define MMC5603_CTRL0_SET_BIT BIT(3) +#define MMC5603_CTRL0_RESET_BIT BIT(4) +#define MMC5603_CTRL0_AUTO_SR_EN BIT(5) + +/* CONTROL1 bits */ +#define MMC5603_CTRL1_BW0_BIT BIT(0) +#define MMC5603_CTRL1_BW1_BIT BIT(1) +#define MMC5603_CTRL1_BW_MASK (MMC5603_CTRL1_BW0_BIT | MMC5603_CTRL1_BW1_BIT) +#define MMC5603_CTRL1_BW_SHIFT 0Seems like a 2 bit field. Use
GENMASK(1, 0)and no need for shift or individual bit definitions that I can spot.drivers/iio/magnetometer/mmc5603.c line 65
Quoted 6 lines
drivers/iio/magnetometer/mmc5603.c new file +6
-0+#define MMC5603_CTRL1_SW_RESET_BIT BIT(7) + +/* WAIT TIMES */ +#define MMC5603_POWERUP_WAIT_TIME 20000/* us */ +#define MMC5603_SR_WAIT_TIME 1000/* us */ +#define MMC5603_MEAS_WAIT_TIME 6600/* us */ rename the define to include the units. Then don't need the comments.
WAIT_USECSthen you don't need to call out its a time as kind of
obvious.drivers/iio/magnetometer/mmc5603.c line 77
Quoted 12 lines
drivers/iio/magnetometer/mmc5603.c new file +12
-0+ +enum mmc5603_resolution { +MMC5603_16_BITS =0 , +MMC5603_18_BITS , +MMC5603_20_BITS , +}; + +enum mmc5603_channels { +AXIS_X =0 , +AXIS_Y , +AXIS_Z , +TEMPERATURE ,Very generic names that are likely to clash in future. Prefix with device name.
Quoted 18 lines
drivers/iio/magnetometer/mmc5603.c new file +18
-0+}; + +static const struct { +int sens; +int nfo; +} mmc5603_props_table[] = { +/* 16 bits operation */ + { +1024 , +32768 , + }, +/* 18 bits operation */ + { +4096 , +131072 , + }, +/* 20 bits operation */ + {drivers/iio/magnetometer/mmc5603.c line 105
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+16384 , + 524288, + } +}; + +static const struct { +int val; +int val2; +} mmc5603_samp_freq[] = { + {1, 200000},{ 1, 200000 },etc preferred for IIO. We are slowly standardizing on this.
drivers/iio/magnetometer/mmc5603.c line 111
Quoted 6 lines
drivers/iio/magnetometer/mmc5603.c new file +6
-0+ {2, 0}, + {3, 500000}, + {6, 600000}, +}; + +static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("1.2 2 3.5 6.6");As below, use the callbacks instead of custom attr.
Quoted 16 lines
drivers/iio/magnetometer/mmc5603.c new file +16
-0+ +#define MMC5603_CHANNEL (_axis) { \ + .type = IIO_MAGN, \ + .modified = 1, \ + .channel2 = IIO_MOD_ ## _axis, \ + .address = AXIS_ ## _axis, \ + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \ + BIT(IIO_CHAN_INFO_SCALE), \ +} + +static const struct iio_chan_spec mmc5603_channels[] = { +MMC5603_CHANNEL (X ), +MMC5603_CHANNEL (Y ), +MMC5603_CHANNEL (Z ), + {drivers/iio/magnetometer/mmc5603.c line 137
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+ .type = IIO_TEMP, + .info_mask_separate =BIT (IIO_CHAN_INFO_RAW ) | +BIT (IIO_CHAN_INFO_SCALE ) | + BIT(IIO_CHAN_INFO_OFFSET), + .address = TEMPERATURE, + }, +}; + +static struct attribute *mmc5603_attributes[] = { + &iio_const_attr_sampling_frequency_available.dev_attr.attr,Should be able to use the
read_avail()callback and the relevant masks to implement this. Eventually I plan to remove the macros that make this approach too easy to do. The reason being that they don't allow in kernel accesses.drivers/iio/magnetometer/mmc5603.c line 145
Quoted 8 lines
drivers/iio/magnetometer/mmc5603.c new file +8
-0+ NULL +}; + +static const struct attribute_group mmc5603_attribute_group = { + .attrs = mmc5603_attributes, +}; + +// Private data Comment not useful, also not C style.
drivers/iio/magnetometer/mmc5603.c line 148
Quoted 3 lines
drivers/iio/magnetometer/mmc5603.c new file +3
-0+struct mmc5603_data { +struct i2c_client *client; +struct mutex mutex;Locks always need a comment to say what data they serialize access to. Often that's either a RMW cycle or some case where we need a bunch of accesses to occur atomically wrt to other accesses. Otherwise the regmap internal locks are sufficent.
Quoted 8 lines
drivers/iio/magnetometer/mmc5603.c new file +7
-0+struct regmap *regmap; + +enum mmc5603_resolution res; +}; +static int mmc5603_sw_reset (struct mmc5603_data *data) +{ +int ret;drivers/iio/magnetometer/mmc5603.c line 191
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+ + ret =regmap_set_bits (data->regmap,MMC5603_REG_CTRL1 , +MMC5603_CTRL1_SW_RESET_BIT ); + +if (ret <0 ) +return ret; + +/* The datasheet says to wait 20ms after a software reset */ +usleep_range (MMC5603_POWERUP_WAIT_TIME , +MMC5603_POWERUP_WAIT_TIME +100 );
fsleep()for all sleeps where we don't need precision on when they finish. That sets standard range values for us so we don't need to justify choices at each call.drivers/iio/magnetometer/mmc5603.c line 202
Quoted 11 lines
drivers/iio/magnetometer/mmc5603.c new file +11
-0+ +return 0 ; +} + +static int mmc5603_init (struct mmc5603_data *data) +{ +int ret; +unsigned int reg_id; + +usleep_range (MMC5603_POWERUP_WAIT_TIME , +MMC5603_POWERUP_WAIT_TIME +100 );
fsleep(). Normally we put this with the regulator enable that's related to this rather than in init.drivers/iio/magnetometer/mmc5603.c line 210
Quoted 8 lines
drivers/iio/magnetometer/mmc5603.c new file +8
-0+ + ret =regmap_read (data->regmap,MMC5603_REG_ID , ®_id); +if (ret <0 ) { +dev_err (&data->client->dev,"Error reading product id\n" ); +return ret; + } + +dev_info (&data->client->dev,"MMC5603 chip id %x\n" , reg_id);Probably too noisy. Use
dev_dbg()and rely on dynamic debug if anyone needs to know this.drivers/iio/magnetometer/mmc5603.c
Quoted 2 lines
drivers/iio/magnetometer/mmc5603.c new file +2
-0+ +/* Do a software reset */ Function name makes that obvious so drop the comment.
drivers/iio/magnetometer/mmc5603.c line 222
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+ ret =mmc5603_sw_reset (data); +if (ret <0 ) +return ret; + +/* Do a SET-RESET cycle to get more accurate readings */ + ret =mmc5603_set (data, true); +if (ret <0 ) +return ret; + +/* Wait 1 millisecond after a SET or RESET */
fsleep()Quoted 7 lines
drivers/iio/magnetometer/mmc5603.c new file +7
-0+usleep_range (MMC5603_SR_WAIT_TIME ,MMC5603_SR_WAIT_TIME +1 ); + + ret =mmc5603_set (data, false); +if (ret <0 ) +return ret; + +usleep_range (MMC5603_SR_WAIT_TIME ,MMC5603_SR_WAIT_TIME +1 );drivers/iio/magnetometer/mmc5603.c line 239
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+ +/* Set default sampling frequency */ + ret =regmap_update_bits (data->regmap,MMC5603_REG_CTRL1 , +MMC5603_CTRL1_BW_MASK , data->res); +if (ret <0 ) +return ret; + +/* Enable automatic periodic SET-RESET */ + ret =regmap_set_bits (data->regmap,MMC5603_REG_CTRL0 , +MMC5603_CTRL0_AUTO_SR_EN );Not checking ret?
Quoted 5 lines
drivers/iio/magnetometer/mmc5603.c new file +5
-0+ +return 0 ; +} + +static int mmc5603_take_magn_measurement (struct mmc5603_data *data)drivers/iio/magnetometer/mmc5603.c line 254
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+{ +int ret, tries =100 ; +unsigned int reg_status; + + ret =regmap_write (data->regmap,MMC5603_REG_CTRL0 , +MMC5603_CTRL0_TM_M_BIT ); +if (ret <0 ) +return ret; + +while (tries-- >0 ) {Similar comments to below.
Quoted 21 lines
drivers/iio/magnetometer/mmc5603.c new file +21
-0+ ret =regmap_read (data->regmap,MMC5603_REG_STATUS , + ®_status); +if (ret <0 ) +return ret; +if (reg_status &MMC5603_STATUS_MEAS_M_DONE_BIT ) +break ; + +/* Minimum time between measurements */ +usleep_range (MMC5603_MEAS_WAIT_TIME , +MMC5603_MEAS_WAIT_TIME +1 ); + } + +if (tries <0 ) { +dev_err (&data->client->dev,"data not ready" ); +return -EIO ; + } + +return 0 ; +} + +static int mmc5603_take_temp_measurement (struct mmc5603_data *data)drivers/iio/magnetometer/mmc5603.c line 285
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+{ +int ret, tries =100 ; +unsigned int reg_status; + + ret =regmap_write (data->regmap,MMC5603_REG_CTRL0 , +MMC5603_CTRL0_TM_T_BIT ); +if (ret <0 ) +return ret; + +while (tries-- >0 ) {Add a comment on why retry and why that many times. Ideally reference the datasheet.
drivers/iio/magnetometer/mmc5603.c line 291
Quoted 6 lines
drivers/iio/magnetometer/mmc5603.c new file +6
-0+ ret =regmap_read (data->regmap,MMC5603_REG_STATUS , + ®_status); +if (ret <0 ) +return ret; +if (reg_status &MMC5603_STATUS_MEAS_T_DONE_BIT ) { +printk (KERN_INFO "Measurement T done bit is 1\n" );
dev_info()if it makes sense. Probablydev_dbg().Quoted 20 lines
drivers/iio/magnetometer/mmc5603.c new file +20
-0+break ; + } + +/* Minimum time between measurements */ +usleep_range (MMC5603_MEAS_WAIT_TIME , +MMC5603_MEAS_WAIT_TIME +1 ); + } + +if (tries <0 ) { +dev_err (&data->client->dev,"data not ready" ); +return -EIO ; + } + +return 0 ; +} + +static int mmc5603_read_magn_measurement (struct mmc5603_data *data, +u32 ret_data[3 ]) +{ +int ret;drivers/iio/magnetometer/mmc5603.c line 321
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+u8 buf[9 ]; + + ret =mmc5603_take_magn_measurement (data); +if (ret <0 ) +return ret; + + ret =regmap_bulk_read (data->regmap,MMC5603_REG_XOUT_0 , buf, +9 *sizeof (u8)); + +// Extract X (registers 0x00, 0x01, 0x06) Can see that, so the comment isn't that helpful.
Looks like you could use aget_unaligned_be16()or similar to help a little. I think I commented on this in Frank's driver.Quoted 14 lines
drivers/iio/magnetometer/mmc5603.c new file +13
-0+ ret_data[AXIS_X ] = (buf[0 ] <<12 ) | (buf[1 ] <<4 ) | (buf[6 ] >>4 ); + +// Extract Y (registers 0x02, 0x03, 0x07) + ret_data[AXIS_Y ] = (buf[2 ] <<12 ) | (buf[3 ] <<4 ) | (buf[7 ] >>4 ); + +// Extract Z (registers 0x04, 0x05, 0x08) + ret_data[AXIS_Z ] = (buf[4 ] <<12 ) | (buf[5 ] <<4 ) | (buf[8 ] >>4 ); + +return ret; +} +static int mmc5603_read_temperature (struct mmc5603_data *data,int *ret_data) +{ +int ret, raw_temp;drivers/iio/magnetometer/mmc5603.c line 357
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+ +mutex_lock (&data->mutex); + ret =mmc5603_read_temp_measurement (data, &raw_temp); +mutex_unlock (&data->mutex); + +if (ret <0 ) +return ret; + +/* 0.8 degrees Celsius/LSB, 0x00 is -75 degrees Celsius */ + *ret_data = raw_temp *800 -75000 ;Why are you doing some maths to convert in driver and leaving some scaling to userspace? IIO convention is push everything to userspace if possible. There we have floating point to help do the maths nicely.
drivers/iio/magnetometer/mmc5603.c line 368
Quoted 11 lines
drivers/iio/magnetometer/mmc5603.c new file +11
-0+return 0 ; +} + +static int mmc5603_raw_to_mgauss (struct mmc5603_data *data,int index, +u32 raw[],int *val) +{ +int sens; +int nfo; + + sens = mmc5603_props_table[data->res].sens; + nfo = mmc5603_props_table[data->res].nfo;Might as well combine assignment and declaration here.
int sens = ...etc
Quoted 20 lines
drivers/iio/magnetometer/mmc5603.c new file +20
-0+ +switch (index) { +case AXIS_X : + *val = ((int )raw[AXIS_X ] - nfo) *1000 / sens; +break ; +case AXIS_Y : + *val = ((int )raw[AXIS_Y ] - nfo) *1000 / sens; +break ; +case AXIS_Z : + *val = ((int )raw[AXIS_Z ] - nfo) *1000 / sens; +break ; +default : +return -EINVAL ; + } + +return 0 ; +} + +static int mmc5603_read_mgauss (struct mmc5603_data *data,int index,int *val) +{drivers/iio/magnetometer/mmc5603.c line 398
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+int ret; +u32 raw[3 ]; + +mutex_lock (&data->mutex); + ret =mmc5603_read_magn_measurement (data, raw); +mutex_unlock (&data->mutex); +if (ret <0 ) +return ret; + ret =mmc5603_raw_to_mgauss (data, index, raw, val); +return ret;return mmc...
Quoted 21 lines
drivers/iio/magnetometer/mmc5603.c new file +21
-0+} + +static int mmc5603_read_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan,int *val, +int *val2,long mask) +{ +struct mmc5603_data *data =iio_priv (indio_dev); +int ret, i; +unsigned int reg; + +switch (mask) { +case IIO_CHAN_INFO_RAW : +switch (chan->address) { +case AXIS_X : +case AXIS_Y : +case AXIS_Z : + ret =mmc5603_read_mgauss (data, chan->address, val); +if (ret <0 ) +return ret; +return IIO_VAL_INT ; +case TEMPERATURE :drivers/iio/magnetometer/mmc5603.c line 429
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+ ret =mmc5603_read_temperature (data, val); +if (ret <0 ) +return ret; +return IIO_VAL_INT ; +default : +return -EINVAL ; + } +case IIO_CHAN_INFO_SCALE : +/* For IIO_TEMP and IIO_MAGN, the result is in milligauss +and millicelcius so the scale is the same. */ Comment slightly confuses me. Is the point that raw values for bother are in micro units so we need to divide by 1000 to correct that?
If so a fraction type might be easier to read.
Multiline comment syntax is.
/* * For IIO_TEM... * and milice... */Quoted 33 lines
drivers/iio/magnetometer/mmc5603.c new file +33
-0+switch (chan->type) { +case IIO_MAGN : +case IIO_TEMP : + *val =0 ; + *val2 =1000 ; +return IIO_VAL_INT_PLUS_MICRO ; +default : +return -EINVAL ; + } + +case IIO_CHAN_INFO_SAMP_FREQ : +mutex_lock (&data->mutex); + ret =regmap_read (data->regmap,MMC5603_REG_CTRL1 , ®); +mutex_unlock (&data->mutex); + +if (ret <0 ) +return ret; + + i = (reg &MMC5603_CTRL1_BW_MASK ); +if (i <0 || i >=ARRAY_SIZE (mmc5603_samp_freq)) +return -EINVAL ; + + *val = mmc5603_samp_freq[i].val; + *val2 = mmc5603_samp_freq[i].val2; +return IIO_VAL_INT_PLUS_MICRO ; +default : +return -EINVAL ; + } +} + +static int mmc5603_write_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan,int val, +int val2,long mask)drivers/iio/magnetometer/mmc5603.c line 472
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+{ +struct mmc5603_data *data =iio_priv (indio_dev); +int i, ret; + +switch (mask) { +case IIO_CHAN_INFO_SAMP_FREQ : + i =mmc5603_get_samp_freq_index (data, val, val2); +if (i <0 ) +return -EINVAL ; +mutex_lock (&data->mutex);I'd add scope with {} around the whole case block and then use
guard(mutex)(&data->mutex);make sure to include cleanuph if you aren't already.
The advantage here is that you can then do
returnregmap_update_bits...Quoted 15 lines
drivers/iio/magnetometer/mmc5603.c new file +15
-0+ ret =regmap_update_bits (data->regmap,MMC5603_REG_CTRL1 , +MMC5603_CTRL1_BW_MASK , + i <<MMC5603_CTRL1_BW_SHIFT ); +mutex_unlock (&data->mutex); +return ret; +default : +return -EINVAL ; + } +} + +static const struct iio_info mmc5603_info = { + .read_raw = mmc5603_read_raw, + .write_raw = mmc5603_write_raw, + .attrs = &mmc5603_attribute_group +};drivers/iio/magnetometer/mmc5603.c line 546
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +9
-0+ +static const struct regmap_config mmc5603_regmap_config = { + .name =MMC5603_REGMAP_NAME , + + .reg_bits =8 , + .val_bits =8 , + + .max_register =MMC5603_REG_ID , + .cache_type =REGCACHE_FLAT ,Generally just use the maple tree one unless very strong reason to do otherwise. It's pretty efficient event for dense cases like I guess you have here.
Quoted 12 lines
drivers/iio/magnetometer/mmc5603.c new file +12
-0+ + .writeable_reg = mmc5603_is_writeable_reg, + .readable_reg = mmc5603_is_readable_reg, + .volatile_reg = mmc5603_is_volatile_reg, + + .reg_defaults = mmc5603_reg_defaults, + .num_reg_defaults = ARRAY_SIZE(mmc5603_reg_defaults), +}; + +static int mmc5603_probe (struct i2c_client *client) { +struct mmc5603_data *data; +struct iio_dev *indio_dev;drivers/iio/magnetometer/mmc5603.c line 568
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+struct regmap *regmap; +int ret; + + indio_dev =devm_iio_device_alloc (&client->dev,sizeof (*data)); +if (!indio_dev) +return -ENOMEM ; + + regmap =devm_regmap_init_i2c (client, &mmc5603_regmap_config); +if (IS_ERR (regmap)) { +dev_err (&client->dev,"regmap initialization failed\n" );For all error messages in probe and functions called from probe only use
return dev_err_probe(...)Might be worth
struct device *dev = &client->dev;just to shorten a few lines.
drivers/iio/magnetometer/mmc5603.c line 573
Quoted 5 lines
drivers/iio/magnetometer/mmc5603.c new file +5
-0+return PTR_ERR (regmap); + } + + data =iio_priv (indio_dev); +i2c_set_clientdata (client, indio_dev);Is this used? Normally only needed if we have some pm callbacks around.
drivers/iio/magnetometer/mmc5603.c line 578
Quoted 5 lines
drivers/iio/magnetometer/mmc5603.c new file +5
-0+ data->client = client; + data->regmap = regmap; + data->res =MMC5603_20_BITS ; + +mutex_init (&data->mutex);For new code I prefer
ret = devm_mutex_init(&data->mutex); if (ret) return ret;Brings only a minor advantage for debugging some use after free conditions but it's cheap so why not?
drivers/iio/magnetometer/mmc5603.c line 588
Quoted 10 lines
drivers/iio/magnetometer/mmc5603.c new file +10
-0+ + indio_dev->info = &mmc5603_info; + indio_dev->name =MMC5603_DRV_NAME ; + indio_dev->channels = mmc5603_channels; + indio_dev->num_channels =ARRAY_SIZE (mmc5603_channels); + indio_dev->modes =INDIO_DIRECT_MODE ; + + ret =mmc5603_init (data); +if (ret <0 ) { +dev_err (&client->dev,"mmc5603 chip init failed\n" );Another return
dev_err_probe()drivers/iio/magnetometer/mmc5603.c line 594
Quoted 6 lines
drivers/iio/magnetometer/mmc5603.c new file +6
-0+return ret; + } +return devm_iio_device_register (&client->dev, indio_dev); +} + +// Device tree matching As below.
drivers/iio/magnetometer/mmc5603.c line 601
Quoted 7 lines
drivers/iio/magnetometer/mmc5603.c new file +7
-0+static const struct of_device_id mmc5603_of_match[] = { + { .compatible ="memsic,mmc5603" , }, + { } +}; +MODULE_DEVICE_TABLE (of, mmc5603_of_match); + +// ACPI matching As below.
drivers/iio/magnetometer/mmc5603.c line 608
Quoted 7 lines
drivers/iio/magnetometer/mmc5603.c new file +7
-0+static const struct acpi_device_id mmc5603_acpi_match[] = { + {"MMC5603" ,0 }, + { } +}; +MODULE_DEVICE_TABLE (acpi, mmc5603_acpi_match); + +// Raw I2C matching Comment unnecessary and for IIO at least stick to traditional C comments except where it's an SPDX tag and slightly different rules apply
/* Raw I2C matching */
Quoted 21 lines
drivers/iio/magnetometer/mmc5603.c new file +21
-0+static const struct i2c_device_id mmc5603_id[] = { + {"mmc5603" }, + { } +}; +MODULE_DEVICE_TABLE (i2c, mmc5603_id); + +static struct i2c_driver mmc5603_driver = { + .driver = { + .name =MMC5603_DRV_NAME , + .of_match_table = mmc5603_of_match, + .acpi_match_table = mmc5603_acpi_match, + }, + .probe = mmc5603_probe, + .id_table = mmc5603_id, +}; + +module_i2c_driver (mmc5603_driver); + +MODULE_AUTHOR ("Adrian Fluturel <[email protected]>" ); +MODULE_DESCRIPTION ("MEMSIC MMC5603 magnetic sensor driver" ); +MODULE_LICENSE ("GPL v2" );