1 // SPDX-License-Identifier: GPL-2.0-only
3 * Hi6220 stub clock driver
5 * Copyright (c) 2015 Hisilicon Limited.
6 * Copyright (c) 2015 Linaro Limited.
8 * Author: Leo Yan <leo.yan@linaro.org>
11 #include <linux/clk-provider.h>
12 #include <linux/err.h>
13 #include <linux/kernel.h>
14 #include <linux/mfd/syscon.h>
15 #include <linux/mailbox_client.h>
17 #include <linux/of_device.h>
18 #include <linux/regmap.h>
21 #define HI6220_STUB_ACPU0 0
22 #define HI6220_STUB_ACPU1 1
23 #define HI6220_STUB_GPU 2
24 #define HI6220_STUB_DDR 5
27 #define HI6220_MBOX_MSG_LEN 8
29 #define HI6220_MBOX_FREQ 0xA
30 #define HI6220_MBOX_CMD_SET 0x3
31 #define HI6220_MBOX_OBJ_AP 0x0
33 /* CPU dynamic frequency scaling */
34 #define ACPU_DFS_FREQ_MAX 0x1724
35 #define ACPU_DFS_CUR_FREQ 0x17CC
36 #define ACPU_DFS_FLAG 0x1B30
37 #define ACPU_DFS_FREQ_REQ 0x1B34
38 #define ACPU_DFS_FREQ_LMT 0x1B38
39 #define ACPU_DFS_LOCK_FLAG 0xAEAEAEAE
41 #define to_stub_clk(hw) container_of(hw, struct hi6220_stub_clk, hw)
43 struct hi6220_stub_clk
{
49 struct regmap
*dfs_map
;
50 struct mbox_client cl
;
51 struct mbox_chan
*mbox
;
54 struct hi6220_mbox_msg
{
59 unsigned char para
[4];
62 union hi6220_mbox_data
{
63 unsigned int data
[HI6220_MBOX_MSG_LEN
];
64 struct hi6220_mbox_msg msg
;
67 static unsigned int hi6220_acpu_get_freq(struct hi6220_stub_clk
*stub_clk
)
71 regmap_read(stub_clk
->dfs_map
, ACPU_DFS_CUR_FREQ
, &freq
);
75 static int hi6220_acpu_set_freq(struct hi6220_stub_clk
*stub_clk
,
78 union hi6220_mbox_data data
;
80 /* set the frequency in sram */
81 regmap_write(stub_clk
->dfs_map
, ACPU_DFS_FREQ_REQ
, freq
);
83 /* compound mailbox message */
84 data
.msg
.type
= HI6220_MBOX_FREQ
;
85 data
.msg
.cmd
= HI6220_MBOX_CMD_SET
;
86 data
.msg
.obj
= HI6220_MBOX_OBJ_AP
;
87 data
.msg
.src
= HI6220_MBOX_OBJ_AP
;
89 mbox_send_message(stub_clk
->mbox
, &data
);
93 static int hi6220_acpu_round_freq(struct hi6220_stub_clk
*stub_clk
,
96 unsigned int limit_flag
, limit_freq
= UINT_MAX
;
97 unsigned int max_freq
;
99 /* check the constrained frequency */
100 regmap_read(stub_clk
->dfs_map
, ACPU_DFS_FLAG
, &limit_flag
);
101 if (limit_flag
== ACPU_DFS_LOCK_FLAG
)
102 regmap_read(stub_clk
->dfs_map
, ACPU_DFS_FREQ_LMT
, &limit_freq
);
104 /* check the supported maximum frequency */
105 regmap_read(stub_clk
->dfs_map
, ACPU_DFS_FREQ_MAX
, &max_freq
);
107 /* calculate the real maximum frequency */
108 max_freq
= min(max_freq
, limit_freq
);
110 if (WARN_ON(freq
> max_freq
))
116 static unsigned long hi6220_stub_clk_recalc_rate(struct clk_hw
*hw
,
117 unsigned long parent_rate
)
120 struct hi6220_stub_clk
*stub_clk
= to_stub_clk(hw
);
122 switch (stub_clk
->id
) {
123 case HI6220_STUB_ACPU0
:
124 rate
= hi6220_acpu_get_freq(stub_clk
);
126 /* convert from kHz to Hz */
131 dev_err(stub_clk
->dev
, "%s: un-supported clock id %d\n",
132 __func__
, stub_clk
->id
);
139 static int hi6220_stub_clk_set_rate(struct clk_hw
*hw
, unsigned long rate
,
140 unsigned long parent_rate
)
142 struct hi6220_stub_clk
*stub_clk
= to_stub_clk(hw
);
143 unsigned long new_rate
= rate
/ 1000; /* kHz */
146 switch (stub_clk
->id
) {
147 case HI6220_STUB_ACPU0
:
148 ret
= hi6220_acpu_set_freq(stub_clk
, new_rate
);
155 dev_err(stub_clk
->dev
, "%s: un-supported clock id %d\n",
156 __func__
, stub_clk
->id
);
160 pr_debug("%s: set rate=%ldkHz\n", __func__
, new_rate
);
164 static long hi6220_stub_clk_round_rate(struct clk_hw
*hw
, unsigned long rate
,
165 unsigned long *parent_rate
)
167 struct hi6220_stub_clk
*stub_clk
= to_stub_clk(hw
);
168 unsigned long new_rate
= rate
/ 1000; /* kHz */
170 switch (stub_clk
->id
) {
171 case HI6220_STUB_ACPU0
:
172 new_rate
= hi6220_acpu_round_freq(stub_clk
, new_rate
);
174 /* convert from kHz to Hz */
179 dev_err(stub_clk
->dev
, "%s: un-supported clock id %d\n",
180 __func__
, stub_clk
->id
);
187 static const struct clk_ops hi6220_stub_clk_ops
= {
188 .recalc_rate
= hi6220_stub_clk_recalc_rate
,
189 .round_rate
= hi6220_stub_clk_round_rate
,
190 .set_rate
= hi6220_stub_clk_set_rate
,
193 static int hi6220_stub_clk_probe(struct platform_device
*pdev
)
195 struct device
*dev
= &pdev
->dev
;
196 struct clk_init_data init
;
197 struct hi6220_stub_clk
*stub_clk
;
199 struct device_node
*np
= pdev
->dev
.of_node
;
202 stub_clk
= devm_kzalloc(dev
, sizeof(*stub_clk
), GFP_KERNEL
);
206 stub_clk
->dfs_map
= syscon_regmap_lookup_by_phandle(np
,
207 "hisilicon,hi6220-clk-sram");
208 if (IS_ERR(stub_clk
->dfs_map
)) {
209 dev_err(dev
, "failed to get sram regmap\n");
210 return PTR_ERR(stub_clk
->dfs_map
);
213 stub_clk
->hw
.init
= &init
;
215 stub_clk
->id
= HI6220_STUB_ACPU0
;
217 /* Use mailbox client with blocking mode */
218 stub_clk
->cl
.dev
= dev
;
219 stub_clk
->cl
.tx_done
= NULL
;
220 stub_clk
->cl
.tx_block
= true;
221 stub_clk
->cl
.tx_tout
= 500;
222 stub_clk
->cl
.knows_txdone
= false;
224 /* Allocate mailbox channel */
225 stub_clk
->mbox
= mbox_request_channel(&stub_clk
->cl
, 0);
226 if (IS_ERR(stub_clk
->mbox
)) {
227 dev_err(dev
, "failed get mailbox channel\n");
228 return PTR_ERR(stub_clk
->mbox
);
232 init
.ops
= &hi6220_stub_clk_ops
;
233 init
.num_parents
= 0;
236 clk
= devm_clk_register(dev
, &stub_clk
->hw
);
240 ret
= of_clk_add_provider(np
, of_clk_src_simple_get
, clk
);
242 dev_err(dev
, "failed to register OF clock provider\n");
246 /* initialize buffer to zero */
247 regmap_write(stub_clk
->dfs_map
, ACPU_DFS_FLAG
, 0x0);
248 regmap_write(stub_clk
->dfs_map
, ACPU_DFS_FREQ_REQ
, 0x0);
249 regmap_write(stub_clk
->dfs_map
, ACPU_DFS_FREQ_LMT
, 0x0);
251 dev_dbg(dev
, "Registered clock '%s'\n", init
.name
);
255 static const struct of_device_id hi6220_stub_clk_of_match
[] = {
256 { .compatible
= "hisilicon,hi6220-stub-clk", },
260 static struct platform_driver hi6220_stub_clk_driver
= {
262 .name
= "hi6220-stub-clk",
263 .of_match_table
= hi6220_stub_clk_of_match
,
265 .probe
= hi6220_stub_clk_probe
,
268 static int __init
hi6220_stub_clk_init(void)
270 return platform_driver_register(&hi6220_stub_clk_driver
);
272 subsys_initcall(hi6220_stub_clk_init
);