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/module.h>
19 #include <linux/of_irq.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/regmap.h>
24 #include <linux/iio/buffer.h>
25 #include <linux/iio/iio.h>
26 #include <linux/iio/kfifo_buf.h>
27 #include <linux/iio/sysfs.h>
29 #include "fxls8962af.h"
31 #define FXLS8962AF_INT_STATUS 0x00
32 #define FXLS8962AF_INT_STATUS_SRC_BOOT BIT(0)
33 #define FXLS8962AF_INT_STATUS_SRC_BUF BIT(5)
34 #define FXLS8962AF_INT_STATUS_SRC_DRDY BIT(7)
35 #define FXLS8962AF_TEMP_OUT 0x01
36 #define FXLS8962AF_VECM_LSB 0x02
37 #define FXLS8962AF_OUT_X_LSB 0x04
38 #define FXLS8962AF_OUT_Y_LSB 0x06
39 #define FXLS8962AF_OUT_Z_LSB 0x08
40 #define FXLS8962AF_BUF_STATUS 0x0b
41 #define FXLS8962AF_BUF_STATUS_BUF_CNT GENMASK(5, 0)
42 #define FXLS8962AF_BUF_STATUS_BUF_OVF BIT(6)
43 #define FXLS8962AF_BUF_STATUS_BUF_WMRK BIT(7)
44 #define FXLS8962AF_BUF_X_LSB 0x0c
45 #define FXLS8962AF_BUF_Y_LSB 0x0e
46 #define FXLS8962AF_BUF_Z_LSB 0x10
48 #define FXLS8962AF_PROD_REV 0x12
49 #define FXLS8962AF_WHO_AM_I 0x13
51 #define FXLS8962AF_SYS_MODE 0x14
52 #define FXLS8962AF_SENS_CONFIG1 0x15
53 #define FXLS8962AF_SENS_CONFIG1_ACTIVE BIT(0)
54 #define FXLS8962AF_SENS_CONFIG1_RST BIT(7)
55 #define FXLS8962AF_SC1_FSR_MASK GENMASK(2, 1)
56 #define FXLS8962AF_SC1_FSR_PREP(x) FIELD_PREP(FXLS8962AF_SC1_FSR_MASK, (x))
57 #define FXLS8962AF_SC1_FSR_GET(x) FIELD_GET(FXLS8962AF_SC1_FSR_MASK, (x))
59 #define FXLS8962AF_SENS_CONFIG2 0x16
60 #define FXLS8962AF_SENS_CONFIG3 0x17
61 #define FXLS8962AF_SC3_WAKE_ODR_MASK GENMASK(7, 4)
62 #define FXLS8962AF_SC3_WAKE_ODR_PREP(x) FIELD_PREP(FXLS8962AF_SC3_WAKE_ODR_MASK, (x))
63 #define FXLS8962AF_SC3_WAKE_ODR_GET(x) FIELD_GET(FXLS8962AF_SC3_WAKE_ODR_MASK, (x))
64 #define FXLS8962AF_SENS_CONFIG4 0x18
65 #define FXLS8962AF_SC4_INT_PP_OD_MASK BIT(1)
66 #define FXLS8962AF_SC4_INT_PP_OD_PREP(x) FIELD_PREP(FXLS8962AF_SC4_INT_PP_OD_MASK, (x))
67 #define FXLS8962AF_SC4_INT_POL_MASK BIT(0)
68 #define FXLS8962AF_SC4_INT_POL_PREP(x) FIELD_PREP(FXLS8962AF_SC4_INT_POL_MASK, (x))
69 #define FXLS8962AF_SENS_CONFIG5 0x19
71 #define FXLS8962AF_WAKE_IDLE_LSB 0x1b
72 #define FXLS8962AF_SLEEP_IDLE_LSB 0x1c
73 #define FXLS8962AF_ASLP_COUNT_LSB 0x1e
75 #define FXLS8962AF_INT_EN 0x20
76 #define FXLS8962AF_INT_EN_BUF_EN BIT(6)
77 #define FXLS8962AF_INT_PIN_SEL 0x21
78 #define FXLS8962AF_INT_PIN_SEL_MASK GENMASK(7, 0)
79 #define FXLS8962AF_INT_PIN_SEL_INT1 0x00
80 #define FXLS8962AF_INT_PIN_SEL_INT2 GENMASK(7, 0)
82 #define FXLS8962AF_OFF_X 0x22
83 #define FXLS8962AF_OFF_Y 0x23
84 #define FXLS8962AF_OFF_Z 0x24
86 #define FXLS8962AF_BUF_CONFIG1 0x26
87 #define FXLS8962AF_BC1_BUF_MODE_MASK GENMASK(6, 5)
88 #define FXLS8962AF_BC1_BUF_MODE_PREP(x) FIELD_PREP(FXLS8962AF_BC1_BUF_MODE_MASK, (x))
89 #define FXLS8962AF_BUF_CONFIG2 0x27
90 #define FXLS8962AF_BUF_CONFIG2_BUF_WMRK GENMASK(5, 0)
92 #define FXLS8962AF_ORIENT_STATUS 0x28
93 #define FXLS8962AF_ORIENT_CONFIG 0x29
94 #define FXLS8962AF_ORIENT_DBCOUNT 0x2a
95 #define FXLS8962AF_ORIENT_BF_ZCOMP 0x2b
96 #define FXLS8962AF_ORIENT_THS_REG 0x2c
98 #define FXLS8962AF_SDCD_INT_SRC1 0x2d
99 #define FXLS8962AF_SDCD_INT_SRC2 0x2e
100 #define FXLS8962AF_SDCD_CONFIG1 0x2f
101 #define FXLS8962AF_SDCD_CONFIG2 0x30
102 #define FXLS8962AF_SDCD_OT_DBCNT 0x31
103 #define FXLS8962AF_SDCD_WT_DBCNT 0x32
104 #define FXLS8962AF_SDCD_LTHS_LSB 0x33
105 #define FXLS8962AF_SDCD_UTHS_LSB 0x35
107 #define FXLS8962AF_SELF_TEST_CONFIG1 0x37
108 #define FXLS8962AF_SELF_TEST_CONFIG2 0x38
110 #define FXLS8962AF_MAX_REG 0x38
112 #define FXLS8962AF_DEVICE_ID 0x62
113 #define FXLS8964AF_DEVICE_ID 0x84
115 /* Raw temp channel offset */
116 #define FXLS8962AF_TEMP_CENTER_VAL 25
118 #define FXLS8962AF_AUTO_SUSPEND_DELAY_MS 2000
120 #define FXLS8962AF_FIFO_LENGTH 32
121 #define FXLS8962AF_SCALE_TABLE_LEN 4
122 #define FXLS8962AF_SAMP_FREQ_TABLE_LEN 13
124 static const int fxls8962af_scale_table[FXLS8962AF_SCALE_TABLE_LEN][2] = {
125 {0, IIO_G_TO_M_S_2(980000)},
126 {0, IIO_G_TO_M_S_2(1950000)},
127 {0, IIO_G_TO_M_S_2(3910000)},
128 {0, IIO_G_TO_M_S_2(7810000)},
131 static const int fxls8962af_samp_freq_table[FXLS8962AF_SAMP_FREQ_TABLE_LEN][2] = {
132 {3200, 0}, {1600, 0}, {800, 0}, {400, 0}, {200, 0}, {100, 0},
133 {50, 0}, {25, 0}, {12, 500000}, {6, 250000}, {3, 125000},
134 {1, 563000}, {0, 781000},
137 struct fxls8962af_chip_info {
139 const struct iio_chan_spec *channels;
144 struct fxls8962af_data {
145 struct regmap *regmap;
146 const struct fxls8962af_chip_info *chip_info;
147 struct regulator *vdd_reg;
152 int64_t timestamp, old_timestamp; /* Only used in hw fifo mode. */
153 struct iio_mount_matrix orientation;
157 const struct regmap_config fxls8962af_regmap_conf = {
160 .max_register = FXLS8962AF_MAX_REG,
162 EXPORT_SYMBOL_GPL(fxls8962af_regmap_conf);
171 enum fxls8962af_int_pin {
176 static int fxls8962af_power_on(struct fxls8962af_data *data)
178 struct device *dev = regmap_get_device(data->regmap);
181 ret = pm_runtime_resume_and_get(dev);
183 dev_err(dev, "failed to power on\n");
188 static int fxls8962af_power_off(struct fxls8962af_data *data)
190 struct device *dev = regmap_get_device(data->regmap);
193 pm_runtime_mark_last_busy(dev);
194 ret = pm_runtime_put_autosuspend(dev);
196 dev_err(dev, "failed to power off\n");
201 static int fxls8962af_standby(struct fxls8962af_data *data)
203 return regmap_update_bits(data->regmap, FXLS8962AF_SENS_CONFIG1,
204 FXLS8962AF_SENS_CONFIG1_ACTIVE, 0);
207 static int fxls8962af_active(struct fxls8962af_data *data)
209 return regmap_update_bits(data->regmap, FXLS8962AF_SENS_CONFIG1,
210 FXLS8962AF_SENS_CONFIG1_ACTIVE, 1);
213 static int fxls8962af_is_active(struct fxls8962af_data *data)
218 ret = regmap_read(data->regmap, FXLS8962AF_SENS_CONFIG1, ®);
222 return reg & FXLS8962AF_SENS_CONFIG1_ACTIVE;
225 static int fxls8962af_get_out(struct fxls8962af_data *data,
226 struct iio_chan_spec const *chan, int *val)
228 struct device *dev = regmap_get_device(data->regmap);
233 is_active = fxls8962af_is_active(data);
235 ret = fxls8962af_power_on(data);
240 ret = regmap_bulk_read(data->regmap, chan->address,
241 &raw_val, (chan->scan_type.storagebits / 8));
244 fxls8962af_power_off(data);
247 dev_err(dev, "failed to get out reg 0x%lx\n", chan->address);
251 *val = sign_extend32(le16_to_cpu(raw_val),
252 chan->scan_type.realbits - 1);
257 static int fxls8962af_read_avail(struct iio_dev *indio_dev,
258 struct iio_chan_spec const *chan,
259 const int **vals, int *type, int *length,
263 case IIO_CHAN_INFO_SCALE:
264 *type = IIO_VAL_INT_PLUS_NANO;
265 *vals = (int *)fxls8962af_scale_table;
266 *length = ARRAY_SIZE(fxls8962af_scale_table) * 2;
267 return IIO_AVAIL_LIST;
268 case IIO_CHAN_INFO_SAMP_FREQ:
269 *type = IIO_VAL_INT_PLUS_MICRO;
270 *vals = (int *)fxls8962af_samp_freq_table;
271 *length = ARRAY_SIZE(fxls8962af_samp_freq_table) * 2;
272 return IIO_AVAIL_LIST;
278 static int fxls8962af_write_raw_get_fmt(struct iio_dev *indio_dev,
279 struct iio_chan_spec const *chan,
283 case IIO_CHAN_INFO_SCALE:
284 return IIO_VAL_INT_PLUS_NANO;
285 case IIO_CHAN_INFO_SAMP_FREQ:
286 return IIO_VAL_INT_PLUS_MICRO;
288 return IIO_VAL_INT_PLUS_NANO;
292 static int fxls8962af_update_config(struct fxls8962af_data *data, u8 reg,
298 is_active = fxls8962af_is_active(data);
300 ret = fxls8962af_standby(data);
305 ret = regmap_update_bits(data->regmap, reg, mask, val);
310 ret = fxls8962af_active(data);
318 static int fxls8962af_set_full_scale(struct fxls8962af_data *data, u32 scale)
322 for (i = 0; i < ARRAY_SIZE(fxls8962af_scale_table); i++)
323 if (scale == fxls8962af_scale_table[i][1])
326 if (i == ARRAY_SIZE(fxls8962af_scale_table))
329 return fxls8962af_update_config(data, FXLS8962AF_SENS_CONFIG1,
330 FXLS8962AF_SC1_FSR_MASK,
331 FXLS8962AF_SC1_FSR_PREP(i));
334 static unsigned int fxls8962af_read_full_scale(struct fxls8962af_data *data,
341 ret = regmap_read(data->regmap, FXLS8962AF_SENS_CONFIG1, ®);
345 range_idx = FXLS8962AF_SC1_FSR_GET(reg);
347 *val = fxls8962af_scale_table[range_idx][1];
349 return IIO_VAL_INT_PLUS_NANO;
352 static int fxls8962af_set_samp_freq(struct fxls8962af_data *data, u32 val,
357 for (i = 0; i < ARRAY_SIZE(fxls8962af_samp_freq_table); i++)
358 if (val == fxls8962af_samp_freq_table[i][0] &&
359 val2 == fxls8962af_samp_freq_table[i][1])
362 if (i == ARRAY_SIZE(fxls8962af_samp_freq_table))
365 return fxls8962af_update_config(data, FXLS8962AF_SENS_CONFIG3,
366 FXLS8962AF_SC3_WAKE_ODR_MASK,
367 FXLS8962AF_SC3_WAKE_ODR_PREP(i));
370 static unsigned int fxls8962af_read_samp_freq(struct fxls8962af_data *data,
377 ret = regmap_read(data->regmap, FXLS8962AF_SENS_CONFIG3, ®);
381 range_idx = FXLS8962AF_SC3_WAKE_ODR_GET(reg);
383 *val = fxls8962af_samp_freq_table[range_idx][0];
384 *val2 = fxls8962af_samp_freq_table[range_idx][1];
386 return IIO_VAL_INT_PLUS_MICRO;
389 static int fxls8962af_read_raw(struct iio_dev *indio_dev,
390 struct iio_chan_spec const *chan,
391 int *val, int *val2, long mask)
393 struct fxls8962af_data *data = iio_priv(indio_dev);
396 case IIO_CHAN_INFO_RAW:
397 switch (chan->type) {
400 return fxls8962af_get_out(data, chan, val);
404 case IIO_CHAN_INFO_OFFSET:
405 if (chan->type != IIO_TEMP)
408 *val = FXLS8962AF_TEMP_CENTER_VAL;
410 case IIO_CHAN_INFO_SCALE:
412 return fxls8962af_read_full_scale(data, val2);
413 case IIO_CHAN_INFO_SAMP_FREQ:
414 return fxls8962af_read_samp_freq(data, val, val2);
420 static int fxls8962af_write_raw(struct iio_dev *indio_dev,
421 struct iio_chan_spec const *chan,
422 int val, int val2, long mask)
424 struct fxls8962af_data *data = iio_priv(indio_dev);
428 case IIO_CHAN_INFO_SCALE:
432 ret = iio_device_claim_direct_mode(indio_dev);
436 ret = fxls8962af_set_full_scale(data, val2);
438 iio_device_release_direct_mode(indio_dev);
440 case IIO_CHAN_INFO_SAMP_FREQ:
441 ret = iio_device_claim_direct_mode(indio_dev);
445 ret = fxls8962af_set_samp_freq(data, val, val2);
447 iio_device_release_direct_mode(indio_dev);
454 static int fxls8962af_set_watermark(struct iio_dev *indio_dev, unsigned val)
456 struct fxls8962af_data *data = iio_priv(indio_dev);
458 if (val > FXLS8962AF_FIFO_LENGTH)
459 val = FXLS8962AF_FIFO_LENGTH;
461 data->watermark = val;
466 #define FXLS8962AF_CHANNEL(axis, reg, idx) { \
470 .channel2 = IIO_MOD_##axis, \
471 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
472 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
473 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
474 .info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SCALE) | \
475 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
482 .endianness = IIO_BE, \
486 #define FXLS8962AF_TEMP_CHANNEL { \
488 .address = FXLS8962AF_TEMP_OUT, \
489 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
490 BIT(IIO_CHAN_INFO_OFFSET),\
498 static const struct iio_chan_spec fxls8962af_channels[] = {
499 FXLS8962AF_CHANNEL(X, FXLS8962AF_OUT_X_LSB, fxls8962af_idx_x),
500 FXLS8962AF_CHANNEL(Y, FXLS8962AF_OUT_Y_LSB, fxls8962af_idx_y),
501 FXLS8962AF_CHANNEL(Z, FXLS8962AF_OUT_Z_LSB, fxls8962af_idx_z),
502 IIO_CHAN_SOFT_TIMESTAMP(fxls8962af_idx_ts),
503 FXLS8962AF_TEMP_CHANNEL,
506 static const struct fxls8962af_chip_info fxls_chip_info_table[] = {
508 .chip_id = FXLS8962AF_DEVICE_ID,
509 .name = "fxls8962af",
510 .channels = fxls8962af_channels,
511 .num_channels = ARRAY_SIZE(fxls8962af_channels),
514 .chip_id = FXLS8964AF_DEVICE_ID,
515 .name = "fxls8964af",
516 .channels = fxls8962af_channels,
517 .num_channels = ARRAY_SIZE(fxls8962af_channels),
521 static const struct iio_info fxls8962af_info = {
522 .read_raw = &fxls8962af_read_raw,
523 .write_raw = &fxls8962af_write_raw,
524 .write_raw_get_fmt = fxls8962af_write_raw_get_fmt,
525 .read_avail = fxls8962af_read_avail,
526 .hwfifo_set_watermark = fxls8962af_set_watermark,
529 static int fxls8962af_reset(struct fxls8962af_data *data)
531 struct device *dev = regmap_get_device(data->regmap);
535 ret = regmap_update_bits(data->regmap, FXLS8962AF_SENS_CONFIG1,
536 FXLS8962AF_SENS_CONFIG1_RST,
537 FXLS8962AF_SENS_CONFIG1_RST);
541 /* TBOOT1, TBOOT2, specifies we have to wait between 1 - 17.7ms */
542 ret = regmap_read_poll_timeout(data->regmap, FXLS8962AF_INT_STATUS, reg,
543 (reg & FXLS8962AF_INT_STATUS_SRC_BOOT),
545 if (ret == -ETIMEDOUT)
546 dev_err(dev, "reset timeout, int_status = 0x%x\n", reg);
551 static int __fxls8962af_fifo_set_mode(struct fxls8962af_data *data, bool onoff)
555 /* Enable watermark at max fifo size */
556 ret = regmap_update_bits(data->regmap, FXLS8962AF_BUF_CONFIG2,
557 FXLS8962AF_BUF_CONFIG2_BUF_WMRK,
562 return regmap_update_bits(data->regmap, FXLS8962AF_BUF_CONFIG1,
563 FXLS8962AF_BC1_BUF_MODE_MASK,
564 FXLS8962AF_BC1_BUF_MODE_PREP(onoff));
567 static int fxls8962af_buffer_preenable(struct iio_dev *indio_dev)
569 return fxls8962af_power_on(iio_priv(indio_dev));
572 static int fxls8962af_buffer_postenable(struct iio_dev *indio_dev)
574 struct fxls8962af_data *data = iio_priv(indio_dev);
577 fxls8962af_standby(data);
579 /* Enable buffer interrupt */
580 ret = regmap_update_bits(data->regmap, FXLS8962AF_INT_EN,
581 FXLS8962AF_INT_EN_BUF_EN,
582 FXLS8962AF_INT_EN_BUF_EN);
586 ret = __fxls8962af_fifo_set_mode(data, true);
588 fxls8962af_active(data);
593 static int fxls8962af_buffer_predisable(struct iio_dev *indio_dev)
595 struct fxls8962af_data *data = iio_priv(indio_dev);
598 fxls8962af_standby(data);
600 /* Disable buffer interrupt */
601 ret = regmap_update_bits(data->regmap, FXLS8962AF_INT_EN,
602 FXLS8962AF_INT_EN_BUF_EN, 0);
606 ret = __fxls8962af_fifo_set_mode(data, false);
608 fxls8962af_active(data);
613 static int fxls8962af_buffer_postdisable(struct iio_dev *indio_dev)
615 struct fxls8962af_data *data = iio_priv(indio_dev);
617 return fxls8962af_power_off(data);
620 static const struct iio_buffer_setup_ops fxls8962af_buffer_ops = {
621 .preenable = fxls8962af_buffer_preenable,
622 .postenable = fxls8962af_buffer_postenable,
623 .predisable = fxls8962af_buffer_predisable,
624 .postdisable = fxls8962af_buffer_postdisable,
627 static int fxls8962af_i2c_raw_read_errata3(struct fxls8962af_data *data,
628 u16 *buffer, int samples,
633 for (i = 0; i < samples; i++) {
634 ret = regmap_raw_read(data->regmap, FXLS8962AF_BUF_X_LSB,
635 &buffer[i * 3], sample_length);
643 static int fxls8962af_fifo_transfer(struct fxls8962af_data *data,
644 u16 *buffer, int samples)
646 struct device *dev = regmap_get_device(data->regmap);
647 int sample_length = 3 * sizeof(*buffer);
648 int total_length = samples * sample_length;
651 if (i2c_verify_client(dev))
654 * E3: FIFO burst read operation error using I2C interface
655 * We have to avoid burst reads on I2C..
657 ret = fxls8962af_i2c_raw_read_errata3(data, buffer, samples,
660 ret = regmap_raw_read(data->regmap, FXLS8962AF_BUF_X_LSB, buffer,
664 dev_err(dev, "Error transferring data from fifo: %d\n", ret);
669 static int fxls8962af_fifo_flush(struct iio_dev *indio_dev)
671 struct fxls8962af_data *data = iio_priv(indio_dev);
672 struct device *dev = regmap_get_device(data->regmap);
673 u16 buffer[FXLS8962AF_FIFO_LENGTH * 3];
674 uint64_t sample_period;
680 ret = regmap_read(data->regmap, FXLS8962AF_BUF_STATUS, ®);
684 if (reg & FXLS8962AF_BUF_STATUS_BUF_OVF) {
685 dev_err(dev, "Buffer overflow");
689 count = reg & FXLS8962AF_BUF_STATUS_BUF_CNT;
693 data->old_timestamp = data->timestamp;
694 data->timestamp = iio_get_time_ns(indio_dev);
697 * Approximate timestamps for each of the sample based on the sampling,
698 * frequency, timestamp for last sample and number of samples.
700 sample_period = (data->timestamp - data->old_timestamp);
701 do_div(sample_period, count);
702 tstamp = data->timestamp - (count - 1) * sample_period;
704 ret = fxls8962af_fifo_transfer(data, buffer, count);
708 /* Demux hw FIFO into kfifo. */
709 for (i = 0; i < count; i++) {
713 for_each_set_bit(bit, indio_dev->active_scan_mask,
714 indio_dev->masklength) {
715 memcpy(&data->scan.channels[j++], &buffer[i * 3 + bit],
716 sizeof(data->scan.channels[0]));
719 iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
722 tstamp += sample_period;
728 static irqreturn_t fxls8962af_interrupt(int irq, void *p)
730 struct iio_dev *indio_dev = p;
731 struct fxls8962af_data *data = iio_priv(indio_dev);
735 ret = regmap_read(data->regmap, FXLS8962AF_INT_STATUS, ®);
739 if (reg & FXLS8962AF_INT_STATUS_SRC_BUF) {
740 ret = fxls8962af_fifo_flush(indio_dev);
750 static void fxls8962af_regulator_disable(void *data_ptr)
752 struct fxls8962af_data *data = data_ptr;
754 regulator_disable(data->vdd_reg);
757 static void fxls8962af_pm_disable(void *dev_ptr)
759 struct device *dev = dev_ptr;
760 struct iio_dev *indio_dev = dev_get_drvdata(dev);
762 pm_runtime_disable(dev);
763 pm_runtime_set_suspended(dev);
764 pm_runtime_put_noidle(dev);
766 fxls8962af_standby(iio_priv(indio_dev));
769 static void fxls8962af_get_irq(struct device_node *of_node,
770 enum fxls8962af_int_pin *pin)
774 irq = of_irq_get_byname(of_node, "INT2");
776 *pin = FXLS8962AF_PIN_INT2;
780 *pin = FXLS8962AF_PIN_INT1;
783 static int fxls8962af_irq_setup(struct iio_dev *indio_dev, int irq)
785 struct fxls8962af_data *data = iio_priv(indio_dev);
786 struct device *dev = regmap_get_device(data->regmap);
787 unsigned long irq_type;
788 bool irq_active_high;
789 enum fxls8962af_int_pin int_pin;
793 fxls8962af_get_irq(dev->of_node, &int_pin);
795 case FXLS8962AF_PIN_INT1:
796 int_pin_sel = FXLS8962AF_INT_PIN_SEL_INT1;
798 case FXLS8962AF_PIN_INT2:
799 int_pin_sel = FXLS8962AF_INT_PIN_SEL_INT2;
802 dev_err(dev, "unsupported int pin selected\n");
806 ret = regmap_update_bits(data->regmap, FXLS8962AF_INT_PIN_SEL,
807 FXLS8962AF_INT_PIN_SEL_MASK, int_pin_sel);
811 irq_type = irqd_get_trigger_type(irq_get_irq_data(irq));
814 case IRQF_TRIGGER_HIGH:
815 case IRQF_TRIGGER_RISING:
816 irq_active_high = true;
818 case IRQF_TRIGGER_LOW:
819 case IRQF_TRIGGER_FALLING:
820 irq_active_high = false;
823 dev_info(dev, "mode %lx unsupported\n", irq_type);
827 ret = regmap_update_bits(data->regmap, FXLS8962AF_SENS_CONFIG4,
828 FXLS8962AF_SC4_INT_POL_MASK,
829 FXLS8962AF_SC4_INT_POL_PREP(irq_active_high));
833 if (device_property_read_bool(dev, "drive-open-drain")) {
834 ret = regmap_update_bits(data->regmap, FXLS8962AF_SENS_CONFIG4,
835 FXLS8962AF_SC4_INT_PP_OD_MASK,
836 FXLS8962AF_SC4_INT_PP_OD_PREP(1));
840 irq_type |= IRQF_SHARED;
843 return devm_request_threaded_irq(dev,
845 NULL, fxls8962af_interrupt,
846 irq_type | IRQF_ONESHOT,
847 indio_dev->name, indio_dev);
850 int fxls8962af_core_probe(struct device *dev, struct regmap *regmap, int irq)
852 struct fxls8962af_data *data;
853 struct iio_dev *indio_dev;
857 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
861 data = iio_priv(indio_dev);
862 dev_set_drvdata(dev, indio_dev);
863 data->regmap = regmap;
865 ret = iio_read_mount_matrix(dev, &data->orientation);
869 data->vdd_reg = devm_regulator_get(dev, "vdd");
870 if (IS_ERR(data->vdd_reg))
871 return dev_err_probe(dev, PTR_ERR(data->vdd_reg),
872 "Failed to get vdd regulator\n");
874 ret = regulator_enable(data->vdd_reg);
876 dev_err(dev, "Failed to enable vdd regulator: %d\n", ret);
880 ret = devm_add_action_or_reset(dev, fxls8962af_regulator_disable, data);
884 ret = regmap_read(data->regmap, FXLS8962AF_WHO_AM_I, ®);
888 for (i = 0; i < ARRAY_SIZE(fxls_chip_info_table); i++) {
889 if (fxls_chip_info_table[i].chip_id == reg) {
890 data->chip_info = &fxls_chip_info_table[i];
894 if (i == ARRAY_SIZE(fxls_chip_info_table)) {
895 dev_err(dev, "failed to match device in table\n");
899 indio_dev->channels = data->chip_info->channels;
900 indio_dev->num_channels = data->chip_info->num_channels;
901 indio_dev->name = data->chip_info->name;
902 indio_dev->info = &fxls8962af_info;
903 indio_dev->modes = INDIO_DIRECT_MODE;
905 ret = fxls8962af_reset(data);
910 ret = fxls8962af_irq_setup(indio_dev, irq);
914 ret = devm_iio_kfifo_buffer_setup(dev, indio_dev,
915 INDIO_BUFFER_SOFTWARE,
916 &fxls8962af_buffer_ops);
921 ret = pm_runtime_set_active(dev);
925 pm_runtime_enable(dev);
926 pm_runtime_set_autosuspend_delay(dev, FXLS8962AF_AUTO_SUSPEND_DELAY_MS);
927 pm_runtime_use_autosuspend(dev);
929 ret = devm_add_action_or_reset(dev, fxls8962af_pm_disable, dev);
933 return devm_iio_device_register(dev, indio_dev);
935 EXPORT_SYMBOL_GPL(fxls8962af_core_probe);
937 static int __maybe_unused fxls8962af_runtime_suspend(struct device *dev)
939 struct fxls8962af_data *data = iio_priv(dev_get_drvdata(dev));
942 ret = fxls8962af_standby(data);
944 dev_err(dev, "powering off device failed\n");
951 static int __maybe_unused fxls8962af_runtime_resume(struct device *dev)
953 struct fxls8962af_data *data = iio_priv(dev_get_drvdata(dev));
955 return fxls8962af_active(data);
958 const struct dev_pm_ops fxls8962af_pm_ops = {
959 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
960 pm_runtime_force_resume)
961 SET_RUNTIME_PM_OPS(fxls8962af_runtime_suspend,
962 fxls8962af_runtime_resume, NULL)
964 EXPORT_SYMBOL_GPL(fxls8962af_pm_ops);
966 MODULE_AUTHOR("Sean Nyekjaer <sean@geanix.com>");
967 MODULE_DESCRIPTION("NXP FXLS8962AF/FXLS8964AF accelerometer driver");
968 MODULE_LICENSE("GPL v2");