2 * drivers/media/i2c/smiapp-pll.c
4 * Generic driver for SMIA/SMIA++ compliant camera modules
6 * Copyright (C) 2011--2012 Nokia Corporation
7 * Contact: Sakari Ailus <sakari.ailus@iki.fi>
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.
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.
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
25 #include <linux/gcd.h>
26 #include <linux/lcm.h>
27 #include <linux/module.h>
29 #include "smiapp-pll.h"
31 /* Return an even number or one. */
32 static inline uint32_t clk_div_even(uint32_t a
)
34 return max_t(uint32_t, 1, a
& ~1);
37 /* Return an even number or one. */
38 static inline uint32_t clk_div_even_up(uint32_t a
)
45 static inline uint32_t is_one_or_even(uint32_t a
)
55 static int bounds_check(struct device
*dev
, uint32_t val
,
56 uint32_t min
, uint32_t max
, char *str
)
58 if (val
>= min
&& val
<= max
)
61 dev_dbg(dev
, "%s out of bounds: %d (%d--%d)\n", str
, val
, min
, max
);
66 static void print_pll(struct device
*dev
, struct smiapp_pll
*pll
)
68 dev_dbg(dev
, "pre_pll_clk_div\t%d\n", pll
->pre_pll_clk_div
);
69 dev_dbg(dev
, "pll_multiplier \t%d\n", pll
->pll_multiplier
);
70 if (pll
->flags
!= SMIAPP_PLL_FLAG_NO_OP_CLOCKS
) {
71 dev_dbg(dev
, "op_sys_clk_div \t%d\n", pll
->op_sys_clk_div
);
72 dev_dbg(dev
, "op_pix_clk_div \t%d\n", pll
->op_pix_clk_div
);
74 dev_dbg(dev
, "vt_sys_clk_div \t%d\n", pll
->vt_sys_clk_div
);
75 dev_dbg(dev
, "vt_pix_clk_div \t%d\n", pll
->vt_pix_clk_div
);
77 dev_dbg(dev
, "ext_clk_freq_hz \t%d\n", pll
->ext_clk_freq_hz
);
78 dev_dbg(dev
, "pll_ip_clk_freq_hz \t%d\n", pll
->pll_ip_clk_freq_hz
);
79 dev_dbg(dev
, "pll_op_clk_freq_hz \t%d\n", pll
->pll_op_clk_freq_hz
);
80 if (pll
->flags
& SMIAPP_PLL_FLAG_NO_OP_CLOCKS
) {
81 dev_dbg(dev
, "op_sys_clk_freq_hz \t%d\n",
82 pll
->op_sys_clk_freq_hz
);
83 dev_dbg(dev
, "op_pix_clk_freq_hz \t%d\n",
84 pll
->op_pix_clk_freq_hz
);
86 dev_dbg(dev
, "vt_sys_clk_freq_hz \t%d\n", pll
->vt_sys_clk_freq_hz
);
87 dev_dbg(dev
, "vt_pix_clk_freq_hz \t%d\n", pll
->vt_pix_clk_freq_hz
);
91 * Heuristically guess the PLL tree for a given common multiplier and
92 * divisor. Begin with the operational timing and continue to video
93 * timing once operational timing has been verified.
95 * @mul is the PLL multiplier and @div is the common divisor
96 * (pre_pll_clk_div and op_sys_clk_div combined). The final PLL
97 * multiplier will be a multiple of @mul.
99 * @return Zero on success, error code on error.
101 static int __smiapp_pll_calculate(struct device
*dev
,
102 const struct smiapp_pll_limits
*limits
,
103 struct smiapp_pll
*pll
, uint32_t mul
,
104 uint32_t div
, uint32_t lane_op_clock_ratio
)
107 uint32_t best_pix_div
= INT_MAX
>> 1;
108 uint32_t vt_op_binning_div
;
110 * Higher multipliers (and divisors) are often required than
111 * necessitated by the external clock and the output clocks.
112 * There are limits for all values in the clock tree. These
113 * are the minimum and maximum multiplier for mul.
115 uint32_t more_mul_min
, more_mul_max
;
116 uint32_t more_mul_factor
;
117 uint32_t min_vt_div
, max_vt_div
, vt_div
;
118 uint32_t min_sys_div
, max_sys_div
;
123 * Get pre_pll_clk_div so that our pll_op_clk_freq_hz won't be
126 dev_dbg(dev
, "pre_pll_clk_div %d\n", pll
->pre_pll_clk_div
);
128 /* Don't go above max pll multiplier. */
129 more_mul_max
= limits
->max_pll_multiplier
/ mul
;
130 dev_dbg(dev
, "more_mul_max: max_pll_multiplier check: %d\n",
132 /* Don't go above max pll op frequency. */
136 limits
->max_pll_op_freq_hz
137 / (pll
->ext_clk_freq_hz
/ pll
->pre_pll_clk_div
* mul
));
138 dev_dbg(dev
, "more_mul_max: max_pll_op_freq_hz check: %d\n",
140 /* Don't go above the division capability of op sys clock divider. */
141 more_mul_max
= min(more_mul_max
,
142 limits
->op
.max_sys_clk_div
* pll
->pre_pll_clk_div
144 dev_dbg(dev
, "more_mul_max: max_op_sys_clk_div check: %d\n",
146 /* Ensure we won't go above min_pll_multiplier. */
147 more_mul_max
= min(more_mul_max
,
148 DIV_ROUND_UP(limits
->max_pll_multiplier
, mul
));
149 dev_dbg(dev
, "more_mul_max: min_pll_multiplier check: %d\n",
152 /* Ensure we won't go below min_pll_op_freq_hz. */
153 more_mul_min
= DIV_ROUND_UP(limits
->min_pll_op_freq_hz
,
154 pll
->ext_clk_freq_hz
/ pll
->pre_pll_clk_div
156 dev_dbg(dev
, "more_mul_min: min_pll_op_freq_hz check: %d\n",
158 /* Ensure we won't go below min_pll_multiplier. */
159 more_mul_min
= max(more_mul_min
,
160 DIV_ROUND_UP(limits
->min_pll_multiplier
, mul
));
161 dev_dbg(dev
, "more_mul_min: min_pll_multiplier check: %d\n",
164 if (more_mul_min
> more_mul_max
) {
166 "unable to compute more_mul_min and more_mul_max\n");
170 more_mul_factor
= lcm(div
, pll
->pre_pll_clk_div
) / div
;
171 dev_dbg(dev
, "more_mul_factor: %d\n", more_mul_factor
);
172 more_mul_factor
= lcm(more_mul_factor
, limits
->op
.min_sys_clk_div
);
173 dev_dbg(dev
, "more_mul_factor: min_op_sys_clk_div: %d\n",
175 i
= roundup(more_mul_min
, more_mul_factor
);
176 if (!is_one_or_even(i
))
179 dev_dbg(dev
, "final more_mul: %d\n", i
);
180 if (i
> more_mul_max
) {
181 dev_dbg(dev
, "final more_mul is bad, max %d\n", more_mul_max
);
185 pll
->pll_multiplier
= mul
* i
;
186 pll
->op_sys_clk_div
= div
* i
/ pll
->pre_pll_clk_div
;
187 dev_dbg(dev
, "op_sys_clk_div: %d\n", pll
->op_sys_clk_div
);
189 pll
->pll_ip_clk_freq_hz
= pll
->ext_clk_freq_hz
190 / pll
->pre_pll_clk_div
;
192 pll
->pll_op_clk_freq_hz
= pll
->pll_ip_clk_freq_hz
193 * pll
->pll_multiplier
;
195 /* Derive pll_op_clk_freq_hz. */
196 pll
->op_sys_clk_freq_hz
=
197 pll
->pll_op_clk_freq_hz
/ pll
->op_sys_clk_div
;
199 pll
->op_pix_clk_div
= pll
->bits_per_pixel
;
200 dev_dbg(dev
, "op_pix_clk_div: %d\n", pll
->op_pix_clk_div
);
202 pll
->op_pix_clk_freq_hz
=
203 pll
->op_sys_clk_freq_hz
/ pll
->op_pix_clk_div
;
206 * Some sensors perform analogue binning and some do this
207 * digitally. The ones doing this digitally can be roughly be
208 * found out using this formula. The ones doing this digitally
209 * should run at higher clock rate, so smaller divisor is used
210 * on video timing side.
212 if (limits
->min_line_length_pck_bin
> limits
->min_line_length_pck
213 / pll
->binning_horizontal
)
214 vt_op_binning_div
= pll
->binning_horizontal
;
216 vt_op_binning_div
= 1;
217 dev_dbg(dev
, "vt_op_binning_div: %d\n", vt_op_binning_div
);
220 * Profile 2 supports vt_pix_clk_div E [4, 10]
222 * Horizontal binning can be used as a base for difference in
223 * divisors. One must make sure that horizontal blanking is
224 * enough to accommodate the CSI-2 sync codes.
226 * Take scaling factor into account as well.
228 * Find absolute limits for the factor of vt divider.
230 dev_dbg(dev
, "scale_m: %d\n", pll
->scale_m
);
231 min_vt_div
= DIV_ROUND_UP(pll
->op_pix_clk_div
* pll
->op_sys_clk_div
233 lane_op_clock_ratio
* vt_op_binning_div
236 /* Find smallest and biggest allowed vt divisor. */
237 dev_dbg(dev
, "min_vt_div: %d\n", min_vt_div
);
238 min_vt_div
= max(min_vt_div
,
239 DIV_ROUND_UP(pll
->pll_op_clk_freq_hz
,
240 limits
->vt
.max_pix_clk_freq_hz
));
241 dev_dbg(dev
, "min_vt_div: max_vt_pix_clk_freq_hz: %d\n",
243 min_vt_div
= max_t(uint32_t, min_vt_div
,
244 limits
->vt
.min_pix_clk_div
245 * limits
->vt
.min_sys_clk_div
);
246 dev_dbg(dev
, "min_vt_div: min_vt_clk_div: %d\n", min_vt_div
);
248 max_vt_div
= limits
->vt
.max_sys_clk_div
* limits
->vt
.max_pix_clk_div
;
249 dev_dbg(dev
, "max_vt_div: %d\n", max_vt_div
);
250 max_vt_div
= min(max_vt_div
,
251 DIV_ROUND_UP(pll
->pll_op_clk_freq_hz
,
252 limits
->vt
.min_pix_clk_freq_hz
));
253 dev_dbg(dev
, "max_vt_div: min_vt_pix_clk_freq_hz: %d\n",
257 * Find limitsits for sys_clk_div. Not all values are possible
258 * with all values of pix_clk_div.
260 min_sys_div
= limits
->vt
.min_sys_clk_div
;
261 dev_dbg(dev
, "min_sys_div: %d\n", min_sys_div
);
262 min_sys_div
= max(min_sys_div
,
263 DIV_ROUND_UP(min_vt_div
,
264 limits
->vt
.max_pix_clk_div
));
265 dev_dbg(dev
, "min_sys_div: max_vt_pix_clk_div: %d\n", min_sys_div
);
266 min_sys_div
= max(min_sys_div
,
267 pll
->pll_op_clk_freq_hz
268 / limits
->vt
.max_sys_clk_freq_hz
);
269 dev_dbg(dev
, "min_sys_div: max_pll_op_clk_freq_hz: %d\n", min_sys_div
);
270 min_sys_div
= clk_div_even_up(min_sys_div
);
271 dev_dbg(dev
, "min_sys_div: one or even: %d\n", min_sys_div
);
273 max_sys_div
= limits
->vt
.max_sys_clk_div
;
274 dev_dbg(dev
, "max_sys_div: %d\n", max_sys_div
);
275 max_sys_div
= min(max_sys_div
,
276 DIV_ROUND_UP(max_vt_div
,
277 limits
->vt
.min_pix_clk_div
));
278 dev_dbg(dev
, "max_sys_div: min_vt_pix_clk_div: %d\n", max_sys_div
);
279 max_sys_div
= min(max_sys_div
,
280 DIV_ROUND_UP(pll
->pll_op_clk_freq_hz
,
281 limits
->vt
.min_pix_clk_freq_hz
));
282 dev_dbg(dev
, "max_sys_div: min_vt_pix_clk_freq_hz: %d\n", max_sys_div
);
285 * Find pix_div such that a legal pix_div * sys_div results
286 * into a value which is not smaller than div, the desired
289 for (vt_div
= min_vt_div
; vt_div
<= max_vt_div
;
290 vt_div
+= 2 - (vt_div
& 1)) {
291 for (sys_div
= min_sys_div
;
292 sys_div
<= max_sys_div
;
293 sys_div
+= 2 - (sys_div
& 1)) {
294 uint16_t pix_div
= DIV_ROUND_UP(vt_div
, sys_div
);
296 if (pix_div
< limits
->vt
.min_pix_clk_div
297 || pix_div
> limits
->vt
.max_pix_clk_div
) {
299 "pix_div %d too small or too big (%d--%d)\n",
301 limits
->vt
.min_pix_clk_div
,
302 limits
->vt
.max_pix_clk_div
);
306 /* Check if this one is better. */
307 if (pix_div
* sys_div
308 <= roundup(min_vt_div
, best_pix_div
))
309 best_pix_div
= pix_div
;
311 if (best_pix_div
< INT_MAX
>> 1)
315 pll
->vt_sys_clk_div
= DIV_ROUND_UP(min_vt_div
, best_pix_div
);
316 pll
->vt_pix_clk_div
= best_pix_div
;
318 pll
->vt_sys_clk_freq_hz
=
319 pll
->pll_op_clk_freq_hz
/ pll
->vt_sys_clk_div
;
320 pll
->vt_pix_clk_freq_hz
=
321 pll
->vt_sys_clk_freq_hz
/ pll
->vt_pix_clk_div
;
323 pll
->pixel_rate_csi
=
324 pll
->op_pix_clk_freq_hz
* lane_op_clock_ratio
;
326 rval
= bounds_check(dev
, pll
->pll_ip_clk_freq_hz
,
327 limits
->min_pll_ip_freq_hz
,
328 limits
->max_pll_ip_freq_hz
,
329 "pll_ip_clk_freq_hz");
332 dev
, pll
->pll_multiplier
,
333 limits
->min_pll_multiplier
, limits
->max_pll_multiplier
,
337 dev
, pll
->pll_op_clk_freq_hz
,
338 limits
->min_pll_op_freq_hz
, limits
->max_pll_op_freq_hz
,
339 "pll_op_clk_freq_hz");
342 dev
, pll
->op_sys_clk_div
,
343 limits
->op
.min_sys_clk_div
, limits
->op
.max_sys_clk_div
,
347 dev
, pll
->op_pix_clk_div
,
348 limits
->op
.min_pix_clk_div
, limits
->op
.max_pix_clk_div
,
352 dev
, pll
->op_sys_clk_freq_hz
,
353 limits
->op
.min_sys_clk_freq_hz
,
354 limits
->op
.max_sys_clk_freq_hz
,
355 "op_sys_clk_freq_hz");
358 dev
, pll
->op_pix_clk_freq_hz
,
359 limits
->op
.min_pix_clk_freq_hz
,
360 limits
->op
.max_pix_clk_freq_hz
,
361 "op_pix_clk_freq_hz");
364 dev
, pll
->vt_sys_clk_freq_hz
,
365 limits
->vt
.min_sys_clk_freq_hz
,
366 limits
->vt
.max_sys_clk_freq_hz
,
367 "vt_sys_clk_freq_hz");
370 dev
, pll
->vt_pix_clk_freq_hz
,
371 limits
->vt
.min_pix_clk_freq_hz
,
372 limits
->vt
.max_pix_clk_freq_hz
,
373 "vt_pix_clk_freq_hz");
378 int smiapp_pll_calculate(struct device
*dev
,
379 const struct smiapp_pll_limits
*limits
,
380 struct smiapp_pll
*pll
)
382 uint16_t min_pre_pll_clk_div
;
383 uint16_t max_pre_pll_clk_div
;
384 uint32_t lane_op_clock_ratio
;
389 if (pll
->flags
& SMIAPP_PLL_FLAG_OP_PIX_CLOCK_PER_LANE
)
390 lane_op_clock_ratio
= pll
->csi2
.lanes
;
392 lane_op_clock_ratio
= 1;
393 dev_dbg(dev
, "lane_op_clock_ratio: %d\n", lane_op_clock_ratio
);
395 dev_dbg(dev
, "binning: %dx%d\n", pll
->binning_horizontal
,
396 pll
->binning_vertical
);
398 switch (pll
->bus_type
) {
399 case SMIAPP_PLL_BUS_TYPE_CSI2
:
400 /* CSI transfers 2 bits per clock per lane; thus times 2 */
401 pll
->pll_op_clk_freq_hz
= pll
->link_freq
* 2
402 * (pll
->csi2
.lanes
/ lane_op_clock_ratio
);
404 case SMIAPP_PLL_BUS_TYPE_PARALLEL
:
405 pll
->pll_op_clk_freq_hz
= pll
->link_freq
* pll
->bits_per_pixel
406 / DIV_ROUND_UP(pll
->bits_per_pixel
,
407 pll
->parallel
.bus_width
);
413 /* Figure out limits for pre-pll divider based on extclk */
414 dev_dbg(dev
, "min / max pre_pll_clk_div: %d / %d\n",
415 limits
->min_pre_pll_clk_div
, limits
->max_pre_pll_clk_div
);
416 max_pre_pll_clk_div
=
417 min_t(uint16_t, limits
->max_pre_pll_clk_div
,
418 clk_div_even(pll
->ext_clk_freq_hz
/
419 limits
->min_pll_ip_freq_hz
));
420 min_pre_pll_clk_div
=
421 max_t(uint16_t, limits
->min_pre_pll_clk_div
,
423 DIV_ROUND_UP(pll
->ext_clk_freq_hz
,
424 limits
->max_pll_ip_freq_hz
)));
425 dev_dbg(dev
, "pre-pll check: min / max pre_pll_clk_div: %d / %d\n",
426 min_pre_pll_clk_div
, max_pre_pll_clk_div
);
428 i
= gcd(pll
->pll_op_clk_freq_hz
, pll
->ext_clk_freq_hz
);
429 mul
= div_u64(pll
->pll_op_clk_freq_hz
, i
);
430 div
= pll
->ext_clk_freq_hz
/ i
;
431 dev_dbg(dev
, "mul %d / div %d\n", mul
, div
);
433 min_pre_pll_clk_div
=
434 max_t(uint16_t, min_pre_pll_clk_div
,
436 DIV_ROUND_UP(mul
* pll
->ext_clk_freq_hz
,
437 limits
->max_pll_op_freq_hz
)));
438 dev_dbg(dev
, "pll_op check: min / max pre_pll_clk_div: %d / %d\n",
439 min_pre_pll_clk_div
, max_pre_pll_clk_div
);
441 for (pll
->pre_pll_clk_div
= min_pre_pll_clk_div
;
442 pll
->pre_pll_clk_div
<= max_pre_pll_clk_div
;
443 pll
->pre_pll_clk_div
+= 2 - (pll
->pre_pll_clk_div
& 1)) {
444 rval
= __smiapp_pll_calculate(dev
, limits
, pll
, mul
, div
,
445 lane_op_clock_ratio
);
453 dev_info(dev
, "unable to compute pre_pll divisor\n");
456 EXPORT_SYMBOL_GPL(smiapp_pll_calculate
);
458 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>");
459 MODULE_DESCRIPTION("Generic SMIA/SMIA++ PLL calculator");
460 MODULE_LICENSE("GPL");