iio-trig-loop.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright 2016 Jonathan Cameron <jic23@kernel.org>
  3. *
  4. * Licensed under the GPL-2.
  5. *
  6. * Based on a mashup of the hrtimer trigger and continuous sampling proposal of
  7. * Gregor Boirie <gregor.boirie@parrot.com>
  8. *
  9. * Note this is still rather experimental and may eat babies.
  10. *
  11. * Todo
  12. * * Protect against connection of devices that 'need' the top half
  13. * handler.
  14. * * Work out how to run top half handlers in this context if it is
  15. * safe to do so (timestamp grabbing for example)
  16. *
  17. * Tested against a max1363. Used about 33% cpu for the thread and 20%
  18. * for generic_buffer piping to /dev/null. Watermark set at 64 on a 128
  19. * element kfifo buffer.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/slab.h>
  25. #include <linux/irq_work.h>
  26. #include <linux/kthread.h>
  27. #include <linux/freezer.h>
  28. #include <linux/iio/iio.h>
  29. #include <linux/iio/trigger.h>
  30. #include <linux/iio/sw_trigger.h>
  31. struct iio_loop_info {
  32. struct iio_sw_trigger swt;
  33. struct task_struct *task;
  34. };
  35. static const struct config_item_type iio_loop_type = {
  36. .ct_owner = THIS_MODULE,
  37. };
  38. static int iio_loop_thread(void *data)
  39. {
  40. struct iio_trigger *trig = data;
  41. set_freezable();
  42. do {
  43. iio_trigger_poll_chained(trig);
  44. } while (likely(!kthread_freezable_should_stop(NULL)));
  45. return 0;
  46. }
  47. static int iio_loop_trigger_set_state(struct iio_trigger *trig, bool state)
  48. {
  49. struct iio_loop_info *loop_trig = iio_trigger_get_drvdata(trig);
  50. if (state) {
  51. loop_trig->task = kthread_run(iio_loop_thread,
  52. trig, trig->name);
  53. if (unlikely(IS_ERR(loop_trig->task))) {
  54. dev_err(&trig->dev,
  55. "failed to create trigger loop thread\n");
  56. return PTR_ERR(loop_trig->task);
  57. }
  58. } else {
  59. kthread_stop(loop_trig->task);
  60. }
  61. return 0;
  62. }
  63. static const struct iio_trigger_ops iio_loop_trigger_ops = {
  64. .set_trigger_state = iio_loop_trigger_set_state,
  65. };
  66. static struct iio_sw_trigger *iio_trig_loop_probe(const char *name)
  67. {
  68. struct iio_loop_info *trig_info;
  69. int ret;
  70. trig_info = kzalloc(sizeof(*trig_info), GFP_KERNEL);
  71. if (!trig_info)
  72. return ERR_PTR(-ENOMEM);
  73. trig_info->swt.trigger = iio_trigger_alloc("%s", name);
  74. if (!trig_info->swt.trigger) {
  75. ret = -ENOMEM;
  76. goto err_free_trig_info;
  77. }
  78. iio_trigger_set_drvdata(trig_info->swt.trigger, trig_info);
  79. trig_info->swt.trigger->ops = &iio_loop_trigger_ops;
  80. ret = iio_trigger_register(trig_info->swt.trigger);
  81. if (ret)
  82. goto err_free_trigger;
  83. iio_swt_group_init_type_name(&trig_info->swt, name, &iio_loop_type);
  84. return &trig_info->swt;
  85. err_free_trigger:
  86. iio_trigger_free(trig_info->swt.trigger);
  87. err_free_trig_info:
  88. kfree(trig_info);
  89. return ERR_PTR(ret);
  90. }
  91. static int iio_trig_loop_remove(struct iio_sw_trigger *swt)
  92. {
  93. struct iio_loop_info *trig_info;
  94. trig_info = iio_trigger_get_drvdata(swt->trigger);
  95. iio_trigger_unregister(swt->trigger);
  96. iio_trigger_free(swt->trigger);
  97. kfree(trig_info);
  98. return 0;
  99. }
  100. static const struct iio_sw_trigger_ops iio_trig_loop_ops = {
  101. .probe = iio_trig_loop_probe,
  102. .remove = iio_trig_loop_remove,
  103. };
  104. static struct iio_sw_trigger_type iio_trig_loop = {
  105. .name = "loop",
  106. .owner = THIS_MODULE,
  107. .ops = &iio_trig_loop_ops,
  108. };
  109. module_iio_sw_trigger_driver(iio_trig_loop);
  110. MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
  111. MODULE_DESCRIPTION("Loop based trigger for the iio subsystem");
  112. MODULE_LICENSE("GPL v2");
  113. MODULE_ALIAS("platform:iio-trig-loop");