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 * @ack: completion for acks
23 * @lock: mutual exclusion around the send/complete pair
24 * @ack_status: result of the rpm request
27 struct rpmsg_endpoint *rpm_channel;
30 struct completion ack;
36 * struct qcom_rpm_header - header for all rpm requests and responses
37 * @service_type: identifier of the service
38 * @length: length of the payload
40 struct qcom_rpm_header {
46 * struct qcom_rpm_request - request message to the rpm
47 * @msg_id: identifier of the outgoing message
48 * @flags: active/sleep state flags
49 * @type: resource type
51 * @data_len: length of the payload following this header
53 struct qcom_rpm_request {
62 * struct qcom_rpm_message - response message from the rpm
63 * @msg_type: indicator of the type of message
64 * @length: the size of this message, including the message header
66 * @message: textual message from the rpm
68 * Multiple of these messages can be stacked in an rpm message.
70 struct qcom_rpm_message {
79 #define RPM_SERVICE_TYPE_REQUEST 0x00716572 /* "req\0" */
81 #define RPM_MSG_TYPE_ERR 0x00727265 /* "err\0" */
82 #define RPM_MSG_TYPE_MSG_ID 0x2367736d /* "msg#" */
85 * qcom_rpm_smd_write - write @buf to @type:@id
87 * @type: resource type
88 * @id: resource identifier
89 * @buf: the data to be written
90 * @count: number of bytes in @buf
92 int qcom_rpm_smd_write(struct qcom_smd_rpm *rpm,
98 static unsigned msg_id = 1;
102 struct qcom_rpm_header hdr;
103 struct qcom_rpm_request req;
106 size_t size = sizeof(*pkt) + count;
108 /* SMD packets to the RPM may not exceed 256 bytes */
109 if (WARN_ON(size >= 256))
112 pkt = kmalloc(size, GFP_KERNEL);
116 mutex_lock(&rpm->lock);
118 pkt->hdr.service_type = cpu_to_le32(RPM_SERVICE_TYPE_REQUEST);
119 pkt->hdr.length = cpu_to_le32(sizeof(struct qcom_rpm_request) + count);
121 pkt->req.msg_id = cpu_to_le32(msg_id++);
122 pkt->req.flags = cpu_to_le32(state);
123 pkt->req.type = cpu_to_le32(type);
124 pkt->req.id = cpu_to_le32(id);
125 pkt->req.data_len = cpu_to_le32(count);
126 memcpy(pkt->payload, buf, count);
128 ret = rpmsg_send(rpm->rpm_channel, pkt, size);
132 left = wait_for_completion_timeout(&rpm->ack, RPM_REQUEST_TIMEOUT);
136 ret = rpm->ack_status;
140 mutex_unlock(&rpm->lock);
143 EXPORT_SYMBOL(qcom_rpm_smd_write);
145 static int qcom_smd_rpm_callback(struct rpmsg_device *rpdev,
151 const struct qcom_rpm_header *hdr = data;
152 size_t hdr_length = le32_to_cpu(hdr->length);
153 const struct qcom_rpm_message *msg;
154 struct qcom_smd_rpm *rpm = dev_get_drvdata(&rpdev->dev);
155 const u8 *buf = data + sizeof(struct qcom_rpm_header);
156 const u8 *end = buf + hdr_length;
161 if (le32_to_cpu(hdr->service_type) != RPM_SERVICE_TYPE_REQUEST ||
162 hdr_length < sizeof(struct qcom_rpm_message)) {
163 dev_err(rpm->dev, "invalid request\n");
168 msg = (struct qcom_rpm_message *)buf;
169 msg_length = le32_to_cpu(msg->length);
170 switch (le32_to_cpu(msg->msg_type)) {
171 case RPM_MSG_TYPE_MSG_ID:
173 case RPM_MSG_TYPE_ERR:
174 len = min_t(u32, ALIGN(msg_length, 4), sizeof(msgbuf));
175 memcpy_fromio(msgbuf, msg->message, len);
178 if (!strcmp(msgbuf, "resource does not exist"))
185 buf = PTR_ALIGN(buf + 2 * sizeof(u32) + msg_length, 4);
188 rpm->ack_status = status;
193 static int qcom_smd_rpm_probe(struct rpmsg_device *rpdev)
195 struct qcom_smd_rpm *rpm;
197 rpm = devm_kzalloc(&rpdev->dev, sizeof(*rpm), GFP_KERNEL);
201 mutex_init(&rpm->lock);
202 init_completion(&rpm->ack);
204 rpm->dev = &rpdev->dev;
205 rpm->rpm_channel = rpdev->ept;
206 dev_set_drvdata(&rpdev->dev, rpm);
208 return of_platform_populate(rpdev->dev.of_node, NULL, NULL, &rpdev->dev);
211 static void qcom_smd_rpm_remove(struct rpmsg_device *rpdev)
213 of_platform_depopulate(&rpdev->dev);
216 static const struct of_device_id qcom_smd_rpm_of_match[] = {
217 { .compatible = "qcom,rpm-apq8084" },
218 { .compatible = "qcom,rpm-msm8916" },
219 { .compatible = "qcom,rpm-msm8974" },
220 { .compatible = "qcom,rpm-msm8996" },
221 { .compatible = "qcom,rpm-msm8998" },
222 { .compatible = "qcom,rpm-sdm660" },
223 { .compatible = "qcom,rpm-qcs404" },
226 MODULE_DEVICE_TABLE(of, qcom_smd_rpm_of_match);
228 static struct rpmsg_driver qcom_smd_rpm_driver = {
229 .probe = qcom_smd_rpm_probe,
230 .remove = qcom_smd_rpm_remove,
231 .callback = qcom_smd_rpm_callback,
233 .name = "qcom_smd_rpm",
234 .of_match_table = qcom_smd_rpm_of_match,
238 static int __init qcom_smd_rpm_init(void)
240 return register_rpmsg_driver(&qcom_smd_rpm_driver);
242 arch_initcall(qcom_smd_rpm_init);
244 static void __exit qcom_smd_rpm_exit(void)
246 unregister_rpmsg_driver(&qcom_smd_rpm_driver);
248 module_exit(qcom_smd_rpm_exit);
250 MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
251 MODULE_DESCRIPTION("Qualcomm SMD backed RPM driver");
252 MODULE_LICENSE("GPL v2");