blob: 64b82b4503adab5912d799784557ae5c939c269b [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) {
Jonathan Cameron5937b862021-05-09 12:33:31 +0100224 ret = pm_runtime_resume_and_get(&client->dev);
Martin Kepplinger96c0cb22016-03-03 09:24:03 +0100225 } 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);
Martin Kepplinger96c0cb22016-03-03 09:24:03 +0100233
234 return ret;
235 }
236#endif
237
238 return 0;
239}
240
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000241static int mma8452_read(struct mma8452_data *data, __be16 buf[3])
242{
243 int ret = mma8452_drdy(data);
Hartmut Knaack686027f2015-08-02 22:43:51 +0200244
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000245 if (ret < 0)
246 return ret;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200247
Martin Kepplinger96c0cb22016-03-03 09:24:03 +0100248 ret = mma8452_set_runtime_pm_state(data->client, true);
249 if (ret)
250 return ret;
251
252 ret = i2c_smbus_read_i2c_block_data(data->client, MMA8452_OUT_X,
253 3 * sizeof(__be16), (u8 *)buf);
254
255 ret = mma8452_set_runtime_pm_state(data->client, false);
256
257 return ret;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000258}
259
Hartmut Knaack686027f2015-08-02 22:43:51 +0200260static ssize_t mma8452_show_int_plus_micros(char *buf, const int (*vals)[2],
261 int n)
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000262{
263 size_t len = 0;
264
265 while (n-- > 0)
Hartmut Knaack686027f2015-08-02 22:43:51 +0200266 len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06d ",
267 vals[n][0], vals[n][1]);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000268
269 /* replace trailing space by newline */
270 buf[len - 1] = '\n';
271
272 return len;
273}
274
275static int mma8452_get_int_plus_micros_index(const int (*vals)[2], int n,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200276 int val, int val2)
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000277{
278 while (n-- > 0)
279 if (val == vals[n][0] && val2 == vals[n][1])
280 return n;
281
282 return -EINVAL;
283}
284
Martin Kepplinger32b28072016-11-21 20:53:54 +0100285static unsigned int mma8452_get_odr_index(struct mma8452_data *data)
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200286{
287 return (data->ctrl_reg1 & MMA8452_CTRL_DR_MASK) >>
288 MMA8452_CTRL_DR_SHIFT;
289}
290
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000291static const int mma8452_samp_freq[8][2] = {
292 {800, 0}, {400, 0}, {200, 0}, {100, 0}, {50, 0}, {12, 500000},
293 {6, 250000}, {1, 560000}
294};
295
Martin Kepplingered859fc2016-04-25 14:08:25 +0200296/* Datasheet table: step time "Relationship with the ODR" (sample frequency) */
Harinath Nampallycc54a662017-11-05 13:00:02 -0500297static const unsigned int mma8452_time_step_us[4][8] = {
Martin Kepplingered859fc2016-04-25 14:08:25 +0200298 { 1250, 2500, 5000, 10000, 20000, 20000, 20000, 20000 }, /* normal */
299 { 1250, 2500, 5000, 10000, 20000, 80000, 80000, 80000 }, /* l p l n */
300 { 1250, 2500, 2500, 2500, 2500, 2500, 2500, 2500 }, /* high res*/
301 { 1250, 2500, 5000, 10000, 20000, 80000, 160000, 160000 } /* l p */
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200302};
303
Martin Kepplingered859fc2016-04-25 14:08:25 +0200304/* Datasheet table "High-Pass Filter Cutoff Options" */
305static const int mma8452_hp_filter_cutoff[4][8][4][2] = {
306 { /* normal */
Martin Fuzzey1e798412015-06-01 15:39:56 +0200307 { {16, 0}, {8, 0}, {4, 0}, {2, 0} }, /* 800 Hz sample */
308 { {16, 0}, {8, 0}, {4, 0}, {2, 0} }, /* 400 Hz sample */
309 { {8, 0}, {4, 0}, {2, 0}, {1, 0} }, /* 200 Hz sample */
310 { {4, 0}, {2, 0}, {1, 0}, {0, 500000} }, /* 100 Hz sample */
311 { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} }, /* 50 Hz sample */
312 { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} }, /* 12.5 Hz sample */
313 { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} }, /* 6.25 Hz sample */
314 { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} } /* 1.56 Hz sample */
Martin Kepplingered859fc2016-04-25 14:08:25 +0200315 },
316 { /* low noise low power */
317 { {16, 0}, {8, 0}, {4, 0}, {2, 0} },
318 { {16, 0}, {8, 0}, {4, 0}, {2, 0} },
319 { {8, 0}, {4, 0}, {2, 0}, {1, 0} },
320 { {4, 0}, {2, 0}, {1, 0}, {0, 500000} },
321 { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} },
322 { {0, 500000}, {0, 250000}, {0, 125000}, {0, 063000} },
323 { {0, 500000}, {0, 250000}, {0, 125000}, {0, 063000} },
324 { {0, 500000}, {0, 250000}, {0, 125000}, {0, 063000} }
325 },
326 { /* high resolution */
327 { {16, 0}, {8, 0}, {4, 0}, {2, 0} },
328 { {16, 0}, {8, 0}, {4, 0}, {2, 0} },
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 },
336 { /* low power */
337 { {16, 0}, {8, 0}, {4, 0}, {2, 0} },
338 { {8, 0}, {4, 0}, {2, 0}, {1, 0} },
339 { {4, 0}, {2, 0}, {1, 0}, {0, 500000} },
340 { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} },
341 { {1, 0}, {0, 500000}, {0, 250000}, {0, 125000} },
342 { {0, 250000}, {0, 125000}, {0, 063000}, {0, 031000} },
343 { {0, 250000}, {0, 125000}, {0, 063000}, {0, 031000} },
344 { {0, 250000}, {0, 125000}, {0, 063000}, {0, 031000} }
345 }
Martin Fuzzey1e798412015-06-01 15:39:56 +0200346};
347
Martin Kepplingered859fc2016-04-25 14:08:25 +0200348/* Datasheet table "MODS Oversampling modes averaging values at each ODR" */
349static const u16 mma8452_os_ratio[4][8] = {
350 /* 800 Hz, 400 Hz, ... , 1.56 Hz */
351 { 2, 4, 4, 4, 4, 16, 32, 128 }, /* normal */
352 { 2, 4, 4, 4, 4, 4, 8, 32 }, /* low power low noise */
353 { 2, 4, 8, 16, 32, 128, 256, 1024 }, /* high resolution */
354 { 2, 2, 2, 2, 2, 2, 4, 16 } /* low power */
355};
356
357static int mma8452_get_power_mode(struct mma8452_data *data)
358{
359 int reg;
360
361 reg = i2c_smbus_read_byte_data(data->client,
362 MMA8452_CTRL_REG2);
363 if (reg < 0)
364 return reg;
365
366 return ((reg & MMA8452_CTRL_REG2_MODS_MASK) >>
367 MMA8452_CTRL_REG2_MODS_SHIFT);
368}
369
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000370static ssize_t mma8452_show_samp_freq_avail(struct device *dev,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200371 struct device_attribute *attr,
372 char *buf)
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000373{
374 return mma8452_show_int_plus_micros(buf, mma8452_samp_freq,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200375 ARRAY_SIZE(mma8452_samp_freq));
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000376}
377
378static ssize_t mma8452_show_scale_avail(struct device *dev,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200379 struct device_attribute *attr,
380 char *buf)
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000381{
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200382 struct mma8452_data *data = iio_priv(i2c_get_clientdata(
383 to_i2c_client(dev)));
384
385 return mma8452_show_int_plus_micros(buf, data->chip_info->mma_scales,
386 ARRAY_SIZE(data->chip_info->mma_scales));
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000387}
388
Martin Fuzzey1e798412015-06-01 15:39:56 +0200389static ssize_t mma8452_show_hp_cutoff_avail(struct device *dev,
390 struct device_attribute *attr,
391 char *buf)
392{
393 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
394 struct mma8452_data *data = iio_priv(indio_dev);
Martin Kepplingered859fc2016-04-25 14:08:25 +0200395 int i, j;
Martin Fuzzey1e798412015-06-01 15:39:56 +0200396
Martin Kepplingered859fc2016-04-25 14:08:25 +0200397 i = mma8452_get_odr_index(data);
398 j = mma8452_get_power_mode(data);
399 if (j < 0)
400 return j;
401
402 return mma8452_show_int_plus_micros(buf, mma8452_hp_filter_cutoff[j][i],
403 ARRAY_SIZE(mma8452_hp_filter_cutoff[0][0]));
404}
405
406static ssize_t mma8452_show_os_ratio_avail(struct device *dev,
407 struct device_attribute *attr,
408 char *buf)
409{
410 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
411 struct mma8452_data *data = iio_priv(indio_dev);
412 int i = mma8452_get_odr_index(data);
413 int j;
414 u16 val = 0;
415 size_t len = 0;
416
417 for (j = 0; j < ARRAY_SIZE(mma8452_os_ratio); j++) {
418 if (val == mma8452_os_ratio[j][i])
419 continue;
420
421 val = mma8452_os_ratio[j][i];
422
423 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", val);
424 }
425 buf[len - 1] = '\n';
426
427 return len;
Martin Fuzzey1e798412015-06-01 15:39:56 +0200428}
429
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000430static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(mma8452_show_samp_freq_avail);
Harinath Nampallycd327b02017-09-23 16:56:29 -0400431static IIO_DEVICE_ATTR(in_accel_scale_available, 0444,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200432 mma8452_show_scale_avail, NULL, 0);
Martin Fuzzey1e798412015-06-01 15:39:56 +0200433static IIO_DEVICE_ATTR(in_accel_filter_high_pass_3db_frequency_available,
Harinath Nampallycd327b02017-09-23 16:56:29 -0400434 0444, mma8452_show_hp_cutoff_avail, NULL, 0);
435static IIO_DEVICE_ATTR(in_accel_oversampling_ratio_available, 0444,
Martin Kepplingered859fc2016-04-25 14:08:25 +0200436 mma8452_show_os_ratio_avail, NULL, 0);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000437
438static int mma8452_get_samp_freq_index(struct mma8452_data *data,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200439 int val, int val2)
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000440{
441 return mma8452_get_int_plus_micros_index(mma8452_samp_freq,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200442 ARRAY_SIZE(mma8452_samp_freq),
443 val, val2);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000444}
445
Hartmut Knaack686027f2015-08-02 22:43:51 +0200446static int mma8452_get_scale_index(struct mma8452_data *data, int val, int val2)
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000447{
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200448 return mma8452_get_int_plus_micros_index(data->chip_info->mma_scales,
449 ARRAY_SIZE(data->chip_info->mma_scales), val, val2);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000450}
451
Martin Fuzzey1e798412015-06-01 15:39:56 +0200452static int mma8452_get_hp_filter_index(struct mma8452_data *data,
453 int val, int val2)
454{
Martin Kepplingered859fc2016-04-25 14:08:25 +0200455 int i, j;
Martin Fuzzey1e798412015-06-01 15:39:56 +0200456
Martin Kepplingered859fc2016-04-25 14:08:25 +0200457 i = mma8452_get_odr_index(data);
458 j = mma8452_get_power_mode(data);
459 if (j < 0)
460 return j;
461
462 return mma8452_get_int_plus_micros_index(mma8452_hp_filter_cutoff[j][i],
463 ARRAY_SIZE(mma8452_hp_filter_cutoff[0][0]), val, val2);
Martin Fuzzey1e798412015-06-01 15:39:56 +0200464}
465
466static int mma8452_read_hp_filter(struct mma8452_data *data, int *hz, int *uHz)
467{
Martin Kepplingered859fc2016-04-25 14:08:25 +0200468 int j, i, ret;
Martin Fuzzey1e798412015-06-01 15:39:56 +0200469
470 ret = i2c_smbus_read_byte_data(data->client, MMA8452_HP_FILTER_CUTOFF);
471 if (ret < 0)
472 return ret;
473
474 i = mma8452_get_odr_index(data);
Martin Kepplingered859fc2016-04-25 14:08:25 +0200475 j = mma8452_get_power_mode(data);
476 if (j < 0)
477 return j;
478
Martin Fuzzey1e798412015-06-01 15:39:56 +0200479 ret &= MMA8452_HP_FILTER_CUTOFF_SEL_MASK;
Martin Kepplingered859fc2016-04-25 14:08:25 +0200480 *hz = mma8452_hp_filter_cutoff[j][i][ret][0];
481 *uHz = mma8452_hp_filter_cutoff[j][i][ret][1];
Martin Fuzzey1e798412015-06-01 15:39:56 +0200482
483 return 0;
484}
485
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000486static int mma8452_read_raw(struct iio_dev *indio_dev,
487 struct iio_chan_spec const *chan,
488 int *val, int *val2, long mask)
489{
490 struct mma8452_data *data = iio_priv(indio_dev);
491 __be16 buffer[3];
492 int i, ret;
493
494 switch (mask) {
495 case IIO_CHAN_INFO_RAW:
Alison Schofield4d9b0412016-10-11 12:31:36 -0700496 ret = iio_device_claim_direct_mode(indio_dev);
497 if (ret)
498 return ret;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000499
500 mutex_lock(&data->lock);
501 ret = mma8452_read(data, buffer);
502 mutex_unlock(&data->lock);
Alison Schofield4d9b0412016-10-11 12:31:36 -0700503 iio_device_release_direct_mode(indio_dev);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000504 if (ret < 0)
505 return ret;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200506
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200507 *val = sign_extend32(be16_to_cpu(
508 buffer[chan->scan_index]) >> chan->scan_type.shift,
509 chan->scan_type.realbits - 1);
Hartmut Knaack686027f2015-08-02 22:43:51 +0200510
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000511 return IIO_VAL_INT;
512 case IIO_CHAN_INFO_SCALE:
513 i = data->data_cfg & MMA8452_DATA_CFG_FS_MASK;
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +0200514 *val = data->chip_info->mma_scales[i][0];
515 *val2 = data->chip_info->mma_scales[i][1];
Hartmut Knaack686027f2015-08-02 22:43:51 +0200516
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000517 return IIO_VAL_INT_PLUS_MICRO;
518 case IIO_CHAN_INFO_SAMP_FREQ:
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200519 i = mma8452_get_odr_index(data);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000520 *val = mma8452_samp_freq[i][0];
521 *val2 = mma8452_samp_freq[i][1];
Hartmut Knaack686027f2015-08-02 22:43:51 +0200522
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000523 return IIO_VAL_INT_PLUS_MICRO;
524 case IIO_CHAN_INFO_CALIBBIAS:
Hartmut Knaack686027f2015-08-02 22:43:51 +0200525 ret = i2c_smbus_read_byte_data(data->client,
Martin Kepplinger8b8ff3a2016-03-03 09:24:01 +0100526 MMA8452_OFF_X +
527 chan->scan_index);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000528 if (ret < 0)
529 return ret;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200530
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000531 *val = sign_extend32(ret, 7);
Hartmut Knaack686027f2015-08-02 22:43:51 +0200532
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000533 return IIO_VAL_INT;
Martin Fuzzey1e798412015-06-01 15:39:56 +0200534 case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY:
535 if (data->data_cfg & MMA8452_DATA_CFG_HPF_MASK) {
536 ret = mma8452_read_hp_filter(data, val, val2);
537 if (ret < 0)
538 return ret;
539 } else {
540 *val = 0;
541 *val2 = 0;
542 }
Hartmut Knaack686027f2015-08-02 22:43:51 +0200543
Martin Fuzzey1e798412015-06-01 15:39:56 +0200544 return IIO_VAL_INT_PLUS_MICRO;
Martin Kepplingered859fc2016-04-25 14:08:25 +0200545 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
546 ret = mma8452_get_power_mode(data);
547 if (ret < 0)
548 return ret;
549
550 i = mma8452_get_odr_index(data);
551
552 *val = mma8452_os_ratio[ret][i];
553 return IIO_VAL_INT;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000554 }
Hartmut Knaack686027f2015-08-02 22:43:51 +0200555
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000556 return -EINVAL;
557}
558
Richard Tresiddera45d1232018-05-11 16:54:59 +0800559static int mma8452_calculate_sleep(struct mma8452_data *data)
560{
561 int ret, i = mma8452_get_odr_index(data);
562
563 if (mma8452_samp_freq[i][0] > 0)
564 ret = 1000 / mma8452_samp_freq[i][0];
565 else
566 ret = 1000;
567
568 return ret == 0 ? 1 : ret;
569}
570
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000571static int mma8452_standby(struct mma8452_data *data)
572{
573 return i2c_smbus_write_byte_data(data->client, MMA8452_CTRL_REG1,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200574 data->ctrl_reg1 & ~MMA8452_CTRL_ACTIVE);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000575}
576
577static int mma8452_active(struct mma8452_data *data)
578{
579 return i2c_smbus_write_byte_data(data->client, MMA8452_CTRL_REG1,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200580 data->ctrl_reg1);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000581}
582
Martin Kepplingere8668532016-03-03 09:24:02 +0100583/* returns >0 if active, 0 if in standby and <0 on error */
584static int mma8452_is_active(struct mma8452_data *data)
585{
586 int reg;
587
588 reg = i2c_smbus_read_byte_data(data->client, MMA8452_CTRL_REG1);
589 if (reg < 0)
590 return reg;
591
592 return reg & MMA8452_CTRL_ACTIVE;
593}
594
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000595static int mma8452_change_config(struct mma8452_data *data, u8 reg, u8 val)
596{
597 int ret;
Martin Kepplingere8668532016-03-03 09:24:02 +0100598 int is_active;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000599
600 mutex_lock(&data->lock);
601
Martin Kepplingere8668532016-03-03 09:24:02 +0100602 is_active = mma8452_is_active(data);
603 if (is_active < 0) {
604 ret = is_active;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000605 goto fail;
Martin Kepplingere8668532016-03-03 09:24:02 +0100606 }
607
608 /* config can only be changed when in standby */
609 if (is_active > 0) {
610 ret = mma8452_standby(data);
611 if (ret < 0)
612 goto fail;
613 }
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000614
615 ret = i2c_smbus_write_byte_data(data->client, reg, val);
616 if (ret < 0)
617 goto fail;
618
Martin Kepplingere8668532016-03-03 09:24:02 +0100619 if (is_active > 0) {
620 ret = mma8452_active(data);
621 if (ret < 0)
622 goto fail;
623 }
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000624
625 ret = 0;
626fail:
627 mutex_unlock(&data->lock);
Hartmut Knaack686027f2015-08-02 22:43:51 +0200628
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000629 return ret;
630}
631
Martin Kepplingered859fc2016-04-25 14:08:25 +0200632static int mma8452_set_power_mode(struct mma8452_data *data, u8 mode)
633{
634 int reg;
635
636 reg = i2c_smbus_read_byte_data(data->client,
637 MMA8452_CTRL_REG2);
638 if (reg < 0)
639 return reg;
640
641 reg &= ~MMA8452_CTRL_REG2_MODS_MASK;
642 reg |= mode << MMA8452_CTRL_REG2_MODS_SHIFT;
643
644 return mma8452_change_config(data, MMA8452_CTRL_REG2, reg);
645}
646
Martin Kepplinger8b8ff3a2016-03-03 09:24:01 +0100647/* returns >0 if in freefall mode, 0 if not or <0 if an error occurred */
Martin Kepplinger4b042662016-01-16 15:35:20 +0100648static int mma8452_freefall_mode_enabled(struct mma8452_data *data)
649{
650 int val;
Martin Kepplinger4b042662016-01-16 15:35:20 +0100651
Harinath Nampally605f72d2017-09-09 15:56:58 -0400652 val = i2c_smbus_read_byte_data(data->client, MMA8452_FF_MT_CFG);
Martin Kepplinger4b042662016-01-16 15:35:20 +0100653 if (val < 0)
654 return val;
655
656 return !(val & MMA8452_FF_MT_CFG_OAE);
657}
658
659static int mma8452_set_freefall_mode(struct mma8452_data *data, bool state)
660{
661 int val;
Martin Kepplinger4b042662016-01-16 15:35:20 +0100662
663 if ((state && mma8452_freefall_mode_enabled(data)) ||
664 (!state && !(mma8452_freefall_mode_enabled(data))))
665 return 0;
666
Harinath Nampally605f72d2017-09-09 15:56:58 -0400667 val = i2c_smbus_read_byte_data(data->client, MMA8452_FF_MT_CFG);
Martin Kepplinger4b042662016-01-16 15:35:20 +0100668 if (val < 0)
669 return val;
670
671 if (state) {
Harinath Nampally605f72d2017-09-09 15:56:58 -0400672 val |= BIT(idx_x + MMA8452_FF_MT_CHAN_SHIFT);
673 val |= BIT(idx_y + MMA8452_FF_MT_CHAN_SHIFT);
674 val |= BIT(idx_z + MMA8452_FF_MT_CHAN_SHIFT);
Martin Kepplinger4b042662016-01-16 15:35:20 +0100675 val &= ~MMA8452_FF_MT_CFG_OAE;
676 } else {
Harinath Nampally605f72d2017-09-09 15:56:58 -0400677 val &= ~BIT(idx_x + MMA8452_FF_MT_CHAN_SHIFT);
678 val &= ~BIT(idx_y + MMA8452_FF_MT_CHAN_SHIFT);
679 val &= ~BIT(idx_z + MMA8452_FF_MT_CHAN_SHIFT);
Martin Kepplinger4b042662016-01-16 15:35:20 +0100680 val |= MMA8452_FF_MT_CFG_OAE;
681 }
682
Harinath Nampally605f72d2017-09-09 15:56:58 -0400683 return mma8452_change_config(data, MMA8452_FF_MT_CFG, val);
Martin Kepplinger4b042662016-01-16 15:35:20 +0100684}
685
Martin Fuzzey1e798412015-06-01 15:39:56 +0200686static int mma8452_set_hp_filter_frequency(struct mma8452_data *data,
687 int val, int val2)
688{
689 int i, reg;
690
691 i = mma8452_get_hp_filter_index(data, val, val2);
692 if (i < 0)
Hartmut Knaackb9fddcd2015-08-02 22:43:48 +0200693 return i;
Martin Fuzzey1e798412015-06-01 15:39:56 +0200694
695 reg = i2c_smbus_read_byte_data(data->client,
696 MMA8452_HP_FILTER_CUTOFF);
697 if (reg < 0)
698 return reg;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200699
Martin Fuzzey1e798412015-06-01 15:39:56 +0200700 reg &= ~MMA8452_HP_FILTER_CUTOFF_SEL_MASK;
701 reg |= i;
702
703 return mma8452_change_config(data, MMA8452_HP_FILTER_CUTOFF, reg);
704}
705
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000706static int mma8452_write_raw(struct iio_dev *indio_dev,
707 struct iio_chan_spec const *chan,
708 int val, int val2, long mask)
709{
710 struct mma8452_data *data = iio_priv(indio_dev);
Martin Fuzzey1e798412015-06-01 15:39:56 +0200711 int i, ret;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000712
Jonathan Cameron79de2ee2016-10-15 15:55:06 +0100713 ret = iio_device_claim_direct_mode(indio_dev);
714 if (ret)
715 return ret;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000716
717 switch (mask) {
718 case IIO_CHAN_INFO_SAMP_FREQ:
719 i = mma8452_get_samp_freq_index(data, val, val2);
Jonathan Cameron79de2ee2016-10-15 15:55:06 +0100720 if (i < 0) {
721 ret = i;
722 break;
723 }
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000724 data->ctrl_reg1 &= ~MMA8452_CTRL_DR_MASK;
725 data->ctrl_reg1 |= i << MMA8452_CTRL_DR_SHIFT;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200726
Richard Tresiddera45d1232018-05-11 16:54:59 +0800727 data->sleep_val = mma8452_calculate_sleep(data);
728
Jonathan Cameron79de2ee2016-10-15 15:55:06 +0100729 ret = mma8452_change_config(data, MMA8452_CTRL_REG1,
730 data->ctrl_reg1);
731 break;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000732 case IIO_CHAN_INFO_SCALE:
733 i = mma8452_get_scale_index(data, val, val2);
Jonathan Cameron79de2ee2016-10-15 15:55:06 +0100734 if (i < 0) {
735 ret = i;
736 break;
737 }
Hartmut Knaack686027f2015-08-02 22:43:51 +0200738
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000739 data->data_cfg &= ~MMA8452_DATA_CFG_FS_MASK;
740 data->data_cfg |= i;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200741
Jonathan Cameron79de2ee2016-10-15 15:55:06 +0100742 ret = mma8452_change_config(data, MMA8452_DATA_CFG,
743 data->data_cfg);
744 break;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000745 case IIO_CHAN_INFO_CALIBBIAS:
Jonathan Cameron79de2ee2016-10-15 15:55:06 +0100746 if (val < -128 || val > 127) {
747 ret = -EINVAL;
748 break;
749 }
Hartmut Knaack686027f2015-08-02 22:43:51 +0200750
Jonathan Cameron79de2ee2016-10-15 15:55:06 +0100751 ret = mma8452_change_config(data,
752 MMA8452_OFF_X + chan->scan_index,
753 val);
754 break;
Martin Fuzzey1e798412015-06-01 15:39:56 +0200755
756 case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY:
757 if (val == 0 && val2 == 0) {
758 data->data_cfg &= ~MMA8452_DATA_CFG_HPF_MASK;
759 } else {
760 data->data_cfg |= MMA8452_DATA_CFG_HPF_MASK;
761 ret = mma8452_set_hp_filter_frequency(data, val, val2);
762 if (ret < 0)
Jonathan Cameron79de2ee2016-10-15 15:55:06 +0100763 break;
Martin Fuzzey1e798412015-06-01 15:39:56 +0200764 }
Hartmut Knaack686027f2015-08-02 22:43:51 +0200765
Jonathan Cameron79de2ee2016-10-15 15:55:06 +0100766 ret = mma8452_change_config(data, MMA8452_DATA_CFG,
Hartmut Knaack686027f2015-08-02 22:43:51 +0200767 data->data_cfg);
Jonathan Cameron79de2ee2016-10-15 15:55:06 +0100768 break;
Martin Fuzzey1e798412015-06-01 15:39:56 +0200769
Martin Kepplingered859fc2016-04-25 14:08:25 +0200770 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
771 ret = mma8452_get_odr_index(data);
772
773 for (i = 0; i < ARRAY_SIZE(mma8452_os_ratio); i++) {
Jonathan Cameron79de2ee2016-10-15 15:55:06 +0100774 if (mma8452_os_ratio[i][ret] == val) {
775 ret = mma8452_set_power_mode(data, i);
776 break;
777 }
Martin Kepplingered859fc2016-04-25 14:08:25 +0200778 }
Jonathan Cameron79de2ee2016-10-15 15:55:06 +0100779 break;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000780 default:
Jonathan Cameron79de2ee2016-10-15 15:55:06 +0100781 ret = -EINVAL;
782 break;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000783 }
Jonathan Cameron79de2ee2016-10-15 15:55:06 +0100784
785 iio_device_release_direct_mode(indio_dev);
786 return ret;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +0000787}
788
Harinath Nampally605f72d2017-09-09 15:56:58 -0400789static int mma8452_get_event_regs(struct mma8452_data *data,
790 const struct iio_chan_spec *chan, enum iio_event_direction dir,
791 const struct mma8452_event_regs **ev_reg)
792{
793 if (!chan)
794 return -EINVAL;
795
796 switch (chan->type) {
797 case IIO_ACCEL:
798 switch (dir) {
799 case IIO_EV_DIR_RISING:
800 if ((data->chip_info->all_events
801 & MMA8452_INT_TRANS) &&
802 (data->chip_info->enabled_events
803 & MMA8452_INT_TRANS))
Harinath Nampallya654c062017-11-05 13:00:03 -0500804 *ev_reg = &trans_ev_regs;
Harinath Nampally605f72d2017-09-09 15:56:58 -0400805 else
Harinath Nampallya654c062017-11-05 13:00:03 -0500806 *ev_reg = &ff_mt_ev_regs;
Harinath Nampally605f72d2017-09-09 15:56:58 -0400807 return 0;
808 case IIO_EV_DIR_FALLING:
Harinath Nampallya654c062017-11-05 13:00:03 -0500809 *ev_reg = &ff_mt_ev_regs;
Harinath Nampally605f72d2017-09-09 15:56:58 -0400810 return 0;
811 default:
812 return -EINVAL;
813 }
814 default:
815 return -EINVAL;
816 }
817}
818
Harinath Nampally4febd9f2017-09-25 06:40:08 -0400819static int mma8452_read_event_value(struct iio_dev *indio_dev,
Martin Fuzzey28e34272015-06-01 15:39:52 +0200820 const struct iio_chan_spec *chan,
821 enum iio_event_type type,
822 enum iio_event_direction dir,
823 enum iio_event_info info,
824 int *val, int *val2)
825{
826 struct mma8452_data *data = iio_priv(indio_dev);
Martin Kepplingered859fc2016-04-25 14:08:25 +0200827 int ret, us, power_mode;
Harinath Nampally605f72d2017-09-09 15:56:58 -0400828 const struct mma8452_event_regs *ev_regs;
829
830 ret = mma8452_get_event_regs(data, chan, dir, &ev_regs);
831 if (ret)
832 return ret;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200833
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200834 switch (info) {
835 case IIO_EV_INFO_VALUE:
Harinath Nampally605f72d2017-09-09 15:56:58 -0400836 ret = i2c_smbus_read_byte_data(data->client, ev_regs->ev_ths);
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200837 if (ret < 0)
838 return ret;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200839
Harinath Nampally605f72d2017-09-09 15:56:58 -0400840 *val = ret & ev_regs->ev_ths_mask;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200841
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200842 return IIO_VAL_INT;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200843
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200844 case IIO_EV_INFO_PERIOD:
Harinath Nampally605f72d2017-09-09 15:56:58 -0400845 ret = i2c_smbus_read_byte_data(data->client, ev_regs->ev_count);
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200846 if (ret < 0)
847 return ret;
848
Martin Kepplingered859fc2016-04-25 14:08:25 +0200849 power_mode = mma8452_get_power_mode(data);
850 if (power_mode < 0)
851 return power_mode;
852
Harinath Nampallycc54a662017-11-05 13:00:02 -0500853 us = ret * mma8452_time_step_us[power_mode][
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200854 mma8452_get_odr_index(data)];
855 *val = us / USEC_PER_SEC;
856 *val2 = us % USEC_PER_SEC;
Hartmut Knaack686027f2015-08-02 22:43:51 +0200857
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200858 return IIO_VAL_INT_PLUS_MICRO;
859
Martin Fuzzey1e798412015-06-01 15:39:56 +0200860 case IIO_EV_INFO_HIGH_PASS_FILTER_3DB:
861 ret = i2c_smbus_read_byte_data(data->client,
862 MMA8452_TRANSIENT_CFG);
863 if (ret < 0)
864 return ret;
865
866 if (ret & MMA8452_TRANSIENT_CFG_HPF_BYP) {
867 *val = 0;
868 *val2 = 0;
869 } else {
870 ret = mma8452_read_hp_filter(data, val, val2);
871 if (ret < 0)
872 return ret;
873 }
Hartmut Knaack686027f2015-08-02 22:43:51 +0200874
Martin Fuzzey1e798412015-06-01 15:39:56 +0200875 return IIO_VAL_INT_PLUS_MICRO;
876
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200877 default:
878 return -EINVAL;
879 }
Martin Fuzzey28e34272015-06-01 15:39:52 +0200880}
881
Harinath Nampally4febd9f2017-09-25 06:40:08 -0400882static int mma8452_write_event_value(struct iio_dev *indio_dev,
Martin Fuzzey28e34272015-06-01 15:39:52 +0200883 const struct iio_chan_spec *chan,
884 enum iio_event_type type,
885 enum iio_event_direction dir,
886 enum iio_event_info info,
887 int val, int val2)
888{
889 struct mma8452_data *data = iio_priv(indio_dev);
Martin Fuzzey1e798412015-06-01 15:39:56 +0200890 int ret, reg, steps;
Harinath Nampally605f72d2017-09-09 15:56:58 -0400891 const struct mma8452_event_regs *ev_regs;
892
893 ret = mma8452_get_event_regs(data, chan, dir, &ev_regs);
894 if (ret)
895 return ret;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200896
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200897 switch (info) {
898 case IIO_EV_INFO_VALUE:
Harinath Nampally605f72d2017-09-09 15:56:58 -0400899 if (val < 0 || val > ev_regs->ev_ths_mask)
Hartmut Knaack11218222015-08-02 22:43:49 +0200900 return -EINVAL;
901
Harinath Nampally605f72d2017-09-09 15:56:58 -0400902 return mma8452_change_config(data, ev_regs->ev_ths, val);
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200903
904 case IIO_EV_INFO_PERIOD:
Martin Kepplingered859fc2016-04-25 14:08:25 +0200905 ret = mma8452_get_power_mode(data);
906 if (ret < 0)
907 return ret;
908
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200909 steps = (val * USEC_PER_SEC + val2) /
Harinath Nampallycc54a662017-11-05 13:00:02 -0500910 mma8452_time_step_us[ret][
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200911 mma8452_get_odr_index(data)];
912
Hartmut Knaack11218222015-08-02 22:43:49 +0200913 if (steps < 0 || steps > 0xff)
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200914 return -EINVAL;
915
Harinath Nampally605f72d2017-09-09 15:56:58 -0400916 return mma8452_change_config(data, ev_regs->ev_count, steps);
Hartmut Knaack686027f2015-08-02 22:43:51 +0200917
Martin Fuzzey1e798412015-06-01 15:39:56 +0200918 case IIO_EV_INFO_HIGH_PASS_FILTER_3DB:
919 reg = i2c_smbus_read_byte_data(data->client,
920 MMA8452_TRANSIENT_CFG);
921 if (reg < 0)
922 return reg;
923
924 if (val == 0 && val2 == 0) {
925 reg |= MMA8452_TRANSIENT_CFG_HPF_BYP;
926 } else {
927 reg &= ~MMA8452_TRANSIENT_CFG_HPF_BYP;
928 ret = mma8452_set_hp_filter_frequency(data, val, val2);
929 if (ret < 0)
930 return ret;
931 }
Hartmut Knaack686027f2015-08-02 22:43:51 +0200932
Martin Fuzzey1e798412015-06-01 15:39:56 +0200933 return mma8452_change_config(data, MMA8452_TRANSIENT_CFG, reg);
934
Martin Fuzzey5dbbd192015-06-01 15:39:54 +0200935 default:
936 return -EINVAL;
937 }
Martin Fuzzey28e34272015-06-01 15:39:52 +0200938}
939
940static int mma8452_read_event_config(struct iio_dev *indio_dev,
941 const struct iio_chan_spec *chan,
942 enum iio_event_type type,
943 enum iio_event_direction dir)
944{
945 struct mma8452_data *data = iio_priv(indio_dev);
946 int ret;
Harinath Nampally605f72d2017-09-09 15:56:58 -0400947 const struct mma8452_event_regs *ev_regs;
948
949 ret = mma8452_get_event_regs(data, chan, dir, &ev_regs);
950 if (ret)
951 return ret;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200952
Martin Kepplinger4b042662016-01-16 15:35:20 +0100953 switch (dir) {
954 case IIO_EV_DIR_FALLING:
955 return mma8452_freefall_mode_enabled(data);
956 case IIO_EV_DIR_RISING:
Martin Kepplinger4b042662016-01-16 15:35:20 +0100957 ret = i2c_smbus_read_byte_data(data->client,
Harinath Nampally605f72d2017-09-09 15:56:58 -0400958 ev_regs->ev_cfg);
Martin Kepplinger4b042662016-01-16 15:35:20 +0100959 if (ret < 0)
960 return ret;
961
Martin Kepplinger8b8ff3a2016-03-03 09:24:01 +0100962 return !!(ret & BIT(chan->scan_index +
Harinath Nampally605f72d2017-09-09 15:56:58 -0400963 ev_regs->ev_cfg_chan_shift));
Martin Kepplinger4b042662016-01-16 15:35:20 +0100964 default:
965 return -EINVAL;
966 }
Martin Fuzzey28e34272015-06-01 15:39:52 +0200967}
968
969static int mma8452_write_event_config(struct iio_dev *indio_dev,
970 const struct iio_chan_spec *chan,
971 enum iio_event_type type,
972 enum iio_event_direction dir,
973 int state)
974{
975 struct mma8452_data *data = iio_priv(indio_dev);
Martin Kepplinger96c0cb22016-03-03 09:24:03 +0100976 int val, ret;
Harinath Nampally605f72d2017-09-09 15:56:58 -0400977 const struct mma8452_event_regs *ev_regs;
978
979 ret = mma8452_get_event_regs(data, chan, dir, &ev_regs);
980 if (ret)
981 return ret;
Martin Kepplinger96c0cb22016-03-03 09:24:03 +0100982
983 ret = mma8452_set_runtime_pm_state(data->client, state);
984 if (ret)
985 return ret;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200986
Martin Kepplinger4b042662016-01-16 15:35:20 +0100987 switch (dir) {
988 case IIO_EV_DIR_FALLING:
989 return mma8452_set_freefall_mode(data, state);
990 case IIO_EV_DIR_RISING:
Harinath Nampally605f72d2017-09-09 15:56:58 -0400991 val = i2c_smbus_read_byte_data(data->client, ev_regs->ev_cfg);
Martin Kepplinger4b042662016-01-16 15:35:20 +0100992 if (val < 0)
993 return val;
Martin Fuzzey28e34272015-06-01 15:39:52 +0200994
Martin Kepplinger4b042662016-01-16 15:35:20 +0100995 if (state) {
996 if (mma8452_freefall_mode_enabled(data)) {
Harinath Nampally605f72d2017-09-09 15:56:58 -0400997 val &= ~BIT(idx_x + ev_regs->ev_cfg_chan_shift);
998 val &= ~BIT(idx_y + ev_regs->ev_cfg_chan_shift);
999 val &= ~BIT(idx_z + ev_regs->ev_cfg_chan_shift);
Martin Kepplinger4b042662016-01-16 15:35:20 +01001000 val |= MMA8452_FF_MT_CFG_OAE;
1001 }
Harinath Nampally605f72d2017-09-09 15:56:58 -04001002 val |= BIT(chan->scan_index +
1003 ev_regs->ev_cfg_chan_shift);
Martin Kepplinger4b042662016-01-16 15:35:20 +01001004 } else {
1005 if (mma8452_freefall_mode_enabled(data))
1006 return 0;
Martin Fuzzey28e34272015-06-01 15:39:52 +02001007
Harinath Nampally605f72d2017-09-09 15:56:58 -04001008 val &= ~BIT(chan->scan_index +
1009 ev_regs->ev_cfg_chan_shift);
Martin Kepplinger4b042662016-01-16 15:35:20 +01001010 }
Martin Fuzzey28e34272015-06-01 15:39:52 +02001011
Harinath Nampally605f72d2017-09-09 15:56:58 -04001012 val |= ev_regs->ev_cfg_ele;
Martin Kepplinger4b042662016-01-16 15:35:20 +01001013
Harinath Nampally605f72d2017-09-09 15:56:58 -04001014 return mma8452_change_config(data, ev_regs->ev_cfg, val);
Martin Kepplinger4b042662016-01-16 15:35:20 +01001015 default:
1016 return -EINVAL;
1017 }
Martin Fuzzey28e34272015-06-01 15:39:52 +02001018}
1019
1020static void mma8452_transient_interrupt(struct iio_dev *indio_dev)
1021{
1022 struct mma8452_data *data = iio_priv(indio_dev);
Gregor Boiriebc2b7da2016-03-09 19:05:49 +01001023 s64 ts = iio_get_time_ns(indio_dev);
Martin Fuzzey28e34272015-06-01 15:39:52 +02001024 int src;
1025
Harinath Nampally605f72d2017-09-09 15:56:58 -04001026 src = i2c_smbus_read_byte_data(data->client, MMA8452_TRANSIENT_SRC);
Martin Fuzzey28e34272015-06-01 15:39:52 +02001027 if (src < 0)
1028 return;
1029
Harinath Nampally605f72d2017-09-09 15:56:58 -04001030 if (src & MMA8452_TRANSIENT_SRC_XTRANSE)
Martin Fuzzey28e34272015-06-01 15:39:52 +02001031 iio_push_event(indio_dev,
1032 IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X,
Martin Kepplingerc5d0db02015-07-05 19:50:18 +02001033 IIO_EV_TYPE_MAG,
Martin Fuzzey28e34272015-06-01 15:39:52 +02001034 IIO_EV_DIR_RISING),
1035 ts);
1036
Harinath Nampally605f72d2017-09-09 15:56:58 -04001037 if (src & MMA8452_TRANSIENT_SRC_YTRANSE)
Martin Fuzzey28e34272015-06-01 15:39:52 +02001038 iio_push_event(indio_dev,
1039 IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_Y,
Martin Kepplingerc5d0db02015-07-05 19:50:18 +02001040 IIO_EV_TYPE_MAG,
Martin Fuzzey28e34272015-06-01 15:39:52 +02001041 IIO_EV_DIR_RISING),
1042 ts);
1043
Harinath Nampally605f72d2017-09-09 15:56:58 -04001044 if (src & MMA8452_TRANSIENT_SRC_ZTRANSE)
Martin Fuzzey28e34272015-06-01 15:39:52 +02001045 iio_push_event(indio_dev,
1046 IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_Z,
Martin Kepplingerc5d0db02015-07-05 19:50:18 +02001047 IIO_EV_TYPE_MAG,
Martin Fuzzey28e34272015-06-01 15:39:52 +02001048 IIO_EV_DIR_RISING),
1049 ts);
1050}
1051
1052static irqreturn_t mma8452_interrupt(int irq, void *p)
1053{
1054 struct iio_dev *indio_dev = p;
1055 struct mma8452_data *data = iio_priv(indio_dev);
Lars-Peter Clauseneb046982021-11-01 11:27:34 +01001056 irqreturn_t ret = IRQ_NONE;
Martin Fuzzey28e34272015-06-01 15:39:52 +02001057 int src;
1058
1059 src = i2c_smbus_read_byte_data(data->client, MMA8452_INT_SRC);
1060 if (src < 0)
1061 return IRQ_NONE;
1062
Leonard Crestezb02ec672018-06-07 21:52:50 +03001063 if (!(src & (data->chip_info->enabled_events | MMA8452_INT_DRDY)))
Harinath Nampally605f72d2017-09-09 15:56:58 -04001064 return IRQ_NONE;
1065
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001066 if (src & MMA8452_INT_DRDY) {
1067 iio_trigger_poll_chained(indio_dev->trig);
1068 ret = IRQ_HANDLED;
Martin Fuzzey28e34272015-06-01 15:39:52 +02001069 }
1070
Harinath Nampally605f72d2017-09-09 15:56:58 -04001071 if (src & MMA8452_INT_FF_MT) {
1072 if (mma8452_freefall_mode_enabled(data)) {
1073 s64 ts = iio_get_time_ns(indio_dev);
1074
1075 iio_push_event(indio_dev,
1076 IIO_MOD_EVENT_CODE(IIO_ACCEL, 0,
1077 IIO_MOD_X_AND_Y_AND_Z,
1078 IIO_EV_TYPE_MAG,
1079 IIO_EV_DIR_FALLING),
1080 ts);
1081 }
1082 ret = IRQ_HANDLED;
1083 }
1084
1085 if (src & MMA8452_INT_TRANS) {
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001086 mma8452_transient_interrupt(indio_dev);
1087 ret = IRQ_HANDLED;
1088 }
1089
1090 return ret;
Martin Fuzzey28e34272015-06-01 15:39:52 +02001091}
1092
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001093static irqreturn_t mma8452_trigger_handler(int irq, void *p)
1094{
1095 struct iio_poll_func *pf = p;
1096 struct iio_dev *indio_dev = pf->indio_dev;
1097 struct mma8452_data *data = iio_priv(indio_dev);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001098 int ret;
1099
Jonathan Cameron89226a22020-07-22 16:50:38 +01001100 ret = mma8452_read(data, data->buffer.channels);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001101 if (ret < 0)
1102 goto done;
1103
Jonathan Cameron89226a22020-07-22 16:50:38 +01001104 iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer,
Gregor Boiriebc2b7da2016-03-09 19:05:49 +01001105 iio_get_time_ns(indio_dev));
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001106
1107done:
1108 iio_trigger_notify_done(indio_dev->trig);
Hartmut Knaack686027f2015-08-02 22:43:51 +02001109
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001110 return IRQ_HANDLED;
1111}
1112
Martin Fuzzey2a17698c2015-05-13 12:26:40 +02001113static int mma8452_reg_access_dbg(struct iio_dev *indio_dev,
Harinath Nampallyf8b7b302017-09-23 16:56:30 -04001114 unsigned int reg, unsigned int writeval,
1115 unsigned int *readval)
Martin Fuzzey2a17698c2015-05-13 12:26:40 +02001116{
1117 int ret;
1118 struct mma8452_data *data = iio_priv(indio_dev);
1119
1120 if (reg > MMA8452_MAX_REG)
1121 return -EINVAL;
1122
1123 if (!readval)
1124 return mma8452_change_config(data, reg, writeval);
1125
1126 ret = i2c_smbus_read_byte_data(data->client, reg);
1127 if (ret < 0)
1128 return ret;
1129
1130 *readval = ret;
1131
1132 return 0;
1133}
1134
Martin Kepplinger4b042662016-01-16 15:35:20 +01001135static const struct iio_event_spec mma8452_freefall_event[] = {
1136 {
1137 .type = IIO_EV_TYPE_MAG,
1138 .dir = IIO_EV_DIR_FALLING,
1139 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
1140 .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
1141 BIT(IIO_EV_INFO_PERIOD) |
1142 BIT(IIO_EV_INFO_HIGH_PASS_FILTER_3DB)
1143 },
1144};
1145
1146static const struct iio_event_spec mma8652_freefall_event[] = {
1147 {
1148 .type = IIO_EV_TYPE_MAG,
1149 .dir = IIO_EV_DIR_FALLING,
1150 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
1151 .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
1152 BIT(IIO_EV_INFO_PERIOD)
1153 },
1154};
1155
Martin Fuzzey28e34272015-06-01 15:39:52 +02001156static const struct iio_event_spec mma8452_transient_event[] = {
1157 {
Martin Kepplingerc5d0db02015-07-05 19:50:18 +02001158 .type = IIO_EV_TYPE_MAG,
Martin Fuzzey28e34272015-06-01 15:39:52 +02001159 .dir = IIO_EV_DIR_RISING,
1160 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
Martin Fuzzey5dbbd192015-06-01 15:39:54 +02001161 .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
Martin Fuzzey1e798412015-06-01 15:39:56 +02001162 BIT(IIO_EV_INFO_PERIOD) |
1163 BIT(IIO_EV_INFO_HIGH_PASS_FILTER_3DB)
Martin Fuzzey28e34272015-06-01 15:39:52 +02001164 },
1165};
1166
Martin Kepplinger60f562e2015-09-01 13:45:10 +02001167static const struct iio_event_spec mma8452_motion_event[] = {
1168 {
1169 .type = IIO_EV_TYPE_MAG,
1170 .dir = IIO_EV_DIR_RISING,
1171 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
1172 .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
1173 BIT(IIO_EV_INFO_PERIOD)
1174 },
1175};
1176
Martin Fuzzey28e34272015-06-01 15:39:52 +02001177/*
1178 * Threshold is configured in fixed 8G/127 steps regardless of
1179 * currently selected scale for measurement.
1180 */
1181static IIO_CONST_ATTR_NAMED(accel_transient_scale, in_accel_scale, "0.617742");
1182
1183static struct attribute *mma8452_event_attributes[] = {
1184 &iio_const_attr_accel_transient_scale.dev_attr.attr,
1185 NULL,
1186};
1187
Rikard Falkeborn681ab2c2020-10-01 01:29:39 +02001188static const struct attribute_group mma8452_event_attribute_group = {
Martin Fuzzey28e34272015-06-01 15:39:52 +02001189 .attrs = mma8452_event_attributes,
Martin Fuzzey28e34272015-06-01 15:39:52 +02001190};
1191
Martin Kepplinger4b042662016-01-16 15:35:20 +01001192#define MMA8452_FREEFALL_CHANNEL(modifier) { \
1193 .type = IIO_ACCEL, \
1194 .modified = 1, \
1195 .channel2 = modifier, \
1196 .scan_index = -1, \
1197 .event_spec = mma8452_freefall_event, \
1198 .num_event_specs = ARRAY_SIZE(mma8452_freefall_event), \
1199}
1200
1201#define MMA8652_FREEFALL_CHANNEL(modifier) { \
1202 .type = IIO_ACCEL, \
1203 .modified = 1, \
1204 .channel2 = modifier, \
1205 .scan_index = -1, \
1206 .event_spec = mma8652_freefall_event, \
1207 .num_event_specs = ARRAY_SIZE(mma8652_freefall_event), \
1208}
1209
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001210#define MMA8452_CHANNEL(axis, idx, bits) { \
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001211 .type = IIO_ACCEL, \
1212 .modified = 1, \
1213 .channel2 = IIO_MOD_##axis, \
1214 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
Hartmut Knaack686027f2015-08-02 22:43:51 +02001215 BIT(IIO_CHAN_INFO_CALIBBIAS), \
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001216 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
Hartmut Knaack686027f2015-08-02 22:43:51 +02001217 BIT(IIO_CHAN_INFO_SCALE) | \
Martin Kepplingered859fc2016-04-25 14:08:25 +02001218 BIT(IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY) | \
1219 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001220 .scan_index = idx, \
1221 .scan_type = { \
1222 .sign = 's', \
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001223 .realbits = (bits), \
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001224 .storagebits = 16, \
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001225 .shift = 16 - (bits), \
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001226 .endianness = IIO_BE, \
1227 }, \
Martin Fuzzey28e34272015-06-01 15:39:52 +02001228 .event_spec = mma8452_transient_event, \
1229 .num_event_specs = ARRAY_SIZE(mma8452_transient_event), \
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001230}
1231
Martin Kepplinger417e0082015-09-01 13:45:11 +02001232#define MMA8652_CHANNEL(axis, idx, bits) { \
1233 .type = IIO_ACCEL, \
1234 .modified = 1, \
1235 .channel2 = IIO_MOD_##axis, \
1236 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
1237 BIT(IIO_CHAN_INFO_CALIBBIAS), \
1238 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
Martin Kepplingered859fc2016-04-25 14:08:25 +02001239 BIT(IIO_CHAN_INFO_SCALE) | \
1240 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
Martin Kepplinger417e0082015-09-01 13:45:11 +02001241 .scan_index = idx, \
1242 .scan_type = { \
1243 .sign = 's', \
1244 .realbits = (bits), \
1245 .storagebits = 16, \
1246 .shift = 16 - (bits), \
1247 .endianness = IIO_BE, \
1248 }, \
1249 .event_spec = mma8452_motion_event, \
1250 .num_event_specs = ARRAY_SIZE(mma8452_motion_event), \
1251}
1252
Martin Kepplinger244a93f2016-01-16 15:35:22 +01001253static const struct iio_chan_spec mma8451_channels[] = {
1254 MMA8452_CHANNEL(X, idx_x, 14),
1255 MMA8452_CHANNEL(Y, idx_y, 14),
1256 MMA8452_CHANNEL(Z, idx_z, 14),
1257 IIO_CHAN_SOFT_TIMESTAMP(idx_ts),
1258 MMA8452_FREEFALL_CHANNEL(IIO_MOD_X_AND_Y_AND_Z),
1259};
1260
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001261static const struct iio_chan_spec mma8452_channels[] = {
Martin Kepplingere60378c2015-12-15 17:45:00 +01001262 MMA8452_CHANNEL(X, idx_x, 12),
1263 MMA8452_CHANNEL(Y, idx_y, 12),
1264 MMA8452_CHANNEL(Z, idx_z, 12),
1265 IIO_CHAN_SOFT_TIMESTAMP(idx_ts),
Martin Kepplinger4b042662016-01-16 15:35:20 +01001266 MMA8452_FREEFALL_CHANNEL(IIO_MOD_X_AND_Y_AND_Z),
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001267};
1268
Martin Kepplingerc5ea1b582015-09-01 13:45:09 +02001269static const struct iio_chan_spec mma8453_channels[] = {
Martin Kepplingere60378c2015-12-15 17:45:00 +01001270 MMA8452_CHANNEL(X, idx_x, 10),
1271 MMA8452_CHANNEL(Y, idx_y, 10),
1272 MMA8452_CHANNEL(Z, idx_z, 10),
1273 IIO_CHAN_SOFT_TIMESTAMP(idx_ts),
Martin Kepplinger4b042662016-01-16 15:35:20 +01001274 MMA8452_FREEFALL_CHANNEL(IIO_MOD_X_AND_Y_AND_Z),
Martin Kepplingerc5ea1b582015-09-01 13:45:09 +02001275};
1276
Martin Kepplinger417e0082015-09-01 13:45:11 +02001277static const struct iio_chan_spec mma8652_channels[] = {
Martin Kepplingere60378c2015-12-15 17:45:00 +01001278 MMA8652_CHANNEL(X, idx_x, 12),
1279 MMA8652_CHANNEL(Y, idx_y, 12),
1280 MMA8652_CHANNEL(Z, idx_z, 12),
1281 IIO_CHAN_SOFT_TIMESTAMP(idx_ts),
Martin Kepplinger4b042662016-01-16 15:35:20 +01001282 MMA8652_FREEFALL_CHANNEL(IIO_MOD_X_AND_Y_AND_Z),
Martin Kepplinger417e0082015-09-01 13:45:11 +02001283};
1284
1285static const struct iio_chan_spec mma8653_channels[] = {
Martin Kepplingere60378c2015-12-15 17:45:00 +01001286 MMA8652_CHANNEL(X, idx_x, 10),
1287 MMA8652_CHANNEL(Y, idx_y, 10),
1288 MMA8652_CHANNEL(Z, idx_z, 10),
1289 IIO_CHAN_SOFT_TIMESTAMP(idx_ts),
Martin Kepplinger4b042662016-01-16 15:35:20 +01001290 MMA8652_FREEFALL_CHANNEL(IIO_MOD_X_AND_Y_AND_Z),
Martin Kepplinger417e0082015-09-01 13:45:11 +02001291};
1292
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001293enum {
Martin Kepplinger244a93f2016-01-16 15:35:22 +01001294 mma8451,
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001295 mma8452,
Martin Kepplingerc5ea1b582015-09-01 13:45:09 +02001296 mma8453,
Martin Kepplinger417e0082015-09-01 13:45:11 +02001297 mma8652,
1298 mma8653,
Martin Kepplingere8731182016-03-09 12:01:29 +01001299 fxls8471,
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001300};
1301
1302static const struct mma_chip_info mma_chip_info_table[] = {
Martin Kepplinger244a93f2016-01-16 15:35:22 +01001303 [mma8451] = {
1304 .chip_id = MMA8451_DEVICE_ID,
1305 .channels = mma8451_channels,
1306 .num_channels = ARRAY_SIZE(mma8451_channels),
1307 /*
1308 * Hardware has fullscale of -2G, -4G, -8G corresponding to
1309 * raw value -8192 for 14 bit, -2048 for 12 bit or -512 for 10
1310 * bit.
1311 * The userspace interface uses m/s^2 and we declare micro units
1312 * So scale factor for 12 bit here is given by:
Martin Kepplinger8b8ff3a2016-03-03 09:24:01 +01001313 * g * N * 1000000 / 2048 for N = 2, 4, 8 and g=9.80665
Martin Kepplinger244a93f2016-01-16 15:35:22 +01001314 */
1315 .mma_scales = { {0, 2394}, {0, 4788}, {0, 9577} },
Harinath Nampally605f72d2017-09-09 15:56:58 -04001316 /*
1317 * Although we enable the interrupt sources once and for
1318 * all here the event detection itself is not enabled until
1319 * userspace asks for it by mma8452_write_event_config()
1320 */
1321 .all_events = MMA8452_INT_DRDY |
1322 MMA8452_INT_TRANS |
1323 MMA8452_INT_FF_MT,
1324 .enabled_events = MMA8452_INT_TRANS |
1325 MMA8452_INT_FF_MT,
Martin Kepplinger244a93f2016-01-16 15:35:22 +01001326 },
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001327 [mma8452] = {
1328 .chip_id = MMA8452_DEVICE_ID,
1329 .channels = mma8452_channels,
1330 .num_channels = ARRAY_SIZE(mma8452_channels),
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001331 .mma_scales = { {0, 9577}, {0, 19154}, {0, 38307} },
Harinath Nampally605f72d2017-09-09 15:56:58 -04001332 /*
1333 * Although we enable the interrupt sources once and for
1334 * all here the event detection itself is not enabled until
1335 * userspace asks for it by mma8452_write_event_config()
1336 */
1337 .all_events = MMA8452_INT_DRDY |
1338 MMA8452_INT_TRANS |
1339 MMA8452_INT_FF_MT,
1340 .enabled_events = MMA8452_INT_TRANS |
1341 MMA8452_INT_FF_MT,
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001342 },
Martin Kepplingerc5ea1b582015-09-01 13:45:09 +02001343 [mma8453] = {
1344 .chip_id = MMA8453_DEVICE_ID,
1345 .channels = mma8453_channels,
1346 .num_channels = ARRAY_SIZE(mma8453_channels),
1347 .mma_scales = { {0, 38307}, {0, 76614}, {0, 153228} },
Harinath Nampally605f72d2017-09-09 15:56:58 -04001348 /*
1349 * Although we enable the interrupt sources once and for
1350 * all here the event detection itself is not enabled until
1351 * userspace asks for it by mma8452_write_event_config()
1352 */
1353 .all_events = MMA8452_INT_DRDY |
1354 MMA8452_INT_TRANS |
1355 MMA8452_INT_FF_MT,
1356 .enabled_events = MMA8452_INT_TRANS |
1357 MMA8452_INT_FF_MT,
Martin Kepplingerc5ea1b582015-09-01 13:45:09 +02001358 },
Martin Kepplinger417e0082015-09-01 13:45:11 +02001359 [mma8652] = {
1360 .chip_id = MMA8652_DEVICE_ID,
1361 .channels = mma8652_channels,
1362 .num_channels = ARRAY_SIZE(mma8652_channels),
1363 .mma_scales = { {0, 9577}, {0, 19154}, {0, 38307} },
Harinath Nampally605f72d2017-09-09 15:56:58 -04001364 .all_events = MMA8452_INT_DRDY |
1365 MMA8452_INT_FF_MT,
1366 .enabled_events = MMA8452_INT_FF_MT,
Martin Kepplinger417e0082015-09-01 13:45:11 +02001367 },
1368 [mma8653] = {
1369 .chip_id = MMA8653_DEVICE_ID,
1370 .channels = mma8653_channels,
1371 .num_channels = ARRAY_SIZE(mma8653_channels),
1372 .mma_scales = { {0, 38307}, {0, 76614}, {0, 153228} },
Harinath Nampally605f72d2017-09-09 15:56:58 -04001373 /*
1374 * Although we enable the interrupt sources once and for
1375 * all here the event detection itself is not enabled until
1376 * userspace asks for it by mma8452_write_event_config()
1377 */
1378 .all_events = MMA8452_INT_DRDY |
1379 MMA8452_INT_FF_MT,
1380 .enabled_events = MMA8452_INT_FF_MT,
Martin Kepplinger417e0082015-09-01 13:45:11 +02001381 },
Martin Kepplingere8731182016-03-09 12:01:29 +01001382 [fxls8471] = {
1383 .chip_id = FXLS8471_DEVICE_ID,
1384 .channels = mma8451_channels,
1385 .num_channels = ARRAY_SIZE(mma8451_channels),
1386 .mma_scales = { {0, 2394}, {0, 4788}, {0, 9577} },
Harinath Nampally605f72d2017-09-09 15:56:58 -04001387 /*
1388 * Although we enable the interrupt sources once and for
1389 * all here the event detection itself is not enabled until
1390 * userspace asks for it by mma8452_write_event_config()
1391 */
1392 .all_events = MMA8452_INT_DRDY |
1393 MMA8452_INT_TRANS |
1394 MMA8452_INT_FF_MT,
1395 .enabled_events = MMA8452_INT_TRANS |
1396 MMA8452_INT_FF_MT,
Martin Kepplingere8731182016-03-09 12:01:29 +01001397 },
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001398};
1399
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001400static struct attribute *mma8452_attributes[] = {
1401 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
1402 &iio_dev_attr_in_accel_scale_available.dev_attr.attr,
Martin Fuzzey1e798412015-06-01 15:39:56 +02001403 &iio_dev_attr_in_accel_filter_high_pass_3db_frequency_available.dev_attr.attr,
Martin Kepplingered859fc2016-04-25 14:08:25 +02001404 &iio_dev_attr_in_accel_oversampling_ratio_available.dev_attr.attr,
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001405 NULL
1406};
1407
1408static const struct attribute_group mma8452_group = {
1409 .attrs = mma8452_attributes,
1410};
1411
1412static const struct iio_info mma8452_info = {
1413 .attrs = &mma8452_group,
1414 .read_raw = &mma8452_read_raw,
1415 .write_raw = &mma8452_write_raw,
Martin Fuzzey28e34272015-06-01 15:39:52 +02001416 .event_attrs = &mma8452_event_attribute_group,
Harinath Nampally4febd9f2017-09-25 06:40:08 -04001417 .read_event_value = &mma8452_read_event_value,
1418 .write_event_value = &mma8452_write_event_value,
Martin Fuzzey28e34272015-06-01 15:39:52 +02001419 .read_event_config = &mma8452_read_event_config,
1420 .write_event_config = &mma8452_write_event_config,
Martin Fuzzey2a17698c2015-05-13 12:26:40 +02001421 .debugfs_reg_access = &mma8452_reg_access_dbg,
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001422};
1423
1424static const unsigned long mma8452_scan_masks[] = {0x7, 0};
1425
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001426static int mma8452_data_rdy_trigger_set_state(struct iio_trigger *trig,
1427 bool state)
1428{
1429 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1430 struct mma8452_data *data = iio_priv(indio_dev);
Martin Kepplinger96c0cb22016-03-03 09:24:03 +01001431 int reg, ret;
1432
1433 ret = mma8452_set_runtime_pm_state(data->client, state);
1434 if (ret)
1435 return ret;
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001436
1437 reg = i2c_smbus_read_byte_data(data->client, MMA8452_CTRL_REG4);
1438 if (reg < 0)
1439 return reg;
1440
1441 if (state)
1442 reg |= MMA8452_INT_DRDY;
1443 else
1444 reg &= ~MMA8452_INT_DRDY;
1445
1446 return mma8452_change_config(data, MMA8452_CTRL_REG4, reg);
1447}
1448
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001449static const struct iio_trigger_ops mma8452_trigger_ops = {
1450 .set_trigger_state = mma8452_data_rdy_trigger_set_state,
Lars-Peter Clausen19808e02016-09-23 17:19:42 +02001451 .validate_device = iio_trigger_validate_own_device,
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001452};
1453
1454static int mma8452_trigger_setup(struct iio_dev *indio_dev)
1455{
1456 struct mma8452_data *data = iio_priv(indio_dev);
1457 struct iio_trigger *trig;
1458 int ret;
1459
1460 trig = devm_iio_trigger_alloc(&data->client->dev, "%s-dev%d",
1461 indio_dev->name,
Jonathan Cameron15ea28782021-04-26 18:49:03 +01001462 iio_device_id(indio_dev));
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001463 if (!trig)
1464 return -ENOMEM;
1465
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001466 trig->ops = &mma8452_trigger_ops;
1467 iio_trigger_set_drvdata(trig, indio_dev);
1468
1469 ret = iio_trigger_register(trig);
1470 if (ret)
1471 return ret;
1472
Lars-Peter Clausencd008222021-10-24 11:26:59 +02001473 indio_dev->trig = iio_trigger_get(trig);
Hartmut Knaack686027f2015-08-02 22:43:51 +02001474
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001475 return 0;
1476}
1477
1478static void mma8452_trigger_cleanup(struct iio_dev *indio_dev)
1479{
1480 if (indio_dev->trig)
1481 iio_trigger_unregister(indio_dev->trig);
1482}
1483
Martin Fuzzeyecabae72015-05-13 12:26:38 +02001484static int mma8452_reset(struct i2c_client *client)
1485{
1486 int i;
1487 int ret;
1488
1489 ret = i2c_smbus_write_byte_data(client, MMA8452_CTRL_REG2,
1490 MMA8452_CTRL_REG2_RST);
1491 if (ret < 0)
1492 return ret;
1493
1494 for (i = 0; i < 10; i++) {
1495 usleep_range(100, 200);
1496 ret = i2c_smbus_read_byte_data(client, MMA8452_CTRL_REG2);
1497 if (ret == -EIO)
1498 continue; /* I2C comm reset */
1499 if (ret < 0)
1500 return ret;
1501 if (!(ret & MMA8452_CTRL_REG2_RST))
1502 return 0;
1503 }
1504
1505 return -ETIMEDOUT;
1506}
1507
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001508static const struct of_device_id mma8452_dt_ids[] = {
Martin Kepplinger244a93f2016-01-16 15:35:22 +01001509 { .compatible = "fsl,mma8451", .data = &mma_chip_info_table[mma8451] },
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001510 { .compatible = "fsl,mma8452", .data = &mma_chip_info_table[mma8452] },
Martin Kepplingerc5ea1b582015-09-01 13:45:09 +02001511 { .compatible = "fsl,mma8453", .data = &mma_chip_info_table[mma8453] },
Martin Kepplinger417e0082015-09-01 13:45:11 +02001512 { .compatible = "fsl,mma8652", .data = &mma_chip_info_table[mma8652] },
1513 { .compatible = "fsl,mma8653", .data = &mma_chip_info_table[mma8653] },
Martin Kepplingere8731182016-03-09 12:01:29 +01001514 { .compatible = "fsl,fxls8471", .data = &mma_chip_info_table[fxls8471] },
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001515 { }
1516};
1517MODULE_DEVICE_TABLE(of, mma8452_dt_ids);
1518
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001519static int mma8452_probe(struct i2c_client *client,
1520 const struct i2c_device_id *id)
1521{
1522 struct mma8452_data *data;
1523 struct iio_dev *indio_dev;
1524 int ret;
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001525 const struct of_device_id *match;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001526
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001527 match = of_match_device(mma8452_dt_ids, &client->dev);
1528 if (!match) {
1529 dev_err(&client->dev, "unknown device model\n");
1530 return -ENODEV;
1531 }
1532
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001533 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
1534 if (!indio_dev)
1535 return -ENOMEM;
1536
1537 data = iio_priv(indio_dev);
1538 data->client = client;
1539 mutex_init(&data->lock);
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001540 data->chip_info = match->data;
1541
Anson Huangf6ff49b2019-01-08 09:14:06 +00001542 data->vdd_reg = devm_regulator_get(&client->dev, "vdd");
Krzysztof Kozlowskic8b9a022020-08-29 08:47:10 +02001543 if (IS_ERR(data->vdd_reg))
1544 return dev_err_probe(&client->dev, PTR_ERR(data->vdd_reg),
1545 "failed to get VDD regulator!\n");
Anson Huangf6ff49b2019-01-08 09:14:06 +00001546
1547 data->vddio_reg = devm_regulator_get(&client->dev, "vddio");
Krzysztof Kozlowskic8b9a022020-08-29 08:47:10 +02001548 if (IS_ERR(data->vddio_reg))
1549 return dev_err_probe(&client->dev, PTR_ERR(data->vddio_reg),
1550 "failed to get VDDIO regulator!\n");
Anson Huangf6ff49b2019-01-08 09:14:06 +00001551
1552 ret = regulator_enable(data->vdd_reg);
1553 if (ret) {
1554 dev_err(&client->dev, "failed to enable VDD regulator!\n");
1555 return ret;
1556 }
1557
1558 ret = regulator_enable(data->vddio_reg);
1559 if (ret) {
1560 dev_err(&client->dev, "failed to enable VDDIO regulator!\n");
1561 goto disable_regulator_vdd;
1562 }
1563
Martin Kepplinger417e0082015-09-01 13:45:11 +02001564 ret = i2c_smbus_read_byte_data(client, MMA8452_WHO_AM_I);
1565 if (ret < 0)
Anson Huangf6ff49b2019-01-08 09:14:06 +00001566 goto disable_regulators;
Martin Kepplinger417e0082015-09-01 13:45:11 +02001567
1568 switch (ret) {
Martin Kepplinger244a93f2016-01-16 15:35:22 +01001569 case MMA8451_DEVICE_ID:
Martin Kepplinger417e0082015-09-01 13:45:11 +02001570 case MMA8452_DEVICE_ID:
1571 case MMA8453_DEVICE_ID:
1572 case MMA8652_DEVICE_ID:
1573 case MMA8653_DEVICE_ID:
Martin Kepplingere8731182016-03-09 12:01:29 +01001574 case FXLS8471_DEVICE_ID:
Martin Kepplinger417e0082015-09-01 13:45:11 +02001575 if (ret == data->chip_info->chip_id)
1576 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001577 fallthrough;
Martin Kepplinger417e0082015-09-01 13:45:11 +02001578 default:
Anson Huangf6ff49b2019-01-08 09:14:06 +00001579 ret = -ENODEV;
1580 goto disable_regulators;
Martin Kepplinger417e0082015-09-01 13:45:11 +02001581 }
1582
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001583 dev_info(&client->dev, "registering %s accelerometer; ID 0x%x\n",
1584 match->compatible, data->chip_info->chip_id);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001585
1586 i2c_set_clientdata(client, indio_dev);
1587 indio_dev->info = &mma8452_info;
1588 indio_dev->name = id->name;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001589 indio_dev->modes = INDIO_DIRECT_MODE;
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001590 indio_dev->channels = data->chip_info->channels;
1591 indio_dev->num_channels = data->chip_info->num_channels;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001592 indio_dev->available_scan_masks = mma8452_scan_masks;
1593
Martin Fuzzeyecabae72015-05-13 12:26:38 +02001594 ret = mma8452_reset(client);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001595 if (ret < 0)
Anson Huangf6ff49b2019-01-08 09:14:06 +00001596 goto disable_regulators;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001597
1598 data->data_cfg = MMA8452_DATA_CFG_FS_2G;
1599 ret = i2c_smbus_write_byte_data(client, MMA8452_DATA_CFG,
Hartmut Knaack686027f2015-08-02 22:43:51 +02001600 data->data_cfg);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001601 if (ret < 0)
Anson Huangf6ff49b2019-01-08 09:14:06 +00001602 goto disable_regulators;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001603
Martin Fuzzey28e34272015-06-01 15:39:52 +02001604 /*
1605 * By default set transient threshold to max to avoid events if
1606 * enabling without configuring threshold.
1607 */
1608 ret = i2c_smbus_write_byte_data(client, MMA8452_TRANSIENT_THS,
1609 MMA8452_TRANSIENT_THS_MASK);
1610 if (ret < 0)
Anson Huangf6ff49b2019-01-08 09:14:06 +00001611 goto disable_regulators;
Martin Fuzzey28e34272015-06-01 15:39:52 +02001612
1613 if (client->irq) {
Martin Kepplingerd2a3e092015-10-15 15:10:32 +02001614 int irq2;
Martin Fuzzey28e34272015-06-01 15:39:52 +02001615
Martin Kepplingerd2a3e092015-10-15 15:10:32 +02001616 irq2 = of_irq_get_byname(client->dev.of_node, "INT2");
1617
1618 if (irq2 == client->irq) {
1619 dev_dbg(&client->dev, "using interrupt line INT2\n");
1620 } else {
1621 ret = i2c_smbus_write_byte_data(client,
Harinath Nampally605f72d2017-09-09 15:56:58 -04001622 MMA8452_CTRL_REG5,
1623 data->chip_info->all_events);
Martin Kepplingerd2a3e092015-10-15 15:10:32 +02001624 if (ret < 0)
Anson Huangf6ff49b2019-01-08 09:14:06 +00001625 goto disable_regulators;
Martin Kepplingerd2a3e092015-10-15 15:10:32 +02001626
1627 dev_dbg(&client->dev, "using interrupt line INT1\n");
1628 }
Martin Fuzzey28e34272015-06-01 15:39:52 +02001629
1630 ret = i2c_smbus_write_byte_data(client,
Harinath Nampally605f72d2017-09-09 15:56:58 -04001631 MMA8452_CTRL_REG4,
1632 data->chip_info->enabled_events);
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001633 if (ret < 0)
Anson Huangf6ff49b2019-01-08 09:14:06 +00001634 goto disable_regulators;
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001635
1636 ret = mma8452_trigger_setup(indio_dev);
Martin Fuzzey28e34272015-06-01 15:39:52 +02001637 if (ret < 0)
Anson Huangf6ff49b2019-01-08 09:14:06 +00001638 goto disable_regulators;
Martin Fuzzey28e34272015-06-01 15:39:52 +02001639 }
1640
Martin Fuzzeyecabae72015-05-13 12:26:38 +02001641 data->ctrl_reg1 = MMA8452_CTRL_ACTIVE |
Hartmut Knaack686027f2015-08-02 22:43:51 +02001642 (MMA8452_CTRL_DR_DEFAULT << MMA8452_CTRL_DR_SHIFT);
Richard Tresiddera45d1232018-05-11 16:54:59 +08001643
1644 data->sleep_val = mma8452_calculate_sleep(data);
1645
Martin Fuzzeyecabae72015-05-13 12:26:38 +02001646 ret = i2c_smbus_write_byte_data(client, MMA8452_CTRL_REG1,
1647 data->ctrl_reg1);
1648 if (ret < 0)
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001649 goto trigger_cleanup;
Martin Fuzzeyecabae72015-05-13 12:26:38 +02001650
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001651 ret = iio_triggered_buffer_setup(indio_dev, NULL,
Hartmut Knaack686027f2015-08-02 22:43:51 +02001652 mma8452_trigger_handler, NULL);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001653 if (ret < 0)
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001654 goto trigger_cleanup;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001655
Martin Fuzzey28e34272015-06-01 15:39:52 +02001656 if (client->irq) {
1657 ret = devm_request_threaded_irq(&client->dev,
1658 client->irq,
1659 NULL, mma8452_interrupt,
1660 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1661 client->name, indio_dev);
1662 if (ret)
1663 goto buffer_cleanup;
1664 }
1665
Martin Kepplinger96c0cb22016-03-03 09:24:03 +01001666 ret = pm_runtime_set_active(&client->dev);
1667 if (ret < 0)
1668 goto buffer_cleanup;
1669
1670 pm_runtime_enable(&client->dev);
1671 pm_runtime_set_autosuspend_delay(&client->dev,
1672 MMA8452_AUTO_SUSPEND_DELAY_MS);
1673 pm_runtime_use_autosuspend(&client->dev);
1674
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001675 ret = iio_device_register(indio_dev);
1676 if (ret < 0)
1677 goto buffer_cleanup;
Martin Fuzzey28e34272015-06-01 15:39:52 +02001678
Martin Kepplinger4b042662016-01-16 15:35:20 +01001679 ret = mma8452_set_freefall_mode(data, false);
Bijosh Thykkoottathil1a965d42016-07-04 10:08:53 +00001680 if (ret < 0)
Chuhong Yuand7369ae2020-05-28 14:41:21 +08001681 goto unregister_device;
Martin Kepplinger4b042662016-01-16 15:35:20 +01001682
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001683 return 0;
1684
Chuhong Yuand7369ae2020-05-28 14:41:21 +08001685unregister_device:
1686 iio_device_unregister(indio_dev);
1687
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001688buffer_cleanup:
1689 iio_triggered_buffer_cleanup(indio_dev);
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001690
1691trigger_cleanup:
1692 mma8452_trigger_cleanup(indio_dev);
1693
Anson Huangf6ff49b2019-01-08 09:14:06 +00001694disable_regulators:
1695 regulator_disable(data->vddio_reg);
1696
1697disable_regulator_vdd:
1698 regulator_disable(data->vdd_reg);
1699
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001700 return ret;
1701}
1702
1703static int mma8452_remove(struct i2c_client *client)
1704{
1705 struct iio_dev *indio_dev = i2c_get_clientdata(client);
Anson Huangf6ff49b2019-01-08 09:14:06 +00001706 struct mma8452_data *data = iio_priv(indio_dev);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001707
1708 iio_device_unregister(indio_dev);
Martin Kepplinger96c0cb22016-03-03 09:24:03 +01001709
1710 pm_runtime_disable(&client->dev);
1711 pm_runtime_set_suspended(&client->dev);
Martin Kepplinger96c0cb22016-03-03 09:24:03 +01001712
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001713 iio_triggered_buffer_cleanup(indio_dev);
Martin Fuzzeyae6d9ce2015-06-01 15:39:58 +02001714 mma8452_trigger_cleanup(indio_dev);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001715 mma8452_standby(iio_priv(indio_dev));
1716
Anson Huangf6ff49b2019-01-08 09:14:06 +00001717 regulator_disable(data->vddio_reg);
1718 regulator_disable(data->vdd_reg);
1719
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001720 return 0;
1721}
1722
Martin Kepplinger96c0cb22016-03-03 09:24:03 +01001723#ifdef CONFIG_PM
1724static int mma8452_runtime_suspend(struct device *dev)
1725{
1726 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1727 struct mma8452_data *data = iio_priv(indio_dev);
1728 int ret;
1729
1730 mutex_lock(&data->lock);
1731 ret = mma8452_standby(data);
1732 mutex_unlock(&data->lock);
1733 if (ret < 0) {
1734 dev_err(&data->client->dev, "powering off device failed\n");
1735 return -EAGAIN;
1736 }
1737
Anson Huangf6ff49b2019-01-08 09:14:06 +00001738 ret = regulator_disable(data->vddio_reg);
1739 if (ret) {
1740 dev_err(dev, "failed to disable VDDIO regulator\n");
1741 return ret;
1742 }
1743
1744 ret = regulator_disable(data->vdd_reg);
1745 if (ret) {
1746 dev_err(dev, "failed to disable VDD regulator\n");
1747 return ret;
1748 }
1749
Martin Kepplinger96c0cb22016-03-03 09:24:03 +01001750 return 0;
1751}
1752
1753static int mma8452_runtime_resume(struct device *dev)
1754{
1755 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1756 struct mma8452_data *data = iio_priv(indio_dev);
1757 int ret, sleep_val;
1758
Anson Huangf6ff49b2019-01-08 09:14:06 +00001759 ret = regulator_enable(data->vdd_reg);
1760 if (ret) {
1761 dev_err(dev, "failed to enable VDD regulator\n");
1762 return ret;
1763 }
1764
1765 ret = regulator_enable(data->vddio_reg);
1766 if (ret) {
1767 dev_err(dev, "failed to enable VDDIO regulator\n");
1768 regulator_disable(data->vdd_reg);
1769 return ret;
1770 }
1771
Martin Kepplinger96c0cb22016-03-03 09:24:03 +01001772 ret = mma8452_active(data);
1773 if (ret < 0)
Anson Huangf6ff49b2019-01-08 09:14:06 +00001774 goto runtime_resume_failed;
Martin Kepplinger96c0cb22016-03-03 09:24:03 +01001775
1776 ret = mma8452_get_odr_index(data);
1777 sleep_val = 1000 / mma8452_samp_freq[ret][0];
1778 if (sleep_val < 20)
1779 usleep_range(sleep_val * 1000, 20000);
1780 else
1781 msleep_interruptible(sleep_val);
1782
1783 return 0;
Martin Kepplinger96c0cb22016-03-03 09:24:03 +01001784
Anson Huangf6ff49b2019-01-08 09:14:06 +00001785runtime_resume_failed:
1786 regulator_disable(data->vddio_reg);
1787 regulator_disable(data->vdd_reg);
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001788
Anson Huangf6ff49b2019-01-08 09:14:06 +00001789 return ret;
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001790}
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001791#endif
1792
Martin Kepplinger96c0cb22016-03-03 09:24:03 +01001793static const struct dev_pm_ops mma8452_pm_ops = {
Anson Huangf6ff49b2019-01-08 09:14:06 +00001794 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)
Martin Kepplinger96c0cb22016-03-03 09:24:03 +01001795 SET_RUNTIME_PM_OPS(mma8452_runtime_suspend,
1796 mma8452_runtime_resume, NULL)
1797};
1798
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001799static const struct i2c_device_id mma8452_id[] = {
Martin Kepplingerddb851a2016-03-14 12:33:14 +01001800 { "mma8451", mma8451 },
Martin Kepplingerc3cdd6e2015-09-01 13:45:08 +02001801 { "mma8452", mma8452 },
Martin Kepplingerc5ea1b582015-09-01 13:45:09 +02001802 { "mma8453", mma8453 },
Martin Kepplinger417e0082015-09-01 13:45:11 +02001803 { "mma8652", mma8652 },
1804 { "mma8653", mma8653 },
Martin Kepplingere8731182016-03-09 12:01:29 +01001805 { "fxls8471", fxls8471 },
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001806 { }
1807};
1808MODULE_DEVICE_TABLE(i2c, mma8452_id);
1809
1810static struct i2c_driver mma8452_driver = {
1811 .driver = {
1812 .name = "mma8452",
Martin Fuzzeya3fb96a2014-11-07 14:06:00 +00001813 .of_match_table = of_match_ptr(mma8452_dt_ids),
Martin Kepplinger96c0cb22016-03-03 09:24:03 +01001814 .pm = &mma8452_pm_ops,
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001815 },
1816 .probe = mma8452_probe,
1817 .remove = mma8452_remove,
1818 .id_table = mma8452_id,
1819};
1820module_i2c_driver(mma8452_driver);
1821
1822MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
Martin Kepplingerf26ab1a2016-06-03 14:51:52 +02001823MODULE_DESCRIPTION("Freescale / NXP MMA8452 accelerometer driver");
Peter Meerwaldc7eeea92014-02-05 09:51:00 +00001824MODULE_LICENSE("GPL");