2 * Hisilicon clock driver
4 * Copyright (c) 2013-2017 Hisilicon Limited.
5 * Copyright (c) 2017 Linaro Limited.
7 * Author: Kai Zhao <zhaokai1@hisilicon.com>
8 * Tao Wang <kevin.wangtao@hisilicon.com>
9 * Leo Yan <leo.yan@linaro.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
23 #include <linux/clk-provider.h>
24 #include <linux/device.h>
25 #include <linux/err.h>
26 #include <linux/init.h>
27 #include <linux/mailbox_client.h>
28 #include <linux/module.h>
30 #include <linux/platform_device.h>
31 #include <dt-bindings/clock/hi3660-clock.h>
33 #define HI3660_STUB_CLOCK_DATA (0x70)
34 #define MHZ (1000 * 1000)
36 #define DEFINE_CLK_STUB(_id, _cmd, _name) \
40 .hw.init = &(struct clk_init_data) { \
42 .ops = &hi3660_stub_clk_ops, \
44 .flags = CLK_GET_RATE_NOCACHE, \
48 #define to_stub_clk(_hw) container_of(_hw, struct hi3660_stub_clk, hw)
50 struct hi3660_stub_clk_chan {
51 struct mbox_client cl;
52 struct mbox_chan *mbox;
55 struct hi3660_stub_clk {
63 static void __iomem *freq_reg;
64 static struct hi3660_stub_clk_chan stub_clk_chan;
66 static unsigned long hi3660_stub_clk_recalc_rate(struct clk_hw *hw,
67 unsigned long parent_rate)
69 struct hi3660_stub_clk *stub_clk = to_stub_clk(hw);
72 * LPM3 writes back the CPU frequency in shared SRAM so read
75 stub_clk->rate = readl(freq_reg + (stub_clk->id << 2)) * MHZ;
76 return stub_clk->rate;
79 static long hi3660_stub_clk_round_rate(struct clk_hw *hw, unsigned long rate,
83 * LPM3 handles rate rounding so just return whatever
89 static int hi3660_stub_clk_set_rate(struct clk_hw *hw, unsigned long rate,
90 unsigned long parent_rate)
92 struct hi3660_stub_clk *stub_clk = to_stub_clk(hw);
94 stub_clk->msg[0] = stub_clk->cmd;
95 stub_clk->msg[1] = rate / MHZ;
97 dev_dbg(stub_clk_chan.cl.dev, "set rate msg[0]=0x%x msg[1]=0x%x\n",
98 stub_clk->msg[0], stub_clk->msg[1]);
100 mbox_send_message(stub_clk_chan.mbox, stub_clk->msg);
101 mbox_client_txdone(stub_clk_chan.mbox, 0);
103 stub_clk->rate = rate;
107 static const struct clk_ops hi3660_stub_clk_ops = {
108 .recalc_rate = hi3660_stub_clk_recalc_rate,
109 .round_rate = hi3660_stub_clk_round_rate,
110 .set_rate = hi3660_stub_clk_set_rate,
113 static struct hi3660_stub_clk hi3660_stub_clks[HI3660_CLK_STUB_NUM] = {
114 DEFINE_CLK_STUB(HI3660_CLK_STUB_CLUSTER0, 0x0001030A, "cpu-cluster.0")
115 DEFINE_CLK_STUB(HI3660_CLK_STUB_CLUSTER1, 0x0002030A, "cpu-cluster.1")
116 DEFINE_CLK_STUB(HI3660_CLK_STUB_GPU, 0x0003030A, "clk-g3d")
117 DEFINE_CLK_STUB(HI3660_CLK_STUB_DDR, 0x00040309, "clk-ddrc")
120 static struct clk_hw *hi3660_stub_clk_hw_get(struct of_phandle_args *clkspec,
123 unsigned int idx = clkspec->args[0];
125 if (idx >= HI3660_CLK_STUB_NUM) {
126 pr_err("%s: invalid index %u\n", __func__, idx);
127 return ERR_PTR(-EINVAL);
130 return &hi3660_stub_clks[idx].hw;
133 static int hi3660_stub_clk_probe(struct platform_device *pdev)
135 struct device *dev = &pdev->dev;
136 struct resource *res;
140 /* Use mailbox client without blocking */
141 stub_clk_chan.cl.dev = dev;
142 stub_clk_chan.cl.tx_done = NULL;
143 stub_clk_chan.cl.tx_block = false;
144 stub_clk_chan.cl.knows_txdone = false;
146 /* Allocate mailbox channel */
147 stub_clk_chan.mbox = mbox_request_channel(&stub_clk_chan.cl, 0);
148 if (IS_ERR(stub_clk_chan.mbox))
149 return PTR_ERR(stub_clk_chan.mbox);
151 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
154 freq_reg = devm_ioremap(dev, res->start, resource_size(res));
158 freq_reg += HI3660_STUB_CLOCK_DATA;
160 for (i = 0; i < HI3660_CLK_STUB_NUM; i++) {
161 ret = devm_clk_hw_register(&pdev->dev, &hi3660_stub_clks[i].hw);
166 return devm_of_clk_add_hw_provider(&pdev->dev, hi3660_stub_clk_hw_get,
170 static const struct of_device_id hi3660_stub_clk_of_match[] = {
171 { .compatible = "hisilicon,hi3660-stub-clk", },
175 static struct platform_driver hi3660_stub_clk_driver = {
176 .probe = hi3660_stub_clk_probe,
178 .name = "hi3660-stub-clk",
179 .of_match_table = hi3660_stub_clk_of_match,
183 static int __init hi3660_stub_clk_init(void)
185 return platform_driver_register(&hi3660_stub_clk_driver);
187 subsys_initcall(hi3660_stub_clk_init);