18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+ 28c2ecf20Sopenharmony_ci// 38c2ecf20Sopenharmony_ci// OWL pll clock driver 48c2ecf20Sopenharmony_ci// 58c2ecf20Sopenharmony_ci// Copyright (c) 2014 Actions Semi Inc. 68c2ecf20Sopenharmony_ci// Author: David Liu <liuwei@actions-semi.com> 78c2ecf20Sopenharmony_ci// 88c2ecf20Sopenharmony_ci// Copyright (c) 2018 Linaro Ltd. 98c2ecf20Sopenharmony_ci// Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org> 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include <linux/clk-provider.h> 128c2ecf20Sopenharmony_ci#include <linux/slab.h> 138c2ecf20Sopenharmony_ci#include <linux/io.h> 148c2ecf20Sopenharmony_ci#include <linux/delay.h> 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#include "owl-pll.h" 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_cistatic u32 owl_pll_calculate_mul(struct owl_pll_hw *pll_hw, unsigned long rate) 198c2ecf20Sopenharmony_ci{ 208c2ecf20Sopenharmony_ci u32 mul; 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci mul = DIV_ROUND_CLOSEST(rate, pll_hw->bfreq); 238c2ecf20Sopenharmony_ci if (mul < pll_hw->min_mul) 248c2ecf20Sopenharmony_ci mul = pll_hw->min_mul; 258c2ecf20Sopenharmony_ci else if (mul > pll_hw->max_mul) 268c2ecf20Sopenharmony_ci mul = pll_hw->max_mul; 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci return mul &= mul_mask(pll_hw); 298c2ecf20Sopenharmony_ci} 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_cistatic unsigned long _get_table_rate(const struct clk_pll_table *table, 328c2ecf20Sopenharmony_ci unsigned int val) 338c2ecf20Sopenharmony_ci{ 348c2ecf20Sopenharmony_ci const struct clk_pll_table *clkt; 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci for (clkt = table; clkt->rate; clkt++) 378c2ecf20Sopenharmony_ci if (clkt->val == val) 388c2ecf20Sopenharmony_ci return clkt->rate; 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci return 0; 418c2ecf20Sopenharmony_ci} 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_cistatic const struct clk_pll_table *_get_pll_table( 448c2ecf20Sopenharmony_ci const struct clk_pll_table *table, unsigned long rate) 458c2ecf20Sopenharmony_ci{ 468c2ecf20Sopenharmony_ci const struct clk_pll_table *clkt; 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci for (clkt = table; clkt->rate; clkt++) { 498c2ecf20Sopenharmony_ci if (clkt->rate == rate) { 508c2ecf20Sopenharmony_ci table = clkt; 518c2ecf20Sopenharmony_ci break; 528c2ecf20Sopenharmony_ci } else if (clkt->rate < rate) 538c2ecf20Sopenharmony_ci table = clkt; 548c2ecf20Sopenharmony_ci } 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci return table; 578c2ecf20Sopenharmony_ci} 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_cistatic long owl_pll_round_rate(struct clk_hw *hw, unsigned long rate, 608c2ecf20Sopenharmony_ci unsigned long *parent_rate) 618c2ecf20Sopenharmony_ci{ 628c2ecf20Sopenharmony_ci struct owl_pll *pll = hw_to_owl_pll(hw); 638c2ecf20Sopenharmony_ci struct owl_pll_hw *pll_hw = &pll->pll_hw; 648c2ecf20Sopenharmony_ci const struct clk_pll_table *clkt; 658c2ecf20Sopenharmony_ci u32 mul; 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci if (pll_hw->table) { 688c2ecf20Sopenharmony_ci clkt = _get_pll_table(pll_hw->table, rate); 698c2ecf20Sopenharmony_ci return clkt->rate; 708c2ecf20Sopenharmony_ci } 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci /* fixed frequency */ 738c2ecf20Sopenharmony_ci if (pll_hw->width == 0) 748c2ecf20Sopenharmony_ci return pll_hw->bfreq; 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci mul = owl_pll_calculate_mul(pll_hw, rate); 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci return pll_hw->bfreq * mul; 798c2ecf20Sopenharmony_ci} 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_cistatic unsigned long owl_pll_recalc_rate(struct clk_hw *hw, 828c2ecf20Sopenharmony_ci unsigned long parent_rate) 838c2ecf20Sopenharmony_ci{ 848c2ecf20Sopenharmony_ci struct owl_pll *pll = hw_to_owl_pll(hw); 858c2ecf20Sopenharmony_ci struct owl_pll_hw *pll_hw = &pll->pll_hw; 868c2ecf20Sopenharmony_ci const struct owl_clk_common *common = &pll->common; 878c2ecf20Sopenharmony_ci u32 val; 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci if (pll_hw->table) { 908c2ecf20Sopenharmony_ci regmap_read(common->regmap, pll_hw->reg, &val); 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci val = val >> pll_hw->shift; 938c2ecf20Sopenharmony_ci val &= mul_mask(pll_hw); 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci return _get_table_rate(pll_hw->table, val); 968c2ecf20Sopenharmony_ci } 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci /* fixed frequency */ 998c2ecf20Sopenharmony_ci if (pll_hw->width == 0) 1008c2ecf20Sopenharmony_ci return pll_hw->bfreq; 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci regmap_read(common->regmap, pll_hw->reg, &val); 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci val = val >> pll_hw->shift; 1058c2ecf20Sopenharmony_ci val &= mul_mask(pll_hw); 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci return pll_hw->bfreq * val; 1088c2ecf20Sopenharmony_ci} 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_cistatic int owl_pll_is_enabled(struct clk_hw *hw) 1118c2ecf20Sopenharmony_ci{ 1128c2ecf20Sopenharmony_ci struct owl_pll *pll = hw_to_owl_pll(hw); 1138c2ecf20Sopenharmony_ci struct owl_pll_hw *pll_hw = &pll->pll_hw; 1148c2ecf20Sopenharmony_ci const struct owl_clk_common *common = &pll->common; 1158c2ecf20Sopenharmony_ci u32 reg; 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci regmap_read(common->regmap, pll_hw->reg, ®); 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci return !!(reg & BIT(pll_hw->bit_idx)); 1208c2ecf20Sopenharmony_ci} 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_cistatic void owl_pll_set(const struct owl_clk_common *common, 1238c2ecf20Sopenharmony_ci const struct owl_pll_hw *pll_hw, bool enable) 1248c2ecf20Sopenharmony_ci{ 1258c2ecf20Sopenharmony_ci u32 reg; 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci regmap_read(common->regmap, pll_hw->reg, ®); 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci if (enable) 1308c2ecf20Sopenharmony_ci reg |= BIT(pll_hw->bit_idx); 1318c2ecf20Sopenharmony_ci else 1328c2ecf20Sopenharmony_ci reg &= ~BIT(pll_hw->bit_idx); 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci regmap_write(common->regmap, pll_hw->reg, reg); 1358c2ecf20Sopenharmony_ci} 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_cistatic int owl_pll_enable(struct clk_hw *hw) 1388c2ecf20Sopenharmony_ci{ 1398c2ecf20Sopenharmony_ci struct owl_pll *pll = hw_to_owl_pll(hw); 1408c2ecf20Sopenharmony_ci const struct owl_clk_common *common = &pll->common; 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci owl_pll_set(common, &pll->pll_hw, true); 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci return 0; 1458c2ecf20Sopenharmony_ci} 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_cistatic void owl_pll_disable(struct clk_hw *hw) 1488c2ecf20Sopenharmony_ci{ 1498c2ecf20Sopenharmony_ci struct owl_pll *pll = hw_to_owl_pll(hw); 1508c2ecf20Sopenharmony_ci const struct owl_clk_common *common = &pll->common; 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci owl_pll_set(common, &pll->pll_hw, false); 1538c2ecf20Sopenharmony_ci} 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_cistatic int owl_pll_set_rate(struct clk_hw *hw, unsigned long rate, 1568c2ecf20Sopenharmony_ci unsigned long parent_rate) 1578c2ecf20Sopenharmony_ci{ 1588c2ecf20Sopenharmony_ci struct owl_pll *pll = hw_to_owl_pll(hw); 1598c2ecf20Sopenharmony_ci struct owl_pll_hw *pll_hw = &pll->pll_hw; 1608c2ecf20Sopenharmony_ci const struct owl_clk_common *common = &pll->common; 1618c2ecf20Sopenharmony_ci const struct clk_pll_table *clkt; 1628c2ecf20Sopenharmony_ci u32 val, reg; 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci /* fixed frequency */ 1658c2ecf20Sopenharmony_ci if (pll_hw->width == 0) 1668c2ecf20Sopenharmony_ci return 0; 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci if (pll_hw->table) { 1698c2ecf20Sopenharmony_ci clkt = _get_pll_table(pll_hw->table, rate); 1708c2ecf20Sopenharmony_ci val = clkt->val; 1718c2ecf20Sopenharmony_ci } else { 1728c2ecf20Sopenharmony_ci val = owl_pll_calculate_mul(pll_hw, rate); 1738c2ecf20Sopenharmony_ci } 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci regmap_read(common->regmap, pll_hw->reg, ®); 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci reg &= ~mul_mask(pll_hw); 1788c2ecf20Sopenharmony_ci reg |= val << pll_hw->shift; 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci regmap_write(common->regmap, pll_hw->reg, reg); 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci udelay(pll_hw->delay); 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci return 0; 1858c2ecf20Sopenharmony_ci} 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ciconst struct clk_ops owl_pll_ops = { 1888c2ecf20Sopenharmony_ci .enable = owl_pll_enable, 1898c2ecf20Sopenharmony_ci .disable = owl_pll_disable, 1908c2ecf20Sopenharmony_ci .is_enabled = owl_pll_is_enabled, 1918c2ecf20Sopenharmony_ci .round_rate = owl_pll_round_rate, 1928c2ecf20Sopenharmony_ci .recalc_rate = owl_pll_recalc_rate, 1938c2ecf20Sopenharmony_ci .set_rate = owl_pll_set_rate, 1948c2ecf20Sopenharmony_ci}; 195