18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci// 38c2ecf20Sopenharmony_ci// Spreadtrum pll clock driver 48c2ecf20Sopenharmony_ci// 58c2ecf20Sopenharmony_ci// Copyright (C) 2015~2017 Spreadtrum, Inc. 68c2ecf20Sopenharmony_ci// Author: Chunyan Zhang <chunyan.zhang@spreadtrum.com> 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/delay.h> 98c2ecf20Sopenharmony_ci#include <linux/err.h> 108c2ecf20Sopenharmony_ci#include <linux/regmap.h> 118c2ecf20Sopenharmony_ci#include <linux/slab.h> 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#include "pll.h" 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#define CLK_PLL_1M 1000000 168c2ecf20Sopenharmony_ci#define CLK_PLL_10M (CLK_PLL_1M * 10) 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci#define pindex(pll, member) \ 198c2ecf20Sopenharmony_ci (pll->factors[member].shift / (8 * sizeof(pll->regs_num))) 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci#define pshift(pll, member) \ 228c2ecf20Sopenharmony_ci (pll->factors[member].shift % (8 * sizeof(pll->regs_num))) 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci#define pwidth(pll, member) \ 258c2ecf20Sopenharmony_ci pll->factors[member].width 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci#define pmask(pll, member) \ 288c2ecf20Sopenharmony_ci ((pwidth(pll, member)) ? \ 298c2ecf20Sopenharmony_ci GENMASK(pwidth(pll, member) + pshift(pll, member) - 1, \ 308c2ecf20Sopenharmony_ci pshift(pll, member)) : 0) 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci#define pinternal(pll, cfg, member) \ 338c2ecf20Sopenharmony_ci (cfg[pindex(pll, member)] & pmask(pll, member)) 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci#define pinternal_val(pll, cfg, member) \ 368c2ecf20Sopenharmony_ci (pinternal(pll, cfg, member) >> pshift(pll, member)) 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_cistatic inline unsigned int 398c2ecf20Sopenharmony_cisprd_pll_read(const struct sprd_pll *pll, u8 index) 408c2ecf20Sopenharmony_ci{ 418c2ecf20Sopenharmony_ci const struct sprd_clk_common *common = &pll->common; 428c2ecf20Sopenharmony_ci unsigned int val = 0; 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci if (WARN_ON(index >= pll->regs_num)) 458c2ecf20Sopenharmony_ci return 0; 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci regmap_read(common->regmap, common->reg + index * 4, &val); 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci return val; 508c2ecf20Sopenharmony_ci} 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_cistatic inline void 538c2ecf20Sopenharmony_cisprd_pll_write(const struct sprd_pll *pll, u8 index, 548c2ecf20Sopenharmony_ci u32 msk, u32 val) 558c2ecf20Sopenharmony_ci{ 568c2ecf20Sopenharmony_ci const struct sprd_clk_common *common = &pll->common; 578c2ecf20Sopenharmony_ci unsigned int offset, reg; 588c2ecf20Sopenharmony_ci int ret = 0; 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci if (WARN_ON(index >= pll->regs_num)) 618c2ecf20Sopenharmony_ci return; 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci offset = common->reg + index * 4; 648c2ecf20Sopenharmony_ci ret = regmap_read(common->regmap, offset, ®); 658c2ecf20Sopenharmony_ci if (!ret) 668c2ecf20Sopenharmony_ci regmap_write(common->regmap, offset, (reg & ~msk) | val); 678c2ecf20Sopenharmony_ci} 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_cistatic unsigned long pll_get_refin(const struct sprd_pll *pll) 708c2ecf20Sopenharmony_ci{ 718c2ecf20Sopenharmony_ci u32 shift, mask, index, refin_id = 3; 728c2ecf20Sopenharmony_ci const unsigned long refin[4] = { 2, 4, 13, 26 }; 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci if (pwidth(pll, PLL_REFIN)) { 758c2ecf20Sopenharmony_ci index = pindex(pll, PLL_REFIN); 768c2ecf20Sopenharmony_ci shift = pshift(pll, PLL_REFIN); 778c2ecf20Sopenharmony_ci mask = pmask(pll, PLL_REFIN); 788c2ecf20Sopenharmony_ci refin_id = (sprd_pll_read(pll, index) & mask) >> shift; 798c2ecf20Sopenharmony_ci if (refin_id > 3) 808c2ecf20Sopenharmony_ci refin_id = 3; 818c2ecf20Sopenharmony_ci } 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci return refin[refin_id]; 848c2ecf20Sopenharmony_ci} 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_cistatic u32 pll_get_ibias(u64 rate, const u64 *table) 878c2ecf20Sopenharmony_ci{ 888c2ecf20Sopenharmony_ci u32 i, num = table[0]; 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci /* table[0] indicates the number of items in this table */ 918c2ecf20Sopenharmony_ci for (i = 0; i < num; i++) 928c2ecf20Sopenharmony_ci if (rate <= table[i + 1]) 938c2ecf20Sopenharmony_ci break; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci return i == num ? num - 1 : i; 968c2ecf20Sopenharmony_ci} 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_cistatic unsigned long _sprd_pll_recalc_rate(const struct sprd_pll *pll, 998c2ecf20Sopenharmony_ci unsigned long parent_rate) 1008c2ecf20Sopenharmony_ci{ 1018c2ecf20Sopenharmony_ci u32 *cfg; 1028c2ecf20Sopenharmony_ci u32 i, mask, regs_num = pll->regs_num; 1038c2ecf20Sopenharmony_ci unsigned long rate, nint, kint = 0; 1048c2ecf20Sopenharmony_ci u64 refin; 1058c2ecf20Sopenharmony_ci u16 k1, k2; 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci cfg = kcalloc(regs_num, sizeof(*cfg), GFP_KERNEL); 1088c2ecf20Sopenharmony_ci if (!cfg) 1098c2ecf20Sopenharmony_ci return parent_rate; 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci for (i = 0; i < regs_num; i++) 1128c2ecf20Sopenharmony_ci cfg[i] = sprd_pll_read(pll, i); 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci refin = pll_get_refin(pll); 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci if (pinternal(pll, cfg, PLL_PREDIV)) 1178c2ecf20Sopenharmony_ci refin = refin * 2; 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci if (pwidth(pll, PLL_POSTDIV) && 1208c2ecf20Sopenharmony_ci ((pll->fflag == 1 && pinternal(pll, cfg, PLL_POSTDIV)) || 1218c2ecf20Sopenharmony_ci (!pll->fflag && !pinternal(pll, cfg, PLL_POSTDIV)))) 1228c2ecf20Sopenharmony_ci refin = refin / 2; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci if (!pinternal(pll, cfg, PLL_DIV_S)) { 1258c2ecf20Sopenharmony_ci rate = refin * pinternal_val(pll, cfg, PLL_N) * CLK_PLL_10M; 1268c2ecf20Sopenharmony_ci } else { 1278c2ecf20Sopenharmony_ci nint = pinternal_val(pll, cfg, PLL_NINT); 1288c2ecf20Sopenharmony_ci if (pinternal(pll, cfg, PLL_SDM_EN)) 1298c2ecf20Sopenharmony_ci kint = pinternal_val(pll, cfg, PLL_KINT); 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci mask = pmask(pll, PLL_KINT); 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci k1 = pll->k1; 1348c2ecf20Sopenharmony_ci k2 = pll->k2; 1358c2ecf20Sopenharmony_ci rate = DIV_ROUND_CLOSEST_ULL(refin * kint * k1, 1368c2ecf20Sopenharmony_ci ((mask >> __ffs(mask)) + 1)) * 1378c2ecf20Sopenharmony_ci k2 + refin * nint * CLK_PLL_1M; 1388c2ecf20Sopenharmony_ci } 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci kfree(cfg); 1418c2ecf20Sopenharmony_ci return rate; 1428c2ecf20Sopenharmony_ci} 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci#define SPRD_PLL_WRITE_CHECK(pll, i, mask, val) \ 1458c2ecf20Sopenharmony_ci (((sprd_pll_read(pll, i) & mask) == val) ? 0 : (-EFAULT)) 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_cistatic int _sprd_pll_set_rate(const struct sprd_pll *pll, 1488c2ecf20Sopenharmony_ci unsigned long rate, 1498c2ecf20Sopenharmony_ci unsigned long parent_rate) 1508c2ecf20Sopenharmony_ci{ 1518c2ecf20Sopenharmony_ci struct reg_cfg *cfg; 1528c2ecf20Sopenharmony_ci int ret = 0; 1538c2ecf20Sopenharmony_ci u32 mask, shift, width, ibias_val, index; 1548c2ecf20Sopenharmony_ci u32 regs_num = pll->regs_num, i = 0; 1558c2ecf20Sopenharmony_ci unsigned long kint, nint; 1568c2ecf20Sopenharmony_ci u64 tmp, refin, fvco = rate; 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci cfg = kcalloc(regs_num, sizeof(*cfg), GFP_KERNEL); 1598c2ecf20Sopenharmony_ci if (!cfg) 1608c2ecf20Sopenharmony_ci return -ENOMEM; 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci refin = pll_get_refin(pll); 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci mask = pmask(pll, PLL_PREDIV); 1658c2ecf20Sopenharmony_ci index = pindex(pll, PLL_PREDIV); 1668c2ecf20Sopenharmony_ci width = pwidth(pll, PLL_PREDIV); 1678c2ecf20Sopenharmony_ci if (width && (sprd_pll_read(pll, index) & mask)) 1688c2ecf20Sopenharmony_ci refin = refin * 2; 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci mask = pmask(pll, PLL_POSTDIV); 1718c2ecf20Sopenharmony_ci index = pindex(pll, PLL_POSTDIV); 1728c2ecf20Sopenharmony_ci width = pwidth(pll, PLL_POSTDIV); 1738c2ecf20Sopenharmony_ci cfg[index].msk = mask; 1748c2ecf20Sopenharmony_ci if (width && ((pll->fflag == 1 && fvco <= pll->fvco) || 1758c2ecf20Sopenharmony_ci (pll->fflag == 0 && fvco > pll->fvco))) 1768c2ecf20Sopenharmony_ci cfg[index].val |= mask; 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci if (width && fvco <= pll->fvco) 1798c2ecf20Sopenharmony_ci fvco = fvco * 2; 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci mask = pmask(pll, PLL_DIV_S); 1828c2ecf20Sopenharmony_ci index = pindex(pll, PLL_DIV_S); 1838c2ecf20Sopenharmony_ci cfg[index].val |= mask; 1848c2ecf20Sopenharmony_ci cfg[index].msk |= mask; 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci mask = pmask(pll, PLL_SDM_EN); 1878c2ecf20Sopenharmony_ci index = pindex(pll, PLL_SDM_EN); 1888c2ecf20Sopenharmony_ci cfg[index].val |= mask; 1898c2ecf20Sopenharmony_ci cfg[index].msk |= mask; 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci nint = do_div(fvco, refin * CLK_PLL_1M); 1928c2ecf20Sopenharmony_ci mask = pmask(pll, PLL_NINT); 1938c2ecf20Sopenharmony_ci index = pindex(pll, PLL_NINT); 1948c2ecf20Sopenharmony_ci shift = pshift(pll, PLL_NINT); 1958c2ecf20Sopenharmony_ci cfg[index].val |= (nint << shift) & mask; 1968c2ecf20Sopenharmony_ci cfg[index].msk |= mask; 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci mask = pmask(pll, PLL_KINT); 1998c2ecf20Sopenharmony_ci index = pindex(pll, PLL_KINT); 2008c2ecf20Sopenharmony_ci width = pwidth(pll, PLL_KINT); 2018c2ecf20Sopenharmony_ci shift = pshift(pll, PLL_KINT); 2028c2ecf20Sopenharmony_ci tmp = fvco - refin * nint * CLK_PLL_1M; 2038c2ecf20Sopenharmony_ci tmp = do_div(tmp, 10000) * ((mask >> shift) + 1); 2048c2ecf20Sopenharmony_ci kint = DIV_ROUND_CLOSEST_ULL(tmp, refin * 100); 2058c2ecf20Sopenharmony_ci cfg[index].val |= (kint << shift) & mask; 2068c2ecf20Sopenharmony_ci cfg[index].msk |= mask; 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci ibias_val = pll_get_ibias(fvco, pll->itable); 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci mask = pmask(pll, PLL_IBIAS); 2118c2ecf20Sopenharmony_ci index = pindex(pll, PLL_IBIAS); 2128c2ecf20Sopenharmony_ci shift = pshift(pll, PLL_IBIAS); 2138c2ecf20Sopenharmony_ci cfg[index].val |= ibias_val << shift & mask; 2148c2ecf20Sopenharmony_ci cfg[index].msk |= mask; 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci for (i = 0; i < regs_num; i++) { 2178c2ecf20Sopenharmony_ci if (cfg[i].msk) { 2188c2ecf20Sopenharmony_ci sprd_pll_write(pll, i, cfg[i].msk, cfg[i].val); 2198c2ecf20Sopenharmony_ci ret |= SPRD_PLL_WRITE_CHECK(pll, i, cfg[i].msk, 2208c2ecf20Sopenharmony_ci cfg[i].val); 2218c2ecf20Sopenharmony_ci } 2228c2ecf20Sopenharmony_ci } 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci if (!ret) 2258c2ecf20Sopenharmony_ci udelay(pll->udelay); 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_ci kfree(cfg); 2288c2ecf20Sopenharmony_ci return ret; 2298c2ecf20Sopenharmony_ci} 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_cistatic unsigned long sprd_pll_recalc_rate(struct clk_hw *hw, 2328c2ecf20Sopenharmony_ci unsigned long parent_rate) 2338c2ecf20Sopenharmony_ci{ 2348c2ecf20Sopenharmony_ci struct sprd_pll *pll = hw_to_sprd_pll(hw); 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci return _sprd_pll_recalc_rate(pll, parent_rate); 2378c2ecf20Sopenharmony_ci} 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_cistatic int sprd_pll_set_rate(struct clk_hw *hw, 2408c2ecf20Sopenharmony_ci unsigned long rate, 2418c2ecf20Sopenharmony_ci unsigned long parent_rate) 2428c2ecf20Sopenharmony_ci{ 2438c2ecf20Sopenharmony_ci struct sprd_pll *pll = hw_to_sprd_pll(hw); 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci return _sprd_pll_set_rate(pll, rate, parent_rate); 2468c2ecf20Sopenharmony_ci} 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_cistatic int sprd_pll_clk_prepare(struct clk_hw *hw) 2498c2ecf20Sopenharmony_ci{ 2508c2ecf20Sopenharmony_ci struct sprd_pll *pll = hw_to_sprd_pll(hw); 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_ci udelay(pll->udelay); 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci return 0; 2558c2ecf20Sopenharmony_ci} 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_cistatic long sprd_pll_round_rate(struct clk_hw *hw, unsigned long rate, 2588c2ecf20Sopenharmony_ci unsigned long *prate) 2598c2ecf20Sopenharmony_ci{ 2608c2ecf20Sopenharmony_ci return rate; 2618c2ecf20Sopenharmony_ci} 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ciconst struct clk_ops sprd_pll_ops = { 2648c2ecf20Sopenharmony_ci .prepare = sprd_pll_clk_prepare, 2658c2ecf20Sopenharmony_ci .recalc_rate = sprd_pll_recalc_rate, 2668c2ecf20Sopenharmony_ci .round_rate = sprd_pll_round_rate, 2678c2ecf20Sopenharmony_ci .set_rate = sprd_pll_set_rate, 2688c2ecf20Sopenharmony_ci}; 2698c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sprd_pll_ops); 270