spf-core.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /* Copyright (c) 2019-2021, The Linux Foundation. All rights reserved.
  2. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/slab.h>
  14. #include <linux/wait.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/delay.h>
  18. #include <linux/sched.h>
  19. #include <linux/of.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/jiffies.h>
  22. #include <ipc/gpr-lite.h>
  23. #include <dsp/spf-core.h>
  24. #include <dsp/digital-cdc-rsc-mgr.h>
  25. #define APM_STATE_READY_TIMEOUT_MS 10000
  26. #define Q6_READY_TIMEOUT_MS 1000
  27. #define APM_CMD_GET_SPF_STATE 0x01001021
  28. #define APM_CMD_CLOSE_ALL 0x01001013
  29. #define APM_CMD_RSP_GET_SPF_STATE 0x02001007
  30. #define APM_MODULE_INSTANCE_ID 0x00000001
  31. #define GPR_SVC_ADSP_CORE 0x3
  32. struct spf_core {
  33. struct gpr_device *adev;
  34. wait_queue_head_t wait;
  35. struct mutex lock;
  36. bool resp_received;
  37. int32_t status;
  38. };
  39. struct spf_core_private {
  40. struct device *dev;
  41. struct mutex lock;
  42. struct spf_core *spf_core_drv;
  43. bool is_initial_boot;
  44. struct work_struct add_chld_dev_work;
  45. };
  46. static struct spf_core_private *spf_core_priv;
  47. /* used to decode basic responses from Gecko */
  48. struct spf_cmd_basic_rsp {
  49. uint32_t opcode;
  50. int32_t status;
  51. };
  52. struct apm_cmd_rsp_get_spf_status_t
  53. {
  54. /* Gecko status
  55. * @values
  56. * 0 -> Not ready
  57. * 1 -> Ready
  58. */
  59. uint32_t status;
  60. };
  61. static int spf_core_callback(struct gpr_device *adev, void *data)
  62. {
  63. struct spf_core *core = dev_get_drvdata(&adev->dev);
  64. struct apm_cmd_rsp_get_spf_status_t *spf_status_rsp;
  65. struct spf_cmd_basic_rsp *basic_rsp;
  66. struct gpr_hdr *hdr = data;
  67. dev_info_ratelimited(&adev->dev, "%s: Payload %x", __func__, hdr->opcode);
  68. switch (hdr->opcode) {
  69. case GPR_IBASIC_RSP_RESULT:
  70. basic_rsp = GPR_PKT_GET_PAYLOAD(
  71. struct spf_cmd_basic_rsp,
  72. data);
  73. dev_info_ratelimited(&adev->dev, "%s: op %x status %d", __func__,
  74. basic_rsp->opcode, basic_rsp->status);
  75. if (basic_rsp->opcode == APM_CMD_CLOSE_ALL) {
  76. core->status = basic_rsp->status;
  77. } else {
  78. dev_err_ratelimited(&adev->dev, "%s: Failed response received",
  79. __func__);
  80. }
  81. core->resp_received = true;
  82. break;
  83. case APM_CMD_RSP_GET_SPF_STATE:
  84. spf_status_rsp =
  85. GPR_PKT_GET_PAYLOAD(
  86. struct apm_cmd_rsp_get_spf_status_t,
  87. data);
  88. dev_info_ratelimited(&adev->dev, "%s: sucess response received", __func__);
  89. core->status = spf_status_rsp->status;
  90. core->resp_received = true;
  91. break;
  92. default:
  93. dev_err_ratelimited(&adev->dev, "Message ID from apm: 0x%x\n",
  94. hdr->opcode);
  95. break;
  96. }
  97. if (core->resp_received)
  98. wake_up(&core->wait);
  99. return 0;
  100. }
  101. static bool __spf_core_is_apm_ready(struct spf_core *core)
  102. {
  103. struct gpr_device *adev = core->adev;
  104. struct gpr_pkt pkt;
  105. int rc;
  106. bool ret = false;
  107. pkt.hdr.header = GPR_SET_FIELD(GPR_PKT_VERSION, GPR_PKT_VER) |
  108. GPR_SET_FIELD(GPR_PKT_HEADER_SIZE, GPR_PKT_HEADER_WORD_SIZE_V) |
  109. GPR_SET_FIELD(GPR_PKT_PACKET_SIZE, GPR_PKT_HEADER_BYTE_SIZE_V);
  110. pkt.hdr.dst_port = APM_MODULE_INSTANCE_ID;
  111. pkt.hdr.src_port = GPR_SVC_ADSP_CORE;
  112. pkt.hdr.dst_domain_id = GPR_IDS_DOMAIN_ID_ADSP_V;
  113. pkt.hdr.src_domain_id = GPR_IDS_DOMAIN_ID_APPS_V;
  114. pkt.hdr.opcode = APM_CMD_GET_SPF_STATE;
  115. dev_err_ratelimited(spf_core_priv->dev, "%s: send_command ret\n", __func__);
  116. rc = gpr_send_pkt(adev, &pkt);
  117. if (rc < 0) {
  118. ret = false;
  119. goto done;
  120. }
  121. rc = wait_event_timeout(core->wait, (core->resp_received),
  122. msecs_to_jiffies(Q6_READY_TIMEOUT_MS));
  123. dev_dbg(spf_core_priv->dev, "%s: wait event unblocked \n", __func__);
  124. if (rc > 0 && core->resp_received) {
  125. ret = core->status;
  126. } else {
  127. dev_err_ratelimited(spf_core_priv->dev, "%s: command timedout, ret\n",
  128. __func__);
  129. }
  130. done:
  131. core->resp_received = false;
  132. return ret;
  133. }
  134. /**
  135. * spf_core_is_apm_ready() - Get status of adsp
  136. *
  137. * Return: Will return true if apm is ready and false if not.
  138. */
  139. bool spf_core_is_apm_ready(void)
  140. {
  141. unsigned long timeout;
  142. bool ret = false;
  143. struct spf_core *core;
  144. if (!spf_core_priv)
  145. return ret;
  146. mutex_lock(&spf_core_priv->lock);
  147. core = spf_core_priv->spf_core_drv;
  148. if (!core)
  149. goto done;
  150. timeout = jiffies + msecs_to_jiffies(APM_STATE_READY_TIMEOUT_MS);
  151. mutex_lock(&core->lock);
  152. for (;;) {
  153. if (__spf_core_is_apm_ready(core)) {
  154. ret = true;
  155. break;
  156. }
  157. usleep_range(50000, 50050);
  158. if (!time_after(timeout, jiffies)) {
  159. ret = false;
  160. break;
  161. }
  162. }
  163. mutex_unlock(&core->lock);
  164. done:
  165. mutex_unlock(&spf_core_priv->lock);
  166. return ret;
  167. }
  168. EXPORT_SYMBOL_GPL(spf_core_is_apm_ready);
  169. /**
  170. * spf_core_apm_close_all() - Get status of adsp
  171. *
  172. * Return: Will be return true if apm is ready and false if not.
  173. */
  174. void spf_core_apm_close_all(void)
  175. {
  176. int rc = 0;
  177. struct spf_core *core;
  178. struct gpr_pkt pkt;
  179. struct gpr_device *adev = NULL;
  180. if (!spf_core_priv)
  181. return;
  182. mutex_lock(&spf_core_priv->lock);
  183. core = spf_core_priv->spf_core_drv;
  184. if (!core) {
  185. mutex_unlock(&spf_core_priv->lock);
  186. return;
  187. }
  188. mutex_lock(&core->lock);
  189. adev = core->adev;
  190. pkt.hdr.header = GPR_SET_FIELD(GPR_PKT_VERSION, GPR_PKT_VER) |
  191. GPR_SET_FIELD(GPR_PKT_HEADER_SIZE,
  192. GPR_PKT_HEADER_WORD_SIZE_V) |
  193. GPR_SET_FIELD(GPR_PKT_PACKET_SIZE,
  194. GPR_PKT_HEADER_BYTE_SIZE_V);
  195. pkt.hdr.dst_port = APM_MODULE_INSTANCE_ID;
  196. pkt.hdr.src_port = GPR_SVC_ADSP_CORE;
  197. pkt.hdr.dst_domain_id = GPR_IDS_DOMAIN_ID_ADSP_V;
  198. pkt.hdr.src_domain_id = GPR_IDS_DOMAIN_ID_APPS_V;
  199. pkt.hdr.opcode = APM_CMD_CLOSE_ALL;
  200. dev_info_ratelimited(spf_core_priv->dev, "%s: send_command \n", __func__);
  201. rc = gpr_send_pkt(adev, &pkt);
  202. if (rc < 0) {
  203. dev_err_ratelimited(spf_core_priv->dev, "%s: send_pkt_failed %d\n",
  204. __func__, rc);
  205. goto done;
  206. }
  207. rc = wait_event_timeout(core->wait, (core->resp_received),
  208. msecs_to_jiffies(Q6_READY_TIMEOUT_MS));
  209. dev_info_ratelimited(spf_core_priv->dev, "%s: wait event unblocked \n", __func__);
  210. if (rc > 0 && core->resp_received) {
  211. if (core->status != 0)
  212. dev_err_ratelimited(spf_core_priv->dev, "%s, cmd failed status %d",
  213. __func__, core->status);
  214. } else {
  215. dev_err_ratelimited(spf_core_priv->dev, "%s: command timedout, ret\n",
  216. __func__);
  217. }
  218. done:
  219. core->resp_received = false;
  220. mutex_unlock(&core->lock);
  221. mutex_unlock(&spf_core_priv->lock);
  222. return;
  223. }
  224. EXPORT_SYMBOL_GPL(spf_core_apm_close_all);
  225. static int spf_core_probe(struct gpr_device *adev)
  226. {
  227. struct spf_core *core;
  228. pr_err("%s",__func__);
  229. if (!spf_core_priv) {
  230. pr_err("%s: spf_core platform probe not yet done\n", __func__);
  231. return -EPROBE_DEFER;
  232. }
  233. mutex_lock(&spf_core_priv->lock);
  234. core = kzalloc(sizeof(*core), GFP_KERNEL);
  235. if (!core) {
  236. mutex_unlock(&spf_core_priv->lock);
  237. return -ENOMEM;
  238. }
  239. dev_set_drvdata(&adev->dev, core);
  240. mutex_init(&core->lock);
  241. core->adev = adev;
  242. init_waitqueue_head(&core->wait);
  243. spf_core_priv->spf_core_drv = core;
  244. if (spf_core_priv->is_initial_boot)
  245. schedule_work(&spf_core_priv->add_chld_dev_work);
  246. mutex_unlock(&spf_core_priv->lock);
  247. return 0;
  248. }
  249. static int spf_core_exit(struct gpr_device *adev)
  250. {
  251. struct spf_core *core = dev_get_drvdata(&adev->dev);
  252. if (!spf_core_priv) {
  253. pr_err("%s: spf_core platform probe not yet done\n", __func__);
  254. return -1;
  255. }
  256. mutex_lock(&spf_core_priv->lock);
  257. spf_core_priv->spf_core_drv = NULL;
  258. kfree(core);
  259. mutex_unlock(&spf_core_priv->lock);
  260. return 0;
  261. }
  262. static const struct of_device_id spf_core_device_id[] = {
  263. { .compatible = "qcom,spf_core" },
  264. {},
  265. };
  266. MODULE_DEVICE_TABLE(of, spf_core_device_id);
  267. static struct gpr_driver qcom_spf_core_driver = {
  268. .probe = spf_core_probe,
  269. .remove = spf_core_exit,
  270. .callback = spf_core_callback,
  271. .driver = {
  272. .name = "qcom-spf_core",
  273. .of_match_table = of_match_ptr(spf_core_device_id),
  274. },
  275. };
  276. static void spf_core_add_child_devices(struct work_struct *work)
  277. {
  278. int ret;
  279. pr_err("%s:enumarate machine driver\n", __func__);
  280. if(spf_core_is_apm_ready()) {
  281. dev_err(spf_core_priv->dev, "%s: apm is up\n",
  282. __func__);
  283. } else {
  284. dev_err(spf_core_priv->dev, "%s: apm is not up\n",
  285. __func__);
  286. return;
  287. }
  288. ret = of_platform_populate(spf_core_priv->dev->of_node,
  289. NULL, NULL, spf_core_priv->dev);
  290. if (ret)
  291. dev_err(spf_core_priv->dev, "%s: failed to add child nodes, ret=%d\n",
  292. __func__, ret);
  293. spf_core_priv->is_initial_boot = false;
  294. }
  295. static int spf_core_platform_driver_probe(struct platform_device *pdev)
  296. {
  297. int ret = 0;
  298. pr_err("%s",__func__);
  299. spf_core_priv = devm_kzalloc(&pdev->dev, sizeof(struct spf_core_private), GFP_KERNEL);
  300. if (!spf_core_priv)
  301. return -ENOMEM;
  302. spf_core_priv->dev = &pdev->dev;
  303. mutex_init(&spf_core_priv->lock);
  304. INIT_WORK(&spf_core_priv->add_chld_dev_work, spf_core_add_child_devices);
  305. spf_core_priv->is_initial_boot = true;
  306. ret = gpr_driver_register(&qcom_spf_core_driver);
  307. if (ret) {
  308. pr_err("%s: gpr driver register failed = %d\n",
  309. __func__, ret);
  310. ret = 0;
  311. }
  312. #if 0
  313. ret = snd_event_client_register(&pdev->dev, &gpr_ssr_ops, NULL);
  314. if (ret) {
  315. pr_err("%s: Registration with SND event fwk failed ret = %d\n",
  316. __func__, ret);
  317. ret = 0;
  318. }
  319. #endif
  320. digital_cdc_rsc_mgr_init();
  321. return ret;
  322. }
  323. static int spf_core_platform_driver_remove(struct platform_device *pdev)
  324. {
  325. //snd_event_client_deregister(&pdev->dev);
  326. gpr_driver_unregister(&qcom_spf_core_driver);
  327. spf_core_priv = NULL;
  328. digital_cdc_rsc_mgr_exit();
  329. return 0;
  330. }
  331. static const struct of_device_id spf_core_of_match[] = {
  332. { .compatible = "qcom,spf-core-platform", },
  333. {},
  334. };
  335. static struct platform_driver spf_core_driver = {
  336. .probe = spf_core_platform_driver_probe,
  337. .remove = spf_core_platform_driver_remove,
  338. .driver = {
  339. .name = "spf-core-platform",
  340. .owner = THIS_MODULE,
  341. .of_match_table = spf_core_of_match,
  342. }
  343. };
  344. module_platform_driver(spf_core_driver);
  345. MODULE_DESCRIPTION("q6 core");
  346. MODULE_LICENSE("GPL v2");