1 // SPDX-License-Identifier: GPL-2.0
3 * NXP FXLS8962AF/FXLS8964AF Accelerometer Core Driver
5 * Copyright 2021 Connected Cars A/S
8 * https://www.nxp.com/docs/en/data-sheet/FXLS8962AF.pdf
9 * https://www.nxp.com/docs/en/data-sheet/FXLS8964AF.pdf
12 * https://www.nxp.com/docs/en/errata/ES_FXLS8962AF.pdf
15 #include <linux/bits.h>
16 #include <linux/bitfield.h>
17 #include <linux/i2c.h>
18 #include <linux/irq.h>
19 #include <linux/module.h>
20 #include <linux/mod_devicetable.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/property.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/regmap.h>
25 #include <linux/types.h>
27 #include <linux/iio/buffer.h>
28 #include <linux/iio/events.h>
29 #include <linux/iio/iio.h>
30 #include <linux/iio/kfifo_buf.h>
31 #include <linux/iio/sysfs.h>
33 #include "fxls8962af.h"
35 #define FXLS8962AF_INT_STATUS 0x00
36 #define FXLS8962AF_INT_STATUS_SRC_BOOT BIT(0)
37 #define FXLS8962AF_INT_STATUS_SRC_SDCD_OT BIT(4)
38 #define FXLS8962AF_INT_STATUS_SRC_BUF BIT(5)
39 #define FXLS8962AF_INT_STATUS_SRC_DRDY BIT(7)
40 #define FXLS8962AF_TEMP_OUT 0x01
41 #define FXLS8962AF_VECM_LSB 0x02
42 #define FXLS8962AF_OUT_X_LSB 0x04
43 #define FXLS8962AF_OUT_Y_LSB 0x06
44 #define FXLS8962AF_OUT_Z_LSB 0x08
45 #define FXLS8962AF_BUF_STATUS 0x0b
46 #define FXLS8962AF_BUF_STATUS_BUF_CNT GENMASK(5, 0)
47 #define FXLS8962AF_BUF_STATUS_BUF_OVF BIT(6)
48 #define FXLS8962AF_BUF_STATUS_BUF_WMRK BIT(7)
49 #define FXLS8962AF_BUF_X_LSB 0x0c
50 #define FXLS8962AF_BUF_Y_LSB 0x0e
51 #define FXLS8962AF_BUF_Z_LSB 0x10
53 #define FXLS8962AF_PROD_REV 0x12
54 #define FXLS8962AF_WHO_AM_I 0x13
56 #define FXLS8962AF_SYS_MODE 0x14
57 #define FXLS8962AF_SENS_CONFIG1 0x15
58 #define FXLS8962AF_SENS_CONFIG1_ACTIVE BIT(0)
59 #define FXLS8962AF_SENS_CONFIG1_RST BIT(7)
60 #define FXLS8962AF_SC1_FSR_MASK GENMASK(2, 1)
61 #define FXLS8962AF_SC1_FSR_PREP(x) FIELD_PREP(FXLS8962AF_SC1_FSR_MASK, (x))
62 #define FXLS8962AF_SC1_FSR_GET(x) FIELD_GET(FXLS8962AF_SC1_FSR_MASK, (x))
64 #define FXLS8962AF_SENS_CONFIG2 0x16
65 #define FXLS8962AF_SENS_CONFIG3 0x17
66 #define FXLS8962AF_SC3_WAKE_ODR_MASK GENMASK(7, 4)
67 #define FXLS8962AF_SC3_WAKE_ODR_PREP(x) FIELD_PREP(FXLS8962AF_SC3_WAKE_ODR_MASK, (x))
68 #define FXLS8962AF_SC3_WAKE_ODR_GET(x) FIELD_GET(FXLS8962AF_SC3_WAKE_ODR_MASK, (x))
69 #define FXLS8962AF_SENS_CONFIG4 0x18
70 #define FXLS8962AF_SC4_INT_PP_OD_MASK BIT(1)
71 #define FXLS8962AF_SC4_INT_PP_OD_PREP(x) FIELD_PREP(FXLS8962AF_SC4_INT_PP_OD_MASK, (x))
72 #define FXLS8962AF_SC4_INT_POL_MASK BIT(0)
73 #define FXLS8962AF_SC4_INT_POL_PREP(x) FIELD_PREP(FXLS8962AF_SC4_INT_POL_MASK, (x))
74 #define FXLS8962AF_SENS_CONFIG5 0x19
76 #define FXLS8962AF_WAKE_IDLE_LSB 0x1b
77 #define FXLS8962AF_SLEEP_IDLE_LSB 0x1c
78 #define FXLS8962AF_ASLP_COUNT_LSB 0x1e
80 #define FXLS8962AF_INT_EN 0x20
81 #define FXLS8962AF_INT_EN_SDCD_OT_EN BIT(5)
82 #define FXLS8962AF_INT_EN_BUF_EN BIT(6)
83 #define FXLS8962AF_INT_PIN_SEL 0x21
84 #define FXLS8962AF_INT_PIN_SEL_MASK GENMASK(7, 0)
85 #define FXLS8962AF_INT_PIN_SEL_INT1 0x00
86 #define FXLS8962AF_INT_PIN_SEL_INT2 GENMASK(7, 0)
88 #define FXLS8962AF_OFF_X 0x22
89 #define FXLS8962AF_OFF_Y 0x23
90 #define FXLS8962AF_OFF_Z 0x24
92 #define FXLS8962AF_BUF_CONFIG1 0x26
93 #define FXLS8962AF_BC1_BUF_MODE_MASK GENMASK(6, 5)
94 #define FXLS8962AF_BC1_BUF_MODE_PREP(x) FIELD_PREP(FXLS8962AF_BC1_BUF_MODE_MASK, (x))
95 #define FXLS8962AF_BUF_CONFIG2 0x27
96 #define FXLS8962AF_BUF_CONFIG2_BUF_WMRK GENMASK(5, 0)
98 #define FXLS8962AF_ORIENT_STATUS 0x28
99 #define FXLS8962AF_ORIENT_CONFIG 0x29
100 #define FXLS8962AF_ORIENT_DBCOUNT 0x2a
101 #define FXLS8962AF_ORIENT_BF_ZCOMP 0x2b
102 #define FXLS8962AF_ORIENT_THS_REG 0x2c
104 #define FXLS8962AF_SDCD_INT_SRC1 0x2d
105 #define FXLS8962AF_SDCD_INT_SRC1_X_OT BIT(5)
106 #define FXLS8962AF_SDCD_INT_SRC1_X_POL BIT(4)
107 #define FXLS8962AF_SDCD_INT_SRC1_Y_OT BIT(3)
108 #define FXLS8962AF_SDCD_INT_SRC1_Y_POL BIT(2)
109 #define FXLS8962AF_SDCD_INT_SRC1_Z_OT BIT(1)
110 #define FXLS8962AF_SDCD_INT_SRC1_Z_POL BIT(0)
111 #define FXLS8962AF_SDCD_INT_SRC2 0x2e
112 #define FXLS8962AF_SDCD_CONFIG1 0x2f
113 #define FXLS8962AF_SDCD_CONFIG1_Z_OT_EN BIT(3)
114 #define FXLS8962AF_SDCD_CONFIG1_Y_OT_EN BIT(4)
115 #define FXLS8962AF_SDCD_CONFIG1_X_OT_EN BIT(5)
116 #define FXLS8962AF_SDCD_CONFIG1_OT_ELE BIT(7)
117 #define FXLS8962AF_SDCD_CONFIG2 0x30
118 #define FXLS8962AF_SDCD_CONFIG2_SDCD_EN BIT(7)
119 #define FXLS8962AF_SC2_REF_UPDM_AC GENMASK(6, 5)
120 #define FXLS8962AF_SDCD_OT_DBCNT 0x31
121 #define FXLS8962AF_SDCD_WT_DBCNT 0x32
122 #define FXLS8962AF_SDCD_LTHS_LSB 0x33
123 #define FXLS8962AF_SDCD_UTHS_LSB 0x35
125 #define FXLS8962AF_SELF_TEST_CONFIG1 0x37
126 #define FXLS8962AF_SELF_TEST_CONFIG2 0x38
128 #define FXLS8962AF_MAX_REG 0x38
130 #define FXLS8962AF_DEVICE_ID 0x62
131 #define FXLS8964AF_DEVICE_ID 0x84
133 /* Raw temp channel offset */
134 #define FXLS8962AF_TEMP_CENTER_VAL 25
136 #define FXLS8962AF_AUTO_SUSPEND_DELAY_MS 2000
138 #define FXLS8962AF_FIFO_LENGTH 32
139 #define FXLS8962AF_SCALE_TABLE_LEN 4
140 #define FXLS8962AF_SAMP_FREQ_TABLE_LEN 13
142 static const int fxls8962af_scale_table
[FXLS8962AF_SCALE_TABLE_LEN
][2] = {
143 {0, IIO_G_TO_M_S_2(980000)},
144 {0, IIO_G_TO_M_S_2(1950000)},
145 {0, IIO_G_TO_M_S_2(3910000)},
146 {0, IIO_G_TO_M_S_2(7810000)},
149 static const int fxls8962af_samp_freq_table
[FXLS8962AF_SAMP_FREQ_TABLE_LEN
][2] = {
150 {3200, 0}, {1600, 0}, {800, 0}, {400, 0}, {200, 0}, {100, 0},
151 {50, 0}, {25, 0}, {12, 500000}, {6, 250000}, {3, 125000},
152 {1, 563000}, {0, 781000},
155 struct fxls8962af_chip_info
{
157 const struct iio_chan_spec
*channels
;
162 struct fxls8962af_data
{
163 struct regmap
*regmap
;
164 const struct fxls8962af_chip_info
*chip_info
;
169 int64_t timestamp
, old_timestamp
; /* Only used in hw fifo mode. */
170 struct iio_mount_matrix orientation
;
178 const struct regmap_config fxls8962af_i2c_regmap_conf
= {
181 .max_register
= FXLS8962AF_MAX_REG
,
183 EXPORT_SYMBOL_NS_GPL(fxls8962af_i2c_regmap_conf
, "IIO_FXLS8962AF");
185 const struct regmap_config fxls8962af_spi_regmap_conf
= {
189 .max_register
= FXLS8962AF_MAX_REG
,
191 EXPORT_SYMBOL_NS_GPL(fxls8962af_spi_regmap_conf
, "IIO_FXLS8962AF");
200 enum fxls8962af_int_pin
{
205 static int fxls8962af_power_on(struct fxls8962af_data
*data
)
207 struct device
*dev
= regmap_get_device(data
->regmap
);
210 ret
= pm_runtime_resume_and_get(dev
);
212 dev_err(dev
, "failed to power on\n");
217 static int fxls8962af_power_off(struct fxls8962af_data
*data
)
219 struct device
*dev
= regmap_get_device(data
->regmap
);
222 pm_runtime_mark_last_busy(dev
);
223 ret
= pm_runtime_put_autosuspend(dev
);
225 dev_err(dev
, "failed to power off\n");
230 static int fxls8962af_standby(struct fxls8962af_data
*data
)
232 return regmap_clear_bits(data
->regmap
, FXLS8962AF_SENS_CONFIG1
,
233 FXLS8962AF_SENS_CONFIG1_ACTIVE
);
236 static int fxls8962af_active(struct fxls8962af_data
*data
)
238 return regmap_update_bits(data
->regmap
, FXLS8962AF_SENS_CONFIG1
,
239 FXLS8962AF_SENS_CONFIG1_ACTIVE
, 1);
242 static int fxls8962af_is_active(struct fxls8962af_data
*data
)
247 ret
= regmap_read(data
->regmap
, FXLS8962AF_SENS_CONFIG1
, ®
);
251 return reg
& FXLS8962AF_SENS_CONFIG1_ACTIVE
;
254 static int fxls8962af_get_out(struct fxls8962af_data
*data
,
255 struct iio_chan_spec
const *chan
, int *val
)
257 struct device
*dev
= regmap_get_device(data
->regmap
);
262 is_active
= fxls8962af_is_active(data
);
264 ret
= fxls8962af_power_on(data
);
269 ret
= regmap_bulk_read(data
->regmap
, chan
->address
,
270 &raw_val
, sizeof(data
->lower_thres
));
273 fxls8962af_power_off(data
);
276 dev_err(dev
, "failed to get out reg 0x%lx\n", chan
->address
);
280 *val
= sign_extend32(le16_to_cpu(raw_val
),
281 chan
->scan_type
.realbits
- 1);
286 static int fxls8962af_read_avail(struct iio_dev
*indio_dev
,
287 struct iio_chan_spec
const *chan
,
288 const int **vals
, int *type
, int *length
,
292 case IIO_CHAN_INFO_SCALE
:
293 *type
= IIO_VAL_INT_PLUS_NANO
;
294 *vals
= (int *)fxls8962af_scale_table
;
295 *length
= ARRAY_SIZE(fxls8962af_scale_table
) * 2;
296 return IIO_AVAIL_LIST
;
297 case IIO_CHAN_INFO_SAMP_FREQ
:
298 *type
= IIO_VAL_INT_PLUS_MICRO
;
299 *vals
= (int *)fxls8962af_samp_freq_table
;
300 *length
= ARRAY_SIZE(fxls8962af_samp_freq_table
) * 2;
301 return IIO_AVAIL_LIST
;
307 static int fxls8962af_write_raw_get_fmt(struct iio_dev
*indio_dev
,
308 struct iio_chan_spec
const *chan
,
312 case IIO_CHAN_INFO_SCALE
:
313 return IIO_VAL_INT_PLUS_NANO
;
314 case IIO_CHAN_INFO_SAMP_FREQ
:
315 return IIO_VAL_INT_PLUS_MICRO
;
317 return IIO_VAL_INT_PLUS_NANO
;
321 static int fxls8962af_update_config(struct fxls8962af_data
*data
, u8 reg
,
327 is_active
= fxls8962af_is_active(data
);
329 ret
= fxls8962af_standby(data
);
334 ret
= regmap_update_bits(data
->regmap
, reg
, mask
, val
);
339 ret
= fxls8962af_active(data
);
347 static int fxls8962af_set_full_scale(struct fxls8962af_data
*data
, u32 scale
)
351 for (i
= 0; i
< ARRAY_SIZE(fxls8962af_scale_table
); i
++)
352 if (scale
== fxls8962af_scale_table
[i
][1])
355 if (i
== ARRAY_SIZE(fxls8962af_scale_table
))
358 return fxls8962af_update_config(data
, FXLS8962AF_SENS_CONFIG1
,
359 FXLS8962AF_SC1_FSR_MASK
,
360 FXLS8962AF_SC1_FSR_PREP(i
));
363 static unsigned int fxls8962af_read_full_scale(struct fxls8962af_data
*data
,
370 ret
= regmap_read(data
->regmap
, FXLS8962AF_SENS_CONFIG1
, ®
);
374 range_idx
= FXLS8962AF_SC1_FSR_GET(reg
);
376 *val
= fxls8962af_scale_table
[range_idx
][1];
378 return IIO_VAL_INT_PLUS_NANO
;
381 static int fxls8962af_set_samp_freq(struct fxls8962af_data
*data
, u32 val
,
386 for (i
= 0; i
< ARRAY_SIZE(fxls8962af_samp_freq_table
); i
++)
387 if (val
== fxls8962af_samp_freq_table
[i
][0] &&
388 val2
== fxls8962af_samp_freq_table
[i
][1])
391 if (i
== ARRAY_SIZE(fxls8962af_samp_freq_table
))
394 return fxls8962af_update_config(data
, FXLS8962AF_SENS_CONFIG3
,
395 FXLS8962AF_SC3_WAKE_ODR_MASK
,
396 FXLS8962AF_SC3_WAKE_ODR_PREP(i
));
399 static unsigned int fxls8962af_read_samp_freq(struct fxls8962af_data
*data
,
406 ret
= regmap_read(data
->regmap
, FXLS8962AF_SENS_CONFIG3
, ®
);
410 range_idx
= FXLS8962AF_SC3_WAKE_ODR_GET(reg
);
412 *val
= fxls8962af_samp_freq_table
[range_idx
][0];
413 *val2
= fxls8962af_samp_freq_table
[range_idx
][1];
415 return IIO_VAL_INT_PLUS_MICRO
;
418 static int fxls8962af_read_raw(struct iio_dev
*indio_dev
,
419 struct iio_chan_spec
const *chan
,
420 int *val
, int *val2
, long mask
)
422 struct fxls8962af_data
*data
= iio_priv(indio_dev
);
425 case IIO_CHAN_INFO_RAW
:
426 switch (chan
->type
) {
429 return fxls8962af_get_out(data
, chan
, val
);
433 case IIO_CHAN_INFO_OFFSET
:
434 if (chan
->type
!= IIO_TEMP
)
437 *val
= FXLS8962AF_TEMP_CENTER_VAL
;
439 case IIO_CHAN_INFO_SCALE
:
441 return fxls8962af_read_full_scale(data
, val2
);
442 case IIO_CHAN_INFO_SAMP_FREQ
:
443 return fxls8962af_read_samp_freq(data
, val
, val2
);
449 static int fxls8962af_write_raw(struct iio_dev
*indio_dev
,
450 struct iio_chan_spec
const *chan
,
451 int val
, int val2
, long mask
)
453 struct fxls8962af_data
*data
= iio_priv(indio_dev
);
457 case IIO_CHAN_INFO_SCALE
:
461 ret
= iio_device_claim_direct_mode(indio_dev
);
465 ret
= fxls8962af_set_full_scale(data
, val2
);
467 iio_device_release_direct_mode(indio_dev
);
469 case IIO_CHAN_INFO_SAMP_FREQ
:
470 ret
= iio_device_claim_direct_mode(indio_dev
);
474 ret
= fxls8962af_set_samp_freq(data
, val
, val2
);
476 iio_device_release_direct_mode(indio_dev
);
483 static int fxls8962af_event_setup(struct fxls8962af_data
*data
, int state
)
485 /* Enable wakeup interrupt */
486 int mask
= FXLS8962AF_INT_EN_SDCD_OT_EN
;
487 int value
= state
? mask
: 0;
489 return regmap_update_bits(data
->regmap
, FXLS8962AF_INT_EN
, mask
, value
);
492 static int fxls8962af_set_watermark(struct iio_dev
*indio_dev
, unsigned val
)
494 struct fxls8962af_data
*data
= iio_priv(indio_dev
);
496 if (val
> FXLS8962AF_FIFO_LENGTH
)
497 val
= FXLS8962AF_FIFO_LENGTH
;
499 data
->watermark
= val
;
504 static int __fxls8962af_set_thresholds(struct fxls8962af_data
*data
,
505 const struct iio_chan_spec
*chan
,
506 enum iio_event_direction dir
,
510 case IIO_EV_DIR_FALLING
:
511 data
->lower_thres
= val
;
512 return regmap_bulk_write(data
->regmap
, FXLS8962AF_SDCD_LTHS_LSB
,
513 &data
->lower_thres
, sizeof(data
->lower_thres
));
514 case IIO_EV_DIR_RISING
:
515 data
->upper_thres
= val
;
516 return regmap_bulk_write(data
->regmap
, FXLS8962AF_SDCD_UTHS_LSB
,
517 &data
->upper_thres
, sizeof(data
->upper_thres
));
523 static int fxls8962af_read_event(struct iio_dev
*indio_dev
,
524 const struct iio_chan_spec
*chan
,
525 enum iio_event_type type
,
526 enum iio_event_direction dir
,
527 enum iio_event_info info
,
530 struct fxls8962af_data
*data
= iio_priv(indio_dev
);
533 if (type
!= IIO_EV_TYPE_THRESH
)
537 case IIO_EV_DIR_FALLING
:
538 ret
= regmap_bulk_read(data
->regmap
, FXLS8962AF_SDCD_LTHS_LSB
,
539 &data
->lower_thres
, sizeof(data
->lower_thres
));
543 *val
= sign_extend32(data
->lower_thres
, chan
->scan_type
.realbits
- 1);
545 case IIO_EV_DIR_RISING
:
546 ret
= regmap_bulk_read(data
->regmap
, FXLS8962AF_SDCD_UTHS_LSB
,
547 &data
->upper_thres
, sizeof(data
->upper_thres
));
551 *val
= sign_extend32(data
->upper_thres
, chan
->scan_type
.realbits
- 1);
558 static int fxls8962af_write_event(struct iio_dev
*indio_dev
,
559 const struct iio_chan_spec
*chan
,
560 enum iio_event_type type
,
561 enum iio_event_direction dir
,
562 enum iio_event_info info
,
565 struct fxls8962af_data
*data
= iio_priv(indio_dev
);
568 if (type
!= IIO_EV_TYPE_THRESH
)
571 if (val
< -2048 || val
> 2047)
574 if (data
->enable_event
)
577 val_masked
= val
& GENMASK(11, 0);
578 if (fxls8962af_is_active(data
)) {
579 ret
= fxls8962af_standby(data
);
583 ret
= __fxls8962af_set_thresholds(data
, chan
, dir
, val_masked
);
587 return fxls8962af_active(data
);
589 return __fxls8962af_set_thresholds(data
, chan
, dir
, val_masked
);
594 fxls8962af_read_event_config(struct iio_dev
*indio_dev
,
595 const struct iio_chan_spec
*chan
,
596 enum iio_event_type type
,
597 enum iio_event_direction dir
)
599 struct fxls8962af_data
*data
= iio_priv(indio_dev
);
601 if (type
!= IIO_EV_TYPE_THRESH
)
604 switch (chan
->channel2
) {
606 return !!(FXLS8962AF_SDCD_CONFIG1_X_OT_EN
& data
->enable_event
);
608 return !!(FXLS8962AF_SDCD_CONFIG1_Y_OT_EN
& data
->enable_event
);
610 return !!(FXLS8962AF_SDCD_CONFIG1_Z_OT_EN
& data
->enable_event
);
617 fxls8962af_write_event_config(struct iio_dev
*indio_dev
,
618 const struct iio_chan_spec
*chan
,
619 enum iio_event_type type
,
620 enum iio_event_direction dir
, bool state
)
622 struct fxls8962af_data
*data
= iio_priv(indio_dev
);
623 u8 enable_event
, enable_bits
;
626 if (type
!= IIO_EV_TYPE_THRESH
)
629 switch (chan
->channel2
) {
631 enable_bits
= FXLS8962AF_SDCD_CONFIG1_X_OT_EN
;
634 enable_bits
= FXLS8962AF_SDCD_CONFIG1_Y_OT_EN
;
637 enable_bits
= FXLS8962AF_SDCD_CONFIG1_Z_OT_EN
;
644 enable_event
= data
->enable_event
| enable_bits
;
646 enable_event
= data
->enable_event
& ~enable_bits
;
648 if (data
->enable_event
== enable_event
)
651 ret
= fxls8962af_standby(data
);
656 value
= enable_event
| FXLS8962AF_SDCD_CONFIG1_OT_ELE
;
657 ret
= regmap_write(data
->regmap
, FXLS8962AF_SDCD_CONFIG1
, value
);
662 * Enable update of SDCD_REF_X/Y/Z values with the current decimated and
663 * trimmed X/Y/Z acceleration input data. This allows for acceleration
664 * slope detection with Data(n) to Data(n–1) always used as the input
665 * to the window comparator.
667 value
= enable_event
?
668 FXLS8962AF_SDCD_CONFIG2_SDCD_EN
| FXLS8962AF_SC2_REF_UPDM_AC
:
670 ret
= regmap_write(data
->regmap
, FXLS8962AF_SDCD_CONFIG2
, value
);
674 ret
= fxls8962af_event_setup(data
, state
);
678 data
->enable_event
= enable_event
;
680 if (data
->enable_event
) {
681 fxls8962af_active(data
);
682 ret
= fxls8962af_power_on(data
);
684 ret
= iio_device_claim_direct_mode(indio_dev
);
688 /* Not in buffered mode so disable power */
689 ret
= fxls8962af_power_off(data
);
691 iio_device_release_direct_mode(indio_dev
);
697 static const struct iio_event_spec fxls8962af_event
[] = {
699 .type
= IIO_EV_TYPE_THRESH
,
700 .dir
= IIO_EV_DIR_EITHER
,
701 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
),
704 .type
= IIO_EV_TYPE_THRESH
,
705 .dir
= IIO_EV_DIR_FALLING
,
706 .mask_separate
= BIT(IIO_EV_INFO_VALUE
),
709 .type
= IIO_EV_TYPE_THRESH
,
710 .dir
= IIO_EV_DIR_RISING
,
711 .mask_separate
= BIT(IIO_EV_INFO_VALUE
),
715 #define FXLS8962AF_CHANNEL(axis, reg, idx) { \
719 .channel2 = IIO_MOD_##axis, \
720 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
721 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
722 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
723 .info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SCALE) | \
724 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
730 .endianness = IIO_LE, \
732 .event_spec = fxls8962af_event, \
733 .num_event_specs = ARRAY_SIZE(fxls8962af_event), \
736 #define FXLS8962AF_TEMP_CHANNEL { \
738 .address = FXLS8962AF_TEMP_OUT, \
739 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
740 BIT(IIO_CHAN_INFO_OFFSET),\
748 static const struct iio_chan_spec fxls8962af_channels
[] = {
749 FXLS8962AF_CHANNEL(X
, FXLS8962AF_OUT_X_LSB
, fxls8962af_idx_x
),
750 FXLS8962AF_CHANNEL(Y
, FXLS8962AF_OUT_Y_LSB
, fxls8962af_idx_y
),
751 FXLS8962AF_CHANNEL(Z
, FXLS8962AF_OUT_Z_LSB
, fxls8962af_idx_z
),
752 IIO_CHAN_SOFT_TIMESTAMP(fxls8962af_idx_ts
),
753 FXLS8962AF_TEMP_CHANNEL
,
756 static const struct fxls8962af_chip_info fxls_chip_info_table
[] = {
758 .chip_id
= FXLS8962AF_DEVICE_ID
,
759 .name
= "fxls8962af",
760 .channels
= fxls8962af_channels
,
761 .num_channels
= ARRAY_SIZE(fxls8962af_channels
),
764 .chip_id
= FXLS8964AF_DEVICE_ID
,
765 .name
= "fxls8964af",
766 .channels
= fxls8962af_channels
,
767 .num_channels
= ARRAY_SIZE(fxls8962af_channels
),
771 static const struct iio_info fxls8962af_info
= {
772 .read_raw
= &fxls8962af_read_raw
,
773 .write_raw
= &fxls8962af_write_raw
,
774 .write_raw_get_fmt
= fxls8962af_write_raw_get_fmt
,
775 .read_event_value
= fxls8962af_read_event
,
776 .write_event_value
= fxls8962af_write_event
,
777 .read_event_config
= fxls8962af_read_event_config
,
778 .write_event_config
= fxls8962af_write_event_config
,
779 .read_avail
= fxls8962af_read_avail
,
780 .hwfifo_set_watermark
= fxls8962af_set_watermark
,
783 static int fxls8962af_reset(struct fxls8962af_data
*data
)
785 struct device
*dev
= regmap_get_device(data
->regmap
);
789 ret
= regmap_set_bits(data
->regmap
, FXLS8962AF_SENS_CONFIG1
,
790 FXLS8962AF_SENS_CONFIG1_RST
);
794 /* TBOOT1, TBOOT2, specifies we have to wait between 1 - 17.7ms */
795 ret
= regmap_read_poll_timeout(data
->regmap
, FXLS8962AF_INT_STATUS
, reg
,
796 (reg
& FXLS8962AF_INT_STATUS_SRC_BOOT
),
798 if (ret
== -ETIMEDOUT
)
799 dev_err(dev
, "reset timeout, int_status = 0x%x\n", reg
);
804 static int __fxls8962af_fifo_set_mode(struct fxls8962af_data
*data
, bool onoff
)
808 /* Enable watermark at max fifo size */
809 ret
= regmap_update_bits(data
->regmap
, FXLS8962AF_BUF_CONFIG2
,
810 FXLS8962AF_BUF_CONFIG2_BUF_WMRK
,
815 return regmap_update_bits(data
->regmap
, FXLS8962AF_BUF_CONFIG1
,
816 FXLS8962AF_BC1_BUF_MODE_MASK
,
817 FXLS8962AF_BC1_BUF_MODE_PREP(onoff
));
820 static int fxls8962af_buffer_preenable(struct iio_dev
*indio_dev
)
822 return fxls8962af_power_on(iio_priv(indio_dev
));
825 static int fxls8962af_buffer_postenable(struct iio_dev
*indio_dev
)
827 struct fxls8962af_data
*data
= iio_priv(indio_dev
);
830 fxls8962af_standby(data
);
832 /* Enable buffer interrupt */
833 ret
= regmap_set_bits(data
->regmap
, FXLS8962AF_INT_EN
,
834 FXLS8962AF_INT_EN_BUF_EN
);
838 ret
= __fxls8962af_fifo_set_mode(data
, true);
840 fxls8962af_active(data
);
845 static int fxls8962af_buffer_predisable(struct iio_dev
*indio_dev
)
847 struct fxls8962af_data
*data
= iio_priv(indio_dev
);
850 fxls8962af_standby(data
);
852 /* Disable buffer interrupt */
853 ret
= regmap_clear_bits(data
->regmap
, FXLS8962AF_INT_EN
,
854 FXLS8962AF_INT_EN_BUF_EN
);
858 ret
= __fxls8962af_fifo_set_mode(data
, false);
860 if (data
->enable_event
)
861 fxls8962af_active(data
);
866 static int fxls8962af_buffer_postdisable(struct iio_dev
*indio_dev
)
868 struct fxls8962af_data
*data
= iio_priv(indio_dev
);
870 if (!data
->enable_event
)
871 fxls8962af_power_off(data
);
876 static const struct iio_buffer_setup_ops fxls8962af_buffer_ops
= {
877 .preenable
= fxls8962af_buffer_preenable
,
878 .postenable
= fxls8962af_buffer_postenable
,
879 .predisable
= fxls8962af_buffer_predisable
,
880 .postdisable
= fxls8962af_buffer_postdisable
,
883 static int fxls8962af_i2c_raw_read_errata3(struct fxls8962af_data
*data
,
884 u16
*buffer
, int samples
,
889 for (i
= 0; i
< samples
; i
++) {
890 ret
= regmap_raw_read(data
->regmap
, FXLS8962AF_BUF_X_LSB
,
891 &buffer
[i
* 3], sample_length
);
899 static int fxls8962af_fifo_transfer(struct fxls8962af_data
*data
,
900 u16
*buffer
, int samples
)
902 struct device
*dev
= regmap_get_device(data
->regmap
);
903 int sample_length
= 3 * sizeof(*buffer
);
904 int total_length
= samples
* sample_length
;
907 if (i2c_verify_client(dev
) &&
908 data
->chip_info
->chip_id
== FXLS8962AF_DEVICE_ID
)
910 * Due to errata bug (only applicable on fxls8962af):
911 * E3: FIFO burst read operation error using I2C interface
912 * We have to avoid burst reads on I2C..
914 ret
= fxls8962af_i2c_raw_read_errata3(data
, buffer
, samples
,
917 ret
= regmap_raw_read(data
->regmap
, FXLS8962AF_BUF_X_LSB
, buffer
,
921 dev_err(dev
, "Error transferring data from fifo: %d\n", ret
);
926 static int fxls8962af_fifo_flush(struct iio_dev
*indio_dev
)
928 struct fxls8962af_data
*data
= iio_priv(indio_dev
);
929 struct device
*dev
= regmap_get_device(data
->regmap
);
930 u16 buffer
[FXLS8962AF_FIFO_LENGTH
* 3];
931 uint64_t sample_period
;
937 ret
= regmap_read(data
->regmap
, FXLS8962AF_BUF_STATUS
, ®
);
941 if (reg
& FXLS8962AF_BUF_STATUS_BUF_OVF
) {
942 dev_err(dev
, "Buffer overflow");
946 count
= reg
& FXLS8962AF_BUF_STATUS_BUF_CNT
;
950 data
->old_timestamp
= data
->timestamp
;
951 data
->timestamp
= iio_get_time_ns(indio_dev
);
954 * Approximate timestamps for each of the sample based on the sampling,
955 * frequency, timestamp for last sample and number of samples.
957 sample_period
= (data
->timestamp
- data
->old_timestamp
);
958 do_div(sample_period
, count
);
959 tstamp
= data
->timestamp
- (count
- 1) * sample_period
;
961 ret
= fxls8962af_fifo_transfer(data
, buffer
, count
);
965 /* Demux hw FIFO into kfifo. */
966 for (i
= 0; i
< count
; i
++) {
970 iio_for_each_active_channel(indio_dev
, bit
) {
971 memcpy(&data
->scan
.channels
[j
++], &buffer
[i
* 3 + bit
],
972 sizeof(data
->scan
.channels
[0]));
975 iio_push_to_buffers_with_timestamp(indio_dev
, &data
->scan
,
978 tstamp
+= sample_period
;
984 static int fxls8962af_event_interrupt(struct iio_dev
*indio_dev
)
986 struct fxls8962af_data
*data
= iio_priv(indio_dev
);
987 s64 ts
= iio_get_time_ns(indio_dev
);
992 ret
= regmap_read(data
->regmap
, FXLS8962AF_SDCD_INT_SRC1
, ®
);
996 if (reg
& FXLS8962AF_SDCD_INT_SRC1_X_OT
) {
997 ev_code
= reg
& FXLS8962AF_SDCD_INT_SRC1_X_POL
?
998 IIO_EV_DIR_RISING
: IIO_EV_DIR_FALLING
;
999 iio_push_event(indio_dev
,
1000 IIO_MOD_EVENT_CODE(IIO_ACCEL
, 0, IIO_MOD_X
,
1001 IIO_EV_TYPE_THRESH
, ev_code
), ts
);
1004 if (reg
& FXLS8962AF_SDCD_INT_SRC1_Y_OT
) {
1005 ev_code
= reg
& FXLS8962AF_SDCD_INT_SRC1_Y_POL
?
1006 IIO_EV_DIR_RISING
: IIO_EV_DIR_FALLING
;
1007 iio_push_event(indio_dev
,
1008 IIO_MOD_EVENT_CODE(IIO_ACCEL
, 0, IIO_MOD_X
,
1009 IIO_EV_TYPE_THRESH
, ev_code
), ts
);
1012 if (reg
& FXLS8962AF_SDCD_INT_SRC1_Z_OT
) {
1013 ev_code
= reg
& FXLS8962AF_SDCD_INT_SRC1_Z_POL
?
1014 IIO_EV_DIR_RISING
: IIO_EV_DIR_FALLING
;
1015 iio_push_event(indio_dev
,
1016 IIO_MOD_EVENT_CODE(IIO_ACCEL
, 0, IIO_MOD_X
,
1017 IIO_EV_TYPE_THRESH
, ev_code
), ts
);
1023 static irqreturn_t
fxls8962af_interrupt(int irq
, void *p
)
1025 struct iio_dev
*indio_dev
= p
;
1026 struct fxls8962af_data
*data
= iio_priv(indio_dev
);
1030 ret
= regmap_read(data
->regmap
, FXLS8962AF_INT_STATUS
, ®
);
1034 if (reg
& FXLS8962AF_INT_STATUS_SRC_BUF
) {
1035 ret
= fxls8962af_fifo_flush(indio_dev
);
1042 if (reg
& FXLS8962AF_INT_STATUS_SRC_SDCD_OT
) {
1043 ret
= fxls8962af_event_interrupt(indio_dev
);
1053 static void fxls8962af_pm_disable(void *dev_ptr
)
1055 struct device
*dev
= dev_ptr
;
1056 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1058 pm_runtime_disable(dev
);
1059 pm_runtime_set_suspended(dev
);
1060 pm_runtime_put_noidle(dev
);
1062 fxls8962af_standby(iio_priv(indio_dev
));
1065 static void fxls8962af_get_irq(struct device
*dev
,
1066 enum fxls8962af_int_pin
*pin
)
1070 irq
= fwnode_irq_get_byname(dev_fwnode(dev
), "INT2");
1072 *pin
= FXLS8962AF_PIN_INT2
;
1076 *pin
= FXLS8962AF_PIN_INT1
;
1079 static int fxls8962af_irq_setup(struct iio_dev
*indio_dev
, int irq
)
1081 struct fxls8962af_data
*data
= iio_priv(indio_dev
);
1082 struct device
*dev
= regmap_get_device(data
->regmap
);
1083 unsigned long irq_type
;
1084 bool irq_active_high
;
1085 enum fxls8962af_int_pin int_pin
;
1089 fxls8962af_get_irq(dev
, &int_pin
);
1091 case FXLS8962AF_PIN_INT1
:
1092 int_pin_sel
= FXLS8962AF_INT_PIN_SEL_INT1
;
1094 case FXLS8962AF_PIN_INT2
:
1095 int_pin_sel
= FXLS8962AF_INT_PIN_SEL_INT2
;
1098 dev_err(dev
, "unsupported int pin selected\n");
1102 ret
= regmap_update_bits(data
->regmap
, FXLS8962AF_INT_PIN_SEL
,
1103 FXLS8962AF_INT_PIN_SEL_MASK
, int_pin_sel
);
1107 irq_type
= irq_get_trigger_type(irq
);
1109 case IRQF_TRIGGER_HIGH
:
1110 case IRQF_TRIGGER_RISING
:
1111 irq_active_high
= true;
1113 case IRQF_TRIGGER_LOW
:
1114 case IRQF_TRIGGER_FALLING
:
1115 irq_active_high
= false;
1118 dev_info(dev
, "mode %lx unsupported\n", irq_type
);
1122 ret
= regmap_update_bits(data
->regmap
, FXLS8962AF_SENS_CONFIG4
,
1123 FXLS8962AF_SC4_INT_POL_MASK
,
1124 FXLS8962AF_SC4_INT_POL_PREP(irq_active_high
));
1128 if (device_property_read_bool(dev
, "drive-open-drain")) {
1129 ret
= regmap_update_bits(data
->regmap
, FXLS8962AF_SENS_CONFIG4
,
1130 FXLS8962AF_SC4_INT_PP_OD_MASK
,
1131 FXLS8962AF_SC4_INT_PP_OD_PREP(1));
1135 irq_type
|= IRQF_SHARED
;
1138 return devm_request_threaded_irq(dev
,
1140 NULL
, fxls8962af_interrupt
,
1141 irq_type
| IRQF_ONESHOT
,
1142 indio_dev
->name
, indio_dev
);
1145 int fxls8962af_core_probe(struct device
*dev
, struct regmap
*regmap
, int irq
)
1147 struct fxls8962af_data
*data
;
1148 struct iio_dev
*indio_dev
;
1152 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
1156 data
= iio_priv(indio_dev
);
1157 dev_set_drvdata(dev
, indio_dev
);
1158 data
->regmap
= regmap
;
1161 ret
= iio_read_mount_matrix(dev
, &data
->orientation
);
1165 ret
= devm_regulator_get_enable(dev
, "vdd");
1167 return dev_err_probe(dev
, ret
,
1168 "Failed to get vdd regulator\n");
1170 ret
= regmap_read(data
->regmap
, FXLS8962AF_WHO_AM_I
, ®
);
1174 for (i
= 0; i
< ARRAY_SIZE(fxls_chip_info_table
); i
++) {
1175 if (fxls_chip_info_table
[i
].chip_id
== reg
) {
1176 data
->chip_info
= &fxls_chip_info_table
[i
];
1180 if (i
== ARRAY_SIZE(fxls_chip_info_table
)) {
1181 dev_err(dev
, "failed to match device in table\n");
1185 indio_dev
->channels
= data
->chip_info
->channels
;
1186 indio_dev
->num_channels
= data
->chip_info
->num_channels
;
1187 indio_dev
->name
= data
->chip_info
->name
;
1188 indio_dev
->info
= &fxls8962af_info
;
1189 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1191 ret
= fxls8962af_reset(data
);
1196 ret
= fxls8962af_irq_setup(indio_dev
, irq
);
1200 ret
= devm_iio_kfifo_buffer_setup(dev
, indio_dev
,
1201 &fxls8962af_buffer_ops
);
1206 ret
= pm_runtime_set_active(dev
);
1210 pm_runtime_enable(dev
);
1211 pm_runtime_set_autosuspend_delay(dev
, FXLS8962AF_AUTO_SUSPEND_DELAY_MS
);
1212 pm_runtime_use_autosuspend(dev
);
1214 ret
= devm_add_action_or_reset(dev
, fxls8962af_pm_disable
, dev
);
1218 if (device_property_read_bool(dev
, "wakeup-source"))
1219 device_init_wakeup(dev
, true);
1221 return devm_iio_device_register(dev
, indio_dev
);
1223 EXPORT_SYMBOL_NS_GPL(fxls8962af_core_probe
, "IIO_FXLS8962AF");
1225 static int fxls8962af_runtime_suspend(struct device
*dev
)
1227 struct fxls8962af_data
*data
= iio_priv(dev_get_drvdata(dev
));
1230 ret
= fxls8962af_standby(data
);
1232 dev_err(dev
, "powering off device failed\n");
1239 static int fxls8962af_runtime_resume(struct device
*dev
)
1241 struct fxls8962af_data
*data
= iio_priv(dev_get_drvdata(dev
));
1243 return fxls8962af_active(data
);
1246 static int fxls8962af_suspend(struct device
*dev
)
1248 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1249 struct fxls8962af_data
*data
= iio_priv(indio_dev
);
1251 if (device_may_wakeup(dev
) && data
->enable_event
) {
1252 enable_irq_wake(data
->irq
);
1255 * Disable buffer, as the buffer is so small the device will wake
1256 * almost immediately.
1258 if (iio_buffer_enabled(indio_dev
))
1259 fxls8962af_buffer_predisable(indio_dev
);
1261 fxls8962af_runtime_suspend(dev
);
1267 static int fxls8962af_resume(struct device
*dev
)
1269 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1270 struct fxls8962af_data
*data
= iio_priv(indio_dev
);
1272 if (device_may_wakeup(dev
) && data
->enable_event
) {
1273 disable_irq_wake(data
->irq
);
1275 if (iio_buffer_enabled(indio_dev
))
1276 fxls8962af_buffer_postenable(indio_dev
);
1278 fxls8962af_runtime_resume(dev
);
1284 EXPORT_NS_GPL_DEV_PM_OPS(fxls8962af_pm_ops
, IIO_FXLS8962AF
) = {
1285 SYSTEM_SLEEP_PM_OPS(fxls8962af_suspend
, fxls8962af_resume
)
1286 RUNTIME_PM_OPS(fxls8962af_runtime_suspend
, fxls8962af_runtime_resume
, NULL
)
1289 MODULE_AUTHOR("Sean Nyekjaer <sean@geanix.com>");
1290 MODULE_DESCRIPTION("NXP FXLS8962AF/FXLS8964AF accelerometer driver");
1291 MODULE_LICENSE("GPL v2");