18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com> 48c2ecf20Sopenharmony_ci * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org> 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Gated clock implementation 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <linux/clk-provider.h> 108c2ecf20Sopenharmony_ci#include <linux/module.h> 118c2ecf20Sopenharmony_ci#include <linux/slab.h> 128c2ecf20Sopenharmony_ci#include <linux/io.h> 138c2ecf20Sopenharmony_ci#include <linux/err.h> 148c2ecf20Sopenharmony_ci#include <linux/string.h> 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci/** 178c2ecf20Sopenharmony_ci * DOC: basic gatable clock which can gate and ungate it's ouput 188c2ecf20Sopenharmony_ci * 198c2ecf20Sopenharmony_ci * Traits of this clock: 208c2ecf20Sopenharmony_ci * prepare - clk_(un)prepare only ensures parent is (un)prepared 218c2ecf20Sopenharmony_ci * enable - clk_enable and clk_disable are functional & control gating 228c2ecf20Sopenharmony_ci * rate - inherits rate from parent. No clk_set_rate support 238c2ecf20Sopenharmony_ci * parent - fixed parent. No clk_set_parent support 248c2ecf20Sopenharmony_ci */ 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_cistatic inline u32 clk_gate_readl(struct clk_gate *gate) 278c2ecf20Sopenharmony_ci{ 288c2ecf20Sopenharmony_ci if (gate->flags & CLK_GATE_BIG_ENDIAN) 298c2ecf20Sopenharmony_ci return ioread32be(gate->reg); 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci return readl(gate->reg); 328c2ecf20Sopenharmony_ci} 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_cistatic inline void clk_gate_writel(struct clk_gate *gate, u32 val) 358c2ecf20Sopenharmony_ci{ 368c2ecf20Sopenharmony_ci if (gate->flags & CLK_GATE_BIG_ENDIAN) 378c2ecf20Sopenharmony_ci iowrite32be(val, gate->reg); 388c2ecf20Sopenharmony_ci else 398c2ecf20Sopenharmony_ci writel(val, gate->reg); 408c2ecf20Sopenharmony_ci} 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci/* 438c2ecf20Sopenharmony_ci * It works on following logic: 448c2ecf20Sopenharmony_ci * 458c2ecf20Sopenharmony_ci * For enabling clock, enable = 1 468c2ecf20Sopenharmony_ci * set2dis = 1 -> clear bit -> set = 0 478c2ecf20Sopenharmony_ci * set2dis = 0 -> set bit -> set = 1 488c2ecf20Sopenharmony_ci * 498c2ecf20Sopenharmony_ci * For disabling clock, enable = 0 508c2ecf20Sopenharmony_ci * set2dis = 1 -> set bit -> set = 1 518c2ecf20Sopenharmony_ci * set2dis = 0 -> clear bit -> set = 0 528c2ecf20Sopenharmony_ci * 538c2ecf20Sopenharmony_ci * So, result is always: enable xor set2dis. 548c2ecf20Sopenharmony_ci */ 558c2ecf20Sopenharmony_cistatic void clk_gate_endisable(struct clk_hw *hw, int enable) 568c2ecf20Sopenharmony_ci{ 578c2ecf20Sopenharmony_ci struct clk_gate *gate = to_clk_gate(hw); 588c2ecf20Sopenharmony_ci int set = gate->flags & CLK_GATE_SET_TO_DISABLE ? 1 : 0; 598c2ecf20Sopenharmony_ci unsigned long flags; 608c2ecf20Sopenharmony_ci u32 reg; 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci set ^= enable; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci if (gate->lock) 658c2ecf20Sopenharmony_ci spin_lock_irqsave(gate->lock, flags); 668c2ecf20Sopenharmony_ci else 678c2ecf20Sopenharmony_ci __acquire(gate->lock); 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci if (gate->flags & CLK_GATE_HIWORD_MASK) { 708c2ecf20Sopenharmony_ci reg = BIT(gate->bit_idx + 16); 718c2ecf20Sopenharmony_ci if (set) 728c2ecf20Sopenharmony_ci reg |= BIT(gate->bit_idx); 738c2ecf20Sopenharmony_ci } else { 748c2ecf20Sopenharmony_ci reg = clk_gate_readl(gate); 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci if (set) 778c2ecf20Sopenharmony_ci reg |= BIT(gate->bit_idx); 788c2ecf20Sopenharmony_ci else 798c2ecf20Sopenharmony_ci reg &= ~BIT(gate->bit_idx); 808c2ecf20Sopenharmony_ci } 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci clk_gate_writel(gate, reg); 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci if (gate->lock) 858c2ecf20Sopenharmony_ci spin_unlock_irqrestore(gate->lock, flags); 868c2ecf20Sopenharmony_ci else 878c2ecf20Sopenharmony_ci __release(gate->lock); 888c2ecf20Sopenharmony_ci} 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_cistatic int clk_gate_enable(struct clk_hw *hw) 918c2ecf20Sopenharmony_ci{ 928c2ecf20Sopenharmony_ci clk_gate_endisable(hw, 1); 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci return 0; 958c2ecf20Sopenharmony_ci} 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_cistatic void clk_gate_disable(struct clk_hw *hw) 988c2ecf20Sopenharmony_ci{ 998c2ecf20Sopenharmony_ci clk_gate_endisable(hw, 0); 1008c2ecf20Sopenharmony_ci} 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ciint clk_gate_is_enabled(struct clk_hw *hw) 1038c2ecf20Sopenharmony_ci{ 1048c2ecf20Sopenharmony_ci u32 reg; 1058c2ecf20Sopenharmony_ci struct clk_gate *gate = to_clk_gate(hw); 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci reg = clk_gate_readl(gate); 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci /* if a set bit disables this clk, flip it before masking */ 1108c2ecf20Sopenharmony_ci if (gate->flags & CLK_GATE_SET_TO_DISABLE) 1118c2ecf20Sopenharmony_ci reg ^= BIT(gate->bit_idx); 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci reg &= BIT(gate->bit_idx); 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci return reg ? 1 : 0; 1168c2ecf20Sopenharmony_ci} 1178c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(clk_gate_is_enabled); 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ciconst struct clk_ops clk_gate_ops = { 1208c2ecf20Sopenharmony_ci .enable = clk_gate_enable, 1218c2ecf20Sopenharmony_ci .disable = clk_gate_disable, 1228c2ecf20Sopenharmony_ci .is_enabled = clk_gate_is_enabled, 1238c2ecf20Sopenharmony_ci}; 1248c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(clk_gate_ops); 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_cistruct clk_hw *__clk_hw_register_gate(struct device *dev, 1278c2ecf20Sopenharmony_ci struct device_node *np, const char *name, 1288c2ecf20Sopenharmony_ci const char *parent_name, const struct clk_hw *parent_hw, 1298c2ecf20Sopenharmony_ci const struct clk_parent_data *parent_data, 1308c2ecf20Sopenharmony_ci unsigned long flags, 1318c2ecf20Sopenharmony_ci void __iomem *reg, u8 bit_idx, 1328c2ecf20Sopenharmony_ci u8 clk_gate_flags, spinlock_t *lock) 1338c2ecf20Sopenharmony_ci{ 1348c2ecf20Sopenharmony_ci struct clk_gate *gate; 1358c2ecf20Sopenharmony_ci struct clk_hw *hw; 1368c2ecf20Sopenharmony_ci struct clk_init_data init = {}; 1378c2ecf20Sopenharmony_ci int ret = -EINVAL; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci if (clk_gate_flags & CLK_GATE_HIWORD_MASK) { 1408c2ecf20Sopenharmony_ci if (bit_idx > 15) { 1418c2ecf20Sopenharmony_ci pr_err("gate bit exceeds LOWORD field\n"); 1428c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 1438c2ecf20Sopenharmony_ci } 1448c2ecf20Sopenharmony_ci } 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci /* allocate the gate */ 1478c2ecf20Sopenharmony_ci gate = kzalloc(sizeof(*gate), GFP_KERNEL); 1488c2ecf20Sopenharmony_ci if (!gate) 1498c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci init.name = name; 1528c2ecf20Sopenharmony_ci init.ops = &clk_gate_ops; 1538c2ecf20Sopenharmony_ci init.flags = flags; 1548c2ecf20Sopenharmony_ci init.parent_names = parent_name ? &parent_name : NULL; 1558c2ecf20Sopenharmony_ci init.parent_hws = parent_hw ? &parent_hw : NULL; 1568c2ecf20Sopenharmony_ci init.parent_data = parent_data; 1578c2ecf20Sopenharmony_ci if (parent_name || parent_hw || parent_data) 1588c2ecf20Sopenharmony_ci init.num_parents = 1; 1598c2ecf20Sopenharmony_ci else 1608c2ecf20Sopenharmony_ci init.num_parents = 0; 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci /* struct clk_gate assignments */ 1638c2ecf20Sopenharmony_ci gate->reg = reg; 1648c2ecf20Sopenharmony_ci gate->bit_idx = bit_idx; 1658c2ecf20Sopenharmony_ci gate->flags = clk_gate_flags; 1668c2ecf20Sopenharmony_ci gate->lock = lock; 1678c2ecf20Sopenharmony_ci gate->hw.init = &init; 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci hw = &gate->hw; 1708c2ecf20Sopenharmony_ci if (dev || !np) 1718c2ecf20Sopenharmony_ci ret = clk_hw_register(dev, hw); 1728c2ecf20Sopenharmony_ci else if (np) 1738c2ecf20Sopenharmony_ci ret = of_clk_hw_register(np, hw); 1748c2ecf20Sopenharmony_ci if (ret) { 1758c2ecf20Sopenharmony_ci kfree(gate); 1768c2ecf20Sopenharmony_ci hw = ERR_PTR(ret); 1778c2ecf20Sopenharmony_ci } 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci return hw; 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci} 1828c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(__clk_hw_register_gate); 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_cistruct clk *clk_register_gate(struct device *dev, const char *name, 1858c2ecf20Sopenharmony_ci const char *parent_name, unsigned long flags, 1868c2ecf20Sopenharmony_ci void __iomem *reg, u8 bit_idx, 1878c2ecf20Sopenharmony_ci u8 clk_gate_flags, spinlock_t *lock) 1888c2ecf20Sopenharmony_ci{ 1898c2ecf20Sopenharmony_ci struct clk_hw *hw; 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci hw = clk_hw_register_gate(dev, name, parent_name, flags, reg, 1928c2ecf20Sopenharmony_ci bit_idx, clk_gate_flags, lock); 1938c2ecf20Sopenharmony_ci if (IS_ERR(hw)) 1948c2ecf20Sopenharmony_ci return ERR_CAST(hw); 1958c2ecf20Sopenharmony_ci return hw->clk; 1968c2ecf20Sopenharmony_ci} 1978c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(clk_register_gate); 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_civoid clk_unregister_gate(struct clk *clk) 2008c2ecf20Sopenharmony_ci{ 2018c2ecf20Sopenharmony_ci struct clk_gate *gate; 2028c2ecf20Sopenharmony_ci struct clk_hw *hw; 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci hw = __clk_get_hw(clk); 2058c2ecf20Sopenharmony_ci if (!hw) 2068c2ecf20Sopenharmony_ci return; 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci gate = to_clk_gate(hw); 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci clk_unregister(clk); 2118c2ecf20Sopenharmony_ci kfree(gate); 2128c2ecf20Sopenharmony_ci} 2138c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(clk_unregister_gate); 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_civoid clk_hw_unregister_gate(struct clk_hw *hw) 2168c2ecf20Sopenharmony_ci{ 2178c2ecf20Sopenharmony_ci struct clk_gate *gate; 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci gate = to_clk_gate(hw); 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci clk_hw_unregister(hw); 2228c2ecf20Sopenharmony_ci kfree(gate); 2238c2ecf20Sopenharmony_ci} 2248c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(clk_hw_unregister_gate); 225