iio_simple_dummy_events.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. * Event handling elements of industrial I/O reference driver.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/irq.h>
  14. #include <linux/iio/iio.h>
  15. #include <linux/iio/sysfs.h>
  16. #include <linux/iio/events.h>
  17. #include "iio_simple_dummy.h"
  18. /* Evgen 'fakes' interrupt events for this example */
  19. #include "iio_dummy_evgen.h"
  20. /**
  21. * iio_simple_dummy_read_event_config() - is event enabled?
  22. * @indio_dev: the device instance data
  23. * @chan: channel for the event whose state is being queried
  24. * @type: type of the event whose state is being queried
  25. * @dir: direction of the vent whose state is being queried
  26. *
  27. * This function would normally query the relevant registers or a cache to
  28. * discover if the event generation is enabled on the device.
  29. */
  30. int iio_simple_dummy_read_event_config(struct iio_dev *indio_dev,
  31. const struct iio_chan_spec *chan,
  32. enum iio_event_type type,
  33. enum iio_event_direction dir)
  34. {
  35. struct iio_dummy_state *st = iio_priv(indio_dev);
  36. return st->event_en;
  37. }
  38. /**
  39. * iio_simple_dummy_write_event_config() - set whether event is enabled
  40. * @indio_dev: the device instance data
  41. * @chan: channel for the event whose state is being set
  42. * @type: type of the event whose state is being set
  43. * @dir: direction of the vent whose state is being set
  44. * @state: whether to enable or disable the device.
  45. *
  46. * This function would normally set the relevant registers on the devices
  47. * so that it generates the specified event. Here it just sets up a cached
  48. * value.
  49. */
  50. int iio_simple_dummy_write_event_config(struct iio_dev *indio_dev,
  51. const struct iio_chan_spec *chan,
  52. enum iio_event_type type,
  53. enum iio_event_direction dir,
  54. int state)
  55. {
  56. struct iio_dummy_state *st = iio_priv(indio_dev);
  57. /*
  58. * Deliberately over the top code splitting to illustrate
  59. * how this is done when multiple events exist.
  60. */
  61. switch (chan->type) {
  62. case IIO_VOLTAGE:
  63. switch (type) {
  64. case IIO_EV_TYPE_THRESH:
  65. if (dir == IIO_EV_DIR_RISING)
  66. st->event_en = state;
  67. else
  68. return -EINVAL;
  69. default:
  70. return -EINVAL;
  71. }
  72. break;
  73. case IIO_ACTIVITY:
  74. switch (type) {
  75. case IIO_EV_TYPE_THRESH:
  76. st->event_en = state;
  77. break;
  78. default:
  79. return -EINVAL;
  80. }
  81. break;
  82. case IIO_STEPS:
  83. switch (type) {
  84. case IIO_EV_TYPE_CHANGE:
  85. st->event_en = state;
  86. break;
  87. default:
  88. return -EINVAL;
  89. }
  90. break;
  91. default:
  92. return -EINVAL;
  93. }
  94. return 0;
  95. }
  96. /**
  97. * iio_simple_dummy_read_event_value() - get value associated with event
  98. * @indio_dev: device instance specific data
  99. * @chan: channel for the event whose value is being read
  100. * @type: type of the event whose value is being read
  101. * @dir: direction of the vent whose value is being read
  102. * @info: info type of the event whose value is being read
  103. * @val: value for the event code.
  104. *
  105. * Many devices provide a large set of events of which only a subset may
  106. * be enabled at a time, with value registers whose meaning changes depending
  107. * on the event enabled. This often means that the driver must cache the values
  108. * associated with each possible events so that the right value is in place when
  109. * the enabled event is changed.
  110. */
  111. int iio_simple_dummy_read_event_value(struct iio_dev *indio_dev,
  112. const struct iio_chan_spec *chan,
  113. enum iio_event_type type,
  114. enum iio_event_direction dir,
  115. enum iio_event_info info,
  116. int *val, int *val2)
  117. {
  118. struct iio_dummy_state *st = iio_priv(indio_dev);
  119. *val = st->event_val;
  120. return IIO_VAL_INT;
  121. }
  122. /**
  123. * iio_simple_dummy_write_event_value() - set value associate with event
  124. * @indio_dev: device instance specific data
  125. * @chan: channel for the event whose value is being set
  126. * @type: type of the event whose value is being set
  127. * @dir: direction of the vent whose value is being set
  128. * @info: info type of the event whose value is being set
  129. * @val: the value to be set.
  130. */
  131. int iio_simple_dummy_write_event_value(struct iio_dev *indio_dev,
  132. const struct iio_chan_spec *chan,
  133. enum iio_event_type type,
  134. enum iio_event_direction dir,
  135. enum iio_event_info info,
  136. int val, int val2)
  137. {
  138. struct iio_dummy_state *st = iio_priv(indio_dev);
  139. st->event_val = val;
  140. return 0;
  141. }
  142. static irqreturn_t iio_simple_dummy_get_timestamp(int irq, void *private)
  143. {
  144. struct iio_dev *indio_dev = private;
  145. struct iio_dummy_state *st = iio_priv(indio_dev);
  146. st->event_timestamp = iio_get_time_ns();
  147. return IRQ_WAKE_THREAD;
  148. }
  149. /**
  150. * iio_simple_dummy_event_handler() - identify and pass on event
  151. * @irq: irq of event line
  152. * @private: pointer to device instance state.
  153. *
  154. * This handler is responsible for querying the device to find out what
  155. * event occurred and for then pushing that event towards userspace.
  156. * Here only one event occurs so we push that directly on with locally
  157. * grabbed timestamp.
  158. */
  159. static irqreturn_t iio_simple_dummy_event_handler(int irq, void *private)
  160. {
  161. struct iio_dev *indio_dev = private;
  162. struct iio_dummy_state *st = iio_priv(indio_dev);
  163. dev_dbg(&indio_dev->dev, "id %x event %x\n",
  164. st->regs->reg_id, st->regs->reg_data);
  165. switch (st->regs->reg_data) {
  166. case 0:
  167. iio_push_event(indio_dev,
  168. IIO_EVENT_CODE(IIO_VOLTAGE, 0, 0,
  169. IIO_EV_DIR_RISING,
  170. IIO_EV_TYPE_THRESH, 0, 0, 0),
  171. st->event_timestamp);
  172. break;
  173. case 1:
  174. if (st->activity_running > st->event_val)
  175. iio_push_event(indio_dev,
  176. IIO_EVENT_CODE(IIO_ACTIVITY, 0,
  177. IIO_MOD_RUNNING,
  178. IIO_EV_DIR_RISING,
  179. IIO_EV_TYPE_THRESH,
  180. 0, 0, 0),
  181. st->event_timestamp);
  182. break;
  183. case 2:
  184. if (st->activity_walking < st->event_val)
  185. iio_push_event(indio_dev,
  186. IIO_EVENT_CODE(IIO_ACTIVITY, 0,
  187. IIO_MOD_WALKING,
  188. IIO_EV_DIR_FALLING,
  189. IIO_EV_TYPE_THRESH,
  190. 0, 0, 0),
  191. st->event_timestamp);
  192. break;
  193. case 3:
  194. iio_push_event(indio_dev,
  195. IIO_EVENT_CODE(IIO_STEPS, 0, IIO_NO_MOD,
  196. IIO_EV_DIR_NONE,
  197. IIO_EV_TYPE_CHANGE, 0, 0, 0),
  198. st->event_timestamp);
  199. break;
  200. default:
  201. break;
  202. }
  203. return IRQ_HANDLED;
  204. }
  205. /**
  206. * iio_simple_dummy_events_register() - setup interrupt handling for events
  207. * @indio_dev: device instance data
  208. *
  209. * This function requests the threaded interrupt to handle the events.
  210. * Normally the irq is a hardware interrupt and the number comes
  211. * from board configuration files. Here we get it from a companion
  212. * module that fakes the interrupt for us. Note that module in
  213. * no way forms part of this example. Just assume that events magically
  214. * appear via the provided interrupt.
  215. */
  216. int iio_simple_dummy_events_register(struct iio_dev *indio_dev)
  217. {
  218. struct iio_dummy_state *st = iio_priv(indio_dev);
  219. int ret;
  220. /* Fire up event source - normally not present */
  221. st->event_irq = iio_dummy_evgen_get_irq();
  222. if (st->event_irq < 0) {
  223. ret = st->event_irq;
  224. goto error_ret;
  225. }
  226. st->regs = iio_dummy_evgen_get_regs(st->event_irq);
  227. ret = request_threaded_irq(st->event_irq,
  228. &iio_simple_dummy_get_timestamp,
  229. &iio_simple_dummy_event_handler,
  230. IRQF_ONESHOT,
  231. "iio_simple_event",
  232. indio_dev);
  233. if (ret < 0)
  234. goto error_free_evgen;
  235. return 0;
  236. error_free_evgen:
  237. iio_dummy_evgen_release_irq(st->event_irq);
  238. error_ret:
  239. return ret;
  240. }
  241. /**
  242. * iio_simple_dummy_events_unregister() - tidy up interrupt handling on remove
  243. * @indio_dev: device instance data
  244. */
  245. void iio_simple_dummy_events_unregister(struct iio_dev *indio_dev)
  246. {
  247. struct iio_dummy_state *st = iio_priv(indio_dev);
  248. free_irq(st->event_irq, indio_dev);
  249. /* Not part of normal driver */
  250. iio_dummy_evgen_release_irq(st->event_irq);
  251. }