ARM: rockchip: rk3228: add grf definition
[firefly-linux-kernel-4.4.55.git] / include / linux / clockchips.h
1 /*  linux/include/linux/clockchips.h
2  *
3  *  This file contains the structure definitions for clockchips.
4  *
5  *  If you are not a clockchip, or the time of day code, you should
6  *  not be including this file!
7  */
8 #ifndef _LINUX_CLOCKCHIPS_H
9 #define _LINUX_CLOCKCHIPS_H
10
11 /* Clock event notification values */
12 enum clock_event_nofitiers {
13         CLOCK_EVT_NOTIFY_ADD,
14         CLOCK_EVT_NOTIFY_BROADCAST_ON,
15         CLOCK_EVT_NOTIFY_BROADCAST_OFF,
16         CLOCK_EVT_NOTIFY_BROADCAST_FORCE,
17         CLOCK_EVT_NOTIFY_BROADCAST_ENTER,
18         CLOCK_EVT_NOTIFY_BROADCAST_EXIT,
19         CLOCK_EVT_NOTIFY_SUSPEND,
20         CLOCK_EVT_NOTIFY_RESUME,
21         CLOCK_EVT_NOTIFY_CPU_DYING,
22         CLOCK_EVT_NOTIFY_CPU_DEAD,
23 };
24
25 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BUILD
26
27 #include <linux/clocksource.h>
28 #include <linux/cpumask.h>
29 #include <linux/ktime.h>
30 #include <linux/notifier.h>
31
32 struct clock_event_device;
33 struct module;
34
35 /* Clock event mode commands */
36 enum clock_event_mode {
37         CLOCK_EVT_MODE_UNUSED = 0,
38         CLOCK_EVT_MODE_SHUTDOWN,
39         CLOCK_EVT_MODE_PERIODIC,
40         CLOCK_EVT_MODE_ONESHOT,
41         CLOCK_EVT_MODE_RESUME,
42 };
43
44 /*
45  * Clock event features
46  */
47 #define CLOCK_EVT_FEAT_PERIODIC         0x000001
48 #define CLOCK_EVT_FEAT_ONESHOT          0x000002
49 #define CLOCK_EVT_FEAT_KTIME            0x000004
50 /*
51  * x86(64) specific misfeatures:
52  *
53  * - Clockevent source stops in C3 State and needs broadcast support.
54  * - Local APIC timer is used as a dummy device.
55  */
56 #define CLOCK_EVT_FEAT_C3STOP           0x000008
57 #define CLOCK_EVT_FEAT_DUMMY            0x000010
58
59 /*
60  * Core shall set the interrupt affinity dynamically in broadcast mode
61  */
62 #define CLOCK_EVT_FEAT_DYNIRQ           0x000020
63
64 /*
65  * Clockevent device is based on a hrtimer for broadcast
66  */
67 #define CLOCK_EVT_FEAT_HRTIMER          0x000080
68
69 /**
70  * struct clock_event_device - clock event device descriptor
71  * @event_handler:      Assigned by the framework to be called by the low
72  *                      level handler of the event source
73  * @set_next_event:     set next event function using a clocksource delta
74  * @set_next_ktime:     set next event function using a direct ktime value
75  * @next_event:         local storage for the next event in oneshot mode
76  * @max_delta_ns:       maximum delta value in ns
77  * @min_delta_ns:       minimum delta value in ns
78  * @mult:               nanosecond to cycles multiplier
79  * @shift:              nanoseconds to cycles divisor (power of two)
80  * @mode:               operating mode assigned by the management code
81  * @features:           features
82  * @retries:            number of forced programming retries
83  * @set_mode:           set mode function
84  * @broadcast:          function to broadcast events
85  * @min_delta_ticks:    minimum delta value in ticks stored for reconfiguration
86  * @max_delta_ticks:    maximum delta value in ticks stored for reconfiguration
87  * @name:               ptr to clock event name
88  * @rating:             variable to rate clock event devices
89  * @irq:                IRQ number (only for non CPU local devices)
90  * @bound_on:           Bound on CPU
91  * @cpumask:            cpumask to indicate for which CPUs this device works
92  * @list:               list head for the management code
93  * @owner:              module reference
94  */
95 struct clock_event_device {
96         void                    (*event_handler)(struct clock_event_device *);
97         int                     (*set_next_event)(unsigned long evt,
98                                                   struct clock_event_device *);
99         int                     (*set_next_ktime)(ktime_t expires,
100                                                   struct clock_event_device *);
101         ktime_t                 next_event;
102         u64                     max_delta_ns;
103         u64                     min_delta_ns;
104         u32                     mult;
105         u32                     shift;
106         enum clock_event_mode   mode;
107         unsigned int            features;
108         unsigned long           retries;
109
110         void                    (*broadcast)(const struct cpumask *mask);
111         void                    (*set_mode)(enum clock_event_mode mode,
112                                             struct clock_event_device *);
113         void                    (*suspend)(struct clock_event_device *);
114         void                    (*resume)(struct clock_event_device *);
115         unsigned long           min_delta_ticks;
116         unsigned long           max_delta_ticks;
117
118         const char              *name;
119         int                     rating;
120         int                     irq;
121         int                     bound_on;
122         const struct cpumask    *cpumask;
123         struct list_head        list;
124         struct module           *owner;
125 } ____cacheline_aligned;
126
127 /*
128  * Calculate a multiplication factor for scaled math, which is used to convert
129  * nanoseconds based values to clock ticks:
130  *
131  * clock_ticks = (nanoseconds * factor) >> shift.
132  *
133  * div_sc is the rearranged equation to calculate a factor from a given clock
134  * ticks / nanoseconds ratio:
135  *
136  * factor = (clock_ticks << shift) / nanoseconds
137  */
138 static inline unsigned long div_sc(unsigned long ticks, unsigned long nsec,
139                                    int shift)
140 {
141         uint64_t tmp = ((uint64_t)ticks) << shift;
142
143         do_div(tmp, nsec);
144         return (unsigned long) tmp;
145 }
146
147 /* Clock event layer functions */
148 extern u64 clockevent_delta2ns(unsigned long latch,
149                                struct clock_event_device *evt);
150 extern void clockevents_register_device(struct clock_event_device *dev);
151
152 extern void clockevents_config(struct clock_event_device *dev, u32 freq);
153 extern void clockevents_config_and_register(struct clock_event_device *dev,
154                                             u32 freq, unsigned long min_delta,
155                                             unsigned long max_delta);
156
157 extern int clockevents_update_freq(struct clock_event_device *ce, u32 freq);
158
159 extern void clockevents_exchange_device(struct clock_event_device *old,
160                                         struct clock_event_device *new);
161 extern void clockevents_set_mode(struct clock_event_device *dev,
162                                  enum clock_event_mode mode);
163 extern int clockevents_program_event(struct clock_event_device *dev,
164                                      ktime_t expires, bool force);
165
166 extern void clockevents_handle_noop(struct clock_event_device *dev);
167
168 static inline void
169 clockevents_calc_mult_shift(struct clock_event_device *ce, u32 freq, u32 minsec)
170 {
171         return clocks_calc_mult_shift(&ce->mult, &ce->shift, NSEC_PER_SEC,
172                                       freq, minsec);
173 }
174
175 extern void clockevents_suspend(void);
176 extern void clockevents_resume(void);
177
178 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
179 #ifdef CONFIG_ARCH_HAS_TICK_BROADCAST
180 extern void tick_broadcast(const struct cpumask *mask);
181 #else
182 #define tick_broadcast  NULL
183 #endif
184 extern int tick_receive_broadcast(void);
185 #endif
186
187 #if defined(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST) && defined(CONFIG_TICK_ONESHOT)
188 extern void tick_setup_hrtimer_broadcast(void);
189 extern int tick_check_broadcast_expired(void);
190 #else
191 static inline int tick_check_broadcast_expired(void) { return 0; }
192 static inline void tick_setup_hrtimer_broadcast(void) {};
193 #endif
194
195 #ifdef CONFIG_GENERIC_CLOCKEVENTS
196 extern int clockevents_notify(unsigned long reason, void *arg);
197 #else
198 static inline int clockevents_notify(unsigned long reason, void *arg) { return 0; }
199 #endif
200
201 #else /* CONFIG_GENERIC_CLOCKEVENTS_BUILD */
202
203 static inline void clockevents_suspend(void) {}
204 static inline void clockevents_resume(void) {}
205
206 static inline int clockevents_notify(unsigned long reason, void *arg) { return 0; }
207 static inline int tick_check_broadcast_expired(void) { return 0; }
208 static inline void tick_setup_hrtimer_broadcast(void) {};
209
210 #endif
211
212 #endif