blob: ca42445b649d4aea77a2ec51aee2eee081cc435a [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Mark Brownf701a2e2011-03-09 19:31:01 +00002/*
3 * wm8958-dsp2.c -- WM8958 DSP2 support
4 *
5 * Copyright 2011 Wolfson Microelectronics plc
6 *
7 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
Mark Brownf701a2e2011-03-09 19:31:01 +00008 */
9
10#include <linux/module.h>
11#include <linux/moduleparam.h>
12#include <linux/init.h>
13#include <linux/delay.h>
14#include <linux/pm.h>
15#include <linux/i2c.h>
16#include <linux/platform_device.h>
17#include <linux/slab.h>
18#include <sound/soc.h>
19#include <sound/initval.h>
20#include <sound/tlv.h>
21#include <trace/events/asoc.h>
22
23#include <linux/mfd/wm8994/core.h>
24#include <linux/mfd/wm8994/registers.h>
25#include <linux/mfd/wm8994/pdata.h>
26#include <linux/mfd/wm8994/gpio.h>
27
Ben Dooks (Codethink)3ad00f62019-10-16 13:01:49 +010028#include <asm/unaligned.h>
29
Mark Brownf701a2e2011-03-09 19:31:01 +000030#include "wm8994.h"
31
Mark Brownfbbf5922011-03-11 18:09:04 +000032#define WM_FW_BLOCK_INFO 0xff
33#define WM_FW_BLOCK_PM 0x00
34#define WM_FW_BLOCK_X 0x01
35#define WM_FW_BLOCK_Y 0x02
36#define WM_FW_BLOCK_Z 0x03
37#define WM_FW_BLOCK_I 0x06
38#define WM_FW_BLOCK_A 0x08
39#define WM_FW_BLOCK_C 0x0c
40
Kuninori Morimoto00a69412018-01-29 03:12:21 +000041static int wm8958_dsp2_fw(struct snd_soc_component *component, const char *name,
Mark Brownfbbf5922011-03-11 18:09:04 +000042 const struct firmware *fw, bool check)
43{
Kuninori Morimoto00a69412018-01-29 03:12:21 +000044 struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
Mark Brownfbbf5922011-03-11 18:09:04 +000045 u64 data64;
46 u32 data32;
47 const u8 *data;
48 char *str;
49 size_t block_len, len;
50 int ret = 0;
51
52 /* Suppress unneeded downloads */
53 if (wm8994->cur_fw == fw)
54 return 0;
55
56 if (fw->size < 32) {
Kuninori Morimoto00a69412018-01-29 03:12:21 +000057 dev_err(component->dev, "%s: firmware too short (%zd bytes)\n",
Mark Brown086834e2011-11-16 13:38:28 +000058 name, fw->size);
Mark Brownfbbf5922011-03-11 18:09:04 +000059 goto err;
60 }
61
62 if (memcmp(fw->data, "WMFW", 4) != 0) {
Ben Dooks (Codethink)3ad00f62019-10-16 13:01:49 +010063 data32 = get_unaligned_be32(fw->data);
Kuninori Morimoto00a69412018-01-29 03:12:21 +000064 dev_err(component->dev, "%s: firmware has bad file magic %08x\n",
Mark Brownfbbf5922011-03-11 18:09:04 +000065 name, data32);
66 goto err;
67 }
68
Ben Dooks (Codethink)3ad00f62019-10-16 13:01:49 +010069 len = get_unaligned_be32(fw->data + 4);
70 data32 = get_unaligned_be32(fw->data + 8);
Mark Brownfbbf5922011-03-11 18:09:04 +000071
Mark Brownfbbf5922011-03-11 18:09:04 +000072 if ((data32 >> 24) & 0xff) {
Kuninori Morimoto00a69412018-01-29 03:12:21 +000073 dev_err(component->dev, "%s: unsupported firmware version %d\n",
Mark Brownfbbf5922011-03-11 18:09:04 +000074 name, (data32 >> 24) & 0xff);
75 goto err;
76 }
77 if ((data32 & 0xffff) != 8958) {
Kuninori Morimoto00a69412018-01-29 03:12:21 +000078 dev_err(component->dev, "%s: unsupported target device %d\n",
Mark Brownfbbf5922011-03-11 18:09:04 +000079 name, data32 & 0xffff);
80 goto err;
81 }
82 if (((data32 >> 16) & 0xff) != 0xc) {
Kuninori Morimoto00a69412018-01-29 03:12:21 +000083 dev_err(component->dev, "%s: unsupported target core %d\n",
Mark Brownfbbf5922011-03-11 18:09:04 +000084 name, (data32 >> 16) & 0xff);
85 goto err;
86 }
87
88 if (check) {
Ben Dooks (Codethink)3ad00f62019-10-16 13:01:49 +010089 data64 = get_unaligned_be64(fw->data + 24);
90 dev_info(component->dev, "%s timestamp %llx\n", name, data64);
Mark Brownfbbf5922011-03-11 18:09:04 +000091 } else {
Kuninori Morimoto00a69412018-01-29 03:12:21 +000092 snd_soc_component_write(component, 0x102, 0x2);
93 snd_soc_component_write(component, 0x900, 0x2);
Mark Brownfbbf5922011-03-11 18:09:04 +000094 }
95
96 data = fw->data + len;
97 len = fw->size - len;
98 while (len) {
99 if (len < 12) {
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000100 dev_err(component->dev, "%s short data block of %zd\n",
Mark Brownfbbf5922011-03-11 18:09:04 +0000101 name, len);
102 goto err;
103 }
104
Ben Dooks (Codethink)3ad00f62019-10-16 13:01:49 +0100105 block_len = get_unaligned_be32(data + 4);
Mark Brownfbbf5922011-03-11 18:09:04 +0000106 if (block_len + 8 > len) {
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000107 dev_err(component->dev, "%zd byte block longer than file\n",
Mark Brownfbbf5922011-03-11 18:09:04 +0000108 block_len);
109 goto err;
110 }
111 if (block_len == 0) {
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000112 dev_err(component->dev, "Zero length block\n");
Mark Brownfbbf5922011-03-11 18:09:04 +0000113 goto err;
114 }
115
Ben Dooks (Codethink)3ad00f62019-10-16 13:01:49 +0100116 data32 = get_unaligned_be32(data);
Mark Brownfbbf5922011-03-11 18:09:04 +0000117
118 switch ((data32 >> 24) & 0xff) {
119 case WM_FW_BLOCK_INFO:
120 /* Informational text */
121 if (!check)
122 break;
123
124 str = kzalloc(block_len + 1, GFP_KERNEL);
125 if (str) {
126 memcpy(str, data + 8, block_len);
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000127 dev_info(component->dev, "%s: %s\n", name, str);
Mark Brownfbbf5922011-03-11 18:09:04 +0000128 kfree(str);
129 } else {
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000130 dev_err(component->dev, "Out of memory\n");
Mark Brownfbbf5922011-03-11 18:09:04 +0000131 }
132 break;
133 case WM_FW_BLOCK_PM:
134 case WM_FW_BLOCK_X:
135 case WM_FW_BLOCK_Y:
136 case WM_FW_BLOCK_Z:
137 case WM_FW_BLOCK_I:
138 case WM_FW_BLOCK_A:
139 case WM_FW_BLOCK_C:
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000140 dev_dbg(component->dev, "%s: %zd bytes of %x@%x\n", name,
Mark Brownfbbf5922011-03-11 18:09:04 +0000141 block_len, (data32 >> 24) & 0xff,
142 data32 & 0xffffff);
143
144 if (check)
145 break;
146
147 data32 &= 0xffffff;
148
Lars-Peter Clausen548da082014-02-22 18:30:13 +0100149 wm8994_bulk_write(wm8994->wm8994,
Mark Brownfbbf5922011-03-11 18:09:04 +0000150 data32 & 0xffffff,
151 block_len / 2,
152 (void *)(data + 8));
153
154 break;
155 default:
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000156 dev_warn(component->dev, "%s: unknown block type %d\n",
Mark Brownfbbf5922011-03-11 18:09:04 +0000157 name, (data32 >> 24) & 0xff);
158 break;
159 }
160
161 /* Round up to the next 32 bit word */
162 block_len += block_len % 4;
163
164 data += block_len + 8;
165 len -= block_len + 8;
166 }
167
168 if (!check) {
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000169 dev_dbg(component->dev, "%s: download done\n", name);
Mark Brownfbbf5922011-03-11 18:09:04 +0000170 wm8994->cur_fw = fw;
171 } else {
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000172 dev_info(component->dev, "%s: got firmware\n", name);
Mark Brownfbbf5922011-03-11 18:09:04 +0000173 }
174
175 goto ok;
176
177err:
178 ret = -EINVAL;
179ok:
180 if (!check) {
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000181 snd_soc_component_write(component, 0x900, 0x0);
182 snd_soc_component_write(component, 0x102, 0x0);
Mark Brownfbbf5922011-03-11 18:09:04 +0000183 }
184
185 return ret;
186}
187
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000188static void wm8958_dsp_start_mbc(struct snd_soc_component *component, int path)
Mark Brownf701a2e2011-03-09 19:31:01 +0000189{
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000190 struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
Mark Brownd9dd4ad2012-10-08 18:36:09 +0900191 struct wm8994 *control = wm8994->wm8994;
Mark Brownfbbf5922011-03-11 18:09:04 +0000192 int i;
193
194 /* If the DSP is already running then noop */
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000195 if (snd_soc_component_read32(component, WM8958_DSP2_PROGRAM) & WM8958_DSP2_ENA)
Mark Brownfbbf5922011-03-11 18:09:04 +0000196 return;
197
198 /* If we have MBC firmware download it */
199 if (wm8994->mbc)
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000200 wm8958_dsp2_fw(component, "MBC", wm8994->mbc, false);
Mark Brownfbbf5922011-03-11 18:09:04 +0000201
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000202 snd_soc_component_update_bits(component, WM8958_DSP2_PROGRAM,
Mark Brownfbbf5922011-03-11 18:09:04 +0000203 WM8958_DSP2_ENA, WM8958_DSP2_ENA);
204
205 /* If we've got user supplied MBC settings use them */
Mark Brownd9dd4ad2012-10-08 18:36:09 +0900206 if (control->pdata.num_mbc_cfgs) {
Mark Brownfbbf5922011-03-11 18:09:04 +0000207 struct wm8958_mbc_cfg *cfg
Mark Brownd9dd4ad2012-10-08 18:36:09 +0900208 = &control->pdata.mbc_cfgs[wm8994->mbc_cfg];
Mark Brownfbbf5922011-03-11 18:09:04 +0000209
210 for (i = 0; i < ARRAY_SIZE(cfg->coeff_regs); i++)
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000211 snd_soc_component_write(component, i + WM8958_MBC_BAND_1_K_1,
Mark Brownfbbf5922011-03-11 18:09:04 +0000212 cfg->coeff_regs[i]);
213
214 for (i = 0; i < ARRAY_SIZE(cfg->cutoff_regs); i++)
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000215 snd_soc_component_write(component,
Mark Brownfbbf5922011-03-11 18:09:04 +0000216 i + WM8958_MBC_BAND_2_LOWER_CUTOFF_C1_1,
217 cfg->cutoff_regs[i]);
218 }
219
220 /* Run the DSP */
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000221 snd_soc_component_write(component, WM8958_DSP2_EXECCONTROL,
Mark Brownfbbf5922011-03-11 18:09:04 +0000222 WM8958_DSP2_RUNR);
223
224 /* And we're off! */
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000225 snd_soc_component_update_bits(component, WM8958_DSP2_CONFIG,
Mark Brownfbbf5922011-03-11 18:09:04 +0000226 WM8958_MBC_ENA |
227 WM8958_MBC_SEL_MASK,
228 path << WM8958_MBC_SEL_SHIFT |
229 WM8958_MBC_ENA);
230}
231
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000232static void wm8958_dsp_start_vss(struct snd_soc_component *component, int path)
Mark Brown09e10d72011-03-16 22:57:47 +0000233{
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000234 struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
Mark Brownd9dd4ad2012-10-08 18:36:09 +0900235 struct wm8994 *control = wm8994->wm8994;
Mark Brown09e10d72011-03-16 22:57:47 +0000236 int i, ena;
237
238 if (wm8994->mbc_vss)
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000239 wm8958_dsp2_fw(component, "MBC+VSS", wm8994->mbc_vss, false);
Mark Brown09e10d72011-03-16 22:57:47 +0000240
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000241 snd_soc_component_update_bits(component, WM8958_DSP2_PROGRAM,
Mark Brown09e10d72011-03-16 22:57:47 +0000242 WM8958_DSP2_ENA, WM8958_DSP2_ENA);
243
244 /* If we've got user supplied settings use them */
Mark Brownd9dd4ad2012-10-08 18:36:09 +0900245 if (control->pdata.num_mbc_cfgs) {
Mark Brown09e10d72011-03-16 22:57:47 +0000246 struct wm8958_mbc_cfg *cfg
Mark Brownd9dd4ad2012-10-08 18:36:09 +0900247 = &control->pdata.mbc_cfgs[wm8994->mbc_cfg];
Mark Brown09e10d72011-03-16 22:57:47 +0000248
249 for (i = 0; i < ARRAY_SIZE(cfg->combined_regs); i++)
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000250 snd_soc_component_write(component, i + 0x2800,
Mark Brown09e10d72011-03-16 22:57:47 +0000251 cfg->combined_regs[i]);
252 }
253
Mark Brownd9dd4ad2012-10-08 18:36:09 +0900254 if (control->pdata.num_vss_cfgs) {
Mark Brown09e10d72011-03-16 22:57:47 +0000255 struct wm8958_vss_cfg *cfg
Mark Brownd9dd4ad2012-10-08 18:36:09 +0900256 = &control->pdata.vss_cfgs[wm8994->vss_cfg];
Mark Brown09e10d72011-03-16 22:57:47 +0000257
258 for (i = 0; i < ARRAY_SIZE(cfg->regs); i++)
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000259 snd_soc_component_write(component, i + 0x2600, cfg->regs[i]);
Mark Brown09e10d72011-03-16 22:57:47 +0000260 }
261
Mark Brownd9dd4ad2012-10-08 18:36:09 +0900262 if (control->pdata.num_vss_hpf_cfgs) {
Mark Brown09e10d72011-03-16 22:57:47 +0000263 struct wm8958_vss_hpf_cfg *cfg
Mark Brownd9dd4ad2012-10-08 18:36:09 +0900264 = &control->pdata.vss_hpf_cfgs[wm8994->vss_hpf_cfg];
Mark Brown09e10d72011-03-16 22:57:47 +0000265
266 for (i = 0; i < ARRAY_SIZE(cfg->regs); i++)
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000267 snd_soc_component_write(component, i + 0x2400, cfg->regs[i]);
Mark Brown09e10d72011-03-16 22:57:47 +0000268 }
269
270 /* Run the DSP */
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000271 snd_soc_component_write(component, WM8958_DSP2_EXECCONTROL,
Mark Brown09e10d72011-03-16 22:57:47 +0000272 WM8958_DSP2_RUNR);
273
274 /* Enable the algorithms we've selected */
275 ena = 0;
276 if (wm8994->mbc_ena[path])
277 ena |= 0x8;
278 if (wm8994->hpf2_ena[path])
279 ena |= 0x4;
280 if (wm8994->hpf1_ena[path])
281 ena |= 0x2;
282 if (wm8994->vss_ena[path])
283 ena |= 0x1;
284
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000285 snd_soc_component_write(component, 0x2201, ena);
Mark Brown09e10d72011-03-16 22:57:47 +0000286
287 /* Switch the DSP into the data path */
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000288 snd_soc_component_update_bits(component, WM8958_DSP2_CONFIG,
Mark Brown09e10d72011-03-16 22:57:47 +0000289 WM8958_MBC_SEL_MASK | WM8958_MBC_ENA,
290 path << WM8958_MBC_SEL_SHIFT | WM8958_MBC_ENA);
291}
292
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000293static void wm8958_dsp_start_enh_eq(struct snd_soc_component *component, int path)
Mark Brown31215872011-03-17 20:23:43 +0000294{
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000295 struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
Mark Brownd9dd4ad2012-10-08 18:36:09 +0900296 struct wm8994 *control = wm8994->wm8994;
Mark Brown31215872011-03-17 20:23:43 +0000297 int i;
298
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000299 wm8958_dsp2_fw(component, "ENH_EQ", wm8994->enh_eq, false);
Mark Brown31215872011-03-17 20:23:43 +0000300
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000301 snd_soc_component_update_bits(component, WM8958_DSP2_PROGRAM,
Mark Brown31215872011-03-17 20:23:43 +0000302 WM8958_DSP2_ENA, WM8958_DSP2_ENA);
303
304 /* If we've got user supplied settings use them */
Mark Brownd9dd4ad2012-10-08 18:36:09 +0900305 if (control->pdata.num_enh_eq_cfgs) {
Mark Brown31215872011-03-17 20:23:43 +0000306 struct wm8958_enh_eq_cfg *cfg
Mark Brownd9dd4ad2012-10-08 18:36:09 +0900307 = &control->pdata.enh_eq_cfgs[wm8994->enh_eq_cfg];
Mark Brown31215872011-03-17 20:23:43 +0000308
309 for (i = 0; i < ARRAY_SIZE(cfg->regs); i++)
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000310 snd_soc_component_write(component, i + 0x2200,
Mark Brown31215872011-03-17 20:23:43 +0000311 cfg->regs[i]);
312 }
313
314 /* Run the DSP */
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000315 snd_soc_component_write(component, WM8958_DSP2_EXECCONTROL,
Mark Brown31215872011-03-17 20:23:43 +0000316 WM8958_DSP2_RUNR);
317
318 /* Switch the DSP into the data path */
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000319 snd_soc_component_update_bits(component, WM8958_DSP2_CONFIG,
Mark Brown31215872011-03-17 20:23:43 +0000320 WM8958_MBC_SEL_MASK | WM8958_MBC_ENA,
321 path << WM8958_MBC_SEL_SHIFT | WM8958_MBC_ENA);
322}
Mark Brown09e10d72011-03-16 22:57:47 +0000323
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000324static void wm8958_dsp_apply(struct snd_soc_component *component, int path, int start)
Mark Brownfbbf5922011-03-11 18:09:04 +0000325{
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000326 struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
327 int pwr_reg = snd_soc_component_read32(component, WM8994_POWER_MANAGEMENT_5);
Mark Brownf20d77c2011-03-16 20:55:37 +0000328 int ena, reg, aif;
Mark Brownf701a2e2011-03-09 19:31:01 +0000329
Mark Brownf20d77c2011-03-16 20:55:37 +0000330 switch (path) {
Mark Brownf701a2e2011-03-09 19:31:01 +0000331 case 0:
332 pwr_reg &= (WM8994_AIF1DAC1L_ENA | WM8994_AIF1DAC1R_ENA);
333 aif = 0;
334 break;
335 case 1:
336 pwr_reg &= (WM8994_AIF1DAC2L_ENA | WM8994_AIF1DAC2R_ENA);
337 aif = 0;
338 break;
339 case 2:
340 pwr_reg &= (WM8994_AIF2DACL_ENA | WM8994_AIF2DACR_ENA);
341 aif = 1;
342 break;
343 default:
Takashi Iwaia845d592013-11-06 11:07:15 +0100344 WARN(1, "Invalid path %d\n", path);
Mark Brownf701a2e2011-03-09 19:31:01 +0000345 return;
346 }
347
Mark Brownf20d77c2011-03-16 20:55:37 +0000348 /* Do we have both an active AIF and an active algorithm? */
Mark Brown09e10d72011-03-16 22:57:47 +0000349 ena = wm8994->mbc_ena[path] || wm8994->vss_ena[path] ||
Mark Brown31215872011-03-17 20:23:43 +0000350 wm8994->hpf1_ena[path] || wm8994->hpf2_ena[path] ||
351 wm8994->enh_eq_ena[path];
Mark Brownf20d77c2011-03-16 20:55:37 +0000352 if (!pwr_reg)
353 ena = 0;
Mark Brownf701a2e2011-03-09 19:31:01 +0000354
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000355 reg = snd_soc_component_read32(component, WM8958_DSP2_PROGRAM);
Mark Brownf701a2e2011-03-09 19:31:01 +0000356
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000357 dev_dbg(component->dev, "DSP path %d %d startup: %d, power: %x, DSP: %x\n",
Mark Brownf20d77c2011-03-16 20:55:37 +0000358 path, wm8994->dsp_active, start, pwr_reg, reg);
Mark Brownf701a2e2011-03-09 19:31:01 +0000359
360 if (start && ena) {
Mark Brownfa63e472011-05-15 18:18:56 -0700361 /* If the DSP is already running then noop */
362 if (reg & WM8958_DSP2_ENA)
363 return;
364
Mark Brownf20d77c2011-03-16 20:55:37 +0000365 /* If either AIFnCLK is not yet enabled postpone */
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000366 if (!(snd_soc_component_read32(component, WM8994_AIF1_CLOCKING_1)
Mark Brownc6b7b572011-03-11 18:13:12 +0000367 & WM8994_AIF1CLK_ENA_MASK) &&
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000368 !(snd_soc_component_read32(component, WM8994_AIF2_CLOCKING_1)
Mark Brownc6b7b572011-03-11 18:13:12 +0000369 & WM8994_AIF2CLK_ENA_MASK))
370 return;
371
Mark Brownf701a2e2011-03-09 19:31:01 +0000372 /* Switch the clock over to the appropriate AIF */
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000373 snd_soc_component_update_bits(component, WM8994_CLOCKING_1,
Mark Brownf701a2e2011-03-09 19:31:01 +0000374 WM8958_DSP2CLK_SRC | WM8958_DSP2CLK_ENA,
375 aif << WM8958_DSP2CLK_SRC_SHIFT |
376 WM8958_DSP2CLK_ENA);
377
Mark Brown31215872011-03-17 20:23:43 +0000378 if (wm8994->enh_eq_ena[path])
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000379 wm8958_dsp_start_enh_eq(component, path);
Mark Brown31215872011-03-17 20:23:43 +0000380 else if (wm8994->vss_ena[path] || wm8994->hpf1_ena[path] ||
Mark Brown09e10d72011-03-16 22:57:47 +0000381 wm8994->hpf2_ena[path])
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000382 wm8958_dsp_start_vss(component, path);
Mark Brown09e10d72011-03-16 22:57:47 +0000383 else if (wm8994->mbc_ena[path])
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000384 wm8958_dsp_start_mbc(component, path);
Mark Brownf701a2e2011-03-09 19:31:01 +0000385
Mark Brown09e10d72011-03-16 22:57:47 +0000386 wm8994->dsp_active = path;
387
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000388 dev_dbg(component->dev, "DSP running in path %d\n", path);
Mark Brown09e10d72011-03-16 22:57:47 +0000389 }
390
391 if (!start && wm8994->dsp_active == path) {
Mark Brownf701a2e2011-03-09 19:31:01 +0000392 /* If the DSP is already stopped then noop */
393 if (!(reg & WM8958_DSP2_ENA))
394 return;
395
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000396 snd_soc_component_update_bits(component, WM8958_DSP2_CONFIG,
Mark Brownf701a2e2011-03-09 19:31:01 +0000397 WM8958_MBC_ENA, 0);
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000398 snd_soc_component_write(component, WM8958_DSP2_EXECCONTROL,
Mark Brownf20d77c2011-03-16 20:55:37 +0000399 WM8958_DSP2_STOP);
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000400 snd_soc_component_update_bits(component, WM8958_DSP2_PROGRAM,
Mark Brownf701a2e2011-03-09 19:31:01 +0000401 WM8958_DSP2_ENA, 0);
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000402 snd_soc_component_update_bits(component, WM8994_CLOCKING_1,
Mark Brownf701a2e2011-03-09 19:31:01 +0000403 WM8958_DSP2CLK_ENA, 0);
Mark Brownf20d77c2011-03-16 20:55:37 +0000404
405 wm8994->dsp_active = -1;
406
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000407 dev_dbg(component->dev, "DSP stopped\n");
Mark Brownf701a2e2011-03-09 19:31:01 +0000408 }
409}
410
411int wm8958_aif_ev(struct snd_soc_dapm_widget *w,
412 struct snd_kcontrol *kcontrol, int event)
413{
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000414 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
Mark Brownc6b7b572011-03-11 18:13:12 +0000415 int i;
Mark Brownf701a2e2011-03-09 19:31:01 +0000416
417 switch (event) {
418 case SND_SOC_DAPM_POST_PMU:
Mark Brownc6b7b572011-03-11 18:13:12 +0000419 case SND_SOC_DAPM_PRE_PMU:
420 for (i = 0; i < 3; i++)
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000421 wm8958_dsp_apply(component, i, 1);
Mark Brownf701a2e2011-03-09 19:31:01 +0000422 break;
423 case SND_SOC_DAPM_POST_PMD:
Mark Brownc6b7b572011-03-11 18:13:12 +0000424 case SND_SOC_DAPM_PRE_PMD:
425 for (i = 0; i < 3; i++)
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000426 wm8958_dsp_apply(component, i, 0);
Mark Brownf701a2e2011-03-09 19:31:01 +0000427 break;
428 }
429
430 return 0;
431}
432
Mark Brownf20d77c2011-03-16 20:55:37 +0000433/* Check if DSP2 is in use on another AIF */
434static int wm8958_dsp2_busy(struct wm8994_priv *wm8994, int aif)
435{
436 int i;
437
438 for (i = 0; i < ARRAY_SIZE(wm8994->mbc_ena); i++) {
439 if (i == aif)
440 continue;
Mark Brown09e10d72011-03-16 22:57:47 +0000441 if (wm8994->mbc_ena[i] || wm8994->vss_ena[i] ||
442 wm8994->hpf1_ena[i] || wm8994->hpf2_ena[i])
Mark Brownf20d77c2011-03-16 20:55:37 +0000443 return 1;
444 }
445
446 return 0;
447}
448
Mark Brownf701a2e2011-03-09 19:31:01 +0000449static int wm8958_put_mbc_enum(struct snd_kcontrol *kcontrol,
450 struct snd_ctl_elem_value *ucontrol)
451{
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000452 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
453 struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
Mark Brownd9dd4ad2012-10-08 18:36:09 +0900454 struct wm8994 *control = wm8994->wm8994;
Takashi Iwaid0784822016-02-29 18:01:12 +0100455 int value = ucontrol->value.enumerated.item[0];
Mark Brownf701a2e2011-03-09 19:31:01 +0000456 int reg;
457
458 /* Don't allow on the fly reconfiguration */
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000459 reg = snd_soc_component_read32(component, WM8994_CLOCKING_1);
Mark Brownf701a2e2011-03-09 19:31:01 +0000460 if (reg < 0 || reg & WM8958_DSP2CLK_ENA)
461 return -EBUSY;
462
Mark Brownd9dd4ad2012-10-08 18:36:09 +0900463 if (value >= control->pdata.num_mbc_cfgs)
Mark Brownf701a2e2011-03-09 19:31:01 +0000464 return -EINVAL;
465
466 wm8994->mbc_cfg = value;
467
468 return 0;
469}
470
471static int wm8958_get_mbc_enum(struct snd_kcontrol *kcontrol,
472 struct snd_ctl_elem_value *ucontrol)
473{
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000474 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
475 struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
Mark Brownf701a2e2011-03-09 19:31:01 +0000476
477 ucontrol->value.enumerated.item[0] = wm8994->mbc_cfg;
478
479 return 0;
480}
481
482static int wm8958_mbc_info(struct snd_kcontrol *kcontrol,
483 struct snd_ctl_elem_info *uinfo)
484{
485 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
486 uinfo->count = 1;
487 uinfo->value.integer.min = 0;
488 uinfo->value.integer.max = 1;
489 return 0;
490}
491
492static int wm8958_mbc_get(struct snd_kcontrol *kcontrol,
493 struct snd_ctl_elem_value *ucontrol)
494{
495 int mbc = kcontrol->private_value;
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000496 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
497 struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
Mark Brownf701a2e2011-03-09 19:31:01 +0000498
499 ucontrol->value.integer.value[0] = wm8994->mbc_ena[mbc];
500
501 return 0;
502}
503
504static int wm8958_mbc_put(struct snd_kcontrol *kcontrol,
505 struct snd_ctl_elem_value *ucontrol)
506{
507 int mbc = kcontrol->private_value;
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000508 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
509 struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
Mark Brownf701a2e2011-03-09 19:31:01 +0000510
Mark Brownd7fdae72011-05-15 18:02:53 -0700511 if (wm8994->mbc_ena[mbc] == ucontrol->value.integer.value[0])
512 return 0;
513
Mark Brownf701a2e2011-03-09 19:31:01 +0000514 if (ucontrol->value.integer.value[0] > 1)
515 return -EINVAL;
516
Mark Brownf20d77c2011-03-16 20:55:37 +0000517 if (wm8958_dsp2_busy(wm8994, mbc)) {
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000518 dev_dbg(component->dev, "DSP2 active on %d already\n", mbc);
Mark Brownf20d77c2011-03-16 20:55:37 +0000519 return -EBUSY;
Mark Brownf701a2e2011-03-09 19:31:01 +0000520 }
521
Mark Brown31215872011-03-17 20:23:43 +0000522 if (wm8994->enh_eq_ena[mbc])
523 return -EBUSY;
524
Mark Brownf701a2e2011-03-09 19:31:01 +0000525 wm8994->mbc_ena[mbc] = ucontrol->value.integer.value[0];
526
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000527 wm8958_dsp_apply(component, mbc, wm8994->mbc_ena[mbc]);
Mark Brownf701a2e2011-03-09 19:31:01 +0000528
529 return 0;
530}
531
532#define WM8958_MBC_SWITCH(xname, xval) {\
533 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), \
534 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,\
535 .info = wm8958_mbc_info, \
536 .get = wm8958_mbc_get, .put = wm8958_mbc_put, \
537 .private_value = xval }
538
Mark Brown09e10d72011-03-16 22:57:47 +0000539static int wm8958_put_vss_enum(struct snd_kcontrol *kcontrol,
540 struct snd_ctl_elem_value *ucontrol)
541{
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000542 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
543 struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
Mark Brownd9dd4ad2012-10-08 18:36:09 +0900544 struct wm8994 *control = wm8994->wm8994;
Takashi Iwaid0784822016-02-29 18:01:12 +0100545 int value = ucontrol->value.enumerated.item[0];
Mark Brown09e10d72011-03-16 22:57:47 +0000546 int reg;
547
548 /* Don't allow on the fly reconfiguration */
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000549 reg = snd_soc_component_read32(component, WM8994_CLOCKING_1);
Mark Brown09e10d72011-03-16 22:57:47 +0000550 if (reg < 0 || reg & WM8958_DSP2CLK_ENA)
551 return -EBUSY;
552
Mark Brownd9dd4ad2012-10-08 18:36:09 +0900553 if (value >= control->pdata.num_vss_cfgs)
Mark Brown09e10d72011-03-16 22:57:47 +0000554 return -EINVAL;
555
556 wm8994->vss_cfg = value;
557
558 return 0;
559}
560
561static int wm8958_get_vss_enum(struct snd_kcontrol *kcontrol,
562 struct snd_ctl_elem_value *ucontrol)
563{
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000564 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
565 struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
Mark Brown09e10d72011-03-16 22:57:47 +0000566
567 ucontrol->value.enumerated.item[0] = wm8994->vss_cfg;
568
569 return 0;
570}
571
572static int wm8958_put_vss_hpf_enum(struct snd_kcontrol *kcontrol,
573 struct snd_ctl_elem_value *ucontrol)
574{
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000575 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
576 struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
Mark Brownd9dd4ad2012-10-08 18:36:09 +0900577 struct wm8994 *control = wm8994->wm8994;
Takashi Iwaid0784822016-02-29 18:01:12 +0100578 int value = ucontrol->value.enumerated.item[0];
Mark Brown09e10d72011-03-16 22:57:47 +0000579 int reg;
580
581 /* Don't allow on the fly reconfiguration */
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000582 reg = snd_soc_component_read32(component, WM8994_CLOCKING_1);
Mark Brown09e10d72011-03-16 22:57:47 +0000583 if (reg < 0 || reg & WM8958_DSP2CLK_ENA)
584 return -EBUSY;
585
Mark Brownd9dd4ad2012-10-08 18:36:09 +0900586 if (value >= control->pdata.num_vss_hpf_cfgs)
Mark Brown09e10d72011-03-16 22:57:47 +0000587 return -EINVAL;
588
589 wm8994->vss_hpf_cfg = value;
590
591 return 0;
592}
593
594static int wm8958_get_vss_hpf_enum(struct snd_kcontrol *kcontrol,
595 struct snd_ctl_elem_value *ucontrol)
596{
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000597 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
598 struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
Mark Brown09e10d72011-03-16 22:57:47 +0000599
600 ucontrol->value.enumerated.item[0] = wm8994->vss_hpf_cfg;
601
602 return 0;
603}
604
605static int wm8958_vss_info(struct snd_kcontrol *kcontrol,
606 struct snd_ctl_elem_info *uinfo)
607{
608 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
609 uinfo->count = 1;
610 uinfo->value.integer.min = 0;
611 uinfo->value.integer.max = 1;
612 return 0;
613}
614
615static int wm8958_vss_get(struct snd_kcontrol *kcontrol,
616 struct snd_ctl_elem_value *ucontrol)
617{
618 int vss = kcontrol->private_value;
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000619 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
620 struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
Mark Brown09e10d72011-03-16 22:57:47 +0000621
622 ucontrol->value.integer.value[0] = wm8994->vss_ena[vss];
623
624 return 0;
625}
626
627static int wm8958_vss_put(struct snd_kcontrol *kcontrol,
628 struct snd_ctl_elem_value *ucontrol)
629{
630 int vss = kcontrol->private_value;
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000631 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
632 struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
Mark Brown09e10d72011-03-16 22:57:47 +0000633
Mark Brownd7fdae72011-05-15 18:02:53 -0700634 if (wm8994->vss_ena[vss] == ucontrol->value.integer.value[0])
635 return 0;
636
Mark Brown09e10d72011-03-16 22:57:47 +0000637 if (ucontrol->value.integer.value[0] > 1)
638 return -EINVAL;
639
640 if (!wm8994->mbc_vss)
641 return -ENODEV;
642
643 if (wm8958_dsp2_busy(wm8994, vss)) {
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000644 dev_dbg(component->dev, "DSP2 active on %d already\n", vss);
Mark Brown09e10d72011-03-16 22:57:47 +0000645 return -EBUSY;
646 }
647
Mark Brown31215872011-03-17 20:23:43 +0000648 if (wm8994->enh_eq_ena[vss])
649 return -EBUSY;
650
Mark Brown09e10d72011-03-16 22:57:47 +0000651 wm8994->vss_ena[vss] = ucontrol->value.integer.value[0];
652
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000653 wm8958_dsp_apply(component, vss, wm8994->vss_ena[vss]);
Mark Brown09e10d72011-03-16 22:57:47 +0000654
655 return 0;
656}
657
658
659#define WM8958_VSS_SWITCH(xname, xval) {\
660 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), \
661 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,\
662 .info = wm8958_vss_info, \
663 .get = wm8958_vss_get, .put = wm8958_vss_put, \
664 .private_value = xval }
665
666static int wm8958_hpf_info(struct snd_kcontrol *kcontrol,
667 struct snd_ctl_elem_info *uinfo)
668{
669 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
670 uinfo->count = 1;
671 uinfo->value.integer.min = 0;
672 uinfo->value.integer.max = 1;
673 return 0;
674}
675
676static int wm8958_hpf_get(struct snd_kcontrol *kcontrol,
677 struct snd_ctl_elem_value *ucontrol)
678{
679 int hpf = kcontrol->private_value;
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000680 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
681 struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
Mark Brown09e10d72011-03-16 22:57:47 +0000682
683 if (hpf < 3)
684 ucontrol->value.integer.value[0] = wm8994->hpf1_ena[hpf % 3];
685 else
686 ucontrol->value.integer.value[0] = wm8994->hpf2_ena[hpf % 3];
687
688 return 0;
689}
690
691static int wm8958_hpf_put(struct snd_kcontrol *kcontrol,
692 struct snd_ctl_elem_value *ucontrol)
693{
694 int hpf = kcontrol->private_value;
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000695 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
696 struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
Mark Brown09e10d72011-03-16 22:57:47 +0000697
Mark Brownd7fdae72011-05-15 18:02:53 -0700698 if (hpf < 3) {
699 if (wm8994->hpf1_ena[hpf % 3] ==
700 ucontrol->value.integer.value[0])
701 return 0;
702 } else {
703 if (wm8994->hpf2_ena[hpf % 3] ==
704 ucontrol->value.integer.value[0])
705 return 0;
706 }
707
Mark Brown09e10d72011-03-16 22:57:47 +0000708 if (ucontrol->value.integer.value[0] > 1)
709 return -EINVAL;
710
711 if (!wm8994->mbc_vss)
712 return -ENODEV;
713
714 if (wm8958_dsp2_busy(wm8994, hpf % 3)) {
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000715 dev_dbg(component->dev, "DSP2 active on %d already\n", hpf);
Mark Brown09e10d72011-03-16 22:57:47 +0000716 return -EBUSY;
717 }
718
Mark Brown31215872011-03-17 20:23:43 +0000719 if (wm8994->enh_eq_ena[hpf % 3])
Mark Brown09e10d72011-03-16 22:57:47 +0000720 return -EBUSY;
721
722 if (hpf < 3)
723 wm8994->hpf1_ena[hpf % 3] = ucontrol->value.integer.value[0];
724 else
725 wm8994->hpf2_ena[hpf % 3] = ucontrol->value.integer.value[0];
726
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000727 wm8958_dsp_apply(component, hpf % 3, ucontrol->value.integer.value[0]);
Mark Brown09e10d72011-03-16 22:57:47 +0000728
729 return 0;
730}
731
732#define WM8958_HPF_SWITCH(xname, xval) {\
733 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), \
734 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,\
735 .info = wm8958_hpf_info, \
736 .get = wm8958_hpf_get, .put = wm8958_hpf_put, \
737 .private_value = xval }
738
Mark Brown31215872011-03-17 20:23:43 +0000739static int wm8958_put_enh_eq_enum(struct snd_kcontrol *kcontrol,
740 struct snd_ctl_elem_value *ucontrol)
741{
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000742 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
743 struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
Mark Brownd9dd4ad2012-10-08 18:36:09 +0900744 struct wm8994 *control = wm8994->wm8994;
Takashi Iwaid0784822016-02-29 18:01:12 +0100745 int value = ucontrol->value.enumerated.item[0];
Mark Brown31215872011-03-17 20:23:43 +0000746 int reg;
747
748 /* Don't allow on the fly reconfiguration */
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000749 reg = snd_soc_component_read32(component, WM8994_CLOCKING_1);
Mark Brown31215872011-03-17 20:23:43 +0000750 if (reg < 0 || reg & WM8958_DSP2CLK_ENA)
751 return -EBUSY;
752
Mark Brownd9dd4ad2012-10-08 18:36:09 +0900753 if (value >= control->pdata.num_enh_eq_cfgs)
Mark Brown31215872011-03-17 20:23:43 +0000754 return -EINVAL;
755
756 wm8994->enh_eq_cfg = value;
757
758 return 0;
759}
760
761static int wm8958_get_enh_eq_enum(struct snd_kcontrol *kcontrol,
762 struct snd_ctl_elem_value *ucontrol)
763{
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000764 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
765 struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
Mark Brown31215872011-03-17 20:23:43 +0000766
767 ucontrol->value.enumerated.item[0] = wm8994->enh_eq_cfg;
768
769 return 0;
770}
771
772static int wm8958_enh_eq_info(struct snd_kcontrol *kcontrol,
773 struct snd_ctl_elem_info *uinfo)
774{
775 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
776 uinfo->count = 1;
777 uinfo->value.integer.min = 0;
778 uinfo->value.integer.max = 1;
779 return 0;
780}
781
782static int wm8958_enh_eq_get(struct snd_kcontrol *kcontrol,
783 struct snd_ctl_elem_value *ucontrol)
784{
785 int eq = kcontrol->private_value;
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000786 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
787 struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
Mark Brown31215872011-03-17 20:23:43 +0000788
789 ucontrol->value.integer.value[0] = wm8994->enh_eq_ena[eq];
790
791 return 0;
792}
793
794static int wm8958_enh_eq_put(struct snd_kcontrol *kcontrol,
795 struct snd_ctl_elem_value *ucontrol)
796{
797 int eq = kcontrol->private_value;
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000798 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
799 struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
Mark Brown31215872011-03-17 20:23:43 +0000800
Mark Brownd7fdae72011-05-15 18:02:53 -0700801 if (wm8994->enh_eq_ena[eq] == ucontrol->value.integer.value[0])
802 return 0;
803
Mark Brown31215872011-03-17 20:23:43 +0000804 if (ucontrol->value.integer.value[0] > 1)
805 return -EINVAL;
806
807 if (!wm8994->enh_eq)
808 return -ENODEV;
809
810 if (wm8958_dsp2_busy(wm8994, eq)) {
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000811 dev_dbg(component->dev, "DSP2 active on %d already\n", eq);
Mark Brown31215872011-03-17 20:23:43 +0000812 return -EBUSY;
813 }
814
815 if (wm8994->mbc_ena[eq] || wm8994->vss_ena[eq] ||
816 wm8994->hpf1_ena[eq] || wm8994->hpf2_ena[eq])
817 return -EBUSY;
818
819 wm8994->enh_eq_ena[eq] = ucontrol->value.integer.value[0];
820
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000821 wm8958_dsp_apply(component, eq, ucontrol->value.integer.value[0]);
Mark Brown31215872011-03-17 20:23:43 +0000822
823 return 0;
824}
825
826#define WM8958_ENH_EQ_SWITCH(xname, xval) {\
827 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), \
828 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,\
829 .info = wm8958_enh_eq_info, \
830 .get = wm8958_enh_eq_get, .put = wm8958_enh_eq_put, \
831 .private_value = xval }
832
Mark Brownf701a2e2011-03-09 19:31:01 +0000833static const struct snd_kcontrol_new wm8958_mbc_snd_controls[] = {
834WM8958_MBC_SWITCH("AIF1DAC1 MBC Switch", 0),
835WM8958_MBC_SWITCH("AIF1DAC2 MBC Switch", 1),
836WM8958_MBC_SWITCH("AIF2DAC MBC Switch", 2),
837};
838
Mark Brown09e10d72011-03-16 22:57:47 +0000839static const struct snd_kcontrol_new wm8958_vss_snd_controls[] = {
840WM8958_VSS_SWITCH("AIF1DAC1 VSS Switch", 0),
841WM8958_VSS_SWITCH("AIF1DAC2 VSS Switch", 1),
842WM8958_VSS_SWITCH("AIF2DAC VSS Switch", 2),
843WM8958_HPF_SWITCH("AIF1DAC1 HPF1 Switch", 0),
844WM8958_HPF_SWITCH("AIF1DAC2 HPF1 Switch", 1),
845WM8958_HPF_SWITCH("AIF2DAC HPF1 Switch", 2),
846WM8958_HPF_SWITCH("AIF1DAC1 HPF2 Switch", 3),
847WM8958_HPF_SWITCH("AIF1DAC2 HPF2 Switch", 4),
848WM8958_HPF_SWITCH("AIF2DAC HPF2 Switch", 5),
849};
850
Mark Brown31215872011-03-17 20:23:43 +0000851static const struct snd_kcontrol_new wm8958_enh_eq_snd_controls[] = {
852WM8958_ENH_EQ_SWITCH("AIF1DAC1 Enhanced EQ Switch", 0),
853WM8958_ENH_EQ_SWITCH("AIF1DAC2 Enhanced EQ Switch", 1),
854WM8958_ENH_EQ_SWITCH("AIF2DAC Enhanced EQ Switch", 2),
855};
856
857static void wm8958_enh_eq_loaded(const struct firmware *fw, void *context)
858{
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000859 struct snd_soc_component *component = context;
860 struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
Mark Brown31215872011-03-17 20:23:43 +0000861
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000862 if (fw && (wm8958_dsp2_fw(component, "ENH_EQ", fw, true) == 0)) {
Lars-Peter Clausenfabfad22014-11-09 17:01:02 +0100863 mutex_lock(&wm8994->fw_lock);
Mark Brown31215872011-03-17 20:23:43 +0000864 wm8994->enh_eq = fw;
Lars-Peter Clausenfabfad22014-11-09 17:01:02 +0100865 mutex_unlock(&wm8994->fw_lock);
Mark Brown31215872011-03-17 20:23:43 +0000866 }
867}
868
Mark Brown09e10d72011-03-16 22:57:47 +0000869static void wm8958_mbc_vss_loaded(const struct firmware *fw, void *context)
870{
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000871 struct snd_soc_component *component = context;
872 struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
Mark Brown09e10d72011-03-16 22:57:47 +0000873
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000874 if (fw && (wm8958_dsp2_fw(component, "MBC+VSS", fw, true) == 0)) {
Lars-Peter Clausenfabfad22014-11-09 17:01:02 +0100875 mutex_lock(&wm8994->fw_lock);
Mark Brown09e10d72011-03-16 22:57:47 +0000876 wm8994->mbc_vss = fw;
Lars-Peter Clausenfabfad22014-11-09 17:01:02 +0100877 mutex_unlock(&wm8994->fw_lock);
Mark Brown09e10d72011-03-16 22:57:47 +0000878 }
Mark Brown09e10d72011-03-16 22:57:47 +0000879}
880
Mark Brownfbbf5922011-03-11 18:09:04 +0000881static void wm8958_mbc_loaded(const struct firmware *fw, void *context)
882{
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000883 struct snd_soc_component *component = context;
884 struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
Mark Brownfbbf5922011-03-11 18:09:04 +0000885
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000886 if (fw && (wm8958_dsp2_fw(component, "MBC", fw, true) == 0)) {
Lars-Peter Clausenfabfad22014-11-09 17:01:02 +0100887 mutex_lock(&wm8994->fw_lock);
Mark Brown0ffecd72012-10-10 10:59:24 +0900888 wm8994->mbc = fw;
Lars-Peter Clausenfabfad22014-11-09 17:01:02 +0100889 mutex_unlock(&wm8994->fw_lock);
Mark Brown0ffecd72012-10-10 10:59:24 +0900890 }
Mark Brownfbbf5922011-03-11 18:09:04 +0000891}
892
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000893void wm8958_dsp2_init(struct snd_soc_component *component)
Mark Brownf701a2e2011-03-09 19:31:01 +0000894{
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000895 struct wm8994_priv *wm8994 = snd_soc_component_get_drvdata(component);
Mark Brownd9dd4ad2012-10-08 18:36:09 +0900896 struct wm8994 *control = wm8994->wm8994;
897 struct wm8994_pdata *pdata = &control->pdata;
Mark Brownf701a2e2011-03-09 19:31:01 +0000898 int ret, i;
899
Mark Brownf20d77c2011-03-16 20:55:37 +0000900 wm8994->dsp_active = -1;
901
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000902 snd_soc_add_component_controls(component, wm8958_mbc_snd_controls,
Mark Brownf701a2e2011-03-09 19:31:01 +0000903 ARRAY_SIZE(wm8958_mbc_snd_controls));
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000904 snd_soc_add_component_controls(component, wm8958_vss_snd_controls,
Mark Brown09e10d72011-03-16 22:57:47 +0000905 ARRAY_SIZE(wm8958_vss_snd_controls));
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000906 snd_soc_add_component_controls(component, wm8958_enh_eq_snd_controls,
Mark Brown31215872011-03-17 20:23:43 +0000907 ARRAY_SIZE(wm8958_enh_eq_snd_controls));
Mark Brown09e10d72011-03-16 22:57:47 +0000908
Mark Brownf701a2e2011-03-09 19:31:01 +0000909
Mark Brownf20d77c2011-03-16 20:55:37 +0000910 /* We don't *require* firmware and don't want to delay boot */
Mark Brownfbbf5922011-03-11 18:09:04 +0000911 request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000912 "wm8958_mbc.wfw", component->dev, GFP_KERNEL,
913 component, wm8958_mbc_loaded);
Mark Brown0ffecd72012-10-10 10:59:24 +0900914 request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000915 "wm8958_mbc_vss.wfw", component->dev, GFP_KERNEL,
916 component, wm8958_mbc_vss_loaded);
Mark Brown0ffecd72012-10-10 10:59:24 +0900917 request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000918 "wm8958_enh_eq.wfw", component->dev, GFP_KERNEL,
919 component, wm8958_enh_eq_loaded);
Mark Brownfbbf5922011-03-11 18:09:04 +0000920
Mark Brownf701a2e2011-03-09 19:31:01 +0000921 if (pdata->num_mbc_cfgs) {
922 struct snd_kcontrol_new control[] = {
923 SOC_ENUM_EXT("MBC Mode", wm8994->mbc_enum,
924 wm8958_get_mbc_enum, wm8958_put_mbc_enum),
925 };
926
927 /* We need an array of texts for the enum API */
Kees Cook6da2ec52018-06-12 13:55:00 -0700928 wm8994->mbc_texts = kmalloc_array(pdata->num_mbc_cfgs,
929 sizeof(char *),
930 GFP_KERNEL);
Sachin Kamat2cec4ff2014-06-20 15:29:09 +0530931 if (!wm8994->mbc_texts)
Mark Brownf701a2e2011-03-09 19:31:01 +0000932 return;
Mark Brownf701a2e2011-03-09 19:31:01 +0000933
934 for (i = 0; i < pdata->num_mbc_cfgs; i++)
935 wm8994->mbc_texts[i] = pdata->mbc_cfgs[i].name;
936
Takashi Iwai9a8d38d2014-02-18 08:11:42 +0100937 wm8994->mbc_enum.items = pdata->num_mbc_cfgs;
Mark Brownf701a2e2011-03-09 19:31:01 +0000938 wm8994->mbc_enum.texts = wm8994->mbc_texts;
939
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000940 ret = snd_soc_add_component_controls(wm8994->hubs.component,
Mark Brown8cb8e832012-07-25 18:10:03 +0100941 control, 1);
Mark Brownf701a2e2011-03-09 19:31:01 +0000942 if (ret != 0)
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000943 dev_err(wm8994->hubs.component->dev,
Mark Brownf701a2e2011-03-09 19:31:01 +0000944 "Failed to add MBC mode controls: %d\n", ret);
945 }
946
Mark Brown09e10d72011-03-16 22:57:47 +0000947 if (pdata->num_vss_cfgs) {
948 struct snd_kcontrol_new control[] = {
949 SOC_ENUM_EXT("VSS Mode", wm8994->vss_enum,
950 wm8958_get_vss_enum, wm8958_put_vss_enum),
951 };
Mark Brownf701a2e2011-03-09 19:31:01 +0000952
Mark Brown09e10d72011-03-16 22:57:47 +0000953 /* We need an array of texts for the enum API */
Kees Cook6da2ec52018-06-12 13:55:00 -0700954 wm8994->vss_texts = kmalloc_array(pdata->num_vss_cfgs,
955 sizeof(char *),
956 GFP_KERNEL);
Sachin Kamat2cec4ff2014-06-20 15:29:09 +0530957 if (!wm8994->vss_texts)
Mark Brown09e10d72011-03-16 22:57:47 +0000958 return;
Mark Brown09e10d72011-03-16 22:57:47 +0000959
960 for (i = 0; i < pdata->num_vss_cfgs; i++)
961 wm8994->vss_texts[i] = pdata->vss_cfgs[i].name;
962
Takashi Iwai9a8d38d2014-02-18 08:11:42 +0100963 wm8994->vss_enum.items = pdata->num_vss_cfgs;
Mark Brown09e10d72011-03-16 22:57:47 +0000964 wm8994->vss_enum.texts = wm8994->vss_texts;
965
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000966 ret = snd_soc_add_component_controls(wm8994->hubs.component,
Mark Brown8cb8e832012-07-25 18:10:03 +0100967 control, 1);
Mark Brown09e10d72011-03-16 22:57:47 +0000968 if (ret != 0)
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000969 dev_err(wm8994->hubs.component->dev,
Mark Brown09e10d72011-03-16 22:57:47 +0000970 "Failed to add VSS mode controls: %d\n", ret);
971 }
972
973 if (pdata->num_vss_hpf_cfgs) {
974 struct snd_kcontrol_new control[] = {
975 SOC_ENUM_EXT("VSS HPF Mode", wm8994->vss_hpf_enum,
976 wm8958_get_vss_hpf_enum,
977 wm8958_put_vss_hpf_enum),
978 };
979
980 /* We need an array of texts for the enum API */
Kees Cook6da2ec52018-06-12 13:55:00 -0700981 wm8994->vss_hpf_texts = kmalloc_array(pdata->num_vss_hpf_cfgs,
982 sizeof(char *),
983 GFP_KERNEL);
Sachin Kamat2cec4ff2014-06-20 15:29:09 +0530984 if (!wm8994->vss_hpf_texts)
Mark Brown09e10d72011-03-16 22:57:47 +0000985 return;
Mark Brown09e10d72011-03-16 22:57:47 +0000986
987 for (i = 0; i < pdata->num_vss_hpf_cfgs; i++)
988 wm8994->vss_hpf_texts[i] = pdata->vss_hpf_cfgs[i].name;
989
Takashi Iwai9a8d38d2014-02-18 08:11:42 +0100990 wm8994->vss_hpf_enum.items = pdata->num_vss_hpf_cfgs;
Mark Brown09e10d72011-03-16 22:57:47 +0000991 wm8994->vss_hpf_enum.texts = wm8994->vss_hpf_texts;
992
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000993 ret = snd_soc_add_component_controls(wm8994->hubs.component,
Mark Brown8cb8e832012-07-25 18:10:03 +0100994 control, 1);
Mark Brown09e10d72011-03-16 22:57:47 +0000995 if (ret != 0)
Kuninori Morimoto00a69412018-01-29 03:12:21 +0000996 dev_err(wm8994->hubs.component->dev,
Mark Brown09e10d72011-03-16 22:57:47 +0000997 "Failed to add VSS HPFmode controls: %d\n",
998 ret);
999 }
Mark Brown31215872011-03-17 20:23:43 +00001000
1001 if (pdata->num_enh_eq_cfgs) {
1002 struct snd_kcontrol_new control[] = {
1003 SOC_ENUM_EXT("Enhanced EQ Mode", wm8994->enh_eq_enum,
1004 wm8958_get_enh_eq_enum,
1005 wm8958_put_enh_eq_enum),
1006 };
1007
1008 /* We need an array of texts for the enum API */
Kees Cook6da2ec52018-06-12 13:55:00 -07001009 wm8994->enh_eq_texts = kmalloc_array(pdata->num_enh_eq_cfgs,
1010 sizeof(char *),
1011 GFP_KERNEL);
Sachin Kamat2cec4ff2014-06-20 15:29:09 +05301012 if (!wm8994->enh_eq_texts)
Mark Brown31215872011-03-17 20:23:43 +00001013 return;
Mark Brown31215872011-03-17 20:23:43 +00001014
1015 for (i = 0; i < pdata->num_enh_eq_cfgs; i++)
1016 wm8994->enh_eq_texts[i] = pdata->enh_eq_cfgs[i].name;
1017
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01001018 wm8994->enh_eq_enum.items = pdata->num_enh_eq_cfgs;
Mark Brown31215872011-03-17 20:23:43 +00001019 wm8994->enh_eq_enum.texts = wm8994->enh_eq_texts;
1020
Kuninori Morimoto00a69412018-01-29 03:12:21 +00001021 ret = snd_soc_add_component_controls(wm8994->hubs.component,
Mark Brown8cb8e832012-07-25 18:10:03 +01001022 control, 1);
Mark Brown31215872011-03-17 20:23:43 +00001023 if (ret != 0)
Kuninori Morimoto00a69412018-01-29 03:12:21 +00001024 dev_err(wm8994->hubs.component->dev,
Mark Brown31215872011-03-17 20:23:43 +00001025 "Failed to add enhanced EQ controls: %d\n",
1026 ret);
1027 }
Mark Brownf701a2e2011-03-09 19:31:01 +00001028}