gecko-core.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /* Copyright (c) 2019-2020, 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/gecko-core.h>
  23. #define APM_STATE_READY_TIMEOUT_MS 10000
  24. #define Q6_READY_TIMEOUT_MS 1000
  25. #define APM_CMD_GET_GECKO_STATE 0x01001021
  26. #define APM_CMD_CLOSE_ALL 0x01001013
  27. #define APM_CMD_RSP_GET_GECKO_STATE 0x02001007
  28. #define APM_MODULE_INSTANCE_ID 0x00000001
  29. #define GPR_SVC_ADSP_CORE 0x3
  30. struct gecko_core {
  31. struct gpr_device *adev;
  32. wait_queue_head_t wait;
  33. struct mutex lock;
  34. bool resp_received;
  35. int32_t status;
  36. };
  37. struct gecko_core_private {
  38. struct device *dev;
  39. struct mutex lock;
  40. struct gecko_core *gecko_core_drv;
  41. bool is_initial_boot;
  42. struct work_struct add_chld_dev_work;
  43. };
  44. static struct gecko_core_private *gecko_core_priv;
  45. /* used to decode basic responses from Gecko */
  46. struct gecko_cmd_basic_rsp {
  47. uint32_t opcode;
  48. int32_t status;
  49. };
  50. struct apm_cmd_rsp_get_gecko_status_t
  51. {
  52. /* Gecko status
  53. * @values
  54. * 0 -> Not ready
  55. * 1 -> Ready
  56. */
  57. uint32_t status;
  58. };
  59. static int gecko_core_callback(struct gpr_device *adev, void *data)
  60. {
  61. struct gecko_core *core = dev_get_drvdata(&adev->dev);
  62. struct apm_cmd_rsp_get_gecko_status_t *gecko_status_rsp;
  63. struct gecko_cmd_basic_rsp *basic_rsp;
  64. struct gpr_hdr *hdr = data;
  65. dev_info(&adev->dev ,"%s: Payload %x",__func__, hdr->opcode);
  66. switch (hdr->opcode) {
  67. case GPR_IBASIC_RSP_RESULT:
  68. basic_rsp = GPR_PKT_GET_PAYLOAD(
  69. struct gecko_cmd_basic_rsp,
  70. data);
  71. dev_info(&adev->dev ,"%s: op %x status %d", __func__,
  72. basic_rsp->opcode, basic_rsp->status);
  73. if (basic_rsp->opcode == APM_CMD_CLOSE_ALL) {
  74. core->status = basic_rsp->status;
  75. } else {
  76. dev_err(&adev->dev ,"%s: Failed response received",
  77. __func__);
  78. }
  79. core->resp_received = true;
  80. break;
  81. case APM_CMD_RSP_GET_GECKO_STATE:
  82. gecko_status_rsp =
  83. GPR_PKT_GET_PAYLOAD(
  84. struct apm_cmd_rsp_get_gecko_status_t,
  85. data);
  86. dev_info(&adev->dev ,"%s: sucess response received",__func__);
  87. core->status = gecko_status_rsp->status;
  88. core->resp_received = true;
  89. break;
  90. default:
  91. dev_err(&adev->dev, "Message ID from apm: 0x%x\n",
  92. hdr->opcode);
  93. break;
  94. }
  95. if (core->resp_received)
  96. wake_up(&core->wait);
  97. return 0;
  98. }
  99. static bool __gecko_core_is_apm_ready(struct gecko_core *core)
  100. {
  101. struct gpr_device *adev = core->adev;
  102. struct gpr_pkt pkt;
  103. int rc;
  104. bool ret = false;
  105. pkt.hdr.header = GPR_SET_FIELD(GPR_PKT_VERSION, GPR_PKT_VER) |
  106. GPR_SET_FIELD(GPR_PKT_HEADER_SIZE, GPR_PKT_HEADER_WORD_SIZE_V) |
  107. GPR_SET_FIELD(GPR_PKT_PACKET_SIZE, GPR_PKT_HEADER_BYTE_SIZE_V);
  108. pkt.hdr.dst_port = APM_MODULE_INSTANCE_ID;
  109. pkt.hdr.src_port = GPR_SVC_ADSP_CORE;
  110. pkt.hdr.dst_domain_id = GPR_IDS_DOMAIN_ID_ADSP_V;
  111. pkt.hdr.src_domain_id = GPR_IDS_DOMAIN_ID_APPS_V;
  112. pkt.hdr.opcode = APM_CMD_GET_GECKO_STATE;
  113. dev_err(gecko_core_priv->dev, "%s: send_command ret\n", __func__);
  114. rc = gpr_send_pkt(adev, &pkt);
  115. if (rc < 0) {
  116. ret = false;
  117. goto done;
  118. }
  119. rc = wait_event_timeout(core->wait, (core->resp_received),
  120. msecs_to_jiffies(Q6_READY_TIMEOUT_MS));
  121. dev_dbg(gecko_core_priv->dev, "%s: wait event unblocked \n", __func__);
  122. if (rc > 0 && core->resp_received) {
  123. ret = core->status;
  124. } else {
  125. dev_err(gecko_core_priv->dev, "%s: command timedout, ret\n",
  126. __func__);
  127. }
  128. done:
  129. core->resp_received = false;
  130. return ret;
  131. }
  132. /**
  133. * gecko_core_is_apm_ready() - Get status of adsp
  134. *
  135. * Return: Will return true if apm is ready and false if not.
  136. */
  137. bool gecko_core_is_apm_ready(void)
  138. {
  139. unsigned long timeout;
  140. bool ret = false;
  141. struct gecko_core *core;
  142. if (!gecko_core_priv)
  143. return ret;
  144. mutex_lock(&gecko_core_priv->lock);
  145. core = gecko_core_priv->gecko_core_drv;
  146. if (!core)
  147. goto done;
  148. timeout = jiffies + msecs_to_jiffies(APM_STATE_READY_TIMEOUT_MS);
  149. mutex_lock(&core->lock);
  150. for (;;) {
  151. if (__gecko_core_is_apm_ready(core)) {
  152. ret = true;
  153. break;
  154. }
  155. usleep_range(300000, 300050);
  156. if (!time_after(timeout, jiffies)) {
  157. ret = false;
  158. break;
  159. }
  160. }
  161. mutex_unlock(&core->lock);
  162. done:
  163. mutex_unlock(&gecko_core_priv->lock);
  164. return ret;
  165. }
  166. EXPORT_SYMBOL_GPL(gecko_core_is_apm_ready);
  167. /**
  168. * gecko_core_apm_close_all() - Get status of adsp
  169. *
  170. * Return: Will be return true if apm is ready and false if not.
  171. */
  172. void gecko_core_apm_close_all(void)
  173. {
  174. int rc = 0;
  175. struct gecko_core *core;
  176. struct gpr_pkt pkt;
  177. struct gpr_device *adev = NULL;
  178. if (!gecko_core_priv)
  179. return;
  180. mutex_lock(&gecko_core_priv->lock);
  181. core = gecko_core_priv->gecko_core_drv;
  182. if (!core) {
  183. mutex_unlock(&gecko_core_priv->lock);
  184. return;
  185. }
  186. mutex_lock(&core->lock);
  187. adev = core->adev;
  188. pkt.hdr.header = GPR_SET_FIELD(GPR_PKT_VERSION, GPR_PKT_VER) |
  189. GPR_SET_FIELD(GPR_PKT_HEADER_SIZE,
  190. GPR_PKT_HEADER_WORD_SIZE_V) |
  191. GPR_SET_FIELD(GPR_PKT_PACKET_SIZE,
  192. GPR_PKT_HEADER_BYTE_SIZE_V);
  193. pkt.hdr.dst_port = APM_MODULE_INSTANCE_ID;
  194. pkt.hdr.src_port = GPR_SVC_ADSP_CORE;
  195. pkt.hdr.dst_domain_id = GPR_IDS_DOMAIN_ID_ADSP_V;
  196. pkt.hdr.src_domain_id = GPR_IDS_DOMAIN_ID_APPS_V;
  197. pkt.hdr.opcode = APM_CMD_CLOSE_ALL;
  198. dev_info(gecko_core_priv->dev, "%s: send_command \n", __func__);
  199. rc = gpr_send_pkt(adev, &pkt);
  200. if (rc < 0) {
  201. dev_err(gecko_core_priv->dev, "%s: send_pkt_failed %d\n",
  202. __func__, rc);
  203. goto done;
  204. }
  205. rc = wait_event_timeout(core->wait, (core->resp_received),
  206. msecs_to_jiffies(Q6_READY_TIMEOUT_MS));
  207. dev_info(gecko_core_priv->dev, "%s: wait event unblocked \n", __func__);
  208. if (rc > 0 && core->resp_received) {
  209. if (core->status != 0)
  210. dev_err(gecko_core_priv->dev, "%s, cmd failed status %d",
  211. __func__, core->status);
  212. } else {
  213. dev_err(gecko_core_priv->dev, "%s: command timedout, ret\n",
  214. __func__);
  215. }
  216. done:
  217. core->resp_received = false;
  218. mutex_unlock(&core->lock);
  219. mutex_unlock(&gecko_core_priv->lock);
  220. return;
  221. }
  222. EXPORT_SYMBOL_GPL(gecko_core_apm_close_all);
  223. static int gecko_core_probe(struct gpr_device *adev)
  224. {
  225. struct gecko_core *core;
  226. pr_err("%s",__func__);
  227. if (!gecko_core_priv) {
  228. pr_err("%s: gecko_core platform probe not yet done\n", __func__);
  229. return -EPROBE_DEFER;
  230. }
  231. mutex_lock(&gecko_core_priv->lock);
  232. core = kzalloc(sizeof(*core), GFP_KERNEL);
  233. if (!core) {
  234. mutex_unlock(&gecko_core_priv->lock);
  235. return -ENOMEM;
  236. }
  237. dev_set_drvdata(&adev->dev, core);
  238. mutex_init(&core->lock);
  239. core->adev = adev;
  240. init_waitqueue_head(&core->wait);
  241. gecko_core_priv->gecko_core_drv = core;
  242. if (gecko_core_priv->is_initial_boot)
  243. schedule_work(&gecko_core_priv->add_chld_dev_work);
  244. mutex_unlock(&gecko_core_priv->lock);
  245. return 0;
  246. }
  247. static int gecko_core_exit(struct gpr_device *adev)
  248. {
  249. struct gecko_core *core = dev_get_drvdata(&adev->dev);
  250. if (!gecko_core_priv) {
  251. pr_err("%s: gecko_core platform probe not yet done\n", __func__);
  252. return -1;
  253. }
  254. mutex_lock(&gecko_core_priv->lock);
  255. gecko_core_priv->gecko_core_drv = NULL;
  256. kfree(core);
  257. mutex_unlock(&gecko_core_priv->lock);
  258. return 0;
  259. }
  260. static const struct of_device_id gecko_core_device_id[] = {
  261. { .compatible = "qcom,gecko_core" },
  262. {},
  263. };
  264. MODULE_DEVICE_TABLE(of, gecko_core_device_id);
  265. static struct gpr_driver qcom_gecko_core_driver = {
  266. .probe = gecko_core_probe,
  267. .remove = gecko_core_exit,
  268. .callback = gecko_core_callback,
  269. .driver = {
  270. .name = "qcom-gecko_core",
  271. .of_match_table = of_match_ptr(gecko_core_device_id),
  272. },
  273. };
  274. static void gecko_core_add_child_devices(struct work_struct *work)
  275. {
  276. int ret;
  277. pr_err("%s:enumarate machine driver\n", __func__);
  278. if(gecko_core_is_apm_ready()) {
  279. dev_err(gecko_core_priv->dev, "%s: apm is up\n",
  280. __func__);
  281. } else {
  282. dev_err(gecko_core_priv->dev, "%s: apm is not up\n",
  283. __func__);
  284. return;
  285. }
  286. ret = of_platform_populate(gecko_core_priv->dev->of_node,
  287. NULL, NULL, gecko_core_priv->dev);
  288. if (ret)
  289. dev_err(gecko_core_priv->dev, "%s: failed to add child nodes, ret=%d\n",
  290. __func__, ret);
  291. gecko_core_priv->is_initial_boot = false;
  292. }
  293. static int gecko_core_platform_driver_probe(struct platform_device *pdev)
  294. {
  295. int ret = 0;
  296. pr_err("%s",__func__);
  297. gecko_core_priv = devm_kzalloc(&pdev->dev, sizeof(struct gecko_core_private), GFP_KERNEL);
  298. if (!gecko_core_priv)
  299. return -ENOMEM;
  300. gecko_core_priv->dev = &pdev->dev;
  301. mutex_init(&gecko_core_priv->lock);
  302. INIT_WORK(&gecko_core_priv->add_chld_dev_work, gecko_core_add_child_devices);
  303. ret = gpr_driver_register(&qcom_gecko_core_driver);
  304. if (ret) {
  305. pr_err("%s: gpr driver register failed = %d\n",
  306. __func__, ret);
  307. ret = 0;
  308. }
  309. gecko_core_priv->is_initial_boot = true;
  310. #if 0
  311. ret = snd_event_client_register(&pdev->dev, &gpr_ssr_ops, NULL);
  312. if (ret) {
  313. pr_err("%s: Registration with SND event fwk failed ret = %d\n",
  314. __func__, ret);
  315. ret = 0;
  316. }
  317. #endif
  318. return ret;
  319. }
  320. static int gecko_core_platform_driver_remove(struct platform_device *pdev)
  321. {
  322. //snd_event_client_deregister(&pdev->dev);
  323. gpr_driver_unregister(&qcom_gecko_core_driver);
  324. gecko_core_priv = NULL;
  325. return 0;
  326. }
  327. static const struct of_device_id gecko_core_of_match[] = {
  328. { .compatible = "qcom,gecko-core-platform", },
  329. {},
  330. };
  331. static struct platform_driver gecko_core_driver = {
  332. .probe = gecko_core_platform_driver_probe,
  333. .remove = gecko_core_platform_driver_remove,
  334. .driver = {
  335. .name = "gecko-core-platform",
  336. .owner = THIS_MODULE,
  337. .of_match_table = gecko_core_of_match,
  338. }
  339. };
  340. module_platform_driver(gecko_core_driver);
  341. MODULE_DESCRIPTION("q6 core");
  342. MODULE_LICENSE("GPL v2");