1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Common library for ADIS16XXX devices
5 * Copyright 2012 Analog Devices Inc.
6 * Author: Lars-Peter Clausen <lars@metafoo.de>
9 #include <linux/interrupt.h>
10 #include <linux/kernel.h>
11 #include <linux/spi/spi.h>
12 #include <linux/export.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/trigger.h>
16 #include <linux/iio/imu/adis.h>
18 static int adis_data_rdy_trigger_set_state(struct iio_trigger
*trig
,
21 struct adis
*adis
= iio_trigger_get_drvdata(trig
);
23 return adis_enable_irq(adis
, state
);
26 static const struct iio_trigger_ops adis_trigger_ops
= {
27 .set_trigger_state
= &adis_data_rdy_trigger_set_state
,
30 static void adis_trigger_setup(struct adis
*adis
)
32 adis
->trig
->dev
.parent
= &adis
->spi
->dev
;
33 adis
->trig
->ops
= &adis_trigger_ops
;
34 iio_trigger_set_drvdata(adis
->trig
, adis
);
37 static int adis_validate_irq_flag(struct adis
*adis
)
40 * Typically this devices have data ready either on the rising edge or
41 * on the falling edge of the data ready pin. This checks enforces that
42 * one of those is set in the drivers... It defaults to
43 * IRQF_TRIGGER_RISING for backward compatibility wiht devices that
44 * don't support changing the pin polarity.
46 if (!adis
->irq_flag
) {
47 adis
->irq_flag
= IRQF_TRIGGER_RISING
;
49 } else if (adis
->irq_flag
!= IRQF_TRIGGER_RISING
&&
50 adis
->irq_flag
!= IRQF_TRIGGER_FALLING
) {
51 dev_err(&adis
->spi
->dev
, "Invalid IRQ mask: %08lx\n",
60 * devm_adis_probe_trigger() - Sets up trigger for a managed adis device
61 * @adis: The adis device
62 * @indio_dev: The IIO device
64 * Returns 0 on success or a negative error code
66 int devm_adis_probe_trigger(struct adis
*adis
, struct iio_dev
*indio_dev
)
70 adis
->trig
= devm_iio_trigger_alloc(&adis
->spi
->dev
, "%s-dev%d",
71 indio_dev
->name
, indio_dev
->id
);
75 adis_trigger_setup(adis
);
77 ret
= adis_validate_irq_flag(adis
);
81 ret
= devm_request_irq(&adis
->spi
->dev
, adis
->spi
->irq
,
82 &iio_trigger_generic_data_rdy_poll
,
89 return devm_iio_trigger_register(&adis
->spi
->dev
, adis
->trig
);
91 EXPORT_SYMBOL_GPL(devm_adis_probe_trigger
);