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