1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ADXRS290 SPI Gyroscope Driver
5 * Copyright (C) 2020 Nishant Malpani <nish.malpani25@gmail.com>
6 * Copyright (C) 2020 Analog Devices, Inc.
9 #include <linux/bitfield.h>
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/spi/spi.h>
16 #include <linux/iio/buffer.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/sysfs.h>
19 #include <linux/iio/trigger.h>
20 #include <linux/iio/triggered_buffer.h>
21 #include <linux/iio/trigger_consumer.h>
23 #define ADXRS290_ADI_ID 0xAD
24 #define ADXRS290_MEMS_ID 0x1D
25 #define ADXRS290_DEV_ID 0x92
27 #define ADXRS290_REG_ADI_ID 0x00
28 #define ADXRS290_REG_MEMS_ID 0x01
29 #define ADXRS290_REG_DEV_ID 0x02
30 #define ADXRS290_REG_REV_ID 0x03
31 #define ADXRS290_REG_SN0 0x04 /* Serial Number Registers, 4 bytes */
32 #define ADXRS290_REG_DATAX0 0x08 /* Roll Rate o/p Data Regs, 2 bytes */
33 #define ADXRS290_REG_DATAY0 0x0A /* Pitch Rate o/p Data Regs, 2 bytes */
34 #define ADXRS290_REG_TEMP0 0x0C
35 #define ADXRS290_REG_POWER_CTL 0x10
36 #define ADXRS290_REG_FILTER 0x11
37 #define ADXRS290_REG_DATA_RDY 0x12
39 #define ADXRS290_READ BIT(7)
40 #define ADXRS290_TSM BIT(0)
41 #define ADXRS290_MEASUREMENT BIT(1)
42 #define ADXRS290_DATA_RDY_OUT BIT(0)
43 #define ADXRS290_SYNC_MASK GENMASK(1, 0)
44 #define ADXRS290_SYNC(x) FIELD_PREP(ADXRS290_SYNC_MASK, x)
45 #define ADXRS290_LPF_MASK GENMASK(2, 0)
46 #define ADXRS290_LPF(x) FIELD_PREP(ADXRS290_LPF_MASK, x)
47 #define ADXRS290_HPF_MASK GENMASK(7, 4)
48 #define ADXRS290_HPF(x) FIELD_PREP(ADXRS290_HPF_MASK, x)
50 #define ADXRS290_READ_REG(reg) (ADXRS290_READ | (reg))
52 #define ADXRS290_MAX_TRANSITION_TIME_MS 100
55 ADXRS290_MODE_STANDBY
,
56 ADXRS290_MODE_MEASUREMENT
,
59 enum adxrs290_scan_index
{
66 struct adxrs290_state
{
67 struct spi_device
*spi
;
68 /* Serialize reads and their subsequent processing */
70 enum adxrs290_mode mode
;
71 unsigned int lpf_3db_freq_idx
;
72 unsigned int hpf_3db_freq_idx
;
73 struct iio_trigger
*dready_trig
;
74 /* Ensure correct alignment of timestamp when present */
82 * Available cut-off frequencies of the low pass filter in Hz.
83 * The integer part and fractional part are represented separately.
85 static const int adxrs290_lpf_3db_freq_hz_table
[][2] = {
97 * Available cut-off frequencies of the high pass filter in Hz.
98 * The integer part and fractional part are represented separately.
100 static const int adxrs290_hpf_3db_freq_hz_table
[][2] = {
114 static int adxrs290_get_rate_data(struct iio_dev
*indio_dev
, const u8 cmd
, int *val
)
116 struct adxrs290_state
*st
= iio_priv(indio_dev
);
120 mutex_lock(&st
->lock
);
121 temp
= spi_w8r16(st
->spi
, cmd
);
130 mutex_unlock(&st
->lock
);
134 static int adxrs290_get_temp_data(struct iio_dev
*indio_dev
, int *val
)
136 const u8 cmd
= ADXRS290_READ_REG(ADXRS290_REG_TEMP0
);
137 struct adxrs290_state
*st
= iio_priv(indio_dev
);
141 mutex_lock(&st
->lock
);
142 temp
= spi_w8r16(st
->spi
, cmd
);
148 /* extract lower 12 bits temperature reading */
149 *val
= temp
& 0x0FFF;
152 mutex_unlock(&st
->lock
);
156 static int adxrs290_get_3db_freq(struct iio_dev
*indio_dev
, u8
*val
, u8
*val2
)
158 const u8 cmd
= ADXRS290_READ_REG(ADXRS290_REG_FILTER
);
159 struct adxrs290_state
*st
= iio_priv(indio_dev
);
163 mutex_lock(&st
->lock
);
164 temp
= spi_w8r8(st
->spi
, cmd
);
170 *val
= FIELD_GET(ADXRS290_LPF_MASK
, temp
);
171 *val2
= FIELD_GET(ADXRS290_HPF_MASK
, temp
);
174 mutex_unlock(&st
->lock
);
178 static int adxrs290_spi_write_reg(struct spi_device
*spi
, const u8 reg
,
186 return spi_write_then_read(spi
, buf
, ARRAY_SIZE(buf
), NULL
, 0);
189 static int adxrs290_find_match(const int (*freq_tbl
)[2], const int n
,
190 const int val
, const int val2
)
194 for (i
= 0; i
< n
; i
++) {
195 if (freq_tbl
[i
][0] == val
&& freq_tbl
[i
][1] == val2
)
202 static int adxrs290_set_filter_freq(struct iio_dev
*indio_dev
,
203 const unsigned int lpf_idx
,
204 const unsigned int hpf_idx
)
206 struct adxrs290_state
*st
= iio_priv(indio_dev
);
209 val
= ADXRS290_HPF(hpf_idx
) | ADXRS290_LPF(lpf_idx
);
211 return adxrs290_spi_write_reg(st
->spi
, ADXRS290_REG_FILTER
, val
);
214 static int adxrs290_set_mode(struct iio_dev
*indio_dev
, enum adxrs290_mode mode
)
216 struct adxrs290_state
*st
= iio_priv(indio_dev
);
219 if (st
->mode
== mode
)
222 mutex_lock(&st
->lock
);
224 ret
= spi_w8r8(st
->spi
, ADXRS290_READ_REG(ADXRS290_REG_POWER_CTL
));
231 case ADXRS290_MODE_STANDBY
:
232 val
&= ~ADXRS290_MEASUREMENT
;
234 case ADXRS290_MODE_MEASUREMENT
:
235 val
|= ADXRS290_MEASUREMENT
;
242 ret
= adxrs290_spi_write_reg(st
->spi
, ADXRS290_REG_POWER_CTL
, val
);
244 dev_err(&st
->spi
->dev
, "unable to set mode: %d\n", ret
);
248 /* update cached mode */
252 mutex_unlock(&st
->lock
);
256 static void adxrs290_chip_off_action(void *data
)
258 struct iio_dev
*indio_dev
= data
;
260 adxrs290_set_mode(indio_dev
, ADXRS290_MODE_STANDBY
);
263 static int adxrs290_initial_setup(struct iio_dev
*indio_dev
)
265 struct adxrs290_state
*st
= iio_priv(indio_dev
);
266 struct spi_device
*spi
= st
->spi
;
269 ret
= adxrs290_spi_write_reg(spi
, ADXRS290_REG_POWER_CTL
,
270 ADXRS290_MEASUREMENT
| ADXRS290_TSM
);
274 st
->mode
= ADXRS290_MODE_MEASUREMENT
;
276 return devm_add_action_or_reset(&spi
->dev
, adxrs290_chip_off_action
,
280 static int adxrs290_read_raw(struct iio_dev
*indio_dev
,
281 struct iio_chan_spec
const *chan
,
286 struct adxrs290_state
*st
= iio_priv(indio_dev
);
291 case IIO_CHAN_INFO_RAW
:
292 ret
= iio_device_claim_direct_mode(indio_dev
);
296 switch (chan
->type
) {
298 ret
= adxrs290_get_rate_data(indio_dev
,
299 ADXRS290_READ_REG(chan
->address
),
307 ret
= adxrs290_get_temp_data(indio_dev
, val
);
318 iio_device_release_direct_mode(indio_dev
);
320 case IIO_CHAN_INFO_SCALE
:
321 switch (chan
->type
) {
323 /* 1 LSB = 0.005 degrees/sec */
326 return IIO_VAL_INT_PLUS_NANO
;
328 /* 1 LSB = 0.1 degrees Celsius */
334 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
335 switch (chan
->type
) {
337 t
= st
->lpf_3db_freq_idx
;
338 *val
= adxrs290_lpf_3db_freq_hz_table
[t
][0];
339 *val2
= adxrs290_lpf_3db_freq_hz_table
[t
][1];
340 return IIO_VAL_INT_PLUS_MICRO
;
344 case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY
:
345 switch (chan
->type
) {
347 t
= st
->hpf_3db_freq_idx
;
348 *val
= adxrs290_hpf_3db_freq_hz_table
[t
][0];
349 *val2
= adxrs290_hpf_3db_freq_hz_table
[t
][1];
350 return IIO_VAL_INT_PLUS_MICRO
;
359 static int adxrs290_write_raw(struct iio_dev
*indio_dev
,
360 struct iio_chan_spec
const *chan
,
365 struct adxrs290_state
*st
= iio_priv(indio_dev
);
366 int ret
, lpf_idx
, hpf_idx
;
368 ret
= iio_device_claim_direct_mode(indio_dev
);
373 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
374 lpf_idx
= adxrs290_find_match(adxrs290_lpf_3db_freq_hz_table
,
375 ARRAY_SIZE(adxrs290_lpf_3db_freq_hz_table
),
382 /* caching the updated state of the low-pass filter */
383 st
->lpf_3db_freq_idx
= lpf_idx
;
384 /* retrieving the current state of the high-pass filter */
385 hpf_idx
= st
->hpf_3db_freq_idx
;
386 ret
= adxrs290_set_filter_freq(indio_dev
, lpf_idx
, hpf_idx
);
389 case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY
:
390 hpf_idx
= adxrs290_find_match(adxrs290_hpf_3db_freq_hz_table
,
391 ARRAY_SIZE(adxrs290_hpf_3db_freq_hz_table
),
398 /* caching the updated state of the high-pass filter */
399 st
->hpf_3db_freq_idx
= hpf_idx
;
400 /* retrieving the current state of the low-pass filter */
401 lpf_idx
= st
->lpf_3db_freq_idx
;
402 ret
= adxrs290_set_filter_freq(indio_dev
, lpf_idx
, hpf_idx
);
410 iio_device_release_direct_mode(indio_dev
);
414 static int adxrs290_read_avail(struct iio_dev
*indio_dev
,
415 struct iio_chan_spec
const *chan
,
416 const int **vals
, int *type
, int *length
,
420 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
421 *vals
= (const int *)adxrs290_lpf_3db_freq_hz_table
;
422 *type
= IIO_VAL_INT_PLUS_MICRO
;
423 /* Values are stored in a 2D matrix */
424 *length
= ARRAY_SIZE(adxrs290_lpf_3db_freq_hz_table
) * 2;
426 return IIO_AVAIL_LIST
;
427 case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY
:
428 *vals
= (const int *)adxrs290_hpf_3db_freq_hz_table
;
429 *type
= IIO_VAL_INT_PLUS_MICRO
;
430 /* Values are stored in a 2D matrix */
431 *length
= ARRAY_SIZE(adxrs290_hpf_3db_freq_hz_table
) * 2;
433 return IIO_AVAIL_LIST
;
439 static int adxrs290_reg_access_rw(struct spi_device
*spi
, unsigned int reg
,
440 unsigned int *readval
)
444 ret
= spi_w8r8(spi
, ADXRS290_READ_REG(reg
));
453 static int adxrs290_reg_access(struct iio_dev
*indio_dev
, unsigned int reg
,
454 unsigned int writeval
, unsigned int *readval
)
456 struct adxrs290_state
*st
= iio_priv(indio_dev
);
459 return adxrs290_reg_access_rw(st
->spi
, reg
, readval
);
461 return adxrs290_spi_write_reg(st
->spi
, reg
, writeval
);
464 static int adxrs290_data_rdy_trigger_set_state(struct iio_trigger
*trig
,
467 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
468 struct adxrs290_state
*st
= iio_priv(indio_dev
);
472 val
= state
? ADXRS290_SYNC(ADXRS290_DATA_RDY_OUT
) : 0;
474 ret
= adxrs290_spi_write_reg(st
->spi
, ADXRS290_REG_DATA_RDY
, val
);
476 dev_err(&st
->spi
->dev
, "failed to start data rdy interrupt\n");
481 static void adxrs290_reset_trig(struct iio_trigger
*trig
)
483 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
487 * Data ready interrupt is reset after a read of the data registers.
488 * Here, we only read the 16b DATAY registers as that marks the end of
489 * a read of the data registers and initiates a reset for the interrupt
492 adxrs290_get_rate_data(indio_dev
,
493 ADXRS290_READ_REG(ADXRS290_REG_DATAY0
), &val
);
496 static const struct iio_trigger_ops adxrs290_trigger_ops
= {
497 .set_trigger_state
= &adxrs290_data_rdy_trigger_set_state
,
498 .validate_device
= &iio_trigger_validate_own_device
,
499 .reenable
= &adxrs290_reset_trig
,
502 static irqreturn_t
adxrs290_trigger_handler(int irq
, void *p
)
504 struct iio_poll_func
*pf
= p
;
505 struct iio_dev
*indio_dev
= pf
->indio_dev
;
506 struct adxrs290_state
*st
= iio_priv(indio_dev
);
507 u8 tx
= ADXRS290_READ_REG(ADXRS290_REG_DATAX0
);
510 mutex_lock(&st
->lock
);
512 /* exercise a bulk data capture starting from reg DATAX0... */
513 ret
= spi_write_then_read(st
->spi
, &tx
, sizeof(tx
), st
->buffer
.channels
,
514 sizeof(st
->buffer
.channels
));
516 goto out_unlock_notify
;
518 iio_push_to_buffers_with_timestamp(indio_dev
, &st
->buffer
,
522 mutex_unlock(&st
->lock
);
523 iio_trigger_notify_done(indio_dev
->trig
);
528 #define ADXRS290_ANGL_VEL_CHANNEL(reg, axis) { \
529 .type = IIO_ANGL_VEL, \
532 .channel2 = IIO_MOD_##axis, \
533 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
534 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
535 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) | \
536 BIT(IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY), \
537 .info_mask_shared_by_type_available = \
538 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) | \
539 BIT(IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY), \
540 .scan_index = ADXRS290_IDX_##axis, \
545 .endianness = IIO_LE, \
549 static const struct iio_chan_spec adxrs290_channels
[] = {
550 ADXRS290_ANGL_VEL_CHANNEL(ADXRS290_REG_DATAX0
, X
),
551 ADXRS290_ANGL_VEL_CHANNEL(ADXRS290_REG_DATAY0
, Y
),
554 .address
= ADXRS290_REG_TEMP0
,
555 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
556 BIT(IIO_CHAN_INFO_SCALE
),
557 .scan_index
= ADXRS290_IDX_TEMP
,
562 .endianness
= IIO_LE
,
565 IIO_CHAN_SOFT_TIMESTAMP(ADXRS290_IDX_TS
),
568 static const unsigned long adxrs290_avail_scan_masks
[] = {
569 BIT(ADXRS290_IDX_X
) | BIT(ADXRS290_IDX_Y
) | BIT(ADXRS290_IDX_TEMP
),
573 static const struct iio_info adxrs290_info
= {
574 .read_raw
= &adxrs290_read_raw
,
575 .write_raw
= &adxrs290_write_raw
,
576 .read_avail
= &adxrs290_read_avail
,
577 .debugfs_reg_access
= &adxrs290_reg_access
,
580 static int adxrs290_probe_trigger(struct iio_dev
*indio_dev
)
582 struct adxrs290_state
*st
= iio_priv(indio_dev
);
586 dev_info(&st
->spi
->dev
, "no irq, using polling\n");
590 st
->dready_trig
= devm_iio_trigger_alloc(&st
->spi
->dev
, "%s-dev%d",
593 if (!st
->dready_trig
)
596 st
->dready_trig
->dev
.parent
= &st
->spi
->dev
;
597 st
->dready_trig
->ops
= &adxrs290_trigger_ops
;
598 iio_trigger_set_drvdata(st
->dready_trig
, indio_dev
);
600 ret
= devm_request_irq(&st
->spi
->dev
, st
->spi
->irq
,
601 &iio_trigger_generic_data_rdy_poll
,
602 IRQF_ONESHOT
, "adxrs290_irq", st
->dready_trig
);
604 return dev_err_probe(&st
->spi
->dev
, ret
,
605 "request irq %d failed\n", st
->spi
->irq
);
607 ret
= devm_iio_trigger_register(&st
->spi
->dev
, st
->dready_trig
);
609 dev_err(&st
->spi
->dev
, "iio trigger register failed\n");
613 indio_dev
->trig
= iio_trigger_get(st
->dready_trig
);
618 static int adxrs290_probe(struct spi_device
*spi
)
620 struct iio_dev
*indio_dev
;
621 struct adxrs290_state
*st
;
625 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
629 st
= iio_priv(indio_dev
);
632 indio_dev
->name
= "adxrs290";
633 indio_dev
->modes
= INDIO_DIRECT_MODE
;
634 indio_dev
->channels
= adxrs290_channels
;
635 indio_dev
->num_channels
= ARRAY_SIZE(adxrs290_channels
);
636 indio_dev
->info
= &adxrs290_info
;
637 indio_dev
->available_scan_masks
= adxrs290_avail_scan_masks
;
639 mutex_init(&st
->lock
);
641 val
= spi_w8r8(spi
, ADXRS290_READ_REG(ADXRS290_REG_ADI_ID
));
642 if (val
!= ADXRS290_ADI_ID
) {
643 dev_err(&spi
->dev
, "Wrong ADI ID 0x%02x\n", val
);
647 val
= spi_w8r8(spi
, ADXRS290_READ_REG(ADXRS290_REG_MEMS_ID
));
648 if (val
!= ADXRS290_MEMS_ID
) {
649 dev_err(&spi
->dev
, "Wrong MEMS ID 0x%02x\n", val
);
653 val
= spi_w8r8(spi
, ADXRS290_READ_REG(ADXRS290_REG_DEV_ID
));
654 if (val
!= ADXRS290_DEV_ID
) {
655 dev_err(&spi
->dev
, "Wrong DEV ID 0x%02x\n", val
);
659 /* default mode the gyroscope starts in */
660 st
->mode
= ADXRS290_MODE_STANDBY
;
662 /* switch to measurement mode and switch on the temperature sensor */
663 ret
= adxrs290_initial_setup(indio_dev
);
667 /* max transition time to measurement mode */
668 msleep(ADXRS290_MAX_TRANSITION_TIME_MS
);
670 ret
= adxrs290_get_3db_freq(indio_dev
, &val
, &val2
);
674 st
->lpf_3db_freq_idx
= val
;
675 st
->hpf_3db_freq_idx
= val2
;
677 ret
= devm_iio_triggered_buffer_setup(&spi
->dev
, indio_dev
,
678 &iio_pollfunc_store_time
,
679 &adxrs290_trigger_handler
, NULL
);
681 return dev_err_probe(&spi
->dev
, ret
,
682 "iio triggered buffer setup failed\n");
684 ret
= adxrs290_probe_trigger(indio_dev
);
688 return devm_iio_device_register(&spi
->dev
, indio_dev
);
691 static const struct of_device_id adxrs290_of_match
[] = {
692 { .compatible
= "adi,adxrs290" },
695 MODULE_DEVICE_TABLE(of
, adxrs290_of_match
);
697 static struct spi_driver adxrs290_driver
= {
700 .of_match_table
= adxrs290_of_match
,
702 .probe
= adxrs290_probe
,
704 module_spi_driver(adxrs290_driver
);
706 MODULE_AUTHOR("Nishant Malpani <nish.malpani25@gmail.com>");
707 MODULE_DESCRIPTION("Analog Devices ADXRS290 Gyroscope SPI driver");
708 MODULE_LICENSE("GPL");