forked from twteamware/raspberrypi-ptp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0001-pps-add-gpio-PPS-signal-generator.patch
355 lines (348 loc) · 10.8 KB
/
0001-pps-add-gpio-PPS-signal-generator.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
From 4f9ccbb00a2703b661aacbe963a7677b9988715d Mon Sep 17 00:00:00 2001
From: Luigi Mastandrea <[email protected]>
Date: Wed, 7 Jun 2017 11:11:24 +0200
Subject: [PATCH 1/2] pps: add gpio PPS signal generator
Add PPS signal generator which utilizes a GPIO pin to send PPS signals.
It relies GPIO framework and hrtimers to control the signal.
GPIO pin can be configured via DT.
Signed-off-by: Andrea Galbusera <[email protected]>
---
.../devicetree/bindings/pps/pps-gen-gpio.txt | 19 ++
drivers/pps/generators/Kconfig | 7 +
drivers/pps/generators/Makefile | 1 +
drivers/pps/generators/pps_gen_gpio.c | 274 +++++++++++++++++++++
4 files changed, 301 insertions(+)
create mode 100644 Documentation/devicetree/bindings/pps/pps-gen-gpio.txt
create mode 100644 drivers/pps/generators/pps_gen_gpio.c
diff --git a/Documentation/devicetree/bindings/pps/pps-gen-gpio.txt b/Documentation/devicetree/bindings/pps/pps-gen-gpio.txt
new file mode 100644
index 00000000..5f3c83df
--- /dev/null
+++ b/Documentation/devicetree/bindings/pps/pps-gen-gpio.txt
@@ -0,0 +1,19 @@
+Device-Tree Bindings for a PPS generated signal on GPIO
+
+These properties describe a PPS (pulse-per-second) signal output
+through a GPIO pin.
+
+Required properties:
+- pps-gen-gpios: one PPS GPIO in the format described by ../gpio/gpio.txt
+
+Optional properties:
+- assert-falling-edge: when present, assert is indicated by a falling edge
+ (instead of by a rising edge)
+
+Example:
+ pps-gen {
+ compatible = "pps-generator-gpios";
+ pps-gen-gpios = <&gpio2 6 0>;
+
+ assert-falling-edge;
+ };
diff --git a/drivers/pps/generators/Kconfig b/drivers/pps/generators/Kconfig
index e4c4f3dc..7b463f50 100644
--- a/drivers/pps/generators/Kconfig
+++ b/drivers/pps/generators/Kconfig
@@ -11,3 +11,10 @@ config PPS_GENERATOR_PARPORT
If you say yes here you get support for a PPS signal generator which
utilizes STROBE pin of a parallel port to send PPS signals. It uses
parport abstraction layer and hrtimers to precisely control the signal.
+
+config PPS_GENERATOR_GPIO
+ tristate "GPIO PPS signal generator"
+ help
+ If you say yes here you get support for a PPS signal generator which
+ utilizes a GPIO pin specified in the Device Tree. It uses hrtimers to
+ precisely control the signal.
diff --git a/drivers/pps/generators/Makefile b/drivers/pps/generators/Makefile
index 303304a6..77ab1255 100644
--- a/drivers/pps/generators/Makefile
+++ b/drivers/pps/generators/Makefile
@@ -3,6 +3,7 @@
#
obj-$(CONFIG_PPS_GENERATOR_PARPORT) += pps_gen_parport.o
+obj-$(CONFIG_PPS_GENERATOR_GPIO) += pps_gen_gpio.o
ifeq ($(CONFIG_PPS_DEBUG),y)
EXTRA_CFLAGS += -DDEBUG
diff --git a/drivers/pps/generators/pps_gen_gpio.c b/drivers/pps/generators/pps_gen_gpio.c
new file mode 100644
index 00000000..a1fb7424
--- /dev/null
+++ b/drivers/pps/generators/pps_gen_gpio.c
@@ -0,0 +1,274 @@
+/*
+ * pps_gen_gpio.c -- kernel GPIO PPS signal generator
+ *
+ *
+ * Copyright (C) 2015 Juan Solano <[email protected]>
+ * 2009 Alexander Gordeev <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+/*
+ * TODO:
+ * fix issues when realtime clock is adjusted in a leap
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/platform_device.h>
+#include <linux/time.h>
+#include <linux/hrtimer.h>
+#include <linux/gpio.h>
+#include <linux/of_gpio.h>
+
+#define DRVDESC "GPIO PPS signal generator"
+#define SEND_DELAY_MAX 100000
+#define SAFETY_INTERVAL 10000 /* set the hrtimer earlier for safety (ns) */
+
+/* module parameters */
+static unsigned int send_delay = 30000;
+MODULE_PARM_DESC(delay, "Delay between setting and dropping the signal (ns)");
+module_param_named(delay, send_delay, uint, 0);
+
+/* device specific private data structure */
+struct pps_gen_gpio_devdata {
+ struct gpio_desc *pps_gpio; /* GPIO port descriptor */
+ struct hrtimer timer;
+ long port_write_time; /* calibrated port write time (ns) */
+};
+
+/* calibrated time between a hrtimer event and the reaction */
+static long hrtimer_error = SAFETY_INTERVAL;
+
+/* the kernel hrtimer event */
+static enum hrtimer_restart hrtimer_event(struct hrtimer *timer)
+{
+ struct timespec expire_time, ts1, ts2, ts3, dts;
+ struct pps_gen_gpio_devdata *devdata;
+ long lim, delta;
+ unsigned long flags;
+
+ /* We have to disable interrupts here. The idea is to prevent
+ * other interrupts on the same processor to introduce random
+ * lags while polling the clock. getnstimeofday() takes <1us on
+ * most machines while other interrupt handlers can take much
+ * more potentially.
+ *
+ * NB: approx time with blocked interrupts =
+ * send_delay + 3 * SAFETY_INTERVAL
+ */
+ local_irq_save(flags);
+
+ /* first of all we get the time stamp... */
+ getnstimeofday(&ts1);
+ expire_time = ktime_to_timespec(hrtimer_get_softexpires(timer));
+ devdata = container_of(timer, struct pps_gen_gpio_devdata, timer);
+ lim = NSEC_PER_SEC - send_delay - devdata->port_write_time;
+
+ /* check if we are late */
+ if (expire_time.tv_sec != ts1.tv_sec || ts1.tv_nsec > lim) {
+ local_irq_restore(flags);
+ pr_err("we are late this time %ld.%09ld\n",
+ ts1.tv_sec, ts1.tv_nsec);
+ goto done;
+ }
+
+ /* busy loop until the time is right for an assert edge */
+ do {
+ getnstimeofday(&ts2);
+ } while (expire_time.tv_sec == ts2.tv_sec && ts2.tv_nsec < lim);
+
+ /* set the signal */
+ gpiod_set_value(devdata->pps_gpio, 1);
+
+ /* busy loop until the time is right for a clear edge */
+ lim = NSEC_PER_SEC - devdata->port_write_time;
+ do {
+ getnstimeofday(&ts2);
+ } while (expire_time.tv_sec == ts2.tv_sec && ts2.tv_nsec < lim);
+
+ /* unset the signal */
+ gpiod_set_value(devdata->pps_gpio, 0);
+
+ getnstimeofday(&ts3);
+
+ local_irq_restore(flags);
+
+ /* update calibrated port write time */
+ dts = timespec_sub(ts3, ts2);
+ devdata->port_write_time =
+ (devdata->port_write_time + timespec_to_ns(&dts)) >> 1;
+
+done:
+ /* update calibrated hrtimer error */
+ dts = timespec_sub(ts1, expire_time);
+ delta = timespec_to_ns(&dts);
+
+ /* If the new error value is bigger then the old, use the new
+ * value, if not then slowly move towards the new value. This
+ * way it should be safe in bad conditions and efficient in
+ * good conditions.
+ */
+ if (delta >= hrtimer_error)
+ hrtimer_error = delta;
+ else
+ hrtimer_error = (3 * hrtimer_error + delta) >> 2;
+
+ /* update the hrtimer expire time */
+ hrtimer_set_expires(timer,
+ ktime_set(expire_time.tv_sec + 1,
+ NSEC_PER_SEC - (send_delay +
+ devdata->port_write_time +
+ SAFETY_INTERVAL +
+ hrtimer_error)));
+
+ return HRTIMER_RESTART;
+}
+
+/* calibrate port write time */
+#define PORT_NTESTS_SHIFT 5
+static void calibrate_port(struct pps_gen_gpio_devdata *devdata)
+{
+ int i;
+ long acc = 0;
+
+ for (i = 0; i < (1 << PORT_NTESTS_SHIFT); i++) {
+ struct timespec a, b;
+ unsigned long irq_flags;
+
+ local_irq_save(irq_flags);
+ getnstimeofday(&a);
+ gpiod_set_value(devdata->pps_gpio, 0);
+ getnstimeofday(&b);
+ local_irq_restore(irq_flags);
+
+ b = timespec_sub(b, a);
+ acc += timespec_to_ns(&b);
+ }
+
+ devdata->port_write_time = acc >> PORT_NTESTS_SHIFT;
+ pr_info("port write takes %ldns\n", devdata->port_write_time);
+}
+
+static inline ktime_t next_intr_time(struct pps_gen_gpio_devdata *devdata)
+{
+ struct timespec ts;
+
+ getnstimeofday(&ts);
+ return ktime_set(ts.tv_sec +
+ ((ts.tv_nsec > 990 * NSEC_PER_MSEC) ? 1 : 0),
+ NSEC_PER_SEC - (send_delay +
+ devdata->port_write_time + 3 * SAFETY_INTERVAL));
+}
+
+static int pps_gen_gpio_probe(struct platform_device *pdev)
+{
+ int ret;
+ struct device *dev = &pdev->dev;
+ struct pps_gen_gpio_devdata *devdata;
+ int num_gpios;
+
+ /* get number of gpios defined in property pps-gen-gpios of DT node
+ * pdev->name */
+ num_gpios = of_gpio_named_count(dev->of_node, "pps-gen-gpios");
+ if (num_gpios < 1) {
+ dev_err(dev,
+ "cannot find a corresponding GPIO defined in DT [%d]\n",
+ num_gpios);
+ return -EINVAL;
+ } else {
+ pr_info("found %d GPIOS defined in DT\n", num_gpios);
+ }
+
+ /* allocate space for device info */
+ devdata = devm_kzalloc(dev, sizeof(struct pps_gen_gpio_devdata),
+ GFP_KERNEL);
+ if (!devdata)
+ return -ENOMEM;
+
+ /* pps-gen is the function associated with gpio list pps-gen-gpios */
+ devdata->pps_gpio = devm_gpiod_get(dev, "pps-gen", GPIOD_OUT_LOW);
+ if (IS_ERR(devdata->pps_gpio)) {
+ dev_err(dev, "cannot get PPS GPIO %ld\n",
+ PTR_ERR(devdata->pps_gpio));
+ return PTR_ERR(devdata->pps_gpio);
+ }
+
+ platform_set_drvdata(pdev, devdata);
+
+ ret = gpiod_direction_output(devdata->pps_gpio, 1);
+ if (ret < 0) {
+ dev_err(dev, "cannot configure PPS GPIO\n");
+ return ret;
+ }
+
+ calibrate_port(devdata);
+
+ hrtimer_init(&devdata->timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
+ devdata->timer.function = hrtimer_event;
+ hrtimer_start(&devdata->timer, next_intr_time(devdata),
+ HRTIMER_MODE_ABS);
+ return 0;
+}
+
+static int pps_gen_gpio_remove(struct platform_device *pdev)
+{
+ struct pps_gen_gpio_devdata *devdata = platform_get_drvdata(pdev);
+ hrtimer_cancel(&devdata->timer);
+ return 0;
+}
+
+/* the compatible property here defined is searched for in DT, and
+ * when a match is found, the corresponding DT node name is passed
+ * backed in pdev->name */
+static const struct of_device_id pps_gen_gpio_dt_ids[] = {
+ { .compatible = "pps-gen-gpios", },
+ { /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, pps_gen_gpio_dt_ids);
+
+static struct platform_driver pps_gen_gpio_driver = {
+ .driver = {
+ .name = "pps_gen_gpio", /* not used to match device */
+ .owner = THIS_MODULE,
+ .of_match_table = of_match_ptr(pps_gen_gpio_dt_ids),
+ },
+ .probe = pps_gen_gpio_probe,
+ .remove = pps_gen_gpio_remove,
+};
+
+static int __init pps_gen_gpio_init(void)
+{
+ pr_info(DRVDESC "\n");
+ if (send_delay > SEND_DELAY_MAX) {
+ pr_err("delay value should be not greater than %d\n",
+ SEND_DELAY_MAX);
+ return -EINVAL;
+ }
+ platform_driver_register(&pps_gen_gpio_driver);
+ return 0;
+}
+
+static void __exit pps_gen_gpio_exit(void)
+{
+ pr_info("hrtimer avg error is %ldns\n", hrtimer_error);
+ platform_driver_unregister(&pps_gen_gpio_driver);
+}
+
+module_init(pps_gen_gpio_init);
+module_exit(pps_gen_gpio_exit);
+
+MODULE_AUTHOR("Juan Solano <[email protected]>");
+MODULE_DESCRIPTION(DRVDESC);
+MODULE_LICENSE("GPL");
--
2.11.0