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