18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Zynq UltraScale+ MPSoC clock controller 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2016-2018 Xilinx 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Gated clock implementation 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/clk-provider.h> 118c2ecf20Sopenharmony_ci#include <linux/slab.h> 128c2ecf20Sopenharmony_ci#include "clk-zynqmp.h" 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci/** 158c2ecf20Sopenharmony_ci * struct clk_gate - gating clock 168c2ecf20Sopenharmony_ci * @hw: handle between common and hardware-specific interfaces 178c2ecf20Sopenharmony_ci * @flags: hardware-specific flags 188c2ecf20Sopenharmony_ci * @clk_id: Id of clock 198c2ecf20Sopenharmony_ci */ 208c2ecf20Sopenharmony_cistruct zynqmp_clk_gate { 218c2ecf20Sopenharmony_ci struct clk_hw hw; 228c2ecf20Sopenharmony_ci u8 flags; 238c2ecf20Sopenharmony_ci u32 clk_id; 248c2ecf20Sopenharmony_ci}; 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci#define to_zynqmp_clk_gate(_hw) container_of(_hw, struct zynqmp_clk_gate, hw) 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci/** 298c2ecf20Sopenharmony_ci * zynqmp_clk_gate_enable() - Enable clock 308c2ecf20Sopenharmony_ci * @hw: handle between common and hardware-specific interfaces 318c2ecf20Sopenharmony_ci * 328c2ecf20Sopenharmony_ci * Return: 0 on success else error code 338c2ecf20Sopenharmony_ci */ 348c2ecf20Sopenharmony_cistatic int zynqmp_clk_gate_enable(struct clk_hw *hw) 358c2ecf20Sopenharmony_ci{ 368c2ecf20Sopenharmony_ci struct zynqmp_clk_gate *gate = to_zynqmp_clk_gate(hw); 378c2ecf20Sopenharmony_ci const char *clk_name = clk_hw_get_name(hw); 388c2ecf20Sopenharmony_ci u32 clk_id = gate->clk_id; 398c2ecf20Sopenharmony_ci int ret; 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci ret = zynqmp_pm_clock_enable(clk_id); 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci if (ret) 448c2ecf20Sopenharmony_ci pr_warn_once("%s() clock enabled failed for %s, ret = %d\n", 458c2ecf20Sopenharmony_ci __func__, clk_name, ret); 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci return ret; 488c2ecf20Sopenharmony_ci} 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci/* 518c2ecf20Sopenharmony_ci * zynqmp_clk_gate_disable() - Disable clock 528c2ecf20Sopenharmony_ci * @hw: handle between common and hardware-specific interfaces 538c2ecf20Sopenharmony_ci */ 548c2ecf20Sopenharmony_cistatic void zynqmp_clk_gate_disable(struct clk_hw *hw) 558c2ecf20Sopenharmony_ci{ 568c2ecf20Sopenharmony_ci struct zynqmp_clk_gate *gate = to_zynqmp_clk_gate(hw); 578c2ecf20Sopenharmony_ci const char *clk_name = clk_hw_get_name(hw); 588c2ecf20Sopenharmony_ci u32 clk_id = gate->clk_id; 598c2ecf20Sopenharmony_ci int ret; 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci ret = zynqmp_pm_clock_disable(clk_id); 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci if (ret) 648c2ecf20Sopenharmony_ci pr_warn_once("%s() clock disable failed for %s, ret = %d\n", 658c2ecf20Sopenharmony_ci __func__, clk_name, ret); 668c2ecf20Sopenharmony_ci} 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci/** 698c2ecf20Sopenharmony_ci * zynqmp_clk_gate_is_enable() - Check clock state 708c2ecf20Sopenharmony_ci * @hw: handle between common and hardware-specific interfaces 718c2ecf20Sopenharmony_ci * 728c2ecf20Sopenharmony_ci * Return: 1 if enabled, 0 if disabled else error code 738c2ecf20Sopenharmony_ci */ 748c2ecf20Sopenharmony_cistatic int zynqmp_clk_gate_is_enabled(struct clk_hw *hw) 758c2ecf20Sopenharmony_ci{ 768c2ecf20Sopenharmony_ci struct zynqmp_clk_gate *gate = to_zynqmp_clk_gate(hw); 778c2ecf20Sopenharmony_ci const char *clk_name = clk_hw_get_name(hw); 788c2ecf20Sopenharmony_ci u32 clk_id = gate->clk_id; 798c2ecf20Sopenharmony_ci int state, ret; 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci ret = zynqmp_pm_clock_getstate(clk_id, &state); 828c2ecf20Sopenharmony_ci if (ret) { 838c2ecf20Sopenharmony_ci pr_warn_once("%s() clock get state failed for %s, ret = %d\n", 848c2ecf20Sopenharmony_ci __func__, clk_name, ret); 858c2ecf20Sopenharmony_ci return -EIO; 868c2ecf20Sopenharmony_ci } 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci return state ? 1 : 0; 898c2ecf20Sopenharmony_ci} 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_cistatic const struct clk_ops zynqmp_clk_gate_ops = { 928c2ecf20Sopenharmony_ci .enable = zynqmp_clk_gate_enable, 938c2ecf20Sopenharmony_ci .disable = zynqmp_clk_gate_disable, 948c2ecf20Sopenharmony_ci .is_enabled = zynqmp_clk_gate_is_enabled, 958c2ecf20Sopenharmony_ci}; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci/** 988c2ecf20Sopenharmony_ci * zynqmp_clk_register_gate() - Register a gate clock with the clock framework 998c2ecf20Sopenharmony_ci * @name: Name of this clock 1008c2ecf20Sopenharmony_ci * @clk_id: Id of this clock 1018c2ecf20Sopenharmony_ci * @parents: Name of this clock's parents 1028c2ecf20Sopenharmony_ci * @num_parents: Number of parents 1038c2ecf20Sopenharmony_ci * @nodes: Clock topology node 1048c2ecf20Sopenharmony_ci * 1058c2ecf20Sopenharmony_ci * Return: clock hardware of the registered clock gate 1068c2ecf20Sopenharmony_ci */ 1078c2ecf20Sopenharmony_cistruct clk_hw *zynqmp_clk_register_gate(const char *name, u32 clk_id, 1088c2ecf20Sopenharmony_ci const char * const *parents, 1098c2ecf20Sopenharmony_ci u8 num_parents, 1108c2ecf20Sopenharmony_ci const struct clock_topology *nodes) 1118c2ecf20Sopenharmony_ci{ 1128c2ecf20Sopenharmony_ci struct zynqmp_clk_gate *gate; 1138c2ecf20Sopenharmony_ci struct clk_hw *hw; 1148c2ecf20Sopenharmony_ci int ret; 1158c2ecf20Sopenharmony_ci struct clk_init_data init; 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci /* allocate the gate */ 1188c2ecf20Sopenharmony_ci gate = kzalloc(sizeof(*gate), GFP_KERNEL); 1198c2ecf20Sopenharmony_ci if (!gate) 1208c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci init.name = name; 1238c2ecf20Sopenharmony_ci init.ops = &zynqmp_clk_gate_ops; 1248c2ecf20Sopenharmony_ci init.flags = nodes->flag; 1258c2ecf20Sopenharmony_ci init.parent_names = parents; 1268c2ecf20Sopenharmony_ci init.num_parents = 1; 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci /* struct clk_gate assignments */ 1298c2ecf20Sopenharmony_ci gate->flags = nodes->type_flag; 1308c2ecf20Sopenharmony_ci gate->hw.init = &init; 1318c2ecf20Sopenharmony_ci gate->clk_id = clk_id; 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci hw = &gate->hw; 1348c2ecf20Sopenharmony_ci ret = clk_hw_register(NULL, hw); 1358c2ecf20Sopenharmony_ci if (ret) { 1368c2ecf20Sopenharmony_ci kfree(gate); 1378c2ecf20Sopenharmony_ci hw = ERR_PTR(ret); 1388c2ecf20Sopenharmony_ci } 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci return hw; 1418c2ecf20Sopenharmony_ci} 142