blob: f30eb9b7a99cdec7270027d054d58ea5afe76d35 [file] [log] [blame]
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001/*
2 * mma8452.c - Support for Freescale MMA8452Q 3-axis 12-bit accelerometer
3 *
4 * Copyright 2014 Peter Meerwald <pmeerw@pmeerw.net>
5 *
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.
9 *
10 * 7-bit I2C slave address 0x1c/0x1d (pin selectable)
11 *
Martin Fuzzey28e34272015-06-01 15:39:52 +020012 * TODO: orientation / freefall events, autosleep
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000013 */
14
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>
Martin Fuzzey28e34272015-06-01 15:39:52 +020022#include <linux/iio/events.h>
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000023#include <linux/delay.h>
24
25#define MMA8452_STATUS 0x00
26#define MMA8452_OUT_X 0x01 /* MSB first, 12-bit */
27#define MMA8452_OUT_Y 0x03
28#define MMA8452_OUT_Z 0x05
Martin Fuzzey28e34272015-06-01 15:39:52 +020029#define MMA8452_INT_SRC 0x0c
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000030#define MMA8452_WHO_AM_I 0x0d
31#define MMA8452_DATA_CFG 0x0e
Martin Fuzzey28e34272015-06-01 15:39:52 +020032#define MMA8452_TRANSIENT_CFG 0x1d
33#define MMA8452_TRANSIENT_CFG_ELE BIT(4)
34#define MMA8452_TRANSIENT_CFG_CHAN(chan) BIT(chan + 1)
35#define MMA8452_TRANSIENT_SRC 0x1e
36#define MMA8452_TRANSIENT_SRC_XTRANSE BIT(1)
37#define MMA8452_TRANSIENT_SRC_YTRANSE BIT(3)
38#define MMA8452_TRANSIENT_SRC_ZTRANSE BIT(5)
39#define MMA8452_TRANSIENT_THS 0x1f
40#define MMA8452_TRANSIENT_THS_MASK 0x7f
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000041#define MMA8452_OFF_X 0x2f
42#define MMA8452_OFF_Y 0x30
43#define MMA8452_OFF_Z 0x31
44#define MMA8452_CTRL_REG1 0x2a
45#define MMA8452_CTRL_REG2 0x2b
Martin Fuzzeyecabae72015-05-13 12:26:38 +020046#define MMA8452_CTRL_REG2_RST BIT(6)
Martin Fuzzey28e34272015-06-01 15:39:52 +020047#define MMA8452_CTRL_REG4 0x2d
48#define MMA8452_CTRL_REG5 0x2e
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000049
Martin Fuzzey2a17698c2015-05-13 12:26:40 +020050#define MMA8452_MAX_REG 0x31
51
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000052#define MMA8452_STATUS_DRDY (BIT(2) | BIT(1) | BIT(0))
53
54#define MMA8452_CTRL_DR_MASK (BIT(5) | BIT(4) | BIT(3))
55#define MMA8452_CTRL_DR_SHIFT 3
56#define MMA8452_CTRL_DR_DEFAULT 0x4 /* 50 Hz sample frequency */
57#define MMA8452_CTRL_ACTIVE BIT(0)
58
59#define MMA8452_DATA_CFG_FS_MASK (BIT(1) | BIT(0))
60#define MMA8452_DATA_CFG_FS_2G 0
61#define MMA8452_DATA_CFG_FS_4G 1
62#define MMA8452_DATA_CFG_FS_8G 2
63
Martin Fuzzey28e34272015-06-01 15:39:52 +020064#define MMA8452_INT_TRANS BIT(5)
65
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000066#define MMA8452_DEVICE_ID 0x2a
67
68struct mma8452_data {
69 struct i2c_client *client;
70 struct mutex lock;
71 u8 ctrl_reg1;
72 u8 data_cfg;
73};
74
75static int mma8452_drdy(struct mma8452_data *data)
76{
77 int tries = 150;
78
79 while (tries-- > 0) {
80 int ret = i2c_smbus_read_byte_data(data->client,
81 MMA8452_STATUS);
82 if (ret < 0)
83 return ret;
84 if ((ret & MMA8452_STATUS_DRDY) == MMA8452_STATUS_DRDY)
85 return 0;
86 msleep(20);
87 }
88
89 dev_err(&data->client->dev, "data not ready\n");
90 return -EIO;
91}
92
93static int mma8452_read(struct mma8452_data *data, __be16 buf[3])
94{
95 int ret = mma8452_drdy(data);
96 if (ret < 0)
97 return ret;
98 return i2c_smbus_read_i2c_block_data(data->client,
99 MMA8452_OUT_X, 3 * sizeof(__be16), (u8 *) buf);
100}
101
102static ssize_t mma8452_show_int_plus_micros(char *buf,
103 const int (*vals)[2], int n)
104{
105 size_t len = 0;
106
107 while (n-- > 0)
108 len += scnprintf(buf + len, PAGE_SIZE - len,
109 "%d.%06d ", vals[n][0], vals[n][1]);
110
111 /* replace trailing space by newline */
112 buf[len - 1] = '\n';
113
114 return len;
115}
116
117static int mma8452_get_int_plus_micros_index(const int (*vals)[2], int n,
118 int val, int val2)
119{
120 while (n-- > 0)
121 if (val == vals[n][0] && val2 == vals[n][1])
122 return n;
123
124 return -EINVAL;
125}
126
127static const int mma8452_samp_freq[8][2] = {
128 {800, 0}, {400, 0}, {200, 0}, {100, 0}, {50, 0}, {12, 500000},
129 {6, 250000}, {1, 560000}
130};
131
Roberta Dobrescuc8761092014-12-30 20:57:54 +0200132/*
Martin Fuzzey71702e62014-11-07 13:54:00 +0000133 * Hardware has fullscale of -2G, -4G, -8G corresponding to raw value -2048
134 * The userspace interface uses m/s^2 and we declare micro units
135 * So scale factor is given by:
136 * g * N * 1000000 / 2048 for N = 2, 4, 8 and g=9.80665
137 */
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000138static const int mma8452_scales[3][2] = {
Martin Fuzzey71702e62014-11-07 13:54:00 +0000139 {0, 9577}, {0, 19154}, {0, 38307}
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000140};
141
142static ssize_t mma8452_show_samp_freq_avail(struct device *dev,
143 struct device_attribute *attr, char *buf)
144{
145 return mma8452_show_int_plus_micros(buf, mma8452_samp_freq,
146 ARRAY_SIZE(mma8452_samp_freq));
147}
148
149static ssize_t mma8452_show_scale_avail(struct device *dev,
150 struct device_attribute *attr, char *buf)
151{
152 return mma8452_show_int_plus_micros(buf, mma8452_scales,
153 ARRAY_SIZE(mma8452_scales));
154}
155
156static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(mma8452_show_samp_freq_avail);
157static IIO_DEVICE_ATTR(in_accel_scale_available, S_IRUGO,
158 mma8452_show_scale_avail, NULL, 0);
159
160static int mma8452_get_samp_freq_index(struct mma8452_data *data,
161 int val, int val2)
162{
163 return mma8452_get_int_plus_micros_index(mma8452_samp_freq,
164 ARRAY_SIZE(mma8452_samp_freq), val, val2);
165}
166
167static int mma8452_get_scale_index(struct mma8452_data *data,
168 int val, int val2)
169{
170 return mma8452_get_int_plus_micros_index(mma8452_scales,
171 ARRAY_SIZE(mma8452_scales), val, val2);
172}
173
174static int mma8452_read_raw(struct iio_dev *indio_dev,
175 struct iio_chan_spec const *chan,
176 int *val, int *val2, long mask)
177{
178 struct mma8452_data *data = iio_priv(indio_dev);
179 __be16 buffer[3];
180 int i, ret;
181
182 switch (mask) {
183 case IIO_CHAN_INFO_RAW:
184 if (iio_buffer_enabled(indio_dev))
185 return -EBUSY;
186
187 mutex_lock(&data->lock);
188 ret = mma8452_read(data, buffer);
189 mutex_unlock(&data->lock);
190 if (ret < 0)
191 return ret;
192 *val = sign_extend32(
193 be16_to_cpu(buffer[chan->scan_index]) >> 4, 11);
194 return IIO_VAL_INT;
195 case IIO_CHAN_INFO_SCALE:
196 i = data->data_cfg & MMA8452_DATA_CFG_FS_MASK;
197 *val = mma8452_scales[i][0];
198 *val2 = mma8452_scales[i][1];
199 return IIO_VAL_INT_PLUS_MICRO;
200 case IIO_CHAN_INFO_SAMP_FREQ:
201 i = (data->ctrl_reg1 & MMA8452_CTRL_DR_MASK) >>
202 MMA8452_CTRL_DR_SHIFT;
203 *val = mma8452_samp_freq[i][0];
204 *val2 = mma8452_samp_freq[i][1];
205 return IIO_VAL_INT_PLUS_MICRO;
206 case IIO_CHAN_INFO_CALIBBIAS:
207 ret = i2c_smbus_read_byte_data(data->client, MMA8452_OFF_X +
208 chan->scan_index);
209 if (ret < 0)
210 return ret;
211 *val = sign_extend32(ret, 7);
212 return IIO_VAL_INT;
213 }
214 return -EINVAL;
215}
216
217static int mma8452_standby(struct mma8452_data *data)
218{
219 return i2c_smbus_write_byte_data(data->client, MMA8452_CTRL_REG1,
220 data->ctrl_reg1 & ~MMA8452_CTRL_ACTIVE);
221}
222
223static int mma8452_active(struct mma8452_data *data)
224{
225 return i2c_smbus_write_byte_data(data->client, MMA8452_CTRL_REG1,
226 data->ctrl_reg1);
227}
228
229static int mma8452_change_config(struct mma8452_data *data, u8 reg, u8 val)
230{
231 int ret;
232
233 mutex_lock(&data->lock);
234
235 /* config can only be changed when in standby */
236 ret = mma8452_standby(data);
237 if (ret < 0)
238 goto fail;
239
240 ret = i2c_smbus_write_byte_data(data->client, reg, val);
241 if (ret < 0)
242 goto fail;
243
244 ret = mma8452_active(data);
245 if (ret < 0)
246 goto fail;
247
248 ret = 0;
249fail:
250 mutex_unlock(&data->lock);
251 return ret;
252}
253
254static int mma8452_write_raw(struct iio_dev *indio_dev,
255 struct iio_chan_spec const *chan,
256 int val, int val2, long mask)
257{
258 struct mma8452_data *data = iio_priv(indio_dev);
259 int i;
260
261 if (iio_buffer_enabled(indio_dev))
262 return -EBUSY;
263
264 switch (mask) {
265 case IIO_CHAN_INFO_SAMP_FREQ:
266 i = mma8452_get_samp_freq_index(data, val, val2);
267 if (i < 0)
268 return -EINVAL;
269
270 data->ctrl_reg1 &= ~MMA8452_CTRL_DR_MASK;
271 data->ctrl_reg1 |= i << MMA8452_CTRL_DR_SHIFT;
272 return mma8452_change_config(data, MMA8452_CTRL_REG1,
273 data->ctrl_reg1);
274 case IIO_CHAN_INFO_SCALE:
275 i = mma8452_get_scale_index(data, val, val2);
276 if (i < 0)
277 return -EINVAL;
278 data->data_cfg &= ~MMA8452_DATA_CFG_FS_MASK;
279 data->data_cfg |= i;
280 return mma8452_change_config(data, MMA8452_DATA_CFG,
281 data->data_cfg);
282 case IIO_CHAN_INFO_CALIBBIAS:
283 if (val < -128 || val > 127)
284 return -EINVAL;
285 return mma8452_change_config(data, MMA8452_OFF_X +
286 chan->scan_index, val);
287 default:
288 return -EINVAL;
289 }
290}
291
Martin Fuzzey28e34272015-06-01 15:39:52 +0200292static int mma8452_read_thresh(struct iio_dev *indio_dev,
293 const struct iio_chan_spec *chan,
294 enum iio_event_type type,
295 enum iio_event_direction dir,
296 enum iio_event_info info,
297 int *val, int *val2)
298{
299 struct mma8452_data *data = iio_priv(indio_dev);
300 int ret;
301
302 ret = i2c_smbus_read_byte_data(data->client, MMA8452_TRANSIENT_THS);
303 if (ret < 0)
304 return ret;
305
306 *val = ret & MMA8452_TRANSIENT_THS_MASK;
307
308 return IIO_VAL_INT;
309}
310
311static int mma8452_write_thresh(struct iio_dev *indio_dev,
312 const struct iio_chan_spec *chan,
313 enum iio_event_type type,
314 enum iio_event_direction dir,
315 enum iio_event_info info,
316 int val, int val2)
317{
318 struct mma8452_data *data = iio_priv(indio_dev);
319
320 return mma8452_change_config(data, MMA8452_TRANSIENT_THS,
321 val & MMA8452_TRANSIENT_THS_MASK);
322}
323
324static int mma8452_read_event_config(struct iio_dev *indio_dev,
325 const struct iio_chan_spec *chan,
326 enum iio_event_type type,
327 enum iio_event_direction dir)
328{
329 struct mma8452_data *data = iio_priv(indio_dev);
330 int ret;
331
332 ret = i2c_smbus_read_byte_data(data->client, MMA8452_TRANSIENT_CFG);
333 if (ret < 0)
334 return ret;
335
336 return ret & MMA8452_TRANSIENT_CFG_CHAN(chan->scan_index) ? 1 : 0;
337}
338
339static int mma8452_write_event_config(struct iio_dev *indio_dev,
340 const struct iio_chan_spec *chan,
341 enum iio_event_type type,
342 enum iio_event_direction dir,
343 int state)
344{
345 struct mma8452_data *data = iio_priv(indio_dev);
346 int val;
347
348 val = i2c_smbus_read_byte_data(data->client, MMA8452_TRANSIENT_CFG);
349 if (val < 0)
350 return val;
351
352 if (state)
353 val |= MMA8452_TRANSIENT_CFG_CHAN(chan->scan_index);
354 else
355 val &= ~MMA8452_TRANSIENT_CFG_CHAN(chan->scan_index);
356
357 val |= MMA8452_TRANSIENT_CFG_ELE;
358
359 return mma8452_change_config(data, MMA8452_TRANSIENT_CFG, val);
360}
361
362static void mma8452_transient_interrupt(struct iio_dev *indio_dev)
363{
364 struct mma8452_data *data = iio_priv(indio_dev);
365 s64 ts = iio_get_time_ns();
366 int src;
367
368 src = i2c_smbus_read_byte_data(data->client, MMA8452_TRANSIENT_SRC);
369 if (src < 0)
370 return;
371
372 if (src & MMA8452_TRANSIENT_SRC_XTRANSE)
373 iio_push_event(indio_dev,
374 IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X,
375 IIO_EV_TYPE_THRESH,
376 IIO_EV_DIR_RISING),
377 ts);
378
379 if (src & MMA8452_TRANSIENT_SRC_YTRANSE)
380 iio_push_event(indio_dev,
381 IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_Y,
382 IIO_EV_TYPE_THRESH,
383 IIO_EV_DIR_RISING),
384 ts);
385
386 if (src & MMA8452_TRANSIENT_SRC_ZTRANSE)
387 iio_push_event(indio_dev,
388 IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_Z,
389 IIO_EV_TYPE_THRESH,
390 IIO_EV_DIR_RISING),
391 ts);
392}
393
394static irqreturn_t mma8452_interrupt(int irq, void *p)
395{
396 struct iio_dev *indio_dev = p;
397 struct mma8452_data *data = iio_priv(indio_dev);
398 int src;
399
400 src = i2c_smbus_read_byte_data(data->client, MMA8452_INT_SRC);
401 if (src < 0)
402 return IRQ_NONE;
403
404 if (src & MMA8452_INT_TRANS) {
405 mma8452_transient_interrupt(indio_dev);
406 return IRQ_HANDLED;
407 }
408
409 return IRQ_NONE;
410}
411
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000412static irqreturn_t mma8452_trigger_handler(int irq, void *p)
413{
414 struct iio_poll_func *pf = p;
415 struct iio_dev *indio_dev = pf->indio_dev;
416 struct mma8452_data *data = iio_priv(indio_dev);
417 u8 buffer[16]; /* 3 16-bit channels + padding + ts */
418 int ret;
419
420 ret = mma8452_read(data, (__be16 *) buffer);
421 if (ret < 0)
422 goto done;
423
424 iio_push_to_buffers_with_timestamp(indio_dev, buffer,
425 iio_get_time_ns());
426
427done:
428 iio_trigger_notify_done(indio_dev->trig);
429 return IRQ_HANDLED;
430}
431
Martin Fuzzey2a17698c2015-05-13 12:26:40 +0200432static int mma8452_reg_access_dbg(struct iio_dev *indio_dev,
433 unsigned reg, unsigned writeval,
434 unsigned *readval)
435{
436 int ret;
437 struct mma8452_data *data = iio_priv(indio_dev);
438
439 if (reg > MMA8452_MAX_REG)
440 return -EINVAL;
441
442 if (!readval)
443 return mma8452_change_config(data, reg, writeval);
444
445 ret = i2c_smbus_read_byte_data(data->client, reg);
446 if (ret < 0)
447 return ret;
448
449 *readval = ret;
450
451 return 0;
452}
453
Martin Fuzzey28e34272015-06-01 15:39:52 +0200454static const struct iio_event_spec mma8452_transient_event[] = {
455 {
456 .type = IIO_EV_TYPE_THRESH,
457 .dir = IIO_EV_DIR_RISING,
458 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
459 .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE)
460 },
461};
462
463/*
464 * Threshold is configured in fixed 8G/127 steps regardless of
465 * currently selected scale for measurement.
466 */
467static IIO_CONST_ATTR_NAMED(accel_transient_scale, in_accel_scale, "0.617742");
468
469static struct attribute *mma8452_event_attributes[] = {
470 &iio_const_attr_accel_transient_scale.dev_attr.attr,
471 NULL,
472};
473
474static struct attribute_group mma8452_event_attribute_group = {
475 .attrs = mma8452_event_attributes,
476 .name = "events",
477};
478
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000479#define MMA8452_CHANNEL(axis, idx) { \
480 .type = IIO_ACCEL, \
481 .modified = 1, \
482 .channel2 = IIO_MOD_##axis, \
483 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
484 BIT(IIO_CHAN_INFO_CALIBBIAS), \
485 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
486 BIT(IIO_CHAN_INFO_SCALE), \
487 .scan_index = idx, \
488 .scan_type = { \
489 .sign = 's', \
490 .realbits = 12, \
491 .storagebits = 16, \
492 .shift = 4, \
493 .endianness = IIO_BE, \
494 }, \
Martin Fuzzey28e34272015-06-01 15:39:52 +0200495 .event_spec = mma8452_transient_event, \
496 .num_event_specs = ARRAY_SIZE(mma8452_transient_event), \
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000497}
498
499static const struct iio_chan_spec mma8452_channels[] = {
500 MMA8452_CHANNEL(X, 0),
501 MMA8452_CHANNEL(Y, 1),
502 MMA8452_CHANNEL(Z, 2),
503 IIO_CHAN_SOFT_TIMESTAMP(3),
504};
505
506static struct attribute *mma8452_attributes[] = {
507 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
508 &iio_dev_attr_in_accel_scale_available.dev_attr.attr,
509 NULL
510};
511
512static const struct attribute_group mma8452_group = {
513 .attrs = mma8452_attributes,
514};
515
516static const struct iio_info mma8452_info = {
517 .attrs = &mma8452_group,
518 .read_raw = &mma8452_read_raw,
519 .write_raw = &mma8452_write_raw,
Martin Fuzzey28e34272015-06-01 15:39:52 +0200520 .event_attrs = &mma8452_event_attribute_group,
521 .read_event_value = &mma8452_read_thresh,
522 .write_event_value = &mma8452_write_thresh,
523 .read_event_config = &mma8452_read_event_config,
524 .write_event_config = &mma8452_write_event_config,
Martin Fuzzey2a17698c2015-05-13 12:26:40 +0200525 .debugfs_reg_access = &mma8452_reg_access_dbg,
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000526 .driver_module = THIS_MODULE,
527};
528
529static const unsigned long mma8452_scan_masks[] = {0x7, 0};
530
Martin Fuzzeyecabae72015-05-13 12:26:38 +0200531static int mma8452_reset(struct i2c_client *client)
532{
533 int i;
534 int ret;
535
536 ret = i2c_smbus_write_byte_data(client, MMA8452_CTRL_REG2,
537 MMA8452_CTRL_REG2_RST);
538 if (ret < 0)
539 return ret;
540
541 for (i = 0; i < 10; i++) {
542 usleep_range(100, 200);
543 ret = i2c_smbus_read_byte_data(client, MMA8452_CTRL_REG2);
544 if (ret == -EIO)
545 continue; /* I2C comm reset */
546 if (ret < 0)
547 return ret;
548 if (!(ret & MMA8452_CTRL_REG2_RST))
549 return 0;
550 }
551
552 return -ETIMEDOUT;
553}
554
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000555static int mma8452_probe(struct i2c_client *client,
556 const struct i2c_device_id *id)
557{
558 struct mma8452_data *data;
559 struct iio_dev *indio_dev;
560 int ret;
561
562 ret = i2c_smbus_read_byte_data(client, MMA8452_WHO_AM_I);
563 if (ret < 0)
564 return ret;
565 if (ret != MMA8452_DEVICE_ID)
566 return -ENODEV;
567
568 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
569 if (!indio_dev)
570 return -ENOMEM;
571
572 data = iio_priv(indio_dev);
573 data->client = client;
574 mutex_init(&data->lock);
575
576 i2c_set_clientdata(client, indio_dev);
577 indio_dev->info = &mma8452_info;
578 indio_dev->name = id->name;
579 indio_dev->dev.parent = &client->dev;
580 indio_dev->modes = INDIO_DIRECT_MODE;
581 indio_dev->channels = mma8452_channels;
582 indio_dev->num_channels = ARRAY_SIZE(mma8452_channels);
583 indio_dev->available_scan_masks = mma8452_scan_masks;
584
Martin Fuzzeyecabae72015-05-13 12:26:38 +0200585 ret = mma8452_reset(client);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000586 if (ret < 0)
587 return ret;
588
589 data->data_cfg = MMA8452_DATA_CFG_FS_2G;
590 ret = i2c_smbus_write_byte_data(client, MMA8452_DATA_CFG,
591 data->data_cfg);
592 if (ret < 0)
593 return ret;
594
Martin Fuzzey28e34272015-06-01 15:39:52 +0200595 /*
596 * By default set transient threshold to max to avoid events if
597 * enabling without configuring threshold.
598 */
599 ret = i2c_smbus_write_byte_data(client, MMA8452_TRANSIENT_THS,
600 MMA8452_TRANSIENT_THS_MASK);
601 if (ret < 0)
602 return ret;
603
604 if (client->irq) {
605 /*
606 * Although we enable the transient interrupt source once and
607 * for all here the transient event detection itself is not
608 * enabled until userspace asks for it by
609 * mma8452_write_event_config()
610 */
611 int supported_interrupts = MMA8452_INT_TRANS;
612
613 /* Assume wired to INT1 pin */
614 ret = i2c_smbus_write_byte_data(client,
615 MMA8452_CTRL_REG5,
616 supported_interrupts);
617 if (ret < 0)
618 return ret;
619
620 ret = i2c_smbus_write_byte_data(client,
621 MMA8452_CTRL_REG4,
622 supported_interrupts);
623 if (ret < 0)
624 return ret;
625 }
626
Martin Fuzzeyecabae72015-05-13 12:26:38 +0200627 data->ctrl_reg1 = MMA8452_CTRL_ACTIVE |
628 (MMA8452_CTRL_DR_DEFAULT << MMA8452_CTRL_DR_SHIFT);
629 ret = i2c_smbus_write_byte_data(client, MMA8452_CTRL_REG1,
630 data->ctrl_reg1);
631 if (ret < 0)
632 return ret;
633
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000634 ret = iio_triggered_buffer_setup(indio_dev, NULL,
635 mma8452_trigger_handler, NULL);
636 if (ret < 0)
637 return ret;
638
Martin Fuzzey28e34272015-06-01 15:39:52 +0200639 if (client->irq) {
640 ret = devm_request_threaded_irq(&client->dev,
641 client->irq,
642 NULL, mma8452_interrupt,
643 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
644 client->name, indio_dev);
645 if (ret)
646 goto buffer_cleanup;
647 }
648
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000649 ret = iio_device_register(indio_dev);
650 if (ret < 0)
651 goto buffer_cleanup;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200652
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000653 return 0;
654
655buffer_cleanup:
656 iio_triggered_buffer_cleanup(indio_dev);
657 return ret;
658}
659
660static int mma8452_remove(struct i2c_client *client)
661{
662 struct iio_dev *indio_dev = i2c_get_clientdata(client);
663
664 iio_device_unregister(indio_dev);
665 iio_triggered_buffer_cleanup(indio_dev);
666 mma8452_standby(iio_priv(indio_dev));
667
668 return 0;
669}
670
671#ifdef CONFIG_PM_SLEEP
672static int mma8452_suspend(struct device *dev)
673{
674 return mma8452_standby(iio_priv(i2c_get_clientdata(
675 to_i2c_client(dev))));
676}
677
678static int mma8452_resume(struct device *dev)
679{
680 return mma8452_active(iio_priv(i2c_get_clientdata(
681 to_i2c_client(dev))));
682}
683
684static SIMPLE_DEV_PM_OPS(mma8452_pm_ops, mma8452_suspend, mma8452_resume);
685#define MMA8452_PM_OPS (&mma8452_pm_ops)
686#else
687#define MMA8452_PM_OPS NULL
688#endif
689
690static const struct i2c_device_id mma8452_id[] = {
691 { "mma8452", 0 },
692 { }
693};
694MODULE_DEVICE_TABLE(i2c, mma8452_id);
695
Martin Fuzzeya3fb96a2014-11-07 14:06:00 +0000696static const struct of_device_id mma8452_dt_ids[] = {
697 { .compatible = "fsl,mma8452" },
698 { }
699};
700
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000701static struct i2c_driver mma8452_driver = {
702 .driver = {
703 .name = "mma8452",
Martin Fuzzeya3fb96a2014-11-07 14:06:00 +0000704 .of_match_table = of_match_ptr(mma8452_dt_ids),
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000705 .pm = MMA8452_PM_OPS,
706 },
707 .probe = mma8452_probe,
708 .remove = mma8452_remove,
709 .id_table = mma8452_id,
710};
711module_i2c_driver(mma8452_driver);
712
713MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
714MODULE_DESCRIPTION("Freescale MMA8452 accelerometer driver");
715MODULE_LICENSE("GPL");