18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * linux/arch/arm/common/icst307.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2003 Deep Blue Solutions, Ltd, All Rights Reserved. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Support functions for calculating clocks/divisors for the ICST307 88c2ecf20Sopenharmony_ci * clock generators. See https://www.idt.com/ for more information 98c2ecf20Sopenharmony_ci * on these devices. 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * This is an almost identical implementation to the ICST525 clock generator. 128c2ecf20Sopenharmony_ci * The s2div and idx2s files are different 138c2ecf20Sopenharmony_ci */ 148c2ecf20Sopenharmony_ci#include <linux/module.h> 158c2ecf20Sopenharmony_ci#include <linux/kernel.h> 168c2ecf20Sopenharmony_ci#include <asm/div64.h> 178c2ecf20Sopenharmony_ci#include "icst.h" 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci/* 208c2ecf20Sopenharmony_ci * Divisors for each OD setting. 218c2ecf20Sopenharmony_ci */ 228c2ecf20Sopenharmony_ciconst unsigned char icst307_s2div[8] = { 10, 2, 8, 4, 5, 7, 3, 6 }; 238c2ecf20Sopenharmony_ciconst unsigned char icst525_s2div[8] = { 10, 2, 8, 4, 5, 7, 9, 6 }; 248c2ecf20Sopenharmony_ciEXPORT_SYMBOL(icst307_s2div); 258c2ecf20Sopenharmony_ciEXPORT_SYMBOL(icst525_s2div); 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ciunsigned long icst_hz(const struct icst_params *p, struct icst_vco vco) 288c2ecf20Sopenharmony_ci{ 298c2ecf20Sopenharmony_ci u64 dividend = p->ref * 2 * (u64)(vco.v + 8); 308c2ecf20Sopenharmony_ci u32 divisor = (vco.r + 2) * p->s2div[vco.s]; 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci do_div(dividend, divisor); 338c2ecf20Sopenharmony_ci return (unsigned long)dividend; 348c2ecf20Sopenharmony_ci} 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ciEXPORT_SYMBOL(icst_hz); 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci/* 398c2ecf20Sopenharmony_ci * Ascending divisor S values. 408c2ecf20Sopenharmony_ci */ 418c2ecf20Sopenharmony_ciconst unsigned char icst307_idx2s[8] = { 1, 6, 3, 4, 7, 5, 2, 0 }; 428c2ecf20Sopenharmony_ciconst unsigned char icst525_idx2s[8] = { 1, 3, 4, 7, 5, 2, 6, 0 }; 438c2ecf20Sopenharmony_ciEXPORT_SYMBOL(icst307_idx2s); 448c2ecf20Sopenharmony_ciEXPORT_SYMBOL(icst525_idx2s); 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_cistruct icst_vco 478c2ecf20Sopenharmony_ciicst_hz_to_vco(const struct icst_params *p, unsigned long freq) 488c2ecf20Sopenharmony_ci{ 498c2ecf20Sopenharmony_ci struct icst_vco vco = { .s = 1, .v = p->vd_max, .r = p->rd_max }; 508c2ecf20Sopenharmony_ci unsigned long f; 518c2ecf20Sopenharmony_ci unsigned int i = 0, rd, best = (unsigned int)-1; 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci /* 548c2ecf20Sopenharmony_ci * First, find the PLL output divisor such 558c2ecf20Sopenharmony_ci * that the PLL output is within spec. 568c2ecf20Sopenharmony_ci */ 578c2ecf20Sopenharmony_ci do { 588c2ecf20Sopenharmony_ci f = freq * p->s2div[p->idx2s[i]]; 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci if (f > p->vco_min && f <= p->vco_max) 618c2ecf20Sopenharmony_ci break; 628c2ecf20Sopenharmony_ci i++; 638c2ecf20Sopenharmony_ci } while (i < 8); 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci if (i >= 8) 668c2ecf20Sopenharmony_ci return vco; 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci vco.s = p->idx2s[i]; 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci /* 718c2ecf20Sopenharmony_ci * Now find the closest divisor combination 728c2ecf20Sopenharmony_ci * which gives a PLL output of 'f'. 738c2ecf20Sopenharmony_ci */ 748c2ecf20Sopenharmony_ci for (rd = p->rd_min; rd <= p->rd_max; rd++) { 758c2ecf20Sopenharmony_ci unsigned long fref_div, f_pll; 768c2ecf20Sopenharmony_ci unsigned int vd; 778c2ecf20Sopenharmony_ci int f_diff; 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci fref_div = (2 * p->ref) / rd; 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci vd = (f + fref_div / 2) / fref_div; 828c2ecf20Sopenharmony_ci if (vd < p->vd_min || vd > p->vd_max) 838c2ecf20Sopenharmony_ci continue; 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci f_pll = fref_div * vd; 868c2ecf20Sopenharmony_ci f_diff = f_pll - f; 878c2ecf20Sopenharmony_ci if (f_diff < 0) 888c2ecf20Sopenharmony_ci f_diff = -f_diff; 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci if ((unsigned)f_diff < best) { 918c2ecf20Sopenharmony_ci vco.v = vd - 8; 928c2ecf20Sopenharmony_ci vco.r = rd - 2; 938c2ecf20Sopenharmony_ci if (f_diff == 0) 948c2ecf20Sopenharmony_ci break; 958c2ecf20Sopenharmony_ci best = f_diff; 968c2ecf20Sopenharmony_ci } 978c2ecf20Sopenharmony_ci } 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci return vco; 1008c2ecf20Sopenharmony_ci} 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ciEXPORT_SYMBOL(icst_hz_to_vco); 103