162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Hisilicon clock driver 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (c) 2013-2017 Hisilicon Limited. 662306a36Sopenharmony_ci * Copyright (c) 2017 Linaro Limited. 762306a36Sopenharmony_ci * 862306a36Sopenharmony_ci * Author: Kai Zhao <zhaokai1@hisilicon.com> 962306a36Sopenharmony_ci * Tao Wang <kevin.wangtao@hisilicon.com> 1062306a36Sopenharmony_ci * Leo Yan <leo.yan@linaro.org> 1162306a36Sopenharmony_ci */ 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ci#include <linux/clk-provider.h> 1462306a36Sopenharmony_ci#include <linux/device.h> 1562306a36Sopenharmony_ci#include <linux/err.h> 1662306a36Sopenharmony_ci#include <linux/init.h> 1762306a36Sopenharmony_ci#include <linux/io.h> 1862306a36Sopenharmony_ci#include <linux/mailbox_client.h> 1962306a36Sopenharmony_ci#include <linux/module.h> 2062306a36Sopenharmony_ci#include <linux/of.h> 2162306a36Sopenharmony_ci#include <linux/platform_device.h> 2262306a36Sopenharmony_ci#include <dt-bindings/clock/hi3660-clock.h> 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ci#define HI3660_STUB_CLOCK_DATA (0x70) 2562306a36Sopenharmony_ci#define MHZ (1000 * 1000) 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_ci#define DEFINE_CLK_STUB(_id, _cmd, _name) \ 2862306a36Sopenharmony_ci { \ 2962306a36Sopenharmony_ci .id = (_id), \ 3062306a36Sopenharmony_ci .cmd = (_cmd), \ 3162306a36Sopenharmony_ci .hw.init = &(struct clk_init_data) { \ 3262306a36Sopenharmony_ci .name = #_name, \ 3362306a36Sopenharmony_ci .ops = &hi3660_stub_clk_ops, \ 3462306a36Sopenharmony_ci .num_parents = 0, \ 3562306a36Sopenharmony_ci .flags = CLK_GET_RATE_NOCACHE, \ 3662306a36Sopenharmony_ci }, \ 3762306a36Sopenharmony_ci }, 3862306a36Sopenharmony_ci 3962306a36Sopenharmony_ci#define to_stub_clk(_hw) container_of(_hw, struct hi3660_stub_clk, hw) 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_cistruct hi3660_stub_clk_chan { 4262306a36Sopenharmony_ci struct mbox_client cl; 4362306a36Sopenharmony_ci struct mbox_chan *mbox; 4462306a36Sopenharmony_ci}; 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_cistruct hi3660_stub_clk { 4762306a36Sopenharmony_ci unsigned int id; 4862306a36Sopenharmony_ci struct clk_hw hw; 4962306a36Sopenharmony_ci unsigned int cmd; 5062306a36Sopenharmony_ci unsigned int msg[8]; 5162306a36Sopenharmony_ci unsigned int rate; 5262306a36Sopenharmony_ci}; 5362306a36Sopenharmony_ci 5462306a36Sopenharmony_cistatic void __iomem *freq_reg; 5562306a36Sopenharmony_cistatic struct hi3660_stub_clk_chan stub_clk_chan; 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_cistatic unsigned long hi3660_stub_clk_recalc_rate(struct clk_hw *hw, 5862306a36Sopenharmony_ci unsigned long parent_rate) 5962306a36Sopenharmony_ci{ 6062306a36Sopenharmony_ci struct hi3660_stub_clk *stub_clk = to_stub_clk(hw); 6162306a36Sopenharmony_ci 6262306a36Sopenharmony_ci /* 6362306a36Sopenharmony_ci * LPM3 writes back the CPU frequency in shared SRAM so read 6462306a36Sopenharmony_ci * back the frequency. 6562306a36Sopenharmony_ci */ 6662306a36Sopenharmony_ci stub_clk->rate = readl(freq_reg + (stub_clk->id << 2)) * MHZ; 6762306a36Sopenharmony_ci return stub_clk->rate; 6862306a36Sopenharmony_ci} 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_cistatic long hi3660_stub_clk_round_rate(struct clk_hw *hw, unsigned long rate, 7162306a36Sopenharmony_ci unsigned long *prate) 7262306a36Sopenharmony_ci{ 7362306a36Sopenharmony_ci /* 7462306a36Sopenharmony_ci * LPM3 handles rate rounding so just return whatever 7562306a36Sopenharmony_ci * rate is requested. 7662306a36Sopenharmony_ci */ 7762306a36Sopenharmony_ci return rate; 7862306a36Sopenharmony_ci} 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_cistatic int hi3660_stub_clk_set_rate(struct clk_hw *hw, unsigned long rate, 8162306a36Sopenharmony_ci unsigned long parent_rate) 8262306a36Sopenharmony_ci{ 8362306a36Sopenharmony_ci struct hi3660_stub_clk *stub_clk = to_stub_clk(hw); 8462306a36Sopenharmony_ci 8562306a36Sopenharmony_ci stub_clk->msg[0] = stub_clk->cmd; 8662306a36Sopenharmony_ci stub_clk->msg[1] = rate / MHZ; 8762306a36Sopenharmony_ci 8862306a36Sopenharmony_ci dev_dbg(stub_clk_chan.cl.dev, "set rate msg[0]=0x%x msg[1]=0x%x\n", 8962306a36Sopenharmony_ci stub_clk->msg[0], stub_clk->msg[1]); 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_ci mbox_send_message(stub_clk_chan.mbox, stub_clk->msg); 9262306a36Sopenharmony_ci mbox_client_txdone(stub_clk_chan.mbox, 0); 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_ci stub_clk->rate = rate; 9562306a36Sopenharmony_ci return 0; 9662306a36Sopenharmony_ci} 9762306a36Sopenharmony_ci 9862306a36Sopenharmony_cistatic const struct clk_ops hi3660_stub_clk_ops = { 9962306a36Sopenharmony_ci .recalc_rate = hi3660_stub_clk_recalc_rate, 10062306a36Sopenharmony_ci .round_rate = hi3660_stub_clk_round_rate, 10162306a36Sopenharmony_ci .set_rate = hi3660_stub_clk_set_rate, 10262306a36Sopenharmony_ci}; 10362306a36Sopenharmony_ci 10462306a36Sopenharmony_cistatic struct hi3660_stub_clk hi3660_stub_clks[HI3660_CLK_STUB_NUM] = { 10562306a36Sopenharmony_ci DEFINE_CLK_STUB(HI3660_CLK_STUB_CLUSTER0, 0x0001030A, "cpu-cluster.0") 10662306a36Sopenharmony_ci DEFINE_CLK_STUB(HI3660_CLK_STUB_CLUSTER1, 0x0002030A, "cpu-cluster.1") 10762306a36Sopenharmony_ci DEFINE_CLK_STUB(HI3660_CLK_STUB_GPU, 0x0003030A, "clk-g3d") 10862306a36Sopenharmony_ci DEFINE_CLK_STUB(HI3660_CLK_STUB_DDR, 0x00040309, "clk-ddrc") 10962306a36Sopenharmony_ci}; 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_cistatic struct clk_hw *hi3660_stub_clk_hw_get(struct of_phandle_args *clkspec, 11262306a36Sopenharmony_ci void *data) 11362306a36Sopenharmony_ci{ 11462306a36Sopenharmony_ci unsigned int idx = clkspec->args[0]; 11562306a36Sopenharmony_ci 11662306a36Sopenharmony_ci if (idx >= HI3660_CLK_STUB_NUM) { 11762306a36Sopenharmony_ci pr_err("%s: invalid index %u\n", __func__, idx); 11862306a36Sopenharmony_ci return ERR_PTR(-EINVAL); 11962306a36Sopenharmony_ci } 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_ci return &hi3660_stub_clks[idx].hw; 12262306a36Sopenharmony_ci} 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_cistatic int hi3660_stub_clk_probe(struct platform_device *pdev) 12562306a36Sopenharmony_ci{ 12662306a36Sopenharmony_ci struct device *dev = &pdev->dev; 12762306a36Sopenharmony_ci struct resource *res; 12862306a36Sopenharmony_ci unsigned int i; 12962306a36Sopenharmony_ci int ret; 13062306a36Sopenharmony_ci 13162306a36Sopenharmony_ci /* Use mailbox client without blocking */ 13262306a36Sopenharmony_ci stub_clk_chan.cl.dev = dev; 13362306a36Sopenharmony_ci stub_clk_chan.cl.tx_done = NULL; 13462306a36Sopenharmony_ci stub_clk_chan.cl.tx_block = false; 13562306a36Sopenharmony_ci stub_clk_chan.cl.knows_txdone = false; 13662306a36Sopenharmony_ci 13762306a36Sopenharmony_ci /* Allocate mailbox channel */ 13862306a36Sopenharmony_ci stub_clk_chan.mbox = mbox_request_channel(&stub_clk_chan.cl, 0); 13962306a36Sopenharmony_ci if (IS_ERR(stub_clk_chan.mbox)) 14062306a36Sopenharmony_ci return PTR_ERR(stub_clk_chan.mbox); 14162306a36Sopenharmony_ci 14262306a36Sopenharmony_ci res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 14362306a36Sopenharmony_ci if (!res) 14462306a36Sopenharmony_ci return -EINVAL; 14562306a36Sopenharmony_ci freq_reg = devm_ioremap(dev, res->start, resource_size(res)); 14662306a36Sopenharmony_ci if (!freq_reg) 14762306a36Sopenharmony_ci return -ENOMEM; 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ci freq_reg += HI3660_STUB_CLOCK_DATA; 15062306a36Sopenharmony_ci 15162306a36Sopenharmony_ci for (i = 0; i < HI3660_CLK_STUB_NUM; i++) { 15262306a36Sopenharmony_ci ret = devm_clk_hw_register(&pdev->dev, &hi3660_stub_clks[i].hw); 15362306a36Sopenharmony_ci if (ret) 15462306a36Sopenharmony_ci return ret; 15562306a36Sopenharmony_ci } 15662306a36Sopenharmony_ci 15762306a36Sopenharmony_ci return devm_of_clk_add_hw_provider(&pdev->dev, hi3660_stub_clk_hw_get, 15862306a36Sopenharmony_ci hi3660_stub_clks); 15962306a36Sopenharmony_ci} 16062306a36Sopenharmony_ci 16162306a36Sopenharmony_cistatic const struct of_device_id hi3660_stub_clk_of_match[] = { 16262306a36Sopenharmony_ci { .compatible = "hisilicon,hi3660-stub-clk", }, 16362306a36Sopenharmony_ci {} 16462306a36Sopenharmony_ci}; 16562306a36Sopenharmony_ci 16662306a36Sopenharmony_cistatic struct platform_driver hi3660_stub_clk_driver = { 16762306a36Sopenharmony_ci .probe = hi3660_stub_clk_probe, 16862306a36Sopenharmony_ci .driver = { 16962306a36Sopenharmony_ci .name = "hi3660-stub-clk", 17062306a36Sopenharmony_ci .of_match_table = hi3660_stub_clk_of_match, 17162306a36Sopenharmony_ci }, 17262306a36Sopenharmony_ci}; 17362306a36Sopenharmony_ci 17462306a36Sopenharmony_cistatic int __init hi3660_stub_clk_init(void) 17562306a36Sopenharmony_ci{ 17662306a36Sopenharmony_ci return platform_driver_register(&hi3660_stub_clk_driver); 17762306a36Sopenharmony_ci} 17862306a36Sopenharmony_cisubsys_initcall(hi3660_stub_clk_init); 179