treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 441
[linux-block.git] / sound / soc / codecs / wm9705.c
CommitLineData
b886d83c 1// SPDX-License-Identifier: GPL-2.0-only
2aceefef
IM
2/*
3 * wm9705.c -- ALSA Soc WM9705 codec support
4 *
5 * Copyright 2008 Ian Molton <spyro@f2s.com>
2aceefef
IM
6 */
7
8#include <linux/init.h>
5a0e3ad6 9#include <linux/slab.h>
aaafcfed 10#include <linux/mfd/wm97xx.h>
2aceefef
IM
11#include <linux/module.h>
12#include <linux/kernel.h>
13#include <linux/device.h>
206964e8 14#include <linux/regmap.h>
2aceefef
IM
15#include <sound/core.h>
16#include <sound/pcm.h>
17#include <sound/ac97_codec.h>
aaafcfed
RJ
18#include <sound/ac97/codec.h>
19#include <sound/ac97/compat.h>
2aceefef
IM
20#include <sound/initval.h>
21#include <sound/soc.h>
2aceefef 22
6e0b73a0
LPC
23#define WM9705_VENDOR_ID 0x574d4c05
24#define WM9705_VENDOR_ID_MASK 0xffffffff
25
c6e46e52
RJ
26struct wm9705_priv {
27 struct snd_ac97 *ac97;
aaafcfed 28 struct wm97xx_platform_data *mfd_pdata;
c6e46e52
RJ
29};
30
206964e8
LPC
31static const struct reg_default wm9705_reg_defaults[] = {
32 { 0x02, 0x8000 },
33 { 0x04, 0x8000 },
34 { 0x06, 0x8000 },
35 { 0x0a, 0x8000 },
36 { 0x0c, 0x8008 },
37 { 0x0e, 0x8008 },
38 { 0x10, 0x8808 },
39 { 0x12, 0x8808 },
40 { 0x14, 0x8808 },
41 { 0x16, 0x8808 },
42 { 0x18, 0x8808 },
43 { 0x1a, 0x0000 },
44 { 0x1c, 0x8000 },
45 { 0x20, 0x0000 },
46 { 0x22, 0x0000 },
47 { 0x26, 0x000f },
48 { 0x28, 0x0605 },
49 { 0x2a, 0x0000 },
50 { 0x2c, 0xbb80 },
51 { 0x32, 0xbb80 },
52 { 0x34, 0x2000 },
53 { 0x5a, 0x0000 },
54 { 0x5c, 0x0000 },
55 { 0x72, 0x0808 },
56 { 0x74, 0x0000 },
57 { 0x76, 0x0006 },
58 { 0x78, 0x0000 },
59 { 0x7a, 0x0000 },
60};
61
62static const struct regmap_config wm9705_regmap_config = {
63 .reg_bits = 16,
64 .reg_stride = 2,
65 .val_bits = 16,
66 .max_register = 0x7e,
67 .cache_type = REGCACHE_RBTREE,
68
69 .volatile_reg = regmap_ac97_default_volatile,
70
71 .reg_defaults = wm9705_reg_defaults,
72 .num_reg_defaults = ARRAY_SIZE(wm9705_reg_defaults),
2aceefef
IM
73};
74
75static const struct snd_kcontrol_new wm9705_snd_ac97_controls[] = {
76 SOC_DOUBLE("Master Playback Volume", AC97_MASTER, 8, 0, 31, 1),
77 SOC_SINGLE("Master Playback Switch", AC97_MASTER, 15, 1, 1),
78 SOC_DOUBLE("Headphone Playback Volume", AC97_HEADPHONE, 8, 0, 31, 1),
79 SOC_SINGLE("Headphone Playback Switch", AC97_HEADPHONE, 15, 1, 1),
80 SOC_DOUBLE("PCM Playback Volume", AC97_PCM, 8, 0, 31, 1),
81 SOC_SINGLE("PCM Playback Switch", AC97_PCM, 15, 1, 1),
82 SOC_SINGLE("Mono Playback Volume", AC97_MASTER_MONO, 0, 31, 1),
83 SOC_SINGLE("Mono Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
84 SOC_SINGLE("PCBeep Playback Volume", AC97_PC_BEEP, 1, 15, 1),
85 SOC_SINGLE("Phone Playback Volume", AC97_PHONE, 0, 31, 1),
86 SOC_DOUBLE("Line Playback Volume", AC97_LINE, 8, 0, 31, 1),
87 SOC_DOUBLE("CD Playback Volume", AC97_CD, 8, 0, 31, 1),
88 SOC_SINGLE("Mic Playback Volume", AC97_MIC, 0, 31, 1),
89 SOC_SINGLE("Mic 20dB Boost Switch", AC97_MIC, 6, 1, 0),
927b0aea
IM
90 SOC_DOUBLE("Capture Volume", AC97_REC_GAIN, 8, 0, 15, 0),
91 SOC_SINGLE("Capture Switch", AC97_REC_GAIN, 15, 1, 1),
2aceefef
IM
92};
93
94static const char *wm9705_mic[] = {"Mic 1", "Mic 2"};
95static const char *wm9705_rec_sel[] = {"Mic", "CD", "NC", "NC",
96 "Line", "Stereo Mix", "Mono Mix", "Phone"};
97
09981dcb
TI
98static SOC_ENUM_SINGLE_DECL(wm9705_enum_mic,
99 AC97_GENERAL_PURPOSE, 8, wm9705_mic);
100static SOC_ENUM_SINGLE_DECL(wm9705_enum_rec_l,
101 AC97_REC_SEL, 8, wm9705_rec_sel);
102static SOC_ENUM_SINGLE_DECL(wm9705_enum_rec_r,
103 AC97_REC_SEL, 0, wm9705_rec_sel);
2aceefef
IM
104
105/* Headphone Mixer */
106static const struct snd_kcontrol_new wm9705_hp_mixer_controls[] = {
107 SOC_DAPM_SINGLE("PCBeep Playback Switch", AC97_PC_BEEP, 15, 1, 1),
108 SOC_DAPM_SINGLE("CD Playback Switch", AC97_CD, 15, 1, 1),
109 SOC_DAPM_SINGLE("Mic Playback Switch", AC97_MIC, 15, 1, 1),
110 SOC_DAPM_SINGLE("Phone Playback Switch", AC97_PHONE, 15, 1, 1),
111 SOC_DAPM_SINGLE("Line Playback Switch", AC97_LINE, 15, 1, 1),
112};
113
114/* Mic source */
115static const struct snd_kcontrol_new wm9705_mic_src_controls =
116 SOC_DAPM_ENUM("Route", wm9705_enum_mic);
117
118/* Capture source */
119static const struct snd_kcontrol_new wm9705_capture_selectl_controls =
120 SOC_DAPM_ENUM("Route", wm9705_enum_rec_l);
121static const struct snd_kcontrol_new wm9705_capture_selectr_controls =
122 SOC_DAPM_ENUM("Route", wm9705_enum_rec_r);
123
124/* DAPM widgets */
125static const struct snd_soc_dapm_widget wm9705_dapm_widgets[] = {
126 SND_SOC_DAPM_MUX("Mic Source", SND_SOC_NOPM, 0, 0,
127 &wm9705_mic_src_controls),
128 SND_SOC_DAPM_MUX("Left Capture Source", SND_SOC_NOPM, 0, 0,
129 &wm9705_capture_selectl_controls),
130 SND_SOC_DAPM_MUX("Right Capture Source", SND_SOC_NOPM, 0, 0,
131 &wm9705_capture_selectr_controls),
132 SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback",
133 SND_SOC_NOPM, 0, 0),
134 SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback",
135 SND_SOC_NOPM, 0, 0),
136 SND_SOC_DAPM_MIXER_NAMED_CTL("HP Mixer", SND_SOC_NOPM, 0, 0,
137 &wm9705_hp_mixer_controls[0],
138 ARRAY_SIZE(wm9705_hp_mixer_controls)),
139 SND_SOC_DAPM_MIXER("Mono Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
140 SND_SOC_DAPM_ADC("Left ADC", "Left HiFi Capture", SND_SOC_NOPM, 0, 0),
141 SND_SOC_DAPM_ADC("Right ADC", "Right HiFi Capture", SND_SOC_NOPM, 0, 0),
142 SND_SOC_DAPM_PGA("Headphone PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
143 SND_SOC_DAPM_PGA("Speaker PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
144 SND_SOC_DAPM_PGA("Line PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
145 SND_SOC_DAPM_PGA("Line out PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
146 SND_SOC_DAPM_PGA("Mono PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
147 SND_SOC_DAPM_PGA("Phone PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
148 SND_SOC_DAPM_PGA("Mic PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
149 SND_SOC_DAPM_PGA("PCBEEP PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
150 SND_SOC_DAPM_PGA("CD PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
151 SND_SOC_DAPM_PGA("ADC PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
152 SND_SOC_DAPM_OUTPUT("HPOUTL"),
153 SND_SOC_DAPM_OUTPUT("HPOUTR"),
154 SND_SOC_DAPM_OUTPUT("LOUT"),
155 SND_SOC_DAPM_OUTPUT("ROUT"),
156 SND_SOC_DAPM_OUTPUT("MONOOUT"),
157 SND_SOC_DAPM_INPUT("PHONE"),
158 SND_SOC_DAPM_INPUT("LINEINL"),
159 SND_SOC_DAPM_INPUT("LINEINR"),
160 SND_SOC_DAPM_INPUT("CDINL"),
161 SND_SOC_DAPM_INPUT("CDINR"),
162 SND_SOC_DAPM_INPUT("PCBEEP"),
163 SND_SOC_DAPM_INPUT("MIC1"),
164 SND_SOC_DAPM_INPUT("MIC2"),
165};
166
167/* Audio map
168 * WM9705 has no switches to disable the route from the inputs to the HP mixer
169 * so in order to prevent active inputs from forcing the audio outputs to be
170 * constantly enabled, we use the mutes on those inputs to simulate such
171 * controls.
172 */
97a58d6e 173static const struct snd_soc_dapm_route wm9705_audio_map[] = {
2aceefef
IM
174 /* HP mixer */
175 {"HP Mixer", "PCBeep Playback Switch", "PCBEEP PGA"},
176 {"HP Mixer", "CD Playback Switch", "CD PGA"},
177 {"HP Mixer", "Mic Playback Switch", "Mic PGA"},
178 {"HP Mixer", "Phone Playback Switch", "Phone PGA"},
179 {"HP Mixer", "Line Playback Switch", "Line PGA"},
180 {"HP Mixer", NULL, "Left DAC"},
181 {"HP Mixer", NULL, "Right DAC"},
182
183 /* mono mixer */
184 {"Mono Mixer", NULL, "HP Mixer"},
185
186 /* outputs */
187 {"Headphone PGA", NULL, "HP Mixer"},
188 {"HPOUTL", NULL, "Headphone PGA"},
189 {"HPOUTR", NULL, "Headphone PGA"},
190 {"Line out PGA", NULL, "HP Mixer"},
191 {"LOUT", NULL, "Line out PGA"},
192 {"ROUT", NULL, "Line out PGA"},
193 {"Mono PGA", NULL, "Mono Mixer"},
194 {"MONOOUT", NULL, "Mono PGA"},
195
196 /* inputs */
197 {"CD PGA", NULL, "CDINL"},
198 {"CD PGA", NULL, "CDINR"},
199 {"Line PGA", NULL, "LINEINL"},
200 {"Line PGA", NULL, "LINEINR"},
201 {"Phone PGA", NULL, "PHONE"},
202 {"Mic Source", "Mic 1", "MIC1"},
203 {"Mic Source", "Mic 2", "MIC2"},
204 {"Mic PGA", NULL, "Mic Source"},
205 {"PCBEEP PGA", NULL, "PCBEEP"},
206
207 /* Left capture selector */
208 {"Left Capture Source", "Mic", "Mic Source"},
209 {"Left Capture Source", "CD", "CDINL"},
210 {"Left Capture Source", "Line", "LINEINL"},
211 {"Left Capture Source", "Stereo Mix", "HP Mixer"},
212 {"Left Capture Source", "Mono Mix", "HP Mixer"},
213 {"Left Capture Source", "Phone", "PHONE"},
214
215 /* Right capture source */
216 {"Right Capture Source", "Mic", "Mic Source"},
217 {"Right Capture Source", "CD", "CDINR"},
218 {"Right Capture Source", "Line", "LINEINR"},
219 {"Right Capture Source", "Stereo Mix", "HP Mixer"},
220 {"Right Capture Source", "Mono Mix", "HP Mixer"},
221 {"Right Capture Source", "Phone", "PHONE"},
222
223 {"ADC PGA", NULL, "Left Capture Source"},
224 {"ADC PGA", NULL, "Right Capture Source"},
225
226 /* ADC's */
227 {"Left ADC", NULL, "ADC PGA"},
228 {"Right ADC", NULL, "ADC PGA"},
229};
230
2aceefef
IM
231static int ac97_prepare(struct snd_pcm_substream *substream,
232 struct snd_soc_dai *dai)
233{
28f89849 234 struct snd_soc_component *component = dai->component;
2aceefef 235 int reg;
2aceefef 236
28f89849 237 snd_soc_component_update_bits(component, AC97_EXTENDED_STATUS, 0x1, 0x1);
2aceefef
IM
238
239 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
240 reg = AC97_PCM_FRONT_DAC_RATE;
241 else
242 reg = AC97_PCM_LR_ADC_RATE;
243
28f89849 244 return snd_soc_component_write(component, reg, substream->runtime->rate);
2aceefef
IM
245}
246
247#define WM9705_AC97_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | \
248 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
249 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
250 SNDRV_PCM_RATE_48000)
251
85e7652d 252static const struct snd_soc_dai_ops wm9705_dai_ops = {
6335d055
EM
253 .prepare = ac97_prepare,
254};
255
f0fba2ad 256static struct snd_soc_dai_driver wm9705_dai[] = {
2aceefef 257 {
f0fba2ad 258 .name = "wm9705-hifi",
2aceefef
IM
259 .playback = {
260 .stream_name = "HiFi Playback",
261 .channels_min = 1,
262 .channels_max = 2,
263 .rates = WM9705_AC97_RATES,
33f503c9 264 .formats = SND_SOC_STD_AC97_FMTS,
2aceefef
IM
265 },
266 .capture = {
267 .stream_name = "HiFi Capture",
268 .channels_min = 1,
269 .channels_max = 2,
270 .rates = WM9705_AC97_RATES,
33f503c9 271 .formats = SND_SOC_STD_AC97_FMTS,
2aceefef 272 },
6335d055 273 .ops = &wm9705_dai_ops,
2aceefef
IM
274 },
275 {
f0fba2ad 276 .name = "wm9705-aux",
2aceefef
IM
277 .playback = {
278 .stream_name = "Aux Playback",
279 .channels_min = 1,
280 .channels_max = 1,
281 .rates = WM9705_AC97_RATES,
282 .formats = SNDRV_PCM_FMTBIT_S16_LE,
283 },
284 }
285};
2aceefef 286
0a11b168 287#ifdef CONFIG_PM
28f89849 288static int wm9705_soc_suspend(struct snd_soc_component *component)
0a11b168 289{
28f89849
KM
290 regcache_cache_bypass(component->regmap, true);
291 snd_soc_component_write(component, AC97_POWERDOWN, 0xffff);
292 regcache_cache_bypass(component->regmap, false);
0a11b168
MB
293
294 return 0;
295}
296
28f89849 297static int wm9705_soc_resume(struct snd_soc_component *component)
0a11b168 298{
28f89849 299 struct wm9705_priv *wm9705 = snd_soc_component_get_drvdata(component);
206964e8 300 int ret;
0a11b168 301
c6e46e52 302 ret = snd_ac97_reset(wm9705->ac97, true, WM9705_VENDOR_ID,
6e0b73a0 303 WM9705_VENDOR_ID_MASK);
9cf766f6 304 if (ret < 0)
0a11b168 305 return ret;
0a11b168 306
28f89849 307 snd_soc_component_cache_sync(component);
0a11b168
MB
308
309 return 0;
310}
311#else
312#define wm9705_soc_suspend NULL
313#define wm9705_soc_resume NULL
314#endif
315
28f89849 316static int wm9705_soc_probe(struct snd_soc_component *component)
2aceefef 317{
28f89849 318 struct wm9705_priv *wm9705 = snd_soc_component_get_drvdata(component);
206964e8 319 struct regmap *regmap;
2aceefef 320
aaafcfed
RJ
321 if (wm9705->mfd_pdata) {
322 wm9705->ac97 = wm9705->mfd_pdata->ac97;
323 regmap = wm9705->mfd_pdata->regmap;
576ce407 324 } else if (IS_ENABLED(CONFIG_SND_SOC_AC97_BUS)) {
28f89849 325 wm9705->ac97 = snd_soc_new_ac97_component(component, WM9705_VENDOR_ID,
aaafcfed
RJ
326 WM9705_VENDOR_ID_MASK);
327 if (IS_ERR(wm9705->ac97)) {
28f89849 328 dev_err(component->dev, "Failed to register AC97 codec\n");
aaafcfed
RJ
329 return PTR_ERR(wm9705->ac97);
330 }
331
332 regmap = regmap_init_ac97(wm9705->ac97, &wm9705_regmap_config);
333 if (IS_ERR(regmap)) {
28f89849 334 snd_soc_free_ac97_component(wm9705->ac97);
6bd25aae 335 return PTR_ERR(regmap);
aaafcfed 336 }
576ce407
AB
337 } else {
338 return -ENXIO;
206964e8
LPC
339 }
340
28f89849
KM
341 snd_soc_component_set_drvdata(component, wm9705->ac97);
342 snd_soc_component_init_regmap(component, regmap);
2aceefef 343
2aceefef 344 return 0;
2aceefef
IM
345}
346
28f89849 347static void wm9705_soc_remove(struct snd_soc_component *component)
2aceefef 348{
28f89849 349 struct wm9705_priv *wm9705 = snd_soc_component_get_drvdata(component);
358a8bb5 350
576ce407 351 if (IS_ENABLED(CONFIG_SND_SOC_AC97_BUS) && !wm9705->mfd_pdata) {
28f89849
KM
352 snd_soc_component_exit_regmap(component);
353 snd_soc_free_ac97_component(wm9705->ac97);
aaafcfed 354 }
2aceefef
IM
355}
356
28f89849
KM
357static const struct snd_soc_component_driver soc_component_dev_wm9705 = {
358 .probe = wm9705_soc_probe,
359 .remove = wm9705_soc_remove,
360 .suspend = wm9705_soc_suspend,
361 .resume = wm9705_soc_resume,
362 .controls = wm9705_snd_ac97_controls,
363 .num_controls = ARRAY_SIZE(wm9705_snd_ac97_controls),
364 .dapm_widgets = wm9705_dapm_widgets,
365 .num_dapm_widgets = ARRAY_SIZE(wm9705_dapm_widgets),
366 .dapm_routes = wm9705_audio_map,
367 .num_dapm_routes = ARRAY_SIZE(wm9705_audio_map),
368 .idle_bias_on = 1,
369 .use_pmdown_time = 1,
370 .endianness = 1,
371 .non_legacy_dai_naming = 1,
f0fba2ad
LG
372};
373
7a79e94e 374static int wm9705_probe(struct platform_device *pdev)
f0fba2ad 375{
c6e46e52
RJ
376 struct wm9705_priv *wm9705;
377
378 wm9705 = devm_kzalloc(&pdev->dev, sizeof(*wm9705), GFP_KERNEL);
379 if (wm9705 == NULL)
380 return -ENOMEM;
381
aaafcfed 382 wm9705->mfd_pdata = dev_get_platdata(&pdev->dev);
c6e46e52
RJ
383 platform_set_drvdata(pdev, wm9705);
384
28f89849
KM
385 return devm_snd_soc_register_component(&pdev->dev,
386 &soc_component_dev_wm9705, wm9705_dai, ARRAY_SIZE(wm9705_dai));
f0fba2ad
LG
387}
388
389static struct platform_driver wm9705_codec_driver = {
390 .driver = {
391 .name = "wm9705-codec",
f0fba2ad
LG
392 },
393
394 .probe = wm9705_probe,
2aceefef 395};
f0fba2ad 396
5bbcc3c0 397module_platform_driver(wm9705_codec_driver);
2aceefef
IM
398
399MODULE_DESCRIPTION("ASoC WM9705 driver");
400MODULE_AUTHOR("Ian Molton");
401MODULE_LICENSE("GPL v2");