18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 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/export.h> 118c2ecf20Sopenharmony_ci#include <linux/module.h> 128c2ecf20Sopenharmony_ci#include <linux/slab.h> 138c2ecf20Sopenharmony_ci#include <linux/io.h> 148c2ecf20Sopenharmony_ci#include <linux/err.h> 158c2ecf20Sopenharmony_ci#include <linux/string.h> 168c2ecf20Sopenharmony_ci#include "clk.h" 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci/** 198c2ecf20Sopenharmony_ci * DOC: basic gateable clock which can gate and ungate its output 208c2ecf20Sopenharmony_ci * 218c2ecf20Sopenharmony_ci * Traits of this clock: 228c2ecf20Sopenharmony_ci * prepare - clk_(un)prepare only ensures parent is (un)prepared 238c2ecf20Sopenharmony_ci * enable - clk_enable and clk_disable are functional & control gating 248c2ecf20Sopenharmony_ci * rate - inherits rate from parent. No clk_set_rate support 258c2ecf20Sopenharmony_ci * parent - fixed parent. No clk_set_parent support 268c2ecf20Sopenharmony_ci */ 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_cistruct clk_gate2 { 298c2ecf20Sopenharmony_ci struct clk_hw hw; 308c2ecf20Sopenharmony_ci void __iomem *reg; 318c2ecf20Sopenharmony_ci u8 bit_idx; 328c2ecf20Sopenharmony_ci u8 cgr_val; 338c2ecf20Sopenharmony_ci u8 flags; 348c2ecf20Sopenharmony_ci spinlock_t *lock; 358c2ecf20Sopenharmony_ci unsigned int *share_count; 368c2ecf20Sopenharmony_ci}; 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci#define to_clk_gate2(_hw) container_of(_hw, struct clk_gate2, hw) 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_cistatic int clk_gate2_enable(struct clk_hw *hw) 418c2ecf20Sopenharmony_ci{ 428c2ecf20Sopenharmony_ci struct clk_gate2 *gate = to_clk_gate2(hw); 438c2ecf20Sopenharmony_ci u32 reg; 448c2ecf20Sopenharmony_ci unsigned long flags; 458c2ecf20Sopenharmony_ci int ret = 0; 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci spin_lock_irqsave(gate->lock, flags); 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci if (gate->share_count && (*gate->share_count)++ > 0) 508c2ecf20Sopenharmony_ci goto out; 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci if (gate->flags & IMX_CLK_GATE2_SINGLE_BIT) { 538c2ecf20Sopenharmony_ci ret = clk_gate_ops.enable(hw); 548c2ecf20Sopenharmony_ci } else { 558c2ecf20Sopenharmony_ci reg = readl(gate->reg); 568c2ecf20Sopenharmony_ci reg &= ~(3 << gate->bit_idx); 578c2ecf20Sopenharmony_ci reg |= gate->cgr_val << gate->bit_idx; 588c2ecf20Sopenharmony_ci writel(reg, gate->reg); 598c2ecf20Sopenharmony_ci } 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ciout: 628c2ecf20Sopenharmony_ci spin_unlock_irqrestore(gate->lock, flags); 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci return ret; 658c2ecf20Sopenharmony_ci} 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_cistatic void clk_gate2_disable(struct clk_hw *hw) 688c2ecf20Sopenharmony_ci{ 698c2ecf20Sopenharmony_ci struct clk_gate2 *gate = to_clk_gate2(hw); 708c2ecf20Sopenharmony_ci u32 reg; 718c2ecf20Sopenharmony_ci unsigned long flags; 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci spin_lock_irqsave(gate->lock, flags); 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci if (gate->share_count) { 768c2ecf20Sopenharmony_ci if (WARN_ON(*gate->share_count == 0)) 778c2ecf20Sopenharmony_ci goto out; 788c2ecf20Sopenharmony_ci else if (--(*gate->share_count) > 0) 798c2ecf20Sopenharmony_ci goto out; 808c2ecf20Sopenharmony_ci } 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci if (gate->flags & IMX_CLK_GATE2_SINGLE_BIT) { 838c2ecf20Sopenharmony_ci clk_gate_ops.disable(hw); 848c2ecf20Sopenharmony_ci } else { 858c2ecf20Sopenharmony_ci reg = readl(gate->reg); 868c2ecf20Sopenharmony_ci reg &= ~(3 << gate->bit_idx); 878c2ecf20Sopenharmony_ci writel(reg, gate->reg); 888c2ecf20Sopenharmony_ci } 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ciout: 918c2ecf20Sopenharmony_ci spin_unlock_irqrestore(gate->lock, flags); 928c2ecf20Sopenharmony_ci} 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_cistatic int clk_gate2_reg_is_enabled(void __iomem *reg, u8 bit_idx) 958c2ecf20Sopenharmony_ci{ 968c2ecf20Sopenharmony_ci u32 val = readl(reg); 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci if (((val >> bit_idx) & 1) == 1) 998c2ecf20Sopenharmony_ci return 1; 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci return 0; 1028c2ecf20Sopenharmony_ci} 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_cistatic int clk_gate2_is_enabled(struct clk_hw *hw) 1058c2ecf20Sopenharmony_ci{ 1068c2ecf20Sopenharmony_ci struct clk_gate2 *gate = to_clk_gate2(hw); 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci if (gate->flags & IMX_CLK_GATE2_SINGLE_BIT) 1098c2ecf20Sopenharmony_ci return clk_gate_ops.is_enabled(hw); 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci return clk_gate2_reg_is_enabled(gate->reg, gate->bit_idx); 1128c2ecf20Sopenharmony_ci} 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_cistatic void clk_gate2_disable_unused(struct clk_hw *hw) 1158c2ecf20Sopenharmony_ci{ 1168c2ecf20Sopenharmony_ci struct clk_gate2 *gate = to_clk_gate2(hw); 1178c2ecf20Sopenharmony_ci unsigned long flags; 1188c2ecf20Sopenharmony_ci u32 reg; 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci if (gate->flags & IMX_CLK_GATE2_SINGLE_BIT) 1218c2ecf20Sopenharmony_ci return; 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci spin_lock_irqsave(gate->lock, flags); 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci if (!gate->share_count || *gate->share_count == 0) { 1268c2ecf20Sopenharmony_ci reg = readl(gate->reg); 1278c2ecf20Sopenharmony_ci reg &= ~(3 << gate->bit_idx); 1288c2ecf20Sopenharmony_ci writel(reg, gate->reg); 1298c2ecf20Sopenharmony_ci } 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci spin_unlock_irqrestore(gate->lock, flags); 1328c2ecf20Sopenharmony_ci} 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_cistatic const struct clk_ops clk_gate2_ops = { 1358c2ecf20Sopenharmony_ci .enable = clk_gate2_enable, 1368c2ecf20Sopenharmony_ci .disable = clk_gate2_disable, 1378c2ecf20Sopenharmony_ci .disable_unused = clk_gate2_disable_unused, 1388c2ecf20Sopenharmony_ci .is_enabled = clk_gate2_is_enabled, 1398c2ecf20Sopenharmony_ci}; 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_cistruct clk_hw *clk_hw_register_gate2(struct device *dev, const char *name, 1428c2ecf20Sopenharmony_ci const char *parent_name, unsigned long flags, 1438c2ecf20Sopenharmony_ci void __iomem *reg, u8 bit_idx, u8 cgr_val, 1448c2ecf20Sopenharmony_ci u8 clk_gate2_flags, spinlock_t *lock, 1458c2ecf20Sopenharmony_ci unsigned int *share_count) 1468c2ecf20Sopenharmony_ci{ 1478c2ecf20Sopenharmony_ci struct clk_gate2 *gate; 1488c2ecf20Sopenharmony_ci struct clk_hw *hw; 1498c2ecf20Sopenharmony_ci struct clk_init_data init; 1508c2ecf20Sopenharmony_ci int ret; 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci gate = kzalloc(sizeof(struct clk_gate2), GFP_KERNEL); 1538c2ecf20Sopenharmony_ci if (!gate) 1548c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci /* struct clk_gate2 assignments */ 1578c2ecf20Sopenharmony_ci gate->reg = reg; 1588c2ecf20Sopenharmony_ci gate->bit_idx = bit_idx; 1598c2ecf20Sopenharmony_ci gate->cgr_val = cgr_val; 1608c2ecf20Sopenharmony_ci gate->flags = clk_gate2_flags; 1618c2ecf20Sopenharmony_ci gate->lock = lock; 1628c2ecf20Sopenharmony_ci gate->share_count = share_count; 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci init.name = name; 1658c2ecf20Sopenharmony_ci init.ops = &clk_gate2_ops; 1668c2ecf20Sopenharmony_ci init.flags = flags; 1678c2ecf20Sopenharmony_ci init.parent_names = parent_name ? &parent_name : NULL; 1688c2ecf20Sopenharmony_ci init.num_parents = parent_name ? 1 : 0; 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci gate->hw.init = &init; 1718c2ecf20Sopenharmony_ci hw = &gate->hw; 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci ret = clk_hw_register(dev, hw); 1748c2ecf20Sopenharmony_ci if (ret) { 1758c2ecf20Sopenharmony_ci kfree(gate); 1768c2ecf20Sopenharmony_ci return ERR_PTR(ret); 1778c2ecf20Sopenharmony_ci } 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci return hw; 1808c2ecf20Sopenharmony_ci} 1818c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(clk_hw_register_gate2); 182