18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci// 38c2ecf20Sopenharmony_ci// Spreadtrum gate clock driver 48c2ecf20Sopenharmony_ci// 58c2ecf20Sopenharmony_ci// Copyright (C) 2017 Spreadtrum, Inc. 68c2ecf20Sopenharmony_ci// Author: Chunyan Zhang <chunyan.zhang@spreadtrum.com> 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/clk-provider.h> 98c2ecf20Sopenharmony_ci#include <linux/regmap.h> 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include "gate.h" 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_cistatic void clk_gate_toggle(const struct sprd_gate *sg, bool en) 148c2ecf20Sopenharmony_ci{ 158c2ecf20Sopenharmony_ci const struct sprd_clk_common *common = &sg->common; 168c2ecf20Sopenharmony_ci unsigned int reg; 178c2ecf20Sopenharmony_ci bool set = sg->flags & CLK_GATE_SET_TO_DISABLE ? true : false; 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci set ^= en; 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci regmap_read(common->regmap, common->reg, ®); 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci if (set) 248c2ecf20Sopenharmony_ci reg |= sg->enable_mask; 258c2ecf20Sopenharmony_ci else 268c2ecf20Sopenharmony_ci reg &= ~sg->enable_mask; 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci regmap_write(common->regmap, common->reg, reg); 298c2ecf20Sopenharmony_ci} 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_cistatic void clk_sc_gate_toggle(const struct sprd_gate *sg, bool en) 328c2ecf20Sopenharmony_ci{ 338c2ecf20Sopenharmony_ci const struct sprd_clk_common *common = &sg->common; 348c2ecf20Sopenharmony_ci bool set = sg->flags & CLK_GATE_SET_TO_DISABLE ? 1 : 0; 358c2ecf20Sopenharmony_ci unsigned int offset; 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci set ^= en; 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci /* 408c2ecf20Sopenharmony_ci * Each set/clear gate clock has three registers: 418c2ecf20Sopenharmony_ci * common->reg - base register 428c2ecf20Sopenharmony_ci * common->reg + offset - set register 438c2ecf20Sopenharmony_ci * common->reg + 2 * offset - clear register 448c2ecf20Sopenharmony_ci */ 458c2ecf20Sopenharmony_ci offset = set ? sg->sc_offset : sg->sc_offset * 2; 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci regmap_write(common->regmap, common->reg + offset, 488c2ecf20Sopenharmony_ci sg->enable_mask); 498c2ecf20Sopenharmony_ci} 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_cistatic void sprd_gate_disable(struct clk_hw *hw) 528c2ecf20Sopenharmony_ci{ 538c2ecf20Sopenharmony_ci struct sprd_gate *sg = hw_to_sprd_gate(hw); 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci clk_gate_toggle(sg, false); 568c2ecf20Sopenharmony_ci} 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_cistatic int sprd_gate_enable(struct clk_hw *hw) 598c2ecf20Sopenharmony_ci{ 608c2ecf20Sopenharmony_ci struct sprd_gate *sg = hw_to_sprd_gate(hw); 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci clk_gate_toggle(sg, true); 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci return 0; 658c2ecf20Sopenharmony_ci} 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_cistatic void sprd_sc_gate_disable(struct clk_hw *hw) 688c2ecf20Sopenharmony_ci{ 698c2ecf20Sopenharmony_ci struct sprd_gate *sg = hw_to_sprd_gate(hw); 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci clk_sc_gate_toggle(sg, false); 728c2ecf20Sopenharmony_ci} 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_cistatic int sprd_sc_gate_enable(struct clk_hw *hw) 758c2ecf20Sopenharmony_ci{ 768c2ecf20Sopenharmony_ci struct sprd_gate *sg = hw_to_sprd_gate(hw); 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci clk_sc_gate_toggle(sg, true); 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci return 0; 818c2ecf20Sopenharmony_ci} 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_cistatic int sprd_pll_sc_gate_prepare(struct clk_hw *hw) 848c2ecf20Sopenharmony_ci{ 858c2ecf20Sopenharmony_ci struct sprd_gate *sg = hw_to_sprd_gate(hw); 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci clk_sc_gate_toggle(sg, true); 888c2ecf20Sopenharmony_ci udelay(sg->udelay); 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci return 0; 918c2ecf20Sopenharmony_ci} 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_cistatic int sprd_gate_is_enabled(struct clk_hw *hw) 948c2ecf20Sopenharmony_ci{ 958c2ecf20Sopenharmony_ci struct sprd_gate *sg = hw_to_sprd_gate(hw); 968c2ecf20Sopenharmony_ci struct sprd_clk_common *common = &sg->common; 978c2ecf20Sopenharmony_ci struct clk_hw *parent; 988c2ecf20Sopenharmony_ci unsigned int reg; 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci if (sg->flags & SPRD_GATE_NON_AON) { 1018c2ecf20Sopenharmony_ci parent = clk_hw_get_parent(hw); 1028c2ecf20Sopenharmony_ci if (!parent || !clk_hw_is_enabled(parent)) 1038c2ecf20Sopenharmony_ci return 0; 1048c2ecf20Sopenharmony_ci } 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci regmap_read(common->regmap, common->reg, ®); 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci if (sg->flags & CLK_GATE_SET_TO_DISABLE) 1098c2ecf20Sopenharmony_ci reg ^= sg->enable_mask; 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci reg &= sg->enable_mask; 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci return reg ? 1 : 0; 1148c2ecf20Sopenharmony_ci} 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ciconst struct clk_ops sprd_gate_ops = { 1178c2ecf20Sopenharmony_ci .disable = sprd_gate_disable, 1188c2ecf20Sopenharmony_ci .enable = sprd_gate_enable, 1198c2ecf20Sopenharmony_ci .is_enabled = sprd_gate_is_enabled, 1208c2ecf20Sopenharmony_ci}; 1218c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sprd_gate_ops); 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ciconst struct clk_ops sprd_sc_gate_ops = { 1248c2ecf20Sopenharmony_ci .disable = sprd_sc_gate_disable, 1258c2ecf20Sopenharmony_ci .enable = sprd_sc_gate_enable, 1268c2ecf20Sopenharmony_ci .is_enabled = sprd_gate_is_enabled, 1278c2ecf20Sopenharmony_ci}; 1288c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sprd_sc_gate_ops); 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ciconst struct clk_ops sprd_pll_sc_gate_ops = { 1318c2ecf20Sopenharmony_ci .unprepare = sprd_sc_gate_disable, 1328c2ecf20Sopenharmony_ci .prepare = sprd_pll_sc_gate_prepare, 1338c2ecf20Sopenharmony_ci .is_enabled = sprd_gate_is_enabled, 1348c2ecf20Sopenharmony_ci}; 1358c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sprd_pll_sc_gate_ops); 136