iio-trig-loop.c 3.3 KB

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