18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2016 Maxime Ripard 48c2ecf20Sopenharmony_ci * Maxime Ripard <maxime.ripard@free-electrons.com> 58c2ecf20Sopenharmony_ci */ 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci#include <linux/clk.h> 88c2ecf20Sopenharmony_ci#include <linux/clk-provider.h> 98c2ecf20Sopenharmony_ci#include <linux/delay.h> 108c2ecf20Sopenharmony_ci#include <linux/io.h> 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#include "ccu_gate.h" 138c2ecf20Sopenharmony_ci#include "ccu_mux.h" 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_cistatic u16 ccu_mux_get_prediv(struct ccu_common *common, 168c2ecf20Sopenharmony_ci struct ccu_mux_internal *cm, 178c2ecf20Sopenharmony_ci int parent_index) 188c2ecf20Sopenharmony_ci{ 198c2ecf20Sopenharmony_ci u16 prediv = 1; 208c2ecf20Sopenharmony_ci u32 reg; 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci if (!((common->features & CCU_FEATURE_FIXED_PREDIV) || 238c2ecf20Sopenharmony_ci (common->features & CCU_FEATURE_VARIABLE_PREDIV) || 248c2ecf20Sopenharmony_ci (common->features & CCU_FEATURE_ALL_PREDIV))) 258c2ecf20Sopenharmony_ci return 1; 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci if (common->features & CCU_FEATURE_ALL_PREDIV) 288c2ecf20Sopenharmony_ci return common->prediv; 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci reg = readl(common->base + common->reg); 318c2ecf20Sopenharmony_ci if (parent_index < 0) { 328c2ecf20Sopenharmony_ci parent_index = reg >> cm->shift; 338c2ecf20Sopenharmony_ci parent_index &= (1 << cm->width) - 1; 348c2ecf20Sopenharmony_ci } 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci if (common->features & CCU_FEATURE_FIXED_PREDIV) { 378c2ecf20Sopenharmony_ci int i; 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci for (i = 0; i < cm->n_predivs; i++) 408c2ecf20Sopenharmony_ci if (parent_index == cm->fixed_predivs[i].index) 418c2ecf20Sopenharmony_ci prediv = cm->fixed_predivs[i].div; 428c2ecf20Sopenharmony_ci } 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci if (common->features & CCU_FEATURE_VARIABLE_PREDIV) { 458c2ecf20Sopenharmony_ci int i; 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci for (i = 0; i < cm->n_var_predivs; i++) 488c2ecf20Sopenharmony_ci if (parent_index == cm->var_predivs[i].index) { 498c2ecf20Sopenharmony_ci u8 div; 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci div = reg >> cm->var_predivs[i].shift; 528c2ecf20Sopenharmony_ci div &= (1 << cm->var_predivs[i].width) - 1; 538c2ecf20Sopenharmony_ci prediv = div + 1; 548c2ecf20Sopenharmony_ci } 558c2ecf20Sopenharmony_ci } 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci return prediv; 588c2ecf20Sopenharmony_ci} 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ciunsigned long ccu_mux_helper_apply_prediv(struct ccu_common *common, 618c2ecf20Sopenharmony_ci struct ccu_mux_internal *cm, 628c2ecf20Sopenharmony_ci int parent_index, 638c2ecf20Sopenharmony_ci unsigned long parent_rate) 648c2ecf20Sopenharmony_ci{ 658c2ecf20Sopenharmony_ci return parent_rate / ccu_mux_get_prediv(common, cm, parent_index); 668c2ecf20Sopenharmony_ci} 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_cistatic unsigned long ccu_mux_helper_unapply_prediv(struct ccu_common *common, 698c2ecf20Sopenharmony_ci struct ccu_mux_internal *cm, 708c2ecf20Sopenharmony_ci int parent_index, 718c2ecf20Sopenharmony_ci unsigned long parent_rate) 728c2ecf20Sopenharmony_ci{ 738c2ecf20Sopenharmony_ci return parent_rate * ccu_mux_get_prediv(common, cm, parent_index); 748c2ecf20Sopenharmony_ci} 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ciint ccu_mux_helper_determine_rate(struct ccu_common *common, 778c2ecf20Sopenharmony_ci struct ccu_mux_internal *cm, 788c2ecf20Sopenharmony_ci struct clk_rate_request *req, 798c2ecf20Sopenharmony_ci unsigned long (*round)(struct ccu_mux_internal *, 808c2ecf20Sopenharmony_ci struct clk_hw *, 818c2ecf20Sopenharmony_ci unsigned long *, 828c2ecf20Sopenharmony_ci unsigned long, 838c2ecf20Sopenharmony_ci void *), 848c2ecf20Sopenharmony_ci void *data) 858c2ecf20Sopenharmony_ci{ 868c2ecf20Sopenharmony_ci unsigned long best_parent_rate = 0, best_rate = 0; 878c2ecf20Sopenharmony_ci struct clk_hw *best_parent, *hw = &common->hw; 888c2ecf20Sopenharmony_ci unsigned int i; 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci if (clk_hw_get_flags(hw) & CLK_SET_RATE_NO_REPARENT) { 918c2ecf20Sopenharmony_ci unsigned long adj_parent_rate; 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci best_parent = clk_hw_get_parent(hw); 948c2ecf20Sopenharmony_ci best_parent_rate = clk_hw_get_rate(best_parent); 958c2ecf20Sopenharmony_ci adj_parent_rate = ccu_mux_helper_apply_prediv(common, cm, -1, 968c2ecf20Sopenharmony_ci best_parent_rate); 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci best_rate = round(cm, best_parent, &adj_parent_rate, 998c2ecf20Sopenharmony_ci req->rate, data); 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci /* 1028c2ecf20Sopenharmony_ci * adj_parent_rate might have been modified by our clock. 1038c2ecf20Sopenharmony_ci * Unapply the pre-divider if there's one, and give 1048c2ecf20Sopenharmony_ci * the actual frequency the parent needs to run at. 1058c2ecf20Sopenharmony_ci */ 1068c2ecf20Sopenharmony_ci best_parent_rate = ccu_mux_helper_unapply_prediv(common, cm, -1, 1078c2ecf20Sopenharmony_ci adj_parent_rate); 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci goto out; 1108c2ecf20Sopenharmony_ci } 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci for (i = 0; i < clk_hw_get_num_parents(hw); i++) { 1138c2ecf20Sopenharmony_ci unsigned long tmp_rate, parent_rate; 1148c2ecf20Sopenharmony_ci struct clk_hw *parent; 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci parent = clk_hw_get_parent_by_index(hw, i); 1178c2ecf20Sopenharmony_ci if (!parent) 1188c2ecf20Sopenharmony_ci continue; 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci parent_rate = ccu_mux_helper_apply_prediv(common, cm, i, 1218c2ecf20Sopenharmony_ci clk_hw_get_rate(parent)); 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci tmp_rate = round(cm, parent, &parent_rate, req->rate, data); 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci /* 1268c2ecf20Sopenharmony_ci * parent_rate might have been modified by our clock. 1278c2ecf20Sopenharmony_ci * Unapply the pre-divider if there's one, and give 1288c2ecf20Sopenharmony_ci * the actual frequency the parent needs to run at. 1298c2ecf20Sopenharmony_ci */ 1308c2ecf20Sopenharmony_ci parent_rate = ccu_mux_helper_unapply_prediv(common, cm, i, 1318c2ecf20Sopenharmony_ci parent_rate); 1328c2ecf20Sopenharmony_ci if (tmp_rate == req->rate) { 1338c2ecf20Sopenharmony_ci best_parent = parent; 1348c2ecf20Sopenharmony_ci best_parent_rate = parent_rate; 1358c2ecf20Sopenharmony_ci best_rate = tmp_rate; 1368c2ecf20Sopenharmony_ci goto out; 1378c2ecf20Sopenharmony_ci } 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci if ((req->rate - tmp_rate) < (req->rate - best_rate)) { 1408c2ecf20Sopenharmony_ci best_rate = tmp_rate; 1418c2ecf20Sopenharmony_ci best_parent_rate = parent_rate; 1428c2ecf20Sopenharmony_ci best_parent = parent; 1438c2ecf20Sopenharmony_ci } 1448c2ecf20Sopenharmony_ci } 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci if (best_rate == 0) 1478c2ecf20Sopenharmony_ci return -EINVAL; 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ciout: 1508c2ecf20Sopenharmony_ci req->best_parent_hw = best_parent; 1518c2ecf20Sopenharmony_ci req->best_parent_rate = best_parent_rate; 1528c2ecf20Sopenharmony_ci req->rate = best_rate; 1538c2ecf20Sopenharmony_ci return 0; 1548c2ecf20Sopenharmony_ci} 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ciu8 ccu_mux_helper_get_parent(struct ccu_common *common, 1578c2ecf20Sopenharmony_ci struct ccu_mux_internal *cm) 1588c2ecf20Sopenharmony_ci{ 1598c2ecf20Sopenharmony_ci u32 reg; 1608c2ecf20Sopenharmony_ci u8 parent; 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci reg = readl(common->base + common->reg); 1638c2ecf20Sopenharmony_ci parent = reg >> cm->shift; 1648c2ecf20Sopenharmony_ci parent &= (1 << cm->width) - 1; 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci if (cm->table) { 1678c2ecf20Sopenharmony_ci int num_parents = clk_hw_get_num_parents(&common->hw); 1688c2ecf20Sopenharmony_ci int i; 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci for (i = 0; i < num_parents; i++) 1718c2ecf20Sopenharmony_ci if (cm->table[i] == parent) 1728c2ecf20Sopenharmony_ci return i; 1738c2ecf20Sopenharmony_ci } 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci return parent; 1768c2ecf20Sopenharmony_ci} 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ciint ccu_mux_helper_set_parent(struct ccu_common *common, 1798c2ecf20Sopenharmony_ci struct ccu_mux_internal *cm, 1808c2ecf20Sopenharmony_ci u8 index) 1818c2ecf20Sopenharmony_ci{ 1828c2ecf20Sopenharmony_ci unsigned long flags; 1838c2ecf20Sopenharmony_ci u32 reg; 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci if (cm->table) 1868c2ecf20Sopenharmony_ci index = cm->table[index]; 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci spin_lock_irqsave(common->lock, flags); 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci reg = readl(common->base + common->reg); 1918c2ecf20Sopenharmony_ci reg &= ~GENMASK(cm->width + cm->shift - 1, cm->shift); 1928c2ecf20Sopenharmony_ci writel(reg | (index << cm->shift), common->base + common->reg); 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci spin_unlock_irqrestore(common->lock, flags); 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci return 0; 1978c2ecf20Sopenharmony_ci} 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_cistatic void ccu_mux_disable(struct clk_hw *hw) 2008c2ecf20Sopenharmony_ci{ 2018c2ecf20Sopenharmony_ci struct ccu_mux *cm = hw_to_ccu_mux(hw); 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ci return ccu_gate_helper_disable(&cm->common, cm->enable); 2048c2ecf20Sopenharmony_ci} 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_cistatic int ccu_mux_enable(struct clk_hw *hw) 2078c2ecf20Sopenharmony_ci{ 2088c2ecf20Sopenharmony_ci struct ccu_mux *cm = hw_to_ccu_mux(hw); 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci return ccu_gate_helper_enable(&cm->common, cm->enable); 2118c2ecf20Sopenharmony_ci} 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_cistatic int ccu_mux_is_enabled(struct clk_hw *hw) 2148c2ecf20Sopenharmony_ci{ 2158c2ecf20Sopenharmony_ci struct ccu_mux *cm = hw_to_ccu_mux(hw); 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci return ccu_gate_helper_is_enabled(&cm->common, cm->enable); 2188c2ecf20Sopenharmony_ci} 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_cistatic u8 ccu_mux_get_parent(struct clk_hw *hw) 2218c2ecf20Sopenharmony_ci{ 2228c2ecf20Sopenharmony_ci struct ccu_mux *cm = hw_to_ccu_mux(hw); 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci return ccu_mux_helper_get_parent(&cm->common, &cm->mux); 2258c2ecf20Sopenharmony_ci} 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_cistatic int ccu_mux_set_parent(struct clk_hw *hw, u8 index) 2288c2ecf20Sopenharmony_ci{ 2298c2ecf20Sopenharmony_ci struct ccu_mux *cm = hw_to_ccu_mux(hw); 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci return ccu_mux_helper_set_parent(&cm->common, &cm->mux, index); 2328c2ecf20Sopenharmony_ci} 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_cistatic unsigned long ccu_mux_recalc_rate(struct clk_hw *hw, 2358c2ecf20Sopenharmony_ci unsigned long parent_rate) 2368c2ecf20Sopenharmony_ci{ 2378c2ecf20Sopenharmony_ci struct ccu_mux *cm = hw_to_ccu_mux(hw); 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci return ccu_mux_helper_apply_prediv(&cm->common, &cm->mux, -1, 2408c2ecf20Sopenharmony_ci parent_rate); 2418c2ecf20Sopenharmony_ci} 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ciconst struct clk_ops ccu_mux_ops = { 2448c2ecf20Sopenharmony_ci .disable = ccu_mux_disable, 2458c2ecf20Sopenharmony_ci .enable = ccu_mux_enable, 2468c2ecf20Sopenharmony_ci .is_enabled = ccu_mux_is_enabled, 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci .get_parent = ccu_mux_get_parent, 2498c2ecf20Sopenharmony_ci .set_parent = ccu_mux_set_parent, 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci .determine_rate = __clk_mux_determine_rate, 2528c2ecf20Sopenharmony_ci .recalc_rate = ccu_mux_recalc_rate, 2538c2ecf20Sopenharmony_ci}; 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ci/* 2568c2ecf20Sopenharmony_ci * This clock notifier is called when the frequency of the of the parent 2578c2ecf20Sopenharmony_ci * PLL clock is to be changed. The idea is to switch the parent to a 2588c2ecf20Sopenharmony_ci * stable clock, such as the main oscillator, while the PLL frequency 2598c2ecf20Sopenharmony_ci * stabilizes. 2608c2ecf20Sopenharmony_ci */ 2618c2ecf20Sopenharmony_cistatic int ccu_mux_notifier_cb(struct notifier_block *nb, 2628c2ecf20Sopenharmony_ci unsigned long event, void *data) 2638c2ecf20Sopenharmony_ci{ 2648c2ecf20Sopenharmony_ci struct ccu_mux_nb *mux = to_ccu_mux_nb(nb); 2658c2ecf20Sopenharmony_ci int ret = 0; 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_ci if (event == PRE_RATE_CHANGE) { 2688c2ecf20Sopenharmony_ci mux->original_index = ccu_mux_helper_get_parent(mux->common, 2698c2ecf20Sopenharmony_ci mux->cm); 2708c2ecf20Sopenharmony_ci ret = ccu_mux_helper_set_parent(mux->common, mux->cm, 2718c2ecf20Sopenharmony_ci mux->bypass_index); 2728c2ecf20Sopenharmony_ci } else if (event == POST_RATE_CHANGE) { 2738c2ecf20Sopenharmony_ci ret = ccu_mux_helper_set_parent(mux->common, mux->cm, 2748c2ecf20Sopenharmony_ci mux->original_index); 2758c2ecf20Sopenharmony_ci } 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_ci udelay(mux->delay_us); 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ci return notifier_from_errno(ret); 2808c2ecf20Sopenharmony_ci} 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ciint ccu_mux_notifier_register(struct clk *clk, struct ccu_mux_nb *mux_nb) 2838c2ecf20Sopenharmony_ci{ 2848c2ecf20Sopenharmony_ci mux_nb->clk_nb.notifier_call = ccu_mux_notifier_cb; 2858c2ecf20Sopenharmony_ci 2868c2ecf20Sopenharmony_ci return clk_notifier_register(clk, &mux_nb->clk_nb); 2878c2ecf20Sopenharmony_ci} 288