1 /**
2  * Copyright (c) 2011 Jonathan Cameron
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 as published by
6  * the Free Software Foundation.
7  *
8  * Companion module to the iio simple dummy example driver.
9  * The purpose of this is to generate 'fake' event interrupts thus
10  * allowing that driver's code to be as close as possible to that of
11  * a normal driver talking to hardware.  The approach used here
12  * is not intended to be general and just happens to work for this
13  * particular use case.
14  */
15 
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 #include <linux/interrupt.h>
19 #include <linux/irq.h>
20 #include <linux/mutex.h>
21 #include <linux/module.h>
22 #include <linux/sysfs.h>
23 
24 #include "iio_dummy_evgen.h"
25 #include <linux/iio/iio.h>
26 #include <linux/iio/sysfs.h>
27 #include <linux/irq_sim.h>
28 
29 /* Fiddly bit of faking and irq without hardware */
30 #define IIO_EVENTGEN_NO 10
31 
32 /**
33  * @regs: irq regs we are faking
34  * @lock: protect the evgen state
35  * @inuse: mask of which irqs are connected
36  * @irq_sim: interrupt simulator
37  * @base: base of irq range
38  */
39 struct iio_dummy_eventgen {
40 	struct iio_dummy_regs regs[IIO_EVENTGEN_NO];
41 	struct mutex lock;
42 	bool inuse[IIO_EVENTGEN_NO];
43 	struct irq_sim irq_sim;
44 	int base;
45 };
46 
47 /* We can only ever have one instance of this 'device' */
48 static struct iio_dummy_eventgen *iio_evgen;
49 
50 static int iio_dummy_evgen_create(void)
51 {
52 	int ret;
53 
54 	iio_evgen = kzalloc(sizeof(*iio_evgen), GFP_KERNEL);
55 	if (!iio_evgen)
56 		return -ENOMEM;
57 
58 	ret = irq_sim_init(&iio_evgen->irq_sim, IIO_EVENTGEN_NO);
59 	if (ret < 0) {
60 		kfree(iio_evgen);
61 		return ret;
62 	}
63 
64 	iio_evgen->base = irq_sim_irqnum(&iio_evgen->irq_sim, 0);
65 	mutex_init(&iio_evgen->lock);
66 
67 	return 0;
68 }
69 
70 /**
71  * iio_dummy_evgen_get_irq() - get an evgen provided irq for a device
72  *
73  * This function will give a free allocated irq to a client device.
74  * That irq can then be caused to 'fire' by using the associated sysfs file.
75  */
76 int iio_dummy_evgen_get_irq(void)
77 {
78 	int i, ret = 0;
79 
80 	if (!iio_evgen)
81 		return -ENODEV;
82 
83 	mutex_lock(&iio_evgen->lock);
84 	for (i = 0; i < IIO_EVENTGEN_NO; i++) {
85 		if (!iio_evgen->inuse[i]) {
86 			ret = irq_sim_irqnum(&iio_evgen->irq_sim, i);
87 			iio_evgen->inuse[i] = true;
88 			break;
89 		}
90 	}
91 	mutex_unlock(&iio_evgen->lock);
92 	if (i == IIO_EVENTGEN_NO)
93 		return -ENOMEM;
94 
95 	return ret;
96 }
97 EXPORT_SYMBOL_GPL(iio_dummy_evgen_get_irq);
98 
99 /**
100  * iio_dummy_evgen_release_irq() - give the irq back.
101  * @irq: irq being returned to the pool
102  *
103  * Used by client driver instances to give the irqs back when they disconnect
104  */
105 void iio_dummy_evgen_release_irq(int irq)
106 {
107 	mutex_lock(&iio_evgen->lock);
108 	iio_evgen->inuse[irq - iio_evgen->base] = false;
109 	mutex_unlock(&iio_evgen->lock);
110 }
111 EXPORT_SYMBOL_GPL(iio_dummy_evgen_release_irq);
112 
113 struct iio_dummy_regs *iio_dummy_evgen_get_regs(int irq)
114 {
115 	return &iio_evgen->regs[irq - iio_evgen->base];
116 }
117 EXPORT_SYMBOL_GPL(iio_dummy_evgen_get_regs);
118 
119 static void iio_dummy_evgen_free(void)
120 {
121 	irq_sim_fini(&iio_evgen->irq_sim);
122 	kfree(iio_evgen);
123 }
124 
125 static void iio_evgen_release(struct device *dev)
126 {
127 	iio_dummy_evgen_free();
128 }
129 
130 static ssize_t iio_evgen_poke(struct device *dev,
131 			      struct device_attribute *attr,
132 			      const char *buf,
133 			      size_t len)
134 {
135 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
136 	unsigned long event;
137 	int ret;
138 
139 	ret = kstrtoul(buf, 10, &event);
140 	if (ret)
141 		return ret;
142 
143 	iio_evgen->regs[this_attr->address].reg_id   = this_attr->address;
144 	iio_evgen->regs[this_attr->address].reg_data = event;
145 
146 	irq_sim_fire(&iio_evgen->irq_sim, this_attr->address);
147 
148 	return len;
149 }
150 
151 static IIO_DEVICE_ATTR(poke_ev0, S_IWUSR, NULL, &iio_evgen_poke, 0);
152 static IIO_DEVICE_ATTR(poke_ev1, S_IWUSR, NULL, &iio_evgen_poke, 1);
153 static IIO_DEVICE_ATTR(poke_ev2, S_IWUSR, NULL, &iio_evgen_poke, 2);
154 static IIO_DEVICE_ATTR(poke_ev3, S_IWUSR, NULL, &iio_evgen_poke, 3);
155 static IIO_DEVICE_ATTR(poke_ev4, S_IWUSR, NULL, &iio_evgen_poke, 4);
156 static IIO_DEVICE_ATTR(poke_ev5, S_IWUSR, NULL, &iio_evgen_poke, 5);
157 static IIO_DEVICE_ATTR(poke_ev6, S_IWUSR, NULL, &iio_evgen_poke, 6);
158 static IIO_DEVICE_ATTR(poke_ev7, S_IWUSR, NULL, &iio_evgen_poke, 7);
159 static IIO_DEVICE_ATTR(poke_ev8, S_IWUSR, NULL, &iio_evgen_poke, 8);
160 static IIO_DEVICE_ATTR(poke_ev9, S_IWUSR, NULL, &iio_evgen_poke, 9);
161 
162 static struct attribute *iio_evgen_attrs[] = {
163 	&iio_dev_attr_poke_ev0.dev_attr.attr,
164 	&iio_dev_attr_poke_ev1.dev_attr.attr,
165 	&iio_dev_attr_poke_ev2.dev_attr.attr,
166 	&iio_dev_attr_poke_ev3.dev_attr.attr,
167 	&iio_dev_attr_poke_ev4.dev_attr.attr,
168 	&iio_dev_attr_poke_ev5.dev_attr.attr,
169 	&iio_dev_attr_poke_ev6.dev_attr.attr,
170 	&iio_dev_attr_poke_ev7.dev_attr.attr,
171 	&iio_dev_attr_poke_ev8.dev_attr.attr,
172 	&iio_dev_attr_poke_ev9.dev_attr.attr,
173 	NULL,
174 };
175 
176 static const struct attribute_group iio_evgen_group = {
177 	.attrs = iio_evgen_attrs,
178 };
179 
180 static const struct attribute_group *iio_evgen_groups[] = {
181 	&iio_evgen_group,
182 	NULL
183 };
184 
185 static struct device iio_evgen_dev = {
186 	.bus = &iio_bus_type,
187 	.groups = iio_evgen_groups,
188 	.release = &iio_evgen_release,
189 };
190 
191 static __init int iio_dummy_evgen_init(void)
192 {
193 	int ret = iio_dummy_evgen_create();
194 
195 	if (ret < 0)
196 		return ret;
197 	device_initialize(&iio_evgen_dev);
198 	dev_set_name(&iio_evgen_dev, "iio_evgen");
199 	return device_add(&iio_evgen_dev);
200 }
201 module_init(iio_dummy_evgen_init);
202 
203 static __exit void iio_dummy_evgen_exit(void)
204 {
205 	device_unregister(&iio_evgen_dev);
206 }
207 module_exit(iio_dummy_evgen_exit);
208 
209 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
210 MODULE_DESCRIPTION("IIO dummy driver");
211 MODULE_LICENSE("GPL v2");
212