1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2022 ROHM Semiconductors
5 * ROHM/KIONIX KX022A accelerometer driver
8 #include <linux/delay.h>
9 #include <linux/device.h>
10 #include <linux/interrupt.h>
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/mutex.h>
14 #include <linux/property.h>
15 #include <linux/regmap.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/slab.h>
18 #include <linux/string_helpers.h>
19 #include <linux/units.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/trigger.h>
24 #include <linux/iio/trigger_consumer.h>
25 #include <linux/iio/triggered_buffer.h>
27 #include "kionix-kx022a.h"
30 * The KX022A has FIFO which can store 43 samples of HiRes data from 2
31 * channels. This equals to 43 (samples) * 3 (channels) * 2 (bytes/sample) to
32 * 258 bytes of sample data. The quirk to know is that the amount of bytes in
33 * the FIFO is advertised via 8 bit register (max value 255). The thing to note
34 * is that full 258 bytes of data is indicated using the max value 255.
36 #define KX022A_FIFO_LENGTH 43
37 #define KX022A_FIFO_FULL_VALUE 255
38 #define KX022A_SOFT_RESET_WAIT_TIME_US (5 * USEC_PER_MSEC)
39 #define KX022A_SOFT_RESET_TOTAL_WAIT_TIME_US (500 * USEC_PER_MSEC)
41 /* 3 axis, 2 bytes of data for each of the axis */
42 #define KX022A_FIFO_SAMPLES_SIZE_BYTES 6
43 #define KX022A_FIFO_MAX_BYTES \
44 (KX022A_FIFO_LENGTH * KX022A_FIFO_SAMPLES_SIZE_BYTES)
52 static const struct regmap_range kx022a_volatile_ranges[] = {
54 .range_min = KX022A_REG_XHP_L,
55 .range_max = KX022A_REG_COTR,
57 .range_min = KX022A_REG_TSCP,
58 .range_max = KX022A_REG_INT_REL,
60 /* The reset bit will be cleared by sensor */
61 .range_min = KX022A_REG_CNTL2,
62 .range_max = KX022A_REG_CNTL2,
64 .range_min = KX022A_REG_BUF_STATUS_1,
65 .range_max = KX022A_REG_BUF_READ,
69 static const struct regmap_access_table kx022a_volatile_regs = {
70 .yes_ranges = &kx022a_volatile_ranges[0],
71 .n_yes_ranges = ARRAY_SIZE(kx022a_volatile_ranges),
74 static const struct regmap_range kx022a_precious_ranges[] = {
76 .range_min = KX022A_REG_INT_REL,
77 .range_max = KX022A_REG_INT_REL,
81 static const struct regmap_access_table kx022a_precious_regs = {
82 .yes_ranges = &kx022a_precious_ranges[0],
83 .n_yes_ranges = ARRAY_SIZE(kx022a_precious_ranges),
87 * The HW does not set WHO_AM_I reg as read-only but we don't want to write it
88 * so we still include it in the read-only ranges.
90 static const struct regmap_range kx022a_read_only_ranges[] = {
92 .range_min = KX022A_REG_XHP_L,
93 .range_max = KX022A_REG_INT_REL,
95 .range_min = KX022A_REG_BUF_STATUS_1,
96 .range_max = KX022A_REG_BUF_STATUS_2,
98 .range_min = KX022A_REG_BUF_READ,
99 .range_max = KX022A_REG_BUF_READ,
103 static const struct regmap_access_table kx022a_ro_regs = {
104 .no_ranges = &kx022a_read_only_ranges[0],
105 .n_no_ranges = ARRAY_SIZE(kx022a_read_only_ranges),
108 static const struct regmap_range kx022a_write_only_ranges[] = {
110 .range_min = KX022A_REG_BTS_WUF_TH,
111 .range_max = KX022A_REG_BTS_WUF_TH,
113 .range_min = KX022A_REG_MAN_WAKE,
114 .range_max = KX022A_REG_MAN_WAKE,
116 .range_min = KX022A_REG_SELF_TEST,
117 .range_max = KX022A_REG_SELF_TEST,
119 .range_min = KX022A_REG_BUF_CLEAR,
120 .range_max = KX022A_REG_BUF_CLEAR,
124 static const struct regmap_access_table kx022a_wo_regs = {
125 .no_ranges = &kx022a_write_only_ranges[0],
126 .n_no_ranges = ARRAY_SIZE(kx022a_write_only_ranges),
129 static const struct regmap_range kx022a_noinc_read_ranges[] = {
131 .range_min = KX022A_REG_BUF_READ,
132 .range_max = KX022A_REG_BUF_READ,
136 static const struct regmap_access_table kx022a_nir_regs = {
137 .yes_ranges = &kx022a_noinc_read_ranges[0],
138 .n_yes_ranges = ARRAY_SIZE(kx022a_noinc_read_ranges),
141 const struct regmap_config kx022a_regmap = {
144 .volatile_table = &kx022a_volatile_regs,
145 .rd_table = &kx022a_wo_regs,
146 .wr_table = &kx022a_ro_regs,
147 .rd_noinc_table = &kx022a_nir_regs,
148 .precious_table = &kx022a_precious_regs,
149 .max_register = KX022A_MAX_REGISTER,
150 .cache_type = REGCACHE_RBTREE,
152 EXPORT_SYMBOL_NS_GPL(kx022a_regmap, IIO_KX022A);
155 struct regmap *regmap;
156 struct iio_trigger *trig;
158 struct iio_mount_matrix orientation;
159 int64_t timestamp, old_timestamp;
168 bool trigger_enabled;
170 * Prevent toggling the sensor stby/active state (PC1 bit) in the
171 * middle of a configuration, or when the fifo is enabled. Also,
172 * protect the data stored/retrieved from this structure from
173 * concurrent accesses.
178 /* 3 x 16bit accel data + timestamp */
179 __le16 buffer[8] __aligned(IIO_DMA_MINALIGN);
186 static const struct iio_mount_matrix *
187 kx022a_get_mount_matrix(const struct iio_dev *idev,
188 const struct iio_chan_spec *chan)
190 struct kx022a_data *data = iio_priv(idev);
192 return &data->orientation;
202 static const unsigned long kx022a_scan_masks[] = {
203 BIT(AXIS_X) | BIT(AXIS_Y) | BIT(AXIS_Z), 0
206 static const struct iio_chan_spec_ext_info kx022a_ext_info[] = {
207 IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, kx022a_get_mount_matrix),
211 #define KX022A_ACCEL_CHAN(axis, index) \
215 .channel2 = IIO_MOD_##axis, \
216 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
217 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
218 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
219 .info_mask_shared_by_type_available = \
220 BIT(IIO_CHAN_INFO_SCALE) | \
221 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
222 .ext_info = kx022a_ext_info, \
223 .address = KX022A_REG_##axis##OUT_L, \
224 .scan_index = index, \
229 .endianness = IIO_LE, \
233 static const struct iio_chan_spec kx022a_channels[] = {
234 KX022A_ACCEL_CHAN(X, 0),
235 KX022A_ACCEL_CHAN(Y, 1),
236 KX022A_ACCEL_CHAN(Z, 2),
237 IIO_CHAN_SOFT_TIMESTAMP(3),
241 * The sensor HW can support ODR up to 1600 Hz, which is beyond what most of the
242 * Linux CPUs can handle without dropping samples. Also, the low power mode is
243 * not available for higher sample rates. Thus, the driver only supports 200 Hz
244 * and slower ODRs. The slowest is 0.78 Hz.
246 static const int kx022a_accel_samp_freq_table[][2] = {
258 static const unsigned int kx022a_odrs[] = {
271 * range is typically +-2G/4G/8G/16G, distributed over the amount of bits.
272 * The scale table can be calculated using
273 * (range / 2^bits) * g = (range / 2^bits) * 9.80665 m/s^2
274 * => KX022A uses 16 bit (HiRes mode - assume the low 8 bits are zeroed
275 * in low-power mode(?) )
276 * => +/-2G => 4 / 2^16 * 9,80665 * 10^6 (to scale to micro)
277 * => +/-2G - 598.550415
280 * +/-16G - 4788.40332
282 static const int kx022a_scale_table[][2] = {
289 static int kx022a_read_avail(struct iio_dev *indio_dev,
290 struct iio_chan_spec const *chan,
291 const int **vals, int *type, int *length,
295 case IIO_CHAN_INFO_SAMP_FREQ:
296 *vals = (const int *)kx022a_accel_samp_freq_table;
297 *length = ARRAY_SIZE(kx022a_accel_samp_freq_table) *
298 ARRAY_SIZE(kx022a_accel_samp_freq_table[0]);
299 *type = IIO_VAL_INT_PLUS_MICRO;
300 return IIO_AVAIL_LIST;
301 case IIO_CHAN_INFO_SCALE:
302 *vals = (const int *)kx022a_scale_table;
303 *length = ARRAY_SIZE(kx022a_scale_table) *
304 ARRAY_SIZE(kx022a_scale_table[0]);
305 *type = IIO_VAL_INT_PLUS_MICRO;
306 return IIO_AVAIL_LIST;
312 #define KX022A_DEFAULT_PERIOD_NS (20 * NSEC_PER_MSEC)
314 static void kx022a_reg2freq(unsigned int val, int *val1, int *val2)
316 *val1 = kx022a_accel_samp_freq_table[val & KX022A_MASK_ODR][0];
317 *val2 = kx022a_accel_samp_freq_table[val & KX022A_MASK_ODR][1];
320 static void kx022a_reg2scale(unsigned int val, unsigned int *val1,
323 val &= KX022A_MASK_GSEL;
324 val >>= KX022A_GSEL_SHIFT;
326 *val1 = kx022a_scale_table[val][0];
327 *val2 = kx022a_scale_table[val][1];
330 static int kx022a_turn_on_off_unlocked(struct kx022a_data *data, bool on)
335 ret = regmap_set_bits(data->regmap, KX022A_REG_CNTL,
338 ret = regmap_clear_bits(data->regmap, KX022A_REG_CNTL,
341 dev_err(data->dev, "Turn %s fail %d\n", str_on_off(on), ret);
347 static int kx022a_turn_off_lock(struct kx022a_data *data)
351 mutex_lock(&data->mutex);
352 ret = kx022a_turn_on_off_unlocked(data, false);
354 mutex_unlock(&data->mutex);
359 static int kx022a_turn_on_unlock(struct kx022a_data *data)
363 ret = kx022a_turn_on_off_unlocked(data, true);
364 mutex_unlock(&data->mutex);
369 static int kx022a_write_raw(struct iio_dev *idev,
370 struct iio_chan_spec const *chan,
371 int val, int val2, long mask)
373 struct kx022a_data *data = iio_priv(idev);
377 * We should not allow changing scale or frequency when FIFO is running
378 * as it will mess the timestamp/scale for samples existing in the
379 * buffer. If this turns out to be an issue we can later change logic
380 * to internally flush the fifo before reconfiguring so the samples in
381 * fifo keep matching the freq/scale settings. (Such setup could cause
382 * issues if users trust the watermark to be reached within known
385 ret = iio_device_claim_direct_mode(idev);
390 case IIO_CHAN_INFO_SAMP_FREQ:
391 n = ARRAY_SIZE(kx022a_accel_samp_freq_table);
394 if (val == kx022a_accel_samp_freq_table[n][0] &&
395 val2 == kx022a_accel_samp_freq_table[n][1])
401 ret = kx022a_turn_off_lock(data);
405 ret = regmap_update_bits(data->regmap,
408 data->odr_ns = kx022a_odrs[n];
409 kx022a_turn_on_unlock(data);
411 case IIO_CHAN_INFO_SCALE:
412 n = ARRAY_SIZE(kx022a_scale_table);
415 if (val == kx022a_scale_table[n][0] &&
416 val2 == kx022a_scale_table[n][1])
423 ret = kx022a_turn_off_lock(data);
427 ret = regmap_update_bits(data->regmap, KX022A_REG_CNTL,
429 n << KX022A_GSEL_SHIFT);
430 kx022a_turn_on_unlock(data);
438 iio_device_release_direct_mode(idev);
443 static int kx022a_fifo_set_wmi(struct kx022a_data *data)
447 threshold = data->watermark;
449 return regmap_update_bits(data->regmap, KX022A_REG_BUF_CNTL1,
450 KX022A_MASK_WM_TH, threshold);
453 static int kx022a_get_axis(struct kx022a_data *data,
454 struct iio_chan_spec const *chan,
459 ret = regmap_bulk_read(data->regmap, chan->address, &data->buffer[0],
464 *val = le16_to_cpu(data->buffer[0]);
469 static int kx022a_read_raw(struct iio_dev *idev,
470 struct iio_chan_spec const *chan,
471 int *val, int *val2, long mask)
473 struct kx022a_data *data = iio_priv(idev);
478 case IIO_CHAN_INFO_RAW:
479 ret = iio_device_claim_direct_mode(idev);
483 mutex_lock(&data->mutex);
484 ret = kx022a_get_axis(data, chan, val);
485 mutex_unlock(&data->mutex);
487 iio_device_release_direct_mode(idev);
491 case IIO_CHAN_INFO_SAMP_FREQ:
492 ret = regmap_read(data->regmap, KX022A_REG_ODCNTL, ®val);
496 if ((regval & KX022A_MASK_ODR) >
497 ARRAY_SIZE(kx022a_accel_samp_freq_table)) {
498 dev_err(data->dev, "Invalid ODR\n");
502 kx022a_reg2freq(regval, val, val2);
504 return IIO_VAL_INT_PLUS_MICRO;
506 case IIO_CHAN_INFO_SCALE:
507 ret = regmap_read(data->regmap, KX022A_REG_CNTL, ®val);
511 kx022a_reg2scale(regval, val, val2);
513 return IIO_VAL_INT_PLUS_MICRO;
519 static int kx022a_validate_trigger(struct iio_dev *idev,
520 struct iio_trigger *trig)
522 struct kx022a_data *data = iio_priv(idev);
524 if (data->trig != trig)
530 static int kx022a_set_watermark(struct iio_dev *idev, unsigned int val)
532 struct kx022a_data *data = iio_priv(idev);
534 if (val > KX022A_FIFO_LENGTH)
535 val = KX022A_FIFO_LENGTH;
537 mutex_lock(&data->mutex);
538 data->watermark = val;
539 mutex_unlock(&data->mutex);
544 static ssize_t hwfifo_enabled_show(struct device *dev,
545 struct device_attribute *attr,
548 struct iio_dev *idev = dev_to_iio_dev(dev);
549 struct kx022a_data *data = iio_priv(idev);
552 mutex_lock(&data->mutex);
554 mutex_unlock(&data->mutex);
556 return sysfs_emit(buf, "%d\n", state);
559 static ssize_t hwfifo_watermark_show(struct device *dev,
560 struct device_attribute *attr,
563 struct iio_dev *idev = dev_to_iio_dev(dev);
564 struct kx022a_data *data = iio_priv(idev);
567 mutex_lock(&data->mutex);
568 wm = data->watermark;
569 mutex_unlock(&data->mutex);
571 return sysfs_emit(buf, "%d\n", wm);
574 static IIO_DEVICE_ATTR_RO(hwfifo_enabled, 0);
575 static IIO_DEVICE_ATTR_RO(hwfifo_watermark, 0);
577 static const struct iio_dev_attr *kx022a_fifo_attributes[] = {
578 &iio_dev_attr_hwfifo_watermark,
579 &iio_dev_attr_hwfifo_enabled,
583 static int kx022a_drop_fifo_contents(struct kx022a_data *data)
586 * We must clear the old time-stamp to avoid computing the timestamps
587 * based on samples acquired when buffer was last enabled.
589 * We don't need to protect the timestamp as long as we are only
590 * called from fifo-disable where we can guarantee the sensor is not
591 * triggering interrupts and where the mutex is locked to prevent the
596 return regmap_write(data->regmap, KX022A_REG_BUF_CLEAR, 0x0);
599 static int __kx022a_fifo_flush(struct iio_dev *idev, unsigned int samples,
602 struct kx022a_data *data = iio_priv(idev);
603 struct device *dev = regmap_get_device(data->regmap);
604 __le16 buffer[KX022A_FIFO_LENGTH * 3];
605 uint64_t sample_period;
606 int count, fifo_bytes;
607 bool renable = false;
611 ret = regmap_read(data->regmap, KX022A_REG_BUF_STATUS_1, &fifo_bytes);
613 dev_err(dev, "Error reading buffer status\n");
617 /* Let's not overflow if we for some reason get bogus value from i2c */
618 if (fifo_bytes == KX022A_FIFO_FULL_VALUE)
619 fifo_bytes = KX022A_FIFO_MAX_BYTES;
621 if (fifo_bytes % KX022A_FIFO_SAMPLES_SIZE_BYTES)
622 dev_warn(data->dev, "Bad FIFO alignment. Data may be corrupt\n");
624 count = fifo_bytes / KX022A_FIFO_SAMPLES_SIZE_BYTES;
629 * If we are being called from IRQ handler we know the stored timestamp
630 * is fairly accurate for the last stored sample. Otherwise, if we are
631 * called as a result of a read operation from userspace and hence
632 * before the watermark interrupt was triggered, take a timestamp
633 * now. We can fall anywhere in between two samples so the error in this
634 * case is at most one sample period.
638 * We need to have the IRQ disabled or we risk of messing-up
639 * the timestamps. If we are ran from IRQ, then the
640 * IRQF_ONESHOT has us covered - but if we are ran by the
641 * user-space read we need to disable the IRQ to be on a safe
642 * side. We do this usng synchronous disable so that if the
643 * IRQ thread is being ran on other CPU we wait for it to be
646 disable_irq(data->irq);
649 data->old_timestamp = data->timestamp;
650 data->timestamp = iio_get_time_ns(idev);
654 * Approximate timestamps for each of the sample based on the sampling
655 * frequency, timestamp for last sample and number of samples.
657 * We'd better not use the current bandwidth settings to compute the
658 * sample period. The real sample rate varies with the device and
659 * small variation adds when we store a large number of samples.
661 * To avoid this issue we compute the actual sample period ourselves
662 * based on the timestamp delta between the last two flush operations.
664 if (data->old_timestamp) {
665 sample_period = data->timestamp - data->old_timestamp;
666 do_div(sample_period, count);
668 sample_period = data->odr_ns;
670 tstamp = data->timestamp - (count - 1) * sample_period;
672 if (samples && count > samples) {
674 * Here we leave some old samples to the buffer. We need to
675 * adjust the timestamp to match the first sample in the buffer
676 * or we will miscalculate the sample_period at next round.
678 data->timestamp -= (count - samples) * sample_period;
682 fifo_bytes = count * KX022A_FIFO_SAMPLES_SIZE_BYTES;
683 ret = regmap_noinc_read(data->regmap, KX022A_REG_BUF_READ,
684 &buffer[0], fifo_bytes);
688 for (i = 0; i < count; i++) {
689 __le16 *sam = &buffer[i * 3];
693 chs = &data->scan.channels[0];
694 for_each_set_bit(bit, idev->active_scan_mask, AXIS_MAX)
697 iio_push_to_buffers_with_timestamp(idev, &data->scan, tstamp);
699 tstamp += sample_period;
706 enable_irq(data->irq);
711 static int kx022a_fifo_flush(struct iio_dev *idev, unsigned int samples)
713 struct kx022a_data *data = iio_priv(idev);
716 mutex_lock(&data->mutex);
717 ret = __kx022a_fifo_flush(idev, samples, false);
718 mutex_unlock(&data->mutex);
723 static const struct iio_info kx022a_info = {
724 .read_raw = &kx022a_read_raw,
725 .write_raw = &kx022a_write_raw,
726 .read_avail = &kx022a_read_avail,
728 .validate_trigger = kx022a_validate_trigger,
729 .hwfifo_set_watermark = kx022a_set_watermark,
730 .hwfifo_flush_to_buffer = kx022a_fifo_flush,
733 static int kx022a_set_drdy_irq(struct kx022a_data *data, bool en)
736 return regmap_set_bits(data->regmap, KX022A_REG_CNTL,
739 return regmap_clear_bits(data->regmap, KX022A_REG_CNTL,
743 static int kx022a_prepare_irq_pin(struct kx022a_data *data)
745 /* Enable IRQ1 pin. Set polarity to active low */
746 int mask = KX022A_MASK_IEN | KX022A_MASK_IPOL |
748 int val = KX022A_MASK_IEN | KX022A_IPOL_LOW |
752 ret = regmap_update_bits(data->regmap, data->inc_reg, mask, val);
756 /* We enable WMI to IRQ pin only at buffer_enable */
757 mask = KX022A_MASK_INS2_DRDY;
759 return regmap_set_bits(data->regmap, data->ien_reg, mask);
762 static int kx022a_fifo_disable(struct kx022a_data *data)
766 ret = kx022a_turn_off_lock(data);
770 ret = regmap_clear_bits(data->regmap, data->ien_reg, KX022A_MASK_WMI);
774 ret = regmap_clear_bits(data->regmap, KX022A_REG_BUF_CNTL2,
779 data->state &= ~KX022A_STATE_FIFO;
781 kx022a_drop_fifo_contents(data);
783 return kx022a_turn_on_unlock(data);
786 mutex_unlock(&data->mutex);
791 static int kx022a_buffer_predisable(struct iio_dev *idev)
793 struct kx022a_data *data = iio_priv(idev);
795 if (iio_device_get_current_mode(idev) == INDIO_BUFFER_TRIGGERED)
798 return kx022a_fifo_disable(data);
801 static int kx022a_fifo_enable(struct kx022a_data *data)
805 ret = kx022a_turn_off_lock(data);
809 /* Update watermark to HW */
810 ret = kx022a_fifo_set_wmi(data);
815 ret = regmap_set_bits(data->regmap, KX022A_REG_BUF_CNTL2,
820 data->state |= KX022A_STATE_FIFO;
821 ret = regmap_set_bits(data->regmap, data->ien_reg,
826 return kx022a_turn_on_unlock(data);
829 mutex_unlock(&data->mutex);
834 static int kx022a_buffer_postenable(struct iio_dev *idev)
836 struct kx022a_data *data = iio_priv(idev);
839 * If we use data-ready trigger, then the IRQ masks should be handled by
840 * trigger enable and the hardware buffer is not used but we just update
841 * results to the IIO fifo when data-ready triggers.
843 if (iio_device_get_current_mode(idev) == INDIO_BUFFER_TRIGGERED)
846 return kx022a_fifo_enable(data);
849 static const struct iio_buffer_setup_ops kx022a_buffer_ops = {
850 .postenable = kx022a_buffer_postenable,
851 .predisable = kx022a_buffer_predisable,
854 static irqreturn_t kx022a_trigger_handler(int irq, void *p)
856 struct iio_poll_func *pf = p;
857 struct iio_dev *idev = pf->indio_dev;
858 struct kx022a_data *data = iio_priv(idev);
861 ret = regmap_bulk_read(data->regmap, KX022A_REG_XOUT_L, data->buffer,
862 KX022A_FIFO_SAMPLES_SIZE_BYTES);
866 iio_push_to_buffers_with_timestamp(idev, data->buffer, data->timestamp);
868 iio_trigger_notify_done(idev->trig);
873 /* Get timestamps and wake the thread if we need to read data */
874 static irqreturn_t kx022a_irq_handler(int irq, void *private)
876 struct iio_dev *idev = private;
877 struct kx022a_data *data = iio_priv(idev);
879 data->old_timestamp = data->timestamp;
880 data->timestamp = iio_get_time_ns(idev);
882 if (data->state & KX022A_STATE_FIFO || data->trigger_enabled)
883 return IRQ_WAKE_THREAD;
889 * WMI and data-ready IRQs are acked when results are read. If we add
890 * TILT/WAKE or other IRQs - then we may need to implement the acking
893 static irqreturn_t kx022a_irq_thread_handler(int irq, void *private)
895 struct iio_dev *idev = private;
896 struct kx022a_data *data = iio_priv(idev);
897 irqreturn_t ret = IRQ_NONE;
899 mutex_lock(&data->mutex);
901 if (data->trigger_enabled) {
902 iio_trigger_poll_nested(data->trig);
906 if (data->state & KX022A_STATE_FIFO) {
909 ok = __kx022a_fifo_flush(idev, KX022A_FIFO_LENGTH, true);
914 mutex_unlock(&data->mutex);
919 static int kx022a_trigger_set_state(struct iio_trigger *trig,
922 struct kx022a_data *data = iio_trigger_get_drvdata(trig);
925 mutex_lock(&data->mutex);
927 if (data->trigger_enabled == state)
930 if (data->state & KX022A_STATE_FIFO) {
931 dev_warn(data->dev, "Can't set trigger when FIFO enabled\n");
936 ret = kx022a_turn_on_off_unlocked(data, false);
940 data->trigger_enabled = state;
941 ret = kx022a_set_drdy_irq(data, state);
945 ret = kx022a_turn_on_off_unlocked(data, true);
948 mutex_unlock(&data->mutex);
953 static const struct iio_trigger_ops kx022a_trigger_ops = {
954 .set_trigger_state = kx022a_trigger_set_state,
957 static int kx022a_chip_init(struct kx022a_data *data)
961 /* Reset the senor */
962 ret = regmap_write(data->regmap, KX022A_REG_CNTL2, KX022A_MASK_SRST);
967 * I've seen I2C read failures if we poll too fast after the sensor
968 * reset. Slight delay gives I2C block the time to recover.
972 ret = regmap_read_poll_timeout(data->regmap, KX022A_REG_CNTL2, val,
973 !(val & KX022A_MASK_SRST),
974 KX022A_SOFT_RESET_WAIT_TIME_US,
975 KX022A_SOFT_RESET_TOTAL_WAIT_TIME_US);
977 dev_err(data->dev, "Sensor reset %s\n",
978 val & KX022A_MASK_SRST ? "timeout" : "fail#");
982 ret = regmap_reinit_cache(data->regmap, &kx022a_regmap);
984 dev_err(data->dev, "Failed to reinit reg cache\n");
988 /* set data res 16bit */
989 ret = regmap_set_bits(data->regmap, KX022A_REG_BUF_CNTL2,
992 dev_err(data->dev, "Failed to set data resolution\n");
996 return kx022a_prepare_irq_pin(data);
999 int kx022a_probe_internal(struct device *dev)
1001 static const char * const regulator_names[] = {"io-vdd", "vdd"};
1002 struct iio_trigger *indio_trig;
1003 struct fwnode_handle *fwnode;
1004 struct kx022a_data *data;
1005 struct regmap *regmap;
1006 unsigned int chip_id;
1007 struct iio_dev *idev;
1011 regmap = dev_get_regmap(dev, NULL);
1013 dev_err(dev, "no regmap\n");
1017 fwnode = dev_fwnode(dev);
1021 idev = devm_iio_device_alloc(dev, sizeof(*data));
1025 data = iio_priv(idev);
1028 * VDD is the analog and digital domain voltage supply and
1029 * IO_VDD is the digital I/O voltage supply.
1031 ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(regulator_names),
1033 if (ret && ret != -ENODEV)
1034 return dev_err_probe(dev, ret, "failed to enable regulator\n");
1036 ret = regmap_read(regmap, KX022A_REG_WHO, &chip_id);
1038 return dev_err_probe(dev, ret, "Failed to access sensor\n");
1040 if (chip_id != KX022A_ID) {
1041 dev_err(dev, "unsupported device 0x%x\n", chip_id);
1045 irq = fwnode_irq_get_byname(fwnode, "INT1");
1047 data->inc_reg = KX022A_REG_INC1;
1048 data->ien_reg = KX022A_REG_INC4;
1050 irq = fwnode_irq_get_byname(fwnode, "INT2");
1052 return dev_err_probe(dev, irq, "No suitable IRQ\n");
1054 data->inc_reg = KX022A_REG_INC5;
1055 data->ien_reg = KX022A_REG_INC6;
1058 data->regmap = regmap;
1061 data->odr_ns = KX022A_DEFAULT_PERIOD_NS;
1062 mutex_init(&data->mutex);
1064 idev->channels = kx022a_channels;
1065 idev->num_channels = ARRAY_SIZE(kx022a_channels);
1066 idev->name = "kx022-accel";
1067 idev->info = &kx022a_info;
1068 idev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
1069 idev->available_scan_masks = kx022a_scan_masks;
1071 /* Read the mounting matrix, if present */
1072 ret = iio_read_mount_matrix(dev, &data->orientation);
1076 /* The sensor must be turned off for configuration */
1077 ret = kx022a_turn_off_lock(data);
1081 ret = kx022a_chip_init(data);
1083 mutex_unlock(&data->mutex);
1087 ret = kx022a_turn_on_unlock(data);
1091 ret = devm_iio_triggered_buffer_setup_ext(dev, idev,
1092 &iio_pollfunc_store_time,
1093 kx022a_trigger_handler,
1094 IIO_BUFFER_DIRECTION_IN,
1096 kx022a_fifo_attributes);
1099 return dev_err_probe(data->dev, ret,
1100 "iio_triggered_buffer_setup_ext FAIL\n");
1101 indio_trig = devm_iio_trigger_alloc(dev, "%sdata-rdy-dev%d", idev->name,
1102 iio_device_id(idev));
1106 data->trig = indio_trig;
1108 indio_trig->ops = &kx022a_trigger_ops;
1109 iio_trigger_set_drvdata(indio_trig, data);
1112 * No need to check for NULL. request_threaded_irq() defaults to
1113 * dev_name() should the alloc fail.
1115 name = devm_kasprintf(data->dev, GFP_KERNEL, "%s-kx022a",
1116 dev_name(data->dev));
1118 ret = devm_request_threaded_irq(data->dev, irq, kx022a_irq_handler,
1119 &kx022a_irq_thread_handler,
1120 IRQF_ONESHOT, name, idev);
1122 return dev_err_probe(data->dev, ret, "Could not request IRQ\n");
1125 ret = devm_iio_trigger_register(dev, indio_trig);
1127 return dev_err_probe(data->dev, ret,
1128 "Trigger registration failed\n");
1130 ret = devm_iio_device_register(data->dev, idev);
1132 return dev_err_probe(dev, ret,
1133 "Unable to register iio device\n");
1137 EXPORT_SYMBOL_NS_GPL(kx022a_probe_internal, IIO_KX022A);
1139 MODULE_DESCRIPTION("ROHM/Kionix KX022A accelerometer driver");
1140 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
1141 MODULE_LICENSE("GPL");