2 * Copyright (c) 2016, Linaro Limited
3 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/clk-provider.h>
16 #include <linux/err.h>
17 #include <linux/export.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/mutex.h>
23 #include <linux/of_device.h>
24 #include <linux/platform_device.h>
25 #include <linux/soc/qcom/smd-rpm.h>
27 #include <dt-bindings/clock/qcom,rpmcc.h>
28 #include <dt-bindings/mfd/qcom-rpm.h>
30 #define QCOM_RPM_KEY_SOFTWARE_ENABLE 0x6e657773
31 #define QCOM_RPM_KEY_PIN_CTRL_CLK_BUFFER_ENABLE_KEY 0x62636370
32 #define QCOM_RPM_SMD_KEY_RATE 0x007a484b
33 #define QCOM_RPM_SMD_KEY_ENABLE 0x62616e45
34 #define QCOM_RPM_SMD_KEY_STATE 0x54415453
35 #define QCOM_RPM_SCALING_ENABLE_ID 0x2
37 #define __DEFINE_CLK_SMD_RPM(_platform, _name, _active, type, r_id, stat_id, \
39 static struct clk_smd_rpm _platform##_##_active; \
40 static struct clk_smd_rpm _platform##_##_name = { \
41 .rpm_res_type = (type), \
42 .rpm_clk_id = (r_id), \
43 .rpm_status_id = (stat_id), \
45 .peer = &_platform##_##_active, \
47 .hw.init = &(struct clk_init_data){ \
48 .ops = &clk_smd_rpm_ops, \
50 .parent_names = (const char *[]){ "xo_board" }, \
54 static struct clk_smd_rpm _platform##_##_active = { \
55 .rpm_res_type = (type), \
56 .rpm_clk_id = (r_id), \
57 .rpm_status_id = (stat_id), \
58 .active_only = true, \
60 .peer = &_platform##_##_name, \
62 .hw.init = &(struct clk_init_data){ \
63 .ops = &clk_smd_rpm_ops, \
65 .parent_names = (const char *[]){ "xo_board" }, \
70 #define __DEFINE_CLK_SMD_RPM_BRANCH(_platform, _name, _active, type, r_id, \
72 static struct clk_smd_rpm _platform##_##_active; \
73 static struct clk_smd_rpm _platform##_##_name = { \
74 .rpm_res_type = (type), \
75 .rpm_clk_id = (r_id), \
76 .rpm_status_id = (stat_id), \
79 .peer = &_platform##_##_active, \
81 .hw.init = &(struct clk_init_data){ \
82 .ops = &clk_smd_rpm_branch_ops, \
84 .parent_names = (const char *[]){ "xo_board" }, \
88 static struct clk_smd_rpm _platform##_##_active = { \
89 .rpm_res_type = (type), \
90 .rpm_clk_id = (r_id), \
91 .rpm_status_id = (stat_id), \
92 .active_only = true, \
95 .peer = &_platform##_##_name, \
97 .hw.init = &(struct clk_init_data){ \
98 .ops = &clk_smd_rpm_branch_ops, \
100 .parent_names = (const char *[]){ "xo_board" }, \
105 #define DEFINE_CLK_SMD_RPM(_platform, _name, _active, type, r_id) \
106 __DEFINE_CLK_SMD_RPM(_platform, _name, _active, type, r_id, \
107 0, QCOM_RPM_SMD_KEY_RATE)
109 #define DEFINE_CLK_SMD_RPM_BRANCH(_platform, _name, _active, type, r_id, r) \
110 __DEFINE_CLK_SMD_RPM_BRANCH(_platform, _name, _active, type, \
111 r_id, 0, r, QCOM_RPM_SMD_KEY_ENABLE)
113 #define DEFINE_CLK_SMD_RPM_QDSS(_platform, _name, _active, type, r_id) \
114 __DEFINE_CLK_SMD_RPM(_platform, _name, _active, type, r_id, \
115 0, QCOM_RPM_SMD_KEY_STATE)
117 #define DEFINE_CLK_SMD_RPM_XO_BUFFER(_platform, _name, _active, r_id) \
118 __DEFINE_CLK_SMD_RPM_BRANCH(_platform, _name, _active, \
119 QCOM_SMD_RPM_CLK_BUF_A, r_id, 0, 1000, \
120 QCOM_RPM_KEY_SOFTWARE_ENABLE)
122 #define DEFINE_CLK_SMD_RPM_XO_BUFFER_PINCTRL(_platform, _name, _active, r_id) \
123 __DEFINE_CLK_SMD_RPM_BRANCH(_platform, _name, _active, \
124 QCOM_SMD_RPM_CLK_BUF_A, r_id, 0, 1000, \
125 QCOM_RPM_KEY_PIN_CTRL_CLK_BUFFER_ENABLE_KEY)
127 #define to_clk_smd_rpm(_hw) container_of(_hw, struct clk_smd_rpm, hw)
130 const int rpm_res_type
;
132 const int rpm_clk_id
;
133 const int rpm_status_id
;
134 const bool active_only
;
137 struct clk_smd_rpm
*peer
;
140 struct qcom_smd_rpm
*rpm
;
143 struct clk_smd_rpm_req
{
150 struct qcom_rpm
*rpm
;
151 struct clk_smd_rpm
**clks
;
155 struct rpm_smd_clk_desc
{
156 struct clk_smd_rpm
**clks
;
160 static DEFINE_MUTEX(rpm_smd_clk_lock
);
162 static int clk_smd_rpm_handoff(struct clk_smd_rpm
*r
)
165 struct clk_smd_rpm_req req
= {
166 .key
= cpu_to_le32(r
->rpm_key
),
167 .nbytes
= cpu_to_le32(sizeof(u32
)),
168 .value
= cpu_to_le32(INT_MAX
),
171 ret
= qcom_rpm_smd_write(r
->rpm
, QCOM_SMD_RPM_ACTIVE_STATE
,
172 r
->rpm_res_type
, r
->rpm_clk_id
, &req
,
176 ret
= qcom_rpm_smd_write(r
->rpm
, QCOM_SMD_RPM_SLEEP_STATE
,
177 r
->rpm_res_type
, r
->rpm_clk_id
, &req
,
185 static int clk_smd_rpm_set_rate_active(struct clk_smd_rpm
*r
,
188 struct clk_smd_rpm_req req
= {
189 .key
= cpu_to_le32(r
->rpm_key
),
190 .nbytes
= cpu_to_le32(sizeof(u32
)),
191 .value
= cpu_to_le32(DIV_ROUND_UP(rate
, 1000)), /* to kHz */
194 return qcom_rpm_smd_write(r
->rpm
, QCOM_SMD_RPM_ACTIVE_STATE
,
195 r
->rpm_res_type
, r
->rpm_clk_id
, &req
,
199 static int clk_smd_rpm_set_rate_sleep(struct clk_smd_rpm
*r
,
202 struct clk_smd_rpm_req req
= {
203 .key
= cpu_to_le32(r
->rpm_key
),
204 .nbytes
= cpu_to_le32(sizeof(u32
)),
205 .value
= cpu_to_le32(DIV_ROUND_UP(rate
, 1000)), /* to kHz */
208 return qcom_rpm_smd_write(r
->rpm
, QCOM_SMD_RPM_SLEEP_STATE
,
209 r
->rpm_res_type
, r
->rpm_clk_id
, &req
,
213 static void to_active_sleep(struct clk_smd_rpm
*r
, unsigned long rate
,
214 unsigned long *active
, unsigned long *sleep
)
219 * Active-only clocks don't care what the rate is during sleep. So,
220 * they vote for zero.
228 static int clk_smd_rpm_prepare(struct clk_hw
*hw
)
230 struct clk_smd_rpm
*r
= to_clk_smd_rpm(hw
);
231 struct clk_smd_rpm
*peer
= r
->peer
;
232 unsigned long this_rate
= 0, this_sleep_rate
= 0;
233 unsigned long peer_rate
= 0, peer_sleep_rate
= 0;
234 unsigned long active_rate
, sleep_rate
;
237 mutex_lock(&rpm_smd_clk_lock
);
239 /* Don't send requests to the RPM if the rate has not been set. */
243 to_active_sleep(r
, r
->rate
, &this_rate
, &this_sleep_rate
);
245 /* Take peer clock's rate into account only if it's enabled. */
247 to_active_sleep(peer
, peer
->rate
,
248 &peer_rate
, &peer_sleep_rate
);
250 active_rate
= max(this_rate
, peer_rate
);
253 active_rate
= !!active_rate
;
255 ret
= clk_smd_rpm_set_rate_active(r
, active_rate
);
259 sleep_rate
= max(this_sleep_rate
, peer_sleep_rate
);
261 sleep_rate
= !!sleep_rate
;
263 ret
= clk_smd_rpm_set_rate_sleep(r
, sleep_rate
);
265 /* Undo the active set vote and restore it */
266 ret
= clk_smd_rpm_set_rate_active(r
, peer_rate
);
272 mutex_unlock(&rpm_smd_clk_lock
);
277 static void clk_smd_rpm_unprepare(struct clk_hw
*hw
)
279 struct clk_smd_rpm
*r
= to_clk_smd_rpm(hw
);
280 struct clk_smd_rpm
*peer
= r
->peer
;
281 unsigned long peer_rate
= 0, peer_sleep_rate
= 0;
282 unsigned long active_rate
, sleep_rate
;
285 mutex_lock(&rpm_smd_clk_lock
);
290 /* Take peer clock's rate into account only if it's enabled. */
292 to_active_sleep(peer
, peer
->rate
, &peer_rate
,
295 active_rate
= r
->branch
? !!peer_rate
: peer_rate
;
296 ret
= clk_smd_rpm_set_rate_active(r
, active_rate
);
300 sleep_rate
= r
->branch
? !!peer_sleep_rate
: peer_sleep_rate
;
301 ret
= clk_smd_rpm_set_rate_sleep(r
, sleep_rate
);
308 mutex_unlock(&rpm_smd_clk_lock
);
311 static int clk_smd_rpm_set_rate(struct clk_hw
*hw
, unsigned long rate
,
312 unsigned long parent_rate
)
314 struct clk_smd_rpm
*r
= to_clk_smd_rpm(hw
);
315 struct clk_smd_rpm
*peer
= r
->peer
;
316 unsigned long active_rate
, sleep_rate
;
317 unsigned long this_rate
= 0, this_sleep_rate
= 0;
318 unsigned long peer_rate
= 0, peer_sleep_rate
= 0;
321 mutex_lock(&rpm_smd_clk_lock
);
326 to_active_sleep(r
, rate
, &this_rate
, &this_sleep_rate
);
328 /* Take peer clock's rate into account only if it's enabled. */
330 to_active_sleep(peer
, peer
->rate
,
331 &peer_rate
, &peer_sleep_rate
);
333 active_rate
= max(this_rate
, peer_rate
);
334 ret
= clk_smd_rpm_set_rate_active(r
, active_rate
);
338 sleep_rate
= max(this_sleep_rate
, peer_sleep_rate
);
339 ret
= clk_smd_rpm_set_rate_sleep(r
, sleep_rate
);
346 mutex_unlock(&rpm_smd_clk_lock
);
351 static long clk_smd_rpm_round_rate(struct clk_hw
*hw
, unsigned long rate
,
352 unsigned long *parent_rate
)
355 * RPM handles rate rounding and we don't have a way to
356 * know what the rate will be, so just return whatever
362 static unsigned long clk_smd_rpm_recalc_rate(struct clk_hw
*hw
,
363 unsigned long parent_rate
)
365 struct clk_smd_rpm
*r
= to_clk_smd_rpm(hw
);
368 * RPM handles rate rounding and we don't have a way to
369 * know what the rate will be, so just return whatever
375 static int clk_smd_rpm_enable_scaling(struct qcom_smd_rpm
*rpm
)
378 struct clk_smd_rpm_req req
= {
379 .key
= cpu_to_le32(QCOM_RPM_SMD_KEY_ENABLE
),
380 .nbytes
= cpu_to_le32(sizeof(u32
)),
381 .value
= cpu_to_le32(1),
384 ret
= qcom_rpm_smd_write(rpm
, QCOM_SMD_RPM_SLEEP_STATE
,
385 QCOM_SMD_RPM_MISC_CLK
,
386 QCOM_RPM_SCALING_ENABLE_ID
, &req
, sizeof(req
));
388 pr_err("RPM clock scaling (sleep set) not enabled!\n");
392 ret
= qcom_rpm_smd_write(rpm
, QCOM_SMD_RPM_ACTIVE_STATE
,
393 QCOM_SMD_RPM_MISC_CLK
,
394 QCOM_RPM_SCALING_ENABLE_ID
, &req
, sizeof(req
));
396 pr_err("RPM clock scaling (active set) not enabled!\n");
400 pr_debug("%s: RPM clock scaling is enabled\n", __func__
);
404 static const struct clk_ops clk_smd_rpm_ops
= {
405 .prepare
= clk_smd_rpm_prepare
,
406 .unprepare
= clk_smd_rpm_unprepare
,
407 .set_rate
= clk_smd_rpm_set_rate
,
408 .round_rate
= clk_smd_rpm_round_rate
,
409 .recalc_rate
= clk_smd_rpm_recalc_rate
,
412 static const struct clk_ops clk_smd_rpm_branch_ops
= {
413 .prepare
= clk_smd_rpm_prepare
,
414 .unprepare
= clk_smd_rpm_unprepare
,
415 .round_rate
= clk_smd_rpm_round_rate
,
416 .recalc_rate
= clk_smd_rpm_recalc_rate
,
420 DEFINE_CLK_SMD_RPM(msm8916
, pcnoc_clk
, pcnoc_a_clk
, QCOM_SMD_RPM_BUS_CLK
, 0);
421 DEFINE_CLK_SMD_RPM(msm8916
, snoc_clk
, snoc_a_clk
, QCOM_SMD_RPM_BUS_CLK
, 1);
422 DEFINE_CLK_SMD_RPM(msm8916
, bimc_clk
, bimc_a_clk
, QCOM_SMD_RPM_MEM_CLK
, 0);
423 DEFINE_CLK_SMD_RPM_QDSS(msm8916
, qdss_clk
, qdss_a_clk
, QCOM_SMD_RPM_MISC_CLK
, 1);
424 DEFINE_CLK_SMD_RPM_XO_BUFFER(msm8916
, bb_clk1
, bb_clk1_a
, 1);
425 DEFINE_CLK_SMD_RPM_XO_BUFFER(msm8916
, bb_clk2
, bb_clk2_a
, 2);
426 DEFINE_CLK_SMD_RPM_XO_BUFFER(msm8916
, rf_clk1
, rf_clk1_a
, 4);
427 DEFINE_CLK_SMD_RPM_XO_BUFFER(msm8916
, rf_clk2
, rf_clk2_a
, 5);
428 DEFINE_CLK_SMD_RPM_XO_BUFFER_PINCTRL(msm8916
, bb_clk1_pin
, bb_clk1_a_pin
, 1);
429 DEFINE_CLK_SMD_RPM_XO_BUFFER_PINCTRL(msm8916
, bb_clk2_pin
, bb_clk2_a_pin
, 2);
430 DEFINE_CLK_SMD_RPM_XO_BUFFER_PINCTRL(msm8916
, rf_clk1_pin
, rf_clk1_a_pin
, 4);
431 DEFINE_CLK_SMD_RPM_XO_BUFFER_PINCTRL(msm8916
, rf_clk2_pin
, rf_clk2_a_pin
, 5);
433 static struct clk_smd_rpm
*msm8916_clks
[] = {
434 [RPM_SMD_PCNOC_CLK
] = &msm8916_pcnoc_clk
,
435 [RPM_SMD_PCNOC_A_CLK
] = &msm8916_pcnoc_a_clk
,
436 [RPM_SMD_SNOC_CLK
] = &msm8916_snoc_clk
,
437 [RPM_SMD_SNOC_A_CLK
] = &msm8916_snoc_a_clk
,
438 [RPM_SMD_BIMC_CLK
] = &msm8916_bimc_clk
,
439 [RPM_SMD_BIMC_A_CLK
] = &msm8916_bimc_a_clk
,
440 [RPM_SMD_QDSS_CLK
] = &msm8916_qdss_clk
,
441 [RPM_SMD_QDSS_A_CLK
] = &msm8916_qdss_a_clk
,
442 [RPM_SMD_BB_CLK1
] = &msm8916_bb_clk1
,
443 [RPM_SMD_BB_CLK1_A
] = &msm8916_bb_clk1_a
,
444 [RPM_SMD_BB_CLK2
] = &msm8916_bb_clk2
,
445 [RPM_SMD_BB_CLK2_A
] = &msm8916_bb_clk2_a
,
446 [RPM_SMD_RF_CLK1
] = &msm8916_rf_clk1
,
447 [RPM_SMD_RF_CLK1_A
] = &msm8916_rf_clk1_a
,
448 [RPM_SMD_RF_CLK2
] = &msm8916_rf_clk2
,
449 [RPM_SMD_RF_CLK2_A
] = &msm8916_rf_clk2_a
,
450 [RPM_SMD_BB_CLK1_PIN
] = &msm8916_bb_clk1_pin
,
451 [RPM_SMD_BB_CLK1_A_PIN
] = &msm8916_bb_clk1_a_pin
,
452 [RPM_SMD_BB_CLK2_PIN
] = &msm8916_bb_clk2_pin
,
453 [RPM_SMD_BB_CLK2_A_PIN
] = &msm8916_bb_clk2_a_pin
,
454 [RPM_SMD_RF_CLK1_PIN
] = &msm8916_rf_clk1_pin
,
455 [RPM_SMD_RF_CLK1_A_PIN
] = &msm8916_rf_clk1_a_pin
,
456 [RPM_SMD_RF_CLK2_PIN
] = &msm8916_rf_clk2_pin
,
457 [RPM_SMD_RF_CLK2_A_PIN
] = &msm8916_rf_clk2_a_pin
,
460 static const struct rpm_smd_clk_desc rpm_clk_msm8916
= {
461 .clks
= msm8916_clks
,
462 .num_clks
= ARRAY_SIZE(msm8916_clks
),
465 static const struct of_device_id rpm_smd_clk_match_table
[] = {
466 { .compatible
= "qcom,rpmcc-msm8916", .data
= &rpm_clk_msm8916
},
469 MODULE_DEVICE_TABLE(of
, rpm_smd_clk_match_table
);
471 static struct clk_hw
*qcom_smdrpm_clk_hw_get(struct of_phandle_args
*clkspec
,
474 struct rpm_cc
*rcc
= data
;
475 unsigned int idx
= clkspec
->args
[0];
477 if (idx
>= rcc
->num_clks
) {
478 pr_err("%s: invalid index %u\n", __func__
, idx
);
479 return ERR_PTR(-EINVAL
);
482 return rcc
->clks
[idx
] ? &rcc
->clks
[idx
]->hw
: ERR_PTR(-ENOENT
);
485 static int rpm_smd_clk_probe(struct platform_device
*pdev
)
490 struct qcom_smd_rpm
*rpm
;
491 struct clk_smd_rpm
**rpm_smd_clks
;
492 const struct rpm_smd_clk_desc
*desc
;
494 rpm
= dev_get_drvdata(pdev
->dev
.parent
);
496 dev_err(&pdev
->dev
, "Unable to retrieve handle to RPM\n");
500 desc
= of_device_get_match_data(&pdev
->dev
);
504 rpm_smd_clks
= desc
->clks
;
505 num_clks
= desc
->num_clks
;
507 rcc
= devm_kzalloc(&pdev
->dev
, sizeof(*rcc
), GFP_KERNEL
);
511 rcc
->clks
= rpm_smd_clks
;
512 rcc
->num_clks
= num_clks
;
514 for (i
= 0; i
< num_clks
; i
++) {
515 if (!rpm_smd_clks
[i
])
518 rpm_smd_clks
[i
]->rpm
= rpm
;
520 ret
= clk_smd_rpm_handoff(rpm_smd_clks
[i
]);
525 ret
= clk_smd_rpm_enable_scaling(rpm
);
529 for (i
= 0; i
< num_clks
; i
++) {
530 if (!rpm_smd_clks
[i
])
533 ret
= devm_clk_hw_register(&pdev
->dev
, &rpm_smd_clks
[i
]->hw
);
538 ret
= of_clk_add_hw_provider(pdev
->dev
.of_node
, qcom_smdrpm_clk_hw_get
,
545 dev_err(&pdev
->dev
, "Error registering SMD clock driver (%d)\n", ret
);
549 static int rpm_smd_clk_remove(struct platform_device
*pdev
)
551 of_clk_del_provider(pdev
->dev
.of_node
);
555 static struct platform_driver rpm_smd_clk_driver
= {
557 .name
= "qcom-clk-smd-rpm",
558 .of_match_table
= rpm_smd_clk_match_table
,
560 .probe
= rpm_smd_clk_probe
,
561 .remove
= rpm_smd_clk_remove
,
564 static int __init
rpm_smd_clk_init(void)
566 return platform_driver_register(&rpm_smd_clk_driver
);
568 core_initcall(rpm_smd_clk_init
);
570 static void __exit
rpm_smd_clk_exit(void)
572 platform_driver_unregister(&rpm_smd_clk_driver
);
574 module_exit(rpm_smd_clk_exit
);
576 MODULE_DESCRIPTION("Qualcomm RPM over SMD Clock Controller Driver");
577 MODULE_LICENSE("GPL v2");
578 MODULE_ALIAS("platform:qcom-clk-smd-rpm");