blob: 116a6e401a6aa382896207786da8229c2ce41092 [file] [log] [blame]
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001/*
Martin Kepplingerc5ea1b582015-09-01 13:45:09 +02002 * mma8452.c - Support for following Freescale 3-axis accelerometers:
3 *
4 * MMA8452Q (12 bit)
5 * MMA8453Q (10 bit)
Martin Kepplinger417e0082015-09-01 13:45:11 +02006 * MMA8652FC (12 bit)
7 * MMA8653FC (10 bit)
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00008 *
Martin Kepplingerd6223c32015-09-01 13:45:12 +02009 * Copyright 2015 Martin Kepplinger <martin.kepplinger@theobroma-systems.com>
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000010 * Copyright 2014 Peter Meerwald <pmeerw@pmeerw.net>
11 *
12 * This file is subject to the terms and conditions of version 2 of
13 * the GNU General Public License. See the file COPYING in the main
14 * directory of this archive for more details.
15 *
16 * 7-bit I2C slave address 0x1c/0x1d (pin selectable)
17 *
Martin Fuzzey28e34272015-06-01 15:39:52 +020018 * TODO: orientation / freefall events, autosleep
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000019 */
20
21#include <linux/module.h>
22#include <linux/i2c.h>
23#include <linux/iio/iio.h>
24#include <linux/iio/sysfs.h>
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000025#include <linux/iio/buffer.h>
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +020026#include <linux/iio/trigger.h>
27#include <linux/iio/trigger_consumer.h>
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000028#include <linux/iio/triggered_buffer.h>
Martin Fuzzey28e34272015-06-01 15:39:52 +020029#include <linux/iio/events.h>
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000030#include <linux/delay.h>
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +020031#include <linux/of_device.h>
Martin Kepplingerd2a3e092015-10-15 15:10:32 +020032#include <linux/of_irq.h>
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000033
Hartmut Knaack69abff82015-08-02 22:43:50 +020034#define MMA8452_STATUS 0x00
35#define MMA8452_STATUS_DRDY (BIT(2) | BIT(1) | BIT(0))
Martin Kepplingerc5ea1b582015-09-01 13:45:09 +020036#define MMA8452_OUT_X 0x01 /* MSB first */
Hartmut Knaack69abff82015-08-02 22:43:50 +020037#define MMA8452_OUT_Y 0x03
38#define MMA8452_OUT_Z 0x05
39#define MMA8452_INT_SRC 0x0c
40#define MMA8452_WHO_AM_I 0x0d
41#define MMA8452_DATA_CFG 0x0e
42#define MMA8452_DATA_CFG_FS_MASK GENMASK(1, 0)
43#define MMA8452_DATA_CFG_FS_2G 0
44#define MMA8452_DATA_CFG_FS_4G 1
45#define MMA8452_DATA_CFG_FS_8G 2
46#define MMA8452_DATA_CFG_HPF_MASK BIT(4)
47#define MMA8452_HP_FILTER_CUTOFF 0x0f
48#define MMA8452_HP_FILTER_CUTOFF_SEL_MASK GENMASK(1, 0)
Martin Kepplinger60f562e2015-09-01 13:45:10 +020049#define MMA8452_FF_MT_CFG 0x15
50#define MMA8452_FF_MT_CFG_OAE BIT(6)
51#define MMA8452_FF_MT_CFG_ELE BIT(7)
52#define MMA8452_FF_MT_SRC 0x16
53#define MMA8452_FF_MT_SRC_XHE BIT(1)
54#define MMA8452_FF_MT_SRC_YHE BIT(3)
55#define MMA8452_FF_MT_SRC_ZHE BIT(5)
56#define MMA8452_FF_MT_THS 0x17
57#define MMA8452_FF_MT_THS_MASK 0x7f
58#define MMA8452_FF_MT_COUNT 0x18
Hartmut Knaack69abff82015-08-02 22:43:50 +020059#define MMA8452_TRANSIENT_CFG 0x1d
60#define MMA8452_TRANSIENT_CFG_HPF_BYP BIT(0)
61#define MMA8452_TRANSIENT_CFG_CHAN(chan) BIT(chan + 1)
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
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000082
Hartmut Knaack69abff82015-08-02 22:43:50 +020083#define MMA8452_MAX_REG 0x31
Martin Fuzzey2a17698c2015-05-13 12:26:40 +020084
Hartmut Knaack69abff82015-08-02 22:43:50 +020085#define MMA8452_INT_DRDY BIT(0)
Martin Kepplinger60f562e2015-09-01 13:45:10 +020086#define MMA8452_INT_FF_MT BIT(2)
Hartmut Knaack69abff82015-08-02 22:43:50 +020087#define MMA8452_INT_TRANS BIT(5)
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000088
Hartmut Knaack69abff82015-08-02 22:43:50 +020089#define MMA8452_DEVICE_ID 0x2a
Martin Kepplingerc5ea1b582015-09-01 13:45:09 +020090#define MMA8453_DEVICE_ID 0x3a
Martin Kepplinger417e0082015-09-01 13:45:11 +020091#define MMA8652_DEVICE_ID 0x4a
92#define MMA8653_DEVICE_ID 0x5a
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000093
94struct mma8452_data {
95 struct i2c_client *client;
96 struct mutex lock;
97 u8 ctrl_reg1;
98 u8 data_cfg;
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +020099 const struct mma_chip_info *chip_info;
100};
101
102/**
103 * struct mma_chip_info - chip specific data for Freescale's accelerometers
104 * @chip_id: WHO_AM_I register's value
105 * @channels: struct iio_chan_spec matching the device's
106 * capabilities
107 * @num_channels: number of channels
108 * @mma_scales: scale factors for converting register values
109 * to m/s^2; 3 modes: 2g, 4g, 8g; 2 integers
110 * per mode: m/s^2 and micro m/s^2
111 * @ev_cfg: event config register address
112 * @ev_cfg_ele: latch bit in event config register
113 * @ev_cfg_chan_shift: number of the bit to enable events in X
114 * direction; in event config register
115 * @ev_src: event source register address
116 * @ev_src_xe: bit in event source register that indicates
117 * an event in X direction
118 * @ev_src_ye: bit in event source register that indicates
119 * an event in Y direction
120 * @ev_src_ze: bit in event source register that indicates
121 * an event in Z direction
122 * @ev_ths: event threshold register address
123 * @ev_ths_mask: mask for the threshold value
124 * @ev_count: event count (period) register address
125 *
126 * Since not all chips supported by the driver support comparing high pass
127 * filtered data for events (interrupts), different interrupt sources are
128 * used for different chips and the relevant registers are included here.
129 */
130struct mma_chip_info {
131 u8 chip_id;
132 const struct iio_chan_spec *channels;
133 int num_channels;
134 const int mma_scales[3][2];
135 u8 ev_cfg;
136 u8 ev_cfg_ele;
137 u8 ev_cfg_chan_shift;
138 u8 ev_src;
139 u8 ev_src_xe;
140 u8 ev_src_ye;
141 u8 ev_src_ze;
142 u8 ev_ths;
143 u8 ev_ths_mask;
144 u8 ev_count;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000145};
146
147static int mma8452_drdy(struct mma8452_data *data)
148{
149 int tries = 150;
150
151 while (tries-- > 0) {
152 int ret = i2c_smbus_read_byte_data(data->client,
153 MMA8452_STATUS);
154 if (ret < 0)
155 return ret;
156 if ((ret & MMA8452_STATUS_DRDY) == MMA8452_STATUS_DRDY)
157 return 0;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200158
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000159 msleep(20);
160 }
161
162 dev_err(&data->client->dev, "data not ready\n");
Hartmut Knaack686027f2015-08-02 22:43:51 +0200163
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000164 return -EIO;
165}
166
167static int mma8452_read(struct mma8452_data *data, __be16 buf[3])
168{
169 int ret = mma8452_drdy(data);
Hartmut Knaack686027f2015-08-02 22:43:51 +0200170
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000171 if (ret < 0)
172 return ret;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200173
174 return i2c_smbus_read_i2c_block_data(data->client, MMA8452_OUT_X,
175 3 * sizeof(__be16), (u8 *)buf);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000176}
177
Hartmut Knaack686027f2015-08-02 22:43:51 +0200178static ssize_t mma8452_show_int_plus_micros(char *buf, const int (*vals)[2],
179 int n)
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000180{
181 size_t len = 0;
182
183 while (n-- > 0)
Hartmut Knaack686027f2015-08-02 22:43:51 +0200184 len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06d ",
185 vals[n][0], vals[n][1]);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000186
187 /* replace trailing space by newline */
188 buf[len - 1] = '\n';
189
190 return len;
191}
192
193static int mma8452_get_int_plus_micros_index(const int (*vals)[2], int n,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200194 int val, int val2)
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000195{
196 while (n-- > 0)
197 if (val == vals[n][0] && val2 == vals[n][1])
198 return n;
199
200 return -EINVAL;
201}
202
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200203static int mma8452_get_odr_index(struct mma8452_data *data)
204{
205 return (data->ctrl_reg1 & MMA8452_CTRL_DR_MASK) >>
206 MMA8452_CTRL_DR_SHIFT;
207}
208
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000209static const int mma8452_samp_freq[8][2] = {
210 {800, 0}, {400, 0}, {200, 0}, {100, 0}, {50, 0}, {12, 500000},
211 {6, 250000}, {1, 560000}
212};
213
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200214/* Datasheet table 35 (step time vs sample frequency) */
215static const int mma8452_transient_time_step_us[8] = {
216 1250,
217 2500,
218 5000,
219 10000,
220 20000,
221 20000,
222 20000,
223 20000
224};
225
Martin Fuzzey1e798412015-06-01 15:39:56 +0200226/* Datasheet table 18 (normal mode) */
227static const int mma8452_hp_filter_cutoff[8][4][2] = {
228 { {16, 0}, {8, 0}, {4, 0}, {2, 0} }, /* 800 Hz sample */
229 { {16, 0}, {8, 0}, {4, 0}, {2, 0} }, /* 400 Hz sample */
230 { {8, 0}, {4, 0}, {2, 0}, {1, 0} }, /* 200 Hz sample */
231 { {4, 0}, {2, 0}, {1, 0}, {0, 500000} }, /* 100 Hz sample */
232 { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} }, /* 50 Hz sample */
233 { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} }, /* 12.5 Hz sample */
234 { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} }, /* 6.25 Hz sample */
235 { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} } /* 1.56 Hz sample */
236};
237
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000238static ssize_t mma8452_show_samp_freq_avail(struct device *dev,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200239 struct device_attribute *attr,
240 char *buf)
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000241{
242 return mma8452_show_int_plus_micros(buf, mma8452_samp_freq,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200243 ARRAY_SIZE(mma8452_samp_freq));
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000244}
245
246static ssize_t mma8452_show_scale_avail(struct device *dev,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200247 struct device_attribute *attr,
248 char *buf)
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000249{
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200250 struct mma8452_data *data = iio_priv(i2c_get_clientdata(
251 to_i2c_client(dev)));
252
253 return mma8452_show_int_plus_micros(buf, data->chip_info->mma_scales,
254 ARRAY_SIZE(data->chip_info->mma_scales));
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000255}
256
Martin Fuzzey1e798412015-06-01 15:39:56 +0200257static ssize_t mma8452_show_hp_cutoff_avail(struct device *dev,
258 struct device_attribute *attr,
259 char *buf)
260{
261 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
262 struct mma8452_data *data = iio_priv(indio_dev);
263 int i = mma8452_get_odr_index(data);
264
265 return mma8452_show_int_plus_micros(buf, mma8452_hp_filter_cutoff[i],
266 ARRAY_SIZE(mma8452_hp_filter_cutoff[0]));
267}
268
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000269static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(mma8452_show_samp_freq_avail);
270static IIO_DEVICE_ATTR(in_accel_scale_available, S_IRUGO,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200271 mma8452_show_scale_avail, NULL, 0);
Martin Fuzzey1e798412015-06-01 15:39:56 +0200272static IIO_DEVICE_ATTR(in_accel_filter_high_pass_3db_frequency_available,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200273 S_IRUGO, mma8452_show_hp_cutoff_avail, NULL, 0);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000274
275static int mma8452_get_samp_freq_index(struct mma8452_data *data,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200276 int val, int val2)
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000277{
278 return mma8452_get_int_plus_micros_index(mma8452_samp_freq,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200279 ARRAY_SIZE(mma8452_samp_freq),
280 val, val2);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000281}
282
Hartmut Knaack686027f2015-08-02 22:43:51 +0200283static int mma8452_get_scale_index(struct mma8452_data *data, int val, int val2)
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000284{
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200285 return mma8452_get_int_plus_micros_index(data->chip_info->mma_scales,
286 ARRAY_SIZE(data->chip_info->mma_scales), val, val2);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000287}
288
Martin Fuzzey1e798412015-06-01 15:39:56 +0200289static int mma8452_get_hp_filter_index(struct mma8452_data *data,
290 int val, int val2)
291{
292 int i = mma8452_get_odr_index(data);
293
294 return mma8452_get_int_plus_micros_index(mma8452_hp_filter_cutoff[i],
Hartmut Knaack001fceb2015-08-02 22:43:46 +0200295 ARRAY_SIZE(mma8452_hp_filter_cutoff[0]), val, val2);
Martin Fuzzey1e798412015-06-01 15:39:56 +0200296}
297
298static int mma8452_read_hp_filter(struct mma8452_data *data, int *hz, int *uHz)
299{
300 int i, ret;
301
302 ret = i2c_smbus_read_byte_data(data->client, MMA8452_HP_FILTER_CUTOFF);
303 if (ret < 0)
304 return ret;
305
306 i = mma8452_get_odr_index(data);
307 ret &= MMA8452_HP_FILTER_CUTOFF_SEL_MASK;
308 *hz = mma8452_hp_filter_cutoff[i][ret][0];
309 *uHz = mma8452_hp_filter_cutoff[i][ret][1];
310
311 return 0;
312}
313
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000314static int mma8452_read_raw(struct iio_dev *indio_dev,
315 struct iio_chan_spec const *chan,
316 int *val, int *val2, long mask)
317{
318 struct mma8452_data *data = iio_priv(indio_dev);
319 __be16 buffer[3];
320 int i, ret;
321
322 switch (mask) {
323 case IIO_CHAN_INFO_RAW:
324 if (iio_buffer_enabled(indio_dev))
325 return -EBUSY;
326
327 mutex_lock(&data->lock);
328 ret = mma8452_read(data, buffer);
329 mutex_unlock(&data->lock);
330 if (ret < 0)
331 return ret;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200332
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200333 *val = sign_extend32(be16_to_cpu(
334 buffer[chan->scan_index]) >> chan->scan_type.shift,
335 chan->scan_type.realbits - 1);
Hartmut Knaack686027f2015-08-02 22:43:51 +0200336
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000337 return IIO_VAL_INT;
338 case IIO_CHAN_INFO_SCALE:
339 i = data->data_cfg & MMA8452_DATA_CFG_FS_MASK;
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200340 *val = data->chip_info->mma_scales[i][0];
341 *val2 = data->chip_info->mma_scales[i][1];
Hartmut Knaack686027f2015-08-02 22:43:51 +0200342
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000343 return IIO_VAL_INT_PLUS_MICRO;
344 case IIO_CHAN_INFO_SAMP_FREQ:
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200345 i = mma8452_get_odr_index(data);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000346 *val = mma8452_samp_freq[i][0];
347 *val2 = mma8452_samp_freq[i][1];
Hartmut Knaack686027f2015-08-02 22:43:51 +0200348
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000349 return IIO_VAL_INT_PLUS_MICRO;
350 case IIO_CHAN_INFO_CALIBBIAS:
Hartmut Knaack686027f2015-08-02 22:43:51 +0200351 ret = i2c_smbus_read_byte_data(data->client,
352 MMA8452_OFF_X + chan->scan_index);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000353 if (ret < 0)
354 return ret;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200355
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000356 *val = sign_extend32(ret, 7);
Hartmut Knaack686027f2015-08-02 22:43:51 +0200357
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000358 return IIO_VAL_INT;
Martin Fuzzey1e798412015-06-01 15:39:56 +0200359 case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY:
360 if (data->data_cfg & MMA8452_DATA_CFG_HPF_MASK) {
361 ret = mma8452_read_hp_filter(data, val, val2);
362 if (ret < 0)
363 return ret;
364 } else {
365 *val = 0;
366 *val2 = 0;
367 }
Hartmut Knaack686027f2015-08-02 22:43:51 +0200368
Martin Fuzzey1e798412015-06-01 15:39:56 +0200369 return IIO_VAL_INT_PLUS_MICRO;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000370 }
Hartmut Knaack686027f2015-08-02 22:43:51 +0200371
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000372 return -EINVAL;
373}
374
375static int mma8452_standby(struct mma8452_data *data)
376{
377 return i2c_smbus_write_byte_data(data->client, MMA8452_CTRL_REG1,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200378 data->ctrl_reg1 & ~MMA8452_CTRL_ACTIVE);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000379}
380
381static int mma8452_active(struct mma8452_data *data)
382{
383 return i2c_smbus_write_byte_data(data->client, MMA8452_CTRL_REG1,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200384 data->ctrl_reg1);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000385}
386
387static int mma8452_change_config(struct mma8452_data *data, u8 reg, u8 val)
388{
389 int ret;
390
391 mutex_lock(&data->lock);
392
393 /* config can only be changed when in standby */
394 ret = mma8452_standby(data);
395 if (ret < 0)
396 goto fail;
397
398 ret = i2c_smbus_write_byte_data(data->client, reg, val);
399 if (ret < 0)
400 goto fail;
401
402 ret = mma8452_active(data);
403 if (ret < 0)
404 goto fail;
405
406 ret = 0;
407fail:
408 mutex_unlock(&data->lock);
Hartmut Knaack686027f2015-08-02 22:43:51 +0200409
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000410 return ret;
411}
412
Martin Fuzzey1e798412015-06-01 15:39:56 +0200413static int mma8452_set_hp_filter_frequency(struct mma8452_data *data,
414 int val, int val2)
415{
416 int i, reg;
417
418 i = mma8452_get_hp_filter_index(data, val, val2);
419 if (i < 0)
Hartmut Knaackb9fddcd2015-08-02 22:43:48 +0200420 return i;
Martin Fuzzey1e798412015-06-01 15:39:56 +0200421
422 reg = i2c_smbus_read_byte_data(data->client,
423 MMA8452_HP_FILTER_CUTOFF);
424 if (reg < 0)
425 return reg;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200426
Martin Fuzzey1e798412015-06-01 15:39:56 +0200427 reg &= ~MMA8452_HP_FILTER_CUTOFF_SEL_MASK;
428 reg |= i;
429
430 return mma8452_change_config(data, MMA8452_HP_FILTER_CUTOFF, reg);
431}
432
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000433static int mma8452_write_raw(struct iio_dev *indio_dev,
434 struct iio_chan_spec const *chan,
435 int val, int val2, long mask)
436{
437 struct mma8452_data *data = iio_priv(indio_dev);
Martin Fuzzey1e798412015-06-01 15:39:56 +0200438 int i, ret;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000439
440 if (iio_buffer_enabled(indio_dev))
441 return -EBUSY;
442
443 switch (mask) {
444 case IIO_CHAN_INFO_SAMP_FREQ:
445 i = mma8452_get_samp_freq_index(data, val, val2);
446 if (i < 0)
Hartmut Knaackb9fddcd2015-08-02 22:43:48 +0200447 return i;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000448
449 data->ctrl_reg1 &= ~MMA8452_CTRL_DR_MASK;
450 data->ctrl_reg1 |= i << MMA8452_CTRL_DR_SHIFT;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200451
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000452 return mma8452_change_config(data, MMA8452_CTRL_REG1,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200453 data->ctrl_reg1);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000454 case IIO_CHAN_INFO_SCALE:
455 i = mma8452_get_scale_index(data, val, val2);
456 if (i < 0)
Hartmut Knaackb9fddcd2015-08-02 22:43:48 +0200457 return i;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200458
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000459 data->data_cfg &= ~MMA8452_DATA_CFG_FS_MASK;
460 data->data_cfg |= i;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200461
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000462 return mma8452_change_config(data, MMA8452_DATA_CFG,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200463 data->data_cfg);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000464 case IIO_CHAN_INFO_CALIBBIAS:
465 if (val < -128 || val > 127)
466 return -EINVAL;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200467
468 return mma8452_change_config(data,
469 MMA8452_OFF_X + chan->scan_index,
470 val);
Martin Fuzzey1e798412015-06-01 15:39:56 +0200471
472 case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY:
473 if (val == 0 && val2 == 0) {
474 data->data_cfg &= ~MMA8452_DATA_CFG_HPF_MASK;
475 } else {
476 data->data_cfg |= MMA8452_DATA_CFG_HPF_MASK;
477 ret = mma8452_set_hp_filter_frequency(data, val, val2);
478 if (ret < 0)
479 return ret;
480 }
Hartmut Knaack686027f2015-08-02 22:43:51 +0200481
Martin Fuzzey1e798412015-06-01 15:39:56 +0200482 return mma8452_change_config(data, MMA8452_DATA_CFG,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200483 data->data_cfg);
Martin Fuzzey1e798412015-06-01 15:39:56 +0200484
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000485 default:
486 return -EINVAL;
487 }
488}
489
Martin Fuzzey28e34272015-06-01 15:39:52 +0200490static int mma8452_read_thresh(struct iio_dev *indio_dev,
491 const struct iio_chan_spec *chan,
492 enum iio_event_type type,
493 enum iio_event_direction dir,
494 enum iio_event_info info,
495 int *val, int *val2)
496{
497 struct mma8452_data *data = iio_priv(indio_dev);
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200498 int ret, us;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200499
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200500 switch (info) {
501 case IIO_EV_INFO_VALUE:
502 ret = i2c_smbus_read_byte_data(data->client,
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200503 data->chip_info->ev_ths);
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200504 if (ret < 0)
505 return ret;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200506
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200507 *val = ret & data->chip_info->ev_ths_mask;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200508
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200509 return IIO_VAL_INT;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200510
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200511 case IIO_EV_INFO_PERIOD:
512 ret = i2c_smbus_read_byte_data(data->client,
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200513 data->chip_info->ev_count);
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200514 if (ret < 0)
515 return ret;
516
517 us = ret * mma8452_transient_time_step_us[
518 mma8452_get_odr_index(data)];
519 *val = us / USEC_PER_SEC;
520 *val2 = us % USEC_PER_SEC;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200521
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200522 return IIO_VAL_INT_PLUS_MICRO;
523
Martin Fuzzey1e798412015-06-01 15:39:56 +0200524 case IIO_EV_INFO_HIGH_PASS_FILTER_3DB:
525 ret = i2c_smbus_read_byte_data(data->client,
526 MMA8452_TRANSIENT_CFG);
527 if (ret < 0)
528 return ret;
529
530 if (ret & MMA8452_TRANSIENT_CFG_HPF_BYP) {
531 *val = 0;
532 *val2 = 0;
533 } else {
534 ret = mma8452_read_hp_filter(data, val, val2);
535 if (ret < 0)
536 return ret;
537 }
Hartmut Knaack686027f2015-08-02 22:43:51 +0200538
Martin Fuzzey1e798412015-06-01 15:39:56 +0200539 return IIO_VAL_INT_PLUS_MICRO;
540
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200541 default:
542 return -EINVAL;
543 }
Martin Fuzzey28e34272015-06-01 15:39:52 +0200544}
545
546static int mma8452_write_thresh(struct iio_dev *indio_dev,
547 const struct iio_chan_spec *chan,
548 enum iio_event_type type,
549 enum iio_event_direction dir,
550 enum iio_event_info info,
551 int val, int val2)
552{
553 struct mma8452_data *data = iio_priv(indio_dev);
Martin Fuzzey1e798412015-06-01 15:39:56 +0200554 int ret, reg, steps;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200555
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200556 switch (info) {
557 case IIO_EV_INFO_VALUE:
Hartmut Knaack11218222015-08-02 22:43:49 +0200558 if (val < 0 || val > MMA8452_TRANSIENT_THS_MASK)
559 return -EINVAL;
560
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200561 return mma8452_change_config(data, data->chip_info->ev_ths,
562 val);
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200563
564 case IIO_EV_INFO_PERIOD:
565 steps = (val * USEC_PER_SEC + val2) /
566 mma8452_transient_time_step_us[
567 mma8452_get_odr_index(data)];
568
Hartmut Knaack11218222015-08-02 22:43:49 +0200569 if (steps < 0 || steps > 0xff)
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200570 return -EINVAL;
571
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200572 return mma8452_change_config(data, data->chip_info->ev_count,
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200573 steps);
Hartmut Knaack686027f2015-08-02 22:43:51 +0200574
Martin Fuzzey1e798412015-06-01 15:39:56 +0200575 case IIO_EV_INFO_HIGH_PASS_FILTER_3DB:
576 reg = i2c_smbus_read_byte_data(data->client,
577 MMA8452_TRANSIENT_CFG);
578 if (reg < 0)
579 return reg;
580
581 if (val == 0 && val2 == 0) {
582 reg |= MMA8452_TRANSIENT_CFG_HPF_BYP;
583 } else {
584 reg &= ~MMA8452_TRANSIENT_CFG_HPF_BYP;
585 ret = mma8452_set_hp_filter_frequency(data, val, val2);
586 if (ret < 0)
587 return ret;
588 }
Hartmut Knaack686027f2015-08-02 22:43:51 +0200589
Martin Fuzzey1e798412015-06-01 15:39:56 +0200590 return mma8452_change_config(data, MMA8452_TRANSIENT_CFG, reg);
591
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200592 default:
593 return -EINVAL;
594 }
Martin Fuzzey28e34272015-06-01 15:39:52 +0200595}
596
597static int mma8452_read_event_config(struct iio_dev *indio_dev,
598 const struct iio_chan_spec *chan,
599 enum iio_event_type type,
600 enum iio_event_direction dir)
601{
602 struct mma8452_data *data = iio_priv(indio_dev);
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200603 const struct mma_chip_info *chip = data->chip_info;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200604 int ret;
605
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200606 ret = i2c_smbus_read_byte_data(data->client,
607 data->chip_info->ev_cfg);
Martin Fuzzey28e34272015-06-01 15:39:52 +0200608 if (ret < 0)
609 return ret;
610
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200611 return !!(ret & BIT(chan->scan_index + chip->ev_cfg_chan_shift));
Martin Fuzzey28e34272015-06-01 15:39:52 +0200612}
613
614static int mma8452_write_event_config(struct iio_dev *indio_dev,
615 const struct iio_chan_spec *chan,
616 enum iio_event_type type,
617 enum iio_event_direction dir,
618 int state)
619{
620 struct mma8452_data *data = iio_priv(indio_dev);
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200621 const struct mma_chip_info *chip = data->chip_info;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200622 int val;
623
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200624 val = i2c_smbus_read_byte_data(data->client, chip->ev_cfg);
Martin Fuzzey28e34272015-06-01 15:39:52 +0200625 if (val < 0)
626 return val;
627
628 if (state)
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200629 val |= BIT(chan->scan_index + chip->ev_cfg_chan_shift);
Martin Fuzzey28e34272015-06-01 15:39:52 +0200630 else
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200631 val &= ~BIT(chan->scan_index + chip->ev_cfg_chan_shift);
Martin Fuzzey28e34272015-06-01 15:39:52 +0200632
Martin Kepplinger60f562e2015-09-01 13:45:10 +0200633 val |= chip->ev_cfg_ele;
634 val |= MMA8452_FF_MT_CFG_OAE;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200635
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200636 return mma8452_change_config(data, chip->ev_cfg, val);
Martin Fuzzey28e34272015-06-01 15:39:52 +0200637}
638
639static void mma8452_transient_interrupt(struct iio_dev *indio_dev)
640{
641 struct mma8452_data *data = iio_priv(indio_dev);
642 s64 ts = iio_get_time_ns();
643 int src;
644
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200645 src = i2c_smbus_read_byte_data(data->client, data->chip_info->ev_src);
Martin Fuzzey28e34272015-06-01 15:39:52 +0200646 if (src < 0)
647 return;
648
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200649 if (src & data->chip_info->ev_src_xe)
Martin Fuzzey28e34272015-06-01 15:39:52 +0200650 iio_push_event(indio_dev,
651 IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X,
Martin Kepplingerc5d0db02015-07-05 19:50:18 +0200652 IIO_EV_TYPE_MAG,
Martin Fuzzey28e34272015-06-01 15:39:52 +0200653 IIO_EV_DIR_RISING),
654 ts);
655
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200656 if (src & data->chip_info->ev_src_ye)
Martin Fuzzey28e34272015-06-01 15:39:52 +0200657 iio_push_event(indio_dev,
658 IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_Y,
Martin Kepplingerc5d0db02015-07-05 19:50:18 +0200659 IIO_EV_TYPE_MAG,
Martin Fuzzey28e34272015-06-01 15:39:52 +0200660 IIO_EV_DIR_RISING),
661 ts);
662
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200663 if (src & data->chip_info->ev_src_ze)
Martin Fuzzey28e34272015-06-01 15:39:52 +0200664 iio_push_event(indio_dev,
665 IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_Z,
Martin Kepplingerc5d0db02015-07-05 19:50:18 +0200666 IIO_EV_TYPE_MAG,
Martin Fuzzey28e34272015-06-01 15:39:52 +0200667 IIO_EV_DIR_RISING),
668 ts);
669}
670
671static irqreturn_t mma8452_interrupt(int irq, void *p)
672{
673 struct iio_dev *indio_dev = p;
674 struct mma8452_data *data = iio_priv(indio_dev);
Martin Kepplinger60f562e2015-09-01 13:45:10 +0200675 const struct mma_chip_info *chip = data->chip_info;
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +0200676 int ret = IRQ_NONE;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200677 int src;
678
679 src = i2c_smbus_read_byte_data(data->client, MMA8452_INT_SRC);
680 if (src < 0)
681 return IRQ_NONE;
682
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +0200683 if (src & MMA8452_INT_DRDY) {
684 iio_trigger_poll_chained(indio_dev->trig);
685 ret = IRQ_HANDLED;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200686 }
687
Martin Kepplinger60f562e2015-09-01 13:45:10 +0200688 if ((src & MMA8452_INT_TRANS &&
689 chip->ev_src == MMA8452_TRANSIENT_SRC) ||
690 (src & MMA8452_INT_FF_MT &&
691 chip->ev_src == MMA8452_FF_MT_SRC)) {
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +0200692 mma8452_transient_interrupt(indio_dev);
693 ret = IRQ_HANDLED;
694 }
695
696 return ret;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200697}
698
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000699static irqreturn_t mma8452_trigger_handler(int irq, void *p)
700{
701 struct iio_poll_func *pf = p;
702 struct iio_dev *indio_dev = pf->indio_dev;
703 struct mma8452_data *data = iio_priv(indio_dev);
704 u8 buffer[16]; /* 3 16-bit channels + padding + ts */
705 int ret;
706
Hartmut Knaack686027f2015-08-02 22:43:51 +0200707 ret = mma8452_read(data, (__be16 *)buffer);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000708 if (ret < 0)
709 goto done;
710
711 iio_push_to_buffers_with_timestamp(indio_dev, buffer,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200712 iio_get_time_ns());
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000713
714done:
715 iio_trigger_notify_done(indio_dev->trig);
Hartmut Knaack686027f2015-08-02 22:43:51 +0200716
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000717 return IRQ_HANDLED;
718}
719
Martin Fuzzey2a17698c2015-05-13 12:26:40 +0200720static int mma8452_reg_access_dbg(struct iio_dev *indio_dev,
721 unsigned reg, unsigned writeval,
722 unsigned *readval)
723{
724 int ret;
725 struct mma8452_data *data = iio_priv(indio_dev);
726
727 if (reg > MMA8452_MAX_REG)
728 return -EINVAL;
729
730 if (!readval)
731 return mma8452_change_config(data, reg, writeval);
732
733 ret = i2c_smbus_read_byte_data(data->client, reg);
734 if (ret < 0)
735 return ret;
736
737 *readval = ret;
738
739 return 0;
740}
741
Martin Fuzzey28e34272015-06-01 15:39:52 +0200742static const struct iio_event_spec mma8452_transient_event[] = {
743 {
Martin Kepplingerc5d0db02015-07-05 19:50:18 +0200744 .type = IIO_EV_TYPE_MAG,
Martin Fuzzey28e34272015-06-01 15:39:52 +0200745 .dir = IIO_EV_DIR_RISING,
746 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200747 .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
Martin Fuzzey1e798412015-06-01 15:39:56 +0200748 BIT(IIO_EV_INFO_PERIOD) |
749 BIT(IIO_EV_INFO_HIGH_PASS_FILTER_3DB)
Martin Fuzzey28e34272015-06-01 15:39:52 +0200750 },
751};
752
Martin Kepplinger60f562e2015-09-01 13:45:10 +0200753static const struct iio_event_spec mma8452_motion_event[] = {
754 {
755 .type = IIO_EV_TYPE_MAG,
756 .dir = IIO_EV_DIR_RISING,
757 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
758 .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
759 BIT(IIO_EV_INFO_PERIOD)
760 },
761};
762
Martin Fuzzey28e34272015-06-01 15:39:52 +0200763/*
764 * Threshold is configured in fixed 8G/127 steps regardless of
765 * currently selected scale for measurement.
766 */
767static IIO_CONST_ATTR_NAMED(accel_transient_scale, in_accel_scale, "0.617742");
768
769static struct attribute *mma8452_event_attributes[] = {
770 &iio_const_attr_accel_transient_scale.dev_attr.attr,
771 NULL,
772};
773
774static struct attribute_group mma8452_event_attribute_group = {
775 .attrs = mma8452_event_attributes,
Martin Fuzzey28e34272015-06-01 15:39:52 +0200776};
777
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200778#define MMA8452_CHANNEL(axis, idx, bits) { \
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000779 .type = IIO_ACCEL, \
780 .modified = 1, \
781 .channel2 = IIO_MOD_##axis, \
782 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
Hartmut Knaack686027f2015-08-02 22:43:51 +0200783 BIT(IIO_CHAN_INFO_CALIBBIAS), \
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000784 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
Hartmut Knaack686027f2015-08-02 22:43:51 +0200785 BIT(IIO_CHAN_INFO_SCALE) | \
786 BIT(IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY), \
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000787 .scan_index = idx, \
788 .scan_type = { \
789 .sign = 's', \
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200790 .realbits = (bits), \
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000791 .storagebits = 16, \
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200792 .shift = 16 - (bits), \
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000793 .endianness = IIO_BE, \
794 }, \
Martin Fuzzey28e34272015-06-01 15:39:52 +0200795 .event_spec = mma8452_transient_event, \
796 .num_event_specs = ARRAY_SIZE(mma8452_transient_event), \
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000797}
798
Martin Kepplinger417e0082015-09-01 13:45:11 +0200799#define MMA8652_CHANNEL(axis, idx, bits) { \
800 .type = IIO_ACCEL, \
801 .modified = 1, \
802 .channel2 = IIO_MOD_##axis, \
803 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
804 BIT(IIO_CHAN_INFO_CALIBBIAS), \
805 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
806 BIT(IIO_CHAN_INFO_SCALE), \
807 .scan_index = idx, \
808 .scan_type = { \
809 .sign = 's', \
810 .realbits = (bits), \
811 .storagebits = 16, \
812 .shift = 16 - (bits), \
813 .endianness = IIO_BE, \
814 }, \
815 .event_spec = mma8452_motion_event, \
816 .num_event_specs = ARRAY_SIZE(mma8452_motion_event), \
817}
818
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000819static const struct iio_chan_spec mma8452_channels[] = {
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200820 MMA8452_CHANNEL(X, 0, 12),
821 MMA8452_CHANNEL(Y, 1, 12),
822 MMA8452_CHANNEL(Z, 2, 12),
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000823 IIO_CHAN_SOFT_TIMESTAMP(3),
824};
825
Martin Kepplingerc5ea1b582015-09-01 13:45:09 +0200826static const struct iio_chan_spec mma8453_channels[] = {
827 MMA8452_CHANNEL(X, 0, 10),
828 MMA8452_CHANNEL(Y, 1, 10),
829 MMA8452_CHANNEL(Z, 2, 10),
830 IIO_CHAN_SOFT_TIMESTAMP(3),
831};
832
Martin Kepplinger417e0082015-09-01 13:45:11 +0200833static const struct iio_chan_spec mma8652_channels[] = {
834 MMA8652_CHANNEL(X, 0, 12),
835 MMA8652_CHANNEL(Y, 1, 12),
836 MMA8652_CHANNEL(Z, 2, 12),
837 IIO_CHAN_SOFT_TIMESTAMP(3),
838};
839
840static const struct iio_chan_spec mma8653_channels[] = {
841 MMA8652_CHANNEL(X, 0, 10),
842 MMA8652_CHANNEL(Y, 1, 10),
843 MMA8652_CHANNEL(Z, 2, 10),
844 IIO_CHAN_SOFT_TIMESTAMP(3),
845};
846
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200847enum {
848 mma8452,
Martin Kepplingerc5ea1b582015-09-01 13:45:09 +0200849 mma8453,
Martin Kepplinger417e0082015-09-01 13:45:11 +0200850 mma8652,
851 mma8653,
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200852};
853
854static const struct mma_chip_info mma_chip_info_table[] = {
855 [mma8452] = {
856 .chip_id = MMA8452_DEVICE_ID,
857 .channels = mma8452_channels,
858 .num_channels = ARRAY_SIZE(mma8452_channels),
859 /*
860 * Hardware has fullscale of -2G, -4G, -8G corresponding to
861 * raw value -2048 for 12 bit or -512 for 10 bit.
862 * The userspace interface uses m/s^2 and we declare micro units
863 * So scale factor for 12 bit here is given by:
864 * g * N * 1000000 / 2048 for N = 2, 4, 8 and g=9.80665
865 */
866 .mma_scales = { {0, 9577}, {0, 19154}, {0, 38307} },
867 .ev_cfg = MMA8452_TRANSIENT_CFG,
868 .ev_cfg_ele = MMA8452_TRANSIENT_CFG_ELE,
869 .ev_cfg_chan_shift = 1,
870 .ev_src = MMA8452_TRANSIENT_SRC,
871 .ev_src_xe = MMA8452_TRANSIENT_SRC_XTRANSE,
872 .ev_src_ye = MMA8452_TRANSIENT_SRC_YTRANSE,
873 .ev_src_ze = MMA8452_TRANSIENT_SRC_ZTRANSE,
874 .ev_ths = MMA8452_TRANSIENT_THS,
875 .ev_ths_mask = MMA8452_TRANSIENT_THS_MASK,
876 .ev_count = MMA8452_TRANSIENT_COUNT,
877 },
Martin Kepplingerc5ea1b582015-09-01 13:45:09 +0200878 [mma8453] = {
879 .chip_id = MMA8453_DEVICE_ID,
880 .channels = mma8453_channels,
881 .num_channels = ARRAY_SIZE(mma8453_channels),
882 .mma_scales = { {0, 38307}, {0, 76614}, {0, 153228} },
883 .ev_cfg = MMA8452_TRANSIENT_CFG,
884 .ev_cfg_ele = MMA8452_TRANSIENT_CFG_ELE,
885 .ev_cfg_chan_shift = 1,
886 .ev_src = MMA8452_TRANSIENT_SRC,
887 .ev_src_xe = MMA8452_TRANSIENT_SRC_XTRANSE,
888 .ev_src_ye = MMA8452_TRANSIENT_SRC_YTRANSE,
889 .ev_src_ze = MMA8452_TRANSIENT_SRC_ZTRANSE,
890 .ev_ths = MMA8452_TRANSIENT_THS,
891 .ev_ths_mask = MMA8452_TRANSIENT_THS_MASK,
892 .ev_count = MMA8452_TRANSIENT_COUNT,
893 },
Martin Kepplinger417e0082015-09-01 13:45:11 +0200894 [mma8652] = {
895 .chip_id = MMA8652_DEVICE_ID,
896 .channels = mma8652_channels,
897 .num_channels = ARRAY_SIZE(mma8652_channels),
898 .mma_scales = { {0, 9577}, {0, 19154}, {0, 38307} },
899 .ev_cfg = MMA8452_FF_MT_CFG,
900 .ev_cfg_ele = MMA8452_FF_MT_CFG_ELE,
901 .ev_cfg_chan_shift = 3,
902 .ev_src = MMA8452_FF_MT_SRC,
903 .ev_src_xe = MMA8452_FF_MT_SRC_XHE,
904 .ev_src_ye = MMA8452_FF_MT_SRC_YHE,
905 .ev_src_ze = MMA8452_FF_MT_SRC_ZHE,
906 .ev_ths = MMA8452_FF_MT_THS,
907 .ev_ths_mask = MMA8452_FF_MT_THS_MASK,
908 .ev_count = MMA8452_FF_MT_COUNT,
909 },
910 [mma8653] = {
911 .chip_id = MMA8653_DEVICE_ID,
912 .channels = mma8653_channels,
913 .num_channels = ARRAY_SIZE(mma8653_channels),
914 .mma_scales = { {0, 38307}, {0, 76614}, {0, 153228} },
915 .ev_cfg = MMA8452_FF_MT_CFG,
916 .ev_cfg_ele = MMA8452_FF_MT_CFG_ELE,
917 .ev_cfg_chan_shift = 3,
918 .ev_src = MMA8452_FF_MT_SRC,
919 .ev_src_xe = MMA8452_FF_MT_SRC_XHE,
920 .ev_src_ye = MMA8452_FF_MT_SRC_YHE,
921 .ev_src_ze = MMA8452_FF_MT_SRC_ZHE,
922 .ev_ths = MMA8452_FF_MT_THS,
923 .ev_ths_mask = MMA8452_FF_MT_THS_MASK,
924 .ev_count = MMA8452_FF_MT_COUNT,
925 },
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200926};
927
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000928static struct attribute *mma8452_attributes[] = {
929 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
930 &iio_dev_attr_in_accel_scale_available.dev_attr.attr,
Martin Fuzzey1e798412015-06-01 15:39:56 +0200931 &iio_dev_attr_in_accel_filter_high_pass_3db_frequency_available.dev_attr.attr,
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000932 NULL
933};
934
935static const struct attribute_group mma8452_group = {
936 .attrs = mma8452_attributes,
937};
938
939static const struct iio_info mma8452_info = {
940 .attrs = &mma8452_group,
941 .read_raw = &mma8452_read_raw,
942 .write_raw = &mma8452_write_raw,
Martin Fuzzey28e34272015-06-01 15:39:52 +0200943 .event_attrs = &mma8452_event_attribute_group,
944 .read_event_value = &mma8452_read_thresh,
945 .write_event_value = &mma8452_write_thresh,
946 .read_event_config = &mma8452_read_event_config,
947 .write_event_config = &mma8452_write_event_config,
Martin Fuzzey2a17698c2015-05-13 12:26:40 +0200948 .debugfs_reg_access = &mma8452_reg_access_dbg,
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000949 .driver_module = THIS_MODULE,
950};
951
952static const unsigned long mma8452_scan_masks[] = {0x7, 0};
953
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +0200954static int mma8452_data_rdy_trigger_set_state(struct iio_trigger *trig,
955 bool state)
956{
957 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
958 struct mma8452_data *data = iio_priv(indio_dev);
959 int reg;
960
961 reg = i2c_smbus_read_byte_data(data->client, MMA8452_CTRL_REG4);
962 if (reg < 0)
963 return reg;
964
965 if (state)
966 reg |= MMA8452_INT_DRDY;
967 else
968 reg &= ~MMA8452_INT_DRDY;
969
970 return mma8452_change_config(data, MMA8452_CTRL_REG4, reg);
971}
972
973static int mma8452_validate_device(struct iio_trigger *trig,
974 struct iio_dev *indio_dev)
975{
976 struct iio_dev *indio = iio_trigger_get_drvdata(trig);
977
978 if (indio != indio_dev)
979 return -EINVAL;
980
981 return 0;
982}
983
984static const struct iio_trigger_ops mma8452_trigger_ops = {
985 .set_trigger_state = mma8452_data_rdy_trigger_set_state,
986 .validate_device = mma8452_validate_device,
987 .owner = THIS_MODULE,
988};
989
990static int mma8452_trigger_setup(struct iio_dev *indio_dev)
991{
992 struct mma8452_data *data = iio_priv(indio_dev);
993 struct iio_trigger *trig;
994 int ret;
995
996 trig = devm_iio_trigger_alloc(&data->client->dev, "%s-dev%d",
997 indio_dev->name,
998 indio_dev->id);
999 if (!trig)
1000 return -ENOMEM;
1001
1002 trig->dev.parent = &data->client->dev;
1003 trig->ops = &mma8452_trigger_ops;
1004 iio_trigger_set_drvdata(trig, indio_dev);
1005
1006 ret = iio_trigger_register(trig);
1007 if (ret)
1008 return ret;
1009
1010 indio_dev->trig = trig;
Hartmut Knaack686027f2015-08-02 22:43:51 +02001011
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001012 return 0;
1013}
1014
1015static void mma8452_trigger_cleanup(struct iio_dev *indio_dev)
1016{
1017 if (indio_dev->trig)
1018 iio_trigger_unregister(indio_dev->trig);
1019}
1020
Martin Fuzzeyecabae72015-05-13 12:26:38 +02001021static int mma8452_reset(struct i2c_client *client)
1022{
1023 int i;
1024 int ret;
1025
1026 ret = i2c_smbus_write_byte_data(client, MMA8452_CTRL_REG2,
1027 MMA8452_CTRL_REG2_RST);
1028 if (ret < 0)
1029 return ret;
1030
1031 for (i = 0; i < 10; i++) {
1032 usleep_range(100, 200);
1033 ret = i2c_smbus_read_byte_data(client, MMA8452_CTRL_REG2);
1034 if (ret == -EIO)
1035 continue; /* I2C comm reset */
1036 if (ret < 0)
1037 return ret;
1038 if (!(ret & MMA8452_CTRL_REG2_RST))
1039 return 0;
1040 }
1041
1042 return -ETIMEDOUT;
1043}
1044
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001045static const struct of_device_id mma8452_dt_ids[] = {
1046 { .compatible = "fsl,mma8452", .data = &mma_chip_info_table[mma8452] },
Martin Kepplingerc5ea1b582015-09-01 13:45:09 +02001047 { .compatible = "fsl,mma8453", .data = &mma_chip_info_table[mma8453] },
Martin Kepplinger417e0082015-09-01 13:45:11 +02001048 { .compatible = "fsl,mma8652", .data = &mma_chip_info_table[mma8652] },
1049 { .compatible = "fsl,mma8653", .data = &mma_chip_info_table[mma8653] },
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001050 { }
1051};
1052MODULE_DEVICE_TABLE(of, mma8452_dt_ids);
1053
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001054static int mma8452_probe(struct i2c_client *client,
1055 const struct i2c_device_id *id)
1056{
1057 struct mma8452_data *data;
1058 struct iio_dev *indio_dev;
1059 int ret;
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001060 const struct of_device_id *match;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001061
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001062 match = of_match_device(mma8452_dt_ids, &client->dev);
1063 if (!match) {
1064 dev_err(&client->dev, "unknown device model\n");
1065 return -ENODEV;
1066 }
1067
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001068 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
1069 if (!indio_dev)
1070 return -ENOMEM;
1071
1072 data = iio_priv(indio_dev);
1073 data->client = client;
1074 mutex_init(&data->lock);
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001075 data->chip_info = match->data;
1076
Martin Kepplinger417e0082015-09-01 13:45:11 +02001077 ret = i2c_smbus_read_byte_data(client, MMA8452_WHO_AM_I);
1078 if (ret < 0)
1079 return ret;
1080
1081 switch (ret) {
1082 case MMA8452_DEVICE_ID:
1083 case MMA8453_DEVICE_ID:
1084 case MMA8652_DEVICE_ID:
1085 case MMA8653_DEVICE_ID:
1086 if (ret == data->chip_info->chip_id)
1087 break;
1088 default:
1089 return -ENODEV;
1090 }
1091
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001092 dev_info(&client->dev, "registering %s accelerometer; ID 0x%x\n",
1093 match->compatible, data->chip_info->chip_id);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001094
1095 i2c_set_clientdata(client, indio_dev);
1096 indio_dev->info = &mma8452_info;
1097 indio_dev->name = id->name;
1098 indio_dev->dev.parent = &client->dev;
1099 indio_dev->modes = INDIO_DIRECT_MODE;
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001100 indio_dev->channels = data->chip_info->channels;
1101 indio_dev->num_channels = data->chip_info->num_channels;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001102 indio_dev->available_scan_masks = mma8452_scan_masks;
1103
Martin Fuzzeyecabae72015-05-13 12:26:38 +02001104 ret = mma8452_reset(client);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001105 if (ret < 0)
1106 return ret;
1107
1108 data->data_cfg = MMA8452_DATA_CFG_FS_2G;
1109 ret = i2c_smbus_write_byte_data(client, MMA8452_DATA_CFG,
Hartmut Knaack686027f2015-08-02 22:43:51 +02001110 data->data_cfg);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001111 if (ret < 0)
1112 return ret;
1113
Martin Fuzzey28e34272015-06-01 15:39:52 +02001114 /*
1115 * By default set transient threshold to max to avoid events if
1116 * enabling without configuring threshold.
1117 */
1118 ret = i2c_smbus_write_byte_data(client, MMA8452_TRANSIENT_THS,
1119 MMA8452_TRANSIENT_THS_MASK);
1120 if (ret < 0)
1121 return ret;
1122
1123 if (client->irq) {
1124 /*
Martin Kepplinger60f562e2015-09-01 13:45:10 +02001125 * Although we enable the interrupt sources once and for
1126 * all here the event detection itself is not enabled until
1127 * userspace asks for it by mma8452_write_event_config()
Martin Fuzzey28e34272015-06-01 15:39:52 +02001128 */
Martin Kepplinger60f562e2015-09-01 13:45:10 +02001129 int supported_interrupts = MMA8452_INT_DRDY |
1130 MMA8452_INT_TRANS |
1131 MMA8452_INT_FF_MT;
1132 int enabled_interrupts = MMA8452_INT_TRANS |
1133 MMA8452_INT_FF_MT;
Martin Kepplingerd2a3e092015-10-15 15:10:32 +02001134 int irq2;
Martin Fuzzey28e34272015-06-01 15:39:52 +02001135
Martin Kepplingerd2a3e092015-10-15 15:10:32 +02001136 irq2 = of_irq_get_byname(client->dev.of_node, "INT2");
1137
1138 if (irq2 == client->irq) {
1139 dev_dbg(&client->dev, "using interrupt line INT2\n");
1140 } else {
1141 ret = i2c_smbus_write_byte_data(client,
1142 MMA8452_CTRL_REG5,
1143 supported_interrupts);
1144 if (ret < 0)
1145 return ret;
1146
1147 dev_dbg(&client->dev, "using interrupt line INT1\n");
1148 }
Martin Fuzzey28e34272015-06-01 15:39:52 +02001149
1150 ret = i2c_smbus_write_byte_data(client,
1151 MMA8452_CTRL_REG4,
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001152 enabled_interrupts);
1153 if (ret < 0)
1154 return ret;
1155
1156 ret = mma8452_trigger_setup(indio_dev);
Martin Fuzzey28e34272015-06-01 15:39:52 +02001157 if (ret < 0)
1158 return ret;
1159 }
1160
Martin Fuzzeyecabae72015-05-13 12:26:38 +02001161 data->ctrl_reg1 = MMA8452_CTRL_ACTIVE |
Hartmut Knaack686027f2015-08-02 22:43:51 +02001162 (MMA8452_CTRL_DR_DEFAULT << MMA8452_CTRL_DR_SHIFT);
Martin Fuzzeyecabae72015-05-13 12:26:38 +02001163 ret = i2c_smbus_write_byte_data(client, MMA8452_CTRL_REG1,
1164 data->ctrl_reg1);
1165 if (ret < 0)
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001166 goto trigger_cleanup;
Martin Fuzzeyecabae72015-05-13 12:26:38 +02001167
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001168 ret = iio_triggered_buffer_setup(indio_dev, NULL,
Hartmut Knaack686027f2015-08-02 22:43:51 +02001169 mma8452_trigger_handler, NULL);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001170 if (ret < 0)
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001171 goto trigger_cleanup;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001172
Martin Fuzzey28e34272015-06-01 15:39:52 +02001173 if (client->irq) {
1174 ret = devm_request_threaded_irq(&client->dev,
1175 client->irq,
1176 NULL, mma8452_interrupt,
1177 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1178 client->name, indio_dev);
1179 if (ret)
1180 goto buffer_cleanup;
1181 }
1182
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001183 ret = iio_device_register(indio_dev);
1184 if (ret < 0)
1185 goto buffer_cleanup;
Martin Fuzzey28e34272015-06-01 15:39:52 +02001186
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001187 return 0;
1188
1189buffer_cleanup:
1190 iio_triggered_buffer_cleanup(indio_dev);
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001191
1192trigger_cleanup:
1193 mma8452_trigger_cleanup(indio_dev);
1194
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001195 return ret;
1196}
1197
1198static int mma8452_remove(struct i2c_client *client)
1199{
1200 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1201
1202 iio_device_unregister(indio_dev);
1203 iio_triggered_buffer_cleanup(indio_dev);
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001204 mma8452_trigger_cleanup(indio_dev);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001205 mma8452_standby(iio_priv(indio_dev));
1206
1207 return 0;
1208}
1209
1210#ifdef CONFIG_PM_SLEEP
1211static int mma8452_suspend(struct device *dev)
1212{
1213 return mma8452_standby(iio_priv(i2c_get_clientdata(
1214 to_i2c_client(dev))));
1215}
1216
1217static int mma8452_resume(struct device *dev)
1218{
1219 return mma8452_active(iio_priv(i2c_get_clientdata(
1220 to_i2c_client(dev))));
1221}
1222
1223static SIMPLE_DEV_PM_OPS(mma8452_pm_ops, mma8452_suspend, mma8452_resume);
1224#define MMA8452_PM_OPS (&mma8452_pm_ops)
1225#else
1226#define MMA8452_PM_OPS NULL
1227#endif
1228
1229static const struct i2c_device_id mma8452_id[] = {
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001230 { "mma8452", mma8452 },
Martin Kepplingerc5ea1b582015-09-01 13:45:09 +02001231 { "mma8453", mma8453 },
Martin Kepplinger417e0082015-09-01 13:45:11 +02001232 { "mma8652", mma8652 },
1233 { "mma8653", mma8653 },
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001234 { }
1235};
1236MODULE_DEVICE_TABLE(i2c, mma8452_id);
1237
1238static struct i2c_driver mma8452_driver = {
1239 .driver = {
1240 .name = "mma8452",
Martin Fuzzeya3fb96a2014-11-07 14:06:00 +00001241 .of_match_table = of_match_ptr(mma8452_dt_ids),
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001242 .pm = MMA8452_PM_OPS,
1243 },
1244 .probe = mma8452_probe,
1245 .remove = mma8452_remove,
1246 .id_table = mma8452_id,
1247};
1248module_i2c_driver(mma8452_driver);
1249
1250MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
1251MODULE_DESCRIPTION("Freescale MMA8452 accelerometer driver");
1252MODULE_LICENSE("GPL");