Merge tag 'pwm/for-4.1-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/thierry...
[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;
6ab2b7b4
DP
232 struct wm_adsp_alg_region region;
233 struct wm_coeff_ctl_ops ops;
234 struct wm_adsp *adsp;
235 void *private;
236 unsigned int enabled:1;
237 struct list_head list;
238 void *cache;
239 size_t len;
0c2e3f34 240 unsigned int set:1;
6ab2b7b4
DP
241 struct snd_kcontrol *kcontrol;
242};
243
1023dbd9
MB
244static int wm_adsp_fw_get(struct snd_kcontrol *kcontrol,
245 struct snd_ctl_elem_value *ucontrol)
246{
ea53bf77 247 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
1023dbd9
MB
248 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
249 struct wm_adsp *adsp = snd_soc_codec_get_drvdata(codec);
250
251 ucontrol->value.integer.value[0] = adsp[e->shift_l].fw;
252
253 return 0;
254}
255
256static int wm_adsp_fw_put(struct snd_kcontrol *kcontrol,
257 struct snd_ctl_elem_value *ucontrol)
258{
ea53bf77 259 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
1023dbd9
MB
260 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
261 struct wm_adsp *adsp = snd_soc_codec_get_drvdata(codec);
262
263 if (ucontrol->value.integer.value[0] == adsp[e->shift_l].fw)
264 return 0;
265
266 if (ucontrol->value.integer.value[0] >= WM_ADSP_NUM_FW)
267 return -EINVAL;
268
269 if (adsp[e->shift_l].running)
270 return -EBUSY;
271
31522764 272 adsp[e->shift_l].fw = ucontrol->value.integer.value[0];
1023dbd9
MB
273
274 return 0;
275}
276
277static const struct soc_enum wm_adsp_fw_enum[] = {
278 SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
279 SOC_ENUM_SINGLE(0, 1, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
280 SOC_ENUM_SINGLE(0, 2, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
281 SOC_ENUM_SINGLE(0, 3, ARRAY_SIZE(wm_adsp_fw_text), wm_adsp_fw_text),
282};
283
b6ed61cf 284const struct snd_kcontrol_new wm_adsp1_fw_controls[] = {
1023dbd9
MB
285 SOC_ENUM_EXT("DSP1 Firmware", wm_adsp_fw_enum[0],
286 wm_adsp_fw_get, wm_adsp_fw_put),
287 SOC_ENUM_EXT("DSP2 Firmware", wm_adsp_fw_enum[1],
288 wm_adsp_fw_get, wm_adsp_fw_put),
289 SOC_ENUM_EXT("DSP3 Firmware", wm_adsp_fw_enum[2],
290 wm_adsp_fw_get, wm_adsp_fw_put),
b6ed61cf
MB
291};
292EXPORT_SYMBOL_GPL(wm_adsp1_fw_controls);
293
294#if IS_ENABLED(CONFIG_SND_SOC_ARIZONA)
295static const struct soc_enum wm_adsp2_rate_enum[] = {
dc91428a
MB
296 SOC_VALUE_ENUM_SINGLE(ARIZONA_DSP1_CONTROL_1,
297 ARIZONA_DSP1_RATE_SHIFT, 0xf,
298 ARIZONA_RATE_ENUM_SIZE,
299 arizona_rate_text, arizona_rate_val),
300 SOC_VALUE_ENUM_SINGLE(ARIZONA_DSP2_CONTROL_1,
301 ARIZONA_DSP1_RATE_SHIFT, 0xf,
302 ARIZONA_RATE_ENUM_SIZE,
303 arizona_rate_text, arizona_rate_val),
304 SOC_VALUE_ENUM_SINGLE(ARIZONA_DSP3_CONTROL_1,
305 ARIZONA_DSP1_RATE_SHIFT, 0xf,
306 ARIZONA_RATE_ENUM_SIZE,
307 arizona_rate_text, arizona_rate_val),
5be9c5b4 308 SOC_VALUE_ENUM_SINGLE(ARIZONA_DSP4_CONTROL_1,
dc91428a
MB
309 ARIZONA_DSP1_RATE_SHIFT, 0xf,
310 ARIZONA_RATE_ENUM_SIZE,
311 arizona_rate_text, arizona_rate_val),
312};
313
b6ed61cf 314const struct snd_kcontrol_new wm_adsp2_fw_controls[] = {
1023dbd9
MB
315 SOC_ENUM_EXT("DSP1 Firmware", wm_adsp_fw_enum[0],
316 wm_adsp_fw_get, wm_adsp_fw_put),
b6ed61cf 317 SOC_ENUM("DSP1 Rate", wm_adsp2_rate_enum[0]),
1023dbd9
MB
318 SOC_ENUM_EXT("DSP2 Firmware", wm_adsp_fw_enum[1],
319 wm_adsp_fw_get, wm_adsp_fw_put),
b6ed61cf 320 SOC_ENUM("DSP2 Rate", wm_adsp2_rate_enum[1]),
1023dbd9
MB
321 SOC_ENUM_EXT("DSP3 Firmware", wm_adsp_fw_enum[2],
322 wm_adsp_fw_get, wm_adsp_fw_put),
b6ed61cf 323 SOC_ENUM("DSP3 Rate", wm_adsp2_rate_enum[2]),
1023dbd9
MB
324 SOC_ENUM_EXT("DSP4 Firmware", wm_adsp_fw_enum[3],
325 wm_adsp_fw_get, wm_adsp_fw_put),
b6ed61cf 326 SOC_ENUM("DSP4 Rate", wm_adsp2_rate_enum[3]),
1023dbd9 327};
b6ed61cf
MB
328EXPORT_SYMBOL_GPL(wm_adsp2_fw_controls);
329#endif
2159ad93
MB
330
331static struct wm_adsp_region const *wm_adsp_find_region(struct wm_adsp *dsp,
332 int type)
333{
334 int i;
335
336 for (i = 0; i < dsp->num_mems; i++)
337 if (dsp->mem[i].type == type)
338 return &dsp->mem[i];
339
340 return NULL;
341}
342
45b9ee72
MB
343static unsigned int wm_adsp_region_to_reg(struct wm_adsp_region const *region,
344 unsigned int offset)
345{
6c452bda
TI
346 if (WARN_ON(!region))
347 return offset;
45b9ee72
MB
348 switch (region->type) {
349 case WMFW_ADSP1_PM:
350 return region->base + (offset * 3);
351 case WMFW_ADSP1_DM:
352 return region->base + (offset * 2);
353 case WMFW_ADSP2_XM:
354 return region->base + (offset * 2);
355 case WMFW_ADSP2_YM:
356 return region->base + (offset * 2);
357 case WMFW_ADSP1_ZM:
358 return region->base + (offset * 2);
359 default:
6c452bda 360 WARN(1, "Unknown memory region type");
45b9ee72
MB
361 return offset;
362 }
363}
364
6ab2b7b4
DP
365static int wm_coeff_info(struct snd_kcontrol *kcontrol,
366 struct snd_ctl_elem_info *uinfo)
367{
368 struct wm_coeff_ctl *ctl = (struct wm_coeff_ctl *)kcontrol->private_value;
369
370 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
371 uinfo->count = ctl->len;
372 return 0;
373}
374
375static int wm_coeff_write_control(struct snd_kcontrol *kcontrol,
376 const void *buf, size_t len)
377{
6ab2b7b4
DP
378 struct wm_coeff_ctl *ctl = (struct wm_coeff_ctl *)kcontrol->private_value;
379 struct wm_adsp_alg_region *region = &ctl->region;
380 const struct wm_adsp_region *mem;
381 struct wm_adsp *adsp = ctl->adsp;
382 void *scratch;
383 int ret;
384 unsigned int reg;
385
386 mem = wm_adsp_find_region(adsp, region->type);
387 if (!mem) {
388 adsp_err(adsp, "No base for region %x\n",
389 region->type);
390 return -EINVAL;
391 }
392
393 reg = ctl->region.base;
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
81ad93ec 400 ret = regmap_raw_write(adsp->regmap, reg, scratch,
6ab2b7b4
DP
401 ctl->len);
402 if (ret) {
43bc3bf6
DP
403 adsp_err(adsp, "Failed to write %zu bytes to %x: %d\n",
404 ctl->len, reg, ret);
6ab2b7b4
DP
405 kfree(scratch);
406 return ret;
407 }
562c5e6f 408 adsp_dbg(adsp, "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
DP
426
427 return wm_coeff_write_control(kcontrol, p, ctl->len);
428}
429
430static int wm_coeff_read_control(struct snd_kcontrol *kcontrol,
431 void *buf, size_t len)
432{
6ab2b7b4
DP
433 struct wm_coeff_ctl *ctl = (struct wm_coeff_ctl *)kcontrol->private_value;
434 struct wm_adsp_alg_region *region = &ctl->region;
435 const struct wm_adsp_region *mem;
436 struct wm_adsp *adsp = ctl->adsp;
437 void *scratch;
438 int ret;
439 unsigned int reg;
440
441 mem = wm_adsp_find_region(adsp, region->type);
442 if (!mem) {
443 adsp_err(adsp, "No base for region %x\n",
444 region->type);
445 return -EINVAL;
446 }
447
448 reg = ctl->region.base;
449 reg = wm_adsp_region_to_reg(mem, reg);
450
451 scratch = kmalloc(ctl->len, GFP_KERNEL | GFP_DMA);
452 if (!scratch)
453 return -ENOMEM;
454
81ad93ec 455 ret = regmap_raw_read(adsp->regmap, reg, scratch, ctl->len);
6ab2b7b4 456 if (ret) {
43bc3bf6
DP
457 adsp_err(adsp, "Failed to read %zu bytes from %x: %d\n",
458 ctl->len, reg, ret);
6ab2b7b4
DP
459 kfree(scratch);
460 return ret;
461 }
562c5e6f 462 adsp_dbg(adsp, "Read %zu bytes from %x\n", ctl->len, reg);
6ab2b7b4
DP
463
464 memcpy(buf, scratch, ctl->len);
465 kfree(scratch);
466
467 return 0;
468}
469
470static int wm_coeff_get(struct snd_kcontrol *kcontrol,
471 struct snd_ctl_elem_value *ucontrol)
472{
473 struct wm_coeff_ctl *ctl = (struct wm_coeff_ctl *)kcontrol->private_value;
474 char *p = ucontrol->value.bytes.data;
475
476 memcpy(p, ctl->cache, ctl->len);
477 return 0;
478}
479
6ab2b7b4 480struct wmfw_ctl_work {
81ad93ec 481 struct wm_adsp *adsp;
6ab2b7b4
DP
482 struct wm_coeff_ctl *ctl;
483 struct work_struct work;
484};
485
81ad93ec 486static int wmfw_add_ctl(struct wm_adsp *adsp, struct wm_coeff_ctl *ctl)
6ab2b7b4
DP
487{
488 struct snd_kcontrol_new *kcontrol;
489 int ret;
490
92bb4c32 491 if (!ctl || !ctl->name)
6ab2b7b4
DP
492 return -EINVAL;
493
494 kcontrol = kzalloc(sizeof(*kcontrol), GFP_KERNEL);
495 if (!kcontrol)
496 return -ENOMEM;
497 kcontrol->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
498
499 kcontrol->name = ctl->name;
500 kcontrol->info = wm_coeff_info;
501 kcontrol->get = wm_coeff_get;
502 kcontrol->put = wm_coeff_put;
503 kcontrol->private_value = (unsigned long)ctl;
504
92bb4c32 505 ret = snd_soc_add_card_controls(adsp->card,
81ad93ec 506 kcontrol, 1);
6ab2b7b4
DP
507 if (ret < 0)
508 goto err_kcontrol;
509
510 kfree(kcontrol);
511
92bb4c32 512 ctl->kcontrol = snd_soc_card_get_kcontrol(adsp->card,
81ad93ec
DP
513 ctl->name);
514
515 list_add(&ctl->list, &adsp->ctl_list);
6ab2b7b4
DP
516 return 0;
517
518err_kcontrol:
519 kfree(kcontrol);
520 return ret;
521}
522
2159ad93
MB
523static int wm_adsp_load(struct wm_adsp *dsp)
524{
cf17c83c 525 LIST_HEAD(buf_list);
2159ad93
MB
526 const struct firmware *firmware;
527 struct regmap *regmap = dsp->regmap;
528 unsigned int pos = 0;
529 const struct wmfw_header *header;
530 const struct wmfw_adsp1_sizes *adsp1_sizes;
531 const struct wmfw_adsp2_sizes *adsp2_sizes;
532 const struct wmfw_footer *footer;
533 const struct wmfw_region *region;
534 const struct wm_adsp_region *mem;
535 const char *region_name;
536 char *file, *text;
cf17c83c 537 struct wm_adsp_buf *buf;
2159ad93
MB
538 unsigned int reg;
539 int regions = 0;
540 int ret, offset, type, sizes;
541
542 file = kzalloc(PAGE_SIZE, GFP_KERNEL);
543 if (file == NULL)
544 return -ENOMEM;
545
1023dbd9
MB
546 snprintf(file, PAGE_SIZE, "%s-dsp%d-%s.wmfw", dsp->part, dsp->num,
547 wm_adsp_fw[dsp->fw].file);
2159ad93
MB
548 file[PAGE_SIZE - 1] = '\0';
549
550 ret = request_firmware(&firmware, file, dsp->dev);
551 if (ret != 0) {
552 adsp_err(dsp, "Failed to request '%s'\n", file);
553 goto out;
554 }
555 ret = -EINVAL;
556
557 pos = sizeof(*header) + sizeof(*adsp1_sizes) + sizeof(*footer);
558 if (pos >= firmware->size) {
559 adsp_err(dsp, "%s: file too short, %zu bytes\n",
560 file, firmware->size);
561 goto out_fw;
562 }
563
564 header = (void*)&firmware->data[0];
565
566 if (memcmp(&header->magic[0], "WMFW", 4) != 0) {
567 adsp_err(dsp, "%s: invalid magic\n", file);
568 goto out_fw;
569 }
570
571 if (header->ver != 0) {
572 adsp_err(dsp, "%s: unknown file format %d\n",
573 file, header->ver);
574 goto out_fw;
575 }
3626992a 576 adsp_info(dsp, "Firmware version: %d\n", header->ver);
2159ad93
MB
577
578 if (header->core != dsp->type) {
579 adsp_err(dsp, "%s: invalid core %d != %d\n",
580 file, header->core, dsp->type);
581 goto out_fw;
582 }
583
584 switch (dsp->type) {
585 case WMFW_ADSP1:
586 pos = sizeof(*header) + sizeof(*adsp1_sizes) + sizeof(*footer);
587 adsp1_sizes = (void *)&(header[1]);
588 footer = (void *)&(adsp1_sizes[1]);
589 sizes = sizeof(*adsp1_sizes);
590
591 adsp_dbg(dsp, "%s: %d DM, %d PM, %d ZM\n",
592 file, le32_to_cpu(adsp1_sizes->dm),
593 le32_to_cpu(adsp1_sizes->pm),
594 le32_to_cpu(adsp1_sizes->zm));
595 break;
596
597 case WMFW_ADSP2:
598 pos = sizeof(*header) + sizeof(*adsp2_sizes) + sizeof(*footer);
599 adsp2_sizes = (void *)&(header[1]);
600 footer = (void *)&(adsp2_sizes[1]);
601 sizes = sizeof(*adsp2_sizes);
602
603 adsp_dbg(dsp, "%s: %d XM, %d YM %d PM, %d ZM\n",
604 file, le32_to_cpu(adsp2_sizes->xm),
605 le32_to_cpu(adsp2_sizes->ym),
606 le32_to_cpu(adsp2_sizes->pm),
607 le32_to_cpu(adsp2_sizes->zm));
608 break;
609
610 default:
6c452bda 611 WARN(1, "Unknown DSP type");
2159ad93
MB
612 goto out_fw;
613 }
614
615 if (le32_to_cpu(header->len) != sizeof(*header) +
616 sizes + sizeof(*footer)) {
617 adsp_err(dsp, "%s: unexpected header length %d\n",
618 file, le32_to_cpu(header->len));
619 goto out_fw;
620 }
621
622 adsp_dbg(dsp, "%s: timestamp %llu\n", file,
623 le64_to_cpu(footer->timestamp));
624
625 while (pos < firmware->size &&
626 pos - firmware->size > sizeof(*region)) {
627 region = (void *)&(firmware->data[pos]);
628 region_name = "Unknown";
629 reg = 0;
630 text = NULL;
631 offset = le32_to_cpu(region->offset) & 0xffffff;
632 type = be32_to_cpu(region->type) & 0xff;
633 mem = wm_adsp_find_region(dsp, type);
634
635 switch (type) {
636 case WMFW_NAME_TEXT:
637 region_name = "Firmware name";
638 text = kzalloc(le32_to_cpu(region->len) + 1,
639 GFP_KERNEL);
640 break;
641 case WMFW_INFO_TEXT:
642 region_name = "Information";
643 text = kzalloc(le32_to_cpu(region->len) + 1,
644 GFP_KERNEL);
645 break;
646 case WMFW_ABSOLUTE:
647 region_name = "Absolute";
648 reg = offset;
649 break;
650 case WMFW_ADSP1_PM:
2159ad93 651 region_name = "PM";
45b9ee72 652 reg = wm_adsp_region_to_reg(mem, offset);
2159ad93
MB
653 break;
654 case WMFW_ADSP1_DM:
2159ad93 655 region_name = "DM";
45b9ee72 656 reg = wm_adsp_region_to_reg(mem, offset);
2159ad93
MB
657 break;
658 case WMFW_ADSP2_XM:
2159ad93 659 region_name = "XM";
45b9ee72 660 reg = wm_adsp_region_to_reg(mem, offset);
2159ad93
MB
661 break;
662 case WMFW_ADSP2_YM:
2159ad93 663 region_name = "YM";
45b9ee72 664 reg = wm_adsp_region_to_reg(mem, offset);
2159ad93
MB
665 break;
666 case WMFW_ADSP1_ZM:
2159ad93 667 region_name = "ZM";
45b9ee72 668 reg = wm_adsp_region_to_reg(mem, offset);
2159ad93
MB
669 break;
670 default:
671 adsp_warn(dsp,
672 "%s.%d: Unknown region type %x at %d(%x)\n",
673 file, regions, type, pos, pos);
674 break;
675 }
676
677 adsp_dbg(dsp, "%s.%d: %d bytes at %d in %s\n", file,
678 regions, le32_to_cpu(region->len), offset,
679 region_name);
680
681 if (text) {
682 memcpy(text, region->data, le32_to_cpu(region->len));
683 adsp_info(dsp, "%s: %s\n", file, text);
684 kfree(text);
685 }
686
687 if (reg) {
cdcd7f72
CK
688 buf = wm_adsp_buf_alloc(region->data,
689 le32_to_cpu(region->len),
690 &buf_list);
691 if (!buf) {
692 adsp_err(dsp, "Out of memory\n");
693 ret = -ENOMEM;
694 goto out_fw;
695 }
c1a7898d 696
cdcd7f72
CK
697 ret = regmap_raw_write_async(regmap, reg, buf->buf,
698 le32_to_cpu(region->len));
699 if (ret != 0) {
700 adsp_err(dsp,
701 "%s.%d: Failed to write %d bytes at %d in %s: %d\n",
702 file, regions,
703 le32_to_cpu(region->len), offset,
704 region_name, ret);
705 goto out_fw;
2159ad93
MB
706 }
707 }
708
709 pos += le32_to_cpu(region->len) + sizeof(*region);
710 regions++;
711 }
cf17c83c
MB
712
713 ret = regmap_async_complete(regmap);
714 if (ret != 0) {
715 adsp_err(dsp, "Failed to complete async write: %d\n", ret);
716 goto out_fw;
717 }
718
2159ad93
MB
719 if (pos > firmware->size)
720 adsp_warn(dsp, "%s.%d: %zu bytes at end of file\n",
721 file, regions, pos - firmware->size);
722
723out_fw:
cf17c83c
MB
724 regmap_async_complete(regmap);
725 wm_adsp_buf_free(&buf_list);
2159ad93
MB
726 release_firmware(firmware);
727out:
728 kfree(file);
729
730 return ret;
731}
732
81ad93ec 733static int wm_coeff_init_control_caches(struct wm_adsp *adsp)
6ab2b7b4
DP
734{
735 struct wm_coeff_ctl *ctl;
736 int ret;
737
81ad93ec 738 list_for_each_entry(ctl, &adsp->ctl_list, list) {
0c2e3f34 739 if (!ctl->enabled || ctl->set)
6ab2b7b4
DP
740 continue;
741 ret = wm_coeff_read_control(ctl->kcontrol,
742 ctl->cache,
743 ctl->len);
744 if (ret < 0)
745 return ret;
746 }
747
748 return 0;
749}
750
81ad93ec 751static int wm_coeff_sync_controls(struct wm_adsp *adsp)
6ab2b7b4
DP
752{
753 struct wm_coeff_ctl *ctl;
754 int ret;
755
81ad93ec 756 list_for_each_entry(ctl, &adsp->ctl_list, list) {
6ab2b7b4
DP
757 if (!ctl->enabled)
758 continue;
0c2e3f34 759 if (ctl->set) {
6ab2b7b4
DP
760 ret = wm_coeff_write_control(ctl->kcontrol,
761 ctl->cache,
762 ctl->len);
763 if (ret < 0)
764 return ret;
6ab2b7b4
DP
765 }
766 }
767
768 return 0;
769}
770
771static void wm_adsp_ctl_work(struct work_struct *work)
772{
773 struct wmfw_ctl_work *ctl_work = container_of(work,
774 struct wmfw_ctl_work,
775 work);
776
81ad93ec 777 wmfw_add_ctl(ctl_work->adsp, ctl_work->ctl);
6ab2b7b4
DP
778 kfree(ctl_work);
779}
780
92bb4c32 781static int wm_adsp_create_control(struct wm_adsp *dsp,
6ab2b7b4
DP
782 const struct wm_adsp_alg_region *region)
783
784{
6ab2b7b4
DP
785 struct wm_coeff_ctl *ctl;
786 struct wmfw_ctl_work *ctl_work;
787 char *name;
788 char *region_name;
789 int ret;
790
791 name = kmalloc(PAGE_SIZE, GFP_KERNEL);
792 if (!name)
793 return -ENOMEM;
794
795 switch (region->type) {
796 case WMFW_ADSP1_PM:
797 region_name = "PM";
798 break;
799 case WMFW_ADSP1_DM:
800 region_name = "DM";
801 break;
802 case WMFW_ADSP2_XM:
803 region_name = "XM";
804 break;
805 case WMFW_ADSP2_YM:
806 region_name = "YM";
807 break;
808 case WMFW_ADSP1_ZM:
809 region_name = "ZM";
810 break;
811 default:
9dbce044
DC
812 ret = -EINVAL;
813 goto err_name;
6ab2b7b4
DP
814 }
815
816 snprintf(name, PAGE_SIZE, "DSP%d %s %x",
817 dsp->num, region_name, region->alg);
818
81ad93ec 819 list_for_each_entry(ctl, &dsp->ctl_list,
6ab2b7b4
DP
820 list) {
821 if (!strcmp(ctl->name, name)) {
822 if (!ctl->enabled)
823 ctl->enabled = 1;
9dbce044 824 goto found;
6ab2b7b4
DP
825 }
826 }
827
828 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
829 if (!ctl) {
830 ret = -ENOMEM;
831 goto err_name;
832 }
833 ctl->region = *region;
834 ctl->name = kmemdup(name, strlen(name) + 1, GFP_KERNEL);
835 if (!ctl->name) {
836 ret = -ENOMEM;
837 goto err_ctl;
838 }
839 ctl->enabled = 1;
0c2e3f34 840 ctl->set = 0;
6ab2b7b4
DP
841 ctl->ops.xget = wm_coeff_get;
842 ctl->ops.xput = wm_coeff_put;
6ab2b7b4
DP
843 ctl->adsp = dsp;
844
845 ctl->len = region->len;
846 ctl->cache = kzalloc(ctl->len, GFP_KERNEL);
847 if (!ctl->cache) {
848 ret = -ENOMEM;
849 goto err_ctl_name;
850 }
851
852 ctl_work = kzalloc(sizeof(*ctl_work), GFP_KERNEL);
853 if (!ctl_work) {
854 ret = -ENOMEM;
855 goto err_ctl_cache;
856 }
857
81ad93ec 858 ctl_work->adsp = dsp;
6ab2b7b4
DP
859 ctl_work->ctl = ctl;
860 INIT_WORK(&ctl_work->work, wm_adsp_ctl_work);
861 schedule_work(&ctl_work->work);
862
9dbce044 863found:
6ab2b7b4
DP
864 kfree(name);
865
866 return 0;
867
868err_ctl_cache:
869 kfree(ctl->cache);
870err_ctl_name:
871 kfree(ctl->name);
872err_ctl:
873 kfree(ctl);
874err_name:
875 kfree(name);
876 return ret;
877}
878
92bb4c32 879static int wm_adsp_setup_algs(struct wm_adsp *dsp)
db40517c
MB
880{
881 struct regmap *regmap = dsp->regmap;
882 struct wmfw_adsp1_id_hdr adsp1_id;
883 struct wmfw_adsp2_id_hdr adsp2_id;
884 struct wmfw_adsp1_alg_hdr *adsp1_alg;
885 struct wmfw_adsp2_alg_hdr *adsp2_alg;
d62f4bc6 886 void *alg, *buf;
471f4885 887 struct wm_adsp_alg_region *region;
db40517c
MB
888 const struct wm_adsp_region *mem;
889 unsigned int pos, term;
d62f4bc6 890 size_t algs, buf_size;
db40517c
MB
891 __be32 val;
892 int i, ret;
893
894 switch (dsp->type) {
895 case WMFW_ADSP1:
896 mem = wm_adsp_find_region(dsp, WMFW_ADSP1_DM);
897 break;
898 case WMFW_ADSP2:
899 mem = wm_adsp_find_region(dsp, WMFW_ADSP2_XM);
900 break;
901 default:
902 mem = NULL;
903 break;
904 }
905
6c452bda 906 if (WARN_ON(!mem))
db40517c 907 return -EINVAL;
db40517c
MB
908
909 switch (dsp->type) {
910 case WMFW_ADSP1:
911 ret = regmap_raw_read(regmap, mem->base, &adsp1_id,
912 sizeof(adsp1_id));
913 if (ret != 0) {
914 adsp_err(dsp, "Failed to read algorithm info: %d\n",
915 ret);
916 return ret;
917 }
918
d62f4bc6
MB
919 buf = &adsp1_id;
920 buf_size = sizeof(adsp1_id);
921
db40517c 922 algs = be32_to_cpu(adsp1_id.algs);
f395a218 923 dsp->fw_id = be32_to_cpu(adsp1_id.fw.id);
db40517c 924 adsp_info(dsp, "Firmware: %x v%d.%d.%d, %zu algorithms\n",
f395a218 925 dsp->fw_id,
db40517c
MB
926 (be32_to_cpu(adsp1_id.fw.ver) & 0xff0000) >> 16,
927 (be32_to_cpu(adsp1_id.fw.ver) & 0xff00) >> 8,
928 be32_to_cpu(adsp1_id.fw.ver) & 0xff,
929 algs);
930
ac50009f
MB
931 region = kzalloc(sizeof(*region), GFP_KERNEL);
932 if (!region)
933 return -ENOMEM;
934 region->type = WMFW_ADSP1_ZM;
935 region->alg = be32_to_cpu(adsp1_id.fw.id);
936 region->base = be32_to_cpu(adsp1_id.zm);
937 list_add_tail(&region->list, &dsp->alg_regions);
938
939 region = kzalloc(sizeof(*region), GFP_KERNEL);
940 if (!region)
941 return -ENOMEM;
942 region->type = WMFW_ADSP1_DM;
943 region->alg = be32_to_cpu(adsp1_id.fw.id);
944 region->base = be32_to_cpu(adsp1_id.dm);
945 list_add_tail(&region->list, &dsp->alg_regions);
946
db40517c
MB
947 pos = sizeof(adsp1_id) / 2;
948 term = pos + ((sizeof(*adsp1_alg) * algs) / 2);
949 break;
950
951 case WMFW_ADSP2:
952 ret = regmap_raw_read(regmap, mem->base, &adsp2_id,
953 sizeof(adsp2_id));
954 if (ret != 0) {
955 adsp_err(dsp, "Failed to read algorithm info: %d\n",
956 ret);
957 return ret;
958 }
959
d62f4bc6
MB
960 buf = &adsp2_id;
961 buf_size = sizeof(adsp2_id);
962
db40517c 963 algs = be32_to_cpu(adsp2_id.algs);
f395a218 964 dsp->fw_id = be32_to_cpu(adsp2_id.fw.id);
db40517c 965 adsp_info(dsp, "Firmware: %x v%d.%d.%d, %zu algorithms\n",
f395a218 966 dsp->fw_id,
db40517c
MB
967 (be32_to_cpu(adsp2_id.fw.ver) & 0xff0000) >> 16,
968 (be32_to_cpu(adsp2_id.fw.ver) & 0xff00) >> 8,
969 be32_to_cpu(adsp2_id.fw.ver) & 0xff,
970 algs);
971
ac50009f
MB
972 region = kzalloc(sizeof(*region), GFP_KERNEL);
973 if (!region)
974 return -ENOMEM;
975 region->type = WMFW_ADSP2_XM;
976 region->alg = be32_to_cpu(adsp2_id.fw.id);
977 region->base = be32_to_cpu(adsp2_id.xm);
978 list_add_tail(&region->list, &dsp->alg_regions);
979
980 region = kzalloc(sizeof(*region), GFP_KERNEL);
981 if (!region)
982 return -ENOMEM;
983 region->type = WMFW_ADSP2_YM;
984 region->alg = be32_to_cpu(adsp2_id.fw.id);
985 region->base = be32_to_cpu(adsp2_id.ym);
986 list_add_tail(&region->list, &dsp->alg_regions);
987
988 region = kzalloc(sizeof(*region), GFP_KERNEL);
989 if (!region)
990 return -ENOMEM;
991 region->type = WMFW_ADSP2_ZM;
992 region->alg = be32_to_cpu(adsp2_id.fw.id);
993 region->base = be32_to_cpu(adsp2_id.zm);
994 list_add_tail(&region->list, &dsp->alg_regions);
995
db40517c
MB
996 pos = sizeof(adsp2_id) / 2;
997 term = pos + ((sizeof(*adsp2_alg) * algs) / 2);
998 break;
999
1000 default:
6c452bda 1001 WARN(1, "Unknown DSP type");
db40517c
MB
1002 return -EINVAL;
1003 }
1004
1005 if (algs == 0) {
1006 adsp_err(dsp, "No algorithms\n");
1007 return -EINVAL;
1008 }
1009
d62f4bc6
MB
1010 if (algs > 1024) {
1011 adsp_err(dsp, "Algorithm count %zx excessive\n", algs);
1012 print_hex_dump_bytes(dev_name(dsp->dev), DUMP_PREFIX_OFFSET,
1013 buf, buf_size);
1014 return -EINVAL;
1015 }
1016
db40517c
MB
1017 /* Read the terminator first to validate the length */
1018 ret = regmap_raw_read(regmap, mem->base + term, &val, sizeof(val));
1019 if (ret != 0) {
1020 adsp_err(dsp, "Failed to read algorithm list end: %d\n",
1021 ret);
1022 return ret;
1023 }
1024
1025 if (be32_to_cpu(val) != 0xbedead)
1026 adsp_warn(dsp, "Algorithm list end %x 0x%x != 0xbeadead\n",
1027 term, be32_to_cpu(val));
1028
f2a93e2a 1029 alg = kzalloc((term - pos) * 2, GFP_KERNEL | GFP_DMA);
db40517c
MB
1030 if (!alg)
1031 return -ENOMEM;
1032
1033 ret = regmap_raw_read(regmap, mem->base + pos, alg, (term - pos) * 2);
1034 if (ret != 0) {
1035 adsp_err(dsp, "Failed to read algorithm list: %d\n",
1036 ret);
1037 goto out;
1038 }
1039
1040 adsp1_alg = alg;
1041 adsp2_alg = alg;
1042
1043 for (i = 0; i < algs; i++) {
1044 switch (dsp->type) {
1045 case WMFW_ADSP1:
471f4885 1046 adsp_info(dsp, "%d: ID %x v%d.%d.%d DM@%x ZM@%x\n",
db40517c
MB
1047 i, be32_to_cpu(adsp1_alg[i].alg.id),
1048 (be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff0000) >> 16,
1049 (be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff00) >> 8,
471f4885
MB
1050 be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff,
1051 be32_to_cpu(adsp1_alg[i].dm),
1052 be32_to_cpu(adsp1_alg[i].zm));
1053
7480800e 1054 region = kzalloc(sizeof(*region), GFP_KERNEL);
d6d52179
JP
1055 if (!region) {
1056 ret = -ENOMEM;
1057 goto out;
1058 }
7480800e
MB
1059 region->type = WMFW_ADSP1_DM;
1060 region->alg = be32_to_cpu(adsp1_alg[i].alg.id);
1061 region->base = be32_to_cpu(adsp1_alg[i].dm);
6ab2b7b4 1062 region->len = 0;
7480800e 1063 list_add_tail(&region->list, &dsp->alg_regions);
6ab2b7b4
DP
1064 if (i + 1 < algs) {
1065 region->len = be32_to_cpu(adsp1_alg[i + 1].dm);
1066 region->len -= be32_to_cpu(adsp1_alg[i].dm);
c01422a4 1067 region->len *= 4;
92bb4c32 1068 wm_adsp_create_control(dsp, region);
6ab2b7b4
DP
1069 } else {
1070 adsp_warn(dsp, "Missing length info for region DM with ID %x\n",
1071 be32_to_cpu(adsp1_alg[i].alg.id));
1072 }
471f4885 1073
7480800e 1074 region = kzalloc(sizeof(*region), GFP_KERNEL);
d6d52179
JP
1075 if (!region) {
1076 ret = -ENOMEM;
1077 goto out;
1078 }
7480800e
MB
1079 region->type = WMFW_ADSP1_ZM;
1080 region->alg = be32_to_cpu(adsp1_alg[i].alg.id);
1081 region->base = be32_to_cpu(adsp1_alg[i].zm);
6ab2b7b4 1082 region->len = 0;
7480800e 1083 list_add_tail(&region->list, &dsp->alg_regions);
6ab2b7b4
DP
1084 if (i + 1 < algs) {
1085 region->len = be32_to_cpu(adsp1_alg[i + 1].zm);
1086 region->len -= be32_to_cpu(adsp1_alg[i].zm);
c01422a4 1087 region->len *= 4;
92bb4c32 1088 wm_adsp_create_control(dsp, region);
6ab2b7b4
DP
1089 } else {
1090 adsp_warn(dsp, "Missing length info for region ZM with ID %x\n",
1091 be32_to_cpu(adsp1_alg[i].alg.id));
1092 }
db40517c
MB
1093 break;
1094
1095 case WMFW_ADSP2:
471f4885
MB
1096 adsp_info(dsp,
1097 "%d: ID %x v%d.%d.%d XM@%x YM@%x ZM@%x\n",
db40517c
MB
1098 i, be32_to_cpu(adsp2_alg[i].alg.id),
1099 (be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff0000) >> 16,
1100 (be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff00) >> 8,
471f4885
MB
1101 be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff,
1102 be32_to_cpu(adsp2_alg[i].xm),
1103 be32_to_cpu(adsp2_alg[i].ym),
1104 be32_to_cpu(adsp2_alg[i].zm));
1105
7480800e 1106 region = kzalloc(sizeof(*region), GFP_KERNEL);
d6d52179
JP
1107 if (!region) {
1108 ret = -ENOMEM;
1109 goto out;
1110 }
7480800e
MB
1111 region->type = WMFW_ADSP2_XM;
1112 region->alg = be32_to_cpu(adsp2_alg[i].alg.id);
1113 region->base = be32_to_cpu(adsp2_alg[i].xm);
6ab2b7b4 1114 region->len = 0;
7480800e 1115 list_add_tail(&region->list, &dsp->alg_regions);
6ab2b7b4
DP
1116 if (i + 1 < algs) {
1117 region->len = be32_to_cpu(adsp2_alg[i + 1].xm);
1118 region->len -= be32_to_cpu(adsp2_alg[i].xm);
c01422a4 1119 region->len *= 4;
92bb4c32 1120 wm_adsp_create_control(dsp, region);
6ab2b7b4
DP
1121 } else {
1122 adsp_warn(dsp, "Missing length info for region XM with ID %x\n",
1123 be32_to_cpu(adsp2_alg[i].alg.id));
1124 }
471f4885 1125
7480800e 1126 region = kzalloc(sizeof(*region), GFP_KERNEL);
d6d52179
JP
1127 if (!region) {
1128 ret = -ENOMEM;
1129 goto out;
1130 }
7480800e
MB
1131 region->type = WMFW_ADSP2_YM;
1132 region->alg = be32_to_cpu(adsp2_alg[i].alg.id);
1133 region->base = be32_to_cpu(adsp2_alg[i].ym);
6ab2b7b4 1134 region->len = 0;
7480800e 1135 list_add_tail(&region->list, &dsp->alg_regions);
6ab2b7b4
DP
1136 if (i + 1 < algs) {
1137 region->len = be32_to_cpu(adsp2_alg[i + 1].ym);
1138 region->len -= be32_to_cpu(adsp2_alg[i].ym);
c01422a4 1139 region->len *= 4;
92bb4c32 1140 wm_adsp_create_control(dsp, region);
6ab2b7b4
DP
1141 } else {
1142 adsp_warn(dsp, "Missing length info for region YM with ID %x\n",
1143 be32_to_cpu(adsp2_alg[i].alg.id));
1144 }
471f4885 1145
7480800e 1146 region = kzalloc(sizeof(*region), GFP_KERNEL);
d6d52179
JP
1147 if (!region) {
1148 ret = -ENOMEM;
1149 goto out;
1150 }
7480800e
MB
1151 region->type = WMFW_ADSP2_ZM;
1152 region->alg = be32_to_cpu(adsp2_alg[i].alg.id);
1153 region->base = be32_to_cpu(adsp2_alg[i].zm);
6ab2b7b4 1154 region->len = 0;
7480800e 1155 list_add_tail(&region->list, &dsp->alg_regions);
6ab2b7b4
DP
1156 if (i + 1 < algs) {
1157 region->len = be32_to_cpu(adsp2_alg[i + 1].zm);
1158 region->len -= be32_to_cpu(adsp2_alg[i].zm);
c01422a4 1159 region->len *= 4;
92bb4c32 1160 wm_adsp_create_control(dsp, region);
6ab2b7b4
DP
1161 } else {
1162 adsp_warn(dsp, "Missing length info for region ZM with ID %x\n",
1163 be32_to_cpu(adsp2_alg[i].alg.id));
1164 }
db40517c
MB
1165 break;
1166 }
1167 }
1168
1169out:
1170 kfree(alg);
1171 return ret;
1172}
1173
2159ad93
MB
1174static int wm_adsp_load_coeff(struct wm_adsp *dsp)
1175{
cf17c83c 1176 LIST_HEAD(buf_list);
2159ad93
MB
1177 struct regmap *regmap = dsp->regmap;
1178 struct wmfw_coeff_hdr *hdr;
1179 struct wmfw_coeff_item *blk;
1180 const struct firmware *firmware;
471f4885
MB
1181 const struct wm_adsp_region *mem;
1182 struct wm_adsp_alg_region *alg_region;
2159ad93
MB
1183 const char *region_name;
1184 int ret, pos, blocks, type, offset, reg;
1185 char *file;
cf17c83c 1186 struct wm_adsp_buf *buf;
2159ad93
MB
1187
1188 file = kzalloc(PAGE_SIZE, GFP_KERNEL);
1189 if (file == NULL)
1190 return -ENOMEM;
1191
1023dbd9
MB
1192 snprintf(file, PAGE_SIZE, "%s-dsp%d-%s.bin", dsp->part, dsp->num,
1193 wm_adsp_fw[dsp->fw].file);
2159ad93
MB
1194 file[PAGE_SIZE - 1] = '\0';
1195
1196 ret = request_firmware(&firmware, file, dsp->dev);
1197 if (ret != 0) {
1198 adsp_warn(dsp, "Failed to request '%s'\n", file);
1199 ret = 0;
1200 goto out;
1201 }
1202 ret = -EINVAL;
1203
1204 if (sizeof(*hdr) >= firmware->size) {
1205 adsp_err(dsp, "%s: file too short, %zu bytes\n",
1206 file, firmware->size);
1207 goto out_fw;
1208 }
1209
1210 hdr = (void*)&firmware->data[0];
1211 if (memcmp(hdr->magic, "WMDR", 4) != 0) {
1212 adsp_err(dsp, "%s: invalid magic\n", file);
a4cdbec7 1213 goto out_fw;
2159ad93
MB
1214 }
1215
c712326d
MB
1216 switch (be32_to_cpu(hdr->rev) & 0xff) {
1217 case 1:
1218 break;
1219 default:
1220 adsp_err(dsp, "%s: Unsupported coefficient file format %d\n",
1221 file, be32_to_cpu(hdr->rev) & 0xff);
1222 ret = -EINVAL;
1223 goto out_fw;
1224 }
1225
2159ad93
MB
1226 adsp_dbg(dsp, "%s: v%d.%d.%d\n", file,
1227 (le32_to_cpu(hdr->ver) >> 16) & 0xff,
1228 (le32_to_cpu(hdr->ver) >> 8) & 0xff,
1229 le32_to_cpu(hdr->ver) & 0xff);
1230
1231 pos = le32_to_cpu(hdr->len);
1232
1233 blocks = 0;
1234 while (pos < firmware->size &&
1235 pos - firmware->size > sizeof(*blk)) {
1236 blk = (void*)(&firmware->data[pos]);
1237
c712326d
MB
1238 type = le16_to_cpu(blk->type);
1239 offset = le16_to_cpu(blk->offset);
2159ad93
MB
1240
1241 adsp_dbg(dsp, "%s.%d: %x v%d.%d.%d\n",
1242 file, blocks, le32_to_cpu(blk->id),
1243 (le32_to_cpu(blk->ver) >> 16) & 0xff,
1244 (le32_to_cpu(blk->ver) >> 8) & 0xff,
1245 le32_to_cpu(blk->ver) & 0xff);
1246 adsp_dbg(dsp, "%s.%d: %d bytes at 0x%x in %x\n",
1247 file, blocks, le32_to_cpu(blk->len), offset, type);
1248
1249 reg = 0;
1250 region_name = "Unknown";
1251 switch (type) {
c712326d
MB
1252 case (WMFW_NAME_TEXT << 8):
1253 case (WMFW_INFO_TEXT << 8):
2159ad93 1254 break;
c712326d 1255 case (WMFW_ABSOLUTE << 8):
f395a218
MB
1256 /*
1257 * Old files may use this for global
1258 * coefficients.
1259 */
1260 if (le32_to_cpu(blk->id) == dsp->fw_id &&
1261 offset == 0) {
1262 region_name = "global coefficients";
1263 mem = wm_adsp_find_region(dsp, type);
1264 if (!mem) {
1265 adsp_err(dsp, "No ZM\n");
1266 break;
1267 }
1268 reg = wm_adsp_region_to_reg(mem, 0);
1269
1270 } else {
1271 region_name = "register";
1272 reg = offset;
1273 }
2159ad93 1274 break;
471f4885
MB
1275
1276 case WMFW_ADSP1_DM:
1277 case WMFW_ADSP1_ZM:
1278 case WMFW_ADSP2_XM:
1279 case WMFW_ADSP2_YM:
1280 adsp_dbg(dsp, "%s.%d: %d bytes in %x for %x\n",
1281 file, blocks, le32_to_cpu(blk->len),
1282 type, le32_to_cpu(blk->id));
1283
1284 mem = wm_adsp_find_region(dsp, type);
1285 if (!mem) {
1286 adsp_err(dsp, "No base for region %x\n", type);
1287 break;
1288 }
1289
1290 reg = 0;
1291 list_for_each_entry(alg_region,
1292 &dsp->alg_regions, list) {
1293 if (le32_to_cpu(blk->id) == alg_region->alg &&
1294 type == alg_region->type) {
338c5188 1295 reg = alg_region->base;
471f4885
MB
1296 reg = wm_adsp_region_to_reg(mem,
1297 reg);
338c5188 1298 reg += offset;
d733dc08 1299 break;
471f4885
MB
1300 }
1301 }
1302
1303 if (reg == 0)
1304 adsp_err(dsp, "No %x for algorithm %x\n",
1305 type, le32_to_cpu(blk->id));
1306 break;
1307
2159ad93 1308 default:
25c62f7e
MB
1309 adsp_err(dsp, "%s.%d: Unknown region type %x at %d\n",
1310 file, blocks, type, pos);
2159ad93
MB
1311 break;
1312 }
1313
1314 if (reg) {
cf17c83c
MB
1315 buf = wm_adsp_buf_alloc(blk->data,
1316 le32_to_cpu(blk->len),
1317 &buf_list);
a76fefab
MB
1318 if (!buf) {
1319 adsp_err(dsp, "Out of memory\n");
f4b82812
WY
1320 ret = -ENOMEM;
1321 goto out_fw;
a76fefab
MB
1322 }
1323
20da6d5a
MB
1324 adsp_dbg(dsp, "%s.%d: Writing %d bytes at %x\n",
1325 file, blocks, le32_to_cpu(blk->len),
1326 reg);
cf17c83c
MB
1327 ret = regmap_raw_write_async(regmap, reg, buf->buf,
1328 le32_to_cpu(blk->len));
2159ad93
MB
1329 if (ret != 0) {
1330 adsp_err(dsp,
43bc3bf6
DP
1331 "%s.%d: Failed to write to %x in %s: %d\n",
1332 file, blocks, reg, region_name, ret);
2159ad93
MB
1333 }
1334 }
1335
be951017 1336 pos += (le32_to_cpu(blk->len) + sizeof(*blk) + 3) & ~0x03;
2159ad93
MB
1337 blocks++;
1338 }
1339
cf17c83c
MB
1340 ret = regmap_async_complete(regmap);
1341 if (ret != 0)
1342 adsp_err(dsp, "Failed to complete async write: %d\n", ret);
1343
2159ad93
MB
1344 if (pos > firmware->size)
1345 adsp_warn(dsp, "%s.%d: %zu bytes at end of file\n",
1346 file, blocks, pos - firmware->size);
1347
1348out_fw:
9da7a5a9 1349 regmap_async_complete(regmap);
2159ad93 1350 release_firmware(firmware);
cf17c83c 1351 wm_adsp_buf_free(&buf_list);
2159ad93
MB
1352out:
1353 kfree(file);
f4b82812 1354 return ret;
2159ad93
MB
1355}
1356
5e7a7a22
MB
1357int wm_adsp1_init(struct wm_adsp *adsp)
1358{
1359 INIT_LIST_HEAD(&adsp->alg_regions);
1360
1361 return 0;
1362}
1363EXPORT_SYMBOL_GPL(wm_adsp1_init);
1364
2159ad93
MB
1365int wm_adsp1_event(struct snd_soc_dapm_widget *w,
1366 struct snd_kcontrol *kcontrol,
1367 int event)
1368{
72718517 1369 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
2159ad93
MB
1370 struct wm_adsp *dsps = snd_soc_codec_get_drvdata(codec);
1371 struct wm_adsp *dsp = &dsps[w->shift];
b0101b4f 1372 struct wm_adsp_alg_region *alg_region;
6ab2b7b4 1373 struct wm_coeff_ctl *ctl;
2159ad93 1374 int ret;
94e205bf 1375 int val;
2159ad93 1376
00200107 1377 dsp->card = codec->component.card;
92bb4c32 1378
2159ad93
MB
1379 switch (event) {
1380 case SND_SOC_DAPM_POST_PMU:
1381 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
1382 ADSP1_SYS_ENA, ADSP1_SYS_ENA);
1383
94e205bf
CR
1384 /*
1385 * For simplicity set the DSP clock rate to be the
1386 * SYSCLK rate rather than making it configurable.
1387 */
1388 if(dsp->sysclk_reg) {
1389 ret = regmap_read(dsp->regmap, dsp->sysclk_reg, &val);
1390 if (ret != 0) {
1391 adsp_err(dsp, "Failed to read SYSCLK state: %d\n",
1392 ret);
1393 return ret;
1394 }
1395
1396 val = (val & dsp->sysclk_mask)
1397 >> dsp->sysclk_shift;
1398
1399 ret = regmap_update_bits(dsp->regmap,
1400 dsp->base + ADSP1_CONTROL_31,
1401 ADSP1_CLK_SEL_MASK, val);
1402 if (ret != 0) {
1403 adsp_err(dsp, "Failed to set clock rate: %d\n",
1404 ret);
1405 return ret;
1406 }
1407 }
1408
2159ad93
MB
1409 ret = wm_adsp_load(dsp);
1410 if (ret != 0)
1411 goto err;
1412
92bb4c32 1413 ret = wm_adsp_setup_algs(dsp);
db40517c
MB
1414 if (ret != 0)
1415 goto err;
1416
2159ad93
MB
1417 ret = wm_adsp_load_coeff(dsp);
1418 if (ret != 0)
1419 goto err;
1420
0c2e3f34 1421 /* Initialize caches for enabled and unset controls */
81ad93ec 1422 ret = wm_coeff_init_control_caches(dsp);
6ab2b7b4
DP
1423 if (ret != 0)
1424 goto err;
1425
0c2e3f34 1426 /* Sync set controls */
81ad93ec 1427 ret = wm_coeff_sync_controls(dsp);
6ab2b7b4
DP
1428 if (ret != 0)
1429 goto err;
1430
2159ad93
MB
1431 /* Start the core running */
1432 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
1433 ADSP1_CORE_ENA | ADSP1_START,
1434 ADSP1_CORE_ENA | ADSP1_START);
1435 break;
1436
1437 case SND_SOC_DAPM_PRE_PMD:
1438 /* Halt the core */
1439 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
1440 ADSP1_CORE_ENA | ADSP1_START, 0);
1441
1442 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_19,
1443 ADSP1_WDMA_BUFFER_LENGTH_MASK, 0);
1444
1445 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
1446 ADSP1_SYS_ENA, 0);
6ab2b7b4 1447
81ad93ec 1448 list_for_each_entry(ctl, &dsp->ctl_list, list)
6ab2b7b4 1449 ctl->enabled = 0;
b0101b4f
DP
1450
1451 while (!list_empty(&dsp->alg_regions)) {
1452 alg_region = list_first_entry(&dsp->alg_regions,
1453 struct wm_adsp_alg_region,
1454 list);
1455 list_del(&alg_region->list);
1456 kfree(alg_region);
1457 }
2159ad93
MB
1458 break;
1459
1460 default:
1461 break;
1462 }
1463
1464 return 0;
1465
1466err:
1467 regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
1468 ADSP1_SYS_ENA, 0);
1469 return ret;
1470}
1471EXPORT_SYMBOL_GPL(wm_adsp1_event);
1472
1473static int wm_adsp2_ena(struct wm_adsp *dsp)
1474{
1475 unsigned int val;
1476 int ret, count;
1477
1552c325
MB
1478 ret = regmap_update_bits_async(dsp->regmap, dsp->base + ADSP2_CONTROL,
1479 ADSP2_SYS_ENA, ADSP2_SYS_ENA);
2159ad93
MB
1480 if (ret != 0)
1481 return ret;
1482
1483 /* Wait for the RAM to start, should be near instantaneous */
939fd1e8 1484 for (count = 0; count < 10; ++count) {
2159ad93
MB
1485 ret = regmap_read(dsp->regmap, dsp->base + ADSP2_STATUS1,
1486 &val);
1487 if (ret != 0)
1488 return ret;
939fd1e8
CK
1489
1490 if (val & ADSP2_RAM_RDY)
1491 break;
1492
1493 msleep(1);
1494 }
2159ad93
MB
1495
1496 if (!(val & ADSP2_RAM_RDY)) {
1497 adsp_err(dsp, "Failed to start DSP RAM\n");
1498 return -EBUSY;
1499 }
1500
1501 adsp_dbg(dsp, "RAM ready after %d polls\n", count);
2159ad93
MB
1502
1503 return 0;
1504}
1505
18b1a902 1506static void wm_adsp2_boot_work(struct work_struct *work)
2159ad93 1507{
d8a64d6a
CK
1508 struct wm_adsp *dsp = container_of(work,
1509 struct wm_adsp,
1510 boot_work);
2159ad93 1511 int ret;
d8a64d6a 1512 unsigned int val;
2159ad93 1513
d8a64d6a
CK
1514 /*
1515 * For simplicity set the DSP clock rate to be the
1516 * SYSCLK rate rather than making it configurable.
1517 */
1518 ret = regmap_read(dsp->regmap, ARIZONA_SYSTEM_CLOCK_1, &val);
1519 if (ret != 0) {
1520 adsp_err(dsp, "Failed to read SYSCLK state: %d\n", ret);
1521 return;
1522 }
1523 val = (val & ARIZONA_SYSCLK_FREQ_MASK)
1524 >> ARIZONA_SYSCLK_FREQ_SHIFT;
92bb4c32 1525
d8a64d6a
CK
1526 ret = regmap_update_bits_async(dsp->regmap,
1527 dsp->base + ADSP2_CLOCKING,
1528 ADSP2_CLK_SEL_MASK, val);
1529 if (ret != 0) {
1530 adsp_err(dsp, "Failed to set clock rate: %d\n", ret);
1531 return;
1532 }
dd49e2c8 1533
d8a64d6a
CK
1534 if (dsp->dvfs) {
1535 ret = regmap_read(dsp->regmap,
1536 dsp->base + ADSP2_CLOCKING, &val);
dd49e2c8 1537 if (ret != 0) {
62c35b3b 1538 adsp_err(dsp, "Failed to read clocking: %d\n", ret);
d8a64d6a 1539 return;
dd49e2c8
MB
1540 }
1541
d8a64d6a
CK
1542 if ((val & ADSP2_CLK_SEL_MASK) >= 3) {
1543 ret = regulator_enable(dsp->dvfs);
973838a0 1544 if (ret != 0) {
62c35b3b
CK
1545 adsp_err(dsp,
1546 "Failed to enable supply: %d\n",
1547 ret);
d8a64d6a 1548 return;
973838a0
MB
1549 }
1550
d8a64d6a
CK
1551 ret = regulator_set_voltage(dsp->dvfs,
1552 1800000,
1553 1800000);
1554 if (ret != 0) {
62c35b3b
CK
1555 adsp_err(dsp,
1556 "Failed to raise supply: %d\n",
1557 ret);
d8a64d6a 1558 return;
973838a0
MB
1559 }
1560 }
d8a64d6a 1561 }
973838a0 1562
d8a64d6a
CK
1563 ret = wm_adsp2_ena(dsp);
1564 if (ret != 0)
1565 return;
2159ad93 1566
d8a64d6a
CK
1567 ret = wm_adsp_load(dsp);
1568 if (ret != 0)
1569 goto err;
2159ad93 1570
d8a64d6a
CK
1571 ret = wm_adsp_setup_algs(dsp);
1572 if (ret != 0)
1573 goto err;
db40517c 1574
d8a64d6a
CK
1575 ret = wm_adsp_load_coeff(dsp);
1576 if (ret != 0)
1577 goto err;
2159ad93 1578
d8a64d6a
CK
1579 /* Initialize caches for enabled and unset controls */
1580 ret = wm_coeff_init_control_caches(dsp);
1581 if (ret != 0)
1582 goto err;
6ab2b7b4 1583
d8a64d6a
CK
1584 /* Sync set controls */
1585 ret = wm_coeff_sync_controls(dsp);
1586 if (ret != 0)
1587 goto err;
1588
d8a64d6a
CK
1589 dsp->running = true;
1590
1591 return;
6ab2b7b4 1592
d8a64d6a
CK
1593err:
1594 regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
1595 ADSP2_SYS_ENA | ADSP2_CORE_ENA | ADSP2_START, 0);
1596}
1597
12db5edd
CK
1598int wm_adsp2_early_event(struct snd_soc_dapm_widget *w,
1599 struct snd_kcontrol *kcontrol, int event)
1600{
72718517 1601 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
12db5edd
CK
1602 struct wm_adsp *dsps = snd_soc_codec_get_drvdata(codec);
1603 struct wm_adsp *dsp = &dsps[w->shift];
1604
00200107 1605 dsp->card = codec->component.card;
12db5edd
CK
1606
1607 switch (event) {
1608 case SND_SOC_DAPM_PRE_PMU:
1609 queue_work(system_unbound_wq, &dsp->boot_work);
1610 break;
1611 default:
1612 break;
cab27258 1613 }
12db5edd
CK
1614
1615 return 0;
1616}
1617EXPORT_SYMBOL_GPL(wm_adsp2_early_event);
1618
d8a64d6a
CK
1619int wm_adsp2_event(struct snd_soc_dapm_widget *w,
1620 struct snd_kcontrol *kcontrol, int event)
1621{
72718517 1622 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
d8a64d6a
CK
1623 struct wm_adsp *dsps = snd_soc_codec_get_drvdata(codec);
1624 struct wm_adsp *dsp = &dsps[w->shift];
1625 struct wm_adsp_alg_region *alg_region;
1626 struct wm_coeff_ctl *ctl;
1627 int ret;
1628
d8a64d6a
CK
1629 switch (event) {
1630 case SND_SOC_DAPM_POST_PMU:
d8a64d6a
CK
1631 flush_work(&dsp->boot_work);
1632
1633 if (!dsp->running)
1634 return -EIO;
6ab2b7b4 1635
d8a64d6a
CK
1636 ret = regmap_update_bits(dsp->regmap,
1637 dsp->base + ADSP2_CONTROL,
00e4c3b6
CK
1638 ADSP2_CORE_ENA | ADSP2_START,
1639 ADSP2_CORE_ENA | ADSP2_START);
2159ad93
MB
1640 if (ret != 0)
1641 goto err;
1642 break;
1643
1644 case SND_SOC_DAPM_PRE_PMD:
1023dbd9
MB
1645 dsp->running = false;
1646
2159ad93 1647 regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
a7f9be7e
MB
1648 ADSP2_SYS_ENA | ADSP2_CORE_ENA |
1649 ADSP2_START, 0);
973838a0 1650
2d30b575
MB
1651 /* Make sure DMAs are quiesced */
1652 regmap_write(dsp->regmap, dsp->base + ADSP2_WDMA_CONFIG_1, 0);
1653 regmap_write(dsp->regmap, dsp->base + ADSP2_WDMA_CONFIG_2, 0);
1654 regmap_write(dsp->regmap, dsp->base + ADSP2_RDMA_CONFIG_1, 0);
1655
973838a0
MB
1656 if (dsp->dvfs) {
1657 ret = regulator_set_voltage(dsp->dvfs, 1200000,
1658 1800000);
1659 if (ret != 0)
62c35b3b
CK
1660 adsp_warn(dsp,
1661 "Failed to lower supply: %d\n",
1662 ret);
973838a0
MB
1663
1664 ret = regulator_disable(dsp->dvfs);
1665 if (ret != 0)
62c35b3b
CK
1666 adsp_err(dsp,
1667 "Failed to enable supply: %d\n",
1668 ret);
973838a0 1669 }
471f4885 1670
81ad93ec 1671 list_for_each_entry(ctl, &dsp->ctl_list, list)
6ab2b7b4 1672 ctl->enabled = 0;
6ab2b7b4 1673
471f4885
MB
1674 while (!list_empty(&dsp->alg_regions)) {
1675 alg_region = list_first_entry(&dsp->alg_regions,
1676 struct wm_adsp_alg_region,
1677 list);
1678 list_del(&alg_region->list);
1679 kfree(alg_region);
1680 }
ddbc5efe
CK
1681
1682 adsp_dbg(dsp, "Shutdown complete\n");
2159ad93
MB
1683 break;
1684
1685 default:
1686 break;
1687 }
1688
1689 return 0;
1690err:
1691 regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
a7f9be7e 1692 ADSP2_SYS_ENA | ADSP2_CORE_ENA | ADSP2_START, 0);
2159ad93
MB
1693 return ret;
1694}
1695EXPORT_SYMBOL_GPL(wm_adsp2_event);
973838a0
MB
1696
1697int wm_adsp2_init(struct wm_adsp *adsp, bool dvfs)
1698{
1699 int ret;
1700
10a2b662
MB
1701 /*
1702 * Disable the DSP memory by default when in reset for a small
1703 * power saving.
1704 */
1705 ret = regmap_update_bits(adsp->regmap, adsp->base + ADSP2_CONTROL,
1706 ADSP2_MEM_ENA, 0);
1707 if (ret != 0) {
1708 adsp_err(adsp, "Failed to clear memory retention: %d\n", ret);
1709 return ret;
1710 }
1711
471f4885 1712 INIT_LIST_HEAD(&adsp->alg_regions);
81ad93ec 1713 INIT_LIST_HEAD(&adsp->ctl_list);
d8a64d6a 1714 INIT_WORK(&adsp->boot_work, wm_adsp2_boot_work);
6ab2b7b4 1715
973838a0
MB
1716 if (dvfs) {
1717 adsp->dvfs = devm_regulator_get(adsp->dev, "DCVDD");
1718 if (IS_ERR(adsp->dvfs)) {
1719 ret = PTR_ERR(adsp->dvfs);
62c35b3b 1720 adsp_err(adsp, "Failed to get DCVDD: %d\n", ret);
81ad93ec 1721 return ret;
973838a0
MB
1722 }
1723
1724 ret = regulator_enable(adsp->dvfs);
1725 if (ret != 0) {
62c35b3b 1726 adsp_err(adsp, "Failed to enable DCVDD: %d\n", ret);
81ad93ec 1727 return ret;
973838a0
MB
1728 }
1729
1730 ret = regulator_set_voltage(adsp->dvfs, 1200000, 1800000);
1731 if (ret != 0) {
62c35b3b 1732 adsp_err(adsp, "Failed to initialise DVFS: %d\n", ret);
81ad93ec 1733 return ret;
973838a0
MB
1734 }
1735
1736 ret = regulator_disable(adsp->dvfs);
1737 if (ret != 0) {
62c35b3b 1738 adsp_err(adsp, "Failed to disable DCVDD: %d\n", ret);
81ad93ec 1739 return ret;
973838a0
MB
1740 }
1741 }
1742
1743 return 0;
1744}
1745EXPORT_SYMBOL_GPL(wm_adsp2_init);
0a37c6ef
PD
1746
1747MODULE_LICENSE("GPL v2");
This page took 0.678392 seconds and 5 git commands to generate.