1 /*
2 * Industrial I/O - gpio based trigger support
3 *
4 * Copyright (c) 2008 Jonathan Cameron
5 *
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.
9 *
10 * Currently this is more of a functioning proof of concept that a fully
11 * fledged trigger driver.
12 *
13 * TODO:
14 *
15 * Add board config elements to allow specification of startup settings.
16 */
17
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>
24
25 #include "../iio.h"
26 #include "../trigger.h"
27
28 static LIST_HEAD(iio_gpio_trigger_list);
29 static DEFINE_MUTEX(iio_gpio_trigger_list_lock);
30
31 struct iio_gpio_trigger_info {
32 struct mutex in_use;
33 unsigned int irq;
34 };
35 /*
36 * Need to reference count these triggers and only enable gpio interrupts
37 * as appropriate.
38 */
39
40 /* So what functionality do we want in here?... */
41 /* set high / low as interrupt type? */
42
iio_gpio_trigger_poll(int irq,void * private)43 static irqreturn_t iio_gpio_trigger_poll(int irq, void *private)
44 {
45 /* Timestamp not currently provided */
46 iio_trigger_poll(private, 0);
47 return IRQ_HANDLED;
48 }
49
50 static IIO_TRIGGER_NAME_ATTR;
51
52 static struct attribute *iio_gpio_trigger_attrs[] = {
53 &dev_attr_name.attr,
54 NULL,
55 };
56
57 static const struct attribute_group iio_gpio_trigger_attr_group = {
58 .attrs = iio_gpio_trigger_attrs,
59 };
60
iio_gpio_trigger_probe(struct platform_device * pdev)61 static int iio_gpio_trigger_probe(struct platform_device *pdev)
62 {
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;
68
69 do {
70 irq_res = platform_get_resource(pdev,
71 IORESOURCE_IRQ, irq_res_cnt);
72
73 if (irq_res == NULL) {
74 if (irq_res_cnt == 0)
75 dev_err(&pdev->dev, "No GPIO IRQs specified");
76 break;
77 }
78 irqflags = (irq_res->flags & IRQF_TRIGGER_MASK) | IRQF_SHARED;
79
80 for (irq = irq_res->start; irq <= irq_res->end; irq++) {
81
82 trig = iio_allocate_trigger();
83 if (!trig) {
84 ret = -ENOMEM;
85 goto error_free_completed_registrations;
86 }
87
88 trig_info = kzalloc(sizeof(*trig_info), GFP_KERNEL);
89 if (!trig_info) {
90 ret = -ENOMEM;
91 goto error_put_trigger;
92 }
93 trig->control_attrs = &iio_gpio_trigger_attr_group;
94 trig->private_data = trig_info;
95 trig_info->irq = irq;
96 trig->owner = THIS_MODULE;
97 trig->name = kasprintf(GFP_KERNEL, "irqtrig%d", irq);
98 if (trig->name == NULL) {
99 ret = -ENOMEM;
100 goto error_free_trig_info;
101 }
102 ret = request_irq(irq, iio_gpio_trigger_poll,
103 irqflags, trig->name, trig);
104 if (ret) {
105 dev_err(&pdev->dev,
106 "request IRQ-%d failed", irq);
107 goto error_free_name;
108 }
109
110 ret = iio_trigger_register(trig);
111 if (ret)
112 goto error_release_irq;
113
114 list_add_tail(&trig->alloc_list,
115 &iio_gpio_trigger_list);
116 }
117
118 irq_res_cnt++;
119 } while (irq_res != NULL);
120
121
122 return 0;
123
124 /* First clean up the partly allocated trigger */
125 error_release_irq:
126 free_irq(irq, trig);
127 error_free_name:
128 kfree(trig->name);
129 error_free_trig_info:
130 kfree(trig_info);
131 error_put_trigger:
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,
136 trig2,
137 &iio_gpio_trigger_list,
138 alloc_list) {
139 trig_info = trig->private_data;
140 free_irq(gpio_to_irq(trig_info->irq), trig);
141 kfree(trig->name);
142 kfree(trig_info);
143 iio_trigger_unregister(trig);
144 }
145
146 return ret;
147 }
148
iio_gpio_trigger_remove(struct platform_device * pdev)149 static int iio_gpio_trigger_remove(struct platform_device *pdev)
150 {
151 struct iio_trigger *trig, *trig2;
152 struct iio_gpio_trigger_info *trig_info;
153
154 mutex_lock(&iio_gpio_trigger_list_lock);
155 list_for_each_entry_safe(trig,
156 trig2,
157 &iio_gpio_trigger_list,
158 alloc_list) {
159 trig_info = trig->private_data;
160 iio_trigger_unregister(trig);
161 free_irq(trig_info->irq, trig);
162 kfree(trig->name);
163 kfree(trig_info);
164 iio_put_trigger(trig);
165 }
166 mutex_unlock(&iio_gpio_trigger_list_lock);
167
168 return 0;
169 }
170
171 static struct platform_driver iio_gpio_trigger_driver = {
172 .probe = iio_gpio_trigger_probe,
173 .remove = iio_gpio_trigger_remove,
174 .driver = {
175 .name = "iio_gpio_trigger",
176 .owner = THIS_MODULE,
177 },
178 };
179
iio_gpio_trig_init(void)180 static int __init iio_gpio_trig_init(void)
181 {
182 return platform_driver_register(&iio_gpio_trigger_driver);
183 }
184 module_init(iio_gpio_trig_init);
185
iio_gpio_trig_exit(void)186 static void __exit iio_gpio_trig_exit(void)
187 {
188 platform_driver_unregister(&iio_gpio_trigger_driver);
189 }
190 module_exit(iio_gpio_trig_exit);
191
192 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
193 MODULE_DESCRIPTION("Example gpio trigger for the iio subsystem");
194 MODULE_LICENSE("GPL v2");
195