audio_prm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2019-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/slab.h>
  6. #include <linux/wait.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/sched.h>
  10. #include <linux/delay.h>
  11. #include <linux/jiffies.h>
  12. #include <linux/of.h>
  13. #include <linux/of_platform.h>
  14. #include <ipc/gpr-lite.h>
  15. #include <soc/snd_event.h>
  16. #include <dsp/audio_prm.h>
  17. #include <dsp/spf-core.h>
  18. #include <dsp/audio_notifier.h>
  19. #define TIMEOUT_MS 500
  20. #define MAX_RETRY_COUNT 3
  21. #define APM_READY_WAIT_DURATION 2
  22. struct audio_prm {
  23. struct gpr_device *adev;
  24. wait_queue_head_t wait;
  25. struct mutex lock;
  26. bool resp_received;
  27. atomic_t state;
  28. atomic_t status;
  29. bool is_adsp_up;
  30. };
  31. static struct audio_prm g_prm;
  32. static bool is_apm_ready_check_done = false;
  33. static int audio_prm_callback(struct gpr_device *adev, void *data)
  34. {
  35. struct gpr_hdr *hdr = (struct gpr_hdr *)data;
  36. uint32_t *payload = GPR_PKT_GET_PAYLOAD(uint32_t, data);
  37. //dev_err(&adev->dev, "%s: Payload %x", __func__, hdr->opcode);
  38. switch (hdr->opcode) {
  39. case GPR_IBASIC_RSP_RESULT:
  40. pr_err("%s: Failed response received",__func__);
  41. atomic_set(&g_prm.status, payload[1]);
  42. g_prm.resp_received = true;
  43. break;
  44. case PRM_CMD_RSP_REQUEST_HW_RSC:
  45. case PRM_CMD_RSP_RELEASE_HW_RSC:
  46. /* payload[1] contains the error status for response */
  47. if (payload[1] != 0) {
  48. atomic_set(&g_prm.status, payload[1]);
  49. pr_err("%s: cmd = 0x%x returned error = 0x%x\n",
  50. __func__, payload[0], payload[1]);
  51. }
  52. g_prm.resp_received = true;
  53. /* payload[0] contains the param_ID for response */
  54. switch (payload[0]) {
  55. case PARAM_ID_RSC_AUDIO_HW_CLK:
  56. case PARAM_ID_RSC_LPASS_CORE:
  57. case PARAM_ID_RSC_HW_CORE:
  58. if (payload[1] != 0)
  59. pr_err("%s: PRM command failed with error %d\n",
  60. __func__, payload[1]);
  61. atomic_set(&g_prm.state, payload[1]);
  62. break;
  63. default:
  64. pr_err("%s: hit default case",__func__);
  65. break;
  66. };
  67. default:
  68. break;
  69. };
  70. if (g_prm.resp_received)
  71. wake_up(&g_prm.wait);
  72. return 0;
  73. }
  74. static int prm_gpr_send_pkt(struct gpr_pkt *pkt, wait_queue_head_t *wait)
  75. {
  76. int ret = 0;
  77. int retry;
  78. if (wait)
  79. atomic_set(&g_prm.state, 1);
  80. atomic_set(&g_prm.status, 0);
  81. mutex_lock(&g_prm.lock);
  82. pr_debug("%s: enter",__func__);
  83. if (g_prm.adev == NULL) {
  84. pr_err("%s: apr is unregistered\n", __func__);
  85. mutex_unlock(&g_prm.lock);
  86. return -ENODEV;
  87. }
  88. if (!is_apm_ready_check_done && g_prm.is_adsp_up) {
  89. pr_info("%s: apm ready check not done\n", __func__);
  90. retry = 0;
  91. while (!spf_core_is_apm_ready() || retry < MAX_RETRY_COUNT) {
  92. msleep(APM_READY_WAIT_DURATION);
  93. retry++;
  94. }
  95. is_apm_ready_check_done = true;
  96. pr_info("%s: apm ready check done\n", __func__);
  97. }
  98. g_prm.resp_received = false;
  99. ret = gpr_send_pkt(g_prm.adev, pkt);
  100. if (ret < 0) {
  101. pr_err("%s: packet not transmitted %d\n", __func__, ret);
  102. mutex_unlock(&g_prm.lock);
  103. return ret;
  104. }
  105. if (wait) {
  106. ret = wait_event_timeout(g_prm.wait,
  107. (g_prm.resp_received),
  108. msecs_to_jiffies(2 * TIMEOUT_MS));
  109. if (!ret) {
  110. pr_err("%s: pkt send timeout\n", __func__);
  111. ret = -ETIMEDOUT;
  112. } else if (atomic_read(&g_prm.status) > 0) {
  113. pr_err("%s: DSP returned error %d\n", __func__,
  114. atomic_read(&g_prm.status));
  115. ret = -EINVAL;
  116. } else {
  117. ret = 0;
  118. }
  119. }
  120. pr_debug("%s: exit",__func__);
  121. mutex_unlock(&g_prm.lock);
  122. return ret;
  123. }
  124. /**
  125. */
  126. int audio_prm_set_lpass_hw_core_req(struct clk_cfg *cfg, uint32_t hw_core_id, uint8_t enable)
  127. {
  128. struct gpr_pkt *pkt;
  129. prm_cmd_request_hw_core_t prm_rsc_request;
  130. int ret = 0;
  131. uint32_t size;
  132. size = GPR_HDR_SIZE + sizeof(prm_cmd_request_hw_core_t);
  133. pkt = kzalloc(size, GFP_KERNEL);
  134. if (!pkt)
  135. return -ENOMEM;
  136. pkt->hdr.header = GPR_SET_FIELD(GPR_PKT_VERSION, GPR_PKT_VER) |
  137. GPR_SET_FIELD(GPR_PKT_HEADER_SIZE, GPR_PKT_HEADER_WORD_SIZE_V) |
  138. GPR_SET_FIELD(GPR_PKT_PACKET_SIZE, size);
  139. pkt->hdr.src_port = GPR_SVC_ASM;
  140. pkt->hdr.dst_port = PRM_MODULE_INSTANCE_ID;
  141. pkt->hdr.dst_domain_id = GPR_IDS_DOMAIN_ID_ADSP_V;
  142. pkt->hdr.src_domain_id = GPR_IDS_DOMAIN_ID_APPS_V;
  143. pkt->hdr.token = 0; /* TBD */
  144. if (enable)
  145. pkt->hdr.opcode = PRM_CMD_REQUEST_HW_RSC;
  146. else
  147. pkt->hdr.opcode = PRM_CMD_RELEASE_HW_RSC;
  148. //pr_err("%s: clk_id %d size of cmd_req %ld \n",__func__, cfg->clk_id, sizeof(prm_cmd_request_hw_core_t));
  149. prm_rsc_request.payload_header.payload_address_lsw = 0;
  150. prm_rsc_request.payload_header.payload_address_msw = 0;
  151. prm_rsc_request.payload_header.mem_map_handle = 0;
  152. prm_rsc_request.payload_header.payload_size = sizeof(prm_cmd_request_hw_core_t) - sizeof(apm_cmd_header_t);
  153. /** Populate the param payload */
  154. prm_rsc_request.module_payload_0.module_instance_id = PRM_MODULE_INSTANCE_ID;
  155. prm_rsc_request.module_payload_0.error_code = 0;
  156. prm_rsc_request.module_payload_0.param_id = PARAM_ID_RSC_HW_CORE;
  157. prm_rsc_request.module_payload_0.param_size =
  158. sizeof(prm_cmd_request_hw_core_t) - sizeof(apm_cmd_header_t) - sizeof(apm_module_param_data_t);
  159. prm_rsc_request.hw_core_id = hw_core_id; // HW_CORE_ID_LPASS;
  160. memcpy(&pkt->payload, &prm_rsc_request, sizeof(prm_cmd_request_hw_core_t));
  161. ret = prm_gpr_send_pkt(pkt, &g_prm.wait);
  162. kfree(pkt);
  163. return ret;
  164. }
  165. EXPORT_SYMBOL(audio_prm_set_lpass_hw_core_req);
  166. /**
  167. * prm_set_lpass_clk_cfg() - Set PRM clock
  168. *
  169. * Return: 0 if clock set is success
  170. */
  171. static int audio_prm_set_lpass_clk_cfg_req(struct clk_cfg *cfg)
  172. {
  173. struct gpr_pkt *pkt;
  174. prm_cmd_request_rsc_t prm_rsc_request;
  175. int ret = 0;
  176. uint32_t size;
  177. size = GPR_HDR_SIZE + sizeof(prm_cmd_request_rsc_t);
  178. pkt = kzalloc(size, GFP_KERNEL);
  179. if (!pkt)
  180. return -ENOMEM;
  181. pkt->hdr.header = GPR_SET_FIELD(GPR_PKT_VERSION, GPR_PKT_VER) |
  182. GPR_SET_FIELD(GPR_PKT_HEADER_SIZE, GPR_PKT_HEADER_WORD_SIZE_V) |
  183. GPR_SET_FIELD(GPR_PKT_PACKET_SIZE, size);
  184. pkt->hdr.src_port = GPR_SVC_ASM;
  185. pkt->hdr.dst_port = PRM_MODULE_INSTANCE_ID;
  186. pkt->hdr.dst_domain_id = GPR_IDS_DOMAIN_ID_ADSP_V;
  187. pkt->hdr.src_domain_id = GPR_IDS_DOMAIN_ID_APPS_V;
  188. pkt->hdr.token = 0; /* TBD */
  189. pkt->hdr.opcode = PRM_CMD_REQUEST_HW_RSC;
  190. //pr_err("%s: clk_id %d size of cmd_req %ld \n",__func__, cfg->clk_id, sizeof(prm_cmd_request_rsc_t));
  191. prm_rsc_request.payload_header.payload_address_lsw = 0;
  192. prm_rsc_request.payload_header.payload_address_msw = 0;
  193. prm_rsc_request.payload_header.mem_map_handle = 0;
  194. prm_rsc_request.payload_header.payload_size = sizeof(prm_cmd_request_rsc_t) - sizeof(apm_cmd_header_t);
  195. /** Populate the param payload */
  196. prm_rsc_request.module_payload_0.module_instance_id = PRM_MODULE_INSTANCE_ID;
  197. prm_rsc_request.module_payload_0.error_code = 0;
  198. prm_rsc_request.module_payload_0.param_id = PARAM_ID_RSC_AUDIO_HW_CLK;
  199. prm_rsc_request.module_payload_0.param_size =
  200. sizeof(prm_cmd_request_rsc_t) - sizeof(apm_cmd_header_t) - sizeof(apm_module_param_data_t);
  201. prm_rsc_request.num_clk_id_t.num_clock_id = MAX_AUD_HW_CLK_NUM_REQ;
  202. prm_rsc_request.clock_ids_t[0].clock_id = cfg->clk_id;
  203. prm_rsc_request.clock_ids_t[0].clock_freq = cfg->clk_freq_in_hz;
  204. prm_rsc_request.clock_ids_t[0].clock_attri = cfg->clk_attri;
  205. prm_rsc_request.clock_ids_t[0].clock_root = cfg->clk_root;
  206. memcpy(&pkt->payload, &prm_rsc_request, sizeof(prm_cmd_request_rsc_t));
  207. ret = prm_gpr_send_pkt(pkt, &g_prm.wait);
  208. kfree(pkt);
  209. return ret;
  210. }
  211. static int audio_prm_set_lpass_clk_cfg_rel(struct clk_cfg *cfg)
  212. {
  213. struct gpr_pkt *pkt;
  214. prm_cmd_release_rsc_t prm_rsc_release;
  215. int ret = 0;
  216. uint32_t size;
  217. size = GPR_HDR_SIZE + sizeof(prm_cmd_release_rsc_t);
  218. pkt = kzalloc(size, GFP_KERNEL);
  219. if (!pkt)
  220. return -ENOMEM;
  221. pkt->hdr.header = GPR_SET_FIELD(GPR_PKT_VERSION, GPR_PKT_VER) |
  222. GPR_SET_FIELD(GPR_PKT_HEADER_SIZE, GPR_PKT_HEADER_WORD_SIZE_V) |
  223. GPR_SET_FIELD(GPR_PKT_PACKET_SIZE, size);
  224. pkt->hdr.src_port = GPR_SVC_ASM;
  225. pkt->hdr.dst_port = PRM_MODULE_INSTANCE_ID;
  226. pkt->hdr.dst_domain_id = GPR_IDS_DOMAIN_ID_ADSP_V;
  227. pkt->hdr.src_domain_id = GPR_IDS_DOMAIN_ID_APPS_V;
  228. pkt->hdr.token = 0; /* TBD */
  229. pkt->hdr.opcode = PRM_CMD_RELEASE_HW_RSC;
  230. //pr_err("%s: clk_id %d size of cmd_req %ld \n",__func__, cfg->clk_id, sizeof(prm_cmd_release_rsc_t));
  231. prm_rsc_release.payload_header.payload_address_lsw = 0;
  232. prm_rsc_release.payload_header.payload_address_msw = 0;
  233. prm_rsc_release.payload_header.mem_map_handle = 0;
  234. prm_rsc_release.payload_header.payload_size = sizeof(prm_cmd_release_rsc_t) - sizeof(apm_cmd_header_t);
  235. /** Populate the param payload */
  236. prm_rsc_release.module_payload_0.module_instance_id = PRM_MODULE_INSTANCE_ID;
  237. prm_rsc_release.module_payload_0.error_code = 0;
  238. prm_rsc_release.module_payload_0.param_id = PARAM_ID_RSC_AUDIO_HW_CLK;
  239. prm_rsc_release.module_payload_0.param_size =
  240. sizeof(prm_cmd_release_rsc_t) - sizeof(apm_cmd_header_t) - sizeof(apm_module_param_data_t);
  241. prm_rsc_release.num_clk_id_t.num_clock_id = MAX_AUD_HW_CLK_NUM_REQ;
  242. prm_rsc_release.clock_ids_t[0].clock_id = cfg->clk_id;
  243. memcpy(&pkt->payload, &prm_rsc_release, sizeof(prm_cmd_release_rsc_t));
  244. ret = prm_gpr_send_pkt(pkt, &g_prm.wait);
  245. kfree(pkt);
  246. return ret;
  247. }
  248. int audio_prm_set_lpass_clk_cfg (struct clk_cfg *clk, uint8_t enable)
  249. {
  250. int ret = 0;
  251. if (enable)
  252. ret = audio_prm_set_lpass_clk_cfg_req (clk);
  253. else
  254. ret = audio_prm_set_lpass_clk_cfg_rel (clk);
  255. return ret;
  256. }
  257. EXPORT_SYMBOL(audio_prm_set_lpass_clk_cfg);
  258. static int audio_prm_service_cb(struct notifier_block *this,
  259. unsigned long opcode, void *data)
  260. {
  261. pr_info("%s: Service opcode 0x%lx\n", __func__, opcode);
  262. switch (opcode) {
  263. case AUDIO_NOTIFIER_SERVICE_DOWN:
  264. mutex_lock(&g_prm.lock);
  265. pr_debug("%s: making apm_ready check false\n", __func__);
  266. is_apm_ready_check_done = false;
  267. g_prm.is_adsp_up = false;
  268. mutex_unlock(&g_prm.lock);
  269. break;
  270. case AUDIO_NOTIFIER_SERVICE_UP:
  271. mutex_lock(&g_prm.lock);
  272. g_prm.is_adsp_up = true;
  273. mutex_unlock(&g_prm.lock);
  274. break;
  275. default:
  276. break;
  277. }
  278. return NOTIFY_OK;
  279. }
  280. static struct notifier_block service_nb = {
  281. .notifier_call = audio_prm_service_cb,
  282. .priority = -INT_MAX,
  283. };
  284. static int audio_prm_probe(struct gpr_device *adev)
  285. {
  286. int ret = 0;
  287. ret = audio_notifier_register("audio_prm", AUDIO_NOTIFIER_ADSP_DOMAIN,
  288. &service_nb);
  289. if (ret < 0) {
  290. if (ret != -EPROBE_DEFER)
  291. pr_err("%s: Audio notifier register failed ret = %d\n",
  292. __func__, ret);
  293. return ret;
  294. }
  295. dev_set_drvdata(&adev->dev, &g_prm);
  296. g_prm.adev = adev;
  297. init_waitqueue_head(&g_prm.wait);
  298. g_prm.is_adsp_up = true;
  299. pr_err("%s: prm probe success\n", __func__);
  300. return ret;
  301. }
  302. static int audio_prm_remove(struct gpr_device *adev)
  303. {
  304. int ret = 0;
  305. audio_notifier_deregister("audio_prm");
  306. mutex_lock(&g_prm.lock);
  307. g_prm.is_adsp_up = false;
  308. g_prm.adev = NULL;
  309. mutex_unlock(&g_prm.lock);
  310. return ret;
  311. }
  312. static const struct of_device_id audio_prm_device_id[] = {
  313. { .compatible = "qcom,audio_prm" },
  314. {},
  315. };
  316. MODULE_DEVICE_TABLE(of, audio_prm_device_id);
  317. static struct gpr_driver qcom_audio_prm_driver = {
  318. .probe = audio_prm_probe,
  319. .remove = audio_prm_remove,
  320. .callback = audio_prm_callback,
  321. .driver = {
  322. .name = "qcom-audio_prm",
  323. .of_match_table = of_match_ptr(audio_prm_device_id),
  324. },
  325. };
  326. static int __init audio_prm_module_init(void)
  327. {
  328. int ret;
  329. ret = gpr_driver_register(&qcom_audio_prm_driver);
  330. if (ret)
  331. pr_err("%s: gpr driver register failed = %d\n", __func__, ret);
  332. mutex_init(&g_prm.lock);
  333. return ret;
  334. }
  335. static void __exit audio_prm_module_exit(void)
  336. {
  337. mutex_destroy(&g_prm.lock);
  338. gpr_driver_unregister(&qcom_audio_prm_driver);
  339. }
  340. module_init(audio_prm_module_init);
  341. module_exit(audio_prm_module_exit);
  342. MODULE_DESCRIPTION("audio prm");
  343. MODULE_LICENSE("GPL v2");