2 * Copyright (c) 2011 Jonathan Cameron
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.
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.
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>
24 #include "iio_dummy_evgen.h"
25 #include <linux/iio/iio.h>
26 #include <linux/iio/sysfs.h>
27 #include <linux/irq_sim.h>
29 /* Fiddly bit of faking and irq without hardware */
30 #define IIO_EVENTGEN_NO 10
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
39 struct iio_dummy_eventgen
{
40 struct iio_dummy_regs regs
[IIO_EVENTGEN_NO
];
42 bool inuse
[IIO_EVENTGEN_NO
];
43 struct irq_sim irq_sim
;
47 /* We can only ever have one instance of this 'device' */
48 static struct iio_dummy_eventgen
*iio_evgen
;
50 static int iio_dummy_evgen_create(void)
54 iio_evgen
= kzalloc(sizeof(*iio_evgen
), GFP_KERNEL
);
58 ret
= irq_sim_init(&iio_evgen
->irq_sim
, IIO_EVENTGEN_NO
);
64 iio_evgen
->base
= irq_sim_irqnum(&iio_evgen
->irq_sim
, 0);
65 mutex_init(&iio_evgen
->lock
);
71 * iio_dummy_evgen_get_irq() - get an evgen provided irq for a device
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.
76 int iio_dummy_evgen_get_irq(void)
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;
91 mutex_unlock(&iio_evgen
->lock
);
92 if (i
== IIO_EVENTGEN_NO
)
97 EXPORT_SYMBOL_GPL(iio_dummy_evgen_get_irq
);
100 * iio_dummy_evgen_release_irq() - give the irq back.
101 * @irq: irq being returned to the pool
103 * Used by client driver instances to give the irqs back when they disconnect
105 void iio_dummy_evgen_release_irq(int irq
)
107 mutex_lock(&iio_evgen
->lock
);
108 iio_evgen
->inuse
[irq
- iio_evgen
->base
] = false;
109 mutex_unlock(&iio_evgen
->lock
);
111 EXPORT_SYMBOL_GPL(iio_dummy_evgen_release_irq
);
113 struct iio_dummy_regs
*iio_dummy_evgen_get_regs(int irq
)
115 return &iio_evgen
->regs
[irq
- iio_evgen
->base
];
117 EXPORT_SYMBOL_GPL(iio_dummy_evgen_get_regs
);
119 static void iio_dummy_evgen_free(void)
121 irq_sim_fini(&iio_evgen
->irq_sim
);
125 static void iio_evgen_release(struct device
*dev
)
127 iio_dummy_evgen_free();
130 static ssize_t
iio_evgen_poke(struct device
*dev
,
131 struct device_attribute
*attr
,
135 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
139 ret
= kstrtoul(buf
, 10, &event
);
143 iio_evgen
->regs
[this_attr
->address
].reg_id
= this_attr
->address
;
144 iio_evgen
->regs
[this_attr
->address
].reg_data
= event
;
146 irq_sim_fire(&iio_evgen
->irq_sim
, this_attr
->address
);
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);
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
,
176 static const struct attribute_group iio_evgen_group
= {
177 .attrs
= iio_evgen_attrs
,
180 static const struct attribute_group
*iio_evgen_groups
[] = {
185 static struct device iio_evgen_dev
= {
186 .bus
= &iio_bus_type
,
187 .groups
= iio_evgen_groups
,
188 .release
= &iio_evgen_release
,
191 static __init
int iio_dummy_evgen_init(void)
193 int ret
= iio_dummy_evgen_create();
197 device_initialize(&iio_evgen_dev
);
198 dev_set_name(&iio_evgen_dev
, "iio_evgen");
199 return device_add(&iio_evgen_dev
);
201 module_init(iio_dummy_evgen_init
);
203 static __exit
void iio_dummy_evgen_exit(void)
205 device_unregister(&iio_evgen_dev
);
207 module_exit(iio_dummy_evgen_exit
);
209 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
210 MODULE_DESCRIPTION("IIO dummy driver");
211 MODULE_LICENSE("GPL v2");