2 * mma8452.c - Support for following Freescale 3-axis accelerometers:
10 * Copyright 2015 Martin Kepplinger <martin.kepplinger@theobroma-systems.com>
11 * Copyright 2014 Peter Meerwald <pmeerw@pmeerw.net>
13 * This file is subject to the terms and conditions of version 2 of
14 * the GNU General Public License. See the file COPYING in the main
15 * directory of this archive for more details.
17 * 7-bit I2C slave address 0x1c/0x1d (pin selectable)
19 * TODO: orientation events, autosleep
22 #include <linux/module.h>
23 #include <linux/i2c.h>
24 #include <linux/iio/iio.h>
25 #include <linux/iio/sysfs.h>
26 #include <linux/iio/buffer.h>
27 #include <linux/iio/trigger.h>
28 #include <linux/iio/trigger_consumer.h>
29 #include <linux/iio/triggered_buffer.h>
30 #include <linux/iio/events.h>
31 #include <linux/delay.h>
32 #include <linux/of_device.h>
33 #include <linux/of_irq.h>
35 #define MMA8452_STATUS 0x00
36 #define MMA8452_STATUS_DRDY (BIT(2) | BIT(1) | BIT(0))
37 #define MMA8452_OUT_X 0x01 /* MSB first */
38 #define MMA8452_OUT_Y 0x03
39 #define MMA8452_OUT_Z 0x05
40 #define MMA8452_INT_SRC 0x0c
41 #define MMA8452_WHO_AM_I 0x0d
42 #define MMA8452_DATA_CFG 0x0e
43 #define MMA8452_DATA_CFG_FS_MASK GENMASK(1, 0)
44 #define MMA8452_DATA_CFG_FS_2G 0
45 #define MMA8452_DATA_CFG_FS_4G 1
46 #define MMA8452_DATA_CFG_FS_8G 2
47 #define MMA8452_DATA_CFG_HPF_MASK BIT(4)
48 #define MMA8452_HP_FILTER_CUTOFF 0x0f
49 #define MMA8452_HP_FILTER_CUTOFF_SEL_MASK GENMASK(1, 0)
50 #define MMA8452_FF_MT_CFG 0x15
51 #define MMA8452_FF_MT_CFG_OAE BIT(6)
52 #define MMA8452_FF_MT_CFG_ELE BIT(7)
53 #define MMA8452_FF_MT_SRC 0x16
54 #define MMA8452_FF_MT_SRC_XHE BIT(1)
55 #define MMA8452_FF_MT_SRC_YHE BIT(3)
56 #define MMA8452_FF_MT_SRC_ZHE BIT(5)
57 #define MMA8452_FF_MT_THS 0x17
58 #define MMA8452_FF_MT_THS_MASK 0x7f
59 #define MMA8452_FF_MT_COUNT 0x18
60 #define MMA8452_TRANSIENT_CFG 0x1d
61 #define MMA8452_TRANSIENT_CFG_HPF_BYP BIT(0)
62 #define MMA8452_TRANSIENT_CFG_ELE BIT(4)
63 #define MMA8452_TRANSIENT_SRC 0x1e
64 #define MMA8452_TRANSIENT_SRC_XTRANSE BIT(1)
65 #define MMA8452_TRANSIENT_SRC_YTRANSE BIT(3)
66 #define MMA8452_TRANSIENT_SRC_ZTRANSE BIT(5)
67 #define MMA8452_TRANSIENT_THS 0x1f
68 #define MMA8452_TRANSIENT_THS_MASK GENMASK(6, 0)
69 #define MMA8452_TRANSIENT_COUNT 0x20
70 #define MMA8452_CTRL_REG1 0x2a
71 #define MMA8452_CTRL_ACTIVE BIT(0)
72 #define MMA8452_CTRL_DR_MASK GENMASK(5, 3)
73 #define MMA8452_CTRL_DR_SHIFT 3
74 #define MMA8452_CTRL_DR_DEFAULT 0x4 /* 50 Hz sample frequency */
75 #define MMA8452_CTRL_REG2 0x2b
76 #define MMA8452_CTRL_REG2_RST BIT(6)
77 #define MMA8452_CTRL_REG4 0x2d
78 #define MMA8452_CTRL_REG5 0x2e
79 #define MMA8452_OFF_X 0x2f
80 #define MMA8452_OFF_Y 0x30
81 #define MMA8452_OFF_Z 0x31
83 #define MMA8452_MAX_REG 0x31
85 #define MMA8452_INT_DRDY BIT(0)
86 #define MMA8452_INT_FF_MT BIT(2)
87 #define MMA8452_INT_TRANS BIT(5)
89 #define MMA8451_DEVICE_ID 0x1a
90 #define MMA8452_DEVICE_ID 0x2a
91 #define MMA8453_DEVICE_ID 0x3a
92 #define MMA8652_DEVICE_ID 0x4a
93 #define MMA8653_DEVICE_ID 0x5a
96 struct i2c_client
*client
;
100 const struct mma_chip_info
*chip_info
;
104 * struct mma_chip_info - chip specific data for Freescale's accelerometers
105 * @chip_id: WHO_AM_I register's value
106 * @channels: struct iio_chan_spec matching the device's
108 * @num_channels: number of channels
109 * @mma_scales: scale factors for converting register values
110 * to m/s^2; 3 modes: 2g, 4g, 8g; 2 integers
111 * per mode: m/s^2 and micro m/s^2
112 * @ev_cfg: event config register address
113 * @ev_cfg_ele: latch bit in event config register
114 * @ev_cfg_chan_shift: number of the bit to enable events in X
115 * direction; in event config register
116 * @ev_src: event source register address
117 * @ev_src_xe: bit in event source register that indicates
118 * an event in X direction
119 * @ev_src_ye: bit in event source register that indicates
120 * an event in Y direction
121 * @ev_src_ze: bit in event source register that indicates
122 * an event in Z direction
123 * @ev_ths: event threshold register address
124 * @ev_ths_mask: mask for the threshold value
125 * @ev_count: event count (period) register address
127 * Since not all chips supported by the driver support comparing high pass
128 * filtered data for events (interrupts), different interrupt sources are
129 * used for different chips and the relevant registers are included here.
131 struct mma_chip_info
{
133 const struct iio_chan_spec
*channels
;
135 const int mma_scales
[3][2];
138 u8 ev_cfg_chan_shift
;
155 static int mma8452_drdy(struct mma8452_data
*data
)
159 while (tries
-- > 0) {
160 int ret
= i2c_smbus_read_byte_data(data
->client
,
164 if ((ret
& MMA8452_STATUS_DRDY
) == MMA8452_STATUS_DRDY
)
170 dev_err(&data
->client
->dev
, "data not ready\n");
175 static int mma8452_read(struct mma8452_data
*data
, __be16 buf
[3])
177 int ret
= mma8452_drdy(data
);
182 return i2c_smbus_read_i2c_block_data(data
->client
, MMA8452_OUT_X
,
183 3 * sizeof(__be16
), (u8
*)buf
);
186 static ssize_t
mma8452_show_int_plus_micros(char *buf
, const int (*vals
)[2],
192 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "%d.%06d ",
193 vals
[n
][0], vals
[n
][1]);
195 /* replace trailing space by newline */
201 static int mma8452_get_int_plus_micros_index(const int (*vals
)[2], int n
,
205 if (val
== vals
[n
][0] && val2
== vals
[n
][1])
211 static int mma8452_get_odr_index(struct mma8452_data
*data
)
213 return (data
->ctrl_reg1
& MMA8452_CTRL_DR_MASK
) >>
214 MMA8452_CTRL_DR_SHIFT
;
217 static const int mma8452_samp_freq
[8][2] = {
218 {800, 0}, {400, 0}, {200, 0}, {100, 0}, {50, 0}, {12, 500000},
219 {6, 250000}, {1, 560000}
222 /* Datasheet table 35 (step time vs sample frequency) */
223 static const int mma8452_transient_time_step_us
[8] = {
234 /* Datasheet table 18 (normal mode) */
235 static const int mma8452_hp_filter_cutoff
[8][4][2] = {
236 { {16, 0}, {8, 0}, {4, 0}, {2, 0} }, /* 800 Hz sample */
237 { {16, 0}, {8, 0}, {4, 0}, {2, 0} }, /* 400 Hz sample */
238 { {8, 0}, {4, 0}, {2, 0}, {1, 0} }, /* 200 Hz sample */
239 { {4, 0}, {2, 0}, {1, 0}, {0, 500000} }, /* 100 Hz sample */
240 { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} }, /* 50 Hz sample */
241 { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} }, /* 12.5 Hz sample */
242 { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} }, /* 6.25 Hz sample */
243 { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} } /* 1.56 Hz sample */
246 static ssize_t
mma8452_show_samp_freq_avail(struct device
*dev
,
247 struct device_attribute
*attr
,
250 return mma8452_show_int_plus_micros(buf
, mma8452_samp_freq
,
251 ARRAY_SIZE(mma8452_samp_freq
));
254 static ssize_t
mma8452_show_scale_avail(struct device
*dev
,
255 struct device_attribute
*attr
,
258 struct mma8452_data
*data
= iio_priv(i2c_get_clientdata(
259 to_i2c_client(dev
)));
261 return mma8452_show_int_plus_micros(buf
, data
->chip_info
->mma_scales
,
262 ARRAY_SIZE(data
->chip_info
->mma_scales
));
265 static ssize_t
mma8452_show_hp_cutoff_avail(struct device
*dev
,
266 struct device_attribute
*attr
,
269 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
270 struct mma8452_data
*data
= iio_priv(indio_dev
);
271 int i
= mma8452_get_odr_index(data
);
273 return mma8452_show_int_plus_micros(buf
, mma8452_hp_filter_cutoff
[i
],
274 ARRAY_SIZE(mma8452_hp_filter_cutoff
[0]));
277 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(mma8452_show_samp_freq_avail
);
278 static IIO_DEVICE_ATTR(in_accel_scale_available
, S_IRUGO
,
279 mma8452_show_scale_avail
, NULL
, 0);
280 static IIO_DEVICE_ATTR(in_accel_filter_high_pass_3db_frequency_available
,
281 S_IRUGO
, mma8452_show_hp_cutoff_avail
, NULL
, 0);
283 static int mma8452_get_samp_freq_index(struct mma8452_data
*data
,
286 return mma8452_get_int_plus_micros_index(mma8452_samp_freq
,
287 ARRAY_SIZE(mma8452_samp_freq
),
291 static int mma8452_get_scale_index(struct mma8452_data
*data
, int val
, int val2
)
293 return mma8452_get_int_plus_micros_index(data
->chip_info
->mma_scales
,
294 ARRAY_SIZE(data
->chip_info
->mma_scales
), val
, val2
);
297 static int mma8452_get_hp_filter_index(struct mma8452_data
*data
,
300 int i
= mma8452_get_odr_index(data
);
302 return mma8452_get_int_plus_micros_index(mma8452_hp_filter_cutoff
[i
],
303 ARRAY_SIZE(mma8452_hp_filter_cutoff
[0]), val
, val2
);
306 static int mma8452_read_hp_filter(struct mma8452_data
*data
, int *hz
, int *uHz
)
310 ret
= i2c_smbus_read_byte_data(data
->client
, MMA8452_HP_FILTER_CUTOFF
);
314 i
= mma8452_get_odr_index(data
);
315 ret
&= MMA8452_HP_FILTER_CUTOFF_SEL_MASK
;
316 *hz
= mma8452_hp_filter_cutoff
[i
][ret
][0];
317 *uHz
= mma8452_hp_filter_cutoff
[i
][ret
][1];
322 static int mma8452_read_raw(struct iio_dev
*indio_dev
,
323 struct iio_chan_spec
const *chan
,
324 int *val
, int *val2
, long mask
)
326 struct mma8452_data
*data
= iio_priv(indio_dev
);
331 case IIO_CHAN_INFO_RAW
:
332 if (iio_buffer_enabled(indio_dev
))
335 mutex_lock(&data
->lock
);
336 ret
= mma8452_read(data
, buffer
);
337 mutex_unlock(&data
->lock
);
341 *val
= sign_extend32(be16_to_cpu(
342 buffer
[chan
->scan_index
]) >> chan
->scan_type
.shift
,
343 chan
->scan_type
.realbits
- 1);
346 case IIO_CHAN_INFO_SCALE
:
347 i
= data
->data_cfg
& MMA8452_DATA_CFG_FS_MASK
;
348 *val
= data
->chip_info
->mma_scales
[i
][0];
349 *val2
= data
->chip_info
->mma_scales
[i
][1];
351 return IIO_VAL_INT_PLUS_MICRO
;
352 case IIO_CHAN_INFO_SAMP_FREQ
:
353 i
= mma8452_get_odr_index(data
);
354 *val
= mma8452_samp_freq
[i
][0];
355 *val2
= mma8452_samp_freq
[i
][1];
357 return IIO_VAL_INT_PLUS_MICRO
;
358 case IIO_CHAN_INFO_CALIBBIAS
:
359 ret
= i2c_smbus_read_byte_data(data
->client
,
360 MMA8452_OFF_X
+ chan
->scan_index
);
364 *val
= sign_extend32(ret
, 7);
367 case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY
:
368 if (data
->data_cfg
& MMA8452_DATA_CFG_HPF_MASK
) {
369 ret
= mma8452_read_hp_filter(data
, val
, val2
);
377 return IIO_VAL_INT_PLUS_MICRO
;
383 static int mma8452_standby(struct mma8452_data
*data
)
385 return i2c_smbus_write_byte_data(data
->client
, MMA8452_CTRL_REG1
,
386 data
->ctrl_reg1
& ~MMA8452_CTRL_ACTIVE
);
389 static int mma8452_active(struct mma8452_data
*data
)
391 return i2c_smbus_write_byte_data(data
->client
, MMA8452_CTRL_REG1
,
395 static int mma8452_change_config(struct mma8452_data
*data
, u8 reg
, u8 val
)
399 mutex_lock(&data
->lock
);
401 /* config can only be changed when in standby */
402 ret
= mma8452_standby(data
);
406 ret
= i2c_smbus_write_byte_data(data
->client
, reg
, val
);
410 ret
= mma8452_active(data
);
416 mutex_unlock(&data
->lock
);
421 /* returns >0 if in freefall mode, 0 if not or <0 if an error occured */
422 static int mma8452_freefall_mode_enabled(struct mma8452_data
*data
)
425 const struct mma_chip_info
*chip
= data
->chip_info
;
427 val
= i2c_smbus_read_byte_data(data
->client
, chip
->ev_cfg
);
431 return !(val
& MMA8452_FF_MT_CFG_OAE
);
434 static int mma8452_set_freefall_mode(struct mma8452_data
*data
, bool state
)
437 const struct mma_chip_info
*chip
= data
->chip_info
;
439 if ((state
&& mma8452_freefall_mode_enabled(data
)) ||
440 (!state
&& !(mma8452_freefall_mode_enabled(data
))))
443 val
= i2c_smbus_read_byte_data(data
->client
, chip
->ev_cfg
);
448 val
|= BIT(idx_x
+ chip
->ev_cfg_chan_shift
);
449 val
|= BIT(idx_y
+ chip
->ev_cfg_chan_shift
);
450 val
|= BIT(idx_z
+ chip
->ev_cfg_chan_shift
);
451 val
&= ~MMA8452_FF_MT_CFG_OAE
;
453 val
&= ~BIT(idx_x
+ chip
->ev_cfg_chan_shift
);
454 val
&= ~BIT(idx_y
+ chip
->ev_cfg_chan_shift
);
455 val
&= ~BIT(idx_z
+ chip
->ev_cfg_chan_shift
);
456 val
|= MMA8452_FF_MT_CFG_OAE
;
459 val
= mma8452_change_config(data
, chip
->ev_cfg
, val
);
466 static int mma8452_set_hp_filter_frequency(struct mma8452_data
*data
,
471 i
= mma8452_get_hp_filter_index(data
, val
, val2
);
475 reg
= i2c_smbus_read_byte_data(data
->client
,
476 MMA8452_HP_FILTER_CUTOFF
);
480 reg
&= ~MMA8452_HP_FILTER_CUTOFF_SEL_MASK
;
483 return mma8452_change_config(data
, MMA8452_HP_FILTER_CUTOFF
, reg
);
486 static int mma8452_write_raw(struct iio_dev
*indio_dev
,
487 struct iio_chan_spec
const *chan
,
488 int val
, int val2
, long mask
)
490 struct mma8452_data
*data
= iio_priv(indio_dev
);
493 if (iio_buffer_enabled(indio_dev
))
497 case IIO_CHAN_INFO_SAMP_FREQ
:
498 i
= mma8452_get_samp_freq_index(data
, val
, val2
);
502 data
->ctrl_reg1
&= ~MMA8452_CTRL_DR_MASK
;
503 data
->ctrl_reg1
|= i
<< MMA8452_CTRL_DR_SHIFT
;
505 return mma8452_change_config(data
, MMA8452_CTRL_REG1
,
507 case IIO_CHAN_INFO_SCALE
:
508 i
= mma8452_get_scale_index(data
, val
, val2
);
512 data
->data_cfg
&= ~MMA8452_DATA_CFG_FS_MASK
;
515 return mma8452_change_config(data
, MMA8452_DATA_CFG
,
517 case IIO_CHAN_INFO_CALIBBIAS
:
518 if (val
< -128 || val
> 127)
521 return mma8452_change_config(data
,
522 MMA8452_OFF_X
+ chan
->scan_index
,
525 case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY
:
526 if (val
== 0 && val2
== 0) {
527 data
->data_cfg
&= ~MMA8452_DATA_CFG_HPF_MASK
;
529 data
->data_cfg
|= MMA8452_DATA_CFG_HPF_MASK
;
530 ret
= mma8452_set_hp_filter_frequency(data
, val
, val2
);
535 return mma8452_change_config(data
, MMA8452_DATA_CFG
,
543 static int mma8452_read_thresh(struct iio_dev
*indio_dev
,
544 const struct iio_chan_spec
*chan
,
545 enum iio_event_type type
,
546 enum iio_event_direction dir
,
547 enum iio_event_info info
,
550 struct mma8452_data
*data
= iio_priv(indio_dev
);
554 case IIO_EV_INFO_VALUE
:
555 ret
= i2c_smbus_read_byte_data(data
->client
,
556 data
->chip_info
->ev_ths
);
560 *val
= ret
& data
->chip_info
->ev_ths_mask
;
564 case IIO_EV_INFO_PERIOD
:
565 ret
= i2c_smbus_read_byte_data(data
->client
,
566 data
->chip_info
->ev_count
);
570 us
= ret
* mma8452_transient_time_step_us
[
571 mma8452_get_odr_index(data
)];
572 *val
= us
/ USEC_PER_SEC
;
573 *val2
= us
% USEC_PER_SEC
;
575 return IIO_VAL_INT_PLUS_MICRO
;
577 case IIO_EV_INFO_HIGH_PASS_FILTER_3DB
:
578 ret
= i2c_smbus_read_byte_data(data
->client
,
579 MMA8452_TRANSIENT_CFG
);
583 if (ret
& MMA8452_TRANSIENT_CFG_HPF_BYP
) {
587 ret
= mma8452_read_hp_filter(data
, val
, val2
);
592 return IIO_VAL_INT_PLUS_MICRO
;
599 static int mma8452_write_thresh(struct iio_dev
*indio_dev
,
600 const struct iio_chan_spec
*chan
,
601 enum iio_event_type type
,
602 enum iio_event_direction dir
,
603 enum iio_event_info info
,
606 struct mma8452_data
*data
= iio_priv(indio_dev
);
610 case IIO_EV_INFO_VALUE
:
611 if (val
< 0 || val
> MMA8452_TRANSIENT_THS_MASK
)
614 return mma8452_change_config(data
, data
->chip_info
->ev_ths
,
617 case IIO_EV_INFO_PERIOD
:
618 steps
= (val
* USEC_PER_SEC
+ val2
) /
619 mma8452_transient_time_step_us
[
620 mma8452_get_odr_index(data
)];
622 if (steps
< 0 || steps
> 0xff)
625 return mma8452_change_config(data
, data
->chip_info
->ev_count
,
628 case IIO_EV_INFO_HIGH_PASS_FILTER_3DB
:
629 reg
= i2c_smbus_read_byte_data(data
->client
,
630 MMA8452_TRANSIENT_CFG
);
634 if (val
== 0 && val2
== 0) {
635 reg
|= MMA8452_TRANSIENT_CFG_HPF_BYP
;
637 reg
&= ~MMA8452_TRANSIENT_CFG_HPF_BYP
;
638 ret
= mma8452_set_hp_filter_frequency(data
, val
, val2
);
643 return mma8452_change_config(data
, MMA8452_TRANSIENT_CFG
, reg
);
650 static int mma8452_read_event_config(struct iio_dev
*indio_dev
,
651 const struct iio_chan_spec
*chan
,
652 enum iio_event_type type
,
653 enum iio_event_direction dir
)
655 struct mma8452_data
*data
= iio_priv(indio_dev
);
656 const struct mma_chip_info
*chip
= data
->chip_info
;
660 case IIO_EV_DIR_FALLING
:
661 return mma8452_freefall_mode_enabled(data
);
662 case IIO_EV_DIR_RISING
:
663 if (mma8452_freefall_mode_enabled(data
))
666 ret
= i2c_smbus_read_byte_data(data
->client
,
667 data
->chip_info
->ev_cfg
);
671 return !!(ret
& BIT(chan
->scan_index
+ chip
->ev_cfg_chan_shift
));
677 static int mma8452_write_event_config(struct iio_dev
*indio_dev
,
678 const struct iio_chan_spec
*chan
,
679 enum iio_event_type type
,
680 enum iio_event_direction dir
,
683 struct mma8452_data
*data
= iio_priv(indio_dev
);
684 const struct mma_chip_info
*chip
= data
->chip_info
;
688 case IIO_EV_DIR_FALLING
:
689 return mma8452_set_freefall_mode(data
, state
);
690 case IIO_EV_DIR_RISING
:
691 val
= i2c_smbus_read_byte_data(data
->client
, chip
->ev_cfg
);
696 if (mma8452_freefall_mode_enabled(data
)) {
697 val
&= ~BIT(idx_x
+ chip
->ev_cfg_chan_shift
);
698 val
&= ~BIT(idx_y
+ chip
->ev_cfg_chan_shift
);
699 val
&= ~BIT(idx_z
+ chip
->ev_cfg_chan_shift
);
700 val
|= MMA8452_FF_MT_CFG_OAE
;
702 val
|= BIT(chan
->scan_index
+ chip
->ev_cfg_chan_shift
);
704 if (mma8452_freefall_mode_enabled(data
))
707 val
&= ~BIT(chan
->scan_index
+ chip
->ev_cfg_chan_shift
);
710 val
|= chip
->ev_cfg_ele
;
712 return mma8452_change_config(data
, chip
->ev_cfg
, val
);
718 static void mma8452_transient_interrupt(struct iio_dev
*indio_dev
)
720 struct mma8452_data
*data
= iio_priv(indio_dev
);
721 s64 ts
= iio_get_time_ns();
724 src
= i2c_smbus_read_byte_data(data
->client
, data
->chip_info
->ev_src
);
728 if (mma8452_freefall_mode_enabled(data
)) {
729 iio_push_event(indio_dev
,
730 IIO_MOD_EVENT_CODE(IIO_ACCEL
, 0,
731 IIO_MOD_X_AND_Y_AND_Z
,
738 if (src
& data
->chip_info
->ev_src_xe
)
739 iio_push_event(indio_dev
,
740 IIO_MOD_EVENT_CODE(IIO_ACCEL
, 0, IIO_MOD_X
,
745 if (src
& data
->chip_info
->ev_src_ye
)
746 iio_push_event(indio_dev
,
747 IIO_MOD_EVENT_CODE(IIO_ACCEL
, 0, IIO_MOD_Y
,
752 if (src
& data
->chip_info
->ev_src_ze
)
753 iio_push_event(indio_dev
,
754 IIO_MOD_EVENT_CODE(IIO_ACCEL
, 0, IIO_MOD_Z
,
760 static irqreturn_t
mma8452_interrupt(int irq
, void *p
)
762 struct iio_dev
*indio_dev
= p
;
763 struct mma8452_data
*data
= iio_priv(indio_dev
);
764 const struct mma_chip_info
*chip
= data
->chip_info
;
768 src
= i2c_smbus_read_byte_data(data
->client
, MMA8452_INT_SRC
);
772 if (src
& MMA8452_INT_DRDY
) {
773 iio_trigger_poll_chained(indio_dev
->trig
);
777 if ((src
& MMA8452_INT_TRANS
&&
778 chip
->ev_src
== MMA8452_TRANSIENT_SRC
) ||
779 (src
& MMA8452_INT_FF_MT
&&
780 chip
->ev_src
== MMA8452_FF_MT_SRC
)) {
781 mma8452_transient_interrupt(indio_dev
);
788 static irqreturn_t
mma8452_trigger_handler(int irq
, void *p
)
790 struct iio_poll_func
*pf
= p
;
791 struct iio_dev
*indio_dev
= pf
->indio_dev
;
792 struct mma8452_data
*data
= iio_priv(indio_dev
);
793 u8 buffer
[16]; /* 3 16-bit channels + padding + ts */
796 ret
= mma8452_read(data
, (__be16
*)buffer
);
800 iio_push_to_buffers_with_timestamp(indio_dev
, buffer
,
804 iio_trigger_notify_done(indio_dev
->trig
);
809 static int mma8452_reg_access_dbg(struct iio_dev
*indio_dev
,
810 unsigned reg
, unsigned writeval
,
814 struct mma8452_data
*data
= iio_priv(indio_dev
);
816 if (reg
> MMA8452_MAX_REG
)
820 return mma8452_change_config(data
, reg
, writeval
);
822 ret
= i2c_smbus_read_byte_data(data
->client
, reg
);
831 static const struct iio_event_spec mma8452_freefall_event
[] = {
833 .type
= IIO_EV_TYPE_MAG
,
834 .dir
= IIO_EV_DIR_FALLING
,
835 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
),
836 .mask_shared_by_type
= BIT(IIO_EV_INFO_VALUE
) |
837 BIT(IIO_EV_INFO_PERIOD
) |
838 BIT(IIO_EV_INFO_HIGH_PASS_FILTER_3DB
)
842 static const struct iio_event_spec mma8652_freefall_event
[] = {
844 .type
= IIO_EV_TYPE_MAG
,
845 .dir
= IIO_EV_DIR_FALLING
,
846 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
),
847 .mask_shared_by_type
= BIT(IIO_EV_INFO_VALUE
) |
848 BIT(IIO_EV_INFO_PERIOD
)
852 static const struct iio_event_spec mma8452_transient_event
[] = {
854 .type
= IIO_EV_TYPE_MAG
,
855 .dir
= IIO_EV_DIR_RISING
,
856 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
),
857 .mask_shared_by_type
= BIT(IIO_EV_INFO_VALUE
) |
858 BIT(IIO_EV_INFO_PERIOD
) |
859 BIT(IIO_EV_INFO_HIGH_PASS_FILTER_3DB
)
863 static const struct iio_event_spec mma8452_motion_event
[] = {
865 .type
= IIO_EV_TYPE_MAG
,
866 .dir
= IIO_EV_DIR_RISING
,
867 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
),
868 .mask_shared_by_type
= BIT(IIO_EV_INFO_VALUE
) |
869 BIT(IIO_EV_INFO_PERIOD
)
874 * Threshold is configured in fixed 8G/127 steps regardless of
875 * currently selected scale for measurement.
877 static IIO_CONST_ATTR_NAMED(accel_transient_scale
, in_accel_scale
, "0.617742");
879 static struct attribute
*mma8452_event_attributes
[] = {
880 &iio_const_attr_accel_transient_scale
.dev_attr
.attr
,
884 static struct attribute_group mma8452_event_attribute_group
= {
885 .attrs
= mma8452_event_attributes
,
888 #define MMA8452_FREEFALL_CHANNEL(modifier) { \
891 .channel2 = modifier, \
893 .event_spec = mma8452_freefall_event, \
894 .num_event_specs = ARRAY_SIZE(mma8452_freefall_event), \
897 #define MMA8652_FREEFALL_CHANNEL(modifier) { \
900 .channel2 = modifier, \
902 .event_spec = mma8652_freefall_event, \
903 .num_event_specs = ARRAY_SIZE(mma8652_freefall_event), \
906 #define MMA8452_CHANNEL(axis, idx, bits) { \
909 .channel2 = IIO_MOD_##axis, \
910 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
911 BIT(IIO_CHAN_INFO_CALIBBIAS), \
912 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
913 BIT(IIO_CHAN_INFO_SCALE) | \
914 BIT(IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY), \
918 .realbits = (bits), \
920 .shift = 16 - (bits), \
921 .endianness = IIO_BE, \
923 .event_spec = mma8452_transient_event, \
924 .num_event_specs = ARRAY_SIZE(mma8452_transient_event), \
927 #define MMA8652_CHANNEL(axis, idx, bits) { \
930 .channel2 = IIO_MOD_##axis, \
931 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
932 BIT(IIO_CHAN_INFO_CALIBBIAS), \
933 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
934 BIT(IIO_CHAN_INFO_SCALE), \
938 .realbits = (bits), \
940 .shift = 16 - (bits), \
941 .endianness = IIO_BE, \
943 .event_spec = mma8452_motion_event, \
944 .num_event_specs = ARRAY_SIZE(mma8452_motion_event), \
947 static const struct iio_chan_spec mma8451_channels
[] = {
948 MMA8452_CHANNEL(X
, idx_x
, 14),
949 MMA8452_CHANNEL(Y
, idx_y
, 14),
950 MMA8452_CHANNEL(Z
, idx_z
, 14),
951 IIO_CHAN_SOFT_TIMESTAMP(idx_ts
),
952 MMA8452_FREEFALL_CHANNEL(IIO_MOD_X_AND_Y_AND_Z
),
955 static const struct iio_chan_spec mma8452_channels
[] = {
956 MMA8452_CHANNEL(X
, idx_x
, 12),
957 MMA8452_CHANNEL(Y
, idx_y
, 12),
958 MMA8452_CHANNEL(Z
, idx_z
, 12),
959 IIO_CHAN_SOFT_TIMESTAMP(idx_ts
),
960 MMA8452_FREEFALL_CHANNEL(IIO_MOD_X_AND_Y_AND_Z
),
963 static const struct iio_chan_spec mma8453_channels
[] = {
964 MMA8452_CHANNEL(X
, idx_x
, 10),
965 MMA8452_CHANNEL(Y
, idx_y
, 10),
966 MMA8452_CHANNEL(Z
, idx_z
, 10),
967 IIO_CHAN_SOFT_TIMESTAMP(idx_ts
),
968 MMA8452_FREEFALL_CHANNEL(IIO_MOD_X_AND_Y_AND_Z
),
971 static const struct iio_chan_spec mma8652_channels
[] = {
972 MMA8652_CHANNEL(X
, idx_x
, 12),
973 MMA8652_CHANNEL(Y
, idx_y
, 12),
974 MMA8652_CHANNEL(Z
, idx_z
, 12),
975 IIO_CHAN_SOFT_TIMESTAMP(idx_ts
),
976 MMA8652_FREEFALL_CHANNEL(IIO_MOD_X_AND_Y_AND_Z
),
979 static const struct iio_chan_spec mma8653_channels
[] = {
980 MMA8652_CHANNEL(X
, idx_x
, 10),
981 MMA8652_CHANNEL(Y
, idx_y
, 10),
982 MMA8652_CHANNEL(Z
, idx_z
, 10),
983 IIO_CHAN_SOFT_TIMESTAMP(idx_ts
),
984 MMA8652_FREEFALL_CHANNEL(IIO_MOD_X_AND_Y_AND_Z
),
995 static const struct mma_chip_info mma_chip_info_table
[] = {
997 .chip_id
= MMA8451_DEVICE_ID
,
998 .channels
= mma8451_channels
,
999 .num_channels
= ARRAY_SIZE(mma8451_channels
),
1001 * Hardware has fullscale of -2G, -4G, -8G corresponding to
1002 * raw value -8192 for 14 bit, -2048 for 12 bit or -512 for 10
1004 * The userspace interface uses m/s^2 and we declare micro units
1005 * So scale factor for 12 bit here is given by:
1006 * g * N * 1000000 / 2048 for N = 2, 4, 8 and g=9.80665
1008 .mma_scales
= { {0, 2394}, {0, 4788}, {0, 9577} },
1009 .ev_cfg
= MMA8452_TRANSIENT_CFG
,
1010 .ev_cfg_ele
= MMA8452_TRANSIENT_CFG_ELE
,
1011 .ev_cfg_chan_shift
= 1,
1012 .ev_src
= MMA8452_TRANSIENT_SRC
,
1013 .ev_src_xe
= MMA8452_TRANSIENT_SRC_XTRANSE
,
1014 .ev_src_ye
= MMA8452_TRANSIENT_SRC_YTRANSE
,
1015 .ev_src_ze
= MMA8452_TRANSIENT_SRC_ZTRANSE
,
1016 .ev_ths
= MMA8452_TRANSIENT_THS
,
1017 .ev_ths_mask
= MMA8452_TRANSIENT_THS_MASK
,
1018 .ev_count
= MMA8452_TRANSIENT_COUNT
,
1021 .chip_id
= MMA8452_DEVICE_ID
,
1022 .channels
= mma8452_channels
,
1023 .num_channels
= ARRAY_SIZE(mma8452_channels
),
1024 .mma_scales
= { {0, 9577}, {0, 19154}, {0, 38307} },
1025 .ev_cfg
= MMA8452_TRANSIENT_CFG
,
1026 .ev_cfg_ele
= MMA8452_TRANSIENT_CFG_ELE
,
1027 .ev_cfg_chan_shift
= 1,
1028 .ev_src
= MMA8452_TRANSIENT_SRC
,
1029 .ev_src_xe
= MMA8452_TRANSIENT_SRC_XTRANSE
,
1030 .ev_src_ye
= MMA8452_TRANSIENT_SRC_YTRANSE
,
1031 .ev_src_ze
= MMA8452_TRANSIENT_SRC_ZTRANSE
,
1032 .ev_ths
= MMA8452_TRANSIENT_THS
,
1033 .ev_ths_mask
= MMA8452_TRANSIENT_THS_MASK
,
1034 .ev_count
= MMA8452_TRANSIENT_COUNT
,
1037 .chip_id
= MMA8453_DEVICE_ID
,
1038 .channels
= mma8453_channels
,
1039 .num_channels
= ARRAY_SIZE(mma8453_channels
),
1040 .mma_scales
= { {0, 38307}, {0, 76614}, {0, 153228} },
1041 .ev_cfg
= MMA8452_TRANSIENT_CFG
,
1042 .ev_cfg_ele
= MMA8452_TRANSIENT_CFG_ELE
,
1043 .ev_cfg_chan_shift
= 1,
1044 .ev_src
= MMA8452_TRANSIENT_SRC
,
1045 .ev_src_xe
= MMA8452_TRANSIENT_SRC_XTRANSE
,
1046 .ev_src_ye
= MMA8452_TRANSIENT_SRC_YTRANSE
,
1047 .ev_src_ze
= MMA8452_TRANSIENT_SRC_ZTRANSE
,
1048 .ev_ths
= MMA8452_TRANSIENT_THS
,
1049 .ev_ths_mask
= MMA8452_TRANSIENT_THS_MASK
,
1050 .ev_count
= MMA8452_TRANSIENT_COUNT
,
1053 .chip_id
= MMA8652_DEVICE_ID
,
1054 .channels
= mma8652_channels
,
1055 .num_channels
= ARRAY_SIZE(mma8652_channels
),
1056 .mma_scales
= { {0, 9577}, {0, 19154}, {0, 38307} },
1057 .ev_cfg
= MMA8452_FF_MT_CFG
,
1058 .ev_cfg_ele
= MMA8452_FF_MT_CFG_ELE
,
1059 .ev_cfg_chan_shift
= 3,
1060 .ev_src
= MMA8452_FF_MT_SRC
,
1061 .ev_src_xe
= MMA8452_FF_MT_SRC_XHE
,
1062 .ev_src_ye
= MMA8452_FF_MT_SRC_YHE
,
1063 .ev_src_ze
= MMA8452_FF_MT_SRC_ZHE
,
1064 .ev_ths
= MMA8452_FF_MT_THS
,
1065 .ev_ths_mask
= MMA8452_FF_MT_THS_MASK
,
1066 .ev_count
= MMA8452_FF_MT_COUNT
,
1069 .chip_id
= MMA8653_DEVICE_ID
,
1070 .channels
= mma8653_channels
,
1071 .num_channels
= ARRAY_SIZE(mma8653_channels
),
1072 .mma_scales
= { {0, 38307}, {0, 76614}, {0, 153228} },
1073 .ev_cfg
= MMA8452_FF_MT_CFG
,
1074 .ev_cfg_ele
= MMA8452_FF_MT_CFG_ELE
,
1075 .ev_cfg_chan_shift
= 3,
1076 .ev_src
= MMA8452_FF_MT_SRC
,
1077 .ev_src_xe
= MMA8452_FF_MT_SRC_XHE
,
1078 .ev_src_ye
= MMA8452_FF_MT_SRC_YHE
,
1079 .ev_src_ze
= MMA8452_FF_MT_SRC_ZHE
,
1080 .ev_ths
= MMA8452_FF_MT_THS
,
1081 .ev_ths_mask
= MMA8452_FF_MT_THS_MASK
,
1082 .ev_count
= MMA8452_FF_MT_COUNT
,
1086 static struct attribute
*mma8452_attributes
[] = {
1087 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
1088 &iio_dev_attr_in_accel_scale_available
.dev_attr
.attr
,
1089 &iio_dev_attr_in_accel_filter_high_pass_3db_frequency_available
.dev_attr
.attr
,
1093 static const struct attribute_group mma8452_group
= {
1094 .attrs
= mma8452_attributes
,
1097 static const struct iio_info mma8452_info
= {
1098 .attrs
= &mma8452_group
,
1099 .read_raw
= &mma8452_read_raw
,
1100 .write_raw
= &mma8452_write_raw
,
1101 .event_attrs
= &mma8452_event_attribute_group
,
1102 .read_event_value
= &mma8452_read_thresh
,
1103 .write_event_value
= &mma8452_write_thresh
,
1104 .read_event_config
= &mma8452_read_event_config
,
1105 .write_event_config
= &mma8452_write_event_config
,
1106 .debugfs_reg_access
= &mma8452_reg_access_dbg
,
1107 .driver_module
= THIS_MODULE
,
1110 static const unsigned long mma8452_scan_masks
[] = {0x7, 0};
1112 static int mma8452_data_rdy_trigger_set_state(struct iio_trigger
*trig
,
1115 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
1116 struct mma8452_data
*data
= iio_priv(indio_dev
);
1119 reg
= i2c_smbus_read_byte_data(data
->client
, MMA8452_CTRL_REG4
);
1124 reg
|= MMA8452_INT_DRDY
;
1126 reg
&= ~MMA8452_INT_DRDY
;
1128 return mma8452_change_config(data
, MMA8452_CTRL_REG4
, reg
);
1131 static int mma8452_validate_device(struct iio_trigger
*trig
,
1132 struct iio_dev
*indio_dev
)
1134 struct iio_dev
*indio
= iio_trigger_get_drvdata(trig
);
1136 if (indio
!= indio_dev
)
1142 static const struct iio_trigger_ops mma8452_trigger_ops
= {
1143 .set_trigger_state
= mma8452_data_rdy_trigger_set_state
,
1144 .validate_device
= mma8452_validate_device
,
1145 .owner
= THIS_MODULE
,
1148 static int mma8452_trigger_setup(struct iio_dev
*indio_dev
)
1150 struct mma8452_data
*data
= iio_priv(indio_dev
);
1151 struct iio_trigger
*trig
;
1154 trig
= devm_iio_trigger_alloc(&data
->client
->dev
, "%s-dev%d",
1160 trig
->dev
.parent
= &data
->client
->dev
;
1161 trig
->ops
= &mma8452_trigger_ops
;
1162 iio_trigger_set_drvdata(trig
, indio_dev
);
1164 ret
= iio_trigger_register(trig
);
1168 indio_dev
->trig
= trig
;
1173 static void mma8452_trigger_cleanup(struct iio_dev
*indio_dev
)
1175 if (indio_dev
->trig
)
1176 iio_trigger_unregister(indio_dev
->trig
);
1179 static int mma8452_reset(struct i2c_client
*client
)
1184 ret
= i2c_smbus_write_byte_data(client
, MMA8452_CTRL_REG2
,
1185 MMA8452_CTRL_REG2_RST
);
1189 for (i
= 0; i
< 10; i
++) {
1190 usleep_range(100, 200);
1191 ret
= i2c_smbus_read_byte_data(client
, MMA8452_CTRL_REG2
);
1193 continue; /* I2C comm reset */
1196 if (!(ret
& MMA8452_CTRL_REG2_RST
))
1203 static const struct of_device_id mma8452_dt_ids
[] = {
1204 { .compatible
= "fsl,mma8451", .data
= &mma_chip_info_table
[mma8451
] },
1205 { .compatible
= "fsl,mma8452", .data
= &mma_chip_info_table
[mma8452
] },
1206 { .compatible
= "fsl,mma8453", .data
= &mma_chip_info_table
[mma8453
] },
1207 { .compatible
= "fsl,mma8652", .data
= &mma_chip_info_table
[mma8652
] },
1208 { .compatible
= "fsl,mma8653", .data
= &mma_chip_info_table
[mma8653
] },
1211 MODULE_DEVICE_TABLE(of
, mma8452_dt_ids
);
1213 static int mma8452_probe(struct i2c_client
*client
,
1214 const struct i2c_device_id
*id
)
1216 struct mma8452_data
*data
;
1217 struct iio_dev
*indio_dev
;
1219 const struct of_device_id
*match
;
1221 match
= of_match_device(mma8452_dt_ids
, &client
->dev
);
1223 dev_err(&client
->dev
, "unknown device model\n");
1227 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
1231 data
= iio_priv(indio_dev
);
1232 data
->client
= client
;
1233 mutex_init(&data
->lock
);
1234 data
->chip_info
= match
->data
;
1236 ret
= i2c_smbus_read_byte_data(client
, MMA8452_WHO_AM_I
);
1241 case MMA8451_DEVICE_ID
:
1242 case MMA8452_DEVICE_ID
:
1243 case MMA8453_DEVICE_ID
:
1244 case MMA8652_DEVICE_ID
:
1245 case MMA8653_DEVICE_ID
:
1246 if (ret
== data
->chip_info
->chip_id
)
1252 dev_info(&client
->dev
, "registering %s accelerometer; ID 0x%x\n",
1253 match
->compatible
, data
->chip_info
->chip_id
);
1255 i2c_set_clientdata(client
, indio_dev
);
1256 indio_dev
->info
= &mma8452_info
;
1257 indio_dev
->name
= id
->name
;
1258 indio_dev
->dev
.parent
= &client
->dev
;
1259 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1260 indio_dev
->channels
= data
->chip_info
->channels
;
1261 indio_dev
->num_channels
= data
->chip_info
->num_channels
;
1262 indio_dev
->available_scan_masks
= mma8452_scan_masks
;
1264 ret
= mma8452_reset(client
);
1268 data
->data_cfg
= MMA8452_DATA_CFG_FS_2G
;
1269 ret
= i2c_smbus_write_byte_data(client
, MMA8452_DATA_CFG
,
1275 * By default set transient threshold to max to avoid events if
1276 * enabling without configuring threshold.
1278 ret
= i2c_smbus_write_byte_data(client
, MMA8452_TRANSIENT_THS
,
1279 MMA8452_TRANSIENT_THS_MASK
);
1285 * Although we enable the interrupt sources once and for
1286 * all here the event detection itself is not enabled until
1287 * userspace asks for it by mma8452_write_event_config()
1289 int supported_interrupts
= MMA8452_INT_DRDY
|
1292 int enabled_interrupts
= MMA8452_INT_TRANS
|
1296 irq2
= of_irq_get_byname(client
->dev
.of_node
, "INT2");
1298 if (irq2
== client
->irq
) {
1299 dev_dbg(&client
->dev
, "using interrupt line INT2\n");
1301 ret
= i2c_smbus_write_byte_data(client
,
1303 supported_interrupts
);
1307 dev_dbg(&client
->dev
, "using interrupt line INT1\n");
1310 ret
= i2c_smbus_write_byte_data(client
,
1312 enabled_interrupts
);
1316 ret
= mma8452_trigger_setup(indio_dev
);
1321 data
->ctrl_reg1
= MMA8452_CTRL_ACTIVE
|
1322 (MMA8452_CTRL_DR_DEFAULT
<< MMA8452_CTRL_DR_SHIFT
);
1323 ret
= i2c_smbus_write_byte_data(client
, MMA8452_CTRL_REG1
,
1326 goto trigger_cleanup
;
1328 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
1329 mma8452_trigger_handler
, NULL
);
1331 goto trigger_cleanup
;
1334 ret
= devm_request_threaded_irq(&client
->dev
,
1336 NULL
, mma8452_interrupt
,
1337 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
1338 client
->name
, indio_dev
);
1340 goto buffer_cleanup
;
1343 ret
= iio_device_register(indio_dev
);
1345 goto buffer_cleanup
;
1347 ret
= mma8452_set_freefall_mode(data
, false);
1354 iio_triggered_buffer_cleanup(indio_dev
);
1357 mma8452_trigger_cleanup(indio_dev
);
1362 static int mma8452_remove(struct i2c_client
*client
)
1364 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1366 iio_device_unregister(indio_dev
);
1367 iio_triggered_buffer_cleanup(indio_dev
);
1368 mma8452_trigger_cleanup(indio_dev
);
1369 mma8452_standby(iio_priv(indio_dev
));
1374 #ifdef CONFIG_PM_SLEEP
1375 static int mma8452_suspend(struct device
*dev
)
1377 return mma8452_standby(iio_priv(i2c_get_clientdata(
1378 to_i2c_client(dev
))));
1381 static int mma8452_resume(struct device
*dev
)
1383 return mma8452_active(iio_priv(i2c_get_clientdata(
1384 to_i2c_client(dev
))));
1387 static SIMPLE_DEV_PM_OPS(mma8452_pm_ops
, mma8452_suspend
, mma8452_resume
);
1388 #define MMA8452_PM_OPS (&mma8452_pm_ops)
1390 #define MMA8452_PM_OPS NULL
1393 static const struct i2c_device_id mma8452_id
[] = {
1394 { "mma8452", mma8452
},
1395 { "mma8453", mma8453
},
1396 { "mma8652", mma8652
},
1397 { "mma8653", mma8653
},
1400 MODULE_DEVICE_TABLE(i2c
, mma8452_id
);
1402 static struct i2c_driver mma8452_driver
= {
1405 .of_match_table
= of_match_ptr(mma8452_dt_ids
),
1406 .pm
= MMA8452_PM_OPS
,
1408 .probe
= mma8452_probe
,
1409 .remove
= mma8452_remove
,
1410 .id_table
= mma8452_id
,
1412 module_i2c_driver(mma8452_driver
);
1414 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
1415 MODULE_DESCRIPTION("Freescale MMA8452 accelerometer driver");
1416 MODULE_LICENSE("GPL");