q6apm.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __Q6APM_H__
  3. #define __Q6APM_H__
  4. #include <linux/types.h>
  5. #include <linux/atomic.h>
  6. #include <linux/slab.h>
  7. #include <linux/wait.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/sched.h>
  11. #include <linux/of.h>
  12. #include <linux/delay.h>
  13. #include <sound/soc.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/jiffies.h>
  16. #include <linux/soc/qcom/apr.h>
  17. #include "audioreach.h"
  18. #define APM_PORT_MAX 127
  19. #define APM_PORT_MAX_AUDIO_CHAN_CNT 8
  20. #define PCM_CHANNEL_NULL 0
  21. #define PCM_CHANNEL_FL 1 /* Front left channel. */
  22. #define PCM_CHANNEL_FR 2 /* Front right channel. */
  23. #define PCM_CHANNEL_FC 3 /* Front center channel. */
  24. #define PCM_CHANNEL_LS 4 /* Left surround channel. */
  25. #define PCM_CHANNEL_RS 5 /* Right surround channel. */
  26. #define PCM_CHANNEL_LFE 6 /* Low frequency effect channel. */
  27. #define PCM_CHANNEL_CS 7 /* Center surround channel; Rear center ch */
  28. #define PCM_CHANNEL_LB 8 /* Left back channel; Rear left channel. */
  29. #define PCM_CHANNEL_RB 9 /* Right back channel; Rear right channel. */
  30. #define PCM_CHANNELS 10 /* Top surround channel. */
  31. #define APM_TIMESTAMP_FLAG 0x80000000
  32. #define FORMAT_LINEAR_PCM 0x0000
  33. /* APM client callback events */
  34. #define APM_CMD_EOS 0x0003
  35. #define APM_CLIENT_EVENT_CMD_EOS_DONE 0x1003
  36. #define APM_CMD_CLOSE 0x0004
  37. #define APM_CLIENT_EVENT_CMD_CLOSE_DONE 0x1004
  38. #define APM_CLIENT_EVENT_CMD_RUN_DONE 0x1008
  39. #define APM_CLIENT_EVENT_DATA_WRITE_DONE 0x1009
  40. #define APM_CLIENT_EVENT_DATA_READ_DONE 0x100a
  41. #define APM_WRITE_TOKEN_MASK GENMASK(15, 0)
  42. #define APM_WRITE_TOKEN_LEN_MASK GENMASK(31, 16)
  43. #define APM_WRITE_TOKEN_LEN_SHIFT 16
  44. #define APM_MAX_SESSIONS 8
  45. #define APM_LAST_BUFFER_FLAG BIT(30)
  46. #define NO_TIMESTAMP 0xFF00
  47. struct q6apm {
  48. struct device *dev;
  49. gpr_port_t *port;
  50. gpr_device_t *gdev;
  51. /* For Graph OPEN/START/STOP/CLOSE operations */
  52. wait_queue_head_t wait;
  53. struct gpr_ibasic_rsp_result_t result;
  54. struct mutex cmd_lock;
  55. struct mutex lock;
  56. uint32_t state;
  57. struct list_head widget_list;
  58. struct idr graph_idr;
  59. struct idr graph_info_idr;
  60. struct idr sub_graphs_idr;
  61. struct idr containers_idr;
  62. struct idr modules_idr;
  63. };
  64. struct audio_buffer {
  65. phys_addr_t phys;
  66. uint32_t size; /* size of buffer */
  67. };
  68. struct audioreach_graph_data {
  69. struct audio_buffer *buf;
  70. uint32_t num_periods;
  71. uint32_t dsp_buf;
  72. uint32_t mem_map_handle;
  73. atomic_t hw_ptr;
  74. };
  75. struct audioreach_graph {
  76. struct audioreach_graph_info *info;
  77. uint32_t id;
  78. int state;
  79. int start_count;
  80. /* Cached Graph data */
  81. void *graph;
  82. struct kref refcount;
  83. struct q6apm *apm;
  84. };
  85. typedef void (*q6apm_cb) (uint32_t opcode, uint32_t token,
  86. void *payload, void *priv);
  87. struct q6apm_graph {
  88. void *priv;
  89. q6apm_cb cb;
  90. uint32_t id;
  91. struct device *dev;
  92. struct q6apm *apm;
  93. gpr_port_t *port;
  94. struct audioreach_graph_data rx_data;
  95. struct audioreach_graph_data tx_data;
  96. struct gpr_ibasic_rsp_result_t result;
  97. wait_queue_head_t cmd_wait;
  98. struct mutex lock;
  99. struct audioreach_graph *ar_graph;
  100. struct audioreach_graph_info *info;
  101. };
  102. /* Graph Operations */
  103. struct q6apm_graph *q6apm_graph_open(struct device *dev, q6apm_cb cb,
  104. void *priv, int graph_id);
  105. int q6apm_graph_close(struct q6apm_graph *graph);
  106. int q6apm_graph_prepare(struct q6apm_graph *graph);
  107. int q6apm_graph_start(struct q6apm_graph *graph);
  108. int q6apm_graph_stop(struct q6apm_graph *graph);
  109. int q6apm_graph_flush(struct q6apm_graph *graph);
  110. /* Media Format */
  111. int q6apm_graph_media_format_pcm(struct q6apm_graph *graph,
  112. struct audioreach_module_config *cfg);
  113. int q6apm_graph_media_format_shmem(struct q6apm_graph *graph,
  114. struct audioreach_module_config *cfg);
  115. /* read/write related */
  116. int q6apm_read(struct q6apm_graph *graph);
  117. int q6apm_write_async(struct q6apm_graph *graph, uint32_t len, uint32_t msw_ts,
  118. uint32_t lsw_ts, uint32_t wflags);
  119. /* Memory Map related */
  120. int q6apm_map_memory_regions(struct q6apm_graph *graph,
  121. unsigned int dir, phys_addr_t phys,
  122. size_t period_sz, unsigned int periods);
  123. int q6apm_unmap_memory_regions(struct q6apm_graph *graph,
  124. unsigned int dir);
  125. /* Helpers */
  126. int q6apm_send_cmd_sync(struct q6apm *apm, struct gpr_pkt *pkt,
  127. uint32_t rsp_opcode);
  128. /* Callback for graph specific */
  129. struct audioreach_module *q6apm_find_module_by_mid(struct q6apm_graph *graph,
  130. uint32_t mid);
  131. int q6apm_graph_get_rx_shmem_module_iid(struct q6apm_graph *graph);
  132. bool q6apm_is_adsp_ready(void);
  133. int q6apm_enable_compress_module(struct device *dev, struct q6apm_graph *graph, bool en);
  134. int q6apm_remove_initial_silence(struct device *dev, struct q6apm_graph *graph, uint32_t samples);
  135. int q6apm_remove_trailing_silence(struct device *dev, struct q6apm_graph *graph, uint32_t samples);
  136. int q6apm_set_real_module_id(struct device *dev, struct q6apm_graph *graph, uint32_t codec_id);
  137. int q6apm_get_hw_pointer(struct q6apm_graph *graph, int dir);
  138. #endif /* __APM_GRAPH_ */