q6apm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2020, Linaro Limited
  3. #include <dt-bindings/soc/qcom,gpr.h>
  4. #include <linux/delay.h>
  5. #include <linux/jiffies.h>
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/of.h>
  9. #include <linux/of_platform.h>
  10. #include <linux/sched.h>
  11. #include <linux/slab.h>
  12. #include <linux/soc/qcom/apr.h>
  13. #include <linux/wait.h>
  14. #include <sound/soc.h>
  15. #include <sound/soc-dapm.h>
  16. #include <sound/pcm.h>
  17. #include "audioreach.h"
  18. #include "q6apm.h"
  19. /* Graph Management */
  20. struct apm_graph_mgmt_cmd {
  21. struct apm_module_param_data param_data;
  22. uint32_t num_sub_graphs;
  23. uint32_t sub_graph_id_list[];
  24. } __packed;
  25. #define APM_GRAPH_MGMT_PSIZE(p, n) ALIGN(struct_size(p, sub_graph_id_list, n), 8)
  26. int q6apm_send_cmd_sync(struct q6apm *apm, struct gpr_pkt *pkt, uint32_t rsp_opcode)
  27. {
  28. gpr_device_t *gdev = apm->gdev;
  29. return audioreach_send_cmd_sync(&gdev->dev, gdev, &apm->result, &apm->lock,
  30. NULL, &apm->wait, pkt, rsp_opcode);
  31. }
  32. static struct audioreach_graph *q6apm_get_audioreach_graph(struct q6apm *apm, uint32_t graph_id)
  33. {
  34. struct audioreach_graph_info *info;
  35. struct audioreach_graph *graph;
  36. int id;
  37. mutex_lock(&apm->lock);
  38. graph = idr_find(&apm->graph_idr, graph_id);
  39. mutex_unlock(&apm->lock);
  40. if (graph) {
  41. kref_get(&graph->refcount);
  42. return graph;
  43. }
  44. info = idr_find(&apm->graph_info_idr, graph_id);
  45. if (!info)
  46. return ERR_PTR(-ENODEV);
  47. graph = kzalloc(sizeof(*graph), GFP_KERNEL);
  48. if (!graph)
  49. return ERR_PTR(-ENOMEM);
  50. graph->apm = apm;
  51. graph->info = info;
  52. graph->id = graph_id;
  53. graph->graph = audioreach_alloc_graph_pkt(apm, &info->sg_list, graph_id);
  54. if (IS_ERR(graph->graph)) {
  55. void *err = graph->graph;
  56. kfree(graph);
  57. return ERR_CAST(err);
  58. }
  59. mutex_lock(&apm->lock);
  60. id = idr_alloc(&apm->graph_idr, graph, graph_id, graph_id + 1, GFP_KERNEL);
  61. if (id < 0) {
  62. dev_err(apm->dev, "Unable to allocate graph id (%d)\n", graph_id);
  63. kfree(graph->graph);
  64. kfree(graph);
  65. mutex_unlock(&apm->lock);
  66. return ERR_PTR(id);
  67. }
  68. mutex_unlock(&apm->lock);
  69. kref_init(&graph->refcount);
  70. q6apm_send_cmd_sync(apm, graph->graph, 0);
  71. return graph;
  72. }
  73. static int audioreach_graph_mgmt_cmd(struct audioreach_graph *graph, uint32_t opcode)
  74. {
  75. struct audioreach_graph_info *info = graph->info;
  76. int num_sub_graphs = info->num_sub_graphs;
  77. struct apm_module_param_data *param_data;
  78. struct apm_graph_mgmt_cmd *mgmt_cmd;
  79. struct audioreach_sub_graph *sg;
  80. struct q6apm *apm = graph->apm;
  81. int i = 0, rc, payload_size;
  82. struct gpr_pkt *pkt;
  83. payload_size = APM_GRAPH_MGMT_PSIZE(mgmt_cmd, num_sub_graphs);
  84. pkt = audioreach_alloc_apm_cmd_pkt(payload_size, opcode, 0);
  85. if (IS_ERR(pkt))
  86. return PTR_ERR(pkt);
  87. mgmt_cmd = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
  88. mgmt_cmd->num_sub_graphs = num_sub_graphs;
  89. param_data = &mgmt_cmd->param_data;
  90. param_data->module_instance_id = APM_MODULE_INSTANCE_ID;
  91. param_data->param_id = APM_PARAM_ID_SUB_GRAPH_LIST;
  92. param_data->param_size = payload_size - APM_MODULE_PARAM_DATA_SIZE;
  93. list_for_each_entry(sg, &info->sg_list, node)
  94. mgmt_cmd->sub_graph_id_list[i++] = sg->sub_graph_id;
  95. rc = q6apm_send_cmd_sync(apm, pkt, 0);
  96. kfree(pkt);
  97. return rc;
  98. }
  99. static void q6apm_put_audioreach_graph(struct kref *ref)
  100. {
  101. struct audioreach_graph *graph;
  102. struct q6apm *apm;
  103. graph = container_of(ref, struct audioreach_graph, refcount);
  104. apm = graph->apm;
  105. audioreach_graph_mgmt_cmd(graph, APM_CMD_GRAPH_CLOSE);
  106. mutex_lock(&apm->lock);
  107. graph = idr_remove(&apm->graph_idr, graph->id);
  108. mutex_unlock(&apm->lock);
  109. kfree(graph->graph);
  110. kfree(graph);
  111. }
  112. static int q6apm_get_apm_state(struct q6apm *apm)
  113. {
  114. struct gpr_pkt *pkt;
  115. pkt = audioreach_alloc_apm_cmd_pkt(0, APM_CMD_GET_SPF_STATE, 0);
  116. if (IS_ERR(pkt))
  117. return PTR_ERR(pkt);
  118. q6apm_send_cmd_sync(apm, pkt, APM_CMD_RSP_GET_SPF_STATE);
  119. kfree(pkt);
  120. return apm->state;
  121. }
  122. static struct audioreach_module *__q6apm_find_module_by_mid(struct q6apm *apm,
  123. struct audioreach_graph_info *info,
  124. uint32_t mid)
  125. {
  126. struct audioreach_container *container;
  127. struct audioreach_sub_graph *sgs;
  128. struct audioreach_module *module;
  129. list_for_each_entry(sgs, &info->sg_list, node) {
  130. list_for_each_entry(container, &sgs->container_list, node) {
  131. list_for_each_entry(module, &container->modules_list, node) {
  132. if (mid == module->module_id)
  133. return module;
  134. }
  135. }
  136. }
  137. return NULL;
  138. }
  139. static struct audioreach_module *q6apm_graph_get_last_module(struct q6apm *apm, u32 sgid)
  140. {
  141. struct audioreach_container *container;
  142. struct audioreach_module *module;
  143. struct audioreach_sub_graph *sg;
  144. mutex_lock(&apm->lock);
  145. sg = idr_find(&apm->sub_graphs_idr, sgid);
  146. mutex_unlock(&apm->lock);
  147. if (!sg)
  148. return NULL;
  149. container = list_last_entry(&sg->container_list, struct audioreach_container, node);
  150. module = audioreach_get_container_last_module(container);
  151. return module;
  152. }
  153. static struct audioreach_module *q6apm_graph_get_first_module(struct q6apm *apm, u32 sgid)
  154. {
  155. struct audioreach_container *container;
  156. struct audioreach_module *module;
  157. struct audioreach_sub_graph *sg;
  158. mutex_lock(&apm->lock);
  159. sg = idr_find(&apm->sub_graphs_idr, sgid);
  160. mutex_unlock(&apm->lock);
  161. if (!sg)
  162. return NULL;
  163. container = list_first_entry(&sg->container_list, struct audioreach_container, node);
  164. module = audioreach_get_container_first_module(container);
  165. return module;
  166. }
  167. bool q6apm_is_sub_graphs_connected(struct q6apm *apm, u32 src_sgid, u32 dst_sgid)
  168. {
  169. struct audioreach_module *module;
  170. u32 iid;
  171. module = q6apm_graph_get_last_module(apm, src_sgid);
  172. if (!module)
  173. return false;
  174. iid = module->instance_id;
  175. module = q6apm_graph_get_first_module(apm, dst_sgid);
  176. if (!module)
  177. return false;
  178. if (module->src_mod_inst_id == iid)
  179. return true;
  180. return false;
  181. }
  182. int q6apm_connect_sub_graphs(struct q6apm *apm, u32 src_sgid, u32 dst_sgid, bool connect)
  183. {
  184. struct audioreach_module *module;
  185. u32 iid;
  186. if (connect) {
  187. module = q6apm_graph_get_last_module(apm, src_sgid);
  188. if (!module)
  189. return -ENODEV;
  190. iid = module->instance_id;
  191. } else {
  192. iid = 0;
  193. }
  194. module = q6apm_graph_get_first_module(apm, dst_sgid);
  195. if (!module)
  196. return -ENODEV;
  197. /* set src module in dst subgraph first module */
  198. module->src_mod_inst_id = iid;
  199. return 0;
  200. }
  201. int q6apm_graph_media_format_shmem(struct q6apm_graph *graph,
  202. struct audioreach_module_config *cfg)
  203. {
  204. struct audioreach_module *module;
  205. if (cfg->direction == SNDRV_PCM_STREAM_CAPTURE)
  206. module = q6apm_find_module_by_mid(graph, MODULE_ID_RD_SHARED_MEM_EP);
  207. else
  208. module = q6apm_find_module_by_mid(graph, MODULE_ID_WR_SHARED_MEM_EP);
  209. if (!module)
  210. return -ENODEV;
  211. audioreach_set_media_format(graph, module, cfg);
  212. return 0;
  213. }
  214. EXPORT_SYMBOL_GPL(q6apm_graph_media_format_shmem);
  215. int q6apm_map_memory_regions(struct q6apm_graph *graph, unsigned int dir, phys_addr_t phys,
  216. size_t period_sz, unsigned int periods)
  217. {
  218. struct audioreach_graph_data *data;
  219. struct audio_buffer *buf;
  220. int cnt;
  221. int rc;
  222. if (dir == SNDRV_PCM_STREAM_PLAYBACK)
  223. data = &graph->rx_data;
  224. else
  225. data = &graph->tx_data;
  226. mutex_lock(&graph->lock);
  227. if (data->buf) {
  228. mutex_unlock(&graph->lock);
  229. return 0;
  230. }
  231. buf = kzalloc(((sizeof(struct audio_buffer)) * periods), GFP_KERNEL);
  232. if (!buf) {
  233. mutex_unlock(&graph->lock);
  234. return -ENOMEM;
  235. }
  236. if (dir == SNDRV_PCM_STREAM_PLAYBACK)
  237. data = &graph->rx_data;
  238. else
  239. data = &graph->tx_data;
  240. data->buf = buf;
  241. buf[0].phys = phys;
  242. buf[0].size = period_sz;
  243. for (cnt = 1; cnt < periods; cnt++) {
  244. if (period_sz > 0) {
  245. buf[cnt].phys = buf[0].phys + (cnt * period_sz);
  246. buf[cnt].size = period_sz;
  247. }
  248. }
  249. data->num_periods = periods;
  250. mutex_unlock(&graph->lock);
  251. rc = audioreach_map_memory_regions(graph, dir, period_sz, periods, 1);
  252. if (rc < 0) {
  253. dev_err(graph->dev, "Memory_map_regions failed\n");
  254. audioreach_graph_free_buf(graph);
  255. }
  256. return rc;
  257. }
  258. EXPORT_SYMBOL_GPL(q6apm_map_memory_regions);
  259. int q6apm_unmap_memory_regions(struct q6apm_graph *graph, unsigned int dir)
  260. {
  261. struct apm_cmd_shared_mem_unmap_regions *cmd;
  262. struct audioreach_graph_data *data;
  263. struct gpr_pkt *pkt;
  264. int rc;
  265. if (dir == SNDRV_PCM_STREAM_PLAYBACK)
  266. data = &graph->rx_data;
  267. else
  268. data = &graph->tx_data;
  269. if (!data->mem_map_handle)
  270. return 0;
  271. pkt = audioreach_alloc_apm_pkt(sizeof(*cmd), APM_CMD_SHARED_MEM_UNMAP_REGIONS, dir,
  272. graph->port->id);
  273. if (IS_ERR(pkt))
  274. return PTR_ERR(pkt);
  275. cmd = (void *)pkt + GPR_HDR_SIZE;
  276. cmd->mem_map_handle = data->mem_map_handle;
  277. rc = audioreach_graph_send_cmd_sync(graph, pkt, APM_CMD_SHARED_MEM_UNMAP_REGIONS);
  278. kfree(pkt);
  279. audioreach_graph_free_buf(graph);
  280. return rc;
  281. }
  282. EXPORT_SYMBOL_GPL(q6apm_unmap_memory_regions);
  283. int q6apm_graph_media_format_pcm(struct q6apm_graph *graph, struct audioreach_module_config *cfg)
  284. {
  285. struct audioreach_graph_info *info = graph->info;
  286. struct audioreach_sub_graph *sgs;
  287. struct audioreach_container *container;
  288. struct audioreach_module *module;
  289. list_for_each_entry(sgs, &info->sg_list, node) {
  290. list_for_each_entry(container, &sgs->container_list, node) {
  291. list_for_each_entry(module, &container->modules_list, node) {
  292. if ((module->module_id == MODULE_ID_WR_SHARED_MEM_EP) ||
  293. (module->module_id == MODULE_ID_RD_SHARED_MEM_EP))
  294. continue;
  295. audioreach_set_media_format(graph, module, cfg);
  296. }
  297. }
  298. }
  299. return 0;
  300. }
  301. EXPORT_SYMBOL_GPL(q6apm_graph_media_format_pcm);
  302. static int q6apm_graph_get_tx_shmem_module_iid(struct q6apm_graph *graph)
  303. {
  304. struct audioreach_module *module;
  305. module = q6apm_find_module_by_mid(graph, MODULE_ID_RD_SHARED_MEM_EP);
  306. if (!module)
  307. return -ENODEV;
  308. return module->instance_id;
  309. }
  310. int q6apm_graph_get_rx_shmem_module_iid(struct q6apm_graph *graph)
  311. {
  312. struct audioreach_module *module;
  313. module = q6apm_find_module_by_mid(graph, MODULE_ID_WR_SHARED_MEM_EP);
  314. if (!module)
  315. return -ENODEV;
  316. return module->instance_id;
  317. }
  318. EXPORT_SYMBOL_GPL(q6apm_graph_get_rx_shmem_module_iid);
  319. int q6apm_write_async(struct q6apm_graph *graph, uint32_t len, uint32_t msw_ts,
  320. uint32_t lsw_ts, uint32_t wflags)
  321. {
  322. struct apm_data_cmd_wr_sh_mem_ep_data_buffer_v2 *write_buffer;
  323. struct audio_buffer *ab;
  324. struct gpr_pkt *pkt;
  325. int rc, iid;
  326. iid = q6apm_graph_get_rx_shmem_module_iid(graph);
  327. pkt = audioreach_alloc_pkt(sizeof(*write_buffer), DATA_CMD_WR_SH_MEM_EP_DATA_BUFFER_V2,
  328. graph->rx_data.dsp_buf | (len << APM_WRITE_TOKEN_LEN_SHIFT),
  329. graph->port->id, iid);
  330. if (IS_ERR(pkt))
  331. return PTR_ERR(pkt);
  332. write_buffer = (void *)pkt + GPR_HDR_SIZE;
  333. mutex_lock(&graph->lock);
  334. ab = &graph->rx_data.buf[graph->rx_data.dsp_buf];
  335. write_buffer->buf_addr_lsw = lower_32_bits(ab->phys);
  336. write_buffer->buf_addr_msw = upper_32_bits(ab->phys);
  337. write_buffer->buf_size = len;
  338. write_buffer->timestamp_lsw = lsw_ts;
  339. write_buffer->timestamp_msw = msw_ts;
  340. write_buffer->mem_map_handle = graph->rx_data.mem_map_handle;
  341. write_buffer->flags = wflags;
  342. graph->rx_data.dsp_buf++;
  343. if (graph->rx_data.dsp_buf >= graph->rx_data.num_periods)
  344. graph->rx_data.dsp_buf = 0;
  345. mutex_unlock(&graph->lock);
  346. rc = gpr_send_port_pkt(graph->port, pkt);
  347. kfree(pkt);
  348. return rc;
  349. }
  350. EXPORT_SYMBOL_GPL(q6apm_write_async);
  351. int q6apm_read(struct q6apm_graph *graph)
  352. {
  353. struct data_cmd_rd_sh_mem_ep_data_buffer_v2 *read_buffer;
  354. struct audioreach_graph_data *port;
  355. struct audio_buffer *ab;
  356. struct gpr_pkt *pkt;
  357. int rc, iid;
  358. iid = q6apm_graph_get_tx_shmem_module_iid(graph);
  359. pkt = audioreach_alloc_pkt(sizeof(*read_buffer), DATA_CMD_RD_SH_MEM_EP_DATA_BUFFER_V2,
  360. graph->tx_data.dsp_buf, graph->port->id, iid);
  361. if (IS_ERR(pkt))
  362. return PTR_ERR(pkt);
  363. read_buffer = (void *)pkt + GPR_HDR_SIZE;
  364. mutex_lock(&graph->lock);
  365. port = &graph->tx_data;
  366. ab = &port->buf[port->dsp_buf];
  367. read_buffer->buf_addr_lsw = lower_32_bits(ab->phys);
  368. read_buffer->buf_addr_msw = upper_32_bits(ab->phys);
  369. read_buffer->mem_map_handle = port->mem_map_handle;
  370. read_buffer->buf_size = ab->size;
  371. port->dsp_buf++;
  372. if (port->dsp_buf >= port->num_periods)
  373. port->dsp_buf = 0;
  374. mutex_unlock(&graph->lock);
  375. rc = gpr_send_port_pkt(graph->port, pkt);
  376. kfree(pkt);
  377. return rc;
  378. }
  379. EXPORT_SYMBOL_GPL(q6apm_read);
  380. static int graph_callback(struct gpr_resp_pkt *data, void *priv, int op)
  381. {
  382. struct data_cmd_rsp_rd_sh_mem_ep_data_buffer_done_v2 *rd_done;
  383. struct data_cmd_rsp_wr_sh_mem_ep_data_buffer_done_v2 *done;
  384. struct apm_cmd_rsp_shared_mem_map_regions *rsp;
  385. struct gpr_ibasic_rsp_result_t *result;
  386. struct q6apm_graph *graph = priv;
  387. struct gpr_hdr *hdr = &data->hdr;
  388. struct device *dev = graph->dev;
  389. uint32_t client_event;
  390. phys_addr_t phys;
  391. int token;
  392. result = data->payload;
  393. switch (hdr->opcode) {
  394. case DATA_CMD_RSP_WR_SH_MEM_EP_DATA_BUFFER_DONE_V2:
  395. if (!graph->ar_graph)
  396. break;
  397. client_event = APM_CLIENT_EVENT_DATA_WRITE_DONE;
  398. mutex_lock(&graph->lock);
  399. token = hdr->token & APM_WRITE_TOKEN_MASK;
  400. done = data->payload;
  401. phys = graph->rx_data.buf[token].phys;
  402. mutex_unlock(&graph->lock);
  403. if (lower_32_bits(phys) == done->buf_addr_lsw &&
  404. upper_32_bits(phys) == done->buf_addr_msw) {
  405. graph->result.opcode = hdr->opcode;
  406. graph->result.status = done->status;
  407. if (graph->cb)
  408. graph->cb(client_event, hdr->token, data->payload, graph->priv);
  409. } else {
  410. dev_err(dev, "WR BUFF Unexpected addr %08x-%08x\n", done->buf_addr_lsw,
  411. done->buf_addr_msw);
  412. }
  413. break;
  414. case APM_CMD_RSP_SHARED_MEM_MAP_REGIONS:
  415. graph->result.opcode = hdr->opcode;
  416. graph->result.status = 0;
  417. rsp = data->payload;
  418. if (hdr->token == SNDRV_PCM_STREAM_PLAYBACK)
  419. graph->rx_data.mem_map_handle = rsp->mem_map_handle;
  420. else
  421. graph->tx_data.mem_map_handle = rsp->mem_map_handle;
  422. wake_up(&graph->cmd_wait);
  423. break;
  424. case DATA_CMD_RSP_RD_SH_MEM_EP_DATA_BUFFER_V2:
  425. if (!graph->ar_graph)
  426. break;
  427. client_event = APM_CLIENT_EVENT_DATA_READ_DONE;
  428. mutex_lock(&graph->lock);
  429. rd_done = data->payload;
  430. phys = graph->tx_data.buf[hdr->token].phys;
  431. mutex_unlock(&graph->lock);
  432. if (upper_32_bits(phys) == rd_done->buf_addr_msw &&
  433. lower_32_bits(phys) == rd_done->buf_addr_lsw) {
  434. graph->result.opcode = hdr->opcode;
  435. graph->result.status = rd_done->status;
  436. if (graph->cb)
  437. graph->cb(client_event, hdr->token, data->payload, graph->priv);
  438. } else {
  439. dev_err(dev, "RD BUFF Unexpected addr %08x-%08x\n", rd_done->buf_addr_lsw,
  440. rd_done->buf_addr_msw);
  441. }
  442. break;
  443. case DATA_CMD_WR_SH_MEM_EP_EOS_RENDERED:
  444. break;
  445. case GPR_BASIC_RSP_RESULT:
  446. switch (result->opcode) {
  447. case APM_CMD_SHARED_MEM_UNMAP_REGIONS:
  448. graph->result.opcode = result->opcode;
  449. graph->result.status = 0;
  450. if (hdr->token == SNDRV_PCM_STREAM_PLAYBACK)
  451. graph->rx_data.mem_map_handle = 0;
  452. else
  453. graph->tx_data.mem_map_handle = 0;
  454. wake_up(&graph->cmd_wait);
  455. break;
  456. case APM_CMD_SHARED_MEM_MAP_REGIONS:
  457. case DATA_CMD_WR_SH_MEM_EP_MEDIA_FORMAT:
  458. case APM_CMD_SET_CFG:
  459. graph->result.opcode = result->opcode;
  460. graph->result.status = result->status;
  461. if (result->status)
  462. dev_err(dev, "Error (%d) Processing 0x%08x cmd\n",
  463. result->status, result->opcode);
  464. wake_up(&graph->cmd_wait);
  465. break;
  466. default:
  467. break;
  468. }
  469. break;
  470. default:
  471. break;
  472. }
  473. return 0;
  474. }
  475. struct q6apm_graph *q6apm_graph_open(struct device *dev, q6apm_cb cb,
  476. void *priv, int graph_id)
  477. {
  478. struct q6apm *apm = dev_get_drvdata(dev->parent);
  479. struct audioreach_graph *ar_graph;
  480. struct q6apm_graph *graph;
  481. int ret;
  482. ar_graph = q6apm_get_audioreach_graph(apm, graph_id);
  483. if (IS_ERR(ar_graph)) {
  484. dev_err(dev, "No graph found with id %d\n", graph_id);
  485. return ERR_CAST(ar_graph);
  486. }
  487. graph = kzalloc(sizeof(*graph), GFP_KERNEL);
  488. if (!graph) {
  489. ret = -ENOMEM;
  490. goto put_ar_graph;
  491. }
  492. graph->apm = apm;
  493. graph->priv = priv;
  494. graph->cb = cb;
  495. graph->info = ar_graph->info;
  496. graph->ar_graph = ar_graph;
  497. graph->id = ar_graph->id;
  498. graph->dev = dev;
  499. mutex_init(&graph->lock);
  500. init_waitqueue_head(&graph->cmd_wait);
  501. graph->port = gpr_alloc_port(apm->gdev, dev, graph_callback, graph);
  502. if (IS_ERR(graph->port)) {
  503. ret = PTR_ERR(graph->port);
  504. goto free_graph;
  505. }
  506. return graph;
  507. free_graph:
  508. kfree(graph);
  509. put_ar_graph:
  510. kref_put(&ar_graph->refcount, q6apm_put_audioreach_graph);
  511. return ERR_PTR(ret);
  512. }
  513. EXPORT_SYMBOL_GPL(q6apm_graph_open);
  514. int q6apm_graph_close(struct q6apm_graph *graph)
  515. {
  516. struct audioreach_graph *ar_graph = graph->ar_graph;
  517. graph->ar_graph = NULL;
  518. kref_put(&ar_graph->refcount, q6apm_put_audioreach_graph);
  519. gpr_free_port(graph->port);
  520. kfree(graph);
  521. return 0;
  522. }
  523. EXPORT_SYMBOL_GPL(q6apm_graph_close);
  524. int q6apm_graph_prepare(struct q6apm_graph *graph)
  525. {
  526. return audioreach_graph_mgmt_cmd(graph->ar_graph, APM_CMD_GRAPH_PREPARE);
  527. }
  528. EXPORT_SYMBOL_GPL(q6apm_graph_prepare);
  529. int q6apm_graph_start(struct q6apm_graph *graph)
  530. {
  531. struct audioreach_graph *ar_graph = graph->ar_graph;
  532. int ret = 0;
  533. if (ar_graph->start_count == 0)
  534. ret = audioreach_graph_mgmt_cmd(ar_graph, APM_CMD_GRAPH_START);
  535. ar_graph->start_count++;
  536. return ret;
  537. }
  538. EXPORT_SYMBOL_GPL(q6apm_graph_start);
  539. int q6apm_graph_stop(struct q6apm_graph *graph)
  540. {
  541. struct audioreach_graph *ar_graph = graph->ar_graph;
  542. if (--ar_graph->start_count > 0)
  543. return 0;
  544. return audioreach_graph_mgmt_cmd(ar_graph, APM_CMD_GRAPH_STOP);
  545. }
  546. EXPORT_SYMBOL_GPL(q6apm_graph_stop);
  547. int q6apm_graph_flush(struct q6apm_graph *graph)
  548. {
  549. return audioreach_graph_mgmt_cmd(graph->ar_graph, APM_CMD_GRAPH_FLUSH);
  550. }
  551. EXPORT_SYMBOL_GPL(q6apm_graph_flush);
  552. static int q6apm_audio_probe(struct snd_soc_component *component)
  553. {
  554. return audioreach_tplg_init(component);
  555. }
  556. static void q6apm_audio_remove(struct snd_soc_component *component)
  557. {
  558. /* remove topology */
  559. snd_soc_tplg_component_remove(component);
  560. }
  561. #define APM_AUDIO_DRV_NAME "q6apm-audio"
  562. static const struct snd_soc_component_driver q6apm_audio_component = {
  563. .name = APM_AUDIO_DRV_NAME,
  564. .probe = q6apm_audio_probe,
  565. .remove = q6apm_audio_remove,
  566. };
  567. static int apm_probe(gpr_device_t *gdev)
  568. {
  569. struct device *dev = &gdev->dev;
  570. struct q6apm *apm;
  571. int ret;
  572. apm = devm_kzalloc(dev, sizeof(*apm), GFP_KERNEL);
  573. if (!apm)
  574. return -ENOMEM;
  575. dev_set_drvdata(dev, apm);
  576. mutex_init(&apm->lock);
  577. apm->dev = dev;
  578. apm->gdev = gdev;
  579. init_waitqueue_head(&apm->wait);
  580. idr_init(&apm->graph_idr);
  581. idr_init(&apm->graph_info_idr);
  582. idr_init(&apm->sub_graphs_idr);
  583. idr_init(&apm->containers_idr);
  584. idr_init(&apm->modules_idr);
  585. q6apm_get_apm_state(apm);
  586. ret = devm_snd_soc_register_component(dev, &q6apm_audio_component, NULL, 0);
  587. if (ret < 0) {
  588. dev_err(dev, "failed to get register q6apm: %d\n", ret);
  589. return ret;
  590. }
  591. return of_platform_populate(dev->of_node, NULL, NULL, dev);
  592. }
  593. struct audioreach_module *q6apm_find_module_by_mid(struct q6apm_graph *graph, uint32_t mid)
  594. {
  595. struct audioreach_graph_info *info = graph->info;
  596. struct q6apm *apm = graph->apm;
  597. return __q6apm_find_module_by_mid(apm, info, mid);
  598. }
  599. static int apm_callback(struct gpr_resp_pkt *data, void *priv, int op)
  600. {
  601. gpr_device_t *gdev = priv;
  602. struct q6apm *apm = dev_get_drvdata(&gdev->dev);
  603. struct device *dev = &gdev->dev;
  604. struct gpr_ibasic_rsp_result_t *result;
  605. struct gpr_hdr *hdr = &data->hdr;
  606. result = data->payload;
  607. switch (hdr->opcode) {
  608. case APM_CMD_RSP_GET_SPF_STATE:
  609. apm->result.opcode = hdr->opcode;
  610. apm->result.status = 0;
  611. /* First word of result it state */
  612. apm->state = result->opcode;
  613. wake_up(&apm->wait);
  614. break;
  615. case GPR_BASIC_RSP_RESULT:
  616. switch (result->opcode) {
  617. case APM_CMD_GRAPH_START:
  618. case APM_CMD_GRAPH_OPEN:
  619. case APM_CMD_GRAPH_PREPARE:
  620. case APM_CMD_GRAPH_CLOSE:
  621. case APM_CMD_GRAPH_FLUSH:
  622. case APM_CMD_GRAPH_STOP:
  623. case APM_CMD_SET_CFG:
  624. apm->result.opcode = result->opcode;
  625. apm->result.status = result->status;
  626. if (result->status)
  627. dev_err(dev, "Error (%d) Processing 0x%08x cmd\n", result->status,
  628. result->opcode);
  629. wake_up(&apm->wait);
  630. break;
  631. default:
  632. break;
  633. }
  634. break;
  635. default:
  636. break;
  637. }
  638. return 0;
  639. }
  640. #ifdef CONFIG_OF
  641. static const struct of_device_id apm_device_id[] = {
  642. { .compatible = "qcom,q6apm" },
  643. {},
  644. };
  645. MODULE_DEVICE_TABLE(of, apm_device_id);
  646. #endif
  647. static gpr_driver_t apm_driver = {
  648. .probe = apm_probe,
  649. .gpr_callback = apm_callback,
  650. .driver = {
  651. .name = "qcom-apm",
  652. .of_match_table = of_match_ptr(apm_device_id),
  653. },
  654. };
  655. module_gpr_driver(apm_driver);
  656. MODULE_DESCRIPTION("Audio Process Manager");
  657. MODULE_LICENSE("GPL");