]>
Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
2159ad93 MB |
2 | /* |
3 | * wm_adsp.c -- Wolfson ADSP support | |
4 | * | |
5 | * Copyright 2012 Wolfson Microelectronics plc | |
6 | * | |
7 | * Author: Mark Brown <[email protected]> | |
2159ad93 MB |
8 | */ |
9 | ||
4693b120 | 10 | #include <linux/array_size.h> |
605391d0 | 11 | #include <linux/ctype.h> |
2159ad93 MB |
12 | #include <linux/module.h> |
13 | #include <linux/moduleparam.h> | |
14 | #include <linux/init.h> | |
15 | #include <linux/delay.h> | |
16 | #include <linux/firmware.h> | |
cf17c83c | 17 | #include <linux/list.h> |
2159ad93 | 18 | #include <linux/pm.h> |
2159ad93 | 19 | #include <linux/regmap.h> |
973838a0 | 20 | #include <linux/regulator/consumer.h> |
2159ad93 | 21 | #include <linux/slab.h> |
7406bdbc | 22 | #include <linux/vmalloc.h> |
6ab2b7b4 | 23 | #include <linux/workqueue.h> |
f9f55e31 | 24 | #include <linux/debugfs.h> |
2159ad93 MB |
25 | #include <sound/core.h> |
26 | #include <sound/pcm.h> | |
27 | #include <sound/pcm_params.h> | |
28 | #include <sound/soc.h> | |
29 | #include <sound/jack.h> | |
30 | #include <sound/initval.h> | |
31 | #include <sound/tlv.h> | |
32 | ||
2159ad93 MB |
33 | #include "wm_adsp.h" |
34 | ||
35 | #define adsp_crit(_dsp, fmt, ...) \ | |
e1468202 | 36 | dev_crit(_dsp->cs_dsp.dev, "%s: " fmt, _dsp->cs_dsp.name, ##__VA_ARGS__) |
2159ad93 | 37 | #define adsp_err(_dsp, fmt, ...) \ |
e1468202 | 38 | dev_err(_dsp->cs_dsp.dev, "%s: " fmt, _dsp->cs_dsp.name, ##__VA_ARGS__) |
2159ad93 | 39 | #define adsp_warn(_dsp, fmt, ...) \ |
e1468202 | 40 | dev_warn(_dsp->cs_dsp.dev, "%s: " fmt, _dsp->cs_dsp.name, ##__VA_ARGS__) |
2159ad93 | 41 | #define adsp_info(_dsp, fmt, ...) \ |
e1468202 | 42 | dev_info(_dsp->cs_dsp.dev, "%s: " fmt, _dsp->cs_dsp.name, ##__VA_ARGS__) |
2159ad93 | 43 | #define adsp_dbg(_dsp, fmt, ...) \ |
e1468202 | 44 | dev_dbg(_dsp->cs_dsp.dev, "%s: " fmt, _dsp->cs_dsp.name, ##__VA_ARGS__) |
2159ad93 | 45 | |
0d3fba3e CK |
46 | #define compr_err(_obj, fmt, ...) \ |
47 | adsp_err(_obj->dsp, "%s: " fmt, _obj->name ? _obj->name : "legacy", \ | |
48 | ##__VA_ARGS__) | |
49 | #define compr_dbg(_obj, fmt, ...) \ | |
50 | adsp_dbg(_obj->dsp, "%s: " fmt, _obj->name ? _obj->name : "legacy", \ | |
51 | ##__VA_ARGS__) | |
52 | ||
9ee78757 CK |
53 | #define ADSP_MAX_STD_CTRL_SIZE 512 |
54 | ||
2dd04464 ST |
55 | static const struct cs_dsp_client_ops wm_adsp1_client_ops; |
56 | static const struct cs_dsp_client_ops wm_adsp2_client_ops; | |
57 | ||
04d1300f CK |
58 | #define WM_ADSP_FW_MBC_VSS 0 |
59 | #define WM_ADSP_FW_HIFI 1 | |
60 | #define WM_ADSP_FW_TX 2 | |
61 | #define WM_ADSP_FW_TX_SPK 3 | |
62 | #define WM_ADSP_FW_RX 4 | |
63 | #define WM_ADSP_FW_RX_ANC 5 | |
64 | #define WM_ADSP_FW_CTRL 6 | |
65 | #define WM_ADSP_FW_ASR 7 | |
66 | #define WM_ADSP_FW_TRACE 8 | |
67 | #define WM_ADSP_FW_SPK_PROT 9 | |
d6fea46e VK |
68 | #define WM_ADSP_FW_SPK_CALI 10 |
69 | #define WM_ADSP_FW_SPK_DIAG 11 | |
70 | #define WM_ADSP_FW_MISC 12 | |
04d1300f | 71 | |
d6fea46e | 72 | #define WM_ADSP_NUM_FW 13 |
dd84f925 | 73 | |
1023dbd9 | 74 | static const char *wm_adsp_fw_text[WM_ADSP_NUM_FW] = { |
04d1300f CK |
75 | [WM_ADSP_FW_MBC_VSS] = "MBC/VSS", |
76 | [WM_ADSP_FW_HIFI] = "MasterHiFi", | |
77 | [WM_ADSP_FW_TX] = "Tx", | |
78 | [WM_ADSP_FW_TX_SPK] = "Tx Speaker", | |
79 | [WM_ADSP_FW_RX] = "Rx", | |
80 | [WM_ADSP_FW_RX_ANC] = "Rx ANC", | |
81 | [WM_ADSP_FW_CTRL] = "Voice Ctrl", | |
82 | [WM_ADSP_FW_ASR] = "ASR Assist", | |
83 | [WM_ADSP_FW_TRACE] = "Dbg Trace", | |
84 | [WM_ADSP_FW_SPK_PROT] = "Protection", | |
d6fea46e VK |
85 | [WM_ADSP_FW_SPK_CALI] = "Calibration", |
86 | [WM_ADSP_FW_SPK_DIAG] = "Diagnostic", | |
04d1300f | 87 | [WM_ADSP_FW_MISC] = "Misc", |
1023dbd9 MB |
88 | }; |
89 | ||
2cd19bdb CK |
90 | struct wm_adsp_system_config_xm_hdr { |
91 | __be32 sys_enable; | |
92 | __be32 fw_id; | |
93 | __be32 fw_rev; | |
94 | __be32 boot_status; | |
95 | __be32 watchdog; | |
96 | __be32 dma_buffer_size; | |
97 | __be32 rdma[6]; | |
98 | __be32 wdma[8]; | |
99 | __be32 build_job_name[3]; | |
100 | __be32 build_job_number; | |
353bb6a5 | 101 | } __packed; |
2cd19bdb | 102 | |
170b1e12 WS |
103 | struct wm_halo_system_config_xm_hdr { |
104 | __be32 halo_heartbeat; | |
105 | __be32 build_job_name[3]; | |
106 | __be32 build_job_number; | |
353bb6a5 | 107 | } __packed; |
170b1e12 | 108 | |
2cd19bdb CK |
109 | struct wm_adsp_alg_xm_struct { |
110 | __be32 magic; | |
111 | __be32 smoothing; | |
112 | __be32 threshold; | |
113 | __be32 host_buf_ptr; | |
114 | __be32 start_seq; | |
115 | __be32 high_water_mark; | |
116 | __be32 low_water_mark; | |
117 | __be64 smoothed_power; | |
353bb6a5 | 118 | } __packed; |
2cd19bdb | 119 | |
4f2d4eab SH |
120 | struct wm_adsp_host_buf_coeff_v1 { |
121 | __be32 host_buf_ptr; /* Host buffer pointer */ | |
122 | __be32 versions; /* Version numbers */ | |
123 | __be32 name[4]; /* The buffer name */ | |
353bb6a5 | 124 | } __packed; |
4f2d4eab | 125 | |
2cd19bdb | 126 | struct wm_adsp_buffer { |
2a2aefa4 RF |
127 | __be32 buf1_base; /* Base addr of first buffer area */ |
128 | __be32 buf1_size; /* Size of buf1 area in DSP words */ | |
129 | __be32 buf2_base; /* Base addr of 2nd buffer area */ | |
130 | __be32 buf1_buf2_size; /* Size of buf1+buf2 in DSP words */ | |
131 | __be32 buf3_base; /* Base addr of buf3 area */ | |
132 | __be32 buf_total_size; /* Size of buf1+buf2+buf3 in DSP words */ | |
2cd19bdb CK |
133 | __be32 high_water_mark; /* Point at which IRQ is asserted */ |
134 | __be32 irq_count; /* bits 1-31 count IRQ assertions */ | |
135 | __be32 irq_ack; /* acked IRQ count, bit 0 enables IRQ */ | |
136 | __be32 next_write_index; /* word index of next write */ | |
137 | __be32 next_read_index; /* word index of next read */ | |
138 | __be32 error; /* error if any */ | |
139 | __be32 oldest_block_index; /* word index of oldest surviving */ | |
140 | __be32 requested_rewind; /* how many blocks rewind was done */ | |
141 | __be32 reserved_space; /* internal */ | |
142 | __be32 min_free; /* min free space since stream start */ | |
143 | __be32 blocks_written[2]; /* total blocks written (64 bit) */ | |
144 | __be32 words_written[2]; /* total words written (64 bit) */ | |
353bb6a5 | 145 | } __packed; |
2cd19bdb | 146 | |
721be3be CK |
147 | struct wm_adsp_compr; |
148 | ||
2cd19bdb | 149 | struct wm_adsp_compr_buf { |
4f2d4eab | 150 | struct list_head list; |
2cd19bdb | 151 | struct wm_adsp *dsp; |
721be3be | 152 | struct wm_adsp_compr *compr; |
2cd19bdb CK |
153 | |
154 | struct wm_adsp_buffer_region *regions; | |
155 | u32 host_buf_ptr; | |
565ace46 CK |
156 | |
157 | u32 error; | |
158 | u32 irq_count; | |
159 | int read_index; | |
160 | int avail; | |
fb13f19d | 161 | int host_buf_mem_type; |
4f2d4eab SH |
162 | |
163 | char *name; | |
2cd19bdb CK |
164 | }; |
165 | ||
406abc95 | 166 | struct wm_adsp_compr { |
4f2d4eab | 167 | struct list_head list; |
406abc95 | 168 | struct wm_adsp *dsp; |
95fe9597 | 169 | struct wm_adsp_compr_buf *buf; |
406abc95 CK |
170 | |
171 | struct snd_compr_stream *stream; | |
172 | struct snd_compressed_buffer size; | |
565ace46 | 173 | |
83a40ce9 | 174 | u32 *raw_buf; |
565ace46 | 175 | unsigned int copied_total; |
da2b3358 CK |
176 | |
177 | unsigned int sample_rate; | |
4f2d4eab SH |
178 | |
179 | const char *name; | |
406abc95 CK |
180 | }; |
181 | ||
406abc95 CK |
182 | #define WM_ADSP_MIN_FRAGMENTS 1 |
183 | #define WM_ADSP_MAX_FRAGMENTS 256 | |
c55b3e46 | 184 | #define WM_ADSP_MIN_FRAGMENT_SIZE (16 * CS_DSP_DATA_WORD_SIZE) |
5beb8eea | 185 | #define WM_ADSP_MAX_FRAGMENT_SIZE (4096 * CS_DSP_DATA_WORD_SIZE) |
406abc95 | 186 | |
2cd19bdb CK |
187 | #define WM_ADSP_ALG_XM_STRUCT_MAGIC 0x49aec7 |
188 | ||
189 | #define HOST_BUFFER_FIELD(field) \ | |
190 | (offsetof(struct wm_adsp_buffer, field) / sizeof(__be32)) | |
191 | ||
192 | #define ALG_XM_FIELD(field) \ | |
193 | (offsetof(struct wm_adsp_alg_xm_struct, field) / sizeof(__be32)) | |
194 | ||
4f2d4eab SH |
195 | #define HOST_BUF_COEFF_SUPPORTED_COMPAT_VER 1 |
196 | ||
197 | #define HOST_BUF_COEFF_COMPAT_VER_MASK 0xFF00 | |
198 | #define HOST_BUF_COEFF_COMPAT_VER_SHIFT 8 | |
199 | ||
2cd19bdb CK |
200 | static int wm_adsp_buffer_init(struct wm_adsp *dsp); |
201 | static int wm_adsp_buffer_free(struct wm_adsp *dsp); | |
202 | ||
203 | struct wm_adsp_buffer_region { | |
204 | unsigned int offset; | |
205 | unsigned int cumulative_size; | |
206 | unsigned int mem_type; | |
207 | unsigned int base_addr; | |
208 | }; | |
209 | ||
210 | struct wm_adsp_buffer_region_def { | |
211 | unsigned int mem_type; | |
212 | unsigned int base_offset; | |
213 | unsigned int size_offset; | |
214 | }; | |
215 | ||
3a9686c4 | 216 | static const struct wm_adsp_buffer_region_def default_regions[] = { |
2cd19bdb CK |
217 | { |
218 | .mem_type = WMFW_ADSP2_XM, | |
2a2aefa4 RF |
219 | .base_offset = HOST_BUFFER_FIELD(buf1_base), |
220 | .size_offset = HOST_BUFFER_FIELD(buf1_size), | |
2cd19bdb CK |
221 | }, |
222 | { | |
223 | .mem_type = WMFW_ADSP2_XM, | |
2a2aefa4 RF |
224 | .base_offset = HOST_BUFFER_FIELD(buf2_base), |
225 | .size_offset = HOST_BUFFER_FIELD(buf1_buf2_size), | |
2cd19bdb CK |
226 | }, |
227 | { | |
228 | .mem_type = WMFW_ADSP2_YM, | |
2a2aefa4 RF |
229 | .base_offset = HOST_BUFFER_FIELD(buf3_base), |
230 | .size_offset = HOST_BUFFER_FIELD(buf_total_size), | |
2cd19bdb CK |
231 | }, |
232 | }; | |
233 | ||
406abc95 CK |
234 | struct wm_adsp_fw_caps { |
235 | u32 id; | |
236 | struct snd_codec_desc desc; | |
2cd19bdb | 237 | int num_regions; |
3a9686c4 | 238 | const struct wm_adsp_buffer_region_def *region_defs; |
406abc95 CK |
239 | }; |
240 | ||
e6d00f34 | 241 | static const struct wm_adsp_fw_caps ctrl_caps[] = { |
406abc95 CK |
242 | { |
243 | .id = SND_AUDIOCODEC_BESPOKE, | |
244 | .desc = { | |
3bbc2705 | 245 | .max_ch = 8, |
406abc95 CK |
246 | .sample_rates = { 16000 }, |
247 | .num_sample_rates = 1, | |
248 | .formats = SNDRV_PCM_FMTBIT_S16_LE, | |
249 | }, | |
e6d00f34 CK |
250 | .num_regions = ARRAY_SIZE(default_regions), |
251 | .region_defs = default_regions, | |
406abc95 CK |
252 | }, |
253 | }; | |
254 | ||
7ce4283c CK |
255 | static const struct wm_adsp_fw_caps trace_caps[] = { |
256 | { | |
257 | .id = SND_AUDIOCODEC_BESPOKE, | |
258 | .desc = { | |
259 | .max_ch = 8, | |
260 | .sample_rates = { | |
261 | 4000, 8000, 11025, 12000, 16000, 22050, | |
262 | 24000, 32000, 44100, 48000, 64000, 88200, | |
263 | 96000, 176400, 192000 | |
264 | }, | |
265 | .num_sample_rates = 15, | |
266 | .formats = SNDRV_PCM_FMTBIT_S16_LE, | |
267 | }, | |
268 | .num_regions = ARRAY_SIZE(default_regions), | |
269 | .region_defs = default_regions, | |
406abc95 CK |
270 | }, |
271 | }; | |
272 | ||
273 | static const struct { | |
1023dbd9 | 274 | const char *file; |
406abc95 CK |
275 | int compr_direction; |
276 | int num_caps; | |
277 | const struct wm_adsp_fw_caps *caps; | |
20b7f7c5 | 278 | bool voice_trigger; |
1023dbd9 | 279 | } wm_adsp_fw[WM_ADSP_NUM_FW] = { |
04d1300f CK |
280 | [WM_ADSP_FW_MBC_VSS] = { .file = "mbc-vss" }, |
281 | [WM_ADSP_FW_HIFI] = { .file = "hifi" }, | |
282 | [WM_ADSP_FW_TX] = { .file = "tx" }, | |
283 | [WM_ADSP_FW_TX_SPK] = { .file = "tx-spk" }, | |
284 | [WM_ADSP_FW_RX] = { .file = "rx" }, | |
285 | [WM_ADSP_FW_RX_ANC] = { .file = "rx-anc" }, | |
406abc95 CK |
286 | [WM_ADSP_FW_CTRL] = { |
287 | .file = "ctrl", | |
288 | .compr_direction = SND_COMPRESS_CAPTURE, | |
e6d00f34 CK |
289 | .num_caps = ARRAY_SIZE(ctrl_caps), |
290 | .caps = ctrl_caps, | |
20b7f7c5 | 291 | .voice_trigger = true, |
406abc95 | 292 | }, |
04d1300f | 293 | [WM_ADSP_FW_ASR] = { .file = "asr" }, |
7ce4283c CK |
294 | [WM_ADSP_FW_TRACE] = { |
295 | .file = "trace", | |
296 | .compr_direction = SND_COMPRESS_CAPTURE, | |
297 | .num_caps = ARRAY_SIZE(trace_caps), | |
298 | .caps = trace_caps, | |
299 | }, | |
c55b3e46 VK |
300 | [WM_ADSP_FW_SPK_PROT] = { |
301 | .file = "spk-prot", | |
302 | .compr_direction = SND_COMPRESS_CAPTURE, | |
303 | .num_caps = ARRAY_SIZE(trace_caps), | |
304 | .caps = trace_caps, | |
305 | }, | |
d6fea46e VK |
306 | [WM_ADSP_FW_SPK_CALI] = { .file = "spk-cali" }, |
307 | [WM_ADSP_FW_SPK_DIAG] = { .file = "spk-diag" }, | |
04d1300f | 308 | [WM_ADSP_FW_MISC] = { .file = "misc" }, |
1023dbd9 MB |
309 | }; |
310 | ||
6ab2b7b4 DP |
311 | struct wm_coeff_ctl { |
312 | const char *name; | |
0700bc2f | 313 | struct cs_dsp_coeff_ctl *cs_ctl; |
9ee78757 | 314 | struct soc_bytes_ext bytes_ext; |
df6c505c | 315 | struct work_struct work; |
6ab2b7b4 DP |
316 | }; |
317 | ||
0a047f07 RF |
318 | int wm_adsp_fw_get(struct snd_kcontrol *kcontrol, |
319 | struct snd_ctl_elem_value *ucontrol) | |
1023dbd9 | 320 | { |
0fe1daa6 | 321 | struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); |
1023dbd9 | 322 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
0fe1daa6 | 323 | struct wm_adsp *dsp = snd_soc_component_get_drvdata(component); |
1023dbd9 | 324 | |
15c66570 | 325 | ucontrol->value.enumerated.item[0] = dsp[e->shift_l].fw; |
1023dbd9 MB |
326 | |
327 | return 0; | |
328 | } | |
0a047f07 | 329 | EXPORT_SYMBOL_GPL(wm_adsp_fw_get); |
1023dbd9 | 330 | |
0a047f07 RF |
331 | int wm_adsp_fw_put(struct snd_kcontrol *kcontrol, |
332 | struct snd_ctl_elem_value *ucontrol) | |
1023dbd9 | 333 | { |
0fe1daa6 | 334 | struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); |
1023dbd9 | 335 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
0fe1daa6 | 336 | struct wm_adsp *dsp = snd_soc_component_get_drvdata(component); |
2abdf9f8 | 337 | int ret = 1; |
1023dbd9 | 338 | |
15c66570 | 339 | if (ucontrol->value.enumerated.item[0] == dsp[e->shift_l].fw) |
1023dbd9 MB |
340 | return 0; |
341 | ||
15c66570 | 342 | if (ucontrol->value.enumerated.item[0] >= WM_ADSP_NUM_FW) |
1023dbd9 MB |
343 | return -EINVAL; |
344 | ||
e1468202 | 345 | mutex_lock(&dsp[e->shift_l].cs_dsp.pwr_lock); |
d27c5e15 | 346 | |
e1468202 | 347 | if (dsp[e->shift_l].cs_dsp.booted || !list_empty(&dsp[e->shift_l].compr_list)) |
d27c5e15 CK |
348 | ret = -EBUSY; |
349 | else | |
15c66570 | 350 | dsp[e->shift_l].fw = ucontrol->value.enumerated.item[0]; |
1023dbd9 | 351 | |
e1468202 | 352 | mutex_unlock(&dsp[e->shift_l].cs_dsp.pwr_lock); |
1023dbd9 | 353 | |
d27c5e15 | 354 | return ret; |
1023dbd9 | 355 | } |
0a047f07 | 356 | EXPORT_SYMBOL_GPL(wm_adsp_fw_put); |
1023dbd9 | 357 | |
0a047f07 | 358 | const struct soc_enum wm_adsp_fw_enum[] = { |
1023dbd9 MB |
359 | SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text), |
360 | SOC_ENUM_SINGLE(0, 1, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text), | |
361 | SOC_ENUM_SINGLE(0, 2, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text), | |
362 | SOC_ENUM_SINGLE(0, 3, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text), | |
e1ea1879 RF |
363 | SOC_ENUM_SINGLE(0, 4, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text), |
364 | SOC_ENUM_SINGLE(0, 5, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text), | |
365 | SOC_ENUM_SINGLE(0, 6, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text), | |
1023dbd9 | 366 | }; |
0a047f07 | 367 | EXPORT_SYMBOL_GPL(wm_adsp_fw_enum); |
2159ad93 | 368 | |
9ee78757 CK |
369 | static inline struct wm_coeff_ctl *bytes_ext_to_ctl(struct soc_bytes_ext *ext) |
370 | { | |
371 | return container_of(ext, struct wm_coeff_ctl, bytes_ext); | |
372 | } | |
373 | ||
7585a5b0 | 374 | static int wm_coeff_info(struct snd_kcontrol *kctl, |
6ab2b7b4 DP |
375 | struct snd_ctl_elem_info *uinfo) |
376 | { | |
9ee78757 CK |
377 | struct soc_bytes_ext *bytes_ext = |
378 | (struct soc_bytes_ext *)kctl->private_value; | |
379 | struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext); | |
0700bc2f | 380 | struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl; |
6ab2b7b4 | 381 | |
0700bc2f | 382 | switch (cs_ctl->type) { |
a23ebba8 RF |
383 | case WMFW_CTL_TYPE_ACKED: |
384 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
5beb8eea ST |
385 | uinfo->value.integer.min = CS_DSP_ACKED_CTL_MIN_VALUE; |
386 | uinfo->value.integer.max = CS_DSP_ACKED_CTL_MAX_VALUE; | |
a23ebba8 RF |
387 | uinfo->value.integer.step = 1; |
388 | uinfo->count = 1; | |
389 | break; | |
390 | default: | |
391 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES; | |
0700bc2f | 392 | uinfo->count = cs_ctl->len; |
a23ebba8 RF |
393 | break; |
394 | } | |
395 | ||
6ab2b7b4 DP |
396 | return 0; |
397 | } | |
398 | ||
7585a5b0 | 399 | static int wm_coeff_put(struct snd_kcontrol *kctl, |
6ab2b7b4 DP |
400 | struct snd_ctl_elem_value *ucontrol) |
401 | { | |
9ee78757 CK |
402 | struct soc_bytes_ext *bytes_ext = |
403 | (struct soc_bytes_ext *)kctl->private_value; | |
404 | struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext); | |
0700bc2f | 405 | struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl; |
6ab2b7b4 | 406 | char *p = ucontrol->value.bytes.data; |
168d10e7 | 407 | |
3802a996 | 408 | return cs_dsp_coeff_lock_and_write_ctrl(cs_ctl, 0, p, cs_ctl->len); |
6ab2b7b4 DP |
409 | } |
410 | ||
9ee78757 CK |
411 | static int wm_coeff_tlv_put(struct snd_kcontrol *kctl, |
412 | const unsigned int __user *bytes, unsigned int size) | |
413 | { | |
414 | struct soc_bytes_ext *bytes_ext = | |
415 | (struct soc_bytes_ext *)kctl->private_value; | |
416 | struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext); | |
0700bc2f | 417 | struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl; |
7406bdbc | 418 | void *scratch; |
9ee78757 CK |
419 | int ret = 0; |
420 | ||
7406bdbc ST |
421 | scratch = vmalloc(size); |
422 | if (!scratch) | |
423 | return -ENOMEM; | |
9ee78757 | 424 | |
3802a996 | 425 | if (copy_from_user(scratch, bytes, size)) |
9ee78757 | 426 | ret = -EFAULT; |
3802a996 ST |
427 | else |
428 | ret = cs_dsp_coeff_lock_and_write_ctrl(cs_ctl, 0, scratch, size); | |
429 | ||
7406bdbc | 430 | vfree(scratch); |
9ee78757 CK |
431 | |
432 | return ret; | |
433 | } | |
434 | ||
a23ebba8 RF |
435 | static int wm_coeff_put_acked(struct snd_kcontrol *kctl, |
436 | struct snd_ctl_elem_value *ucontrol) | |
437 | { | |
438 | struct soc_bytes_ext *bytes_ext = | |
439 | (struct soc_bytes_ext *)kctl->private_value; | |
440 | struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext); | |
0700bc2f | 441 | struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl; |
a23ebba8 RF |
442 | unsigned int val = ucontrol->value.integer.value[0]; |
443 | int ret; | |
444 | ||
445 | if (val == 0) | |
446 | return 0; /* 0 means no event */ | |
447 | ||
0700bc2f | 448 | mutex_lock(&cs_ctl->dsp->pwr_lock); |
a23ebba8 | 449 | |
edb1d6d7 | 450 | if (cs_ctl->enabled) |
0700bc2f | 451 | ret = cs_dsp_coeff_write_acked_control(cs_ctl, val); |
a23ebba8 RF |
452 | else |
453 | ret = -EPERM; | |
454 | ||
0700bc2f | 455 | mutex_unlock(&cs_ctl->dsp->pwr_lock); |
a23ebba8 | 456 | |
7406bdbc ST |
457 | if (ret < 0) |
458 | return ret; | |
459 | ||
460 | return 1; | |
a23ebba8 RF |
461 | } |
462 | ||
f6bc909e ST |
463 | static int wm_coeff_get(struct snd_kcontrol *kctl, |
464 | struct snd_ctl_elem_value *ucontrol) | |
6ab2b7b4 | 465 | { |
f6bc909e ST |
466 | struct soc_bytes_ext *bytes_ext = |
467 | (struct soc_bytes_ext *)kctl->private_value; | |
468 | struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext); | |
469 | struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl; | |
470 | char *p = ucontrol->value.bytes.data; | |
73ecf1a6 | 471 | |
3802a996 | 472 | return cs_dsp_coeff_lock_and_read_ctrl(cs_ctl, 0, p, cs_ctl->len); |
6ab2b7b4 DP |
473 | } |
474 | ||
9ee78757 CK |
475 | static int wm_coeff_tlv_get(struct snd_kcontrol *kctl, |
476 | unsigned int __user *bytes, unsigned int size) | |
477 | { | |
478 | struct soc_bytes_ext *bytes_ext = | |
479 | (struct soc_bytes_ext *)kctl->private_value; | |
480 | struct wm_coeff_ctl *ctl = bytes_ext_to_ctl(bytes_ext); | |
0700bc2f | 481 | struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl; |
9ee78757 CK |
482 | int ret = 0; |
483 | ||
0700bc2f | 484 | mutex_lock(&cs_ctl->dsp->pwr_lock); |
9ee78757 | 485 | |
f444da38 | 486 | ret = cs_dsp_coeff_read_ctrl(cs_ctl, 0, cs_ctl->cache, size); |
9ee78757 | 487 | |
0700bc2f | 488 | if (!ret && copy_to_user(bytes, cs_ctl->cache, size)) |
9ee78757 CK |
489 | ret = -EFAULT; |
490 | ||
0700bc2f | 491 | mutex_unlock(&cs_ctl->dsp->pwr_lock); |
9ee78757 CK |
492 | |
493 | return ret; | |
494 | } | |
495 | ||
a23ebba8 RF |
496 | static int wm_coeff_get_acked(struct snd_kcontrol *kcontrol, |
497 | struct snd_ctl_elem_value *ucontrol) | |
498 | { | |
499 | /* | |
500 | * Although it's not useful to read an acked control, we must satisfy | |
501 | * user-side assumptions that all controls are readable and that a | |
502 | * write of the same value should be filtered out (it's valid to send | |
503 | * the same event number again to the firmware). We therefore return 0, | |
504 | * meaning "no event" so valid event numbers will always be a change | |
505 | */ | |
506 | ucontrol->value.integer.value[0] = 0; | |
507 | ||
508 | return 0; | |
509 | } | |
510 | ||
9ee78757 CK |
511 | static unsigned int wmfw_convert_flags(unsigned int in, unsigned int len) |
512 | { | |
513 | unsigned int out, rd, wr, vol; | |
514 | ||
515 | if (len > ADSP_MAX_STD_CTRL_SIZE) { | |
516 | rd = SNDRV_CTL_ELEM_ACCESS_TLV_READ; | |
517 | wr = SNDRV_CTL_ELEM_ACCESS_TLV_WRITE; | |
518 | vol = SNDRV_CTL_ELEM_ACCESS_VOLATILE; | |
519 | ||
520 | out = SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK; | |
521 | } else { | |
522 | rd = SNDRV_CTL_ELEM_ACCESS_READ; | |
523 | wr = SNDRV_CTL_ELEM_ACCESS_WRITE; | |
524 | vol = SNDRV_CTL_ELEM_ACCESS_VOLATILE; | |
525 | ||
526 | out = 0; | |
527 | } | |
528 | ||
529 | if (in) { | |
3ae7359c | 530 | out |= rd; |
9ee78757 CK |
531 | if (in & WMFW_CTL_FLAG_WRITEABLE) |
532 | out |= wr; | |
533 | if (in & WMFW_CTL_FLAG_VOLATILE) | |
534 | out |= vol; | |
535 | } else { | |
536 | out |= rd | wr | vol; | |
537 | } | |
538 | ||
539 | return out; | |
540 | } | |
541 | ||
56717d72 | 542 | static void wm_adsp_ctl_work(struct work_struct *work) |
6ab2b7b4 | 543 | { |
56717d72 CK |
544 | struct wm_coeff_ctl *ctl = container_of(work, |
545 | struct wm_coeff_ctl, | |
546 | work); | |
0700bc2f | 547 | struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl; |
56717d72 CK |
548 | struct wm_adsp *dsp = container_of(cs_ctl->dsp, |
549 | struct wm_adsp, | |
550 | cs_dsp); | |
6ab2b7b4 | 551 | struct snd_kcontrol_new *kcontrol; |
6ab2b7b4 | 552 | |
6ab2b7b4 DP |
553 | kcontrol = kzalloc(sizeof(*kcontrol), GFP_KERNEL); |
554 | if (!kcontrol) | |
56717d72 | 555 | return; |
6ab2b7b4 DP |
556 | |
557 | kcontrol->name = ctl->name; | |
558 | kcontrol->info = wm_coeff_info; | |
9ee78757 CK |
559 | kcontrol->iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
560 | kcontrol->tlv.c = snd_soc_bytes_tlv_callback; | |
561 | kcontrol->private_value = (unsigned long)&ctl->bytes_ext; | |
0700bc2f | 562 | kcontrol->access = wmfw_convert_flags(cs_ctl->flags, cs_ctl->len); |
6ab2b7b4 | 563 | |
0700bc2f | 564 | switch (cs_ctl->type) { |
a23ebba8 RF |
565 | case WMFW_CTL_TYPE_ACKED: |
566 | kcontrol->get = wm_coeff_get_acked; | |
567 | kcontrol->put = wm_coeff_put_acked; | |
568 | break; | |
569 | default: | |
d7789f5b | 570 | if (kcontrol->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { |
0700bc2f | 571 | ctl->bytes_ext.max = cs_ctl->len; |
d7789f5b RF |
572 | ctl->bytes_ext.get = wm_coeff_tlv_get; |
573 | ctl->bytes_ext.put = wm_coeff_tlv_put; | |
574 | } else { | |
575 | kcontrol->get = wm_coeff_get; | |
576 | kcontrol->put = wm_coeff_put; | |
577 | } | |
a23ebba8 RF |
578 | break; |
579 | } | |
26c22a19 | 580 | |
56717d72 | 581 | snd_soc_add_component_controls(dsp->component, kcontrol, 1); |
6ab2b7b4 | 582 | |
6ab2b7b4 | 583 | kfree(kcontrol); |
b21acc1c CK |
584 | } |
585 | ||
45b4acab | 586 | int wm_adsp_control_add(struct cs_dsp_coeff_ctl *cs_ctl) |
b21acc1c | 587 | { |
e1468202 ST |
588 | struct wm_adsp *dsp = container_of(cs_ctl->dsp, struct wm_adsp, cs_dsp); |
589 | struct cs_dsp *cs_dsp = &dsp->cs_dsp; | |
b21acc1c | 590 | struct wm_coeff_ctl *ctl; |
b21acc1c | 591 | char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; |
9ce5e6e6 | 592 | const char *region_name; |
b21acc1c CK |
593 | int ret; |
594 | ||
0700bc2f ST |
595 | if (cs_ctl->flags & WMFW_CTL_FLAG_SYS) |
596 | return 0; | |
64779607 | 597 | |
0700bc2f | 598 | region_name = cs_dsp_mem_region_name(cs_ctl->alg_region.type); |
9ce5e6e6 | 599 | if (!region_name) { |
0700bc2f | 600 | adsp_err(dsp, "Unknown region type: %d\n", cs_ctl->alg_region.type); |
b21acc1c CK |
601 | return -EINVAL; |
602 | } | |
603 | ||
dc0e5ca8 | 604 | switch (cs_dsp->wmfw_ver) { |
cb5b57a9 CK |
605 | case 0: |
606 | case 1: | |
a6e849d0 ST |
607 | ret = scnprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, |
608 | "%s %s %x", cs_dsp->name, region_name, | |
609 | cs_ctl->alg_region.alg); | |
cb5b57a9 | 610 | break; |
170b1e12 | 611 | case 2: |
57819429 | 612 | ret = scnprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, |
e1468202 | 613 | "%s%c %.12s %x", cs_dsp->name, *region_name, |
0700bc2f | 614 | wm_adsp_fw_text[dsp->fw], cs_ctl->alg_region.alg); |
170b1e12 WS |
615 | break; |
616 | default: | |
57819429 | 617 | ret = scnprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, |
e1468202 | 618 | "%s %.12s %x", cs_dsp->name, |
0700bc2f | 619 | wm_adsp_fw_text[dsp->fw], cs_ctl->alg_region.alg); |
170b1e12 WS |
620 | break; |
621 | } | |
cb5b57a9 | 622 | |
0700bc2f | 623 | if (cs_ctl->subname) { |
170b1e12 WS |
624 | int avail = SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret - 2; |
625 | int skip = 0; | |
cb5b57a9 | 626 | |
170b1e12 WS |
627 | if (dsp->component->name_prefix) |
628 | avail -= strlen(dsp->component->name_prefix) + 1; | |
b7ede5af | 629 | |
170b1e12 | 630 | /* Truncate the subname from the start if it is too long */ |
0700bc2f ST |
631 | if (cs_ctl->subname_len > avail) |
632 | skip = cs_ctl->subname_len - avail; | |
cb5b57a9 | 633 | |
170b1e12 | 634 | snprintf(name + ret, SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret, |
0700bc2f | 635 | " %.*s", cs_ctl->subname_len - skip, cs_ctl->subname + skip); |
cb5b57a9 | 636 | } |
b21acc1c | 637 | |
b21acc1c CK |
638 | ctl = kzalloc(sizeof(*ctl), GFP_KERNEL); |
639 | if (!ctl) | |
640 | return -ENOMEM; | |
0700bc2f ST |
641 | ctl->cs_ctl = cs_ctl; |
642 | ||
b21acc1c CK |
643 | ctl->name = kmemdup(name, strlen(name) + 1, GFP_KERNEL); |
644 | if (!ctl->name) { | |
645 | ret = -ENOMEM; | |
646 | goto err_ctl; | |
647 | } | |
0700bc2f ST |
648 | |
649 | cs_ctl->priv = ctl; | |
650 | ||
651 | INIT_WORK(&ctl->work, wm_adsp_ctl_work); | |
652 | schedule_work(&ctl->work); | |
653 | ||
654 | return 0; | |
655 | ||
656 | err_ctl: | |
657 | kfree(ctl); | |
658 | ||
659 | return ret; | |
660 | } | |
45b4acab ST |
661 | EXPORT_SYMBOL_GPL(wm_adsp_control_add); |
662 | ||
663 | static int wm_adsp_control_add_cb(struct cs_dsp_coeff_ctl *cs_ctl) | |
664 | { | |
665 | struct wm_adsp *dsp = container_of(cs_ctl->dsp, struct wm_adsp, cs_dsp); | |
666 | ||
667 | if (dsp->control_add) | |
668 | return (dsp->control_add)(dsp, cs_ctl); | |
669 | else | |
670 | return wm_adsp_control_add(cs_ctl); | |
671 | } | |
0700bc2f ST |
672 | |
673 | static void wm_adsp_control_remove(struct cs_dsp_coeff_ctl *cs_ctl) | |
674 | { | |
675 | struct wm_coeff_ctl *ctl = cs_ctl->priv; | |
676 | ||
677 | cancel_work_sync(&ctl->work); | |
678 | ||
679 | kfree(ctl->name); | |
680 | kfree(ctl); | |
681 | } | |
682 | ||
f6bc909e ST |
683 | int wm_adsp_write_ctl(struct wm_adsp *dsp, const char *name, int type, |
684 | unsigned int alg, void *buf, size_t len) | |
0700bc2f | 685 | { |
f193957b | 686 | struct cs_dsp_coeff_ctl *cs_ctl; |
0700bc2f ST |
687 | int ret; |
688 | ||
781118bc | 689 | mutex_lock(&dsp->cs_dsp.pwr_lock); |
f193957b | 690 | cs_ctl = cs_dsp_get_ctl(&dsp->cs_dsp, name, type, alg); |
f444da38 | 691 | ret = cs_dsp_coeff_write_ctrl(cs_ctl, 0, buf, len); |
781118bc RF |
692 | mutex_unlock(&dsp->cs_dsp.pwr_lock); |
693 | ||
7406bdbc | 694 | if (ret < 0) |
f6bc909e | 695 | return ret; |
cb5b57a9 | 696 | |
e81f5c9f | 697 | return 0; |
2323736d | 698 | } |
f6bc909e | 699 | EXPORT_SYMBOL_GPL(wm_adsp_write_ctl); |
2323736d | 700 | |
f6bc909e ST |
701 | int wm_adsp_read_ctl(struct wm_adsp *dsp, const char *name, int type, |
702 | unsigned int alg, void *buf, size_t len) | |
4e08d50d | 703 | { |
781118bc RF |
704 | int ret; |
705 | ||
706 | mutex_lock(&dsp->cs_dsp.pwr_lock); | |
707 | ret = cs_dsp_coeff_read_ctrl(cs_dsp_get_ctl(&dsp->cs_dsp, name, type, alg), | |
708 | 0, buf, len); | |
709 | mutex_unlock(&dsp->cs_dsp.pwr_lock); | |
710 | ||
711 | return ret; | |
4e08d50d | 712 | } |
f6bc909e | 713 | EXPORT_SYMBOL_GPL(wm_adsp_read_ctl); |
4e08d50d | 714 | |
a828056f ST |
715 | static void wm_adsp_release_firmware_files(struct wm_adsp *dsp, |
716 | const struct firmware *wmfw_firmware, | |
717 | char *wmfw_filename, | |
718 | const struct firmware *coeff_firmware, | |
719 | char *coeff_filename) | |
720 | { | |
721 | if (wmfw_firmware) | |
722 | release_firmware(wmfw_firmware); | |
723 | kfree(wmfw_filename); | |
724 | ||
725 | if (coeff_firmware) | |
726 | release_firmware(coeff_firmware); | |
727 | kfree(coeff_filename); | |
728 | } | |
729 | ||
730 | static int wm_adsp_request_firmware_file(struct wm_adsp *dsp, | |
b6b62d94 ST |
731 | const struct firmware **firmware, char **filename, |
732 | const char *dir, const char *system_name, | |
733 | const char *asoc_component_prefix, | |
734 | const char *filetype) | |
a828056f | 735 | { |
e1468202 | 736 | struct cs_dsp *cs_dsp = &dsp->cs_dsp; |
daf3f0f9 | 737 | const char *fwf; |
b6b62d94 | 738 | char *s, c; |
a828056f ST |
739 | int ret = 0; |
740 | ||
daf3f0f9 RF |
741 | if (dsp->fwf_name) |
742 | fwf = dsp->fwf_name; | |
743 | else | |
744 | fwf = dsp->cs_dsp.name; | |
745 | ||
b6b62d94 ST |
746 | if (system_name && asoc_component_prefix) |
747 | *filename = kasprintf(GFP_KERNEL, "%s%s-%s-%s-%s-%s.%s", dir, dsp->part, | |
daf3f0f9 | 748 | fwf, wm_adsp_fw[dsp->fw].file, system_name, |
b6b62d94 ST |
749 | asoc_component_prefix, filetype); |
750 | else if (system_name) | |
751 | *filename = kasprintf(GFP_KERNEL, "%s%s-%s-%s-%s.%s", dir, dsp->part, | |
daf3f0f9 | 752 | fwf, wm_adsp_fw[dsp->fw].file, system_name, |
b6b62d94 ST |
753 | filetype); |
754 | else | |
daf3f0f9 | 755 | *filename = kasprintf(GFP_KERNEL, "%s%s-%s-%s.%s", dir, dsp->part, fwf, |
b6b62d94 ST |
756 | wm_adsp_fw[dsp->fw].file, filetype); |
757 | ||
a828056f ST |
758 | if (*filename == NULL) |
759 | return -ENOMEM; | |
760 | ||
b6b62d94 ST |
761 | /* |
762 | * Make sure that filename is lower-case and any non alpha-numeric | |
763 | * characters except full stop and forward slash are replaced with | |
764 | * hyphens. | |
765 | */ | |
766 | s = *filename; | |
767 | while (*s) { | |
768 | c = *s; | |
769 | if (isalnum(c)) | |
770 | *s = tolower(c); | |
771 | else if ((c != '.') && (c != '/')) | |
772 | *s = '-'; | |
773 | s++; | |
774 | } | |
775 | ||
776 | ret = firmware_request_nowarn(firmware, *filename, cs_dsp->dev); | |
a828056f | 777 | if (ret != 0) { |
b6b62d94 | 778 | adsp_dbg(dsp, "Failed to request '%s'\n", *filename); |
a828056f ST |
779 | kfree(*filename); |
780 | *filename = NULL; | |
991b1de8 ST |
781 | } else { |
782 | adsp_dbg(dsp, "Found '%s'\n", *filename); | |
a828056f ST |
783 | } |
784 | ||
785 | return ret; | |
786 | } | |
787 | ||
b6b62d94 | 788 | static const char *cirrus_dir = "cirrus/"; |
a828056f ST |
789 | static int wm_adsp_request_firmware_files(struct wm_adsp *dsp, |
790 | const struct firmware **wmfw_firmware, | |
791 | char **wmfw_filename, | |
792 | const struct firmware **coeff_firmware, | |
793 | char **coeff_filename) | |
794 | { | |
b6b62d94 ST |
795 | const char *system_name = dsp->system_name; |
796 | const char *asoc_component_prefix = dsp->component->name_prefix; | |
a828056f ST |
797 | int ret = 0; |
798 | ||
b6b62d94 ST |
799 | if (system_name && asoc_component_prefix) { |
800 | if (!wm_adsp_request_firmware_file(dsp, wmfw_firmware, wmfw_filename, | |
801 | cirrus_dir, system_name, | |
802 | asoc_component_prefix, "wmfw")) { | |
b6b62d94 ST |
803 | wm_adsp_request_firmware_file(dsp, coeff_firmware, coeff_filename, |
804 | cirrus_dir, system_name, | |
805 | asoc_component_prefix, "bin"); | |
806 | return 0; | |
807 | } | |
808 | } | |
a828056f | 809 | |
b6b62d94 ST |
810 | if (system_name) { |
811 | if (!wm_adsp_request_firmware_file(dsp, wmfw_firmware, wmfw_filename, | |
812 | cirrus_dir, system_name, | |
813 | NULL, "wmfw")) { | |
b6b62d94 ST |
814 | if (asoc_component_prefix) |
815 | wm_adsp_request_firmware_file(dsp, coeff_firmware, coeff_filename, | |
816 | cirrus_dir, system_name, | |
817 | asoc_component_prefix, "bin"); | |
818 | ||
819 | if (!*coeff_firmware) | |
820 | wm_adsp_request_firmware_file(dsp, coeff_firmware, coeff_filename, | |
821 | cirrus_dir, system_name, | |
822 | NULL, "bin"); | |
823 | return 0; | |
824 | } | |
825 | } | |
a828056f | 826 | |
3657e4cb RF |
827 | /* Check system-specific bin without wmfw before falling back to generic */ |
828 | if (dsp->wmfw_optional && system_name) { | |
829 | if (asoc_component_prefix) | |
830 | wm_adsp_request_firmware_file(dsp, coeff_firmware, coeff_filename, | |
831 | cirrus_dir, system_name, | |
832 | asoc_component_prefix, "bin"); | |
833 | ||
834 | if (!*coeff_firmware) | |
835 | wm_adsp_request_firmware_file(dsp, coeff_firmware, coeff_filename, | |
836 | cirrus_dir, system_name, | |
837 | NULL, "bin"); | |
838 | ||
839 | if (*coeff_firmware) | |
840 | return 0; | |
841 | } | |
842 | ||
843 | /* Check legacy location */ | |
b6b62d94 ST |
844 | if (!wm_adsp_request_firmware_file(dsp, wmfw_firmware, wmfw_filename, |
845 | "", NULL, NULL, "wmfw")) { | |
b6b62d94 ST |
846 | wm_adsp_request_firmware_file(dsp, coeff_firmware, coeff_filename, |
847 | "", NULL, NULL, "bin"); | |
848 | return 0; | |
849 | } | |
850 | ||
3657e4cb | 851 | /* Fall back to generic wmfw and optional matching bin */ |
b6b62d94 ST |
852 | ret = wm_adsp_request_firmware_file(dsp, wmfw_firmware, wmfw_filename, |
853 | cirrus_dir, NULL, NULL, "wmfw"); | |
3657e4cb | 854 | if (!ret || dsp->wmfw_optional) { |
b6b62d94 ST |
855 | wm_adsp_request_firmware_file(dsp, coeff_firmware, coeff_filename, |
856 | cirrus_dir, NULL, NULL, "bin"); | |
857 | return 0; | |
858 | } | |
859 | ||
860 | adsp_err(dsp, "Failed to request firmware <%s>%s-%s-%s<-%s<%s>>.wmfw\n", | |
daf3f0f9 RF |
861 | cirrus_dir, dsp->part, |
862 | dsp->fwf_name ? dsp->fwf_name : dsp->cs_dsp.name, | |
863 | wm_adsp_fw[dsp->fw].file, system_name, asoc_component_prefix); | |
b6b62d94 ST |
864 | |
865 | return -ENOENT; | |
a828056f ST |
866 | } |
867 | ||
f6bc909e | 868 | static int wm_adsp_common_init(struct wm_adsp *dsp) |
4e08d50d | 869 | { |
f6bc909e ST |
870 | INIT_LIST_HEAD(&dsp->compr_list); |
871 | INIT_LIST_HEAD(&dsp->buffer_list); | |
4e08d50d | 872 | |
f6bc909e | 873 | return 0; |
170b1e12 WS |
874 | } |
875 | ||
f6bc909e | 876 | int wm_adsp1_init(struct wm_adsp *dsp) |
2159ad93 | 877 | { |
f6bc909e | 878 | int ret; |
2159ad93 | 879 | |
f6bc909e | 880 | dsp->cs_dsp.client_ops = &wm_adsp1_client_ops; |
2159ad93 | 881 | |
f6bc909e ST |
882 | ret = cs_dsp_adsp1_init(&dsp->cs_dsp); |
883 | if (ret) | |
884 | return ret; | |
2159ad93 | 885 | |
f6bc909e ST |
886 | return wm_adsp_common_init(dsp); |
887 | } | |
888 | EXPORT_SYMBOL_GPL(wm_adsp1_init); | |
2159ad93 | 889 | |
f6bc909e ST |
890 | int wm_adsp1_event(struct snd_soc_dapm_widget *w, |
891 | struct snd_kcontrol *kcontrol, | |
892 | int event) | |
893 | { | |
894 | struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); | |
895 | struct wm_adsp *dsps = snd_soc_component_get_drvdata(component); | |
896 | struct wm_adsp *dsp = &dsps[w->shift]; | |
897 | int ret = 0; | |
898 | char *wmfw_filename = NULL; | |
899 | const struct firmware *wmfw_firmware = NULL; | |
900 | char *coeff_filename = NULL; | |
901 | const struct firmware *coeff_firmware = NULL; | |
2159ad93 | 902 | |
f6bc909e | 903 | dsp->component = component; |
186152df ST |
904 | |
905 | switch (event) { | |
906 | case SND_SOC_DAPM_POST_PMU: | |
a828056f ST |
907 | ret = wm_adsp_request_firmware_files(dsp, |
908 | &wmfw_firmware, &wmfw_filename, | |
909 | &coeff_firmware, &coeff_filename); | |
f6bc909e ST |
910 | if (ret) |
911 | break; | |
170b1e12 | 912 | |
f6bc909e ST |
913 | ret = cs_dsp_adsp1_power_up(&dsp->cs_dsp, |
914 | wmfw_firmware, wmfw_filename, | |
915 | coeff_firmware, coeff_filename, | |
916 | wm_adsp_fw_text[dsp->fw]); | |
d82d767f | 917 | |
f6bc909e ST |
918 | wm_adsp_release_firmware_files(dsp, |
919 | wmfw_firmware, wmfw_filename, | |
920 | coeff_firmware, coeff_filename); | |
921 | break; | |
922 | case SND_SOC_DAPM_PRE_PMD: | |
923 | cs_dsp_adsp1_power_down(&dsp->cs_dsp); | |
924 | break; | |
925 | default: | |
926 | break; | |
927 | } | |
b9070df4 RF |
928 | |
929 | return ret; | |
d82d767f | 930 | } |
f6bc909e | 931 | EXPORT_SYMBOL_GPL(wm_adsp1_event); |
25ca837b ST |
932 | |
933 | int wm_adsp2_set_dspclk(struct snd_soc_dapm_widget *w, unsigned int freq) | |
934 | { | |
935 | struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); | |
936 | struct wm_adsp *dsps = snd_soc_component_get_drvdata(component); | |
937 | struct wm_adsp *dsp = &dsps[w->shift]; | |
938 | ||
e1468202 | 939 | return cs_dsp_set_dspclk(&dsp->cs_dsp, freq); |
25ca837b | 940 | } |
b9070df4 | 941 | EXPORT_SYMBOL_GPL(wm_adsp2_set_dspclk); |
d82d767f | 942 | |
af813a6f CK |
943 | int wm_adsp2_preloader_get(struct snd_kcontrol *kcontrol, |
944 | struct snd_ctl_elem_value *ucontrol) | |
945 | { | |
0fe1daa6 | 946 | struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); |
b1470d4c AP |
947 | struct wm_adsp *dsps = snd_soc_component_get_drvdata(component); |
948 | struct soc_mixer_control *mc = | |
949 | (struct soc_mixer_control *)kcontrol->private_value; | |
950 | struct wm_adsp *dsp = &dsps[mc->shift - 1]; | |
af813a6f CK |
951 | |
952 | ucontrol->value.integer.value[0] = dsp->preloaded; | |
953 | ||
954 | return 0; | |
955 | } | |
956 | EXPORT_SYMBOL_GPL(wm_adsp2_preloader_get); | |
957 | ||
958 | int wm_adsp2_preloader_put(struct snd_kcontrol *kcontrol, | |
959 | struct snd_ctl_elem_value *ucontrol) | |
960 | { | |
0fe1daa6 | 961 | struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); |
b1470d4c | 962 | struct wm_adsp *dsps = snd_soc_component_get_drvdata(component); |
0fe1daa6 | 963 | struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); |
af813a6f CK |
964 | struct soc_mixer_control *mc = |
965 | (struct soc_mixer_control *)kcontrol->private_value; | |
b1470d4c | 966 | struct wm_adsp *dsp = &dsps[mc->shift - 1]; |
af813a6f CK |
967 | char preload[32]; |
968 | ||
ba235634 CK |
969 | if (dsp->preloaded == ucontrol->value.integer.value[0]) |
970 | return 0; | |
af813a6f | 971 | |
ba235634 | 972 | snprintf(preload, ARRAY_SIZE(preload), "%s Preload", dsp->cs_dsp.name); |
af813a6f | 973 | |
ba235634 | 974 | if (ucontrol->value.integer.value[0] || dsp->toggle_preload) |
95a594d0 | 975 | snd_soc_component_force_enable_pin(component, preload); |
af813a6f | 976 | else |
95a594d0 | 977 | snd_soc_component_disable_pin(component, preload); |
af813a6f CK |
978 | |
979 | snd_soc_dapm_sync(dapm); | |
980 | ||
868e49a4 SH |
981 | flush_work(&dsp->boot_work); |
982 | ||
ba235634 CK |
983 | dsp->preloaded = ucontrol->value.integer.value[0]; |
984 | ||
985 | if (dsp->toggle_preload) { | |
986 | snd_soc_component_disable_pin(component, preload); | |
987 | snd_soc_dapm_sync(dapm); | |
988 | } | |
989 | ||
81d74dda | 990 | return 1; |
af813a6f CK |
991 | } |
992 | EXPORT_SYMBOL_GPL(wm_adsp2_preloader_put); | |
993 | ||
62ddad42 | 994 | int wm_adsp_power_up(struct wm_adsp *dsp, bool load_firmware) |
186152df | 995 | { |
a828056f ST |
996 | int ret = 0; |
997 | char *wmfw_filename = NULL; | |
998 | const struct firmware *wmfw_firmware = NULL; | |
999 | char *coeff_filename = NULL; | |
1000 | const struct firmware *coeff_firmware = NULL; | |
1001 | ||
62ddad42 RF |
1002 | if (load_firmware) { |
1003 | ret = wm_adsp_request_firmware_files(dsp, | |
1004 | &wmfw_firmware, &wmfw_filename, | |
1005 | &coeff_firmware, &coeff_filename); | |
1006 | if (ret) | |
1007 | return ret; | |
1008 | } | |
186152df | 1009 | |
11520b87 ST |
1010 | ret = cs_dsp_power_up(&dsp->cs_dsp, |
1011 | wmfw_firmware, wmfw_filename, | |
1012 | coeff_firmware, coeff_filename, | |
1013 | wm_adsp_fw_text[dsp->fw]); | |
a828056f ST |
1014 | |
1015 | wm_adsp_release_firmware_files(dsp, | |
1016 | wmfw_firmware, wmfw_filename, | |
1017 | coeff_firmware, coeff_filename); | |
11520b87 ST |
1018 | |
1019 | return ret; | |
1020 | } | |
1021 | EXPORT_SYMBOL_GPL(wm_adsp_power_up); | |
1022 | ||
d0a3a6ad ST |
1023 | void wm_adsp_power_down(struct wm_adsp *dsp) |
1024 | { | |
1025 | cs_dsp_power_down(&dsp->cs_dsp); | |
1026 | } | |
1027 | EXPORT_SYMBOL_GPL(wm_adsp_power_down); | |
1028 | ||
11520b87 ST |
1029 | static void wm_adsp_boot_work(struct work_struct *work) |
1030 | { | |
1031 | struct wm_adsp *dsp = container_of(work, | |
1032 | struct wm_adsp, | |
1033 | boot_work); | |
1034 | ||
62ddad42 | 1035 | wm_adsp_power_up(dsp, true); |
186152df ST |
1036 | } |
1037 | ||
4e08d50d CK |
1038 | int wm_adsp_early_event(struct snd_soc_dapm_widget *w, |
1039 | struct snd_kcontrol *kcontrol, int event) | |
12db5edd | 1040 | { |
0fe1daa6 KM |
1041 | struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); |
1042 | struct wm_adsp *dsps = snd_soc_component_get_drvdata(component); | |
12db5edd CK |
1043 | struct wm_adsp *dsp = &dsps[w->shift]; |
1044 | ||
12db5edd CK |
1045 | switch (event) { |
1046 | case SND_SOC_DAPM_PRE_PMU: | |
1047 | queue_work(system_unbound_wq, &dsp->boot_work); | |
1048 | break; | |
57a60cc3 | 1049 | case SND_SOC_DAPM_PRE_PMD: |
d0a3a6ad | 1050 | wm_adsp_power_down(dsp); |
57a60cc3 | 1051 | break; |
12db5edd CK |
1052 | default: |
1053 | break; | |
cab27258 | 1054 | } |
12db5edd CK |
1055 | |
1056 | return 0; | |
1057 | } | |
4e08d50d | 1058 | EXPORT_SYMBOL_GPL(wm_adsp_early_event); |
12db5edd | 1059 | |
fe071308 RF |
1060 | static int wm_adsp_pre_run(struct cs_dsp *cs_dsp) |
1061 | { | |
1062 | struct wm_adsp *dsp = container_of(cs_dsp, struct wm_adsp, cs_dsp); | |
1063 | ||
1064 | if (!dsp->pre_run) | |
1065 | return 0; | |
1066 | ||
1067 | return (*dsp->pre_run)(dsp); | |
1068 | } | |
1069 | ||
e1468202 | 1070 | static int wm_adsp_event_post_run(struct cs_dsp *cs_dsp) |
186152df | 1071 | { |
e1468202 ST |
1072 | struct wm_adsp *dsp = container_of(cs_dsp, struct wm_adsp, cs_dsp); |
1073 | ||
186152df ST |
1074 | if (wm_adsp_fw[dsp->fw].num_caps != 0) |
1075 | return wm_adsp_buffer_init(dsp); | |
1076 | ||
1077 | return 0; | |
1078 | } | |
1079 | ||
e1468202 | 1080 | static void wm_adsp_event_post_stop(struct cs_dsp *cs_dsp) |
186152df | 1081 | { |
e1468202 ST |
1082 | struct wm_adsp *dsp = container_of(cs_dsp, struct wm_adsp, cs_dsp); |
1083 | ||
186152df ST |
1084 | if (wm_adsp_fw[dsp->fw].num_caps != 0) |
1085 | wm_adsp_buffer_free(dsp); | |
1086 | ||
1087 | dsp->fatal_error = false; | |
1088 | } | |
1089 | ||
5519ac3a RF |
1090 | int wm_adsp_run(struct wm_adsp *dsp) |
1091 | { | |
1092 | flush_work(&dsp->boot_work); | |
1093 | ||
1094 | return cs_dsp_run(&dsp->cs_dsp); | |
1095 | } | |
1096 | EXPORT_SYMBOL_GPL(wm_adsp_run); | |
1097 | ||
1098 | void wm_adsp_stop(struct wm_adsp *dsp) | |
1099 | { | |
1100 | cs_dsp_stop(&dsp->cs_dsp); | |
1101 | } | |
1102 | EXPORT_SYMBOL_GPL(wm_adsp_stop); | |
1103 | ||
186152df ST |
1104 | int wm_adsp_event(struct snd_soc_dapm_widget *w, |
1105 | struct snd_kcontrol *kcontrol, int event) | |
1106 | { | |
1107 | struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); | |
1108 | struct wm_adsp *dsps = snd_soc_component_get_drvdata(component); | |
1109 | struct wm_adsp *dsp = &dsps[w->shift]; | |
2159ad93 | 1110 | |
186152df ST |
1111 | switch (event) { |
1112 | case SND_SOC_DAPM_POST_PMU: | |
5519ac3a | 1113 | return wm_adsp_run(dsp); |
186152df | 1114 | case SND_SOC_DAPM_PRE_PMD: |
5519ac3a RF |
1115 | wm_adsp_stop(dsp); |
1116 | return 0; | |
2159ad93 | 1117 | default: |
5519ac3a | 1118 | return 0; |
2159ad93 | 1119 | } |
2159ad93 | 1120 | } |
4e08d50d | 1121 | EXPORT_SYMBOL_GPL(wm_adsp_event); |
973838a0 | 1122 | |
0fe1daa6 | 1123 | int wm_adsp2_component_probe(struct wm_adsp *dsp, struct snd_soc_component *component) |
f5e2ce92 | 1124 | { |
af813a6f CK |
1125 | char preload[32]; |
1126 | ||
0cd1fd57 ST |
1127 | if (!dsp->cs_dsp.no_core_startstop) { |
1128 | snprintf(preload, ARRAY_SIZE(preload), "%s Preload", dsp->cs_dsp.name); | |
1129 | snd_soc_component_disable_pin(component, preload); | |
1130 | } | |
685f51a5 | 1131 | |
e1468202 | 1132 | cs_dsp_init_debugfs(&dsp->cs_dsp, component->debugfs_root); |
f9f55e31 | 1133 | |
0fe1daa6 | 1134 | dsp->component = component; |
af813a6f | 1135 | |
0a047f07 | 1136 | return 0; |
f5e2ce92 | 1137 | } |
0fe1daa6 | 1138 | EXPORT_SYMBOL_GPL(wm_adsp2_component_probe); |
f5e2ce92 | 1139 | |
0fe1daa6 | 1140 | int wm_adsp2_component_remove(struct wm_adsp *dsp, struct snd_soc_component *component) |
f5e2ce92 | 1141 | { |
e1468202 | 1142 | cs_dsp_cleanup_debugfs(&dsp->cs_dsp); |
f9f55e31 | 1143 | |
f5e2ce92 RF |
1144 | return 0; |
1145 | } | |
0fe1daa6 | 1146 | EXPORT_SYMBOL_GPL(wm_adsp2_component_remove); |
f5e2ce92 | 1147 | |
25ca837b ST |
1148 | int wm_adsp2_init(struct wm_adsp *dsp) |
1149 | { | |
e1468202 ST |
1150 | int ret; |
1151 | ||
4e08d50d | 1152 | INIT_WORK(&dsp->boot_work, wm_adsp_boot_work); |
6ab2b7b4 | 1153 | |
6092be2d | 1154 | dsp->sys_config_size = sizeof(struct wm_adsp_system_config_xm_hdr); |
2dd04464 | 1155 | dsp->cs_dsp.client_ops = &wm_adsp2_client_ops; |
6092be2d | 1156 | |
e1468202 ST |
1157 | ret = cs_dsp_adsp2_init(&dsp->cs_dsp); |
1158 | if (ret) | |
1159 | return ret; | |
25ca837b | 1160 | |
e1468202 | 1161 | return wm_adsp_common_init(dsp); |
973838a0 MB |
1162 | } |
1163 | EXPORT_SYMBOL_GPL(wm_adsp2_init); | |
0a37c6ef | 1164 | |
25ca837b ST |
1165 | int wm_halo_init(struct wm_adsp *dsp) |
1166 | { | |
e1468202 ST |
1167 | int ret; |
1168 | ||
170b1e12 WS |
1169 | INIT_WORK(&dsp->boot_work, wm_adsp_boot_work); |
1170 | ||
6092be2d | 1171 | dsp->sys_config_size = sizeof(struct wm_halo_system_config_xm_hdr); |
2dd04464 | 1172 | dsp->cs_dsp.client_ops = &wm_adsp2_client_ops; |
6092be2d | 1173 | |
e1468202 ST |
1174 | ret = cs_dsp_halo_init(&dsp->cs_dsp); |
1175 | if (ret) | |
1176 | return ret; | |
25ca837b | 1177 | |
e1468202 | 1178 | return wm_adsp_common_init(dsp); |
170b1e12 WS |
1179 | } |
1180 | EXPORT_SYMBOL_GPL(wm_halo_init); | |
1181 | ||
25ca837b ST |
1182 | void wm_adsp2_remove(struct wm_adsp *dsp) |
1183 | { | |
e1468202 | 1184 | cs_dsp_remove(&dsp->cs_dsp); |
25ca837b | 1185 | } |
66225e98 RF |
1186 | EXPORT_SYMBOL_GPL(wm_adsp2_remove); |
1187 | ||
edd71350 CK |
1188 | static inline int wm_adsp_compr_attached(struct wm_adsp_compr *compr) |
1189 | { | |
1190 | return compr->buf != NULL; | |
1191 | } | |
1192 | ||
1193 | static int wm_adsp_compr_attach(struct wm_adsp_compr *compr) | |
1194 | { | |
4f2d4eab SH |
1195 | struct wm_adsp_compr_buf *buf = NULL, *tmp; |
1196 | ||
a2bcbc1b CK |
1197 | if (compr->dsp->fatal_error) |
1198 | return -EINVAL; | |
1199 | ||
4f2d4eab SH |
1200 | list_for_each_entry(tmp, &compr->dsp->buffer_list, list) { |
1201 | if (!tmp->name || !strcmp(compr->name, tmp->name)) { | |
1202 | buf = tmp; | |
1203 | break; | |
1204 | } | |
1205 | } | |
1206 | ||
1207 | if (!buf) | |
edd71350 CK |
1208 | return -EINVAL; |
1209 | ||
4f2d4eab | 1210 | compr->buf = buf; |
789b930a | 1211 | buf->compr = compr; |
edd71350 CK |
1212 | |
1213 | return 0; | |
1214 | } | |
1215 | ||
721be3be CK |
1216 | static void wm_adsp_compr_detach(struct wm_adsp_compr *compr) |
1217 | { | |
1218 | if (!compr) | |
1219 | return; | |
1220 | ||
1221 | /* Wake the poll so it can see buffer is no longer attached */ | |
1222 | if (compr->stream) | |
1223 | snd_compr_fragment_elapsed(compr->stream); | |
1224 | ||
1225 | if (wm_adsp_compr_attached(compr)) { | |
1226 | compr->buf->compr = NULL; | |
1227 | compr->buf = NULL; | |
1228 | } | |
1229 | } | |
1230 | ||
406abc95 CK |
1231 | int wm_adsp_compr_open(struct wm_adsp *dsp, struct snd_compr_stream *stream) |
1232 | { | |
4f2d4eab SH |
1233 | struct wm_adsp_compr *compr, *tmp; |
1234 | struct snd_soc_pcm_runtime *rtd = stream->private_data; | |
406abc95 CK |
1235 | int ret = 0; |
1236 | ||
e1468202 | 1237 | mutex_lock(&dsp->cs_dsp.pwr_lock); |
406abc95 CK |
1238 | |
1239 | if (wm_adsp_fw[dsp->fw].num_caps == 0) { | |
0d3fba3e | 1240 | adsp_err(dsp, "%s: Firmware does not support compressed API\n", |
221a3d28 | 1241 | snd_soc_rtd_to_codec(rtd, 0)->name); |
406abc95 CK |
1242 | ret = -ENXIO; |
1243 | goto out; | |
1244 | } | |
1245 | ||
1246 | if (wm_adsp_fw[dsp->fw].compr_direction != stream->direction) { | |
0d3fba3e | 1247 | adsp_err(dsp, "%s: Firmware does not support stream direction\n", |
221a3d28 | 1248 | snd_soc_rtd_to_codec(rtd, 0)->name); |
406abc95 CK |
1249 | ret = -EINVAL; |
1250 | goto out; | |
1251 | } | |
1252 | ||
4f2d4eab | 1253 | list_for_each_entry(tmp, &dsp->compr_list, list) { |
221a3d28 | 1254 | if (!strcmp(tmp->name, snd_soc_rtd_to_codec(rtd, 0)->name)) { |
0d3fba3e | 1255 | adsp_err(dsp, "%s: Only a single stream supported per dai\n", |
221a3d28 | 1256 | snd_soc_rtd_to_codec(rtd, 0)->name); |
4f2d4eab SH |
1257 | ret = -EBUSY; |
1258 | goto out; | |
1259 | } | |
95fe9597 CK |
1260 | } |
1261 | ||
406abc95 CK |
1262 | compr = kzalloc(sizeof(*compr), GFP_KERNEL); |
1263 | if (!compr) { | |
1264 | ret = -ENOMEM; | |
1265 | goto out; | |
1266 | } | |
1267 | ||
1268 | compr->dsp = dsp; | |
1269 | compr->stream = stream; | |
221a3d28 | 1270 | compr->name = snd_soc_rtd_to_codec(rtd, 0)->name; |
406abc95 | 1271 | |
4f2d4eab | 1272 | list_add_tail(&compr->list, &dsp->compr_list); |
406abc95 CK |
1273 | |
1274 | stream->runtime->private_data = compr; | |
1275 | ||
1276 | out: | |
e1468202 | 1277 | mutex_unlock(&dsp->cs_dsp.pwr_lock); |
406abc95 CK |
1278 | |
1279 | return ret; | |
1280 | } | |
1281 | EXPORT_SYMBOL_GPL(wm_adsp_compr_open); | |
1282 | ||
3a5ccf25 KM |
1283 | int wm_adsp_compr_free(struct snd_soc_component *component, |
1284 | struct snd_compr_stream *stream) | |
406abc95 CK |
1285 | { |
1286 | struct wm_adsp_compr *compr = stream->runtime->private_data; | |
1287 | struct wm_adsp *dsp = compr->dsp; | |
1288 | ||
e1468202 | 1289 | mutex_lock(&dsp->cs_dsp.pwr_lock); |
406abc95 | 1290 | |
721be3be | 1291 | wm_adsp_compr_detach(compr); |
4f2d4eab | 1292 | list_del(&compr->list); |
406abc95 | 1293 | |
83a40ce9 | 1294 | kfree(compr->raw_buf); |
406abc95 CK |
1295 | kfree(compr); |
1296 | ||
e1468202 | 1297 | mutex_unlock(&dsp->cs_dsp.pwr_lock); |
406abc95 CK |
1298 | |
1299 | return 0; | |
1300 | } | |
1301 | EXPORT_SYMBOL_GPL(wm_adsp_compr_free); | |
1302 | ||
1303 | static int wm_adsp_compr_check_params(struct snd_compr_stream *stream, | |
1304 | struct snd_compr_params *params) | |
1305 | { | |
1306 | struct wm_adsp_compr *compr = stream->runtime->private_data; | |
1307 | struct wm_adsp *dsp = compr->dsp; | |
1308 | const struct wm_adsp_fw_caps *caps; | |
1309 | const struct snd_codec_desc *desc; | |
1310 | int i, j; | |
1311 | ||
1312 | if (params->buffer.fragment_size < WM_ADSP_MIN_FRAGMENT_SIZE || | |
1313 | params->buffer.fragment_size > WM_ADSP_MAX_FRAGMENT_SIZE || | |
1314 | params->buffer.fragments < WM_ADSP_MIN_FRAGMENTS || | |
1315 | params->buffer.fragments > WM_ADSP_MAX_FRAGMENTS || | |
5beb8eea | 1316 | params->buffer.fragment_size % CS_DSP_DATA_WORD_SIZE) { |
0d3fba3e CK |
1317 | compr_err(compr, "Invalid buffer fragsize=%d fragments=%d\n", |
1318 | params->buffer.fragment_size, | |
1319 | params->buffer.fragments); | |
406abc95 CK |
1320 | |
1321 | return -EINVAL; | |
1322 | } | |
1323 | ||
1324 | for (i = 0; i < wm_adsp_fw[dsp->fw].num_caps; i++) { | |
1325 | caps = &wm_adsp_fw[dsp->fw].caps[i]; | |
1326 | desc = &caps->desc; | |
1327 | ||
1328 | if (caps->id != params->codec.id) | |
1329 | continue; | |
1330 | ||
1331 | if (stream->direction == SND_COMPRESS_PLAYBACK) { | |
1332 | if (desc->max_ch < params->codec.ch_out) | |
1333 | continue; | |
1334 | } else { | |
1335 | if (desc->max_ch < params->codec.ch_in) | |
1336 | continue; | |
1337 | } | |
1338 | ||
1339 | if (!(desc->formats & (1 << params->codec.format))) | |
1340 | continue; | |
1341 | ||
1342 | for (j = 0; j < desc->num_sample_rates; ++j) | |
1343 | if (desc->sample_rates[j] == params->codec.sample_rate) | |
1344 | return 0; | |
1345 | } | |
1346 | ||
0d3fba3e CK |
1347 | compr_err(compr, "Invalid params id=%u ch=%u,%u rate=%u fmt=%u\n", |
1348 | params->codec.id, params->codec.ch_in, params->codec.ch_out, | |
1349 | params->codec.sample_rate, params->codec.format); | |
406abc95 CK |
1350 | return -EINVAL; |
1351 | } | |
1352 | ||
565ace46 CK |
1353 | static inline unsigned int wm_adsp_compr_frag_words(struct wm_adsp_compr *compr) |
1354 | { | |
5beb8eea | 1355 | return compr->size.fragment_size / CS_DSP_DATA_WORD_SIZE; |
565ace46 CK |
1356 | } |
1357 | ||
3a5ccf25 KM |
1358 | int wm_adsp_compr_set_params(struct snd_soc_component *component, |
1359 | struct snd_compr_stream *stream, | |
406abc95 CK |
1360 | struct snd_compr_params *params) |
1361 | { | |
1362 | struct wm_adsp_compr *compr = stream->runtime->private_data; | |
83a40ce9 | 1363 | unsigned int size; |
406abc95 CK |
1364 | int ret; |
1365 | ||
1366 | ret = wm_adsp_compr_check_params(stream, params); | |
1367 | if (ret) | |
1368 | return ret; | |
1369 | ||
1370 | compr->size = params->buffer; | |
1371 | ||
0d3fba3e CK |
1372 | compr_dbg(compr, "fragment_size=%d fragments=%d\n", |
1373 | compr->size.fragment_size, compr->size.fragments); | |
406abc95 | 1374 | |
83a40ce9 CK |
1375 | size = wm_adsp_compr_frag_words(compr) * sizeof(*compr->raw_buf); |
1376 | compr->raw_buf = kmalloc(size, GFP_DMA | GFP_KERNEL); | |
1377 | if (!compr->raw_buf) | |
1378 | return -ENOMEM; | |
1379 | ||
da2b3358 CK |
1380 | compr->sample_rate = params->codec.sample_rate; |
1381 | ||
406abc95 CK |
1382 | return 0; |
1383 | } | |
1384 | EXPORT_SYMBOL_GPL(wm_adsp_compr_set_params); | |
1385 | ||
3a5ccf25 KM |
1386 | int wm_adsp_compr_get_caps(struct snd_soc_component *component, |
1387 | struct snd_compr_stream *stream, | |
406abc95 CK |
1388 | struct snd_compr_caps *caps) |
1389 | { | |
1390 | struct wm_adsp_compr *compr = stream->runtime->private_data; | |
1391 | int fw = compr->dsp->fw; | |
1392 | int i; | |
1393 | ||
1394 | if (wm_adsp_fw[fw].caps) { | |
1395 | for (i = 0; i < wm_adsp_fw[fw].num_caps; i++) | |
1396 | caps->codecs[i] = wm_adsp_fw[fw].caps[i].id; | |
1397 | ||
1398 | caps->num_codecs = i; | |
1399 | caps->direction = wm_adsp_fw[fw].compr_direction; | |
1400 | ||
1401 | caps->min_fragment_size = WM_ADSP_MIN_FRAGMENT_SIZE; | |
1402 | caps->max_fragment_size = WM_ADSP_MAX_FRAGMENT_SIZE; | |
1403 | caps->min_fragments = WM_ADSP_MIN_FRAGMENTS; | |
1404 | caps->max_fragments = WM_ADSP_MAX_FRAGMENTS; | |
1405 | } | |
1406 | ||
1407 | return 0; | |
1408 | } | |
1409 | EXPORT_SYMBOL_GPL(wm_adsp_compr_get_caps); | |
1410 | ||
2cd19bdb CK |
1411 | static inline int wm_adsp_buffer_read(struct wm_adsp_compr_buf *buf, |
1412 | unsigned int field_offset, u32 *data) | |
1413 | { | |
e1468202 | 1414 | return cs_dsp_read_data_word(&buf->dsp->cs_dsp, buf->host_buf_mem_type, |
5beb8eea | 1415 | buf->host_buf_ptr + field_offset, data); |
2cd19bdb CK |
1416 | } |
1417 | ||
1418 | static inline int wm_adsp_buffer_write(struct wm_adsp_compr_buf *buf, | |
1419 | unsigned int field_offset, u32 data) | |
1420 | { | |
e1468202 ST |
1421 | return cs_dsp_write_data_word(&buf->dsp->cs_dsp, buf->host_buf_mem_type, |
1422 | buf->host_buf_ptr + field_offset, | |
1423 | data); | |
2cd19bdb CK |
1424 | } |
1425 | ||
1e38f069 CK |
1426 | static int wm_adsp_buffer_populate(struct wm_adsp_compr_buf *buf) |
1427 | { | |
1428 | const struct wm_adsp_fw_caps *caps = wm_adsp_fw[buf->dsp->fw].caps; | |
1429 | struct wm_adsp_buffer_region *region; | |
1430 | u32 offset = 0; | |
1431 | int i, ret; | |
1432 | ||
a792af69 CK |
1433 | buf->regions = kcalloc(caps->num_regions, sizeof(*buf->regions), |
1434 | GFP_KERNEL); | |
1435 | if (!buf->regions) | |
1436 | return -ENOMEM; | |
1437 | ||
1e38f069 CK |
1438 | for (i = 0; i < caps->num_regions; ++i) { |
1439 | region = &buf->regions[i]; | |
1440 | ||
1441 | region->offset = offset; | |
1442 | region->mem_type = caps->region_defs[i].mem_type; | |
1443 | ||
1444 | ret = wm_adsp_buffer_read(buf, caps->region_defs[i].base_offset, | |
1445 | ®ion->base_addr); | |
1446 | if (ret < 0) | |
29046a78 | 1447 | goto err; |
1e38f069 CK |
1448 | |
1449 | ret = wm_adsp_buffer_read(buf, caps->region_defs[i].size_offset, | |
1450 | &offset); | |
1451 | if (ret < 0) | |
29046a78 | 1452 | goto err; |
1e38f069 CK |
1453 | |
1454 | region->cumulative_size = offset; | |
1455 | ||
0d3fba3e CK |
1456 | compr_dbg(buf, |
1457 | "region=%d type=%d base=%08x off=%08x size=%08x\n", | |
1458 | i, region->mem_type, region->base_addr, | |
1459 | region->offset, region->cumulative_size); | |
1e38f069 CK |
1460 | } |
1461 | ||
1462 | return 0; | |
29046a78 DL |
1463 | |
1464 | err: | |
1465 | kfree(buf->regions); | |
1466 | return ret; | |
1e38f069 CK |
1467 | } |
1468 | ||
1469 | static void wm_adsp_buffer_clear(struct wm_adsp_compr_buf *buf) | |
1470 | { | |
1471 | buf->irq_count = 0xFFFFFFFF; | |
1472 | buf->read_index = -1; | |
1473 | buf->avail = 0; | |
1474 | } | |
1475 | ||
a792af69 CK |
1476 | static struct wm_adsp_compr_buf *wm_adsp_buffer_alloc(struct wm_adsp *dsp) |
1477 | { | |
1478 | struct wm_adsp_compr_buf *buf; | |
1479 | ||
1480 | buf = kzalloc(sizeof(*buf), GFP_KERNEL); | |
1481 | if (!buf) | |
1482 | return NULL; | |
1483 | ||
1484 | buf->dsp = dsp; | |
1485 | ||
1486 | wm_adsp_buffer_clear(buf); | |
1487 | ||
a792af69 CK |
1488 | return buf; |
1489 | } | |
1490 | ||
1491 | static int wm_adsp_buffer_parse_legacy(struct wm_adsp *dsp) | |
2cd19bdb | 1492 | { |
5beb8eea | 1493 | struct cs_dsp_alg_region *alg_region; |
a792af69 | 1494 | struct wm_adsp_compr_buf *buf; |
2cd19bdb CK |
1495 | u32 xmalg, addr, magic; |
1496 | int i, ret; | |
1497 | ||
e1468202 | 1498 | alg_region = cs_dsp_find_alg_region(&dsp->cs_dsp, WMFW_ADSP2_XM, dsp->cs_dsp.fw_id); |
9daf4fd0 LX |
1499 | if (!alg_region) { |
1500 | adsp_err(dsp, "No algorithm region found\n"); | |
1501 | return -EINVAL; | |
1502 | } | |
1503 | ||
6092be2d | 1504 | xmalg = dsp->sys_config_size / sizeof(__be32); |
2cd19bdb CK |
1505 | |
1506 | addr = alg_region->base + xmalg + ALG_XM_FIELD(magic); | |
e1468202 | 1507 | ret = cs_dsp_read_data_word(&dsp->cs_dsp, WMFW_ADSP2_XM, addr, &magic); |
2cd19bdb CK |
1508 | if (ret < 0) |
1509 | return ret; | |
1510 | ||
1511 | if (magic != WM_ADSP_ALG_XM_STRUCT_MAGIC) | |
a792af69 | 1512 | return -ENODEV; |
2cd19bdb | 1513 | |
0f1d41a8 CK |
1514 | buf = wm_adsp_buffer_alloc(dsp); |
1515 | if (!buf) | |
1516 | return -ENOMEM; | |
1517 | ||
2cd19bdb CK |
1518 | addr = alg_region->base + xmalg + ALG_XM_FIELD(host_buf_ptr); |
1519 | for (i = 0; i < 5; ++i) { | |
e1468202 | 1520 | ret = cs_dsp_read_data_word(&dsp->cs_dsp, WMFW_ADSP2_XM, addr, |
5beb8eea | 1521 | &buf->host_buf_ptr); |
2cd19bdb | 1522 | if (ret < 0) |
0f1d41a8 | 1523 | goto err; |
2cd19bdb CK |
1524 | |
1525 | if (buf->host_buf_ptr) | |
1526 | break; | |
1527 | ||
1528 | usleep_range(1000, 2000); | |
1529 | } | |
1530 | ||
0f1d41a8 CK |
1531 | if (!buf->host_buf_ptr) { |
1532 | ret = -EIO; | |
1533 | goto err; | |
1534 | } | |
2cd19bdb | 1535 | |
fb13f19d AF |
1536 | buf->host_buf_mem_type = WMFW_ADSP2_XM; |
1537 | ||
a792af69 CK |
1538 | ret = wm_adsp_buffer_populate(buf); |
1539 | if (ret < 0) | |
0f1d41a8 CK |
1540 | goto err; |
1541 | ||
1542 | list_add_tail(&buf->list, &dsp->buffer_list); | |
d52ed4b0 | 1543 | |
0d3fba3e | 1544 | compr_dbg(buf, "legacy host_buf_ptr=%x\n", buf->host_buf_ptr); |
d52ed4b0 | 1545 | |
a792af69 | 1546 | return 0; |
0f1d41a8 CK |
1547 | |
1548 | err: | |
1549 | kfree(buf); | |
1550 | ||
1551 | return ret; | |
d52ed4b0 RF |
1552 | } |
1553 | ||
0700bc2f | 1554 | static int wm_adsp_buffer_parse_coeff(struct cs_dsp_coeff_ctl *cs_ctl) |
d52ed4b0 | 1555 | { |
4f2d4eab | 1556 | struct wm_adsp_host_buf_coeff_v1 coeff_v1; |
a792af69 | 1557 | struct wm_adsp_compr_buf *buf; |
e1468202 | 1558 | struct wm_adsp *dsp = container_of(cs_ctl->dsp, struct wm_adsp, cs_dsp); |
0f1d41a8 | 1559 | unsigned int version = 0; |
a792af69 | 1560 | int ret, i; |
d52ed4b0 | 1561 | |
d52ed4b0 | 1562 | for (i = 0; i < 5; ++i) { |
a887f9c7 CK |
1563 | ret = cs_dsp_coeff_read_ctrl(cs_ctl, 0, &coeff_v1, |
1564 | min(cs_ctl->len, sizeof(coeff_v1))); | |
d52ed4b0 RF |
1565 | if (ret < 0) |
1566 | return ret; | |
1567 | ||
04ae0859 | 1568 | if (coeff_v1.host_buf_ptr) |
d52ed4b0 RF |
1569 | break; |
1570 | ||
1571 | usleep_range(1000, 2000); | |
1572 | } | |
1573 | ||
04ae0859 | 1574 | if (!coeff_v1.host_buf_ptr) { |
0700bc2f | 1575 | adsp_err(dsp, "Failed to acquire host buffer\n"); |
d52ed4b0 | 1576 | return -EIO; |
a792af69 CK |
1577 | } |
1578 | ||
0700bc2f | 1579 | buf = wm_adsp_buffer_alloc(dsp); |
a792af69 CK |
1580 | if (!buf) |
1581 | return -ENOMEM; | |
d52ed4b0 | 1582 | |
0700bc2f | 1583 | buf->host_buf_mem_type = cs_ctl->alg_region.type; |
04ae0859 | 1584 | buf->host_buf_ptr = be32_to_cpu(coeff_v1.host_buf_ptr); |
a792af69 CK |
1585 | |
1586 | ret = wm_adsp_buffer_populate(buf); | |
1587 | if (ret < 0) | |
0f1d41a8 | 1588 | goto err; |
a792af69 | 1589 | |
4f2d4eab SH |
1590 | /* |
1591 | * v0 host_buffer coefficients didn't have versioning, so if the | |
1592 | * control is one word, assume version 0. | |
1593 | */ | |
0f1d41a8 CK |
1594 | if (cs_ctl->len == 4) |
1595 | goto done; | |
4f2d4eab | 1596 | |
a0b653e8 RF |
1597 | version = be32_to_cpu(coeff_v1.versions) & HOST_BUF_COEFF_COMPAT_VER_MASK; |
1598 | version >>= HOST_BUF_COEFF_COMPAT_VER_SHIFT; | |
d52ed4b0 | 1599 | |
a0b653e8 | 1600 | if (version > HOST_BUF_COEFF_SUPPORTED_COMPAT_VER) { |
0700bc2f | 1601 | adsp_err(dsp, |
4f2d4eab | 1602 | "Host buffer coeff ver %u > supported version %u\n", |
a0b653e8 | 1603 | version, HOST_BUF_COEFF_SUPPORTED_COMPAT_VER); |
0f1d41a8 CK |
1604 | ret = -EINVAL; |
1605 | goto err; | |
4f2d4eab SH |
1606 | } |
1607 | ||
5beb8eea | 1608 | cs_dsp_remove_padding((u32 *)&coeff_v1.name, ARRAY_SIZE(coeff_v1.name)); |
4f2d4eab | 1609 | |
0700bc2f | 1610 | buf->name = kasprintf(GFP_KERNEL, "%s-dsp-%s", dsp->part, |
4f2d4eab SH |
1611 | (char *)&coeff_v1.name); |
1612 | ||
0f1d41a8 CK |
1613 | done: |
1614 | list_add_tail(&buf->list, &dsp->buffer_list); | |
1615 | ||
0d3fba3e | 1616 | compr_dbg(buf, "host_buf_ptr=%x coeff version %u\n", |
a0b653e8 | 1617 | buf->host_buf_ptr, version); |
4f2d4eab | 1618 | |
a0b653e8 | 1619 | return version; |
0f1d41a8 CK |
1620 | |
1621 | err: | |
1622 | kfree(buf); | |
1623 | ||
1624 | return ret; | |
d52ed4b0 RF |
1625 | } |
1626 | ||
2cd19bdb CK |
1627 | static int wm_adsp_buffer_init(struct wm_adsp *dsp) |
1628 | { | |
0700bc2f | 1629 | struct cs_dsp_coeff_ctl *cs_ctl; |
2cd19bdb CK |
1630 | int ret; |
1631 | ||
e1468202 | 1632 | list_for_each_entry(cs_ctl, &dsp->cs_dsp.ctl_list, list) { |
0700bc2f | 1633 | if (cs_ctl->type != WMFW_CTL_TYPE_HOST_BUFFER) |
a792af69 | 1634 | continue; |
61fc060c | 1635 | |
0700bc2f | 1636 | if (!cs_ctl->enabled) |
a792af69 | 1637 | continue; |
2cd19bdb | 1638 | |
0700bc2f | 1639 | ret = wm_adsp_buffer_parse_coeff(cs_ctl); |
a792af69 CK |
1640 | if (ret < 0) { |
1641 | adsp_err(dsp, "Failed to parse coeff: %d\n", ret); | |
1642 | goto error; | |
4f2d4eab SH |
1643 | } else if (ret == 0) { |
1644 | /* Only one buffer supported for version 0 */ | |
1645 | return 0; | |
a792af69 | 1646 | } |
2cd19bdb CK |
1647 | } |
1648 | ||
4f2d4eab | 1649 | if (list_empty(&dsp->buffer_list)) { |
a792af69 CK |
1650 | /* Fall back to legacy support */ |
1651 | ret = wm_adsp_buffer_parse_legacy(dsp); | |
35c8ae25 CC |
1652 | if (ret == -ENODEV) |
1653 | adsp_info(dsp, "Legacy support not available\n"); | |
1654 | else if (ret) | |
0f1d41a8 | 1655 | adsp_warn(dsp, "Failed to parse legacy: %d\n", ret); |
2cd19bdb CK |
1656 | } |
1657 | ||
2cd19bdb CK |
1658 | return 0; |
1659 | ||
a792af69 CK |
1660 | error: |
1661 | wm_adsp_buffer_free(dsp); | |
2cd19bdb CK |
1662 | return ret; |
1663 | } | |
1664 | ||
1665 | static int wm_adsp_buffer_free(struct wm_adsp *dsp) | |
1666 | { | |
4f2d4eab | 1667 | struct wm_adsp_compr_buf *buf, *tmp; |
721be3be | 1668 | |
4f2d4eab | 1669 | list_for_each_entry_safe(buf, tmp, &dsp->buffer_list, list) { |
26ffa016 | 1670 | wm_adsp_compr_detach(buf->compr); |
2cd19bdb | 1671 | |
4f2d4eab SH |
1672 | kfree(buf->name); |
1673 | kfree(buf->regions); | |
1674 | list_del(&buf->list); | |
1675 | kfree(buf); | |
2cd19bdb CK |
1676 | } |
1677 | ||
1678 | return 0; | |
1679 | } | |
1680 | ||
f938f348 SH |
1681 | static int wm_adsp_buffer_get_error(struct wm_adsp_compr_buf *buf) |
1682 | { | |
1683 | int ret; | |
1684 | ||
1685 | ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(error), &buf->error); | |
1686 | if (ret < 0) { | |
48ead31c | 1687 | compr_err(buf, "Failed to check buffer error: %d\n", ret); |
f938f348 SH |
1688 | return ret; |
1689 | } | |
1690 | if (buf->error != 0) { | |
48ead31c | 1691 | compr_err(buf, "Buffer error occurred: %d\n", buf->error); |
f938f348 SH |
1692 | return -EIO; |
1693 | } | |
1694 | ||
1695 | return 0; | |
1696 | } | |
1697 | ||
3a5ccf25 KM |
1698 | int wm_adsp_compr_trigger(struct snd_soc_component *component, |
1699 | struct snd_compr_stream *stream, int cmd) | |
95fe9597 CK |
1700 | { |
1701 | struct wm_adsp_compr *compr = stream->runtime->private_data; | |
1702 | struct wm_adsp *dsp = compr->dsp; | |
1703 | int ret = 0; | |
1704 | ||
0d3fba3e | 1705 | compr_dbg(compr, "Trigger: %d\n", cmd); |
95fe9597 | 1706 | |
e1468202 | 1707 | mutex_lock(&dsp->cs_dsp.pwr_lock); |
95fe9597 CK |
1708 | |
1709 | switch (cmd) { | |
1710 | case SNDRV_PCM_TRIGGER_START: | |
61fc060c CK |
1711 | if (!wm_adsp_compr_attached(compr)) { |
1712 | ret = wm_adsp_compr_attach(compr); | |
1713 | if (ret < 0) { | |
0d3fba3e CK |
1714 | compr_err(compr, "Failed to link buffer and stream: %d\n", |
1715 | ret); | |
61fc060c CK |
1716 | break; |
1717 | } | |
95fe9597 | 1718 | } |
565ace46 | 1719 | |
f938f348 SH |
1720 | ret = wm_adsp_buffer_get_error(compr->buf); |
1721 | if (ret < 0) | |
1722 | break; | |
1723 | ||
565ace46 CK |
1724 | /* Trigger the IRQ at one fragment of data */ |
1725 | ret = wm_adsp_buffer_write(compr->buf, | |
1726 | HOST_BUFFER_FIELD(high_water_mark), | |
1727 | wm_adsp_compr_frag_words(compr)); | |
1728 | if (ret < 0) { | |
0d3fba3e CK |
1729 | compr_err(compr, "Failed to set high water mark: %d\n", |
1730 | ret); | |
565ace46 CK |
1731 | break; |
1732 | } | |
95fe9597 CK |
1733 | break; |
1734 | case SNDRV_PCM_TRIGGER_STOP: | |
43d147be CK |
1735 | if (wm_adsp_compr_attached(compr)) |
1736 | wm_adsp_buffer_clear(compr->buf); | |
95fe9597 CK |
1737 | break; |
1738 | default: | |
1739 | ret = -EINVAL; | |
1740 | break; | |
1741 | } | |
1742 | ||
e1468202 | 1743 | mutex_unlock(&dsp->cs_dsp.pwr_lock); |
95fe9597 CK |
1744 | |
1745 | return ret; | |
1746 | } | |
1747 | EXPORT_SYMBOL_GPL(wm_adsp_compr_trigger); | |
1748 | ||
565ace46 CK |
1749 | static inline int wm_adsp_buffer_size(struct wm_adsp_compr_buf *buf) |
1750 | { | |
1751 | int last_region = wm_adsp_fw[buf->dsp->fw].caps->num_regions - 1; | |
1752 | ||
1753 | return buf->regions[last_region].cumulative_size; | |
1754 | } | |
1755 | ||
1756 | static int wm_adsp_buffer_update_avail(struct wm_adsp_compr_buf *buf) | |
1757 | { | |
1758 | u32 next_read_index, next_write_index; | |
1759 | int write_index, read_index, avail; | |
1760 | int ret; | |
1761 | ||
1762 | /* Only sync read index if we haven't already read a valid index */ | |
1763 | if (buf->read_index < 0) { | |
1764 | ret = wm_adsp_buffer_read(buf, | |
1765 | HOST_BUFFER_FIELD(next_read_index), | |
1766 | &next_read_index); | |
1767 | if (ret < 0) | |
1768 | return ret; | |
1769 | ||
1770 | read_index = sign_extend32(next_read_index, 23); | |
1771 | ||
1772 | if (read_index < 0) { | |
0d3fba3e | 1773 | compr_dbg(buf, "Avail check on unstarted stream\n"); |
565ace46 CK |
1774 | return 0; |
1775 | } | |
1776 | ||
1777 | buf->read_index = read_index; | |
1778 | } | |
1779 | ||
1780 | ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(next_write_index), | |
1781 | &next_write_index); | |
1782 | if (ret < 0) | |
1783 | return ret; | |
1784 | ||
1785 | write_index = sign_extend32(next_write_index, 23); | |
1786 | ||
1787 | avail = write_index - buf->read_index; | |
1788 | if (avail < 0) | |
1789 | avail += wm_adsp_buffer_size(buf); | |
1790 | ||
0d3fba3e | 1791 | compr_dbg(buf, "readindex=0x%x, writeindex=0x%x, avail=%d\n", |
5beb8eea | 1792 | buf->read_index, write_index, avail * CS_DSP_DATA_WORD_SIZE); |
565ace46 CK |
1793 | |
1794 | buf->avail = avail; | |
1795 | ||
1796 | return 0; | |
1797 | } | |
1798 | ||
1799 | int wm_adsp_compr_handle_irq(struct wm_adsp *dsp) | |
1800 | { | |
612047f0 CK |
1801 | struct wm_adsp_compr_buf *buf; |
1802 | struct wm_adsp_compr *compr; | |
565ace46 CK |
1803 | int ret = 0; |
1804 | ||
e1468202 | 1805 | mutex_lock(&dsp->cs_dsp.pwr_lock); |
565ace46 | 1806 | |
4f2d4eab | 1807 | if (list_empty(&dsp->buffer_list)) { |
565ace46 CK |
1808 | ret = -ENODEV; |
1809 | goto out; | |
1810 | } | |
0d3fba3e | 1811 | |
565ace46 CK |
1812 | adsp_dbg(dsp, "Handling buffer IRQ\n"); |
1813 | ||
4f2d4eab SH |
1814 | list_for_each_entry(buf, &dsp->buffer_list, list) { |
1815 | compr = buf->compr; | |
565ace46 | 1816 | |
4f2d4eab SH |
1817 | ret = wm_adsp_buffer_get_error(buf); |
1818 | if (ret < 0) | |
1819 | goto out_notify; /* Wake poll to report error */ | |
565ace46 | 1820 | |
4f2d4eab SH |
1821 | ret = wm_adsp_buffer_read(buf, HOST_BUFFER_FIELD(irq_count), |
1822 | &buf->irq_count); | |
1823 | if (ret < 0) { | |
0d3fba3e | 1824 | compr_err(buf, "Failed to get irq_count: %d\n", ret); |
4f2d4eab SH |
1825 | goto out; |
1826 | } | |
565ace46 | 1827 | |
4f2d4eab SH |
1828 | ret = wm_adsp_buffer_update_avail(buf); |
1829 | if (ret < 0) { | |
0d3fba3e | 1830 | compr_err(buf, "Error reading avail: %d\n", ret); |
4f2d4eab SH |
1831 | goto out; |
1832 | } | |
1833 | ||
1834 | if (wm_adsp_fw[dsp->fw].voice_trigger && buf->irq_count == 2) | |
1835 | ret = WM_ADSP_COMPR_VOICE_TRIGGER; | |
20b7f7c5 | 1836 | |
5847609e | 1837 | out_notify: |
4f2d4eab SH |
1838 | if (compr && compr->stream) |
1839 | snd_compr_fragment_elapsed(compr->stream); | |
1840 | } | |
83a40ce9 | 1841 | |
565ace46 | 1842 | out: |
e1468202 | 1843 | mutex_unlock(&dsp->cs_dsp.pwr_lock); |
565ace46 CK |
1844 | |
1845 | return ret; | |
1846 | } | |
1847 | EXPORT_SYMBOL_GPL(wm_adsp_compr_handle_irq); | |
1848 | ||
1849 | static int wm_adsp_buffer_reenable_irq(struct wm_adsp_compr_buf *buf) | |
1850 | { | |
1851 | if (buf->irq_count & 0x01) | |
1852 | return 0; | |
1853 | ||
0d3fba3e | 1854 | compr_dbg(buf, "Enable IRQ(0x%x) for next fragment\n", buf->irq_count); |
565ace46 CK |
1855 | |
1856 | buf->irq_count |= 0x01; | |
1857 | ||
1858 | return wm_adsp_buffer_write(buf, HOST_BUFFER_FIELD(irq_ack), | |
1859 | buf->irq_count); | |
1860 | } | |
1861 | ||
3a5ccf25 KM |
1862 | int wm_adsp_compr_pointer(struct snd_soc_component *component, |
1863 | struct snd_compr_stream *stream, | |
565ace46 CK |
1864 | struct snd_compr_tstamp *tstamp) |
1865 | { | |
1866 | struct wm_adsp_compr *compr = stream->runtime->private_data; | |
565ace46 | 1867 | struct wm_adsp *dsp = compr->dsp; |
612047f0 | 1868 | struct wm_adsp_compr_buf *buf; |
565ace46 CK |
1869 | int ret = 0; |
1870 | ||
0d3fba3e | 1871 | compr_dbg(compr, "Pointer request\n"); |
565ace46 | 1872 | |
e1468202 | 1873 | mutex_lock(&dsp->cs_dsp.pwr_lock); |
565ace46 | 1874 | |
612047f0 CK |
1875 | buf = compr->buf; |
1876 | ||
aa612f2b | 1877 | if (dsp->fatal_error || !buf || buf->error) { |
8d280664 | 1878 | snd_compr_stop_error(stream, SNDRV_PCM_STATE_XRUN); |
565ace46 CK |
1879 | ret = -EIO; |
1880 | goto out; | |
1881 | } | |
1882 | ||
1883 | if (buf->avail < wm_adsp_compr_frag_words(compr)) { | |
1884 | ret = wm_adsp_buffer_update_avail(buf); | |
1885 | if (ret < 0) { | |
0d3fba3e | 1886 | compr_err(compr, "Error reading avail: %d\n", ret); |
565ace46 CK |
1887 | goto out; |
1888 | } | |
1889 | ||
1890 | /* | |
1891 | * If we really have less than 1 fragment available tell the | |
1892 | * DSP to inform us once a whole fragment is available. | |
1893 | */ | |
1894 | if (buf->avail < wm_adsp_compr_frag_words(compr)) { | |
5847609e | 1895 | ret = wm_adsp_buffer_get_error(buf); |
8d280664 | 1896 | if (ret < 0) { |
789b930a | 1897 | if (buf->error) |
8d280664 CK |
1898 | snd_compr_stop_error(stream, |
1899 | SNDRV_PCM_STATE_XRUN); | |
5847609e | 1900 | goto out; |
8d280664 | 1901 | } |
5847609e | 1902 | |
565ace46 CK |
1903 | ret = wm_adsp_buffer_reenable_irq(buf); |
1904 | if (ret < 0) { | |
0d3fba3e CK |
1905 | compr_err(compr, "Failed to re-enable buffer IRQ: %d\n", |
1906 | ret); | |
565ace46 CK |
1907 | goto out; |
1908 | } | |
1909 | } | |
1910 | } | |
1911 | ||
1912 | tstamp->copied_total = compr->copied_total; | |
5beb8eea | 1913 | tstamp->copied_total += buf->avail * CS_DSP_DATA_WORD_SIZE; |
da2b3358 | 1914 | tstamp->sampling_rate = compr->sample_rate; |
565ace46 CK |
1915 | |
1916 | out: | |
e1468202 | 1917 | mutex_unlock(&dsp->cs_dsp.pwr_lock); |
565ace46 CK |
1918 | |
1919 | return ret; | |
1920 | } | |
1921 | EXPORT_SYMBOL_GPL(wm_adsp_compr_pointer); | |
1922 | ||
83a40ce9 CK |
1923 | static int wm_adsp_buffer_capture_block(struct wm_adsp_compr *compr, int target) |
1924 | { | |
1925 | struct wm_adsp_compr_buf *buf = compr->buf; | |
83a40ce9 CK |
1926 | unsigned int adsp_addr; |
1927 | int mem_type, nwords, max_read; | |
cc7d6ce9 | 1928 | int i, ret; |
83a40ce9 CK |
1929 | |
1930 | /* Calculate read parameters */ | |
1931 | for (i = 0; i < wm_adsp_fw[buf->dsp->fw].caps->num_regions; ++i) | |
1932 | if (buf->read_index < buf->regions[i].cumulative_size) | |
1933 | break; | |
1934 | ||
1935 | if (i == wm_adsp_fw[buf->dsp->fw].caps->num_regions) | |
1936 | return -EINVAL; | |
1937 | ||
1938 | mem_type = buf->regions[i].mem_type; | |
1939 | adsp_addr = buf->regions[i].base_addr + | |
1940 | (buf->read_index - buf->regions[i].offset); | |
1941 | ||
1942 | max_read = wm_adsp_compr_frag_words(compr); | |
1943 | nwords = buf->regions[i].cumulative_size - buf->read_index; | |
1944 | ||
1945 | if (nwords > target) | |
1946 | nwords = target; | |
1947 | if (nwords > buf->avail) | |
1948 | nwords = buf->avail; | |
1949 | if (nwords > max_read) | |
1950 | nwords = max_read; | |
1951 | if (!nwords) | |
1952 | return 0; | |
1953 | ||
1954 | /* Read data from DSP */ | |
e1468202 | 1955 | ret = cs_dsp_read_raw_data_block(&buf->dsp->cs_dsp, mem_type, adsp_addr, |
5beb8eea | 1956 | nwords, (__be32 *)compr->raw_buf); |
83a40ce9 CK |
1957 | if (ret < 0) |
1958 | return ret; | |
1959 | ||
5beb8eea | 1960 | cs_dsp_remove_padding(compr->raw_buf, nwords); |
83a40ce9 CK |
1961 | |
1962 | /* update read index to account for words read */ | |
1963 | buf->read_index += nwords; | |
1964 | if (buf->read_index == wm_adsp_buffer_size(buf)) | |
1965 | buf->read_index = 0; | |
1966 | ||
1967 | ret = wm_adsp_buffer_write(buf, HOST_BUFFER_FIELD(next_read_index), | |
1968 | buf->read_index); | |
1969 | if (ret < 0) | |
1970 | return ret; | |
1971 | ||
1972 | /* update avail to account for words read */ | |
1973 | buf->avail -= nwords; | |
1974 | ||
1975 | return nwords; | |
1976 | } | |
1977 | ||
1978 | static int wm_adsp_compr_read(struct wm_adsp_compr *compr, | |
1979 | char __user *buf, size_t count) | |
1980 | { | |
aa612f2b | 1981 | struct wm_adsp *dsp = compr->dsp; |
83a40ce9 CK |
1982 | int ntotal = 0; |
1983 | int nwords, nbytes; | |
1984 | ||
0d3fba3e | 1985 | compr_dbg(compr, "Requested read of %zu bytes\n", count); |
83a40ce9 | 1986 | |
aa612f2b | 1987 | if (dsp->fatal_error || !compr->buf || compr->buf->error) { |
8d280664 | 1988 | snd_compr_stop_error(compr->stream, SNDRV_PCM_STATE_XRUN); |
83a40ce9 | 1989 | return -EIO; |
8d280664 | 1990 | } |
83a40ce9 | 1991 | |
5beb8eea | 1992 | count /= CS_DSP_DATA_WORD_SIZE; |
83a40ce9 CK |
1993 | |
1994 | do { | |
1995 | nwords = wm_adsp_buffer_capture_block(compr, count); | |
1996 | if (nwords < 0) { | |
0d3fba3e CK |
1997 | compr_err(compr, "Failed to capture block: %d\n", |
1998 | nwords); | |
83a40ce9 CK |
1999 | return nwords; |
2000 | } | |
2001 | ||
5beb8eea | 2002 | nbytes = nwords * CS_DSP_DATA_WORD_SIZE; |
83a40ce9 | 2003 | |
0d3fba3e | 2004 | compr_dbg(compr, "Read %d bytes\n", nbytes); |
83a40ce9 CK |
2005 | |
2006 | if (copy_to_user(buf + ntotal, compr->raw_buf, nbytes)) { | |
0d3fba3e CK |
2007 | compr_err(compr, "Failed to copy data to user: %d, %d\n", |
2008 | ntotal, nbytes); | |
83a40ce9 CK |
2009 | return -EFAULT; |
2010 | } | |
2011 | ||
2012 | count -= nwords; | |
2013 | ntotal += nbytes; | |
2014 | } while (nwords > 0 && count > 0); | |
2015 | ||
2016 | compr->copied_total += ntotal; | |
2017 | ||
2018 | return ntotal; | |
2019 | } | |
2020 | ||
3a5ccf25 KM |
2021 | int wm_adsp_compr_copy(struct snd_soc_component *component, |
2022 | struct snd_compr_stream *stream, char __user *buf, | |
83a40ce9 CK |
2023 | size_t count) |
2024 | { | |
2025 | struct wm_adsp_compr *compr = stream->runtime->private_data; | |
2026 | struct wm_adsp *dsp = compr->dsp; | |
2027 | int ret; | |
2028 | ||
e1468202 | 2029 | mutex_lock(&dsp->cs_dsp.pwr_lock); |
83a40ce9 CK |
2030 | |
2031 | if (stream->direction == SND_COMPRESS_CAPTURE) | |
2032 | ret = wm_adsp_compr_read(compr, buf, count); | |
2033 | else | |
2034 | ret = -ENOTSUPP; | |
2035 | ||
e1468202 | 2036 | mutex_unlock(&dsp->cs_dsp.pwr_lock); |
83a40ce9 CK |
2037 | |
2038 | return ret; | |
2039 | } | |
2040 | EXPORT_SYMBOL_GPL(wm_adsp_compr_copy); | |
2041 | ||
e1468202 | 2042 | static void wm_adsp_fatal_error(struct cs_dsp *cs_dsp) |
a2bcbc1b | 2043 | { |
e1468202 | 2044 | struct wm_adsp *dsp = container_of(cs_dsp, struct wm_adsp, cs_dsp); |
a2bcbc1b CK |
2045 | struct wm_adsp_compr *compr; |
2046 | ||
2047 | dsp->fatal_error = true; | |
2048 | ||
2049 | list_for_each_entry(compr, &dsp->compr_list, list) { | |
aa612f2b | 2050 | if (compr->stream) |
a2bcbc1b | 2051 | snd_compr_fragment_elapsed(compr->stream); |
a2bcbc1b CK |
2052 | } |
2053 | } | |
2054 | ||
25ca837b ST |
2055 | irqreturn_t wm_adsp2_bus_error(int irq, void *data) |
2056 | { | |
2057 | struct wm_adsp *dsp = (struct wm_adsp *)data; | |
2058 | ||
e1468202 | 2059 | cs_dsp_adsp2_bus_error(&dsp->cs_dsp); |
a2225a6d | 2060 | |
51a2c944 MK |
2061 | return IRQ_HANDLED; |
2062 | } | |
2063 | EXPORT_SYMBOL_GPL(wm_adsp2_bus_error); | |
2064 | ||
25ca837b ST |
2065 | irqreturn_t wm_halo_bus_error(int irq, void *data) |
2066 | { | |
2067 | struct wm_adsp *dsp = (struct wm_adsp *)data; | |
2068 | ||
e1468202 | 2069 | cs_dsp_halo_bus_error(&dsp->cs_dsp); |
2ae58138 RF |
2070 | |
2071 | return IRQ_HANDLED; | |
2072 | } | |
2073 | EXPORT_SYMBOL_GPL(wm_halo_bus_error); | |
2074 | ||
25ca837b ST |
2075 | irqreturn_t wm_halo_wdt_expire(int irq, void *data) |
2076 | { | |
2077 | struct wm_adsp *dsp = data; | |
2078 | ||
e1468202 | 2079 | cs_dsp_halo_wdt_expire(&dsp->cs_dsp); |
8bc144f9 SH |
2080 | |
2081 | return IRQ_HANDLED; | |
2082 | } | |
2083 | EXPORT_SYMBOL_GPL(wm_halo_wdt_expire); | |
2084 | ||
2dd04464 | 2085 | static const struct cs_dsp_client_ops wm_adsp1_client_ops = { |
45b4acab | 2086 | .control_add = wm_adsp_control_add_cb, |
2dd04464 ST |
2087 | .control_remove = wm_adsp_control_remove, |
2088 | }; | |
2089 | ||
2dd04464 | 2090 | static const struct cs_dsp_client_ops wm_adsp2_client_ops = { |
45b4acab | 2091 | .control_add = wm_adsp_control_add_cb, |
2dd04464 | 2092 | .control_remove = wm_adsp_control_remove, |
fe071308 | 2093 | .pre_run = wm_adsp_pre_run, |
2dd04464 ST |
2094 | .post_run = wm_adsp_event_post_run, |
2095 | .post_stop = wm_adsp_event_post_stop, | |
2096 | .watchdog_expired = wm_adsp_fatal_error, | |
2097 | }; | |
2098 | ||
8d34c12e | 2099 | MODULE_DESCRIPTION("Cirrus Logic ASoC DSP Support"); |
0a37c6ef | 2100 | MODULE_LICENSE("GPL v2"); |
cdd30ebb | 2101 | MODULE_IMPORT_NS("FW_CS_DSP"); |