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 than a full
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 <linux/iio/iio.h>
26 #include <linux/iio/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 const struct iio_trigger_ops iio_gpio_trigger_ops = {
51 .owner = THIS_MODULE,
52 };
53
iio_gpio_trigger_probe(struct platform_device * pdev)54 static int iio_gpio_trigger_probe(struct platform_device *pdev)
55 {
56 struct iio_gpio_trigger_info *trig_info;
57 struct iio_trigger *trig, *trig2;
58 unsigned long irqflags;
59 struct resource *irq_res;
60 int irq, ret = 0, irq_res_cnt = 0;
61
62 do {
63 irq_res = platform_get_resource(pdev,
64 IORESOURCE_IRQ, irq_res_cnt);
65
66 if (irq_res == NULL) {
67 if (irq_res_cnt == 0)
68 dev_err(&pdev->dev, "No GPIO IRQs specified");
69 break;
70 }
71 irqflags = (irq_res->flags & IRQF_TRIGGER_MASK) | IRQF_SHARED;
72
73 for (irq = irq_res->start; irq <= irq_res->end; irq++) {
74
75 trig = iio_trigger_alloc("irqtrig%d", irq);
76 if (!trig) {
77 ret = -ENOMEM;
78 goto error_free_completed_registrations;
79 }
80
81 trig_info = kzalloc(sizeof(*trig_info), GFP_KERNEL);
82 if (!trig_info) {
83 ret = -ENOMEM;
84 goto error_put_trigger;
85 }
86 iio_trigger_set_drvdata(trig, trig_info);
87 trig_info->irq = irq;
88 trig->ops = &iio_gpio_trigger_ops;
89 ret = request_irq(irq, iio_gpio_trigger_poll,
90 irqflags, trig->name, trig);
91 if (ret) {
92 dev_err(&pdev->dev,
93 "request IRQ-%d failed", irq);
94 goto error_free_trig_info;
95 }
96
97 ret = iio_trigger_register(trig);
98 if (ret)
99 goto error_release_irq;
100
101 list_add_tail(&trig->alloc_list,
102 &iio_gpio_trigger_list);
103 }
104
105 irq_res_cnt++;
106 } while (irq_res != NULL);
107
108
109 return 0;
110
111 /* First clean up the partly allocated trigger */
112 error_release_irq:
113 free_irq(irq, trig);
114 error_free_trig_info:
115 kfree(trig_info);
116 error_put_trigger:
117 iio_trigger_put(trig);
118 error_free_completed_registrations:
119 /* The rest should have been added to the iio_gpio_trigger_list */
120 list_for_each_entry_safe(trig,
121 trig2,
122 &iio_gpio_trigger_list,
123 alloc_list) {
124 trig_info = iio_trigger_get_drvdata(trig);
125 free_irq(gpio_to_irq(trig_info->irq), trig);
126 kfree(trig_info);
127 iio_trigger_unregister(trig);
128 }
129
130 return ret;
131 }
132
iio_gpio_trigger_remove(struct platform_device * pdev)133 static int iio_gpio_trigger_remove(struct platform_device *pdev)
134 {
135 struct iio_trigger *trig, *trig2;
136 struct iio_gpio_trigger_info *trig_info;
137
138 mutex_lock(&iio_gpio_trigger_list_lock);
139 list_for_each_entry_safe(trig,
140 trig2,
141 &iio_gpio_trigger_list,
142 alloc_list) {
143 trig_info = iio_trigger_get_drvdata(trig);
144 iio_trigger_unregister(trig);
145 free_irq(trig_info->irq, trig);
146 kfree(trig_info);
147 iio_trigger_put(trig);
148 }
149 mutex_unlock(&iio_gpio_trigger_list_lock);
150
151 return 0;
152 }
153
154 static struct platform_driver iio_gpio_trigger_driver = {
155 .probe = iio_gpio_trigger_probe,
156 .remove = iio_gpio_trigger_remove,
157 .driver = {
158 .name = "iio_gpio_trigger",
159 .owner = THIS_MODULE,
160 },
161 };
162
163 module_platform_driver(iio_gpio_trigger_driver);
164
165 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
166 MODULE_DESCRIPTION("Example gpio trigger for the iio subsystem");
167 MODULE_LICENSE("GPL v2");
168