1 // SPDX-License-Identifier: GPL-2.0-only
3 * mag3110.c - Support for Freescale MAG3110 magnetometer sensor
5 * Copyright (c) 2013 Peter Meerwald <pmeerw@pmeerw.net>
7 * (7-bit I2C slave address 0x0e)
9 * TODO: irq, user offset, oversampling, continuous mode
12 #include <linux/module.h>
13 #include <linux/i2c.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/sysfs.h>
16 #include <linux/iio/trigger_consumer.h>
17 #include <linux/iio/buffer.h>
18 #include <linux/iio/triggered_buffer.h>
19 #include <linux/delay.h>
20 #include <linux/regulator/consumer.h>
22 #define MAG3110_STATUS 0x00
23 #define MAG3110_OUT_X 0x01 /* MSB first */
24 #define MAG3110_OUT_Y 0x03
25 #define MAG3110_OUT_Z 0x05
26 #define MAG3110_WHO_AM_I 0x07
27 #define MAG3110_SYSMOD 0x08
28 #define MAG3110_OFF_X 0x09 /* MSB first */
29 #define MAG3110_OFF_Y 0x0b
30 #define MAG3110_OFF_Z 0x0d
31 #define MAG3110_DIE_TEMP 0x0f
32 #define MAG3110_CTRL_REG1 0x10
33 #define MAG3110_CTRL_REG2 0x11
35 #define MAG3110_STATUS_DRDY (BIT(2) | BIT(1) | BIT(0))
37 #define MAG3110_CTRL_DR_MASK (BIT(7) | BIT(6) | BIT(5))
38 #define MAG3110_CTRL_DR_SHIFT 5
39 #define MAG3110_CTRL_DR_DEFAULT 0
41 #define MAG3110_SYSMOD_MODE_MASK GENMASK(1, 0)
43 #define MAG3110_CTRL_TM BIT(1) /* trigger single measurement */
44 #define MAG3110_CTRL_AC BIT(0) /* continuous measurements */
46 #define MAG3110_CTRL_AUTO_MRST_EN BIT(7) /* magnetic auto-reset */
47 #define MAG3110_CTRL_RAW BIT(5) /* measurements not user-offset corrected */
49 #define MAG3110_DEVICE_ID 0xc4
51 /* Each client has this additional data */
53 struct i2c_client
*client
;
57 struct regulator
*vdd_reg
;
58 struct regulator
*vddio_reg
;
59 /* Ensure natural alignment of timestamp */
67 static int mag3110_request(struct mag3110_data
*data
)
71 if ((data
->ctrl_reg1
& MAG3110_CTRL_AC
) == 0) {
72 /* trigger measurement */
73 ret
= i2c_smbus_write_byte_data(data
->client
, MAG3110_CTRL_REG1
,
74 data
->ctrl_reg1
| MAG3110_CTRL_TM
);
80 ret
= i2c_smbus_read_byte_data(data
->client
, MAG3110_STATUS
);
83 /* wait for data ready */
84 if ((ret
& MAG3110_STATUS_DRDY
) == MAG3110_STATUS_DRDY
)
87 if (data
->sleep_val
<= 20)
88 usleep_range(data
->sleep_val
* 250, data
->sleep_val
* 500);
94 dev_err(&data
->client
->dev
, "data not ready\n");
101 static int mag3110_read(struct mag3110_data
*data
, __be16 buf
[3])
105 mutex_lock(&data
->lock
);
106 ret
= mag3110_request(data
);
108 mutex_unlock(&data
->lock
);
111 ret
= i2c_smbus_read_i2c_block_data(data
->client
,
112 MAG3110_OUT_X
, 3 * sizeof(__be16
), (u8
*) buf
);
113 mutex_unlock(&data
->lock
);
118 static ssize_t
mag3110_show_int_plus_micros(char *buf
,
119 const int (*vals
)[2], int n
)
124 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
125 "%d.%06d ", vals
[n
][0], vals
[n
][1]);
127 /* replace trailing space by newline */
133 static int mag3110_get_int_plus_micros_index(const int (*vals
)[2], int n
,
137 if (val
== vals
[n
][0] && val2
== vals
[n
][1])
143 static const int mag3110_samp_freq
[8][2] = {
144 {80, 0}, {40, 0}, {20, 0}, {10, 0}, {5, 0}, {2, 500000},
145 {1, 250000}, {0, 625000}
148 static ssize_t
mag3110_show_samp_freq_avail(struct device
*dev
,
149 struct device_attribute
*attr
, char *buf
)
151 return mag3110_show_int_plus_micros(buf
, mag3110_samp_freq
, 8);
154 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(mag3110_show_samp_freq_avail
);
156 static int mag3110_get_samp_freq_index(struct mag3110_data
*data
,
159 return mag3110_get_int_plus_micros_index(mag3110_samp_freq
, 8, val
,
163 static int mag3110_calculate_sleep(struct mag3110_data
*data
)
165 int ret
, i
= data
->ctrl_reg1
>> MAG3110_CTRL_DR_SHIFT
;
167 if (mag3110_samp_freq
[i
][0] > 0)
168 ret
= 1000 / mag3110_samp_freq
[i
][0];
172 return ret
== 0 ? 1 : ret
;
175 static int mag3110_standby(struct mag3110_data
*data
)
177 return i2c_smbus_write_byte_data(data
->client
, MAG3110_CTRL_REG1
,
178 data
->ctrl_reg1
& ~MAG3110_CTRL_AC
);
181 static int mag3110_wait_standby(struct mag3110_data
*data
)
186 * Takes up to 1/ODR to come out of active mode into stby
187 * Longest expected period is 12.5seconds.
188 * We'll sleep for 500ms between checks
190 while (tries
-- > 0) {
191 ret
= i2c_smbus_read_byte_data(data
->client
, MAG3110_SYSMOD
);
193 dev_err(&data
->client
->dev
, "i2c error\n");
196 /* wait for standby */
197 if ((ret
& MAG3110_SYSMOD_MODE_MASK
) == 0)
200 msleep_interruptible(500);
204 dev_err(&data
->client
->dev
, "device not entering standby mode\n");
211 static int mag3110_active(struct mag3110_data
*data
)
213 return i2c_smbus_write_byte_data(data
->client
, MAG3110_CTRL_REG1
,
217 /* returns >0 if active, 0 if in standby and <0 on error */
218 static int mag3110_is_active(struct mag3110_data
*data
)
222 reg
= i2c_smbus_read_byte_data(data
->client
, MAG3110_CTRL_REG1
);
226 return reg
& MAG3110_CTRL_AC
;
229 static int mag3110_change_config(struct mag3110_data
*data
, u8 reg
, u8 val
)
234 mutex_lock(&data
->lock
);
236 is_active
= mag3110_is_active(data
);
242 /* config can only be changed when in standby */
244 ret
= mag3110_standby(data
);
250 * After coming out of active we must wait for the part
251 * to transition to STBY. This can take up to 1 /ODR to occur
253 ret
= mag3110_wait_standby(data
);
257 ret
= i2c_smbus_write_byte_data(data
->client
, reg
, val
);
262 ret
= mag3110_active(data
);
269 mutex_unlock(&data
->lock
);
274 static int mag3110_read_raw(struct iio_dev
*indio_dev
,
275 struct iio_chan_spec
const *chan
,
276 int *val
, int *val2
, long mask
)
278 struct mag3110_data
*data
= iio_priv(indio_dev
);
283 case IIO_CHAN_INFO_RAW
:
284 ret
= iio_device_claim_direct_mode(indio_dev
);
288 switch (chan
->type
) {
289 case IIO_MAGN
: /* in 0.1 uT / LSB */
290 ret
= mag3110_read(data
, buffer
);
293 *val
= sign_extend32(
294 be16_to_cpu(buffer
[chan
->scan_index
]), 15);
297 case IIO_TEMP
: /* in 1 C / LSB */
298 mutex_lock(&data
->lock
);
299 ret
= mag3110_request(data
);
301 mutex_unlock(&data
->lock
);
304 ret
= i2c_smbus_read_byte_data(data
->client
,
306 mutex_unlock(&data
->lock
);
309 *val
= sign_extend32(ret
, 7);
316 iio_device_release_direct_mode(indio_dev
);
319 case IIO_CHAN_INFO_SCALE
:
320 switch (chan
->type
) {
324 return IIO_VAL_INT_PLUS_MICRO
;
331 case IIO_CHAN_INFO_SAMP_FREQ
:
332 i
= data
->ctrl_reg1
>> MAG3110_CTRL_DR_SHIFT
;
333 *val
= mag3110_samp_freq
[i
][0];
334 *val2
= mag3110_samp_freq
[i
][1];
335 return IIO_VAL_INT_PLUS_MICRO
;
336 case IIO_CHAN_INFO_CALIBBIAS
:
337 ret
= i2c_smbus_read_word_swapped(data
->client
,
338 MAG3110_OFF_X
+ 2 * chan
->scan_index
);
341 *val
= sign_extend32(ret
>> 1, 14);
347 static int mag3110_write_raw(struct iio_dev
*indio_dev
,
348 struct iio_chan_spec
const *chan
,
349 int val
, int val2
, long mask
)
351 struct mag3110_data
*data
= iio_priv(indio_dev
);
354 ret
= iio_device_claim_direct_mode(indio_dev
);
359 case IIO_CHAN_INFO_SAMP_FREQ
:
360 rate
= mag3110_get_samp_freq_index(data
, val
, val2
);
365 data
->ctrl_reg1
&= 0xff & ~MAG3110_CTRL_DR_MASK
367 data
->ctrl_reg1
|= rate
<< MAG3110_CTRL_DR_SHIFT
;
368 data
->sleep_val
= mag3110_calculate_sleep(data
);
369 if (data
->sleep_val
< 40)
370 data
->ctrl_reg1
|= MAG3110_CTRL_AC
;
372 ret
= mag3110_change_config(data
, MAG3110_CTRL_REG1
,
375 case IIO_CHAN_INFO_CALIBBIAS
:
376 if (val
< -10000 || val
> 10000) {
380 ret
= i2c_smbus_write_word_swapped(data
->client
,
381 MAG3110_OFF_X
+ 2 * chan
->scan_index
, val
<< 1);
387 iio_device_release_direct_mode(indio_dev
);
391 static irqreturn_t
mag3110_trigger_handler(int irq
, void *p
)
393 struct iio_poll_func
*pf
= p
;
394 struct iio_dev
*indio_dev
= pf
->indio_dev
;
395 struct mag3110_data
*data
= iio_priv(indio_dev
);
398 ret
= mag3110_read(data
, data
->scan
.channels
);
402 if (test_bit(3, indio_dev
->active_scan_mask
)) {
403 ret
= i2c_smbus_read_byte_data(data
->client
,
407 data
->scan
.temperature
= ret
;
410 iio_push_to_buffers_with_timestamp(indio_dev
, &data
->scan
,
411 iio_get_time_ns(indio_dev
));
414 iio_trigger_notify_done(indio_dev
->trig
);
418 #define MAG3110_CHANNEL(axis, idx) { \
421 .channel2 = IIO_MOD_##axis, \
422 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
423 BIT(IIO_CHAN_INFO_CALIBBIAS), \
424 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
425 BIT(IIO_CHAN_INFO_SCALE), \
431 .endianness = IIO_BE, \
435 static const struct iio_chan_spec mag3110_channels
[] = {
436 MAG3110_CHANNEL(X
, 0),
437 MAG3110_CHANNEL(Y
, 1),
438 MAG3110_CHANNEL(Z
, 2),
441 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
442 BIT(IIO_CHAN_INFO_SCALE
),
450 IIO_CHAN_SOFT_TIMESTAMP(4),
453 static struct attribute
*mag3110_attributes
[] = {
454 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
458 static const struct attribute_group mag3110_group
= {
459 .attrs
= mag3110_attributes
,
462 static const struct iio_info mag3110_info
= {
463 .attrs
= &mag3110_group
,
464 .read_raw
= &mag3110_read_raw
,
465 .write_raw
= &mag3110_write_raw
,
468 static const unsigned long mag3110_scan_masks
[] = {0x7, 0xf, 0};
470 static int mag3110_probe(struct i2c_client
*client
,
471 const struct i2c_device_id
*id
)
473 struct mag3110_data
*data
;
474 struct iio_dev
*indio_dev
;
477 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
481 data
= iio_priv(indio_dev
);
483 data
->vdd_reg
= devm_regulator_get(&client
->dev
, "vdd");
484 if (IS_ERR(data
->vdd_reg
))
485 return dev_err_probe(&client
->dev
, PTR_ERR(data
->vdd_reg
),
486 "failed to get VDD regulator!\n");
488 data
->vddio_reg
= devm_regulator_get(&client
->dev
, "vddio");
489 if (IS_ERR(data
->vddio_reg
))
490 return dev_err_probe(&client
->dev
, PTR_ERR(data
->vddio_reg
),
491 "failed to get VDDIO regulator!\n");
493 ret
= regulator_enable(data
->vdd_reg
);
495 dev_err(&client
->dev
, "failed to enable VDD regulator!\n");
499 ret
= regulator_enable(data
->vddio_reg
);
501 dev_err(&client
->dev
, "failed to enable VDDIO regulator!\n");
502 goto disable_regulator_vdd
;
505 ret
= i2c_smbus_read_byte_data(client
, MAG3110_WHO_AM_I
);
507 goto disable_regulators
;
508 if (ret
!= MAG3110_DEVICE_ID
) {
510 goto disable_regulators
;
513 data
->client
= client
;
514 mutex_init(&data
->lock
);
516 i2c_set_clientdata(client
, indio_dev
);
517 indio_dev
->info
= &mag3110_info
;
518 indio_dev
->name
= id
->name
;
519 indio_dev
->modes
= INDIO_DIRECT_MODE
;
520 indio_dev
->channels
= mag3110_channels
;
521 indio_dev
->num_channels
= ARRAY_SIZE(mag3110_channels
);
522 indio_dev
->available_scan_masks
= mag3110_scan_masks
;
524 data
->ctrl_reg1
= MAG3110_CTRL_DR_DEFAULT
<< MAG3110_CTRL_DR_SHIFT
;
525 data
->sleep_val
= mag3110_calculate_sleep(data
);
526 if (data
->sleep_val
< 40)
527 data
->ctrl_reg1
|= MAG3110_CTRL_AC
;
529 ret
= mag3110_change_config(data
, MAG3110_CTRL_REG1
, data
->ctrl_reg1
);
531 goto disable_regulators
;
533 ret
= i2c_smbus_write_byte_data(client
, MAG3110_CTRL_REG2
,
534 MAG3110_CTRL_AUTO_MRST_EN
);
536 goto standby_on_error
;
538 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
539 mag3110_trigger_handler
, NULL
);
541 goto standby_on_error
;
543 ret
= iio_device_register(indio_dev
);
549 iio_triggered_buffer_cleanup(indio_dev
);
551 mag3110_standby(iio_priv(indio_dev
));
553 regulator_disable(data
->vddio_reg
);
554 disable_regulator_vdd
:
555 regulator_disable(data
->vdd_reg
);
560 static int mag3110_remove(struct i2c_client
*client
)
562 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
563 struct mag3110_data
*data
= iio_priv(indio_dev
);
565 iio_device_unregister(indio_dev
);
566 iio_triggered_buffer_cleanup(indio_dev
);
567 mag3110_standby(iio_priv(indio_dev
));
568 regulator_disable(data
->vddio_reg
);
569 regulator_disable(data
->vdd_reg
);
574 #ifdef CONFIG_PM_SLEEP
575 static int mag3110_suspend(struct device
*dev
)
577 struct mag3110_data
*data
= iio_priv(i2c_get_clientdata(
578 to_i2c_client(dev
)));
581 ret
= mag3110_standby(iio_priv(i2c_get_clientdata(
582 to_i2c_client(dev
))));
586 ret
= regulator_disable(data
->vddio_reg
);
588 dev_err(dev
, "failed to disable VDDIO regulator\n");
592 ret
= regulator_disable(data
->vdd_reg
);
594 dev_err(dev
, "failed to disable VDD regulator\n");
601 static int mag3110_resume(struct device
*dev
)
603 struct mag3110_data
*data
= iio_priv(i2c_get_clientdata(
604 to_i2c_client(dev
)));
607 ret
= regulator_enable(data
->vdd_reg
);
609 dev_err(dev
, "failed to enable VDD regulator\n");
613 ret
= regulator_enable(data
->vddio_reg
);
615 dev_err(dev
, "failed to enable VDDIO regulator\n");
616 regulator_disable(data
->vdd_reg
);
620 return i2c_smbus_write_byte_data(data
->client
, MAG3110_CTRL_REG1
,
624 static SIMPLE_DEV_PM_OPS(mag3110_pm_ops
, mag3110_suspend
, mag3110_resume
);
625 #define MAG3110_PM_OPS (&mag3110_pm_ops)
627 #define MAG3110_PM_OPS NULL
630 static const struct i2c_device_id mag3110_id
[] = {
634 MODULE_DEVICE_TABLE(i2c
, mag3110_id
);
636 static const struct of_device_id mag3110_of_match
[] = {
637 { .compatible
= "fsl,mag3110" },
640 MODULE_DEVICE_TABLE(of
, mag3110_of_match
);
642 static struct i2c_driver mag3110_driver
= {
645 .of_match_table
= mag3110_of_match
,
646 .pm
= MAG3110_PM_OPS
,
648 .probe
= mag3110_probe
,
649 .remove
= mag3110_remove
,
650 .id_table
= mag3110_id
,
652 module_i2c_driver(mag3110_driver
);
654 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
655 MODULE_DESCRIPTION("Freescale MAG3110 magnetometer driver");
656 MODULE_LICENSE("GPL");