1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2022 ROHM Semiconductors
5 * ROHM/KIONIX 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_choices.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)
51 /* kx022a Regmap configs */
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 static const struct regmap_config kx022a_regmap_config
= {
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
,
153 /* Regmap configs kx132 */
154 static const struct regmap_range kx132_volatile_ranges
[] = {
156 .range_min
= KX132_REG_XADP_L
,
157 .range_max
= KX132_REG_COTR
,
159 .range_min
= KX132_REG_TSCP
,
160 .range_max
= KX132_REG_INT_REL
,
162 /* The reset bit will be cleared by sensor */
163 .range_min
= KX132_REG_CNTL2
,
164 .range_max
= KX132_REG_CNTL2
,
166 .range_min
= KX132_REG_CNTL5
,
167 .range_max
= KX132_REG_CNTL5
,
169 .range_min
= KX132_REG_BUF_STATUS_1
,
170 .range_max
= KX132_REG_BUF_READ
,
174 static const struct regmap_access_table kx132_volatile_regs
= {
175 .yes_ranges
= &kx132_volatile_ranges
[0],
176 .n_yes_ranges
= ARRAY_SIZE(kx132_volatile_ranges
),
179 static const struct regmap_range kx132_precious_ranges
[] = {
181 .range_min
= KX132_REG_INT_REL
,
182 .range_max
= KX132_REG_INT_REL
,
186 static const struct regmap_access_table kx132_precious_regs
= {
187 .yes_ranges
= &kx132_precious_ranges
[0],
188 .n_yes_ranges
= ARRAY_SIZE(kx132_precious_ranges
),
191 static const struct regmap_range kx132_read_only_ranges
[] = {
193 .range_min
= KX132_REG_XADP_L
,
194 .range_max
= KX132_REG_INT_REL
,
196 .range_min
= KX132_REG_BUF_STATUS_1
,
197 .range_max
= KX132_REG_BUF_STATUS_2
,
199 .range_min
= KX132_REG_BUF_READ
,
200 .range_max
= KX132_REG_BUF_READ
,
202 /* Kionix reserved registers: should not be written */
220 static const struct regmap_access_table kx132_ro_regs
= {
221 .no_ranges
= &kx132_read_only_ranges
[0],
222 .n_no_ranges
= ARRAY_SIZE(kx132_read_only_ranges
),
225 static const struct regmap_range kx132_write_only_ranges
[] = {
227 .range_min
= KX132_REG_SELF_TEST
,
228 .range_max
= KX132_REG_SELF_TEST
,
230 .range_min
= KX132_REG_BUF_CLEAR
,
231 .range_max
= KX132_REG_BUF_CLEAR
,
235 static const struct regmap_access_table kx132_wo_regs
= {
236 .no_ranges
= &kx132_write_only_ranges
[0],
237 .n_no_ranges
= ARRAY_SIZE(kx132_write_only_ranges
),
240 static const struct regmap_range kx132_noinc_read_ranges
[] = {
242 .range_min
= KX132_REG_BUF_READ
,
243 .range_max
= KX132_REG_BUF_READ
,
247 static const struct regmap_access_table kx132_nir_regs
= {
248 .yes_ranges
= &kx132_noinc_read_ranges
[0],
249 .n_yes_ranges
= ARRAY_SIZE(kx132_noinc_read_ranges
),
252 static const struct regmap_config kx132_regmap_config
= {
255 .volatile_table
= &kx132_volatile_regs
,
256 .rd_table
= &kx132_wo_regs
,
257 .wr_table
= &kx132_ro_regs
,
258 .rd_noinc_table
= &kx132_nir_regs
,
259 .precious_table
= &kx132_precious_regs
,
260 .max_register
= KX132_MAX_REGISTER
,
261 .cache_type
= REGCACHE_RBTREE
,
265 struct regmap
*regmap
;
266 const struct kx022a_chip_info
*chip_info
;
267 struct iio_trigger
*trig
;
269 struct iio_mount_matrix orientation
;
270 int64_t timestamp
, old_timestamp
;
279 bool trigger_enabled
;
281 * Prevent toggling the sensor stby/active state (PC1 bit) in the
282 * middle of a configuration, or when the fifo is enabled. Also,
283 * protect the data stored/retrieved from this structure from
284 * concurrent accesses.
291 /* 3 x 16bit accel data + timestamp */
292 __le16 buffer
[8] __aligned(IIO_DMA_MINALIGN
);
299 static const struct iio_mount_matrix
*
300 kx022a_get_mount_matrix(const struct iio_dev
*idev
,
301 const struct iio_chan_spec
*chan
)
303 struct kx022a_data
*data
= iio_priv(idev
);
305 return &data
->orientation
;
315 static const unsigned long kx022a_scan_masks
[] = {
316 BIT(AXIS_X
) | BIT(AXIS_Y
) | BIT(AXIS_Z
), 0
319 static const struct iio_chan_spec_ext_info kx022a_ext_info
[] = {
320 IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE
, kx022a_get_mount_matrix
),
324 #define KX022A_ACCEL_CHAN(axis, reg, index) \
328 .channel2 = IIO_MOD_##axis, \
329 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
330 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
331 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
332 .info_mask_shared_by_type_available = \
333 BIT(IIO_CHAN_INFO_SCALE) | \
334 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
335 .ext_info = kx022a_ext_info, \
337 .scan_index = index, \
342 .endianness = IIO_LE, \
346 static const struct iio_chan_spec kx022a_channels
[] = {
347 KX022A_ACCEL_CHAN(X
, KX022A_REG_XOUT_L
, 0),
348 KX022A_ACCEL_CHAN(Y
, KX022A_REG_YOUT_L
, 1),
349 KX022A_ACCEL_CHAN(Z
, KX022A_REG_ZOUT_L
, 2),
350 IIO_CHAN_SOFT_TIMESTAMP(3),
353 static const struct iio_chan_spec kx132_channels
[] = {
354 KX022A_ACCEL_CHAN(X
, KX132_REG_XOUT_L
, 0),
355 KX022A_ACCEL_CHAN(Y
, KX132_REG_YOUT_L
, 1),
356 KX022A_ACCEL_CHAN(Z
, KX132_REG_ZOUT_L
, 2),
357 IIO_CHAN_SOFT_TIMESTAMP(3),
361 * The sensor HW can support ODR up to 1600 Hz, which is beyond what most of the
362 * Linux CPUs can handle without dropping samples. Also, the low power mode is
363 * not available for higher sample rates. Thus, the driver only supports 200 Hz
364 * and slower ODRs. The slowest is 0.78 Hz.
366 static const int kx022a_accel_samp_freq_table
[][2] = {
378 static const unsigned int kx022a_odrs
[] = {
391 * range is typically +-2G/4G/8G/16G, distributed over the amount of bits.
392 * The scale table can be calculated using
393 * (range / 2^bits) * g = (range / 2^bits) * 9.80665 m/s^2
394 * => KX022A uses 16 bit (HiRes mode - assume the low 8 bits are zeroed
395 * in low-power mode(?) )
396 * => +/-2G => 4 / 2^16 * 9,80665
397 * => +/-2G - 0.000598550415
398 * +/-4G - 0.00119710083
399 * +/-8G - 0.00239420166
400 * +/-16G - 0.00478840332
402 static const int kx022a_scale_table
[][2] = {
409 static int kx022a_read_avail(struct iio_dev
*indio_dev
,
410 struct iio_chan_spec
const *chan
,
411 const int **vals
, int *type
, int *length
,
415 case IIO_CHAN_INFO_SAMP_FREQ
:
416 *vals
= (const int *)kx022a_accel_samp_freq_table
;
417 *length
= ARRAY_SIZE(kx022a_accel_samp_freq_table
) *
418 ARRAY_SIZE(kx022a_accel_samp_freq_table
[0]);
419 *type
= IIO_VAL_INT_PLUS_MICRO
;
420 return IIO_AVAIL_LIST
;
421 case IIO_CHAN_INFO_SCALE
:
422 *vals
= (const int *)kx022a_scale_table
;
423 *length
= ARRAY_SIZE(kx022a_scale_table
) *
424 ARRAY_SIZE(kx022a_scale_table
[0]);
425 *type
= IIO_VAL_INT_PLUS_NANO
;
426 return IIO_AVAIL_LIST
;
432 #define KX022A_DEFAULT_PERIOD_NS (20 * NSEC_PER_MSEC)
434 static void kx022a_reg2freq(unsigned int val
, int *val1
, int *val2
)
436 *val1
= kx022a_accel_samp_freq_table
[val
& KX022A_MASK_ODR
][0];
437 *val2
= kx022a_accel_samp_freq_table
[val
& KX022A_MASK_ODR
][1];
440 static void kx022a_reg2scale(unsigned int val
, unsigned int *val1
,
443 val
&= KX022A_MASK_GSEL
;
444 val
>>= KX022A_GSEL_SHIFT
;
446 *val1
= kx022a_scale_table
[val
][0];
447 *val2
= kx022a_scale_table
[val
][1];
450 static int kx022a_turn_on_off_unlocked(struct kx022a_data
*data
, bool on
)
455 ret
= regmap_set_bits(data
->regmap
, data
->chip_info
->cntl
,
458 ret
= regmap_clear_bits(data
->regmap
, data
->chip_info
->cntl
,
461 dev_err(data
->dev
, "Turn %s fail %d\n", str_on_off(on
), ret
);
466 static int kx022a_turn_off_lock(struct kx022a_data
*data
)
470 mutex_lock(&data
->mutex
);
471 ret
= kx022a_turn_on_off_unlocked(data
, false);
473 mutex_unlock(&data
->mutex
);
478 static int kx022a_turn_on_unlock(struct kx022a_data
*data
)
482 ret
= kx022a_turn_on_off_unlocked(data
, true);
483 mutex_unlock(&data
->mutex
);
488 static int kx022a_write_raw_get_fmt(struct iio_dev
*idev
,
489 struct iio_chan_spec
const *chan
,
493 case IIO_CHAN_INFO_SCALE
:
494 return IIO_VAL_INT_PLUS_NANO
;
495 case IIO_CHAN_INFO_SAMP_FREQ
:
496 return IIO_VAL_INT_PLUS_MICRO
;
502 static int kx022a_write_raw(struct iio_dev
*idev
,
503 struct iio_chan_spec
const *chan
,
504 int val
, int val2
, long mask
)
506 struct kx022a_data
*data
= iio_priv(idev
);
510 * We should not allow changing scale or frequency when FIFO is running
511 * as it will mess the timestamp/scale for samples existing in the
512 * buffer. If this turns out to be an issue we can later change logic
513 * to internally flush the fifo before reconfiguring so the samples in
514 * fifo keep matching the freq/scale settings. (Such setup could cause
515 * issues if users trust the watermark to be reached within known
518 ret
= iio_device_claim_direct_mode(idev
);
523 case IIO_CHAN_INFO_SAMP_FREQ
:
524 n
= ARRAY_SIZE(kx022a_accel_samp_freq_table
);
527 if (val
== kx022a_accel_samp_freq_table
[n
][0] &&
528 val2
== kx022a_accel_samp_freq_table
[n
][1])
534 ret
= kx022a_turn_off_lock(data
);
538 ret
= regmap_update_bits(data
->regmap
,
539 data
->chip_info
->odcntl
,
541 data
->odr_ns
= kx022a_odrs
[n
];
542 kx022a_turn_on_unlock(data
);
544 case IIO_CHAN_INFO_SCALE
:
545 n
= ARRAY_SIZE(kx022a_scale_table
);
548 if (val
== kx022a_scale_table
[n
][0] &&
549 val2
== kx022a_scale_table
[n
][1])
556 ret
= kx022a_turn_off_lock(data
);
560 ret
= regmap_update_bits(data
->regmap
, data
->chip_info
->cntl
,
562 n
<< KX022A_GSEL_SHIFT
);
563 kx022a_turn_on_unlock(data
);
571 iio_device_release_direct_mode(idev
);
576 static int kx022a_fifo_set_wmi(struct kx022a_data
*data
)
580 threshold
= data
->watermark
;
582 return regmap_update_bits(data
->regmap
, data
->chip_info
->buf_cntl1
,
583 KX022A_MASK_WM_TH
, threshold
);
586 static int kx022a_get_axis(struct kx022a_data
*data
,
587 struct iio_chan_spec
const *chan
,
592 ret
= regmap_bulk_read(data
->regmap
, chan
->address
, &data
->buffer
[0],
597 *val
= le16_to_cpu(data
->buffer
[0]);
602 static int kx022a_read_raw(struct iio_dev
*idev
,
603 struct iio_chan_spec
const *chan
,
604 int *val
, int *val2
, long mask
)
606 struct kx022a_data
*data
= iio_priv(idev
);
611 case IIO_CHAN_INFO_RAW
:
612 ret
= iio_device_claim_direct_mode(idev
);
616 mutex_lock(&data
->mutex
);
617 ret
= kx022a_get_axis(data
, chan
, val
);
618 mutex_unlock(&data
->mutex
);
620 iio_device_release_direct_mode(idev
);
624 case IIO_CHAN_INFO_SAMP_FREQ
:
625 ret
= regmap_read(data
->regmap
, data
->chip_info
->odcntl
, ®val
);
629 if ((regval
& KX022A_MASK_ODR
) >
630 ARRAY_SIZE(kx022a_accel_samp_freq_table
)) {
631 dev_err(data
->dev
, "Invalid ODR\n");
635 kx022a_reg2freq(regval
, val
, val2
);
637 return IIO_VAL_INT_PLUS_MICRO
;
639 case IIO_CHAN_INFO_SCALE
:
640 ret
= regmap_read(data
->regmap
, data
->chip_info
->cntl
, ®val
);
644 kx022a_reg2scale(regval
, val
, val2
);
646 return IIO_VAL_INT_PLUS_NANO
;
652 static int kx022a_set_watermark(struct iio_dev
*idev
, unsigned int val
)
654 struct kx022a_data
*data
= iio_priv(idev
);
656 val
= min(data
->chip_info
->fifo_length
, val
);
658 mutex_lock(&data
->mutex
);
659 data
->watermark
= val
;
660 mutex_unlock(&data
->mutex
);
665 static ssize_t
hwfifo_enabled_show(struct device
*dev
,
666 struct device_attribute
*attr
,
669 struct iio_dev
*idev
= dev_to_iio_dev(dev
);
670 struct kx022a_data
*data
= iio_priv(idev
);
673 mutex_lock(&data
->mutex
);
675 mutex_unlock(&data
->mutex
);
677 return sysfs_emit(buf
, "%d\n", state
);
680 static ssize_t
hwfifo_watermark_show(struct device
*dev
,
681 struct device_attribute
*attr
,
684 struct iio_dev
*idev
= dev_to_iio_dev(dev
);
685 struct kx022a_data
*data
= iio_priv(idev
);
688 mutex_lock(&data
->mutex
);
689 wm
= data
->watermark
;
690 mutex_unlock(&data
->mutex
);
692 return sysfs_emit(buf
, "%d\n", wm
);
695 static IIO_DEVICE_ATTR_RO(hwfifo_enabled
, 0);
696 static IIO_DEVICE_ATTR_RO(hwfifo_watermark
, 0);
698 static const struct iio_dev_attr
*kx022a_fifo_attributes
[] = {
699 &iio_dev_attr_hwfifo_watermark
,
700 &iio_dev_attr_hwfifo_enabled
,
704 static int kx022a_drop_fifo_contents(struct kx022a_data
*data
)
707 * We must clear the old time-stamp to avoid computing the timestamps
708 * based on samples acquired when buffer was last enabled.
710 * We don't need to protect the timestamp as long as we are only
711 * called from fifo-disable where we can guarantee the sensor is not
712 * triggering interrupts and where the mutex is locked to prevent the
717 return regmap_write(data
->regmap
, data
->chip_info
->buf_clear
, 0x0);
720 static int kx022a_get_fifo_bytes_available(struct kx022a_data
*data
)
724 ret
= regmap_read(data
->regmap
, KX022A_REG_BUF_STATUS_1
, &fifo_bytes
);
726 dev_err(data
->dev
, "Error reading buffer status\n");
730 if (fifo_bytes
== KX022A_FIFO_FULL_VALUE
)
731 return KX022A_FIFO_MAX_BYTES
;
736 static int kx132_get_fifo_bytes_available(struct kx022a_data
*data
)
741 ret
= regmap_bulk_read(data
->regmap
, data
->chip_info
->buf_status1
,
742 &buf_status
, sizeof(buf_status
));
744 dev_err(data
->dev
, "Error reading buffer status\n");
748 fifo_bytes
= le16_to_cpu(buf_status
);
749 fifo_bytes
&= data
->chip_info
->buf_smp_lvl_mask
;
750 fifo_bytes
= min((unsigned int)fifo_bytes
, data
->chip_info
->fifo_length
*
751 KX022A_FIFO_SAMPLES_SIZE_BYTES
);
756 static int __kx022a_fifo_flush(struct iio_dev
*idev
, unsigned int samples
,
759 struct kx022a_data
*data
= iio_priv(idev
);
760 uint64_t sample_period
;
761 int count
, fifo_bytes
;
762 bool renable
= false;
766 fifo_bytes
= data
->chip_info
->get_fifo_bytes_available(data
);
768 if (fifo_bytes
% KX022A_FIFO_SAMPLES_SIZE_BYTES
)
769 dev_warn(data
->dev
, "Bad FIFO alignment. Data may be corrupt\n");
771 count
= fifo_bytes
/ KX022A_FIFO_SAMPLES_SIZE_BYTES
;
776 * If we are being called from IRQ handler we know the stored timestamp
777 * is fairly accurate for the last stored sample. Otherwise, if we are
778 * called as a result of a read operation from userspace and hence
779 * before the watermark interrupt was triggered, take a timestamp
780 * now. We can fall anywhere in between two samples so the error in this
781 * case is at most one sample period.
785 * We need to have the IRQ disabled or we risk of messing-up
786 * the timestamps. If we are ran from IRQ, then the
787 * IRQF_ONESHOT has us covered - but if we are ran by the
788 * user-space read we need to disable the IRQ to be on a safe
789 * side. We do this usng synchronous disable so that if the
790 * IRQ thread is being ran on other CPU we wait for it to be
793 disable_irq(data
->irq
);
796 data
->old_timestamp
= data
->timestamp
;
797 data
->timestamp
= iio_get_time_ns(idev
);
801 * Approximate timestamps for each of the sample based on the sampling
802 * frequency, timestamp for last sample and number of samples.
804 * We'd better not use the current bandwidth settings to compute the
805 * sample period. The real sample rate varies with the device and
806 * small variation adds when we store a large number of samples.
808 * To avoid this issue we compute the actual sample period ourselves
809 * based on the timestamp delta between the last two flush operations.
811 if (data
->old_timestamp
) {
812 sample_period
= data
->timestamp
- data
->old_timestamp
;
813 do_div(sample_period
, count
);
815 sample_period
= data
->odr_ns
;
817 tstamp
= data
->timestamp
- (count
- 1) * sample_period
;
819 if (samples
&& count
> samples
) {
821 * Here we leave some old samples to the buffer. We need to
822 * adjust the timestamp to match the first sample in the buffer
823 * or we will miscalculate the sample_period at next round.
825 data
->timestamp
-= (count
- samples
) * sample_period
;
829 fifo_bytes
= count
* KX022A_FIFO_SAMPLES_SIZE_BYTES
;
830 ret
= regmap_noinc_read(data
->regmap
, data
->chip_info
->buf_read
,
831 data
->fifo_buffer
, fifo_bytes
);
835 for (i
= 0; i
< count
; i
++) {
836 __le16
*sam
= &data
->fifo_buffer
[i
* 3];
840 chs
= &data
->scan
.channels
[0];
841 for_each_set_bit(bit
, idev
->active_scan_mask
, AXIS_MAX
)
844 iio_push_to_buffers_with_timestamp(idev
, &data
->scan
, tstamp
);
846 tstamp
+= sample_period
;
853 enable_irq(data
->irq
);
858 static int kx022a_fifo_flush(struct iio_dev
*idev
, unsigned int samples
)
860 struct kx022a_data
*data
= iio_priv(idev
);
863 mutex_lock(&data
->mutex
);
864 ret
= __kx022a_fifo_flush(idev
, samples
, false);
865 mutex_unlock(&data
->mutex
);
870 static const struct iio_info kx022a_info
= {
871 .read_raw
= &kx022a_read_raw
,
872 .write_raw
= &kx022a_write_raw
,
873 .write_raw_get_fmt
= &kx022a_write_raw_get_fmt
,
874 .read_avail
= &kx022a_read_avail
,
876 .validate_trigger
= iio_validate_own_trigger
,
877 .hwfifo_set_watermark
= kx022a_set_watermark
,
878 .hwfifo_flush_to_buffer
= kx022a_fifo_flush
,
881 static int kx022a_set_drdy_irq(struct kx022a_data
*data
, bool en
)
884 return regmap_set_bits(data
->regmap
, data
->chip_info
->cntl
,
887 return regmap_clear_bits(data
->regmap
, data
->chip_info
->cntl
,
891 static int kx022a_prepare_irq_pin(struct kx022a_data
*data
)
893 /* Enable IRQ1 pin. Set polarity to active low */
894 int mask
= KX022A_MASK_IEN
| KX022A_MASK_IPOL
|
896 int val
= KX022A_MASK_IEN
| KX022A_IPOL_LOW
|
900 ret
= regmap_update_bits(data
->regmap
, data
->inc_reg
, mask
, val
);
904 /* We enable WMI to IRQ pin only at buffer_enable */
905 mask
= KX022A_MASK_INS2_DRDY
;
907 return regmap_set_bits(data
->regmap
, data
->ien_reg
, mask
);
910 static int kx022a_fifo_disable(struct kx022a_data
*data
)
914 ret
= kx022a_turn_off_lock(data
);
918 ret
= regmap_clear_bits(data
->regmap
, data
->ien_reg
, KX022A_MASK_WMI
);
922 ret
= regmap_clear_bits(data
->regmap
, data
->chip_info
->buf_cntl2
,
927 data
->state
&= ~KX022A_STATE_FIFO
;
929 kx022a_drop_fifo_contents(data
);
931 kfree(data
->fifo_buffer
);
933 return kx022a_turn_on_unlock(data
);
936 mutex_unlock(&data
->mutex
);
941 static int kx022a_buffer_predisable(struct iio_dev
*idev
)
943 struct kx022a_data
*data
= iio_priv(idev
);
945 if (iio_device_get_current_mode(idev
) == INDIO_BUFFER_TRIGGERED
)
948 return kx022a_fifo_disable(data
);
951 static int kx022a_fifo_enable(struct kx022a_data
*data
)
955 data
->fifo_buffer
= kmalloc_array(data
->chip_info
->fifo_length
,
956 KX022A_FIFO_SAMPLES_SIZE_BYTES
,
958 if (!data
->fifo_buffer
)
961 ret
= kx022a_turn_off_lock(data
);
965 /* Update watermark to HW */
966 ret
= kx022a_fifo_set_wmi(data
);
971 ret
= regmap_set_bits(data
->regmap
, data
->chip_info
->buf_cntl2
,
976 data
->state
|= KX022A_STATE_FIFO
;
977 ret
= regmap_set_bits(data
->regmap
, data
->ien_reg
,
982 return kx022a_turn_on_unlock(data
);
985 mutex_unlock(&data
->mutex
);
990 static int kx022a_buffer_postenable(struct iio_dev
*idev
)
992 struct kx022a_data
*data
= iio_priv(idev
);
995 * If we use data-ready trigger, then the IRQ masks should be handled by
996 * trigger enable and the hardware buffer is not used but we just update
997 * results to the IIO fifo when data-ready triggers.
999 if (iio_device_get_current_mode(idev
) == INDIO_BUFFER_TRIGGERED
)
1002 return kx022a_fifo_enable(data
);
1005 static const struct iio_buffer_setup_ops kx022a_buffer_ops
= {
1006 .postenable
= kx022a_buffer_postenable
,
1007 .predisable
= kx022a_buffer_predisable
,
1010 static irqreturn_t
kx022a_trigger_handler(int irq
, void *p
)
1012 struct iio_poll_func
*pf
= p
;
1013 struct iio_dev
*idev
= pf
->indio_dev
;
1014 struct kx022a_data
*data
= iio_priv(idev
);
1017 ret
= regmap_bulk_read(data
->regmap
, data
->chip_info
->xout_l
, data
->buffer
,
1018 KX022A_FIFO_SAMPLES_SIZE_BYTES
);
1022 iio_push_to_buffers_with_timestamp(idev
, data
->buffer
, data
->timestamp
);
1024 iio_trigger_notify_done(idev
->trig
);
1029 /* Get timestamps and wake the thread if we need to read data */
1030 static irqreturn_t
kx022a_irq_handler(int irq
, void *private)
1032 struct iio_dev
*idev
= private;
1033 struct kx022a_data
*data
= iio_priv(idev
);
1035 data
->old_timestamp
= data
->timestamp
;
1036 data
->timestamp
= iio_get_time_ns(idev
);
1038 if (data
->state
& KX022A_STATE_FIFO
|| data
->trigger_enabled
)
1039 return IRQ_WAKE_THREAD
;
1045 * WMI and data-ready IRQs are acked when results are read. If we add
1046 * TILT/WAKE or other IRQs - then we may need to implement the acking
1049 static irqreturn_t
kx022a_irq_thread_handler(int irq
, void *private)
1051 struct iio_dev
*idev
= private;
1052 struct kx022a_data
*data
= iio_priv(idev
);
1053 irqreturn_t ret
= IRQ_NONE
;
1055 mutex_lock(&data
->mutex
);
1057 if (data
->trigger_enabled
) {
1058 iio_trigger_poll_nested(data
->trig
);
1062 if (data
->state
& KX022A_STATE_FIFO
) {
1065 ok
= __kx022a_fifo_flush(idev
, data
->chip_info
->fifo_length
, true);
1070 mutex_unlock(&data
->mutex
);
1075 static int kx022a_trigger_set_state(struct iio_trigger
*trig
,
1078 struct kx022a_data
*data
= iio_trigger_get_drvdata(trig
);
1081 mutex_lock(&data
->mutex
);
1083 if (data
->trigger_enabled
== state
)
1086 if (data
->state
& KX022A_STATE_FIFO
) {
1087 dev_warn(data
->dev
, "Can't set trigger when FIFO enabled\n");
1092 ret
= kx022a_turn_on_off_unlocked(data
, false);
1096 data
->trigger_enabled
= state
;
1097 ret
= kx022a_set_drdy_irq(data
, state
);
1101 ret
= kx022a_turn_on_off_unlocked(data
, true);
1104 mutex_unlock(&data
->mutex
);
1109 static const struct iio_trigger_ops kx022a_trigger_ops
= {
1110 .set_trigger_state
= kx022a_trigger_set_state
,
1113 static int kx022a_chip_init(struct kx022a_data
*data
)
1117 /* Reset the senor */
1118 ret
= regmap_write(data
->regmap
, data
->chip_info
->cntl2
, KX022A_MASK_SRST
);
1123 * I've seen I2C read failures if we poll too fast after the sensor
1124 * reset. Slight delay gives I2C block the time to recover.
1128 ret
= regmap_read_poll_timeout(data
->regmap
, data
->chip_info
->cntl2
, val
,
1129 !(val
& KX022A_MASK_SRST
),
1130 KX022A_SOFT_RESET_WAIT_TIME_US
,
1131 KX022A_SOFT_RESET_TOTAL_WAIT_TIME_US
);
1133 dev_err(data
->dev
, "Sensor reset %s\n",
1134 val
& KX022A_MASK_SRST
? "timeout" : "fail#");
1138 ret
= regmap_reinit_cache(data
->regmap
, data
->chip_info
->regmap_config
);
1140 dev_err(data
->dev
, "Failed to reinit reg cache\n");
1144 /* set data res 16bit */
1145 ret
= regmap_set_bits(data
->regmap
, data
->chip_info
->buf_cntl2
,
1146 KX022A_MASK_BRES16
);
1148 dev_err(data
->dev
, "Failed to set data resolution\n");
1152 return kx022a_prepare_irq_pin(data
);
1155 const struct kx022a_chip_info kx022a_chip_info
= {
1156 .name
= "kx022-accel",
1157 .regmap_config
= &kx022a_regmap_config
,
1158 .channels
= kx022a_channels
,
1159 .num_channels
= ARRAY_SIZE(kx022a_channels
),
1160 .fifo_length
= KX022A_FIFO_LENGTH
,
1161 .who
= KX022A_REG_WHO
,
1163 .cntl
= KX022A_REG_CNTL
,
1164 .cntl2
= KX022A_REG_CNTL2
,
1165 .odcntl
= KX022A_REG_ODCNTL
,
1166 .buf_cntl1
= KX022A_REG_BUF_CNTL1
,
1167 .buf_cntl2
= KX022A_REG_BUF_CNTL2
,
1168 .buf_clear
= KX022A_REG_BUF_CLEAR
,
1169 .buf_status1
= KX022A_REG_BUF_STATUS_1
,
1170 .buf_read
= KX022A_REG_BUF_READ
,
1171 .inc1
= KX022A_REG_INC1
,
1172 .inc4
= KX022A_REG_INC4
,
1173 .inc5
= KX022A_REG_INC5
,
1174 .inc6
= KX022A_REG_INC6
,
1175 .xout_l
= KX022A_REG_XOUT_L
,
1176 .get_fifo_bytes_available
= kx022a_get_fifo_bytes_available
,
1178 EXPORT_SYMBOL_NS_GPL(kx022a_chip_info
, IIO_KX022A
);
1180 const struct kx022a_chip_info kx132_chip_info
= {
1181 .name
= "kx132-1211",
1182 .regmap_config
= &kx132_regmap_config
,
1183 .channels
= kx132_channels
,
1184 .num_channels
= ARRAY_SIZE(kx132_channels
),
1185 .fifo_length
= KX132_FIFO_LENGTH
,
1186 .who
= KX132_REG_WHO
,
1188 .cntl
= KX132_REG_CNTL
,
1189 .cntl2
= KX132_REG_CNTL2
,
1190 .odcntl
= KX132_REG_ODCNTL
,
1191 .buf_cntl1
= KX132_REG_BUF_CNTL1
,
1192 .buf_cntl2
= KX132_REG_BUF_CNTL2
,
1193 .buf_clear
= KX132_REG_BUF_CLEAR
,
1194 .buf_status1
= KX132_REG_BUF_STATUS_1
,
1195 .buf_smp_lvl_mask
= KX132_MASK_BUF_SMP_LVL
,
1196 .buf_read
= KX132_REG_BUF_READ
,
1197 .inc1
= KX132_REG_INC1
,
1198 .inc4
= KX132_REG_INC4
,
1199 .inc5
= KX132_REG_INC5
,
1200 .inc6
= KX132_REG_INC6
,
1201 .xout_l
= KX132_REG_XOUT_L
,
1202 .get_fifo_bytes_available
= kx132_get_fifo_bytes_available
,
1204 EXPORT_SYMBOL_NS_GPL(kx132_chip_info
, IIO_KX022A
);
1207 * Despite the naming, KX132ACR-LBZ is not similar to KX132-1211 but it is
1208 * exact subset of KX022A. KX132ACR-LBZ is meant to be used for industrial
1209 * applications and the tap/double tap, free fall and tilt engines were
1210 * removed. Rest of the registers and functionalities (excluding the ID
1211 * register) are exact match to what is found in KX022.
1213 const struct kx022a_chip_info kx132acr_chip_info
= {
1214 .name
= "kx132acr-lbz",
1215 .regmap_config
= &kx022a_regmap_config
,
1216 .channels
= kx022a_channels
,
1217 .num_channels
= ARRAY_SIZE(kx022a_channels
),
1218 .fifo_length
= KX022A_FIFO_LENGTH
,
1219 .who
= KX022A_REG_WHO
,
1220 .id
= KX132ACR_LBZ_ID
,
1221 .cntl
= KX022A_REG_CNTL
,
1222 .cntl2
= KX022A_REG_CNTL2
,
1223 .odcntl
= KX022A_REG_ODCNTL
,
1224 .buf_cntl1
= KX022A_REG_BUF_CNTL1
,
1225 .buf_cntl2
= KX022A_REG_BUF_CNTL2
,
1226 .buf_clear
= KX022A_REG_BUF_CLEAR
,
1227 .buf_status1
= KX022A_REG_BUF_STATUS_1
,
1228 .buf_read
= KX022A_REG_BUF_READ
,
1229 .inc1
= KX022A_REG_INC1
,
1230 .inc4
= KX022A_REG_INC4
,
1231 .inc5
= KX022A_REG_INC5
,
1232 .inc6
= KX022A_REG_INC6
,
1233 .xout_l
= KX022A_REG_XOUT_L
,
1234 .get_fifo_bytes_available
= kx022a_get_fifo_bytes_available
,
1236 EXPORT_SYMBOL_NS_GPL(kx132acr_chip_info
, IIO_KX022A
);
1238 int kx022a_probe_internal(struct device
*dev
, const struct kx022a_chip_info
*chip_info
)
1240 static const char * const regulator_names
[] = {"io-vdd", "vdd"};
1241 struct iio_trigger
*indio_trig
;
1242 struct fwnode_handle
*fwnode
;
1243 struct kx022a_data
*data
;
1244 struct regmap
*regmap
;
1245 unsigned int chip_id
;
1246 struct iio_dev
*idev
;
1250 regmap
= dev_get_regmap(dev
, NULL
);
1252 dev_err(dev
, "no regmap\n");
1256 fwnode
= dev_fwnode(dev
);
1260 idev
= devm_iio_device_alloc(dev
, sizeof(*data
));
1264 data
= iio_priv(idev
);
1265 data
->chip_info
= chip_info
;
1268 * VDD is the analog and digital domain voltage supply and
1269 * IO_VDD is the digital I/O voltage supply.
1271 ret
= devm_regulator_bulk_get_enable(dev
, ARRAY_SIZE(regulator_names
),
1273 if (ret
&& ret
!= -ENODEV
)
1274 return dev_err_probe(dev
, ret
, "failed to enable regulator\n");
1276 ret
= regmap_read(regmap
, chip_info
->who
, &chip_id
);
1278 return dev_err_probe(dev
, ret
, "Failed to access sensor\n");
1280 if (chip_id
!= chip_info
->id
)
1281 dev_warn(dev
, "unknown device 0x%x\n", chip_id
);
1283 irq
= fwnode_irq_get_byname(fwnode
, "INT1");
1285 data
->inc_reg
= chip_info
->inc1
;
1286 data
->ien_reg
= chip_info
->inc4
;
1288 irq
= fwnode_irq_get_byname(fwnode
, "INT2");
1290 return dev_err_probe(dev
, irq
, "No suitable IRQ\n");
1292 data
->inc_reg
= chip_info
->inc5
;
1293 data
->ien_reg
= chip_info
->inc6
;
1296 data
->regmap
= regmap
;
1299 data
->odr_ns
= KX022A_DEFAULT_PERIOD_NS
;
1300 mutex_init(&data
->mutex
);
1302 idev
->channels
= chip_info
->channels
;
1303 idev
->num_channels
= chip_info
->num_channels
;
1304 idev
->name
= chip_info
->name
;
1305 idev
->info
= &kx022a_info
;
1306 idev
->modes
= INDIO_DIRECT_MODE
| INDIO_BUFFER_SOFTWARE
;
1307 idev
->available_scan_masks
= kx022a_scan_masks
;
1309 /* Read the mounting matrix, if present */
1310 ret
= iio_read_mount_matrix(dev
, &data
->orientation
);
1314 /* The sensor must be turned off for configuration */
1315 ret
= kx022a_turn_off_lock(data
);
1319 ret
= kx022a_chip_init(data
);
1321 mutex_unlock(&data
->mutex
);
1325 ret
= kx022a_turn_on_unlock(data
);
1329 ret
= devm_iio_triggered_buffer_setup_ext(dev
, idev
,
1330 &iio_pollfunc_store_time
,
1331 kx022a_trigger_handler
,
1332 IIO_BUFFER_DIRECTION_IN
,
1334 kx022a_fifo_attributes
);
1337 return dev_err_probe(data
->dev
, ret
,
1338 "iio_triggered_buffer_setup_ext FAIL\n");
1339 indio_trig
= devm_iio_trigger_alloc(dev
, "%sdata-rdy-dev%d", idev
->name
,
1340 iio_device_id(idev
));
1344 data
->trig
= indio_trig
;
1346 indio_trig
->ops
= &kx022a_trigger_ops
;
1347 iio_trigger_set_drvdata(indio_trig
, data
);
1350 * No need to check for NULL. request_threaded_irq() defaults to
1351 * dev_name() should the alloc fail.
1353 name
= devm_kasprintf(data
->dev
, GFP_KERNEL
, "%s-kx022a",
1354 dev_name(data
->dev
));
1356 ret
= devm_request_threaded_irq(data
->dev
, irq
, kx022a_irq_handler
,
1357 &kx022a_irq_thread_handler
,
1358 IRQF_ONESHOT
, name
, idev
);
1360 return dev_err_probe(data
->dev
, ret
, "Could not request IRQ\n");
1362 ret
= devm_iio_trigger_register(dev
, indio_trig
);
1364 return dev_err_probe(data
->dev
, ret
,
1365 "Trigger registration failed\n");
1367 ret
= devm_iio_device_register(data
->dev
, idev
);
1369 return dev_err_probe(dev
, ret
,
1370 "Unable to register iio device\n");
1374 EXPORT_SYMBOL_NS_GPL(kx022a_probe_internal
, IIO_KX022A
);
1376 MODULE_DESCRIPTION("ROHM/Kionix KX022A accelerometer driver");
1377 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
1378 MODULE_LICENSE("GPL");