ASoC: wm_adsp: Warn that firmware file format 0 is depreciated
[deliverable/linux.git] / sound / soc / codecs / wm_adsp.c
CommitLineData
2159ad93
MB
1/*
2 * wm_adsp.c -- Wolfson ADSP support
3 *
4 * Copyright 2012 Wolfson Microelectronics plc
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
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>
cf17c83c 18#include <linux/list.h>
2159ad93
MB
19#include <linux/pm.h>
20#include <linux/pm_runtime.h>
21#include <linux/regmap.h>
973838a0 22#include <linux/regulator/consumer.h>
2159ad93 23#include <linux/slab.h>
cdcd7f72 24#include <linux/vmalloc.h>
6ab2b7b4 25#include <linux/workqueue.h>
2159ad93
MB
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 <linux/mfd/arizona/registers.h>
35
dc91428a 36#include "arizona.h"
2159ad93
MB
37#include "wm_adsp.h"
38
39#define adsp_crit(_dsp, fmt, ...) \
40 dev_crit(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__)
41#define adsp_err(_dsp, fmt, ...) \
42 dev_err(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__)
43#define adsp_warn(_dsp, fmt, ...) \
44 dev_warn(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__)
45#define adsp_info(_dsp, fmt, ...) \
46 dev_info(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__)
47#define adsp_dbg(_dsp, fmt, ...) \
48 dev_dbg(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__)
49
50#define ADSP1_CONTROL_1 0x00
51#define ADSP1_CONTROL_2 0x02
52#define ADSP1_CONTROL_3 0x03
53#define ADSP1_CONTROL_4 0x04
54#define ADSP1_CONTROL_5 0x06
55#define ADSP1_CONTROL_6 0x07
56#define ADSP1_CONTROL_7 0x08
57#define ADSP1_CONTROL_8 0x09
58#define ADSP1_CONTROL_9 0x0A
59#define ADSP1_CONTROL_10 0x0B
60#define ADSP1_CONTROL_11 0x0C
61#define ADSP1_CONTROL_12 0x0D
62#define ADSP1_CONTROL_13 0x0F
63#define ADSP1_CONTROL_14 0x10
64#define ADSP1_CONTROL_15 0x11
65#define ADSP1_CONTROL_16 0x12
66#define ADSP1_CONTROL_17 0x13
67#define ADSP1_CONTROL_18 0x14
68#define ADSP1_CONTROL_19 0x16
69#define ADSP1_CONTROL_20 0x17
70#define ADSP1_CONTROL_21 0x18
71#define ADSP1_CONTROL_22 0x1A
72#define ADSP1_CONTROL_23 0x1B
73#define ADSP1_CONTROL_24 0x1C
74#define ADSP1_CONTROL_25 0x1E
75#define ADSP1_CONTROL_26 0x20
76#define ADSP1_CONTROL_27 0x21
77#define ADSP1_CONTROL_28 0x22
78#define ADSP1_CONTROL_29 0x23
79#define ADSP1_CONTROL_30 0x24
80#define ADSP1_CONTROL_31 0x26
81
82/*
83 * ADSP1 Control 19
84 */
85#define ADSP1_WDMA_BUFFER_LENGTH_MASK 0x00FF /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */
86#define ADSP1_WDMA_BUFFER_LENGTH_SHIFT 0 /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */
87#define ADSP1_WDMA_BUFFER_LENGTH_WIDTH 8 /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */
88
89
90/*
91 * ADSP1 Control 30
92 */
93#define ADSP1_DBG_CLK_ENA 0x0008 /* DSP1_DBG_CLK_ENA */
94#define ADSP1_DBG_CLK_ENA_MASK 0x0008 /* DSP1_DBG_CLK_ENA */
95#define ADSP1_DBG_CLK_ENA_SHIFT 3 /* DSP1_DBG_CLK_ENA */
96#define ADSP1_DBG_CLK_ENA_WIDTH 1 /* DSP1_DBG_CLK_ENA */
97#define ADSP1_SYS_ENA 0x0004 /* DSP1_SYS_ENA */
98#define ADSP1_SYS_ENA_MASK 0x0004 /* DSP1_SYS_ENA */
99#define ADSP1_SYS_ENA_SHIFT 2 /* DSP1_SYS_ENA */
100#define ADSP1_SYS_ENA_WIDTH 1 /* DSP1_SYS_ENA */
101#define ADSP1_CORE_ENA 0x0002 /* DSP1_CORE_ENA */
102#define ADSP1_CORE_ENA_MASK 0x0002 /* DSP1_CORE_ENA */
103#define ADSP1_CORE_ENA_SHIFT 1 /* DSP1_CORE_ENA */
104#define ADSP1_CORE_ENA_WIDTH 1 /* DSP1_CORE_ENA */
105#define ADSP1_START 0x0001 /* DSP1_START */
106#define ADSP1_START_MASK 0x0001 /* DSP1_START */
107#define ADSP1_START_SHIFT 0 /* DSP1_START */
108#define ADSP1_START_WIDTH 1 /* DSP1_START */
109
94e205bf
CR
110/*
111 * ADSP1 Control 31
112 */
113#define ADSP1_CLK_SEL_MASK 0x0007 /* CLK_SEL_ENA */
114#define ADSP1_CLK_SEL_SHIFT 0 /* CLK_SEL_ENA */
115#define ADSP1_CLK_SEL_WIDTH 3 /* CLK_SEL_ENA */
116
2d30b575
MB
117#define ADSP2_CONTROL 0x0
118#define ADSP2_CLOCKING 0x1
119#define ADSP2_STATUS1 0x4
120#define ADSP2_WDMA_CONFIG_1 0x30
121#define ADSP2_WDMA_CONFIG_2 0x31
122#define ADSP2_RDMA_CONFIG_1 0x34
2159ad93
MB
123
124/*
125 * ADSP2 Control
126 */
127
128#define ADSP2_MEM_ENA 0x0010 /* DSP1_MEM_ENA */
129#define ADSP2_MEM_ENA_MASK 0x0010 /* DSP1_MEM_ENA */
130#define ADSP2_MEM_ENA_SHIFT 4 /* DSP1_MEM_ENA */
131#define ADSP2_MEM_ENA_WIDTH 1 /* DSP1_MEM_ENA */
132#define ADSP2_SYS_ENA 0x0004 /* DSP1_SYS_ENA */
133#define ADSP2_SYS_ENA_MASK 0x0004 /* DSP1_SYS_ENA */
134#define ADSP2_SYS_ENA_SHIFT 2 /* DSP1_SYS_ENA */
135#define ADSP2_SYS_ENA_WIDTH 1 /* DSP1_SYS_ENA */
136#define ADSP2_CORE_ENA 0x0002 /* DSP1_CORE_ENA */
137#define ADSP2_CORE_ENA_MASK 0x0002 /* DSP1_CORE_ENA */
138#define ADSP2_CORE_ENA_SHIFT 1 /* DSP1_CORE_ENA */
139#define ADSP2_CORE_ENA_WIDTH 1 /* DSP1_CORE_ENA */
140#define ADSP2_START 0x0001 /* DSP1_START */
141#define ADSP2_START_MASK 0x0001 /* DSP1_START */
142#define ADSP2_START_SHIFT 0 /* DSP1_START */
143#define ADSP2_START_WIDTH 1 /* DSP1_START */
144
973838a0
MB
145/*
146 * ADSP2 clocking
147 */
148#define ADSP2_CLK_SEL_MASK 0x0007 /* CLK_SEL_ENA */
149#define ADSP2_CLK_SEL_SHIFT 0 /* CLK_SEL_ENA */
150#define ADSP2_CLK_SEL_WIDTH 3 /* CLK_SEL_ENA */
151
2159ad93
MB
152/*
153 * ADSP2 Status 1
154 */
155#define ADSP2_RAM_RDY 0x0001
156#define ADSP2_RAM_RDY_MASK 0x0001
157#define ADSP2_RAM_RDY_SHIFT 0
158#define ADSP2_RAM_RDY_WIDTH 1
159
cf17c83c
MB
160struct wm_adsp_buf {
161 struct list_head list;
162 void *buf;
163};
164
165static struct wm_adsp_buf *wm_adsp_buf_alloc(const void *src, size_t len,
166 struct list_head *list)
167{
168 struct wm_adsp_buf *buf = kzalloc(sizeof(*buf), GFP_KERNEL);
169
170 if (buf == NULL)
171 return NULL;
172
cdcd7f72 173 buf->buf = vmalloc(len);
cf17c83c 174 if (!buf->buf) {
cdcd7f72 175 vfree(buf);
cf17c83c
MB
176 return NULL;
177 }
cdcd7f72 178 memcpy(buf->buf, src, len);
cf17c83c
MB
179
180 if (list)
181 list_add_tail(&buf->list, list);
182
183 return buf;
184}
185
186static void wm_adsp_buf_free(struct list_head *list)
187{
188 while (!list_empty(list)) {
189 struct wm_adsp_buf *buf = list_first_entry(list,
190 struct wm_adsp_buf,
191 list);
192 list_del(&buf->list);
cdcd7f72 193 vfree(buf->buf);
cf17c83c
MB
194 kfree(buf);
195 }
196}
197
36e8fe99 198#define WM_ADSP_NUM_FW 4
1023dbd9 199
dd84f925
MB
200#define WM_ADSP_FW_MBC_VSS 0
201#define WM_ADSP_FW_TX 1
202#define WM_ADSP_FW_TX_SPK 2
203#define WM_ADSP_FW_RX_ANC 3
204
1023dbd9 205static const char *wm_adsp_fw_text[WM_ADSP_NUM_FW] = {
dd84f925
MB
206 [WM_ADSP_FW_MBC_VSS] = "MBC/VSS",
207 [WM_ADSP_FW_TX] = "Tx",
208 [WM_ADSP_FW_TX_SPK] = "Tx Speaker",
209 [WM_ADSP_FW_RX_ANC] = "Rx ANC",
1023dbd9
MB
210};
211
212static struct {
213 const char *file;
214} wm_adsp_fw[WM_ADSP_NUM_FW] = {
dd84f925
MB
215 [WM_ADSP_FW_MBC_VSS] = { .file = "mbc-vss" },
216 [WM_ADSP_FW_TX] = { .file = "tx" },
217 [WM_ADSP_FW_TX_SPK] = { .file = "tx-spk" },
218 [WM_ADSP_FW_RX_ANC] = { .file = "rx-anc" },
1023dbd9
MB
219};
220
6ab2b7b4
DP
221struct wm_coeff_ctl_ops {
222 int (*xget)(struct snd_kcontrol *kcontrol,
223 struct snd_ctl_elem_value *ucontrol);
224 int (*xput)(struct snd_kcontrol *kcontrol,
225 struct snd_ctl_elem_value *ucontrol);
226 int (*xinfo)(struct snd_kcontrol *kcontrol,
227 struct snd_ctl_elem_info *uinfo);
228};
229
6ab2b7b4
DP
230struct wm_coeff_ctl {
231 const char *name;
2323736d 232 const char *fw_name;
3809f001 233 struct wm_adsp_alg_region alg_region;
6ab2b7b4 234 struct wm_coeff_ctl_ops ops;
3809f001 235 struct wm_adsp *dsp;
6ab2b7b4
DP
236 unsigned int enabled:1;
237 struct list_head list;
238 void *cache;
2323736d 239 unsigned int offset;
6ab2b7b4 240 size_t len;
0c2e3f34 241 unsigned int set:1;
6ab2b7b4
DP
242 struct snd_kcontrol *kcontrol;
243};
244
1023dbd9
MB
245static int wm_adsp_fw_get(struct snd_kcontrol *kcontrol,
246 struct snd_ctl_elem_value *ucontrol)
247{
ea53bf77 248 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
1023dbd9 249 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3809f001 250 struct wm_adsp *dsp = snd_soc_codec_get_drvdata(codec);
1023dbd9 251
3809f001 252 ucontrol->value.integer.value[0] = dsp[e->shift_l].fw;
1023dbd9
MB
253
254 return 0;
255}
256
257static int wm_adsp_fw_put(struct snd_kcontrol *kcontrol,
258 struct snd_ctl_elem_value *ucontrol)
259{
ea53bf77 260 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
1023dbd9 261 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3809f001 262 struct wm_adsp *dsp = snd_soc_codec_get_drvdata(codec);
1023dbd9 263
3809f001 264 if (ucontrol->value.integer.value[0] == dsp[e->shift_l].fw)
1023dbd9
MB
265 return 0;
266
267 if (ucontrol->value.integer.value[0] >= WM_ADSP_NUM_FW)
268 return -EINVAL;
269
3809f001 270 if (dsp[e->shift_l].running)
1023dbd9
MB
271 return -EBUSY;
272
3809f001 273 dsp[e->shift_l].fw = ucontrol->value.integer.value[0];
1023dbd9
MB
274
275 return 0;
276}
277
278static const struct soc_enum wm_adsp_fw_enum[] = {
279 SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
280 SOC_ENUM_SINGLE(0, 1, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
281 SOC_ENUM_SINGLE(0, 2, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
282 SOC_ENUM_SINGLE(0, 3, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
283};
284
b6ed61cf 285const struct snd_kcontrol_new wm_adsp1_fw_controls[] = {
1023dbd9
MB
286 SOC_ENUM_EXT("DSP1 Firmware", wm_adsp_fw_enum[0],
287 wm_adsp_fw_get, wm_adsp_fw_put),
288 SOC_ENUM_EXT("DSP2 Firmware", wm_adsp_fw_enum[1],
289 wm_adsp_fw_get, wm_adsp_fw_put),
290 SOC_ENUM_EXT("DSP3 Firmware", wm_adsp_fw_enum[2],
291 wm_adsp_fw_get, wm_adsp_fw_put),
b6ed61cf
MB
292};
293EXPORT_SYMBOL_GPL(wm_adsp1_fw_controls);
294
295#if IS_ENABLED(CONFIG_SND_SOC_ARIZONA)
296static const struct soc_enum wm_adsp2_rate_enum[] = {
dc91428a
MB
297 SOC_VALUE_ENUM_SINGLE(ARIZONA_DSP1_CONTROL_1,
298 ARIZONA_DSP1_RATE_SHIFT, 0xf,
299 ARIZONA_RATE_ENUM_SIZE,
300 arizona_rate_text, arizona_rate_val),
301 SOC_VALUE_ENUM_SINGLE(ARIZONA_DSP2_CONTROL_1,
302 ARIZONA_DSP1_RATE_SHIFT, 0xf,
303 ARIZONA_RATE_ENUM_SIZE,
304 arizona_rate_text, arizona_rate_val),
305 SOC_VALUE_ENUM_SINGLE(ARIZONA_DSP3_CONTROL_1,
306 ARIZONA_DSP1_RATE_SHIFT, 0xf,
307 ARIZONA_RATE_ENUM_SIZE,
308 arizona_rate_text, arizona_rate_val),
5be9c5b4 309 SOC_VALUE_ENUM_SINGLE(ARIZONA_DSP4_CONTROL_1,
dc91428a
MB
310 ARIZONA_DSP1_RATE_SHIFT, 0xf,
311 ARIZONA_RATE_ENUM_SIZE,
312 arizona_rate_text, arizona_rate_val),
313};
314
b6ed61cf 315const struct snd_kcontrol_new wm_adsp2_fw_controls[] = {
1023dbd9
MB
316 SOC_ENUM_EXT("DSP1 Firmware", wm_adsp_fw_enum[0],
317 wm_adsp_fw_get, wm_adsp_fw_put),
b6ed61cf 318 SOC_ENUM("DSP1 Rate", wm_adsp2_rate_enum[0]),
1023dbd9
MB
319 SOC_ENUM_EXT("DSP2 Firmware", wm_adsp_fw_enum[1],
320 wm_adsp_fw_get, wm_adsp_fw_put),
b6ed61cf 321 SOC_ENUM("DSP2 Rate", wm_adsp2_rate_enum[1]),
1023dbd9
MB
322 SOC_ENUM_EXT("DSP3 Firmware", wm_adsp_fw_enum[2],
323 wm_adsp_fw_get, wm_adsp_fw_put),
b6ed61cf 324 SOC_ENUM("DSP3 Rate", wm_adsp2_rate_enum[2]),
1023dbd9
MB
325 SOC_ENUM_EXT("DSP4 Firmware", wm_adsp_fw_enum[3],
326 wm_adsp_fw_get, wm_adsp_fw_put),
b6ed61cf 327 SOC_ENUM("DSP4 Rate", wm_adsp2_rate_enum[3]),
1023dbd9 328};
b6ed61cf
MB
329EXPORT_SYMBOL_GPL(wm_adsp2_fw_controls);
330#endif
2159ad93
MB
331
332static struct wm_adsp_region const *wm_adsp_find_region(struct wm_adsp *dsp,
333 int type)
334{
335 int i;
336
337 for (i = 0; i < dsp->num_mems; i++)
338 if (dsp->mem[i].type == type)
339 return &dsp->mem[i];
340
341 return NULL;
342}
343
3809f001 344static unsigned int wm_adsp_region_to_reg(struct wm_adsp_region const *mem,
45b9ee72
MB
345 unsigned int offset)
346{
3809f001 347 if (WARN_ON(!mem))
6c452bda 348 return offset;
3809f001 349 switch (mem->type) {
45b9ee72 350 case WMFW_ADSP1_PM:
3809f001 351 return mem->base + (offset * 3);
45b9ee72 352 case WMFW_ADSP1_DM:
3809f001 353 return mem->base + (offset * 2);
45b9ee72 354 case WMFW_ADSP2_XM:
3809f001 355 return mem->base + (offset * 2);
45b9ee72 356 case WMFW_ADSP2_YM:
3809f001 357 return mem->base + (offset * 2);
45b9ee72 358 case WMFW_ADSP1_ZM:
3809f001 359 return mem->base + (offset * 2);
45b9ee72 360 default:
6c452bda 361 WARN(1, "Unknown memory region type");
45b9ee72
MB
362 return offset;
363 }
364}
365
6ab2b7b4
DP
366static int wm_coeff_info(struct snd_kcontrol *kcontrol,
367 struct snd_ctl_elem_info *uinfo)
368{
369 struct wm_coeff_ctl *ctl = (struct wm_coeff_ctl *)kcontrol->private_value;
370
371 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
372 uinfo->count = ctl->len;
373 return 0;
374}
375
c9f8dd71 376static int wm_coeff_write_control(struct wm_coeff_ctl *ctl,
6ab2b7b4
DP
377 const void *buf, size_t len)
378{
3809f001 379 struct wm_adsp_alg_region *alg_region = &ctl->alg_region;
6ab2b7b4 380 const struct wm_adsp_region *mem;
3809f001 381 struct wm_adsp *dsp = ctl->dsp;
6ab2b7b4
DP
382 void *scratch;
383 int ret;
384 unsigned int reg;
385
3809f001 386 mem = wm_adsp_find_region(dsp, alg_region->type);
6ab2b7b4 387 if (!mem) {
3809f001
CK
388 adsp_err(dsp, "No base for region %x\n",
389 alg_region->type);
6ab2b7b4
DP
390 return -EINVAL;
391 }
392
2323736d 393 reg = ctl->alg_region.base + ctl->offset;
6ab2b7b4
DP
394 reg = wm_adsp_region_to_reg(mem, reg);
395
396 scratch = kmemdup(buf, ctl->len, GFP_KERNEL | GFP_DMA);
397 if (!scratch)
398 return -ENOMEM;
399
3809f001 400 ret = regmap_raw_write(dsp->regmap, reg, scratch,
6ab2b7b4
DP
401 ctl->len);
402 if (ret) {
3809f001 403 adsp_err(dsp, "Failed to write %zu bytes to %x: %d\n",
43bc3bf6 404 ctl->len, reg, ret);
6ab2b7b4
DP
405 kfree(scratch);
406 return ret;
407 }
3809f001 408 adsp_dbg(dsp, "Wrote %zu bytes to %x\n", ctl->len, reg);
6ab2b7b4
DP
409
410 kfree(scratch);
411
412 return 0;
413}
414
415static int wm_coeff_put(struct snd_kcontrol *kcontrol,
416 struct snd_ctl_elem_value *ucontrol)
417{
418 struct wm_coeff_ctl *ctl = (struct wm_coeff_ctl *)kcontrol->private_value;
419 char *p = ucontrol->value.bytes.data;
420
421 memcpy(ctl->cache, p, ctl->len);
422
65d17a9c
NO
423 ctl->set = 1;
424 if (!ctl->enabled)
6ab2b7b4 425 return 0;
6ab2b7b4 426
c9f8dd71 427 return wm_coeff_write_control(ctl, p, ctl->len);
6ab2b7b4
DP
428}
429
c9f8dd71 430static int wm_coeff_read_control(struct wm_coeff_ctl *ctl,
6ab2b7b4
DP
431 void *buf, size_t len)
432{
3809f001 433 struct wm_adsp_alg_region *alg_region = &ctl->alg_region;
6ab2b7b4 434 const struct wm_adsp_region *mem;
3809f001 435 struct wm_adsp *dsp = ctl->dsp;
6ab2b7b4
DP
436 void *scratch;
437 int ret;
438 unsigned int reg;
439
3809f001 440 mem = wm_adsp_find_region(dsp, alg_region->type);
6ab2b7b4 441 if (!mem) {
3809f001
CK
442 adsp_err(dsp, "No base for region %x\n",
443 alg_region->type);
6ab2b7b4
DP
444 return -EINVAL;
445 }
446
2323736d 447 reg = ctl->alg_region.base + ctl->offset;
6ab2b7b4
DP
448 reg = wm_adsp_region_to_reg(mem, reg);
449
450 scratch = kmalloc(ctl->len, GFP_KERNEL | GFP_DMA);
451 if (!scratch)
452 return -ENOMEM;
453
3809f001 454 ret = regmap_raw_read(dsp->regmap, reg, scratch, ctl->len);
6ab2b7b4 455 if (ret) {
3809f001 456 adsp_err(dsp, "Failed to read %zu bytes from %x: %d\n",
43bc3bf6 457 ctl->len, reg, ret);
6ab2b7b4
DP
458 kfree(scratch);
459 return ret;
460 }
3809f001 461 adsp_dbg(dsp, "Read %zu bytes from %x\n", ctl->len, reg);
6ab2b7b4
DP
462
463 memcpy(buf, scratch, ctl->len);
464 kfree(scratch);
465
466 return 0;
467}
468
469static int wm_coeff_get(struct snd_kcontrol *kcontrol,
470 struct snd_ctl_elem_value *ucontrol)
471{
472 struct wm_coeff_ctl *ctl = (struct wm_coeff_ctl *)kcontrol->private_value;
473 char *p = ucontrol->value.bytes.data;
474
475 memcpy(p, ctl->cache, ctl->len);
476 return 0;
477}
478
6ab2b7b4 479struct wmfw_ctl_work {
3809f001 480 struct wm_adsp *dsp;
6ab2b7b4
DP
481 struct wm_coeff_ctl *ctl;
482 struct work_struct work;
483};
484
3809f001 485static int wmfw_add_ctl(struct wm_adsp *dsp, struct wm_coeff_ctl *ctl)
6ab2b7b4
DP
486{
487 struct snd_kcontrol_new *kcontrol;
488 int ret;
489
92bb4c32 490 if (!ctl || !ctl->name)
6ab2b7b4
DP
491 return -EINVAL;
492
493 kcontrol = kzalloc(sizeof(*kcontrol), GFP_KERNEL);
494 if (!kcontrol)
495 return -ENOMEM;
496 kcontrol->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
497
498 kcontrol->name = ctl->name;
499 kcontrol->info = wm_coeff_info;
500 kcontrol->get = wm_coeff_get;
501 kcontrol->put = wm_coeff_put;
502 kcontrol->private_value = (unsigned long)ctl;
503
3809f001 504 ret = snd_soc_add_card_controls(dsp->card,
81ad93ec 505 kcontrol, 1);
6ab2b7b4
DP
506 if (ret < 0)
507 goto err_kcontrol;
508
509 kfree(kcontrol);
510
3809f001 511 ctl->kcontrol = snd_soc_card_get_kcontrol(dsp->card,
81ad93ec
DP
512 ctl->name);
513
6ab2b7b4
DP
514 return 0;
515
516err_kcontrol:
517 kfree(kcontrol);
518 return ret;
519}
520
b21acc1c
CK
521static int wm_coeff_init_control_caches(struct wm_adsp *dsp)
522{
523 struct wm_coeff_ctl *ctl;
524 int ret;
525
526 list_for_each_entry(ctl, &dsp->ctl_list, list) {
527 if (!ctl->enabled || ctl->set)
528 continue;
529 ret = wm_coeff_read_control(ctl,
530 ctl->cache,
531 ctl->len);
532 if (ret < 0)
533 return ret;
534 }
535
536 return 0;
537}
538
539static int wm_coeff_sync_controls(struct wm_adsp *dsp)
540{
541 struct wm_coeff_ctl *ctl;
542 int ret;
543
544 list_for_each_entry(ctl, &dsp->ctl_list, list) {
545 if (!ctl->enabled)
546 continue;
547 if (ctl->set) {
548 ret = wm_coeff_write_control(ctl,
549 ctl->cache,
550 ctl->len);
551 if (ret < 0)
552 return ret;
553 }
554 }
555
556 return 0;
557}
558
559static void wm_adsp_ctl_work(struct work_struct *work)
560{
561 struct wmfw_ctl_work *ctl_work = container_of(work,
562 struct wmfw_ctl_work,
563 work);
564
565 wmfw_add_ctl(ctl_work->dsp, ctl_work->ctl);
566 kfree(ctl_work);
567}
568
569static int wm_adsp_create_control(struct wm_adsp *dsp,
570 const struct wm_adsp_alg_region *alg_region,
2323736d
CK
571 unsigned int offset, unsigned int len,
572 const char *subname, unsigned int subname_len)
b21acc1c
CK
573{
574 struct wm_coeff_ctl *ctl;
575 struct wmfw_ctl_work *ctl_work;
576 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
577 char *region_name;
578 int ret;
579
580 switch (alg_region->type) {
581 case WMFW_ADSP1_PM:
582 region_name = "PM";
583 break;
584 case WMFW_ADSP1_DM:
585 region_name = "DM";
586 break;
587 case WMFW_ADSP2_XM:
588 region_name = "XM";
589 break;
590 case WMFW_ADSP2_YM:
591 region_name = "YM";
592 break;
593 case WMFW_ADSP1_ZM:
594 region_name = "ZM";
595 break;
596 default:
2323736d 597 adsp_err(dsp, "Unknown region type: %d\n", alg_region->type);
b21acc1c
CK
598 return -EINVAL;
599 }
600
cb5b57a9
CK
601 switch (dsp->fw_ver) {
602 case 0:
603 case 1:
604 snprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "DSP%d %s %x",
605 dsp->num, region_name, alg_region->alg);
606 break;
607 default:
608 ret = snprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN,
609 "DSP%d%c %.12s %x", dsp->num, *region_name,
610 wm_adsp_fw_text[dsp->fw], alg_region->alg);
611
612 /* Truncate the subname from the start if it is too long */
613 if (subname) {
614 int avail = SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret - 2;
615 int skip = 0;
616
617 if (subname_len > avail)
618 skip = subname_len - avail;
619
620 snprintf(name + ret,
621 SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret, " %.*s",
622 subname_len - skip, subname + skip);
623 }
624 break;
625 }
b21acc1c
CK
626
627 list_for_each_entry(ctl, &dsp->ctl_list,
628 list) {
629 if (!strcmp(ctl->name, name)) {
630 if (!ctl->enabled)
631 ctl->enabled = 1;
632 return 0;
633 }
634 }
635
636 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
637 if (!ctl)
638 return -ENOMEM;
2323736d 639 ctl->fw_name = wm_adsp_fw_text[dsp->fw];
b21acc1c
CK
640 ctl->alg_region = *alg_region;
641 ctl->name = kmemdup(name, strlen(name) + 1, GFP_KERNEL);
642 if (!ctl->name) {
643 ret = -ENOMEM;
644 goto err_ctl;
645 }
646 ctl->enabled = 1;
647 ctl->set = 0;
648 ctl->ops.xget = wm_coeff_get;
649 ctl->ops.xput = wm_coeff_put;
650 ctl->dsp = dsp;
651
2323736d 652 ctl->offset = offset;
b21acc1c
CK
653 if (len > 512) {
654 adsp_warn(dsp, "Truncating control %s from %d\n",
655 ctl->name, len);
656 len = 512;
657 }
658 ctl->len = len;
659 ctl->cache = kzalloc(ctl->len, GFP_KERNEL);
660 if (!ctl->cache) {
661 ret = -ENOMEM;
662 goto err_ctl_name;
663 }
664
2323736d
CK
665 list_add(&ctl->list, &dsp->ctl_list);
666
b21acc1c
CK
667 ctl_work = kzalloc(sizeof(*ctl_work), GFP_KERNEL);
668 if (!ctl_work) {
669 ret = -ENOMEM;
670 goto err_ctl_cache;
671 }
672
673 ctl_work->dsp = dsp;
674 ctl_work->ctl = ctl;
675 INIT_WORK(&ctl_work->work, wm_adsp_ctl_work);
676 schedule_work(&ctl_work->work);
677
678 return 0;
679
680err_ctl_cache:
681 kfree(ctl->cache);
682err_ctl_name:
683 kfree(ctl->name);
684err_ctl:
685 kfree(ctl);
686
687 return ret;
688}
689
2323736d
CK
690struct wm_coeff_parsed_alg {
691 int id;
692 const u8 *name;
693 int name_len;
694 int ncoeff;
695};
696
697struct wm_coeff_parsed_coeff {
698 int offset;
699 int mem_type;
700 const u8 *name;
701 int name_len;
702 int ctl_type;
703 int flags;
704 int len;
705};
706
cb5b57a9
CK
707static int wm_coeff_parse_string(int bytes, const u8 **pos, const u8 **str)
708{
709 int length;
710
711 switch (bytes) {
712 case 1:
713 length = **pos;
714 break;
715 case 2:
716 length = le16_to_cpu(*((u16 *)*pos));
717 break;
718 default:
719 return 0;
720 }
721
722 if (str)
723 *str = *pos + bytes;
724
725 *pos += ((length + bytes) + 3) & ~0x03;
726
727 return length;
728}
729
730static int wm_coeff_parse_int(int bytes, const u8 **pos)
731{
732 int val = 0;
733
734 switch (bytes) {
735 case 2:
736 val = le16_to_cpu(*((u16 *)*pos));
737 break;
738 case 4:
739 val = le32_to_cpu(*((u32 *)*pos));
740 break;
741 default:
742 break;
743 }
744
745 *pos += bytes;
746
747 return val;
748}
749
2323736d
CK
750static inline void wm_coeff_parse_alg(struct wm_adsp *dsp, const u8 **data,
751 struct wm_coeff_parsed_alg *blk)
752{
753 const struct wmfw_adsp_alg_data *raw;
754
cb5b57a9
CK
755 switch (dsp->fw_ver) {
756 case 0:
757 case 1:
758 raw = (const struct wmfw_adsp_alg_data *)*data;
759 *data = raw->data;
2323736d 760
cb5b57a9
CK
761 blk->id = le32_to_cpu(raw->id);
762 blk->name = raw->name;
763 blk->name_len = strlen(raw->name);
764 blk->ncoeff = le32_to_cpu(raw->ncoeff);
765 break;
766 default:
767 blk->id = wm_coeff_parse_int(sizeof(raw->id), data);
768 blk->name_len = wm_coeff_parse_string(sizeof(u8), data,
769 &blk->name);
770 wm_coeff_parse_string(sizeof(u16), data, NULL);
771 blk->ncoeff = wm_coeff_parse_int(sizeof(raw->ncoeff), data);
772 break;
773 }
2323736d
CK
774
775 adsp_dbg(dsp, "Algorithm ID: %#x\n", blk->id);
776 adsp_dbg(dsp, "Algorithm name: %.*s\n", blk->name_len, blk->name);
777 adsp_dbg(dsp, "# of coefficient descriptors: %#x\n", blk->ncoeff);
778}
779
780static inline void wm_coeff_parse_coeff(struct wm_adsp *dsp, const u8 **data,
781 struct wm_coeff_parsed_coeff *blk)
782{
783 const struct wmfw_adsp_coeff_data *raw;
cb5b57a9
CK
784 const u8 *tmp;
785 int length;
2323736d 786
cb5b57a9
CK
787 switch (dsp->fw_ver) {
788 case 0:
789 case 1:
790 raw = (const struct wmfw_adsp_coeff_data *)*data;
791 *data = *data + sizeof(raw->hdr) + le32_to_cpu(raw->hdr.size);
792
793 blk->offset = le16_to_cpu(raw->hdr.offset);
794 blk->mem_type = le16_to_cpu(raw->hdr.type);
795 blk->name = raw->name;
796 blk->name_len = strlen(raw->name);
797 blk->ctl_type = le16_to_cpu(raw->ctl_type);
798 blk->flags = le16_to_cpu(raw->flags);
799 blk->len = le32_to_cpu(raw->len);
800 break;
801 default:
802 tmp = *data;
803 blk->offset = wm_coeff_parse_int(sizeof(raw->hdr.offset), &tmp);
804 blk->mem_type = wm_coeff_parse_int(sizeof(raw->hdr.type), &tmp);
805 length = wm_coeff_parse_int(sizeof(raw->hdr.size), &tmp);
806 blk->name_len = wm_coeff_parse_string(sizeof(u8), &tmp,
807 &blk->name);
808 wm_coeff_parse_string(sizeof(u8), &tmp, NULL);
809 wm_coeff_parse_string(sizeof(u16), &tmp, NULL);
810 blk->ctl_type = wm_coeff_parse_int(sizeof(raw->ctl_type), &tmp);
811 blk->flags = wm_coeff_parse_int(sizeof(raw->flags), &tmp);
812 blk->len = wm_coeff_parse_int(sizeof(raw->len), &tmp);
813
814 *data = *data + sizeof(raw->hdr) + length;
815 break;
816 }
2323736d
CK
817
818 adsp_dbg(dsp, "\tCoefficient type: %#x\n", blk->mem_type);
819 adsp_dbg(dsp, "\tCoefficient offset: %#x\n", blk->offset);
820 adsp_dbg(dsp, "\tCoefficient name: %.*s\n", blk->name_len, blk->name);
821 adsp_dbg(dsp, "\tCoefficient flags: %#x\n", blk->flags);
822 adsp_dbg(dsp, "\tALSA control type: %#x\n", blk->ctl_type);
823 adsp_dbg(dsp, "\tALSA control len: %#x\n", blk->len);
824}
825
826static int wm_adsp_parse_coeff(struct wm_adsp *dsp,
827 const struct wmfw_region *region)
828{
829 struct wm_adsp_alg_region alg_region = {};
830 struct wm_coeff_parsed_alg alg_blk;
831 struct wm_coeff_parsed_coeff coeff_blk;
832 const u8 *data = region->data;
833 int i, ret;
834
835 wm_coeff_parse_alg(dsp, &data, &alg_blk);
836 for (i = 0; i < alg_blk.ncoeff; i++) {
837 wm_coeff_parse_coeff(dsp, &data, &coeff_blk);
838
839 switch (coeff_blk.ctl_type) {
840 case SNDRV_CTL_ELEM_TYPE_BYTES:
841 break;
842 default:
843 adsp_err(dsp, "Unknown control type: %d\n",
844 coeff_blk.ctl_type);
845 return -EINVAL;
846 }
847
848 alg_region.type = coeff_blk.mem_type;
849 alg_region.alg = alg_blk.id;
850
851 ret = wm_adsp_create_control(dsp, &alg_region,
852 coeff_blk.offset,
853 coeff_blk.len,
854 coeff_blk.name,
855 coeff_blk.name_len);
856 if (ret < 0)
857 adsp_err(dsp, "Failed to create control: %.*s, %d\n",
858 coeff_blk.name_len, coeff_blk.name, ret);
859 }
860
861 return 0;
862}
863
2159ad93
MB
864static int wm_adsp_load(struct wm_adsp *dsp)
865{
cf17c83c 866 LIST_HEAD(buf_list);
2159ad93
MB
867 const struct firmware *firmware;
868 struct regmap *regmap = dsp->regmap;
869 unsigned int pos = 0;
870 const struct wmfw_header *header;
871 const struct wmfw_adsp1_sizes *adsp1_sizes;
872 const struct wmfw_adsp2_sizes *adsp2_sizes;
873 const struct wmfw_footer *footer;
874 const struct wmfw_region *region;
875 const struct wm_adsp_region *mem;
876 const char *region_name;
877 char *file, *text;
cf17c83c 878 struct wm_adsp_buf *buf;
2159ad93
MB
879 unsigned int reg;
880 int regions = 0;
881 int ret, offset, type, sizes;
882
883 file = kzalloc(PAGE_SIZE, GFP_KERNEL);
884 if (file == NULL)
885 return -ENOMEM;
886
1023dbd9
MB
887 snprintf(file, PAGE_SIZE, "%s-dsp%d-%s.wmfw", dsp->part, dsp->num,
888 wm_adsp_fw[dsp->fw].file);
2159ad93
MB
889 file[PAGE_SIZE - 1] = '\0';
890
891 ret = request_firmware(&firmware, file, dsp->dev);
892 if (ret != 0) {
893 adsp_err(dsp, "Failed to request '%s'\n", file);
894 goto out;
895 }
896 ret = -EINVAL;
897
898 pos = sizeof(*header) + sizeof(*adsp1_sizes) + sizeof(*footer);
899 if (pos >= firmware->size) {
900 adsp_err(dsp, "%s: file too short, %zu bytes\n",
901 file, firmware->size);
902 goto out_fw;
903 }
904
905 header = (void*)&firmware->data[0];
906
907 if (memcmp(&header->magic[0], "WMFW", 4) != 0) {
908 adsp_err(dsp, "%s: invalid magic\n", file);
909 goto out_fw;
910 }
911
2323736d
CK
912 switch (header->ver) {
913 case 0:
c61e59fe
CK
914 adsp_warn(dsp, "%s: Depreciated file format %d\n",
915 file, header->ver);
916 break;
2323736d 917 case 1:
cb5b57a9 918 case 2:
2323736d
CK
919 break;
920 default:
2159ad93
MB
921 adsp_err(dsp, "%s: unknown file format %d\n",
922 file, header->ver);
923 goto out_fw;
924 }
2323736d 925
3626992a 926 adsp_info(dsp, "Firmware version: %d\n", header->ver);
2323736d 927 dsp->fw_ver = header->ver;
2159ad93
MB
928
929 if (header->core != dsp->type) {
930 adsp_err(dsp, "%s: invalid core %d != %d\n",
931 file, header->core, dsp->type);
932 goto out_fw;
933 }
934
935 switch (dsp->type) {
936 case WMFW_ADSP1:
937 pos = sizeof(*header) + sizeof(*adsp1_sizes) + sizeof(*footer);
938 adsp1_sizes = (void *)&(header[1]);
939 footer = (void *)&(adsp1_sizes[1]);
940 sizes = sizeof(*adsp1_sizes);
941
942 adsp_dbg(dsp, "%s: %d DM, %d PM, %d ZM\n",
943 file, le32_to_cpu(adsp1_sizes->dm),
944 le32_to_cpu(adsp1_sizes->pm),
945 le32_to_cpu(adsp1_sizes->zm));
946 break;
947
948 case WMFW_ADSP2:
949 pos = sizeof(*header) + sizeof(*adsp2_sizes) + sizeof(*footer);
950 adsp2_sizes = (void *)&(header[1]);
951 footer = (void *)&(adsp2_sizes[1]);
952 sizes = sizeof(*adsp2_sizes);
953
954 adsp_dbg(dsp, "%s: %d XM, %d YM %d PM, %d ZM\n",
955 file, le32_to_cpu(adsp2_sizes->xm),
956 le32_to_cpu(adsp2_sizes->ym),
957 le32_to_cpu(adsp2_sizes->pm),
958 le32_to_cpu(adsp2_sizes->zm));
959 break;
960
961 default:
6c452bda 962 WARN(1, "Unknown DSP type");
2159ad93
MB
963 goto out_fw;
964 }
965
966 if (le32_to_cpu(header->len) != sizeof(*header) +
967 sizes + sizeof(*footer)) {
968 adsp_err(dsp, "%s: unexpected header length %d\n",
969 file, le32_to_cpu(header->len));
970 goto out_fw;
971 }
972
973 adsp_dbg(dsp, "%s: timestamp %llu\n", file,
974 le64_to_cpu(footer->timestamp));
975
976 while (pos < firmware->size &&
977 pos - firmware->size > sizeof(*region)) {
978 region = (void *)&(firmware->data[pos]);
979 region_name = "Unknown";
980 reg = 0;
981 text = NULL;
982 offset = le32_to_cpu(region->offset) & 0xffffff;
983 type = be32_to_cpu(region->type) & 0xff;
984 mem = wm_adsp_find_region(dsp, type);
985
986 switch (type) {
987 case WMFW_NAME_TEXT:
988 region_name = "Firmware name";
989 text = kzalloc(le32_to_cpu(region->len) + 1,
990 GFP_KERNEL);
991 break;
2323736d
CK
992 case WMFW_ALGORITHM_DATA:
993 region_name = "Algorithm";
994 ret = wm_adsp_parse_coeff(dsp, region);
995 if (ret != 0)
996 goto out_fw;
997 break;
2159ad93
MB
998 case WMFW_INFO_TEXT:
999 region_name = "Information";
1000 text = kzalloc(le32_to_cpu(region->len) + 1,
1001 GFP_KERNEL);
1002 break;
1003 case WMFW_ABSOLUTE:
1004 region_name = "Absolute";
1005 reg = offset;
1006 break;
1007 case WMFW_ADSP1_PM:
2159ad93 1008 region_name = "PM";
45b9ee72 1009 reg = wm_adsp_region_to_reg(mem, offset);
2159ad93
MB
1010 break;
1011 case WMFW_ADSP1_DM:
2159ad93 1012 region_name = "DM";
45b9ee72 1013 reg = wm_adsp_region_to_reg(mem, offset);
2159ad93
MB
1014 break;
1015 case WMFW_ADSP2_XM:
2159ad93 1016 region_name = "XM";
45b9ee72 1017 reg = wm_adsp_region_to_reg(mem, offset);
2159ad93
MB
1018 break;
1019 case WMFW_ADSP2_YM:
2159ad93 1020 region_name = "YM";
45b9ee72 1021 reg = wm_adsp_region_to_reg(mem, offset);
2159ad93
MB
1022 break;
1023 case WMFW_ADSP1_ZM:
2159ad93 1024 region_name = "ZM";
45b9ee72 1025 reg = wm_adsp_region_to_reg(mem, offset);
2159ad93
MB
1026 break;
1027 default:
1028 adsp_warn(dsp,
1029 "%s.%d: Unknown region type %x at %d(%x)\n",
1030 file, regions, type, pos, pos);
1031 break;
1032 }
1033
1034 adsp_dbg(dsp, "%s.%d: %d bytes at %d in %s\n", file,
1035 regions, le32_to_cpu(region->len), offset,
1036 region_name);
1037
1038 if (text) {
1039 memcpy(text, region->data, le32_to_cpu(region->len));
1040 adsp_info(dsp, "%s: %s\n", file, text);
1041 kfree(text);
1042 }
1043
1044 if (reg) {
cdcd7f72
CK
1045 buf = wm_adsp_buf_alloc(region->data,
1046 le32_to_cpu(region->len),
1047 &buf_list);
1048 if (!buf) {
1049 adsp_err(dsp, "Out of memory\n");
1050 ret = -ENOMEM;
1051 goto out_fw;
1052 }
c1a7898d 1053
cdcd7f72
CK
1054 ret = regmap_raw_write_async(regmap, reg, buf->buf,
1055 le32_to_cpu(region->len));
1056 if (ret != 0) {
1057 adsp_err(dsp,
1058 "%s.%d: Failed to write %d bytes at %d in %s: %d\n",
1059 file, regions,
1060 le32_to_cpu(region->len), offset,
1061 region_name, ret);
1062 goto out_fw;
2159ad93
MB
1063 }
1064 }
1065
1066 pos += le32_to_cpu(region->len) + sizeof(*region);
1067 regions++;
1068 }
cf17c83c
MB
1069
1070 ret = regmap_async_complete(regmap);
1071 if (ret != 0) {
1072 adsp_err(dsp, "Failed to complete async write: %d\n", ret);
1073 goto out_fw;
1074 }
1075
2159ad93
MB
1076 if (pos > firmware->size)
1077 adsp_warn(dsp, "%s.%d: %zu bytes at end of file\n",
1078 file, regions, pos - firmware->size);
1079
1080out_fw:
cf17c83c
MB
1081 regmap_async_complete(regmap);
1082 wm_adsp_buf_free(&buf_list);
2159ad93
MB
1083 release_firmware(firmware);
1084out:
1085 kfree(file);
1086
1087 return ret;
1088}
1089
2323736d
CK
1090static void wm_adsp_ctl_fixup_base(struct wm_adsp *dsp,
1091 const struct wm_adsp_alg_region *alg_region)
1092{
1093 struct wm_coeff_ctl *ctl;
1094
1095 list_for_each_entry(ctl, &dsp->ctl_list, list) {
1096 if (ctl->fw_name == wm_adsp_fw_text[dsp->fw] &&
1097 alg_region->alg == ctl->alg_region.alg &&
1098 alg_region->type == ctl->alg_region.type) {
1099 ctl->alg_region.base = alg_region->base;
1100 }
1101 }
1102}
1103
3809f001 1104static void *wm_adsp_read_algs(struct wm_adsp *dsp, size_t n_algs,
b618a185 1105 unsigned int pos, unsigned int len)
db40517c 1106{
b618a185
CK
1107 void *alg;
1108 int ret;
db40517c 1109 __be32 val;
db40517c 1110
3809f001 1111 if (n_algs == 0) {
b618a185
CK
1112 adsp_err(dsp, "No algorithms\n");
1113 return ERR_PTR(-EINVAL);
db40517c
MB
1114 }
1115
3809f001
CK
1116 if (n_algs > 1024) {
1117 adsp_err(dsp, "Algorithm count %zx excessive\n", n_algs);
b618a185
CK
1118 return ERR_PTR(-EINVAL);
1119 }
db40517c 1120
b618a185
CK
1121 /* Read the terminator first to validate the length */
1122 ret = regmap_raw_read(dsp->regmap, pos + len, &val, sizeof(val));
1123 if (ret != 0) {
1124 adsp_err(dsp, "Failed to read algorithm list end: %d\n",
1125 ret);
1126 return ERR_PTR(ret);
1127 }
db40517c 1128
b618a185
CK
1129 if (be32_to_cpu(val) != 0xbedead)
1130 adsp_warn(dsp, "Algorithm list end %x 0x%x != 0xbeadead\n",
1131 pos + len, be32_to_cpu(val));
d62f4bc6 1132
b618a185
CK
1133 alg = kzalloc(len * 2, GFP_KERNEL | GFP_DMA);
1134 if (!alg)
1135 return ERR_PTR(-ENOMEM);
db40517c 1136
b618a185
CK
1137 ret = regmap_raw_read(dsp->regmap, pos, alg, len * 2);
1138 if (ret != 0) {
1139 adsp_err(dsp, "Failed to read algorithm list: %d\n",
1140 ret);
1141 kfree(alg);
1142 return ERR_PTR(ret);
1143 }
ac50009f 1144
b618a185
CK
1145 return alg;
1146}
ac50009f 1147
d9d20e17
CK
1148static struct wm_adsp_alg_region *wm_adsp_create_region(struct wm_adsp *dsp,
1149 int type, __be32 id,
1150 __be32 base)
1151{
1152 struct wm_adsp_alg_region *alg_region;
1153
1154 alg_region = kzalloc(sizeof(*alg_region), GFP_KERNEL);
1155 if (!alg_region)
1156 return ERR_PTR(-ENOMEM);
1157
1158 alg_region->type = type;
1159 alg_region->alg = be32_to_cpu(id);
1160 alg_region->base = be32_to_cpu(base);
1161
1162 list_add_tail(&alg_region->list, &dsp->alg_regions);
1163
2323736d
CK
1164 if (dsp->fw_ver > 0)
1165 wm_adsp_ctl_fixup_base(dsp, alg_region);
1166
d9d20e17
CK
1167 return alg_region;
1168}
1169
b618a185
CK
1170static int wm_adsp1_setup_algs(struct wm_adsp *dsp)
1171{
1172 struct wmfw_adsp1_id_hdr adsp1_id;
1173 struct wmfw_adsp1_alg_hdr *adsp1_alg;
3809f001 1174 struct wm_adsp_alg_region *alg_region;
b618a185
CK
1175 const struct wm_adsp_region *mem;
1176 unsigned int pos, len;
3809f001 1177 size_t n_algs;
b618a185 1178 int i, ret;
db40517c 1179
b618a185
CK
1180 mem = wm_adsp_find_region(dsp, WMFW_ADSP1_DM);
1181 if (WARN_ON(!mem))
1182 return -EINVAL;
1183
1184 ret = regmap_raw_read(dsp->regmap, mem->base, &adsp1_id,
1185 sizeof(adsp1_id));
1186 if (ret != 0) {
1187 adsp_err(dsp, "Failed to read algorithm info: %d\n",
1188 ret);
1189 return ret;
1190 }
db40517c 1191
3809f001 1192 n_algs = be32_to_cpu(adsp1_id.n_algs);
b618a185
CK
1193 dsp->fw_id = be32_to_cpu(adsp1_id.fw.id);
1194 adsp_info(dsp, "Firmware: %x v%d.%d.%d, %zu algorithms\n",
1195 dsp->fw_id,
1196 (be32_to_cpu(adsp1_id.fw.ver) & 0xff0000) >> 16,
1197 (be32_to_cpu(adsp1_id.fw.ver) & 0xff00) >> 8,
1198 be32_to_cpu(adsp1_id.fw.ver) & 0xff,
3809f001 1199 n_algs);
b618a185 1200
d9d20e17
CK
1201 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_ZM,
1202 adsp1_id.fw.id, adsp1_id.zm);
1203 if (IS_ERR(alg_region))
1204 return PTR_ERR(alg_region);
d62f4bc6 1205
d9d20e17
CK
1206 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_DM,
1207 adsp1_id.fw.id, adsp1_id.dm);
1208 if (IS_ERR(alg_region))
1209 return PTR_ERR(alg_region);
db40517c 1210
b618a185 1211 pos = sizeof(adsp1_id) / 2;
3809f001 1212 len = (sizeof(*adsp1_alg) * n_algs) / 2;
b618a185 1213
3809f001 1214 adsp1_alg = wm_adsp_read_algs(dsp, n_algs, mem->base + pos, len);
b618a185
CK
1215 if (IS_ERR(adsp1_alg))
1216 return PTR_ERR(adsp1_alg);
1217
3809f001 1218 for (i = 0; i < n_algs; i++) {
b618a185
CK
1219 adsp_info(dsp, "%d: ID %x v%d.%d.%d DM@%x ZM@%x\n",
1220 i, be32_to_cpu(adsp1_alg[i].alg.id),
1221 (be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff0000) >> 16,
1222 (be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff00) >> 8,
1223 be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff,
1224 be32_to_cpu(adsp1_alg[i].dm),
1225 be32_to_cpu(adsp1_alg[i].zm));
ac50009f 1226
d9d20e17
CK
1227 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_DM,
1228 adsp1_alg[i].alg.id,
1229 adsp1_alg[i].dm);
1230 if (IS_ERR(alg_region)) {
1231 ret = PTR_ERR(alg_region);
b618a185
CK
1232 goto out;
1233 }
2323736d
CK
1234 if (dsp->fw_ver == 0) {
1235 if (i + 1 < n_algs) {
1236 len = be32_to_cpu(adsp1_alg[i + 1].dm);
1237 len -= be32_to_cpu(adsp1_alg[i].dm);
1238 len *= 4;
1239 wm_adsp_create_control(dsp, alg_region, 0,
1240 len, NULL, 0);
1241 } else {
1242 adsp_warn(dsp, "Missing length info for region DM with ID %x\n",
1243 be32_to_cpu(adsp1_alg[i].alg.id));
1244 }
b618a185 1245 }
ac50009f 1246
d9d20e17
CK
1247 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP1_ZM,
1248 adsp1_alg[i].alg.id,
1249 adsp1_alg[i].zm);
1250 if (IS_ERR(alg_region)) {
1251 ret = PTR_ERR(alg_region);
b618a185
CK
1252 goto out;
1253 }
2323736d
CK
1254 if (dsp->fw_ver == 0) {
1255 if (i + 1 < n_algs) {
1256 len = be32_to_cpu(adsp1_alg[i + 1].zm);
1257 len -= be32_to_cpu(adsp1_alg[i].zm);
1258 len *= 4;
1259 wm_adsp_create_control(dsp, alg_region, 0,
1260 len, NULL, 0);
1261 } else {
1262 adsp_warn(dsp, "Missing length info for region ZM with ID %x\n",
1263 be32_to_cpu(adsp1_alg[i].alg.id));
1264 }
b618a185 1265 }
db40517c
MB
1266 }
1267
b618a185
CK
1268out:
1269 kfree(adsp1_alg);
1270 return ret;
1271}
db40517c 1272
b618a185
CK
1273static int wm_adsp2_setup_algs(struct wm_adsp *dsp)
1274{
1275 struct wmfw_adsp2_id_hdr adsp2_id;
1276 struct wmfw_adsp2_alg_hdr *adsp2_alg;
3809f001 1277 struct wm_adsp_alg_region *alg_region;
b618a185
CK
1278 const struct wm_adsp_region *mem;
1279 unsigned int pos, len;
3809f001 1280 size_t n_algs;
b618a185
CK
1281 int i, ret;
1282
1283 mem = wm_adsp_find_region(dsp, WMFW_ADSP2_XM);
1284 if (WARN_ON(!mem))
d62f4bc6 1285 return -EINVAL;
d62f4bc6 1286
b618a185
CK
1287 ret = regmap_raw_read(dsp->regmap, mem->base, &adsp2_id,
1288 sizeof(adsp2_id));
db40517c 1289 if (ret != 0) {
b618a185
CK
1290 adsp_err(dsp, "Failed to read algorithm info: %d\n",
1291 ret);
db40517c
MB
1292 return ret;
1293 }
1294
3809f001 1295 n_algs = be32_to_cpu(adsp2_id.n_algs);
b618a185
CK
1296 dsp->fw_id = be32_to_cpu(adsp2_id.fw.id);
1297 adsp_info(dsp, "Firmware: %x v%d.%d.%d, %zu algorithms\n",
1298 dsp->fw_id,
1299 (be32_to_cpu(adsp2_id.fw.ver) & 0xff0000) >> 16,
1300 (be32_to_cpu(adsp2_id.fw.ver) & 0xff00) >> 8,
1301 be32_to_cpu(adsp2_id.fw.ver) & 0xff,
3809f001 1302 n_algs);
b618a185 1303
d9d20e17
CK
1304 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_XM,
1305 adsp2_id.fw.id, adsp2_id.xm);
1306 if (IS_ERR(alg_region))
1307 return PTR_ERR(alg_region);
db40517c 1308
d9d20e17
CK
1309 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_YM,
1310 adsp2_id.fw.id, adsp2_id.ym);
1311 if (IS_ERR(alg_region))
1312 return PTR_ERR(alg_region);
db40517c 1313
d9d20e17
CK
1314 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_ZM,
1315 adsp2_id.fw.id, adsp2_id.zm);
1316 if (IS_ERR(alg_region))
1317 return PTR_ERR(alg_region);
db40517c 1318
b618a185 1319 pos = sizeof(adsp2_id) / 2;
3809f001 1320 len = (sizeof(*adsp2_alg) * n_algs) / 2;
db40517c 1321
3809f001 1322 adsp2_alg = wm_adsp_read_algs(dsp, n_algs, mem->base + pos, len);
b618a185
CK
1323 if (IS_ERR(adsp2_alg))
1324 return PTR_ERR(adsp2_alg);
471f4885 1325
3809f001 1326 for (i = 0; i < n_algs; i++) {
b618a185
CK
1327 adsp_info(dsp,
1328 "%d: ID %x v%d.%d.%d XM@%x YM@%x ZM@%x\n",
1329 i, be32_to_cpu(adsp2_alg[i].alg.id),
1330 (be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff0000) >> 16,
1331 (be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff00) >> 8,
1332 be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff,
1333 be32_to_cpu(adsp2_alg[i].xm),
1334 be32_to_cpu(adsp2_alg[i].ym),
1335 be32_to_cpu(adsp2_alg[i].zm));
db40517c 1336
d9d20e17
CK
1337 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_XM,
1338 adsp2_alg[i].alg.id,
1339 adsp2_alg[i].xm);
1340 if (IS_ERR(alg_region)) {
1341 ret = PTR_ERR(alg_region);
b618a185
CK
1342 goto out;
1343 }
2323736d
CK
1344 if (dsp->fw_ver == 0) {
1345 if (i + 1 < n_algs) {
1346 len = be32_to_cpu(adsp2_alg[i + 1].xm);
1347 len -= be32_to_cpu(adsp2_alg[i].xm);
1348 len *= 4;
1349 wm_adsp_create_control(dsp, alg_region, 0,
1350 len, NULL, 0);
1351 } else {
1352 adsp_warn(dsp, "Missing length info for region XM with ID %x\n",
1353 be32_to_cpu(adsp2_alg[i].alg.id));
1354 }
b618a185 1355 }
471f4885 1356
d9d20e17
CK
1357 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_YM,
1358 adsp2_alg[i].alg.id,
1359 adsp2_alg[i].ym);
1360 if (IS_ERR(alg_region)) {
1361 ret = PTR_ERR(alg_region);
b618a185
CK
1362 goto out;
1363 }
2323736d
CK
1364 if (dsp->fw_ver == 0) {
1365 if (i + 1 < n_algs) {
1366 len = be32_to_cpu(adsp2_alg[i + 1].ym);
1367 len -= be32_to_cpu(adsp2_alg[i].ym);
1368 len *= 4;
1369 wm_adsp_create_control(dsp, alg_region, 0,
1370 len, NULL, 0);
1371 } else {
1372 adsp_warn(dsp, "Missing length info for region YM with ID %x\n",
1373 be32_to_cpu(adsp2_alg[i].alg.id));
1374 }
b618a185 1375 }
471f4885 1376
d9d20e17
CK
1377 alg_region = wm_adsp_create_region(dsp, WMFW_ADSP2_ZM,
1378 adsp2_alg[i].alg.id,
1379 adsp2_alg[i].zm);
1380 if (IS_ERR(alg_region)) {
1381 ret = PTR_ERR(alg_region);
b618a185
CK
1382 goto out;
1383 }
2323736d
CK
1384 if (dsp->fw_ver == 0) {
1385 if (i + 1 < n_algs) {
1386 len = be32_to_cpu(adsp2_alg[i + 1].zm);
1387 len -= be32_to_cpu(adsp2_alg[i].zm);
1388 len *= 4;
1389 wm_adsp_create_control(dsp, alg_region, 0,
1390 len, NULL, 0);
1391 } else {
1392 adsp_warn(dsp, "Missing length info for region ZM with ID %x\n",
1393 be32_to_cpu(adsp2_alg[i].alg.id));
1394 }
db40517c
MB
1395 }
1396 }
1397
1398out:
b618a185 1399 kfree(adsp2_alg);
db40517c
MB
1400 return ret;
1401}
1402
2159ad93
MB
1403static int wm_adsp_load_coeff(struct wm_adsp *dsp)
1404{
cf17c83c 1405 LIST_HEAD(buf_list);
2159ad93
MB
1406 struct regmap *regmap = dsp->regmap;
1407 struct wmfw_coeff_hdr *hdr;
1408 struct wmfw_coeff_item *blk;
1409 const struct firmware *firmware;
471f4885
MB
1410 const struct wm_adsp_region *mem;
1411 struct wm_adsp_alg_region *alg_region;
2159ad93
MB
1412 const char *region_name;
1413 int ret, pos, blocks, type, offset, reg;
1414 char *file;
cf17c83c 1415 struct wm_adsp_buf *buf;
2159ad93
MB
1416
1417 file = kzalloc(PAGE_SIZE, GFP_KERNEL);
1418 if (file == NULL)
1419 return -ENOMEM;
1420
1023dbd9
MB
1421 snprintf(file, PAGE_SIZE, "%s-dsp%d-%s.bin", dsp->part, dsp->num,
1422 wm_adsp_fw[dsp->fw].file);
2159ad93
MB
1423 file[PAGE_SIZE - 1] = '\0';
1424
1425 ret = request_firmware(&firmware, file, dsp->dev);
1426 if (ret != 0) {
1427 adsp_warn(dsp, "Failed to request '%s'\n", file);
1428 ret = 0;
1429 goto out;
1430 }
1431 ret = -EINVAL;
1432
1433 if (sizeof(*hdr) >= firmware->size) {
1434 adsp_err(dsp, "%s: file too short, %zu bytes\n",
1435 file, firmware->size);
1436 goto out_fw;
1437 }
1438
1439 hdr = (void*)&firmware->data[0];
1440 if (memcmp(hdr->magic, "WMDR", 4) != 0) {
1441 adsp_err(dsp, "%s: invalid magic\n", file);
a4cdbec7 1442 goto out_fw;
2159ad93
MB
1443 }
1444
c712326d
MB
1445 switch (be32_to_cpu(hdr->rev) & 0xff) {
1446 case 1:
1447 break;
1448 default:
1449 adsp_err(dsp, "%s: Unsupported coefficient file format %d\n",
1450 file, be32_to_cpu(hdr->rev) & 0xff);
1451 ret = -EINVAL;
1452 goto out_fw;
1453 }
1454
2159ad93
MB
1455 adsp_dbg(dsp, "%s: v%d.%d.%d\n", file,
1456 (le32_to_cpu(hdr->ver) >> 16) & 0xff,
1457 (le32_to_cpu(hdr->ver) >> 8) & 0xff,
1458 le32_to_cpu(hdr->ver) & 0xff);
1459
1460 pos = le32_to_cpu(hdr->len);
1461
1462 blocks = 0;
1463 while (pos < firmware->size &&
1464 pos - firmware->size > sizeof(*blk)) {
1465 blk = (void*)(&firmware->data[pos]);
1466
c712326d
MB
1467 type = le16_to_cpu(blk->type);
1468 offset = le16_to_cpu(blk->offset);
2159ad93
MB
1469
1470 adsp_dbg(dsp, "%s.%d: %x v%d.%d.%d\n",
1471 file, blocks, le32_to_cpu(blk->id),
1472 (le32_to_cpu(blk->ver) >> 16) & 0xff,
1473 (le32_to_cpu(blk->ver) >> 8) & 0xff,
1474 le32_to_cpu(blk->ver) & 0xff);
1475 adsp_dbg(dsp, "%s.%d: %d bytes at 0x%x in %x\n",
1476 file, blocks, le32_to_cpu(blk->len), offset, type);
1477
1478 reg = 0;
1479 region_name = "Unknown";
1480 switch (type) {
c712326d
MB
1481 case (WMFW_NAME_TEXT << 8):
1482 case (WMFW_INFO_TEXT << 8):
2159ad93 1483 break;
c712326d 1484 case (WMFW_ABSOLUTE << 8):
f395a218
MB
1485 /*
1486 * Old files may use this for global
1487 * coefficients.
1488 */
1489 if (le32_to_cpu(blk->id) == dsp->fw_id &&
1490 offset == 0) {
1491 region_name = "global coefficients";
1492 mem = wm_adsp_find_region(dsp, type);
1493 if (!mem) {
1494 adsp_err(dsp, "No ZM\n");
1495 break;
1496 }
1497 reg = wm_adsp_region_to_reg(mem, 0);
1498
1499 } else {
1500 region_name = "register";
1501 reg = offset;
1502 }
2159ad93 1503 break;
471f4885
MB
1504
1505 case WMFW_ADSP1_DM:
1506 case WMFW_ADSP1_ZM:
1507 case WMFW_ADSP2_XM:
1508 case WMFW_ADSP2_YM:
1509 adsp_dbg(dsp, "%s.%d: %d bytes in %x for %x\n",
1510 file, blocks, le32_to_cpu(blk->len),
1511 type, le32_to_cpu(blk->id));
1512
1513 mem = wm_adsp_find_region(dsp, type);
1514 if (!mem) {
1515 adsp_err(dsp, "No base for region %x\n", type);
1516 break;
1517 }
1518
1519 reg = 0;
1520 list_for_each_entry(alg_region,
1521 &dsp->alg_regions, list) {
1522 if (le32_to_cpu(blk->id) == alg_region->alg &&
1523 type == alg_region->type) {
338c5188 1524 reg = alg_region->base;
471f4885
MB
1525 reg = wm_adsp_region_to_reg(mem,
1526 reg);
338c5188 1527 reg += offset;
d733dc08 1528 break;
471f4885
MB
1529 }
1530 }
1531
1532 if (reg == 0)
1533 adsp_err(dsp, "No %x for algorithm %x\n",
1534 type, le32_to_cpu(blk->id));
1535 break;
1536
2159ad93 1537 default:
25c62f7e
MB
1538 adsp_err(dsp, "%s.%d: Unknown region type %x at %d\n",
1539 file, blocks, type, pos);
2159ad93
MB
1540 break;
1541 }
1542
1543 if (reg) {
cf17c83c
MB
1544 buf = wm_adsp_buf_alloc(blk->data,
1545 le32_to_cpu(blk->len),
1546 &buf_list);
a76fefab
MB
1547 if (!buf) {
1548 adsp_err(dsp, "Out of memory\n");
f4b82812
WY
1549 ret = -ENOMEM;
1550 goto out_fw;
a76fefab
MB
1551 }
1552
20da6d5a
MB
1553 adsp_dbg(dsp, "%s.%d: Writing %d bytes at %x\n",
1554 file, blocks, le32_to_cpu(blk->len),
1555 reg);
cf17c83c
MB
1556 ret = regmap_raw_write_async(regmap, reg, buf->buf,
1557 le32_to_cpu(blk->len));
2159ad93
MB
1558 if (ret != 0) {
1559 adsp_err(dsp,
43bc3bf6
DP
1560 "%s.%d: Failed to write to %x in %s: %d\n",
1561 file, blocks, reg, region_name, ret);
2159ad93
MB
1562 }
1563 }
1564
be951017 1565 pos += (le32_to_cpu(blk->len) + sizeof(*blk) + 3) & ~0x03;
2159ad93
MB
1566 blocks++;
1567 }
1568
cf17c83c
MB
1569 ret = regmap_async_complete(regmap);
1570 if (ret != 0)
1571 adsp_err(dsp, "Failed to complete async write: %d\n", ret);
1572
2159ad93
MB
1573 if (pos > firmware->size)
1574 adsp_warn(dsp, "%s.%d: %zu bytes at end of file\n",
1575 file, blocks, pos - firmware->size);
1576
1577out_fw:
9da7a5a9 1578 regmap_async_complete(regmap);
2159ad93 1579 release_firmware(firmware);
cf17c83c 1580 wm_adsp_buf_free(&buf_list);
2159ad93
MB
1581out:
1582 kfree(file);
f4b82812 1583 return ret;
2159ad93
MB
1584}
1585
3809f001 1586int wm_adsp1_init(struct wm_adsp *dsp)
5e7a7a22 1587{
3809f001 1588 INIT_LIST_HEAD(&dsp->alg_regions);
5e7a7a22
MB
1589
1590 return 0;
1591}
1592EXPORT_SYMBOL_GPL(wm_adsp1_init);
1593
2159ad93
MB
1594int wm_adsp1_event(struct snd_soc_dapm_widget *w,
1595 struct snd_kcontrol *kcontrol,
1596 int event)
1597{
72718517 1598 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
2159ad93
MB
1599 struct wm_adsp *dsps = snd_soc_codec_get_drvdata(codec);
1600 struct wm_adsp *dsp = &dsps[w->shift];
b0101b4f 1601 struct wm_adsp_alg_region *alg_region;
6ab2b7b4 1602 struct wm_coeff_ctl *ctl;
2159ad93 1603 int ret;
94e205bf 1604 int val;
2159ad93 1605
00200107 1606 dsp->card = codec->component.card;
92bb4c32 1607
2159ad93
MB
1608 switch (event) {
1609 case SND_SOC_DAPM_POST_PMU:
1610 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
1611 ADSP1_SYS_ENA, ADSP1_SYS_ENA);
1612
94e205bf
CR
1613 /*
1614 * For simplicity set the DSP clock rate to be the
1615 * SYSCLK rate rather than making it configurable.
1616 */
1617 if(dsp->sysclk_reg) {
1618 ret = regmap_read(dsp->regmap, dsp->sysclk_reg, &val);
1619 if (ret != 0) {
1620 adsp_err(dsp, "Failed to read SYSCLK state: %d\n",
1621 ret);
1622 return ret;
1623 }
1624
1625 val = (val & dsp->sysclk_mask)
1626 >> dsp->sysclk_shift;
1627
1628 ret = regmap_update_bits(dsp->regmap,
1629 dsp->base + ADSP1_CONTROL_31,
1630 ADSP1_CLK_SEL_MASK, val);
1631 if (ret != 0) {
1632 adsp_err(dsp, "Failed to set clock rate: %d\n",
1633 ret);
1634 return ret;
1635 }
1636 }
1637
2159ad93
MB
1638 ret = wm_adsp_load(dsp);
1639 if (ret != 0)
1640 goto err;
1641
b618a185 1642 ret = wm_adsp1_setup_algs(dsp);
db40517c
MB
1643 if (ret != 0)
1644 goto err;
1645
2159ad93
MB
1646 ret = wm_adsp_load_coeff(dsp);
1647 if (ret != 0)
1648 goto err;
1649
0c2e3f34 1650 /* Initialize caches for enabled and unset controls */
81ad93ec 1651 ret = wm_coeff_init_control_caches(dsp);
6ab2b7b4
DP
1652 if (ret != 0)
1653 goto err;
1654
0c2e3f34 1655 /* Sync set controls */
81ad93ec 1656 ret = wm_coeff_sync_controls(dsp);
6ab2b7b4
DP
1657 if (ret != 0)
1658 goto err;
1659
2159ad93
MB
1660 /* Start the core running */
1661 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
1662 ADSP1_CORE_ENA | ADSP1_START,
1663 ADSP1_CORE_ENA | ADSP1_START);
1664 break;
1665
1666 case SND_SOC_DAPM_PRE_PMD:
1667 /* Halt the core */
1668 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
1669 ADSP1_CORE_ENA | ADSP1_START, 0);
1670
1671 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_19,
1672 ADSP1_WDMA_BUFFER_LENGTH_MASK, 0);
1673
1674 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
1675 ADSP1_SYS_ENA, 0);
6ab2b7b4 1676
81ad93ec 1677 list_for_each_entry(ctl, &dsp->ctl_list, list)
6ab2b7b4 1678 ctl->enabled = 0;
b0101b4f
DP
1679
1680 while (!list_empty(&dsp->alg_regions)) {
1681 alg_region = list_first_entry(&dsp->alg_regions,
1682 struct wm_adsp_alg_region,
1683 list);
1684 list_del(&alg_region->list);
1685 kfree(alg_region);
1686 }
2159ad93
MB
1687 break;
1688
1689 default:
1690 break;
1691 }
1692
1693 return 0;
1694
1695err:
1696 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
1697 ADSP1_SYS_ENA, 0);
1698 return ret;
1699}
1700EXPORT_SYMBOL_GPL(wm_adsp1_event);
1701
1702static int wm_adsp2_ena(struct wm_adsp *dsp)
1703{
1704 unsigned int val;
1705 int ret, count;
1706
1552c325
MB
1707 ret = regmap_update_bits_async(dsp->regmap, dsp->base + ADSP2_CONTROL,
1708 ADSP2_SYS_ENA, ADSP2_SYS_ENA);
2159ad93
MB
1709 if (ret != 0)
1710 return ret;
1711
1712 /* Wait for the RAM to start, should be near instantaneous */
939fd1e8 1713 for (count = 0; count < 10; ++count) {
2159ad93
MB
1714 ret = regmap_read(dsp->regmap, dsp->base + ADSP2_STATUS1,
1715 &val);
1716 if (ret != 0)
1717 return ret;
939fd1e8
CK
1718
1719 if (val & ADSP2_RAM_RDY)
1720 break;
1721
1722 msleep(1);
1723 }
2159ad93
MB
1724
1725 if (!(val & ADSP2_RAM_RDY)) {
1726 adsp_err(dsp, "Failed to start DSP RAM\n");
1727 return -EBUSY;
1728 }
1729
1730 adsp_dbg(dsp, "RAM ready after %d polls\n", count);
2159ad93
MB
1731
1732 return 0;
1733}
1734
18b1a902 1735static void wm_adsp2_boot_work(struct work_struct *work)
2159ad93 1736{
d8a64d6a
CK
1737 struct wm_adsp *dsp = container_of(work,
1738 struct wm_adsp,
1739 boot_work);
2159ad93 1740 int ret;
d8a64d6a 1741 unsigned int val;
2159ad93 1742
d8a64d6a
CK
1743 /*
1744 * For simplicity set the DSP clock rate to be the
1745 * SYSCLK rate rather than making it configurable.
1746 */
1747 ret = regmap_read(dsp->regmap, ARIZONA_SYSTEM_CLOCK_1, &val);
1748 if (ret != 0) {
1749 adsp_err(dsp, "Failed to read SYSCLK state: %d\n", ret);
1750 return;
1751 }
1752 val = (val & ARIZONA_SYSCLK_FREQ_MASK)
1753 >> ARIZONA_SYSCLK_FREQ_SHIFT;
92bb4c32 1754
d8a64d6a
CK
1755 ret = regmap_update_bits_async(dsp->regmap,
1756 dsp->base + ADSP2_CLOCKING,
1757 ADSP2_CLK_SEL_MASK, val);
1758 if (ret != 0) {
1759 adsp_err(dsp, "Failed to set clock rate: %d\n", ret);
1760 return;
1761 }
dd49e2c8 1762
d8a64d6a
CK
1763 if (dsp->dvfs) {
1764 ret = regmap_read(dsp->regmap,
1765 dsp->base + ADSP2_CLOCKING, &val);
dd49e2c8 1766 if (ret != 0) {
62c35b3b 1767 adsp_err(dsp, "Failed to read clocking: %d\n", ret);
d8a64d6a 1768 return;
dd49e2c8
MB
1769 }
1770
d8a64d6a
CK
1771 if ((val & ADSP2_CLK_SEL_MASK) >= 3) {
1772 ret = regulator_enable(dsp->dvfs);
973838a0 1773 if (ret != 0) {
62c35b3b
CK
1774 adsp_err(dsp,
1775 "Failed to enable supply: %d\n",
1776 ret);
d8a64d6a 1777 return;
973838a0
MB
1778 }
1779
d8a64d6a
CK
1780 ret = regulator_set_voltage(dsp->dvfs,
1781 1800000,
1782 1800000);
1783 if (ret != 0) {
62c35b3b
CK
1784 adsp_err(dsp,
1785 "Failed to raise supply: %d\n",
1786 ret);
d8a64d6a 1787 return;
973838a0
MB
1788 }
1789 }
d8a64d6a 1790 }
973838a0 1791
d8a64d6a
CK
1792 ret = wm_adsp2_ena(dsp);
1793 if (ret != 0)
1794 return;
2159ad93 1795
d8a64d6a
CK
1796 ret = wm_adsp_load(dsp);
1797 if (ret != 0)
1798 goto err;
2159ad93 1799
b618a185 1800 ret = wm_adsp2_setup_algs(dsp);
d8a64d6a
CK
1801 if (ret != 0)
1802 goto err;
db40517c 1803
d8a64d6a
CK
1804 ret = wm_adsp_load_coeff(dsp);
1805 if (ret != 0)
1806 goto err;
2159ad93 1807
d8a64d6a
CK
1808 /* Initialize caches for enabled and unset controls */
1809 ret = wm_coeff_init_control_caches(dsp);
1810 if (ret != 0)
1811 goto err;
6ab2b7b4 1812
d8a64d6a
CK
1813 /* Sync set controls */
1814 ret = wm_coeff_sync_controls(dsp);
1815 if (ret != 0)
1816 goto err;
1817
d8a64d6a
CK
1818 dsp->running = true;
1819
1820 return;
6ab2b7b4 1821
d8a64d6a
CK
1822err:
1823 regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
1824 ADSP2_SYS_ENA | ADSP2_CORE_ENA | ADSP2_START, 0);
1825}
1826
12db5edd
CK
1827int wm_adsp2_early_event(struct snd_soc_dapm_widget *w,
1828 struct snd_kcontrol *kcontrol, int event)
1829{
72718517 1830 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
12db5edd
CK
1831 struct wm_adsp *dsps = snd_soc_codec_get_drvdata(codec);
1832 struct wm_adsp *dsp = &dsps[w->shift];
1833
00200107 1834 dsp->card = codec->component.card;
12db5edd
CK
1835
1836 switch (event) {
1837 case SND_SOC_DAPM_PRE_PMU:
1838 queue_work(system_unbound_wq, &dsp->boot_work);
1839 break;
1840 default:
1841 break;
cab27258 1842 }
12db5edd
CK
1843
1844 return 0;
1845}
1846EXPORT_SYMBOL_GPL(wm_adsp2_early_event);
1847
d8a64d6a
CK
1848int wm_adsp2_event(struct snd_soc_dapm_widget *w,
1849 struct snd_kcontrol *kcontrol, int event)
1850{
72718517 1851 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
d8a64d6a
CK
1852 struct wm_adsp *dsps = snd_soc_codec_get_drvdata(codec);
1853 struct wm_adsp *dsp = &dsps[w->shift];
1854 struct wm_adsp_alg_region *alg_region;
1855 struct wm_coeff_ctl *ctl;
1856 int ret;
1857
d8a64d6a
CK
1858 switch (event) {
1859 case SND_SOC_DAPM_POST_PMU:
d8a64d6a
CK
1860 flush_work(&dsp->boot_work);
1861
1862 if (!dsp->running)
1863 return -EIO;
6ab2b7b4 1864
d8a64d6a
CK
1865 ret = regmap_update_bits(dsp->regmap,
1866 dsp->base + ADSP2_CONTROL,
00e4c3b6
CK
1867 ADSP2_CORE_ENA | ADSP2_START,
1868 ADSP2_CORE_ENA | ADSP2_START);
2159ad93
MB
1869 if (ret != 0)
1870 goto err;
1871 break;
1872
1873 case SND_SOC_DAPM_PRE_PMD:
1023dbd9
MB
1874 dsp->running = false;
1875
2159ad93 1876 regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
a7f9be7e
MB
1877 ADSP2_SYS_ENA | ADSP2_CORE_ENA |
1878 ADSP2_START, 0);
973838a0 1879
2d30b575
MB
1880 /* Make sure DMAs are quiesced */
1881 regmap_write(dsp->regmap, dsp->base + ADSP2_WDMA_CONFIG_1, 0);
1882 regmap_write(dsp->regmap, dsp->base + ADSP2_WDMA_CONFIG_2, 0);
1883 regmap_write(dsp->regmap, dsp->base + ADSP2_RDMA_CONFIG_1, 0);
1884
973838a0
MB
1885 if (dsp->dvfs) {
1886 ret = regulator_set_voltage(dsp->dvfs, 1200000,
1887 1800000);
1888 if (ret != 0)
62c35b3b
CK
1889 adsp_warn(dsp,
1890 "Failed to lower supply: %d\n",
1891 ret);
973838a0
MB
1892
1893 ret = regulator_disable(dsp->dvfs);
1894 if (ret != 0)
62c35b3b
CK
1895 adsp_err(dsp,
1896 "Failed to enable supply: %d\n",
1897 ret);
973838a0 1898 }
471f4885 1899
81ad93ec 1900 list_for_each_entry(ctl, &dsp->ctl_list, list)
6ab2b7b4 1901 ctl->enabled = 0;
6ab2b7b4 1902
471f4885
MB
1903 while (!list_empty(&dsp->alg_regions)) {
1904 alg_region = list_first_entry(&dsp->alg_regions,
1905 struct wm_adsp_alg_region,
1906 list);
1907 list_del(&alg_region->list);
1908 kfree(alg_region);
1909 }
ddbc5efe
CK
1910
1911 adsp_dbg(dsp, "Shutdown complete\n");
2159ad93
MB
1912 break;
1913
1914 default:
1915 break;
1916 }
1917
1918 return 0;
1919err:
1920 regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
a7f9be7e 1921 ADSP2_SYS_ENA | ADSP2_CORE_ENA | ADSP2_START, 0);
2159ad93
MB
1922 return ret;
1923}
1924EXPORT_SYMBOL_GPL(wm_adsp2_event);
973838a0 1925
3809f001 1926int wm_adsp2_init(struct wm_adsp *dsp, bool dvfs)
973838a0
MB
1927{
1928 int ret;
1929
10a2b662
MB
1930 /*
1931 * Disable the DSP memory by default when in reset for a small
1932 * power saving.
1933 */
3809f001 1934 ret = regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
10a2b662
MB
1935 ADSP2_MEM_ENA, 0);
1936 if (ret != 0) {
3809f001 1937 adsp_err(dsp, "Failed to clear memory retention: %d\n", ret);
10a2b662
MB
1938 return ret;
1939 }
1940
3809f001
CK
1941 INIT_LIST_HEAD(&dsp->alg_regions);
1942 INIT_LIST_HEAD(&dsp->ctl_list);
1943 INIT_WORK(&dsp->boot_work, wm_adsp2_boot_work);
6ab2b7b4 1944
973838a0 1945 if (dvfs) {
3809f001
CK
1946 dsp->dvfs = devm_regulator_get(dsp->dev, "DCVDD");
1947 if (IS_ERR(dsp->dvfs)) {
1948 ret = PTR_ERR(dsp->dvfs);
1949 adsp_err(dsp, "Failed to get DCVDD: %d\n", ret);
81ad93ec 1950 return ret;
973838a0
MB
1951 }
1952
3809f001 1953 ret = regulator_enable(dsp->dvfs);
973838a0 1954 if (ret != 0) {
3809f001 1955 adsp_err(dsp, "Failed to enable DCVDD: %d\n", ret);
81ad93ec 1956 return ret;
973838a0
MB
1957 }
1958
3809f001 1959 ret = regulator_set_voltage(dsp->dvfs, 1200000, 1800000);
973838a0 1960 if (ret != 0) {
3809f001 1961 adsp_err(dsp, "Failed to initialise DVFS: %d\n", ret);
81ad93ec 1962 return ret;
973838a0
MB
1963 }
1964
3809f001 1965 ret = regulator_disable(dsp->dvfs);
973838a0 1966 if (ret != 0) {
3809f001 1967 adsp_err(dsp, "Failed to disable DCVDD: %d\n", ret);
81ad93ec 1968 return ret;
973838a0
MB
1969 }
1970 }
1971
1972 return 0;
1973}
1974EXPORT_SYMBOL_GPL(wm_adsp2_init);
0a37c6ef
PD
1975
1976MODULE_LICENSE("GPL v2");
This page took 0.702575 seconds and 5 git commands to generate.