blob: 7429df3a191eb21da0705f2e0f47fb12f39ac431 [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
Martin Fuzzey5dbbd192015-06-01 15:39:54 +020041#define MMA8452_TRANSIENT_COUNT 0x20
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000042#define MMA8452_OFF_X 0x2f
43#define MMA8452_OFF_Y 0x30
44#define MMA8452_OFF_Z 0x31
45#define MMA8452_CTRL_REG1 0x2a
46#define MMA8452_CTRL_REG2 0x2b
Martin Fuzzeyecabae72015-05-13 12:26:38 +020047#define MMA8452_CTRL_REG2_RST BIT(6)
Martin Fuzzey28e34272015-06-01 15:39:52 +020048#define MMA8452_CTRL_REG4 0x2d
49#define MMA8452_CTRL_REG5 0x2e
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000050
Martin Fuzzey2a17698c2015-05-13 12:26:40 +020051#define MMA8452_MAX_REG 0x31
52
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000053#define MMA8452_STATUS_DRDY (BIT(2) | BIT(1) | BIT(0))
54
55#define MMA8452_CTRL_DR_MASK (BIT(5) | BIT(4) | BIT(3))
56#define MMA8452_CTRL_DR_SHIFT 3
57#define MMA8452_CTRL_DR_DEFAULT 0x4 /* 50 Hz sample frequency */
58#define MMA8452_CTRL_ACTIVE BIT(0)
59
60#define MMA8452_DATA_CFG_FS_MASK (BIT(1) | BIT(0))
61#define MMA8452_DATA_CFG_FS_2G 0
62#define MMA8452_DATA_CFG_FS_4G 1
63#define MMA8452_DATA_CFG_FS_8G 2
64
Martin Fuzzey28e34272015-06-01 15:39:52 +020065#define MMA8452_INT_TRANS BIT(5)
66
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000067#define MMA8452_DEVICE_ID 0x2a
68
69struct mma8452_data {
70 struct i2c_client *client;
71 struct mutex lock;
72 u8 ctrl_reg1;
73 u8 data_cfg;
74};
75
76static int mma8452_drdy(struct mma8452_data *data)
77{
78 int tries = 150;
79
80 while (tries-- > 0) {
81 int ret = i2c_smbus_read_byte_data(data->client,
82 MMA8452_STATUS);
83 if (ret < 0)
84 return ret;
85 if ((ret & MMA8452_STATUS_DRDY) == MMA8452_STATUS_DRDY)
86 return 0;
87 msleep(20);
88 }
89
90 dev_err(&data->client->dev, "data not ready\n");
91 return -EIO;
92}
93
94static int mma8452_read(struct mma8452_data *data, __be16 buf[3])
95{
96 int ret = mma8452_drdy(data);
97 if (ret < 0)
98 return ret;
99 return i2c_smbus_read_i2c_block_data(data->client,
100 MMA8452_OUT_X, 3 * sizeof(__be16), (u8 *) buf);
101}
102
103static ssize_t mma8452_show_int_plus_micros(char *buf,
104 const int (*vals)[2], int n)
105{
106 size_t len = 0;
107
108 while (n-- > 0)
109 len += scnprintf(buf + len, PAGE_SIZE - len,
110 "%d.%06d ", vals[n][0], vals[n][1]);
111
112 /* replace trailing space by newline */
113 buf[len - 1] = '\n';
114
115 return len;
116}
117
118static int mma8452_get_int_plus_micros_index(const int (*vals)[2], int n,
119 int val, int val2)
120{
121 while (n-- > 0)
122 if (val == vals[n][0] && val2 == vals[n][1])
123 return n;
124
125 return -EINVAL;
126}
127
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200128static int mma8452_get_odr_index(struct mma8452_data *data)
129{
130 return (data->ctrl_reg1 & MMA8452_CTRL_DR_MASK) >>
131 MMA8452_CTRL_DR_SHIFT;
132}
133
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000134static const int mma8452_samp_freq[8][2] = {
135 {800, 0}, {400, 0}, {200, 0}, {100, 0}, {50, 0}, {12, 500000},
136 {6, 250000}, {1, 560000}
137};
138
Roberta Dobrescuc8761092014-12-30 20:57:54 +0200139/*
Martin Fuzzey71702e62014-11-07 13:54:00 +0000140 * Hardware has fullscale of -2G, -4G, -8G corresponding to raw value -2048
141 * The userspace interface uses m/s^2 and we declare micro units
142 * So scale factor is given by:
143 * g * N * 1000000 / 2048 for N = 2, 4, 8 and g=9.80665
144 */
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000145static const int mma8452_scales[3][2] = {
Martin Fuzzey71702e62014-11-07 13:54:00 +0000146 {0, 9577}, {0, 19154}, {0, 38307}
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000147};
148
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200149/* Datasheet table 35 (step time vs sample frequency) */
150static const int mma8452_transient_time_step_us[8] = {
151 1250,
152 2500,
153 5000,
154 10000,
155 20000,
156 20000,
157 20000,
158 20000
159};
160
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000161static ssize_t mma8452_show_samp_freq_avail(struct device *dev,
162 struct device_attribute *attr, char *buf)
163{
164 return mma8452_show_int_plus_micros(buf, mma8452_samp_freq,
165 ARRAY_SIZE(mma8452_samp_freq));
166}
167
168static ssize_t mma8452_show_scale_avail(struct device *dev,
169 struct device_attribute *attr, char *buf)
170{
171 return mma8452_show_int_plus_micros(buf, mma8452_scales,
172 ARRAY_SIZE(mma8452_scales));
173}
174
175static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(mma8452_show_samp_freq_avail);
176static IIO_DEVICE_ATTR(in_accel_scale_available, S_IRUGO,
177 mma8452_show_scale_avail, NULL, 0);
178
179static int mma8452_get_samp_freq_index(struct mma8452_data *data,
180 int val, int val2)
181{
182 return mma8452_get_int_plus_micros_index(mma8452_samp_freq,
183 ARRAY_SIZE(mma8452_samp_freq), val, val2);
184}
185
186static int mma8452_get_scale_index(struct mma8452_data *data,
187 int val, int val2)
188{
189 return mma8452_get_int_plus_micros_index(mma8452_scales,
190 ARRAY_SIZE(mma8452_scales), val, val2);
191}
192
193static int mma8452_read_raw(struct iio_dev *indio_dev,
194 struct iio_chan_spec const *chan,
195 int *val, int *val2, long mask)
196{
197 struct mma8452_data *data = iio_priv(indio_dev);
198 __be16 buffer[3];
199 int i, ret;
200
201 switch (mask) {
202 case IIO_CHAN_INFO_RAW:
203 if (iio_buffer_enabled(indio_dev))
204 return -EBUSY;
205
206 mutex_lock(&data->lock);
207 ret = mma8452_read(data, buffer);
208 mutex_unlock(&data->lock);
209 if (ret < 0)
210 return ret;
211 *val = sign_extend32(
212 be16_to_cpu(buffer[chan->scan_index]) >> 4, 11);
213 return IIO_VAL_INT;
214 case IIO_CHAN_INFO_SCALE:
215 i = data->data_cfg & MMA8452_DATA_CFG_FS_MASK;
216 *val = mma8452_scales[i][0];
217 *val2 = mma8452_scales[i][1];
218 return IIO_VAL_INT_PLUS_MICRO;
219 case IIO_CHAN_INFO_SAMP_FREQ:
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200220 i = mma8452_get_odr_index(data);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000221 *val = mma8452_samp_freq[i][0];
222 *val2 = mma8452_samp_freq[i][1];
223 return IIO_VAL_INT_PLUS_MICRO;
224 case IIO_CHAN_INFO_CALIBBIAS:
225 ret = i2c_smbus_read_byte_data(data->client, MMA8452_OFF_X +
226 chan->scan_index);
227 if (ret < 0)
228 return ret;
229 *val = sign_extend32(ret, 7);
230 return IIO_VAL_INT;
231 }
232 return -EINVAL;
233}
234
235static int mma8452_standby(struct mma8452_data *data)
236{
237 return i2c_smbus_write_byte_data(data->client, MMA8452_CTRL_REG1,
238 data->ctrl_reg1 & ~MMA8452_CTRL_ACTIVE);
239}
240
241static int mma8452_active(struct mma8452_data *data)
242{
243 return i2c_smbus_write_byte_data(data->client, MMA8452_CTRL_REG1,
244 data->ctrl_reg1);
245}
246
247static int mma8452_change_config(struct mma8452_data *data, u8 reg, u8 val)
248{
249 int ret;
250
251 mutex_lock(&data->lock);
252
253 /* config can only be changed when in standby */
254 ret = mma8452_standby(data);
255 if (ret < 0)
256 goto fail;
257
258 ret = i2c_smbus_write_byte_data(data->client, reg, val);
259 if (ret < 0)
260 goto fail;
261
262 ret = mma8452_active(data);
263 if (ret < 0)
264 goto fail;
265
266 ret = 0;
267fail:
268 mutex_unlock(&data->lock);
269 return ret;
270}
271
272static int mma8452_write_raw(struct iio_dev *indio_dev,
273 struct iio_chan_spec const *chan,
274 int val, int val2, long mask)
275{
276 struct mma8452_data *data = iio_priv(indio_dev);
277 int i;
278
279 if (iio_buffer_enabled(indio_dev))
280 return -EBUSY;
281
282 switch (mask) {
283 case IIO_CHAN_INFO_SAMP_FREQ:
284 i = mma8452_get_samp_freq_index(data, val, val2);
285 if (i < 0)
286 return -EINVAL;
287
288 data->ctrl_reg1 &= ~MMA8452_CTRL_DR_MASK;
289 data->ctrl_reg1 |= i << MMA8452_CTRL_DR_SHIFT;
290 return mma8452_change_config(data, MMA8452_CTRL_REG1,
291 data->ctrl_reg1);
292 case IIO_CHAN_INFO_SCALE:
293 i = mma8452_get_scale_index(data, val, val2);
294 if (i < 0)
295 return -EINVAL;
296 data->data_cfg &= ~MMA8452_DATA_CFG_FS_MASK;
297 data->data_cfg |= i;
298 return mma8452_change_config(data, MMA8452_DATA_CFG,
299 data->data_cfg);
300 case IIO_CHAN_INFO_CALIBBIAS:
301 if (val < -128 || val > 127)
302 return -EINVAL;
303 return mma8452_change_config(data, MMA8452_OFF_X +
304 chan->scan_index, val);
305 default:
306 return -EINVAL;
307 }
308}
309
Martin Fuzzey28e34272015-06-01 15:39:52 +0200310static int mma8452_read_thresh(struct iio_dev *indio_dev,
311 const struct iio_chan_spec *chan,
312 enum iio_event_type type,
313 enum iio_event_direction dir,
314 enum iio_event_info info,
315 int *val, int *val2)
316{
317 struct mma8452_data *data = iio_priv(indio_dev);
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200318 int ret, us;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200319
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200320 switch (info) {
321 case IIO_EV_INFO_VALUE:
322 ret = i2c_smbus_read_byte_data(data->client,
323 MMA8452_TRANSIENT_THS);
324 if (ret < 0)
325 return ret;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200326
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200327 *val = ret & MMA8452_TRANSIENT_THS_MASK;
328 return IIO_VAL_INT;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200329
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200330 case IIO_EV_INFO_PERIOD:
331 ret = i2c_smbus_read_byte_data(data->client,
332 MMA8452_TRANSIENT_COUNT);
333 if (ret < 0)
334 return ret;
335
336 us = ret * mma8452_transient_time_step_us[
337 mma8452_get_odr_index(data)];
338 *val = us / USEC_PER_SEC;
339 *val2 = us % USEC_PER_SEC;
340 return IIO_VAL_INT_PLUS_MICRO;
341
342 default:
343 return -EINVAL;
344 }
Martin Fuzzey28e34272015-06-01 15:39:52 +0200345}
346
347static int mma8452_write_thresh(struct iio_dev *indio_dev,
348 const struct iio_chan_spec *chan,
349 enum iio_event_type type,
350 enum iio_event_direction dir,
351 enum iio_event_info info,
352 int val, int val2)
353{
354 struct mma8452_data *data = iio_priv(indio_dev);
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200355 int steps;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200356
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200357 switch (info) {
358 case IIO_EV_INFO_VALUE:
359 return mma8452_change_config(data, MMA8452_TRANSIENT_THS,
360 val & MMA8452_TRANSIENT_THS_MASK);
361
362 case IIO_EV_INFO_PERIOD:
363 steps = (val * USEC_PER_SEC + val2) /
364 mma8452_transient_time_step_us[
365 mma8452_get_odr_index(data)];
366
367 if (steps > 0xff)
368 return -EINVAL;
369
370 return mma8452_change_config(data, MMA8452_TRANSIENT_COUNT,
371 steps);
372 default:
373 return -EINVAL;
374 }
Martin Fuzzey28e34272015-06-01 15:39:52 +0200375}
376
377static int mma8452_read_event_config(struct iio_dev *indio_dev,
378 const struct iio_chan_spec *chan,
379 enum iio_event_type type,
380 enum iio_event_direction dir)
381{
382 struct mma8452_data *data = iio_priv(indio_dev);
383 int ret;
384
385 ret = i2c_smbus_read_byte_data(data->client, MMA8452_TRANSIENT_CFG);
386 if (ret < 0)
387 return ret;
388
389 return ret & MMA8452_TRANSIENT_CFG_CHAN(chan->scan_index) ? 1 : 0;
390}
391
392static int mma8452_write_event_config(struct iio_dev *indio_dev,
393 const struct iio_chan_spec *chan,
394 enum iio_event_type type,
395 enum iio_event_direction dir,
396 int state)
397{
398 struct mma8452_data *data = iio_priv(indio_dev);
399 int val;
400
401 val = i2c_smbus_read_byte_data(data->client, MMA8452_TRANSIENT_CFG);
402 if (val < 0)
403 return val;
404
405 if (state)
406 val |= MMA8452_TRANSIENT_CFG_CHAN(chan->scan_index);
407 else
408 val &= ~MMA8452_TRANSIENT_CFG_CHAN(chan->scan_index);
409
410 val |= MMA8452_TRANSIENT_CFG_ELE;
411
412 return mma8452_change_config(data, MMA8452_TRANSIENT_CFG, val);
413}
414
415static void mma8452_transient_interrupt(struct iio_dev *indio_dev)
416{
417 struct mma8452_data *data = iio_priv(indio_dev);
418 s64 ts = iio_get_time_ns();
419 int src;
420
421 src = i2c_smbus_read_byte_data(data->client, MMA8452_TRANSIENT_SRC);
422 if (src < 0)
423 return;
424
425 if (src & MMA8452_TRANSIENT_SRC_XTRANSE)
426 iio_push_event(indio_dev,
427 IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X,
428 IIO_EV_TYPE_THRESH,
429 IIO_EV_DIR_RISING),
430 ts);
431
432 if (src & MMA8452_TRANSIENT_SRC_YTRANSE)
433 iio_push_event(indio_dev,
434 IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_Y,
435 IIO_EV_TYPE_THRESH,
436 IIO_EV_DIR_RISING),
437 ts);
438
439 if (src & MMA8452_TRANSIENT_SRC_ZTRANSE)
440 iio_push_event(indio_dev,
441 IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_Z,
442 IIO_EV_TYPE_THRESH,
443 IIO_EV_DIR_RISING),
444 ts);
445}
446
447static irqreturn_t mma8452_interrupt(int irq, void *p)
448{
449 struct iio_dev *indio_dev = p;
450 struct mma8452_data *data = iio_priv(indio_dev);
451 int src;
452
453 src = i2c_smbus_read_byte_data(data->client, MMA8452_INT_SRC);
454 if (src < 0)
455 return IRQ_NONE;
456
457 if (src & MMA8452_INT_TRANS) {
458 mma8452_transient_interrupt(indio_dev);
459 return IRQ_HANDLED;
460 }
461
462 return IRQ_NONE;
463}
464
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000465static irqreturn_t mma8452_trigger_handler(int irq, void *p)
466{
467 struct iio_poll_func *pf = p;
468 struct iio_dev *indio_dev = pf->indio_dev;
469 struct mma8452_data *data = iio_priv(indio_dev);
470 u8 buffer[16]; /* 3 16-bit channels + padding + ts */
471 int ret;
472
473 ret = mma8452_read(data, (__be16 *) buffer);
474 if (ret < 0)
475 goto done;
476
477 iio_push_to_buffers_with_timestamp(indio_dev, buffer,
478 iio_get_time_ns());
479
480done:
481 iio_trigger_notify_done(indio_dev->trig);
482 return IRQ_HANDLED;
483}
484
Martin Fuzzey2a17698c2015-05-13 12:26:40 +0200485static int mma8452_reg_access_dbg(struct iio_dev *indio_dev,
486 unsigned reg, unsigned writeval,
487 unsigned *readval)
488{
489 int ret;
490 struct mma8452_data *data = iio_priv(indio_dev);
491
492 if (reg > MMA8452_MAX_REG)
493 return -EINVAL;
494
495 if (!readval)
496 return mma8452_change_config(data, reg, writeval);
497
498 ret = i2c_smbus_read_byte_data(data->client, reg);
499 if (ret < 0)
500 return ret;
501
502 *readval = ret;
503
504 return 0;
505}
506
Martin Fuzzey28e34272015-06-01 15:39:52 +0200507static const struct iio_event_spec mma8452_transient_event[] = {
508 {
509 .type = IIO_EV_TYPE_THRESH,
510 .dir = IIO_EV_DIR_RISING,
511 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200512 .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
513 BIT(IIO_EV_INFO_PERIOD)
Martin Fuzzey28e34272015-06-01 15:39:52 +0200514 },
515};
516
517/*
518 * Threshold is configured in fixed 8G/127 steps regardless of
519 * currently selected scale for measurement.
520 */
521static IIO_CONST_ATTR_NAMED(accel_transient_scale, in_accel_scale, "0.617742");
522
523static struct attribute *mma8452_event_attributes[] = {
524 &iio_const_attr_accel_transient_scale.dev_attr.attr,
525 NULL,
526};
527
528static struct attribute_group mma8452_event_attribute_group = {
529 .attrs = mma8452_event_attributes,
530 .name = "events",
531};
532
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000533#define MMA8452_CHANNEL(axis, idx) { \
534 .type = IIO_ACCEL, \
535 .modified = 1, \
536 .channel2 = IIO_MOD_##axis, \
537 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
538 BIT(IIO_CHAN_INFO_CALIBBIAS), \
539 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
540 BIT(IIO_CHAN_INFO_SCALE), \
541 .scan_index = idx, \
542 .scan_type = { \
543 .sign = 's', \
544 .realbits = 12, \
545 .storagebits = 16, \
546 .shift = 4, \
547 .endianness = IIO_BE, \
548 }, \
Martin Fuzzey28e34272015-06-01 15:39:52 +0200549 .event_spec = mma8452_transient_event, \
550 .num_event_specs = ARRAY_SIZE(mma8452_transient_event), \
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000551}
552
553static const struct iio_chan_spec mma8452_channels[] = {
554 MMA8452_CHANNEL(X, 0),
555 MMA8452_CHANNEL(Y, 1),
556 MMA8452_CHANNEL(Z, 2),
557 IIO_CHAN_SOFT_TIMESTAMP(3),
558};
559
560static struct attribute *mma8452_attributes[] = {
561 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
562 &iio_dev_attr_in_accel_scale_available.dev_attr.attr,
563 NULL
564};
565
566static const struct attribute_group mma8452_group = {
567 .attrs = mma8452_attributes,
568};
569
570static const struct iio_info mma8452_info = {
571 .attrs = &mma8452_group,
572 .read_raw = &mma8452_read_raw,
573 .write_raw = &mma8452_write_raw,
Martin Fuzzey28e34272015-06-01 15:39:52 +0200574 .event_attrs = &mma8452_event_attribute_group,
575 .read_event_value = &mma8452_read_thresh,
576 .write_event_value = &mma8452_write_thresh,
577 .read_event_config = &mma8452_read_event_config,
578 .write_event_config = &mma8452_write_event_config,
Martin Fuzzey2a17698c2015-05-13 12:26:40 +0200579 .debugfs_reg_access = &mma8452_reg_access_dbg,
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000580 .driver_module = THIS_MODULE,
581};
582
583static const unsigned long mma8452_scan_masks[] = {0x7, 0};
584
Martin Fuzzeyecabae72015-05-13 12:26:38 +0200585static int mma8452_reset(struct i2c_client *client)
586{
587 int i;
588 int ret;
589
590 ret = i2c_smbus_write_byte_data(client, MMA8452_CTRL_REG2,
591 MMA8452_CTRL_REG2_RST);
592 if (ret < 0)
593 return ret;
594
595 for (i = 0; i < 10; i++) {
596 usleep_range(100, 200);
597 ret = i2c_smbus_read_byte_data(client, MMA8452_CTRL_REG2);
598 if (ret == -EIO)
599 continue; /* I2C comm reset */
600 if (ret < 0)
601 return ret;
602 if (!(ret & MMA8452_CTRL_REG2_RST))
603 return 0;
604 }
605
606 return -ETIMEDOUT;
607}
608
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000609static int mma8452_probe(struct i2c_client *client,
610 const struct i2c_device_id *id)
611{
612 struct mma8452_data *data;
613 struct iio_dev *indio_dev;
614 int ret;
615
616 ret = i2c_smbus_read_byte_data(client, MMA8452_WHO_AM_I);
617 if (ret < 0)
618 return ret;
619 if (ret != MMA8452_DEVICE_ID)
620 return -ENODEV;
621
622 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
623 if (!indio_dev)
624 return -ENOMEM;
625
626 data = iio_priv(indio_dev);
627 data->client = client;
628 mutex_init(&data->lock);
629
630 i2c_set_clientdata(client, indio_dev);
631 indio_dev->info = &mma8452_info;
632 indio_dev->name = id->name;
633 indio_dev->dev.parent = &client->dev;
634 indio_dev->modes = INDIO_DIRECT_MODE;
635 indio_dev->channels = mma8452_channels;
636 indio_dev->num_channels = ARRAY_SIZE(mma8452_channels);
637 indio_dev->available_scan_masks = mma8452_scan_masks;
638
Martin Fuzzeyecabae72015-05-13 12:26:38 +0200639 ret = mma8452_reset(client);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000640 if (ret < 0)
641 return ret;
642
643 data->data_cfg = MMA8452_DATA_CFG_FS_2G;
644 ret = i2c_smbus_write_byte_data(client, MMA8452_DATA_CFG,
645 data->data_cfg);
646 if (ret < 0)
647 return ret;
648
Martin Fuzzey28e34272015-06-01 15:39:52 +0200649 /*
650 * By default set transient threshold to max to avoid events if
651 * enabling without configuring threshold.
652 */
653 ret = i2c_smbus_write_byte_data(client, MMA8452_TRANSIENT_THS,
654 MMA8452_TRANSIENT_THS_MASK);
655 if (ret < 0)
656 return ret;
657
658 if (client->irq) {
659 /*
660 * Although we enable the transient interrupt source once and
661 * for all here the transient event detection itself is not
662 * enabled until userspace asks for it by
663 * mma8452_write_event_config()
664 */
665 int supported_interrupts = MMA8452_INT_TRANS;
666
667 /* Assume wired to INT1 pin */
668 ret = i2c_smbus_write_byte_data(client,
669 MMA8452_CTRL_REG5,
670 supported_interrupts);
671 if (ret < 0)
672 return ret;
673
674 ret = i2c_smbus_write_byte_data(client,
675 MMA8452_CTRL_REG4,
676 supported_interrupts);
677 if (ret < 0)
678 return ret;
679 }
680
Martin Fuzzeyecabae72015-05-13 12:26:38 +0200681 data->ctrl_reg1 = MMA8452_CTRL_ACTIVE |
682 (MMA8452_CTRL_DR_DEFAULT << MMA8452_CTRL_DR_SHIFT);
683 ret = i2c_smbus_write_byte_data(client, MMA8452_CTRL_REG1,
684 data->ctrl_reg1);
685 if (ret < 0)
686 return ret;
687
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000688 ret = iio_triggered_buffer_setup(indio_dev, NULL,
689 mma8452_trigger_handler, NULL);
690 if (ret < 0)
691 return ret;
692
Martin Fuzzey28e34272015-06-01 15:39:52 +0200693 if (client->irq) {
694 ret = devm_request_threaded_irq(&client->dev,
695 client->irq,
696 NULL, mma8452_interrupt,
697 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
698 client->name, indio_dev);
699 if (ret)
700 goto buffer_cleanup;
701 }
702
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000703 ret = iio_device_register(indio_dev);
704 if (ret < 0)
705 goto buffer_cleanup;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200706
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000707 return 0;
708
709buffer_cleanup:
710 iio_triggered_buffer_cleanup(indio_dev);
711 return ret;
712}
713
714static int mma8452_remove(struct i2c_client *client)
715{
716 struct iio_dev *indio_dev = i2c_get_clientdata(client);
717
718 iio_device_unregister(indio_dev);
719 iio_triggered_buffer_cleanup(indio_dev);
720 mma8452_standby(iio_priv(indio_dev));
721
722 return 0;
723}
724
725#ifdef CONFIG_PM_SLEEP
726static int mma8452_suspend(struct device *dev)
727{
728 return mma8452_standby(iio_priv(i2c_get_clientdata(
729 to_i2c_client(dev))));
730}
731
732static int mma8452_resume(struct device *dev)
733{
734 return mma8452_active(iio_priv(i2c_get_clientdata(
735 to_i2c_client(dev))));
736}
737
738static SIMPLE_DEV_PM_OPS(mma8452_pm_ops, mma8452_suspend, mma8452_resume);
739#define MMA8452_PM_OPS (&mma8452_pm_ops)
740#else
741#define MMA8452_PM_OPS NULL
742#endif
743
744static const struct i2c_device_id mma8452_id[] = {
745 { "mma8452", 0 },
746 { }
747};
748MODULE_DEVICE_TABLE(i2c, mma8452_id);
749
Martin Fuzzeya3fb96a2014-11-07 14:06:00 +0000750static const struct of_device_id mma8452_dt_ids[] = {
751 { .compatible = "fsl,mma8452" },
752 { }
753};
754
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000755static struct i2c_driver mma8452_driver = {
756 .driver = {
757 .name = "mma8452",
Martin Fuzzeya3fb96a2014-11-07 14:06:00 +0000758 .of_match_table = of_match_ptr(mma8452_dt_ids),
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000759 .pm = MMA8452_PM_OPS,
760 },
761 .probe = mma8452_probe,
762 .remove = mma8452_remove,
763 .id_table = mma8452_id,
764};
765module_i2c_driver(mma8452_driver);
766
767MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
768MODULE_DESCRIPTION("Freescale MMA8452 accelerometer driver");
769MODULE_LICENSE("GPL");