2 * Copyright (c) 2015, Sony Mobile Communications AB.
3 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 and
7 * only version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/of_platform.h>
19 #include <linux/interrupt.h>
20 #include <linux/slab.h>
22 #include <linux/soc/qcom/smd.h>
23 #include <linux/soc/qcom/smd-rpm.h>
25 #define RPM_REQUEST_TIMEOUT (5 * HZ)
28 * struct qcom_smd_rpm - state of the rpm device driver
29 * @rpm_channel: reference to the smd channel
30 * @ack: completion for acks
31 * @lock: mutual exclusion around the send/complete pair
32 * @ack_status: result of the rpm request
35 struct qcom_smd_channel *rpm_channel;
37 struct completion ack;
43 * struct qcom_rpm_header - header for all rpm requests and responses
44 * @service_type: identifier of the service
45 * @length: length of the payload
47 struct qcom_rpm_header {
53 * struct qcom_rpm_request - request message to the rpm
54 * @msg_id: identifier of the outgoing message
55 * @flags: active/sleep state flags
56 * @type: resource type
58 * @data_len: length of the payload following this header
60 struct qcom_rpm_request {
69 * struct qcom_rpm_message - response message from the rpm
70 * @msg_type: indicator of the type of message
71 * @length: the size of this message, including the message header
73 * @message: textual message from the rpm
75 * Multiple of these messages can be stacked in an rpm message.
77 struct qcom_rpm_message {
86 #define RPM_SERVICE_TYPE_REQUEST 0x00716572 /* "req\0" */
88 #define RPM_MSG_TYPE_ERR 0x00727265 /* "err\0" */
89 #define RPM_MSG_TYPE_MSG_ID 0x2367736d /* "msg#" */
92 * qcom_rpm_smd_write - write @buf to @type:@id
94 * @type: resource type
95 * @id: resource identifier
96 * @buf: the data to be written
97 * @count: number of bytes in @buf
99 int qcom_rpm_smd_write(struct qcom_smd_rpm *rpm,
105 static unsigned msg_id = 1;
109 struct qcom_rpm_header hdr;
110 struct qcom_rpm_request req;
113 size_t size = sizeof(*pkt) + count;
115 /* SMD packets to the RPM may not exceed 256 bytes */
116 if (WARN_ON(size >= 256))
119 pkt = kmalloc(size, GFP_KERNEL);
123 mutex_lock(&rpm->lock);
125 pkt->hdr.service_type = RPM_SERVICE_TYPE_REQUEST;
126 pkt->hdr.length = sizeof(struct qcom_rpm_request) + count;
128 pkt->req.msg_id = msg_id++;
129 pkt->req.flags = BIT(state);
130 pkt->req.type = type;
132 pkt->req.data_len = count;
133 memcpy(pkt->payload, buf, count);
135 ret = qcom_smd_send(rpm->rpm_channel, pkt, sizeof(*pkt));
139 left = wait_for_completion_timeout(&rpm->ack, RPM_REQUEST_TIMEOUT);
143 ret = rpm->ack_status;
147 mutex_unlock(&rpm->lock);
150 EXPORT_SYMBOL(qcom_rpm_smd_write);
152 static int qcom_smd_rpm_callback(struct qcom_smd_device *qsdev,
156 const struct qcom_rpm_header *hdr = data;
157 const struct qcom_rpm_message *msg;
158 struct qcom_smd_rpm *rpm = dev_get_drvdata(&qsdev->dev);
159 const u8 *buf = data + sizeof(struct qcom_rpm_header);
160 const u8 *end = buf + hdr->length;
165 if (hdr->service_type != RPM_SERVICE_TYPE_REQUEST ||
166 hdr->length < sizeof(struct qcom_rpm_message)) {
167 dev_err(&qsdev->dev, "invalid request\n");
172 msg = (struct qcom_rpm_message *)buf;
173 switch (msg->msg_type) {
174 case RPM_MSG_TYPE_MSG_ID:
176 case RPM_MSG_TYPE_ERR:
177 len = min_t(u32, ALIGN(msg->length, 4), sizeof(msgbuf));
178 memcpy_fromio(msgbuf, msg->message, len);
181 if (!strcmp(msgbuf, "resource does not exist"))
188 buf = PTR_ALIGN(buf + 2 * sizeof(u32) + msg->length, 4);
191 rpm->ack_status = status;
196 static int qcom_smd_rpm_probe(struct qcom_smd_device *sdev)
198 struct qcom_smd_rpm *rpm;
200 rpm = devm_kzalloc(&sdev->dev, sizeof(*rpm), GFP_KERNEL);
204 mutex_init(&rpm->lock);
205 init_completion(&rpm->ack);
207 rpm->rpm_channel = sdev->channel;
209 dev_set_drvdata(&sdev->dev, rpm);
211 return of_platform_populate(sdev->dev.of_node, NULL, NULL, &sdev->dev);
214 static void qcom_smd_rpm_remove(struct qcom_smd_device *sdev)
216 of_platform_depopulate(&sdev->dev);
219 static const struct of_device_id qcom_smd_rpm_of_match[] = {
220 { .compatible = "qcom,rpm-msm8974" },
223 MODULE_DEVICE_TABLE(of, qcom_smd_rpm_of_match);
225 static struct qcom_smd_driver qcom_smd_rpm_driver = {
226 .probe = qcom_smd_rpm_probe,
227 .remove = qcom_smd_rpm_remove,
228 .callback = qcom_smd_rpm_callback,
230 .name = "qcom_smd_rpm",
231 .owner = THIS_MODULE,
232 .of_match_table = qcom_smd_rpm_of_match,
236 static int __init qcom_smd_rpm_init(void)
238 return qcom_smd_driver_register(&qcom_smd_rpm_driver);
240 arch_initcall(qcom_smd_rpm_init);
242 static void __exit qcom_smd_rpm_exit(void)
244 qcom_smd_driver_unregister(&qcom_smd_rpm_driver);
246 module_exit(qcom_smd_rpm_exit);
248 MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
249 MODULE_DESCRIPTION("Qualcomm SMD backed RPM driver");
250 MODULE_LICENSE("GPL v2");