blob: 4d307dfb9169c502e972c2d83ec6626a25e501d0 [file] [log] [blame]
Martin Kepplinger80e3f012017-11-18 10:10:11 +01001// SPDX-License-Identifier: GPL-2.0
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00002/*
Martin Kepplingerf26ab1a2016-06-03 14:51:52 +02003 * mma8452.c - Support for following Freescale / NXP 3-axis accelerometers:
Martin Kepplingerc5ea1b582015-09-01 13:45:09 +02004 *
Martin Kepplinger16df6662016-06-03 14:51:51 +02005 * device name digital output 7-bit I2C slave address (pin selectable)
6 * ---------------------------------------------------------------------
7 * MMA8451Q 14 bit 0x1c / 0x1d
8 * MMA8452Q 12 bit 0x1c / 0x1d
9 * MMA8453Q 10 bit 0x1c / 0x1d
10 * MMA8652FC 12 bit 0x1d
11 * MMA8653FC 10 bit 0x1d
12 * FXLS8471Q 14 bit 0x1e / 0x1d / 0x1c / 0x1f
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000013 *
Martin Kepplinger40836bc2016-06-03 14:51:50 +020014 * Copyright 2015 Martin Kepplinger <martink@posteo.de>
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000015 * Copyright 2014 Peter Meerwald <pmeerw@pmeerw.net>
16 *
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000017 *
Martin Kepplingerbce59b62016-03-14 12:26:29 +010018 * TODO: orientation events
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>
Martin Kepplinger96c0cb22016-03-03 09:24:03 +010033#include <linux/pm_runtime.h>
Anson Huangf6ff49b2019-01-08 09:14:06 +000034#include <linux/regulator/consumer.h>
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000035
Hartmut Knaack69abff82015-08-02 22:43:50 +020036#define MMA8452_STATUS 0x00
37#define MMA8452_STATUS_DRDY (BIT(2) | BIT(1) | BIT(0))
Martin Kepplingerc5ea1b582015-09-01 13:45:09 +020038#define MMA8452_OUT_X 0x01 /* MSB first */
Hartmut Knaack69abff82015-08-02 22:43:50 +020039#define MMA8452_OUT_Y 0x03
40#define MMA8452_OUT_Z 0x05
41#define MMA8452_INT_SRC 0x0c
42#define MMA8452_WHO_AM_I 0x0d
43#define MMA8452_DATA_CFG 0x0e
44#define MMA8452_DATA_CFG_FS_MASK GENMASK(1, 0)
45#define MMA8452_DATA_CFG_FS_2G 0
46#define MMA8452_DATA_CFG_FS_4G 1
47#define MMA8452_DATA_CFG_FS_8G 2
48#define MMA8452_DATA_CFG_HPF_MASK BIT(4)
49#define MMA8452_HP_FILTER_CUTOFF 0x0f
50#define MMA8452_HP_FILTER_CUTOFF_SEL_MASK GENMASK(1, 0)
Martin Kepplinger60f562e2015-09-01 13:45:10 +020051#define MMA8452_FF_MT_CFG 0x15
52#define MMA8452_FF_MT_CFG_OAE BIT(6)
53#define MMA8452_FF_MT_CFG_ELE BIT(7)
54#define MMA8452_FF_MT_SRC 0x16
55#define MMA8452_FF_MT_SRC_XHE BIT(1)
56#define MMA8452_FF_MT_SRC_YHE BIT(3)
57#define MMA8452_FF_MT_SRC_ZHE BIT(5)
58#define MMA8452_FF_MT_THS 0x17
59#define MMA8452_FF_MT_THS_MASK 0x7f
60#define MMA8452_FF_MT_COUNT 0x18
Sean Nyekjaer9013b1d2021-03-01 09:00:28 +010061#define MMA8452_FF_MT_CHAN_SHIFT 3
Hartmut Knaack69abff82015-08-02 22:43:50 +020062#define MMA8452_TRANSIENT_CFG 0x1d
Harinath Nampally605f72d2017-09-09 15:56:58 -040063#define MMA8452_TRANSIENT_CFG_CHAN(chan) BIT(chan + 1)
Hartmut Knaack69abff82015-08-02 22:43:50 +020064#define MMA8452_TRANSIENT_CFG_HPF_BYP BIT(0)
Hartmut Knaack69abff82015-08-02 22:43:50 +020065#define MMA8452_TRANSIENT_CFG_ELE BIT(4)
66#define MMA8452_TRANSIENT_SRC 0x1e
67#define MMA8452_TRANSIENT_SRC_XTRANSE BIT(1)
68#define MMA8452_TRANSIENT_SRC_YTRANSE BIT(3)
69#define MMA8452_TRANSIENT_SRC_ZTRANSE BIT(5)
70#define MMA8452_TRANSIENT_THS 0x1f
71#define MMA8452_TRANSIENT_THS_MASK GENMASK(6, 0)
72#define MMA8452_TRANSIENT_COUNT 0x20
Sean Nyekjaer9013b1d2021-03-01 09:00:28 +010073#define MMA8452_TRANSIENT_CHAN_SHIFT 1
Hartmut Knaack69abff82015-08-02 22:43:50 +020074#define MMA8452_CTRL_REG1 0x2a
75#define MMA8452_CTRL_ACTIVE BIT(0)
76#define MMA8452_CTRL_DR_MASK GENMASK(5, 3)
77#define MMA8452_CTRL_DR_SHIFT 3
78#define MMA8452_CTRL_DR_DEFAULT 0x4 /* 50 Hz sample frequency */
79#define MMA8452_CTRL_REG2 0x2b
80#define MMA8452_CTRL_REG2_RST BIT(6)
Martin Kepplingered859fc2016-04-25 14:08:25 +020081#define MMA8452_CTRL_REG2_MODS_SHIFT 3
82#define MMA8452_CTRL_REG2_MODS_MASK 0x1b
Hartmut Knaack69abff82015-08-02 22:43:50 +020083#define MMA8452_CTRL_REG4 0x2d
84#define MMA8452_CTRL_REG5 0x2e
85#define MMA8452_OFF_X 0x2f
86#define MMA8452_OFF_Y 0x30
87#define MMA8452_OFF_Z 0x31
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000088
Hartmut Knaack69abff82015-08-02 22:43:50 +020089#define MMA8452_MAX_REG 0x31
Martin Fuzzey2a17698c2015-05-13 12:26:40 +020090
Hartmut Knaack69abff82015-08-02 22:43:50 +020091#define MMA8452_INT_DRDY BIT(0)
Martin Kepplinger60f562e2015-09-01 13:45:10 +020092#define MMA8452_INT_FF_MT BIT(2)
Hartmut Knaack69abff82015-08-02 22:43:50 +020093#define MMA8452_INT_TRANS BIT(5)
Peter Meerwaldc7eeea92014-02-05 09:51:00 +000094
Martin Kepplinger244a93f2016-01-16 15:35:22 +010095#define MMA8451_DEVICE_ID 0x1a
Martin Kepplinger36775d52016-01-16 15:35:21 +010096#define MMA8452_DEVICE_ID 0x2a
97#define MMA8453_DEVICE_ID 0x3a
Martin Kepplinger417e0082015-09-01 13:45:11 +020098#define MMA8652_DEVICE_ID 0x4a
99#define MMA8653_DEVICE_ID 0x5a
Martin Kepplingere8731182016-03-09 12:01:29 +0100100#define FXLS8471_DEVICE_ID 0x6a
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000101
Martin Kepplinger96c0cb22016-03-03 09:24:03 +0100102#define MMA8452_AUTO_SUSPEND_DELAY_MS 2000
103
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000104struct mma8452_data {
105 struct i2c_client *client;
106 struct mutex lock;
107 u8 ctrl_reg1;
108 u8 data_cfg;
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200109 const struct mma_chip_info *chip_info;
Richard Tresiddera45d1232018-05-11 16:54:59 +0800110 int sleep_val;
Anson Huangf6ff49b2019-01-08 09:14:06 +0000111 struct regulator *vdd_reg;
112 struct regulator *vddio_reg;
Jonathan Cameron89226a22020-07-22 16:50:38 +0100113
114 /* Ensure correct alignment of time stamp when present */
115 struct {
116 __be16 channels[3];
117 s64 ts __aligned(8);
118 } buffer;
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200119};
120
Harinath Nampally605f72d2017-09-09 15:56:58 -0400121 /**
122 * struct mma8452_event_regs - chip specific data related to events
123 * @ev_cfg: event config register address
124 * @ev_cfg_ele: latch bit in event config register
125 * @ev_cfg_chan_shift: number of the bit to enable events in X
126 * direction; in event config register
127 * @ev_src: event source register address
128 * @ev_ths: event threshold register address
129 * @ev_ths_mask: mask for the threshold value
130 * @ev_count: event count (period) register address
131 *
132 * Since not all chips supported by the driver support comparing high pass
133 * filtered data for events (interrupts), different interrupt sources are
134 * used for different chips and the relevant registers are included here.
135 */
136struct mma8452_event_regs {
Sean Nyekjaer9013b1d2021-03-01 09:00:28 +0100137 u8 ev_cfg;
138 u8 ev_cfg_ele;
139 u8 ev_cfg_chan_shift;
140 u8 ev_src;
141 u8 ev_ths;
142 u8 ev_ths_mask;
143 u8 ev_count;
Harinath Nampally605f72d2017-09-09 15:56:58 -0400144};
145
Harinath Nampallya654c062017-11-05 13:00:03 -0500146static const struct mma8452_event_regs ff_mt_ev_regs = {
Sean Nyekjaer9013b1d2021-03-01 09:00:28 +0100147 .ev_cfg = MMA8452_FF_MT_CFG,
148 .ev_cfg_ele = MMA8452_FF_MT_CFG_ELE,
149 .ev_cfg_chan_shift = MMA8452_FF_MT_CHAN_SHIFT,
150 .ev_src = MMA8452_FF_MT_SRC,
151 .ev_ths = MMA8452_FF_MT_THS,
152 .ev_ths_mask = MMA8452_FF_MT_THS_MASK,
153 .ev_count = MMA8452_FF_MT_COUNT
Harinath Nampally605f72d2017-09-09 15:56:58 -0400154};
155
Harinath Nampallya654c062017-11-05 13:00:03 -0500156static const struct mma8452_event_regs trans_ev_regs = {
Sean Nyekjaer9013b1d2021-03-01 09:00:28 +0100157 .ev_cfg = MMA8452_TRANSIENT_CFG,
158 .ev_cfg_ele = MMA8452_TRANSIENT_CFG_ELE,
159 .ev_cfg_chan_shift = MMA8452_TRANSIENT_CHAN_SHIFT,
160 .ev_src = MMA8452_TRANSIENT_SRC,
161 .ev_ths = MMA8452_TRANSIENT_THS,
162 .ev_ths_mask = MMA8452_TRANSIENT_THS_MASK,
163 .ev_count = MMA8452_TRANSIENT_COUNT,
Harinath Nampally605f72d2017-09-09 15:56:58 -0400164};
165
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200166/**
Martin Kepplingerf26ab1a2016-06-03 14:51:52 +0200167 * struct mma_chip_info - chip specific data
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200168 * @chip_id: WHO_AM_I register's value
169 * @channels: struct iio_chan_spec matching the device's
170 * capabilities
171 * @num_channels: number of channels
172 * @mma_scales: scale factors for converting register values
173 * to m/s^2; 3 modes: 2g, 4g, 8g; 2 integers
174 * per mode: m/s^2 and micro m/s^2
Harinath Nampally605f72d2017-09-09 15:56:58 -0400175 * @all_events: all events supported by this chip
176 * @enabled_events: event flags enabled and handled by this driver
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200177 */
178struct mma_chip_info {
179 u8 chip_id;
180 const struct iio_chan_spec *channels;
181 int num_channels;
182 const int mma_scales[3][2];
Harinath Nampally605f72d2017-09-09 15:56:58 -0400183 int all_events;
184 int enabled_events;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000185};
186
Martin Kepplingere60378c2015-12-15 17:45:00 +0100187enum {
188 idx_x,
189 idx_y,
190 idx_z,
191 idx_ts,
192};
193
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000194static int mma8452_drdy(struct mma8452_data *data)
195{
196 int tries = 150;
197
198 while (tries-- > 0) {
199 int ret = i2c_smbus_read_byte_data(data->client,
200 MMA8452_STATUS);
201 if (ret < 0)
202 return ret;
203 if ((ret & MMA8452_STATUS_DRDY) == MMA8452_STATUS_DRDY)
204 return 0;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200205
Richard Tresiddera45d1232018-05-11 16:54:59 +0800206 if (data->sleep_val <= 20)
207 usleep_range(data->sleep_val * 250,
208 data->sleep_val * 500);
209 else
210 msleep(20);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000211 }
212
213 dev_err(&data->client->dev, "data not ready\n");
Hartmut Knaack686027f2015-08-02 22:43:51 +0200214
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000215 return -EIO;
216}
217
Martin Kepplinger96c0cb22016-03-03 09:24:03 +0100218static int mma8452_set_runtime_pm_state(struct i2c_client *client, bool on)
219{
220#ifdef CONFIG_PM
221 int ret;
222
223 if (on) {
224 ret = pm_runtime_get_sync(&client->dev);
225 } else {
226 pm_runtime_mark_last_busy(&client->dev);
227 ret = pm_runtime_put_autosuspend(&client->dev);
228 }
229
230 if (ret < 0) {
231 dev_err(&client->dev,
232 "failed to change power state to %d\n", on);
233 if (on)
234 pm_runtime_put_noidle(&client->dev);
235
236 return ret;
237 }
238#endif
239
240 return 0;
241}
242
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000243static int mma8452_read(struct mma8452_data *data, __be16 buf[3])
244{
245 int ret = mma8452_drdy(data);
Hartmut Knaack686027f2015-08-02 22:43:51 +0200246
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000247 if (ret < 0)
248 return ret;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200249
Martin Kepplinger96c0cb22016-03-03 09:24:03 +0100250 ret = mma8452_set_runtime_pm_state(data->client, true);
251 if (ret)
252 return ret;
253
254 ret = i2c_smbus_read_i2c_block_data(data->client, MMA8452_OUT_X,
255 3 * sizeof(__be16), (u8 *)buf);
256
257 ret = mma8452_set_runtime_pm_state(data->client, false);
258
259 return ret;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000260}
261
Hartmut Knaack686027f2015-08-02 22:43:51 +0200262static ssize_t mma8452_show_int_plus_micros(char *buf, const int (*vals)[2],
263 int n)
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000264{
265 size_t len = 0;
266
267 while (n-- > 0)
Hartmut Knaack686027f2015-08-02 22:43:51 +0200268 len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06d ",
269 vals[n][0], vals[n][1]);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000270
271 /* replace trailing space by newline */
272 buf[len - 1] = '\n';
273
274 return len;
275}
276
277static int mma8452_get_int_plus_micros_index(const int (*vals)[2], int n,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200278 int val, int val2)
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000279{
280 while (n-- > 0)
281 if (val == vals[n][0] && val2 == vals[n][1])
282 return n;
283
284 return -EINVAL;
285}
286
Martin Kepplinger32b28072016-11-21 20:53:54 +0100287static unsigned int mma8452_get_odr_index(struct mma8452_data *data)
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200288{
289 return (data->ctrl_reg1 & MMA8452_CTRL_DR_MASK) >>
290 MMA8452_CTRL_DR_SHIFT;
291}
292
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000293static const int mma8452_samp_freq[8][2] = {
294 {800, 0}, {400, 0}, {200, 0}, {100, 0}, {50, 0}, {12, 500000},
295 {6, 250000}, {1, 560000}
296};
297
Martin Kepplingered859fc2016-04-25 14:08:25 +0200298/* Datasheet table: step time "Relationship with the ODR" (sample frequency) */
Harinath Nampallycc54a662017-11-05 13:00:02 -0500299static const unsigned int mma8452_time_step_us[4][8] = {
Martin Kepplingered859fc2016-04-25 14:08:25 +0200300 { 1250, 2500, 5000, 10000, 20000, 20000, 20000, 20000 }, /* normal */
301 { 1250, 2500, 5000, 10000, 20000, 80000, 80000, 80000 }, /* l p l n */
302 { 1250, 2500, 2500, 2500, 2500, 2500, 2500, 2500 }, /* high res*/
303 { 1250, 2500, 5000, 10000, 20000, 80000, 160000, 160000 } /* l p */
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200304};
305
Martin Kepplingered859fc2016-04-25 14:08:25 +0200306/* Datasheet table "High-Pass Filter Cutoff Options" */
307static const int mma8452_hp_filter_cutoff[4][8][4][2] = {
308 { /* normal */
Martin Fuzzey1e798412015-06-01 15:39:56 +0200309 { {16, 0}, {8, 0}, {4, 0}, {2, 0} }, /* 800 Hz sample */
310 { {16, 0}, {8, 0}, {4, 0}, {2, 0} }, /* 400 Hz sample */
311 { {8, 0}, {4, 0}, {2, 0}, {1, 0} }, /* 200 Hz sample */
312 { {4, 0}, {2, 0}, {1, 0}, {0, 500000} }, /* 100 Hz sample */
313 { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} }, /* 50 Hz sample */
314 { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} }, /* 12.5 Hz sample */
315 { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} }, /* 6.25 Hz sample */
316 { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} } /* 1.56 Hz sample */
Martin Kepplingered859fc2016-04-25 14:08:25 +0200317 },
318 { /* low noise low power */
319 { {16, 0}, {8, 0}, {4, 0}, {2, 0} },
320 { {16, 0}, {8, 0}, {4, 0}, {2, 0} },
321 { {8, 0}, {4, 0}, {2, 0}, {1, 0} },
322 { {4, 0}, {2, 0}, {1, 0}, {0, 500000} },
323 { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} },
324 { {0, 500000}, {0, 250000}, {0, 125000}, {0, 063000} },
325 { {0, 500000}, {0, 250000}, {0, 125000}, {0, 063000} },
326 { {0, 500000}, {0, 250000}, {0, 125000}, {0, 063000} }
327 },
328 { /* high resolution */
329 { {16, 0}, {8, 0}, {4, 0}, {2, 0} },
330 { {16, 0}, {8, 0}, {4, 0}, {2, 0} },
331 { {16, 0}, {8, 0}, {4, 0}, {2, 0} },
332 { {16, 0}, {8, 0}, {4, 0}, {2, 0} },
333 { {16, 0}, {8, 0}, {4, 0}, {2, 0} },
334 { {16, 0}, {8, 0}, {4, 0}, {2, 0} },
335 { {16, 0}, {8, 0}, {4, 0}, {2, 0} },
336 { {16, 0}, {8, 0}, {4, 0}, {2, 0} }
337 },
338 { /* low power */
339 { {16, 0}, {8, 0}, {4, 0}, {2, 0} },
340 { {8, 0}, {4, 0}, {2, 0}, {1, 0} },
341 { {4, 0}, {2, 0}, {1, 0}, {0, 500000} },
342 { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} },
343 { {1, 0}, {0, 500000}, {0, 250000}, {0, 125000} },
344 { {0, 250000}, {0, 125000}, {0, 063000}, {0, 031000} },
345 { {0, 250000}, {0, 125000}, {0, 063000}, {0, 031000} },
346 { {0, 250000}, {0, 125000}, {0, 063000}, {0, 031000} }
347 }
Martin Fuzzey1e798412015-06-01 15:39:56 +0200348};
349
Martin Kepplingered859fc2016-04-25 14:08:25 +0200350/* Datasheet table "MODS Oversampling modes averaging values at each ODR" */
351static const u16 mma8452_os_ratio[4][8] = {
352 /* 800 Hz, 400 Hz, ... , 1.56 Hz */
353 { 2, 4, 4, 4, 4, 16, 32, 128 }, /* normal */
354 { 2, 4, 4, 4, 4, 4, 8, 32 }, /* low power low noise */
355 { 2, 4, 8, 16, 32, 128, 256, 1024 }, /* high resolution */
356 { 2, 2, 2, 2, 2, 2, 4, 16 } /* low power */
357};
358
359static int mma8452_get_power_mode(struct mma8452_data *data)
360{
361 int reg;
362
363 reg = i2c_smbus_read_byte_data(data->client,
364 MMA8452_CTRL_REG2);
365 if (reg < 0)
366 return reg;
367
368 return ((reg & MMA8452_CTRL_REG2_MODS_MASK) >>
369 MMA8452_CTRL_REG2_MODS_SHIFT);
370}
371
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000372static ssize_t mma8452_show_samp_freq_avail(struct device *dev,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200373 struct device_attribute *attr,
374 char *buf)
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000375{
376 return mma8452_show_int_plus_micros(buf, mma8452_samp_freq,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200377 ARRAY_SIZE(mma8452_samp_freq));
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000378}
379
380static ssize_t mma8452_show_scale_avail(struct device *dev,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200381 struct device_attribute *attr,
382 char *buf)
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000383{
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200384 struct mma8452_data *data = iio_priv(i2c_get_clientdata(
385 to_i2c_client(dev)));
386
387 return mma8452_show_int_plus_micros(buf, data->chip_info->mma_scales,
388 ARRAY_SIZE(data->chip_info->mma_scales));
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000389}
390
Martin Fuzzey1e798412015-06-01 15:39:56 +0200391static ssize_t mma8452_show_hp_cutoff_avail(struct device *dev,
392 struct device_attribute *attr,
393 char *buf)
394{
395 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
396 struct mma8452_data *data = iio_priv(indio_dev);
Martin Kepplingered859fc2016-04-25 14:08:25 +0200397 int i, j;
Martin Fuzzey1e798412015-06-01 15:39:56 +0200398
Martin Kepplingered859fc2016-04-25 14:08:25 +0200399 i = mma8452_get_odr_index(data);
400 j = mma8452_get_power_mode(data);
401 if (j < 0)
402 return j;
403
404 return mma8452_show_int_plus_micros(buf, mma8452_hp_filter_cutoff[j][i],
405 ARRAY_SIZE(mma8452_hp_filter_cutoff[0][0]));
406}
407
408static ssize_t mma8452_show_os_ratio_avail(struct device *dev,
409 struct device_attribute *attr,
410 char *buf)
411{
412 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
413 struct mma8452_data *data = iio_priv(indio_dev);
414 int i = mma8452_get_odr_index(data);
415 int j;
416 u16 val = 0;
417 size_t len = 0;
418
419 for (j = 0; j < ARRAY_SIZE(mma8452_os_ratio); j++) {
420 if (val == mma8452_os_ratio[j][i])
421 continue;
422
423 val = mma8452_os_ratio[j][i];
424
425 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", val);
426 }
427 buf[len - 1] = '\n';
428
429 return len;
Martin Fuzzey1e798412015-06-01 15:39:56 +0200430}
431
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000432static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(mma8452_show_samp_freq_avail);
Harinath Nampallycd327b02017-09-23 16:56:29 -0400433static IIO_DEVICE_ATTR(in_accel_scale_available, 0444,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200434 mma8452_show_scale_avail, NULL, 0);
Martin Fuzzey1e798412015-06-01 15:39:56 +0200435static IIO_DEVICE_ATTR(in_accel_filter_high_pass_3db_frequency_available,
Harinath Nampallycd327b02017-09-23 16:56:29 -0400436 0444, mma8452_show_hp_cutoff_avail, NULL, 0);
437static IIO_DEVICE_ATTR(in_accel_oversampling_ratio_available, 0444,
Martin Kepplingered859fc2016-04-25 14:08:25 +0200438 mma8452_show_os_ratio_avail, NULL, 0);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000439
440static int mma8452_get_samp_freq_index(struct mma8452_data *data,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200441 int val, int val2)
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000442{
443 return mma8452_get_int_plus_micros_index(mma8452_samp_freq,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200444 ARRAY_SIZE(mma8452_samp_freq),
445 val, val2);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000446}
447
Hartmut Knaack686027f2015-08-02 22:43:51 +0200448static int mma8452_get_scale_index(struct mma8452_data *data, int val, int val2)
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000449{
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200450 return mma8452_get_int_plus_micros_index(data->chip_info->mma_scales,
451 ARRAY_SIZE(data->chip_info->mma_scales), val, val2);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000452}
453
Martin Fuzzey1e798412015-06-01 15:39:56 +0200454static int mma8452_get_hp_filter_index(struct mma8452_data *data,
455 int val, int val2)
456{
Martin Kepplingered859fc2016-04-25 14:08:25 +0200457 int i, j;
Martin Fuzzey1e798412015-06-01 15:39:56 +0200458
Martin Kepplingered859fc2016-04-25 14:08:25 +0200459 i = mma8452_get_odr_index(data);
460 j = mma8452_get_power_mode(data);
461 if (j < 0)
462 return j;
463
464 return mma8452_get_int_plus_micros_index(mma8452_hp_filter_cutoff[j][i],
465 ARRAY_SIZE(mma8452_hp_filter_cutoff[0][0]), val, val2);
Martin Fuzzey1e798412015-06-01 15:39:56 +0200466}
467
468static int mma8452_read_hp_filter(struct mma8452_data *data, int *hz, int *uHz)
469{
Martin Kepplingered859fc2016-04-25 14:08:25 +0200470 int j, i, ret;
Martin Fuzzey1e798412015-06-01 15:39:56 +0200471
472 ret = i2c_smbus_read_byte_data(data->client, MMA8452_HP_FILTER_CUTOFF);
473 if (ret < 0)
474 return ret;
475
476 i = mma8452_get_odr_index(data);
Martin Kepplingered859fc2016-04-25 14:08:25 +0200477 j = mma8452_get_power_mode(data);
478 if (j < 0)
479 return j;
480
Martin Fuzzey1e798412015-06-01 15:39:56 +0200481 ret &= MMA8452_HP_FILTER_CUTOFF_SEL_MASK;
Martin Kepplingered859fc2016-04-25 14:08:25 +0200482 *hz = mma8452_hp_filter_cutoff[j][i][ret][0];
483 *uHz = mma8452_hp_filter_cutoff[j][i][ret][1];
Martin Fuzzey1e798412015-06-01 15:39:56 +0200484
485 return 0;
486}
487
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000488static int mma8452_read_raw(struct iio_dev *indio_dev,
489 struct iio_chan_spec const *chan,
490 int *val, int *val2, long mask)
491{
492 struct mma8452_data *data = iio_priv(indio_dev);
493 __be16 buffer[3];
494 int i, ret;
495
496 switch (mask) {
497 case IIO_CHAN_INFO_RAW:
Alison Schofield4d9b0412016-10-11 12:31:36 -0700498 ret = iio_device_claim_direct_mode(indio_dev);
499 if (ret)
500 return ret;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000501
502 mutex_lock(&data->lock);
503 ret = mma8452_read(data, buffer);
504 mutex_unlock(&data->lock);
Alison Schofield4d9b0412016-10-11 12:31:36 -0700505 iio_device_release_direct_mode(indio_dev);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000506 if (ret < 0)
507 return ret;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200508
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200509 *val = sign_extend32(be16_to_cpu(
510 buffer[chan->scan_index]) >> chan->scan_type.shift,
511 chan->scan_type.realbits - 1);
Hartmut Knaack686027f2015-08-02 22:43:51 +0200512
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000513 return IIO_VAL_INT;
514 case IIO_CHAN_INFO_SCALE:
515 i = data->data_cfg & MMA8452_DATA_CFG_FS_MASK;
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200516 *val = data->chip_info->mma_scales[i][0];
517 *val2 = data->chip_info->mma_scales[i][1];
Hartmut Knaack686027f2015-08-02 22:43:51 +0200518
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000519 return IIO_VAL_INT_PLUS_MICRO;
520 case IIO_CHAN_INFO_SAMP_FREQ:
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200521 i = mma8452_get_odr_index(data);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000522 *val = mma8452_samp_freq[i][0];
523 *val2 = mma8452_samp_freq[i][1];
Hartmut Knaack686027f2015-08-02 22:43:51 +0200524
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000525 return IIO_VAL_INT_PLUS_MICRO;
526 case IIO_CHAN_INFO_CALIBBIAS:
Hartmut Knaack686027f2015-08-02 22:43:51 +0200527 ret = i2c_smbus_read_byte_data(data->client,
Martin Kepplinger8b8ff3a2016-03-03 09:24:01 +0100528 MMA8452_OFF_X +
529 chan->scan_index);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000530 if (ret < 0)
531 return ret;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200532
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000533 *val = sign_extend32(ret, 7);
Hartmut Knaack686027f2015-08-02 22:43:51 +0200534
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000535 return IIO_VAL_INT;
Martin Fuzzey1e798412015-06-01 15:39:56 +0200536 case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY:
537 if (data->data_cfg & MMA8452_DATA_CFG_HPF_MASK) {
538 ret = mma8452_read_hp_filter(data, val, val2);
539 if (ret < 0)
540 return ret;
541 } else {
542 *val = 0;
543 *val2 = 0;
544 }
Hartmut Knaack686027f2015-08-02 22:43:51 +0200545
Martin Fuzzey1e798412015-06-01 15:39:56 +0200546 return IIO_VAL_INT_PLUS_MICRO;
Martin Kepplingered859fc2016-04-25 14:08:25 +0200547 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
548 ret = mma8452_get_power_mode(data);
549 if (ret < 0)
550 return ret;
551
552 i = mma8452_get_odr_index(data);
553
554 *val = mma8452_os_ratio[ret][i];
555 return IIO_VAL_INT;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000556 }
Hartmut Knaack686027f2015-08-02 22:43:51 +0200557
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000558 return -EINVAL;
559}
560
Richard Tresiddera45d1232018-05-11 16:54:59 +0800561static int mma8452_calculate_sleep(struct mma8452_data *data)
562{
563 int ret, i = mma8452_get_odr_index(data);
564
565 if (mma8452_samp_freq[i][0] > 0)
566 ret = 1000 / mma8452_samp_freq[i][0];
567 else
568 ret = 1000;
569
570 return ret == 0 ? 1 : ret;
571}
572
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000573static int mma8452_standby(struct mma8452_data *data)
574{
575 return i2c_smbus_write_byte_data(data->client, MMA8452_CTRL_REG1,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200576 data->ctrl_reg1 & ~MMA8452_CTRL_ACTIVE);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000577}
578
579static int mma8452_active(struct mma8452_data *data)
580{
581 return i2c_smbus_write_byte_data(data->client, MMA8452_CTRL_REG1,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200582 data->ctrl_reg1);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000583}
584
Martin Kepplingere8668532016-03-03 09:24:02 +0100585/* returns >0 if active, 0 if in standby and <0 on error */
586static int mma8452_is_active(struct mma8452_data *data)
587{
588 int reg;
589
590 reg = i2c_smbus_read_byte_data(data->client, MMA8452_CTRL_REG1);
591 if (reg < 0)
592 return reg;
593
594 return reg & MMA8452_CTRL_ACTIVE;
595}
596
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000597static int mma8452_change_config(struct mma8452_data *data, u8 reg, u8 val)
598{
599 int ret;
Martin Kepplingere8668532016-03-03 09:24:02 +0100600 int is_active;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000601
602 mutex_lock(&data->lock);
603
Martin Kepplingere8668532016-03-03 09:24:02 +0100604 is_active = mma8452_is_active(data);
605 if (is_active < 0) {
606 ret = is_active;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000607 goto fail;
Martin Kepplingere8668532016-03-03 09:24:02 +0100608 }
609
610 /* config can only be changed when in standby */
611 if (is_active > 0) {
612 ret = mma8452_standby(data);
613 if (ret < 0)
614 goto fail;
615 }
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000616
617 ret = i2c_smbus_write_byte_data(data->client, reg, val);
618 if (ret < 0)
619 goto fail;
620
Martin Kepplingere8668532016-03-03 09:24:02 +0100621 if (is_active > 0) {
622 ret = mma8452_active(data);
623 if (ret < 0)
624 goto fail;
625 }
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000626
627 ret = 0;
628fail:
629 mutex_unlock(&data->lock);
Hartmut Knaack686027f2015-08-02 22:43:51 +0200630
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000631 return ret;
632}
633
Martin Kepplingered859fc2016-04-25 14:08:25 +0200634static int mma8452_set_power_mode(struct mma8452_data *data, u8 mode)
635{
636 int reg;
637
638 reg = i2c_smbus_read_byte_data(data->client,
639 MMA8452_CTRL_REG2);
640 if (reg < 0)
641 return reg;
642
643 reg &= ~MMA8452_CTRL_REG2_MODS_MASK;
644 reg |= mode << MMA8452_CTRL_REG2_MODS_SHIFT;
645
646 return mma8452_change_config(data, MMA8452_CTRL_REG2, reg);
647}
648
Martin Kepplinger8b8ff3a2016-03-03 09:24:01 +0100649/* returns >0 if in freefall mode, 0 if not or <0 if an error occurred */
Martin Kepplinger4b042662016-01-16 15:35:20 +0100650static int mma8452_freefall_mode_enabled(struct mma8452_data *data)
651{
652 int val;
Martin Kepplinger4b042662016-01-16 15:35:20 +0100653
Harinath Nampally605f72d2017-09-09 15:56:58 -0400654 val = i2c_smbus_read_byte_data(data->client, MMA8452_FF_MT_CFG);
Martin Kepplinger4b042662016-01-16 15:35:20 +0100655 if (val < 0)
656 return val;
657
658 return !(val & MMA8452_FF_MT_CFG_OAE);
659}
660
661static int mma8452_set_freefall_mode(struct mma8452_data *data, bool state)
662{
663 int val;
Martin Kepplinger4b042662016-01-16 15:35:20 +0100664
665 if ((state && mma8452_freefall_mode_enabled(data)) ||
666 (!state && !(mma8452_freefall_mode_enabled(data))))
667 return 0;
668
Harinath Nampally605f72d2017-09-09 15:56:58 -0400669 val = i2c_smbus_read_byte_data(data->client, MMA8452_FF_MT_CFG);
Martin Kepplinger4b042662016-01-16 15:35:20 +0100670 if (val < 0)
671 return val;
672
673 if (state) {
Harinath Nampally605f72d2017-09-09 15:56:58 -0400674 val |= BIT(idx_x + MMA8452_FF_MT_CHAN_SHIFT);
675 val |= BIT(idx_y + MMA8452_FF_MT_CHAN_SHIFT);
676 val |= BIT(idx_z + MMA8452_FF_MT_CHAN_SHIFT);
Martin Kepplinger4b042662016-01-16 15:35:20 +0100677 val &= ~MMA8452_FF_MT_CFG_OAE;
678 } else {
Harinath Nampally605f72d2017-09-09 15:56:58 -0400679 val &= ~BIT(idx_x + MMA8452_FF_MT_CHAN_SHIFT);
680 val &= ~BIT(idx_y + MMA8452_FF_MT_CHAN_SHIFT);
681 val &= ~BIT(idx_z + MMA8452_FF_MT_CHAN_SHIFT);
Martin Kepplinger4b042662016-01-16 15:35:20 +0100682 val |= MMA8452_FF_MT_CFG_OAE;
683 }
684
Harinath Nampally605f72d2017-09-09 15:56:58 -0400685 return mma8452_change_config(data, MMA8452_FF_MT_CFG, val);
Martin Kepplinger4b042662016-01-16 15:35:20 +0100686}
687
Martin Fuzzey1e798412015-06-01 15:39:56 +0200688static int mma8452_set_hp_filter_frequency(struct mma8452_data *data,
689 int val, int val2)
690{
691 int i, reg;
692
693 i = mma8452_get_hp_filter_index(data, val, val2);
694 if (i < 0)
Hartmut Knaackb9fddcd2015-08-02 22:43:48 +0200695 return i;
Martin Fuzzey1e798412015-06-01 15:39:56 +0200696
697 reg = i2c_smbus_read_byte_data(data->client,
698 MMA8452_HP_FILTER_CUTOFF);
699 if (reg < 0)
700 return reg;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200701
Martin Fuzzey1e798412015-06-01 15:39:56 +0200702 reg &= ~MMA8452_HP_FILTER_CUTOFF_SEL_MASK;
703 reg |= i;
704
705 return mma8452_change_config(data, MMA8452_HP_FILTER_CUTOFF, reg);
706}
707
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000708static int mma8452_write_raw(struct iio_dev *indio_dev,
709 struct iio_chan_spec const *chan,
710 int val, int val2, long mask)
711{
712 struct mma8452_data *data = iio_priv(indio_dev);
Martin Fuzzey1e798412015-06-01 15:39:56 +0200713 int i, ret;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000714
Jonathan Cameron79de2ee2016-10-15 15:55:06 +0100715 ret = iio_device_claim_direct_mode(indio_dev);
716 if (ret)
717 return ret;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000718
719 switch (mask) {
720 case IIO_CHAN_INFO_SAMP_FREQ:
721 i = mma8452_get_samp_freq_index(data, val, val2);
Jonathan Cameron79de2ee2016-10-15 15:55:06 +0100722 if (i < 0) {
723 ret = i;
724 break;
725 }
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000726 data->ctrl_reg1 &= ~MMA8452_CTRL_DR_MASK;
727 data->ctrl_reg1 |= i << MMA8452_CTRL_DR_SHIFT;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200728
Richard Tresiddera45d1232018-05-11 16:54:59 +0800729 data->sleep_val = mma8452_calculate_sleep(data);
730
Jonathan Cameron79de2ee2016-10-15 15:55:06 +0100731 ret = mma8452_change_config(data, MMA8452_CTRL_REG1,
732 data->ctrl_reg1);
733 break;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000734 case IIO_CHAN_INFO_SCALE:
735 i = mma8452_get_scale_index(data, val, val2);
Jonathan Cameron79de2ee2016-10-15 15:55:06 +0100736 if (i < 0) {
737 ret = i;
738 break;
739 }
Hartmut Knaack686027f2015-08-02 22:43:51 +0200740
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000741 data->data_cfg &= ~MMA8452_DATA_CFG_FS_MASK;
742 data->data_cfg |= i;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200743
Jonathan Cameron79de2ee2016-10-15 15:55:06 +0100744 ret = mma8452_change_config(data, MMA8452_DATA_CFG,
745 data->data_cfg);
746 break;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000747 case IIO_CHAN_INFO_CALIBBIAS:
Jonathan Cameron79de2ee2016-10-15 15:55:06 +0100748 if (val < -128 || val > 127) {
749 ret = -EINVAL;
750 break;
751 }
Hartmut Knaack686027f2015-08-02 22:43:51 +0200752
Jonathan Cameron79de2ee2016-10-15 15:55:06 +0100753 ret = mma8452_change_config(data,
754 MMA8452_OFF_X + chan->scan_index,
755 val);
756 break;
Martin Fuzzey1e798412015-06-01 15:39:56 +0200757
758 case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY:
759 if (val == 0 && val2 == 0) {
760 data->data_cfg &= ~MMA8452_DATA_CFG_HPF_MASK;
761 } else {
762 data->data_cfg |= MMA8452_DATA_CFG_HPF_MASK;
763 ret = mma8452_set_hp_filter_frequency(data, val, val2);
764 if (ret < 0)
Jonathan Cameron79de2ee2016-10-15 15:55:06 +0100765 break;
Martin Fuzzey1e798412015-06-01 15:39:56 +0200766 }
Hartmut Knaack686027f2015-08-02 22:43:51 +0200767
Jonathan Cameron79de2ee2016-10-15 15:55:06 +0100768 ret = mma8452_change_config(data, MMA8452_DATA_CFG,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200769 data->data_cfg);
Jonathan Cameron79de2ee2016-10-15 15:55:06 +0100770 break;
Martin Fuzzey1e798412015-06-01 15:39:56 +0200771
Martin Kepplingered859fc2016-04-25 14:08:25 +0200772 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
773 ret = mma8452_get_odr_index(data);
774
775 for (i = 0; i < ARRAY_SIZE(mma8452_os_ratio); i++) {
Jonathan Cameron79de2ee2016-10-15 15:55:06 +0100776 if (mma8452_os_ratio[i][ret] == val) {
777 ret = mma8452_set_power_mode(data, i);
778 break;
779 }
Martin Kepplingered859fc2016-04-25 14:08:25 +0200780 }
Jonathan Cameron79de2ee2016-10-15 15:55:06 +0100781 break;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000782 default:
Jonathan Cameron79de2ee2016-10-15 15:55:06 +0100783 ret = -EINVAL;
784 break;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000785 }
Jonathan Cameron79de2ee2016-10-15 15:55:06 +0100786
787 iio_device_release_direct_mode(indio_dev);
788 return ret;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000789}
790
Harinath Nampally605f72d2017-09-09 15:56:58 -0400791static int mma8452_get_event_regs(struct mma8452_data *data,
792 const struct iio_chan_spec *chan, enum iio_event_direction dir,
793 const struct mma8452_event_regs **ev_reg)
794{
795 if (!chan)
796 return -EINVAL;
797
798 switch (chan->type) {
799 case IIO_ACCEL:
800 switch (dir) {
801 case IIO_EV_DIR_RISING:
802 if ((data->chip_info->all_events
803 & MMA8452_INT_TRANS) &&
804 (data->chip_info->enabled_events
805 & MMA8452_INT_TRANS))
Harinath Nampallya654c062017-11-05 13:00:03 -0500806 *ev_reg = &trans_ev_regs;
Harinath Nampally605f72d2017-09-09 15:56:58 -0400807 else
Harinath Nampallya654c062017-11-05 13:00:03 -0500808 *ev_reg = &ff_mt_ev_regs;
Harinath Nampally605f72d2017-09-09 15:56:58 -0400809 return 0;
810 case IIO_EV_DIR_FALLING:
Harinath Nampallya654c062017-11-05 13:00:03 -0500811 *ev_reg = &ff_mt_ev_regs;
Harinath Nampally605f72d2017-09-09 15:56:58 -0400812 return 0;
813 default:
814 return -EINVAL;
815 }
816 default:
817 return -EINVAL;
818 }
819}
820
Harinath Nampally4febd9f2017-09-25 06:40:08 -0400821static int mma8452_read_event_value(struct iio_dev *indio_dev,
Martin Fuzzey28e34272015-06-01 15:39:52 +0200822 const struct iio_chan_spec *chan,
823 enum iio_event_type type,
824 enum iio_event_direction dir,
825 enum iio_event_info info,
826 int *val, int *val2)
827{
828 struct mma8452_data *data = iio_priv(indio_dev);
Martin Kepplingered859fc2016-04-25 14:08:25 +0200829 int ret, us, power_mode;
Harinath Nampally605f72d2017-09-09 15:56:58 -0400830 const struct mma8452_event_regs *ev_regs;
831
832 ret = mma8452_get_event_regs(data, chan, dir, &ev_regs);
833 if (ret)
834 return ret;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200835
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200836 switch (info) {
837 case IIO_EV_INFO_VALUE:
Harinath Nampally605f72d2017-09-09 15:56:58 -0400838 ret = i2c_smbus_read_byte_data(data->client, ev_regs->ev_ths);
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200839 if (ret < 0)
840 return ret;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200841
Harinath Nampally605f72d2017-09-09 15:56:58 -0400842 *val = ret & ev_regs->ev_ths_mask;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200843
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200844 return IIO_VAL_INT;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200845
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200846 case IIO_EV_INFO_PERIOD:
Harinath Nampally605f72d2017-09-09 15:56:58 -0400847 ret = i2c_smbus_read_byte_data(data->client, ev_regs->ev_count);
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200848 if (ret < 0)
849 return ret;
850
Martin Kepplingered859fc2016-04-25 14:08:25 +0200851 power_mode = mma8452_get_power_mode(data);
852 if (power_mode < 0)
853 return power_mode;
854
Harinath Nampallycc54a662017-11-05 13:00:02 -0500855 us = ret * mma8452_time_step_us[power_mode][
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200856 mma8452_get_odr_index(data)];
857 *val = us / USEC_PER_SEC;
858 *val2 = us % USEC_PER_SEC;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200859
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200860 return IIO_VAL_INT_PLUS_MICRO;
861
Martin Fuzzey1e798412015-06-01 15:39:56 +0200862 case IIO_EV_INFO_HIGH_PASS_FILTER_3DB:
863 ret = i2c_smbus_read_byte_data(data->client,
864 MMA8452_TRANSIENT_CFG);
865 if (ret < 0)
866 return ret;
867
868 if (ret & MMA8452_TRANSIENT_CFG_HPF_BYP) {
869 *val = 0;
870 *val2 = 0;
871 } else {
872 ret = mma8452_read_hp_filter(data, val, val2);
873 if (ret < 0)
874 return ret;
875 }
Hartmut Knaack686027f2015-08-02 22:43:51 +0200876
Martin Fuzzey1e798412015-06-01 15:39:56 +0200877 return IIO_VAL_INT_PLUS_MICRO;
878
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200879 default:
880 return -EINVAL;
881 }
Martin Fuzzey28e34272015-06-01 15:39:52 +0200882}
883
Harinath Nampally4febd9f2017-09-25 06:40:08 -0400884static int mma8452_write_event_value(struct iio_dev *indio_dev,
Martin Fuzzey28e34272015-06-01 15:39:52 +0200885 const struct iio_chan_spec *chan,
886 enum iio_event_type type,
887 enum iio_event_direction dir,
888 enum iio_event_info info,
889 int val, int val2)
890{
891 struct mma8452_data *data = iio_priv(indio_dev);
Martin Fuzzey1e798412015-06-01 15:39:56 +0200892 int ret, reg, steps;
Harinath Nampally605f72d2017-09-09 15:56:58 -0400893 const struct mma8452_event_regs *ev_regs;
894
895 ret = mma8452_get_event_regs(data, chan, dir, &ev_regs);
896 if (ret)
897 return ret;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200898
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200899 switch (info) {
900 case IIO_EV_INFO_VALUE:
Harinath Nampally605f72d2017-09-09 15:56:58 -0400901 if (val < 0 || val > ev_regs->ev_ths_mask)
Hartmut Knaack11218222015-08-02 22:43:49 +0200902 return -EINVAL;
903
Harinath Nampally605f72d2017-09-09 15:56:58 -0400904 return mma8452_change_config(data, ev_regs->ev_ths, val);
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200905
906 case IIO_EV_INFO_PERIOD:
Martin Kepplingered859fc2016-04-25 14:08:25 +0200907 ret = mma8452_get_power_mode(data);
908 if (ret < 0)
909 return ret;
910
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200911 steps = (val * USEC_PER_SEC + val2) /
Harinath Nampallycc54a662017-11-05 13:00:02 -0500912 mma8452_time_step_us[ret][
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200913 mma8452_get_odr_index(data)];
914
Hartmut Knaack11218222015-08-02 22:43:49 +0200915 if (steps < 0 || steps > 0xff)
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200916 return -EINVAL;
917
Harinath Nampally605f72d2017-09-09 15:56:58 -0400918 return mma8452_change_config(data, ev_regs->ev_count, steps);
Hartmut Knaack686027f2015-08-02 22:43:51 +0200919
Martin Fuzzey1e798412015-06-01 15:39:56 +0200920 case IIO_EV_INFO_HIGH_PASS_FILTER_3DB:
921 reg = i2c_smbus_read_byte_data(data->client,
922 MMA8452_TRANSIENT_CFG);
923 if (reg < 0)
924 return reg;
925
926 if (val == 0 && val2 == 0) {
927 reg |= MMA8452_TRANSIENT_CFG_HPF_BYP;
928 } else {
929 reg &= ~MMA8452_TRANSIENT_CFG_HPF_BYP;
930 ret = mma8452_set_hp_filter_frequency(data, val, val2);
931 if (ret < 0)
932 return ret;
933 }
Hartmut Knaack686027f2015-08-02 22:43:51 +0200934
Martin Fuzzey1e798412015-06-01 15:39:56 +0200935 return mma8452_change_config(data, MMA8452_TRANSIENT_CFG, reg);
936
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200937 default:
938 return -EINVAL;
939 }
Martin Fuzzey28e34272015-06-01 15:39:52 +0200940}
941
942static int mma8452_read_event_config(struct iio_dev *indio_dev,
943 const struct iio_chan_spec *chan,
944 enum iio_event_type type,
945 enum iio_event_direction dir)
946{
947 struct mma8452_data *data = iio_priv(indio_dev);
948 int ret;
Harinath Nampally605f72d2017-09-09 15:56:58 -0400949 const struct mma8452_event_regs *ev_regs;
950
951 ret = mma8452_get_event_regs(data, chan, dir, &ev_regs);
952 if (ret)
953 return ret;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200954
Martin Kepplinger4b042662016-01-16 15:35:20 +0100955 switch (dir) {
956 case IIO_EV_DIR_FALLING:
957 return mma8452_freefall_mode_enabled(data);
958 case IIO_EV_DIR_RISING:
Martin Kepplinger4b042662016-01-16 15:35:20 +0100959 ret = i2c_smbus_read_byte_data(data->client,
Harinath Nampally605f72d2017-09-09 15:56:58 -0400960 ev_regs->ev_cfg);
Martin Kepplinger4b042662016-01-16 15:35:20 +0100961 if (ret < 0)
962 return ret;
963
Martin Kepplinger8b8ff3a2016-03-03 09:24:01 +0100964 return !!(ret & BIT(chan->scan_index +
Harinath Nampally605f72d2017-09-09 15:56:58 -0400965 ev_regs->ev_cfg_chan_shift));
Martin Kepplinger4b042662016-01-16 15:35:20 +0100966 default:
967 return -EINVAL;
968 }
Martin Fuzzey28e34272015-06-01 15:39:52 +0200969}
970
971static int mma8452_write_event_config(struct iio_dev *indio_dev,
972 const struct iio_chan_spec *chan,
973 enum iio_event_type type,
974 enum iio_event_direction dir,
975 int state)
976{
977 struct mma8452_data *data = iio_priv(indio_dev);
Martin Kepplinger96c0cb22016-03-03 09:24:03 +0100978 int val, ret;
Harinath Nampally605f72d2017-09-09 15:56:58 -0400979 const struct mma8452_event_regs *ev_regs;
980
981 ret = mma8452_get_event_regs(data, chan, dir, &ev_regs);
982 if (ret)
983 return ret;
Martin Kepplinger96c0cb22016-03-03 09:24:03 +0100984
985 ret = mma8452_set_runtime_pm_state(data->client, state);
986 if (ret)
987 return ret;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200988
Martin Kepplinger4b042662016-01-16 15:35:20 +0100989 switch (dir) {
990 case IIO_EV_DIR_FALLING:
991 return mma8452_set_freefall_mode(data, state);
992 case IIO_EV_DIR_RISING:
Harinath Nampally605f72d2017-09-09 15:56:58 -0400993 val = i2c_smbus_read_byte_data(data->client, ev_regs->ev_cfg);
Martin Kepplinger4b042662016-01-16 15:35:20 +0100994 if (val < 0)
995 return val;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200996
Martin Kepplinger4b042662016-01-16 15:35:20 +0100997 if (state) {
998 if (mma8452_freefall_mode_enabled(data)) {
Harinath Nampally605f72d2017-09-09 15:56:58 -0400999 val &= ~BIT(idx_x + ev_regs->ev_cfg_chan_shift);
1000 val &= ~BIT(idx_y + ev_regs->ev_cfg_chan_shift);
1001 val &= ~BIT(idx_z + ev_regs->ev_cfg_chan_shift);
Martin Kepplinger4b042662016-01-16 15:35:20 +01001002 val |= MMA8452_FF_MT_CFG_OAE;
1003 }
Harinath Nampally605f72d2017-09-09 15:56:58 -04001004 val |= BIT(chan->scan_index +
1005 ev_regs->ev_cfg_chan_shift);
Martin Kepplinger4b042662016-01-16 15:35:20 +01001006 } else {
1007 if (mma8452_freefall_mode_enabled(data))
1008 return 0;
Martin Fuzzey28e34272015-06-01 15:39:52 +02001009
Harinath Nampally605f72d2017-09-09 15:56:58 -04001010 val &= ~BIT(chan->scan_index +
1011 ev_regs->ev_cfg_chan_shift);
Martin Kepplinger4b042662016-01-16 15:35:20 +01001012 }
Martin Fuzzey28e34272015-06-01 15:39:52 +02001013
Harinath Nampally605f72d2017-09-09 15:56:58 -04001014 val |= ev_regs->ev_cfg_ele;
Martin Kepplinger4b042662016-01-16 15:35:20 +01001015
Harinath Nampally605f72d2017-09-09 15:56:58 -04001016 return mma8452_change_config(data, ev_regs->ev_cfg, val);
Martin Kepplinger4b042662016-01-16 15:35:20 +01001017 default:
1018 return -EINVAL;
1019 }
Martin Fuzzey28e34272015-06-01 15:39:52 +02001020}
1021
1022static void mma8452_transient_interrupt(struct iio_dev *indio_dev)
1023{
1024 struct mma8452_data *data = iio_priv(indio_dev);
Gregor Boiriebc2b7da2016-03-09 19:05:49 +01001025 s64 ts = iio_get_time_ns(indio_dev);
Martin Fuzzey28e34272015-06-01 15:39:52 +02001026 int src;
1027
Harinath Nampally605f72d2017-09-09 15:56:58 -04001028 src = i2c_smbus_read_byte_data(data->client, MMA8452_TRANSIENT_SRC);
Martin Fuzzey28e34272015-06-01 15:39:52 +02001029 if (src < 0)
1030 return;
1031
Harinath Nampally605f72d2017-09-09 15:56:58 -04001032 if (src & MMA8452_TRANSIENT_SRC_XTRANSE)
Martin Fuzzey28e34272015-06-01 15:39:52 +02001033 iio_push_event(indio_dev,
1034 IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X,
Martin Kepplingerc5d0db02015-07-05 19:50:18 +02001035 IIO_EV_TYPE_MAG,
Martin Fuzzey28e34272015-06-01 15:39:52 +02001036 IIO_EV_DIR_RISING),
1037 ts);
1038
Harinath Nampally605f72d2017-09-09 15:56:58 -04001039 if (src & MMA8452_TRANSIENT_SRC_YTRANSE)
Martin Fuzzey28e34272015-06-01 15:39:52 +02001040 iio_push_event(indio_dev,
1041 IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_Y,
Martin Kepplingerc5d0db02015-07-05 19:50:18 +02001042 IIO_EV_TYPE_MAG,
Martin Fuzzey28e34272015-06-01 15:39:52 +02001043 IIO_EV_DIR_RISING),
1044 ts);
1045
Harinath Nampally605f72d2017-09-09 15:56:58 -04001046 if (src & MMA8452_TRANSIENT_SRC_ZTRANSE)
Martin Fuzzey28e34272015-06-01 15:39:52 +02001047 iio_push_event(indio_dev,
1048 IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_Z,
Martin Kepplingerc5d0db02015-07-05 19:50:18 +02001049 IIO_EV_TYPE_MAG,
Martin Fuzzey28e34272015-06-01 15:39:52 +02001050 IIO_EV_DIR_RISING),
1051 ts);
1052}
1053
1054static irqreturn_t mma8452_interrupt(int irq, void *p)
1055{
1056 struct iio_dev *indio_dev = p;
1057 struct mma8452_data *data = iio_priv(indio_dev);
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001058 int ret = IRQ_NONE;
Martin Fuzzey28e34272015-06-01 15:39:52 +02001059 int src;
1060
1061 src = i2c_smbus_read_byte_data(data->client, MMA8452_INT_SRC);
1062 if (src < 0)
1063 return IRQ_NONE;
1064
Leonard Crestezb02ec672018-06-07 21:52:50 +03001065 if (!(src & (data->chip_info->enabled_events | MMA8452_INT_DRDY)))
Harinath Nampally605f72d2017-09-09 15:56:58 -04001066 return IRQ_NONE;
1067
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001068 if (src & MMA8452_INT_DRDY) {
1069 iio_trigger_poll_chained(indio_dev->trig);
1070 ret = IRQ_HANDLED;
Martin Fuzzey28e34272015-06-01 15:39:52 +02001071 }
1072
Harinath Nampally605f72d2017-09-09 15:56:58 -04001073 if (src & MMA8452_INT_FF_MT) {
1074 if (mma8452_freefall_mode_enabled(data)) {
1075 s64 ts = iio_get_time_ns(indio_dev);
1076
1077 iio_push_event(indio_dev,
1078 IIO_MOD_EVENT_CODE(IIO_ACCEL, 0,
1079 IIO_MOD_X_AND_Y_AND_Z,
1080 IIO_EV_TYPE_MAG,
1081 IIO_EV_DIR_FALLING),
1082 ts);
1083 }
1084 ret = IRQ_HANDLED;
1085 }
1086
1087 if (src & MMA8452_INT_TRANS) {
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001088 mma8452_transient_interrupt(indio_dev);
1089 ret = IRQ_HANDLED;
1090 }
1091
1092 return ret;
Martin Fuzzey28e34272015-06-01 15:39:52 +02001093}
1094
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001095static irqreturn_t mma8452_trigger_handler(int irq, void *p)
1096{
1097 struct iio_poll_func *pf = p;
1098 struct iio_dev *indio_dev = pf->indio_dev;
1099 struct mma8452_data *data = iio_priv(indio_dev);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001100 int ret;
1101
Jonathan Cameron89226a22020-07-22 16:50:38 +01001102 ret = mma8452_read(data, data->buffer.channels);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001103 if (ret < 0)
1104 goto done;
1105
Jonathan Cameron89226a22020-07-22 16:50:38 +01001106 iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer,
Gregor Boiriebc2b7da2016-03-09 19:05:49 +01001107 iio_get_time_ns(indio_dev));
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001108
1109done:
1110 iio_trigger_notify_done(indio_dev->trig);
Hartmut Knaack686027f2015-08-02 22:43:51 +02001111
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001112 return IRQ_HANDLED;
1113}
1114
Martin Fuzzey2a17698c2015-05-13 12:26:40 +02001115static int mma8452_reg_access_dbg(struct iio_dev *indio_dev,
Harinath Nampallyf8b7b302017-09-23 16:56:30 -04001116 unsigned int reg, unsigned int writeval,
1117 unsigned int *readval)
Martin Fuzzey2a17698c2015-05-13 12:26:40 +02001118{
1119 int ret;
1120 struct mma8452_data *data = iio_priv(indio_dev);
1121
1122 if (reg > MMA8452_MAX_REG)
1123 return -EINVAL;
1124
1125 if (!readval)
1126 return mma8452_change_config(data, reg, writeval);
1127
1128 ret = i2c_smbus_read_byte_data(data->client, reg);
1129 if (ret < 0)
1130 return ret;
1131
1132 *readval = ret;
1133
1134 return 0;
1135}
1136
Martin Kepplinger4b042662016-01-16 15:35:20 +01001137static const struct iio_event_spec mma8452_freefall_event[] = {
1138 {
1139 .type = IIO_EV_TYPE_MAG,
1140 .dir = IIO_EV_DIR_FALLING,
1141 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
1142 .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
1143 BIT(IIO_EV_INFO_PERIOD) |
1144 BIT(IIO_EV_INFO_HIGH_PASS_FILTER_3DB)
1145 },
1146};
1147
1148static const struct iio_event_spec mma8652_freefall_event[] = {
1149 {
1150 .type = IIO_EV_TYPE_MAG,
1151 .dir = IIO_EV_DIR_FALLING,
1152 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
1153 .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
1154 BIT(IIO_EV_INFO_PERIOD)
1155 },
1156};
1157
Martin Fuzzey28e34272015-06-01 15:39:52 +02001158static const struct iio_event_spec mma8452_transient_event[] = {
1159 {
Martin Kepplingerc5d0db02015-07-05 19:50:18 +02001160 .type = IIO_EV_TYPE_MAG,
Martin Fuzzey28e34272015-06-01 15:39:52 +02001161 .dir = IIO_EV_DIR_RISING,
1162 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
Martin Fuzzey5dbbd192015-06-01 15:39:54 +02001163 .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
Martin Fuzzey1e798412015-06-01 15:39:56 +02001164 BIT(IIO_EV_INFO_PERIOD) |
1165 BIT(IIO_EV_INFO_HIGH_PASS_FILTER_3DB)
Martin Fuzzey28e34272015-06-01 15:39:52 +02001166 },
1167};
1168
Martin Kepplinger60f562e2015-09-01 13:45:10 +02001169static const struct iio_event_spec mma8452_motion_event[] = {
1170 {
1171 .type = IIO_EV_TYPE_MAG,
1172 .dir = IIO_EV_DIR_RISING,
1173 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
1174 .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
1175 BIT(IIO_EV_INFO_PERIOD)
1176 },
1177};
1178
Martin Fuzzey28e34272015-06-01 15:39:52 +02001179/*
1180 * Threshold is configured in fixed 8G/127 steps regardless of
1181 * currently selected scale for measurement.
1182 */
1183static IIO_CONST_ATTR_NAMED(accel_transient_scale, in_accel_scale, "0.617742");
1184
1185static struct attribute *mma8452_event_attributes[] = {
1186 &iio_const_attr_accel_transient_scale.dev_attr.attr,
1187 NULL,
1188};
1189
Rikard Falkeborn681ab2c2020-10-01 01:29:39 +02001190static const struct attribute_group mma8452_event_attribute_group = {
Martin Fuzzey28e34272015-06-01 15:39:52 +02001191 .attrs = mma8452_event_attributes,
Martin Fuzzey28e34272015-06-01 15:39:52 +02001192};
1193
Martin Kepplinger4b042662016-01-16 15:35:20 +01001194#define MMA8452_FREEFALL_CHANNEL(modifier) { \
1195 .type = IIO_ACCEL, \
1196 .modified = 1, \
1197 .channel2 = modifier, \
1198 .scan_index = -1, \
1199 .event_spec = mma8452_freefall_event, \
1200 .num_event_specs = ARRAY_SIZE(mma8452_freefall_event), \
1201}
1202
1203#define MMA8652_FREEFALL_CHANNEL(modifier) { \
1204 .type = IIO_ACCEL, \
1205 .modified = 1, \
1206 .channel2 = modifier, \
1207 .scan_index = -1, \
1208 .event_spec = mma8652_freefall_event, \
1209 .num_event_specs = ARRAY_SIZE(mma8652_freefall_event), \
1210}
1211
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001212#define MMA8452_CHANNEL(axis, idx, bits) { \
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001213 .type = IIO_ACCEL, \
1214 .modified = 1, \
1215 .channel2 = IIO_MOD_##axis, \
1216 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
Hartmut Knaack686027f2015-08-02 22:43:51 +02001217 BIT(IIO_CHAN_INFO_CALIBBIAS), \
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001218 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
Hartmut Knaack686027f2015-08-02 22:43:51 +02001219 BIT(IIO_CHAN_INFO_SCALE) | \
Martin Kepplingered859fc2016-04-25 14:08:25 +02001220 BIT(IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY) | \
1221 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001222 .scan_index = idx, \
1223 .scan_type = { \
1224 .sign = 's', \
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001225 .realbits = (bits), \
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001226 .storagebits = 16, \
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001227 .shift = 16 - (bits), \
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001228 .endianness = IIO_BE, \
1229 }, \
Martin Fuzzey28e34272015-06-01 15:39:52 +02001230 .event_spec = mma8452_transient_event, \
1231 .num_event_specs = ARRAY_SIZE(mma8452_transient_event), \
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001232}
1233
Martin Kepplinger417e0082015-09-01 13:45:11 +02001234#define MMA8652_CHANNEL(axis, idx, bits) { \
1235 .type = IIO_ACCEL, \
1236 .modified = 1, \
1237 .channel2 = IIO_MOD_##axis, \
1238 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
1239 BIT(IIO_CHAN_INFO_CALIBBIAS), \
1240 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
Martin Kepplingered859fc2016-04-25 14:08:25 +02001241 BIT(IIO_CHAN_INFO_SCALE) | \
1242 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
Martin Kepplinger417e0082015-09-01 13:45:11 +02001243 .scan_index = idx, \
1244 .scan_type = { \
1245 .sign = 's', \
1246 .realbits = (bits), \
1247 .storagebits = 16, \
1248 .shift = 16 - (bits), \
1249 .endianness = IIO_BE, \
1250 }, \
1251 .event_spec = mma8452_motion_event, \
1252 .num_event_specs = ARRAY_SIZE(mma8452_motion_event), \
1253}
1254
Martin Kepplinger244a93f2016-01-16 15:35:22 +01001255static const struct iio_chan_spec mma8451_channels[] = {
1256 MMA8452_CHANNEL(X, idx_x, 14),
1257 MMA8452_CHANNEL(Y, idx_y, 14),
1258 MMA8452_CHANNEL(Z, idx_z, 14),
1259 IIO_CHAN_SOFT_TIMESTAMP(idx_ts),
1260 MMA8452_FREEFALL_CHANNEL(IIO_MOD_X_AND_Y_AND_Z),
1261};
1262
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001263static const struct iio_chan_spec mma8452_channels[] = {
Martin Kepplingere60378c2015-12-15 17:45:00 +01001264 MMA8452_CHANNEL(X, idx_x, 12),
1265 MMA8452_CHANNEL(Y, idx_y, 12),
1266 MMA8452_CHANNEL(Z, idx_z, 12),
1267 IIO_CHAN_SOFT_TIMESTAMP(idx_ts),
Martin Kepplinger4b042662016-01-16 15:35:20 +01001268 MMA8452_FREEFALL_CHANNEL(IIO_MOD_X_AND_Y_AND_Z),
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001269};
1270
Martin Kepplingerc5ea1b582015-09-01 13:45:09 +02001271static const struct iio_chan_spec mma8453_channels[] = {
Martin Kepplingere60378c2015-12-15 17:45:00 +01001272 MMA8452_CHANNEL(X, idx_x, 10),
1273 MMA8452_CHANNEL(Y, idx_y, 10),
1274 MMA8452_CHANNEL(Z, idx_z, 10),
1275 IIO_CHAN_SOFT_TIMESTAMP(idx_ts),
Martin Kepplinger4b042662016-01-16 15:35:20 +01001276 MMA8452_FREEFALL_CHANNEL(IIO_MOD_X_AND_Y_AND_Z),
Martin Kepplingerc5ea1b582015-09-01 13:45:09 +02001277};
1278
Martin Kepplinger417e0082015-09-01 13:45:11 +02001279static const struct iio_chan_spec mma8652_channels[] = {
Martin Kepplingere60378c2015-12-15 17:45:00 +01001280 MMA8652_CHANNEL(X, idx_x, 12),
1281 MMA8652_CHANNEL(Y, idx_y, 12),
1282 MMA8652_CHANNEL(Z, idx_z, 12),
1283 IIO_CHAN_SOFT_TIMESTAMP(idx_ts),
Martin Kepplinger4b042662016-01-16 15:35:20 +01001284 MMA8652_FREEFALL_CHANNEL(IIO_MOD_X_AND_Y_AND_Z),
Martin Kepplinger417e0082015-09-01 13:45:11 +02001285};
1286
1287static const struct iio_chan_spec mma8653_channels[] = {
Martin Kepplingere60378c2015-12-15 17:45:00 +01001288 MMA8652_CHANNEL(X, idx_x, 10),
1289 MMA8652_CHANNEL(Y, idx_y, 10),
1290 MMA8652_CHANNEL(Z, idx_z, 10),
1291 IIO_CHAN_SOFT_TIMESTAMP(idx_ts),
Martin Kepplinger4b042662016-01-16 15:35:20 +01001292 MMA8652_FREEFALL_CHANNEL(IIO_MOD_X_AND_Y_AND_Z),
Martin Kepplinger417e0082015-09-01 13:45:11 +02001293};
1294
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001295enum {
Martin Kepplinger244a93f2016-01-16 15:35:22 +01001296 mma8451,
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001297 mma8452,
Martin Kepplingerc5ea1b582015-09-01 13:45:09 +02001298 mma8453,
Martin Kepplinger417e0082015-09-01 13:45:11 +02001299 mma8652,
1300 mma8653,
Martin Kepplingere8731182016-03-09 12:01:29 +01001301 fxls8471,
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001302};
1303
1304static const struct mma_chip_info mma_chip_info_table[] = {
Martin Kepplinger244a93f2016-01-16 15:35:22 +01001305 [mma8451] = {
1306 .chip_id = MMA8451_DEVICE_ID,
1307 .channels = mma8451_channels,
1308 .num_channels = ARRAY_SIZE(mma8451_channels),
1309 /*
1310 * Hardware has fullscale of -2G, -4G, -8G corresponding to
1311 * raw value -8192 for 14 bit, -2048 for 12 bit or -512 for 10
1312 * bit.
1313 * The userspace interface uses m/s^2 and we declare micro units
1314 * So scale factor for 12 bit here is given by:
Martin Kepplinger8b8ff3a2016-03-03 09:24:01 +01001315 * g * N * 1000000 / 2048 for N = 2, 4, 8 and g=9.80665
Martin Kepplinger244a93f2016-01-16 15:35:22 +01001316 */
1317 .mma_scales = { {0, 2394}, {0, 4788}, {0, 9577} },
Harinath Nampally605f72d2017-09-09 15:56:58 -04001318 /*
1319 * Although we enable the interrupt sources once and for
1320 * all here the event detection itself is not enabled until
1321 * userspace asks for it by mma8452_write_event_config()
1322 */
1323 .all_events = MMA8452_INT_DRDY |
1324 MMA8452_INT_TRANS |
1325 MMA8452_INT_FF_MT,
1326 .enabled_events = MMA8452_INT_TRANS |
1327 MMA8452_INT_FF_MT,
Martin Kepplinger244a93f2016-01-16 15:35:22 +01001328 },
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001329 [mma8452] = {
1330 .chip_id = MMA8452_DEVICE_ID,
1331 .channels = mma8452_channels,
1332 .num_channels = ARRAY_SIZE(mma8452_channels),
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001333 .mma_scales = { {0, 9577}, {0, 19154}, {0, 38307} },
Harinath Nampally605f72d2017-09-09 15:56:58 -04001334 /*
1335 * Although we enable the interrupt sources once and for
1336 * all here the event detection itself is not enabled until
1337 * userspace asks for it by mma8452_write_event_config()
1338 */
1339 .all_events = MMA8452_INT_DRDY |
1340 MMA8452_INT_TRANS |
1341 MMA8452_INT_FF_MT,
1342 .enabled_events = MMA8452_INT_TRANS |
1343 MMA8452_INT_FF_MT,
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001344 },
Martin Kepplingerc5ea1b582015-09-01 13:45:09 +02001345 [mma8453] = {
1346 .chip_id = MMA8453_DEVICE_ID,
1347 .channels = mma8453_channels,
1348 .num_channels = ARRAY_SIZE(mma8453_channels),
1349 .mma_scales = { {0, 38307}, {0, 76614}, {0, 153228} },
Harinath Nampally605f72d2017-09-09 15:56:58 -04001350 /*
1351 * Although we enable the interrupt sources once and for
1352 * all here the event detection itself is not enabled until
1353 * userspace asks for it by mma8452_write_event_config()
1354 */
1355 .all_events = MMA8452_INT_DRDY |
1356 MMA8452_INT_TRANS |
1357 MMA8452_INT_FF_MT,
1358 .enabled_events = MMA8452_INT_TRANS |
1359 MMA8452_INT_FF_MT,
Martin Kepplingerc5ea1b582015-09-01 13:45:09 +02001360 },
Martin Kepplinger417e0082015-09-01 13:45:11 +02001361 [mma8652] = {
1362 .chip_id = MMA8652_DEVICE_ID,
1363 .channels = mma8652_channels,
1364 .num_channels = ARRAY_SIZE(mma8652_channels),
1365 .mma_scales = { {0, 9577}, {0, 19154}, {0, 38307} },
Harinath Nampally605f72d2017-09-09 15:56:58 -04001366 .all_events = MMA8452_INT_DRDY |
1367 MMA8452_INT_FF_MT,
1368 .enabled_events = MMA8452_INT_FF_MT,
Martin Kepplinger417e0082015-09-01 13:45:11 +02001369 },
1370 [mma8653] = {
1371 .chip_id = MMA8653_DEVICE_ID,
1372 .channels = mma8653_channels,
1373 .num_channels = ARRAY_SIZE(mma8653_channels),
1374 .mma_scales = { {0, 38307}, {0, 76614}, {0, 153228} },
Harinath Nampally605f72d2017-09-09 15:56:58 -04001375 /*
1376 * Although we enable the interrupt sources once and for
1377 * all here the event detection itself is not enabled until
1378 * userspace asks for it by mma8452_write_event_config()
1379 */
1380 .all_events = MMA8452_INT_DRDY |
1381 MMA8452_INT_FF_MT,
1382 .enabled_events = MMA8452_INT_FF_MT,
Martin Kepplinger417e0082015-09-01 13:45:11 +02001383 },
Martin Kepplingere8731182016-03-09 12:01:29 +01001384 [fxls8471] = {
1385 .chip_id = FXLS8471_DEVICE_ID,
1386 .channels = mma8451_channels,
1387 .num_channels = ARRAY_SIZE(mma8451_channels),
1388 .mma_scales = { {0, 2394}, {0, 4788}, {0, 9577} },
Harinath Nampally605f72d2017-09-09 15:56:58 -04001389 /*
1390 * Although we enable the interrupt sources once and for
1391 * all here the event detection itself is not enabled until
1392 * userspace asks for it by mma8452_write_event_config()
1393 */
1394 .all_events = MMA8452_INT_DRDY |
1395 MMA8452_INT_TRANS |
1396 MMA8452_INT_FF_MT,
1397 .enabled_events = MMA8452_INT_TRANS |
1398 MMA8452_INT_FF_MT,
Martin Kepplingere8731182016-03-09 12:01:29 +01001399 },
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001400};
1401
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001402static struct attribute *mma8452_attributes[] = {
1403 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
1404 &iio_dev_attr_in_accel_scale_available.dev_attr.attr,
Martin Fuzzey1e798412015-06-01 15:39:56 +02001405 &iio_dev_attr_in_accel_filter_high_pass_3db_frequency_available.dev_attr.attr,
Martin Kepplingered859fc2016-04-25 14:08:25 +02001406 &iio_dev_attr_in_accel_oversampling_ratio_available.dev_attr.attr,
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001407 NULL
1408};
1409
1410static const struct attribute_group mma8452_group = {
1411 .attrs = mma8452_attributes,
1412};
1413
1414static const struct iio_info mma8452_info = {
1415 .attrs = &mma8452_group,
1416 .read_raw = &mma8452_read_raw,
1417 .write_raw = &mma8452_write_raw,
Martin Fuzzey28e34272015-06-01 15:39:52 +02001418 .event_attrs = &mma8452_event_attribute_group,
Harinath Nampally4febd9f2017-09-25 06:40:08 -04001419 .read_event_value = &mma8452_read_event_value,
1420 .write_event_value = &mma8452_write_event_value,
Martin Fuzzey28e34272015-06-01 15:39:52 +02001421 .read_event_config = &mma8452_read_event_config,
1422 .write_event_config = &mma8452_write_event_config,
Martin Fuzzey2a17698c2015-05-13 12:26:40 +02001423 .debugfs_reg_access = &mma8452_reg_access_dbg,
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001424};
1425
1426static const unsigned long mma8452_scan_masks[] = {0x7, 0};
1427
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001428static int mma8452_data_rdy_trigger_set_state(struct iio_trigger *trig,
1429 bool state)
1430{
1431 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1432 struct mma8452_data *data = iio_priv(indio_dev);
Martin Kepplinger96c0cb22016-03-03 09:24:03 +01001433 int reg, ret;
1434
1435 ret = mma8452_set_runtime_pm_state(data->client, state);
1436 if (ret)
1437 return ret;
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001438
1439 reg = i2c_smbus_read_byte_data(data->client, MMA8452_CTRL_REG4);
1440 if (reg < 0)
1441 return reg;
1442
1443 if (state)
1444 reg |= MMA8452_INT_DRDY;
1445 else
1446 reg &= ~MMA8452_INT_DRDY;
1447
1448 return mma8452_change_config(data, MMA8452_CTRL_REG4, reg);
1449}
1450
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001451static const struct iio_trigger_ops mma8452_trigger_ops = {
1452 .set_trigger_state = mma8452_data_rdy_trigger_set_state,
Lars-Peter Clausen19808e02016-09-23 17:19:42 +02001453 .validate_device = iio_trigger_validate_own_device,
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001454};
1455
1456static int mma8452_trigger_setup(struct iio_dev *indio_dev)
1457{
1458 struct mma8452_data *data = iio_priv(indio_dev);
1459 struct iio_trigger *trig;
1460 int ret;
1461
1462 trig = devm_iio_trigger_alloc(&data->client->dev, "%s-dev%d",
1463 indio_dev->name,
1464 indio_dev->id);
1465 if (!trig)
1466 return -ENOMEM;
1467
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001468 trig->ops = &mma8452_trigger_ops;
1469 iio_trigger_set_drvdata(trig, indio_dev);
1470
1471 ret = iio_trigger_register(trig);
1472 if (ret)
1473 return ret;
1474
1475 indio_dev->trig = trig;
Hartmut Knaack686027f2015-08-02 22:43:51 +02001476
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001477 return 0;
1478}
1479
1480static void mma8452_trigger_cleanup(struct iio_dev *indio_dev)
1481{
1482 if (indio_dev->trig)
1483 iio_trigger_unregister(indio_dev->trig);
1484}
1485
Martin Fuzzeyecabae72015-05-13 12:26:38 +02001486static int mma8452_reset(struct i2c_client *client)
1487{
1488 int i;
1489 int ret;
1490
1491 ret = i2c_smbus_write_byte_data(client, MMA8452_CTRL_REG2,
1492 MMA8452_CTRL_REG2_RST);
1493 if (ret < 0)
1494 return ret;
1495
1496 for (i = 0; i < 10; i++) {
1497 usleep_range(100, 200);
1498 ret = i2c_smbus_read_byte_data(client, MMA8452_CTRL_REG2);
1499 if (ret == -EIO)
1500 continue; /* I2C comm reset */
1501 if (ret < 0)
1502 return ret;
1503 if (!(ret & MMA8452_CTRL_REG2_RST))
1504 return 0;
1505 }
1506
1507 return -ETIMEDOUT;
1508}
1509
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001510static const struct of_device_id mma8452_dt_ids[] = {
Martin Kepplinger244a93f2016-01-16 15:35:22 +01001511 { .compatible = "fsl,mma8451", .data = &mma_chip_info_table[mma8451] },
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001512 { .compatible = "fsl,mma8452", .data = &mma_chip_info_table[mma8452] },
Martin Kepplingerc5ea1b582015-09-01 13:45:09 +02001513 { .compatible = "fsl,mma8453", .data = &mma_chip_info_table[mma8453] },
Martin Kepplinger417e0082015-09-01 13:45:11 +02001514 { .compatible = "fsl,mma8652", .data = &mma_chip_info_table[mma8652] },
1515 { .compatible = "fsl,mma8653", .data = &mma_chip_info_table[mma8653] },
Martin Kepplingere8731182016-03-09 12:01:29 +01001516 { .compatible = "fsl,fxls8471", .data = &mma_chip_info_table[fxls8471] },
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001517 { }
1518};
1519MODULE_DEVICE_TABLE(of, mma8452_dt_ids);
1520
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001521static int mma8452_probe(struct i2c_client *client,
1522 const struct i2c_device_id *id)
1523{
1524 struct mma8452_data *data;
1525 struct iio_dev *indio_dev;
1526 int ret;
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001527 const struct of_device_id *match;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001528
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001529 match = of_match_device(mma8452_dt_ids, &client->dev);
1530 if (!match) {
1531 dev_err(&client->dev, "unknown device model\n");
1532 return -ENODEV;
1533 }
1534
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001535 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
1536 if (!indio_dev)
1537 return -ENOMEM;
1538
1539 data = iio_priv(indio_dev);
1540 data->client = client;
1541 mutex_init(&data->lock);
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001542 data->chip_info = match->data;
1543
Anson Huangf6ff49b2019-01-08 09:14:06 +00001544 data->vdd_reg = devm_regulator_get(&client->dev, "vdd");
Krzysztof Kozlowskic8b9a022020-08-29 08:47:10 +02001545 if (IS_ERR(data->vdd_reg))
1546 return dev_err_probe(&client->dev, PTR_ERR(data->vdd_reg),
1547 "failed to get VDD regulator!\n");
Anson Huangf6ff49b2019-01-08 09:14:06 +00001548
1549 data->vddio_reg = devm_regulator_get(&client->dev, "vddio");
Krzysztof Kozlowskic8b9a022020-08-29 08:47:10 +02001550 if (IS_ERR(data->vddio_reg))
1551 return dev_err_probe(&client->dev, PTR_ERR(data->vddio_reg),
1552 "failed to get VDDIO regulator!\n");
Anson Huangf6ff49b2019-01-08 09:14:06 +00001553
1554 ret = regulator_enable(data->vdd_reg);
1555 if (ret) {
1556 dev_err(&client->dev, "failed to enable VDD regulator!\n");
1557 return ret;
1558 }
1559
1560 ret = regulator_enable(data->vddio_reg);
1561 if (ret) {
1562 dev_err(&client->dev, "failed to enable VDDIO regulator!\n");
1563 goto disable_regulator_vdd;
1564 }
1565
Martin Kepplinger417e0082015-09-01 13:45:11 +02001566 ret = i2c_smbus_read_byte_data(client, MMA8452_WHO_AM_I);
1567 if (ret < 0)
Anson Huangf6ff49b2019-01-08 09:14:06 +00001568 goto disable_regulators;
Martin Kepplinger417e0082015-09-01 13:45:11 +02001569
1570 switch (ret) {
Martin Kepplinger244a93f2016-01-16 15:35:22 +01001571 case MMA8451_DEVICE_ID:
Martin Kepplinger417e0082015-09-01 13:45:11 +02001572 case MMA8452_DEVICE_ID:
1573 case MMA8453_DEVICE_ID:
1574 case MMA8652_DEVICE_ID:
1575 case MMA8653_DEVICE_ID:
Martin Kepplingere8731182016-03-09 12:01:29 +01001576 case FXLS8471_DEVICE_ID:
Martin Kepplinger417e0082015-09-01 13:45:11 +02001577 if (ret == data->chip_info->chip_id)
1578 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001579 fallthrough;
Martin Kepplinger417e0082015-09-01 13:45:11 +02001580 default:
Anson Huangf6ff49b2019-01-08 09:14:06 +00001581 ret = -ENODEV;
1582 goto disable_regulators;
Martin Kepplinger417e0082015-09-01 13:45:11 +02001583 }
1584
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001585 dev_info(&client->dev, "registering %s accelerometer; ID 0x%x\n",
1586 match->compatible, data->chip_info->chip_id);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001587
1588 i2c_set_clientdata(client, indio_dev);
1589 indio_dev->info = &mma8452_info;
1590 indio_dev->name = id->name;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001591 indio_dev->modes = INDIO_DIRECT_MODE;
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001592 indio_dev->channels = data->chip_info->channels;
1593 indio_dev->num_channels = data->chip_info->num_channels;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001594 indio_dev->available_scan_masks = mma8452_scan_masks;
1595
Martin Fuzzeyecabae72015-05-13 12:26:38 +02001596 ret = mma8452_reset(client);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001597 if (ret < 0)
Anson Huangf6ff49b2019-01-08 09:14:06 +00001598 goto disable_regulators;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001599
1600 data->data_cfg = MMA8452_DATA_CFG_FS_2G;
1601 ret = i2c_smbus_write_byte_data(client, MMA8452_DATA_CFG,
Hartmut Knaack686027f2015-08-02 22:43:51 +02001602 data->data_cfg);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001603 if (ret < 0)
Anson Huangf6ff49b2019-01-08 09:14:06 +00001604 goto disable_regulators;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001605
Martin Fuzzey28e34272015-06-01 15:39:52 +02001606 /*
1607 * By default set transient threshold to max to avoid events if
1608 * enabling without configuring threshold.
1609 */
1610 ret = i2c_smbus_write_byte_data(client, MMA8452_TRANSIENT_THS,
1611 MMA8452_TRANSIENT_THS_MASK);
1612 if (ret < 0)
Anson Huangf6ff49b2019-01-08 09:14:06 +00001613 goto disable_regulators;
Martin Fuzzey28e34272015-06-01 15:39:52 +02001614
1615 if (client->irq) {
Martin Kepplingerd2a3e092015-10-15 15:10:32 +02001616 int irq2;
Martin Fuzzey28e34272015-06-01 15:39:52 +02001617
Martin Kepplingerd2a3e092015-10-15 15:10:32 +02001618 irq2 = of_irq_get_byname(client->dev.of_node, "INT2");
1619
1620 if (irq2 == client->irq) {
1621 dev_dbg(&client->dev, "using interrupt line INT2\n");
1622 } else {
1623 ret = i2c_smbus_write_byte_data(client,
Harinath Nampally605f72d2017-09-09 15:56:58 -04001624 MMA8452_CTRL_REG5,
1625 data->chip_info->all_events);
Martin Kepplingerd2a3e092015-10-15 15:10:32 +02001626 if (ret < 0)
Anson Huangf6ff49b2019-01-08 09:14:06 +00001627 goto disable_regulators;
Martin Kepplingerd2a3e092015-10-15 15:10:32 +02001628
1629 dev_dbg(&client->dev, "using interrupt line INT1\n");
1630 }
Martin Fuzzey28e34272015-06-01 15:39:52 +02001631
1632 ret = i2c_smbus_write_byte_data(client,
Harinath Nampally605f72d2017-09-09 15:56:58 -04001633 MMA8452_CTRL_REG4,
1634 data->chip_info->enabled_events);
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001635 if (ret < 0)
Anson Huangf6ff49b2019-01-08 09:14:06 +00001636 goto disable_regulators;
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001637
1638 ret = mma8452_trigger_setup(indio_dev);
Martin Fuzzey28e34272015-06-01 15:39:52 +02001639 if (ret < 0)
Anson Huangf6ff49b2019-01-08 09:14:06 +00001640 goto disable_regulators;
Martin Fuzzey28e34272015-06-01 15:39:52 +02001641 }
1642
Martin Fuzzeyecabae72015-05-13 12:26:38 +02001643 data->ctrl_reg1 = MMA8452_CTRL_ACTIVE |
Hartmut Knaack686027f2015-08-02 22:43:51 +02001644 (MMA8452_CTRL_DR_DEFAULT << MMA8452_CTRL_DR_SHIFT);
Richard Tresiddera45d1232018-05-11 16:54:59 +08001645
1646 data->sleep_val = mma8452_calculate_sleep(data);
1647
Martin Fuzzeyecabae72015-05-13 12:26:38 +02001648 ret = i2c_smbus_write_byte_data(client, MMA8452_CTRL_REG1,
1649 data->ctrl_reg1);
1650 if (ret < 0)
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001651 goto trigger_cleanup;
Martin Fuzzeyecabae72015-05-13 12:26:38 +02001652
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001653 ret = iio_triggered_buffer_setup(indio_dev, NULL,
Hartmut Knaack686027f2015-08-02 22:43:51 +02001654 mma8452_trigger_handler, NULL);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001655 if (ret < 0)
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001656 goto trigger_cleanup;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001657
Martin Fuzzey28e34272015-06-01 15:39:52 +02001658 if (client->irq) {
1659 ret = devm_request_threaded_irq(&client->dev,
1660 client->irq,
1661 NULL, mma8452_interrupt,
1662 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1663 client->name, indio_dev);
1664 if (ret)
1665 goto buffer_cleanup;
1666 }
1667
Martin Kepplinger96c0cb22016-03-03 09:24:03 +01001668 ret = pm_runtime_set_active(&client->dev);
1669 if (ret < 0)
1670 goto buffer_cleanup;
1671
1672 pm_runtime_enable(&client->dev);
1673 pm_runtime_set_autosuspend_delay(&client->dev,
1674 MMA8452_AUTO_SUSPEND_DELAY_MS);
1675 pm_runtime_use_autosuspend(&client->dev);
1676
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001677 ret = iio_device_register(indio_dev);
1678 if (ret < 0)
1679 goto buffer_cleanup;
Martin Fuzzey28e34272015-06-01 15:39:52 +02001680
Martin Kepplinger4b042662016-01-16 15:35:20 +01001681 ret = mma8452_set_freefall_mode(data, false);
Bijosh Thykkoottathil1a965d42016-07-04 10:08:53 +00001682 if (ret < 0)
Chuhong Yuand7369ae2020-05-28 14:41:21 +08001683 goto unregister_device;
Martin Kepplinger4b042662016-01-16 15:35:20 +01001684
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001685 return 0;
1686
Chuhong Yuand7369ae2020-05-28 14:41:21 +08001687unregister_device:
1688 iio_device_unregister(indio_dev);
1689
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001690buffer_cleanup:
1691 iio_triggered_buffer_cleanup(indio_dev);
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001692
1693trigger_cleanup:
1694 mma8452_trigger_cleanup(indio_dev);
1695
Anson Huangf6ff49b2019-01-08 09:14:06 +00001696disable_regulators:
1697 regulator_disable(data->vddio_reg);
1698
1699disable_regulator_vdd:
1700 regulator_disable(data->vdd_reg);
1701
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001702 return ret;
1703}
1704
1705static int mma8452_remove(struct i2c_client *client)
1706{
1707 struct iio_dev *indio_dev = i2c_get_clientdata(client);
Anson Huangf6ff49b2019-01-08 09:14:06 +00001708 struct mma8452_data *data = iio_priv(indio_dev);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001709
1710 iio_device_unregister(indio_dev);
Martin Kepplinger96c0cb22016-03-03 09:24:03 +01001711
1712 pm_runtime_disable(&client->dev);
1713 pm_runtime_set_suspended(&client->dev);
1714 pm_runtime_put_noidle(&client->dev);
1715
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001716 iio_triggered_buffer_cleanup(indio_dev);
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001717 mma8452_trigger_cleanup(indio_dev);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001718 mma8452_standby(iio_priv(indio_dev));
1719
Anson Huangf6ff49b2019-01-08 09:14:06 +00001720 regulator_disable(data->vddio_reg);
1721 regulator_disable(data->vdd_reg);
1722
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001723 return 0;
1724}
1725
Martin Kepplinger96c0cb22016-03-03 09:24:03 +01001726#ifdef CONFIG_PM
1727static int mma8452_runtime_suspend(struct device *dev)
1728{
1729 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1730 struct mma8452_data *data = iio_priv(indio_dev);
1731 int ret;
1732
1733 mutex_lock(&data->lock);
1734 ret = mma8452_standby(data);
1735 mutex_unlock(&data->lock);
1736 if (ret < 0) {
1737 dev_err(&data->client->dev, "powering off device failed\n");
1738 return -EAGAIN;
1739 }
1740
Anson Huangf6ff49b2019-01-08 09:14:06 +00001741 ret = regulator_disable(data->vddio_reg);
1742 if (ret) {
1743 dev_err(dev, "failed to disable VDDIO regulator\n");
1744 return ret;
1745 }
1746
1747 ret = regulator_disable(data->vdd_reg);
1748 if (ret) {
1749 dev_err(dev, "failed to disable VDD regulator\n");
1750 return ret;
1751 }
1752
Martin Kepplinger96c0cb22016-03-03 09:24:03 +01001753 return 0;
1754}
1755
1756static int mma8452_runtime_resume(struct device *dev)
1757{
1758 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1759 struct mma8452_data *data = iio_priv(indio_dev);
1760 int ret, sleep_val;
1761
Anson Huangf6ff49b2019-01-08 09:14:06 +00001762 ret = regulator_enable(data->vdd_reg);
1763 if (ret) {
1764 dev_err(dev, "failed to enable VDD regulator\n");
1765 return ret;
1766 }
1767
1768 ret = regulator_enable(data->vddio_reg);
1769 if (ret) {
1770 dev_err(dev, "failed to enable VDDIO regulator\n");
1771 regulator_disable(data->vdd_reg);
1772 return ret;
1773 }
1774
Martin Kepplinger96c0cb22016-03-03 09:24:03 +01001775 ret = mma8452_active(data);
1776 if (ret < 0)
Anson Huangf6ff49b2019-01-08 09:14:06 +00001777 goto runtime_resume_failed;
Martin Kepplinger96c0cb22016-03-03 09:24:03 +01001778
1779 ret = mma8452_get_odr_index(data);
1780 sleep_val = 1000 / mma8452_samp_freq[ret][0];
1781 if (sleep_val < 20)
1782 usleep_range(sleep_val * 1000, 20000);
1783 else
1784 msleep_interruptible(sleep_val);
1785
1786 return 0;
Martin Kepplinger96c0cb22016-03-03 09:24:03 +01001787
Anson Huangf6ff49b2019-01-08 09:14:06 +00001788runtime_resume_failed:
1789 regulator_disable(data->vddio_reg);
1790 regulator_disable(data->vdd_reg);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001791
Anson Huangf6ff49b2019-01-08 09:14:06 +00001792 return ret;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001793}
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001794#endif
1795
Martin Kepplinger96c0cb22016-03-03 09:24:03 +01001796static const struct dev_pm_ops mma8452_pm_ops = {
Anson Huangf6ff49b2019-01-08 09:14:06 +00001797 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)
Martin Kepplinger96c0cb22016-03-03 09:24:03 +01001798 SET_RUNTIME_PM_OPS(mma8452_runtime_suspend,
1799 mma8452_runtime_resume, NULL)
1800};
1801
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001802static const struct i2c_device_id mma8452_id[] = {
Martin Kepplingerddb851a2016-03-14 12:33:14 +01001803 { "mma8451", mma8451 },
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001804 { "mma8452", mma8452 },
Martin Kepplingerc5ea1b582015-09-01 13:45:09 +02001805 { "mma8453", mma8453 },
Martin Kepplinger417e0082015-09-01 13:45:11 +02001806 { "mma8652", mma8652 },
1807 { "mma8653", mma8653 },
Martin Kepplingere8731182016-03-09 12:01:29 +01001808 { "fxls8471", fxls8471 },
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001809 { }
1810};
1811MODULE_DEVICE_TABLE(i2c, mma8452_id);
1812
1813static struct i2c_driver mma8452_driver = {
1814 .driver = {
1815 .name = "mma8452",
Martin Fuzzeya3fb96a2014-11-07 14:06:00 +00001816 .of_match_table = of_match_ptr(mma8452_dt_ids),
Martin Kepplinger96c0cb22016-03-03 09:24:03 +01001817 .pm = &mma8452_pm_ops,
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001818 },
1819 .probe = mma8452_probe,
1820 .remove = mma8452_remove,
1821 .id_table = mma8452_id,
1822};
1823module_i2c_driver(mma8452_driver);
1824
1825MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
Martin Kepplingerf26ab1a2016-06-03 14:51:52 +02001826MODULE_DESCRIPTION("Freescale / NXP MMA8452 accelerometer driver");
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001827MODULE_LICENSE("GPL");