audio_prm.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. 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. 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. g_prm.resp_received = false;
  89. ret = gpr_send_pkt(g_prm.adev, pkt);
  90. if (ret < 0) {
  91. pr_err("%s: packet not transmitted %d\n", __func__, ret);
  92. mutex_unlock(&g_prm.lock);
  93. return ret;
  94. }
  95. if (wait) {
  96. ret = wait_event_timeout(g_prm.wait,
  97. (g_prm.resp_received),
  98. msecs_to_jiffies(2 * TIMEOUT_MS));
  99. if (!ret) {
  100. pr_err("%s: pkt send timeout\n", __func__);
  101. ret = -ETIMEDOUT;
  102. } else if (atomic_read(&g_prm.status) > 0) {
  103. pr_err("%s: DSP returned error %d\n", __func__,
  104. atomic_read(&g_prm.status));
  105. ret = -EINVAL;
  106. } else {
  107. ret = 0;
  108. }
  109. }
  110. pr_debug("%s: exit",__func__);
  111. mutex_unlock(&g_prm.lock);
  112. return ret;
  113. }
  114. /**
  115. */
  116. int audio_prm_set_lpass_hw_core_req(struct clk_cfg *cfg, uint32_t hw_core_id, uint8_t enable)
  117. {
  118. struct gpr_pkt *pkt;
  119. prm_cmd_request_hw_core_t prm_rsc_request;
  120. int ret = 0;
  121. uint32_t size;
  122. size = GPR_HDR_SIZE + sizeof(prm_cmd_request_hw_core_t);
  123. pkt = kzalloc(size, GFP_KERNEL);
  124. if (!pkt)
  125. return -ENOMEM;
  126. pkt->hdr.header = GPR_SET_FIELD(GPR_PKT_VERSION, GPR_PKT_VER) |
  127. GPR_SET_FIELD(GPR_PKT_HEADER_SIZE, GPR_PKT_HEADER_WORD_SIZE_V) |
  128. GPR_SET_FIELD(GPR_PKT_PACKET_SIZE, size);
  129. pkt->hdr.src_port = GPR_SVC_ASM;
  130. pkt->hdr.dst_port = PRM_MODULE_INSTANCE_ID;
  131. pkt->hdr.dst_domain_id = GPR_IDS_DOMAIN_ID_ADSP_V;
  132. pkt->hdr.src_domain_id = GPR_IDS_DOMAIN_ID_APPS_V;
  133. pkt->hdr.token = 0; /* TBD */
  134. if (enable)
  135. pkt->hdr.opcode = PRM_CMD_REQUEST_HW_RSC;
  136. else
  137. pkt->hdr.opcode = PRM_CMD_RELEASE_HW_RSC;
  138. //pr_err("%s: clk_id %d size of cmd_req %ld \n",__func__, cfg->clk_id, sizeof(prm_cmd_request_hw_core_t));
  139. prm_rsc_request.payload_header.payload_address_lsw = 0;
  140. prm_rsc_request.payload_header.payload_address_msw = 0;
  141. prm_rsc_request.payload_header.mem_map_handle = 0;
  142. prm_rsc_request.payload_header.payload_size = sizeof(prm_cmd_request_hw_core_t) - sizeof(apm_cmd_header_t);
  143. /** Populate the param payload */
  144. prm_rsc_request.module_payload_0.module_instance_id = PRM_MODULE_INSTANCE_ID;
  145. prm_rsc_request.module_payload_0.error_code = 0;
  146. prm_rsc_request.module_payload_0.param_id = PARAM_ID_RSC_HW_CORE;
  147. prm_rsc_request.module_payload_0.param_size =
  148. sizeof(prm_cmd_request_hw_core_t) - sizeof(apm_cmd_header_t) - sizeof(apm_module_param_data_t);
  149. prm_rsc_request.hw_core_id = hw_core_id; // HW_CORE_ID_LPASS;
  150. memcpy(&pkt->payload, &prm_rsc_request, sizeof(prm_cmd_request_hw_core_t));
  151. ret = prm_gpr_send_pkt(pkt, &g_prm.wait);
  152. kfree(pkt);
  153. return ret;
  154. }
  155. EXPORT_SYMBOL(audio_prm_set_lpass_hw_core_req);
  156. /**
  157. * prm_set_lpass_clk_cfg() - Set PRM clock
  158. *
  159. * Return: 0 if clock set is success
  160. */
  161. static int audio_prm_set_lpass_clk_cfg_req(struct clk_cfg *cfg)
  162. {
  163. struct gpr_pkt *pkt;
  164. prm_cmd_request_rsc_t prm_rsc_request;
  165. int ret = 0;
  166. uint32_t size;
  167. size = GPR_HDR_SIZE + sizeof(prm_cmd_request_rsc_t);
  168. pkt = kzalloc(size, GFP_KERNEL);
  169. if (!pkt)
  170. return -ENOMEM;
  171. pkt->hdr.header = GPR_SET_FIELD(GPR_PKT_VERSION, GPR_PKT_VER) |
  172. GPR_SET_FIELD(GPR_PKT_HEADER_SIZE, GPR_PKT_HEADER_WORD_SIZE_V) |
  173. GPR_SET_FIELD(GPR_PKT_PACKET_SIZE, size);
  174. pkt->hdr.src_port = GPR_SVC_ASM;
  175. pkt->hdr.dst_port = PRM_MODULE_INSTANCE_ID;
  176. pkt->hdr.dst_domain_id = GPR_IDS_DOMAIN_ID_ADSP_V;
  177. pkt->hdr.src_domain_id = GPR_IDS_DOMAIN_ID_APPS_V;
  178. pkt->hdr.token = 0; /* TBD */
  179. pkt->hdr.opcode = PRM_CMD_REQUEST_HW_RSC;
  180. //pr_err("%s: clk_id %d size of cmd_req %ld \n",__func__, cfg->clk_id, sizeof(prm_cmd_request_rsc_t));
  181. prm_rsc_request.payload_header.payload_address_lsw = 0;
  182. prm_rsc_request.payload_header.payload_address_msw = 0;
  183. prm_rsc_request.payload_header.mem_map_handle = 0;
  184. prm_rsc_request.payload_header.payload_size = sizeof(prm_cmd_request_rsc_t) - sizeof(apm_cmd_header_t);
  185. /** Populate the param payload */
  186. prm_rsc_request.module_payload_0.module_instance_id = PRM_MODULE_INSTANCE_ID;
  187. prm_rsc_request.module_payload_0.error_code = 0;
  188. prm_rsc_request.module_payload_0.param_id = PARAM_ID_RSC_AUDIO_HW_CLK;
  189. prm_rsc_request.module_payload_0.param_size =
  190. sizeof(prm_cmd_request_rsc_t) - sizeof(apm_cmd_header_t) - sizeof(apm_module_param_data_t);
  191. prm_rsc_request.num_clk_id_t.num_clock_id = MAX_AUD_HW_CLK_NUM_REQ;
  192. prm_rsc_request.clock_ids_t[0].clock_id = cfg->clk_id;
  193. prm_rsc_request.clock_ids_t[0].clock_freq = cfg->clk_freq_in_hz;
  194. prm_rsc_request.clock_ids_t[0].clock_attri = cfg->clk_attri;
  195. prm_rsc_request.clock_ids_t[0].clock_root = cfg->clk_root;
  196. memcpy(&pkt->payload, &prm_rsc_request, sizeof(prm_cmd_request_rsc_t));
  197. ret = prm_gpr_send_pkt(pkt, &g_prm.wait);
  198. kfree(pkt);
  199. return ret;
  200. }
  201. static int audio_prm_set_lpass_clk_cfg_rel(struct clk_cfg *cfg)
  202. {
  203. struct gpr_pkt *pkt;
  204. prm_cmd_release_rsc_t prm_rsc_release;
  205. int ret = 0;
  206. uint32_t size;
  207. size = GPR_HDR_SIZE + sizeof(prm_cmd_release_rsc_t);
  208. pkt = kzalloc(size, GFP_KERNEL);
  209. if (!pkt)
  210. return -ENOMEM;
  211. pkt->hdr.header = GPR_SET_FIELD(GPR_PKT_VERSION, GPR_PKT_VER) |
  212. GPR_SET_FIELD(GPR_PKT_HEADER_SIZE, GPR_PKT_HEADER_WORD_SIZE_V) |
  213. GPR_SET_FIELD(GPR_PKT_PACKET_SIZE, size);
  214. pkt->hdr.src_port = GPR_SVC_ASM;
  215. pkt->hdr.dst_port = PRM_MODULE_INSTANCE_ID;
  216. pkt->hdr.dst_domain_id = GPR_IDS_DOMAIN_ID_ADSP_V;
  217. pkt->hdr.src_domain_id = GPR_IDS_DOMAIN_ID_APPS_V;
  218. pkt->hdr.token = 0; /* TBD */
  219. pkt->hdr.opcode = PRM_CMD_RELEASE_HW_RSC;
  220. //pr_err("%s: clk_id %d size of cmd_req %ld \n",__func__, cfg->clk_id, sizeof(prm_cmd_release_rsc_t));
  221. prm_rsc_release.payload_header.payload_address_lsw = 0;
  222. prm_rsc_release.payload_header.payload_address_msw = 0;
  223. prm_rsc_release.payload_header.mem_map_handle = 0;
  224. prm_rsc_release.payload_header.payload_size = sizeof(prm_cmd_release_rsc_t) - sizeof(apm_cmd_header_t);
  225. /** Populate the param payload */
  226. prm_rsc_release.module_payload_0.module_instance_id = PRM_MODULE_INSTANCE_ID;
  227. prm_rsc_release.module_payload_0.error_code = 0;
  228. prm_rsc_release.module_payload_0.param_id = PARAM_ID_RSC_AUDIO_HW_CLK;
  229. prm_rsc_release.module_payload_0.param_size =
  230. sizeof(prm_cmd_release_rsc_t) - sizeof(apm_cmd_header_t) - sizeof(apm_module_param_data_t);
  231. prm_rsc_release.num_clk_id_t.num_clock_id = MAX_AUD_HW_CLK_NUM_REQ;
  232. prm_rsc_release.clock_ids_t[0].clock_id = cfg->clk_id;
  233. memcpy(&pkt->payload, &prm_rsc_release, sizeof(prm_cmd_release_rsc_t));
  234. ret = prm_gpr_send_pkt(pkt, &g_prm.wait);
  235. kfree(pkt);
  236. return ret;
  237. }
  238. int audio_prm_set_lpass_clk_cfg (struct clk_cfg *clk, uint8_t enable)
  239. {
  240. int ret = 0;
  241. if (enable)
  242. ret = audio_prm_set_lpass_clk_cfg_req (clk);
  243. else
  244. ret = audio_prm_set_lpass_clk_cfg_rel (clk);
  245. return ret;
  246. }
  247. EXPORT_SYMBOL(audio_prm_set_lpass_clk_cfg);
  248. static int audio_prm_probe(struct gpr_device *adev)
  249. {
  250. int ret = 0;
  251. dev_set_drvdata(&adev->dev, &g_prm);
  252. mutex_init(&g_prm.lock);
  253. g_prm.adev = adev;
  254. init_waitqueue_head(&g_prm.wait);
  255. pr_err("%s: prm probe success\n", __func__);
  256. return ret;
  257. }
  258. static int audio_prm_remove(struct gpr_device *adev)
  259. {
  260. int ret = 0;
  261. mutex_lock(&g_prm.lock);
  262. g_prm.adev = NULL;
  263. mutex_unlock(&g_prm.lock);
  264. return ret;
  265. }
  266. static const struct of_device_id audio_prm_device_id[] = {
  267. { .compatible = "qcom,audio_prm" },
  268. {},
  269. };
  270. MODULE_DEVICE_TABLE(of, audio_prm_device_id);
  271. static struct gpr_driver qcom_audio_prm_driver = {
  272. .probe = audio_prm_probe,
  273. .remove = audio_prm_remove,
  274. .callback = audio_prm_callback,
  275. .driver = {
  276. .name = "qcom-audio_prm",
  277. .of_match_table = of_match_ptr(audio_prm_device_id),
  278. },
  279. };
  280. module_gpr_driver(qcom_audio_prm_driver);
  281. MODULE_DESCRIPTION("audio prm");
  282. MODULE_LICENSE("GPL v2");