blob: 056daa0010f99238ce4edc1f34d0a6bfe959f655 [file] [log] [blame]
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +00001/*
2 * wm8940.c -- WM8940 ALSA Soc Audio driver
3 *
4 * Author: Jonathan Cameron <jic23@cam.ac.uk>
5 *
6 * Based on wm8510.c
7 * Copyright 2006 Wolfson Microelectronics PLC.
8 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 * Not currently handled:
15 * Notch filter control
16 * AUXMode (inverting vs mixer)
17 * No means to obtain current gain if alc enabled.
18 * No use made of gpio
19 * Fast VMID discharge for power down
20 * Soft Start
21 * DLR and ALR Swaps not enabled
22 * Digital Sidetone not supported
23 */
24#include <linux/module.h>
25#include <linux/moduleparam.h>
26#include <linux/kernel.h>
27#include <linux/init.h>
28#include <linux/delay.h>
29#include <linux/pm.h>
30#include <linux/i2c.h>
31#include <linux/platform_device.h>
32#include <linux/spi/spi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +000034#include <sound/core.h>
35#include <sound/pcm.h>
36#include <sound/pcm_params.h>
37#include <sound/soc.h>
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +000038#include <sound/initval.h>
39#include <sound/tlv.h>
40
41#include "wm8940.h"
42
43struct wm8940_priv {
44 unsigned int sysclk;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000045 enum snd_soc_control_type control_type;
46 void *control_data;
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +000047};
48
49static u16 wm8940_reg_defaults[] = {
50 0x8940, /* Soft Reset */
51 0x0000, /* Power 1 */
52 0x0000, /* Power 2 */
53 0x0000, /* Power 3 */
54 0x0010, /* Interface Control */
55 0x0000, /* Companding Control */
56 0x0140, /* Clock Control */
57 0x0000, /* Additional Controls */
58 0x0000, /* GPIO Control */
59 0x0002, /* Auto Increment Control */
60 0x0000, /* DAC Control */
61 0x00FF, /* DAC Volume */
62 0,
63 0,
64 0x0100, /* ADC Control */
65 0x00FF, /* ADC Volume */
66 0x0000, /* Notch Filter 1 Control 1 */
67 0x0000, /* Notch Filter 1 Control 2 */
68 0x0000, /* Notch Filter 2 Control 1 */
69 0x0000, /* Notch Filter 2 Control 2 */
70 0x0000, /* Notch Filter 3 Control 1 */
71 0x0000, /* Notch Filter 3 Control 2 */
72 0x0000, /* Notch Filter 4 Control 1 */
73 0x0000, /* Notch Filter 4 Control 2 */
74 0x0032, /* DAC Limit Control 1 */
75 0x0000, /* DAC Limit Control 2 */
76 0,
77 0,
78 0,
79 0,
80 0,
81 0,
82 0x0038, /* ALC Control 1 */
83 0x000B, /* ALC Control 2 */
84 0x0032, /* ALC Control 3 */
85 0x0000, /* Noise Gate */
86 0x0041, /* PLLN */
87 0x000C, /* PLLK1 */
88 0x0093, /* PLLK2 */
89 0x00E9, /* PLLK3 */
90 0,
91 0,
92 0x0030, /* ALC Control 4 */
93 0,
94 0x0002, /* Input Control */
95 0x0050, /* PGA Gain */
96 0,
97 0x0002, /* ADC Boost Control */
98 0,
99 0x0002, /* Output Control */
100 0x0000, /* Speaker Mixer Control */
101 0,
102 0,
103 0,
104 0x0079, /* Speaker Volume */
105 0,
106 0x0000, /* Mono Mixer Control */
107};
108
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000109static const char *wm8940_companding[] = { "Off", "NC", "u-law", "A-law" };
110static const struct soc_enum wm8940_adc_companding_enum
111= SOC_ENUM_SINGLE(WM8940_COMPANDINGCTL, 1, 4, wm8940_companding);
112static const struct soc_enum wm8940_dac_companding_enum
113= SOC_ENUM_SINGLE(WM8940_COMPANDINGCTL, 3, 4, wm8940_companding);
114
115static const char *wm8940_alc_mode_text[] = {"ALC", "Limiter"};
116static const struct soc_enum wm8940_alc_mode_enum
117= SOC_ENUM_SINGLE(WM8940_ALC3, 8, 2, wm8940_alc_mode_text);
118
119static const char *wm8940_mic_bias_level_text[] = {"0.9", "0.65"};
120static const struct soc_enum wm8940_mic_bias_level_enum
121= SOC_ENUM_SINGLE(WM8940_INPUTCTL, 8, 2, wm8940_mic_bias_level_text);
122
123static const char *wm8940_filter_mode_text[] = {"Audio", "Application"};
124static const struct soc_enum wm8940_filter_mode_enum
125= SOC_ENUM_SINGLE(WM8940_ADC, 7, 2, wm8940_filter_mode_text);
126
Mark Brown6be01cf2009-04-27 20:57:42 +0100127static DECLARE_TLV_DB_SCALE(wm8940_spk_vol_tlv, -5700, 100, 1);
128static DECLARE_TLV_DB_SCALE(wm8940_att_tlv, -1000, 1000, 0);
129static DECLARE_TLV_DB_SCALE(wm8940_pga_vol_tlv, -1200, 75, 0);
130static DECLARE_TLV_DB_SCALE(wm8940_alc_min_tlv, -1200, 600, 0);
131static DECLARE_TLV_DB_SCALE(wm8940_alc_max_tlv, 675, 600, 0);
132static DECLARE_TLV_DB_SCALE(wm8940_alc_tar_tlv, -2250, 50, 0);
133static DECLARE_TLV_DB_SCALE(wm8940_lim_boost_tlv, 0, 100, 0);
134static DECLARE_TLV_DB_SCALE(wm8940_lim_thresh_tlv, -600, 100, 0);
135static DECLARE_TLV_DB_SCALE(wm8940_adc_tlv, -12750, 50, 1);
136static DECLARE_TLV_DB_SCALE(wm8940_capture_boost_vol_tlv, 0, 2000, 0);
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000137
138static const struct snd_kcontrol_new wm8940_snd_controls[] = {
139 SOC_SINGLE("Digital Loopback Switch", WM8940_COMPANDINGCTL,
140 6, 1, 0),
141 SOC_ENUM("DAC Companding", wm8940_dac_companding_enum),
142 SOC_ENUM("ADC Companding", wm8940_adc_companding_enum),
143
144 SOC_ENUM("ALC Mode", wm8940_alc_mode_enum),
145 SOC_SINGLE("ALC Switch", WM8940_ALC1, 8, 1, 0),
146 SOC_SINGLE_TLV("ALC Capture Max Gain", WM8940_ALC1,
147 3, 7, 1, wm8940_alc_max_tlv),
148 SOC_SINGLE_TLV("ALC Capture Min Gain", WM8940_ALC1,
149 0, 7, 0, wm8940_alc_min_tlv),
150 SOC_SINGLE_TLV("ALC Capture Target", WM8940_ALC2,
151 0, 14, 0, wm8940_alc_tar_tlv),
152 SOC_SINGLE("ALC Capture Hold", WM8940_ALC2, 4, 10, 0),
153 SOC_SINGLE("ALC Capture Decay", WM8940_ALC3, 4, 10, 0),
154 SOC_SINGLE("ALC Capture Attach", WM8940_ALC3, 0, 10, 0),
155 SOC_SINGLE("ALC ZC Switch", WM8940_ALC4, 1, 1, 0),
156 SOC_SINGLE("ALC Capture Noise Gate Switch", WM8940_NOISEGATE,
157 3, 1, 0),
158 SOC_SINGLE("ALC Capture Noise Gate Threshold", WM8940_NOISEGATE,
159 0, 7, 0),
160
161 SOC_SINGLE("DAC Playback Limiter Switch", WM8940_DACLIM1, 8, 1, 0),
162 SOC_SINGLE("DAC Playback Limiter Attack", WM8940_DACLIM1, 0, 9, 0),
163 SOC_SINGLE("DAC Playback Limiter Decay", WM8940_DACLIM1, 4, 11, 0),
164 SOC_SINGLE_TLV("DAC Playback Limiter Threshold", WM8940_DACLIM2,
165 4, 9, 1, wm8940_lim_thresh_tlv),
166 SOC_SINGLE_TLV("DAC Playback Limiter Boost", WM8940_DACLIM2,
167 0, 12, 0, wm8940_lim_boost_tlv),
168
169 SOC_SINGLE("Capture PGA ZC Switch", WM8940_PGAGAIN, 7, 1, 0),
170 SOC_SINGLE_TLV("Capture PGA Volume", WM8940_PGAGAIN,
171 0, 63, 0, wm8940_pga_vol_tlv),
172 SOC_SINGLE_TLV("Digital Playback Volume", WM8940_DACVOL,
173 0, 255, 0, wm8940_adc_tlv),
174 SOC_SINGLE_TLV("Digital Capture Volume", WM8940_ADCVOL,
175 0, 255, 0, wm8940_adc_tlv),
176 SOC_ENUM("Mic Bias Level", wm8940_mic_bias_level_enum),
177 SOC_SINGLE_TLV("Capture Boost Volue", WM8940_ADCBOOST,
178 8, 1, 0, wm8940_capture_boost_vol_tlv),
179 SOC_SINGLE_TLV("Speaker Playback Volume", WM8940_SPKVOL,
180 0, 63, 0, wm8940_spk_vol_tlv),
181 SOC_SINGLE("Speaker Playback Switch", WM8940_SPKVOL, 6, 1, 1),
182
183 SOC_SINGLE_TLV("Speaker Mixer Line Bypass Volume", WM8940_SPKVOL,
184 8, 1, 1, wm8940_att_tlv),
185 SOC_SINGLE("Speaker Playback ZC Switch", WM8940_SPKVOL, 7, 1, 0),
186
187 SOC_SINGLE("Mono Out Switch", WM8940_MONOMIX, 6, 1, 1),
188 SOC_SINGLE_TLV("Mono Mixer Line Bypass Volume", WM8940_MONOMIX,
189 7, 1, 1, wm8940_att_tlv),
190
191 SOC_SINGLE("High Pass Filter Switch", WM8940_ADC, 8, 1, 0),
192 SOC_ENUM("High Pass Filter Mode", wm8940_filter_mode_enum),
193 SOC_SINGLE("High Pass Filter Cut Off", WM8940_ADC, 4, 7, 0),
194 SOC_SINGLE("ADC Inversion Switch", WM8940_ADC, 0, 1, 0),
195 SOC_SINGLE("DAC Inversion Switch", WM8940_DAC, 0, 1, 0),
196 SOC_SINGLE("DAC Auto Mute Switch", WM8940_DAC, 2, 1, 0),
197 SOC_SINGLE("ZC Timeout Clock Switch", WM8940_ADDCNTRL, 0, 1, 0),
198};
199
200static const struct snd_kcontrol_new wm8940_speaker_mixer_controls[] = {
201 SOC_DAPM_SINGLE("Line Bypass Switch", WM8940_SPKMIX, 1, 1, 0),
202 SOC_DAPM_SINGLE("Aux Playback Switch", WM8940_SPKMIX, 5, 1, 0),
203 SOC_DAPM_SINGLE("PCM Playback Switch", WM8940_SPKMIX, 0, 1, 0),
204};
205
206static const struct snd_kcontrol_new wm8940_mono_mixer_controls[] = {
207 SOC_DAPM_SINGLE("Line Bypass Switch", WM8940_MONOMIX, 1, 1, 0),
208 SOC_DAPM_SINGLE("Aux Playback Switch", WM8940_MONOMIX, 2, 1, 0),
209 SOC_DAPM_SINGLE("PCM Playback Switch", WM8940_MONOMIX, 0, 1, 0),
210};
211
Mark Brown6be01cf2009-04-27 20:57:42 +0100212static DECLARE_TLV_DB_SCALE(wm8940_boost_vol_tlv, -1500, 300, 1);
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000213static const struct snd_kcontrol_new wm8940_input_boost_controls[] = {
214 SOC_DAPM_SINGLE("Mic PGA Switch", WM8940_PGAGAIN, 6, 1, 1),
215 SOC_DAPM_SINGLE_TLV("Aux Volume", WM8940_ADCBOOST,
216 0, 7, 0, wm8940_boost_vol_tlv),
217 SOC_DAPM_SINGLE_TLV("Mic Volume", WM8940_ADCBOOST,
218 4, 7, 0, wm8940_boost_vol_tlv),
219};
220
221static const struct snd_kcontrol_new wm8940_micpga_controls[] = {
222 SOC_DAPM_SINGLE("AUX Switch", WM8940_INPUTCTL, 2, 1, 0),
223 SOC_DAPM_SINGLE("MICP Switch", WM8940_INPUTCTL, 0, 1, 0),
224 SOC_DAPM_SINGLE("MICN Switch", WM8940_INPUTCTL, 1, 1, 0),
225};
226
227static const struct snd_soc_dapm_widget wm8940_dapm_widgets[] = {
228 SND_SOC_DAPM_MIXER("Speaker Mixer", WM8940_POWER3, 2, 0,
229 &wm8940_speaker_mixer_controls[0],
230 ARRAY_SIZE(wm8940_speaker_mixer_controls)),
231 SND_SOC_DAPM_MIXER("Mono Mixer", WM8940_POWER3, 3, 0,
232 &wm8940_mono_mixer_controls[0],
233 ARRAY_SIZE(wm8940_mono_mixer_controls)),
234 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8940_POWER3, 0, 0),
235
236 SND_SOC_DAPM_PGA("SpkN Out", WM8940_POWER3, 5, 0, NULL, 0),
237 SND_SOC_DAPM_PGA("SpkP Out", WM8940_POWER3, 6, 0, NULL, 0),
238 SND_SOC_DAPM_PGA("Mono Out", WM8940_POWER3, 7, 0, NULL, 0),
239 SND_SOC_DAPM_OUTPUT("MONOOUT"),
240 SND_SOC_DAPM_OUTPUT("SPKOUTP"),
241 SND_SOC_DAPM_OUTPUT("SPKOUTN"),
242
243 SND_SOC_DAPM_PGA("Aux Input", WM8940_POWER1, 6, 0, NULL, 0),
244 SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8940_POWER2, 0, 0),
245 SND_SOC_DAPM_MIXER("Mic PGA", WM8940_POWER2, 2, 0,
246 &wm8940_micpga_controls[0],
247 ARRAY_SIZE(wm8940_micpga_controls)),
248 SND_SOC_DAPM_MIXER("Boost Mixer", WM8940_POWER2, 4, 0,
249 &wm8940_input_boost_controls[0],
250 ARRAY_SIZE(wm8940_input_boost_controls)),
251 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8940_POWER1, 4, 0),
252
253 SND_SOC_DAPM_INPUT("MICN"),
254 SND_SOC_DAPM_INPUT("MICP"),
255 SND_SOC_DAPM_INPUT("AUX"),
256};
257
258static const struct snd_soc_dapm_route audio_map[] = {
259 /* Mono output mixer */
260 {"Mono Mixer", "PCM Playback Switch", "DAC"},
261 {"Mono Mixer", "Aux Playback Switch", "Aux Input"},
262 {"Mono Mixer", "Line Bypass Switch", "Boost Mixer"},
263
264 /* Speaker output mixer */
265 {"Speaker Mixer", "PCM Playback Switch", "DAC"},
266 {"Speaker Mixer", "Aux Playback Switch", "Aux Input"},
267 {"Speaker Mixer", "Line Bypass Switch", "Boost Mixer"},
268
269 /* Outputs */
270 {"Mono Out", NULL, "Mono Mixer"},
271 {"MONOOUT", NULL, "Mono Out"},
272 {"SpkN Out", NULL, "Speaker Mixer"},
273 {"SpkP Out", NULL, "Speaker Mixer"},
274 {"SPKOUTN", NULL, "SpkN Out"},
275 {"SPKOUTP", NULL, "SpkP Out"},
276
277 /* Microphone PGA */
278 {"Mic PGA", "MICN Switch", "MICN"},
279 {"Mic PGA", "MICP Switch", "MICP"},
280 {"Mic PGA", "AUX Switch", "AUX"},
281
282 /* Boost Mixer */
283 {"Boost Mixer", "Mic PGA Switch", "Mic PGA"},
284 {"Boost Mixer", "Mic Volume", "MICP"},
285 {"Boost Mixer", "Aux Volume", "Aux Input"},
286
287 {"ADC", NULL, "Boost Mixer"},
288};
289
290static int wm8940_add_widgets(struct snd_soc_codec *codec)
291{
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200292 struct snd_soc_dapm_context *dapm = &codec->dapm;
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000293 int ret;
294
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200295 ret = snd_soc_dapm_new_controls(dapm, wm8940_dapm_widgets,
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000296 ARRAY_SIZE(wm8940_dapm_widgets));
297 if (ret)
298 goto error_ret;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200299 ret = snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000300
301error_ret:
302 return ret;
303}
304
Mark Brown8d50e442009-07-10 23:12:01 +0100305#define wm8940_reset(c) snd_soc_write(c, WM8940_SOFTRESET, 0);
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000306
307static int wm8940_set_dai_fmt(struct snd_soc_dai *codec_dai,
308 unsigned int fmt)
309{
310 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brown8d50e442009-07-10 23:12:01 +0100311 u16 iface = snd_soc_read(codec, WM8940_IFACE) & 0xFE67;
312 u16 clk = snd_soc_read(codec, WM8940_CLOCK) & 0x1fe;
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000313
314 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
315 case SND_SOC_DAIFMT_CBM_CFM:
316 clk |= 1;
317 break;
318 case SND_SOC_DAIFMT_CBS_CFS:
319 break;
320 default:
321 return -EINVAL;
322 }
Mark Brown8d50e442009-07-10 23:12:01 +0100323 snd_soc_write(codec, WM8940_CLOCK, clk);
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000324
325 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
326 case SND_SOC_DAIFMT_I2S:
327 iface |= (2 << 3);
328 break;
329 case SND_SOC_DAIFMT_LEFT_J:
330 iface |= (1 << 3);
331 break;
332 case SND_SOC_DAIFMT_RIGHT_J:
333 break;
334 case SND_SOC_DAIFMT_DSP_A:
335 iface |= (3 << 3);
336 break;
337 case SND_SOC_DAIFMT_DSP_B:
338 iface |= (3 << 3) | (1 << 7);
339 break;
340 }
341
342 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
343 case SND_SOC_DAIFMT_NB_NF:
344 break;
345 case SND_SOC_DAIFMT_NB_IF:
346 iface |= (1 << 7);
347 break;
348 case SND_SOC_DAIFMT_IB_NF:
349 iface |= (1 << 8);
350 break;
351 case SND_SOC_DAIFMT_IB_IF:
352 iface |= (1 << 8) | (1 << 7);
353 break;
354 }
355
Mark Brown8d50e442009-07-10 23:12:01 +0100356 snd_soc_write(codec, WM8940_IFACE, iface);
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000357
358 return 0;
359}
360
361static int wm8940_i2s_hw_params(struct snd_pcm_substream *substream,
362 struct snd_pcm_hw_params *params,
363 struct snd_soc_dai *dai)
364{
365 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000366 struct snd_soc_codec *codec = rtd->codec;
Mark Brown8d50e442009-07-10 23:12:01 +0100367 u16 iface = snd_soc_read(codec, WM8940_IFACE) & 0xFD9F;
368 u16 addcntrl = snd_soc_read(codec, WM8940_ADDCNTRL) & 0xFFF1;
369 u16 companding = snd_soc_read(codec,
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000370 WM8940_COMPANDINGCTL) & 0xFFDF;
371 int ret;
372
373 /* LoutR control */
374 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE
375 && params_channels(params) == 2)
376 iface |= (1 << 9);
377
378 switch (params_rate(params)) {
Guennadi Liakhovetskib3172f22009-12-24 01:13:51 +0100379 case 8000:
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000380 addcntrl |= (0x5 << 1);
381 break;
Guennadi Liakhovetskib3172f22009-12-24 01:13:51 +0100382 case 11025:
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000383 addcntrl |= (0x4 << 1);
384 break;
Guennadi Liakhovetskib3172f22009-12-24 01:13:51 +0100385 case 16000:
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000386 addcntrl |= (0x3 << 1);
387 break;
Guennadi Liakhovetskib3172f22009-12-24 01:13:51 +0100388 case 22050:
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000389 addcntrl |= (0x2 << 1);
390 break;
Guennadi Liakhovetskib3172f22009-12-24 01:13:51 +0100391 case 32000:
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000392 addcntrl |= (0x1 << 1);
393 break;
Guennadi Liakhovetskib3172f22009-12-24 01:13:51 +0100394 case 44100:
395 case 48000:
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000396 break;
397 }
Mark Brown8d50e442009-07-10 23:12:01 +0100398 ret = snd_soc_write(codec, WM8940_ADDCNTRL, addcntrl);
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000399 if (ret)
400 goto error_ret;
401
402 switch (params_format(params)) {
403 case SNDRV_PCM_FORMAT_S8:
404 companding = companding | (1 << 5);
405 break;
406 case SNDRV_PCM_FORMAT_S16_LE:
407 break;
408 case SNDRV_PCM_FORMAT_S20_3LE:
409 iface |= (1 << 5);
410 break;
411 case SNDRV_PCM_FORMAT_S24_LE:
412 iface |= (2 << 5);
413 break;
414 case SNDRV_PCM_FORMAT_S32_LE:
415 iface |= (3 << 5);
416 break;
417 }
Mark Brown8d50e442009-07-10 23:12:01 +0100418 ret = snd_soc_write(codec, WM8940_COMPANDINGCTL, companding);
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000419 if (ret)
420 goto error_ret;
Mark Brown8d50e442009-07-10 23:12:01 +0100421 ret = snd_soc_write(codec, WM8940_IFACE, iface);
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000422
423error_ret:
424 return ret;
425}
426
427static int wm8940_mute(struct snd_soc_dai *dai, int mute)
428{
429 struct snd_soc_codec *codec = dai->codec;
Mark Brown8d50e442009-07-10 23:12:01 +0100430 u16 mute_reg = snd_soc_read(codec, WM8940_DAC) & 0xffbf;
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000431
432 if (mute)
433 mute_reg |= 0x40;
434
Mark Brown8d50e442009-07-10 23:12:01 +0100435 return snd_soc_write(codec, WM8940_DAC, mute_reg);
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000436}
437
438static int wm8940_set_bias_level(struct snd_soc_codec *codec,
439 enum snd_soc_bias_level level)
440{
441 u16 val;
Mark Brown8d50e442009-07-10 23:12:01 +0100442 u16 pwr_reg = snd_soc_read(codec, WM8940_POWER1) & 0x1F0;
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000443 int ret = 0;
444
445 switch (level) {
446 case SND_SOC_BIAS_ON:
447 /* ensure bufioen and biasen */
448 pwr_reg |= (1 << 2) | (1 << 3);
449 /* Enable thermal shutdown */
Mark Brown8d50e442009-07-10 23:12:01 +0100450 val = snd_soc_read(codec, WM8940_OUTPUTCTL);
451 ret = snd_soc_write(codec, WM8940_OUTPUTCTL, val | 0x2);
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000452 if (ret)
453 break;
454 /* set vmid to 75k */
Mark Brown8d50e442009-07-10 23:12:01 +0100455 ret = snd_soc_write(codec, WM8940_POWER1, pwr_reg | 0x1);
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000456 break;
457 case SND_SOC_BIAS_PREPARE:
458 /* ensure bufioen and biasen */
459 pwr_reg |= (1 << 2) | (1 << 3);
Mark Brown8d50e442009-07-10 23:12:01 +0100460 ret = snd_soc_write(codec, WM8940_POWER1, pwr_reg | 0x1);
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000461 break;
462 case SND_SOC_BIAS_STANDBY:
463 /* ensure bufioen and biasen */
464 pwr_reg |= (1 << 2) | (1 << 3);
465 /* set vmid to 300k for standby */
Mark Brown8d50e442009-07-10 23:12:01 +0100466 ret = snd_soc_write(codec, WM8940_POWER1, pwr_reg | 0x2);
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000467 break;
468 case SND_SOC_BIAS_OFF:
Mark Brown8d50e442009-07-10 23:12:01 +0100469 ret = snd_soc_write(codec, WM8940_POWER1, pwr_reg);
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000470 break;
471 }
472
473 return ret;
474}
475
476struct pll_ {
477 unsigned int pre_scale:2;
478 unsigned int n:4;
479 unsigned int k;
480};
481
482static struct pll_ pll_div;
483
484/* The size in bits of the pll divide multiplied by 10
485 * to allow rounding later */
486#define FIXED_PLL_SIZE ((1 << 24) * 10)
487static void pll_factors(unsigned int target, unsigned int source)
488{
489 unsigned long long Kpart;
490 unsigned int K, Ndiv, Nmod;
491 /* The left shift ist to avoid accuracy loss when right shifting */
492 Ndiv = target / source;
493
494 if (Ndiv > 12) {
495 source <<= 1;
496 /* Multiply by 2 */
497 pll_div.pre_scale = 0;
498 Ndiv = target / source;
499 } else if (Ndiv < 3) {
500 source >>= 2;
501 /* Divide by 4 */
502 pll_div.pre_scale = 3;
503 Ndiv = target / source;
504 } else if (Ndiv < 6) {
505 source >>= 1;
506 /* divide by 2 */
507 pll_div.pre_scale = 2;
508 Ndiv = target / source;
509 } else
510 pll_div.pre_scale = 1;
511
512 if ((Ndiv < 6) || (Ndiv > 12))
513 printk(KERN_WARNING
514 "WM8940 N value %d outwith recommended range!d\n",
515 Ndiv);
516
517 pll_div.n = Ndiv;
518 Nmod = target % source;
519 Kpart = FIXED_PLL_SIZE * (long long)Nmod;
520
521 do_div(Kpart, source);
522
523 K = Kpart & 0xFFFFFFFF;
524
525 /* Check if we need to round */
526 if ((K % 10) >= 5)
527 K += 5;
528
529 /* Move down to proper range now rounding is done */
530 K /= 10;
531
532 pll_div.k = K;
533}
534
535/* Untested at the moment */
Mark Brown85488032009-09-05 18:52:16 +0100536static int wm8940_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
537 int source, unsigned int freq_in, unsigned int freq_out)
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000538{
539 struct snd_soc_codec *codec = codec_dai->codec;
540 u16 reg;
541
542 /* Turn off PLL */
Mark Brown8d50e442009-07-10 23:12:01 +0100543 reg = snd_soc_read(codec, WM8940_POWER1);
544 snd_soc_write(codec, WM8940_POWER1, reg & 0x1df);
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000545
546 if (freq_in == 0 || freq_out == 0) {
547 /* Clock CODEC directly from MCLK */
Mark Brown8d50e442009-07-10 23:12:01 +0100548 reg = snd_soc_read(codec, WM8940_CLOCK);
549 snd_soc_write(codec, WM8940_CLOCK, reg & 0x0ff);
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000550 /* Pll power down */
Mark Brown8d50e442009-07-10 23:12:01 +0100551 snd_soc_write(codec, WM8940_PLLN, (1 << 7));
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000552 return 0;
553 }
554
555 /* Pll is followed by a frequency divide by 4 */
556 pll_factors(freq_out*4, freq_in);
557 if (pll_div.k)
Mark Brown8d50e442009-07-10 23:12:01 +0100558 snd_soc_write(codec, WM8940_PLLN,
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000559 (pll_div.pre_scale << 4) | pll_div.n | (1 << 6));
560 else /* No factional component */
Mark Brown8d50e442009-07-10 23:12:01 +0100561 snd_soc_write(codec, WM8940_PLLN,
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000562 (pll_div.pre_scale << 4) | pll_div.n);
Mark Brown8d50e442009-07-10 23:12:01 +0100563 snd_soc_write(codec, WM8940_PLLK1, pll_div.k >> 18);
564 snd_soc_write(codec, WM8940_PLLK2, (pll_div.k >> 9) & 0x1ff);
565 snd_soc_write(codec, WM8940_PLLK3, pll_div.k & 0x1ff);
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000566 /* Enable the PLL */
Mark Brown8d50e442009-07-10 23:12:01 +0100567 reg = snd_soc_read(codec, WM8940_POWER1);
568 snd_soc_write(codec, WM8940_POWER1, reg | 0x020);
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000569
570 /* Run CODEC from PLL instead of MCLK */
Mark Brown8d50e442009-07-10 23:12:01 +0100571 reg = snd_soc_read(codec, WM8940_CLOCK);
572 snd_soc_write(codec, WM8940_CLOCK, reg | 0x100);
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000573
574 return 0;
575}
576
577static int wm8940_set_dai_sysclk(struct snd_soc_dai *codec_dai,
578 int clk_id, unsigned int freq, int dir)
579{
580 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900581 struct wm8940_priv *wm8940 = snd_soc_codec_get_drvdata(codec);
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000582
583 switch (freq) {
584 case 11289600:
585 case 12000000:
586 case 12288000:
587 case 16934400:
588 case 18432000:
589 wm8940->sysclk = freq;
590 return 0;
591 }
592 return -EINVAL;
593}
594
595static int wm8940_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
596 int div_id, int div)
597{
598 struct snd_soc_codec *codec = codec_dai->codec;
599 u16 reg;
600 int ret = 0;
601
602 switch (div_id) {
603 case WM8940_BCLKDIV:
Mark Brown8d50e442009-07-10 23:12:01 +0100604 reg = snd_soc_read(codec, WM8940_CLOCK) & 0xFFEF3;
605 ret = snd_soc_write(codec, WM8940_CLOCK, reg | (div << 2));
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000606 break;
607 case WM8940_MCLKDIV:
Mark Brown8d50e442009-07-10 23:12:01 +0100608 reg = snd_soc_read(codec, WM8940_CLOCK) & 0xFF1F;
609 ret = snd_soc_write(codec, WM8940_CLOCK, reg | (div << 5));
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000610 break;
611 case WM8940_OPCLKDIV:
Mark Brown8d50e442009-07-10 23:12:01 +0100612 reg = snd_soc_read(codec, WM8940_ADDCNTRL) & 0xFFCF;
613 ret = snd_soc_write(codec, WM8940_ADDCNTRL, reg | (div << 4));
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000614 break;
615 }
616 return ret;
617}
618
619#define WM8940_RATES SNDRV_PCM_RATE_8000_48000
620
621#define WM8940_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
622 SNDRV_PCM_FMTBIT_S16_LE | \
623 SNDRV_PCM_FMTBIT_S20_3LE | \
624 SNDRV_PCM_FMTBIT_S24_LE | \
625 SNDRV_PCM_FMTBIT_S32_LE)
626
627static struct snd_soc_dai_ops wm8940_dai_ops = {
628 .hw_params = wm8940_i2s_hw_params,
629 .set_sysclk = wm8940_set_dai_sysclk,
630 .digital_mute = wm8940_mute,
631 .set_fmt = wm8940_set_dai_fmt,
632 .set_clkdiv = wm8940_set_dai_clkdiv,
633 .set_pll = wm8940_set_dai_pll,
634};
635
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000636static struct snd_soc_dai_driver wm8940_dai = {
637 .name = "wm8940-hifi",
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000638 .playback = {
639 .stream_name = "Playback",
640 .channels_min = 1,
641 .channels_max = 2,
642 .rates = WM8940_RATES,
643 .formats = WM8940_FORMATS,
644 },
645 .capture = {
646 .stream_name = "Capture",
647 .channels_min = 1,
648 .channels_max = 2,
649 .rates = WM8940_RATES,
650 .formats = WM8940_FORMATS,
651 },
652 .ops = &wm8940_dai_ops,
653 .symmetric_rates = 1,
654};
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000655
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000656static int wm8940_suspend(struct snd_soc_codec *codec, pm_message_t state)
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000657{
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000658 return wm8940_set_bias_level(codec, SND_SOC_BIAS_OFF);
659}
660
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000661static int wm8940_resume(struct snd_soc_codec *codec)
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000662{
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000663 int i;
664 int ret;
665 u8 data[3];
666 u16 *cache = codec->reg_cache;
667
668 /* Sync reg_cache with the hardware
669 * Could use auto incremented writes to speed this up
670 */
671 for (i = 0; i < ARRAY_SIZE(wm8940_reg_defaults); i++) {
672 data[0] = i;
673 data[1] = (cache[i] & 0xFF00) >> 8;
674 data[2] = cache[i] & 0x00FF;
675 ret = codec->hw_write(codec->control_data, data, 3);
676 if (ret < 0)
677 goto error_ret;
678 else if (ret != 3) {
679 ret = -EIO;
680 goto error_ret;
681 }
682 }
683 ret = wm8940_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000684
685error_ret:
686 return ret;
687}
688
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000689static int wm8940_probe(struct snd_soc_codec *codec)
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000690{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000691 struct wm8940_priv *wm8940 = snd_soc_codec_get_drvdata(codec);
692 struct wm8940_setup_data *pdata = codec->dev->platform_data;
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000693 int ret;
694 u16 reg;
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000695
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000696 codec->control_data = wm8940->control_data;
697 ret = snd_soc_codec_set_cache_io(codec, 8, 16, wm8940->control_type);
Jonathan Camerone655a432009-10-02 16:09:49 +0100698 if (ret < 0) {
Mark Brown8d50e442009-07-10 23:12:01 +0100699 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
700 return ret;
701 }
702
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000703 ret = wm8940_reset(codec);
704 if (ret < 0) {
705 dev_err(codec->dev, "Failed to issue reset\n");
706 return ret;
707 }
708
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000709 wm8940_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
710
Mark Brown8d50e442009-07-10 23:12:01 +0100711 ret = snd_soc_write(codec, WM8940_POWER1, 0x180);
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000712 if (ret < 0)
713 return ret;
714
715 if (!pdata)
716 dev_warn(codec->dev, "No platform data supplied\n");
717 else {
Mark Brown8d50e442009-07-10 23:12:01 +0100718 reg = snd_soc_read(codec, WM8940_OUTPUTCTL);
719 ret = snd_soc_write(codec, WM8940_OUTPUTCTL, reg | pdata->vroi);
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000720 if (ret < 0)
721 return ret;
722 }
723
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000724 ret = snd_soc_add_controls(codec, wm8940_snd_controls,
725 ARRAY_SIZE(wm8940_snd_controls));
726 if (ret)
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000727 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000728 ret = wm8940_add_widgets(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000729 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000730}
731
732static int wm8940_remove(struct snd_soc_codec *codec)
733{
734 wm8940_set_bias_level(codec, SND_SOC_BIAS_OFF);
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000735 return 0;
736}
737
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000738static struct snd_soc_codec_driver soc_codec_dev_wm8940 = {
739 .probe = wm8940_probe,
740 .remove = wm8940_remove,
741 .suspend = wm8940_suspend,
742 .resume = wm8940_resume,
743 .set_bias_level = wm8940_set_bias_level,
Dimitris Papastamose5eec342010-09-10 18:14:56 +0100744 .reg_cache_size = ARRAY_SIZE(wm8940_reg_defaults),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000745 .reg_word_size = sizeof(u16),
746 .reg_cache_default = wm8940_reg_defaults,
747};
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000748
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000749#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
750static __devinit int wm8940_i2c_probe(struct i2c_client *i2c,
751 const struct i2c_device_id *id)
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000752{
753 struct wm8940_priv *wm8940;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000754 int ret;
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000755
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000756 wm8940 = kzalloc(sizeof(struct wm8940_priv), GFP_KERNEL);
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000757 if (wm8940 == NULL)
758 return -ENOMEM;
759
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000760 i2c_set_clientdata(i2c, wm8940);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000761 wm8940->control_data = i2c;
Lars-Peter Clausen7f984b52010-12-28 21:08:57 +0100762 wm8940->control_type = SND_SOC_I2C;
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000763
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000764 ret = snd_soc_register_codec(&i2c->dev,
765 &soc_codec_dev_wm8940, &wm8940_dai, 1);
Axel Lindb1e18d2010-07-23 05:53:49 +0000766 if (ret < 0)
767 kfree(wm8940);
Axel Lindb1e18d2010-07-23 05:53:49 +0000768 return ret;
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000769}
770
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000771static __devexit int wm8940_i2c_remove(struct i2c_client *client)
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000772{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000773 snd_soc_unregister_codec(&client->dev);
774 kfree(i2c_get_clientdata(client));
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000775 return 0;
776}
777
778static const struct i2c_device_id wm8940_i2c_id[] = {
779 { "wm8940", 0 },
780 { }
781};
782MODULE_DEVICE_TABLE(i2c, wm8940_i2c_id);
783
784static struct i2c_driver wm8940_i2c_driver = {
785 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000786 .name = "wm8940-codec",
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000787 .owner = THIS_MODULE,
788 },
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000789 .probe = wm8940_i2c_probe,
790 .remove = __devexit_p(wm8940_i2c_remove),
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000791 .id_table = wm8940_i2c_id,
792};
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000793#endif
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000794
795static int __init wm8940_modinit(void)
796{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000797 int ret = 0;
798#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000799 ret = i2c_add_driver(&wm8940_i2c_driver);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000800 if (ret != 0) {
801 printk(KERN_ERR "Failed to register wm8940 I2C driver: %d\n",
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000802 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000803 }
804#endif
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000805 return ret;
806}
807module_init(wm8940_modinit);
808
809static void __exit wm8940_exit(void)
810{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000811#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000812 i2c_del_driver(&wm8940_i2c_driver);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000813#endif
Jonathan Cameron0b5e92c52009-04-27 13:49:44 +0000814}
815module_exit(wm8940_exit);
816
817MODULE_DESCRIPTION("ASoC WM8940 driver");
818MODULE_AUTHOR("Jonathan Cameron");
819MODULE_LICENSE("GPL");