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 void adis_trigger_setup(struct adis *adis) 31 { 32 adis->trig->dev.parent = &adis->spi->dev; 33 adis->trig->ops = &adis_trigger_ops; 34 iio_trigger_set_drvdata(adis->trig, adis); 35 } 36 37 static int adis_validate_irq_flag(struct adis *adis) 38 { 39 /* 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. 45 */ 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; 54 } 55 56 return 0; 57 } 58 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 63 * 64 * Returns 0 on success or a negative error code 65 */ 66 int devm_adis_probe_trigger(struct adis *adis, struct iio_dev *indio_dev) 67 { 68 int ret; 69 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; 74 75 adis_trigger_setup(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