forked from ddrown/pps-gmtimer
-
Notifications
You must be signed in to change notification settings - Fork 3
/
pps-gmtimer.c
519 lines (419 loc) · 15.4 KB
/
pps-gmtimer.c
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*
* pps-gmtimer.c -- PPS client driver using OMAP Timers
*
* Copyright (C) 2014 Daniel Drown <[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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#define MODULE_NAME "pps-gmtimer"
#define pr_fmt(fmt) MODULE_NAME ": " fmt
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/clk.h>
#include <linux/string.h>
#include <linux/platform_device.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/interrupt.h>
#include <linux/device.h>
#include <linux/pinctrl/consumer.h>
#include <linux/pps_kernel.h>
#include <linux/clocksource.h>
#include <clocksource/timer-ti-dm.h>
#include <linux/platform_data/dmtimer-omap.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Dan Drown");
MODULE_DESCRIPTION("PPS Client Driver using OMAP Timer hardware");
MODULE_VERSION("0.1.0");
struct pps_gmtimer_platform_data
{
struct omap_dm_timer *capture_timer;
const struct omap_dm_timer_ops *timer_ops;
const char *timer_name;
uint32_t frequency;
unsigned int capture;
unsigned int overflow;
unsigned int count_at_interrupt;
struct pps_event_time ts;
struct timespec64 delta;
struct pps_device *pps;
struct pps_source_info info;
int ready;
struct clocksource clksrc;
};
/* kobject *******************/
static ssize_t timer_name_show(struct device *dev, struct device_attribute *attr, char *buf)
{
struct pps_gmtimer_platform_data *pdata = dev->platform_data;
return sprintf(buf, "%s\n", pdata->timer_name);
}
static DEVICE_ATTR(timer_name, S_IRUGO, timer_name_show, NULL);
static ssize_t stats_show(struct device *dev, struct device_attribute *attr, char *buf)
{
struct pps_gmtimer_platform_data *pdata = dev->platform_data;
return sprintf(buf, "capture: %u\noverflow: %u\n", pdata->capture, pdata->overflow);
}
static DEVICE_ATTR(stats, S_IRUGO, stats_show, NULL);
static ssize_t interrupt_delta_show(struct device *dev, struct device_attribute *attr, char *buf)
{
struct pps_gmtimer_platform_data *pdata = dev->platform_data;
return sprintf(buf, "%lld.%09ld\n", (long long)pdata->delta.tv_sec, pdata->delta.tv_nsec);
}
static DEVICE_ATTR(interrupt_delta, S_IRUGO, interrupt_delta_show, NULL);
static ssize_t pps_ts_show(struct device *dev, struct device_attribute *attr, char *buf)
{
struct pps_gmtimer_platform_data *pdata = dev->platform_data;
return sprintf(buf, "%lld.%09ld\n", (long long)pdata->ts.ts_real.tv_sec, pdata->ts.ts_real.tv_nsec);
}
static DEVICE_ATTR(pps_ts, S_IRUGO, pps_ts_show, NULL);
static ssize_t count_at_interrupt_show(struct device *dev, struct device_attribute *attr, char *buf)
{
struct pps_gmtimer_platform_data *pdata = dev->platform_data;
return sprintf(buf, "%u\n", pdata->count_at_interrupt);
}
static DEVICE_ATTR(count_at_interrupt, S_IRUGO, count_at_interrupt_show, NULL);
static ssize_t capture_show(struct device *dev, struct device_attribute *attr, char *buf)
{
struct pps_gmtimer_platform_data *pdata = dev->platform_data;
return sprintf(buf, "%u\n",
__omap_dm_timer_read(pdata->capture_timer, OMAP_TIMER_CAPTURE_REG, pdata->capture_timer->posted));
}
static DEVICE_ATTR(capture, S_IRUGO, capture_show, NULL);
static ssize_t ctrlstatus_show(struct device *dev, struct device_attribute *attr, char *buf)
{
struct pps_gmtimer_platform_data *pdata = dev->platform_data;
return sprintf(buf, "%x\n",
__omap_dm_timer_read(pdata->capture_timer, OMAP_TIMER_CTRL_REG, pdata->capture_timer->posted));
}
static DEVICE_ATTR(ctrlstatus, S_IRUGO, ctrlstatus_show, NULL);
static ssize_t timer_counter_show(struct device *dev, struct device_attribute *attr, char *buf)
{
struct pps_gmtimer_platform_data *pdata = dev->platform_data;
unsigned int current_count = 0;
current_count = pdata->timer_ops->read_counter(pdata->capture_timer);
return sprintf(buf, "%u\n", current_count);
}
static DEVICE_ATTR(timer_counter, S_IRUGO, timer_counter_show, NULL);
static struct attribute *attrs[] = {
&dev_attr_timer_counter.attr,
&dev_attr_ctrlstatus.attr,
&dev_attr_capture.attr,
&dev_attr_count_at_interrupt.attr,
&dev_attr_pps_ts.attr,
&dev_attr_interrupt_delta.attr,
&dev_attr_stats.attr,
&dev_attr_timer_name.attr,
NULL,
};
static struct attribute_group attr_group = {
.attrs = attrs,
};
/* timers ********************/
static irqreturn_t pps_gmtimer_interrupt(int irq, void *data)
{
struct pps_gmtimer_platform_data *pdata;
pdata = data;
if (pdata->ready)
{
unsigned int irq_status;
irq_status = pdata->timer_ops->read_status(pdata->capture_timer);
if (irq_status & OMAP_TIMER_INT_CAPTURE)
{
uint32_t ps_per_hz;
unsigned int count_at_capture;
pps_get_ts(&pdata->ts);
pdata->count_at_interrupt = pdata->timer_ops->read_counter(pdata->capture_timer);
count_at_capture = __omap_dm_timer_read(pdata->capture_timer, OMAP_TIMER_CAPTURE_REG, pdata->capture_timer->posted);
pdata->delta.tv_sec = 0;
// use picoseconds per hz to avoid floating point and limit the rounding error
ps_per_hz = 1000000000 / (pdata->frequency / 1000);
pdata->delta.tv_nsec = ((pdata->count_at_interrupt - count_at_capture) * ps_per_hz) / 1000;
pps_sub_ts(&pdata->ts, pdata->delta);
pps_event(pdata->pps, &pdata->ts, PPS_CAPTUREASSERT, NULL);
pdata->capture++;
__omap_dm_timer_write_status(pdata->capture_timer, OMAP_TIMER_INT_CAPTURE);
}
if (irq_status & OMAP_TIMER_INT_OVERFLOW)
{
pdata->overflow++;
__omap_dm_timer_write_status(pdata->capture_timer, OMAP_TIMER_INT_OVERFLOW);
}
}
return IRQ_HANDLED; // TODO: shared interrupts?
}
static void omap_dm_timer_setup_capture(struct omap_dm_timer *timer, const struct omap_dm_timer_ops *timer_ops)
{
u32 ctrl;
timer_ops->set_source(timer, OMAP_TIMER_SRC_SYS_CLK);
timer_ops->enable(timer);
ctrl = __omap_dm_timer_read(timer, OMAP_TIMER_CTRL_REG, timer->posted);
// disable prescaler
ctrl &= ~(OMAP_TIMER_CTRL_PRE | (0x07 << 2));
// autoreload
ctrl |= OMAP_TIMER_CTRL_AR;
__omap_dm_timer_write(timer, OMAP_TIMER_LOAD_REG, 0, timer->posted);
// start timer
ctrl |= OMAP_TIMER_CTRL_ST;
// set capture
ctrl |= OMAP_TIMER_CTRL_TCM_LOWTOHIGH | OMAP_TIMER_CTRL_GPOCFG; // TODO: configurable direction
__omap_dm_timer_load_start(timer, ctrl, 0, timer->posted);
/* Save the context */
timer->context.tclr = ctrl;
timer->context.tldr = 0;
timer->context.tcrr = 0;
}
/* if tclkin has no clock, writes to the timer registers will stall and you will get a message like:
* Unhandled fault: external abort on non-linefetch (0x1028) at 0xfa044048
*/
static void omap_dm_timer_use_tclkin(struct pps_gmtimer_platform_data *pdata)
{
struct clk *gt_fclk;
pdata->timer_ops->set_source(pdata->capture_timer, OMAP_TIMER_SRC_EXT_CLK);
gt_fclk = pdata->timer_ops->get_fclk(pdata->capture_timer);
pdata->frequency = clk_get_rate(gt_fclk);
pr_info("timer(%s) switched to tclkin, rate=%uHz\n", pdata->timer_name, pdata->frequency);
}
static void pps_gmtimer_enable_irq(struct pps_gmtimer_platform_data *pdata)
{
unsigned int interrupt_mask;
interrupt_mask = OMAP_TIMER_INT_CAPTURE | OMAP_TIMER_INT_OVERFLOW;
__omap_dm_timer_int_enable(pdata->capture_timer, interrupt_mask);
pdata->capture_timer->context.tier = interrupt_mask;
pdata->capture_timer->context.twer = interrupt_mask;
}
static int pps_gmtimer_init_timer(struct device_node *timer_dn, struct pps_gmtimer_platform_data *pdata)
{
struct clk *gt_fclk;
of_property_read_string_index(timer_dn, "ti,hwmods", 0, &pdata->timer_name);
if (!pdata->timer_name)
{
pr_err("ti,hwmods property missing?\n");
return -ENODEV;
}
pdata->capture_timer = pdata->timer_ops->request_by_node(timer_dn);
if (!pdata->capture_timer)
{
pr_err("request_by_node failed\n");
return -ENODEV;
}
// TODO: use devm_request_irq?
if (request_irq(pdata->capture_timer->irq, pps_gmtimer_interrupt, IRQF_TIMER, MODULE_NAME, pdata))
{
pr_err("cannot register IRQ %d\n", pdata->capture_timer->irq);
return -EIO;
}
omap_dm_timer_setup_capture(pdata->capture_timer, pdata->timer_ops);
gt_fclk = pdata->timer_ops->get_fclk(pdata->capture_timer);
pdata->frequency = clk_get_rate(gt_fclk);
pr_info("timer name=%s rate=%uHz\n", pdata->timer_name, pdata->frequency);
return 0;
}
static void pps_gmtimer_cleanup_timer(struct pps_gmtimer_platform_data *pdata)
{
if (pdata->capture_timer)
{
pdata->timer_ops->set_source(pdata->capture_timer, OMAP_TIMER_SRC_SYS_CLK); // in case TCLKIN is stopped during boot
pdata->timer_ops->set_int_disable(pdata->capture_timer, OMAP_TIMER_INT_CAPTURE | OMAP_TIMER_INT_OVERFLOW);
free_irq(pdata->capture_timer->irq, pdata);
pdata->timer_ops->stop(pdata->capture_timer);
pdata->timer_ops->free(pdata->capture_timer);
pdata->capture_timer = NULL;
pr_info("Exiting.\n");
}
}
/* clocksource ***************/
static struct pps_gmtimer_platform_data *clocksource_timer = NULL;
static u64 pps_gmtimer_read_cycles(struct clocksource *cs)
{
return (u64)__omap_dm_timer_read_counter(clocksource_timer->capture_timer, clocksource_timer->capture_timer->posted);
}
static void pps_gmtimer_clocksource_init(struct pps_gmtimer_platform_data *pdata)
{
if (!clocksource_timer)
{
pdata->clksrc.name = pdata->timer_name;
pdata->clksrc.rating = 299;
pdata->clksrc.read = pps_gmtimer_read_cycles;
pdata->clksrc.mask = CLOCKSOURCE_MASK(32);
pdata->clksrc.flags = CLOCK_SOURCE_IS_CONTINUOUS;
clocksource_timer = pdata;
if (clocksource_register_hz(&pdata->clksrc, pdata->frequency))
{
pr_err("Could not register clocksource %s\n", pdata->clksrc.name);
clocksource_timer = NULL;
}
else
{
pr_info("clocksource: %s at %u Hz\n", pdata->clksrc.name, pdata->frequency);
}
}
}
static void pps_gmtimer_clocksource_cleanup(struct pps_gmtimer_platform_data *pdata)
{
if (pdata == clocksource_timer)
{
clocksource_unregister(&pdata->clksrc);
clocksource_timer = NULL;
}
}
/* module ********************/
static struct pps_gmtimer_platform_data *of_get_pps_gmtimer_pdata(struct platform_device *pdev)
{
struct device_node *np = pdev->dev.of_node, *timer_dn;
struct platform_device *timer_pdev;
struct dmtimer_platform_data *timer_pdata;
struct pps_gmtimer_platform_data *pdata;
pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
if (!pdata)
{
return NULL;
}
pdata->ready = 0;
timer_dn = of_parse_phandle(np, "timer", 0);
if (!timer_dn)
{
dev_err(&pdev->dev, "Unable to parse device node\n");
return NULL;
}
timer_pdev = of_find_device_by_node(timer_dn);
if (!timer_pdev)
{
dev_err(&pdev->dev, "Unable to find Timer pdev\n");
goto fail2;
}
timer_pdata = dev_get_platdata(&timer_pdev->dev);
if (!timer_pdata)
{
dev_err(&pdev->dev, "dmtimer pdata structure NULL\n");
goto fail2;
}
pdata->timer_ops = timer_pdata->timer_ops;
if (!pdata->timer_ops || !pdata->timer_ops->request_by_node ||
!pdata->timer_ops->free ||
!pdata->timer_ops->enable ||
!pdata->timer_ops->disable ||
!pdata->timer_ops->get_fclk ||
!pdata->timer_ops->start ||
!pdata->timer_ops->stop ||
!pdata->timer_ops->set_load ||
!pdata->timer_ops->set_match ||
!pdata->timer_ops->set_pwm ||
!pdata->timer_ops->set_prescaler ||
!pdata->timer_ops->write_counter)
{
dev_err(&pdev->dev, "Incomplete dmtimer pdata structure\n");
goto fail2;
}
if (pps_gmtimer_init_timer(timer_dn, pdata) < 0)
{
goto fail2;
}
of_node_put(timer_dn);
return pdata;
fail2:
of_node_put(timer_dn);
devm_kfree(&pdev->dev, pdata);
return NULL;
}
static const struct of_device_id pps_gmtimer_dt_ids[] = {
{
.compatible = "pps-gmtimer",
},
{/* sentinel */}};
MODULE_DEVICE_TABLE(of, pps_gmtimer_dt_ids);
static int pps_gmtimer_probe(struct platform_device *pdev)
{
const struct of_device_id *match;
struct pps_gmtimer_platform_data *pdata;
struct pinctrl *pinctrl;
const __be32 *use_tclkin;
match = of_match_device(pps_gmtimer_dt_ids, &pdev->dev);
if (match)
{
pdev->dev.platform_data = of_get_pps_gmtimer_pdata(pdev);
}
else
{
pr_err("of_match_device failed\n");
}
pdata = pdev->dev.platform_data;
if (!pdata)
return -ENODEV;
pdata->ready = 0;
if (sysfs_create_group(&pdev->dev.kobj, &attr_group))
{
pr_err("sysfs_create_group failed\n");
}
pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
if (IS_ERR(pinctrl))
pr_warning("pins are not configured from the driver\n");
use_tclkin = of_get_property(pdev->dev.of_node, "use-tclkin", NULL);
if (use_tclkin && be32_to_cpup(use_tclkin) == 1)
{
omap_dm_timer_use_tclkin(pdata);
}
else
{
pr_info("using system clock\n");
}
pdata->info.mode = PPS_CAPTUREASSERT | PPS_ECHOASSERT | PPS_CANWAIT | PPS_TSFMT_TSPEC;
pdata->info.owner = THIS_MODULE;
snprintf(pdata->info.name, PPS_MAX_NAME_LEN - 1, "%s", pdata->timer_name);
pdata->pps = pps_register_source(&pdata->info, PPS_CAPTUREASSERT);
if (pdata->pps == NULL)
{
pr_err("failed to register %s as PPS source\n", pdata->timer_name);
}
else
{
// ready to go
pdata->ready = 1;
pps_gmtimer_clocksource_init(pdata);
pps_gmtimer_enable_irq(pdata);
}
return 0;
}
static int pps_gmtimer_remove(struct platform_device *pdev)
{
struct pps_gmtimer_platform_data *pdata;
pdata = pdev->dev.platform_data;
if (pdata)
{
pps_gmtimer_clocksource_cleanup(pdata);
pps_gmtimer_cleanup_timer(pdata);
if (pdata->pps)
{
pps_unregister_source(pdata->pps);
pdata->pps = NULL;
}
devm_kfree(&pdev->dev, pdata);
pdev->dev.platform_data = NULL;
sysfs_remove_group(&pdev->dev.kobj, &attr_group);
}
platform_set_drvdata(pdev, NULL);
return 0;
}
static struct platform_driver pps_gmtimer_driver = {
.probe = pps_gmtimer_probe,
.remove = pps_gmtimer_remove,
.driver = {
.name = MODULE_NAME,
.owner = THIS_MODULE,
.of_match_table = of_match_ptr(pps_gmtimer_dt_ids),
},
};
module_platform_driver(pps_gmtimer_driver);