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