blob: f28428faaf1494e600e58f66d38d69f568720b4f [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>
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000019#include <linux/iio/buffer.h>
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +020020#include <linux/iio/trigger.h>
21#include <linux/iio/trigger_consumer.h>
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000022#include <linux/iio/triggered_buffer.h>
Martin Fuzzey28e34272015-06-01 15:39:52 +020023#include <linux/iio/events.h>
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000024#include <linux/delay.h>
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +020025#include <linux/of_device.h>
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000026
Hartmut Knaack69abff82015-08-02 22:43:50 +020027#define MMA8452_STATUS 0x00
28#define MMA8452_STATUS_DRDY (BIT(2) | BIT(1) | BIT(0))
29#define MMA8452_OUT_X 0x01 /* MSB first, 12-bit */
30#define MMA8452_OUT_Y 0x03
31#define MMA8452_OUT_Z 0x05
32#define MMA8452_INT_SRC 0x0c
33#define MMA8452_WHO_AM_I 0x0d
34#define MMA8452_DATA_CFG 0x0e
35#define MMA8452_DATA_CFG_FS_MASK GENMASK(1, 0)
36#define MMA8452_DATA_CFG_FS_2G 0
37#define MMA8452_DATA_CFG_FS_4G 1
38#define MMA8452_DATA_CFG_FS_8G 2
39#define MMA8452_DATA_CFG_HPF_MASK BIT(4)
40#define MMA8452_HP_FILTER_CUTOFF 0x0f
41#define MMA8452_HP_FILTER_CUTOFF_SEL_MASK GENMASK(1, 0)
42#define MMA8452_TRANSIENT_CFG 0x1d
43#define MMA8452_TRANSIENT_CFG_HPF_BYP BIT(0)
44#define MMA8452_TRANSIENT_CFG_CHAN(chan) BIT(chan + 1)
45#define MMA8452_TRANSIENT_CFG_ELE BIT(4)
46#define MMA8452_TRANSIENT_SRC 0x1e
47#define MMA8452_TRANSIENT_SRC_XTRANSE BIT(1)
48#define MMA8452_TRANSIENT_SRC_YTRANSE BIT(3)
49#define MMA8452_TRANSIENT_SRC_ZTRANSE BIT(5)
50#define MMA8452_TRANSIENT_THS 0x1f
51#define MMA8452_TRANSIENT_THS_MASK GENMASK(6, 0)
52#define MMA8452_TRANSIENT_COUNT 0x20
53#define MMA8452_CTRL_REG1 0x2a
54#define MMA8452_CTRL_ACTIVE BIT(0)
55#define MMA8452_CTRL_DR_MASK GENMASK(5, 3)
56#define MMA8452_CTRL_DR_SHIFT 3
57#define MMA8452_CTRL_DR_DEFAULT 0x4 /* 50 Hz sample frequency */
58#define MMA8452_CTRL_REG2 0x2b
59#define MMA8452_CTRL_REG2_RST BIT(6)
60#define MMA8452_CTRL_REG4 0x2d
61#define MMA8452_CTRL_REG5 0x2e
62#define MMA8452_OFF_X 0x2f
63#define MMA8452_OFF_Y 0x30
64#define MMA8452_OFF_Z 0x31
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000065
Hartmut Knaack69abff82015-08-02 22:43:50 +020066#define MMA8452_MAX_REG 0x31
Martin Fuzzey2a17698c2015-05-13 12:26:40 +020067
Hartmut Knaack69abff82015-08-02 22:43:50 +020068#define MMA8452_INT_DRDY BIT(0)
69#define MMA8452_INT_TRANS BIT(5)
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000070
Hartmut Knaack69abff82015-08-02 22:43:50 +020071#define MMA8452_DEVICE_ID 0x2a
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000072
73struct mma8452_data {
74 struct i2c_client *client;
75 struct mutex lock;
76 u8 ctrl_reg1;
77 u8 data_cfg;
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +020078 const struct mma_chip_info *chip_info;
79};
80
81/**
82 * struct mma_chip_info - chip specific data for Freescale's accelerometers
83 * @chip_id: WHO_AM_I register's value
84 * @channels: struct iio_chan_spec matching the device's
85 * capabilities
86 * @num_channels: number of channels
87 * @mma_scales: scale factors for converting register values
88 * to m/s^2; 3 modes: 2g, 4g, 8g; 2 integers
89 * per mode: m/s^2 and micro m/s^2
90 * @ev_cfg: event config register address
91 * @ev_cfg_ele: latch bit in event config register
92 * @ev_cfg_chan_shift: number of the bit to enable events in X
93 * direction; in event config register
94 * @ev_src: event source register address
95 * @ev_src_xe: bit in event source register that indicates
96 * an event in X direction
97 * @ev_src_ye: bit in event source register that indicates
98 * an event in Y direction
99 * @ev_src_ze: bit in event source register that indicates
100 * an event in Z direction
101 * @ev_ths: event threshold register address
102 * @ev_ths_mask: mask for the threshold value
103 * @ev_count: event count (period) register address
104 *
105 * Since not all chips supported by the driver support comparing high pass
106 * filtered data for events (interrupts), different interrupt sources are
107 * used for different chips and the relevant registers are included here.
108 */
109struct mma_chip_info {
110 u8 chip_id;
111 const struct iio_chan_spec *channels;
112 int num_channels;
113 const int mma_scales[3][2];
114 u8 ev_cfg;
115 u8 ev_cfg_ele;
116 u8 ev_cfg_chan_shift;
117 u8 ev_src;
118 u8 ev_src_xe;
119 u8 ev_src_ye;
120 u8 ev_src_ze;
121 u8 ev_ths;
122 u8 ev_ths_mask;
123 u8 ev_count;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000124};
125
126static int mma8452_drdy(struct mma8452_data *data)
127{
128 int tries = 150;
129
130 while (tries-- > 0) {
131 int ret = i2c_smbus_read_byte_data(data->client,
132 MMA8452_STATUS);
133 if (ret < 0)
134 return ret;
135 if ((ret & MMA8452_STATUS_DRDY) == MMA8452_STATUS_DRDY)
136 return 0;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200137
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000138 msleep(20);
139 }
140
141 dev_err(&data->client->dev, "data not ready\n");
Hartmut Knaack686027f2015-08-02 22:43:51 +0200142
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000143 return -EIO;
144}
145
146static int mma8452_read(struct mma8452_data *data, __be16 buf[3])
147{
148 int ret = mma8452_drdy(data);
Hartmut Knaack686027f2015-08-02 22:43:51 +0200149
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000150 if (ret < 0)
151 return ret;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200152
153 return i2c_smbus_read_i2c_block_data(data->client, MMA8452_OUT_X,
154 3 * sizeof(__be16), (u8 *)buf);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000155}
156
Hartmut Knaack686027f2015-08-02 22:43:51 +0200157static ssize_t mma8452_show_int_plus_micros(char *buf, const int (*vals)[2],
158 int n)
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000159{
160 size_t len = 0;
161
162 while (n-- > 0)
Hartmut Knaack686027f2015-08-02 22:43:51 +0200163 len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06d ",
164 vals[n][0], vals[n][1]);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000165
166 /* replace trailing space by newline */
167 buf[len - 1] = '\n';
168
169 return len;
170}
171
172static int mma8452_get_int_plus_micros_index(const int (*vals)[2], int n,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200173 int val, int val2)
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000174{
175 while (n-- > 0)
176 if (val == vals[n][0] && val2 == vals[n][1])
177 return n;
178
179 return -EINVAL;
180}
181
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200182static int mma8452_get_odr_index(struct mma8452_data *data)
183{
184 return (data->ctrl_reg1 & MMA8452_CTRL_DR_MASK) >>
185 MMA8452_CTRL_DR_SHIFT;
186}
187
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000188static const int mma8452_samp_freq[8][2] = {
189 {800, 0}, {400, 0}, {200, 0}, {100, 0}, {50, 0}, {12, 500000},
190 {6, 250000}, {1, 560000}
191};
192
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200193/* Datasheet table 35 (step time vs sample frequency) */
194static const int mma8452_transient_time_step_us[8] = {
195 1250,
196 2500,
197 5000,
198 10000,
199 20000,
200 20000,
201 20000,
202 20000
203};
204
Martin Fuzzey1e798412015-06-01 15:39:56 +0200205/* Datasheet table 18 (normal mode) */
206static const int mma8452_hp_filter_cutoff[8][4][2] = {
207 { {16, 0}, {8, 0}, {4, 0}, {2, 0} }, /* 800 Hz sample */
208 { {16, 0}, {8, 0}, {4, 0}, {2, 0} }, /* 400 Hz sample */
209 { {8, 0}, {4, 0}, {2, 0}, {1, 0} }, /* 200 Hz sample */
210 { {4, 0}, {2, 0}, {1, 0}, {0, 500000} }, /* 100 Hz sample */
211 { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} }, /* 50 Hz sample */
212 { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} }, /* 12.5 Hz sample */
213 { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} }, /* 6.25 Hz sample */
214 { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} } /* 1.56 Hz sample */
215};
216
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000217static ssize_t mma8452_show_samp_freq_avail(struct device *dev,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200218 struct device_attribute *attr,
219 char *buf)
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000220{
221 return mma8452_show_int_plus_micros(buf, mma8452_samp_freq,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200222 ARRAY_SIZE(mma8452_samp_freq));
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000223}
224
225static ssize_t mma8452_show_scale_avail(struct device *dev,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200226 struct device_attribute *attr,
227 char *buf)
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000228{
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200229 struct mma8452_data *data = iio_priv(i2c_get_clientdata(
230 to_i2c_client(dev)));
231
232 return mma8452_show_int_plus_micros(buf, data->chip_info->mma_scales,
233 ARRAY_SIZE(data->chip_info->mma_scales));
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000234}
235
Martin Fuzzey1e798412015-06-01 15:39:56 +0200236static ssize_t mma8452_show_hp_cutoff_avail(struct device *dev,
237 struct device_attribute *attr,
238 char *buf)
239{
240 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
241 struct mma8452_data *data = iio_priv(indio_dev);
242 int i = mma8452_get_odr_index(data);
243
244 return mma8452_show_int_plus_micros(buf, mma8452_hp_filter_cutoff[i],
245 ARRAY_SIZE(mma8452_hp_filter_cutoff[0]));
246}
247
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000248static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(mma8452_show_samp_freq_avail);
249static IIO_DEVICE_ATTR(in_accel_scale_available, S_IRUGO,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200250 mma8452_show_scale_avail, NULL, 0);
Martin Fuzzey1e798412015-06-01 15:39:56 +0200251static IIO_DEVICE_ATTR(in_accel_filter_high_pass_3db_frequency_available,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200252 S_IRUGO, mma8452_show_hp_cutoff_avail, NULL, 0);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000253
254static int mma8452_get_samp_freq_index(struct mma8452_data *data,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200255 int val, int val2)
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000256{
257 return mma8452_get_int_plus_micros_index(mma8452_samp_freq,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200258 ARRAY_SIZE(mma8452_samp_freq),
259 val, val2);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000260}
261
Hartmut Knaack686027f2015-08-02 22:43:51 +0200262static int mma8452_get_scale_index(struct mma8452_data *data, int val, int val2)
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000263{
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200264 return mma8452_get_int_plus_micros_index(data->chip_info->mma_scales,
265 ARRAY_SIZE(data->chip_info->mma_scales), val, val2);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000266}
267
Martin Fuzzey1e798412015-06-01 15:39:56 +0200268static int mma8452_get_hp_filter_index(struct mma8452_data *data,
269 int val, int val2)
270{
271 int i = mma8452_get_odr_index(data);
272
273 return mma8452_get_int_plus_micros_index(mma8452_hp_filter_cutoff[i],
Hartmut Knaack001fceb2015-08-02 22:43:46 +0200274 ARRAY_SIZE(mma8452_hp_filter_cutoff[0]), val, val2);
Martin Fuzzey1e798412015-06-01 15:39:56 +0200275}
276
277static int mma8452_read_hp_filter(struct mma8452_data *data, int *hz, int *uHz)
278{
279 int i, ret;
280
281 ret = i2c_smbus_read_byte_data(data->client, MMA8452_HP_FILTER_CUTOFF);
282 if (ret < 0)
283 return ret;
284
285 i = mma8452_get_odr_index(data);
286 ret &= MMA8452_HP_FILTER_CUTOFF_SEL_MASK;
287 *hz = mma8452_hp_filter_cutoff[i][ret][0];
288 *uHz = mma8452_hp_filter_cutoff[i][ret][1];
289
290 return 0;
291}
292
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000293static int mma8452_read_raw(struct iio_dev *indio_dev,
294 struct iio_chan_spec const *chan,
295 int *val, int *val2, long mask)
296{
297 struct mma8452_data *data = iio_priv(indio_dev);
298 __be16 buffer[3];
299 int i, ret;
300
301 switch (mask) {
302 case IIO_CHAN_INFO_RAW:
303 if (iio_buffer_enabled(indio_dev))
304 return -EBUSY;
305
306 mutex_lock(&data->lock);
307 ret = mma8452_read(data, buffer);
308 mutex_unlock(&data->lock);
309 if (ret < 0)
310 return ret;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200311
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200312 *val = sign_extend32(be16_to_cpu(
313 buffer[chan->scan_index]) >> chan->scan_type.shift,
314 chan->scan_type.realbits - 1);
Hartmut Knaack686027f2015-08-02 22:43:51 +0200315
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000316 return IIO_VAL_INT;
317 case IIO_CHAN_INFO_SCALE:
318 i = data->data_cfg & MMA8452_DATA_CFG_FS_MASK;
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200319 *val = data->chip_info->mma_scales[i][0];
320 *val2 = data->chip_info->mma_scales[i][1];
Hartmut Knaack686027f2015-08-02 22:43:51 +0200321
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000322 return IIO_VAL_INT_PLUS_MICRO;
323 case IIO_CHAN_INFO_SAMP_FREQ:
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200324 i = mma8452_get_odr_index(data);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000325 *val = mma8452_samp_freq[i][0];
326 *val2 = mma8452_samp_freq[i][1];
Hartmut Knaack686027f2015-08-02 22:43:51 +0200327
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000328 return IIO_VAL_INT_PLUS_MICRO;
329 case IIO_CHAN_INFO_CALIBBIAS:
Hartmut Knaack686027f2015-08-02 22:43:51 +0200330 ret = i2c_smbus_read_byte_data(data->client,
331 MMA8452_OFF_X + chan->scan_index);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000332 if (ret < 0)
333 return ret;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200334
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000335 *val = sign_extend32(ret, 7);
Hartmut Knaack686027f2015-08-02 22:43:51 +0200336
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000337 return IIO_VAL_INT;
Martin Fuzzey1e798412015-06-01 15:39:56 +0200338 case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY:
339 if (data->data_cfg & MMA8452_DATA_CFG_HPF_MASK) {
340 ret = mma8452_read_hp_filter(data, val, val2);
341 if (ret < 0)
342 return ret;
343 } else {
344 *val = 0;
345 *val2 = 0;
346 }
Hartmut Knaack686027f2015-08-02 22:43:51 +0200347
Martin Fuzzey1e798412015-06-01 15:39:56 +0200348 return IIO_VAL_INT_PLUS_MICRO;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000349 }
Hartmut Knaack686027f2015-08-02 22:43:51 +0200350
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000351 return -EINVAL;
352}
353
354static int mma8452_standby(struct mma8452_data *data)
355{
356 return i2c_smbus_write_byte_data(data->client, MMA8452_CTRL_REG1,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200357 data->ctrl_reg1 & ~MMA8452_CTRL_ACTIVE);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000358}
359
360static int mma8452_active(struct mma8452_data *data)
361{
362 return i2c_smbus_write_byte_data(data->client, MMA8452_CTRL_REG1,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200363 data->ctrl_reg1);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000364}
365
366static int mma8452_change_config(struct mma8452_data *data, u8 reg, u8 val)
367{
368 int ret;
369
370 mutex_lock(&data->lock);
371
372 /* config can only be changed when in standby */
373 ret = mma8452_standby(data);
374 if (ret < 0)
375 goto fail;
376
377 ret = i2c_smbus_write_byte_data(data->client, reg, val);
378 if (ret < 0)
379 goto fail;
380
381 ret = mma8452_active(data);
382 if (ret < 0)
383 goto fail;
384
385 ret = 0;
386fail:
387 mutex_unlock(&data->lock);
Hartmut Knaack686027f2015-08-02 22:43:51 +0200388
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000389 return ret;
390}
391
Martin Fuzzey1e798412015-06-01 15:39:56 +0200392static int mma8452_set_hp_filter_frequency(struct mma8452_data *data,
393 int val, int val2)
394{
395 int i, reg;
396
397 i = mma8452_get_hp_filter_index(data, val, val2);
398 if (i < 0)
Hartmut Knaackb9fddcd2015-08-02 22:43:48 +0200399 return i;
Martin Fuzzey1e798412015-06-01 15:39:56 +0200400
401 reg = i2c_smbus_read_byte_data(data->client,
402 MMA8452_HP_FILTER_CUTOFF);
403 if (reg < 0)
404 return reg;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200405
Martin Fuzzey1e798412015-06-01 15:39:56 +0200406 reg &= ~MMA8452_HP_FILTER_CUTOFF_SEL_MASK;
407 reg |= i;
408
409 return mma8452_change_config(data, MMA8452_HP_FILTER_CUTOFF, reg);
410}
411
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000412static int mma8452_write_raw(struct iio_dev *indio_dev,
413 struct iio_chan_spec const *chan,
414 int val, int val2, long mask)
415{
416 struct mma8452_data *data = iio_priv(indio_dev);
Martin Fuzzey1e798412015-06-01 15:39:56 +0200417 int i, ret;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000418
419 if (iio_buffer_enabled(indio_dev))
420 return -EBUSY;
421
422 switch (mask) {
423 case IIO_CHAN_INFO_SAMP_FREQ:
424 i = mma8452_get_samp_freq_index(data, val, val2);
425 if (i < 0)
Hartmut Knaackb9fddcd2015-08-02 22:43:48 +0200426 return i;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000427
428 data->ctrl_reg1 &= ~MMA8452_CTRL_DR_MASK;
429 data->ctrl_reg1 |= i << MMA8452_CTRL_DR_SHIFT;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200430
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000431 return mma8452_change_config(data, MMA8452_CTRL_REG1,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200432 data->ctrl_reg1);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000433 case IIO_CHAN_INFO_SCALE:
434 i = mma8452_get_scale_index(data, val, val2);
435 if (i < 0)
Hartmut Knaackb9fddcd2015-08-02 22:43:48 +0200436 return i;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200437
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000438 data->data_cfg &= ~MMA8452_DATA_CFG_FS_MASK;
439 data->data_cfg |= i;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200440
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000441 return mma8452_change_config(data, MMA8452_DATA_CFG,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200442 data->data_cfg);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000443 case IIO_CHAN_INFO_CALIBBIAS:
444 if (val < -128 || val > 127)
445 return -EINVAL;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200446
447 return mma8452_change_config(data,
448 MMA8452_OFF_X + chan->scan_index,
449 val);
Martin Fuzzey1e798412015-06-01 15:39:56 +0200450
451 case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY:
452 if (val == 0 && val2 == 0) {
453 data->data_cfg &= ~MMA8452_DATA_CFG_HPF_MASK;
454 } else {
455 data->data_cfg |= MMA8452_DATA_CFG_HPF_MASK;
456 ret = mma8452_set_hp_filter_frequency(data, val, val2);
457 if (ret < 0)
458 return ret;
459 }
Hartmut Knaack686027f2015-08-02 22:43:51 +0200460
Martin Fuzzey1e798412015-06-01 15:39:56 +0200461 return mma8452_change_config(data, MMA8452_DATA_CFG,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200462 data->data_cfg);
Martin Fuzzey1e798412015-06-01 15:39:56 +0200463
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000464 default:
465 return -EINVAL;
466 }
467}
468
Martin Fuzzey28e34272015-06-01 15:39:52 +0200469static int mma8452_read_thresh(struct iio_dev *indio_dev,
470 const struct iio_chan_spec *chan,
471 enum iio_event_type type,
472 enum iio_event_direction dir,
473 enum iio_event_info info,
474 int *val, int *val2)
475{
476 struct mma8452_data *data = iio_priv(indio_dev);
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200477 int ret, us;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200478
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200479 switch (info) {
480 case IIO_EV_INFO_VALUE:
481 ret = i2c_smbus_read_byte_data(data->client,
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200482 data->chip_info->ev_ths);
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200483 if (ret < 0)
484 return ret;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200485
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200486 *val = ret & data->chip_info->ev_ths_mask;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200487
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200488 return IIO_VAL_INT;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200489
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200490 case IIO_EV_INFO_PERIOD:
491 ret = i2c_smbus_read_byte_data(data->client,
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200492 data->chip_info->ev_count);
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200493 if (ret < 0)
494 return ret;
495
496 us = ret * mma8452_transient_time_step_us[
497 mma8452_get_odr_index(data)];
498 *val = us / USEC_PER_SEC;
499 *val2 = us % USEC_PER_SEC;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200500
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200501 return IIO_VAL_INT_PLUS_MICRO;
502
Martin Fuzzey1e798412015-06-01 15:39:56 +0200503 case IIO_EV_INFO_HIGH_PASS_FILTER_3DB:
504 ret = i2c_smbus_read_byte_data(data->client,
505 MMA8452_TRANSIENT_CFG);
506 if (ret < 0)
507 return ret;
508
509 if (ret & MMA8452_TRANSIENT_CFG_HPF_BYP) {
510 *val = 0;
511 *val2 = 0;
512 } else {
513 ret = mma8452_read_hp_filter(data, val, val2);
514 if (ret < 0)
515 return ret;
516 }
Hartmut Knaack686027f2015-08-02 22:43:51 +0200517
Martin Fuzzey1e798412015-06-01 15:39:56 +0200518 return IIO_VAL_INT_PLUS_MICRO;
519
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200520 default:
521 return -EINVAL;
522 }
Martin Fuzzey28e34272015-06-01 15:39:52 +0200523}
524
525static int mma8452_write_thresh(struct iio_dev *indio_dev,
526 const struct iio_chan_spec *chan,
527 enum iio_event_type type,
528 enum iio_event_direction dir,
529 enum iio_event_info info,
530 int val, int val2)
531{
532 struct mma8452_data *data = iio_priv(indio_dev);
Martin Fuzzey1e798412015-06-01 15:39:56 +0200533 int ret, reg, steps;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200534
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200535 switch (info) {
536 case IIO_EV_INFO_VALUE:
Hartmut Knaack11218222015-08-02 22:43:49 +0200537 if (val < 0 || val > MMA8452_TRANSIENT_THS_MASK)
538 return -EINVAL;
539
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200540 return mma8452_change_config(data, data->chip_info->ev_ths,
541 val);
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200542
543 case IIO_EV_INFO_PERIOD:
544 steps = (val * USEC_PER_SEC + val2) /
545 mma8452_transient_time_step_us[
546 mma8452_get_odr_index(data)];
547
Hartmut Knaack11218222015-08-02 22:43:49 +0200548 if (steps < 0 || steps > 0xff)
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200549 return -EINVAL;
550
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200551 return mma8452_change_config(data, data->chip_info->ev_count,
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200552 steps);
Hartmut Knaack686027f2015-08-02 22:43:51 +0200553
Martin Fuzzey1e798412015-06-01 15:39:56 +0200554 case IIO_EV_INFO_HIGH_PASS_FILTER_3DB:
555 reg = i2c_smbus_read_byte_data(data->client,
556 MMA8452_TRANSIENT_CFG);
557 if (reg < 0)
558 return reg;
559
560 if (val == 0 && val2 == 0) {
561 reg |= MMA8452_TRANSIENT_CFG_HPF_BYP;
562 } else {
563 reg &= ~MMA8452_TRANSIENT_CFG_HPF_BYP;
564 ret = mma8452_set_hp_filter_frequency(data, val, val2);
565 if (ret < 0)
566 return ret;
567 }
Hartmut Knaack686027f2015-08-02 22:43:51 +0200568
Martin Fuzzey1e798412015-06-01 15:39:56 +0200569 return mma8452_change_config(data, MMA8452_TRANSIENT_CFG, reg);
570
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200571 default:
572 return -EINVAL;
573 }
Martin Fuzzey28e34272015-06-01 15:39:52 +0200574}
575
576static int mma8452_read_event_config(struct iio_dev *indio_dev,
577 const struct iio_chan_spec *chan,
578 enum iio_event_type type,
579 enum iio_event_direction dir)
580{
581 struct mma8452_data *data = iio_priv(indio_dev);
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200582 const struct mma_chip_info *chip = data->chip_info;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200583 int ret;
584
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200585 ret = i2c_smbus_read_byte_data(data->client,
586 data->chip_info->ev_cfg);
Martin Fuzzey28e34272015-06-01 15:39:52 +0200587 if (ret < 0)
588 return ret;
589
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200590 return !!(ret & BIT(chan->scan_index + chip->ev_cfg_chan_shift));
Martin Fuzzey28e34272015-06-01 15:39:52 +0200591}
592
593static int mma8452_write_event_config(struct iio_dev *indio_dev,
594 const struct iio_chan_spec *chan,
595 enum iio_event_type type,
596 enum iio_event_direction dir,
597 int state)
598{
599 struct mma8452_data *data = iio_priv(indio_dev);
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200600 const struct mma_chip_info *chip = data->chip_info;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200601 int val;
602
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200603 val = i2c_smbus_read_byte_data(data->client, chip->ev_cfg);
Martin Fuzzey28e34272015-06-01 15:39:52 +0200604 if (val < 0)
605 return val;
606
607 if (state)
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200608 val |= BIT(chan->scan_index + chip->ev_cfg_chan_shift);
Martin Fuzzey28e34272015-06-01 15:39:52 +0200609 else
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200610 val &= ~BIT(chan->scan_index + chip->ev_cfg_chan_shift);
Martin Fuzzey28e34272015-06-01 15:39:52 +0200611
612 val |= MMA8452_TRANSIENT_CFG_ELE;
613
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200614 return mma8452_change_config(data, chip->ev_cfg, val);
Martin Fuzzey28e34272015-06-01 15:39:52 +0200615}
616
617static void mma8452_transient_interrupt(struct iio_dev *indio_dev)
618{
619 struct mma8452_data *data = iio_priv(indio_dev);
620 s64 ts = iio_get_time_ns();
621 int src;
622
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200623 src = i2c_smbus_read_byte_data(data->client, data->chip_info->ev_src);
Martin Fuzzey28e34272015-06-01 15:39:52 +0200624 if (src < 0)
625 return;
626
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200627 if (src & data->chip_info->ev_src_xe)
Martin Fuzzey28e34272015-06-01 15:39:52 +0200628 iio_push_event(indio_dev,
629 IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X,
Martin Kepplingerc5d0db02015-07-05 19:50:18 +0200630 IIO_EV_TYPE_MAG,
Martin Fuzzey28e34272015-06-01 15:39:52 +0200631 IIO_EV_DIR_RISING),
632 ts);
633
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200634 if (src & data->chip_info->ev_src_ye)
Martin Fuzzey28e34272015-06-01 15:39:52 +0200635 iio_push_event(indio_dev,
636 IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_Y,
Martin Kepplingerc5d0db02015-07-05 19:50:18 +0200637 IIO_EV_TYPE_MAG,
Martin Fuzzey28e34272015-06-01 15:39:52 +0200638 IIO_EV_DIR_RISING),
639 ts);
640
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200641 if (src & data->chip_info->ev_src_ze)
Martin Fuzzey28e34272015-06-01 15:39:52 +0200642 iio_push_event(indio_dev,
643 IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_Z,
Martin Kepplingerc5d0db02015-07-05 19:50:18 +0200644 IIO_EV_TYPE_MAG,
Martin Fuzzey28e34272015-06-01 15:39:52 +0200645 IIO_EV_DIR_RISING),
646 ts);
647}
648
649static irqreturn_t mma8452_interrupt(int irq, void *p)
650{
651 struct iio_dev *indio_dev = p;
652 struct mma8452_data *data = iio_priv(indio_dev);
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +0200653 int ret = IRQ_NONE;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200654 int src;
655
656 src = i2c_smbus_read_byte_data(data->client, MMA8452_INT_SRC);
657 if (src < 0)
658 return IRQ_NONE;
659
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +0200660 if (src & MMA8452_INT_DRDY) {
661 iio_trigger_poll_chained(indio_dev->trig);
662 ret = IRQ_HANDLED;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200663 }
664
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +0200665 if (src & MMA8452_INT_TRANS) {
666 mma8452_transient_interrupt(indio_dev);
667 ret = IRQ_HANDLED;
668 }
669
670 return ret;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200671}
672
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000673static irqreturn_t mma8452_trigger_handler(int irq, void *p)
674{
675 struct iio_poll_func *pf = p;
676 struct iio_dev *indio_dev = pf->indio_dev;
677 struct mma8452_data *data = iio_priv(indio_dev);
678 u8 buffer[16]; /* 3 16-bit channels + padding + ts */
679 int ret;
680
Hartmut Knaack686027f2015-08-02 22:43:51 +0200681 ret = mma8452_read(data, (__be16 *)buffer);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000682 if (ret < 0)
683 goto done;
684
685 iio_push_to_buffers_with_timestamp(indio_dev, buffer,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200686 iio_get_time_ns());
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000687
688done:
689 iio_trigger_notify_done(indio_dev->trig);
Hartmut Knaack686027f2015-08-02 22:43:51 +0200690
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000691 return IRQ_HANDLED;
692}
693
Martin Fuzzey2a17698c2015-05-13 12:26:40 +0200694static int mma8452_reg_access_dbg(struct iio_dev *indio_dev,
695 unsigned reg, unsigned writeval,
696 unsigned *readval)
697{
698 int ret;
699 struct mma8452_data *data = iio_priv(indio_dev);
700
701 if (reg > MMA8452_MAX_REG)
702 return -EINVAL;
703
704 if (!readval)
705 return mma8452_change_config(data, reg, writeval);
706
707 ret = i2c_smbus_read_byte_data(data->client, reg);
708 if (ret < 0)
709 return ret;
710
711 *readval = ret;
712
713 return 0;
714}
715
Martin Fuzzey28e34272015-06-01 15:39:52 +0200716static const struct iio_event_spec mma8452_transient_event[] = {
717 {
Martin Kepplingerc5d0db02015-07-05 19:50:18 +0200718 .type = IIO_EV_TYPE_MAG,
Martin Fuzzey28e34272015-06-01 15:39:52 +0200719 .dir = IIO_EV_DIR_RISING,
720 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200721 .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
Martin Fuzzey1e798412015-06-01 15:39:56 +0200722 BIT(IIO_EV_INFO_PERIOD) |
723 BIT(IIO_EV_INFO_HIGH_PASS_FILTER_3DB)
Martin Fuzzey28e34272015-06-01 15:39:52 +0200724 },
725};
726
727/*
728 * Threshold is configured in fixed 8G/127 steps regardless of
729 * currently selected scale for measurement.
730 */
731static IIO_CONST_ATTR_NAMED(accel_transient_scale, in_accel_scale, "0.617742");
732
733static struct attribute *mma8452_event_attributes[] = {
734 &iio_const_attr_accel_transient_scale.dev_attr.attr,
735 NULL,
736};
737
738static struct attribute_group mma8452_event_attribute_group = {
739 .attrs = mma8452_event_attributes,
740 .name = "events",
741};
742
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200743#define MMA8452_CHANNEL(axis, idx, bits) { \
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000744 .type = IIO_ACCEL, \
745 .modified = 1, \
746 .channel2 = IIO_MOD_##axis, \
747 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
Hartmut Knaack686027f2015-08-02 22:43:51 +0200748 BIT(IIO_CHAN_INFO_CALIBBIAS), \
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000749 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
Hartmut Knaack686027f2015-08-02 22:43:51 +0200750 BIT(IIO_CHAN_INFO_SCALE) | \
751 BIT(IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY), \
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000752 .scan_index = idx, \
753 .scan_type = { \
754 .sign = 's', \
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200755 .realbits = (bits), \
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000756 .storagebits = 16, \
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200757 .shift = 16 - (bits), \
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000758 .endianness = IIO_BE, \
759 }, \
Martin Fuzzey28e34272015-06-01 15:39:52 +0200760 .event_spec = mma8452_transient_event, \
761 .num_event_specs = ARRAY_SIZE(mma8452_transient_event), \
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000762}
763
764static const struct iio_chan_spec mma8452_channels[] = {
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200765 MMA8452_CHANNEL(X, 0, 12),
766 MMA8452_CHANNEL(Y, 1, 12),
767 MMA8452_CHANNEL(Z, 2, 12),
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000768 IIO_CHAN_SOFT_TIMESTAMP(3),
769};
770
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200771enum {
772 mma8452,
773};
774
775static const struct mma_chip_info mma_chip_info_table[] = {
776 [mma8452] = {
777 .chip_id = MMA8452_DEVICE_ID,
778 .channels = mma8452_channels,
779 .num_channels = ARRAY_SIZE(mma8452_channels),
780 /*
781 * Hardware has fullscale of -2G, -4G, -8G corresponding to
782 * raw value -2048 for 12 bit or -512 for 10 bit.
783 * The userspace interface uses m/s^2 and we declare micro units
784 * So scale factor for 12 bit here is given by:
785 * g * N * 1000000 / 2048 for N = 2, 4, 8 and g=9.80665
786 */
787 .mma_scales = { {0, 9577}, {0, 19154}, {0, 38307} },
788 .ev_cfg = MMA8452_TRANSIENT_CFG,
789 .ev_cfg_ele = MMA8452_TRANSIENT_CFG_ELE,
790 .ev_cfg_chan_shift = 1,
791 .ev_src = MMA8452_TRANSIENT_SRC,
792 .ev_src_xe = MMA8452_TRANSIENT_SRC_XTRANSE,
793 .ev_src_ye = MMA8452_TRANSIENT_SRC_YTRANSE,
794 .ev_src_ze = MMA8452_TRANSIENT_SRC_ZTRANSE,
795 .ev_ths = MMA8452_TRANSIENT_THS,
796 .ev_ths_mask = MMA8452_TRANSIENT_THS_MASK,
797 .ev_count = MMA8452_TRANSIENT_COUNT,
798 },
799};
800
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000801static struct attribute *mma8452_attributes[] = {
802 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
803 &iio_dev_attr_in_accel_scale_available.dev_attr.attr,
Martin Fuzzey1e798412015-06-01 15:39:56 +0200804 &iio_dev_attr_in_accel_filter_high_pass_3db_frequency_available.dev_attr.attr,
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000805 NULL
806};
807
808static const struct attribute_group mma8452_group = {
809 .attrs = mma8452_attributes,
810};
811
812static const struct iio_info mma8452_info = {
813 .attrs = &mma8452_group,
814 .read_raw = &mma8452_read_raw,
815 .write_raw = &mma8452_write_raw,
Martin Fuzzey28e34272015-06-01 15:39:52 +0200816 .event_attrs = &mma8452_event_attribute_group,
817 .read_event_value = &mma8452_read_thresh,
818 .write_event_value = &mma8452_write_thresh,
819 .read_event_config = &mma8452_read_event_config,
820 .write_event_config = &mma8452_write_event_config,
Martin Fuzzey2a17698c2015-05-13 12:26:40 +0200821 .debugfs_reg_access = &mma8452_reg_access_dbg,
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000822 .driver_module = THIS_MODULE,
823};
824
825static const unsigned long mma8452_scan_masks[] = {0x7, 0};
826
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +0200827static int mma8452_data_rdy_trigger_set_state(struct iio_trigger *trig,
828 bool state)
829{
830 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
831 struct mma8452_data *data = iio_priv(indio_dev);
832 int reg;
833
834 reg = i2c_smbus_read_byte_data(data->client, MMA8452_CTRL_REG4);
835 if (reg < 0)
836 return reg;
837
838 if (state)
839 reg |= MMA8452_INT_DRDY;
840 else
841 reg &= ~MMA8452_INT_DRDY;
842
843 return mma8452_change_config(data, MMA8452_CTRL_REG4, reg);
844}
845
846static int mma8452_validate_device(struct iio_trigger *trig,
847 struct iio_dev *indio_dev)
848{
849 struct iio_dev *indio = iio_trigger_get_drvdata(trig);
850
851 if (indio != indio_dev)
852 return -EINVAL;
853
854 return 0;
855}
856
857static const struct iio_trigger_ops mma8452_trigger_ops = {
858 .set_trigger_state = mma8452_data_rdy_trigger_set_state,
859 .validate_device = mma8452_validate_device,
860 .owner = THIS_MODULE,
861};
862
863static int mma8452_trigger_setup(struct iio_dev *indio_dev)
864{
865 struct mma8452_data *data = iio_priv(indio_dev);
866 struct iio_trigger *trig;
867 int ret;
868
869 trig = devm_iio_trigger_alloc(&data->client->dev, "%s-dev%d",
870 indio_dev->name,
871 indio_dev->id);
872 if (!trig)
873 return -ENOMEM;
874
875 trig->dev.parent = &data->client->dev;
876 trig->ops = &mma8452_trigger_ops;
877 iio_trigger_set_drvdata(trig, indio_dev);
878
879 ret = iio_trigger_register(trig);
880 if (ret)
881 return ret;
882
883 indio_dev->trig = trig;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200884
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +0200885 return 0;
886}
887
888static void mma8452_trigger_cleanup(struct iio_dev *indio_dev)
889{
890 if (indio_dev->trig)
891 iio_trigger_unregister(indio_dev->trig);
892}
893
Martin Fuzzeyecabae72015-05-13 12:26:38 +0200894static int mma8452_reset(struct i2c_client *client)
895{
896 int i;
897 int ret;
898
899 ret = i2c_smbus_write_byte_data(client, MMA8452_CTRL_REG2,
900 MMA8452_CTRL_REG2_RST);
901 if (ret < 0)
902 return ret;
903
904 for (i = 0; i < 10; i++) {
905 usleep_range(100, 200);
906 ret = i2c_smbus_read_byte_data(client, MMA8452_CTRL_REG2);
907 if (ret == -EIO)
908 continue; /* I2C comm reset */
909 if (ret < 0)
910 return ret;
911 if (!(ret & MMA8452_CTRL_REG2_RST))
912 return 0;
913 }
914
915 return -ETIMEDOUT;
916}
917
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200918static const struct of_device_id mma8452_dt_ids[] = {
919 { .compatible = "fsl,mma8452", .data = &mma_chip_info_table[mma8452] },
920 { }
921};
922MODULE_DEVICE_TABLE(of, mma8452_dt_ids);
923
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000924static int mma8452_probe(struct i2c_client *client,
925 const struct i2c_device_id *id)
926{
927 struct mma8452_data *data;
928 struct iio_dev *indio_dev;
929 int ret;
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200930 const struct of_device_id *match;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000931
932 ret = i2c_smbus_read_byte_data(client, MMA8452_WHO_AM_I);
933 if (ret < 0)
934 return ret;
935 if (ret != MMA8452_DEVICE_ID)
936 return -ENODEV;
937
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200938 match = of_match_device(mma8452_dt_ids, &client->dev);
939 if (!match) {
940 dev_err(&client->dev, "unknown device model\n");
941 return -ENODEV;
942 }
943
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000944 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
945 if (!indio_dev)
946 return -ENOMEM;
947
948 data = iio_priv(indio_dev);
949 data->client = client;
950 mutex_init(&data->lock);
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200951 data->chip_info = match->data;
952
953 dev_info(&client->dev, "registering %s accelerometer; ID 0x%x\n",
954 match->compatible, data->chip_info->chip_id);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000955
956 i2c_set_clientdata(client, indio_dev);
957 indio_dev->info = &mma8452_info;
958 indio_dev->name = id->name;
959 indio_dev->dev.parent = &client->dev;
960 indio_dev->modes = INDIO_DIRECT_MODE;
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200961 indio_dev->channels = data->chip_info->channels;
962 indio_dev->num_channels = data->chip_info->num_channels;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000963 indio_dev->available_scan_masks = mma8452_scan_masks;
964
Martin Fuzzeyecabae72015-05-13 12:26:38 +0200965 ret = mma8452_reset(client);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000966 if (ret < 0)
967 return ret;
968
969 data->data_cfg = MMA8452_DATA_CFG_FS_2G;
970 ret = i2c_smbus_write_byte_data(client, MMA8452_DATA_CFG,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200971 data->data_cfg);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000972 if (ret < 0)
973 return ret;
974
Martin Fuzzey28e34272015-06-01 15:39:52 +0200975 /*
976 * By default set transient threshold to max to avoid events if
977 * enabling without configuring threshold.
978 */
979 ret = i2c_smbus_write_byte_data(client, MMA8452_TRANSIENT_THS,
980 MMA8452_TRANSIENT_THS_MASK);
981 if (ret < 0)
982 return ret;
983
984 if (client->irq) {
985 /*
986 * Although we enable the transient interrupt source once and
987 * for all here the transient event detection itself is not
988 * enabled until userspace asks for it by
989 * mma8452_write_event_config()
990 */
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +0200991 int supported_interrupts = MMA8452_INT_DRDY | MMA8452_INT_TRANS;
992 int enabled_interrupts = MMA8452_INT_TRANS;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200993
994 /* Assume wired to INT1 pin */
995 ret = i2c_smbus_write_byte_data(client,
996 MMA8452_CTRL_REG5,
997 supported_interrupts);
998 if (ret < 0)
999 return ret;
1000
1001 ret = i2c_smbus_write_byte_data(client,
1002 MMA8452_CTRL_REG4,
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001003 enabled_interrupts);
1004 if (ret < 0)
1005 return ret;
1006
1007 ret = mma8452_trigger_setup(indio_dev);
Martin Fuzzey28e34272015-06-01 15:39:52 +02001008 if (ret < 0)
1009 return ret;
1010 }
1011
Martin Fuzzeyecabae72015-05-13 12:26:38 +02001012 data->ctrl_reg1 = MMA8452_CTRL_ACTIVE |
Hartmut Knaack686027f2015-08-02 22:43:51 +02001013 (MMA8452_CTRL_DR_DEFAULT << MMA8452_CTRL_DR_SHIFT);
Martin Fuzzeyecabae72015-05-13 12:26:38 +02001014 ret = i2c_smbus_write_byte_data(client, MMA8452_CTRL_REG1,
1015 data->ctrl_reg1);
1016 if (ret < 0)
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001017 goto trigger_cleanup;
Martin Fuzzeyecabae72015-05-13 12:26:38 +02001018
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001019 ret = iio_triggered_buffer_setup(indio_dev, NULL,
Hartmut Knaack686027f2015-08-02 22:43:51 +02001020 mma8452_trigger_handler, NULL);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001021 if (ret < 0)
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001022 goto trigger_cleanup;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001023
Martin Fuzzey28e34272015-06-01 15:39:52 +02001024 if (client->irq) {
1025 ret = devm_request_threaded_irq(&client->dev,
1026 client->irq,
1027 NULL, mma8452_interrupt,
1028 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1029 client->name, indio_dev);
1030 if (ret)
1031 goto buffer_cleanup;
1032 }
1033
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001034 ret = iio_device_register(indio_dev);
1035 if (ret < 0)
1036 goto buffer_cleanup;
Martin Fuzzey28e34272015-06-01 15:39:52 +02001037
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001038 return 0;
1039
1040buffer_cleanup:
1041 iio_triggered_buffer_cleanup(indio_dev);
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001042
1043trigger_cleanup:
1044 mma8452_trigger_cleanup(indio_dev);
1045
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001046 return ret;
1047}
1048
1049static int mma8452_remove(struct i2c_client *client)
1050{
1051 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1052
1053 iio_device_unregister(indio_dev);
1054 iio_triggered_buffer_cleanup(indio_dev);
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001055 mma8452_trigger_cleanup(indio_dev);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001056 mma8452_standby(iio_priv(indio_dev));
1057
1058 return 0;
1059}
1060
1061#ifdef CONFIG_PM_SLEEP
1062static int mma8452_suspend(struct device *dev)
1063{
1064 return mma8452_standby(iio_priv(i2c_get_clientdata(
1065 to_i2c_client(dev))));
1066}
1067
1068static int mma8452_resume(struct device *dev)
1069{
1070 return mma8452_active(iio_priv(i2c_get_clientdata(
1071 to_i2c_client(dev))));
1072}
1073
1074static SIMPLE_DEV_PM_OPS(mma8452_pm_ops, mma8452_suspend, mma8452_resume);
1075#define MMA8452_PM_OPS (&mma8452_pm_ops)
1076#else
1077#define MMA8452_PM_OPS NULL
1078#endif
1079
1080static const struct i2c_device_id mma8452_id[] = {
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001081 { "mma8452", mma8452 },
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001082 { }
1083};
1084MODULE_DEVICE_TABLE(i2c, mma8452_id);
1085
1086static struct i2c_driver mma8452_driver = {
1087 .driver = {
1088 .name = "mma8452",
Martin Fuzzeya3fb96a2014-11-07 14:06:00 +00001089 .of_match_table = of_match_ptr(mma8452_dt_ids),
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001090 .pm = MMA8452_PM_OPS,
1091 },
1092 .probe = mma8452_probe,
1093 .remove = mma8452_remove,
1094 .id_table = mma8452_id,
1095};
1096module_i2c_driver(mma8452_driver);
1097
1098MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
1099MODULE_DESCRIPTION("Freescale MMA8452 accelerometer driver");
1100MODULE_LICENSE("GPL");