18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Aptina Sensor PLL Configuration 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2012 Laurent Pinchart <laurent.pinchart@ideasonboard.com> 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/device.h> 98c2ecf20Sopenharmony_ci#include <linux/gcd.h> 108c2ecf20Sopenharmony_ci#include <linux/kernel.h> 118c2ecf20Sopenharmony_ci#include <linux/lcm.h> 128c2ecf20Sopenharmony_ci#include <linux/module.h> 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#include "aptina-pll.h" 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ciint aptina_pll_calculate(struct device *dev, 178c2ecf20Sopenharmony_ci const struct aptina_pll_limits *limits, 188c2ecf20Sopenharmony_ci struct aptina_pll *pll) 198c2ecf20Sopenharmony_ci{ 208c2ecf20Sopenharmony_ci unsigned int mf_min; 218c2ecf20Sopenharmony_ci unsigned int mf_max; 228c2ecf20Sopenharmony_ci unsigned int p1_min; 238c2ecf20Sopenharmony_ci unsigned int p1_max; 248c2ecf20Sopenharmony_ci unsigned int p1; 258c2ecf20Sopenharmony_ci unsigned int div; 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci dev_dbg(dev, "PLL: ext clock %u pix clock %u\n", 288c2ecf20Sopenharmony_ci pll->ext_clock, pll->pix_clock); 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci if (pll->ext_clock < limits->ext_clock_min || 318c2ecf20Sopenharmony_ci pll->ext_clock > limits->ext_clock_max) { 328c2ecf20Sopenharmony_ci dev_err(dev, "pll: invalid external clock frequency.\n"); 338c2ecf20Sopenharmony_ci return -EINVAL; 348c2ecf20Sopenharmony_ci } 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci if (pll->pix_clock == 0 || pll->pix_clock > limits->pix_clock_max) { 378c2ecf20Sopenharmony_ci dev_err(dev, "pll: invalid pixel clock frequency.\n"); 388c2ecf20Sopenharmony_ci return -EINVAL; 398c2ecf20Sopenharmony_ci } 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci /* Compute the multiplier M and combined N*P1 divisor. */ 428c2ecf20Sopenharmony_ci div = gcd(pll->pix_clock, pll->ext_clock); 438c2ecf20Sopenharmony_ci pll->m = pll->pix_clock / div; 448c2ecf20Sopenharmony_ci div = pll->ext_clock / div; 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci /* We now have the smallest M and N*P1 values that will result in the 478c2ecf20Sopenharmony_ci * desired pixel clock frequency, but they might be out of the valid 488c2ecf20Sopenharmony_ci * range. Compute the factor by which we should multiply them given the 498c2ecf20Sopenharmony_ci * following constraints: 508c2ecf20Sopenharmony_ci * 518c2ecf20Sopenharmony_ci * - minimum/maximum multiplier 528c2ecf20Sopenharmony_ci * - minimum/maximum multiplier output clock frequency assuming the 538c2ecf20Sopenharmony_ci * minimum/maximum N value 548c2ecf20Sopenharmony_ci * - minimum/maximum combined N*P1 divisor 558c2ecf20Sopenharmony_ci */ 568c2ecf20Sopenharmony_ci mf_min = DIV_ROUND_UP(limits->m_min, pll->m); 578c2ecf20Sopenharmony_ci mf_min = max(mf_min, limits->out_clock_min / 588c2ecf20Sopenharmony_ci (pll->ext_clock / limits->n_min * pll->m)); 598c2ecf20Sopenharmony_ci mf_min = max(mf_min, limits->n_min * limits->p1_min / div); 608c2ecf20Sopenharmony_ci mf_max = limits->m_max / pll->m; 618c2ecf20Sopenharmony_ci mf_max = min(mf_max, limits->out_clock_max / 628c2ecf20Sopenharmony_ci (pll->ext_clock / limits->n_max * pll->m)); 638c2ecf20Sopenharmony_ci mf_max = min(mf_max, DIV_ROUND_UP(limits->n_max * limits->p1_max, div)); 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci dev_dbg(dev, "pll: mf min %u max %u\n", mf_min, mf_max); 668c2ecf20Sopenharmony_ci if (mf_min > mf_max) { 678c2ecf20Sopenharmony_ci dev_err(dev, "pll: no valid combined N*P1 divisor.\n"); 688c2ecf20Sopenharmony_ci return -EINVAL; 698c2ecf20Sopenharmony_ci } 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci /* 728c2ecf20Sopenharmony_ci * We're looking for the highest acceptable P1 value for which a 738c2ecf20Sopenharmony_ci * multiplier factor MF exists that fulfills the following conditions: 748c2ecf20Sopenharmony_ci * 758c2ecf20Sopenharmony_ci * 1. p1 is in the [p1_min, p1_max] range given by the limits and is 768c2ecf20Sopenharmony_ci * even 778c2ecf20Sopenharmony_ci * 2. mf is in the [mf_min, mf_max] range computed above 788c2ecf20Sopenharmony_ci * 3. div * mf is a multiple of p1, in order to compute 798c2ecf20Sopenharmony_ci * n = div * mf / p1 808c2ecf20Sopenharmony_ci * m = pll->m * mf 818c2ecf20Sopenharmony_ci * 4. the internal clock frequency, given by ext_clock / n, is in the 828c2ecf20Sopenharmony_ci * [int_clock_min, int_clock_max] range given by the limits 838c2ecf20Sopenharmony_ci * 5. the output clock frequency, given by ext_clock / n * m, is in the 848c2ecf20Sopenharmony_ci * [out_clock_min, out_clock_max] range given by the limits 858c2ecf20Sopenharmony_ci * 868c2ecf20Sopenharmony_ci * The first naive approach is to iterate over all p1 values acceptable 878c2ecf20Sopenharmony_ci * according to (1) and all mf values acceptable according to (2), and 888c2ecf20Sopenharmony_ci * stop at the first combination that fulfills (3), (4) and (5). This 898c2ecf20Sopenharmony_ci * has a O(n^2) complexity. 908c2ecf20Sopenharmony_ci * 918c2ecf20Sopenharmony_ci * Instead of iterating over all mf values in the [mf_min, mf_max] range 928c2ecf20Sopenharmony_ci * we can compute the mf increment between two acceptable values 938c2ecf20Sopenharmony_ci * according to (3) with 948c2ecf20Sopenharmony_ci * 958c2ecf20Sopenharmony_ci * mf_inc = p1 / gcd(div, p1) (6) 968c2ecf20Sopenharmony_ci * 978c2ecf20Sopenharmony_ci * and round the minimum up to the nearest multiple of mf_inc. This will 988c2ecf20Sopenharmony_ci * restrict the number of mf values to be checked. 998c2ecf20Sopenharmony_ci * 1008c2ecf20Sopenharmony_ci * Furthermore, conditions (4) and (5) only restrict the range of 1018c2ecf20Sopenharmony_ci * acceptable p1 and mf values by modifying the minimum and maximum 1028c2ecf20Sopenharmony_ci * limits. (5) can be expressed as 1038c2ecf20Sopenharmony_ci * 1048c2ecf20Sopenharmony_ci * ext_clock / (div * mf / p1) * m * mf >= out_clock_min 1058c2ecf20Sopenharmony_ci * ext_clock / (div * mf / p1) * m * mf <= out_clock_max 1068c2ecf20Sopenharmony_ci * 1078c2ecf20Sopenharmony_ci * or 1088c2ecf20Sopenharmony_ci * 1098c2ecf20Sopenharmony_ci * p1 >= out_clock_min * div / (ext_clock * m) (7) 1108c2ecf20Sopenharmony_ci * p1 <= out_clock_max * div / (ext_clock * m) 1118c2ecf20Sopenharmony_ci * 1128c2ecf20Sopenharmony_ci * Similarly, (4) can be expressed as 1138c2ecf20Sopenharmony_ci * 1148c2ecf20Sopenharmony_ci * mf >= ext_clock * p1 / (int_clock_max * div) (8) 1158c2ecf20Sopenharmony_ci * mf <= ext_clock * p1 / (int_clock_min * div) 1168c2ecf20Sopenharmony_ci * 1178c2ecf20Sopenharmony_ci * We can thus iterate over the restricted p1 range defined by the 1188c2ecf20Sopenharmony_ci * combination of (1) and (7), and then compute the restricted mf range 1198c2ecf20Sopenharmony_ci * defined by the combination of (2), (6) and (8). If the resulting mf 1208c2ecf20Sopenharmony_ci * range is not empty, any value in the mf range is acceptable. We thus 1218c2ecf20Sopenharmony_ci * select the mf lwoer bound and the corresponding p1 value. 1228c2ecf20Sopenharmony_ci */ 1238c2ecf20Sopenharmony_ci if (limits->p1_min == 0) { 1248c2ecf20Sopenharmony_ci dev_err(dev, "pll: P1 minimum value must be >0.\n"); 1258c2ecf20Sopenharmony_ci return -EINVAL; 1268c2ecf20Sopenharmony_ci } 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci p1_min = max(limits->p1_min, DIV_ROUND_UP(limits->out_clock_min * div, 1298c2ecf20Sopenharmony_ci pll->ext_clock * pll->m)); 1308c2ecf20Sopenharmony_ci p1_max = min(limits->p1_max, limits->out_clock_max * div / 1318c2ecf20Sopenharmony_ci (pll->ext_clock * pll->m)); 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci for (p1 = p1_max & ~1; p1 >= p1_min; p1 -= 2) { 1348c2ecf20Sopenharmony_ci unsigned int mf_inc = p1 / gcd(div, p1); 1358c2ecf20Sopenharmony_ci unsigned int mf_high; 1368c2ecf20Sopenharmony_ci unsigned int mf_low; 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci mf_low = roundup(max(mf_min, DIV_ROUND_UP(pll->ext_clock * p1, 1398c2ecf20Sopenharmony_ci limits->int_clock_max * div)), mf_inc); 1408c2ecf20Sopenharmony_ci mf_high = min(mf_max, pll->ext_clock * p1 / 1418c2ecf20Sopenharmony_ci (limits->int_clock_min * div)); 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci if (mf_low > mf_high) 1448c2ecf20Sopenharmony_ci continue; 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci pll->n = div * mf_low / p1; 1478c2ecf20Sopenharmony_ci pll->m *= mf_low; 1488c2ecf20Sopenharmony_ci pll->p1 = p1; 1498c2ecf20Sopenharmony_ci dev_dbg(dev, "PLL: N %u M %u P1 %u\n", pll->n, pll->m, pll->p1); 1508c2ecf20Sopenharmony_ci return 0; 1518c2ecf20Sopenharmony_ci } 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci dev_err(dev, "pll: no valid N and P1 divisors found.\n"); 1548c2ecf20Sopenharmony_ci return -EINVAL; 1558c2ecf20Sopenharmony_ci} 1568c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(aptina_pll_calculate); 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Aptina PLL Helpers"); 1598c2ecf20Sopenharmony_ciMODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>"); 1608c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 161