18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com> 48c2ecf20Sopenharmony_ci * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org> 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Fixed rate clock implementation 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <linux/clk-provider.h> 108c2ecf20Sopenharmony_ci#include <linux/module.h> 118c2ecf20Sopenharmony_ci#include <linux/slab.h> 128c2ecf20Sopenharmony_ci#include <linux/io.h> 138c2ecf20Sopenharmony_ci#include <linux/err.h> 148c2ecf20Sopenharmony_ci#include <linux/of.h> 158c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci/* 188c2ecf20Sopenharmony_ci * DOC: basic fixed-rate clock that cannot gate 198c2ecf20Sopenharmony_ci * 208c2ecf20Sopenharmony_ci * Traits of this clock: 218c2ecf20Sopenharmony_ci * prepare - clk_(un)prepare only ensures parents are prepared 228c2ecf20Sopenharmony_ci * enable - clk_enable only ensures parents are enabled 238c2ecf20Sopenharmony_ci * rate - rate is always a fixed value. No clk_set_rate support 248c2ecf20Sopenharmony_ci * parent - fixed parent. No clk_set_parent support 258c2ecf20Sopenharmony_ci */ 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci#define to_clk_fixed_rate(_hw) container_of(_hw, struct clk_fixed_rate, hw) 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_cistatic unsigned long clk_fixed_rate_recalc_rate(struct clk_hw *hw, 308c2ecf20Sopenharmony_ci unsigned long parent_rate) 318c2ecf20Sopenharmony_ci{ 328c2ecf20Sopenharmony_ci return to_clk_fixed_rate(hw)->fixed_rate; 338c2ecf20Sopenharmony_ci} 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_cistatic unsigned long clk_fixed_rate_recalc_accuracy(struct clk_hw *hw, 368c2ecf20Sopenharmony_ci unsigned long parent_accuracy) 378c2ecf20Sopenharmony_ci{ 388c2ecf20Sopenharmony_ci struct clk_fixed_rate *fixed = to_clk_fixed_rate(hw); 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci if (fixed->flags & CLK_FIXED_RATE_PARENT_ACCURACY) 418c2ecf20Sopenharmony_ci return parent_accuracy; 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci return fixed->fixed_accuracy; 448c2ecf20Sopenharmony_ci} 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ciconst struct clk_ops clk_fixed_rate_ops = { 478c2ecf20Sopenharmony_ci .recalc_rate = clk_fixed_rate_recalc_rate, 488c2ecf20Sopenharmony_ci .recalc_accuracy = clk_fixed_rate_recalc_accuracy, 498c2ecf20Sopenharmony_ci}; 508c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(clk_fixed_rate_ops); 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_cistatic void devm_clk_hw_register_fixed_rate_release(struct device *dev, void *res) 538c2ecf20Sopenharmony_ci{ 548c2ecf20Sopenharmony_ci struct clk_fixed_rate *fix = res; 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci /* 578c2ecf20Sopenharmony_ci * We can not use clk_hw_unregister_fixed_rate, since it will kfree() 588c2ecf20Sopenharmony_ci * the hw, resulting in double free. Just unregister the hw and let 598c2ecf20Sopenharmony_ci * devres code kfree() it. 608c2ecf20Sopenharmony_ci */ 618c2ecf20Sopenharmony_ci clk_hw_unregister(&fix->hw); 628c2ecf20Sopenharmony_ci} 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_cistruct clk_hw *__clk_hw_register_fixed_rate(struct device *dev, 658c2ecf20Sopenharmony_ci struct device_node *np, const char *name, 668c2ecf20Sopenharmony_ci const char *parent_name, const struct clk_hw *parent_hw, 678c2ecf20Sopenharmony_ci const struct clk_parent_data *parent_data, unsigned long flags, 688c2ecf20Sopenharmony_ci unsigned long fixed_rate, unsigned long fixed_accuracy, 698c2ecf20Sopenharmony_ci unsigned long clk_fixed_flags, bool devm) 708c2ecf20Sopenharmony_ci{ 718c2ecf20Sopenharmony_ci struct clk_fixed_rate *fixed; 728c2ecf20Sopenharmony_ci struct clk_hw *hw; 738c2ecf20Sopenharmony_ci struct clk_init_data init = {}; 748c2ecf20Sopenharmony_ci int ret = -EINVAL; 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci /* allocate fixed-rate clock */ 778c2ecf20Sopenharmony_ci if (devm) 788c2ecf20Sopenharmony_ci fixed = devres_alloc(devm_clk_hw_register_fixed_rate_release, 798c2ecf20Sopenharmony_ci sizeof(*fixed), GFP_KERNEL); 808c2ecf20Sopenharmony_ci else 818c2ecf20Sopenharmony_ci fixed = kzalloc(sizeof(*fixed), GFP_KERNEL); 828c2ecf20Sopenharmony_ci if (!fixed) 838c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci init.name = name; 868c2ecf20Sopenharmony_ci init.ops = &clk_fixed_rate_ops; 878c2ecf20Sopenharmony_ci init.flags = flags; 888c2ecf20Sopenharmony_ci init.parent_names = parent_name ? &parent_name : NULL; 898c2ecf20Sopenharmony_ci init.parent_hws = parent_hw ? &parent_hw : NULL; 908c2ecf20Sopenharmony_ci init.parent_data = parent_data; 918c2ecf20Sopenharmony_ci if (parent_name || parent_hw || parent_data) 928c2ecf20Sopenharmony_ci init.num_parents = 1; 938c2ecf20Sopenharmony_ci else 948c2ecf20Sopenharmony_ci init.num_parents = 0; 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci /* struct clk_fixed_rate assignments */ 978c2ecf20Sopenharmony_ci fixed->flags = clk_fixed_flags; 988c2ecf20Sopenharmony_ci fixed->fixed_rate = fixed_rate; 998c2ecf20Sopenharmony_ci fixed->fixed_accuracy = fixed_accuracy; 1008c2ecf20Sopenharmony_ci fixed->hw.init = &init; 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci /* register the clock */ 1038c2ecf20Sopenharmony_ci hw = &fixed->hw; 1048c2ecf20Sopenharmony_ci if (dev || !np) 1058c2ecf20Sopenharmony_ci ret = clk_hw_register(dev, hw); 1068c2ecf20Sopenharmony_ci else if (np) 1078c2ecf20Sopenharmony_ci ret = of_clk_hw_register(np, hw); 1088c2ecf20Sopenharmony_ci if (ret) { 1098c2ecf20Sopenharmony_ci if (devm) 1108c2ecf20Sopenharmony_ci devres_free(fixed); 1118c2ecf20Sopenharmony_ci else 1128c2ecf20Sopenharmony_ci kfree(fixed); 1138c2ecf20Sopenharmony_ci hw = ERR_PTR(ret); 1148c2ecf20Sopenharmony_ci } else if (devm) 1158c2ecf20Sopenharmony_ci devres_add(dev, fixed); 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci return hw; 1188c2ecf20Sopenharmony_ci} 1198c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(__clk_hw_register_fixed_rate); 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_cistruct clk *clk_register_fixed_rate(struct device *dev, const char *name, 1228c2ecf20Sopenharmony_ci const char *parent_name, unsigned long flags, 1238c2ecf20Sopenharmony_ci unsigned long fixed_rate) 1248c2ecf20Sopenharmony_ci{ 1258c2ecf20Sopenharmony_ci struct clk_hw *hw; 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci hw = clk_hw_register_fixed_rate_with_accuracy(dev, name, parent_name, 1288c2ecf20Sopenharmony_ci flags, fixed_rate, 0); 1298c2ecf20Sopenharmony_ci if (IS_ERR(hw)) 1308c2ecf20Sopenharmony_ci return ERR_CAST(hw); 1318c2ecf20Sopenharmony_ci return hw->clk; 1328c2ecf20Sopenharmony_ci} 1338c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(clk_register_fixed_rate); 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_civoid clk_unregister_fixed_rate(struct clk *clk) 1368c2ecf20Sopenharmony_ci{ 1378c2ecf20Sopenharmony_ci struct clk_hw *hw; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci hw = __clk_get_hw(clk); 1408c2ecf20Sopenharmony_ci if (!hw) 1418c2ecf20Sopenharmony_ci return; 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci clk_unregister(clk); 1448c2ecf20Sopenharmony_ci kfree(to_clk_fixed_rate(hw)); 1458c2ecf20Sopenharmony_ci} 1468c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(clk_unregister_fixed_rate); 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_civoid clk_hw_unregister_fixed_rate(struct clk_hw *hw) 1498c2ecf20Sopenharmony_ci{ 1508c2ecf20Sopenharmony_ci struct clk_fixed_rate *fixed; 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci fixed = to_clk_fixed_rate(hw); 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci clk_hw_unregister(hw); 1558c2ecf20Sopenharmony_ci kfree(fixed); 1568c2ecf20Sopenharmony_ci} 1578c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(clk_hw_unregister_fixed_rate); 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci#ifdef CONFIG_OF 1608c2ecf20Sopenharmony_cistatic struct clk_hw *_of_fixed_clk_setup(struct device_node *node) 1618c2ecf20Sopenharmony_ci{ 1628c2ecf20Sopenharmony_ci struct clk_hw *hw; 1638c2ecf20Sopenharmony_ci const char *clk_name = node->name; 1648c2ecf20Sopenharmony_ci u32 rate; 1658c2ecf20Sopenharmony_ci u32 accuracy = 0; 1668c2ecf20Sopenharmony_ci int ret; 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci if (of_property_read_u32(node, "clock-frequency", &rate)) 1698c2ecf20Sopenharmony_ci return ERR_PTR(-EIO); 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci of_property_read_u32(node, "clock-accuracy", &accuracy); 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci of_property_read_string(node, "clock-output-names", &clk_name); 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci hw = clk_hw_register_fixed_rate_with_accuracy(NULL, clk_name, NULL, 1768c2ecf20Sopenharmony_ci 0, rate, accuracy); 1778c2ecf20Sopenharmony_ci if (IS_ERR(hw)) 1788c2ecf20Sopenharmony_ci return hw; 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci ret = of_clk_add_hw_provider(node, of_clk_hw_simple_get, hw); 1818c2ecf20Sopenharmony_ci if (ret) { 1828c2ecf20Sopenharmony_ci clk_hw_unregister_fixed_rate(hw); 1838c2ecf20Sopenharmony_ci return ERR_PTR(ret); 1848c2ecf20Sopenharmony_ci } 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci return hw; 1878c2ecf20Sopenharmony_ci} 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci/** 1908c2ecf20Sopenharmony_ci * of_fixed_clk_setup() - Setup function for simple fixed rate clock 1918c2ecf20Sopenharmony_ci * @node: device node for the clock 1928c2ecf20Sopenharmony_ci */ 1938c2ecf20Sopenharmony_civoid __init of_fixed_clk_setup(struct device_node *node) 1948c2ecf20Sopenharmony_ci{ 1958c2ecf20Sopenharmony_ci _of_fixed_clk_setup(node); 1968c2ecf20Sopenharmony_ci} 1978c2ecf20Sopenharmony_ciCLK_OF_DECLARE(fixed_clk, "fixed-clock", of_fixed_clk_setup); 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_cistatic int of_fixed_clk_remove(struct platform_device *pdev) 2008c2ecf20Sopenharmony_ci{ 2018c2ecf20Sopenharmony_ci struct clk_hw *hw = platform_get_drvdata(pdev); 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ci of_clk_del_provider(pdev->dev.of_node); 2048c2ecf20Sopenharmony_ci clk_hw_unregister_fixed_rate(hw); 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci return 0; 2078c2ecf20Sopenharmony_ci} 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_cistatic int of_fixed_clk_probe(struct platform_device *pdev) 2108c2ecf20Sopenharmony_ci{ 2118c2ecf20Sopenharmony_ci struct clk_hw *hw; 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci /* 2148c2ecf20Sopenharmony_ci * This function is not executed when of_fixed_clk_setup 2158c2ecf20Sopenharmony_ci * succeeded. 2168c2ecf20Sopenharmony_ci */ 2178c2ecf20Sopenharmony_ci hw = _of_fixed_clk_setup(pdev->dev.of_node); 2188c2ecf20Sopenharmony_ci if (IS_ERR(hw)) 2198c2ecf20Sopenharmony_ci return PTR_ERR(hw); 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, hw); 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ci return 0; 2248c2ecf20Sopenharmony_ci} 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_cistatic const struct of_device_id of_fixed_clk_ids[] = { 2278c2ecf20Sopenharmony_ci { .compatible = "fixed-clock" }, 2288c2ecf20Sopenharmony_ci { } 2298c2ecf20Sopenharmony_ci}; 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_cistatic struct platform_driver of_fixed_clk_driver = { 2328c2ecf20Sopenharmony_ci .driver = { 2338c2ecf20Sopenharmony_ci .name = "of_fixed_clk", 2348c2ecf20Sopenharmony_ci .of_match_table = of_fixed_clk_ids, 2358c2ecf20Sopenharmony_ci }, 2368c2ecf20Sopenharmony_ci .probe = of_fixed_clk_probe, 2378c2ecf20Sopenharmony_ci .remove = of_fixed_clk_remove, 2388c2ecf20Sopenharmony_ci}; 2398c2ecf20Sopenharmony_cibuiltin_platform_driver(of_fixed_clk_driver); 2408c2ecf20Sopenharmony_ci#endif 241