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