blob: 5a0072727ba4b1f3c57a858c6cb0042937c75763 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Lee Jonesee210142020-07-16 14:59:13 +01002/*
Jonathan Camerone6477002011-10-14 16:34:14 +01003 * Copyright (c) 2011 Jonathan Cameron
4 *
Jonathan Camerone6477002011-10-14 16:34:14 +01005 * Companion module to the iio simple dummy example driver.
6 * The purpose of this is to generate 'fake' event interrupts thus
7 * allowing that driver's code to be as close as possible to that of
8 * a normal driver talking to hardware. The approach used here
9 * is not intended to be general and just happens to work for this
10 * particular use case.
11 */
12
13#include <linux/kernel.h>
14#include <linux/slab.h>
15#include <linux/interrupt.h>
16#include <linux/irq.h>
17#include <linux/mutex.h>
18#include <linux/module.h>
19#include <linux/sysfs.h>
20
21#include "iio_dummy_evgen.h"
Jonathan Cameron06458e22012-04-25 15:54:58 +010022#include <linux/iio/iio.h>
23#include <linux/iio/sysfs.h>
Bartosz Golaszewskica481392017-09-28 12:56:41 +020024#include <linux/irq_sim.h>
Jonathan Camerone6477002011-10-14 16:34:14 +010025
26/* Fiddly bit of faking and irq without hardware */
27#define IIO_EVENTGEN_NO 10
Cristina Opriceanafd2bb312015-09-11 16:59:30 +030028
29/**
Lee Jonesee210142020-07-16 14:59:13 +010030 * struct iio_dummy_eventgen - event generator specific state
Daniel Baluta356ae942014-11-10 14:45:29 +020031 * @regs: irq regs we are faking
Jonathan Camerone6477002011-10-14 16:34:14 +010032 * @lock: protect the evgen state
Bartosz Golaszewskica481392017-09-28 12:56:41 +020033 * @inuse: mask of which irqs are connected
34 * @irq_sim: interrupt simulator
35 * @base: base of irq range
Lee Jonesee210142020-07-16 14:59:13 +010036 * @irq_sim_domain: irq simulator domain
Jonathan Camerone6477002011-10-14 16:34:14 +010037 */
38struct iio_dummy_eventgen {
Daniel Baluta356ae942014-11-10 14:45:29 +020039 struct iio_dummy_regs regs[IIO_EVENTGEN_NO];
Jonathan Camerone6477002011-10-14 16:34:14 +010040 struct mutex lock;
Bartosz Golaszewskica481392017-09-28 12:56:41 +020041 bool inuse[IIO_EVENTGEN_NO];
Bartosz Golaszewski337cbeb2020-05-14 10:39:01 +020042 struct irq_domain *irq_sim_domain;
Jonathan Camerone6477002011-10-14 16:34:14 +010043};
44
45/* We can only ever have one instance of this 'device' */
46static struct iio_dummy_eventgen *iio_evgen;
Cristina Opriceanafd2bb312015-09-11 16:59:30 +030047
Jonathan Camerone6477002011-10-14 16:34:14 +010048static int iio_dummy_evgen_create(void)
49{
Dan Carpenter128516e2020-05-20 15:03:06 +030050 int ret;
51
Jonathan Camerone6477002011-10-14 16:34:14 +010052 iio_evgen = kzalloc(sizeof(*iio_evgen), GFP_KERNEL);
Cristina Opriceana025c7da2015-03-31 12:51:10 +030053 if (!iio_evgen)
Jonathan Camerone6477002011-10-14 16:34:14 +010054 return -ENOMEM;
55
Bartosz Golaszewski337cbeb2020-05-14 10:39:01 +020056 iio_evgen->irq_sim_domain = irq_domain_create_sim(NULL,
57 IIO_EVENTGEN_NO);
58 if (IS_ERR(iio_evgen->irq_sim_domain)) {
Dan Carpenter128516e2020-05-20 15:03:06 +030059 ret = PTR_ERR(iio_evgen->irq_sim_domain);
Jonathan Camerone6477002011-10-14 16:34:14 +010060 kfree(iio_evgen);
Dan Carpenter128516e2020-05-20 15:03:06 +030061 return ret;
Jonathan Camerone6477002011-10-14 16:34:14 +010062 }
Bartosz Golaszewskica481392017-09-28 12:56:41 +020063
Jonathan Camerone6477002011-10-14 16:34:14 +010064 mutex_init(&iio_evgen->lock);
Bartosz Golaszewskica481392017-09-28 12:56:41 +020065
Jonathan Camerone6477002011-10-14 16:34:14 +010066 return 0;
67}
68
69/**
70 * iio_dummy_evgen_get_irq() - get an evgen provided irq for a device
71 *
72 * This function will give a free allocated irq to a client device.
73 * That irq can then be caused to 'fire' by using the associated sysfs file.
74 */
75int iio_dummy_evgen_get_irq(void)
76{
77 int i, ret = 0;
Sasha Levin5ae8f442011-11-22 08:02:21 +020078
Cristina Opriceana025c7da2015-03-31 12:51:10 +030079 if (!iio_evgen)
Sasha Levin5ae8f442011-11-22 08:02:21 +020080 return -ENODEV;
81
Jonathan Camerone6477002011-10-14 16:34:14 +010082 mutex_lock(&iio_evgen->lock);
Bartosz Golaszewskica481392017-09-28 12:56:41 +020083 for (i = 0; i < IIO_EVENTGEN_NO; i++) {
Lars-Peter Clausen6fae58f2012-10-18 15:43:00 +010084 if (!iio_evgen->inuse[i]) {
Bartosz Golaszewski337cbeb2020-05-14 10:39:01 +020085 ret = irq_create_mapping(iio_evgen->irq_sim_domain, i);
Jonathan Camerone6477002011-10-14 16:34:14 +010086 iio_evgen->inuse[i] = true;
87 break;
88 }
Bartosz Golaszewskica481392017-09-28 12:56:41 +020089 }
Jonathan Camerone6477002011-10-14 16:34:14 +010090 mutex_unlock(&iio_evgen->lock);
91 if (i == IIO_EVENTGEN_NO)
92 return -ENOMEM;
Bartosz Golaszewskica481392017-09-28 12:56:41 +020093
Jonathan Camerone6477002011-10-14 16:34:14 +010094 return ret;
95}
96EXPORT_SYMBOL_GPL(iio_dummy_evgen_get_irq);
97
98/**
99 * iio_dummy_evgen_release_irq() - give the irq back.
100 * @irq: irq being returned to the pool
101 *
102 * Used by client driver instances to give the irqs back when they disconnect
103 */
Vladimirs Ambrosovs62a90da2015-05-30 11:20:16 +0300104void iio_dummy_evgen_release_irq(int irq)
Jonathan Camerone6477002011-10-14 16:34:14 +0100105{
Bartosz Golaszewski337cbeb2020-05-14 10:39:01 +0200106 struct irq_data *irqd = irq_get_irq_data(irq);
107
Jonathan Camerone6477002011-10-14 16:34:14 +0100108 mutex_lock(&iio_evgen->lock);
Bartosz Golaszewski337cbeb2020-05-14 10:39:01 +0200109 iio_evgen->inuse[irqd_to_hwirq(irqd)] = false;
110 irq_dispose_mapping(irq);
Jonathan Camerone6477002011-10-14 16:34:14 +0100111 mutex_unlock(&iio_evgen->lock);
Jonathan Camerone6477002011-10-14 16:34:14 +0100112}
113EXPORT_SYMBOL_GPL(iio_dummy_evgen_release_irq);
114
Daniel Baluta356ae942014-11-10 14:45:29 +0200115struct iio_dummy_regs *iio_dummy_evgen_get_regs(int irq)
116{
Bartosz Golaszewski337cbeb2020-05-14 10:39:01 +0200117 struct irq_data *irqd = irq_get_irq_data(irq);
118
119 return &iio_evgen->regs[irqd_to_hwirq(irqd)];
120
Daniel Baluta356ae942014-11-10 14:45:29 +0200121}
122EXPORT_SYMBOL_GPL(iio_dummy_evgen_get_regs);
123
Jonathan Camerone6477002011-10-14 16:34:14 +0100124static void iio_dummy_evgen_free(void)
125{
Bartosz Golaszewski337cbeb2020-05-14 10:39:01 +0200126 irq_domain_remove_sim(iio_evgen->irq_sim_domain);
Jonathan Camerone6477002011-10-14 16:34:14 +0100127 kfree(iio_evgen);
128}
129
130static void iio_evgen_release(struct device *dev)
131{
132 iio_dummy_evgen_free();
133}
134
135static ssize_t iio_evgen_poke(struct device *dev,
136 struct device_attribute *attr,
137 const char *buf,
138 size_t len)
139{
140 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Daniel Baluta356ae942014-11-10 14:45:29 +0200141 unsigned long event;
Bartosz Golaszewski337cbeb2020-05-14 10:39:01 +0200142 int ret, irq;
Daniel Baluta356ae942014-11-10 14:45:29 +0200143
144 ret = kstrtoul(buf, 10, &event);
145 if (ret)
146 return ret;
147
148 iio_evgen->regs[this_attr->address].reg_id = this_attr->address;
149 iio_evgen->regs[this_attr->address].reg_data = event;
Jonathan Camerone6477002011-10-14 16:34:14 +0100150
Bartosz Golaszewski337cbeb2020-05-14 10:39:01 +0200151 irq = irq_find_mapping(iio_evgen->irq_sim_domain, this_attr->address);
152 ret = irq_set_irqchip_state(irq, IRQCHIP_STATE_PENDING, true);
153 if (ret)
154 return ret;
Jonathan Camerone6477002011-10-14 16:34:14 +0100155
156 return len;
157}
158
159static IIO_DEVICE_ATTR(poke_ev0, S_IWUSR, NULL, &iio_evgen_poke, 0);
160static IIO_DEVICE_ATTR(poke_ev1, S_IWUSR, NULL, &iio_evgen_poke, 1);
161static IIO_DEVICE_ATTR(poke_ev2, S_IWUSR, NULL, &iio_evgen_poke, 2);
162static IIO_DEVICE_ATTR(poke_ev3, S_IWUSR, NULL, &iio_evgen_poke, 3);
163static IIO_DEVICE_ATTR(poke_ev4, S_IWUSR, NULL, &iio_evgen_poke, 4);
164static IIO_DEVICE_ATTR(poke_ev5, S_IWUSR, NULL, &iio_evgen_poke, 5);
165static IIO_DEVICE_ATTR(poke_ev6, S_IWUSR, NULL, &iio_evgen_poke, 6);
166static IIO_DEVICE_ATTR(poke_ev7, S_IWUSR, NULL, &iio_evgen_poke, 7);
167static IIO_DEVICE_ATTR(poke_ev8, S_IWUSR, NULL, &iio_evgen_poke, 8);
168static IIO_DEVICE_ATTR(poke_ev9, S_IWUSR, NULL, &iio_evgen_poke, 9);
169
170static struct attribute *iio_evgen_attrs[] = {
171 &iio_dev_attr_poke_ev0.dev_attr.attr,
172 &iio_dev_attr_poke_ev1.dev_attr.attr,
173 &iio_dev_attr_poke_ev2.dev_attr.attr,
174 &iio_dev_attr_poke_ev3.dev_attr.attr,
175 &iio_dev_attr_poke_ev4.dev_attr.attr,
176 &iio_dev_attr_poke_ev5.dev_attr.attr,
177 &iio_dev_attr_poke_ev6.dev_attr.attr,
178 &iio_dev_attr_poke_ev7.dev_attr.attr,
179 &iio_dev_attr_poke_ev8.dev_attr.attr,
180 &iio_dev_attr_poke_ev9.dev_attr.attr,
181 NULL,
182};
183
184static const struct attribute_group iio_evgen_group = {
185 .attrs = iio_evgen_attrs,
186};
187
188static const struct attribute_group *iio_evgen_groups[] = {
189 &iio_evgen_group,
190 NULL
191};
192
193static struct device iio_evgen_dev = {
194 .bus = &iio_bus_type,
195 .groups = iio_evgen_groups,
196 .release = &iio_evgen_release,
197};
Cristina Opriceana862cb6c2015-07-10 17:10:21 +0300198
Jonathan Camerone6477002011-10-14 16:34:14 +0100199static __init int iio_dummy_evgen_init(void)
200{
201 int ret = iio_dummy_evgen_create();
Catalina Mocanu4b4c7272014-09-18 14:55:06 -0700202
Jonathan Camerone6477002011-10-14 16:34:14 +0100203 if (ret < 0)
204 return ret;
205 device_initialize(&iio_evgen_dev);
206 dev_set_name(&iio_evgen_dev, "iio_evgen");
Pan Biandfd4f642019-04-18 11:02:56 +0800207 ret = device_add(&iio_evgen_dev);
208 if (ret)
209 put_device(&iio_evgen_dev);
210 return ret;
Jonathan Camerone6477002011-10-14 16:34:14 +0100211}
212module_init(iio_dummy_evgen_init);
213
214static __exit void iio_dummy_evgen_exit(void)
215{
216 device_unregister(&iio_evgen_dev);
217}
218module_exit(iio_dummy_evgen_exit);
219
Jonathan Cameron0f8c9622012-09-02 21:34:59 +0100220MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
Jonathan Camerone6477002011-10-14 16:34:14 +0100221MODULE_DESCRIPTION("IIO dummy driver");
222MODULE_LICENSE("GPL v2");