Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 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" |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 25 | #include <linux/iio/iio.h> |
| 26 | #include <linux/iio/sysfs.h> |
Bartosz Golaszewski | ca48139 | 2017-09-28 12:56:41 +0200 | [diff] [blame] | 27 | #include <linux/irq_sim.h> |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 28 | |
| 29 | /* Fiddly bit of faking and irq without hardware */ |
| 30 | #define IIO_EVENTGEN_NO 10 |
Cristina Opriceana | fd2bb31 | 2015-09-11 16:59:30 +0300 | [diff] [blame] | 31 | |
| 32 | /** |
Daniel Baluta | 356ae94 | 2014-11-10 14:45:29 +0200 | [diff] [blame] | 33 | * @regs: irq regs we are faking |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 34 | * @lock: protect the evgen state |
Bartosz Golaszewski | ca48139 | 2017-09-28 12:56:41 +0200 | [diff] [blame] | 35 | * @inuse: mask of which irqs are connected |
| 36 | * @irq_sim: interrupt simulator |
| 37 | * @base: base of irq range |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 38 | */ |
| 39 | struct iio_dummy_eventgen { |
Daniel Baluta | 356ae94 | 2014-11-10 14:45:29 +0200 | [diff] [blame] | 40 | struct iio_dummy_regs regs[IIO_EVENTGEN_NO]; |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 41 | struct mutex lock; |
Bartosz Golaszewski | ca48139 | 2017-09-28 12:56:41 +0200 | [diff] [blame] | 42 | bool inuse[IIO_EVENTGEN_NO]; |
| 43 | struct irq_sim irq_sim; |
| 44 | int base; |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | /* We can only ever have one instance of this 'device' */ |
| 48 | static struct iio_dummy_eventgen *iio_evgen; |
Cristina Opriceana | fd2bb31 | 2015-09-11 16:59:30 +0300 | [diff] [blame] | 49 | |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 50 | static int iio_dummy_evgen_create(void) |
| 51 | { |
Bartosz Golaszewski | ca48139 | 2017-09-28 12:56:41 +0200 | [diff] [blame] | 52 | int ret; |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 53 | |
| 54 | iio_evgen = kzalloc(sizeof(*iio_evgen), GFP_KERNEL); |
Cristina Opriceana | 025c7da | 2015-03-31 12:51:10 +0300 | [diff] [blame] | 55 | if (!iio_evgen) |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 56 | return -ENOMEM; |
| 57 | |
Bartosz Golaszewski | ca48139 | 2017-09-28 12:56:41 +0200 | [diff] [blame] | 58 | ret = irq_sim_init(&iio_evgen->irq_sim, IIO_EVENTGEN_NO); |
Bartosz Golaszewski | 1b39eac | 2017-11-27 11:54:49 +0100 | [diff] [blame] | 59 | if (ret < 0) { |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 60 | kfree(iio_evgen); |
| 61 | return ret; |
| 62 | } |
Bartosz Golaszewski | ca48139 | 2017-09-28 12:56:41 +0200 | [diff] [blame] | 63 | |
| 64 | iio_evgen->base = irq_sim_irqnum(&iio_evgen->irq_sim, 0); |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 65 | mutex_init(&iio_evgen->lock); |
Bartosz Golaszewski | ca48139 | 2017-09-28 12:56:41 +0200 | [diff] [blame] | 66 | |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 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; |
Sasha Levin | 5ae8f44 | 2011-11-22 08:02:21 +0200 | [diff] [blame] | 79 | |
Cristina Opriceana | 025c7da | 2015-03-31 12:51:10 +0300 | [diff] [blame] | 80 | if (!iio_evgen) |
Sasha Levin | 5ae8f44 | 2011-11-22 08:02:21 +0200 | [diff] [blame] | 81 | return -ENODEV; |
| 82 | |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 83 | mutex_lock(&iio_evgen->lock); |
Bartosz Golaszewski | ca48139 | 2017-09-28 12:56:41 +0200 | [diff] [blame] | 84 | for (i = 0; i < IIO_EVENTGEN_NO; i++) { |
Lars-Peter Clausen | 6fae58f | 2012-10-18 15:43:00 +0100 | [diff] [blame] | 85 | if (!iio_evgen->inuse[i]) { |
Bartosz Golaszewski | ca48139 | 2017-09-28 12:56:41 +0200 | [diff] [blame] | 86 | ret = irq_sim_irqnum(&iio_evgen->irq_sim, i); |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 87 | iio_evgen->inuse[i] = true; |
| 88 | break; |
| 89 | } |
Bartosz Golaszewski | ca48139 | 2017-09-28 12:56:41 +0200 | [diff] [blame] | 90 | } |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 91 | mutex_unlock(&iio_evgen->lock); |
| 92 | if (i == IIO_EVENTGEN_NO) |
| 93 | return -ENOMEM; |
Bartosz Golaszewski | ca48139 | 2017-09-28 12:56:41 +0200 | [diff] [blame] | 94 | |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 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 | */ |
Vladimirs Ambrosovs | 62a90da | 2015-05-30 11:20:16 +0300 | [diff] [blame] | 105 | void iio_dummy_evgen_release_irq(int irq) |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 106 | { |
| 107 | mutex_lock(&iio_evgen->lock); |
| 108 | iio_evgen->inuse[irq - iio_evgen->base] = false; |
| 109 | mutex_unlock(&iio_evgen->lock); |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 110 | } |
| 111 | EXPORT_SYMBOL_GPL(iio_dummy_evgen_release_irq); |
| 112 | |
Daniel Baluta | 356ae94 | 2014-11-10 14:45:29 +0200 | [diff] [blame] | 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 | |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 119 | static void iio_dummy_evgen_free(void) |
| 120 | { |
Bartosz Golaszewski | ca48139 | 2017-09-28 12:56:41 +0200 | [diff] [blame] | 121 | irq_sim_fini(&iio_evgen->irq_sim); |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 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); |
Daniel Baluta | 356ae94 | 2014-11-10 14:45:29 +0200 | [diff] [blame] | 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; |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 145 | |
Bartosz Golaszewski | ca48139 | 2017-09-28 12:56:41 +0200 | [diff] [blame] | 146 | irq_sim_fire(&iio_evgen->irq_sim, this_attr->address); |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 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 | }; |
Cristina Opriceana | 862cb6c | 2015-07-10 17:10:21 +0300 | [diff] [blame] | 190 | |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 191 | static __init int iio_dummy_evgen_init(void) |
| 192 | { |
| 193 | int ret = iio_dummy_evgen_create(); |
Catalina Mocanu | 4b4c727 | 2014-09-18 14:55:06 -0700 | [diff] [blame] | 194 | |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 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 | |
Jonathan Cameron | 0f8c962 | 2012-09-02 21:34:59 +0100 | [diff] [blame] | 209 | MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 210 | MODULE_DESCRIPTION("IIO dummy driver"); |
| 211 | MODULE_LICENSE("GPL v2"); |