iio_dummy_evgen.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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_sim.h>
  26. /* Fiddly bit of faking and irq without hardware */
  27. #define IIO_EVENTGEN_NO 10
  28. /**
  29. * @regs: irq regs we are faking
  30. * @lock: protect the evgen state
  31. * @inuse: mask of which irqs are connected
  32. * @irq_sim: interrupt simulator
  33. * @base: base of irq range
  34. */
  35. struct iio_dummy_eventgen {
  36. struct iio_dummy_regs regs[IIO_EVENTGEN_NO];
  37. struct mutex lock;
  38. bool inuse[IIO_EVENTGEN_NO];
  39. struct irq_sim irq_sim;
  40. int base;
  41. };
  42. /* We can only ever have one instance of this 'device' */
  43. static struct iio_dummy_eventgen *iio_evgen;
  44. static int iio_dummy_evgen_create(void)
  45. {
  46. int ret;
  47. iio_evgen = kzalloc(sizeof(*iio_evgen), GFP_KERNEL);
  48. if (!iio_evgen)
  49. return -ENOMEM;
  50. ret = irq_sim_init(&iio_evgen->irq_sim, IIO_EVENTGEN_NO);
  51. if (ret < 0) {
  52. kfree(iio_evgen);
  53. return ret;
  54. }
  55. iio_evgen->base = irq_sim_irqnum(&iio_evgen->irq_sim, 0);
  56. mutex_init(&iio_evgen->lock);
  57. return 0;
  58. }
  59. /**
  60. * iio_dummy_evgen_get_irq() - get an evgen provided irq for a device
  61. *
  62. * This function will give a free allocated irq to a client device.
  63. * That irq can then be caused to 'fire' by using the associated sysfs file.
  64. */
  65. int iio_dummy_evgen_get_irq(void)
  66. {
  67. int i, ret = 0;
  68. if (!iio_evgen)
  69. return -ENODEV;
  70. mutex_lock(&iio_evgen->lock);
  71. for (i = 0; i < IIO_EVENTGEN_NO; i++) {
  72. if (!iio_evgen->inuse[i]) {
  73. ret = irq_sim_irqnum(&iio_evgen->irq_sim, i);
  74. iio_evgen->inuse[i] = true;
  75. break;
  76. }
  77. }
  78. mutex_unlock(&iio_evgen->lock);
  79. if (i == IIO_EVENTGEN_NO)
  80. return -ENOMEM;
  81. return ret;
  82. }
  83. EXPORT_SYMBOL_GPL(iio_dummy_evgen_get_irq);
  84. /**
  85. * iio_dummy_evgen_release_irq() - give the irq back.
  86. * @irq: irq being returned to the pool
  87. *
  88. * Used by client driver instances to give the irqs back when they disconnect
  89. */
  90. void iio_dummy_evgen_release_irq(int irq)
  91. {
  92. mutex_lock(&iio_evgen->lock);
  93. iio_evgen->inuse[irq - iio_evgen->base] = false;
  94. mutex_unlock(&iio_evgen->lock);
  95. }
  96. EXPORT_SYMBOL_GPL(iio_dummy_evgen_release_irq);
  97. struct iio_dummy_regs *iio_dummy_evgen_get_regs(int irq)
  98. {
  99. return &iio_evgen->regs[irq - iio_evgen->base];
  100. }
  101. EXPORT_SYMBOL_GPL(iio_dummy_evgen_get_regs);
  102. static void iio_dummy_evgen_free(void)
  103. {
  104. irq_sim_fini(&iio_evgen->irq_sim);
  105. kfree(iio_evgen);
  106. }
  107. static void iio_evgen_release(struct device *dev)
  108. {
  109. iio_dummy_evgen_free();
  110. }
  111. static ssize_t iio_evgen_poke(struct device *dev,
  112. struct device_attribute *attr,
  113. const char *buf,
  114. size_t len)
  115. {
  116. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  117. unsigned long event;
  118. int ret;
  119. ret = kstrtoul(buf, 10, &event);
  120. if (ret)
  121. return ret;
  122. iio_evgen->regs[this_attr->address].reg_id = this_attr->address;
  123. iio_evgen->regs[this_attr->address].reg_data = event;
  124. irq_sim_fire(&iio_evgen->irq_sim, this_attr->address);
  125. return len;
  126. }
  127. static IIO_DEVICE_ATTR(poke_ev0, S_IWUSR, NULL, &iio_evgen_poke, 0);
  128. static IIO_DEVICE_ATTR(poke_ev1, S_IWUSR, NULL, &iio_evgen_poke, 1);
  129. static IIO_DEVICE_ATTR(poke_ev2, S_IWUSR, NULL, &iio_evgen_poke, 2);
  130. static IIO_DEVICE_ATTR(poke_ev3, S_IWUSR, NULL, &iio_evgen_poke, 3);
  131. static IIO_DEVICE_ATTR(poke_ev4, S_IWUSR, NULL, &iio_evgen_poke, 4);
  132. static IIO_DEVICE_ATTR(poke_ev5, S_IWUSR, NULL, &iio_evgen_poke, 5);
  133. static IIO_DEVICE_ATTR(poke_ev6, S_IWUSR, NULL, &iio_evgen_poke, 6);
  134. static IIO_DEVICE_ATTR(poke_ev7, S_IWUSR, NULL, &iio_evgen_poke, 7);
  135. static IIO_DEVICE_ATTR(poke_ev8, S_IWUSR, NULL, &iio_evgen_poke, 8);
  136. static IIO_DEVICE_ATTR(poke_ev9, S_IWUSR, NULL, &iio_evgen_poke, 9);
  137. static struct attribute *iio_evgen_attrs[] = {
  138. &iio_dev_attr_poke_ev0.dev_attr.attr,
  139. &iio_dev_attr_poke_ev1.dev_attr.attr,
  140. &iio_dev_attr_poke_ev2.dev_attr.attr,
  141. &iio_dev_attr_poke_ev3.dev_attr.attr,
  142. &iio_dev_attr_poke_ev4.dev_attr.attr,
  143. &iio_dev_attr_poke_ev5.dev_attr.attr,
  144. &iio_dev_attr_poke_ev6.dev_attr.attr,
  145. &iio_dev_attr_poke_ev7.dev_attr.attr,
  146. &iio_dev_attr_poke_ev8.dev_attr.attr,
  147. &iio_dev_attr_poke_ev9.dev_attr.attr,
  148. NULL,
  149. };
  150. static const struct attribute_group iio_evgen_group = {
  151. .attrs = iio_evgen_attrs,
  152. };
  153. static const struct attribute_group *iio_evgen_groups[] = {
  154. &iio_evgen_group,
  155. NULL
  156. };
  157. static struct device iio_evgen_dev = {
  158. .bus = &iio_bus_type,
  159. .groups = iio_evgen_groups,
  160. .release = &iio_evgen_release,
  161. };
  162. static __init int iio_dummy_evgen_init(void)
  163. {
  164. int ret = iio_dummy_evgen_create();
  165. if (ret < 0)
  166. return ret;
  167. device_initialize(&iio_evgen_dev);
  168. dev_set_name(&iio_evgen_dev, "iio_evgen");
  169. return device_add(&iio_evgen_dev);
  170. }
  171. module_init(iio_dummy_evgen_init);
  172. static __exit void iio_dummy_evgen_exit(void)
  173. {
  174. device_unregister(&iio_evgen_dev);
  175. }
  176. module_exit(iio_dummy_evgen_exit);
  177. MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
  178. MODULE_DESCRIPTION("IIO dummy driver");
  179. MODULE_LICENSE("GPL v2");