Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / iio / trigger / iio-trig-interrupt.c
blob94a487caf42143c96f69d8e99f14ffd7e87b9106
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Industrial I/O - generic interrupt based trigger support
5 * Copyright (c) 2008-2013 Jonathan Cameron
6 */
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/interrupt.h>
12 #include <linux/slab.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/trigger.h>
18 struct iio_interrupt_trigger_info {
19 unsigned int irq;
22 static irqreturn_t iio_interrupt_trigger_poll(int irq, void *private)
24 iio_trigger_poll(private);
25 return IRQ_HANDLED;
28 static const struct iio_trigger_ops iio_interrupt_trigger_ops = {
31 static int iio_interrupt_trigger_probe(struct platform_device *pdev)
33 struct iio_interrupt_trigger_info *trig_info;
34 struct iio_trigger *trig;
35 unsigned long irqflags;
36 struct resource *irq_res;
37 int irq, ret = 0;
39 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
41 if (irq_res == NULL)
42 return -ENODEV;
44 irqflags = (irq_res->flags & IRQF_TRIGGER_MASK) | IRQF_SHARED;
46 irq = irq_res->start;
48 trig = iio_trigger_alloc("irqtrig%d", irq);
49 if (!trig) {
50 ret = -ENOMEM;
51 goto error_ret;
54 trig_info = kzalloc(sizeof(*trig_info), GFP_KERNEL);
55 if (!trig_info) {
56 ret = -ENOMEM;
57 goto error_free_trigger;
59 iio_trigger_set_drvdata(trig, trig_info);
60 trig_info->irq = irq;
61 trig->ops = &iio_interrupt_trigger_ops;
62 ret = request_irq(irq, iio_interrupt_trigger_poll,
63 irqflags, trig->name, trig);
64 if (ret) {
65 dev_err(&pdev->dev,
66 "request IRQ-%d failed", irq);
67 goto error_free_trig_info;
70 ret = iio_trigger_register(trig);
71 if (ret)
72 goto error_release_irq;
73 platform_set_drvdata(pdev, trig);
75 return 0;
77 /* First clean up the partly allocated trigger */
78 error_release_irq:
79 free_irq(irq, trig);
80 error_free_trig_info:
81 kfree(trig_info);
82 error_free_trigger:
83 iio_trigger_free(trig);
84 error_ret:
85 return ret;
88 static int iio_interrupt_trigger_remove(struct platform_device *pdev)
90 struct iio_trigger *trig;
91 struct iio_interrupt_trigger_info *trig_info;
93 trig = platform_get_drvdata(pdev);
94 trig_info = iio_trigger_get_drvdata(trig);
95 iio_trigger_unregister(trig);
96 free_irq(trig_info->irq, trig);
97 kfree(trig_info);
98 iio_trigger_free(trig);
100 return 0;
103 static struct platform_driver iio_interrupt_trigger_driver = {
104 .probe = iio_interrupt_trigger_probe,
105 .remove = iio_interrupt_trigger_remove,
106 .driver = {
107 .name = "iio_interrupt_trigger",
111 module_platform_driver(iio_interrupt_trigger_driver);
113 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
114 MODULE_DESCRIPTION("Interrupt trigger for the iio subsystem");
115 MODULE_LICENSE("GPL v2");