smiapp-pll.c 15 KB

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