smiapp-pll.c 15 KB

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