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>
26 #include <linux/iio/buffer.h>
27 #include <linux/iio/events.h>
28 #include <linux/iio/iio.h>
29 #include <linux/iio/kfifo_buf.h>
30 #include <linux/iio/sysfs.h>
32 #include "fxls8962af.h"
34 #define FXLS8962AF_INT_STATUS 0x00
35 #define FXLS8962AF_INT_STATUS_SRC_BOOT BIT(0)
36 #define FXLS8962AF_INT_STATUS_SRC_SDCD_OT BIT(4)
37 #define FXLS8962AF_INT_STATUS_SRC_BUF BIT(5)
38 #define FXLS8962AF_INT_STATUS_SRC_DRDY BIT(7)
39 #define FXLS8962AF_TEMP_OUT 0x01
40 #define FXLS8962AF_VECM_LSB 0x02
41 #define FXLS8962AF_OUT_X_LSB 0x04
42 #define FXLS8962AF_OUT_Y_LSB 0x06
43 #define FXLS8962AF_OUT_Z_LSB 0x08
44 #define FXLS8962AF_BUF_STATUS 0x0b
45 #define FXLS8962AF_BUF_STATUS_BUF_CNT GENMASK(5, 0)
46 #define FXLS8962AF_BUF_STATUS_BUF_OVF BIT(6)
47 #define FXLS8962AF_BUF_STATUS_BUF_WMRK BIT(7)
48 #define FXLS8962AF_BUF_X_LSB 0x0c
49 #define FXLS8962AF_BUF_Y_LSB 0x0e
50 #define FXLS8962AF_BUF_Z_LSB 0x10
52 #define FXLS8962AF_PROD_REV 0x12
53 #define FXLS8962AF_WHO_AM_I 0x13
55 #define FXLS8962AF_SYS_MODE 0x14
56 #define FXLS8962AF_SENS_CONFIG1 0x15
57 #define FXLS8962AF_SENS_CONFIG1_ACTIVE BIT(0)
58 #define FXLS8962AF_SENS_CONFIG1_RST BIT(7)
59 #define FXLS8962AF_SC1_FSR_MASK GENMASK(2, 1)
60 #define FXLS8962AF_SC1_FSR_PREP(x) FIELD_PREP(FXLS8962AF_SC1_FSR_MASK, (x))
61 #define FXLS8962AF_SC1_FSR_GET(x) FIELD_GET(FXLS8962AF_SC1_FSR_MASK, (x))
63 #define FXLS8962AF_SENS_CONFIG2 0x16
64 #define FXLS8962AF_SENS_CONFIG3 0x17
65 #define FXLS8962AF_SC3_WAKE_ODR_MASK GENMASK(7, 4)
66 #define FXLS8962AF_SC3_WAKE_ODR_PREP(x) FIELD_PREP(FXLS8962AF_SC3_WAKE_ODR_MASK, (x))
67 #define FXLS8962AF_SC3_WAKE_ODR_GET(x) FIELD_GET(FXLS8962AF_SC3_WAKE_ODR_MASK, (x))
68 #define FXLS8962AF_SENS_CONFIG4 0x18
69 #define FXLS8962AF_SC4_INT_PP_OD_MASK BIT(1)
70 #define FXLS8962AF_SC4_INT_PP_OD_PREP(x) FIELD_PREP(FXLS8962AF_SC4_INT_PP_OD_MASK, (x))
71 #define FXLS8962AF_SC4_INT_POL_MASK BIT(0)
72 #define FXLS8962AF_SC4_INT_POL_PREP(x) FIELD_PREP(FXLS8962AF_SC4_INT_POL_MASK, (x))
73 #define FXLS8962AF_SENS_CONFIG5 0x19
75 #define FXLS8962AF_WAKE_IDLE_LSB 0x1b
76 #define FXLS8962AF_SLEEP_IDLE_LSB 0x1c
77 #define FXLS8962AF_ASLP_COUNT_LSB 0x1e
79 #define FXLS8962AF_INT_EN 0x20
80 #define FXLS8962AF_INT_EN_SDCD_OT_EN BIT(5)
81 #define FXLS8962AF_INT_EN_BUF_EN BIT(6)
82 #define FXLS8962AF_INT_PIN_SEL 0x21
83 #define FXLS8962AF_INT_PIN_SEL_MASK GENMASK(7, 0)
84 #define FXLS8962AF_INT_PIN_SEL_INT1 0x00
85 #define FXLS8962AF_INT_PIN_SEL_INT2 GENMASK(7, 0)
87 #define FXLS8962AF_OFF_X 0x22
88 #define FXLS8962AF_OFF_Y 0x23
89 #define FXLS8962AF_OFF_Z 0x24
91 #define FXLS8962AF_BUF_CONFIG1 0x26
92 #define FXLS8962AF_BC1_BUF_MODE_MASK GENMASK(6, 5)
93 #define FXLS8962AF_BC1_BUF_MODE_PREP(x) FIELD_PREP(FXLS8962AF_BC1_BUF_MODE_MASK, (x))
94 #define FXLS8962AF_BUF_CONFIG2 0x27
95 #define FXLS8962AF_BUF_CONFIG2_BUF_WMRK GENMASK(5, 0)
97 #define FXLS8962AF_ORIENT_STATUS 0x28
98 #define FXLS8962AF_ORIENT_CONFIG 0x29
99 #define FXLS8962AF_ORIENT_DBCOUNT 0x2a
100 #define FXLS8962AF_ORIENT_BF_ZCOMP 0x2b
101 #define FXLS8962AF_ORIENT_THS_REG 0x2c
103 #define FXLS8962AF_SDCD_INT_SRC1 0x2d
104 #define FXLS8962AF_SDCD_INT_SRC1_X_OT BIT(5)
105 #define FXLS8962AF_SDCD_INT_SRC1_X_POL BIT(4)
106 #define FXLS8962AF_SDCD_INT_SRC1_Y_OT BIT(3)
107 #define FXLS8962AF_SDCD_INT_SRC1_Y_POL BIT(2)
108 #define FXLS8962AF_SDCD_INT_SRC1_Z_OT BIT(1)
109 #define FXLS8962AF_SDCD_INT_SRC1_Z_POL BIT(0)
110 #define FXLS8962AF_SDCD_INT_SRC2 0x2e
111 #define FXLS8962AF_SDCD_CONFIG1 0x2f
112 #define FXLS8962AF_SDCD_CONFIG1_Z_OT_EN BIT(3)
113 #define FXLS8962AF_SDCD_CONFIG1_Y_OT_EN BIT(4)
114 #define FXLS8962AF_SDCD_CONFIG1_X_OT_EN BIT(5)
115 #define FXLS8962AF_SDCD_CONFIG1_OT_ELE BIT(7)
116 #define FXLS8962AF_SDCD_CONFIG2 0x30
117 #define FXLS8962AF_SDCD_CONFIG2_SDCD_EN BIT(7)
118 #define FXLS8962AF_SC2_REF_UPDM_AC GENMASK(6, 5)
119 #define FXLS8962AF_SDCD_OT_DBCNT 0x31
120 #define FXLS8962AF_SDCD_WT_DBCNT 0x32
121 #define FXLS8962AF_SDCD_LTHS_LSB 0x33
122 #define FXLS8962AF_SDCD_UTHS_LSB 0x35
124 #define FXLS8962AF_SELF_TEST_CONFIG1 0x37
125 #define FXLS8962AF_SELF_TEST_CONFIG2 0x38
127 #define FXLS8962AF_MAX_REG 0x38
129 #define FXLS8962AF_DEVICE_ID 0x62
130 #define FXLS8964AF_DEVICE_ID 0x84
132 /* Raw temp channel offset */
133 #define FXLS8962AF_TEMP_CENTER_VAL 25
135 #define FXLS8962AF_AUTO_SUSPEND_DELAY_MS 2000
137 #define FXLS8962AF_FIFO_LENGTH 32
138 #define FXLS8962AF_SCALE_TABLE_LEN 4
139 #define FXLS8962AF_SAMP_FREQ_TABLE_LEN 13
141 static const int fxls8962af_scale_table
[FXLS8962AF_SCALE_TABLE_LEN
][2] = {
142 {0, IIO_G_TO_M_S_2(980000)},
143 {0, IIO_G_TO_M_S_2(1950000)},
144 {0, IIO_G_TO_M_S_2(3910000)},
145 {0, IIO_G_TO_M_S_2(7810000)},
148 static const int fxls8962af_samp_freq_table
[FXLS8962AF_SAMP_FREQ_TABLE_LEN
][2] = {
149 {3200, 0}, {1600, 0}, {800, 0}, {400, 0}, {200, 0}, {100, 0},
150 {50, 0}, {25, 0}, {12, 500000}, {6, 250000}, {3, 125000},
151 {1, 563000}, {0, 781000},
154 struct fxls8962af_chip_info
{
156 const struct iio_chan_spec
*channels
;
161 struct fxls8962af_data
{
162 struct regmap
*regmap
;
163 const struct fxls8962af_chip_info
*chip_info
;
168 int64_t timestamp
, old_timestamp
; /* Only used in hw fifo mode. */
169 struct iio_mount_matrix orientation
;
177 const struct regmap_config fxls8962af_i2c_regmap_conf
= {
180 .max_register
= FXLS8962AF_MAX_REG
,
182 EXPORT_SYMBOL_NS_GPL(fxls8962af_i2c_regmap_conf
, IIO_FXLS8962AF
);
184 const struct regmap_config fxls8962af_spi_regmap_conf
= {
188 .max_register
= FXLS8962AF_MAX_REG
,
190 EXPORT_SYMBOL_NS_GPL(fxls8962af_spi_regmap_conf
, IIO_FXLS8962AF
);
199 enum fxls8962af_int_pin
{
204 static int fxls8962af_power_on(struct fxls8962af_data
*data
)
206 struct device
*dev
= regmap_get_device(data
->regmap
);
209 ret
= pm_runtime_resume_and_get(dev
);
211 dev_err(dev
, "failed to power on\n");
216 static int fxls8962af_power_off(struct fxls8962af_data
*data
)
218 struct device
*dev
= regmap_get_device(data
->regmap
);
221 pm_runtime_mark_last_busy(dev
);
222 ret
= pm_runtime_put_autosuspend(dev
);
224 dev_err(dev
, "failed to power off\n");
229 static int fxls8962af_standby(struct fxls8962af_data
*data
)
231 return regmap_clear_bits(data
->regmap
, FXLS8962AF_SENS_CONFIG1
,
232 FXLS8962AF_SENS_CONFIG1_ACTIVE
);
235 static int fxls8962af_active(struct fxls8962af_data
*data
)
237 return regmap_update_bits(data
->regmap
, FXLS8962AF_SENS_CONFIG1
,
238 FXLS8962AF_SENS_CONFIG1_ACTIVE
, 1);
241 static int fxls8962af_is_active(struct fxls8962af_data
*data
)
246 ret
= regmap_read(data
->regmap
, FXLS8962AF_SENS_CONFIG1
, ®
);
250 return reg
& FXLS8962AF_SENS_CONFIG1_ACTIVE
;
253 static int fxls8962af_get_out(struct fxls8962af_data
*data
,
254 struct iio_chan_spec
const *chan
, int *val
)
256 struct device
*dev
= regmap_get_device(data
->regmap
);
261 is_active
= fxls8962af_is_active(data
);
263 ret
= fxls8962af_power_on(data
);
268 ret
= regmap_bulk_read(data
->regmap
, chan
->address
,
269 &raw_val
, sizeof(data
->lower_thres
));
272 fxls8962af_power_off(data
);
275 dev_err(dev
, "failed to get out reg 0x%lx\n", chan
->address
);
279 *val
= sign_extend32(le16_to_cpu(raw_val
),
280 chan
->scan_type
.realbits
- 1);
285 static int fxls8962af_read_avail(struct iio_dev
*indio_dev
,
286 struct iio_chan_spec
const *chan
,
287 const int **vals
, int *type
, int *length
,
291 case IIO_CHAN_INFO_SCALE
:
292 *type
= IIO_VAL_INT_PLUS_NANO
;
293 *vals
= (int *)fxls8962af_scale_table
;
294 *length
= ARRAY_SIZE(fxls8962af_scale_table
) * 2;
295 return IIO_AVAIL_LIST
;
296 case IIO_CHAN_INFO_SAMP_FREQ
:
297 *type
= IIO_VAL_INT_PLUS_MICRO
;
298 *vals
= (int *)fxls8962af_samp_freq_table
;
299 *length
= ARRAY_SIZE(fxls8962af_samp_freq_table
) * 2;
300 return IIO_AVAIL_LIST
;
306 static int fxls8962af_write_raw_get_fmt(struct iio_dev
*indio_dev
,
307 struct iio_chan_spec
const *chan
,
311 case IIO_CHAN_INFO_SCALE
:
312 return IIO_VAL_INT_PLUS_NANO
;
313 case IIO_CHAN_INFO_SAMP_FREQ
:
314 return IIO_VAL_INT_PLUS_MICRO
;
316 return IIO_VAL_INT_PLUS_NANO
;
320 static int fxls8962af_update_config(struct fxls8962af_data
*data
, u8 reg
,
326 is_active
= fxls8962af_is_active(data
);
328 ret
= fxls8962af_standby(data
);
333 ret
= regmap_update_bits(data
->regmap
, reg
, mask
, val
);
338 ret
= fxls8962af_active(data
);
346 static int fxls8962af_set_full_scale(struct fxls8962af_data
*data
, u32 scale
)
350 for (i
= 0; i
< ARRAY_SIZE(fxls8962af_scale_table
); i
++)
351 if (scale
== fxls8962af_scale_table
[i
][1])
354 if (i
== ARRAY_SIZE(fxls8962af_scale_table
))
357 return fxls8962af_update_config(data
, FXLS8962AF_SENS_CONFIG1
,
358 FXLS8962AF_SC1_FSR_MASK
,
359 FXLS8962AF_SC1_FSR_PREP(i
));
362 static unsigned int fxls8962af_read_full_scale(struct fxls8962af_data
*data
,
369 ret
= regmap_read(data
->regmap
, FXLS8962AF_SENS_CONFIG1
, ®
);
373 range_idx
= FXLS8962AF_SC1_FSR_GET(reg
);
375 *val
= fxls8962af_scale_table
[range_idx
][1];
377 return IIO_VAL_INT_PLUS_NANO
;
380 static int fxls8962af_set_samp_freq(struct fxls8962af_data
*data
, u32 val
,
385 for (i
= 0; i
< ARRAY_SIZE(fxls8962af_samp_freq_table
); i
++)
386 if (val
== fxls8962af_samp_freq_table
[i
][0] &&
387 val2
== fxls8962af_samp_freq_table
[i
][1])
390 if (i
== ARRAY_SIZE(fxls8962af_samp_freq_table
))
393 return fxls8962af_update_config(data
, FXLS8962AF_SENS_CONFIG3
,
394 FXLS8962AF_SC3_WAKE_ODR_MASK
,
395 FXLS8962AF_SC3_WAKE_ODR_PREP(i
));
398 static unsigned int fxls8962af_read_samp_freq(struct fxls8962af_data
*data
,
405 ret
= regmap_read(data
->regmap
, FXLS8962AF_SENS_CONFIG3
, ®
);
409 range_idx
= FXLS8962AF_SC3_WAKE_ODR_GET(reg
);
411 *val
= fxls8962af_samp_freq_table
[range_idx
][0];
412 *val2
= fxls8962af_samp_freq_table
[range_idx
][1];
414 return IIO_VAL_INT_PLUS_MICRO
;
417 static int fxls8962af_read_raw(struct iio_dev
*indio_dev
,
418 struct iio_chan_spec
const *chan
,
419 int *val
, int *val2
, long mask
)
421 struct fxls8962af_data
*data
= iio_priv(indio_dev
);
424 case IIO_CHAN_INFO_RAW
:
425 switch (chan
->type
) {
428 return fxls8962af_get_out(data
, chan
, val
);
432 case IIO_CHAN_INFO_OFFSET
:
433 if (chan
->type
!= IIO_TEMP
)
436 *val
= FXLS8962AF_TEMP_CENTER_VAL
;
438 case IIO_CHAN_INFO_SCALE
:
440 return fxls8962af_read_full_scale(data
, val2
);
441 case IIO_CHAN_INFO_SAMP_FREQ
:
442 return fxls8962af_read_samp_freq(data
, val
, val2
);
448 static int fxls8962af_write_raw(struct iio_dev
*indio_dev
,
449 struct iio_chan_spec
const *chan
,
450 int val
, int val2
, long mask
)
452 struct fxls8962af_data
*data
= iio_priv(indio_dev
);
456 case IIO_CHAN_INFO_SCALE
:
460 ret
= iio_device_claim_direct_mode(indio_dev
);
464 ret
= fxls8962af_set_full_scale(data
, val2
);
466 iio_device_release_direct_mode(indio_dev
);
468 case IIO_CHAN_INFO_SAMP_FREQ
:
469 ret
= iio_device_claim_direct_mode(indio_dev
);
473 ret
= fxls8962af_set_samp_freq(data
, val
, val2
);
475 iio_device_release_direct_mode(indio_dev
);
482 static int fxls8962af_event_setup(struct fxls8962af_data
*data
, int state
)
484 /* Enable wakeup interrupt */
485 int mask
= FXLS8962AF_INT_EN_SDCD_OT_EN
;
486 int value
= state
? mask
: 0;
488 return regmap_update_bits(data
->regmap
, FXLS8962AF_INT_EN
, mask
, value
);
491 static int fxls8962af_set_watermark(struct iio_dev
*indio_dev
, unsigned val
)
493 struct fxls8962af_data
*data
= iio_priv(indio_dev
);
495 if (val
> FXLS8962AF_FIFO_LENGTH
)
496 val
= FXLS8962AF_FIFO_LENGTH
;
498 data
->watermark
= val
;
503 static int __fxls8962af_set_thresholds(struct fxls8962af_data
*data
,
504 const struct iio_chan_spec
*chan
,
505 enum iio_event_direction dir
,
509 case IIO_EV_DIR_FALLING
:
510 data
->lower_thres
= val
;
511 return regmap_bulk_write(data
->regmap
, FXLS8962AF_SDCD_LTHS_LSB
,
512 &data
->lower_thres
, sizeof(data
->lower_thres
));
513 case IIO_EV_DIR_RISING
:
514 data
->upper_thres
= val
;
515 return regmap_bulk_write(data
->regmap
, FXLS8962AF_SDCD_UTHS_LSB
,
516 &data
->upper_thres
, sizeof(data
->upper_thres
));
522 static int fxls8962af_read_event(struct iio_dev
*indio_dev
,
523 const struct iio_chan_spec
*chan
,
524 enum iio_event_type type
,
525 enum iio_event_direction dir
,
526 enum iio_event_info info
,
529 struct fxls8962af_data
*data
= iio_priv(indio_dev
);
532 if (type
!= IIO_EV_TYPE_THRESH
)
536 case IIO_EV_DIR_FALLING
:
537 ret
= regmap_bulk_read(data
->regmap
, FXLS8962AF_SDCD_LTHS_LSB
,
538 &data
->lower_thres
, sizeof(data
->lower_thres
));
542 *val
= sign_extend32(data
->lower_thres
, chan
->scan_type
.realbits
- 1);
544 case IIO_EV_DIR_RISING
:
545 ret
= regmap_bulk_read(data
->regmap
, FXLS8962AF_SDCD_UTHS_LSB
,
546 &data
->upper_thres
, sizeof(data
->upper_thres
));
550 *val
= sign_extend32(data
->upper_thres
, chan
->scan_type
.realbits
- 1);
557 static int fxls8962af_write_event(struct iio_dev
*indio_dev
,
558 const struct iio_chan_spec
*chan
,
559 enum iio_event_type type
,
560 enum iio_event_direction dir
,
561 enum iio_event_info info
,
564 struct fxls8962af_data
*data
= iio_priv(indio_dev
);
567 if (type
!= IIO_EV_TYPE_THRESH
)
570 if (val
< -2048 || val
> 2047)
573 if (data
->enable_event
)
576 val_masked
= val
& GENMASK(11, 0);
577 if (fxls8962af_is_active(data
)) {
578 ret
= fxls8962af_standby(data
);
582 ret
= __fxls8962af_set_thresholds(data
, chan
, dir
, val_masked
);
586 return fxls8962af_active(data
);
588 return __fxls8962af_set_thresholds(data
, chan
, dir
, val_masked
);
593 fxls8962af_read_event_config(struct iio_dev
*indio_dev
,
594 const struct iio_chan_spec
*chan
,
595 enum iio_event_type type
,
596 enum iio_event_direction dir
)
598 struct fxls8962af_data
*data
= iio_priv(indio_dev
);
600 if (type
!= IIO_EV_TYPE_THRESH
)
603 switch (chan
->channel2
) {
605 return !!(FXLS8962AF_SDCD_CONFIG1_X_OT_EN
& data
->enable_event
);
607 return !!(FXLS8962AF_SDCD_CONFIG1_Y_OT_EN
& data
->enable_event
);
609 return !!(FXLS8962AF_SDCD_CONFIG1_Z_OT_EN
& data
->enable_event
);
616 fxls8962af_write_event_config(struct iio_dev
*indio_dev
,
617 const struct iio_chan_spec
*chan
,
618 enum iio_event_type type
,
619 enum iio_event_direction dir
, int state
)
621 struct fxls8962af_data
*data
= iio_priv(indio_dev
);
622 u8 enable_event
, enable_bits
;
625 if (type
!= IIO_EV_TYPE_THRESH
)
628 switch (chan
->channel2
) {
630 enable_bits
= FXLS8962AF_SDCD_CONFIG1_X_OT_EN
;
633 enable_bits
= FXLS8962AF_SDCD_CONFIG1_Y_OT_EN
;
636 enable_bits
= FXLS8962AF_SDCD_CONFIG1_Z_OT_EN
;
643 enable_event
= data
->enable_event
| enable_bits
;
645 enable_event
= data
->enable_event
& ~enable_bits
;
647 if (data
->enable_event
== enable_event
)
650 ret
= fxls8962af_standby(data
);
655 value
= enable_event
| FXLS8962AF_SDCD_CONFIG1_OT_ELE
;
656 ret
= regmap_write(data
->regmap
, FXLS8962AF_SDCD_CONFIG1
, value
);
661 * Enable update of SDCD_REF_X/Y/Z values with the current decimated and
662 * trimmed X/Y/Z acceleration input data. This allows for acceleration
663 * slope detection with Data(n) to Data(n–1) always used as the input
664 * to the window comparator.
666 value
= enable_event
?
667 FXLS8962AF_SDCD_CONFIG2_SDCD_EN
| FXLS8962AF_SC2_REF_UPDM_AC
:
669 ret
= regmap_write(data
->regmap
, FXLS8962AF_SDCD_CONFIG2
, value
);
673 ret
= fxls8962af_event_setup(data
, state
);
677 data
->enable_event
= enable_event
;
679 if (data
->enable_event
) {
680 fxls8962af_active(data
);
681 ret
= fxls8962af_power_on(data
);
683 ret
= iio_device_claim_direct_mode(indio_dev
);
687 /* Not in buffered mode so disable power */
688 ret
= fxls8962af_power_off(data
);
690 iio_device_release_direct_mode(indio_dev
);
696 static const struct iio_event_spec fxls8962af_event
[] = {
698 .type
= IIO_EV_TYPE_THRESH
,
699 .dir
= IIO_EV_DIR_EITHER
,
700 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
),
703 .type
= IIO_EV_TYPE_THRESH
,
704 .dir
= IIO_EV_DIR_FALLING
,
705 .mask_separate
= BIT(IIO_EV_INFO_VALUE
),
708 .type
= IIO_EV_TYPE_THRESH
,
709 .dir
= IIO_EV_DIR_RISING
,
710 .mask_separate
= BIT(IIO_EV_INFO_VALUE
),
714 #define FXLS8962AF_CHANNEL(axis, reg, idx) { \
718 .channel2 = IIO_MOD_##axis, \
719 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
720 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
721 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
722 .info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SCALE) | \
723 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
729 .endianness = IIO_LE, \
731 .event_spec = fxls8962af_event, \
732 .num_event_specs = ARRAY_SIZE(fxls8962af_event), \
735 #define FXLS8962AF_TEMP_CHANNEL { \
737 .address = FXLS8962AF_TEMP_OUT, \
738 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
739 BIT(IIO_CHAN_INFO_OFFSET),\
747 static const struct iio_chan_spec fxls8962af_channels
[] = {
748 FXLS8962AF_CHANNEL(X
, FXLS8962AF_OUT_X_LSB
, fxls8962af_idx_x
),
749 FXLS8962AF_CHANNEL(Y
, FXLS8962AF_OUT_Y_LSB
, fxls8962af_idx_y
),
750 FXLS8962AF_CHANNEL(Z
, FXLS8962AF_OUT_Z_LSB
, fxls8962af_idx_z
),
751 IIO_CHAN_SOFT_TIMESTAMP(fxls8962af_idx_ts
),
752 FXLS8962AF_TEMP_CHANNEL
,
755 static const struct fxls8962af_chip_info fxls_chip_info_table
[] = {
757 .chip_id
= FXLS8962AF_DEVICE_ID
,
758 .name
= "fxls8962af",
759 .channels
= fxls8962af_channels
,
760 .num_channels
= ARRAY_SIZE(fxls8962af_channels
),
763 .chip_id
= FXLS8964AF_DEVICE_ID
,
764 .name
= "fxls8964af",
765 .channels
= fxls8962af_channels
,
766 .num_channels
= ARRAY_SIZE(fxls8962af_channels
),
770 static const struct iio_info fxls8962af_info
= {
771 .read_raw
= &fxls8962af_read_raw
,
772 .write_raw
= &fxls8962af_write_raw
,
773 .write_raw_get_fmt
= fxls8962af_write_raw_get_fmt
,
774 .read_event_value
= fxls8962af_read_event
,
775 .write_event_value
= fxls8962af_write_event
,
776 .read_event_config
= fxls8962af_read_event_config
,
777 .write_event_config
= fxls8962af_write_event_config
,
778 .read_avail
= fxls8962af_read_avail
,
779 .hwfifo_set_watermark
= fxls8962af_set_watermark
,
782 static int fxls8962af_reset(struct fxls8962af_data
*data
)
784 struct device
*dev
= regmap_get_device(data
->regmap
);
788 ret
= regmap_set_bits(data
->regmap
, FXLS8962AF_SENS_CONFIG1
,
789 FXLS8962AF_SENS_CONFIG1_RST
);
793 /* TBOOT1, TBOOT2, specifies we have to wait between 1 - 17.7ms */
794 ret
= regmap_read_poll_timeout(data
->regmap
, FXLS8962AF_INT_STATUS
, reg
,
795 (reg
& FXLS8962AF_INT_STATUS_SRC_BOOT
),
797 if (ret
== -ETIMEDOUT
)
798 dev_err(dev
, "reset timeout, int_status = 0x%x\n", reg
);
803 static int __fxls8962af_fifo_set_mode(struct fxls8962af_data
*data
, bool onoff
)
807 /* Enable watermark at max fifo size */
808 ret
= regmap_update_bits(data
->regmap
, FXLS8962AF_BUF_CONFIG2
,
809 FXLS8962AF_BUF_CONFIG2_BUF_WMRK
,
814 return regmap_update_bits(data
->regmap
, FXLS8962AF_BUF_CONFIG1
,
815 FXLS8962AF_BC1_BUF_MODE_MASK
,
816 FXLS8962AF_BC1_BUF_MODE_PREP(onoff
));
819 static int fxls8962af_buffer_preenable(struct iio_dev
*indio_dev
)
821 return fxls8962af_power_on(iio_priv(indio_dev
));
824 static int fxls8962af_buffer_postenable(struct iio_dev
*indio_dev
)
826 struct fxls8962af_data
*data
= iio_priv(indio_dev
);
829 fxls8962af_standby(data
);
831 /* Enable buffer interrupt */
832 ret
= regmap_set_bits(data
->regmap
, FXLS8962AF_INT_EN
,
833 FXLS8962AF_INT_EN_BUF_EN
);
837 ret
= __fxls8962af_fifo_set_mode(data
, true);
839 fxls8962af_active(data
);
844 static int fxls8962af_buffer_predisable(struct iio_dev
*indio_dev
)
846 struct fxls8962af_data
*data
= iio_priv(indio_dev
);
849 fxls8962af_standby(data
);
851 /* Disable buffer interrupt */
852 ret
= regmap_clear_bits(data
->regmap
, FXLS8962AF_INT_EN
,
853 FXLS8962AF_INT_EN_BUF_EN
);
857 ret
= __fxls8962af_fifo_set_mode(data
, false);
859 if (data
->enable_event
)
860 fxls8962af_active(data
);
865 static int fxls8962af_buffer_postdisable(struct iio_dev
*indio_dev
)
867 struct fxls8962af_data
*data
= iio_priv(indio_dev
);
869 if (!data
->enable_event
)
870 fxls8962af_power_off(data
);
875 static const struct iio_buffer_setup_ops fxls8962af_buffer_ops
= {
876 .preenable
= fxls8962af_buffer_preenable
,
877 .postenable
= fxls8962af_buffer_postenable
,
878 .predisable
= fxls8962af_buffer_predisable
,
879 .postdisable
= fxls8962af_buffer_postdisable
,
882 static int fxls8962af_i2c_raw_read_errata3(struct fxls8962af_data
*data
,
883 u16
*buffer
, int samples
,
888 for (i
= 0; i
< samples
; i
++) {
889 ret
= regmap_raw_read(data
->regmap
, FXLS8962AF_BUF_X_LSB
,
890 &buffer
[i
* 3], sample_length
);
898 static int fxls8962af_fifo_transfer(struct fxls8962af_data
*data
,
899 u16
*buffer
, int samples
)
901 struct device
*dev
= regmap_get_device(data
->regmap
);
902 int sample_length
= 3 * sizeof(*buffer
);
903 int total_length
= samples
* sample_length
;
906 if (i2c_verify_client(dev
) &&
907 data
->chip_info
->chip_id
== FXLS8962AF_DEVICE_ID
)
909 * Due to errata bug (only applicable on fxls8962af):
910 * E3: FIFO burst read operation error using I2C interface
911 * We have to avoid burst reads on I2C..
913 ret
= fxls8962af_i2c_raw_read_errata3(data
, buffer
, samples
,
916 ret
= regmap_raw_read(data
->regmap
, FXLS8962AF_BUF_X_LSB
, buffer
,
920 dev_err(dev
, "Error transferring data from fifo: %d\n", ret
);
925 static int fxls8962af_fifo_flush(struct iio_dev
*indio_dev
)
927 struct fxls8962af_data
*data
= iio_priv(indio_dev
);
928 struct device
*dev
= regmap_get_device(data
->regmap
);
929 u16 buffer
[FXLS8962AF_FIFO_LENGTH
* 3];
930 uint64_t sample_period
;
936 ret
= regmap_read(data
->regmap
, FXLS8962AF_BUF_STATUS
, ®
);
940 if (reg
& FXLS8962AF_BUF_STATUS_BUF_OVF
) {
941 dev_err(dev
, "Buffer overflow");
945 count
= reg
& FXLS8962AF_BUF_STATUS_BUF_CNT
;
949 data
->old_timestamp
= data
->timestamp
;
950 data
->timestamp
= iio_get_time_ns(indio_dev
);
953 * Approximate timestamps for each of the sample based on the sampling,
954 * frequency, timestamp for last sample and number of samples.
956 sample_period
= (data
->timestamp
- data
->old_timestamp
);
957 do_div(sample_period
, count
);
958 tstamp
= data
->timestamp
- (count
- 1) * sample_period
;
960 ret
= fxls8962af_fifo_transfer(data
, buffer
, count
);
964 /* Demux hw FIFO into kfifo. */
965 for (i
= 0; i
< count
; i
++) {
969 iio_for_each_active_channel(indio_dev
, bit
) {
970 memcpy(&data
->scan
.channels
[j
++], &buffer
[i
* 3 + bit
],
971 sizeof(data
->scan
.channels
[0]));
974 iio_push_to_buffers_with_timestamp(indio_dev
, &data
->scan
,
977 tstamp
+= sample_period
;
983 static int fxls8962af_event_interrupt(struct iio_dev
*indio_dev
)
985 struct fxls8962af_data
*data
= iio_priv(indio_dev
);
986 s64 ts
= iio_get_time_ns(indio_dev
);
991 ret
= regmap_read(data
->regmap
, FXLS8962AF_SDCD_INT_SRC1
, ®
);
995 if (reg
& FXLS8962AF_SDCD_INT_SRC1_X_OT
) {
996 ev_code
= reg
& FXLS8962AF_SDCD_INT_SRC1_X_POL
?
997 IIO_EV_DIR_RISING
: IIO_EV_DIR_FALLING
;
998 iio_push_event(indio_dev
,
999 IIO_MOD_EVENT_CODE(IIO_ACCEL
, 0, IIO_MOD_X
,
1000 IIO_EV_TYPE_THRESH
, ev_code
), ts
);
1003 if (reg
& FXLS8962AF_SDCD_INT_SRC1_Y_OT
) {
1004 ev_code
= reg
& FXLS8962AF_SDCD_INT_SRC1_Y_POL
?
1005 IIO_EV_DIR_RISING
: IIO_EV_DIR_FALLING
;
1006 iio_push_event(indio_dev
,
1007 IIO_MOD_EVENT_CODE(IIO_ACCEL
, 0, IIO_MOD_X
,
1008 IIO_EV_TYPE_THRESH
, ev_code
), ts
);
1011 if (reg
& FXLS8962AF_SDCD_INT_SRC1_Z_OT
) {
1012 ev_code
= reg
& FXLS8962AF_SDCD_INT_SRC1_Z_POL
?
1013 IIO_EV_DIR_RISING
: IIO_EV_DIR_FALLING
;
1014 iio_push_event(indio_dev
,
1015 IIO_MOD_EVENT_CODE(IIO_ACCEL
, 0, IIO_MOD_X
,
1016 IIO_EV_TYPE_THRESH
, ev_code
), ts
);
1022 static irqreturn_t
fxls8962af_interrupt(int irq
, void *p
)
1024 struct iio_dev
*indio_dev
= p
;
1025 struct fxls8962af_data
*data
= iio_priv(indio_dev
);
1029 ret
= regmap_read(data
->regmap
, FXLS8962AF_INT_STATUS
, ®
);
1033 if (reg
& FXLS8962AF_INT_STATUS_SRC_BUF
) {
1034 ret
= fxls8962af_fifo_flush(indio_dev
);
1041 if (reg
& FXLS8962AF_INT_STATUS_SRC_SDCD_OT
) {
1042 ret
= fxls8962af_event_interrupt(indio_dev
);
1052 static void fxls8962af_pm_disable(void *dev_ptr
)
1054 struct device
*dev
= dev_ptr
;
1055 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1057 pm_runtime_disable(dev
);
1058 pm_runtime_set_suspended(dev
);
1059 pm_runtime_put_noidle(dev
);
1061 fxls8962af_standby(iio_priv(indio_dev
));
1064 static void fxls8962af_get_irq(struct device
*dev
,
1065 enum fxls8962af_int_pin
*pin
)
1069 irq
= fwnode_irq_get_byname(dev_fwnode(dev
), "INT2");
1071 *pin
= FXLS8962AF_PIN_INT2
;
1075 *pin
= FXLS8962AF_PIN_INT1
;
1078 static int fxls8962af_irq_setup(struct iio_dev
*indio_dev
, int irq
)
1080 struct fxls8962af_data
*data
= iio_priv(indio_dev
);
1081 struct device
*dev
= regmap_get_device(data
->regmap
);
1082 unsigned long irq_type
;
1083 bool irq_active_high
;
1084 enum fxls8962af_int_pin int_pin
;
1088 fxls8962af_get_irq(dev
, &int_pin
);
1090 case FXLS8962AF_PIN_INT1
:
1091 int_pin_sel
= FXLS8962AF_INT_PIN_SEL_INT1
;
1093 case FXLS8962AF_PIN_INT2
:
1094 int_pin_sel
= FXLS8962AF_INT_PIN_SEL_INT2
;
1097 dev_err(dev
, "unsupported int pin selected\n");
1101 ret
= regmap_update_bits(data
->regmap
, FXLS8962AF_INT_PIN_SEL
,
1102 FXLS8962AF_INT_PIN_SEL_MASK
, int_pin_sel
);
1106 irq_type
= irqd_get_trigger_type(irq_get_irq_data(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");