2 * mma8452.c - Support for Freescale MMA8452Q 3-axis 12-bit accelerometer
4 * Copyright 2014 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 0x1c/0x1d (pin selectable)
12 * TODO: orientation / freefall events, autosleep
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/trigger.h>
22 #include <linux/iio/trigger_consumer.h>
23 #include <linux/iio/triggered_buffer.h>
24 #include <linux/iio/events.h>
25 #include <linux/delay.h>
27 #define MMA8452_STATUS 0x00
28 #define MMA8452_OUT_X 0x01 /* MSB first, 12-bit */
29 #define MMA8452_OUT_Y 0x03
30 #define MMA8452_OUT_Z 0x05
31 #define MMA8452_INT_SRC 0x0c
32 #define MMA8452_WHO_AM_I 0x0d
33 #define MMA8452_DATA_CFG 0x0e
34 #define MMA8452_HP_FILTER_CUTOFF 0x0f
35 #define MMA8452_HP_FILTER_CUTOFF_SEL_MASK (BIT(0) | BIT(1))
36 #define MMA8452_TRANSIENT_CFG 0x1d
37 #define MMA8452_TRANSIENT_CFG_ELE BIT(4)
38 #define MMA8452_TRANSIENT_CFG_CHAN(chan) BIT(chan + 1)
39 #define MMA8452_TRANSIENT_CFG_HPF_BYP BIT(0)
40 #define MMA8452_TRANSIENT_SRC 0x1e
41 #define MMA8452_TRANSIENT_SRC_XTRANSE BIT(1)
42 #define MMA8452_TRANSIENT_SRC_YTRANSE BIT(3)
43 #define MMA8452_TRANSIENT_SRC_ZTRANSE BIT(5)
44 #define MMA8452_TRANSIENT_THS 0x1f
45 #define MMA8452_TRANSIENT_THS_MASK 0x7f
46 #define MMA8452_TRANSIENT_COUNT 0x20
47 #define MMA8452_OFF_X 0x2f
48 #define MMA8452_OFF_Y 0x30
49 #define MMA8452_OFF_Z 0x31
50 #define MMA8452_CTRL_REG1 0x2a
51 #define MMA8452_CTRL_REG2 0x2b
52 #define MMA8452_CTRL_REG2_RST BIT(6)
53 #define MMA8452_CTRL_REG4 0x2d
54 #define MMA8452_CTRL_REG5 0x2e
56 #define MMA8452_MAX_REG 0x31
58 #define MMA8452_STATUS_DRDY (BIT(2) | BIT(1) | BIT(0))
60 #define MMA8452_CTRL_DR_MASK (BIT(5) | BIT(4) | BIT(3))
61 #define MMA8452_CTRL_DR_SHIFT 3
62 #define MMA8452_CTRL_DR_DEFAULT 0x4 /* 50 Hz sample frequency */
63 #define MMA8452_CTRL_ACTIVE BIT(0)
65 #define MMA8452_DATA_CFG_FS_MASK (BIT(1) | BIT(0))
66 #define MMA8452_DATA_CFG_FS_2G 0
67 #define MMA8452_DATA_CFG_FS_4G 1
68 #define MMA8452_DATA_CFG_FS_8G 2
69 #define MMA8452_DATA_CFG_HPF_MASK BIT(4)
71 #define MMA8452_INT_DRDY BIT(0)
72 #define MMA8452_INT_TRANS BIT(5)
74 #define MMA8452_DEVICE_ID 0x2a
77 struct i2c_client *client;
83 static int mma8452_drdy(struct mma8452_data *data)
88 int ret = i2c_smbus_read_byte_data(data->client,
92 if ((ret & MMA8452_STATUS_DRDY) == MMA8452_STATUS_DRDY)
97 dev_err(&data->client->dev, "data not ready\n");
101 static int mma8452_read(struct mma8452_data *data, __be16 buf[3])
103 int ret = mma8452_drdy(data);
106 return i2c_smbus_read_i2c_block_data(data->client,
107 MMA8452_OUT_X, 3 * sizeof(__be16), (u8 *) buf);
110 static ssize_t mma8452_show_int_plus_micros(char *buf,
111 const int (*vals)[2], int n)
116 len += scnprintf(buf + len, PAGE_SIZE - len,
117 "%d.%06d ", vals[n][0], vals[n][1]);
119 /* replace trailing space by newline */
125 static int mma8452_get_int_plus_micros_index(const int (*vals)[2], int n,
129 if (val == vals[n][0] && val2 == vals[n][1])
135 static int mma8452_get_odr_index(struct mma8452_data *data)
137 return (data->ctrl_reg1 & MMA8452_CTRL_DR_MASK) >>
138 MMA8452_CTRL_DR_SHIFT;
141 static const int mma8452_samp_freq[8][2] = {
142 {800, 0}, {400, 0}, {200, 0}, {100, 0}, {50, 0}, {12, 500000},
143 {6, 250000}, {1, 560000}
147 * Hardware has fullscale of -2G, -4G, -8G corresponding to raw value -2048
148 * The userspace interface uses m/s^2 and we declare micro units
149 * So scale factor is given by:
150 * g * N * 1000000 / 2048 for N = 2, 4, 8 and g=9.80665
152 static const int mma8452_scales[3][2] = {
153 {0, 9577}, {0, 19154}, {0, 38307}
156 /* Datasheet table 35 (step time vs sample frequency) */
157 static const int mma8452_transient_time_step_us[8] = {
168 /* Datasheet table 18 (normal mode) */
169 static const int mma8452_hp_filter_cutoff[8][4][2] = {
170 { {16, 0}, {8, 0}, {4, 0}, {2, 0} }, /* 800 Hz sample */
171 { {16, 0}, {8, 0}, {4, 0}, {2, 0} }, /* 400 Hz sample */
172 { {8, 0}, {4, 0}, {2, 0}, {1, 0} }, /* 200 Hz sample */
173 { {4, 0}, {2, 0}, {1, 0}, {0, 500000} }, /* 100 Hz sample */
174 { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} }, /* 50 Hz sample */
175 { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} }, /* 12.5 Hz sample */
176 { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} }, /* 6.25 Hz sample */
177 { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} } /* 1.56 Hz sample */
180 static ssize_t mma8452_show_samp_freq_avail(struct device *dev,
181 struct device_attribute *attr, char *buf)
183 return mma8452_show_int_plus_micros(buf, mma8452_samp_freq,
184 ARRAY_SIZE(mma8452_samp_freq));
187 static ssize_t mma8452_show_scale_avail(struct device *dev,
188 struct device_attribute *attr, char *buf)
190 return mma8452_show_int_plus_micros(buf, mma8452_scales,
191 ARRAY_SIZE(mma8452_scales));
194 static ssize_t mma8452_show_hp_cutoff_avail(struct device *dev,
195 struct device_attribute *attr,
198 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
199 struct mma8452_data *data = iio_priv(indio_dev);
200 int i = mma8452_get_odr_index(data);
202 return mma8452_show_int_plus_micros(buf, mma8452_hp_filter_cutoff[i],
203 ARRAY_SIZE(mma8452_hp_filter_cutoff[0]));
206 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(mma8452_show_samp_freq_avail);
207 static IIO_DEVICE_ATTR(in_accel_scale_available, S_IRUGO,
208 mma8452_show_scale_avail, NULL, 0);
209 static IIO_DEVICE_ATTR(in_accel_filter_high_pass_3db_frequency_available,
210 S_IRUGO, mma8452_show_hp_cutoff_avail, NULL, 0);
212 static int mma8452_get_samp_freq_index(struct mma8452_data *data,
215 return mma8452_get_int_plus_micros_index(mma8452_samp_freq,
216 ARRAY_SIZE(mma8452_samp_freq), val, val2);
219 static int mma8452_get_scale_index(struct mma8452_data *data,
222 return mma8452_get_int_plus_micros_index(mma8452_scales,
223 ARRAY_SIZE(mma8452_scales), val, val2);
226 static int mma8452_get_hp_filter_index(struct mma8452_data *data,
229 int i = mma8452_get_odr_index(data);
231 return mma8452_get_int_plus_micros_index(mma8452_hp_filter_cutoff[i],
232 ARRAY_SIZE(mma8452_scales[0]), val, val2);
235 static int mma8452_read_hp_filter(struct mma8452_data *data, int *hz, int *uHz)
239 ret = i2c_smbus_read_byte_data(data->client, MMA8452_HP_FILTER_CUTOFF);
243 i = mma8452_get_odr_index(data);
244 ret &= MMA8452_HP_FILTER_CUTOFF_SEL_MASK;
245 *hz = mma8452_hp_filter_cutoff[i][ret][0];
246 *uHz = mma8452_hp_filter_cutoff[i][ret][1];
251 static int mma8452_read_raw(struct iio_dev *indio_dev,
252 struct iio_chan_spec const *chan,
253 int *val, int *val2, long mask)
255 struct mma8452_data *data = iio_priv(indio_dev);
260 case IIO_CHAN_INFO_RAW:
261 if (iio_buffer_enabled(indio_dev))
264 mutex_lock(&data->lock);
265 ret = mma8452_read(data, buffer);
266 mutex_unlock(&data->lock);
269 *val = sign_extend32(
270 be16_to_cpu(buffer[chan->scan_index]) >> 4, 11);
272 case IIO_CHAN_INFO_SCALE:
273 i = data->data_cfg & MMA8452_DATA_CFG_FS_MASK;
274 *val = mma8452_scales[i][0];
275 *val2 = mma8452_scales[i][1];
276 return IIO_VAL_INT_PLUS_MICRO;
277 case IIO_CHAN_INFO_SAMP_FREQ:
278 i = mma8452_get_odr_index(data);
279 *val = mma8452_samp_freq[i][0];
280 *val2 = mma8452_samp_freq[i][1];
281 return IIO_VAL_INT_PLUS_MICRO;
282 case IIO_CHAN_INFO_CALIBBIAS:
283 ret = i2c_smbus_read_byte_data(data->client, MMA8452_OFF_X +
287 *val = sign_extend32(ret, 7);
289 case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY:
290 if (data->data_cfg & MMA8452_DATA_CFG_HPF_MASK) {
291 ret = mma8452_read_hp_filter(data, val, val2);
298 return IIO_VAL_INT_PLUS_MICRO;
303 static int mma8452_standby(struct mma8452_data *data)
305 return i2c_smbus_write_byte_data(data->client, MMA8452_CTRL_REG1,
306 data->ctrl_reg1 & ~MMA8452_CTRL_ACTIVE);
309 static int mma8452_active(struct mma8452_data *data)
311 return i2c_smbus_write_byte_data(data->client, MMA8452_CTRL_REG1,
315 static int mma8452_change_config(struct mma8452_data *data, u8 reg, u8 val)
319 mutex_lock(&data->lock);
321 /* config can only be changed when in standby */
322 ret = mma8452_standby(data);
326 ret = i2c_smbus_write_byte_data(data->client, reg, val);
330 ret = mma8452_active(data);
336 mutex_unlock(&data->lock);
340 static int mma8452_set_hp_filter_frequency(struct mma8452_data *data,
345 i = mma8452_get_hp_filter_index(data, val, val2);
349 reg = i2c_smbus_read_byte_data(data->client,
350 MMA8452_HP_FILTER_CUTOFF);
353 reg &= ~MMA8452_HP_FILTER_CUTOFF_SEL_MASK;
356 return mma8452_change_config(data, MMA8452_HP_FILTER_CUTOFF, reg);
359 static int mma8452_write_raw(struct iio_dev *indio_dev,
360 struct iio_chan_spec const *chan,
361 int val, int val2, long mask)
363 struct mma8452_data *data = iio_priv(indio_dev);
366 if (iio_buffer_enabled(indio_dev))
370 case IIO_CHAN_INFO_SAMP_FREQ:
371 i = mma8452_get_samp_freq_index(data, val, val2);
375 data->ctrl_reg1 &= ~MMA8452_CTRL_DR_MASK;
376 data->ctrl_reg1 |= i << MMA8452_CTRL_DR_SHIFT;
377 return mma8452_change_config(data, MMA8452_CTRL_REG1,
379 case IIO_CHAN_INFO_SCALE:
380 i = mma8452_get_scale_index(data, val, val2);
383 data->data_cfg &= ~MMA8452_DATA_CFG_FS_MASK;
385 return mma8452_change_config(data, MMA8452_DATA_CFG,
387 case IIO_CHAN_INFO_CALIBBIAS:
388 if (val < -128 || val > 127)
390 return mma8452_change_config(data, MMA8452_OFF_X +
391 chan->scan_index, val);
393 case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY:
394 if (val == 0 && val2 == 0) {
395 data->data_cfg &= ~MMA8452_DATA_CFG_HPF_MASK;
397 data->data_cfg |= MMA8452_DATA_CFG_HPF_MASK;
398 ret = mma8452_set_hp_filter_frequency(data, val, val2);
402 return mma8452_change_config(data, MMA8452_DATA_CFG,
410 static int mma8452_read_thresh(struct iio_dev *indio_dev,
411 const struct iio_chan_spec *chan,
412 enum iio_event_type type,
413 enum iio_event_direction dir,
414 enum iio_event_info info,
417 struct mma8452_data *data = iio_priv(indio_dev);
421 case IIO_EV_INFO_VALUE:
422 ret = i2c_smbus_read_byte_data(data->client,
423 MMA8452_TRANSIENT_THS);
427 *val = ret & MMA8452_TRANSIENT_THS_MASK;
430 case IIO_EV_INFO_PERIOD:
431 ret = i2c_smbus_read_byte_data(data->client,
432 MMA8452_TRANSIENT_COUNT);
436 us = ret * mma8452_transient_time_step_us[
437 mma8452_get_odr_index(data)];
438 *val = us / USEC_PER_SEC;
439 *val2 = us % USEC_PER_SEC;
440 return IIO_VAL_INT_PLUS_MICRO;
442 case IIO_EV_INFO_HIGH_PASS_FILTER_3DB:
443 ret = i2c_smbus_read_byte_data(data->client,
444 MMA8452_TRANSIENT_CFG);
448 if (ret & MMA8452_TRANSIENT_CFG_HPF_BYP) {
452 ret = mma8452_read_hp_filter(data, val, val2);
456 return IIO_VAL_INT_PLUS_MICRO;
463 static int mma8452_write_thresh(struct iio_dev *indio_dev,
464 const struct iio_chan_spec *chan,
465 enum iio_event_type type,
466 enum iio_event_direction dir,
467 enum iio_event_info info,
470 struct mma8452_data *data = iio_priv(indio_dev);
474 case IIO_EV_INFO_VALUE:
475 return mma8452_change_config(data, MMA8452_TRANSIENT_THS,
476 val & MMA8452_TRANSIENT_THS_MASK);
478 case IIO_EV_INFO_PERIOD:
479 steps = (val * USEC_PER_SEC + val2) /
480 mma8452_transient_time_step_us[
481 mma8452_get_odr_index(data)];
486 return mma8452_change_config(data, MMA8452_TRANSIENT_COUNT,
488 case IIO_EV_INFO_HIGH_PASS_FILTER_3DB:
489 reg = i2c_smbus_read_byte_data(data->client,
490 MMA8452_TRANSIENT_CFG);
494 if (val == 0 && val2 == 0) {
495 reg |= MMA8452_TRANSIENT_CFG_HPF_BYP;
497 reg &= ~MMA8452_TRANSIENT_CFG_HPF_BYP;
498 ret = mma8452_set_hp_filter_frequency(data, val, val2);
502 return mma8452_change_config(data, MMA8452_TRANSIENT_CFG, reg);
509 static int mma8452_read_event_config(struct iio_dev *indio_dev,
510 const struct iio_chan_spec *chan,
511 enum iio_event_type type,
512 enum iio_event_direction dir)
514 struct mma8452_data *data = iio_priv(indio_dev);
517 ret = i2c_smbus_read_byte_data(data->client, MMA8452_TRANSIENT_CFG);
521 return ret & MMA8452_TRANSIENT_CFG_CHAN(chan->scan_index) ? 1 : 0;
524 static int mma8452_write_event_config(struct iio_dev *indio_dev,
525 const struct iio_chan_spec *chan,
526 enum iio_event_type type,
527 enum iio_event_direction dir,
530 struct mma8452_data *data = iio_priv(indio_dev);
533 val = i2c_smbus_read_byte_data(data->client, MMA8452_TRANSIENT_CFG);
538 val |= MMA8452_TRANSIENT_CFG_CHAN(chan->scan_index);
540 val &= ~MMA8452_TRANSIENT_CFG_CHAN(chan->scan_index);
542 val |= MMA8452_TRANSIENT_CFG_ELE;
544 return mma8452_change_config(data, MMA8452_TRANSIENT_CFG, val);
547 static void mma8452_transient_interrupt(struct iio_dev *indio_dev)
549 struct mma8452_data *data = iio_priv(indio_dev);
550 s64 ts = iio_get_time_ns();
553 src = i2c_smbus_read_byte_data(data->client, MMA8452_TRANSIENT_SRC);
557 if (src & MMA8452_TRANSIENT_SRC_XTRANSE)
558 iio_push_event(indio_dev,
559 IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X,
564 if (src & MMA8452_TRANSIENT_SRC_YTRANSE)
565 iio_push_event(indio_dev,
566 IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_Y,
571 if (src & MMA8452_TRANSIENT_SRC_ZTRANSE)
572 iio_push_event(indio_dev,
573 IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_Z,
579 static irqreturn_t mma8452_interrupt(int irq, void *p)
581 struct iio_dev *indio_dev = p;
582 struct mma8452_data *data = iio_priv(indio_dev);
586 src = i2c_smbus_read_byte_data(data->client, MMA8452_INT_SRC);
590 if (src & MMA8452_INT_DRDY) {
591 iio_trigger_poll_chained(indio_dev->trig);
595 if (src & MMA8452_INT_TRANS) {
596 mma8452_transient_interrupt(indio_dev);
603 static irqreturn_t mma8452_trigger_handler(int irq, void *p)
605 struct iio_poll_func *pf = p;
606 struct iio_dev *indio_dev = pf->indio_dev;
607 struct mma8452_data *data = iio_priv(indio_dev);
608 u8 buffer[16]; /* 3 16-bit channels + padding + ts */
611 ret = mma8452_read(data, (__be16 *) buffer);
615 iio_push_to_buffers_with_timestamp(indio_dev, buffer,
619 iio_trigger_notify_done(indio_dev->trig);
623 static int mma8452_reg_access_dbg(struct iio_dev *indio_dev,
624 unsigned reg, unsigned writeval,
628 struct mma8452_data *data = iio_priv(indio_dev);
630 if (reg > MMA8452_MAX_REG)
634 return mma8452_change_config(data, reg, writeval);
636 ret = i2c_smbus_read_byte_data(data->client, reg);
645 static const struct iio_event_spec mma8452_transient_event[] = {
647 .type = IIO_EV_TYPE_THRESH,
648 .dir = IIO_EV_DIR_RISING,
649 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
650 .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
651 BIT(IIO_EV_INFO_PERIOD) |
652 BIT(IIO_EV_INFO_HIGH_PASS_FILTER_3DB)
657 * Threshold is configured in fixed 8G/127 steps regardless of
658 * currently selected scale for measurement.
660 static IIO_CONST_ATTR_NAMED(accel_transient_scale, in_accel_scale, "0.617742");
662 static struct attribute *mma8452_event_attributes[] = {
663 &iio_const_attr_accel_transient_scale.dev_attr.attr,
667 static struct attribute_group mma8452_event_attribute_group = {
668 .attrs = mma8452_event_attributes,
672 #define MMA8452_CHANNEL(axis, idx) { \
675 .channel2 = IIO_MOD_##axis, \
676 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
677 BIT(IIO_CHAN_INFO_CALIBBIAS), \
678 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
679 BIT(IIO_CHAN_INFO_SCALE) | \
680 BIT(IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY), \
687 .endianness = IIO_BE, \
689 .event_spec = mma8452_transient_event, \
690 .num_event_specs = ARRAY_SIZE(mma8452_transient_event), \
693 static const struct iio_chan_spec mma8452_channels[] = {
694 MMA8452_CHANNEL(X, 0),
695 MMA8452_CHANNEL(Y, 1),
696 MMA8452_CHANNEL(Z, 2),
697 IIO_CHAN_SOFT_TIMESTAMP(3),
700 static struct attribute *mma8452_attributes[] = {
701 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
702 &iio_dev_attr_in_accel_scale_available.dev_attr.attr,
703 &iio_dev_attr_in_accel_filter_high_pass_3db_frequency_available.dev_attr.attr,
707 static const struct attribute_group mma8452_group = {
708 .attrs = mma8452_attributes,
711 static const struct iio_info mma8452_info = {
712 .attrs = &mma8452_group,
713 .read_raw = &mma8452_read_raw,
714 .write_raw = &mma8452_write_raw,
715 .event_attrs = &mma8452_event_attribute_group,
716 .read_event_value = &mma8452_read_thresh,
717 .write_event_value = &mma8452_write_thresh,
718 .read_event_config = &mma8452_read_event_config,
719 .write_event_config = &mma8452_write_event_config,
720 .debugfs_reg_access = &mma8452_reg_access_dbg,
721 .driver_module = THIS_MODULE,
724 static const unsigned long mma8452_scan_masks[] = {0x7, 0};
726 static int mma8452_data_rdy_trigger_set_state(struct iio_trigger *trig,
729 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
730 struct mma8452_data *data = iio_priv(indio_dev);
733 reg = i2c_smbus_read_byte_data(data->client, MMA8452_CTRL_REG4);
738 reg |= MMA8452_INT_DRDY;
740 reg &= ~MMA8452_INT_DRDY;
742 return mma8452_change_config(data, MMA8452_CTRL_REG4, reg);
745 static int mma8452_validate_device(struct iio_trigger *trig,
746 struct iio_dev *indio_dev)
748 struct iio_dev *indio = iio_trigger_get_drvdata(trig);
750 if (indio != indio_dev)
756 static const struct iio_trigger_ops mma8452_trigger_ops = {
757 .set_trigger_state = mma8452_data_rdy_trigger_set_state,
758 .validate_device = mma8452_validate_device,
759 .owner = THIS_MODULE,
762 static int mma8452_trigger_setup(struct iio_dev *indio_dev)
764 struct mma8452_data *data = iio_priv(indio_dev);
765 struct iio_trigger *trig;
768 trig = devm_iio_trigger_alloc(&data->client->dev, "%s-dev%d",
774 trig->dev.parent = &data->client->dev;
775 trig->ops = &mma8452_trigger_ops;
776 iio_trigger_set_drvdata(trig, indio_dev);
778 ret = iio_trigger_register(trig);
782 indio_dev->trig = trig;
786 static void mma8452_trigger_cleanup(struct iio_dev *indio_dev)
789 iio_trigger_unregister(indio_dev->trig);
792 static int mma8452_reset(struct i2c_client *client)
797 ret = i2c_smbus_write_byte_data(client, MMA8452_CTRL_REG2,
798 MMA8452_CTRL_REG2_RST);
802 for (i = 0; i < 10; i++) {
803 usleep_range(100, 200);
804 ret = i2c_smbus_read_byte_data(client, MMA8452_CTRL_REG2);
806 continue; /* I2C comm reset */
809 if (!(ret & MMA8452_CTRL_REG2_RST))
816 static int mma8452_probe(struct i2c_client *client,
817 const struct i2c_device_id *id)
819 struct mma8452_data *data;
820 struct iio_dev *indio_dev;
823 ret = i2c_smbus_read_byte_data(client, MMA8452_WHO_AM_I);
826 if (ret != MMA8452_DEVICE_ID)
829 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
833 data = iio_priv(indio_dev);
834 data->client = client;
835 mutex_init(&data->lock);
837 i2c_set_clientdata(client, indio_dev);
838 indio_dev->info = &mma8452_info;
839 indio_dev->name = id->name;
840 indio_dev->dev.parent = &client->dev;
841 indio_dev->modes = INDIO_DIRECT_MODE;
842 indio_dev->channels = mma8452_channels;
843 indio_dev->num_channels = ARRAY_SIZE(mma8452_channels);
844 indio_dev->available_scan_masks = mma8452_scan_masks;
846 ret = mma8452_reset(client);
850 data->data_cfg = MMA8452_DATA_CFG_FS_2G;
851 ret = i2c_smbus_write_byte_data(client, MMA8452_DATA_CFG,
857 * By default set transient threshold to max to avoid events if
858 * enabling without configuring threshold.
860 ret = i2c_smbus_write_byte_data(client, MMA8452_TRANSIENT_THS,
861 MMA8452_TRANSIENT_THS_MASK);
867 * Although we enable the transient interrupt source once and
868 * for all here the transient event detection itself is not
869 * enabled until userspace asks for it by
870 * mma8452_write_event_config()
872 int supported_interrupts = MMA8452_INT_DRDY | MMA8452_INT_TRANS;
873 int enabled_interrupts = MMA8452_INT_TRANS;
875 /* Assume wired to INT1 pin */
876 ret = i2c_smbus_write_byte_data(client,
878 supported_interrupts);
882 ret = i2c_smbus_write_byte_data(client,
888 ret = mma8452_trigger_setup(indio_dev);
893 data->ctrl_reg1 = MMA8452_CTRL_ACTIVE |
894 (MMA8452_CTRL_DR_DEFAULT << MMA8452_CTRL_DR_SHIFT);
895 ret = i2c_smbus_write_byte_data(client, MMA8452_CTRL_REG1,
898 goto trigger_cleanup;
900 ret = iio_triggered_buffer_setup(indio_dev, NULL,
901 mma8452_trigger_handler, NULL);
903 goto trigger_cleanup;
906 ret = devm_request_threaded_irq(&client->dev,
908 NULL, mma8452_interrupt,
909 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
910 client->name, indio_dev);
915 ret = iio_device_register(indio_dev);
922 iio_triggered_buffer_cleanup(indio_dev);
925 mma8452_trigger_cleanup(indio_dev);
930 static int mma8452_remove(struct i2c_client *client)
932 struct iio_dev *indio_dev = i2c_get_clientdata(client);
934 iio_device_unregister(indio_dev);
935 iio_triggered_buffer_cleanup(indio_dev);
936 mma8452_trigger_cleanup(indio_dev);
937 mma8452_standby(iio_priv(indio_dev));
942 #ifdef CONFIG_PM_SLEEP
943 static int mma8452_suspend(struct device *dev)
945 return mma8452_standby(iio_priv(i2c_get_clientdata(
946 to_i2c_client(dev))));
949 static int mma8452_resume(struct device *dev)
951 return mma8452_active(iio_priv(i2c_get_clientdata(
952 to_i2c_client(dev))));
955 static SIMPLE_DEV_PM_OPS(mma8452_pm_ops, mma8452_suspend, mma8452_resume);
956 #define MMA8452_PM_OPS (&mma8452_pm_ops)
958 #define MMA8452_PM_OPS NULL
961 static const struct i2c_device_id mma8452_id[] = {
965 MODULE_DEVICE_TABLE(i2c, mma8452_id);
967 static const struct of_device_id mma8452_dt_ids[] = {
968 { .compatible = "fsl,mma8452" },
972 static struct i2c_driver mma8452_driver = {
975 .of_match_table = of_match_ptr(mma8452_dt_ids),
976 .pm = MMA8452_PM_OPS,
978 .probe = mma8452_probe,
979 .remove = mma8452_remove,
980 .id_table = mma8452_id,
982 module_i2c_driver(mma8452_driver);
984 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
985 MODULE_DESCRIPTION("Freescale MMA8452 accelerometer driver");
986 MODULE_LICENSE("GPL");