min/max: remove sparse warnings when they're nested
[cascardo/linux.git] / drivers / staging / greybus / timesync_platform.c
1 /*
2  * TimeSync API driver.
3  *
4  * Copyright 2016 Google Inc.
5  * Copyright 2016 Linaro Ltd.
6  *
7  * Released under the GPLv2 only.
8  *
9  * This code reads directly from an ARMv7 memory-mapped timer that lives in
10  * MMIO space. Since this counter lives inside of MMIO space its shared between
11  * cores and that means we don't have to worry about issues like TSC on x86
12  * where each time-stamp-counter (TSC) is local to a particular core.
13  *
14  * Register-level access code is based on
15  * drivers/clocksource/arm_arch_timer.c
16  */
17 #include <linux/cpufreq.h>
18 #include <linux/of_platform.h>
19
20 #include "greybus.h"
21 #include "arche_platform.h"
22
23 #define DEFAULT_FRAMETIME_CLOCK_HZ 19200000
24
25 static u32 gb_timesync_clock_frequency;
26 int (*arche_platform_change_state_cb)(enum arche_platform_state state,
27                                       struct gb_timesync_svc *pdata);
28 EXPORT_SYMBOL_GPL(arche_platform_change_state_cb);
29
30 u64 gb_timesync_platform_get_counter(void)
31 {
32         return (u64)get_cycles();
33 }
34
35 u32 gb_timesync_platform_get_clock_rate(void)
36 {
37         if (unlikely(!gb_timesync_clock_frequency)) {
38                 gb_timesync_clock_frequency = cpufreq_get(0);
39                 if (!gb_timesync_clock_frequency)
40                         gb_timesync_clock_frequency = DEFAULT_FRAMETIME_CLOCK_HZ;
41         }
42
43         return gb_timesync_clock_frequency;
44 }
45
46 int gb_timesync_platform_lock_bus(struct gb_timesync_svc *pdata)
47 {
48         return arche_platform_change_state_cb(ARCHE_PLATFORM_STATE_TIME_SYNC,
49                                               pdata);
50 }
51
52 void gb_timesync_platform_unlock_bus(void)
53 {
54         arche_platform_change_state_cb(ARCHE_PLATFORM_STATE_ACTIVE, NULL);
55 }
56
57 static const struct of_device_id arch_timer_of_match[] = {
58         { .compatible   = "google,greybus-frame-time-counter", },
59         {},
60 };
61
62 int __init gb_timesync_platform_init(void)
63 {
64         struct device_node *np;
65
66         np = of_find_matching_node(NULL, arch_timer_of_match);
67         if (!np) {
68                 /* Tolerate not finding to allow BBB etc to continue */
69                 pr_warn("Unable to find a compatible ARMv7 timer\n");
70                 return 0;
71         }
72
73         if (of_property_read_u32(np, "clock-frequency",
74                                  &gb_timesync_clock_frequency)) {
75                 pr_err("Unable to find timer clock-frequency\n");
76                 return -ENODEV;
77         }
78
79         return 0;
80 }
81
82 void gb_timesync_platform_exit(void) {}