smd-rpm.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (c) 2015, Sony Mobile Communications AB.
  3. * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
  4. *
  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.
  8. *
  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.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/io.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/slab.h>
  20. #include <linux/soc/qcom/smd.h>
  21. #include <linux/soc/qcom/smd-rpm.h>
  22. #define RPM_REQUEST_TIMEOUT (5 * HZ)
  23. /**
  24. * struct qcom_smd_rpm - state of the rpm device driver
  25. * @rpm_channel: reference to the smd channel
  26. * @ack: completion for acks
  27. * @lock: mutual exclusion around the send/complete pair
  28. * @ack_status: result of the rpm request
  29. */
  30. struct qcom_smd_rpm {
  31. struct qcom_smd_channel *rpm_channel;
  32. struct completion ack;
  33. struct mutex lock;
  34. int ack_status;
  35. };
  36. /**
  37. * struct qcom_rpm_header - header for all rpm requests and responses
  38. * @service_type: identifier of the service
  39. * @length: length of the payload
  40. */
  41. struct qcom_rpm_header {
  42. __le32 service_type;
  43. __le32 length;
  44. };
  45. /**
  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
  50. * @id: resource id
  51. * @data_len: length of the payload following this header
  52. */
  53. struct qcom_rpm_request {
  54. __le32 msg_id;
  55. __le32 flags;
  56. __le32 type;
  57. __le32 id;
  58. __le32 data_len;
  59. };
  60. /**
  61. * struct qcom_rpm_message - response message from the rpm
  62. * @msg_type: indicator of the type of message
  63. * @length: the size of this message, including the message header
  64. * @msg_id: message id
  65. * @message: textual message from the rpm
  66. *
  67. * Multiple of these messages can be stacked in an rpm message.
  68. */
  69. struct qcom_rpm_message {
  70. __le32 msg_type;
  71. __le32 length;
  72. union {
  73. __le32 msg_id;
  74. u8 message[0];
  75. };
  76. };
  77. #define RPM_SERVICE_TYPE_REQUEST 0x00716572 /* "req\0" */
  78. #define RPM_MSG_TYPE_ERR 0x00727265 /* "err\0" */
  79. #define RPM_MSG_TYPE_MSG_ID 0x2367736d /* "msg#" */
  80. /**
  81. * qcom_rpm_smd_write - write @buf to @type:@id
  82. * @rpm: rpm handle
  83. * @type: resource type
  84. * @id: resource identifier
  85. * @buf: the data to be written
  86. * @count: number of bytes in @buf
  87. */
  88. int qcom_rpm_smd_write(struct qcom_smd_rpm *rpm,
  89. int state,
  90. u32 type, u32 id,
  91. void *buf,
  92. size_t count)
  93. {
  94. static unsigned msg_id = 1;
  95. int left;
  96. int ret;
  97. struct {
  98. struct qcom_rpm_header hdr;
  99. struct qcom_rpm_request req;
  100. u8 payload[];
  101. } *pkt;
  102. size_t size = sizeof(*pkt) + count;
  103. /* SMD packets to the RPM may not exceed 256 bytes */
  104. if (WARN_ON(size >= 256))
  105. return -EINVAL;
  106. pkt = kmalloc(size, GFP_KERNEL);
  107. if (!pkt)
  108. return -ENOMEM;
  109. mutex_lock(&rpm->lock);
  110. pkt->hdr.service_type = cpu_to_le32(RPM_SERVICE_TYPE_REQUEST);
  111. pkt->hdr.length = cpu_to_le32(sizeof(struct qcom_rpm_request) + count);
  112. pkt->req.msg_id = cpu_to_le32(msg_id++);
  113. pkt->req.flags = cpu_to_le32(state);
  114. pkt->req.type = cpu_to_le32(type);
  115. pkt->req.id = cpu_to_le32(id);
  116. pkt->req.data_len = cpu_to_le32(count);
  117. memcpy(pkt->payload, buf, count);
  118. ret = qcom_smd_send(rpm->rpm_channel, pkt, size);
  119. if (ret)
  120. goto out;
  121. left = wait_for_completion_timeout(&rpm->ack, RPM_REQUEST_TIMEOUT);
  122. if (!left)
  123. ret = -ETIMEDOUT;
  124. else
  125. ret = rpm->ack_status;
  126. out:
  127. kfree(pkt);
  128. mutex_unlock(&rpm->lock);
  129. return ret;
  130. }
  131. EXPORT_SYMBOL(qcom_rpm_smd_write);
  132. static int qcom_smd_rpm_callback(struct qcom_smd_device *qsdev,
  133. const void *data,
  134. size_t count)
  135. {
  136. const struct qcom_rpm_header *hdr = data;
  137. size_t hdr_length = le32_to_cpu(hdr->length);
  138. const struct qcom_rpm_message *msg;
  139. struct qcom_smd_rpm *rpm = dev_get_drvdata(&qsdev->dev);
  140. const u8 *buf = data + sizeof(struct qcom_rpm_header);
  141. const u8 *end = buf + hdr_length;
  142. char msgbuf[32];
  143. int status = 0;
  144. u32 len, msg_length;
  145. if (le32_to_cpu(hdr->service_type) != RPM_SERVICE_TYPE_REQUEST ||
  146. hdr_length < sizeof(struct qcom_rpm_message)) {
  147. dev_err(&qsdev->dev, "invalid request\n");
  148. return 0;
  149. }
  150. while (buf < end) {
  151. msg = (struct qcom_rpm_message *)buf;
  152. msg_length = le32_to_cpu(msg->length);
  153. switch (le32_to_cpu(msg->msg_type)) {
  154. case RPM_MSG_TYPE_MSG_ID:
  155. break;
  156. case RPM_MSG_TYPE_ERR:
  157. len = min_t(u32, ALIGN(msg_length, 4), sizeof(msgbuf));
  158. memcpy_fromio(msgbuf, msg->message, len);
  159. msgbuf[len - 1] = 0;
  160. if (!strcmp(msgbuf, "resource does not exist"))
  161. status = -ENXIO;
  162. else
  163. status = -EINVAL;
  164. break;
  165. }
  166. buf = PTR_ALIGN(buf + 2 * sizeof(u32) + msg_length, 4);
  167. }
  168. rpm->ack_status = status;
  169. complete(&rpm->ack);
  170. return 0;
  171. }
  172. static int qcom_smd_rpm_probe(struct qcom_smd_device *sdev)
  173. {
  174. struct qcom_smd_rpm *rpm;
  175. rpm = devm_kzalloc(&sdev->dev, sizeof(*rpm), GFP_KERNEL);
  176. if (!rpm)
  177. return -ENOMEM;
  178. mutex_init(&rpm->lock);
  179. init_completion(&rpm->ack);
  180. rpm->rpm_channel = sdev->channel;
  181. dev_set_drvdata(&sdev->dev, rpm);
  182. return of_platform_populate(sdev->dev.of_node, NULL, NULL, &sdev->dev);
  183. }
  184. static void qcom_smd_rpm_remove(struct qcom_smd_device *sdev)
  185. {
  186. of_platform_depopulate(&sdev->dev);
  187. }
  188. static const struct of_device_id qcom_smd_rpm_of_match[] = {
  189. { .compatible = "qcom,rpm-msm8974" },
  190. {}
  191. };
  192. MODULE_DEVICE_TABLE(of, qcom_smd_rpm_of_match);
  193. static struct qcom_smd_driver qcom_smd_rpm_driver = {
  194. .probe = qcom_smd_rpm_probe,
  195. .remove = qcom_smd_rpm_remove,
  196. .callback = qcom_smd_rpm_callback,
  197. .driver = {
  198. .name = "qcom_smd_rpm",
  199. .owner = THIS_MODULE,
  200. .of_match_table = qcom_smd_rpm_of_match,
  201. },
  202. };
  203. static int __init qcom_smd_rpm_init(void)
  204. {
  205. return qcom_smd_driver_register(&qcom_smd_rpm_driver);
  206. }
  207. arch_initcall(qcom_smd_rpm_init);
  208. static void __exit qcom_smd_rpm_exit(void)
  209. {
  210. qcom_smd_driver_unregister(&qcom_smd_rpm_driver);
  211. }
  212. module_exit(qcom_smd_rpm_exit);
  213. MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
  214. MODULE_DESCRIPTION("Qualcomm SMD backed RPM driver");
  215. MODULE_LICENSE("GPL v2");