audio_prm.c 9.2 KB

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