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