2 * MMC35240 - MEMSIC 3-axis Magnetic Sensor
4 * Copyright (c) 2015, Intel Corporation.
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
10 * IIO driver for MMC35240 (7-bit I2C slave address 0x30).
12 * TODO: offset, ACPI, continuous measurement mode, PM
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/i2c.h>
18 #include <linux/delay.h>
19 #include <linux/regmap.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
25 #define MMC35240_DRV_NAME "mmc35240"
26 #define MMC35240_REGMAP_NAME "mmc35240_regmap"
28 #define MMC35240_REG_XOUT_L 0x00
29 #define MMC35240_REG_XOUT_H 0x01
30 #define MMC35240_REG_YOUT_L 0x02
31 #define MMC35240_REG_YOUT_H 0x03
32 #define MMC35240_REG_ZOUT_L 0x04
33 #define MMC35240_REG_ZOUT_H 0x05
35 #define MMC35240_REG_STATUS 0x06
36 #define MMC35240_REG_CTRL0 0x07
37 #define MMC35240_REG_CTRL1 0x08
39 #define MMC35240_REG_ID 0x20
41 #define MMC35240_STATUS_MEAS_DONE_BIT BIT(0)
43 #define MMC35240_CTRL0_REFILL_BIT BIT(7)
44 #define MMC35240_CTRL0_RESET_BIT BIT(6)
45 #define MMC35240_CTRL0_SET_BIT BIT(5)
46 #define MMC35240_CTRL0_CMM_BIT BIT(1)
47 #define MMC35240_CTRL0_TM_BIT BIT(0)
49 /* output resolution bits */
50 #define MMC35240_CTRL1_BW0_BIT BIT(0)
51 #define MMC35240_CTRL1_BW1_BIT BIT(1)
53 #define MMC35240_CTRL1_BW_MASK (MMC35240_CTRL1_BW0_BIT | \
54 MMC35240_CTRL1_BW1_BIT)
55 #define MMC35240_CTRL1_BW_SHIFT 0
57 #define MMC35240_WAIT_CHARGE_PUMP 50000 /* us */
58 #define MMC53240_WAIT_SET_RESET 1000 /* us */
60 enum mmc35240_resolution {
61 MMC35240_16_BITS_SLOW = 0, /* 100 Hz */
62 MMC35240_16_BITS_FAST, /* 200 Hz */
63 MMC35240_14_BITS, /* 333 Hz */
64 MMC35240_12_BITS, /* 666 Hz */
74 int sens[3]; /* sensitivity per X, Y, Z axis */
75 int nfo; /* null field output */
76 } mmc35240_props_table[] = {
77 /* 16 bits, 100Hz ODR */
82 /* 16 bits, 200Hz ODR */
87 /* 14 bits, 333Hz ODR */
92 /* 12 bits, 666Hz ODR */
99 struct mmc35240_data {
100 struct i2c_client *client;
102 struct regmap *regmap;
103 enum mmc35240_resolution res;
106 static const int mmc35240_samp_freq[] = {100, 200, 333, 666};
108 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("100 200 333 666");
110 #define MMC35240_CHANNEL(_axis) { \
113 .channel2 = IIO_MOD_ ## _axis, \
114 .address = AXIS_ ## _axis, \
115 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
116 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
119 static const struct iio_chan_spec mmc35240_channels[] = {
125 static struct attribute *mmc35240_attributes[] = {
126 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
129 static const struct attribute_group mmc35240_attribute_group = {
130 .attrs = mmc35240_attributes,
133 static int mmc35240_get_samp_freq_index(struct mmc35240_data *data,
138 for (i = 0; i < ARRAY_SIZE(mmc35240_samp_freq); i++)
139 if (mmc35240_samp_freq[i] == val)
144 static int mmc35240_hw_set(struct mmc35240_data *data, bool set)
150 * Recharge the capacitor at VCAP pin, requested to be issued
151 * before a SET/RESET command.
153 ret = regmap_update_bits(data->regmap, MMC35240_REG_CTRL0,
154 MMC35240_CTRL0_REFILL_BIT,
155 MMC35240_CTRL0_REFILL_BIT);
158 usleep_range(MMC35240_WAIT_CHARGE_PUMP, MMC35240_WAIT_CHARGE_PUMP + 1);
161 coil_bit = MMC35240_CTRL0_SET_BIT;
163 coil_bit = MMC35240_CTRL0_RESET_BIT;
165 return regmap_update_bits(data->regmap, MMC35240_REG_CTRL0,
166 MMC35240_CTRL0_REFILL_BIT,
170 static int mmc35240_init(struct mmc35240_data *data)
175 ret = regmap_read(data->regmap, MMC35240_REG_ID, ®_id);
177 dev_err(&data->client->dev, "Error reading product id\n");
181 dev_dbg(&data->client->dev, "MMC35240 chip id %x\n", reg_id);
184 * make sure we restore sensor characteristics, by doing
185 * a RESET/SET sequence
187 ret = mmc35240_hw_set(data, false);
190 usleep_range(MMC53240_WAIT_SET_RESET, MMC53240_WAIT_SET_RESET + 1);
192 ret = mmc35240_hw_set(data, true);
196 /* set default sampling frequency */
197 return regmap_update_bits(data->regmap, MMC35240_REG_CTRL1,
198 MMC35240_CTRL1_BW_MASK,
199 data->res << MMC35240_CTRL1_BW_SHIFT);
202 static int mmc35240_take_measurement(struct mmc35240_data *data)
204 int ret, tries = 100;
205 unsigned int reg_status;
207 ret = regmap_write(data->regmap, MMC35240_REG_CTRL0,
208 MMC35240_CTRL0_TM_BIT);
212 while (tries-- > 0) {
213 ret = regmap_read(data->regmap, MMC35240_REG_STATUS,
217 if (reg_status & MMC35240_STATUS_MEAS_DONE_BIT)
223 dev_err(&data->client->dev, "data not ready\n");
230 static int mmc35240_read_measurement(struct mmc35240_data *data, __le16 buf[3])
234 ret = mmc35240_take_measurement(data);
238 return regmap_bulk_read(data->regmap, MMC35240_REG_XOUT_L, (u8 *)buf,
242 static int mmc35240_raw_to_gauss(struct mmc35240_data *data, int index,
246 int raw_x, raw_y, raw_z;
247 int sens_x, sens_y, sens_z;
250 raw_x = le16_to_cpu(buf[AXIS_X]);
251 raw_y = le16_to_cpu(buf[AXIS_Y]);
252 raw_z = le16_to_cpu(buf[AXIS_Z]);
254 sens_x = mmc35240_props_table[data->res].sens[AXIS_X];
255 sens_y = mmc35240_props_table[data->res].sens[AXIS_Y];
256 sens_z = mmc35240_props_table[data->res].sens[AXIS_Z];
258 nfo = mmc35240_props_table[data->res].nfo;
262 *val = (raw_x - nfo) / sens_x;
263 *val2 = ((raw_x - nfo) % sens_x) * 1000000;
266 *val = (raw_y - nfo) / sens_y - (raw_z - nfo) / sens_z;
267 *val2 = (((raw_y - nfo) % sens_y - (raw_z - nfo) % sens_z))
271 *val = (raw_y - nfo) / sens_y + (raw_z - nfo) / sens_z;
272 *val2 = (((raw_y - nfo) % sens_y + (raw_z - nfo) % sens_z))
281 static int mmc35240_read_raw(struct iio_dev *indio_dev,
282 struct iio_chan_spec const *chan, int *val,
283 int *val2, long mask)
285 struct mmc35240_data *data = iio_priv(indio_dev);
291 case IIO_CHAN_INFO_PROCESSED:
292 mutex_lock(&data->mutex);
293 ret = mmc35240_read_measurement(data, buf);
294 mutex_unlock(&data->mutex);
297 ret = mmc35240_raw_to_gauss(data, chan->address,
301 return IIO_VAL_INT_PLUS_MICRO;
302 case IIO_CHAN_INFO_SAMP_FREQ:
303 mutex_lock(&data->mutex);
304 ret = regmap_read(data->regmap, MMC35240_REG_CTRL1, ®);
305 mutex_unlock(&data->mutex);
309 i = (reg & MMC35240_CTRL1_BW_MASK) >> MMC35240_CTRL1_BW_SHIFT;
310 if (i < 0 || i > ARRAY_SIZE(mmc35240_samp_freq))
313 *val = mmc35240_samp_freq[i];
321 static int mmc35240_write_raw(struct iio_dev *indio_dev,
322 struct iio_chan_spec const *chan, int val,
325 struct mmc35240_data *data = iio_priv(indio_dev);
329 case IIO_CHAN_INFO_SAMP_FREQ:
330 i = mmc35240_get_samp_freq_index(data, val, val2);
333 mutex_lock(&data->mutex);
334 ret = regmap_update_bits(data->regmap, MMC35240_REG_CTRL1,
335 MMC35240_CTRL1_BW_MASK,
336 i << MMC35240_CTRL1_BW_SHIFT);
337 mutex_unlock(&data->mutex);
344 static const struct iio_info mmc35240_info = {
345 .driver_module = THIS_MODULE,
346 .read_raw = mmc35240_read_raw,
347 .write_raw = mmc35240_write_raw,
348 .attrs = &mmc35240_attribute_group,
351 static bool mmc35240_is_writeable_reg(struct device *dev, unsigned int reg)
354 case MMC35240_REG_CTRL0:
355 case MMC35240_REG_CTRL1:
362 static bool mmc35240_is_readable_reg(struct device *dev, unsigned int reg)
365 case MMC35240_REG_XOUT_L:
366 case MMC35240_REG_XOUT_H:
367 case MMC35240_REG_YOUT_L:
368 case MMC35240_REG_YOUT_H:
369 case MMC35240_REG_ZOUT_L:
370 case MMC35240_REG_ZOUT_H:
371 case MMC35240_REG_STATUS:
372 case MMC35240_REG_ID:
379 static bool mmc35240_is_volatile_reg(struct device *dev, unsigned int reg)
382 case MMC35240_REG_CTRL0:
383 case MMC35240_REG_CTRL1:
390 static struct reg_default mmc35240_reg_defaults[] = {
391 { MMC35240_REG_CTRL0, 0x00 },
392 { MMC35240_REG_CTRL1, 0x00 },
395 static const struct regmap_config mmc35240_regmap_config = {
396 .name = MMC35240_REGMAP_NAME,
401 .max_register = MMC35240_REG_ID,
402 .cache_type = REGCACHE_FLAT,
404 .writeable_reg = mmc35240_is_writeable_reg,
405 .readable_reg = mmc35240_is_readable_reg,
406 .volatile_reg = mmc35240_is_volatile_reg,
408 .reg_defaults = mmc35240_reg_defaults,
409 .num_reg_defaults = ARRAY_SIZE(mmc35240_reg_defaults),
412 static int mmc35240_probe(struct i2c_client *client,
413 const struct i2c_device_id *id)
415 struct mmc35240_data *data;
416 struct iio_dev *indio_dev;
417 struct regmap *regmap;
420 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
424 regmap = devm_regmap_init_i2c(client, &mmc35240_regmap_config);
425 if (IS_ERR(regmap)) {
426 dev_err(&client->dev, "regmap initialization failed\n");
427 return PTR_ERR(regmap);
430 data = iio_priv(indio_dev);
431 data->client = client;
432 data->regmap = regmap;
433 data->res = MMC35240_16_BITS_SLOW;
435 mutex_init(&data->mutex);
437 indio_dev->dev.parent = &client->dev;
438 indio_dev->info = &mmc35240_info;
439 indio_dev->name = MMC35240_DRV_NAME;
440 indio_dev->channels = mmc35240_channels;
441 indio_dev->num_channels = ARRAY_SIZE(mmc35240_channels);
442 indio_dev->modes = INDIO_DIRECT_MODE;
444 ret = mmc35240_init(data);
446 dev_err(&client->dev, "mmc35240 chip init failed\n");
449 return devm_iio_device_register(&client->dev, indio_dev);
452 #ifdef CONFIG_PM_SLEEP
453 static int mmc35240_suspend(struct device *dev)
455 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
456 struct mmc35240_data *data = iio_priv(indio_dev);
458 regcache_cache_only(data->regmap, true);
463 static int mmc35240_resume(struct device *dev)
465 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
466 struct mmc35240_data *data = iio_priv(indio_dev);
469 regcache_mark_dirty(data->regmap);
470 ret = regcache_sync_region(data->regmap, MMC35240_REG_CTRL0,
473 dev_err(dev, "Failed to restore control registers\n");
475 regcache_cache_only(data->regmap, false);
481 static const struct dev_pm_ops mmc35240_pm_ops = {
482 SET_SYSTEM_SLEEP_PM_OPS(mmc35240_suspend, mmc35240_resume)
485 static const struct i2c_device_id mmc35240_id[] = {
489 MODULE_DEVICE_TABLE(i2c, mmc35240_id);
491 static struct i2c_driver mmc35240_driver = {
493 .name = MMC35240_DRV_NAME,
494 .pm = &mmc35240_pm_ops,
496 .probe = mmc35240_probe,
497 .id_table = mmc35240_id,
500 module_i2c_driver(mmc35240_driver);
502 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
503 MODULE_DESCRIPTION("MEMSIC MMC35240 magnetic sensor driver");
504 MODULE_LICENSE("GPL v2");