Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 1 | /* |
Mauro Carvalho Chehab | cb7a01a | 2012-08-14 16:23:43 -0300 | [diff] [blame] | 2 | * drivers/media/i2c/smiapp-pll.c |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 3 | * |
| 4 | * Generic driver for SMIA/SMIA++ compliant camera modules |
| 5 | * |
| 6 | * Copyright (C) 2011--2012 Nokia Corporation |
Sakari Ailus | 8c5dff9 | 2012-10-28 06:44:17 -0300 | [diff] [blame] | 7 | * Contact: Sakari Ailus <sakari.ailus@iki.fi> |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License |
| 11 | * version 2 as published by the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 21 | * 02110-1301 USA |
| 22 | * |
| 23 | */ |
| 24 | |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 25 | #include <linux/gcd.h> |
| 26 | #include <linux/lcm.h> |
| 27 | #include <linux/module.h> |
| 28 | |
| 29 | #include "smiapp-pll.h" |
| 30 | |
| 31 | /* Return an even number or one. */ |
| 32 | static inline uint32_t clk_div_even(uint32_t a) |
| 33 | { |
| 34 | return max_t(uint32_t, 1, a & ~1); |
| 35 | } |
| 36 | |
| 37 | /* Return an even number or one. */ |
| 38 | static inline uint32_t clk_div_even_up(uint32_t a) |
| 39 | { |
| 40 | if (a == 1) |
| 41 | return 1; |
| 42 | return (a + 1) & ~1; |
| 43 | } |
| 44 | |
| 45 | static inline uint32_t is_one_or_even(uint32_t a) |
| 46 | { |
| 47 | if (a == 1) |
| 48 | return 1; |
| 49 | if (a & 1) |
| 50 | return 0; |
| 51 | |
| 52 | return 1; |
| 53 | } |
| 54 | |
| 55 | static int bounds_check(struct device *dev, uint32_t val, |
| 56 | uint32_t min, uint32_t max, char *str) |
| 57 | { |
| 58 | if (val >= min && val <= max) |
| 59 | return 0; |
| 60 | |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 61 | dev_dbg(dev, "%s out of bounds: %d (%d--%d)\n", str, val, min, max); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 62 | |
| 63 | return -EINVAL; |
| 64 | } |
| 65 | |
| 66 | static void print_pll(struct device *dev, struct smiapp_pll *pll) |
| 67 | { |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 68 | dev_dbg(dev, "pre_pll_clk_div\t%u\n", pll->pre_pll_clk_div); |
| 69 | dev_dbg(dev, "pll_multiplier \t%u\n", pll->pll_multiplier); |
Sakari Ailus | bc47150 | 2014-04-01 10:22:46 -0300 | [diff] [blame] | 70 | if (!(pll->flags & SMIAPP_PLL_FLAG_NO_OP_CLOCKS)) { |
Sakari Ailus | e3f8bc8 | 2014-09-16 09:07:11 -0300 | [diff] [blame] | 71 | dev_dbg(dev, "op_sys_clk_div \t%u\n", pll->op.sys_clk_div); |
| 72 | dev_dbg(dev, "op_pix_clk_div \t%u\n", pll->op.pix_clk_div); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 73 | } |
Sakari Ailus | e3f8bc8 | 2014-09-16 09:07:11 -0300 | [diff] [blame] | 74 | dev_dbg(dev, "vt_sys_clk_div \t%u\n", pll->vt.sys_clk_div); |
| 75 | dev_dbg(dev, "vt_pix_clk_div \t%u\n", pll->vt.pix_clk_div); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 76 | |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 77 | dev_dbg(dev, "ext_clk_freq_hz \t%u\n", pll->ext_clk_freq_hz); |
| 78 | dev_dbg(dev, "pll_ip_clk_freq_hz \t%u\n", pll->pll_ip_clk_freq_hz); |
| 79 | dev_dbg(dev, "pll_op_clk_freq_hz \t%u\n", pll->pll_op_clk_freq_hz); |
Sakari Ailus | bc47150 | 2014-04-01 10:22:46 -0300 | [diff] [blame] | 80 | if (!(pll->flags & SMIAPP_PLL_FLAG_NO_OP_CLOCKS)) { |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 81 | dev_dbg(dev, "op_sys_clk_freq_hz \t%u\n", |
Sakari Ailus | e3f8bc8 | 2014-09-16 09:07:11 -0300 | [diff] [blame] | 82 | pll->op.sys_clk_freq_hz); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 83 | dev_dbg(dev, "op_pix_clk_freq_hz \t%u\n", |
Sakari Ailus | e3f8bc8 | 2014-09-16 09:07:11 -0300 | [diff] [blame] | 84 | pll->op.pix_clk_freq_hz); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 85 | } |
Sakari Ailus | e3f8bc8 | 2014-09-16 09:07:11 -0300 | [diff] [blame] | 86 | dev_dbg(dev, "vt_sys_clk_freq_hz \t%u\n", pll->vt.sys_clk_freq_hz); |
| 87 | dev_dbg(dev, "vt_pix_clk_freq_hz \t%u\n", pll->vt.pix_clk_freq_hz); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 88 | } |
| 89 | |
Sakari Ailus | c859470 | 2014-09-15 18:35:18 -0300 | [diff] [blame] | 90 | static int check_all_bounds(struct device *dev, |
| 91 | const struct smiapp_pll_limits *limits, |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame^] | 92 | const struct smiapp_pll_branch_limits *op_limits, |
| 93 | struct smiapp_pll *pll, |
| 94 | struct smiapp_pll_branch *op_pll) |
Sakari Ailus | c859470 | 2014-09-15 18:35:18 -0300 | [diff] [blame] | 95 | { |
| 96 | int rval; |
| 97 | |
| 98 | rval = bounds_check(dev, pll->pll_ip_clk_freq_hz, |
| 99 | limits->min_pll_ip_freq_hz, |
| 100 | limits->max_pll_ip_freq_hz, |
| 101 | "pll_ip_clk_freq_hz"); |
| 102 | if (!rval) |
| 103 | rval = bounds_check( |
| 104 | dev, pll->pll_multiplier, |
| 105 | limits->min_pll_multiplier, limits->max_pll_multiplier, |
| 106 | "pll_multiplier"); |
| 107 | if (!rval) |
| 108 | rval = bounds_check( |
| 109 | dev, pll->pll_op_clk_freq_hz, |
| 110 | limits->min_pll_op_freq_hz, limits->max_pll_op_freq_hz, |
| 111 | "pll_op_clk_freq_hz"); |
| 112 | if (!rval) |
| 113 | rval = bounds_check( |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame^] | 114 | dev, op_pll->sys_clk_div, |
| 115 | op_limits->min_sys_clk_div, op_limits->max_sys_clk_div, |
Sakari Ailus | c859470 | 2014-09-15 18:35:18 -0300 | [diff] [blame] | 116 | "op_sys_clk_div"); |
| 117 | if (!rval) |
| 118 | rval = bounds_check( |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame^] | 119 | dev, op_pll->pix_clk_div, |
| 120 | op_limits->min_pix_clk_div, op_limits->max_pix_clk_div, |
Sakari Ailus | c859470 | 2014-09-15 18:35:18 -0300 | [diff] [blame] | 121 | "op_pix_clk_div"); |
| 122 | if (!rval) |
| 123 | rval = bounds_check( |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame^] | 124 | dev, op_pll->sys_clk_freq_hz, |
| 125 | op_limits->min_sys_clk_freq_hz, |
| 126 | op_limits->max_sys_clk_freq_hz, |
Sakari Ailus | c859470 | 2014-09-15 18:35:18 -0300 | [diff] [blame] | 127 | "op_sys_clk_freq_hz"); |
| 128 | if (!rval) |
| 129 | rval = bounds_check( |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame^] | 130 | dev, op_pll->pix_clk_freq_hz, |
| 131 | op_limits->min_pix_clk_freq_hz, |
| 132 | op_limits->max_pix_clk_freq_hz, |
Sakari Ailus | c859470 | 2014-09-15 18:35:18 -0300 | [diff] [blame] | 133 | "op_pix_clk_freq_hz"); |
| 134 | if (!rval) |
| 135 | rval = bounds_check( |
Sakari Ailus | e3f8bc8 | 2014-09-16 09:07:11 -0300 | [diff] [blame] | 136 | dev, pll->vt.sys_clk_freq_hz, |
Sakari Ailus | c859470 | 2014-09-15 18:35:18 -0300 | [diff] [blame] | 137 | limits->vt.min_sys_clk_freq_hz, |
| 138 | limits->vt.max_sys_clk_freq_hz, |
| 139 | "vt_sys_clk_freq_hz"); |
| 140 | if (!rval) |
| 141 | rval = bounds_check( |
Sakari Ailus | e3f8bc8 | 2014-09-16 09:07:11 -0300 | [diff] [blame] | 142 | dev, pll->vt.pix_clk_freq_hz, |
Sakari Ailus | c859470 | 2014-09-15 18:35:18 -0300 | [diff] [blame] | 143 | limits->vt.min_pix_clk_freq_hz, |
| 144 | limits->vt.max_pix_clk_freq_hz, |
| 145 | "vt_pix_clk_freq_hz"); |
| 146 | |
| 147 | return rval; |
| 148 | } |
| 149 | |
Sakari Ailus | 367da7a | 2013-08-10 14:49:46 -0300 | [diff] [blame] | 150 | /* |
| 151 | * Heuristically guess the PLL tree for a given common multiplier and |
| 152 | * divisor. Begin with the operational timing and continue to video |
| 153 | * timing once operational timing has been verified. |
| 154 | * |
| 155 | * @mul is the PLL multiplier and @div is the common divisor |
| 156 | * (pre_pll_clk_div and op_sys_clk_div combined). The final PLL |
| 157 | * multiplier will be a multiple of @mul. |
| 158 | * |
| 159 | * @return Zero on success, error code on error. |
| 160 | */ |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame^] | 161 | static int __smiapp_pll_calculate( |
| 162 | struct device *dev, const struct smiapp_pll_limits *limits, |
| 163 | const struct smiapp_pll_branch_limits *op_limits, |
| 164 | struct smiapp_pll *pll, struct smiapp_pll_branch *op_pll, uint32_t mul, |
| 165 | uint32_t div, uint32_t lane_op_clock_ratio) |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 166 | { |
| 167 | uint32_t sys_div; |
| 168 | uint32_t best_pix_div = INT_MAX >> 1; |
| 169 | uint32_t vt_op_binning_div; |
Sakari Ailus | 367da7a | 2013-08-10 14:49:46 -0300 | [diff] [blame] | 170 | /* |
| 171 | * Higher multipliers (and divisors) are often required than |
| 172 | * necessitated by the external clock and the output clocks. |
| 173 | * There are limits for all values in the clock tree. These |
| 174 | * are the minimum and maximum multiplier for mul. |
| 175 | */ |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 176 | uint32_t more_mul_min, more_mul_max; |
| 177 | uint32_t more_mul_factor; |
| 178 | uint32_t min_vt_div, max_vt_div, vt_div; |
| 179 | uint32_t min_sys_div, max_sys_div; |
| 180 | unsigned int i; |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 181 | |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 182 | /* |
| 183 | * Get pre_pll_clk_div so that our pll_op_clk_freq_hz won't be |
| 184 | * too high. |
| 185 | */ |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 186 | dev_dbg(dev, "pre_pll_clk_div %u\n", pll->pre_pll_clk_div); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 187 | |
| 188 | /* Don't go above max pll multiplier. */ |
| 189 | more_mul_max = limits->max_pll_multiplier / mul; |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 190 | dev_dbg(dev, "more_mul_max: max_pll_multiplier check: %u\n", |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 191 | more_mul_max); |
| 192 | /* Don't go above max pll op frequency. */ |
| 193 | more_mul_max = |
Sakari Ailus | c2ebca0 | 2012-10-20 09:08:22 -0300 | [diff] [blame] | 194 | min_t(uint32_t, |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 195 | more_mul_max, |
| 196 | limits->max_pll_op_freq_hz |
| 197 | / (pll->ext_clk_freq_hz / pll->pre_pll_clk_div * mul)); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 198 | dev_dbg(dev, "more_mul_max: max_pll_op_freq_hz check: %u\n", |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 199 | more_mul_max); |
| 200 | /* Don't go above the division capability of op sys clock divider. */ |
| 201 | more_mul_max = min(more_mul_max, |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame^] | 202 | op_limits->max_sys_clk_div * pll->pre_pll_clk_div |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 203 | / div); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 204 | dev_dbg(dev, "more_mul_max: max_op_sys_clk_div check: %u\n", |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 205 | more_mul_max); |
| 206 | /* Ensure we won't go above min_pll_multiplier. */ |
| 207 | more_mul_max = min(more_mul_max, |
| 208 | DIV_ROUND_UP(limits->max_pll_multiplier, mul)); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 209 | dev_dbg(dev, "more_mul_max: min_pll_multiplier check: %u\n", |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 210 | more_mul_max); |
| 211 | |
| 212 | /* Ensure we won't go below min_pll_op_freq_hz. */ |
| 213 | more_mul_min = DIV_ROUND_UP(limits->min_pll_op_freq_hz, |
| 214 | pll->ext_clk_freq_hz / pll->pre_pll_clk_div |
| 215 | * mul); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 216 | dev_dbg(dev, "more_mul_min: min_pll_op_freq_hz check: %u\n", |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 217 | more_mul_min); |
| 218 | /* Ensure we won't go below min_pll_multiplier. */ |
| 219 | more_mul_min = max(more_mul_min, |
| 220 | DIV_ROUND_UP(limits->min_pll_multiplier, mul)); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 221 | dev_dbg(dev, "more_mul_min: min_pll_multiplier check: %u\n", |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 222 | more_mul_min); |
| 223 | |
| 224 | if (more_mul_min > more_mul_max) { |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 225 | dev_dbg(dev, |
| 226 | "unable to compute more_mul_min and more_mul_max\n"); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 227 | return -EINVAL; |
| 228 | } |
| 229 | |
| 230 | more_mul_factor = lcm(div, pll->pre_pll_clk_div) / div; |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 231 | dev_dbg(dev, "more_mul_factor: %u\n", more_mul_factor); |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame^] | 232 | more_mul_factor = lcm(more_mul_factor, op_limits->min_sys_clk_div); |
| 233 | dev_dbg(dev, "more_mul_factor: min_op_sys_clk_div: %d\n", |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 234 | more_mul_factor); |
| 235 | i = roundup(more_mul_min, more_mul_factor); |
| 236 | if (!is_one_or_even(i)) |
| 237 | i <<= 1; |
| 238 | |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 239 | dev_dbg(dev, "final more_mul: %u\n", i); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 240 | if (i > more_mul_max) { |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 241 | dev_dbg(dev, "final more_mul is bad, max %u\n", more_mul_max); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 242 | return -EINVAL; |
| 243 | } |
| 244 | |
| 245 | pll->pll_multiplier = mul * i; |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame^] | 246 | op_pll->sys_clk_div = div * i / pll->pre_pll_clk_div; |
| 247 | dev_dbg(dev, "op_sys_clk_div: %u\n", op_pll->sys_clk_div); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 248 | |
| 249 | pll->pll_ip_clk_freq_hz = pll->ext_clk_freq_hz |
| 250 | / pll->pre_pll_clk_div; |
| 251 | |
| 252 | pll->pll_op_clk_freq_hz = pll->pll_ip_clk_freq_hz |
| 253 | * pll->pll_multiplier; |
| 254 | |
| 255 | /* Derive pll_op_clk_freq_hz. */ |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame^] | 256 | op_pll->sys_clk_freq_hz = |
| 257 | pll->pll_op_clk_freq_hz / op_pll->sys_clk_div; |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 258 | |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame^] | 259 | op_pll->pix_clk_div = pll->bits_per_pixel; |
| 260 | dev_dbg(dev, "op_pix_clk_div: %u\n", op_pll->pix_clk_div); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 261 | |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame^] | 262 | op_pll->pix_clk_freq_hz = |
| 263 | op_pll->sys_clk_freq_hz / op_pll->pix_clk_div; |
| 264 | |
| 265 | if (pll->flags & SMIAPP_PLL_FLAG_NO_OP_CLOCKS) { |
| 266 | /* No OP clocks --- VT clocks are used instead. */ |
| 267 | goto out_skip_vt_calc; |
| 268 | } |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 269 | |
| 270 | /* |
| 271 | * Some sensors perform analogue binning and some do this |
| 272 | * digitally. The ones doing this digitally can be roughly be |
| 273 | * found out using this formula. The ones doing this digitally |
| 274 | * should run at higher clock rate, so smaller divisor is used |
| 275 | * on video timing side. |
| 276 | */ |
| 277 | if (limits->min_line_length_pck_bin > limits->min_line_length_pck |
| 278 | / pll->binning_horizontal) |
| 279 | vt_op_binning_div = pll->binning_horizontal; |
| 280 | else |
| 281 | vt_op_binning_div = 1; |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 282 | dev_dbg(dev, "vt_op_binning_div: %u\n", vt_op_binning_div); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 283 | |
| 284 | /* |
| 285 | * Profile 2 supports vt_pix_clk_div E [4, 10] |
| 286 | * |
| 287 | * Horizontal binning can be used as a base for difference in |
| 288 | * divisors. One must make sure that horizontal blanking is |
| 289 | * enough to accommodate the CSI-2 sync codes. |
| 290 | * |
| 291 | * Take scaling factor into account as well. |
| 292 | * |
| 293 | * Find absolute limits for the factor of vt divider. |
| 294 | */ |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 295 | dev_dbg(dev, "scale_m: %u\n", pll->scale_m); |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame^] | 296 | min_vt_div = DIV_ROUND_UP(op_pll->pix_clk_div * op_pll->sys_clk_div |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 297 | * pll->scale_n, |
| 298 | lane_op_clock_ratio * vt_op_binning_div |
| 299 | * pll->scale_m); |
| 300 | |
| 301 | /* Find smallest and biggest allowed vt divisor. */ |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 302 | dev_dbg(dev, "min_vt_div: %u\n", min_vt_div); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 303 | min_vt_div = max(min_vt_div, |
| 304 | DIV_ROUND_UP(pll->pll_op_clk_freq_hz, |
Laurent Pinchart | 6ec84a2 | 2012-10-22 11:40:56 -0300 | [diff] [blame] | 305 | limits->vt.max_pix_clk_freq_hz)); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 306 | dev_dbg(dev, "min_vt_div: max_vt_pix_clk_freq_hz: %u\n", |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 307 | min_vt_div); |
| 308 | min_vt_div = max_t(uint32_t, min_vt_div, |
Laurent Pinchart | 6ec84a2 | 2012-10-22 11:40:56 -0300 | [diff] [blame] | 309 | limits->vt.min_pix_clk_div |
| 310 | * limits->vt.min_sys_clk_div); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 311 | dev_dbg(dev, "min_vt_div: min_vt_clk_div: %u\n", min_vt_div); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 312 | |
Laurent Pinchart | 6ec84a2 | 2012-10-22 11:40:56 -0300 | [diff] [blame] | 313 | max_vt_div = limits->vt.max_sys_clk_div * limits->vt.max_pix_clk_div; |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 314 | dev_dbg(dev, "max_vt_div: %u\n", max_vt_div); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 315 | max_vt_div = min(max_vt_div, |
| 316 | DIV_ROUND_UP(pll->pll_op_clk_freq_hz, |
Laurent Pinchart | 6ec84a2 | 2012-10-22 11:40:56 -0300 | [diff] [blame] | 317 | limits->vt.min_pix_clk_freq_hz)); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 318 | dev_dbg(dev, "max_vt_div: min_vt_pix_clk_freq_hz: %u\n", |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 319 | max_vt_div); |
| 320 | |
| 321 | /* |
| 322 | * Find limitsits for sys_clk_div. Not all values are possible |
| 323 | * with all values of pix_clk_div. |
| 324 | */ |
Laurent Pinchart | 6ec84a2 | 2012-10-22 11:40:56 -0300 | [diff] [blame] | 325 | min_sys_div = limits->vt.min_sys_clk_div; |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 326 | dev_dbg(dev, "min_sys_div: %u\n", min_sys_div); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 327 | min_sys_div = max(min_sys_div, |
| 328 | DIV_ROUND_UP(min_vt_div, |
Laurent Pinchart | 6ec84a2 | 2012-10-22 11:40:56 -0300 | [diff] [blame] | 329 | limits->vt.max_pix_clk_div)); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 330 | dev_dbg(dev, "min_sys_div: max_vt_pix_clk_div: %u\n", min_sys_div); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 331 | min_sys_div = max(min_sys_div, |
| 332 | pll->pll_op_clk_freq_hz |
Laurent Pinchart | 6ec84a2 | 2012-10-22 11:40:56 -0300 | [diff] [blame] | 333 | / limits->vt.max_sys_clk_freq_hz); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 334 | dev_dbg(dev, "min_sys_div: max_pll_op_clk_freq_hz: %u\n", min_sys_div); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 335 | min_sys_div = clk_div_even_up(min_sys_div); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 336 | dev_dbg(dev, "min_sys_div: one or even: %u\n", min_sys_div); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 337 | |
Laurent Pinchart | 6ec84a2 | 2012-10-22 11:40:56 -0300 | [diff] [blame] | 338 | max_sys_div = limits->vt.max_sys_clk_div; |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 339 | dev_dbg(dev, "max_sys_div: %u\n", max_sys_div); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 340 | max_sys_div = min(max_sys_div, |
| 341 | DIV_ROUND_UP(max_vt_div, |
Laurent Pinchart | 6ec84a2 | 2012-10-22 11:40:56 -0300 | [diff] [blame] | 342 | limits->vt.min_pix_clk_div)); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 343 | dev_dbg(dev, "max_sys_div: min_vt_pix_clk_div: %u\n", max_sys_div); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 344 | max_sys_div = min(max_sys_div, |
| 345 | DIV_ROUND_UP(pll->pll_op_clk_freq_hz, |
Laurent Pinchart | 6ec84a2 | 2012-10-22 11:40:56 -0300 | [diff] [blame] | 346 | limits->vt.min_pix_clk_freq_hz)); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 347 | dev_dbg(dev, "max_sys_div: min_vt_pix_clk_freq_hz: %u\n", max_sys_div); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 348 | |
| 349 | /* |
| 350 | * Find pix_div such that a legal pix_div * sys_div results |
| 351 | * into a value which is not smaller than div, the desired |
| 352 | * divisor. |
| 353 | */ |
| 354 | for (vt_div = min_vt_div; vt_div <= max_vt_div; |
| 355 | vt_div += 2 - (vt_div & 1)) { |
| 356 | for (sys_div = min_sys_div; |
| 357 | sys_div <= max_sys_div; |
| 358 | sys_div += 2 - (sys_div & 1)) { |
Sakari Ailus | c2ebca0 | 2012-10-20 09:08:22 -0300 | [diff] [blame] | 359 | uint16_t pix_div = DIV_ROUND_UP(vt_div, sys_div); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 360 | |
Laurent Pinchart | 6ec84a2 | 2012-10-22 11:40:56 -0300 | [diff] [blame] | 361 | if (pix_div < limits->vt.min_pix_clk_div |
| 362 | || pix_div > limits->vt.max_pix_clk_div) { |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 363 | dev_dbg(dev, |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 364 | "pix_div %u too small or too big (%u--%u)\n", |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 365 | pix_div, |
Laurent Pinchart | 6ec84a2 | 2012-10-22 11:40:56 -0300 | [diff] [blame] | 366 | limits->vt.min_pix_clk_div, |
| 367 | limits->vt.max_pix_clk_div); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 368 | continue; |
| 369 | } |
| 370 | |
| 371 | /* Check if this one is better. */ |
| 372 | if (pix_div * sys_div |
| 373 | <= roundup(min_vt_div, best_pix_div)) |
| 374 | best_pix_div = pix_div; |
| 375 | } |
| 376 | if (best_pix_div < INT_MAX >> 1) |
| 377 | break; |
| 378 | } |
| 379 | |
Sakari Ailus | e3f8bc8 | 2014-09-16 09:07:11 -0300 | [diff] [blame] | 380 | pll->vt.sys_clk_div = DIV_ROUND_UP(min_vt_div, best_pix_div); |
| 381 | pll->vt.pix_clk_div = best_pix_div; |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 382 | |
Sakari Ailus | e3f8bc8 | 2014-09-16 09:07:11 -0300 | [diff] [blame] | 383 | pll->vt.sys_clk_freq_hz = |
| 384 | pll->pll_op_clk_freq_hz / pll->vt.sys_clk_div; |
| 385 | pll->vt.pix_clk_freq_hz = |
| 386 | pll->vt.sys_clk_freq_hz / pll->vt.pix_clk_div; |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 387 | |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame^] | 388 | out_skip_vt_calc: |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 389 | pll->pixel_rate_csi = |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame^] | 390 | op_pll->pix_clk_freq_hz * lane_op_clock_ratio; |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 391 | |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame^] | 392 | return check_all_bounds(dev, limits, op_limits, pll, op_pll); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 393 | } |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 394 | |
Laurent Pinchart | 8f7e91a | 2012-10-22 11:40:57 -0300 | [diff] [blame] | 395 | int smiapp_pll_calculate(struct device *dev, |
| 396 | const struct smiapp_pll_limits *limits, |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 397 | struct smiapp_pll *pll) |
| 398 | { |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame^] | 399 | const struct smiapp_pll_branch_limits *op_limits = &limits->op; |
| 400 | struct smiapp_pll_branch *op_pll = &pll->op; |
Laurent Pinchart | 8f7e91a | 2012-10-22 11:40:57 -0300 | [diff] [blame] | 401 | uint16_t min_pre_pll_clk_div; |
| 402 | uint16_t max_pre_pll_clk_div; |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 403 | uint32_t lane_op_clock_ratio; |
| 404 | uint32_t mul, div; |
| 405 | unsigned int i; |
| 406 | int rval = -EINVAL; |
| 407 | |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame^] | 408 | if (pll->flags & SMIAPP_PLL_FLAG_NO_OP_CLOCKS) { |
| 409 | /* |
| 410 | * If there's no OP PLL at all, use the VT values |
| 411 | * instead. The OP values are ignored for the rest of |
| 412 | * the PLL calculation. |
| 413 | */ |
| 414 | op_limits = &limits->vt; |
| 415 | op_pll = &pll->vt; |
| 416 | } |
| 417 | |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 418 | if (pll->flags & SMIAPP_PLL_FLAG_OP_PIX_CLOCK_PER_LANE) |
Sakari Ailus | f5984bb | 2012-10-20 10:35:25 -0300 | [diff] [blame] | 419 | lane_op_clock_ratio = pll->csi2.lanes; |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 420 | else |
| 421 | lane_op_clock_ratio = 1; |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 422 | dev_dbg(dev, "lane_op_clock_ratio: %u\n", lane_op_clock_ratio); |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 423 | |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 424 | dev_dbg(dev, "binning: %ux%u\n", pll->binning_horizontal, |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 425 | pll->binning_vertical); |
| 426 | |
Sakari Ailus | f5984bb | 2012-10-20 10:35:25 -0300 | [diff] [blame] | 427 | switch (pll->bus_type) { |
| 428 | case SMIAPP_PLL_BUS_TYPE_CSI2: |
| 429 | /* CSI transfers 2 bits per clock per lane; thus times 2 */ |
| 430 | pll->pll_op_clk_freq_hz = pll->link_freq * 2 |
| 431 | * (pll->csi2.lanes / lane_op_clock_ratio); |
| 432 | break; |
| 433 | case SMIAPP_PLL_BUS_TYPE_PARALLEL: |
| 434 | pll->pll_op_clk_freq_hz = pll->link_freq * pll->bits_per_pixel |
| 435 | / DIV_ROUND_UP(pll->bits_per_pixel, |
| 436 | pll->parallel.bus_width); |
| 437 | break; |
| 438 | default: |
| 439 | return -EINVAL; |
| 440 | } |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 441 | |
| 442 | /* Figure out limits for pre-pll divider based on extclk */ |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 443 | dev_dbg(dev, "min / max pre_pll_clk_div: %u / %u\n", |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 444 | limits->min_pre_pll_clk_div, limits->max_pre_pll_clk_div); |
Laurent Pinchart | 8f7e91a | 2012-10-22 11:40:57 -0300 | [diff] [blame] | 445 | max_pre_pll_clk_div = |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 446 | min_t(uint16_t, limits->max_pre_pll_clk_div, |
| 447 | clk_div_even(pll->ext_clk_freq_hz / |
| 448 | limits->min_pll_ip_freq_hz)); |
Laurent Pinchart | 8f7e91a | 2012-10-22 11:40:57 -0300 | [diff] [blame] | 449 | min_pre_pll_clk_div = |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 450 | max_t(uint16_t, limits->min_pre_pll_clk_div, |
| 451 | clk_div_even_up( |
| 452 | DIV_ROUND_UP(pll->ext_clk_freq_hz, |
| 453 | limits->max_pll_ip_freq_hz))); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 454 | dev_dbg(dev, "pre-pll check: min / max pre_pll_clk_div: %u / %u\n", |
Laurent Pinchart | 8f7e91a | 2012-10-22 11:40:57 -0300 | [diff] [blame] | 455 | min_pre_pll_clk_div, max_pre_pll_clk_div); |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 456 | |
| 457 | i = gcd(pll->pll_op_clk_freq_hz, pll->ext_clk_freq_hz); |
| 458 | mul = div_u64(pll->pll_op_clk_freq_hz, i); |
| 459 | div = pll->ext_clk_freq_hz / i; |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 460 | dev_dbg(dev, "mul %u / div %u\n", mul, div); |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 461 | |
Laurent Pinchart | 8f7e91a | 2012-10-22 11:40:57 -0300 | [diff] [blame] | 462 | min_pre_pll_clk_div = |
| 463 | max_t(uint16_t, min_pre_pll_clk_div, |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 464 | clk_div_even_up( |
| 465 | DIV_ROUND_UP(mul * pll->ext_clk_freq_hz, |
| 466 | limits->max_pll_op_freq_hz))); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 467 | dev_dbg(dev, "pll_op check: min / max pre_pll_clk_div: %u / %u\n", |
Laurent Pinchart | 8f7e91a | 2012-10-22 11:40:57 -0300 | [diff] [blame] | 468 | min_pre_pll_clk_div, max_pre_pll_clk_div); |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 469 | |
Laurent Pinchart | 8f7e91a | 2012-10-22 11:40:57 -0300 | [diff] [blame] | 470 | for (pll->pre_pll_clk_div = min_pre_pll_clk_div; |
| 471 | pll->pre_pll_clk_div <= max_pre_pll_clk_div; |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 472 | pll->pre_pll_clk_div += 2 - (pll->pre_pll_clk_div & 1)) { |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame^] | 473 | rval = __smiapp_pll_calculate(dev, limits, op_limits, pll, |
| 474 | op_pll, mul, div, |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 475 | lane_op_clock_ratio); |
| 476 | if (rval) |
| 477 | continue; |
| 478 | |
| 479 | print_pll(dev, pll); |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | dev_info(dev, "unable to compute pre_pll divisor\n"); |
| 484 | return rval; |
| 485 | } |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 486 | EXPORT_SYMBOL_GPL(smiapp_pll_calculate); |
| 487 | |
Sakari Ailus | 8c5dff9 | 2012-10-28 06:44:17 -0300 | [diff] [blame] | 488 | MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>"); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 489 | MODULE_DESCRIPTION("Generic SMIA/SMIA++ PLL calculator"); |
| 490 | MODULE_LICENSE("GPL"); |