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