spf-core.c 9.8 KB

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