1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2017, Linaro Ltd
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/io.h>
9 #include <linux/slab.h>
10 #include <linux/of.h>
11 #include <linux/of_platform.h>
12 #include <linux/platform_device.h>
13 #include <linux/regmap.h>
14 #include <linux/mailbox_controller.h>
15 
16 #define QCOM_APCS_IPC_BITS	32
17 
18 struct qcom_apcs_ipc {
19 	struct mbox_controller mbox;
20 	struct mbox_chan mbox_chans[QCOM_APCS_IPC_BITS];
21 
22 	struct regmap *regmap;
23 	unsigned long offset;
24 	struct platform_device *clk;
25 };
26 
27 struct qcom_apcs_ipc_data {
28 	int offset;
29 	char *clk_name;
30 };
31 
32 static const struct qcom_apcs_ipc_data ipq6018_apcs_data = {
33 	.offset = 8, .clk_name = "qcom,apss-ipq6018-clk"
34 };
35 
36 static const struct qcom_apcs_ipc_data ipq8074_apcs_data = {
37 	.offset = 8, .clk_name = NULL
38 };
39 
40 static const struct qcom_apcs_ipc_data msm8916_apcs_data = {
41 	.offset = 8, .clk_name = "qcom-apcs-msm8916-clk"
42 };
43 
44 static const struct qcom_apcs_ipc_data msm8994_apcs_data = {
45 	.offset = 8, .clk_name = NULL
46 };
47 
48 static const struct qcom_apcs_ipc_data msm8996_apcs_data = {
49 	.offset = 16, .clk_name = NULL
50 };
51 
52 static const struct qcom_apcs_ipc_data msm8998_apcs_data = {
53 	.offset = 8, .clk_name = NULL
54 };
55 
56 static const struct qcom_apcs_ipc_data sdm660_apcs_data = {
57 	.offset = 8, .clk_name = NULL
58 };
59 
60 static const struct qcom_apcs_ipc_data sm6125_apcs_data = {
61 	.offset = 8, .clk_name = NULL
62 };
63 
64 static const struct qcom_apcs_ipc_data apps_shared_apcs_data = {
65 	.offset = 12, .clk_name = NULL
66 };
67 
68 static const struct qcom_apcs_ipc_data sdx55_apcs_data = {
69 	.offset = 0x1008, .clk_name = "qcom-sdx55-acps-clk"
70 };
71 
72 static const struct regmap_config apcs_regmap_config = {
73 	.reg_bits = 32,
74 	.reg_stride = 4,
75 	.val_bits = 32,
76 	.max_register = 0x1008,
77 	.fast_io = true,
78 };
79 
80 static int qcom_apcs_ipc_send_data(struct mbox_chan *chan, void *data)
81 {
82 	struct qcom_apcs_ipc *apcs = container_of(chan->mbox,
83 						  struct qcom_apcs_ipc, mbox);
84 	unsigned long idx = (unsigned long)chan->con_priv;
85 
86 	return regmap_write(apcs->regmap, apcs->offset, BIT(idx));
87 }
88 
89 static const struct mbox_chan_ops qcom_apcs_ipc_ops = {
90 	.send_data = qcom_apcs_ipc_send_data,
91 };
92 
93 static int qcom_apcs_ipc_probe(struct platform_device *pdev)
94 {
95 	struct qcom_apcs_ipc *apcs;
96 	const struct qcom_apcs_ipc_data *apcs_data;
97 	struct regmap *regmap;
98 	struct resource *res;
99 	void __iomem *base;
100 	unsigned long i;
101 	int ret;
102 
103 	apcs = devm_kzalloc(&pdev->dev, sizeof(*apcs), GFP_KERNEL);
104 	if (!apcs)
105 		return -ENOMEM;
106 
107 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
108 	base = devm_ioremap_resource(&pdev->dev, res);
109 	if (IS_ERR(base))
110 		return PTR_ERR(base);
111 
112 	regmap = devm_regmap_init_mmio(&pdev->dev, base, &apcs_regmap_config);
113 	if (IS_ERR(regmap))
114 		return PTR_ERR(regmap);
115 
116 	apcs_data = of_device_get_match_data(&pdev->dev);
117 
118 	apcs->regmap = regmap;
119 	apcs->offset = apcs_data->offset;
120 
121 	/* Initialize channel identifiers */
122 	for (i = 0; i < ARRAY_SIZE(apcs->mbox_chans); i++)
123 		apcs->mbox_chans[i].con_priv = (void *)i;
124 
125 	apcs->mbox.dev = &pdev->dev;
126 	apcs->mbox.ops = &qcom_apcs_ipc_ops;
127 	apcs->mbox.chans = apcs->mbox_chans;
128 	apcs->mbox.num_chans = ARRAY_SIZE(apcs->mbox_chans);
129 
130 	ret = devm_mbox_controller_register(&pdev->dev, &apcs->mbox);
131 	if (ret) {
132 		dev_err(&pdev->dev, "failed to register APCS IPC controller\n");
133 		return ret;
134 	}
135 
136 	if (apcs_data->clk_name) {
137 		apcs->clk = platform_device_register_data(&pdev->dev,
138 							  apcs_data->clk_name,
139 							  PLATFORM_DEVID_AUTO,
140 							  NULL, 0);
141 		if (IS_ERR(apcs->clk))
142 			dev_err(&pdev->dev, "failed to register APCS clk\n");
143 	}
144 
145 	platform_set_drvdata(pdev, apcs);
146 
147 	return 0;
148 }
149 
150 static int qcom_apcs_ipc_remove(struct platform_device *pdev)
151 {
152 	struct qcom_apcs_ipc *apcs = platform_get_drvdata(pdev);
153 	struct platform_device *clk = apcs->clk;
154 
155 	platform_device_unregister(clk);
156 
157 	return 0;
158 }
159 
160 /* .data is the offset of the ipc register within the global block */
161 static const struct of_device_id qcom_apcs_ipc_of_match[] = {
162 	{ .compatible = "qcom,ipq6018-apcs-apps-global", .data = &ipq6018_apcs_data },
163 	{ .compatible = "qcom,ipq8074-apcs-apps-global", .data = &ipq8074_apcs_data },
164 	{ .compatible = "qcom,msm8916-apcs-kpss-global", .data = &msm8916_apcs_data },
165 	{ .compatible = "qcom,msm8939-apcs-kpss-global", .data = &msm8916_apcs_data },
166 	{ .compatible = "qcom,msm8994-apcs-kpss-global", .data = &msm8994_apcs_data },
167 	{ .compatible = "qcom,msm8996-apcs-hmss-global", .data = &msm8996_apcs_data },
168 	{ .compatible = "qcom,msm8998-apcs-hmss-global", .data = &msm8998_apcs_data },
169 	{ .compatible = "qcom,qcs404-apcs-apps-global", .data = &msm8916_apcs_data },
170 	{ .compatible = "qcom,sc7180-apss-shared", .data = &apps_shared_apcs_data },
171 	{ .compatible = "qcom,sc8180x-apss-shared", .data = &apps_shared_apcs_data },
172 	{ .compatible = "qcom,sdm660-apcs-hmss-global", .data = &sdm660_apcs_data },
173 	{ .compatible = "qcom,sdm845-apss-shared", .data = &apps_shared_apcs_data },
174 	{ .compatible = "qcom,sm6125-apcs-hmss-global", .data = &sm6125_apcs_data },
175 	{ .compatible = "qcom,sm8150-apss-shared", .data = &apps_shared_apcs_data },
176 	{ .compatible = "qcom,sdx55-apcs-gcc", .data = &sdx55_apcs_data },
177 	{}
178 };
179 MODULE_DEVICE_TABLE(of, qcom_apcs_ipc_of_match);
180 
181 static struct platform_driver qcom_apcs_ipc_driver = {
182 	.probe = qcom_apcs_ipc_probe,
183 	.remove = qcom_apcs_ipc_remove,
184 	.driver = {
185 		.name = "qcom_apcs_ipc",
186 		.of_match_table = qcom_apcs_ipc_of_match,
187 	},
188 };
189 
190 static int __init qcom_apcs_ipc_init(void)
191 {
192 	return platform_driver_register(&qcom_apcs_ipc_driver);
193 }
194 postcore_initcall(qcom_apcs_ipc_init);
195 
196 static void __exit qcom_apcs_ipc_exit(void)
197 {
198 	platform_driver_unregister(&qcom_apcs_ipc_driver);
199 }
200 module_exit(qcom_apcs_ipc_exit);
201 
202 MODULE_LICENSE("GPL v2");
203 MODULE_DESCRIPTION("Qualcomm APCS IPC driver");
204