diff options
author | Alexander Kuleshov <kuleshovmail@gmail.com> | 2016-02-26 22:14:13 -0500 |
---|---|---|
committer | Thomas Gleixner <tglx@linutronix.de> | 2016-02-27 02:55:30 -0500 |
commit | 7aca0c07207385cca76025cc85231519935722b9 (patch) | |
tree | 3ef5dafb42ad1f6772df77eb499d660176c75599 /include/linux/clocksource.h | |
parent | 07f101d33def572c7018626735475f88dabecebf (diff) |
clocksource: Introduce clocksource_freq2mult()
The clocksource_khz2mult() and clocksource_hz2mult() share similar
code wihch calculates a mult from the given frequency. Both implementations
in differ only in value of a frequency. This patch introduces the
clocksource_freq2mult() helper with generic implementation of
mult calculation to prevent code duplication.
Signed-off-by: Alexander Kuleshov <kuleshovmail@gmail.com>
Signed-off-by: John Stultz <john.stultz@linaro.org>
Cc: Prarit Bhargava <prarit@redhat.com>
Cc: Richard Cochran <richardcochran@gmail.com>
Link: http://lkml.kernel.org/r/1456542854-22104-2-git-send-email-john.stultz@linaro.org
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Diffstat (limited to 'include/linux/clocksource.h')
-rw-r--r-- | include/linux/clocksource.h | 45 |
1 files changed, 19 insertions, 26 deletions
diff --git a/include/linux/clocksource.h b/include/linux/clocksource.h index 6013021a3b39..a307bf62974f 100644 --- a/include/linux/clocksource.h +++ b/include/linux/clocksource.h | |||
@@ -118,6 +118,23 @@ struct clocksource { | |||
118 | /* simplify initialization of mask field */ | 118 | /* simplify initialization of mask field */ |
119 | #define CLOCKSOURCE_MASK(bits) (cycle_t)((bits) < 64 ? ((1ULL<<(bits))-1) : -1) | 119 | #define CLOCKSOURCE_MASK(bits) (cycle_t)((bits) < 64 ? ((1ULL<<(bits))-1) : -1) |
120 | 120 | ||
121 | static inline u32 clocksource_freq2mult(u32 freq, u32 shift_constant, u64 from) | ||
122 | { | ||
123 | /* freq = cyc/from | ||
124 | * mult/2^shift = ns/cyc | ||
125 | * mult = ns/cyc * 2^shift | ||
126 | * mult = from/freq * 2^shift | ||
127 | * mult = from * 2^shift / freq | ||
128 | * mult = (from<<shift) / freq | ||
129 | */ | ||
130 | u64 tmp = ((u64)from) << shift_constant; | ||
131 | |||
132 | tmp += freq/2; /* round for do_div */ | ||
133 | do_div(tmp, freq); | ||
134 | |||
135 | return (u32)tmp; | ||
136 | } | ||
137 | |||
121 | /** | 138 | /** |
122 | * clocksource_khz2mult - calculates mult from khz and shift | 139 | * clocksource_khz2mult - calculates mult from khz and shift |
123 | * @khz: Clocksource frequency in KHz | 140 | * @khz: Clocksource frequency in KHz |
@@ -128,19 +145,7 @@ struct clocksource { | |||
128 | */ | 145 | */ |
129 | static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant) | 146 | static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant) |
130 | { | 147 | { |
131 | /* khz = cyc/(Million ns) | 148 | return clocksource_freq2mult(khz, shift_constant, NSEC_PER_MSEC); |
132 | * mult/2^shift = ns/cyc | ||
133 | * mult = ns/cyc * 2^shift | ||
134 | * mult = 1Million/khz * 2^shift | ||
135 | * mult = 1000000 * 2^shift / khz | ||
136 | * mult = (1000000<<shift) / khz | ||
137 | */ | ||
138 | u64 tmp = ((u64)1000000) << shift_constant; | ||
139 | |||
140 | tmp += khz/2; /* round for do_div */ | ||
141 | do_div(tmp, khz); | ||
142 | |||
143 | return (u32)tmp; | ||
144 | } | 149 | } |
145 | 150 | ||
146 | /** | 151 | /** |
@@ -154,19 +159,7 @@ static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant) | |||
154 | */ | 159 | */ |
155 | static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant) | 160 | static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant) |
156 | { | 161 | { |
157 | /* hz = cyc/(Billion ns) | 162 | return clocksource_freq2mult(hz, shift_constant, NSEC_PER_SEC); |
158 | * mult/2^shift = ns/cyc | ||
159 | * mult = ns/cyc * 2^shift | ||
160 | * mult = 1Billion/hz * 2^shift | ||
161 | * mult = 1000000000 * 2^shift / hz | ||
162 | * mult = (1000000000<<shift) / hz | ||
163 | */ | ||
164 | u64 tmp = ((u64)1000000000) << shift_constant; | ||
165 | |||
166 | tmp += hz/2; /* round for do_div */ | ||
167 | do_div(tmp, hz); | ||
168 | |||
169 | return (u32)tmp; | ||
170 | } | 163 | } |
171 | 164 | ||
172 | /** | 165 | /** |