Merge tag 'soc-dt-6.4' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
[linux-block.git] / drivers / soc / qcom / smd-rpm.c
CommitLineData
97fb5e8d 1// SPDX-License-Identifier: GPL-2.0-only
936f14cf
BA
2/*
3 * Copyright (c) 2015, Sony Mobile Communications AB.
4 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
936f14cf
BA
5 */
6
7#include <linux/module.h>
8#include <linux/platform_device.h>
9#include <linux/of_platform.h>
10#include <linux/io.h>
11#include <linux/interrupt.h>
50e1b29b 12#include <linux/slab.h>
936f14cf 13
5052de8d 14#include <linux/rpmsg.h>
936f14cf
BA
15#include <linux/soc/qcom/smd-rpm.h>
16
17#define RPM_REQUEST_TIMEOUT (5 * HZ)
18
19/**
20 * struct qcom_smd_rpm - state of the rpm device driver
21 * @rpm_channel: reference to the smd channel
66e6a633 22 * @icc: interconnect proxy device
e434e0c4 23 * @dev: rpm device
936f14cf
BA
24 * @ack: completion for acks
25 * @lock: mutual exclusion around the send/complete pair
26 * @ack_status: result of the rpm request
27 */
28struct qcom_smd_rpm {
5052de8d 29 struct rpmsg_endpoint *rpm_channel;
66e6a633 30 struct platform_device *icc;
b853cb96 31 struct device *dev;
936f14cf
BA
32
33 struct completion ack;
34 struct mutex lock;
35 int ack_status;
36};
37
38/**
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
42 */
43struct qcom_rpm_header {
30b7ea5e
SB
44 __le32 service_type;
45 __le32 length;
936f14cf
BA
46};
47
48/**
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
53 * @id: resource id
54 * @data_len: length of the payload following this header
55 */
56struct qcom_rpm_request {
30b7ea5e
SB
57 __le32 msg_id;
58 __le32 flags;
59 __le32 type;
60 __le32 id;
61 __le32 data_len;
936f14cf
BA
62};
63
64/**
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
68 * @msg_id: message id
69 * @message: textual message from the rpm
70 *
71 * Multiple of these messages can be stacked in an rpm message.
72 */
73struct qcom_rpm_message {
30b7ea5e
SB
74 __le32 msg_type;
75 __le32 length;
936f14cf 76 union {
30b7ea5e 77 __le32 msg_id;
1e9dd807 78 DECLARE_FLEX_ARRAY(u8, message);
936f14cf
BA
79 };
80};
81
82#define RPM_SERVICE_TYPE_REQUEST 0x00716572 /* "req\0" */
83
84#define RPM_MSG_TYPE_ERR 0x00727265 /* "err\0" */
85#define RPM_MSG_TYPE_MSG_ID 0x2367736d /* "msg#" */
86
87/**
88 * qcom_rpm_smd_write - write @buf to @type:@id
89 * @rpm: rpm handle
e434e0c4 90 * @state: active/sleep state flags
936f14cf
BA
91 * @type: resource type
92 * @id: resource identifier
93 * @buf: the data to be written
94 * @count: number of bytes in @buf
95 */
96int qcom_rpm_smd_write(struct qcom_smd_rpm *rpm,
97 int state,
98 u32 type, u32 id,
99 void *buf,
100 size_t count)
101{
102 static unsigned msg_id = 1;
103 int left;
104 int ret;
936f14cf
BA
105 struct {
106 struct qcom_rpm_header hdr;
107 struct qcom_rpm_request req;
50e1b29b
SB
108 u8 payload[];
109 } *pkt;
110 size_t size = sizeof(*pkt) + count;
936f14cf
BA
111
112 /* SMD packets to the RPM may not exceed 256 bytes */
50e1b29b 113 if (WARN_ON(size >= 256))
936f14cf
BA
114 return -EINVAL;
115
5808c532 116 pkt = kmalloc(size, GFP_ATOMIC);
50e1b29b
SB
117 if (!pkt)
118 return -ENOMEM;
119
936f14cf
BA
120 mutex_lock(&rpm->lock);
121
30b7ea5e
SB
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);
936f14cf 124
30b7ea5e 125 pkt->req.msg_id = cpu_to_le32(msg_id++);
a8ddd1b9 126 pkt->req.flags = cpu_to_le32(state);
30b7ea5e
SB
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);
50e1b29b 130 memcpy(pkt->payload, buf, count);
936f14cf 131
5052de8d 132 ret = rpmsg_send(rpm->rpm_channel, pkt, size);
936f14cf
BA
133 if (ret)
134 goto out;
135
136 left = wait_for_completion_timeout(&rpm->ack, RPM_REQUEST_TIMEOUT);
137 if (!left)
138 ret = -ETIMEDOUT;
139 else
140 ret = rpm->ack_status;
141
142out:
50e1b29b 143 kfree(pkt);
936f14cf
BA
144 mutex_unlock(&rpm->lock);
145 return ret;
146}
147EXPORT_SYMBOL(qcom_rpm_smd_write);
148
5052de8d
BA
149static int qcom_smd_rpm_callback(struct rpmsg_device *rpdev,
150 void *data,
151 int count,
152 void *priv,
153 u32 addr)
936f14cf
BA
154{
155 const struct qcom_rpm_header *hdr = data;
30b7ea5e 156 size_t hdr_length = le32_to_cpu(hdr->length);
936f14cf 157 const struct qcom_rpm_message *msg;
5052de8d 158 struct qcom_smd_rpm *rpm = dev_get_drvdata(&rpdev->dev);
936f14cf 159 const u8 *buf = data + sizeof(struct qcom_rpm_header);
30b7ea5e 160 const u8 *end = buf + hdr_length;
936f14cf
BA
161 char msgbuf[32];
162 int status = 0;
30b7ea5e 163 u32 len, msg_length;
936f14cf 164
30b7ea5e
SB
165 if (le32_to_cpu(hdr->service_type) != RPM_SERVICE_TYPE_REQUEST ||
166 hdr_length < sizeof(struct qcom_rpm_message)) {
b853cb96 167 dev_err(rpm->dev, "invalid request\n");
936f14cf
BA
168 return 0;
169 }
170
171 while (buf < end) {
172 msg = (struct qcom_rpm_message *)buf;
30b7ea5e
SB
173 msg_length = le32_to_cpu(msg->length);
174 switch (le32_to_cpu(msg->msg_type)) {
936f14cf
BA
175 case RPM_MSG_TYPE_MSG_ID:
176 break;
177 case RPM_MSG_TYPE_ERR:
30b7ea5e 178 len = min_t(u32, ALIGN(msg_length, 4), sizeof(msgbuf));
936f14cf
BA
179 memcpy_fromio(msgbuf, msg->message, len);
180 msgbuf[len - 1] = 0;
181
182 if (!strcmp(msgbuf, "resource does not exist"))
183 status = -ENXIO;
184 else
185 status = -EINVAL;
186 break;
187 }
188
30b7ea5e 189 buf = PTR_ALIGN(buf + 2 * sizeof(u32) + msg_length, 4);
936f14cf
BA
190 }
191
192 rpm->ack_status = status;
193 complete(&rpm->ack);
194 return 0;
195}
196
5052de8d 197static int qcom_smd_rpm_probe(struct rpmsg_device *rpdev)
936f14cf
BA
198{
199 struct qcom_smd_rpm *rpm;
66e6a633 200 int ret;
936f14cf 201
5052de8d 202 rpm = devm_kzalloc(&rpdev->dev, sizeof(*rpm), GFP_KERNEL);
936f14cf
BA
203 if (!rpm)
204 return -ENOMEM;
205
206 mutex_init(&rpm->lock);
207 init_completion(&rpm->ack);
208
5052de8d
BA
209 rpm->dev = &rpdev->dev;
210 rpm->rpm_channel = rpdev->ept;
211 dev_set_drvdata(&rpdev->dev, rpm);
936f14cf 212
66e6a633
GD
213 rpm->icc = platform_device_register_data(&rpdev->dev, "icc_smd_rpm", -1,
214 NULL, 0);
215 if (IS_ERR(rpm->icc))
216 return PTR_ERR(rpm->icc);
217
218 ret = of_platform_populate(rpdev->dev.of_node, NULL, NULL, &rpdev->dev);
219 if (ret)
220 platform_device_unregister(rpm->icc);
221
222 return ret;
936f14cf
BA
223}
224
5052de8d 225static void qcom_smd_rpm_remove(struct rpmsg_device *rpdev)
936f14cf 226{
66e6a633
GD
227 struct qcom_smd_rpm *rpm = dev_get_drvdata(&rpdev->dev);
228
229 platform_device_unregister(rpm->icc);
5052de8d 230 of_platform_depopulate(&rpdev->dev);
936f14cf
BA
231}
232
233static const struct of_device_id qcom_smd_rpm_of_match[] = {
1f0947fe 234 { .compatible = "qcom,rpm-apq8084" },
84da0cf9 235 { .compatible = "qcom,rpm-ipq6018" },
64dc69f3 236 { .compatible = "qcom,rpm-ipq9574" },
d8ea59e7 237 { .compatible = "qcom,rpm-msm8226" },
73579f2a 238 { .compatible = "qcom,rpm-msm8909" },
1f0947fe 239 { .compatible = "qcom,rpm-msm8916" },
d6e52482 240 { .compatible = "qcom,rpm-msm8936" },
e972a290 241 { .compatible = "qcom,rpm-msm8953" },
936f14cf 242 { .compatible = "qcom,rpm-msm8974" },
83a81c1b 243 { .compatible = "qcom,rpm-msm8976" },
a9541d2e 244 { .compatible = "qcom,rpm-msm8994" },
2b624250 245 { .compatible = "qcom,rpm-msm8996" },
64bf6b26 246 { .compatible = "qcom,rpm-msm8998" },
4e2256d3 247 { .compatible = "qcom,rpm-sdm660" },
593cb55b 248 { .compatible = "qcom,rpm-sm6115" },
055c9aff 249 { .compatible = "qcom,rpm-sm6125" },
a30c3c6a 250 { .compatible = "qcom,rpm-sm6375" },
3e035cbd 251 { .compatible = "qcom,rpm-qcm2290" },
b7e38617 252 { .compatible = "qcom,rpm-qcs404" },
936f14cf
BA
253 {}
254};
255MODULE_DEVICE_TABLE(of, qcom_smd_rpm_of_match);
256
5052de8d 257static struct rpmsg_driver qcom_smd_rpm_driver = {
936f14cf
BA
258 .probe = qcom_smd_rpm_probe,
259 .remove = qcom_smd_rpm_remove,
260 .callback = qcom_smd_rpm_callback,
5052de8d 261 .drv = {
936f14cf 262 .name = "qcom_smd_rpm",
936f14cf
BA
263 .of_match_table = qcom_smd_rpm_of_match,
264 },
265};
266
267static int __init qcom_smd_rpm_init(void)
268{
5052de8d 269 return register_rpmsg_driver(&qcom_smd_rpm_driver);
936f14cf
BA
270}
271arch_initcall(qcom_smd_rpm_init);
272
273static void __exit qcom_smd_rpm_exit(void)
274{
5052de8d 275 unregister_rpmsg_driver(&qcom_smd_rpm_driver);
936f14cf
BA
276}
277module_exit(qcom_smd_rpm_exit);
278
279MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
280MODULE_DESCRIPTION("Qualcomm SMD backed RPM driver");
281MODULE_LICENSE("GPL v2");