2 * wm_adsp.c -- Wolfson ADSP support
4 * Copyright 2012 Wolfson Microelectronics plc
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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.
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/init.h>
16 #include <linux/delay.h>
17 #include <linux/firmware.h>
18 #include <linux/list.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/regmap.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/slab.h>
24 #include <linux/workqueue.h>
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 #include <sound/soc.h>
29 #include <sound/jack.h>
30 #include <sound/initval.h>
31 #include <sound/tlv.h>
33 #include <linux/mfd/arizona/registers.h>
38 #define adsp_crit(_dsp, fmt, ...) \
39 dev_crit(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__)
40 #define adsp_err(_dsp, fmt, ...) \
41 dev_err(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__)
42 #define adsp_warn(_dsp, fmt, ...) \
43 dev_warn(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__)
44 #define adsp_info(_dsp, fmt, ...) \
45 dev_info(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__)
46 #define adsp_dbg(_dsp, fmt, ...) \
47 dev_dbg(_dsp->dev, "DSP%d: " fmt, _dsp->num, ##__VA_ARGS__)
49 #define ADSP1_CONTROL_1 0x00
50 #define ADSP1_CONTROL_2 0x02
51 #define ADSP1_CONTROL_3 0x03
52 #define ADSP1_CONTROL_4 0x04
53 #define ADSP1_CONTROL_5 0x06
54 #define ADSP1_CONTROL_6 0x07
55 #define ADSP1_CONTROL_7 0x08
56 #define ADSP1_CONTROL_8 0x09
57 #define ADSP1_CONTROL_9 0x0A
58 #define ADSP1_CONTROL_10 0x0B
59 #define ADSP1_CONTROL_11 0x0C
60 #define ADSP1_CONTROL_12 0x0D
61 #define ADSP1_CONTROL_13 0x0F
62 #define ADSP1_CONTROL_14 0x10
63 #define ADSP1_CONTROL_15 0x11
64 #define ADSP1_CONTROL_16 0x12
65 #define ADSP1_CONTROL_17 0x13
66 #define ADSP1_CONTROL_18 0x14
67 #define ADSP1_CONTROL_19 0x16
68 #define ADSP1_CONTROL_20 0x17
69 #define ADSP1_CONTROL_21 0x18
70 #define ADSP1_CONTROL_22 0x1A
71 #define ADSP1_CONTROL_23 0x1B
72 #define ADSP1_CONTROL_24 0x1C
73 #define ADSP1_CONTROL_25 0x1E
74 #define ADSP1_CONTROL_26 0x20
75 #define ADSP1_CONTROL_27 0x21
76 #define ADSP1_CONTROL_28 0x22
77 #define ADSP1_CONTROL_29 0x23
78 #define ADSP1_CONTROL_30 0x24
79 #define ADSP1_CONTROL_31 0x26
84 #define ADSP1_WDMA_BUFFER_LENGTH_MASK 0x00FF /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */
85 #define ADSP1_WDMA_BUFFER_LENGTH_SHIFT 0 /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */
86 #define ADSP1_WDMA_BUFFER_LENGTH_WIDTH 8 /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */
92 #define ADSP1_DBG_CLK_ENA 0x0008 /* DSP1_DBG_CLK_ENA */
93 #define ADSP1_DBG_CLK_ENA_MASK 0x0008 /* DSP1_DBG_CLK_ENA */
94 #define ADSP1_DBG_CLK_ENA_SHIFT 3 /* DSP1_DBG_CLK_ENA */
95 #define ADSP1_DBG_CLK_ENA_WIDTH 1 /* DSP1_DBG_CLK_ENA */
96 #define ADSP1_SYS_ENA 0x0004 /* DSP1_SYS_ENA */
97 #define ADSP1_SYS_ENA_MASK 0x0004 /* DSP1_SYS_ENA */
98 #define ADSP1_SYS_ENA_SHIFT 2 /* DSP1_SYS_ENA */
99 #define ADSP1_SYS_ENA_WIDTH 1 /* DSP1_SYS_ENA */
100 #define ADSP1_CORE_ENA 0x0002 /* DSP1_CORE_ENA */
101 #define ADSP1_CORE_ENA_MASK 0x0002 /* DSP1_CORE_ENA */
102 #define ADSP1_CORE_ENA_SHIFT 1 /* DSP1_CORE_ENA */
103 #define ADSP1_CORE_ENA_WIDTH 1 /* DSP1_CORE_ENA */
104 #define ADSP1_START 0x0001 /* DSP1_START */
105 #define ADSP1_START_MASK 0x0001 /* DSP1_START */
106 #define ADSP1_START_SHIFT 0 /* DSP1_START */
107 #define ADSP1_START_WIDTH 1 /* DSP1_START */
112 #define ADSP1_CLK_SEL_MASK 0x0007 /* CLK_SEL_ENA */
113 #define ADSP1_CLK_SEL_SHIFT 0 /* CLK_SEL_ENA */
114 #define ADSP1_CLK_SEL_WIDTH 3 /* CLK_SEL_ENA */
116 #define ADSP2_CONTROL 0x0
117 #define ADSP2_CLOCKING 0x1
118 #define ADSP2_STATUS1 0x4
119 #define ADSP2_WDMA_CONFIG_1 0x30
120 #define ADSP2_WDMA_CONFIG_2 0x31
121 #define ADSP2_RDMA_CONFIG_1 0x34
127 #define ADSP2_MEM_ENA 0x0010 /* DSP1_MEM_ENA */
128 #define ADSP2_MEM_ENA_MASK 0x0010 /* DSP1_MEM_ENA */
129 #define ADSP2_MEM_ENA_SHIFT 4 /* DSP1_MEM_ENA */
130 #define ADSP2_MEM_ENA_WIDTH 1 /* DSP1_MEM_ENA */
131 #define ADSP2_SYS_ENA 0x0004 /* DSP1_SYS_ENA */
132 #define ADSP2_SYS_ENA_MASK 0x0004 /* DSP1_SYS_ENA */
133 #define ADSP2_SYS_ENA_SHIFT 2 /* DSP1_SYS_ENA */
134 #define ADSP2_SYS_ENA_WIDTH 1 /* DSP1_SYS_ENA */
135 #define ADSP2_CORE_ENA 0x0002 /* DSP1_CORE_ENA */
136 #define ADSP2_CORE_ENA_MASK 0x0002 /* DSP1_CORE_ENA */
137 #define ADSP2_CORE_ENA_SHIFT 1 /* DSP1_CORE_ENA */
138 #define ADSP2_CORE_ENA_WIDTH 1 /* DSP1_CORE_ENA */
139 #define ADSP2_START 0x0001 /* DSP1_START */
140 #define ADSP2_START_MASK 0x0001 /* DSP1_START */
141 #define ADSP2_START_SHIFT 0 /* DSP1_START */
142 #define ADSP2_START_WIDTH 1 /* DSP1_START */
147 #define ADSP2_CLK_SEL_MASK 0x0007 /* CLK_SEL_ENA */
148 #define ADSP2_CLK_SEL_SHIFT 0 /* CLK_SEL_ENA */
149 #define ADSP2_CLK_SEL_WIDTH 3 /* CLK_SEL_ENA */
154 #define ADSP2_RAM_RDY 0x0001
155 #define ADSP2_RAM_RDY_MASK 0x0001
156 #define ADSP2_RAM_RDY_SHIFT 0
157 #define ADSP2_RAM_RDY_WIDTH 1
160 struct list_head list
;
164 static struct wm_adsp_buf
*wm_adsp_buf_alloc(const void *src
, size_t len
,
165 struct list_head
*list
)
167 struct wm_adsp_buf
*buf
= kzalloc(sizeof(*buf
), GFP_KERNEL
);
172 buf
->buf
= kmemdup(src
, len
, GFP_KERNEL
| GFP_DMA
);
179 list_add_tail(&buf
->list
, list
);
184 static void wm_adsp_buf_free(struct list_head
*list
)
186 while (!list_empty(list
)) {
187 struct wm_adsp_buf
*buf
= list_first_entry(list
,
190 list_del(&buf
->list
);
196 #define WM_ADSP_NUM_FW 4
198 #define WM_ADSP_FW_MBC_VSS 0
199 #define WM_ADSP_FW_TX 1
200 #define WM_ADSP_FW_TX_SPK 2
201 #define WM_ADSP_FW_RX_ANC 3
203 static const char *wm_adsp_fw_text
[WM_ADSP_NUM_FW
] = {
204 [WM_ADSP_FW_MBC_VSS
] = "MBC/VSS",
205 [WM_ADSP_FW_TX
] = "Tx",
206 [WM_ADSP_FW_TX_SPK
] = "Tx Speaker",
207 [WM_ADSP_FW_RX_ANC
] = "Rx ANC",
212 } wm_adsp_fw
[WM_ADSP_NUM_FW
] = {
213 [WM_ADSP_FW_MBC_VSS
] = { .file
= "mbc-vss" },
214 [WM_ADSP_FW_TX
] = { .file
= "tx" },
215 [WM_ADSP_FW_TX_SPK
] = { .file
= "tx-spk" },
216 [WM_ADSP_FW_RX_ANC
] = { .file
= "rx-anc" },
219 struct wm_coeff_ctl_ops
{
220 int (*xget
)(struct snd_kcontrol
*kcontrol
,
221 struct snd_ctl_elem_value
*ucontrol
);
222 int (*xput
)(struct snd_kcontrol
*kcontrol
,
223 struct snd_ctl_elem_value
*ucontrol
);
224 int (*xinfo
)(struct snd_kcontrol
*kcontrol
,
225 struct snd_ctl_elem_info
*uinfo
);
230 struct list_head ctl_list
;
231 struct regmap
*regmap
;
234 struct wm_coeff_ctl
{
236 struct snd_card
*card
;
237 struct wm_adsp_alg_region region
;
238 struct wm_coeff_ctl_ops ops
;
239 struct wm_adsp
*adsp
;
241 unsigned int enabled
:1;
242 struct list_head list
;
246 struct snd_kcontrol
*kcontrol
;
249 static int wm_adsp_fw_get(struct snd_kcontrol
*kcontrol
,
250 struct snd_ctl_elem_value
*ucontrol
)
252 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
253 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
254 struct wm_adsp
*adsp
= snd_soc_codec_get_drvdata(codec
);
256 ucontrol
->value
.integer
.value
[0] = adsp
[e
->shift_l
].fw
;
261 static int wm_adsp_fw_put(struct snd_kcontrol
*kcontrol
,
262 struct snd_ctl_elem_value
*ucontrol
)
264 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
265 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
266 struct wm_adsp
*adsp
= snd_soc_codec_get_drvdata(codec
);
268 if (ucontrol
->value
.integer
.value
[0] == adsp
[e
->shift_l
].fw
)
271 if (ucontrol
->value
.integer
.value
[0] >= WM_ADSP_NUM_FW
)
274 if (adsp
[e
->shift_l
].running
)
277 adsp
[e
->shift_l
].fw
= ucontrol
->value
.integer
.value
[0];
282 static const struct soc_enum wm_adsp_fw_enum
[] = {
283 SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(wm_adsp_fw_text
), wm_adsp_fw_text
),
284 SOC_ENUM_SINGLE(0, 1, ARRAY_SIZE(wm_adsp_fw_text
), wm_adsp_fw_text
),
285 SOC_ENUM_SINGLE(0, 2, ARRAY_SIZE(wm_adsp_fw_text
), wm_adsp_fw_text
),
286 SOC_ENUM_SINGLE(0, 3, ARRAY_SIZE(wm_adsp_fw_text
), wm_adsp_fw_text
),
289 const struct snd_kcontrol_new wm_adsp1_fw_controls
[] = {
290 SOC_ENUM_EXT("DSP1 Firmware", wm_adsp_fw_enum
[0],
291 wm_adsp_fw_get
, wm_adsp_fw_put
),
292 SOC_ENUM_EXT("DSP2 Firmware", wm_adsp_fw_enum
[1],
293 wm_adsp_fw_get
, wm_adsp_fw_put
),
294 SOC_ENUM_EXT("DSP3 Firmware", wm_adsp_fw_enum
[2],
295 wm_adsp_fw_get
, wm_adsp_fw_put
),
297 EXPORT_SYMBOL_GPL(wm_adsp1_fw_controls
);
299 #if IS_ENABLED(CONFIG_SND_SOC_ARIZONA)
300 static const struct soc_enum wm_adsp2_rate_enum
[] = {
301 SOC_VALUE_ENUM_SINGLE(ARIZONA_DSP1_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_DSP2_CONTROL_1
,
306 ARIZONA_DSP1_RATE_SHIFT
, 0xf,
307 ARIZONA_RATE_ENUM_SIZE
,
308 arizona_rate_text
, arizona_rate_val
),
309 SOC_VALUE_ENUM_SINGLE(ARIZONA_DSP3_CONTROL_1
,
310 ARIZONA_DSP1_RATE_SHIFT
, 0xf,
311 ARIZONA_RATE_ENUM_SIZE
,
312 arizona_rate_text
, arizona_rate_val
),
313 SOC_VALUE_ENUM_SINGLE(ARIZONA_DSP4_CONTROL_1
,
314 ARIZONA_DSP1_RATE_SHIFT
, 0xf,
315 ARIZONA_RATE_ENUM_SIZE
,
316 arizona_rate_text
, arizona_rate_val
),
319 const struct snd_kcontrol_new wm_adsp2_fw_controls
[] = {
320 SOC_ENUM_EXT("DSP1 Firmware", wm_adsp_fw_enum
[0],
321 wm_adsp_fw_get
, wm_adsp_fw_put
),
322 SOC_ENUM("DSP1 Rate", wm_adsp2_rate_enum
[0]),
323 SOC_ENUM_EXT("DSP2 Firmware", wm_adsp_fw_enum
[1],
324 wm_adsp_fw_get
, wm_adsp_fw_put
),
325 SOC_ENUM("DSP2 Rate", wm_adsp2_rate_enum
[1]),
326 SOC_ENUM_EXT("DSP3 Firmware", wm_adsp_fw_enum
[2],
327 wm_adsp_fw_get
, wm_adsp_fw_put
),
328 SOC_ENUM("DSP3 Rate", wm_adsp2_rate_enum
[2]),
329 SOC_ENUM_EXT("DSP4 Firmware", wm_adsp_fw_enum
[3],
330 wm_adsp_fw_get
, wm_adsp_fw_put
),
331 SOC_ENUM("DSP4 Rate", wm_adsp2_rate_enum
[3]),
333 EXPORT_SYMBOL_GPL(wm_adsp2_fw_controls
);
336 static struct wm_adsp_region
const *wm_adsp_find_region(struct wm_adsp
*dsp
,
341 for (i
= 0; i
< dsp
->num_mems
; i
++)
342 if (dsp
->mem
[i
].type
== type
)
348 static unsigned int wm_adsp_region_to_reg(struct wm_adsp_region
const *region
,
351 switch (region
->type
) {
353 return region
->base
+ (offset
* 3);
355 return region
->base
+ (offset
* 2);
357 return region
->base
+ (offset
* 2);
359 return region
->base
+ (offset
* 2);
361 return region
->base
+ (offset
* 2);
363 WARN_ON(NULL
!= "Unknown memory region type");
368 static int wm_coeff_info(struct snd_kcontrol
*kcontrol
,
369 struct snd_ctl_elem_info
*uinfo
)
371 struct wm_coeff_ctl
*ctl
= (struct wm_coeff_ctl
*)kcontrol
->private_value
;
373 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BYTES
;
374 uinfo
->count
= ctl
->len
;
378 static int wm_coeff_write_control(struct snd_kcontrol
*kcontrol
,
379 const void *buf
, size_t len
)
381 struct wm_coeff
*wm_coeff
= snd_kcontrol_chip(kcontrol
);
382 struct wm_coeff_ctl
*ctl
= (struct wm_coeff_ctl
*)kcontrol
->private_value
;
383 struct wm_adsp_alg_region
*region
= &ctl
->region
;
384 const struct wm_adsp_region
*mem
;
385 struct wm_adsp
*adsp
= ctl
->adsp
;
390 mem
= wm_adsp_find_region(adsp
, region
->type
);
392 adsp_err(adsp
, "No base for region %x\n",
397 reg
= ctl
->region
.base
;
398 reg
= wm_adsp_region_to_reg(mem
, reg
);
400 scratch
= kmemdup(buf
, ctl
->len
, GFP_KERNEL
| GFP_DMA
);
404 ret
= regmap_raw_write(wm_coeff
->regmap
, reg
, scratch
,
407 adsp_err(adsp
, "Failed to write %zu bytes to %x\n",
418 static int wm_coeff_put(struct snd_kcontrol
*kcontrol
,
419 struct snd_ctl_elem_value
*ucontrol
)
421 struct wm_coeff_ctl
*ctl
= (struct wm_coeff_ctl
*)kcontrol
->private_value
;
422 char *p
= ucontrol
->value
.bytes
.data
;
424 memcpy(ctl
->cache
, p
, ctl
->len
);
431 return wm_coeff_write_control(kcontrol
, p
, ctl
->len
);
434 static int wm_coeff_read_control(struct snd_kcontrol
*kcontrol
,
435 void *buf
, size_t len
)
437 struct wm_coeff
*wm_coeff
= snd_kcontrol_chip(kcontrol
);
438 struct wm_coeff_ctl
*ctl
= (struct wm_coeff_ctl
*)kcontrol
->private_value
;
439 struct wm_adsp_alg_region
*region
= &ctl
->region
;
440 const struct wm_adsp_region
*mem
;
441 struct wm_adsp
*adsp
= ctl
->adsp
;
446 mem
= wm_adsp_find_region(adsp
, region
->type
);
448 adsp_err(adsp
, "No base for region %x\n",
453 reg
= ctl
->region
.base
;
454 reg
= wm_adsp_region_to_reg(mem
, reg
);
456 scratch
= kmalloc(ctl
->len
, GFP_KERNEL
| GFP_DMA
);
460 ret
= regmap_raw_read(wm_coeff
->regmap
, reg
, scratch
, ctl
->len
);
462 adsp_err(adsp
, "Failed to read %zu bytes from %x\n",
468 memcpy(buf
, scratch
, ctl
->len
);
474 static int wm_coeff_get(struct snd_kcontrol
*kcontrol
,
475 struct snd_ctl_elem_value
*ucontrol
)
477 struct wm_coeff_ctl
*ctl
= (struct wm_coeff_ctl
*)kcontrol
->private_value
;
478 char *p
= ucontrol
->value
.bytes
.data
;
480 memcpy(p
, ctl
->cache
, ctl
->len
);
484 static int wm_coeff_add_kcontrol(struct wm_coeff
*wm_coeff
,
485 struct wm_coeff_ctl
*ctl
,
486 const struct snd_kcontrol_new
*kctl
)
489 struct snd_kcontrol
*kcontrol
;
491 kcontrol
= snd_ctl_new1(kctl
, wm_coeff
);
492 ret
= snd_ctl_add(ctl
->card
, kcontrol
);
494 dev_err(wm_coeff
->dev
, "Failed to add %s: %d\n",
498 ctl
->kcontrol
= kcontrol
;
502 struct wmfw_ctl_work
{
503 struct wm_coeff
*wm_coeff
;
504 struct wm_coeff_ctl
*ctl
;
505 struct work_struct work
;
508 static int wmfw_add_ctl(struct wm_coeff
*wm_coeff
,
509 struct wm_coeff_ctl
*ctl
)
511 struct snd_kcontrol_new
*kcontrol
;
514 if (!wm_coeff
|| !ctl
|| !ctl
->name
|| !ctl
->card
)
517 kcontrol
= kzalloc(sizeof(*kcontrol
), GFP_KERNEL
);
520 kcontrol
->iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
522 kcontrol
->name
= ctl
->name
;
523 kcontrol
->info
= wm_coeff_info
;
524 kcontrol
->get
= wm_coeff_get
;
525 kcontrol
->put
= wm_coeff_put
;
526 kcontrol
->private_value
= (unsigned long)ctl
;
528 ret
= wm_coeff_add_kcontrol(wm_coeff
,
535 list_add(&ctl
->list
, &wm_coeff
->ctl_list
);
543 static int wm_adsp_load(struct wm_adsp
*dsp
)
546 const struct firmware
*firmware
;
547 struct regmap
*regmap
= dsp
->regmap
;
548 unsigned int pos
= 0;
549 const struct wmfw_header
*header
;
550 const struct wmfw_adsp1_sizes
*adsp1_sizes
;
551 const struct wmfw_adsp2_sizes
*adsp2_sizes
;
552 const struct wmfw_footer
*footer
;
553 const struct wmfw_region
*region
;
554 const struct wm_adsp_region
*mem
;
555 const char *region_name
;
557 struct wm_adsp_buf
*buf
;
560 int ret
, offset
, type
, sizes
;
562 file
= kzalloc(PAGE_SIZE
, GFP_KERNEL
);
566 snprintf(file
, PAGE_SIZE
, "%s-dsp%d-%s.wmfw", dsp
->part
, dsp
->num
,
567 wm_adsp_fw
[dsp
->fw
].file
);
568 file
[PAGE_SIZE
- 1] = '\0';
570 ret
= request_firmware(&firmware
, file
, dsp
->dev
);
572 adsp_err(dsp
, "Failed to request '%s'\n", file
);
577 pos
= sizeof(*header
) + sizeof(*adsp1_sizes
) + sizeof(*footer
);
578 if (pos
>= firmware
->size
) {
579 adsp_err(dsp
, "%s: file too short, %zu bytes\n",
580 file
, firmware
->size
);
584 header
= (void*)&firmware
->data
[0];
586 if (memcmp(&header
->magic
[0], "WMFW", 4) != 0) {
587 adsp_err(dsp
, "%s: invalid magic\n", file
);
591 if (header
->ver
!= 0) {
592 adsp_err(dsp
, "%s: unknown file format %d\n",
597 if (header
->core
!= dsp
->type
) {
598 adsp_err(dsp
, "%s: invalid core %d != %d\n",
599 file
, header
->core
, dsp
->type
);
605 pos
= sizeof(*header
) + sizeof(*adsp1_sizes
) + sizeof(*footer
);
606 adsp1_sizes
= (void *)&(header
[1]);
607 footer
= (void *)&(adsp1_sizes
[1]);
608 sizes
= sizeof(*adsp1_sizes
);
610 adsp_dbg(dsp
, "%s: %d DM, %d PM, %d ZM\n",
611 file
, le32_to_cpu(adsp1_sizes
->dm
),
612 le32_to_cpu(adsp1_sizes
->pm
),
613 le32_to_cpu(adsp1_sizes
->zm
));
617 pos
= sizeof(*header
) + sizeof(*adsp2_sizes
) + sizeof(*footer
);
618 adsp2_sizes
= (void *)&(header
[1]);
619 footer
= (void *)&(adsp2_sizes
[1]);
620 sizes
= sizeof(*adsp2_sizes
);
622 adsp_dbg(dsp
, "%s: %d XM, %d YM %d PM, %d ZM\n",
623 file
, le32_to_cpu(adsp2_sizes
->xm
),
624 le32_to_cpu(adsp2_sizes
->ym
),
625 le32_to_cpu(adsp2_sizes
->pm
),
626 le32_to_cpu(adsp2_sizes
->zm
));
630 BUG_ON(NULL
== "Unknown DSP type");
634 if (le32_to_cpu(header
->len
) != sizeof(*header
) +
635 sizes
+ sizeof(*footer
)) {
636 adsp_err(dsp
, "%s: unexpected header length %d\n",
637 file
, le32_to_cpu(header
->len
));
641 adsp_dbg(dsp
, "%s: timestamp %llu\n", file
,
642 le64_to_cpu(footer
->timestamp
));
644 while (pos
< firmware
->size
&&
645 pos
- firmware
->size
> sizeof(*region
)) {
646 region
= (void *)&(firmware
->data
[pos
]);
647 region_name
= "Unknown";
650 offset
= le32_to_cpu(region
->offset
) & 0xffffff;
651 type
= be32_to_cpu(region
->type
) & 0xff;
652 mem
= wm_adsp_find_region(dsp
, type
);
656 region_name
= "Firmware name";
657 text
= kzalloc(le32_to_cpu(region
->len
) + 1,
661 region_name
= "Information";
662 text
= kzalloc(le32_to_cpu(region
->len
) + 1,
666 region_name
= "Absolute";
672 reg
= wm_adsp_region_to_reg(mem
, offset
);
677 reg
= wm_adsp_region_to_reg(mem
, offset
);
682 reg
= wm_adsp_region_to_reg(mem
, offset
);
687 reg
= wm_adsp_region_to_reg(mem
, offset
);
692 reg
= wm_adsp_region_to_reg(mem
, offset
);
696 "%s.%d: Unknown region type %x at %d(%x)\n",
697 file
, regions
, type
, pos
, pos
);
701 adsp_dbg(dsp
, "%s.%d: %d bytes at %d in %s\n", file
,
702 regions
, le32_to_cpu(region
->len
), offset
,
706 memcpy(text
, region
->data
, le32_to_cpu(region
->len
));
707 adsp_info(dsp
, "%s: %s\n", file
, text
);
712 buf
= wm_adsp_buf_alloc(region
->data
,
713 le32_to_cpu(region
->len
),
716 adsp_err(dsp
, "Out of memory\n");
720 ret
= regmap_raw_write_async(regmap
, reg
, buf
->buf
,
721 le32_to_cpu(region
->len
));
724 "%s.%d: Failed to write %d bytes at %d in %s: %d\n",
726 le32_to_cpu(region
->len
), offset
,
732 pos
+= le32_to_cpu(region
->len
) + sizeof(*region
);
736 ret
= regmap_async_complete(regmap
);
738 adsp_err(dsp
, "Failed to complete async write: %d\n", ret
);
742 if (pos
> firmware
->size
)
743 adsp_warn(dsp
, "%s.%d: %zu bytes at end of file\n",
744 file
, regions
, pos
- firmware
->size
);
747 regmap_async_complete(regmap
);
748 wm_adsp_buf_free(&buf_list
);
749 release_firmware(firmware
);
756 static int wm_coeff_init_control_caches(struct wm_coeff
*wm_coeff
)
758 struct wm_coeff_ctl
*ctl
;
761 list_for_each_entry(ctl
, &wm_coeff
->ctl_list
,
763 if (!ctl
->enabled
|| ctl
->set
)
765 ret
= wm_coeff_read_control(ctl
->kcontrol
,
775 static int wm_coeff_sync_controls(struct wm_coeff
*wm_coeff
)
777 struct wm_coeff_ctl
*ctl
;
780 list_for_each_entry(ctl
, &wm_coeff
->ctl_list
,
785 ret
= wm_coeff_write_control(ctl
->kcontrol
,
796 static void wm_adsp_ctl_work(struct work_struct
*work
)
798 struct wmfw_ctl_work
*ctl_work
= container_of(work
,
799 struct wmfw_ctl_work
,
802 wmfw_add_ctl(ctl_work
->wm_coeff
, ctl_work
->ctl
);
806 static int wm_adsp_create_control(struct snd_soc_codec
*codec
,
807 const struct wm_adsp_alg_region
*region
)
810 struct wm_adsp
*dsp
= snd_soc_codec_get_drvdata(codec
);
811 struct wm_coeff_ctl
*ctl
;
812 struct wmfw_ctl_work
*ctl_work
;
817 name
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
821 switch (region
->type
) {
842 snprintf(name
, PAGE_SIZE
, "DSP%d %s %x",
843 dsp
->num
, region_name
, region
->alg
);
845 list_for_each_entry(ctl
, &dsp
->wm_coeff
->ctl_list
,
847 if (!strcmp(ctl
->name
, name
)) {
854 ctl
= kzalloc(sizeof(*ctl
), GFP_KERNEL
);
859 ctl
->region
= *region
;
860 ctl
->name
= kmemdup(name
, strlen(name
) + 1, GFP_KERNEL
);
867 ctl
->ops
.xget
= wm_coeff_get
;
868 ctl
->ops
.xput
= wm_coeff_put
;
869 ctl
->card
= codec
->card
->snd_card
;
872 ctl
->len
= region
->len
;
873 ctl
->cache
= kzalloc(ctl
->len
, GFP_KERNEL
);
879 ctl_work
= kzalloc(sizeof(*ctl_work
), GFP_KERNEL
);
885 ctl_work
->wm_coeff
= dsp
->wm_coeff
;
887 INIT_WORK(&ctl_work
->work
, wm_adsp_ctl_work
);
888 schedule_work(&ctl_work
->work
);
906 static int wm_adsp_setup_algs(struct wm_adsp
*dsp
, struct snd_soc_codec
*codec
)
908 struct regmap
*regmap
= dsp
->regmap
;
909 struct wmfw_adsp1_id_hdr adsp1_id
;
910 struct wmfw_adsp2_id_hdr adsp2_id
;
911 struct wmfw_adsp1_alg_hdr
*adsp1_alg
;
912 struct wmfw_adsp2_alg_hdr
*adsp2_alg
;
914 struct wm_adsp_alg_region
*region
;
915 const struct wm_adsp_region
*mem
;
916 unsigned int pos
, term
;
917 size_t algs
, buf_size
;
923 mem
= wm_adsp_find_region(dsp
, WMFW_ADSP1_DM
);
926 mem
= wm_adsp_find_region(dsp
, WMFW_ADSP2_XM
);
940 ret
= regmap_raw_read(regmap
, mem
->base
, &adsp1_id
,
943 adsp_err(dsp
, "Failed to read algorithm info: %d\n",
949 buf_size
= sizeof(adsp1_id
);
951 algs
= be32_to_cpu(adsp1_id
.algs
);
952 dsp
->fw_id
= be32_to_cpu(adsp1_id
.fw
.id
);
953 adsp_info(dsp
, "Firmware: %x v%d.%d.%d, %zu algorithms\n",
955 (be32_to_cpu(adsp1_id
.fw
.ver
) & 0xff0000) >> 16,
956 (be32_to_cpu(adsp1_id
.fw
.ver
) & 0xff00) >> 8,
957 be32_to_cpu(adsp1_id
.fw
.ver
) & 0xff,
960 region
= kzalloc(sizeof(*region
), GFP_KERNEL
);
963 region
->type
= WMFW_ADSP1_ZM
;
964 region
->alg
= be32_to_cpu(adsp1_id
.fw
.id
);
965 region
->base
= be32_to_cpu(adsp1_id
.zm
);
966 list_add_tail(®ion
->list
, &dsp
->alg_regions
);
968 region
= kzalloc(sizeof(*region
), GFP_KERNEL
);
971 region
->type
= WMFW_ADSP1_DM
;
972 region
->alg
= be32_to_cpu(adsp1_id
.fw
.id
);
973 region
->base
= be32_to_cpu(adsp1_id
.dm
);
974 list_add_tail(®ion
->list
, &dsp
->alg_regions
);
976 pos
= sizeof(adsp1_id
) / 2;
977 term
= pos
+ ((sizeof(*adsp1_alg
) * algs
) / 2);
981 ret
= regmap_raw_read(regmap
, mem
->base
, &adsp2_id
,
984 adsp_err(dsp
, "Failed to read algorithm info: %d\n",
990 buf_size
= sizeof(adsp2_id
);
992 algs
= be32_to_cpu(adsp2_id
.algs
);
993 dsp
->fw_id
= be32_to_cpu(adsp2_id
.fw
.id
);
994 adsp_info(dsp
, "Firmware: %x v%d.%d.%d, %zu algorithms\n",
996 (be32_to_cpu(adsp2_id
.fw
.ver
) & 0xff0000) >> 16,
997 (be32_to_cpu(adsp2_id
.fw
.ver
) & 0xff00) >> 8,
998 be32_to_cpu(adsp2_id
.fw
.ver
) & 0xff,
1001 region
= kzalloc(sizeof(*region
), GFP_KERNEL
);
1004 region
->type
= WMFW_ADSP2_XM
;
1005 region
->alg
= be32_to_cpu(adsp2_id
.fw
.id
);
1006 region
->base
= be32_to_cpu(adsp2_id
.xm
);
1007 list_add_tail(®ion
->list
, &dsp
->alg_regions
);
1009 region
= kzalloc(sizeof(*region
), GFP_KERNEL
);
1012 region
->type
= WMFW_ADSP2_YM
;
1013 region
->alg
= be32_to_cpu(adsp2_id
.fw
.id
);
1014 region
->base
= be32_to_cpu(adsp2_id
.ym
);
1015 list_add_tail(®ion
->list
, &dsp
->alg_regions
);
1017 region
= kzalloc(sizeof(*region
), GFP_KERNEL
);
1020 region
->type
= WMFW_ADSP2_ZM
;
1021 region
->alg
= be32_to_cpu(adsp2_id
.fw
.id
);
1022 region
->base
= be32_to_cpu(adsp2_id
.zm
);
1023 list_add_tail(®ion
->list
, &dsp
->alg_regions
);
1025 pos
= sizeof(adsp2_id
) / 2;
1026 term
= pos
+ ((sizeof(*adsp2_alg
) * algs
) / 2);
1030 BUG_ON(NULL
== "Unknown DSP type");
1035 adsp_err(dsp
, "No algorithms\n");
1040 adsp_err(dsp
, "Algorithm count %zx excessive\n", algs
);
1041 print_hex_dump_bytes(dev_name(dsp
->dev
), DUMP_PREFIX_OFFSET
,
1046 /* Read the terminator first to validate the length */
1047 ret
= regmap_raw_read(regmap
, mem
->base
+ term
, &val
, sizeof(val
));
1049 adsp_err(dsp
, "Failed to read algorithm list end: %d\n",
1054 if (be32_to_cpu(val
) != 0xbedead)
1055 adsp_warn(dsp
, "Algorithm list end %x 0x%x != 0xbeadead\n",
1056 term
, be32_to_cpu(val
));
1058 alg
= kzalloc((term
- pos
) * 2, GFP_KERNEL
| GFP_DMA
);
1062 ret
= regmap_raw_read(regmap
, mem
->base
+ pos
, alg
, (term
- pos
) * 2);
1064 adsp_err(dsp
, "Failed to read algorithm list: %d\n",
1072 for (i
= 0; i
< algs
; i
++) {
1073 switch (dsp
->type
) {
1075 adsp_info(dsp
, "%d: ID %x v%d.%d.%d DM@%x ZM@%x\n",
1076 i
, be32_to_cpu(adsp1_alg
[i
].alg
.id
),
1077 (be32_to_cpu(adsp1_alg
[i
].alg
.ver
) & 0xff0000) >> 16,
1078 (be32_to_cpu(adsp1_alg
[i
].alg
.ver
) & 0xff00) >> 8,
1079 be32_to_cpu(adsp1_alg
[i
].alg
.ver
) & 0xff,
1080 be32_to_cpu(adsp1_alg
[i
].dm
),
1081 be32_to_cpu(adsp1_alg
[i
].zm
));
1083 region
= kzalloc(sizeof(*region
), GFP_KERNEL
);
1086 region
->type
= WMFW_ADSP1_DM
;
1087 region
->alg
= be32_to_cpu(adsp1_alg
[i
].alg
.id
);
1088 region
->base
= be32_to_cpu(adsp1_alg
[i
].dm
);
1090 list_add_tail(®ion
->list
, &dsp
->alg_regions
);
1092 region
->len
= be32_to_cpu(adsp1_alg
[i
+ 1].dm
);
1093 region
->len
-= be32_to_cpu(adsp1_alg
[i
].dm
);
1094 wm_adsp_create_control(codec
, region
);
1096 adsp_warn(dsp
, "Missing length info for region DM with ID %x\n",
1097 be32_to_cpu(adsp1_alg
[i
].alg
.id
));
1100 region
= kzalloc(sizeof(*region
), GFP_KERNEL
);
1103 region
->type
= WMFW_ADSP1_ZM
;
1104 region
->alg
= be32_to_cpu(adsp1_alg
[i
].alg
.id
);
1105 region
->base
= be32_to_cpu(adsp1_alg
[i
].zm
);
1107 list_add_tail(®ion
->list
, &dsp
->alg_regions
);
1109 region
->len
= be32_to_cpu(adsp1_alg
[i
+ 1].zm
);
1110 region
->len
-= be32_to_cpu(adsp1_alg
[i
].zm
);
1111 wm_adsp_create_control(codec
, region
);
1113 adsp_warn(dsp
, "Missing length info for region ZM with ID %x\n",
1114 be32_to_cpu(adsp1_alg
[i
].alg
.id
));
1120 "%d: ID %x v%d.%d.%d XM@%x YM@%x ZM@%x\n",
1121 i
, be32_to_cpu(adsp2_alg
[i
].alg
.id
),
1122 (be32_to_cpu(adsp2_alg
[i
].alg
.ver
) & 0xff0000) >> 16,
1123 (be32_to_cpu(adsp2_alg
[i
].alg
.ver
) & 0xff00) >> 8,
1124 be32_to_cpu(adsp2_alg
[i
].alg
.ver
) & 0xff,
1125 be32_to_cpu(adsp2_alg
[i
].xm
),
1126 be32_to_cpu(adsp2_alg
[i
].ym
),
1127 be32_to_cpu(adsp2_alg
[i
].zm
));
1129 region
= kzalloc(sizeof(*region
), GFP_KERNEL
);
1132 region
->type
= WMFW_ADSP2_XM
;
1133 region
->alg
= be32_to_cpu(adsp2_alg
[i
].alg
.id
);
1134 region
->base
= be32_to_cpu(adsp2_alg
[i
].xm
);
1136 list_add_tail(®ion
->list
, &dsp
->alg_regions
);
1138 region
->len
= be32_to_cpu(adsp2_alg
[i
+ 1].xm
);
1139 region
->len
-= be32_to_cpu(adsp2_alg
[i
].xm
);
1140 wm_adsp_create_control(codec
, region
);
1142 adsp_warn(dsp
, "Missing length info for region XM with ID %x\n",
1143 be32_to_cpu(adsp2_alg
[i
].alg
.id
));
1146 region
= kzalloc(sizeof(*region
), GFP_KERNEL
);
1149 region
->type
= WMFW_ADSP2_YM
;
1150 region
->alg
= be32_to_cpu(adsp2_alg
[i
].alg
.id
);
1151 region
->base
= be32_to_cpu(adsp2_alg
[i
].ym
);
1153 list_add_tail(®ion
->list
, &dsp
->alg_regions
);
1155 region
->len
= be32_to_cpu(adsp2_alg
[i
+ 1].ym
);
1156 region
->len
-= be32_to_cpu(adsp2_alg
[i
].ym
);
1157 wm_adsp_create_control(codec
, region
);
1159 adsp_warn(dsp
, "Missing length info for region YM with ID %x\n",
1160 be32_to_cpu(adsp2_alg
[i
].alg
.id
));
1163 region
= kzalloc(sizeof(*region
), GFP_KERNEL
);
1166 region
->type
= WMFW_ADSP2_ZM
;
1167 region
->alg
= be32_to_cpu(adsp2_alg
[i
].alg
.id
);
1168 region
->base
= be32_to_cpu(adsp2_alg
[i
].zm
);
1170 list_add_tail(®ion
->list
, &dsp
->alg_regions
);
1172 region
->len
= be32_to_cpu(adsp2_alg
[i
+ 1].zm
);
1173 region
->len
-= be32_to_cpu(adsp2_alg
[i
].zm
);
1174 wm_adsp_create_control(codec
, region
);
1176 adsp_warn(dsp
, "Missing length info for region ZM with ID %x\n",
1177 be32_to_cpu(adsp2_alg
[i
].alg
.id
));
1188 static int wm_adsp_load_coeff(struct wm_adsp
*dsp
)
1190 LIST_HEAD(buf_list
);
1191 struct regmap
*regmap
= dsp
->regmap
;
1192 struct wmfw_coeff_hdr
*hdr
;
1193 struct wmfw_coeff_item
*blk
;
1194 const struct firmware
*firmware
;
1195 const struct wm_adsp_region
*mem
;
1196 struct wm_adsp_alg_region
*alg_region
;
1197 const char *region_name
;
1198 int ret
, pos
, blocks
, type
, offset
, reg
;
1200 struct wm_adsp_buf
*buf
;
1203 file
= kzalloc(PAGE_SIZE
, GFP_KERNEL
);
1207 snprintf(file
, PAGE_SIZE
, "%s-dsp%d-%s.bin", dsp
->part
, dsp
->num
,
1208 wm_adsp_fw
[dsp
->fw
].file
);
1209 file
[PAGE_SIZE
- 1] = '\0';
1211 ret
= request_firmware(&firmware
, file
, dsp
->dev
);
1213 adsp_warn(dsp
, "Failed to request '%s'\n", file
);
1219 if (sizeof(*hdr
) >= firmware
->size
) {
1220 adsp_err(dsp
, "%s: file too short, %zu bytes\n",
1221 file
, firmware
->size
);
1225 hdr
= (void*)&firmware
->data
[0];
1226 if (memcmp(hdr
->magic
, "WMDR", 4) != 0) {
1227 adsp_err(dsp
, "%s: invalid magic\n", file
);
1231 switch (be32_to_cpu(hdr
->rev
) & 0xff) {
1235 adsp_err(dsp
, "%s: Unsupported coefficient file format %d\n",
1236 file
, be32_to_cpu(hdr
->rev
) & 0xff);
1241 adsp_dbg(dsp
, "%s: v%d.%d.%d\n", file
,
1242 (le32_to_cpu(hdr
->ver
) >> 16) & 0xff,
1243 (le32_to_cpu(hdr
->ver
) >> 8) & 0xff,
1244 le32_to_cpu(hdr
->ver
) & 0xff);
1246 pos
= le32_to_cpu(hdr
->len
);
1249 while (pos
< firmware
->size
&&
1250 pos
- firmware
->size
> sizeof(*blk
)) {
1251 blk
= (void*)(&firmware
->data
[pos
]);
1253 type
= le16_to_cpu(blk
->type
);
1254 offset
= le16_to_cpu(blk
->offset
);
1256 adsp_dbg(dsp
, "%s.%d: %x v%d.%d.%d\n",
1257 file
, blocks
, le32_to_cpu(blk
->id
),
1258 (le32_to_cpu(blk
->ver
) >> 16) & 0xff,
1259 (le32_to_cpu(blk
->ver
) >> 8) & 0xff,
1260 le32_to_cpu(blk
->ver
) & 0xff);
1261 adsp_dbg(dsp
, "%s.%d: %d bytes at 0x%x in %x\n",
1262 file
, blocks
, le32_to_cpu(blk
->len
), offset
, type
);
1265 region_name
= "Unknown";
1267 case (WMFW_NAME_TEXT
<< 8):
1268 case (WMFW_INFO_TEXT
<< 8):
1270 case (WMFW_ABSOLUTE
<< 8):
1272 * Old files may use this for global
1275 if (le32_to_cpu(blk
->id
) == dsp
->fw_id
&&
1277 region_name
= "global coefficients";
1278 mem
= wm_adsp_find_region(dsp
, type
);
1280 adsp_err(dsp
, "No ZM\n");
1283 reg
= wm_adsp_region_to_reg(mem
, 0);
1286 region_name
= "register";
1295 adsp_dbg(dsp
, "%s.%d: %d bytes in %x for %x\n",
1296 file
, blocks
, le32_to_cpu(blk
->len
),
1297 type
, le32_to_cpu(blk
->id
));
1299 mem
= wm_adsp_find_region(dsp
, type
);
1301 adsp_err(dsp
, "No base for region %x\n", type
);
1306 list_for_each_entry(alg_region
,
1307 &dsp
->alg_regions
, list
) {
1308 if (le32_to_cpu(blk
->id
) == alg_region
->alg
&&
1309 type
== alg_region
->type
) {
1310 reg
= alg_region
->base
;
1311 reg
= wm_adsp_region_to_reg(mem
,
1318 adsp_err(dsp
, "No %x for algorithm %x\n",
1319 type
, le32_to_cpu(blk
->id
));
1323 adsp_err(dsp
, "%s.%d: Unknown region type %x at %d\n",
1324 file
, blocks
, type
, pos
);
1329 buf
= wm_adsp_buf_alloc(blk
->data
,
1330 le32_to_cpu(blk
->len
),
1333 adsp_err(dsp
, "Out of memory\n");
1338 adsp_dbg(dsp
, "%s.%d: Writing %d bytes at %x\n",
1339 file
, blocks
, le32_to_cpu(blk
->len
),
1341 ret
= regmap_raw_write_async(regmap
, reg
, buf
->buf
,
1342 le32_to_cpu(blk
->len
));
1345 "%s.%d: Failed to write to %x in %s\n",
1346 file
, blocks
, reg
, region_name
);
1350 tmp
= le32_to_cpu(blk
->len
) % 4;
1352 pos
+= le32_to_cpu(blk
->len
) + (4 - tmp
) + sizeof(*blk
);
1354 pos
+= le32_to_cpu(blk
->len
) + sizeof(*blk
);
1359 ret
= regmap_async_complete(regmap
);
1361 adsp_err(dsp
, "Failed to complete async write: %d\n", ret
);
1363 if (pos
> firmware
->size
)
1364 adsp_warn(dsp
, "%s.%d: %zu bytes at end of file\n",
1365 file
, blocks
, pos
- firmware
->size
);
1368 release_firmware(firmware
);
1369 wm_adsp_buf_free(&buf_list
);
1375 int wm_adsp1_init(struct wm_adsp
*adsp
)
1377 INIT_LIST_HEAD(&adsp
->alg_regions
);
1381 EXPORT_SYMBOL_GPL(wm_adsp1_init
);
1383 int wm_adsp1_event(struct snd_soc_dapm_widget
*w
,
1384 struct snd_kcontrol
*kcontrol
,
1387 struct snd_soc_codec
*codec
= w
->codec
;
1388 struct wm_adsp
*dsps
= snd_soc_codec_get_drvdata(codec
);
1389 struct wm_adsp
*dsp
= &dsps
[w
->shift
];
1390 struct wm_coeff_ctl
*ctl
;
1395 case SND_SOC_DAPM_POST_PMU
:
1396 regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP1_CONTROL_30
,
1397 ADSP1_SYS_ENA
, ADSP1_SYS_ENA
);
1400 * For simplicity set the DSP clock rate to be the
1401 * SYSCLK rate rather than making it configurable.
1403 if(dsp
->sysclk_reg
) {
1404 ret
= regmap_read(dsp
->regmap
, dsp
->sysclk_reg
, &val
);
1406 adsp_err(dsp
, "Failed to read SYSCLK state: %d\n",
1411 val
= (val
& dsp
->sysclk_mask
)
1412 >> dsp
->sysclk_shift
;
1414 ret
= regmap_update_bits(dsp
->regmap
,
1415 dsp
->base
+ ADSP1_CONTROL_31
,
1416 ADSP1_CLK_SEL_MASK
, val
);
1418 adsp_err(dsp
, "Failed to set clock rate: %d\n",
1424 ret
= wm_adsp_load(dsp
);
1428 ret
= wm_adsp_setup_algs(dsp
, codec
);
1432 ret
= wm_adsp_load_coeff(dsp
);
1436 /* Initialize caches for enabled and unset controls */
1437 ret
= wm_coeff_init_control_caches(dsp
->wm_coeff
);
1441 /* Sync set controls */
1442 ret
= wm_coeff_sync_controls(dsp
->wm_coeff
);
1446 /* Start the core running */
1447 regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP1_CONTROL_30
,
1448 ADSP1_CORE_ENA
| ADSP1_START
,
1449 ADSP1_CORE_ENA
| ADSP1_START
);
1452 case SND_SOC_DAPM_PRE_PMD
:
1454 regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP1_CONTROL_30
,
1455 ADSP1_CORE_ENA
| ADSP1_START
, 0);
1457 regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP1_CONTROL_19
,
1458 ADSP1_WDMA_BUFFER_LENGTH_MASK
, 0);
1460 regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP1_CONTROL_30
,
1463 list_for_each_entry(ctl
, &dsp
->wm_coeff
->ctl_list
,
1476 regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP1_CONTROL_30
,
1480 EXPORT_SYMBOL_GPL(wm_adsp1_event
);
1482 static int wm_adsp2_ena(struct wm_adsp
*dsp
)
1487 ret
= regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP2_CONTROL
,
1488 ADSP2_SYS_ENA
, ADSP2_SYS_ENA
);
1492 /* Wait for the RAM to start, should be near instantaneous */
1495 ret
= regmap_read(dsp
->regmap
, dsp
->base
+ ADSP2_STATUS1
,
1499 } while (!(val
& ADSP2_RAM_RDY
) && ++count
< 10);
1501 if (!(val
& ADSP2_RAM_RDY
)) {
1502 adsp_err(dsp
, "Failed to start DSP RAM\n");
1506 adsp_dbg(dsp
, "RAM ready after %d polls\n", count
);
1507 adsp_info(dsp
, "RAM ready after %d polls\n", count
);
1512 int wm_adsp2_event(struct snd_soc_dapm_widget
*w
,
1513 struct snd_kcontrol
*kcontrol
, int event
)
1515 struct snd_soc_codec
*codec
= w
->codec
;
1516 struct wm_adsp
*dsps
= snd_soc_codec_get_drvdata(codec
);
1517 struct wm_adsp
*dsp
= &dsps
[w
->shift
];
1518 struct wm_adsp_alg_region
*alg_region
;
1519 struct wm_coeff_ctl
*ctl
;
1524 case SND_SOC_DAPM_POST_PMU
:
1526 * For simplicity set the DSP clock rate to be the
1527 * SYSCLK rate rather than making it configurable.
1529 ret
= regmap_read(dsp
->regmap
, ARIZONA_SYSTEM_CLOCK_1
, &val
);
1531 adsp_err(dsp
, "Failed to read SYSCLK state: %d\n",
1535 val
= (val
& ARIZONA_SYSCLK_FREQ_MASK
)
1536 >> ARIZONA_SYSCLK_FREQ_SHIFT
;
1538 ret
= regmap_update_bits(dsp
->regmap
,
1539 dsp
->base
+ ADSP2_CLOCKING
,
1540 ADSP2_CLK_SEL_MASK
, val
);
1542 adsp_err(dsp
, "Failed to set clock rate: %d\n",
1548 ret
= regmap_read(dsp
->regmap
,
1549 dsp
->base
+ ADSP2_CLOCKING
, &val
);
1552 "Failed to read clocking: %d\n", ret
);
1556 if ((val
& ADSP2_CLK_SEL_MASK
) >= 3) {
1557 ret
= regulator_enable(dsp
->dvfs
);
1560 "Failed to enable supply: %d\n",
1565 ret
= regulator_set_voltage(dsp
->dvfs
,
1570 "Failed to raise supply: %d\n",
1577 ret
= wm_adsp2_ena(dsp
);
1581 ret
= wm_adsp_load(dsp
);
1585 ret
= wm_adsp_setup_algs(dsp
, codec
);
1589 ret
= wm_adsp_load_coeff(dsp
);
1593 /* Initialize caches for enabled and unset controls */
1594 ret
= wm_coeff_init_control_caches(dsp
->wm_coeff
);
1598 /* Sync set controls */
1599 ret
= wm_coeff_sync_controls(dsp
->wm_coeff
);
1603 ret
= regmap_update_bits(dsp
->regmap
,
1604 dsp
->base
+ ADSP2_CONTROL
,
1605 ADSP2_CORE_ENA
| ADSP2_START
,
1606 ADSP2_CORE_ENA
| ADSP2_START
);
1610 dsp
->running
= true;
1613 case SND_SOC_DAPM_PRE_PMD
:
1614 dsp
->running
= false;
1616 regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP2_CONTROL
,
1617 ADSP2_SYS_ENA
| ADSP2_CORE_ENA
|
1620 /* Make sure DMAs are quiesced */
1621 regmap_write(dsp
->regmap
, dsp
->base
+ ADSP2_WDMA_CONFIG_1
, 0);
1622 regmap_write(dsp
->regmap
, dsp
->base
+ ADSP2_WDMA_CONFIG_2
, 0);
1623 regmap_write(dsp
->regmap
, dsp
->base
+ ADSP2_RDMA_CONFIG_1
, 0);
1626 ret
= regulator_set_voltage(dsp
->dvfs
, 1200000,
1630 "Failed to lower supply: %d\n",
1633 ret
= regulator_disable(dsp
->dvfs
);
1636 "Failed to enable supply: %d\n",
1640 list_for_each_entry(ctl
, &dsp
->wm_coeff
->ctl_list
,
1645 while (!list_empty(&dsp
->alg_regions
)) {
1646 alg_region
= list_first_entry(&dsp
->alg_regions
,
1647 struct wm_adsp_alg_region
,
1649 list_del(&alg_region
->list
);
1660 regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP2_CONTROL
,
1661 ADSP2_SYS_ENA
| ADSP2_CORE_ENA
| ADSP2_START
, 0);
1664 EXPORT_SYMBOL_GPL(wm_adsp2_event
);
1666 int wm_adsp2_init(struct wm_adsp
*adsp
, bool dvfs
)
1671 * Disable the DSP memory by default when in reset for a small
1674 ret
= regmap_update_bits(adsp
->regmap
, adsp
->base
+ ADSP2_CONTROL
,
1677 adsp_err(adsp
, "Failed to clear memory retention: %d\n", ret
);
1681 INIT_LIST_HEAD(&adsp
->alg_regions
);
1683 adsp
->wm_coeff
= kzalloc(sizeof(*adsp
->wm_coeff
),
1685 if (!adsp
->wm_coeff
)
1687 adsp
->wm_coeff
->regmap
= adsp
->regmap
;
1688 adsp
->wm_coeff
->dev
= adsp
->dev
;
1689 INIT_LIST_HEAD(&adsp
->wm_coeff
->ctl_list
);
1692 adsp
->dvfs
= devm_regulator_get(adsp
->dev
, "DCVDD");
1693 if (IS_ERR(adsp
->dvfs
)) {
1694 ret
= PTR_ERR(adsp
->dvfs
);
1695 dev_err(adsp
->dev
, "Failed to get DCVDD: %d\n", ret
);
1699 ret
= regulator_enable(adsp
->dvfs
);
1701 dev_err(adsp
->dev
, "Failed to enable DCVDD: %d\n",
1706 ret
= regulator_set_voltage(adsp
->dvfs
, 1200000, 1800000);
1708 dev_err(adsp
->dev
, "Failed to initialise DVFS: %d\n",
1713 ret
= regulator_disable(adsp
->dvfs
);
1715 dev_err(adsp
->dev
, "Failed to disable DCVDD: %d\n",
1724 kfree(adsp
->wm_coeff
);
1727 EXPORT_SYMBOL_GPL(wm_adsp2_init
);