/* * _dpll_test_mult - test a DPLL multiplier value * @m: pointer to the DPLL m (multiplier) value under test * @n: current DPLL n (divider) value under test * @new_rate: pointer to storage for the resulting rounded rate * @target_rate: the desired DPLL rate * @parent_rate: the DPLL's parent clock rate * * This code tests a DPLL multiplier value, ensuring that the * resulting rate will not be higher than the target_rate, and that * the multiplier value itself is valid for the DPLL. Initially, the * integer pointed to by the m argument should be prescaled by * multiplying by DPLL_SCALE_FACTOR. The code will replace this with * a non-scaled m upon return. This non-scaled m will result in a * new_rate as close as possible to target_rate (but not greater than * target_rate) given the current (parent_rate, n, prescaled m) * triple. Returns DPLL_MULT_UNDERFLOW in the event that the * non-scaled m attempted to underflow, which can allow the calling * function to bail out early; or 0 upon success. */ static int _dpll_test_mult(int *m, int n, unsigned long *new_rate, unsigned long target_rate, unsigned long parent_rate) { int r = 0, carry = 0; /* Unscale m and round if necessary */ if (*m % DPLL_SCALE_FACTOR >= DPLL_ROUNDING_VAL) carry = 1; *m = (*m / DPLL_SCALE_FACTOR) + carry; /* * The new rate must be <= the target rate to avoid programming * a rate that is impossible for the hardware to handle */ *new_rate = _dpll_compute_new_rate(parent_rate, *m, n); if (*new_rate > target_rate) { (*m)--; *new_rate = 0; } /* Guard against m underflow */ if (*m < DPLL_MIN_MULTIPLIER) { *m = DPLL_MIN_MULTIPLIER; *new_rate = 0; r = DPLL_MULT_UNDERFLOW; } if (*new_rate == 0) *new_rate = _dpll_compute_new_rate(parent_rate, *m, n); return r; }
static int _dpll_test_mult(int *m, int n, unsigned long *new_rate, unsigned long target_rate, unsigned long parent_rate) { int r = 0, carry = 0; if (*m % DPLL_SCALE_FACTOR >= DPLL_ROUNDING_VAL) carry = 1; *m = (*m / DPLL_SCALE_FACTOR) + carry; *new_rate = _dpll_compute_new_rate(parent_rate, *m, n); if (*new_rate > target_rate) { (*m)--; *new_rate = 0; } if (*m < DPLL_MIN_MULTIPLIER) { *m = DPLL_MIN_MULTIPLIER; *new_rate = 0; r = DPLL_MULT_UNDERFLOW; } if (*new_rate == 0) *new_rate = _dpll_compute_new_rate(parent_rate, *m, n); return r; }
/** * omap2_dpll_round_rate - round a target rate for an OMAP DPLL * @clk: struct clk * for a DPLL * @target_rate: desired DPLL clock rate * * Given a DPLL, a desired target rate, and a rate tolerance, round * the target rate to a possible, programmable rate for this DPLL. * Rate tolerance is assumed to be set by the caller before this * function is called. Attempts to select the minimum possible n * within the tolerance to reduce power consumption. Stores the * computed (m, n) in the DPLL's dpll_data structure so set_rate() * will not need to call this (expensive) function again. Returns ~0 * if the target rate cannot be rounded, either because the rate is * too low or because the rate tolerance is set too tightly; or the * rounded rate upon success. */ long omap2_dpll_round_rate(struct clk *clk, unsigned long target_rate) { int m, n, r, e, scaled_max_m; unsigned long scaled_rt_rp, new_rate; int min_e = -1, min_e_m = -1, min_e_n = -1; struct dpll_data *dd; if (!clk || !clk->dpll_data) return ~0; dd = clk->dpll_data; pr_debug("clock: starting DPLL round_rate for clock %s, target rate " "%ld\n", clk->name, target_rate); scaled_rt_rp = target_rate / (dd->clk_ref->rate / DPLL_SCALE_FACTOR); scaled_max_m = dd->max_multiplier * DPLL_SCALE_FACTOR; dd->last_rounded_rate = 0; for (n = dd->min_divider; n <= dd->max_divider; n++) { /* Is the (input clk, divider) pair valid for the DPLL? */ r = _dpll_test_fint(clk, n); if (r == DPLL_FINT_UNDERFLOW) break; else if (r == DPLL_FINT_INVALID) continue; /* Compute the scaled DPLL multiplier, based on the divider */ m = scaled_rt_rp * n; /* * Since we're counting n up, a m overflow means we * can bail out completely (since as n increases in * the next iteration, there's no way that m can * increase beyond the current m) */ if (m > scaled_max_m) break; r = _dpll_test_mult(&m, n, &new_rate, target_rate, dd->clk_ref->rate); /* m can't be set low enough for this n - try with a larger n */ if (r == DPLL_MULT_UNDERFLOW) continue; e = target_rate - new_rate; pr_debug("clock: n = %d: m = %d: rate error is %d " "(new_rate = %ld)\n", n, m, e, new_rate); if (min_e == -1 || min_e >= (int)(abs(e) - dd->rate_tolerance)) { min_e = e; min_e_m = m; min_e_n = n; pr_debug("clock: found new least error %d\n", min_e); /* We found good settings -- bail out now */ if (min_e <= dd->rate_tolerance) break; } } if (min_e < 0) { pr_debug("clock: error: target rate or tolerance too low\n"); return ~0; } dd->last_rounded_m = min_e_m; dd->last_rounded_n = min_e_n; dd->last_rounded_rate = _dpll_compute_new_rate(dd->clk_ref->rate, min_e_m, min_e_n); pr_debug("clock: final least error: e = %d, m = %d, n = %d\n", min_e, min_e_m, min_e_n); pr_debug("clock: final rate: %ld (target rate: %ld)\n", dd->last_rounded_rate, target_rate); return dd->last_rounded_rate; }
long omap2_dpll_round_rate(struct clk *clk, unsigned long target_rate) { int m, n, r, e, scaled_max_m; unsigned long scaled_rt_rp, new_rate; int min_e = -1, min_e_m = -1, min_e_n = -1; struct dpll_data *dd; if (!clk || !clk->dpll_data) return ~0; dd = clk->dpll_data; pr_debug("clock: starting DPLL round_rate for clock %s, target rate " "%ld\n", clk->name, target_rate); scaled_rt_rp = target_rate / (dd->clk_ref->rate / DPLL_SCALE_FACTOR); scaled_max_m = dd->max_multiplier * DPLL_SCALE_FACTOR; dd->last_rounded_rate = 0; for (n = dd->min_divider; n <= dd->max_divider; n++) { r = _dpll_test_fint(clk, n); if (r == DPLL_FINT_UNDERFLOW) break; else if (r == DPLL_FINT_INVALID) continue; m = scaled_rt_rp * n; if (m > scaled_max_m) break; r = _dpll_test_mult(&m, n, &new_rate, target_rate, dd->clk_ref->rate); if (r == DPLL_MULT_UNDERFLOW) continue; e = target_rate - new_rate; pr_debug("clock: n = %d: m = %d: rate error is %d " "(new_rate = %ld)\n", n, m, e, new_rate); if (min_e == -1 || min_e >= (int)(abs(e) - dd->rate_tolerance)) { min_e = e; min_e_m = m; min_e_n = n; pr_debug("clock: found new least error %d\n", min_e); if (min_e <= dd->rate_tolerance) break; } } if (min_e < 0) { pr_debug("clock: error: target rate or tolerance too low\n"); return ~0; } dd->last_rounded_m = min_e_m; dd->last_rounded_n = min_e_n; dd->last_rounded_rate = _dpll_compute_new_rate(dd->clk_ref->rate, min_e_m, min_e_n); pr_debug("clock: final least error: e = %d, m = %d, n = %d\n", min_e, min_e_m, min_e_n); pr_debug("clock: final rate: %ld (target rate: %ld)\n", dd->last_rounded_rate, target_rate); return dd->last_rounded_rate; }