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 
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
26 #include <linux/irq_work.h>
27 #include <linux/kthread.h>
28 #include <linux/freezer.h>
29 
30 #include <linux/iio/iio.h>
31 #include <linux/iio/trigger.h>
32 #include <linux/iio/sw_trigger.h>
33 
34 struct iio_loop_info {
35 	struct iio_sw_trigger swt;
36 	struct task_struct *task;
37 };
38 
39 static const struct config_item_type iio_loop_type = {
40 	.ct_owner = THIS_MODULE,
41 };
42 
43 static int iio_loop_thread(void *data)
44 {
45 	struct iio_trigger *trig = data;
46 
47 	set_freezable();
48 
49 	do {
50 		iio_trigger_poll_chained(trig);
51 	} while (likely(!kthread_freezable_should_stop(NULL)));
52 
53 	return 0;
54 }
55 
56 static int iio_loop_trigger_set_state(struct iio_trigger *trig, bool state)
57 {
58 	struct iio_loop_info *loop_trig = iio_trigger_get_drvdata(trig);
59 
60 	if (state) {
61 		loop_trig->task = kthread_run(iio_loop_thread,
62 					      trig, trig->name);
63 		if (unlikely(IS_ERR(loop_trig->task))) {
64 			dev_err(&trig->dev,
65 				"failed to create trigger loop thread\n");
66 			return PTR_ERR(loop_trig->task);
67 		}
68 	} else {
69 		kthread_stop(loop_trig->task);
70 	}
71 
72 	return 0;
73 }
74 
75 static const struct iio_trigger_ops iio_loop_trigger_ops = {
76 	.set_trigger_state = iio_loop_trigger_set_state,
77 };
78 
79 static struct iio_sw_trigger *iio_trig_loop_probe(const char *name)
80 {
81 	struct iio_loop_info *trig_info;
82 	int ret;
83 
84 	trig_info = kzalloc(sizeof(*trig_info), GFP_KERNEL);
85 	if (!trig_info)
86 		return ERR_PTR(-ENOMEM);
87 
88 	trig_info->swt.trigger = iio_trigger_alloc("%s", name);
89 	if (!trig_info->swt.trigger) {
90 		ret = -ENOMEM;
91 		goto err_free_trig_info;
92 	}
93 
94 	iio_trigger_set_drvdata(trig_info->swt.trigger, trig_info);
95 	trig_info->swt.trigger->ops = &iio_loop_trigger_ops;
96 
97 	ret = iio_trigger_register(trig_info->swt.trigger);
98 	if (ret)
99 		goto err_free_trigger;
100 
101 	iio_swt_group_init_type_name(&trig_info->swt, name, &iio_loop_type);
102 
103 	return &trig_info->swt;
104 
105 err_free_trigger:
106 	iio_trigger_free(trig_info->swt.trigger);
107 err_free_trig_info:
108 	kfree(trig_info);
109 
110 	return ERR_PTR(ret);
111 }
112 
113 static int iio_trig_loop_remove(struct iio_sw_trigger *swt)
114 {
115 	struct iio_loop_info *trig_info;
116 
117 	trig_info = iio_trigger_get_drvdata(swt->trigger);
118 
119 	iio_trigger_unregister(swt->trigger);
120 	iio_trigger_free(swt->trigger);
121 	kfree(trig_info);
122 
123 	return 0;
124 }
125 
126 static const struct iio_sw_trigger_ops iio_trig_loop_ops = {
127 	.probe = iio_trig_loop_probe,
128 	.remove = iio_trig_loop_remove,
129 };
130 
131 static struct iio_sw_trigger_type iio_trig_loop = {
132 	.name = "loop",
133 	.owner = THIS_MODULE,
134 	.ops = &iio_trig_loop_ops,
135 };
136 
137 module_iio_sw_trigger_driver(iio_trig_loop);
138 
139 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
140 MODULE_DESCRIPTION("Loop based trigger for the iio subsystem");
141 MODULE_LICENSE("GPL v2");
142 MODULE_ALIAS("platform:iio-trig-loop");
143