audio_prm.c 11 KB

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