blob: efd0005f59b4d0d61a492949971ba198ee7885e5 [file] [log] [blame]
Jonathan Camerone6477002011-10-14 16:34:14 +01001/**
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"
Jonathan Cameron06458e22012-04-25 15:54:58 +010025#include <linux/iio/iio.h>
26#include <linux/iio/sysfs.h>
Bartosz Golaszewskica481392017-09-28 12:56:41 +020027#include <linux/irq_sim.h>
Jonathan Camerone6477002011-10-14 16:34:14 +010028
29/* Fiddly bit of faking and irq without hardware */
30#define IIO_EVENTGEN_NO 10
Cristina Opriceanafd2bb312015-09-11 16:59:30 +030031
32/**
Daniel Baluta356ae942014-11-10 14:45:29 +020033 * @regs: irq regs we are faking
Jonathan Camerone6477002011-10-14 16:34:14 +010034 * @lock: protect the evgen state
Bartosz Golaszewskica481392017-09-28 12:56:41 +020035 * @inuse: mask of which irqs are connected
36 * @irq_sim: interrupt simulator
37 * @base: base of irq range
Jonathan Camerone6477002011-10-14 16:34:14 +010038 */
39struct iio_dummy_eventgen {
Daniel Baluta356ae942014-11-10 14:45:29 +020040 struct iio_dummy_regs regs[IIO_EVENTGEN_NO];
Jonathan Camerone6477002011-10-14 16:34:14 +010041 struct mutex lock;
Bartosz Golaszewskica481392017-09-28 12:56:41 +020042 bool inuse[IIO_EVENTGEN_NO];
43 struct irq_sim irq_sim;
44 int base;
Jonathan Camerone6477002011-10-14 16:34:14 +010045};
46
47/* We can only ever have one instance of this 'device' */
48static struct iio_dummy_eventgen *iio_evgen;
Cristina Opriceanafd2bb312015-09-11 16:59:30 +030049
Jonathan Camerone6477002011-10-14 16:34:14 +010050static int iio_dummy_evgen_create(void)
51{
Bartosz Golaszewskica481392017-09-28 12:56:41 +020052 int ret;
Jonathan Camerone6477002011-10-14 16:34:14 +010053
54 iio_evgen = kzalloc(sizeof(*iio_evgen), GFP_KERNEL);
Cristina Opriceana025c7da2015-03-31 12:51:10 +030055 if (!iio_evgen)
Jonathan Camerone6477002011-10-14 16:34:14 +010056 return -ENOMEM;
57
Bartosz Golaszewskica481392017-09-28 12:56:41 +020058 ret = irq_sim_init(&iio_evgen->irq_sim, IIO_EVENTGEN_NO);
Bartosz Golaszewski1b39eac2017-11-27 11:54:49 +010059 if (ret < 0) {
Jonathan Camerone6477002011-10-14 16:34:14 +010060 kfree(iio_evgen);
61 return ret;
62 }
Bartosz Golaszewskica481392017-09-28 12:56:41 +020063
64 iio_evgen->base = irq_sim_irqnum(&iio_evgen->irq_sim, 0);
Jonathan Camerone6477002011-10-14 16:34:14 +010065 mutex_init(&iio_evgen->lock);
Bartosz Golaszewskica481392017-09-28 12:56:41 +020066
Jonathan Camerone6477002011-10-14 16:34:14 +010067 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 */
76int iio_dummy_evgen_get_irq(void)
77{
78 int i, ret = 0;
Sasha Levin5ae8f442011-11-22 08:02:21 +020079
Cristina Opriceana025c7da2015-03-31 12:51:10 +030080 if (!iio_evgen)
Sasha Levin5ae8f442011-11-22 08:02:21 +020081 return -ENODEV;
82
Jonathan Camerone6477002011-10-14 16:34:14 +010083 mutex_lock(&iio_evgen->lock);
Bartosz Golaszewskica481392017-09-28 12:56:41 +020084 for (i = 0; i < IIO_EVENTGEN_NO; i++) {
Lars-Peter Clausen6fae58f2012-10-18 15:43:00 +010085 if (!iio_evgen->inuse[i]) {
Bartosz Golaszewskica481392017-09-28 12:56:41 +020086 ret = irq_sim_irqnum(&iio_evgen->irq_sim, i);
Jonathan Camerone6477002011-10-14 16:34:14 +010087 iio_evgen->inuse[i] = true;
88 break;
89 }
Bartosz Golaszewskica481392017-09-28 12:56:41 +020090 }
Jonathan Camerone6477002011-10-14 16:34:14 +010091 mutex_unlock(&iio_evgen->lock);
92 if (i == IIO_EVENTGEN_NO)
93 return -ENOMEM;
Bartosz Golaszewskica481392017-09-28 12:56:41 +020094
Jonathan Camerone6477002011-10-14 16:34:14 +010095 return ret;
96}
97EXPORT_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 */
Vladimirs Ambrosovs62a90da2015-05-30 11:20:16 +0300105void iio_dummy_evgen_release_irq(int irq)
Jonathan Camerone6477002011-10-14 16:34:14 +0100106{
107 mutex_lock(&iio_evgen->lock);
108 iio_evgen->inuse[irq - iio_evgen->base] = false;
109 mutex_unlock(&iio_evgen->lock);
Jonathan Camerone6477002011-10-14 16:34:14 +0100110}
111EXPORT_SYMBOL_GPL(iio_dummy_evgen_release_irq);
112
Daniel Baluta356ae942014-11-10 14:45:29 +0200113struct iio_dummy_regs *iio_dummy_evgen_get_regs(int irq)
114{
115 return &iio_evgen->regs[irq - iio_evgen->base];
116}
117EXPORT_SYMBOL_GPL(iio_dummy_evgen_get_regs);
118
Jonathan Camerone6477002011-10-14 16:34:14 +0100119static void iio_dummy_evgen_free(void)
120{
Bartosz Golaszewskica481392017-09-28 12:56:41 +0200121 irq_sim_fini(&iio_evgen->irq_sim);
Jonathan Camerone6477002011-10-14 16:34:14 +0100122 kfree(iio_evgen);
123}
124
125static void iio_evgen_release(struct device *dev)
126{
127 iio_dummy_evgen_free();
128}
129
130static 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);
Daniel Baluta356ae942014-11-10 14:45:29 +0200136 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;
Jonathan Camerone6477002011-10-14 16:34:14 +0100145
Bartosz Golaszewskica481392017-09-28 12:56:41 +0200146 irq_sim_fire(&iio_evgen->irq_sim, this_attr->address);
Jonathan Camerone6477002011-10-14 16:34:14 +0100147
148 return len;
149}
150
151static IIO_DEVICE_ATTR(poke_ev0, S_IWUSR, NULL, &iio_evgen_poke, 0);
152static IIO_DEVICE_ATTR(poke_ev1, S_IWUSR, NULL, &iio_evgen_poke, 1);
153static IIO_DEVICE_ATTR(poke_ev2, S_IWUSR, NULL, &iio_evgen_poke, 2);
154static IIO_DEVICE_ATTR(poke_ev3, S_IWUSR, NULL, &iio_evgen_poke, 3);
155static IIO_DEVICE_ATTR(poke_ev4, S_IWUSR, NULL, &iio_evgen_poke, 4);
156static IIO_DEVICE_ATTR(poke_ev5, S_IWUSR, NULL, &iio_evgen_poke, 5);
157static IIO_DEVICE_ATTR(poke_ev6, S_IWUSR, NULL, &iio_evgen_poke, 6);
158static IIO_DEVICE_ATTR(poke_ev7, S_IWUSR, NULL, &iio_evgen_poke, 7);
159static IIO_DEVICE_ATTR(poke_ev8, S_IWUSR, NULL, &iio_evgen_poke, 8);
160static IIO_DEVICE_ATTR(poke_ev9, S_IWUSR, NULL, &iio_evgen_poke, 9);
161
162static 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
176static const struct attribute_group iio_evgen_group = {
177 .attrs = iio_evgen_attrs,
178};
179
180static const struct attribute_group *iio_evgen_groups[] = {
181 &iio_evgen_group,
182 NULL
183};
184
185static struct device iio_evgen_dev = {
186 .bus = &iio_bus_type,
187 .groups = iio_evgen_groups,
188 .release = &iio_evgen_release,
189};
Cristina Opriceana862cb6c2015-07-10 17:10:21 +0300190
Jonathan Camerone6477002011-10-14 16:34:14 +0100191static __init int iio_dummy_evgen_init(void)
192{
193 int ret = iio_dummy_evgen_create();
Catalina Mocanu4b4c7272014-09-18 14:55:06 -0700194
Jonathan Camerone6477002011-10-14 16:34:14 +0100195 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}
201module_init(iio_dummy_evgen_init);
202
203static __exit void iio_dummy_evgen_exit(void)
204{
205 device_unregister(&iio_evgen_dev);
206}
207module_exit(iio_dummy_evgen_exit);
208
Jonathan Cameron0f8c9622012-09-02 21:34:59 +0100209MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
Jonathan Camerone6477002011-10-14 16:34:14 +0100210MODULE_DESCRIPTION("IIO dummy driver");
211MODULE_LICENSE("GPL v2");