iio_dummy_evgen.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. #include <linux/kernel.h>
  16. #include <linux/slab.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/irq.h>
  19. #include <linux/mutex.h>
  20. #include <linux/module.h>
  21. #include <linux/sysfs.h>
  22. #include "iio_dummy_evgen.h"
  23. #include <linux/iio/iio.h>
  24. #include <linux/iio/sysfs.h>
  25. #include <linux/irq_work.h>
  26. /* Fiddly bit of faking and irq without hardware */
  27. #define IIO_EVENTGEN_NO 10
  28. /**
  29. * struct iio_dummy_handle_irq - helper struct to simulate interrupt generation
  30. * @work: irq_work used to run handlers from hardirq context
  31. * @irq: fake irq line number to trigger an interrupt
  32. */
  33. struct iio_dummy_handle_irq {
  34. struct irq_work work;
  35. int irq;
  36. };
  37. /**
  38. * struct iio_dummy_evgen - evgen state
  39. * @chip: irq chip we are faking
  40. * @base: base of irq range
  41. * @enabled: mask of which irqs are enabled
  42. * @inuse: mask of which irqs are connected
  43. * @regs: irq regs we are faking
  44. * @lock: protect the evgen state
  45. * @handler: helper for a 'hardware-like' interrupt simulation
  46. */
  47. struct iio_dummy_eventgen {
  48. struct irq_chip chip;
  49. int base;
  50. bool enabled[IIO_EVENTGEN_NO];
  51. bool inuse[IIO_EVENTGEN_NO];
  52. struct iio_dummy_regs regs[IIO_EVENTGEN_NO];
  53. struct mutex lock;
  54. struct iio_dummy_handle_irq handler;
  55. };
  56. /* We can only ever have one instance of this 'device' */
  57. static struct iio_dummy_eventgen *iio_evgen;
  58. static const char *iio_evgen_name = "iio_dummy_evgen";
  59. static void iio_dummy_event_irqmask(struct irq_data *d)
  60. {
  61. struct irq_chip *chip = irq_data_get_irq_chip(d);
  62. struct iio_dummy_eventgen *evgen =
  63. container_of(chip, struct iio_dummy_eventgen, chip);
  64. evgen->enabled[d->irq - evgen->base] = false;
  65. }
  66. static void iio_dummy_event_irqunmask(struct irq_data *d)
  67. {
  68. struct irq_chip *chip = irq_data_get_irq_chip(d);
  69. struct iio_dummy_eventgen *evgen =
  70. container_of(chip, struct iio_dummy_eventgen, chip);
  71. evgen->enabled[d->irq - evgen->base] = true;
  72. }
  73. static void iio_dummy_work_handler(struct irq_work *work)
  74. {
  75. struct iio_dummy_handle_irq *irq_handler;
  76. irq_handler = container_of(work, struct iio_dummy_handle_irq, work);
  77. handle_simple_irq(irq_to_desc(irq_handler->irq));
  78. }
  79. static int iio_dummy_evgen_create(void)
  80. {
  81. int ret, i;
  82. iio_evgen = kzalloc(sizeof(*iio_evgen), GFP_KERNEL);
  83. if (!iio_evgen)
  84. return -ENOMEM;
  85. iio_evgen->base = irq_alloc_descs(-1, 0, IIO_EVENTGEN_NO, 0);
  86. if (iio_evgen->base < 0) {
  87. ret = iio_evgen->base;
  88. kfree(iio_evgen);
  89. return ret;
  90. }
  91. iio_evgen->chip.name = iio_evgen_name;
  92. iio_evgen->chip.irq_mask = &iio_dummy_event_irqmask;
  93. iio_evgen->chip.irq_unmask = &iio_dummy_event_irqunmask;
  94. for (i = 0; i < IIO_EVENTGEN_NO; i++) {
  95. irq_set_chip(iio_evgen->base + i, &iio_evgen->chip);
  96. irq_set_handler(iio_evgen->base + i, &handle_simple_irq);
  97. irq_modify_status(iio_evgen->base + i,
  98. IRQ_NOREQUEST | IRQ_NOAUTOEN,
  99. IRQ_NOPROBE);
  100. }
  101. init_irq_work(&iio_evgen->handler.work, iio_dummy_work_handler);
  102. mutex_init(&iio_evgen->lock);
  103. return 0;
  104. }
  105. /**
  106. * iio_dummy_evgen_get_irq() - get an evgen provided irq for a device
  107. *
  108. * This function will give a free allocated irq to a client device.
  109. * That irq can then be caused to 'fire' by using the associated sysfs file.
  110. */
  111. int iio_dummy_evgen_get_irq(void)
  112. {
  113. int i, ret = 0;
  114. if (!iio_evgen)
  115. return -ENODEV;
  116. mutex_lock(&iio_evgen->lock);
  117. for (i = 0; i < IIO_EVENTGEN_NO; i++)
  118. if (!iio_evgen->inuse[i]) {
  119. ret = iio_evgen->base + i;
  120. iio_evgen->inuse[i] = true;
  121. break;
  122. }
  123. mutex_unlock(&iio_evgen->lock);
  124. if (i == IIO_EVENTGEN_NO)
  125. return -ENOMEM;
  126. return ret;
  127. }
  128. EXPORT_SYMBOL_GPL(iio_dummy_evgen_get_irq);
  129. /**
  130. * iio_dummy_evgen_release_irq() - give the irq back.
  131. * @irq: irq being returned to the pool
  132. *
  133. * Used by client driver instances to give the irqs back when they disconnect
  134. */
  135. void iio_dummy_evgen_release_irq(int irq)
  136. {
  137. mutex_lock(&iio_evgen->lock);
  138. iio_evgen->inuse[irq - iio_evgen->base] = false;
  139. mutex_unlock(&iio_evgen->lock);
  140. }
  141. EXPORT_SYMBOL_GPL(iio_dummy_evgen_release_irq);
  142. struct iio_dummy_regs *iio_dummy_evgen_get_regs(int irq)
  143. {
  144. return &iio_evgen->regs[irq - iio_evgen->base];
  145. }
  146. EXPORT_SYMBOL_GPL(iio_dummy_evgen_get_regs);
  147. static void iio_dummy_evgen_free(void)
  148. {
  149. irq_free_descs(iio_evgen->base, IIO_EVENTGEN_NO);
  150. kfree(iio_evgen);
  151. }
  152. static void iio_evgen_release(struct device *dev)
  153. {
  154. iio_dummy_evgen_free();
  155. }
  156. static ssize_t iio_evgen_poke(struct device *dev,
  157. struct device_attribute *attr,
  158. const char *buf,
  159. size_t len)
  160. {
  161. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  162. unsigned long event;
  163. int ret;
  164. ret = kstrtoul(buf, 10, &event);
  165. if (ret)
  166. return ret;
  167. iio_evgen->regs[this_attr->address].reg_id = this_attr->address;
  168. iio_evgen->regs[this_attr->address].reg_data = event;
  169. iio_evgen->handler.irq = iio_evgen->base + this_attr->address;
  170. if (iio_evgen->enabled[this_attr->address])
  171. irq_work_queue(&iio_evgen->handler.work);
  172. return len;
  173. }
  174. static IIO_DEVICE_ATTR(poke_ev0, S_IWUSR, NULL, &iio_evgen_poke, 0);
  175. static IIO_DEVICE_ATTR(poke_ev1, S_IWUSR, NULL, &iio_evgen_poke, 1);
  176. static IIO_DEVICE_ATTR(poke_ev2, S_IWUSR, NULL, &iio_evgen_poke, 2);
  177. static IIO_DEVICE_ATTR(poke_ev3, S_IWUSR, NULL, &iio_evgen_poke, 3);
  178. static IIO_DEVICE_ATTR(poke_ev4, S_IWUSR, NULL, &iio_evgen_poke, 4);
  179. static IIO_DEVICE_ATTR(poke_ev5, S_IWUSR, NULL, &iio_evgen_poke, 5);
  180. static IIO_DEVICE_ATTR(poke_ev6, S_IWUSR, NULL, &iio_evgen_poke, 6);
  181. static IIO_DEVICE_ATTR(poke_ev7, S_IWUSR, NULL, &iio_evgen_poke, 7);
  182. static IIO_DEVICE_ATTR(poke_ev8, S_IWUSR, NULL, &iio_evgen_poke, 8);
  183. static IIO_DEVICE_ATTR(poke_ev9, S_IWUSR, NULL, &iio_evgen_poke, 9);
  184. static struct attribute *iio_evgen_attrs[] = {
  185. &iio_dev_attr_poke_ev0.dev_attr.attr,
  186. &iio_dev_attr_poke_ev1.dev_attr.attr,
  187. &iio_dev_attr_poke_ev2.dev_attr.attr,
  188. &iio_dev_attr_poke_ev3.dev_attr.attr,
  189. &iio_dev_attr_poke_ev4.dev_attr.attr,
  190. &iio_dev_attr_poke_ev5.dev_attr.attr,
  191. &iio_dev_attr_poke_ev6.dev_attr.attr,
  192. &iio_dev_attr_poke_ev7.dev_attr.attr,
  193. &iio_dev_attr_poke_ev8.dev_attr.attr,
  194. &iio_dev_attr_poke_ev9.dev_attr.attr,
  195. NULL,
  196. };
  197. static const struct attribute_group iio_evgen_group = {
  198. .attrs = iio_evgen_attrs,
  199. };
  200. static const struct attribute_group *iio_evgen_groups[] = {
  201. &iio_evgen_group,
  202. NULL
  203. };
  204. static struct device iio_evgen_dev = {
  205. .bus = &iio_bus_type,
  206. .groups = iio_evgen_groups,
  207. .release = &iio_evgen_release,
  208. };
  209. static __init int iio_dummy_evgen_init(void)
  210. {
  211. int ret = iio_dummy_evgen_create();
  212. if (ret < 0)
  213. return ret;
  214. device_initialize(&iio_evgen_dev);
  215. dev_set_name(&iio_evgen_dev, "iio_evgen");
  216. return device_add(&iio_evgen_dev);
  217. }
  218. module_init(iio_dummy_evgen_init);
  219. static __exit void iio_dummy_evgen_exit(void)
  220. {
  221. device_unregister(&iio_evgen_dev);
  222. }
  223. module_exit(iio_dummy_evgen_exit);
  224. MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
  225. MODULE_DESCRIPTION("IIO dummy driver");
  226. MODULE_LICENSE("GPL v2");