1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * RPM over SMD communication wrapper for interconnects
4  *
5  * Copyright (C) 2019 Linaro Ltd
6  * Author: Georgi Djakov <georgi.djakov@linaro.org>
7  */
8 
9 #include <linux/interconnect-provider.h>
10 #include <linux/module.h>
11 #include <linux/of.h>
12 #include <linux/of_platform.h>
13 #include <linux/platform_device.h>
14 #include <linux/soc/qcom/smd-rpm.h>
15 
16 #include "icc-rpm.h"
17 
18 #define RPM_KEY_BW		0x00007762
19 #define QCOM_RPM_SMD_KEY_RATE	0x007a484b
20 
21 static struct qcom_smd_rpm *icc_smd_rpm;
22 
23 struct icc_rpm_smd_req {
24 	__le32 key;
25 	__le32 nbytes;
26 	__le32 value;
27 };
28 
29 bool qcom_icc_rpm_smd_available(void)
30 {
31 	return !!icc_smd_rpm;
32 }
33 EXPORT_SYMBOL_GPL(qcom_icc_rpm_smd_available);
34 
35 int qcom_icc_rpm_smd_send(int ctx, int rsc_type, int id, u32 val)
36 {
37 	struct icc_rpm_smd_req req = {
38 		.key = cpu_to_le32(RPM_KEY_BW),
39 		.nbytes = cpu_to_le32(sizeof(u32)),
40 		.value = cpu_to_le32(val),
41 	};
42 
43 	return qcom_rpm_smd_write(icc_smd_rpm, ctx, rsc_type, id, &req,
44 				  sizeof(req));
45 }
46 EXPORT_SYMBOL_GPL(qcom_icc_rpm_smd_send);
47 
48 int qcom_icc_rpm_set_bus_rate(const struct rpm_clk_resource *clk, int ctx, u32 rate)
49 {
50 	struct clk_smd_rpm_req req = {
51 		.key = cpu_to_le32(QCOM_RPM_SMD_KEY_RATE),
52 		.nbytes = cpu_to_le32(sizeof(u32)),
53 	};
54 
55 	/* Branch clocks are only on/off */
56 	if (clk->branch)
57 		rate = !!rate;
58 
59 	req.value = cpu_to_le32(rate);
60 	return qcom_rpm_smd_write(icc_smd_rpm,
61 				  ctx,
62 				  clk->resource_type,
63 				  clk->clock_id,
64 				  &req, sizeof(req));
65 }
66 EXPORT_SYMBOL_GPL(qcom_icc_rpm_set_bus_rate);
67 
68 static int qcom_icc_rpm_smd_remove(struct platform_device *pdev)
69 {
70 	icc_smd_rpm = NULL;
71 
72 	return 0;
73 }
74 
75 static int qcom_icc_rpm_smd_probe(struct platform_device *pdev)
76 {
77 	icc_smd_rpm = dev_get_drvdata(pdev->dev.parent);
78 
79 	if (!icc_smd_rpm) {
80 		dev_err(&pdev->dev, "unable to retrieve handle to RPM\n");
81 		return -ENODEV;
82 	}
83 
84 	return 0;
85 }
86 
87 static struct platform_driver qcom_interconnect_rpm_smd_driver = {
88 	.driver = {
89 		.name		= "icc_smd_rpm",
90 	},
91 	.probe = qcom_icc_rpm_smd_probe,
92 	.remove = qcom_icc_rpm_smd_remove,
93 };
94 module_platform_driver(qcom_interconnect_rpm_smd_driver);
95 MODULE_AUTHOR("Georgi Djakov <georgi.djakov@linaro.org>");
96 MODULE_DESCRIPTION("Qualcomm SMD RPM interconnect proxy driver");
97 MODULE_LICENSE("GPL v2");
98 MODULE_ALIAS("platform:icc_smd_rpm");
99