1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015, Sony Mobile Communications AB.
4 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
7 #include <linux/module.h>
8 #include <linux/platform_device.h>
9 #include <linux/of_platform.h>
11 #include <linux/interrupt.h>
12 #include <linux/slab.h>
14 #include <linux/rpmsg.h>
15 #include <linux/soc/qcom/smd-rpm.h>
17 #define RPM_REQUEST_TIMEOUT (5 * HZ)
20 * struct qcom_smd_rpm - state of the rpm device driver
21 * @rpm_channel: reference to the smd channel
22 * @icc: interconnect proxy device
24 * @ack: completion for acks
25 * @lock: mutual exclusion around the send/complete pair
26 * @ack_status: result of the rpm request
29 struct rpmsg_endpoint *rpm_channel;
30 struct platform_device *icc;
33 struct completion ack;
39 * struct qcom_rpm_header - header for all rpm requests and responses
40 * @service_type: identifier of the service
41 * @length: length of the payload
43 struct qcom_rpm_header {
49 * struct qcom_rpm_request - request message to the rpm
50 * @msg_id: identifier of the outgoing message
51 * @flags: active/sleep state flags
52 * @type: resource type
54 * @data_len: length of the payload following this header
56 struct qcom_rpm_request {
65 * struct qcom_rpm_message - response message from the rpm
66 * @msg_type: indicator of the type of message
67 * @length: the size of this message, including the message header
69 * @message: textual message from the rpm
71 * Multiple of these messages can be stacked in an rpm message.
73 struct qcom_rpm_message {
82 #define RPM_SERVICE_TYPE_REQUEST 0x00716572 /* "req\0" */
84 #define RPM_MSG_TYPE_ERR 0x00727265 /* "err\0" */
85 #define RPM_MSG_TYPE_MSG_ID 0x2367736d /* "msg#" */
88 * qcom_rpm_smd_write - write @buf to @type:@id
90 * @state: active/sleep state flags
91 * @type: resource type
92 * @id: resource identifier
93 * @buf: the data to be written
94 * @count: number of bytes in @buf
96 int qcom_rpm_smd_write(struct qcom_smd_rpm *rpm,
102 static unsigned msg_id = 1;
106 struct qcom_rpm_header hdr;
107 struct qcom_rpm_request req;
110 size_t size = sizeof(*pkt) + count;
112 /* SMD packets to the RPM may not exceed 256 bytes */
113 if (WARN_ON(size >= 256))
116 pkt = kmalloc(size, GFP_KERNEL);
120 mutex_lock(&rpm->lock);
122 pkt->hdr.service_type = cpu_to_le32(RPM_SERVICE_TYPE_REQUEST);
123 pkt->hdr.length = cpu_to_le32(sizeof(struct qcom_rpm_request) + count);
125 pkt->req.msg_id = cpu_to_le32(msg_id++);
126 pkt->req.flags = cpu_to_le32(state);
127 pkt->req.type = cpu_to_le32(type);
128 pkt->req.id = cpu_to_le32(id);
129 pkt->req.data_len = cpu_to_le32(count);
130 memcpy(pkt->payload, buf, count);
132 ret = rpmsg_send(rpm->rpm_channel, pkt, size);
136 left = wait_for_completion_timeout(&rpm->ack, RPM_REQUEST_TIMEOUT);
140 ret = rpm->ack_status;
144 mutex_unlock(&rpm->lock);
147 EXPORT_SYMBOL(qcom_rpm_smd_write);
149 static int qcom_smd_rpm_callback(struct rpmsg_device *rpdev,
155 const struct qcom_rpm_header *hdr = data;
156 size_t hdr_length = le32_to_cpu(hdr->length);
157 const struct qcom_rpm_message *msg;
158 struct qcom_smd_rpm *rpm = dev_get_drvdata(&rpdev->dev);
159 const u8 *buf = data + sizeof(struct qcom_rpm_header);
160 const u8 *end = buf + hdr_length;
165 if (le32_to_cpu(hdr->service_type) != RPM_SERVICE_TYPE_REQUEST ||
166 hdr_length < sizeof(struct qcom_rpm_message)) {
167 dev_err(rpm->dev, "invalid request\n");
172 msg = (struct qcom_rpm_message *)buf;
173 msg_length = le32_to_cpu(msg->length);
174 switch (le32_to_cpu(msg->msg_type)) {
175 case RPM_MSG_TYPE_MSG_ID:
177 case RPM_MSG_TYPE_ERR:
178 len = min_t(u32, ALIGN(msg_length, 4), sizeof(msgbuf));
179 memcpy_fromio(msgbuf, msg->message, len);
182 if (!strcmp(msgbuf, "resource does not exist"))
189 buf = PTR_ALIGN(buf + 2 * sizeof(u32) + msg_length, 4);
192 rpm->ack_status = status;
197 static int qcom_smd_rpm_probe(struct rpmsg_device *rpdev)
199 struct qcom_smd_rpm *rpm;
202 rpm = devm_kzalloc(&rpdev->dev, sizeof(*rpm), GFP_KERNEL);
206 mutex_init(&rpm->lock);
207 init_completion(&rpm->ack);
209 rpm->dev = &rpdev->dev;
210 rpm->rpm_channel = rpdev->ept;
211 dev_set_drvdata(&rpdev->dev, rpm);
213 rpm->icc = platform_device_register_data(&rpdev->dev, "icc_smd_rpm", -1,
215 if (IS_ERR(rpm->icc))
216 return PTR_ERR(rpm->icc);
218 ret = of_platform_populate(rpdev->dev.of_node, NULL, NULL, &rpdev->dev);
220 platform_device_unregister(rpm->icc);
225 static void qcom_smd_rpm_remove(struct rpmsg_device *rpdev)
227 struct qcom_smd_rpm *rpm = dev_get_drvdata(&rpdev->dev);
229 platform_device_unregister(rpm->icc);
230 of_platform_depopulate(&rpdev->dev);
233 static const struct of_device_id qcom_smd_rpm_of_match[] = {
234 { .compatible = "qcom,rpm-apq8084" },
235 { .compatible = "qcom,rpm-ipq6018" },
236 { .compatible = "qcom,rpm-msm8916" },
237 { .compatible = "qcom,rpm-msm8936" },
238 { .compatible = "qcom,rpm-msm8974" },
239 { .compatible = "qcom,rpm-msm8976" },
240 { .compatible = "qcom,rpm-msm8994" },
241 { .compatible = "qcom,rpm-msm8996" },
242 { .compatible = "qcom,rpm-msm8998" },
243 { .compatible = "qcom,rpm-sdm660" },
244 { .compatible = "qcom,rpm-qcs404" },
247 MODULE_DEVICE_TABLE(of, qcom_smd_rpm_of_match);
249 static struct rpmsg_driver qcom_smd_rpm_driver = {
250 .probe = qcom_smd_rpm_probe,
251 .remove = qcom_smd_rpm_remove,
252 .callback = qcom_smd_rpm_callback,
254 .name = "qcom_smd_rpm",
255 .of_match_table = qcom_smd_rpm_of_match,
259 static int __init qcom_smd_rpm_init(void)
261 return register_rpmsg_driver(&qcom_smd_rpm_driver);
263 arch_initcall(qcom_smd_rpm_init);
265 static void __exit qcom_smd_rpm_exit(void)
267 unregister_rpmsg_driver(&qcom_smd_rpm_driver);
269 module_exit(qcom_smd_rpm_exit);
271 MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
272 MODULE_DESCRIPTION("Qualcomm SMD backed RPM driver");
273 MODULE_LICENSE("GPL v2");