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 "../common.h" 18 #include "audioreach.h" 19 20 #define APM_PORT_MAX LPASS_MAX_PORT 21 #define APM_PORT_MAX_AUDIO_CHAN_CNT 8 22 #define PCM_CHANNEL_NULL 0 23 #define PCM_CHANNEL_FL 1 /* Front left channel. */ 24 #define PCM_CHANNEL_FR 2 /* Front right channel. */ 25 #define PCM_CHANNEL_FC 3 /* Front center channel. */ 26 #define PCM_CHANNEL_LS 4 /* Left surround channel. */ 27 #define PCM_CHANNEL_RS 5 /* Right surround channel. */ 28 #define PCM_CHANNEL_LFE 6 /* Low frequency effect channel. */ 29 #define PCM_CHANNEL_CS 7 /* Center surround channel; Rear center ch */ 30 #define PCM_CHANNEL_LB 8 /* Left back channel; Rear left channel. */ 31 #define PCM_CHANNEL_RB 9 /* Right back channel; Rear right channel. */ 32 #define PCM_CHANNELS 10 /* Top surround channel. */ 33 34 #define APM_TIMESTAMP_FLAG 0x80000000 35 #define FORMAT_LINEAR_PCM 0x0000 36 /* APM client callback events */ 37 #define APM_CMD_EOS 0x0003 38 #define APM_CLIENT_EVENT_CMD_EOS_DONE 0x1003 39 #define APM_CMD_CLOSE 0x0004 40 #define APM_CLIENT_EVENT_CMD_CLOSE_DONE 0x1004 41 #define APM_CLIENT_EVENT_CMD_RUN_DONE 0x1008 42 #define APM_CLIENT_EVENT_DATA_WRITE_DONE 0x1009 43 #define APM_CLIENT_EVENT_DATA_READ_DONE 0x100a 44 #define APM_CLIENT_EVENT_WATERMARK_EVENT 0x100b 45 #define APM_WRITE_TOKEN_MASK GENMASK(15, 0) 46 #define APM_WRITE_TOKEN_LEN_MASK GENMASK(31, 16) 47 #define APM_WRITE_TOKEN_LEN_SHIFT 16 48 49 #define APM_MAX_SESSIONS 8 50 #define APM_LAST_BUFFER_FLAG BIT(30) 51 #define NO_TIMESTAMP 0xFF00 52 53 struct q6apm { 54 struct device *dev; 55 gpr_port_t *port; 56 gpr_device_t *gdev; 57 /* For Graph OPEN/START/STOP/CLOSE operations */ 58 wait_queue_head_t wait; 59 struct gpr_ibasic_rsp_result_t result; 60 61 struct mutex cmd_lock; 62 struct mutex lock; 63 uint32_t state; 64 65 struct list_head widget_list; 66 struct idr graph_idr; 67 struct idr graph_info_idr; 68 struct idr sub_graphs_idr; 69 struct idr containers_idr; 70 struct idr modules_idr; 71 }; 72 73 struct audio_buffer { 74 phys_addr_t phys; 75 uint32_t size; /* size of buffer */ 76 }; 77 78 struct audioreach_graph_data { 79 struct audio_buffer *buf; 80 uint32_t num_periods; 81 uint32_t dsp_buf; 82 atomic_t hw_ptr; 83 }; 84 85 struct audioreach_graph { 86 struct audioreach_graph_info *info; 87 uint32_t id; 88 int state; 89 int start_count; 90 /* Cached Graph data */ 91 void *graph; 92 struct kref refcount; 93 struct q6apm *apm; 94 }; 95 96 typedef void (*q6apm_cb) (uint32_t opcode, uint32_t token, 97 void *payload, void *priv); 98 struct q6apm_graph { 99 void *priv; 100 q6apm_cb cb; 101 uint32_t id; 102 uint32_t shm_iid; 103 struct device *dev; 104 struct q6apm *apm; 105 gpr_port_t *port; 106 struct audioreach_graph_data rx_data; 107 struct audioreach_graph_data tx_data; 108 struct gpr_ibasic_rsp_result_t result; 109 wait_queue_head_t cmd_wait; 110 struct mutex lock; 111 struct audioreach_graph *ar_graph; 112 struct audioreach_graph_info *info; 113 }; 114 115 /* Graph Operations */ 116 struct q6apm_graph *q6apm_graph_open(struct device *dev, q6apm_cb cb, 117 void *priv, int graph_id, int dir); 118 int q6apm_graph_close(struct q6apm_graph *graph); 119 int q6apm_graph_prepare(struct q6apm_graph *graph); 120 int q6apm_graph_start(struct q6apm_graph *graph); 121 int q6apm_graph_stop(struct q6apm_graph *graph); 122 int q6apm_graph_flush(struct q6apm_graph *graph); 123 124 /* Media Format */ 125 int q6apm_graph_media_format_pcm(struct q6apm_graph *graph, 126 struct audioreach_module_config *cfg); 127 128 int q6apm_graph_media_format_shmem(struct q6apm_graph *graph, 129 struct audioreach_module_config *cfg); 130 131 /* read/write related */ 132 int q6apm_read(struct q6apm_graph *graph); 133 int q6apm_write_async(struct q6apm_graph *graph, uint32_t len, uint32_t msw_ts, 134 uint32_t lsw_ts, uint32_t wflags); 135 136 /* Memory Map related */ 137 int q6apm_map_memory_fixed_region(struct device *dev, 138 unsigned int graph_id, phys_addr_t phys, 139 size_t sz); 140 int q6apm_map_pos_buffer(struct device *dev, 141 unsigned int graph_id, phys_addr_t phys, 142 size_t sz); 143 int q6apm_unmap_pos_buffer(struct device *dev, unsigned int graph_id); 144 int q6apm_alloc_fragments(struct q6apm_graph *graph, 145 unsigned int dir, phys_addr_t phys, 146 size_t period_sz, unsigned int periods); 147 int q6apm_free_fragments(struct q6apm_graph *graph, unsigned int dir); 148 int q6apm_unmap_memory_fixed_region(struct device *dev, unsigned int graph_id); 149 /* Helpers */ 150 int q6apm_send_cmd_sync(struct q6apm *apm, const struct gpr_pkt *pkt, 151 uint32_t rsp_opcode); 152 153 /* Callback for graph specific */ 154 struct audioreach_module *q6apm_find_module_by_mid(struct q6apm_graph *graph, 155 uint32_t mid); 156 bool q6apm_is_adsp_ready(void); 157 158 int q6apm_enable_compress_module(struct device *dev, struct q6apm_graph *graph, bool en); 159 int q6apm_remove_initial_silence(struct device *dev, struct q6apm_graph *graph, uint32_t samples); 160 int q6apm_remove_trailing_silence(struct device *dev, struct q6apm_graph *graph, uint32_t samples); 161 int q6apm_set_real_module_id(struct device *dev, struct q6apm_graph *graph, uint32_t codec_id); 162 int q6apm_get_hw_pointer(struct q6apm_graph *graph, int dir); 163 bool q6apm_is_graph_in_push_pull_mode(struct q6apm_graph *graph); 164 bool q6apm_is_graph_in_push_pull_mode_from_id(struct device *dev, unsigned int graph_id, int dir); 165 int q6apm_push_pull_config(struct q6apm_graph *graph, phys_addr_t bphys, 166 phys_addr_t pphys, uint32_t size); 167 168 int q6apm_register_watermark_event(struct q6apm_graph *graph, int watermark_bytes, int num_levels); 169 #endif /* __APM_GRAPH_ */ 170