2 * Industrial I/O - gpio based trigger support
4 * Copyright (c) 2008 Jonathan Cameron
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
10 * Currently this is more of a functioning proof of concept that a fully
11 * fledged trigger driver.
15 * Add board config elements to allow specification of startup settings.
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/interrupt.h>
22 #include <linux/gpio.h>
23 #include <linux/slab.h>
26 #include "../trigger.h"
28 static LIST_HEAD(iio_gpio_trigger_list);
29 static DEFINE_MUTEX(iio_gpio_trigger_list_lock);
31 struct iio_gpio_trigger_info {
36 * Need to reference count these triggers and only enable gpio interrupts
40 /* So what functionality do we want in here?... */
41 /* set high / low as interrupt type? */
43 static irqreturn_t iio_gpio_trigger_poll(int irq, void *private)
45 /* Timestamp not currently provided */
46 iio_trigger_poll(private, 0);
50 static IIO_TRIGGER_NAME_ATTR;
52 static struct attribute *iio_gpio_trigger_attrs[] = {
57 static const struct attribute_group iio_gpio_trigger_attr_group = {
58 .attrs = iio_gpio_trigger_attrs,
61 static int iio_gpio_trigger_probe(struct platform_device *pdev)
63 struct iio_gpio_trigger_info *trig_info;
64 struct iio_trigger *trig, *trig2;
65 unsigned long irqflags;
66 struct resource *irq_res;
67 int irq, ret = 0, irq_res_cnt = 0;
70 irq_res = platform_get_resource(pdev,
71 IORESOURCE_IRQ, irq_res_cnt);
73 if (irq_res == NULL) {
75 dev_err(&pdev->dev, "No GPIO IRQs specified");
78 irqflags = (irq_res->flags & IRQF_TRIGGER_MASK) | IRQF_SHARED;
80 for (irq = irq_res->start; irq <= irq_res->end; irq++) {
82 trig = iio_allocate_trigger();
85 goto error_free_completed_registrations;
88 trig_info = kzalloc(sizeof(*trig_info), GFP_KERNEL);
91 goto error_put_trigger;
93 trig->control_attrs = &iio_gpio_trigger_attr_group;
94 trig->private_data = trig_info;
96 trig->owner = THIS_MODULE;
97 trig->name = kasprintf(GFP_KERNEL, "irqtrig%d", irq);
98 if (trig->name == NULL) {
100 goto error_free_trig_info;
102 ret = request_irq(irq, iio_gpio_trigger_poll,
103 irqflags, trig->name, trig);
106 "request IRQ-%d failed", irq);
107 goto error_free_name;
110 ret = iio_trigger_register(trig);
112 goto error_release_irq;
114 list_add_tail(&trig->alloc_list,
115 &iio_gpio_trigger_list);
119 } while (irq_res != NULL);
124 /* First clean up the partly allocated trigger */
129 error_free_trig_info:
132 iio_put_trigger(trig);
133 error_free_completed_registrations:
134 /* The rest should have been added to the iio_gpio_trigger_list */
135 list_for_each_entry_safe(trig,
137 &iio_gpio_trigger_list,
139 trig_info = trig->private_data;
140 free_irq(gpio_to_irq(trig_info->irq), trig);
143 iio_trigger_unregister(trig);
149 static int iio_gpio_trigger_remove(struct platform_device *pdev)
151 struct iio_trigger *trig, *trig2;
152 struct iio_gpio_trigger_info *trig_info;
154 mutex_lock(&iio_gpio_trigger_list_lock);
155 list_for_each_entry_safe(trig,
157 &iio_gpio_trigger_list,
159 trig_info = trig->private_data;
160 iio_trigger_unregister(trig);
161 free_irq(trig_info->irq, trig);
164 iio_put_trigger(trig);
166 mutex_unlock(&iio_gpio_trigger_list_lock);
171 static struct platform_driver iio_gpio_trigger_driver = {
172 .probe = iio_gpio_trigger_probe,
173 .remove = iio_gpio_trigger_remove,
175 .name = "iio_gpio_trigger",
176 .owner = THIS_MODULE,
180 static int __init iio_gpio_trig_init(void)
182 return platform_driver_register(&iio_gpio_trigger_driver);
184 module_init(iio_gpio_trig_init);
186 static void __exit iio_gpio_trig_exit(void)
188 platform_driver_unregister(&iio_gpio_trigger_driver);
190 module_exit(iio_gpio_trig_exit);
192 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
193 MODULE_DESCRIPTION("Example gpio trigger for the iio subsystem");
194 MODULE_LICENSE("GPL v2");