18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2016 Rafał Miłecki <rafal@milecki.pl> 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#include <linux/clk-provider.h> 78c2ecf20Sopenharmony_ci#include <linux/err.h> 88c2ecf20Sopenharmony_ci#include <linux/io.h> 98c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h> 108c2ecf20Sopenharmony_ci#include <linux/of.h> 118c2ecf20Sopenharmony_ci#include <linux/of_address.h> 128c2ecf20Sopenharmony_ci#include <linux/regmap.h> 138c2ecf20Sopenharmony_ci#include <linux/slab.h> 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#define PMU_XTAL_FREQ_RATIO 0x66c 168c2ecf20Sopenharmony_ci#define XTAL_ALP_PER_4ILP 0x00001fff 178c2ecf20Sopenharmony_ci#define XTAL_CTL_EN 0x80000000 188c2ecf20Sopenharmony_ci#define PMU_SLOW_CLK_PERIOD 0x6dc 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_cistruct bcm53573_ilp { 218c2ecf20Sopenharmony_ci struct clk_hw hw; 228c2ecf20Sopenharmony_ci struct regmap *regmap; 238c2ecf20Sopenharmony_ci}; 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_cistatic int bcm53573_ilp_enable(struct clk_hw *hw) 268c2ecf20Sopenharmony_ci{ 278c2ecf20Sopenharmony_ci struct bcm53573_ilp *ilp = container_of(hw, struct bcm53573_ilp, hw); 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci regmap_write(ilp->regmap, PMU_SLOW_CLK_PERIOD, 0x10199); 308c2ecf20Sopenharmony_ci regmap_write(ilp->regmap, 0x674, 0x10000); 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci return 0; 338c2ecf20Sopenharmony_ci} 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_cistatic void bcm53573_ilp_disable(struct clk_hw *hw) 368c2ecf20Sopenharmony_ci{ 378c2ecf20Sopenharmony_ci struct bcm53573_ilp *ilp = container_of(hw, struct bcm53573_ilp, hw); 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci regmap_write(ilp->regmap, PMU_SLOW_CLK_PERIOD, 0); 408c2ecf20Sopenharmony_ci regmap_write(ilp->regmap, 0x674, 0); 418c2ecf20Sopenharmony_ci} 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_cistatic unsigned long bcm53573_ilp_recalc_rate(struct clk_hw *hw, 448c2ecf20Sopenharmony_ci unsigned long parent_rate) 458c2ecf20Sopenharmony_ci{ 468c2ecf20Sopenharmony_ci struct bcm53573_ilp *ilp = container_of(hw, struct bcm53573_ilp, hw); 478c2ecf20Sopenharmony_ci struct regmap *regmap = ilp->regmap; 488c2ecf20Sopenharmony_ci u32 last_val, cur_val; 498c2ecf20Sopenharmony_ci int sum = 0, num = 0, loop_num = 0; 508c2ecf20Sopenharmony_ci int avg; 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci /* Enable measurement */ 538c2ecf20Sopenharmony_ci regmap_write(regmap, PMU_XTAL_FREQ_RATIO, XTAL_CTL_EN); 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci /* Read initial value */ 568c2ecf20Sopenharmony_ci regmap_read(regmap, PMU_XTAL_FREQ_RATIO, &last_val); 578c2ecf20Sopenharmony_ci last_val &= XTAL_ALP_PER_4ILP; 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci /* 608c2ecf20Sopenharmony_ci * At minimum we should loop for a bit to let hardware do the 618c2ecf20Sopenharmony_ci * measurement. This isn't very accurate however, so for a better 628c2ecf20Sopenharmony_ci * precision lets try getting 20 different values for and use average. 638c2ecf20Sopenharmony_ci */ 648c2ecf20Sopenharmony_ci while (num < 20) { 658c2ecf20Sopenharmony_ci regmap_read(regmap, PMU_XTAL_FREQ_RATIO, &cur_val); 668c2ecf20Sopenharmony_ci cur_val &= XTAL_ALP_PER_4ILP; 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci if (cur_val != last_val) { 698c2ecf20Sopenharmony_ci /* Got different value, use it */ 708c2ecf20Sopenharmony_ci sum += cur_val; 718c2ecf20Sopenharmony_ci num++; 728c2ecf20Sopenharmony_ci loop_num = 0; 738c2ecf20Sopenharmony_ci last_val = cur_val; 748c2ecf20Sopenharmony_ci } else if (++loop_num > 5000) { 758c2ecf20Sopenharmony_ci /* Same value over and over, give up */ 768c2ecf20Sopenharmony_ci sum += cur_val; 778c2ecf20Sopenharmony_ci num++; 788c2ecf20Sopenharmony_ci break; 798c2ecf20Sopenharmony_ci } 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci cpu_relax(); 828c2ecf20Sopenharmony_ci } 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci /* Disable measurement to save power */ 858c2ecf20Sopenharmony_ci regmap_write(regmap, PMU_XTAL_FREQ_RATIO, 0x0); 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci avg = sum / num; 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci return parent_rate * 4 / avg; 908c2ecf20Sopenharmony_ci} 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_cistatic const struct clk_ops bcm53573_ilp_clk_ops = { 938c2ecf20Sopenharmony_ci .enable = bcm53573_ilp_enable, 948c2ecf20Sopenharmony_ci .disable = bcm53573_ilp_disable, 958c2ecf20Sopenharmony_ci .recalc_rate = bcm53573_ilp_recalc_rate, 968c2ecf20Sopenharmony_ci}; 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_cistatic void bcm53573_ilp_init(struct device_node *np) 998c2ecf20Sopenharmony_ci{ 1008c2ecf20Sopenharmony_ci struct bcm53573_ilp *ilp; 1018c2ecf20Sopenharmony_ci struct clk_init_data init = { }; 1028c2ecf20Sopenharmony_ci const char *parent_name; 1038c2ecf20Sopenharmony_ci int err; 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci ilp = kzalloc(sizeof(*ilp), GFP_KERNEL); 1068c2ecf20Sopenharmony_ci if (!ilp) 1078c2ecf20Sopenharmony_ci return; 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci parent_name = of_clk_get_parent_name(np, 0); 1108c2ecf20Sopenharmony_ci if (!parent_name) { 1118c2ecf20Sopenharmony_ci err = -ENOENT; 1128c2ecf20Sopenharmony_ci goto err_free_ilp; 1138c2ecf20Sopenharmony_ci } 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci ilp->regmap = syscon_node_to_regmap(of_get_parent(np)); 1168c2ecf20Sopenharmony_ci if (IS_ERR(ilp->regmap)) { 1178c2ecf20Sopenharmony_ci err = PTR_ERR(ilp->regmap); 1188c2ecf20Sopenharmony_ci goto err_free_ilp; 1198c2ecf20Sopenharmony_ci } 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci init.name = np->name; 1228c2ecf20Sopenharmony_ci init.ops = &bcm53573_ilp_clk_ops; 1238c2ecf20Sopenharmony_ci init.parent_names = &parent_name; 1248c2ecf20Sopenharmony_ci init.num_parents = 1; 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci ilp->hw.init = &init; 1278c2ecf20Sopenharmony_ci err = clk_hw_register(NULL, &ilp->hw); 1288c2ecf20Sopenharmony_ci if (err) 1298c2ecf20Sopenharmony_ci goto err_free_ilp; 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci err = of_clk_add_hw_provider(np, of_clk_hw_simple_get, &ilp->hw); 1328c2ecf20Sopenharmony_ci if (err) 1338c2ecf20Sopenharmony_ci goto err_clk_hw_unregister; 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci return; 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_cierr_clk_hw_unregister: 1388c2ecf20Sopenharmony_ci clk_hw_unregister(&ilp->hw); 1398c2ecf20Sopenharmony_cierr_free_ilp: 1408c2ecf20Sopenharmony_ci kfree(ilp); 1418c2ecf20Sopenharmony_ci pr_err("Failed to init ILP clock: %d\n", err); 1428c2ecf20Sopenharmony_ci} 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci/* We need it very early for arch code, before device model gets ready */ 1458c2ecf20Sopenharmony_ciCLK_OF_DECLARE(bcm53573_ilp_clk, "brcm,bcm53573-ilp", bcm53573_ilp_init); 146