iio_dummy_evgen.c 5.7 KB

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