1 !This is the adjusted version of patch that has been submitted to upstream and that, unfortunately, provides support for RPM clocks only for apq8064 board.
3 !TODO: make a patch that adds support for ipq806x along with apq8064 and not replaces it.
5 From patchwork Wed Nov 2 15:56:57 2016
6 Content-Type: text/plain; charset="utf-8"
8 Content-Transfer-Encoding: 7bit
9 Subject: [v9,2/3] clk: qcom: Add support for RPM Clocks
10 From: Georgi Djakov <georgi.djakov@linaro.org>
11 X-Patchwork-Id: 9409425
12 Message-Id: <20161102155658.32203-3-georgi.djakov@linaro.org>
13 To: sboyd@codeaurora.org, mturquette@baylibre.com
14 Cc: linux-clk@vger.kernel.org, devicetree@vger.kernel.org,
15 robh+dt@kernel.org, mark.rutland@arm.com,
16 linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org,
17 georgi.djakov@linaro.org
18 Date: Wed, 2 Nov 2016 17:56:57 +0200
20 This adds initial support for clocks controlled by the Resource
21 Power Manager (RPM) processor on some Qualcomm SoCs, which use
22 the qcom_rpm driver to communicate with RPM.
23 Such platforms are ipq806x and msm8960.
25 Signed-off-by: Georgi Djakov <georgi.djakov@linaro.org>
27 .../devicetree/bindings/clock/qcom,rpmcc.txt | 1 +
28 drivers/clk/qcom/Kconfig | 13 +
29 drivers/clk/qcom/Makefile | 1 +
30 drivers/clk/qcom/clk-rpm.c | 489 +++++++++++++++++++++
31 include/dt-bindings/clock/qcom,rpmcc.h | 24 +
32 5 files changed, 528 insertions(+)
33 create mode 100644 drivers/clk/qcom/clk-rpm.c
36 To unsubscribe from this list: send the line "unsubscribe linux-arm-msm" in
37 the body of a message to majordomo@vger.kernel.org
38 More majordomo info at http://vger.kernel.org/majordomo-info.html
40 --- a/Documentation/devicetree/bindings/clock/qcom,rpmcc.txt
41 +++ b/Documentation/devicetree/bindings/clock/qcom,rpmcc.txt
42 @@ -11,6 +11,7 @@ Required properties :
43 compatible "qcom,rpmcc" should be also included.
45 "qcom,rpmcc-msm8916", "qcom,rpmcc"
46 + "qcom,rpmcc-ipq806x", "qcom,rpmcc"
48 - #clock-cells : shall contain 1
50 --- a/drivers/clk/qcom/Kconfig
51 +++ b/drivers/clk/qcom/Kconfig
52 @@ -12,6 +12,19 @@ config COMMON_CLK_QCOM
54 select RESET_CONTROLLER
57 + tristate "RPM based Clock Controller"
58 + depends on COMMON_CLK_QCOM && MFD_QCOM_RPM
61 + The RPM (Resource Power Manager) is a dedicated hardware engine for
62 + managing the shared SoC resources in order to keep the lowest power
63 + profile. It communicates with other hardware subsystems via shared
64 + memory and accepts clock requests, aggregates the requests and turns
65 + the clocks on/off or scales them on demand.
66 + Say Y if you want to support the clocks exposed by the RPM on
67 + platforms such as ipq806x, msm8660, msm8960 etc.
69 config QCOM_CLK_SMD_RPM
70 tristate "RPM over SMD based Clock Controller"
71 depends on COMMON_CLK_QCOM && QCOM_SMD_RPM
72 --- a/drivers/clk/qcom/Makefile
73 +++ b/drivers/clk/qcom/Makefile
74 @@ -29,3 +29,4 @@ obj-$(CONFIG_MSM_MMCC_8974) += mmcc-msm8
75 obj-$(CONFIG_QCOM_HFPLL) += hfpll.o
76 obj-$(CONFIG_KRAITCC) += krait-cc.o
77 obj-$(CONFIG_QCOM_CLK_SMD_RPM) += clk-smd-rpm.o
78 +obj-$(CONFIG_QCOM_CLK_RPM) += clk-rpm.o
80 +++ b/drivers/clk/qcom/clk-rpm.c
83 + * Copyright (c) 2016, Linaro Limited
84 + * Copyright (c) 2014, The Linux Foundation. All rights reserved.
86 + * This software is licensed under the terms of the GNU General Public
87 + * License version 2, as published by the Free Software Foundation, and
88 + * may be copied, distributed, and modified under those terms.
90 + * This program is distributed in the hope that it will be useful,
91 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
92 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
93 + * GNU General Public License for more details.
96 +#include <linux/clk-provider.h>
97 +#include <linux/err.h>
98 +#include <linux/export.h>
99 +#include <linux/init.h>
100 +#include <linux/kernel.h>
101 +#include <linux/module.h>
102 +#include <linux/mutex.h>
103 +#include <linux/mfd/qcom_rpm.h>
104 +#include <linux/of.h>
105 +#include <linux/of_device.h>
106 +#include <linux/platform_device.h>
108 +#include <dt-bindings/mfd/qcom-rpm.h>
109 +#include <dt-bindings/clock/qcom,rpmcc.h>
111 +#define QCOM_RPM_MISC_CLK_TYPE 0x306b6c63
112 +#define QCOM_RPM_SCALING_ENABLE_ID 0x2
114 +#define DEFINE_CLK_RPM(_platform, _name, _active, r_id) \
115 + static struct clk_rpm _platform##_##_active; \
116 + static struct clk_rpm _platform##_##_name = { \
117 + .rpm_clk_id = (r_id), \
118 + .peer = &_platform##_##_active, \
120 + .hw.init = &(struct clk_init_data){ \
121 + .ops = &clk_rpm_ops, \
123 + .parent_names = (const char *[]){ "pxo_board" }, \
124 + .num_parents = 1, \
127 + static struct clk_rpm _platform##_##_active = { \
128 + .rpm_clk_id = (r_id), \
129 + .peer = &_platform##_##_name, \
130 + .active_only = true, \
132 + .hw.init = &(struct clk_init_data){ \
133 + .ops = &clk_rpm_ops, \
134 + .name = #_active, \
135 + .parent_names = (const char *[]){ "pxo_board" }, \
136 + .num_parents = 1, \
140 +#define DEFINE_CLK_RPM_PXO_BRANCH(_platform, _name, _active, r_id, r) \
141 + static struct clk_rpm _platform##_##_active; \
142 + static struct clk_rpm _platform##_##_name = { \
143 + .rpm_clk_id = (r_id), \
144 + .active_only = true, \
145 + .peer = &_platform##_##_active, \
148 + .hw.init = &(struct clk_init_data){ \
149 + .ops = &clk_rpm_branch_ops, \
151 + .parent_names = (const char *[]){ "pxo_board" }, \
152 + .num_parents = 1, \
155 + static struct clk_rpm _platform##_##_active = { \
156 + .rpm_clk_id = (r_id), \
157 + .peer = &_platform##_##_name, \
160 + .hw.init = &(struct clk_init_data){ \
161 + .ops = &clk_rpm_branch_ops, \
162 + .name = #_active, \
163 + .parent_names = (const char *[]){ "pxo_board" }, \
164 + .num_parents = 1, \
168 +#define DEFINE_CLK_RPM_CXO_BRANCH(_platform, _name, _active, r_id, r) \
169 + static struct clk_rpm _platform##_##_active; \
170 + static struct clk_rpm _platform##_##_name = { \
171 + .rpm_clk_id = (r_id), \
172 + .peer = &_platform##_##_active, \
175 + .hw.init = &(struct clk_init_data){ \
176 + .ops = &clk_rpm_branch_ops, \
178 + .parent_names = (const char *[]){ "cxo_board" }, \
179 + .num_parents = 1, \
182 + static struct clk_rpm _platform##_##_active = { \
183 + .rpm_clk_id = (r_id), \
184 + .active_only = true, \
185 + .peer = &_platform##_##_name, \
188 + .hw.init = &(struct clk_init_data){ \
189 + .ops = &clk_rpm_branch_ops, \
190 + .name = #_active, \
191 + .parent_names = (const char *[]){ "cxo_board" }, \
192 + .num_parents = 1, \
196 +#define to_clk_rpm(_hw) container_of(_hw, struct clk_rpm, hw)
199 + const int rpm_clk_id;
200 + const bool active_only;
201 + unsigned long rate;
204 + struct clk_rpm *peer;
206 + struct qcom_rpm *rpm;
210 + struct qcom_rpm *rpm;
211 + struct clk_hw_onecell_data data;
212 + struct clk_hw *hws[];
215 +struct rpm_clk_desc {
216 + struct clk_rpm **clks;
220 +static DEFINE_MUTEX(rpm_clk_lock);
222 +static int clk_rpm_handoff(struct clk_rpm *r)
225 + u32 value = INT_MAX;
227 + ret = qcom_rpm_write(r->rpm, QCOM_RPM_ACTIVE_STATE,
228 + r->rpm_clk_id, &value, 1);
231 + ret = qcom_rpm_write(r->rpm, QCOM_RPM_SLEEP_STATE,
232 + r->rpm_clk_id, &value, 1);
239 +static int clk_rpm_set_rate_active(struct clk_rpm *r, unsigned long rate)
241 + u32 value = DIV_ROUND_UP(rate, 1000); /* to kHz */
243 + return qcom_rpm_write(r->rpm, QCOM_RPM_ACTIVE_STATE,
244 + r->rpm_clk_id, &value, 1);
247 +static int clk_rpm_set_rate_sleep(struct clk_rpm *r, unsigned long rate)
249 + u32 value = DIV_ROUND_UP(rate, 1000); /* to kHz */
251 + return qcom_rpm_write(r->rpm, QCOM_RPM_SLEEP_STATE,
252 + r->rpm_clk_id, &value, 1);
255 +static void to_active_sleep(struct clk_rpm *r, unsigned long rate,
256 + unsigned long *active, unsigned long *sleep)
261 + * Active-only clocks don't care what the rate is during sleep. So,
262 + * they vote for zero.
264 + if (r->active_only)
270 +static int clk_rpm_prepare(struct clk_hw *hw)
272 + struct clk_rpm *r = to_clk_rpm(hw);
273 + struct clk_rpm *peer = r->peer;
274 + unsigned long this_rate = 0, this_sleep_rate = 0;
275 + unsigned long peer_rate = 0, peer_sleep_rate = 0;
276 + unsigned long active_rate, sleep_rate;
279 + mutex_lock(&rpm_clk_lock);
281 + /* Don't send requests to the RPM if the rate has not been set. */
285 + to_active_sleep(r, r->rate, &this_rate, &this_sleep_rate);
287 + /* Take peer clock's rate into account only if it's enabled. */
289 + to_active_sleep(peer, peer->rate,
290 + &peer_rate, &peer_sleep_rate);
292 + active_rate = max(this_rate, peer_rate);
295 + active_rate = !!active_rate;
297 + ret = clk_rpm_set_rate_active(r, active_rate);
301 + sleep_rate = max(this_sleep_rate, peer_sleep_rate);
303 + sleep_rate = !!sleep_rate;
305 + ret = clk_rpm_set_rate_sleep(r, sleep_rate);
307 + /* Undo the active set vote and restore it */
308 + ret = clk_rpm_set_rate_active(r, peer_rate);
314 + mutex_unlock(&rpm_clk_lock);
319 +static void clk_rpm_unprepare(struct clk_hw *hw)
321 + struct clk_rpm *r = to_clk_rpm(hw);
322 + struct clk_rpm *peer = r->peer;
323 + unsigned long peer_rate = 0, peer_sleep_rate = 0;
324 + unsigned long active_rate, sleep_rate;
327 + mutex_lock(&rpm_clk_lock);
332 + /* Take peer clock's rate into account only if it's enabled. */
334 + to_active_sleep(peer, peer->rate, &peer_rate,
337 + active_rate = r->branch ? !!peer_rate : peer_rate;
338 + ret = clk_rpm_set_rate_active(r, active_rate);
342 + sleep_rate = r->branch ? !!peer_sleep_rate : peer_sleep_rate;
343 + ret = clk_rpm_set_rate_sleep(r, sleep_rate);
347 + r->enabled = false;
350 + mutex_unlock(&rpm_clk_lock);
353 +static int clk_rpm_set_rate(struct clk_hw *hw,
354 + unsigned long rate, unsigned long parent_rate)
356 + struct clk_rpm *r = to_clk_rpm(hw);
357 + struct clk_rpm *peer = r->peer;
358 + unsigned long active_rate, sleep_rate;
359 + unsigned long this_rate = 0, this_sleep_rate = 0;
360 + unsigned long peer_rate = 0, peer_sleep_rate = 0;
363 + mutex_lock(&rpm_clk_lock);
368 + to_active_sleep(r, rate, &this_rate, &this_sleep_rate);
370 + /* Take peer clock's rate into account only if it's enabled. */
372 + to_active_sleep(peer, peer->rate,
373 + &peer_rate, &peer_sleep_rate);
375 + active_rate = max(this_rate, peer_rate);
376 + ret = clk_rpm_set_rate_active(r, active_rate);
380 + sleep_rate = max(this_sleep_rate, peer_sleep_rate);
381 + ret = clk_rpm_set_rate_sleep(r, sleep_rate);
388 + mutex_unlock(&rpm_clk_lock);
393 +static long clk_rpm_round_rate(struct clk_hw *hw, unsigned long rate,
394 + unsigned long *parent_rate)
397 + * RPM handles rate rounding and we don't have a way to
398 + * know what the rate will be, so just return whatever
399 + * rate is requested.
404 +static unsigned long clk_rpm_recalc_rate(struct clk_hw *hw,
405 + unsigned long parent_rate)
407 + struct clk_rpm *r = to_clk_rpm(hw);
410 + * RPM handles rate rounding and we don't have a way to
411 + * know what the rate will be, so just return whatever
417 +static const struct clk_ops clk_rpm_ops = {
418 + .prepare = clk_rpm_prepare,
419 + .unprepare = clk_rpm_unprepare,
420 + .set_rate = clk_rpm_set_rate,
421 + .round_rate = clk_rpm_round_rate,
422 + .recalc_rate = clk_rpm_recalc_rate,
425 +static const struct clk_ops clk_rpm_branch_ops = {
426 + .prepare = clk_rpm_prepare,
427 + .unprepare = clk_rpm_unprepare,
428 + .round_rate = clk_rpm_round_rate,
429 + .recalc_rate = clk_rpm_recalc_rate,
433 +DEFINE_CLK_RPM(ipq806x, afab_clk, afab_a_clk, QCOM_RPM_APPS_FABRIC_CLK);
434 +DEFINE_CLK_RPM(ipq806x, cfpb_clk, cfpb_a_clk, QCOM_RPM_CFPB_CLK);
435 +DEFINE_CLK_RPM(ipq806x, daytona_clk, daytona_a_clk, QCOM_RPM_DAYTONA_FABRIC_CLK);
436 +DEFINE_CLK_RPM(ipq806x, ebi1_clk, ebi1_a_clk, QCOM_RPM_EBI1_CLK);
437 +DEFINE_CLK_RPM(ipq806x, nss_fabric_0_clk, nss_fabric_0_a_clk, QCOM_RPM_NSS_FABRIC_0_CLK);
438 +DEFINE_CLK_RPM(ipq806x, nss_fabric_1_clk, nss_fabric_1_a_clk, QCOM_RPM_NSS_FABRIC_1_CLK);
439 +DEFINE_CLK_RPM(ipq806x, sfab_clk, sfab_a_clk, QCOM_RPM_SYS_FABRIC_CLK);
440 +DEFINE_CLK_RPM(ipq806x, sfpb_clk, sfpb_a_clk, QCOM_RPM_SFPB_CLK);
442 +static struct clk_rpm *ipq806x_clks[] = {
443 + [RPM_APPS_FABRIC_CLK] = &ipq806x_afab_clk,
444 + [RPM_APPS_FABRIC_A_CLK] = &ipq806x_afab_a_clk,
445 + [RPM_CFPB_CLK] = &ipq806x_cfpb_clk,
446 + [RPM_CFPB_A_CLK] = &ipq806x_cfpb_a_clk,
447 + [RPM_DAYTONA_FABRIC_CLK] = &ipq806x_daytona_clk,
448 + [RPM_DAYTONA_FABRIC_A_CLK] = &ipq806x_daytona_a_clk,
449 + [RPM_EBI1_CLK] = &ipq806x_ebi1_clk,
450 + [RPM_EBI1_A_CLK] = &ipq806x_ebi1_a_clk,
451 + [RPM_NSS_FABRIC_0_CLK] = &ipq806x_nss_fabric_0_clk,
452 + [RPM_NSS_FABRIC_0_A_CLK] = &ipq806x_nss_fabric_0_a_clk,
453 + [RPM_NSS_FABRIC_1_CLK] = &ipq806x_nss_fabric_1_clk,
454 + [RPM_NSS_FABRIC_1_A_CLK] = &ipq806x_nss_fabric_1_a_clk,
455 + [RPM_SYS_FABRIC_CLK] = &ipq806x_sfab_clk,
456 + [RPM_SYS_FABRIC_A_CLK] = &ipq806x_sfab_a_clk,
457 + [RPM_SFPB_CLK] = &ipq806x_sfpb_clk,
458 + [RPM_SFPB_A_CLK] = &ipq806x_sfpb_a_clk,
461 +static const struct rpm_clk_desc rpm_clk_ipq806x = {
462 + .clks = ipq806x_clks,
463 + .num_clks = ARRAY_SIZE(ipq806x_clks),
466 +static const struct of_device_id rpm_clk_match_table[] = {
467 + { .compatible = "qcom,rpmcc-ipq806x", .data = &rpm_clk_ipq806x },
470 +MODULE_DEVICE_TABLE(of, rpm_clk_match_table);
472 +static int rpm_clk_probe(struct platform_device *pdev)
474 + struct clk_hw **hws;
475 + struct rpm_cc *rcc;
476 + struct clk_hw_onecell_data *data;
478 + size_t num_clks, i;
479 + struct qcom_rpm *rpm;
480 + struct clk_rpm **rpm_clks;
481 + const struct rpm_clk_desc *desc;
483 + rpm = dev_get_drvdata(pdev->dev.parent);
485 + dev_err(&pdev->dev, "Unable to retrieve handle to RPM\n");
489 + desc = of_device_get_match_data(&pdev->dev);
493 + rpm_clks = desc->clks;
494 + num_clks = desc->num_clks;
496 + rcc = devm_kzalloc(&pdev->dev, sizeof(*rcc) + sizeof(*hws) * num_clks,
503 + data->num = num_clks;
505 + for (i = 0; i < num_clks; i++) {
509 + rpm_clks[i]->rpm = rpm;
511 + ret = clk_rpm_handoff(rpm_clks[i]);
516 + for (i = 0; i < num_clks; i++) {
517 + if (!rpm_clks[i]) {
518 + data->hws[i] = ERR_PTR(-ENOENT);
522 + ret = devm_clk_hw_register(&pdev->dev, &rpm_clks[i]->hw);
527 + ret = of_clk_add_hw_provider(pdev->dev.of_node, of_clk_hw_onecell_get,
534 + dev_err(&pdev->dev, "Error registering RPM Clock driver (%d)\n", ret);
538 +static int rpm_clk_remove(struct platform_device *pdev)
540 + of_clk_del_provider(pdev->dev.of_node);
544 +static struct platform_driver rpm_clk_driver = {
546 + .name = "qcom-clk-rpm",
547 + .of_match_table = rpm_clk_match_table,
549 + .probe = rpm_clk_probe,
550 + .remove = rpm_clk_remove,
553 +static int __init rpm_clk_init(void)
555 + return platform_driver_register(&rpm_clk_driver);
557 +core_initcall(rpm_clk_init);
559 +static void __exit rpm_clk_exit(void)
561 + platform_driver_unregister(&rpm_clk_driver);
563 +module_exit(rpm_clk_exit);
565 +MODULE_DESCRIPTION("Qualcomm RPM Clock Controller Driver");
566 +MODULE_LICENSE("GPL v2");
567 +MODULE_ALIAS("platform:qcom-clk-rpm");
568 --- a/include/dt-bindings/clock/qcom,rpmcc.h
569 +++ b/include/dt-bindings/clock/qcom,rpmcc.h
571 #ifndef _DT_BINDINGS_CLK_MSM_RPMCC_H
572 #define _DT_BINDINGS_CLK_MSM_RPMCC_H
575 +#define RPM_PXO_CLK 0
576 +#define RPM_PXO_A_CLK 1
577 +#define RPM_CXO_CLK 2
578 +#define RPM_CXO_A_CLK 3
579 +#define RPM_APPS_FABRIC_CLK 4
580 +#define RPM_APPS_FABRIC_A_CLK 5
581 +#define RPM_CFPB_CLK 6
582 +#define RPM_CFPB_A_CLK 7
583 +#define RPM_DAYTONA_FABRIC_CLK 8
584 +#define RPM_DAYTONA_FABRIC_A_CLK 9
585 +#define RPM_EBI1_CLK 10
586 +#define RPM_EBI1_A_CLK 11
587 +#define RPM_NSS_FABRIC_0_CLK 12
588 +#define RPM_NSS_FABRIC_0_A_CLK 13
589 +#define RPM_NSS_FABRIC_1_CLK 14
590 +#define RPM_NSS_FABRIC_1_A_CLK 15
591 +#define RPM_SYS_FABRIC_CLK 16
592 +#define RPM_SYS_FABRIC_A_CLK 17
593 +#define RPM_SFPB_CLK 18
594 +#define RPM_SFPB_A_CLK 19
597 #define RPM_SMD_XO_CLK_SRC 0
598 #define RPM_SMD_XO_A_CLK_SRC 1