audio_prm.c 12 KB

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