audio_prm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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/sched.h>
  17. #include <linux/jiffies.h>
  18. #include <linux/of.h>
  19. #include <linux/of_platform.h>
  20. #include <ipc/gpr-lite.h>
  21. #include <soc/snd_event.h>
  22. #include <dsp/audio_prm.h>
  23. #define TIMEOUT_MS 500
  24. struct audio_prm {
  25. struct gpr_device *adev;
  26. wait_queue_head_t wait;
  27. struct mutex lock;
  28. bool resp_received;
  29. atomic_t state;
  30. atomic_t status;
  31. bool is_adsp_up;
  32. };
  33. static struct audio_prm g_prm;
  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. wake_up(&g_prm.wait);
  64. break;
  65. default:
  66. pr_err("%s: hit default case",__func__);
  67. break;
  68. };
  69. default:
  70. break;
  71. };
  72. if (g_prm.resp_received)
  73. wake_up(&g_prm.wait);
  74. return 0;
  75. }
  76. static int prm_gpr_send_pkt(struct gpr_pkt *pkt, wait_queue_head_t *wait)
  77. {
  78. int ret = 0;
  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.is_adsp_up) {
  85. pr_err("%s: ADSP is not up\n", __func__);
  86. mutex_unlock(&g_prm.lock);
  87. return -ENODEV;
  88. }
  89. if (g_prm.adev == NULL) {
  90. pr_err("%s: apr is unregistered\n", __func__);
  91. mutex_unlock(&g_prm.lock);
  92. return -ENODEV;
  93. }
  94. ret = gpr_send_pkt(g_prm.adev, pkt);
  95. if (ret < 0) {
  96. pr_err("%s: packet not transmitted %d\n", __func__, ret);
  97. mutex_unlock(&g_prm.lock);
  98. return ret;
  99. }
  100. if (wait) {
  101. g_prm.resp_received = false;
  102. ret = wait_event_timeout(g_prm.wait,
  103. (g_prm.resp_received),
  104. msecs_to_jiffies(2 * TIMEOUT_MS));
  105. if (!ret) {
  106. pr_err("%s: pkt send timeout\n", __func__);
  107. ret = -ETIMEDOUT;
  108. } else if (atomic_read(&g_prm.status) > 0) {
  109. pr_err("%s: DSP returned error %d\n", __func__,
  110. atomic_read(&g_prm.status));
  111. ret = -EINVAL;
  112. } else {
  113. pr_err("%s: DSP returned %d\n", __func__,
  114. atomic_read(&g_prm.state));
  115. ret = 0;
  116. }
  117. }
  118. pr_debug("%s: exit",__func__);
  119. mutex_unlock(&g_prm.lock);
  120. return ret;
  121. }
  122. /**
  123. */
  124. int audio_prm_set_lpass_hw_core_req(struct clk_cfg *cfg, uint32_t hw_core_id, uint8_t enable)
  125. {
  126. struct gpr_pkt *pkt;
  127. prm_cmd_request_hw_core_t prm_rsc_request;
  128. int ret = 0;
  129. uint32_t size;
  130. size = GPR_HDR_SIZE + sizeof(prm_cmd_request_hw_core_t);
  131. pkt = kzalloc(size, GFP_KERNEL);
  132. if (!pkt)
  133. return -ENOMEM;
  134. pkt->hdr.header = GPR_SET_FIELD(GPR_PKT_VERSION, GPR_PKT_VER) |
  135. GPR_SET_FIELD(GPR_PKT_HEADER_SIZE, GPR_PKT_HEADER_WORD_SIZE_V) |
  136. GPR_SET_FIELD(GPR_PKT_PACKET_SIZE, size);
  137. pkt->hdr.src_port = GPR_SVC_ASM;
  138. pkt->hdr.dst_port = PRM_MODULE_INSTANCE_ID;
  139. pkt->hdr.dst_domain_id = GPR_IDS_DOMAIN_ID_ADSP_V;
  140. pkt->hdr.src_domain_id = GPR_IDS_DOMAIN_ID_APPS_V;
  141. pkt->hdr.token = 0; /* TBD */
  142. if (enable)
  143. pkt->hdr.opcode = PRM_CMD_REQUEST_HW_RSC;
  144. else
  145. pkt->hdr.opcode = PRM_CMD_RELEASE_HW_RSC;
  146. pr_err("%s: clk_id %d size of cmd_req %ld \n",__func__, cfg->clk_id, sizeof(prm_cmd_request_hw_core_t));
  147. prm_rsc_request.payload_header.payload_address_lsw = 0;
  148. prm_rsc_request.payload_header.payload_address_msw = 0;
  149. prm_rsc_request.payload_header.mem_map_handle = 0;
  150. prm_rsc_request.payload_header.payload_size = sizeof(prm_cmd_request_hw_core_t) - sizeof(apm_cmd_header_t);
  151. /** Populate the param payload */
  152. prm_rsc_request.module_payload_0.module_instance_id = PRM_MODULE_INSTANCE_ID;
  153. prm_rsc_request.module_payload_0.error_code = 0;
  154. prm_rsc_request.module_payload_0.param_id = PARAM_ID_RSC_HW_CORE;
  155. prm_rsc_request.module_payload_0.param_size =
  156. sizeof(prm_cmd_request_hw_core_t) - sizeof(apm_cmd_header_t) - sizeof(apm_module_param_data_t);
  157. prm_rsc_request.hw_core_id = hw_core_id; // HW_CORE_ID_LPASS;
  158. memcpy(&pkt->payload, &prm_rsc_request, sizeof(prm_cmd_request_hw_core_t));
  159. ret = prm_gpr_send_pkt(pkt, &g_prm.wait);
  160. kfree(pkt);
  161. return ret;
  162. }
  163. EXPORT_SYMBOL(audio_prm_set_lpass_hw_core_req);
  164. /**
  165. * prm_set_lpass_clk_cfg() - Set PRM clock
  166. *
  167. * Return: 0 if clock set is success
  168. */
  169. static int audio_prm_set_lpass_clk_cfg_req(struct clk_cfg *cfg)
  170. {
  171. struct gpr_pkt *pkt;
  172. prm_cmd_request_rsc_t prm_rsc_request;
  173. int ret = 0;
  174. uint32_t size;
  175. size = GPR_HDR_SIZE + sizeof(prm_cmd_request_rsc_t);
  176. pkt = kzalloc(size, GFP_KERNEL);
  177. if (!pkt)
  178. return -ENOMEM;
  179. pkt->hdr.header = GPR_SET_FIELD(GPR_PKT_VERSION, GPR_PKT_VER) |
  180. GPR_SET_FIELD(GPR_PKT_HEADER_SIZE, GPR_PKT_HEADER_WORD_SIZE_V) |
  181. GPR_SET_FIELD(GPR_PKT_PACKET_SIZE, size);
  182. pkt->hdr.src_port = GPR_SVC_ASM;
  183. pkt->hdr.dst_port = PRM_MODULE_INSTANCE_ID;
  184. pkt->hdr.dst_domain_id = GPR_IDS_DOMAIN_ID_ADSP_V;
  185. pkt->hdr.src_domain_id = GPR_IDS_DOMAIN_ID_APPS_V;
  186. pkt->hdr.token = 0; /* TBD */
  187. pkt->hdr.opcode = PRM_CMD_REQUEST_HW_RSC;
  188. pr_err("%s: clk_id %d size of cmd_req %ld \n",__func__, cfg->clk_id, sizeof(prm_cmd_request_rsc_t));
  189. prm_rsc_request.payload_header.payload_address_lsw = 0;
  190. prm_rsc_request.payload_header.payload_address_msw = 0;
  191. prm_rsc_request.payload_header.mem_map_handle = 0;
  192. prm_rsc_request.payload_header.payload_size = sizeof(prm_cmd_request_rsc_t) - sizeof(apm_cmd_header_t);
  193. /** Populate the param payload */
  194. prm_rsc_request.module_payload_0.module_instance_id = PRM_MODULE_INSTANCE_ID;
  195. prm_rsc_request.module_payload_0.error_code = 0;
  196. prm_rsc_request.module_payload_0.param_id = PARAM_ID_RSC_AUDIO_HW_CLK;
  197. prm_rsc_request.module_payload_0.param_size =
  198. sizeof(prm_cmd_request_rsc_t) - sizeof(apm_cmd_header_t) - sizeof(apm_module_param_data_t);
  199. prm_rsc_request.num_clk_id_t.num_clock_id = MAX_AUD_HW_CLK_NUM_REQ;
  200. prm_rsc_request.clock_ids_t[0].clock_id = cfg->clk_id;
  201. prm_rsc_request.clock_ids_t[0].clock_freq = cfg->clk_freq_in_hz;
  202. prm_rsc_request.clock_ids_t[0].clock_attri = cfg->clk_attri;
  203. prm_rsc_request.clock_ids_t[0].clock_root = cfg->clk_root;
  204. memcpy(&pkt->payload, &prm_rsc_request, sizeof(prm_cmd_request_rsc_t));
  205. ret = prm_gpr_send_pkt(pkt, &g_prm.wait);
  206. kfree(pkt);
  207. return ret;
  208. }
  209. static int audio_prm_set_lpass_clk_cfg_rel(struct clk_cfg *cfg)
  210. {
  211. struct gpr_pkt *pkt;
  212. prm_cmd_release_rsc_t prm_rsc_release;
  213. int ret = 0;
  214. uint32_t size;
  215. size = GPR_HDR_SIZE + sizeof(prm_cmd_release_rsc_t);
  216. pkt = kzalloc(size, GFP_KERNEL);
  217. if (!pkt)
  218. return -ENOMEM;
  219. pkt->hdr.header = GPR_SET_FIELD(GPR_PKT_VERSION, GPR_PKT_VER) |
  220. GPR_SET_FIELD(GPR_PKT_HEADER_SIZE, GPR_PKT_HEADER_WORD_SIZE_V) |
  221. GPR_SET_FIELD(GPR_PKT_PACKET_SIZE, size);
  222. pkt->hdr.src_port = GPR_SVC_ASM;
  223. pkt->hdr.dst_port = PRM_MODULE_INSTANCE_ID;
  224. pkt->hdr.dst_domain_id = GPR_IDS_DOMAIN_ID_ADSP_V;
  225. pkt->hdr.src_domain_id = GPR_IDS_DOMAIN_ID_APPS_V;
  226. pkt->hdr.token = 0; /* TBD */
  227. pkt->hdr.opcode = PRM_CMD_RELEASE_HW_RSC;
  228. pr_err("%s: clk_id %d size of cmd_req %ld \n",__func__, cfg->clk_id, sizeof(prm_cmd_release_rsc_t));
  229. prm_rsc_release.payload_header.payload_address_lsw = 0;
  230. prm_rsc_release.payload_header.payload_address_msw = 0;
  231. prm_rsc_release.payload_header.mem_map_handle = 0;
  232. prm_rsc_release.payload_header.payload_size = sizeof(prm_cmd_release_rsc_t) - sizeof(apm_cmd_header_t);
  233. /** Populate the param payload */
  234. prm_rsc_release.module_payload_0.module_instance_id = PRM_MODULE_INSTANCE_ID;
  235. prm_rsc_release.module_payload_0.error_code = 0;
  236. prm_rsc_release.module_payload_0.param_id = PARAM_ID_RSC_AUDIO_HW_CLK;
  237. prm_rsc_release.module_payload_0.param_size =
  238. sizeof(prm_cmd_release_rsc_t) - sizeof(apm_cmd_header_t) - sizeof(apm_module_param_data_t);
  239. prm_rsc_release.num_clk_id_t.num_clock_id = MAX_AUD_HW_CLK_NUM_REQ;
  240. prm_rsc_release.clock_ids_t[0].clock_id = cfg->clk_id;
  241. memcpy(&pkt->payload, &prm_rsc_release, sizeof(prm_cmd_release_rsc_t));
  242. ret = prm_gpr_send_pkt(pkt, &g_prm.wait);
  243. kfree(pkt);
  244. return ret;
  245. }
  246. int audio_prm_set_lpass_clk_cfg (struct clk_cfg *clk, uint8_t enable)
  247. {
  248. int ret = 0;
  249. if (enable)
  250. ret = audio_prm_set_lpass_clk_cfg_req (clk);
  251. else
  252. ret = audio_prm_set_lpass_clk_cfg_rel (clk);
  253. return ret;
  254. }
  255. EXPORT_SYMBOL(audio_prm_set_lpass_clk_cfg);
  256. static int prm_ssr_enable(struct device *dev, void *data)
  257. {
  258. mutex_lock(&g_prm.lock);
  259. g_prm.is_adsp_up = true;
  260. mutex_unlock(&g_prm.lock);
  261. return 0;
  262. }
  263. static void prm_ssr_disable(struct device *dev, void *data)
  264. {
  265. mutex_lock(&g_prm.lock);
  266. g_prm.is_adsp_up = true;
  267. mutex_unlock(&g_prm.lock);
  268. }
  269. static const struct snd_event_ops prm_ssr_ops = {
  270. .enable = prm_ssr_enable,
  271. .disable = prm_ssr_disable,
  272. };
  273. static int audio_prm_probe(struct gpr_device *adev)
  274. {
  275. int ret = 0;
  276. dev_set_drvdata(&adev->dev, &g_prm);
  277. mutex_init(&g_prm.lock);
  278. g_prm.adev = adev;
  279. g_prm.is_adsp_up = true;
  280. init_waitqueue_head(&g_prm.wait);
  281. ret = snd_event_client_register(&adev->dev, &prm_ssr_ops, NULL);
  282. if (ret) {
  283. pr_err("%s: Registration with snd event failed \n",__func__);
  284. goto err;
  285. }
  286. pr_err("%s: prm probe success\n", __func__);
  287. err:
  288. return ret;
  289. }
  290. static int audio_prm_remove(struct gpr_device *adev)
  291. {
  292. int ret = 0;
  293. mutex_lock(&g_prm.lock);
  294. ret = snd_event_client_deregister(&adev->dev);
  295. if (ret) {
  296. pr_err("%s: Deregistration from SNF failed \n",__func__);
  297. goto err;
  298. }
  299. g_prm.adev = NULL;
  300. mutex_unlock(&g_prm.lock);
  301. kfree(&g_prm);
  302. err:
  303. return ret;
  304. }
  305. static const struct of_device_id audio_prm_device_id[] = {
  306. { .compatible = "qcom,audio_prm" },
  307. {},
  308. };
  309. MODULE_DEVICE_TABLE(of, audio_prm_device_id);
  310. static struct gpr_driver qcom_audio_prm_driver = {
  311. .probe = audio_prm_probe,
  312. .remove = audio_prm_remove,
  313. .callback = audio_prm_callback,
  314. .driver = {
  315. .name = "qcom-audio_prm",
  316. .of_match_table = of_match_ptr(audio_prm_device_id),
  317. },
  318. };
  319. module_gpr_driver(qcom_audio_prm_driver);
  320. MODULE_DESCRIPTION("audio prm");
  321. MODULE_LICENSE("GPL v2");