18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Hisilicon clock separated gate driver 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 2012-2013 Hisilicon Limited. 68c2ecf20Sopenharmony_ci * Copyright (c) 2012-2013 Linaro Limited. 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Author: Haojian Zhuang <haojian.zhuang@linaro.org> 98c2ecf20Sopenharmony_ci * Xin Li <li.xin@linaro.org> 108c2ecf20Sopenharmony_ci */ 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#include <linux/kernel.h> 138c2ecf20Sopenharmony_ci#include <linux/clk-provider.h> 148c2ecf20Sopenharmony_ci#include <linux/io.h> 158c2ecf20Sopenharmony_ci#include <linux/slab.h> 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#include "clk.h" 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci/* clock separated gate register offset */ 208c2ecf20Sopenharmony_ci#define CLKGATE_SEPERATED_ENABLE 0x0 218c2ecf20Sopenharmony_ci#define CLKGATE_SEPERATED_DISABLE 0x4 228c2ecf20Sopenharmony_ci#define CLKGATE_SEPERATED_STATUS 0x8 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_cistruct clkgate_separated { 258c2ecf20Sopenharmony_ci struct clk_hw hw; 268c2ecf20Sopenharmony_ci void __iomem *enable; /* enable register */ 278c2ecf20Sopenharmony_ci u8 bit_idx; /* bits in enable/disable register */ 288c2ecf20Sopenharmony_ci u8 flags; 298c2ecf20Sopenharmony_ci spinlock_t *lock; 308c2ecf20Sopenharmony_ci}; 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_cistatic int clkgate_separated_enable(struct clk_hw *hw) 338c2ecf20Sopenharmony_ci{ 348c2ecf20Sopenharmony_ci struct clkgate_separated *sclk; 358c2ecf20Sopenharmony_ci unsigned long flags = 0; 368c2ecf20Sopenharmony_ci u32 reg; 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci sclk = container_of(hw, struct clkgate_separated, hw); 398c2ecf20Sopenharmony_ci if (sclk->lock) 408c2ecf20Sopenharmony_ci spin_lock_irqsave(sclk->lock, flags); 418c2ecf20Sopenharmony_ci reg = BIT(sclk->bit_idx); 428c2ecf20Sopenharmony_ci writel_relaxed(reg, sclk->enable); 438c2ecf20Sopenharmony_ci readl_relaxed(sclk->enable + CLKGATE_SEPERATED_STATUS); 448c2ecf20Sopenharmony_ci if (sclk->lock) 458c2ecf20Sopenharmony_ci spin_unlock_irqrestore(sclk->lock, flags); 468c2ecf20Sopenharmony_ci return 0; 478c2ecf20Sopenharmony_ci} 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_cistatic void clkgate_separated_disable(struct clk_hw *hw) 508c2ecf20Sopenharmony_ci{ 518c2ecf20Sopenharmony_ci struct clkgate_separated *sclk; 528c2ecf20Sopenharmony_ci unsigned long flags = 0; 538c2ecf20Sopenharmony_ci u32 reg; 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci sclk = container_of(hw, struct clkgate_separated, hw); 568c2ecf20Sopenharmony_ci if (sclk->lock) 578c2ecf20Sopenharmony_ci spin_lock_irqsave(sclk->lock, flags); 588c2ecf20Sopenharmony_ci reg = BIT(sclk->bit_idx); 598c2ecf20Sopenharmony_ci writel_relaxed(reg, sclk->enable + CLKGATE_SEPERATED_DISABLE); 608c2ecf20Sopenharmony_ci readl_relaxed(sclk->enable + CLKGATE_SEPERATED_STATUS); 618c2ecf20Sopenharmony_ci if (sclk->lock) 628c2ecf20Sopenharmony_ci spin_unlock_irqrestore(sclk->lock, flags); 638c2ecf20Sopenharmony_ci} 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_cistatic int clkgate_separated_is_enabled(struct clk_hw *hw) 668c2ecf20Sopenharmony_ci{ 678c2ecf20Sopenharmony_ci struct clkgate_separated *sclk; 688c2ecf20Sopenharmony_ci u32 reg; 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci sclk = container_of(hw, struct clkgate_separated, hw); 718c2ecf20Sopenharmony_ci reg = readl_relaxed(sclk->enable + CLKGATE_SEPERATED_STATUS); 728c2ecf20Sopenharmony_ci reg &= BIT(sclk->bit_idx); 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci return reg ? 1 : 0; 758c2ecf20Sopenharmony_ci} 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_cistatic const struct clk_ops clkgate_separated_ops = { 788c2ecf20Sopenharmony_ci .enable = clkgate_separated_enable, 798c2ecf20Sopenharmony_ci .disable = clkgate_separated_disable, 808c2ecf20Sopenharmony_ci .is_enabled = clkgate_separated_is_enabled, 818c2ecf20Sopenharmony_ci}; 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_cistruct clk *hisi_register_clkgate_sep(struct device *dev, const char *name, 848c2ecf20Sopenharmony_ci const char *parent_name, 858c2ecf20Sopenharmony_ci unsigned long flags, 868c2ecf20Sopenharmony_ci void __iomem *reg, u8 bit_idx, 878c2ecf20Sopenharmony_ci u8 clk_gate_flags, spinlock_t *lock) 888c2ecf20Sopenharmony_ci{ 898c2ecf20Sopenharmony_ci struct clkgate_separated *sclk; 908c2ecf20Sopenharmony_ci struct clk *clk; 918c2ecf20Sopenharmony_ci struct clk_init_data init; 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci sclk = kzalloc(sizeof(*sclk), GFP_KERNEL); 948c2ecf20Sopenharmony_ci if (!sclk) 958c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci init.name = name; 988c2ecf20Sopenharmony_ci init.ops = &clkgate_separated_ops; 998c2ecf20Sopenharmony_ci init.flags = flags; 1008c2ecf20Sopenharmony_ci init.parent_names = (parent_name ? &parent_name : NULL); 1018c2ecf20Sopenharmony_ci init.num_parents = (parent_name ? 1 : 0); 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci sclk->enable = reg + CLKGATE_SEPERATED_ENABLE; 1048c2ecf20Sopenharmony_ci sclk->bit_idx = bit_idx; 1058c2ecf20Sopenharmony_ci sclk->flags = clk_gate_flags; 1068c2ecf20Sopenharmony_ci sclk->hw.init = &init; 1078c2ecf20Sopenharmony_ci sclk->lock = lock; 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci clk = clk_register(dev, &sclk->hw); 1108c2ecf20Sopenharmony_ci if (IS_ERR(clk)) 1118c2ecf20Sopenharmony_ci kfree(sclk); 1128c2ecf20Sopenharmony_ci return clk; 1138c2ecf20Sopenharmony_ci} 114