blob: f07775ebec045e175874b561af66a17eb9ae982a [file] [log] [blame]
Mark Brownf701a2e2011-03-09 19:31:01 +00001/*
2 * wm8958-dsp2.c -- WM8958 DSP2 support
3 *
4 * Copyright 2011 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/pm.h>
18#include <linux/i2c.h>
19#include <linux/platform_device.h>
20#include <linux/slab.h>
21#include <sound/soc.h>
22#include <sound/initval.h>
23#include <sound/tlv.h>
24#include <trace/events/asoc.h>
25
26#include <linux/mfd/wm8994/core.h>
27#include <linux/mfd/wm8994/registers.h>
28#include <linux/mfd/wm8994/pdata.h>
29#include <linux/mfd/wm8994/gpio.h>
30
31#include "wm8994.h"
32
33static void wm8958_mbc_apply(struct snd_soc_codec *codec, int mbc, int start)
34{
35 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
36 struct wm8994_pdata *pdata = wm8994->pdata;
37 int pwr_reg = snd_soc_read(codec, WM8994_POWER_MANAGEMENT_5);
38 int ena, reg, aif, i;
39
40 switch (mbc) {
41 case 0:
42 pwr_reg &= (WM8994_AIF1DAC1L_ENA | WM8994_AIF1DAC1R_ENA);
43 aif = 0;
44 break;
45 case 1:
46 pwr_reg &= (WM8994_AIF1DAC2L_ENA | WM8994_AIF1DAC2R_ENA);
47 aif = 0;
48 break;
49 case 2:
50 pwr_reg &= (WM8994_AIF2DACL_ENA | WM8994_AIF2DACR_ENA);
51 aif = 1;
52 break;
53 default:
54 BUG();
55 return;
56 }
57
58 /* We can only enable the MBC if the AIF is enabled and we
59 * want it to be enabled. */
60 ena = pwr_reg && wm8994->mbc_ena[mbc];
61
62 reg = snd_soc_read(codec, WM8958_DSP2_PROGRAM);
63
64 dev_dbg(codec->dev, "MBC %d startup: %d, power: %x, DSP: %x\n",
65 mbc, start, pwr_reg, reg);
66
67 if (start && ena) {
68 /* If the DSP is already running then noop */
69 if (reg & WM8958_DSP2_ENA)
70 return;
71
Mark Brownc6b7b572011-03-11 18:13:12 +000072 /* If neither AIFnCLK is not yet enabled postpone */
73 if (!(snd_soc_read(codec, WM8994_AIF1_CLOCKING_1)
74 & WM8994_AIF1CLK_ENA_MASK) &&
75 !(snd_soc_read(codec, WM8994_AIF2_CLOCKING_1)
76 & WM8994_AIF2CLK_ENA_MASK))
77 return;
78
Mark Brownf701a2e2011-03-09 19:31:01 +000079 /* Switch the clock over to the appropriate AIF */
80 snd_soc_update_bits(codec, WM8994_CLOCKING_1,
81 WM8958_DSP2CLK_SRC | WM8958_DSP2CLK_ENA,
82 aif << WM8958_DSP2CLK_SRC_SHIFT |
83 WM8958_DSP2CLK_ENA);
84
85 snd_soc_update_bits(codec, WM8958_DSP2_PROGRAM,
86 WM8958_DSP2_ENA, WM8958_DSP2_ENA);
87
88 /* If we've got user supplied MBC settings use them */
89 if (pdata && pdata->num_mbc_cfgs) {
90 struct wm8958_mbc_cfg *cfg
91 = &pdata->mbc_cfgs[wm8994->mbc_cfg];
92
93 for (i = 0; i < ARRAY_SIZE(cfg->coeff_regs); i++)
94 snd_soc_write(codec, i + WM8958_MBC_BAND_1_K_1,
95 cfg->coeff_regs[i]);
96
97 for (i = 0; i < ARRAY_SIZE(cfg->cutoff_regs); i++)
98 snd_soc_write(codec,
99 i + WM8958_MBC_BAND_2_LOWER_CUTOFF_C1_1,
100 cfg->cutoff_regs[i]);
101 }
102
103 /* Run the DSP */
104 snd_soc_write(codec, WM8958_DSP2_EXECCONTROL,
105 WM8958_DSP2_RUNR);
106
107 /* And we're off! */
108 snd_soc_update_bits(codec, WM8958_DSP2_CONFIG,
109 WM8958_MBC_ENA | WM8958_MBC_SEL_MASK,
110 mbc << WM8958_MBC_SEL_SHIFT |
111 WM8958_MBC_ENA);
112 } else {
113 /* If the DSP is already stopped then noop */
114 if (!(reg & WM8958_DSP2_ENA))
115 return;
116
117 snd_soc_update_bits(codec, WM8958_DSP2_CONFIG,
118 WM8958_MBC_ENA, 0);
119 snd_soc_update_bits(codec, WM8958_DSP2_PROGRAM,
120 WM8958_DSP2_ENA, 0);
121 snd_soc_update_bits(codec, WM8994_CLOCKING_1,
122 WM8958_DSP2CLK_ENA, 0);
123 }
124}
125
126int wm8958_aif_ev(struct snd_soc_dapm_widget *w,
127 struct snd_kcontrol *kcontrol, int event)
128{
129 struct snd_soc_codec *codec = w->codec;
Mark Brownc6b7b572011-03-11 18:13:12 +0000130 int i;
Mark Brownf701a2e2011-03-09 19:31:01 +0000131
132 switch (event) {
133 case SND_SOC_DAPM_POST_PMU:
Mark Brownc6b7b572011-03-11 18:13:12 +0000134 case SND_SOC_DAPM_PRE_PMU:
135 for (i = 0; i < 3; i++)
136 wm8958_mbc_apply(codec, i, 1);
Mark Brownf701a2e2011-03-09 19:31:01 +0000137 break;
138 case SND_SOC_DAPM_POST_PMD:
Mark Brownc6b7b572011-03-11 18:13:12 +0000139 case SND_SOC_DAPM_PRE_PMD:
140 for (i = 0; i < 3; i++)
141 wm8958_mbc_apply(codec, i, 0);
Mark Brownf701a2e2011-03-09 19:31:01 +0000142 break;
143 }
144
145 return 0;
146}
147
148static int wm8958_put_mbc_enum(struct snd_kcontrol *kcontrol,
149 struct snd_ctl_elem_value *ucontrol)
150{
151 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
152 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
153 struct wm8994_pdata *pdata = wm8994->pdata;
154 int value = ucontrol->value.integer.value[0];
155 int reg;
156
157 /* Don't allow on the fly reconfiguration */
158 reg = snd_soc_read(codec, WM8994_CLOCKING_1);
159 if (reg < 0 || reg & WM8958_DSP2CLK_ENA)
160 return -EBUSY;
161
162 if (value >= pdata->num_mbc_cfgs)
163 return -EINVAL;
164
165 wm8994->mbc_cfg = value;
166
167 return 0;
168}
169
170static int wm8958_get_mbc_enum(struct snd_kcontrol *kcontrol,
171 struct snd_ctl_elem_value *ucontrol)
172{
173 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
174 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
175
176 ucontrol->value.enumerated.item[0] = wm8994->mbc_cfg;
177
178 return 0;
179}
180
181static int wm8958_mbc_info(struct snd_kcontrol *kcontrol,
182 struct snd_ctl_elem_info *uinfo)
183{
184 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
185 uinfo->count = 1;
186 uinfo->value.integer.min = 0;
187 uinfo->value.integer.max = 1;
188 return 0;
189}
190
191static int wm8958_mbc_get(struct snd_kcontrol *kcontrol,
192 struct snd_ctl_elem_value *ucontrol)
193{
194 int mbc = kcontrol->private_value;
195 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
196 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
197
198 ucontrol->value.integer.value[0] = wm8994->mbc_ena[mbc];
199
200 return 0;
201}
202
203static int wm8958_mbc_put(struct snd_kcontrol *kcontrol,
204 struct snd_ctl_elem_value *ucontrol)
205{
206 int mbc = kcontrol->private_value;
207 int i;
208 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
209 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
210
211 if (ucontrol->value.integer.value[0] > 1)
212 return -EINVAL;
213
214 for (i = 0; i < ARRAY_SIZE(wm8994->mbc_ena); i++) {
215 if (mbc != i && wm8994->mbc_ena[i]) {
216 dev_dbg(codec->dev, "MBC %d active already\n", mbc);
217 return -EBUSY;
218 }
219 }
220
221 wm8994->mbc_ena[mbc] = ucontrol->value.integer.value[0];
222
223 wm8958_mbc_apply(codec, mbc, wm8994->mbc_ena[mbc]);
224
225 return 0;
226}
227
228#define WM8958_MBC_SWITCH(xname, xval) {\
229 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), \
230 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,\
231 .info = wm8958_mbc_info, \
232 .get = wm8958_mbc_get, .put = wm8958_mbc_put, \
233 .private_value = xval }
234
235static const struct snd_kcontrol_new wm8958_mbc_snd_controls[] = {
236WM8958_MBC_SWITCH("AIF1DAC1 MBC Switch", 0),
237WM8958_MBC_SWITCH("AIF1DAC2 MBC Switch", 1),
238WM8958_MBC_SWITCH("AIF2DAC MBC Switch", 2),
239};
240
241void wm8958_dsp2_init(struct snd_soc_codec *codec)
242{
243 struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
244 struct wm8994_pdata *pdata = wm8994->pdata;
245 int ret, i;
246
247 snd_soc_add_controls(codec, wm8958_mbc_snd_controls,
248 ARRAY_SIZE(wm8958_mbc_snd_controls));
249
250 if (!pdata)
251 return;
252
253 if (pdata->num_mbc_cfgs) {
254 struct snd_kcontrol_new control[] = {
255 SOC_ENUM_EXT("MBC Mode", wm8994->mbc_enum,
256 wm8958_get_mbc_enum, wm8958_put_mbc_enum),
257 };
258
259 /* We need an array of texts for the enum API */
260 wm8994->mbc_texts = kmalloc(sizeof(char *)
261 * pdata->num_mbc_cfgs, GFP_KERNEL);
262 if (!wm8994->mbc_texts) {
263 dev_err(wm8994->codec->dev,
264 "Failed to allocate %d MBC config texts\n",
265 pdata->num_mbc_cfgs);
266 return;
267 }
268
269 for (i = 0; i < pdata->num_mbc_cfgs; i++)
270 wm8994->mbc_texts[i] = pdata->mbc_cfgs[i].name;
271
272 wm8994->mbc_enum.max = pdata->num_mbc_cfgs;
273 wm8994->mbc_enum.texts = wm8994->mbc_texts;
274
275 ret = snd_soc_add_controls(wm8994->codec, control, 1);
276 if (ret != 0)
277 dev_err(wm8994->codec->dev,
278 "Failed to add MBC mode controls: %d\n", ret);
279 }
280
281
282}