kernel: 5.4: backport fxos8700 accel support from 5.5
[openwrt/openwrt.git] / target / linux / generic / backport-5.4 / 800-v5.5-iio-imu-Add-support-for-the-FXOS8700-IMU.patch
1 From 84e5ddd5c46ea3bf0cad670da32028994cad5936 Mon Sep 17 00:00:00 2001
2 From: Robert Jones <rjones@gateworks.com>
3 Date: Mon, 14 Oct 2019 11:49:21 -0700
4 Subject: [PATCH] iio: imu: Add support for the FXOS8700 IMU
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 FXOS8700CQ is a small, low-power, 3-axis linear accelerometer and 3-axis
10 magnetometer combined into a single package. The device features a
11 selectable I2C or point-to-point SPI serial interface with 14-bit
12 accelerometer and 16-bit magnetometer ADC resolution along with
13 smart-embedded functions.
14
15 FXOS8700CQ has dynamically selectable accelerationfull-scale ranges of
16 ±2 g/±4 g/±8 g and a fixed magnetic measurement range of ±1200 μT.
17 Output data rates (ODR) from 1.563 Hz to 800 Hz are selectable by the user
18 for each sensor. Interleaved magnetic and acceleration data is available
19 at ODR rates of up to 400 Hz. FXOS8700CQ is available in a plastic QFN
20 package and it is guaranteed to operate over the extended temperature
21 range of –40 °C to +85 °C.
22
23 TODO: Trigger and IRQ configuration support
24
25 Datasheet:
26 http://cache.freescale.com/files/sensors/doc/data_sheet/FXOS8700CQ.pdf
27
28 Signed-off-by: Robert Jones <rjones@gateworks.com>
29 Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
30 ---
31 drivers/iio/imu/Kconfig | 27 ++
32 drivers/iio/imu/Makefile | 5 +
33 drivers/iio/imu/fxos8700.h | 10 +
34 drivers/iio/imu/fxos8700_core.c | 649 ++++++++++++++++++++++++++++++++++++++++
35 drivers/iio/imu/fxos8700_i2c.c | 71 +++++
36 drivers/iio/imu/fxos8700_spi.c | 59 ++++
37 6 files changed, 821 insertions(+)
38 create mode 100644 drivers/iio/imu/fxos8700.h
39 create mode 100644 drivers/iio/imu/fxos8700_core.c
40 create mode 100644 drivers/iio/imu/fxos8700_i2c.c
41 create mode 100644 drivers/iio/imu/fxos8700_spi.c
42
43 diff --git a/drivers/iio/imu/Kconfig b/drivers/iio/imu/Kconfig
44 index f3c7282..60bb102 100644
45 --- a/drivers/iio/imu/Kconfig
46 +++ b/drivers/iio/imu/Kconfig
47 @@ -40,6 +40,33 @@ config ADIS16480
48
49 source "drivers/iio/imu/bmi160/Kconfig"
50
51 +config FXOS8700
52 + tristate
53 +
54 +config FXOS8700_I2C
55 + tristate "NXP FXOS8700 I2C driver"
56 + depends on I2C
57 + select FXOS8700
58 + select REGMAP_I2C
59 + help
60 + Say yes here to build support for the NXP FXOS8700 m+g combo
61 + sensor on I2C.
62 +
63 + This driver can also be built as a module. If so, the module will be
64 + called fxos8700_i2c.
65 +
66 +config FXOS8700_SPI
67 + tristate "NXP FXOS8700 SPI driver"
68 + depends on SPI
69 + select FXOS8700
70 + select REGMAP_SPI
71 + help
72 + Say yes here to build support for the NXP FXOS8700 m+g combo
73 + sensor on SPI.
74 +
75 + This driver can also be built as a module. If so, the module will be
76 + called fxos8700_spi.
77 +
78 config KMX61
79 tristate "Kionix KMX61 6-axis accelerometer and magnetometer"
80 depends on I2C
81 diff --git a/drivers/iio/imu/Makefile b/drivers/iio/imu/Makefile
82 index 4a69588..5237fd4 100644
83 --- a/drivers/iio/imu/Makefile
84 +++ b/drivers/iio/imu/Makefile
85 @@ -14,6 +14,11 @@ adis_lib-$(CONFIG_IIO_ADIS_LIB_BUFFER) += adis_buffer.o
86 obj-$(CONFIG_IIO_ADIS_LIB) += adis_lib.o
87
88 obj-y += bmi160/
89 +
90 +obj-$(CONFIG_FXOS8700) += fxos8700_core.o
91 +obj-$(CONFIG_FXOS8700_I2C) += fxos8700_i2c.o
92 +obj-$(CONFIG_FXOS8700_SPI) += fxos8700_spi.o
93 +
94 obj-y += inv_mpu6050/
95
96 obj-$(CONFIG_KMX61) += kmx61.o
97 diff --git a/drivers/iio/imu/fxos8700.h b/drivers/iio/imu/fxos8700.h
98 new file mode 100644
99 index 00000000..6dfb8d7
100 --- /dev/null
101 +++ b/drivers/iio/imu/fxos8700.h
102 @@ -0,0 +1,10 @@
103 +/* SPDX-License-Identifier: GPL-2.0 */
104 +#ifndef FXOS8700_H_
105 +#define FXOS8700_H_
106 +
107 +extern const struct regmap_config fxos8700_regmap_config;
108 +
109 +int fxos8700_core_probe(struct device *dev, struct regmap *regmap,
110 + const char *name, bool use_spi);
111 +
112 +#endif /* FXOS8700_H_ */
113 diff --git a/drivers/iio/imu/fxos8700_core.c b/drivers/iio/imu/fxos8700_core.c
114 new file mode 100644
115 index 00000000..7b47be4
116 --- /dev/null
117 +++ b/drivers/iio/imu/fxos8700_core.c
118 @@ -0,0 +1,649 @@
119 +// SPDX-License-Identifier: GPL-2.0
120 +/*
121 + * FXOS8700 - NXP IMU (accelerometer plus magnetometer)
122 + *
123 + * IIO core driver for FXOS8700, with support for I2C/SPI busses
124 + *
125 + * TODO: Buffer, trigger, and IRQ support
126 + */
127 +#include <linux/module.h>
128 +#include <linux/regmap.h>
129 +#include <linux/acpi.h>
130 +#include <linux/bitops.h>
131 +
132 +#include <linux/iio/iio.h>
133 +#include <linux/iio/sysfs.h>
134 +
135 +#include "fxos8700.h"
136 +
137 +/* Register Definitions */
138 +#define FXOS8700_STATUS 0x00
139 +#define FXOS8700_OUT_X_MSB 0x01
140 +#define FXOS8700_OUT_X_LSB 0x02
141 +#define FXOS8700_OUT_Y_MSB 0x03
142 +#define FXOS8700_OUT_Y_LSB 0x04
143 +#define FXOS8700_OUT_Z_MSB 0x05
144 +#define FXOS8700_OUT_Z_LSB 0x06
145 +#define FXOS8700_F_SETUP 0x09
146 +#define FXOS8700_TRIG_CFG 0x0a
147 +#define FXOS8700_SYSMOD 0x0b
148 +#define FXOS8700_INT_SOURCE 0x0c
149 +#define FXOS8700_WHO_AM_I 0x0d
150 +#define FXOS8700_XYZ_DATA_CFG 0x0e
151 +#define FXOS8700_HP_FILTER_CUTOFF 0x0f
152 +#define FXOS8700_PL_STATUS 0x10
153 +#define FXOS8700_PL_CFG 0x11
154 +#define FXOS8700_PL_COUNT 0x12
155 +#define FXOS8700_PL_BF_ZCOMP 0x13
156 +#define FXOS8700_PL_THS_REG 0x14
157 +#define FXOS8700_A_FFMT_CFG 0x15
158 +#define FXOS8700_A_FFMT_SRC 0x16
159 +#define FXOS8700_A_FFMT_THS 0x17
160 +#define FXOS8700_A_FFMT_COUNT 0x18
161 +#define FXOS8700_TRANSIENT_CFG 0x1d
162 +#define FXOS8700_TRANSIENT_SRC 0x1e
163 +#define FXOS8700_TRANSIENT_THS 0x1f
164 +#define FXOS8700_TRANSIENT_COUNT 0x20
165 +#define FXOS8700_PULSE_CFG 0x21
166 +#define FXOS8700_PULSE_SRC 0x22
167 +#define FXOS8700_PULSE_THSX 0x23
168 +#define FXOS8700_PULSE_THSY 0x24
169 +#define FXOS8700_PULSE_THSZ 0x25
170 +#define FXOS8700_PULSE_TMLT 0x26
171 +#define FXOS8700_PULSE_LTCY 0x27
172 +#define FXOS8700_PULSE_WIND 0x28
173 +#define FXOS8700_ASLP_COUNT 0x29
174 +#define FXOS8700_CTRL_REG1 0x2a
175 +#define FXOS8700_CTRL_REG2 0x2b
176 +#define FXOS8700_CTRL_REG3 0x2c
177 +#define FXOS8700_CTRL_REG4 0x2d
178 +#define FXOS8700_CTRL_REG5 0x2e
179 +#define FXOS8700_OFF_X 0x2f
180 +#define FXOS8700_OFF_Y 0x30
181 +#define FXOS8700_OFF_Z 0x31
182 +#define FXOS8700_M_DR_STATUS 0x32
183 +#define FXOS8700_M_OUT_X_MSB 0x33
184 +#define FXOS8700_M_OUT_X_LSB 0x34
185 +#define FXOS8700_M_OUT_Y_MSB 0x35
186 +#define FXOS8700_M_OUT_Y_LSB 0x36
187 +#define FXOS8700_M_OUT_Z_MSB 0x37
188 +#define FXOS8700_M_OUT_Z_LSB 0x38
189 +#define FXOS8700_CMP_X_MSB 0x39
190 +#define FXOS8700_CMP_X_LSB 0x3a
191 +#define FXOS8700_CMP_Y_MSB 0x3b
192 +#define FXOS8700_CMP_Y_LSB 0x3c
193 +#define FXOS8700_CMP_Z_MSB 0x3d
194 +#define FXOS8700_CMP_Z_LSB 0x3e
195 +#define FXOS8700_M_OFF_X_MSB 0x3f
196 +#define FXOS8700_M_OFF_X_LSB 0x40
197 +#define FXOS8700_M_OFF_Y_MSB 0x41
198 +#define FXOS8700_M_OFF_Y_LSB 0x42
199 +#define FXOS8700_M_OFF_Z_MSB 0x43
200 +#define FXOS8700_M_OFF_Z_LSB 0x44
201 +#define FXOS8700_MAX_X_MSB 0x45
202 +#define FXOS8700_MAX_X_LSB 0x46
203 +#define FXOS8700_MAX_Y_MSB 0x47
204 +#define FXOS8700_MAX_Y_LSB 0x48
205 +#define FXOS8700_MAX_Z_MSB 0x49
206 +#define FXOS8700_MAX_Z_LSB 0x4a
207 +#define FXOS8700_MIN_X_MSB 0x4b
208 +#define FXOS8700_MIN_X_LSB 0x4c
209 +#define FXOS8700_MIN_Y_MSB 0x4d
210 +#define FXOS8700_MIN_Y_LSB 0x4e
211 +#define FXOS8700_MIN_Z_MSB 0x4f
212 +#define FXOS8700_MIN_Z_LSB 0x50
213 +#define FXOS8700_TEMP 0x51
214 +#define FXOS8700_M_THS_CFG 0x52
215 +#define FXOS8700_M_THS_SRC 0x53
216 +#define FXOS8700_M_THS_X_MSB 0x54
217 +#define FXOS8700_M_THS_X_LSB 0x55
218 +#define FXOS8700_M_THS_Y_MSB 0x56
219 +#define FXOS8700_M_THS_Y_LSB 0x57
220 +#define FXOS8700_M_THS_Z_MSB 0x58
221 +#define FXOS8700_M_THS_Z_LSB 0x59
222 +#define FXOS8700_M_THS_COUNT 0x5a
223 +#define FXOS8700_M_CTRL_REG1 0x5b
224 +#define FXOS8700_M_CTRL_REG2 0x5c
225 +#define FXOS8700_M_CTRL_REG3 0x5d
226 +#define FXOS8700_M_INT_SRC 0x5e
227 +#define FXOS8700_A_VECM_CFG 0x5f
228 +#define FXOS8700_A_VECM_THS_MSB 0x60
229 +#define FXOS8700_A_VECM_THS_LSB 0x61
230 +#define FXOS8700_A_VECM_CNT 0x62
231 +#define FXOS8700_A_VECM_INITX_MSB 0x63
232 +#define FXOS8700_A_VECM_INITX_LSB 0x64
233 +#define FXOS8700_A_VECM_INITY_MSB 0x65
234 +#define FXOS8700_A_VECM_INITY_LSB 0x66
235 +#define FXOS8700_A_VECM_INITZ_MSB 0x67
236 +#define FXOS8700_A_VECM_INITZ_LSB 0x68
237 +#define FXOS8700_M_VECM_CFG 0x69
238 +#define FXOS8700_M_VECM_THS_MSB 0x6a
239 +#define FXOS8700_M_VECM_THS_LSB 0x6b
240 +#define FXOS8700_M_VECM_CNT 0x6c
241 +#define FXOS8700_M_VECM_INITX_MSB 0x6d
242 +#define FXOS8700_M_VECM_INITX_LSB 0x6e
243 +#define FXOS8700_M_VECM_INITY_MSB 0x6f
244 +#define FXOS8700_M_VECM_INITY_LSB 0x70
245 +#define FXOS8700_M_VECM_INITZ_MSB 0x71
246 +#define FXOS8700_M_VECM_INITZ_LSB 0x72
247 +#define FXOS8700_A_FFMT_THS_X_MSB 0x73
248 +#define FXOS8700_A_FFMT_THS_X_LSB 0x74
249 +#define FXOS8700_A_FFMT_THS_Y_MSB 0x75
250 +#define FXOS8700_A_FFMT_THS_Y_LSB 0x76
251 +#define FXOS8700_A_FFMT_THS_Z_MSB 0x77
252 +#define FXOS8700_A_FFMT_THS_Z_LSB 0x78
253 +#define FXOS8700_A_TRAN_INIT_MSB 0x79
254 +#define FXOS8700_A_TRAN_INIT_LSB_X 0x7a
255 +#define FXOS8700_A_TRAN_INIT_LSB_Y 0x7b
256 +#define FXOS8700_A_TRAN_INIT_LSB_Z 0x7d
257 +#define FXOS8700_TM_NVM_LOCK 0x7e
258 +#define FXOS8700_NVM_DATA0_35 0x80
259 +#define FXOS8700_NVM_DATA_BNK3 0xa4
260 +#define FXOS8700_NVM_DATA_BNK2 0xa5
261 +#define FXOS8700_NVM_DATA_BNK1 0xa6
262 +#define FXOS8700_NVM_DATA_BNK0 0xa7
263 +
264 +/* Bit definitions for FXOS8700_CTRL_REG1 */
265 +#define FXOS8700_CTRL_ODR_MSK 0x38
266 +#define FXOS8700_CTRL_ODR_MAX 0x00
267 +#define FXOS8700_CTRL_ODR_MIN GENMASK(4, 3)
268 +
269 +/* Bit definitions for FXOS8700_M_CTRL_REG1 */
270 +#define FXOS8700_HMS_MASK GENMASK(1, 0)
271 +#define FXOS8700_OS_MASK GENMASK(4, 2)
272 +
273 +/* Bit definitions for FXOS8700_M_CTRL_REG2 */
274 +#define FXOS8700_MAXMIN_RST BIT(2)
275 +#define FXOS8700_MAXMIN_DIS_THS BIT(3)
276 +#define FXOS8700_MAXMIN_DIS BIT(4)
277 +
278 +#define FXOS8700_ACTIVE 0x01
279 +#define FXOS8700_ACTIVE_MIN_USLEEP 4000 /* from table 6 in datasheet */
280 +
281 +#define FXOS8700_DEVICE_ID 0xC7
282 +#define FXOS8700_PRE_DEVICE_ID 0xC4
283 +#define FXOS8700_DATA_BUF_SIZE 3
284 +
285 +struct fxos8700_data {
286 + struct regmap *regmap;
287 + struct iio_trigger *trig;
288 + __be16 buf[FXOS8700_DATA_BUF_SIZE] ____cacheline_aligned;
289 +};
290 +
291 +/* Regmap info */
292 +static const struct regmap_range read_range[] = {
293 + {
294 + .range_min = FXOS8700_STATUS,
295 + .range_max = FXOS8700_A_FFMT_COUNT,
296 + }, {
297 + .range_min = FXOS8700_TRANSIENT_CFG,
298 + .range_max = FXOS8700_A_FFMT_THS_Z_LSB,
299 + },
300 +};
301 +
302 +static const struct regmap_range write_range[] = {
303 + {
304 + .range_min = FXOS8700_F_SETUP,
305 + .range_max = FXOS8700_TRIG_CFG,
306 + }, {
307 + .range_min = FXOS8700_XYZ_DATA_CFG,
308 + .range_max = FXOS8700_HP_FILTER_CUTOFF,
309 + }, {
310 + .range_min = FXOS8700_PL_CFG,
311 + .range_max = FXOS8700_A_FFMT_CFG,
312 + }, {
313 + .range_min = FXOS8700_A_FFMT_THS,
314 + .range_max = FXOS8700_TRANSIENT_CFG,
315 + }, {
316 + .range_min = FXOS8700_TRANSIENT_THS,
317 + .range_max = FXOS8700_PULSE_CFG,
318 + }, {
319 + .range_min = FXOS8700_PULSE_THSX,
320 + .range_max = FXOS8700_OFF_Z,
321 + }, {
322 + .range_min = FXOS8700_M_OFF_X_MSB,
323 + .range_max = FXOS8700_M_OFF_Z_LSB,
324 + }, {
325 + .range_min = FXOS8700_M_THS_CFG,
326 + .range_max = FXOS8700_M_THS_CFG,
327 + }, {
328 + .range_min = FXOS8700_M_THS_X_MSB,
329 + .range_max = FXOS8700_M_CTRL_REG3,
330 + }, {
331 + .range_min = FXOS8700_A_VECM_CFG,
332 + .range_max = FXOS8700_A_FFMT_THS_Z_LSB,
333 + },
334 +};
335 +
336 +static const struct regmap_access_table driver_read_table = {
337 + .yes_ranges = read_range,
338 + .n_yes_ranges = ARRAY_SIZE(read_range),
339 +};
340 +
341 +static const struct regmap_access_table driver_write_table = {
342 + .yes_ranges = write_range,
343 + .n_yes_ranges = ARRAY_SIZE(write_range),
344 +};
345 +
346 +const struct regmap_config fxos8700_regmap_config = {
347 + .reg_bits = 8,
348 + .val_bits = 8,
349 + .max_register = FXOS8700_NVM_DATA_BNK0,
350 + .rd_table = &driver_read_table,
351 + .wr_table = &driver_write_table,
352 +};
353 +EXPORT_SYMBOL(fxos8700_regmap_config);
354 +
355 +#define FXOS8700_CHANNEL(_type, _axis) { \
356 + .type = _type, \
357 + .modified = 1, \
358 + .channel2 = IIO_MOD_##_axis, \
359 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
360 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
361 + BIT(IIO_CHAN_INFO_SAMP_FREQ), \
362 +}
363 +
364 +enum fxos8700_accel_scale_bits {
365 + MODE_2G = 0,
366 + MODE_4G,
367 + MODE_8G,
368 +};
369 +
370 +/* scan indexes follow DATA register order */
371 +enum fxos8700_scan_axis {
372 + FXOS8700_SCAN_ACCEL_X = 0,
373 + FXOS8700_SCAN_ACCEL_Y,
374 + FXOS8700_SCAN_ACCEL_Z,
375 + FXOS8700_SCAN_MAGN_X,
376 + FXOS8700_SCAN_MAGN_Y,
377 + FXOS8700_SCAN_MAGN_Z,
378 + FXOS8700_SCAN_RHALL,
379 + FXOS8700_SCAN_TIMESTAMP,
380 +};
381 +
382 +enum fxos8700_sensor {
383 + FXOS8700_ACCEL = 0,
384 + FXOS8700_MAGN,
385 + FXOS8700_NUM_SENSORS /* must be last */
386 +};
387 +
388 +enum fxos8700_int_pin {
389 + FXOS8700_PIN_INT1,
390 + FXOS8700_PIN_INT2
391 +};
392 +
393 +struct fxos8700_scale {
394 + u8 bits;
395 + int uscale;
396 +};
397 +
398 +struct fxos8700_odr {
399 + u8 bits;
400 + int odr;
401 + int uodr;
402 +};
403 +
404 +static const struct fxos8700_scale fxos8700_accel_scale[] = {
405 + { MODE_2G, 244},
406 + { MODE_4G, 488},
407 + { MODE_8G, 976},
408 +};
409 +
410 +/*
411 + * Accellerometer and magnetometer have the same ODR options, set in the
412 + * CTRL_REG1 register. ODR is halved when using both sensors at once in
413 + * hybrid mode.
414 + */
415 +static const struct fxos8700_odr fxos8700_odr[] = {
416 + {0x00, 800, 0},
417 + {0x01, 400, 0},
418 + {0x02, 200, 0},
419 + {0x03, 100, 0},
420 + {0x04, 50, 0},
421 + {0x05, 12, 500000},
422 + {0x06, 6, 250000},
423 + {0x07, 1, 562500},
424 +};
425 +
426 +static const struct iio_chan_spec fxos8700_channels[] = {
427 + FXOS8700_CHANNEL(IIO_ACCEL, X),
428 + FXOS8700_CHANNEL(IIO_ACCEL, Y),
429 + FXOS8700_CHANNEL(IIO_ACCEL, Z),
430 + FXOS8700_CHANNEL(IIO_MAGN, X),
431 + FXOS8700_CHANNEL(IIO_MAGN, Y),
432 + FXOS8700_CHANNEL(IIO_MAGN, Z),
433 + IIO_CHAN_SOFT_TIMESTAMP(FXOS8700_SCAN_TIMESTAMP),
434 +};
435 +
436 +static enum fxos8700_sensor fxos8700_to_sensor(enum iio_chan_type iio_type)
437 +{
438 + switch (iio_type) {
439 + case IIO_ACCEL:
440 + return FXOS8700_ACCEL;
441 + case IIO_ANGL_VEL:
442 + return FXOS8700_MAGN;
443 + default:
444 + return -EINVAL;
445 + }
446 +}
447 +
448 +static int fxos8700_set_active_mode(struct fxos8700_data *data,
449 + enum fxos8700_sensor t, bool mode)
450 +{
451 + int ret;
452 +
453 + ret = regmap_write(data->regmap, FXOS8700_CTRL_REG1, mode);
454 + if (ret)
455 + return ret;
456 +
457 + usleep_range(FXOS8700_ACTIVE_MIN_USLEEP,
458 + FXOS8700_ACTIVE_MIN_USLEEP + 1000);
459 +
460 + return 0;
461 +}
462 +
463 +static int fxos8700_set_scale(struct fxos8700_data *data,
464 + enum fxos8700_sensor t, int uscale)
465 +{
466 + int i;
467 + static const int scale_num = ARRAY_SIZE(fxos8700_accel_scale);
468 + struct device *dev = regmap_get_device(data->regmap);
469 +
470 + if (t == FXOS8700_MAGN) {
471 + dev_err(dev, "Magnetometer scale is locked at 1200uT\n");
472 + return -EINVAL;
473 + }
474 +
475 + for (i = 0; i < scale_num; i++)
476 + if (fxos8700_accel_scale[i].uscale == uscale)
477 + break;
478 +
479 + if (i == scale_num)
480 + return -EINVAL;
481 +
482 + return regmap_write(data->regmap, FXOS8700_XYZ_DATA_CFG,
483 + fxos8700_accel_scale[i].bits);
484 +}
485 +
486 +static int fxos8700_get_scale(struct fxos8700_data *data,
487 + enum fxos8700_sensor t, int *uscale)
488 +{
489 + int i, ret, val;
490 + static const int scale_num = ARRAY_SIZE(fxos8700_accel_scale);
491 +
492 + if (t == FXOS8700_MAGN) {
493 + *uscale = 1200; /* Magnetometer is locked at 1200uT */
494 + return 0;
495 + }
496 +
497 + ret = regmap_read(data->regmap, FXOS8700_XYZ_DATA_CFG, &val);
498 + if (ret)
499 + return ret;
500 +
501 + for (i = 0; i < scale_num; i++) {
502 + if (fxos8700_accel_scale[i].bits == (val & 0x3)) {
503 + *uscale = fxos8700_accel_scale[i].uscale;
504 + return 0;
505 + }
506 + }
507 +
508 + return -EINVAL;
509 +}
510 +
511 +static int fxos8700_get_data(struct fxos8700_data *data, int chan_type,
512 + int axis, int *val)
513 +{
514 + u8 base, reg;
515 + int ret;
516 + enum fxos8700_sensor type = fxos8700_to_sensor(chan_type);
517 +
518 + base = type ? FXOS8700_OUT_X_MSB : FXOS8700_M_OUT_X_MSB;
519 +
520 + /* Block read 6 bytes of device output registers to avoid data loss */
521 + ret = regmap_bulk_read(data->regmap, base, data->buf,
522 + FXOS8700_DATA_BUF_SIZE);
523 + if (ret)
524 + return ret;
525 +
526 + /* Convert axis to buffer index */
527 + reg = axis - IIO_MOD_X;
528 +
529 + /* Convert to native endianness */
530 + *val = sign_extend32(be16_to_cpu(data->buf[reg]), 15);
531 +
532 + return 0;
533 +}
534 +
535 +static int fxos8700_set_odr(struct fxos8700_data *data, enum fxos8700_sensor t,
536 + int odr, int uodr)
537 +{
538 + int i, ret, val;
539 + bool active_mode;
540 + static const int odr_num = ARRAY_SIZE(fxos8700_odr);
541 +
542 + ret = regmap_read(data->regmap, FXOS8700_CTRL_REG1, &val);
543 + if (ret)
544 + return ret;
545 +
546 + active_mode = val & FXOS8700_ACTIVE;
547 +
548 + if (active_mode) {
549 + /*
550 + * The device must be in standby mode to change any of the
551 + * other fields within CTRL_REG1
552 + */
553 + ret = regmap_write(data->regmap, FXOS8700_CTRL_REG1,
554 + val & ~FXOS8700_ACTIVE);
555 + if (ret)
556 + return ret;
557 + }
558 +
559 + for (i = 0; i < odr_num; i++)
560 + if (fxos8700_odr[i].odr == odr && fxos8700_odr[i].uodr == uodr)
561 + break;
562 +
563 + if (i >= odr_num)
564 + return -EINVAL;
565 +
566 + return regmap_update_bits(data->regmap,
567 + FXOS8700_CTRL_REG1,
568 + FXOS8700_CTRL_ODR_MSK + FXOS8700_ACTIVE,
569 + fxos8700_odr[i].bits << 3 | active_mode);
570 +}
571 +
572 +static int fxos8700_get_odr(struct fxos8700_data *data, enum fxos8700_sensor t,
573 + int *odr, int *uodr)
574 +{
575 + int i, val, ret;
576 + static const int odr_num = ARRAY_SIZE(fxos8700_odr);
577 +
578 + ret = regmap_read(data->regmap, FXOS8700_CTRL_REG1, &val);
579 + if (ret)
580 + return ret;
581 +
582 + val &= FXOS8700_CTRL_ODR_MSK;
583 +
584 + for (i = 0; i < odr_num; i++)
585 + if (val == fxos8700_odr[i].bits)
586 + break;
587 +
588 + if (i >= odr_num)
589 + return -EINVAL;
590 +
591 + *odr = fxos8700_odr[i].odr;
592 + *uodr = fxos8700_odr[i].uodr;
593 +
594 + return 0;
595 +}
596 +
597 +static int fxos8700_read_raw(struct iio_dev *indio_dev,
598 + struct iio_chan_spec const *chan,
599 + int *val, int *val2, long mask)
600 +{
601 + int ret;
602 + struct fxos8700_data *data = iio_priv(indio_dev);
603 +
604 + switch (mask) {
605 + case IIO_CHAN_INFO_RAW:
606 + ret = fxos8700_get_data(data, chan->type, chan->channel2, val);
607 + if (ret)
608 + return ret;
609 + return IIO_VAL_INT;
610 + case IIO_CHAN_INFO_SCALE:
611 + *val = 0;
612 + ret = fxos8700_get_scale(data, fxos8700_to_sensor(chan->type),
613 + val2);
614 + return ret ? ret : IIO_VAL_INT_PLUS_MICRO;
615 + case IIO_CHAN_INFO_SAMP_FREQ:
616 + ret = fxos8700_get_odr(data, fxos8700_to_sensor(chan->type),
617 + val, val2);
618 + return ret ? ret : IIO_VAL_INT_PLUS_MICRO;
619 + default:
620 + return -EINVAL;
621 + }
622 +}
623 +
624 +static int fxos8700_write_raw(struct iio_dev *indio_dev,
625 + struct iio_chan_spec const *chan,
626 + int val, int val2, long mask)
627 +{
628 + struct fxos8700_data *data = iio_priv(indio_dev);
629 +
630 + switch (mask) {
631 + case IIO_CHAN_INFO_SCALE:
632 + return fxos8700_set_scale(data, fxos8700_to_sensor(chan->type),
633 + val2);
634 + case IIO_CHAN_INFO_SAMP_FREQ:
635 + return fxos8700_set_odr(data, fxos8700_to_sensor(chan->type),
636 + val, val2);
637 + default:
638 + return -EINVAL;
639 + }
640 +}
641 +
642 +static IIO_CONST_ATTR(in_accel_sampling_frequency_available,
643 + "1.5625 6.25 12.5 50 100 200 400 800");
644 +static IIO_CONST_ATTR(in_magn_sampling_frequency_available,
645 + "1.5625 6.25 12.5 50 100 200 400 800");
646 +static IIO_CONST_ATTR(in_accel_scale_available, "0.000244 0.000488 0.000976");
647 +static IIO_CONST_ATTR(in_magn_scale_available, "0.000001200");
648 +
649 +static struct attribute *fxos8700_attrs[] = {
650 + &iio_const_attr_in_accel_sampling_frequency_available.dev_attr.attr,
651 + &iio_const_attr_in_magn_sampling_frequency_available.dev_attr.attr,
652 + &iio_const_attr_in_accel_scale_available.dev_attr.attr,
653 + &iio_const_attr_in_magn_scale_available.dev_attr.attr,
654 + NULL,
655 +};
656 +
657 +static const struct attribute_group fxos8700_attrs_group = {
658 + .attrs = fxos8700_attrs,
659 +};
660 +
661 +static const struct iio_info fxos8700_info = {
662 + .read_raw = fxos8700_read_raw,
663 + .write_raw = fxos8700_write_raw,
664 + .attrs = &fxos8700_attrs_group,
665 +};
666 +
667 +static int fxos8700_chip_init(struct fxos8700_data *data, bool use_spi)
668 +{
669 + int ret;
670 + unsigned int val;
671 + struct device *dev = regmap_get_device(data->regmap);
672 +
673 + ret = regmap_read(data->regmap, FXOS8700_WHO_AM_I, &val);
674 + if (ret) {
675 + dev_err(dev, "Error reading chip id\n");
676 + return ret;
677 + }
678 + if (val != FXOS8700_DEVICE_ID && val != FXOS8700_PRE_DEVICE_ID) {
679 + dev_err(dev, "Wrong chip id, got %x expected %x or %x\n",
680 + val, FXOS8700_DEVICE_ID, FXOS8700_PRE_DEVICE_ID);
681 + return -ENODEV;
682 + }
683 +
684 + ret = fxos8700_set_active_mode(data, FXOS8700_ACCEL, true);
685 + if (ret)
686 + return ret;
687 +
688 + ret = fxos8700_set_active_mode(data, FXOS8700_MAGN, true);
689 + if (ret)
690 + return ret;
691 +
692 + /*
693 + * The device must be in standby mode to change any of the other fields
694 + * within CTRL_REG1
695 + */
696 + ret = regmap_write(data->regmap, FXOS8700_CTRL_REG1, 0x00);
697 + if (ret)
698 + return ret;
699 +
700 + /* Set max oversample ratio (OSR) and both devices active */
701 + ret = regmap_write(data->regmap, FXOS8700_M_CTRL_REG1,
702 + FXOS8700_HMS_MASK | FXOS8700_OS_MASK);
703 + if (ret)
704 + return ret;
705 +
706 + /* Disable and rst min/max measurements & threshold */
707 + ret = regmap_write(data->regmap, FXOS8700_M_CTRL_REG2,
708 + FXOS8700_MAXMIN_RST | FXOS8700_MAXMIN_DIS_THS |
709 + FXOS8700_MAXMIN_DIS);
710 + if (ret)
711 + return ret;
712 +
713 + /* Max ODR (800Hz individual or 400Hz hybrid), active mode */
714 + ret = regmap_write(data->regmap, FXOS8700_CTRL_REG1,
715 + FXOS8700_CTRL_ODR_MAX | FXOS8700_ACTIVE);
716 + if (ret)
717 + return ret;
718 +
719 + /* Set for max full-scale range (+/-8G) */
720 + return regmap_write(data->regmap, FXOS8700_XYZ_DATA_CFG, MODE_8G);
721 +}
722 +
723 +static void fxos8700_chip_uninit(void *data)
724 +{
725 + struct fxos8700_data *fxos8700_data = data;
726 +
727 + fxos8700_set_active_mode(fxos8700_data, FXOS8700_ACCEL, false);
728 + fxos8700_set_active_mode(fxos8700_data, FXOS8700_MAGN, false);
729 +}
730 +
731 +int fxos8700_core_probe(struct device *dev, struct regmap *regmap,
732 + const char *name, bool use_spi)
733 +{
734 + struct iio_dev *indio_dev;
735 + struct fxos8700_data *data;
736 + int ret;
737 +
738 + indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
739 + if (!indio_dev)
740 + return -ENOMEM;
741 +
742 + data = iio_priv(indio_dev);
743 + dev_set_drvdata(dev, indio_dev);
744 + data->regmap = regmap;
745 +
746 + ret = fxos8700_chip_init(data, use_spi);
747 + if (ret)
748 + return ret;
749 +
750 + ret = devm_add_action_or_reset(dev, fxos8700_chip_uninit, data);
751 + if (ret)
752 + return ret;
753 +
754 + indio_dev->dev.parent = dev;
755 + indio_dev->channels = fxos8700_channels;
756 + indio_dev->num_channels = ARRAY_SIZE(fxos8700_channels);
757 + indio_dev->name = name ? name : "fxos8700";
758 + indio_dev->modes = INDIO_DIRECT_MODE;
759 + indio_dev->info = &fxos8700_info;
760 +
761 + return devm_iio_device_register(dev, indio_dev);
762 +}
763 +EXPORT_SYMBOL_GPL(fxos8700_core_probe);
764 +
765 +MODULE_AUTHOR("Robert Jones <rjones@gateworks.com>");
766 +MODULE_DESCRIPTION("FXOS8700 6-Axis Acc and Mag Combo Sensor driver");
767 +MODULE_LICENSE("GPL v2");
768 diff --git a/drivers/iio/imu/fxos8700_i2c.c b/drivers/iio/imu/fxos8700_i2c.c
769 new file mode 100644
770 index 00000000..3ceb763
771 --- /dev/null
772 +++ b/drivers/iio/imu/fxos8700_i2c.c
773 @@ -0,0 +1,71 @@
774 +// SPDX-License-Identifier: GPL-2.0
775 +/*
776 + * FXOS8700 - NXP IMU, I2C bits
777 + *
778 + * 7-bit I2C slave address determined by SA1 and SA0 logic level
779 + * inputs represented in the following table:
780 + * SA1 | SA0 | Slave Address
781 + * 0 | 0 | 0x1E
782 + * 0 | 1 | 0x1D
783 + * 1 | 0 | 0x1C
784 + * 1 | 1 | 0x1F
785 + */
786 +#include <linux/acpi.h>
787 +#include <linux/i2c.h>
788 +#include <linux/module.h>
789 +#include <linux/mod_devicetable.h>
790 +#include <linux/regmap.h>
791 +
792 +#include "fxos8700.h"
793 +
794 +static int fxos8700_i2c_probe(struct i2c_client *client,
795 + const struct i2c_device_id *id)
796 +{
797 + struct regmap *regmap;
798 + const char *name = NULL;
799 +
800 + regmap = devm_regmap_init_i2c(client, &fxos8700_regmap_config);
801 + if (IS_ERR(regmap)) {
802 + dev_err(&client->dev, "Failed to register i2c regmap %d\n",
803 + (int)PTR_ERR(regmap));
804 + return PTR_ERR(regmap);
805 + }
806 +
807 + if (id)
808 + name = id->name;
809 +
810 + return fxos8700_core_probe(&client->dev, regmap, name, false);
811 +}
812 +
813 +static const struct i2c_device_id fxos8700_i2c_id[] = {
814 + {"fxos8700", 0},
815 + { }
816 +};
817 +MODULE_DEVICE_TABLE(i2c, fxos8700_i2c_id);
818 +
819 +static const struct acpi_device_id fxos8700_acpi_match[] = {
820 + {"FXOS8700", 0},
821 + { }
822 +};
823 +MODULE_DEVICE_TABLE(acpi, fxos8700_acpi_match);
824 +
825 +static const struct of_device_id fxos8700_of_match[] = {
826 + { .compatible = "nxp,fxos8700" },
827 + { }
828 +};
829 +MODULE_DEVICE_TABLE(of, fxos8700_of_match);
830 +
831 +static struct i2c_driver fxos8700_i2c_driver = {
832 + .driver = {
833 + .name = "fxos8700_i2c",
834 + .acpi_match_table = ACPI_PTR(fxos8700_acpi_match),
835 + .of_match_table = fxos8700_of_match,
836 + },
837 + .probe = fxos8700_i2c_probe,
838 + .id_table = fxos8700_i2c_id,
839 +};
840 +module_i2c_driver(fxos8700_i2c_driver);
841 +
842 +MODULE_AUTHOR("Robert Jones <rjones@gateworks.com>");
843 +MODULE_DESCRIPTION("FXOS8700 I2C driver");
844 +MODULE_LICENSE("GPL v2");
845 diff --git a/drivers/iio/imu/fxos8700_spi.c b/drivers/iio/imu/fxos8700_spi.c
846 new file mode 100644
847 index 00000000..57e7bb6
848 --- /dev/null
849 +++ b/drivers/iio/imu/fxos8700_spi.c
850 @@ -0,0 +1,59 @@
851 +// SPDX-License-Identifier: GPL-2.0
852 +/*
853 + * FXOS8700 - NXP IMU, SPI bits
854 + */
855 +#include <linux/acpi.h>
856 +#include <linux/module.h>
857 +#include <linux/mod_devicetable.h>
858 +#include <linux/regmap.h>
859 +#include <linux/spi/spi.h>
860 +
861 +#include "fxos8700.h"
862 +
863 +static int fxos8700_spi_probe(struct spi_device *spi)
864 +{
865 + struct regmap *regmap;
866 + const struct spi_device_id *id = spi_get_device_id(spi);
867 +
868 + regmap = devm_regmap_init_spi(spi, &fxos8700_regmap_config);
869 + if (IS_ERR(regmap)) {
870 + dev_err(&spi->dev, "Failed to register spi regmap %d\n",
871 + (int)PTR_ERR(regmap));
872 + return PTR_ERR(regmap);
873 + }
874 +
875 + return fxos8700_core_probe(&spi->dev, regmap, id->name, true);
876 +}
877 +
878 +static const struct spi_device_id fxos8700_spi_id[] = {
879 + {"fxos8700", 0},
880 + { }
881 +};
882 +MODULE_DEVICE_TABLE(spi, fxos8700_spi_id);
883 +
884 +static const struct acpi_device_id fxos8700_acpi_match[] = {
885 + {"FXOS8700", 0},
886 + { }
887 +};
888 +MODULE_DEVICE_TABLE(acpi, fxos8700_acpi_match);
889 +
890 +static const struct of_device_id fxos8700_of_match[] = {
891 + { .compatible = "nxp,fxos8700" },
892 + { }
893 +};
894 +MODULE_DEVICE_TABLE(of, fxos8700_of_match);
895 +
896 +static struct spi_driver fxos8700_spi_driver = {
897 + .probe = fxos8700_spi_probe,
898 + .id_table = fxos8700_spi_id,
899 + .driver = {
900 + .acpi_match_table = ACPI_PTR(fxos8700_acpi_match),
901 + .of_match_table = fxos8700_of_match,
902 + .name = "fxos8700_spi",
903 + },
904 +};
905 +module_spi_driver(fxos8700_spi_driver);
906 +
907 +MODULE_AUTHOR("Robert Jones <rjones@gateworks.com>");
908 +MODULE_DESCRIPTION("FXOS8700 SPI driver");
909 +MODULE_LICENSE("GPL v2");
910 --
911 2.7.4
912