Skip to content

Commit 243a738

Browse files
Vladislav Kulikovjic23
authored andcommitted
iio: magnetometer: add driver for MEMSIC MMC5983MA
Add support for the MEMSIC MMC5983MA 3-axis magnetometer. The driver provides raw magnetic field readings via IIO sysfs with SET/RESET offset cancellation for each measurement. Reviewed-by: David Lechner <dlechner@baylibre.com> Signed-off-by: Vladislav Kulikov <vlad.kulikov.c@gmail.com> Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
1 parent ecedfa4 commit 243a738

4 files changed

Lines changed: 361 additions & 0 deletions

File tree

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17184,6 +17184,7 @@ M: Vladislav Kulikov <vlad.kulikov.c@gmail.com>
1718417184
L: linux-iio@vger.kernel.org
1718517185
S: Maintained
1718617186
F: Documentation/devicetree/bindings/iio/magnetometer/memsic,mmc5983.yaml
17187+
F: drivers/iio/magnetometer/mmc5983.c
1718717188

1718817189
MEN A21 WATCHDOG DRIVER
1718917190
M: Johannes Thumshirn <morbidrsa@gmail.com>

drivers/iio/magnetometer/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,17 @@ config MMC5633
151151
To compile this driver as a module, choose M here: the module
152152
will be called mmc5633
153153

154+
config MMC5983
155+
tristate "MEMSIC MMC5983 3-axis magnetic sensor"
156+
depends on I2C
157+
select REGMAP_I2C
158+
help
159+
Say yes here to build support for the MEMSIC MMC5983 3-axis
160+
magnetic sensor.
161+
162+
To compile this driver as a module, choose M here: the module
163+
will be called mmc5983
164+
154165
config IIO_ST_MAGN_3AXIS
155166
tristate "STMicroelectronics magnetometers 3-Axis Driver"
156167
depends on (I2C || SPI_MASTER) && SYSFS

drivers/iio/magnetometer/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ obj-$(CONFIG_MAG3110) += mag3110.o
1616
obj-$(CONFIG_HID_SENSOR_MAGNETOMETER_3D) += hid-sensor-magn-3d.o
1717
obj-$(CONFIG_MMC35240) += mmc35240.o
1818
obj-$(CONFIG_MMC5633) += mmc5633.o
19+
obj-$(CONFIG_MMC5983) += mmc5983.o
1920

2021
obj-$(CONFIG_IIO_ST_MAGN_3AXIS) += st_magn.o
2122
st_magn-y := st_magn_core.o

drivers/iio/magnetometer/mmc5983.c

Lines changed: 348 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,348 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* MMC5983 - MEMSIC 3-axis Magnetic Sensor
4+
*
5+
* Copyright (c) 2026, Vlad Kulikov <vlad.kulikov.c@gmail.com>
6+
*
7+
* IIO driver for MMC5983
8+
*/
9+
10+
#include <linux/array_size.h>
11+
#include <linux/bits.h>
12+
#include <linux/cleanup.h>
13+
#include <linux/delay.h>
14+
#include <linux/dev_printk.h>
15+
#include <linux/err.h>
16+
#include <linux/i2c.h>
17+
#include <linux/iio/iio.h>
18+
#include <linux/mod_devicetable.h>
19+
#include <linux/module.h>
20+
#include <linux/mutex.h>
21+
#include <linux/regmap.h>
22+
#include <linux/time.h>
23+
#include <linux/types.h>
24+
25+
#define MMC5983_REG_XOUT0 0x00
26+
#define MMC5983_REG_XOUT1 0x01
27+
#define MMC5983_REG_YOUT0 0x02
28+
#define MMC5983_REG_YOUT1 0x03
29+
#define MMC5983_REG_ZOUT0 0x04
30+
#define MMC5983_REG_ZOUT1 0x05
31+
#define MMC5983_REG_XYZOUT2 0x06
32+
33+
#define MMC5983_REG_STATUS 0x08
34+
35+
#define MMC5983_REG_CTRL0 0x09
36+
#define MMC5983_REG_CTRL1 0x0A
37+
#define MMC5983_REG_CTRL2 0x0B
38+
#define MMC5983_REG_CTRL3 0x0C
39+
40+
#define MMC5983_REG_ID 0x2F
41+
42+
#define MMC5983_PRODUCT_ID 0x30
43+
44+
#define MMC5983_STATUS_MEAS_M_DONE_BIT BIT(0)
45+
#define MMC5983_STATUS_OTP_RD_DONE_BIT BIT(4)
46+
47+
#define MMC5983_CTRL0_TM_M_BIT BIT(0)
48+
#define MMC5983_CTRL0_SET_BIT BIT(3)
49+
#define MMC5983_CTRL0_RESET_BIT BIT(4)
50+
#define MMC5983_CTRL0_OTP_RD_BIT BIT(6)
51+
52+
#define MMC5983_CTRL1_SW_RST_BIT BIT(7)
53+
54+
enum mmc5983_axis {
55+
MMC5983_AXIS_X,
56+
MMC5983_AXIS_Y,
57+
MMC5983_AXIS_Z,
58+
};
59+
60+
struct mmc5983_data {
61+
struct regmap *regmap;
62+
/* Protects chip access during SET/RESET measurement sequence */
63+
struct mutex mutex;
64+
};
65+
66+
#define MMC5983_CHANNEL(_axis) { \
67+
.type = IIO_MAGN, \
68+
.modified = 1, \
69+
.channel2 = IIO_MOD_##_axis, \
70+
.address = MMC5983_AXIS_##_axis, \
71+
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
72+
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
73+
}
74+
75+
static const struct iio_chan_spec mmc5983_channels[] = {
76+
MMC5983_CHANNEL(X),
77+
MMC5983_CHANNEL(Y),
78+
MMC5983_CHANNEL(Z),
79+
};
80+
81+
static int mmc5983_pulse_coil(struct mmc5983_data *data, unsigned int coil_bit)
82+
{
83+
int ret;
84+
85+
ret = regmap_write(data->regmap, MMC5983_REG_CTRL0, coil_bit);
86+
if (ret)
87+
return ret;
88+
89+
/*
90+
* Datasheet page 15: SET/RESET coil pulse is 500 ns.
91+
* Vendor sample code waits 500 us before the next operation.
92+
*/
93+
fsleep(500);
94+
95+
return 0;
96+
}
97+
98+
static int mmc5983_take_measurement(struct mmc5983_data *data, int m[3])
99+
{
100+
unsigned int status;
101+
u8 buf[7];
102+
int ret;
103+
104+
ret = regmap_write(data->regmap, MMC5983_REG_CTRL0,
105+
MMC5983_CTRL0_TM_M_BIT);
106+
if (ret)
107+
return ret;
108+
109+
/*
110+
* Datasheet page 15: measurement time is 8 ms at BW=00 (default,
111+
* slowest setting). Use a 50 ms timeout for margin.
112+
*/
113+
ret = regmap_read_poll_timeout(data->regmap, MMC5983_REG_STATUS,
114+
status,
115+
status & MMC5983_STATUS_MEAS_M_DONE_BIT,
116+
10 * USEC_PER_MSEC,
117+
50 * USEC_PER_MSEC);
118+
if (ret)
119+
return ret;
120+
121+
ret = regmap_bulk_read(data->regmap, MMC5983_REG_XOUT0, buf,
122+
sizeof(buf));
123+
if (ret)
124+
return ret;
125+
126+
m[0] = (buf[0] << 10) | (buf[1] << 2) | ((buf[6] >> 6) & 0x3);
127+
m[1] = (buf[2] << 10) | (buf[3] << 2) | ((buf[6] >> 4) & 0x3);
128+
m[2] = (buf[4] << 10) | (buf[5] << 2) | ((buf[6] >> 2) & 0x3);
129+
130+
return 0;
131+
}
132+
133+
static int mmc5983_read_raw(struct iio_dev *indio_dev,
134+
const struct iio_chan_spec *chan, int *val,
135+
int *val2, long mask)
136+
{
137+
struct mmc5983_data *data = iio_priv(indio_dev);
138+
int m1[3], m2[3];
139+
int ret;
140+
141+
switch (mask) {
142+
case IIO_CHAN_INFO_RAW: {
143+
guard(mutex)(&data->mutex);
144+
145+
/* SET: magnetize sensor elements in forward direction */
146+
ret = mmc5983_pulse_coil(data, MMC5983_CTRL0_SET_BIT);
147+
if (ret)
148+
return ret;
149+
150+
ret = mmc5983_take_measurement(data, m1);
151+
if (ret)
152+
return ret;
153+
154+
/* RESET: magnetize sensor elements in reverse direction */
155+
ret = mmc5983_pulse_coil(data, MMC5983_CTRL0_RESET_BIT);
156+
if (ret)
157+
return ret;
158+
159+
ret = mmc5983_take_measurement(data, m2);
160+
if (ret)
161+
return ret;
162+
163+
*val = (m1[chan->address] - m2[chan->address]) / 2;
164+
return IIO_VAL_INT;
165+
}
166+
case IIO_CHAN_INFO_SCALE:
167+
*val = 0;
168+
*val2 = 61035;
169+
return IIO_VAL_INT_PLUS_NANO;
170+
default:
171+
return -EINVAL;
172+
}
173+
}
174+
175+
static const struct iio_info mmc5983_info = {
176+
.read_raw = mmc5983_read_raw,
177+
};
178+
179+
static bool mmc5983_is_writeable_reg(struct device *dev, unsigned int reg)
180+
{
181+
switch (reg) {
182+
case MMC5983_REG_CTRL0:
183+
case MMC5983_REG_CTRL1:
184+
case MMC5983_REG_CTRL2:
185+
case MMC5983_REG_CTRL3:
186+
return true;
187+
default:
188+
return false;
189+
}
190+
}
191+
192+
static bool mmc5983_is_readable_reg(struct device *dev, unsigned int reg)
193+
{
194+
switch (reg) {
195+
case MMC5983_REG_XOUT0:
196+
case MMC5983_REG_XOUT1:
197+
case MMC5983_REG_YOUT0:
198+
case MMC5983_REG_YOUT1:
199+
case MMC5983_REG_ZOUT0:
200+
case MMC5983_REG_ZOUT1:
201+
case MMC5983_REG_XYZOUT2:
202+
case MMC5983_REG_STATUS:
203+
case MMC5983_REG_CTRL0:
204+
case MMC5983_REG_CTRL1:
205+
case MMC5983_REG_CTRL2:
206+
case MMC5983_REG_CTRL3:
207+
case MMC5983_REG_ID:
208+
return true;
209+
default:
210+
return false;
211+
}
212+
}
213+
214+
static bool mmc5983_is_volatile_reg(struct device *dev, unsigned int reg)
215+
{
216+
switch (reg) {
217+
case MMC5983_REG_XOUT0:
218+
case MMC5983_REG_XOUT1:
219+
case MMC5983_REG_YOUT0:
220+
case MMC5983_REG_YOUT1:
221+
case MMC5983_REG_ZOUT0:
222+
case MMC5983_REG_ZOUT1:
223+
case MMC5983_REG_XYZOUT2:
224+
case MMC5983_REG_STATUS:
225+
case MMC5983_REG_CTRL0:
226+
case MMC5983_REG_CTRL1:
227+
return true;
228+
default:
229+
return false;
230+
}
231+
}
232+
233+
static const struct regmap_config mmc5983_regmap_config = {
234+
.name = "mmc5983_regmap",
235+
.reg_bits = 8,
236+
.val_bits = 8,
237+
.max_register = MMC5983_REG_ID,
238+
.writeable_reg = mmc5983_is_writeable_reg,
239+
.readable_reg = mmc5983_is_readable_reg,
240+
.volatile_reg = mmc5983_is_volatile_reg,
241+
};
242+
243+
static int mmc5983_init(struct mmc5983_data *data)
244+
{
245+
struct regmap *regmap = data->regmap;
246+
struct device *dev = regmap_get_device(regmap);
247+
unsigned int reg_id, status;
248+
int ret;
249+
250+
ret = regmap_read(regmap, MMC5983_REG_ID, &reg_id);
251+
if (ret)
252+
return dev_err_probe(dev, ret, "Error reading product id\n");
253+
254+
if (reg_id != MMC5983_PRODUCT_ID)
255+
dev_info(dev, "unexpected product id 0x%02x\n", reg_id);
256+
257+
ret = regmap_write(regmap, MMC5983_REG_CTRL1, MMC5983_CTRL1_SW_RST_BIT);
258+
if (ret)
259+
return ret;
260+
261+
/* Datasheet page 15: power-on time after SW_RST is 10 ms */
262+
fsleep(10 * USEC_PER_MSEC);
263+
264+
ret = regmap_write(regmap, MMC5983_REG_CTRL0, MMC5983_CTRL0_OTP_RD_BIT);
265+
if (ret)
266+
return ret;
267+
268+
/*
269+
* Datasheet page 15: OTP read completes and self-clears. No separate
270+
* OTP refresh timeout is specified, so use the 10 ms power-on time as
271+
* a conservative upper bound.
272+
*/
273+
ret = regmap_read_poll_timeout(regmap, MMC5983_REG_STATUS, status,
274+
status & MMC5983_STATUS_OTP_RD_DONE_BIT,
275+
USEC_PER_MSEC,
276+
10 * USEC_PER_MSEC);
277+
if (ret)
278+
return ret;
279+
280+
/* SET: magnetize sensor elements in forward direction */
281+
ret = mmc5983_pulse_coil(data, MMC5983_CTRL0_SET_BIT);
282+
if (ret)
283+
return ret;
284+
285+
/* RESET: magnetize sensor elements in reverse direction */
286+
return mmc5983_pulse_coil(data, MMC5983_CTRL0_RESET_BIT);
287+
}
288+
289+
static int mmc5983_probe(struct i2c_client *i2c)
290+
{
291+
struct device *dev = &i2c->dev;
292+
struct mmc5983_data *data;
293+
struct iio_dev *indio_dev;
294+
int ret;
295+
296+
indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
297+
if (!indio_dev)
298+
return -ENOMEM;
299+
300+
data = iio_priv(indio_dev);
301+
302+
ret = devm_mutex_init(dev, &data->mutex);
303+
if (ret)
304+
return ret;
305+
306+
data->regmap = devm_regmap_init_i2c(i2c, &mmc5983_regmap_config);
307+
if (IS_ERR(data->regmap))
308+
return dev_err_probe(dev, PTR_ERR(data->regmap),
309+
"failed to allocate register map\n");
310+
311+
indio_dev->info = &mmc5983_info;
312+
indio_dev->modes = INDIO_DIRECT_MODE;
313+
indio_dev->name = "mmc5983";
314+
indio_dev->channels = mmc5983_channels;
315+
indio_dev->num_channels = ARRAY_SIZE(mmc5983_channels);
316+
317+
ret = mmc5983_init(data);
318+
if (ret)
319+
return dev_err_probe(dev, ret, "mmc5983 chip init failed\n");
320+
321+
return devm_iio_device_register(dev, indio_dev);
322+
}
323+
324+
static const struct of_device_id mmc5983_of_match[] = {
325+
{ .compatible = "memsic,mmc5983" },
326+
{ }
327+
};
328+
MODULE_DEVICE_TABLE(of, mmc5983_of_match);
329+
330+
static const struct i2c_device_id mmc5983_id[] = {
331+
{ "mmc5983" },
332+
{ }
333+
};
334+
MODULE_DEVICE_TABLE(i2c, mmc5983_id);
335+
336+
static struct i2c_driver mmc5983_driver = {
337+
.driver = {
338+
.name = "mmc5983",
339+
.of_match_table = mmc5983_of_match,
340+
},
341+
.probe = mmc5983_probe,
342+
.id_table = mmc5983_id,
343+
};
344+
module_i2c_driver(mmc5983_driver);
345+
346+
MODULE_AUTHOR("Vladislav Kulikov <vlad.kulikov.c@gmail.com>");
347+
MODULE_DESCRIPTION("MEMSIC MMC5983 magnetic sensor driver");
348+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)