Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

clk: bcm2835: Minimise clock jitter for PCM clock

Fractional clock dividers generate accurate average frequencies but
with jitter, particularly when the integer divisor is small.

Introduce a new metric of clock accuracy to penalise clocks with a good
average but worse jitter compared to clocks with an average which is no
better but with lower jitter. The metric is the ideal rate minus the
worse deviation from that ideal using the nearest integer divisors.

Use this metric for parent selection for clocks requiring low jitter
(currently just PCM).

Signed-off-by: Phil Elwell <phil@raspberrypi.org>
Reviewed-by: Eric Anholt <eric@anholt.net>
Acked-by: Stefan Wahren <stefan.wahren@i2se.com>
Signed-off-by: Stephen Boyd <sboyd@codeaurora.org>

authored by

Phil Elwell and committed by
Stephen Boyd
3542976d 8c0de581

+29 -5
+29 -5
drivers/clk/bcm/clk-bcm2835.c
··· 530 530 531 531 bool is_vpu_clock; 532 532 bool is_mash_clock; 533 + bool low_jitter; 533 534 534 535 u32 tcnt_mux; 535 536 }; ··· 1127 1126 int parent_idx, 1128 1127 unsigned long rate, 1129 1128 u32 *div, 1130 - unsigned long *prate) 1129 + unsigned long *prate, 1130 + unsigned long *avgrate) 1131 1131 { 1132 1132 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); 1133 1133 struct bcm2835_cprman *cprman = clock->cprman; ··· 1143 1141 *prate = clk_hw_get_rate(parent); 1144 1142 *div = bcm2835_clock_choose_div(hw, rate, *prate, true); 1145 1143 1146 - return bcm2835_clock_rate_from_divisor(clock, *prate, 1147 - *div); 1144 + *avgrate = bcm2835_clock_rate_from_divisor(clock, *prate, *div); 1145 + 1146 + if (data->low_jitter && (*div & CM_DIV_FRAC_MASK)) { 1147 + unsigned long high, low; 1148 + u32 int_div = *div & ~CM_DIV_FRAC_MASK; 1149 + 1150 + high = bcm2835_clock_rate_from_divisor(clock, *prate, 1151 + int_div); 1152 + int_div += CM_DIV_FRAC_MASK + 1; 1153 + low = bcm2835_clock_rate_from_divisor(clock, *prate, 1154 + int_div); 1155 + 1156 + /* 1157 + * Return a value which is the maximum deviation 1158 + * below the ideal rate, for use as a metric. 1159 + */ 1160 + return *avgrate - max(*avgrate - low, high - *avgrate); 1161 + } 1162 + return *avgrate; 1148 1163 } 1149 1164 1150 1165 if (data->frac_bits) ··· 1188 1169 1189 1170 *div = curdiv << CM_DIV_FRAC_BITS; 1190 1171 *prate = curdiv * best_rate; 1172 + *avgrate = best_rate; 1191 1173 1192 1174 return best_rate; 1193 1175 } ··· 1200 1180 bool current_parent_is_pllc; 1201 1181 unsigned long rate, best_rate = 0; 1202 1182 unsigned long prate, best_prate = 0; 1183 + unsigned long avgrate, best_avgrate = 0; 1203 1184 size_t i; 1204 1185 u32 div; 1205 1186 ··· 1225 1204 continue; 1226 1205 1227 1206 rate = bcm2835_clock_choose_div_and_prate(hw, i, req->rate, 1228 - &div, &prate); 1207 + &div, &prate, 1208 + &avgrate); 1229 1209 if (rate > best_rate && rate <= req->rate) { 1230 1210 best_parent = parent; 1231 1211 best_prate = prate; 1232 1212 best_rate = rate; 1213 + best_avgrate = avgrate; 1233 1214 } 1234 1215 } 1235 1216 ··· 1241 1218 req->best_parent_hw = best_parent; 1242 1219 req->best_parent_rate = best_prate; 1243 1220 1244 - req->rate = best_rate; 1221 + req->rate = best_avgrate; 1245 1222 1246 1223 return 0; 1247 1224 } ··· 2050 2027 .int_bits = 12, 2051 2028 .frac_bits = 12, 2052 2029 .is_mash_clock = true, 2030 + .low_jitter = true, 2053 2031 .tcnt_mux = 23), 2054 2032 [BCM2835_CLOCK_PWM] = REGISTER_PER_CLK( 2055 2033 .name = "pwm",