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->sys_clk_freq_hz, |
| 120 | op_limits->min_sys_clk_freq_hz, |
| 121 | op_limits->max_sys_clk_freq_hz, |
Sakari Ailus | c859470 | 2014-09-15 18:35:18 -0300 | [diff] [blame] | 122 | "op_sys_clk_freq_hz"); |
| 123 | if (!rval) |
| 124 | rval = bounds_check( |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame] | 125 | dev, op_pll->pix_clk_freq_hz, |
| 126 | op_limits->min_pix_clk_freq_hz, |
| 127 | op_limits->max_pix_clk_freq_hz, |
Sakari Ailus | c859470 | 2014-09-15 18:35:18 -0300 | [diff] [blame] | 128 | "op_pix_clk_freq_hz"); |
Sakari Ailus | 63516b5 | 2014-09-15 18:47:32 -0300 | [diff] [blame] | 129 | |
| 130 | /* |
| 131 | * If there are no OP clocks, the VT clocks are contained in |
| 132 | * the OP clock struct. |
| 133 | */ |
| 134 | if (pll->flags & SMIAPP_PLL_FLAG_NO_OP_CLOCKS) |
| 135 | return rval; |
| 136 | |
Sakari Ailus | c859470 | 2014-09-15 18:35:18 -0300 | [diff] [blame] | 137 | if (!rval) |
| 138 | rval = bounds_check( |
Sakari Ailus | e3f8bc8 | 2014-09-16 09:07:11 -0300 | [diff] [blame] | 139 | dev, pll->vt.sys_clk_freq_hz, |
Sakari Ailus | c859470 | 2014-09-15 18:35:18 -0300 | [diff] [blame] | 140 | limits->vt.min_sys_clk_freq_hz, |
| 141 | limits->vt.max_sys_clk_freq_hz, |
| 142 | "vt_sys_clk_freq_hz"); |
| 143 | if (!rval) |
| 144 | rval = bounds_check( |
Sakari Ailus | e3f8bc8 | 2014-09-16 09:07:11 -0300 | [diff] [blame] | 145 | dev, pll->vt.pix_clk_freq_hz, |
Sakari Ailus | c859470 | 2014-09-15 18:35:18 -0300 | [diff] [blame] | 146 | limits->vt.min_pix_clk_freq_hz, |
| 147 | limits->vt.max_pix_clk_freq_hz, |
| 148 | "vt_pix_clk_freq_hz"); |
| 149 | |
| 150 | return rval; |
| 151 | } |
| 152 | |
Sakari Ailus | 367da7a | 2013-08-10 14:49:46 -0300 | [diff] [blame] | 153 | /* |
| 154 | * Heuristically guess the PLL tree for a given common multiplier and |
| 155 | * divisor. Begin with the operational timing and continue to video |
| 156 | * timing once operational timing has been verified. |
| 157 | * |
| 158 | * @mul is the PLL multiplier and @div is the common divisor |
| 159 | * (pre_pll_clk_div and op_sys_clk_div combined). The final PLL |
| 160 | * multiplier will be a multiple of @mul. |
| 161 | * |
| 162 | * @return Zero on success, error code on error. |
| 163 | */ |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame] | 164 | static int __smiapp_pll_calculate( |
| 165 | struct device *dev, const struct smiapp_pll_limits *limits, |
| 166 | const struct smiapp_pll_branch_limits *op_limits, |
| 167 | struct smiapp_pll *pll, struct smiapp_pll_branch *op_pll, uint32_t mul, |
| 168 | uint32_t div, uint32_t lane_op_clock_ratio) |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 169 | { |
| 170 | uint32_t sys_div; |
| 171 | uint32_t best_pix_div = INT_MAX >> 1; |
| 172 | uint32_t vt_op_binning_div; |
Sakari Ailus | 367da7a | 2013-08-10 14:49:46 -0300 | [diff] [blame] | 173 | /* |
| 174 | * Higher multipliers (and divisors) are often required than |
| 175 | * necessitated by the external clock and the output clocks. |
| 176 | * There are limits for all values in the clock tree. These |
| 177 | * are the minimum and maximum multiplier for mul. |
| 178 | */ |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 179 | uint32_t more_mul_min, more_mul_max; |
| 180 | uint32_t more_mul_factor; |
| 181 | uint32_t min_vt_div, max_vt_div, vt_div; |
| 182 | uint32_t min_sys_div, max_sys_div; |
| 183 | unsigned int i; |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 184 | |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 185 | /* |
| 186 | * Get pre_pll_clk_div so that our pll_op_clk_freq_hz won't be |
| 187 | * too high. |
| 188 | */ |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 189 | 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] | 190 | |
| 191 | /* Don't go above max pll multiplier. */ |
| 192 | more_mul_max = limits->max_pll_multiplier / mul; |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 193 | dev_dbg(dev, "more_mul_max: max_pll_multiplier check: %u\n", |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 194 | more_mul_max); |
| 195 | /* Don't go above max pll op frequency. */ |
| 196 | more_mul_max = |
Sakari Ailus | c2ebca0 | 2012-10-20 09:08:22 -0300 | [diff] [blame] | 197 | min_t(uint32_t, |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 198 | more_mul_max, |
| 199 | limits->max_pll_op_freq_hz |
| 200 | / (pll->ext_clk_freq_hz / pll->pre_pll_clk_div * mul)); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 201 | 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] | 202 | more_mul_max); |
| 203 | /* Don't go above the division capability of op sys clock divider. */ |
| 204 | more_mul_max = min(more_mul_max, |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame] | 205 | op_limits->max_sys_clk_div * pll->pre_pll_clk_div |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 206 | / div); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 207 | 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] | 208 | more_mul_max); |
| 209 | /* Ensure we won't go above min_pll_multiplier. */ |
| 210 | more_mul_max = min(more_mul_max, |
| 211 | DIV_ROUND_UP(limits->max_pll_multiplier, mul)); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 212 | dev_dbg(dev, "more_mul_max: min_pll_multiplier check: %u\n", |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 213 | more_mul_max); |
| 214 | |
| 215 | /* Ensure we won't go below min_pll_op_freq_hz. */ |
| 216 | more_mul_min = DIV_ROUND_UP(limits->min_pll_op_freq_hz, |
| 217 | pll->ext_clk_freq_hz / pll->pre_pll_clk_div |
| 218 | * mul); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 219 | 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] | 220 | more_mul_min); |
| 221 | /* Ensure we won't go below min_pll_multiplier. */ |
| 222 | more_mul_min = max(more_mul_min, |
| 223 | DIV_ROUND_UP(limits->min_pll_multiplier, mul)); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 224 | dev_dbg(dev, "more_mul_min: min_pll_multiplier check: %u\n", |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 225 | more_mul_min); |
| 226 | |
| 227 | if (more_mul_min > more_mul_max) { |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 228 | dev_dbg(dev, |
| 229 | "unable to compute more_mul_min and more_mul_max\n"); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 230 | return -EINVAL; |
| 231 | } |
| 232 | |
| 233 | more_mul_factor = lcm(div, pll->pre_pll_clk_div) / div; |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 234 | dev_dbg(dev, "more_mul_factor: %u\n", more_mul_factor); |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame] | 235 | more_mul_factor = lcm(more_mul_factor, op_limits->min_sys_clk_div); |
| 236 | 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] | 237 | more_mul_factor); |
| 238 | i = roundup(more_mul_min, more_mul_factor); |
| 239 | if (!is_one_or_even(i)) |
| 240 | i <<= 1; |
| 241 | |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 242 | dev_dbg(dev, "final more_mul: %u\n", i); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 243 | if (i > more_mul_max) { |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 244 | 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] | 245 | return -EINVAL; |
| 246 | } |
| 247 | |
| 248 | pll->pll_multiplier = mul * i; |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame] | 249 | op_pll->sys_clk_div = div * i / pll->pre_pll_clk_div; |
| 250 | 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] | 251 | |
| 252 | pll->pll_ip_clk_freq_hz = pll->ext_clk_freq_hz |
| 253 | / pll->pre_pll_clk_div; |
| 254 | |
| 255 | pll->pll_op_clk_freq_hz = pll->pll_ip_clk_freq_hz |
| 256 | * pll->pll_multiplier; |
| 257 | |
| 258 | /* Derive pll_op_clk_freq_hz. */ |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame] | 259 | op_pll->sys_clk_freq_hz = |
| 260 | pll->pll_op_clk_freq_hz / op_pll->sys_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_div = pll->bits_per_pixel; |
| 263 | 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] | 264 | |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame] | 265 | op_pll->pix_clk_freq_hz = |
| 266 | op_pll->sys_clk_freq_hz / op_pll->pix_clk_div; |
| 267 | |
| 268 | if (pll->flags & SMIAPP_PLL_FLAG_NO_OP_CLOCKS) { |
| 269 | /* No OP clocks --- VT clocks are used instead. */ |
| 270 | goto out_skip_vt_calc; |
| 271 | } |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 272 | |
| 273 | /* |
| 274 | * Some sensors perform analogue binning and some do this |
| 275 | * digitally. The ones doing this digitally can be roughly be |
| 276 | * found out using this formula. The ones doing this digitally |
| 277 | * should run at higher clock rate, so smaller divisor is used |
| 278 | * on video timing side. |
| 279 | */ |
| 280 | if (limits->min_line_length_pck_bin > limits->min_line_length_pck |
| 281 | / pll->binning_horizontal) |
| 282 | vt_op_binning_div = pll->binning_horizontal; |
| 283 | else |
| 284 | vt_op_binning_div = 1; |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 285 | 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] | 286 | |
| 287 | /* |
| 288 | * Profile 2 supports vt_pix_clk_div E [4, 10] |
| 289 | * |
| 290 | * Horizontal binning can be used as a base for difference in |
| 291 | * divisors. One must make sure that horizontal blanking is |
| 292 | * enough to accommodate the CSI-2 sync codes. |
| 293 | * |
| 294 | * Take scaling factor into account as well. |
| 295 | * |
| 296 | * Find absolute limits for the factor of vt divider. |
| 297 | */ |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 298 | dev_dbg(dev, "scale_m: %u\n", pll->scale_m); |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame] | 299 | 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] | 300 | * pll->scale_n, |
| 301 | lane_op_clock_ratio * vt_op_binning_div |
| 302 | * pll->scale_m); |
| 303 | |
| 304 | /* Find smallest and biggest allowed vt divisor. */ |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 305 | dev_dbg(dev, "min_vt_div: %u\n", min_vt_div); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 306 | min_vt_div = max(min_vt_div, |
| 307 | DIV_ROUND_UP(pll->pll_op_clk_freq_hz, |
Laurent Pinchart | 6ec84a2 | 2012-10-22 11:40:56 -0300 | [diff] [blame] | 308 | limits->vt.max_pix_clk_freq_hz)); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 309 | 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] | 310 | min_vt_div); |
| 311 | min_vt_div = max_t(uint32_t, min_vt_div, |
Laurent Pinchart | 6ec84a2 | 2012-10-22 11:40:56 -0300 | [diff] [blame] | 312 | limits->vt.min_pix_clk_div |
| 313 | * limits->vt.min_sys_clk_div); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 314 | 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] | 315 | |
Laurent Pinchart | 6ec84a2 | 2012-10-22 11:40:56 -0300 | [diff] [blame] | 316 | 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] | 317 | dev_dbg(dev, "max_vt_div: %u\n", max_vt_div); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 318 | max_vt_div = min(max_vt_div, |
| 319 | DIV_ROUND_UP(pll->pll_op_clk_freq_hz, |
Laurent Pinchart | 6ec84a2 | 2012-10-22 11:40:56 -0300 | [diff] [blame] | 320 | limits->vt.min_pix_clk_freq_hz)); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 321 | 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] | 322 | max_vt_div); |
| 323 | |
| 324 | /* |
| 325 | * Find limitsits for sys_clk_div. Not all values are possible |
| 326 | * with all values of pix_clk_div. |
| 327 | */ |
Laurent Pinchart | 6ec84a2 | 2012-10-22 11:40:56 -0300 | [diff] [blame] | 328 | min_sys_div = limits->vt.min_sys_clk_div; |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 329 | dev_dbg(dev, "min_sys_div: %u\n", min_sys_div); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 330 | min_sys_div = max(min_sys_div, |
| 331 | DIV_ROUND_UP(min_vt_div, |
Laurent Pinchart | 6ec84a2 | 2012-10-22 11:40:56 -0300 | [diff] [blame] | 332 | limits->vt.max_pix_clk_div)); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 333 | 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] | 334 | min_sys_div = max(min_sys_div, |
| 335 | pll->pll_op_clk_freq_hz |
Laurent Pinchart | 6ec84a2 | 2012-10-22 11:40:56 -0300 | [diff] [blame] | 336 | / limits->vt.max_sys_clk_freq_hz); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 337 | 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] | 338 | min_sys_div = clk_div_even_up(min_sys_div); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 339 | 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] | 340 | |
Laurent Pinchart | 6ec84a2 | 2012-10-22 11:40:56 -0300 | [diff] [blame] | 341 | max_sys_div = limits->vt.max_sys_clk_div; |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 342 | dev_dbg(dev, "max_sys_div: %u\n", max_sys_div); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 343 | max_sys_div = min(max_sys_div, |
| 344 | DIV_ROUND_UP(max_vt_div, |
Laurent Pinchart | 6ec84a2 | 2012-10-22 11:40:56 -0300 | [diff] [blame] | 345 | limits->vt.min_pix_clk_div)); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 346 | 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] | 347 | max_sys_div = min(max_sys_div, |
| 348 | DIV_ROUND_UP(pll->pll_op_clk_freq_hz, |
Laurent Pinchart | 6ec84a2 | 2012-10-22 11:40:56 -0300 | [diff] [blame] | 349 | limits->vt.min_pix_clk_freq_hz)); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 350 | 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] | 351 | |
| 352 | /* |
| 353 | * Find pix_div such that a legal pix_div * sys_div results |
| 354 | * into a value which is not smaller than div, the desired |
| 355 | * divisor. |
| 356 | */ |
| 357 | for (vt_div = min_vt_div; vt_div <= max_vt_div; |
| 358 | vt_div += 2 - (vt_div & 1)) { |
| 359 | for (sys_div = min_sys_div; |
| 360 | sys_div <= max_sys_div; |
| 361 | sys_div += 2 - (sys_div & 1)) { |
Sakari Ailus | c2ebca0 | 2012-10-20 09:08:22 -0300 | [diff] [blame] | 362 | uint16_t pix_div = DIV_ROUND_UP(vt_div, sys_div); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 363 | |
Laurent Pinchart | 6ec84a2 | 2012-10-22 11:40:56 -0300 | [diff] [blame] | 364 | if (pix_div < limits->vt.min_pix_clk_div |
| 365 | || pix_div > limits->vt.max_pix_clk_div) { |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 366 | dev_dbg(dev, |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 367 | "pix_div %u too small or too big (%u--%u)\n", |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 368 | pix_div, |
Laurent Pinchart | 6ec84a2 | 2012-10-22 11:40:56 -0300 | [diff] [blame] | 369 | limits->vt.min_pix_clk_div, |
| 370 | limits->vt.max_pix_clk_div); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 371 | continue; |
| 372 | } |
| 373 | |
| 374 | /* Check if this one is better. */ |
| 375 | if (pix_div * sys_div |
| 376 | <= roundup(min_vt_div, best_pix_div)) |
| 377 | best_pix_div = pix_div; |
| 378 | } |
| 379 | if (best_pix_div < INT_MAX >> 1) |
| 380 | break; |
| 381 | } |
| 382 | |
Sakari Ailus | e3f8bc8 | 2014-09-16 09:07:11 -0300 | [diff] [blame] | 383 | pll->vt.sys_clk_div = DIV_ROUND_UP(min_vt_div, best_pix_div); |
| 384 | pll->vt.pix_clk_div = best_pix_div; |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 385 | |
Sakari Ailus | e3f8bc8 | 2014-09-16 09:07:11 -0300 | [diff] [blame] | 386 | pll->vt.sys_clk_freq_hz = |
| 387 | pll->pll_op_clk_freq_hz / pll->vt.sys_clk_div; |
| 388 | pll->vt.pix_clk_freq_hz = |
| 389 | pll->vt.sys_clk_freq_hz / pll->vt.pix_clk_div; |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 390 | |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame] | 391 | out_skip_vt_calc: |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 392 | pll->pixel_rate_csi = |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame] | 393 | op_pll->pix_clk_freq_hz * lane_op_clock_ratio; |
Sakari Ailus | e7c329a | 2014-04-01 19:18:09 -0300 | [diff] [blame^] | 394 | pll->pixel_rate_pixel_array = pll->vt.pix_clk_freq_hz; |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 395 | |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame] | 396 | return check_all_bounds(dev, limits, op_limits, pll, op_pll); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 397 | } |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 398 | |
Laurent Pinchart | 8f7e91a | 2012-10-22 11:40:57 -0300 | [diff] [blame] | 399 | int smiapp_pll_calculate(struct device *dev, |
| 400 | const struct smiapp_pll_limits *limits, |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 401 | struct smiapp_pll *pll) |
| 402 | { |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame] | 403 | const struct smiapp_pll_branch_limits *op_limits = &limits->op; |
| 404 | struct smiapp_pll_branch *op_pll = &pll->op; |
Laurent Pinchart | 8f7e91a | 2012-10-22 11:40:57 -0300 | [diff] [blame] | 405 | uint16_t min_pre_pll_clk_div; |
| 406 | uint16_t max_pre_pll_clk_div; |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 407 | uint32_t lane_op_clock_ratio; |
| 408 | uint32_t mul, div; |
| 409 | unsigned int i; |
| 410 | int rval = -EINVAL; |
| 411 | |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame] | 412 | if (pll->flags & SMIAPP_PLL_FLAG_NO_OP_CLOCKS) { |
| 413 | /* |
| 414 | * If there's no OP PLL at all, use the VT values |
| 415 | * instead. The OP values are ignored for the rest of |
| 416 | * the PLL calculation. |
| 417 | */ |
| 418 | op_limits = &limits->vt; |
| 419 | op_pll = &pll->vt; |
| 420 | } |
| 421 | |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 422 | if (pll->flags & SMIAPP_PLL_FLAG_OP_PIX_CLOCK_PER_LANE) |
Sakari Ailus | f5984bb | 2012-10-20 10:35:25 -0300 | [diff] [blame] | 423 | lane_op_clock_ratio = pll->csi2.lanes; |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 424 | else |
| 425 | lane_op_clock_ratio = 1; |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 426 | 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] | 427 | |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 428 | dev_dbg(dev, "binning: %ux%u\n", pll->binning_horizontal, |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 429 | pll->binning_vertical); |
| 430 | |
Sakari Ailus | f5984bb | 2012-10-20 10:35:25 -0300 | [diff] [blame] | 431 | switch (pll->bus_type) { |
| 432 | case SMIAPP_PLL_BUS_TYPE_CSI2: |
| 433 | /* CSI transfers 2 bits per clock per lane; thus times 2 */ |
| 434 | pll->pll_op_clk_freq_hz = pll->link_freq * 2 |
| 435 | * (pll->csi2.lanes / lane_op_clock_ratio); |
| 436 | break; |
| 437 | case SMIAPP_PLL_BUS_TYPE_PARALLEL: |
| 438 | pll->pll_op_clk_freq_hz = pll->link_freq * pll->bits_per_pixel |
| 439 | / DIV_ROUND_UP(pll->bits_per_pixel, |
| 440 | pll->parallel.bus_width); |
| 441 | break; |
| 442 | default: |
| 443 | return -EINVAL; |
| 444 | } |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 445 | |
| 446 | /* Figure out limits for pre-pll divider based on extclk */ |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 447 | dev_dbg(dev, "min / max pre_pll_clk_div: %u / %u\n", |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 448 | limits->min_pre_pll_clk_div, limits->max_pre_pll_clk_div); |
Laurent Pinchart | 8f7e91a | 2012-10-22 11:40:57 -0300 | [diff] [blame] | 449 | max_pre_pll_clk_div = |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 450 | min_t(uint16_t, limits->max_pre_pll_clk_div, |
| 451 | clk_div_even(pll->ext_clk_freq_hz / |
| 452 | limits->min_pll_ip_freq_hz)); |
Laurent Pinchart | 8f7e91a | 2012-10-22 11:40:57 -0300 | [diff] [blame] | 453 | min_pre_pll_clk_div = |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 454 | max_t(uint16_t, limits->min_pre_pll_clk_div, |
| 455 | clk_div_even_up( |
| 456 | DIV_ROUND_UP(pll->ext_clk_freq_hz, |
| 457 | limits->max_pll_ip_freq_hz))); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 458 | 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] | 459 | min_pre_pll_clk_div, max_pre_pll_clk_div); |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 460 | |
| 461 | i = gcd(pll->pll_op_clk_freq_hz, pll->ext_clk_freq_hz); |
| 462 | mul = div_u64(pll->pll_op_clk_freq_hz, i); |
| 463 | div = pll->ext_clk_freq_hz / i; |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 464 | dev_dbg(dev, "mul %u / div %u\n", mul, div); |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 465 | |
Laurent Pinchart | 8f7e91a | 2012-10-22 11:40:57 -0300 | [diff] [blame] | 466 | min_pre_pll_clk_div = |
| 467 | max_t(uint16_t, min_pre_pll_clk_div, |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 468 | clk_div_even_up( |
| 469 | DIV_ROUND_UP(mul * pll->ext_clk_freq_hz, |
| 470 | limits->max_pll_op_freq_hz))); |
Sakari Ailus | c37f9bf | 2014-04-01 10:31:59 -0300 | [diff] [blame] | 471 | 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] | 472 | min_pre_pll_clk_div, max_pre_pll_clk_div); |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 473 | |
Laurent Pinchart | 8f7e91a | 2012-10-22 11:40:57 -0300 | [diff] [blame] | 474 | for (pll->pre_pll_clk_div = min_pre_pll_clk_div; |
| 475 | pll->pre_pll_clk_div <= max_pre_pll_clk_div; |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 476 | pll->pre_pll_clk_div += 2 - (pll->pre_pll_clk_div & 1)) { |
Sakari Ailus | 974abe4 | 2014-09-16 09:39:08 -0300 | [diff] [blame] | 477 | rval = __smiapp_pll_calculate(dev, limits, op_limits, pll, |
| 478 | op_pll, mul, div, |
Sakari Ailus | 6de1b14 | 2012-10-22 16:27:27 -0300 | [diff] [blame] | 479 | lane_op_clock_ratio); |
| 480 | if (rval) |
| 481 | continue; |
| 482 | |
| 483 | print_pll(dev, pll); |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | dev_info(dev, "unable to compute pre_pll divisor\n"); |
| 488 | return rval; |
| 489 | } |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 490 | EXPORT_SYMBOL_GPL(smiapp_pll_calculate); |
| 491 | |
Sakari Ailus | 8c5dff9 | 2012-10-28 06:44:17 -0300 | [diff] [blame] | 492 | MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>"); |
Sakari Ailus | cf1c5fa | 2011-12-07 13:45:25 -0300 | [diff] [blame] | 493 | MODULE_DESCRIPTION("Generic SMIA/SMIA++ PLL calculator"); |
| 494 | MODULE_LICENSE("GPL"); |