WIP FPC-III support
[linux/fpc-iii.git] / drivers / iio / imu / adis_trigger.c
blob64e0ba51cb18e3df5c625392bd88cf80c0ca7f70
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Common library for ADIS16XXX devices
5 * Copyright 2012 Analog Devices Inc.
6 * Author: Lars-Peter Clausen <lars@metafoo.de>
7 */
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,
19 bool state)
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;
48 return 0;
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",
52 adis->irq_flag);
53 return -EINVAL;
56 return 0;
59 /**
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)
68 int ret;
70 adis->trig = devm_iio_trigger_alloc(&adis->spi->dev, "%s-dev%d",
71 indio_dev->name, indio_dev->id);
72 if (!adis->trig)
73 return -ENOMEM;
75 adis_trigger_setup(adis);
77 ret = adis_validate_irq_flag(adis);
78 if (ret)
79 return ret;
81 ret = devm_request_irq(&adis->spi->dev, adis->spi->irq,
82 &iio_trigger_generic_data_rdy_poll,
83 adis->irq_flag,
84 indio_dev->name,
85 adis->trig);
86 if (ret)
87 return ret;
89 return devm_iio_trigger_register(&adis->spi->dev, adis->trig);
91 EXPORT_SYMBOL_GPL(devm_adis_probe_trigger);