1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b9d453a5SGwendal Grignou /*
3ac5006a2SDaniel Baluta * The industrial I/O periodic hrtimer trigger driver
4ac5006a2SDaniel Baluta *
5ac5006a2SDaniel Baluta * Copyright (C) Intuitive Aerial AB
6ac5006a2SDaniel Baluta * Written by Marten Svanfeldt, marten@intuitiveaerial.com
78cba4a99SGeert Uytterhoeven * Copyright (C) 2012, Analog Devices Inc.
8ac5006a2SDaniel Baluta * Author: Lars-Peter Clausen <lars@metafoo.de>
9ac5006a2SDaniel Baluta * Copyright (C) 2015, Intel Corporation
10ac5006a2SDaniel Baluta */
11ac5006a2SDaniel Baluta #include <linux/kernel.h>
12ac5006a2SDaniel Baluta #include <linux/slab.h>
13ac5006a2SDaniel Baluta #include <linux/hrtimer.h>
14ac5006a2SDaniel Baluta
15ac5006a2SDaniel Baluta #include <linux/iio/iio.h>
16ac5006a2SDaniel Baluta #include <linux/iio/trigger.h>
17ac5006a2SDaniel Baluta #include <linux/iio/sw_trigger.h>
18ac5006a2SDaniel Baluta
19dafcf4edSGwendal Grignou /* Defined locally, not in time64.h yet. */
20dafcf4edSGwendal Grignou #define PSEC_PER_SEC 1000000000000LL
21dafcf4edSGwendal Grignou
22ac5006a2SDaniel Baluta /* default sampling frequency - 100Hz */
23ac5006a2SDaniel Baluta #define HRTIMER_DEFAULT_SAMPLING_FREQUENCY 100
24ac5006a2SDaniel Baluta
25ac5006a2SDaniel Baluta struct iio_hrtimer_info {
26ac5006a2SDaniel Baluta struct iio_sw_trigger swt;
27ac5006a2SDaniel Baluta struct hrtimer timer;
28dafcf4edSGwendal Grignou int sampling_frequency[2];
29ac5006a2SDaniel Baluta ktime_t period;
30ac5006a2SDaniel Baluta };
31ac5006a2SDaniel Baluta
32612a462aSBhumika Goyal static const struct config_item_type iio_hrtimer_type = {
33ac5006a2SDaniel Baluta .ct_owner = THIS_MODULE,
34ac5006a2SDaniel Baluta };
35ac5006a2SDaniel Baluta
36ac5006a2SDaniel Baluta static
iio_hrtimer_show_sampling_frequency(struct device * dev,struct device_attribute * attr,char * buf)37ac5006a2SDaniel Baluta ssize_t iio_hrtimer_show_sampling_frequency(struct device *dev,
38ac5006a2SDaniel Baluta struct device_attribute *attr,
39ac5006a2SDaniel Baluta char *buf)
40ac5006a2SDaniel Baluta {
41ac5006a2SDaniel Baluta struct iio_trigger *trig = to_iio_trigger(dev);
42ac5006a2SDaniel Baluta struct iio_hrtimer_info *info = iio_trigger_get_drvdata(trig);
43ac5006a2SDaniel Baluta
44dafcf4edSGwendal Grignou return iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO,
45dafcf4edSGwendal Grignou ARRAY_SIZE(info->sampling_frequency),
46dafcf4edSGwendal Grignou info->sampling_frequency);
47ac5006a2SDaniel Baluta }
48ac5006a2SDaniel Baluta
49ac5006a2SDaniel Baluta static
iio_hrtimer_store_sampling_frequency(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)50ac5006a2SDaniel Baluta ssize_t iio_hrtimer_store_sampling_frequency(struct device *dev,
51ac5006a2SDaniel Baluta struct device_attribute *attr,
52ac5006a2SDaniel Baluta const char *buf, size_t len)
53ac5006a2SDaniel Baluta {
54ac5006a2SDaniel Baluta struct iio_trigger *trig = to_iio_trigger(dev);
55ac5006a2SDaniel Baluta struct iio_hrtimer_info *info = iio_trigger_get_drvdata(trig);
56dafcf4edSGwendal Grignou unsigned long long val;
57dafcf4edSGwendal Grignou u64 period;
58dafcf4edSGwendal Grignou int integer, fract, ret;
59ac5006a2SDaniel Baluta
60dafcf4edSGwendal Grignou ret = iio_str_to_fixpoint(buf, 100, &integer, &fract);
61ac5006a2SDaniel Baluta if (ret)
62ac5006a2SDaniel Baluta return ret;
63dafcf4edSGwendal Grignou if (integer < 0 || fract < 0)
64dafcf4edSGwendal Grignou return -ERANGE;
65ac5006a2SDaniel Baluta
66*c10f8109SGustavo A. R. Silva val = fract + 1000ULL * integer; /* mHz */
67dafcf4edSGwendal Grignou
68dafcf4edSGwendal Grignou if (!val || val > UINT_MAX)
69ac5006a2SDaniel Baluta return -EINVAL;
70ac5006a2SDaniel Baluta
71dafcf4edSGwendal Grignou info->sampling_frequency[0] = integer; /* Hz */
72dafcf4edSGwendal Grignou info->sampling_frequency[1] = fract * 1000; /* uHz */
73dafcf4edSGwendal Grignou period = PSEC_PER_SEC;
74dafcf4edSGwendal Grignou do_div(period, val);
75dafcf4edSGwendal Grignou info->period = period; /* nS */
76ac5006a2SDaniel Baluta
77ac5006a2SDaniel Baluta return len;
78ac5006a2SDaniel Baluta }
79ac5006a2SDaniel Baluta
80ac5006a2SDaniel Baluta static DEVICE_ATTR(sampling_frequency, S_IRUGO | S_IWUSR,
81ac5006a2SDaniel Baluta iio_hrtimer_show_sampling_frequency,
82ac5006a2SDaniel Baluta iio_hrtimer_store_sampling_frequency);
83ac5006a2SDaniel Baluta
84ac5006a2SDaniel Baluta static struct attribute *iio_hrtimer_attrs[] = {
85ac5006a2SDaniel Baluta &dev_attr_sampling_frequency.attr,
86ac5006a2SDaniel Baluta NULL
87ac5006a2SDaniel Baluta };
88ac5006a2SDaniel Baluta
89ac5006a2SDaniel Baluta static const struct attribute_group iio_hrtimer_attr_group = {
90ac5006a2SDaniel Baluta .attrs = iio_hrtimer_attrs,
91ac5006a2SDaniel Baluta };
92ac5006a2SDaniel Baluta
93ac5006a2SDaniel Baluta static const struct attribute_group *iio_hrtimer_attr_groups[] = {
94ac5006a2SDaniel Baluta &iio_hrtimer_attr_group,
95ac5006a2SDaniel Baluta NULL
96ac5006a2SDaniel Baluta };
97ac5006a2SDaniel Baluta
iio_hrtimer_trig_handler(struct hrtimer * timer)98ac5006a2SDaniel Baluta static enum hrtimer_restart iio_hrtimer_trig_handler(struct hrtimer *timer)
99ac5006a2SDaniel Baluta {
100ac5006a2SDaniel Baluta struct iio_hrtimer_info *info;
101ac5006a2SDaniel Baluta
102ac5006a2SDaniel Baluta info = container_of(timer, struct iio_hrtimer_info, timer);
103ac5006a2SDaniel Baluta
104ac5006a2SDaniel Baluta hrtimer_forward_now(timer, info->period);
105ac5006a2SDaniel Baluta iio_trigger_poll(info->swt.trigger);
106ac5006a2SDaniel Baluta
107ac5006a2SDaniel Baluta return HRTIMER_RESTART;
108ac5006a2SDaniel Baluta }
109ac5006a2SDaniel Baluta
iio_trig_hrtimer_set_state(struct iio_trigger * trig,bool state)110ac5006a2SDaniel Baluta static int iio_trig_hrtimer_set_state(struct iio_trigger *trig, bool state)
111ac5006a2SDaniel Baluta {
112ac5006a2SDaniel Baluta struct iio_hrtimer_info *trig_info;
113ac5006a2SDaniel Baluta
114ac5006a2SDaniel Baluta trig_info = iio_trigger_get_drvdata(trig);
115ac5006a2SDaniel Baluta
116ac5006a2SDaniel Baluta if (state)
117ac5006a2SDaniel Baluta hrtimer_start(&trig_info->timer, trig_info->period,
1180178297cSLars-Peter Clausen HRTIMER_MODE_REL_HARD);
119ac5006a2SDaniel Baluta else
120ac5006a2SDaniel Baluta hrtimer_cancel(&trig_info->timer);
121ac5006a2SDaniel Baluta
122ac5006a2SDaniel Baluta return 0;
123ac5006a2SDaniel Baluta }
124ac5006a2SDaniel Baluta
125ac5006a2SDaniel Baluta static const struct iio_trigger_ops iio_hrtimer_trigger_ops = {
126ac5006a2SDaniel Baluta .set_trigger_state = iio_trig_hrtimer_set_state,
127ac5006a2SDaniel Baluta };
128ac5006a2SDaniel Baluta
iio_trig_hrtimer_probe(const char * name)129ac5006a2SDaniel Baluta static struct iio_sw_trigger *iio_trig_hrtimer_probe(const char *name)
130ac5006a2SDaniel Baluta {
131ac5006a2SDaniel Baluta struct iio_hrtimer_info *trig_info;
132ac5006a2SDaniel Baluta int ret;
133ac5006a2SDaniel Baluta
134ac5006a2SDaniel Baluta trig_info = kzalloc(sizeof(*trig_info), GFP_KERNEL);
135ac5006a2SDaniel Baluta if (!trig_info)
136ac5006a2SDaniel Baluta return ERR_PTR(-ENOMEM);
137ac5006a2SDaniel Baluta
138995071d3SGwendal Grignou trig_info->swt.trigger = iio_trigger_alloc(NULL, "%s", name);
139ac5006a2SDaniel Baluta if (!trig_info->swt.trigger) {
140ac5006a2SDaniel Baluta ret = -ENOMEM;
141ac5006a2SDaniel Baluta goto err_free_trig_info;
142ac5006a2SDaniel Baluta }
143ac5006a2SDaniel Baluta
144ac5006a2SDaniel Baluta iio_trigger_set_drvdata(trig_info->swt.trigger, trig_info);
145ac5006a2SDaniel Baluta trig_info->swt.trigger->ops = &iio_hrtimer_trigger_ops;
146ac5006a2SDaniel Baluta trig_info->swt.trigger->dev.groups = iio_hrtimer_attr_groups;
147ac5006a2SDaniel Baluta
1480178297cSLars-Peter Clausen hrtimer_init(&trig_info->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
149ac5006a2SDaniel Baluta trig_info->timer.function = iio_hrtimer_trig_handler;
150ac5006a2SDaniel Baluta
151dafcf4edSGwendal Grignou trig_info->sampling_frequency[0] = HRTIMER_DEFAULT_SAMPLING_FREQUENCY;
152dafcf4edSGwendal Grignou trig_info->period = NSEC_PER_SEC / trig_info->sampling_frequency[0];
153ac5006a2SDaniel Baluta
154ac5006a2SDaniel Baluta ret = iio_trigger_register(trig_info->swt.trigger);
155ac5006a2SDaniel Baluta if (ret)
156ac5006a2SDaniel Baluta goto err_free_trigger;
157ac5006a2SDaniel Baluta
158ac5006a2SDaniel Baluta iio_swt_group_init_type_name(&trig_info->swt, name, &iio_hrtimer_type);
159ac5006a2SDaniel Baluta return &trig_info->swt;
160ac5006a2SDaniel Baluta err_free_trigger:
161ac5006a2SDaniel Baluta iio_trigger_free(trig_info->swt.trigger);
162ac5006a2SDaniel Baluta err_free_trig_info:
163ac5006a2SDaniel Baluta kfree(trig_info);
164ac5006a2SDaniel Baluta
165ac5006a2SDaniel Baluta return ERR_PTR(ret);
166ac5006a2SDaniel Baluta }
167ac5006a2SDaniel Baluta
iio_trig_hrtimer_remove(struct iio_sw_trigger * swt)168ac5006a2SDaniel Baluta static int iio_trig_hrtimer_remove(struct iio_sw_trigger *swt)
169ac5006a2SDaniel Baluta {
170ac5006a2SDaniel Baluta struct iio_hrtimer_info *trig_info;
171ac5006a2SDaniel Baluta
172ac5006a2SDaniel Baluta trig_info = iio_trigger_get_drvdata(swt->trigger);
173ac5006a2SDaniel Baluta
174ac5006a2SDaniel Baluta iio_trigger_unregister(swt->trigger);
175ac5006a2SDaniel Baluta
176ac5006a2SDaniel Baluta /* cancel the timer after unreg to make sure no one rearms it */
177ac5006a2SDaniel Baluta hrtimer_cancel(&trig_info->timer);
178ac5006a2SDaniel Baluta iio_trigger_free(swt->trigger);
179ac5006a2SDaniel Baluta kfree(trig_info);
180ac5006a2SDaniel Baluta
181ac5006a2SDaniel Baluta return 0;
182ac5006a2SDaniel Baluta }
183ac5006a2SDaniel Baluta
184ac5006a2SDaniel Baluta static const struct iio_sw_trigger_ops iio_trig_hrtimer_ops = {
185ac5006a2SDaniel Baluta .probe = iio_trig_hrtimer_probe,
186ac5006a2SDaniel Baluta .remove = iio_trig_hrtimer_remove,
187ac5006a2SDaniel Baluta };
188ac5006a2SDaniel Baluta
189ac5006a2SDaniel Baluta static struct iio_sw_trigger_type iio_trig_hrtimer = {
190ac5006a2SDaniel Baluta .name = "hrtimer",
191ac5006a2SDaniel Baluta .owner = THIS_MODULE,
192ac5006a2SDaniel Baluta .ops = &iio_trig_hrtimer_ops,
193ac5006a2SDaniel Baluta };
194ac5006a2SDaniel Baluta
195ac5006a2SDaniel Baluta module_iio_sw_trigger_driver(iio_trig_hrtimer);
196ac5006a2SDaniel Baluta
197ac5006a2SDaniel Baluta MODULE_AUTHOR("Marten Svanfeldt <marten@intuitiveaerial.com>");
198ac5006a2SDaniel Baluta MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
199ac5006a2SDaniel Baluta MODULE_DESCRIPTION("Periodic hrtimer trigger for the IIO subsystem");
200ac5006a2SDaniel Baluta MODULE_LICENSE("GPL v2");
201