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
= cpu_to_le32(RPM_SERVICE_TYPE_REQUEST
);
126 pkt
->hdr
.length
= cpu_to_le32(sizeof(struct qcom_rpm_request
) + count
);
128 pkt
->req
.msg_id
= cpu_to_le32(msg_id
++);
129 pkt
->req
.flags
= cpu_to_le32(state
);
130 pkt
->req
.type
= cpu_to_le32(type
);
131 pkt
->req
.id
= cpu_to_le32(id
);
132 pkt
->req
.data_len
= cpu_to_le32(count
);
133 memcpy(pkt
->payload
, buf
, count
);
135 ret
= qcom_smd_send(rpm
->rpm_channel
, pkt
, size
);
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 size_t hdr_length
= le32_to_cpu(hdr
->length
);
158 const struct qcom_rpm_message
*msg
;
159 struct qcom_smd_rpm
*rpm
= dev_get_drvdata(&qsdev
->dev
);
160 const u8
*buf
= data
+ sizeof(struct qcom_rpm_header
);
161 const u8
*end
= buf
+ hdr_length
;
166 if (le32_to_cpu(hdr
->service_type
) != RPM_SERVICE_TYPE_REQUEST
||
167 hdr_length
< sizeof(struct qcom_rpm_message
)) {
168 dev_err(&qsdev
->dev
, "invalid request\n");
173 msg
= (struct qcom_rpm_message
*)buf
;
174 msg_length
= le32_to_cpu(msg
->length
);
175 switch (le32_to_cpu(msg
->msg_type
)) {
176 case RPM_MSG_TYPE_MSG_ID
:
178 case RPM_MSG_TYPE_ERR
:
179 len
= min_t(u32
, ALIGN(msg_length
, 4), sizeof(msgbuf
));
180 memcpy_fromio(msgbuf
, msg
->message
, len
);
183 if (!strcmp(msgbuf
, "resource does not exist"))
190 buf
= PTR_ALIGN(buf
+ 2 * sizeof(u32
) + msg_length
, 4);
193 rpm
->ack_status
= status
;
198 static int qcom_smd_rpm_probe(struct qcom_smd_device
*sdev
)
200 struct qcom_smd_rpm
*rpm
;
202 rpm
= devm_kzalloc(&sdev
->dev
, sizeof(*rpm
), GFP_KERNEL
);
206 mutex_init(&rpm
->lock
);
207 init_completion(&rpm
->ack
);
209 rpm
->rpm_channel
= sdev
->channel
;
211 dev_set_drvdata(&sdev
->dev
, rpm
);
213 return of_platform_populate(sdev
->dev
.of_node
, NULL
, NULL
, &sdev
->dev
);
216 static void qcom_smd_rpm_remove(struct qcom_smd_device
*sdev
)
218 of_platform_depopulate(&sdev
->dev
);
221 static const struct of_device_id qcom_smd_rpm_of_match
[] = {
222 { .compatible
= "qcom,rpm-msm8974" },
225 MODULE_DEVICE_TABLE(of
, qcom_smd_rpm_of_match
);
227 static struct qcom_smd_driver qcom_smd_rpm_driver
= {
228 .probe
= qcom_smd_rpm_probe
,
229 .remove
= qcom_smd_rpm_remove
,
230 .callback
= qcom_smd_rpm_callback
,
232 .name
= "qcom_smd_rpm",
233 .owner
= THIS_MODULE
,
234 .of_match_table
= qcom_smd_rpm_of_match
,
238 static int __init
qcom_smd_rpm_init(void)
240 return qcom_smd_driver_register(&qcom_smd_rpm_driver
);
242 arch_initcall(qcom_smd_rpm_init
);
244 static void __exit
qcom_smd_rpm_exit(void)
246 qcom_smd_driver_unregister(&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");