2 * mag3110.c - Support for Freescale MAG3110 magnetometer sensor
4 * Copyright (c) 2013 Peter Meerwald <pmeerw@pmeerw.net>
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 * (7-bit I2C slave address 0x0e)
12 * TODO: irq, user offset, oversampling, continuous mode
15 #include <linux/module.h>
16 #include <linux/i2c.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/sysfs.h>
19 #include <linux/iio/trigger_consumer.h>
20 #include <linux/iio/buffer.h>
21 #include <linux/iio/triggered_buffer.h>
22 #include <linux/delay.h>
24 #define MAG3110_STATUS 0x00
25 #define MAG3110_OUT_X 0x01 /* MSB first */
26 #define MAG3110_OUT_Y 0x03
27 #define MAG3110_OUT_Z 0x05
28 #define MAG3110_WHO_AM_I 0x07
29 #define MAG3110_OFF_X 0x09 /* MSB first */
30 #define MAG3110_OFF_Y 0x0b
31 #define MAG3110_OFF_Z 0x0d
32 #define MAG3110_DIE_TEMP 0x0f
33 #define MAG3110_CTRL_REG1 0x10
34 #define MAG3110_CTRL_REG2 0x11
36 #define MAG3110_STATUS_DRDY (BIT(2) | BIT(1) | BIT(0))
38 #define MAG3110_CTRL_DR_MASK (BIT(7) | BIT(6) | BIT(5))
39 #define MAG3110_CTRL_DR_SHIFT 5
40 #define MAG3110_CTRL_DR_DEFAULT 0
42 #define MAG3110_CTRL_TM BIT(1) /* trigger single measurement */
43 #define MAG3110_CTRL_AC BIT(0) /* continuous measurements */
45 #define MAG3110_CTRL_AUTO_MRST_EN BIT(7) /* magnetic auto-reset */
46 #define MAG3110_CTRL_RAW BIT(5) /* measurements not user-offset corrected */
48 #define MAG3110_DEVICE_ID 0xc4
50 /* Each client has this additional data */
52 struct i2c_client
*client
;
57 static int mag3110_request(struct mag3110_data
*data
)
61 /* trigger measurement */
62 ret
= i2c_smbus_write_byte_data(data
->client
, MAG3110_CTRL_REG1
,
63 data
->ctrl_reg1
| MAG3110_CTRL_TM
);
68 ret
= i2c_smbus_read_byte_data(data
->client
, MAG3110_STATUS
);
71 /* wait for data ready */
72 if ((ret
& MAG3110_STATUS_DRDY
) == MAG3110_STATUS_DRDY
)
78 dev_err(&data
->client
->dev
, "data not ready\n");
85 static int mag3110_read(struct mag3110_data
*data
, __be16 buf
[3])
89 mutex_lock(&data
->lock
);
90 ret
= mag3110_request(data
);
92 mutex_unlock(&data
->lock
);
95 ret
= i2c_smbus_read_i2c_block_data(data
->client
,
96 MAG3110_OUT_X
, 3 * sizeof(__be16
), (u8
*) buf
);
97 mutex_unlock(&data
->lock
);
102 static ssize_t
mag3110_show_int_plus_micros(char *buf
,
103 const int (*vals
)[2], int n
)
108 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
109 "%d.%06d ", vals
[n
][0], vals
[n
][1]);
111 /* replace trailing space by newline */
117 static int mag3110_get_int_plus_micros_index(const int (*vals
)[2], int n
,
121 if (val
== vals
[n
][0] && val2
== vals
[n
][1])
127 static const int mag3110_samp_freq
[8][2] = {
128 {80, 0}, {40, 0}, {20, 0}, {10, 0}, {5, 0}, {2, 500000},
129 {1, 250000}, {0, 625000}
132 static ssize_t
mag3110_show_samp_freq_avail(struct device
*dev
,
133 struct device_attribute
*attr
, char *buf
)
135 return mag3110_show_int_plus_micros(buf
, mag3110_samp_freq
, 8);
138 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(mag3110_show_samp_freq_avail
);
140 static int mag3110_get_samp_freq_index(struct mag3110_data
*data
,
143 return mag3110_get_int_plus_micros_index(mag3110_samp_freq
, 8, val
,
147 static int mag3110_read_raw(struct iio_dev
*indio_dev
,
148 struct iio_chan_spec
const *chan
,
149 int *val
, int *val2
, long mask
)
151 struct mag3110_data
*data
= iio_priv(indio_dev
);
156 case IIO_CHAN_INFO_RAW
:
157 if (iio_buffer_enabled(indio_dev
))
160 switch (chan
->type
) {
161 case IIO_MAGN
: /* in 0.1 uT / LSB */
162 ret
= mag3110_read(data
, buffer
);
165 *val
= sign_extend32(
166 be16_to_cpu(buffer
[chan
->scan_index
]), 15);
168 case IIO_TEMP
: /* in 1 C / LSB */
169 mutex_lock(&data
->lock
);
170 ret
= mag3110_request(data
);
172 mutex_unlock(&data
->lock
);
175 ret
= i2c_smbus_read_byte_data(data
->client
,
177 mutex_unlock(&data
->lock
);
180 *val
= sign_extend32(ret
, 7);
185 case IIO_CHAN_INFO_SCALE
:
188 return IIO_VAL_INT_PLUS_MICRO
;
189 case IIO_CHAN_INFO_SAMP_FREQ
:
190 i
= data
->ctrl_reg1
>> MAG3110_CTRL_DR_SHIFT
;
191 *val
= mag3110_samp_freq
[i
][0];
192 *val2
= mag3110_samp_freq
[i
][1];
193 return IIO_VAL_INT_PLUS_MICRO
;
198 static int mag3110_write_raw(struct iio_dev
*indio_dev
,
199 struct iio_chan_spec
const *chan
,
200 int val
, int val2
, long mask
)
202 struct mag3110_data
*data
= iio_priv(indio_dev
);
205 if (iio_buffer_enabled(indio_dev
))
209 case IIO_CHAN_INFO_SAMP_FREQ
:
210 rate
= mag3110_get_samp_freq_index(data
, val
, val2
);
214 data
->ctrl_reg1
&= ~MAG3110_CTRL_DR_MASK
;
215 data
->ctrl_reg1
|= rate
<< MAG3110_CTRL_DR_SHIFT
;
216 return i2c_smbus_write_byte_data(data
->client
,
217 MAG3110_CTRL_REG1
, data
->ctrl_reg1
);
223 static irqreturn_t
mag3110_trigger_handler(int irq
, void *p
)
225 struct iio_poll_func
*pf
= p
;
226 struct iio_dev
*indio_dev
= pf
->indio_dev
;
227 struct mag3110_data
*data
= iio_priv(indio_dev
);
228 u8 buffer
[16]; /* 3 16-bit channels + 1 byte temp + padding + ts */
231 ret
= mag3110_read(data
, (__be16
*) buffer
);
235 if (test_bit(3, indio_dev
->active_scan_mask
)) {
236 ret
= i2c_smbus_read_byte_data(data
->client
,
243 iio_push_to_buffers_with_timestamp(indio_dev
, buffer
,
247 iio_trigger_notify_done(indio_dev
->trig
);
251 #define MAG3110_CHANNEL(axis, idx) { \
254 .channel2 = IIO_MOD_##axis, \
255 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
256 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
257 BIT(IIO_CHAN_INFO_SCALE), \
263 .endianness = IIO_BE, \
267 static const struct iio_chan_spec mag3110_channels
[] = {
268 MAG3110_CHANNEL(X
, 0),
269 MAG3110_CHANNEL(Y
, 1),
270 MAG3110_CHANNEL(Z
, 2),
273 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
281 IIO_CHAN_SOFT_TIMESTAMP(4),
284 static struct attribute
*mag3110_attributes
[] = {
285 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
289 static const struct attribute_group mag3110_group
= {
290 .attrs
= mag3110_attributes
,
293 static const struct iio_info mag3110_info
= {
294 .attrs
= &mag3110_group
,
295 .read_raw
= &mag3110_read_raw
,
296 .write_raw
= &mag3110_write_raw
,
297 .driver_module
= THIS_MODULE
,
300 static const unsigned long mag3110_scan_masks
[] = {0x7, 0xf, 0};
302 static int mag3110_probe(struct i2c_client
*client
,
303 const struct i2c_device_id
*id
)
305 struct mag3110_data
*data
;
306 struct iio_dev
*indio_dev
;
309 ret
= i2c_smbus_read_byte_data(client
, MAG3110_WHO_AM_I
);
312 if (ret
!= MAG3110_DEVICE_ID
)
315 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
319 data
= iio_priv(indio_dev
);
320 data
->client
= client
;
321 mutex_init(&data
->lock
);
323 i2c_set_clientdata(client
, indio_dev
);
324 indio_dev
->info
= &mag3110_info
;
325 indio_dev
->name
= id
->name
;
326 indio_dev
->dev
.parent
= &client
->dev
;
327 indio_dev
->modes
= INDIO_DIRECT_MODE
;
328 indio_dev
->channels
= mag3110_channels
;
329 indio_dev
->num_channels
= ARRAY_SIZE(mag3110_channels
);
330 indio_dev
->available_scan_masks
= mag3110_scan_masks
;
332 data
->ctrl_reg1
= MAG3110_CTRL_DR_DEFAULT
;
333 ret
= i2c_smbus_write_byte_data(client
, MAG3110_CTRL_REG1
,
338 ret
= i2c_smbus_write_byte_data(client
, MAG3110_CTRL_REG2
,
339 MAG3110_CTRL_AUTO_MRST_EN
| MAG3110_CTRL_RAW
);
343 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
344 mag3110_trigger_handler
, NULL
);
348 ret
= iio_device_register(indio_dev
);
354 iio_triggered_buffer_cleanup(indio_dev
);
358 static int mag3110_standby(struct mag3110_data
*data
)
360 return i2c_smbus_write_byte_data(data
->client
, MAG3110_CTRL_REG1
,
361 data
->ctrl_reg1
& ~MAG3110_CTRL_AC
);
364 static int mag3110_remove(struct i2c_client
*client
)
366 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
368 iio_device_unregister(indio_dev
);
369 iio_triggered_buffer_cleanup(indio_dev
);
370 mag3110_standby(iio_priv(indio_dev
));
375 #ifdef CONFIG_PM_SLEEP
376 static int mag3110_suspend(struct device
*dev
)
378 return mag3110_standby(iio_priv(i2c_get_clientdata(
379 to_i2c_client(dev
))));
382 static int mag3110_resume(struct device
*dev
)
384 struct mag3110_data
*data
= iio_priv(i2c_get_clientdata(
385 to_i2c_client(dev
)));
387 return i2c_smbus_write_byte_data(data
->client
, MAG3110_CTRL_REG1
,
391 static SIMPLE_DEV_PM_OPS(mag3110_pm_ops
, mag3110_suspend
, mag3110_resume
);
392 #define MAG3110_PM_OPS (&mag3110_pm_ops)
394 #define MAG3110_PM_OPS NULL
397 static const struct i2c_device_id mag3110_id
[] = {
401 MODULE_DEVICE_TABLE(i2c
, mag3110_id
);
403 static struct i2c_driver mag3110_driver
= {
406 .pm
= MAG3110_PM_OPS
,
408 .probe
= mag3110_probe
,
409 .remove
= mag3110_remove
,
410 .id_table
= mag3110_id
,
412 module_i2c_driver(mag3110_driver
);
414 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
415 MODULE_DESCRIPTION("Freescale MAG3110 magnetometer driver");
416 MODULE_LICENSE("GPL");