18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com> 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#include <linux/clk-provider.h> 78c2ecf20Sopenharmony_ci#include <linux/clkdev.h> 88c2ecf20Sopenharmony_ci#include <linux/clk/at91_pmc.h> 98c2ecf20Sopenharmony_ci#include <linux/of.h> 108c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h> 118c2ecf20Sopenharmony_ci#include <linux/regmap.h> 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#include "pmc.h" 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#define SYSTEM_MAX_ID 31 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#define SYSTEM_MAX_NAME_SZ 32 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#define to_clk_system(hw) container_of(hw, struct clk_system, hw) 208c2ecf20Sopenharmony_cistruct clk_system { 218c2ecf20Sopenharmony_ci struct clk_hw hw; 228c2ecf20Sopenharmony_ci struct regmap *regmap; 238c2ecf20Sopenharmony_ci u8 id; 248c2ecf20Sopenharmony_ci}; 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_cistatic inline int is_pck(int id) 278c2ecf20Sopenharmony_ci{ 288c2ecf20Sopenharmony_ci return (id >= 8) && (id <= 15); 298c2ecf20Sopenharmony_ci} 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_cistatic inline bool clk_system_ready(struct regmap *regmap, int id) 328c2ecf20Sopenharmony_ci{ 338c2ecf20Sopenharmony_ci unsigned int status; 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci regmap_read(regmap, AT91_PMC_SR, &status); 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci return !!(status & (1 << id)); 388c2ecf20Sopenharmony_ci} 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_cistatic int clk_system_prepare(struct clk_hw *hw) 418c2ecf20Sopenharmony_ci{ 428c2ecf20Sopenharmony_ci struct clk_system *sys = to_clk_system(hw); 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci regmap_write(sys->regmap, AT91_PMC_SCER, 1 << sys->id); 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci if (!is_pck(sys->id)) 478c2ecf20Sopenharmony_ci return 0; 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci while (!clk_system_ready(sys->regmap, sys->id)) 508c2ecf20Sopenharmony_ci cpu_relax(); 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci return 0; 538c2ecf20Sopenharmony_ci} 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_cistatic void clk_system_unprepare(struct clk_hw *hw) 568c2ecf20Sopenharmony_ci{ 578c2ecf20Sopenharmony_ci struct clk_system *sys = to_clk_system(hw); 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci regmap_write(sys->regmap, AT91_PMC_SCDR, 1 << sys->id); 608c2ecf20Sopenharmony_ci} 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_cistatic int clk_system_is_prepared(struct clk_hw *hw) 638c2ecf20Sopenharmony_ci{ 648c2ecf20Sopenharmony_ci struct clk_system *sys = to_clk_system(hw); 658c2ecf20Sopenharmony_ci unsigned int status; 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci regmap_read(sys->regmap, AT91_PMC_SCSR, &status); 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci if (!(status & (1 << sys->id))) 708c2ecf20Sopenharmony_ci return 0; 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci if (!is_pck(sys->id)) 738c2ecf20Sopenharmony_ci return 1; 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci regmap_read(sys->regmap, AT91_PMC_SR, &status); 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci return !!(status & (1 << sys->id)); 788c2ecf20Sopenharmony_ci} 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_cistatic const struct clk_ops system_ops = { 818c2ecf20Sopenharmony_ci .prepare = clk_system_prepare, 828c2ecf20Sopenharmony_ci .unprepare = clk_system_unprepare, 838c2ecf20Sopenharmony_ci .is_prepared = clk_system_is_prepared, 848c2ecf20Sopenharmony_ci}; 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_cistruct clk_hw * __init 878c2ecf20Sopenharmony_ciat91_clk_register_system(struct regmap *regmap, const char *name, 888c2ecf20Sopenharmony_ci const char *parent_name, u8 id) 898c2ecf20Sopenharmony_ci{ 908c2ecf20Sopenharmony_ci struct clk_system *sys; 918c2ecf20Sopenharmony_ci struct clk_hw *hw; 928c2ecf20Sopenharmony_ci struct clk_init_data init; 938c2ecf20Sopenharmony_ci int ret; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci if (!parent_name || id > SYSTEM_MAX_ID) 968c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci sys = kzalloc(sizeof(*sys), GFP_KERNEL); 998c2ecf20Sopenharmony_ci if (!sys) 1008c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci init.name = name; 1038c2ecf20Sopenharmony_ci init.ops = &system_ops; 1048c2ecf20Sopenharmony_ci init.parent_names = &parent_name; 1058c2ecf20Sopenharmony_ci init.num_parents = 1; 1068c2ecf20Sopenharmony_ci init.flags = CLK_SET_RATE_PARENT; 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci sys->id = id; 1098c2ecf20Sopenharmony_ci sys->hw.init = &init; 1108c2ecf20Sopenharmony_ci sys->regmap = regmap; 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci hw = &sys->hw; 1138c2ecf20Sopenharmony_ci ret = clk_hw_register(NULL, &sys->hw); 1148c2ecf20Sopenharmony_ci if (ret) { 1158c2ecf20Sopenharmony_ci kfree(sys); 1168c2ecf20Sopenharmony_ci hw = ERR_PTR(ret); 1178c2ecf20Sopenharmony_ci } 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci return hw; 1208c2ecf20Sopenharmony_ci} 121