xref: /openbmc/linux/drivers/iio/imu/adis_trigger.c (revision d9679d00)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Common library for ADIS16XXX devices
4  *
5  * Copyright 2012 Analog Devices Inc.
6  *   Author: Lars-Peter Clausen <lars@metafoo.de>
7  */
8 
9 #include <linux/interrupt.h>
10 #include <linux/kernel.h>
11 #include <linux/spi/spi.h>
12 #include <linux/export.h>
13 
14 #include <linux/iio/iio.h>
15 #include <linux/iio/trigger.h>
16 #include <linux/iio/imu/adis.h>
17 
18 static int adis_data_rdy_trigger_set_state(struct iio_trigger *trig,
19 						bool state)
20 {
21 	struct adis *adis = iio_trigger_get_drvdata(trig);
22 
23 	return adis_enable_irq(adis, state);
24 }
25 
26 static const struct iio_trigger_ops adis_trigger_ops = {
27 	.set_trigger_state = &adis_data_rdy_trigger_set_state,
28 };
29 
30 static int adis_validate_irq_flag(struct adis *adis)
31 {
32 	unsigned long direction = adis->irq_flag & IRQF_TRIGGER_MASK;
33 
34 	/* We cannot mask the interrupt so ensure it's not enabled at request */
35 	if (adis->data->unmasked_drdy)
36 		adis->irq_flag |= IRQF_NO_AUTOEN;
37 	/*
38 	 * Typically this devices have data ready either on the rising edge or
39 	 * on the falling edge of the data ready pin. This checks enforces that
40 	 * one of those is set in the drivers... It defaults to
41 	 * IRQF_TRIGGER_RISING for backward compatibility with devices that
42 	 * don't support changing the pin polarity.
43 	 */
44 	if (direction == IRQF_TRIGGER_NONE) {
45 		adis->irq_flag |= IRQF_TRIGGER_RISING;
46 		return 0;
47 	} else if (direction != IRQF_TRIGGER_RISING &&
48 		   direction != IRQF_TRIGGER_FALLING) {
49 		dev_err(&adis->spi->dev, "Invalid IRQ mask: %08lx\n",
50 			adis->irq_flag);
51 		return -EINVAL;
52 	}
53 
54 	return 0;
55 }
56 
57 /**
58  * devm_adis_probe_trigger() - Sets up trigger for a managed adis device
59  * @adis: The adis device
60  * @indio_dev: The IIO device
61  *
62  * Returns 0 on success or a negative error code
63  */
64 int devm_adis_probe_trigger(struct adis *adis, struct iio_dev *indio_dev)
65 {
66 	int ret;
67 
68 	adis->trig = devm_iio_trigger_alloc(&adis->spi->dev, "%s-dev%d",
69 					    indio_dev->name,
70 					    iio_device_id(indio_dev));
71 	if (!adis->trig)
72 		return -ENOMEM;
73 
74 	adis->trig->ops = &adis_trigger_ops;
75 	iio_trigger_set_drvdata(adis->trig, adis);
76 
77 	ret = adis_validate_irq_flag(adis);
78 	if (ret)
79 		return ret;
80 
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;
88 
89 	return devm_iio_trigger_register(&adis->spi->dev, adis->trig);
90 }
91 EXPORT_SYMBOL_GPL(devm_adis_probe_trigger);
92 
93