blob: 76a5c7b05dfbfdde7647d92af0f112816efb17f5 [file] [log] [blame]
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001/*
2 * wm8753.c -- WM8753 ALSA Soc Audio driver
3 *
4 * Copyright 2003 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood
6 * liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
13 * Notes:
14 * The WM8753 is a low power, high quality stereo codec with integrated PCM
15 * codec designed for portable digital telephony applications.
16 *
17 * Dual DAI:-
18 *
19 * This driver support 2 DAI PCM's. This makes the default PCM available for
20 * HiFi audio (e.g. MP3, ogg) playback/capture and the other PCM available for
21 * voice.
22 *
23 * Please note that the voice PCM can be connected directly to a Bluetooth
24 * codec or GSM modem and thus cannot be read or written to, although it is
25 * available to be configured with snd_hw_params(), etc and kcontrols in the
26 * normal alsa manner.
27 *
28 * Fast DAI switching:-
29 *
30 * The driver can now fast switch between the DAI configurations via a
31 * an alsa kcontrol. This allows the PCM to remain open.
32 *
33 */
34
35#include <linux/module.h>
36#include <linux/moduleparam.h>
37#include <linux/version.h>
38#include <linux/kernel.h>
39#include <linux/init.h>
40#include <linux/delay.h>
41#include <linux/pm.h>
42#include <linux/i2c.h>
43#include <linux/platform_device.h>
Liam Girdwood1f53aee2007-04-16 19:17:44 +020044#include <sound/core.h>
45#include <sound/pcm.h>
46#include <sound/pcm_params.h>
47#include <sound/soc.h>
48#include <sound/soc-dapm.h>
49#include <sound/initval.h>
Liam Girdwood2d6a4ac2008-01-10 14:43:48 +010050#include <sound/tlv.h>
Liam Girdwood1f53aee2007-04-16 19:17:44 +020051#include <asm/div64.h>
52
53#include "wm8753.h"
54
55#define AUDIO_NAME "wm8753"
56#define WM8753_VERSION "0.16"
57
58/*
59 * Debug
60 */
61
62#define WM8753_DEBUG 0
63
64#ifdef WM8753_DEBUG
65#define dbg(format, arg...) \
66 printk(KERN_DEBUG AUDIO_NAME ": " format "\n" , ## arg)
67#else
68#define dbg(format, arg...) do {} while (0)
69#endif
70#define err(format, arg...) \
71 printk(KERN_ERR AUDIO_NAME ": " format "\n" , ## arg)
72#define info(format, arg...) \
73 printk(KERN_INFO AUDIO_NAME ": " format "\n" , ## arg)
74#define warn(format, arg...) \
75 printk(KERN_WARNING AUDIO_NAME ": " format "\n" , ## arg)
76
77static int caps_charge = 2000;
78module_param(caps_charge, int, 0);
79MODULE_PARM_DESC(caps_charge, "WM8753 cap charge time (msecs)");
80
81static void wm8753_set_dai_mode(struct snd_soc_codec *codec,
82 unsigned int mode);
83
84/* codec private data */
85struct wm8753_priv {
86 unsigned int sysclk;
87 unsigned int pcmclk;
88};
89
90/*
91 * wm8753 register cache
92 * We can't read the WM8753 register space when we
93 * are using 2 wire for device control, so we cache them instead.
94 */
95static const u16 wm8753_reg[] = {
96 0x0008, 0x0000, 0x000a, 0x000a,
97 0x0033, 0x0000, 0x0007, 0x00ff,
98 0x00ff, 0x000f, 0x000f, 0x007b,
99 0x0000, 0x0032, 0x0000, 0x00c3,
100 0x00c3, 0x00c0, 0x0000, 0x0000,
101 0x0000, 0x0000, 0x0000, 0x0000,
102 0x0000, 0x0000, 0x0000, 0x0000,
103 0x0000, 0x0000, 0x0000, 0x0055,
104 0x0005, 0x0050, 0x0055, 0x0050,
105 0x0055, 0x0050, 0x0055, 0x0079,
106 0x0079, 0x0079, 0x0079, 0x0079,
107 0x0000, 0x0000, 0x0000, 0x0000,
108 0x0097, 0x0097, 0x0000, 0x0004,
109 0x0000, 0x0083, 0x0024, 0x01ba,
110 0x0000, 0x0083, 0x0024, 0x01ba,
111 0x0000, 0x0000
112};
113
114/*
115 * read wm8753 register cache
116 */
117static inline unsigned int wm8753_read_reg_cache(struct snd_soc_codec *codec,
118 unsigned int reg)
119{
120 u16 *cache = codec->reg_cache;
121 if (reg < 1 || reg > (ARRAY_SIZE(wm8753_reg) + 1))
122 return -1;
123 return cache[reg - 1];
124}
125
126/*
127 * write wm8753 register cache
128 */
129static inline void wm8753_write_reg_cache(struct snd_soc_codec *codec,
130 unsigned int reg, unsigned int value)
131{
132 u16 *cache = codec->reg_cache;
133 if (reg < 1 || reg > 0x3f)
134 return;
135 cache[reg - 1] = value;
136}
137
138/*
139 * write to the WM8753 register space
140 */
141static int wm8753_write(struct snd_soc_codec *codec, unsigned int reg,
142 unsigned int value)
143{
144 u8 data[2];
145
146 /* data is
147 * D15..D9 WM8753 register offset
148 * D8...D0 register data
149 */
150 data[0] = (reg << 1) | ((value >> 8) & 0x0001);
151 data[1] = value & 0x00ff;
152
153 wm8753_write_reg_cache (codec, reg, value);
154 if (codec->hw_write(codec->control_data, data, 2) == 2)
155 return 0;
156 else
157 return -EIO;
158}
159
160#define wm8753_reset(c) wm8753_write(c, WM8753_RESET, 0)
161
162/*
163 * WM8753 Controls
164 */
165static const char *wm8753_base[] = {"Linear Control", "Adaptive Boost"};
166static const char *wm8753_base_filter[] =
167 {"130Hz @ 48kHz", "200Hz @ 48kHz", "100Hz @ 16kHz", "400Hz @ 48kHz",
168 "100Hz @ 8kHz", "200Hz @ 8kHz"};
169static const char *wm8753_treble[] = {"8kHz", "4kHz"};
170static const char *wm8753_alc_func[] = {"Off", "Right", "Left", "Stereo"};
171static const char *wm8753_ng_type[] = {"Constant PGA Gain", "Mute ADC Output"};
172static const char *wm8753_3d_func[] = {"Capture", "Playback"};
173static const char *wm8753_3d_uc[] = {"2.2kHz", "1.5kHz"};
174static const char *wm8753_3d_lc[] = {"200Hz", "500Hz"};
175static const char *wm8753_deemp[] = {"None", "32kHz", "44.1kHz", "48kHz"};
176static const char *wm8753_mono_mix[] = {"Stereo", "Left", "Right", "Mono"};
177static const char *wm8753_dac_phase[] = {"Non Inverted", "Inverted"};
178static const char *wm8753_line_mix[] = {"Line 1 + 2", "Line 1 - 2",
179 "Line 1", "Line 2"};
180static const char *wm8753_mono_mux[] = {"Line Mix", "Rx Mix"};
181static const char *wm8753_right_mux[] = {"Line 2", "Rx Mix"};
182static const char *wm8753_left_mux[] = {"Line 1", "Rx Mix"};
183static const char *wm8753_rxmsel[] = {"RXP - RXN", "RXP + RXN", "RXP", "RXN"};
184static const char *wm8753_sidetone_mux[] = {"Left PGA", "Mic 1", "Mic 2",
185 "Right PGA"};
186static const char *wm8753_mono2_src[] = {"Inverted Mono 1", "Left", "Right",
187 "Left + Right"};
188static const char *wm8753_out3[] = {"VREF", "ROUT2", "Left + Right"};
189static const char *wm8753_out4[] = {"VREF", "Capture ST", "LOUT2"};
190static const char *wm8753_radcsel[] = {"PGA", "Line or RXP-RXN", "Sidetone"};
191static const char *wm8753_ladcsel[] = {"PGA", "Line or RXP-RXN", "Line"};
192static const char *wm8753_mono_adc[] = {"Stereo", "Analogue Mix Left",
193 "Analogue Mix Right", "Digital Mono Mix"};
194static const char *wm8753_adc_hp[] = {"3.4Hz @ 48kHz", "82Hz @ 16k",
195 "82Hz @ 8kHz", "170Hz @ 8kHz"};
196static const char *wm8753_adc_filter[] = {"HiFi", "Voice"};
197static const char *wm8753_mic_sel[] = {"Mic 1", "Mic 2", "Mic 3"};
198static const char *wm8753_dai_mode[] = {"DAI 0", "DAI 1", "DAI 2", "DAI 3"};
199static const char *wm8753_dat_sel[] = {"Stereo", "Left ADC", "Right ADC",
200 "Channel Swap"};
Graeme Gregoryae092c92008-03-03 17:19:45 +0100201static const char *wm8753_rout2_phase[] = {"Non Inverted", "Inverted"};
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200202
203static const struct soc_enum wm8753_enum[] = {
204SOC_ENUM_SINGLE(WM8753_BASS, 7, 2, wm8753_base),
205SOC_ENUM_SINGLE(WM8753_BASS, 4, 6, wm8753_base_filter),
206SOC_ENUM_SINGLE(WM8753_TREBLE, 6, 2, wm8753_treble),
207SOC_ENUM_SINGLE(WM8753_ALC1, 7, 4, wm8753_alc_func),
208SOC_ENUM_SINGLE(WM8753_NGATE, 1, 2, wm8753_ng_type),
209SOC_ENUM_SINGLE(WM8753_3D, 7, 2, wm8753_3d_func),
210SOC_ENUM_SINGLE(WM8753_3D, 6, 2, wm8753_3d_uc),
211SOC_ENUM_SINGLE(WM8753_3D, 5, 2, wm8753_3d_lc),
212SOC_ENUM_SINGLE(WM8753_DAC, 1, 4, wm8753_deemp),
213SOC_ENUM_SINGLE(WM8753_DAC, 4, 4, wm8753_mono_mix),
214SOC_ENUM_SINGLE(WM8753_DAC, 6, 2, wm8753_dac_phase),
215SOC_ENUM_SINGLE(WM8753_INCTL1, 3, 4, wm8753_line_mix),
216SOC_ENUM_SINGLE(WM8753_INCTL1, 2, 2, wm8753_mono_mux),
217SOC_ENUM_SINGLE(WM8753_INCTL1, 1, 2, wm8753_right_mux),
218SOC_ENUM_SINGLE(WM8753_INCTL1, 0, 2, wm8753_left_mux),
219SOC_ENUM_SINGLE(WM8753_INCTL2, 6, 4, wm8753_rxmsel),
220SOC_ENUM_SINGLE(WM8753_INCTL2, 4, 4, wm8753_sidetone_mux),
221SOC_ENUM_SINGLE(WM8753_OUTCTL, 7, 4, wm8753_mono2_src),
222SOC_ENUM_SINGLE(WM8753_OUTCTL, 0, 3, wm8753_out3),
223SOC_ENUM_SINGLE(WM8753_ADCTL2, 7, 3, wm8753_out4),
224SOC_ENUM_SINGLE(WM8753_ADCIN, 2, 3, wm8753_radcsel),
225SOC_ENUM_SINGLE(WM8753_ADCIN, 0, 3, wm8753_ladcsel),
226SOC_ENUM_SINGLE(WM8753_ADCIN, 4, 4, wm8753_mono_adc),
227SOC_ENUM_SINGLE(WM8753_ADC, 2, 4, wm8753_adc_hp),
228SOC_ENUM_SINGLE(WM8753_ADC, 4, 2, wm8753_adc_filter),
229SOC_ENUM_SINGLE(WM8753_MICBIAS, 6, 3, wm8753_mic_sel),
230SOC_ENUM_SINGLE(WM8753_IOCTL, 2, 4, wm8753_dai_mode),
231SOC_ENUM_SINGLE(WM8753_ADC, 7, 4, wm8753_dat_sel),
Graeme Gregoryae092c92008-03-03 17:19:45 +0100232SOC_ENUM_SINGLE(WM8753_OUTCTL, 2, 2, wm8753_rout2_phase),
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200233};
234
235
236static int wm8753_get_dai(struct snd_kcontrol *kcontrol,
237 struct snd_ctl_elem_value *ucontrol)
238{
239 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
240 int mode = wm8753_read_reg_cache(codec, WM8753_IOCTL);
241
242 ucontrol->value.integer.value[0] = (mode & 0xc) >> 2;
243 return 0;
244}
245
246static int wm8753_set_dai(struct snd_kcontrol *kcontrol,
247 struct snd_ctl_elem_value *ucontrol)
248{
249 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
250 int mode = wm8753_read_reg_cache(codec, WM8753_IOCTL);
251
252 if (((mode &0xc) >> 2) == ucontrol->value.integer.value[0])
253 return 0;
254
255 mode &= 0xfff3;
256 mode |= (ucontrol->value.integer.value[0] << 2);
257
258 wm8753_write(codec, WM8753_IOCTL, mode);
259 wm8753_set_dai_mode(codec, ucontrol->value.integer.value[0]);
260 return 1;
261}
262
Liam Girdwood2d6a4ac2008-01-10 14:43:48 +0100263static const DECLARE_TLV_DB_LINEAR(rec_mix_tlv, -1500, 600);
264
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200265static const struct snd_kcontrol_new wm8753_snd_controls[] = {
266SOC_DOUBLE_R("PCM Volume", WM8753_LDAC, WM8753_RDAC, 0, 255, 0),
267
268SOC_DOUBLE_R("ADC Capture Volume", WM8753_LADC, WM8753_RADC, 0, 255, 0),
269
270SOC_DOUBLE_R("Headphone Playback Volume", WM8753_LOUT1V, WM8753_ROUT1V, 0, 127, 0),
271SOC_DOUBLE_R("Speaker Playback Volume", WM8753_LOUT2V, WM8753_ROUT2V, 0, 127, 0),
272
273SOC_SINGLE("Mono Playback Volume", WM8753_MOUTV, 0, 127, 0),
274
275SOC_DOUBLE_R("Bypass Playback Volume", WM8753_LOUTM1, WM8753_ROUTM1, 4, 7, 1),
276SOC_DOUBLE_R("Sidetone Playback Volume", WM8753_LOUTM2, WM8753_ROUTM2, 4, 7, 1),
277SOC_DOUBLE_R("Voice Playback Volume", WM8753_LOUTM2, WM8753_ROUTM2, 0, 7, 1),
278
279SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8753_LOUT1V, WM8753_ROUT1V, 7, 1, 0),
280SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8753_LOUT2V, WM8753_ROUT2V, 7, 1, 0),
281
282SOC_SINGLE("Mono Bypass Playback Volume", WM8753_MOUTM1, 4, 7, 1),
283SOC_SINGLE("Mono Sidetone Playback Volume", WM8753_MOUTM2, 4, 7, 1),
Mike Montour10264602008-02-01 13:12:12 +0100284SOC_SINGLE("Mono Voice Playback Volume", WM8753_MOUTM2, 0, 7, 1),
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200285SOC_SINGLE("Mono Playback ZC Switch", WM8753_MOUTV, 7, 1, 0),
286
287SOC_ENUM("Bass Boost", wm8753_enum[0]),
288SOC_ENUM("Bass Filter", wm8753_enum[1]),
289SOC_SINGLE("Bass Volume", WM8753_BASS, 0, 15, 1),
290
291SOC_SINGLE("Treble Volume", WM8753_TREBLE, 0, 15, 1),
292SOC_ENUM("Treble Cut-off", wm8753_enum[2]),
293
Liam Girdwood2d6a4ac2008-01-10 14:43:48 +0100294SOC_DOUBLE_TLV("Sidetone Capture Volume", WM8753_RECMIX1, 0, 4, 7, 1, rec_mix_tlv),
295SOC_SINGLE_TLV("Voice Sidetone Capture Volume", WM8753_RECMIX2, 0, 7, 1, rec_mix_tlv),
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200296
297SOC_DOUBLE_R("Capture Volume", WM8753_LINVOL, WM8753_RINVOL, 0, 63, 0),
298SOC_DOUBLE_R("Capture ZC Switch", WM8753_LINVOL, WM8753_RINVOL, 6, 1, 0),
299SOC_DOUBLE_R("Capture Switch", WM8753_LINVOL, WM8753_RINVOL, 7, 1, 1),
300
301SOC_ENUM("Capture Filter Select", wm8753_enum[23]),
302SOC_ENUM("Capture Filter Cut-off", wm8753_enum[24]),
303SOC_SINGLE("Capture Filter Switch", WM8753_ADC, 0, 1, 1),
304
305SOC_SINGLE("ALC Capture Target Volume", WM8753_ALC1, 0, 7, 0),
306SOC_SINGLE("ALC Capture Max Volume", WM8753_ALC1, 4, 7, 0),
307SOC_ENUM("ALC Capture Function", wm8753_enum[3]),
308SOC_SINGLE("ALC Capture ZC Switch", WM8753_ALC2, 8, 1, 0),
309SOC_SINGLE("ALC Capture Hold Time", WM8753_ALC2, 0, 15, 1),
310SOC_SINGLE("ALC Capture Decay Time", WM8753_ALC3, 4, 15, 1),
311SOC_SINGLE("ALC Capture Attack Time", WM8753_ALC3, 0, 15, 0),
312SOC_SINGLE("ALC Capture NG Threshold", WM8753_NGATE, 3, 31, 0),
313SOC_ENUM("ALC Capture NG Type", wm8753_enum[4]),
314SOC_SINGLE("ALC Capture NG Switch", WM8753_NGATE, 0, 1, 0),
315
316SOC_ENUM("3D Function", wm8753_enum[5]),
317SOC_ENUM("3D Upper Cut-off", wm8753_enum[6]),
318SOC_ENUM("3D Lower Cut-off", wm8753_enum[7]),
319SOC_SINGLE("3D Volume", WM8753_3D, 1, 15, 0),
320SOC_SINGLE("3D Switch", WM8753_3D, 0, 1, 0),
321
322SOC_SINGLE("Capture 6dB Attenuate", WM8753_ADCTL1, 2, 1, 0),
323SOC_SINGLE("Playback 6dB Attenuate", WM8753_ADCTL1, 1, 1, 0),
324
325SOC_ENUM("De-emphasis", wm8753_enum[8]),
326SOC_ENUM("Playback Mono Mix", wm8753_enum[9]),
327SOC_ENUM("Playback Phase", wm8753_enum[10]),
328
329SOC_SINGLE("Mic2 Capture Volume", WM8753_INCTL1, 7, 3, 0),
330SOC_SINGLE("Mic1 Capture Volume", WM8753_INCTL1, 5, 3, 0),
331
332SOC_ENUM_EXT("DAI Mode", wm8753_enum[26], wm8753_get_dai, wm8753_set_dai),
333
334SOC_ENUM("ADC Data Select", wm8753_enum[27]),
Graeme Gregoryae092c92008-03-03 17:19:45 +0100335SOC_ENUM("ROUT2 Phase", wm8753_enum[28]),
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200336};
337
338/* add non dapm controls */
339static int wm8753_add_controls(struct snd_soc_codec *codec)
340{
341 int err, i;
342
343 for (i = 0; i < ARRAY_SIZE(wm8753_snd_controls); i++) {
344 err = snd_ctl_add(codec->card,
345 snd_soc_cnew(&wm8753_snd_controls[i],codec, NULL));
346 if (err < 0)
347 return err;
348 }
349 return 0;
350}
351
352/*
353 * _DAPM_ Controls
354 */
355
356/* Left Mixer */
357static const struct snd_kcontrol_new wm8753_left_mixer_controls[] = {
358SOC_DAPM_SINGLE("Voice Playback Switch", WM8753_LOUTM2, 8, 1, 0),
359SOC_DAPM_SINGLE("Sidetone Playback Switch", WM8753_LOUTM2, 7, 1, 0),
360SOC_DAPM_SINGLE("Left Playback Switch", WM8753_LOUTM1, 8, 1, 0),
361SOC_DAPM_SINGLE("Bypass Playback Switch", WM8753_LOUTM1, 7, 1, 0),
362};
363
364/* Right mixer */
365static const struct snd_kcontrol_new wm8753_right_mixer_controls[] = {
366SOC_DAPM_SINGLE("Voice Playback Switch", WM8753_ROUTM2, 8, 1, 0),
367SOC_DAPM_SINGLE("Sidetone Playback Switch", WM8753_ROUTM2, 7, 1, 0),
368SOC_DAPM_SINGLE("Right Playback Switch", WM8753_ROUTM1, 8, 1, 0),
369SOC_DAPM_SINGLE("Bypass Playback Switch", WM8753_ROUTM1, 7, 1, 0),
370};
371
372/* Mono mixer */
373static const struct snd_kcontrol_new wm8753_mono_mixer_controls[] = {
374SOC_DAPM_SINGLE("Left Playback Switch", WM8753_MOUTM1, 8, 1, 0),
375SOC_DAPM_SINGLE("Right Playback Switch", WM8753_MOUTM2, 8, 1, 0),
376SOC_DAPM_SINGLE("Voice Playback Switch", WM8753_MOUTM2, 3, 1, 0),
377SOC_DAPM_SINGLE("Sidetone Playback Switch", WM8753_MOUTM2, 7, 1, 0),
378SOC_DAPM_SINGLE("Bypass Playback Switch", WM8753_MOUTM1, 7, 1, 0),
379};
380
381/* Mono 2 Mux */
382static const struct snd_kcontrol_new wm8753_mono2_controls =
383SOC_DAPM_ENUM("Route", wm8753_enum[17]);
384
385/* Out 3 Mux */
386static const struct snd_kcontrol_new wm8753_out3_controls =
387SOC_DAPM_ENUM("Route", wm8753_enum[18]);
388
389/* Out 4 Mux */
390static const struct snd_kcontrol_new wm8753_out4_controls =
391SOC_DAPM_ENUM("Route", wm8753_enum[19]);
392
393/* ADC Mono Mix */
394static const struct snd_kcontrol_new wm8753_adc_mono_controls =
395SOC_DAPM_ENUM("Route", wm8753_enum[22]);
396
397/* Record mixer */
398static const struct snd_kcontrol_new wm8753_record_mixer_controls[] = {
399SOC_DAPM_SINGLE("Voice Capture Switch", WM8753_RECMIX2, 3, 1, 0),
400SOC_DAPM_SINGLE("Left Capture Switch", WM8753_RECMIX1, 3, 1, 0),
401SOC_DAPM_SINGLE("Right Capture Switch", WM8753_RECMIX1, 7, 1, 0),
402};
403
404/* Left ADC mux */
405static const struct snd_kcontrol_new wm8753_adc_left_controls =
406SOC_DAPM_ENUM("Route", wm8753_enum[21]);
407
408/* Right ADC mux */
409static const struct snd_kcontrol_new wm8753_adc_right_controls =
410SOC_DAPM_ENUM("Route", wm8753_enum[20]);
411
412/* MIC mux */
413static const struct snd_kcontrol_new wm8753_mic_mux_controls =
414SOC_DAPM_ENUM("Route", wm8753_enum[16]);
415
416/* ALC mixer */
417static const struct snd_kcontrol_new wm8753_alc_mixer_controls[] = {
418SOC_DAPM_SINGLE("Line Capture Switch", WM8753_INCTL2, 3, 1, 0),
419SOC_DAPM_SINGLE("Mic2 Capture Switch", WM8753_INCTL2, 2, 1, 0),
420SOC_DAPM_SINGLE("Mic1 Capture Switch", WM8753_INCTL2, 1, 1, 0),
421SOC_DAPM_SINGLE("Rx Capture Switch", WM8753_INCTL2, 0, 1, 0),
422};
423
424/* Left Line mux */
425static const struct snd_kcontrol_new wm8753_line_left_controls =
426SOC_DAPM_ENUM("Route", wm8753_enum[14]);
427
428/* Right Line mux */
429static const struct snd_kcontrol_new wm8753_line_right_controls =
430SOC_DAPM_ENUM("Route", wm8753_enum[13]);
431
432/* Mono Line mux */
433static const struct snd_kcontrol_new wm8753_line_mono_controls =
434SOC_DAPM_ENUM("Route", wm8753_enum[12]);
435
436/* Line mux and mixer */
437static const struct snd_kcontrol_new wm8753_line_mux_mix_controls =
438SOC_DAPM_ENUM("Route", wm8753_enum[11]);
439
440/* Rx mux and mixer */
441static const struct snd_kcontrol_new wm8753_rx_mux_mix_controls =
442SOC_DAPM_ENUM("Route", wm8753_enum[15]);
443
444/* Mic Selector Mux */
445static const struct snd_kcontrol_new wm8753_mic_sel_mux_controls =
446SOC_DAPM_ENUM("Route", wm8753_enum[25]);
447
448static const struct snd_soc_dapm_widget wm8753_dapm_widgets[] = {
449SND_SOC_DAPM_MICBIAS("Mic Bias", WM8753_PWR1, 5, 0),
450SND_SOC_DAPM_MIXER("Left Mixer", WM8753_PWR4, 0, 0,
451 &wm8753_left_mixer_controls[0], ARRAY_SIZE(wm8753_left_mixer_controls)),
452SND_SOC_DAPM_PGA("Left Out 1", WM8753_PWR3, 8, 0, NULL, 0),
453SND_SOC_DAPM_PGA("Left Out 2", WM8753_PWR3, 6, 0, NULL, 0),
454SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback", WM8753_PWR1, 3, 0),
455SND_SOC_DAPM_OUTPUT("LOUT1"),
456SND_SOC_DAPM_OUTPUT("LOUT2"),
457SND_SOC_DAPM_MIXER("Right Mixer", WM8753_PWR4, 1, 0,
458 &wm8753_right_mixer_controls[0], ARRAY_SIZE(wm8753_right_mixer_controls)),
459SND_SOC_DAPM_PGA("Right Out 1", WM8753_PWR3, 7, 0, NULL, 0),
460SND_SOC_DAPM_PGA("Right Out 2", WM8753_PWR3, 5, 0, NULL, 0),
461SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback", WM8753_PWR1, 2, 0),
462SND_SOC_DAPM_OUTPUT("ROUT1"),
463SND_SOC_DAPM_OUTPUT("ROUT2"),
464SND_SOC_DAPM_MIXER("Mono Mixer", WM8753_PWR4, 2, 0,
465 &wm8753_mono_mixer_controls[0], ARRAY_SIZE(wm8753_mono_mixer_controls)),
466SND_SOC_DAPM_PGA("Mono Out 1", WM8753_PWR3, 2, 0, NULL, 0),
467SND_SOC_DAPM_PGA("Mono Out 2", WM8753_PWR3, 1, 0, NULL, 0),
468SND_SOC_DAPM_DAC("Voice DAC", "Voice Playback", WM8753_PWR1, 4, 0),
469SND_SOC_DAPM_OUTPUT("MONO1"),
470SND_SOC_DAPM_MUX("Mono 2 Mux", SND_SOC_NOPM, 0, 0, &wm8753_mono2_controls),
471SND_SOC_DAPM_OUTPUT("MONO2"),
472SND_SOC_DAPM_MIXER("Out3 Left + Right", -1, 0, 0, NULL, 0),
473SND_SOC_DAPM_MUX("Out3 Mux", SND_SOC_NOPM, 0, 0, &wm8753_out3_controls),
474SND_SOC_DAPM_PGA("Out 3", WM8753_PWR3, 4, 0, NULL, 0),
475SND_SOC_DAPM_OUTPUT("OUT3"),
476SND_SOC_DAPM_MUX("Out4 Mux", SND_SOC_NOPM, 0, 0, &wm8753_out4_controls),
477SND_SOC_DAPM_PGA("Out 4", WM8753_PWR3, 3, 0, NULL, 0),
478SND_SOC_DAPM_OUTPUT("OUT4"),
479SND_SOC_DAPM_MIXER("Playback Mixer", WM8753_PWR4, 3, 0,
480 &wm8753_record_mixer_controls[0],
481 ARRAY_SIZE(wm8753_record_mixer_controls)),
482SND_SOC_DAPM_ADC("Left ADC", "Left Capture", WM8753_PWR2, 3, 0),
483SND_SOC_DAPM_ADC("Right ADC", "Right Capture", WM8753_PWR2, 2, 0),
484SND_SOC_DAPM_MUX("Capture Left Mixer", SND_SOC_NOPM, 0, 0,
485 &wm8753_adc_mono_controls),
486SND_SOC_DAPM_MUX("Capture Right Mixer", SND_SOC_NOPM, 0, 0,
487 &wm8753_adc_mono_controls),
488SND_SOC_DAPM_MUX("Capture Left Mux", SND_SOC_NOPM, 0, 0,
489 &wm8753_adc_left_controls),
490SND_SOC_DAPM_MUX("Capture Right Mux", SND_SOC_NOPM, 0, 0,
491 &wm8753_adc_right_controls),
492SND_SOC_DAPM_MUX("Mic Sidetone Mux", SND_SOC_NOPM, 0, 0,
493 &wm8753_mic_mux_controls),
494SND_SOC_DAPM_PGA("Left Capture Volume", WM8753_PWR2, 5, 0, NULL, 0),
495SND_SOC_DAPM_PGA("Right Capture Volume", WM8753_PWR2, 4, 0, NULL, 0),
496SND_SOC_DAPM_MIXER("ALC Mixer", WM8753_PWR2, 6, 0,
497 &wm8753_alc_mixer_controls[0], ARRAY_SIZE(wm8753_alc_mixer_controls)),
498SND_SOC_DAPM_MUX("Line Left Mux", SND_SOC_NOPM, 0, 0,
499 &wm8753_line_left_controls),
500SND_SOC_DAPM_MUX("Line Right Mux", SND_SOC_NOPM, 0, 0,
501 &wm8753_line_right_controls),
502SND_SOC_DAPM_MUX("Line Mono Mux", SND_SOC_NOPM, 0, 0,
503 &wm8753_line_mono_controls),
504SND_SOC_DAPM_MUX("Line Mixer", WM8753_PWR2, 0, 0,
505 &wm8753_line_mux_mix_controls),
506SND_SOC_DAPM_MUX("Rx Mixer", WM8753_PWR2, 1, 0,
507 &wm8753_rx_mux_mix_controls),
508SND_SOC_DAPM_PGA("Mic 1 Volume", WM8753_PWR2, 8, 0, NULL, 0),
509SND_SOC_DAPM_PGA("Mic 2 Volume", WM8753_PWR2, 7, 0, NULL, 0),
510SND_SOC_DAPM_MUX("Mic Selection Mux", SND_SOC_NOPM, 0, 0,
511 &wm8753_mic_sel_mux_controls),
512SND_SOC_DAPM_INPUT("LINE1"),
513SND_SOC_DAPM_INPUT("LINE2"),
514SND_SOC_DAPM_INPUT("RXP"),
515SND_SOC_DAPM_INPUT("RXN"),
516SND_SOC_DAPM_INPUT("ACIN"),
517SND_SOC_DAPM_OUTPUT("ACOP"),
518SND_SOC_DAPM_INPUT("MIC1N"),
519SND_SOC_DAPM_INPUT("MIC1"),
520SND_SOC_DAPM_INPUT("MIC2N"),
521SND_SOC_DAPM_INPUT("MIC2"),
522SND_SOC_DAPM_VMID("VREF"),
523};
524
525static const char *audio_map[][3] = {
526 /* left mixer */
527 {"Left Mixer", "Left Playback Switch", "Left DAC"},
528 {"Left Mixer", "Voice Playback Switch", "Voice DAC"},
529 {"Left Mixer", "Sidetone Playback Switch", "Mic Sidetone Mux"},
530 {"Left Mixer", "Bypass Playback Switch", "Line Left Mux"},
531
532 /* right mixer */
533 {"Right Mixer", "Right Playback Switch", "Right DAC"},
534 {"Right Mixer", "Voice Playback Switch", "Voice DAC"},
535 {"Right Mixer", "Sidetone Playback Switch", "Mic Sidetone Mux"},
536 {"Right Mixer", "Bypass Playback Switch", "Line Right Mux"},
537
538 /* mono mixer */
539 {"Mono Mixer", "Voice Playback Switch", "Voice DAC"},
540 {"Mono Mixer", "Left Playback Switch", "Left DAC"},
541 {"Mono Mixer", "Right Playback Switch", "Right DAC"},
542 {"Mono Mixer", "Sidetone Playback Switch", "Mic Sidetone Mux"},
543 {"Mono Mixer", "Bypass Playback Switch", "Line Mono Mux"},
544
545 /* left out */
546 {"Left Out 1", NULL, "Left Mixer"},
547 {"Left Out 2", NULL, "Left Mixer"},
548 {"LOUT1", NULL, "Left Out 1"},
549 {"LOUT2", NULL, "Left Out 2"},
550
551 /* right out */
552 {"Right Out 1", NULL, "Right Mixer"},
553 {"Right Out 2", NULL, "Right Mixer"},
554 {"ROUT1", NULL, "Right Out 1"},
555 {"ROUT2", NULL, "Right Out 2"},
556
557 /* mono 1 out */
558 {"Mono Out 1", NULL, "Mono Mixer"},
559 {"MONO1", NULL, "Mono Out 1"},
560
561 /* mono 2 out */
562 {"Mono 2 Mux", "Left + Right", "Out3 Left + Right"},
563 {"Mono 2 Mux", "Inverted Mono 1", "MONO1"},
564 {"Mono 2 Mux", "Left", "Left Mixer"},
565 {"Mono 2 Mux", "Right", "Right Mixer"},
566 {"Mono Out 2", NULL, "Mono 2 Mux"},
567 {"MONO2", NULL, "Mono Out 2"},
568
569 /* out 3 */
570 {"Out3 Left + Right", NULL, "Left Mixer"},
571 {"Out3 Left + Right", NULL, "Right Mixer"},
572 {"Out3 Mux", "VREF", "VREF"},
573 {"Out3 Mux", "Left + Right", "Out3 Left + Right"},
574 {"Out3 Mux", "ROUT2", "ROUT2"},
575 {"Out 3", NULL, "Out3 Mux"},
576 {"OUT3", NULL, "Out 3"},
577
578 /* out 4 */
579 {"Out4 Mux", "VREF", "VREF"},
580 {"Out4 Mux", "Capture ST", "Capture ST Mixer"},
581 {"Out4 Mux", "LOUT2", "LOUT2"},
582 {"Out 4", NULL, "Out4 Mux"},
583 {"OUT4", NULL, "Out 4"},
584
585 /* record mixer */
586 {"Playback Mixer", "Left Capture Switch", "Left Mixer"},
587 {"Playback Mixer", "Voice Capture Switch", "Mono Mixer"},
588 {"Playback Mixer", "Right Capture Switch", "Right Mixer"},
589
590 /* Mic/SideTone Mux */
591 {"Mic Sidetone Mux", "Left PGA", "Left Capture Volume"},
592 {"Mic Sidetone Mux", "Right PGA", "Right Capture Volume"},
593 {"Mic Sidetone Mux", "Mic 1", "Mic 1 Volume"},
594 {"Mic Sidetone Mux", "Mic 2", "Mic 2 Volume"},
595
596 /* Capture Left Mux */
597 {"Capture Left Mux", "PGA", "Left Capture Volume"},
598 {"Capture Left Mux", "Line or RXP-RXN", "Line Left Mux"},
599 {"Capture Left Mux", "Line", "LINE1"},
600
601 /* Capture Right Mux */
602 {"Capture Right Mux", "PGA", "Right Capture Volume"},
603 {"Capture Right Mux", "Line or RXP-RXN", "Line Right Mux"},
604 {"Capture Right Mux", "Sidetone", "Capture ST Mixer"},
605
606 /* Mono Capture mixer-mux */
607 {"Capture Right Mixer", "Stereo", "Capture Right Mux"},
608 {"Capture Left Mixer", "Analogue Mix Left", "Capture Left Mux"},
609 {"Capture Left Mixer", "Analogue Mix Left", "Capture Right Mux"},
610 {"Capture Right Mixer", "Analogue Mix Right", "Capture Left Mux"},
611 {"Capture Right Mixer", "Analogue Mix Right", "Capture Right Mux"},
612 {"Capture Left Mixer", "Digital Mono Mix", "Capture Left Mux"},
613 {"Capture Left Mixer", "Digital Mono Mix", "Capture Right Mux"},
614 {"Capture Right Mixer", "Digital Mono Mix", "Capture Left Mux"},
615 {"Capture Right Mixer", "Digital Mono Mix", "Capture Right Mux"},
616
617 /* ADC */
618 {"Left ADC", NULL, "Capture Left Mixer"},
619 {"Right ADC", NULL, "Capture Right Mixer"},
620
621 /* Left Capture Volume */
622 {"Left Capture Volume", NULL, "ACIN"},
623
624 /* Right Capture Volume */
625 {"Right Capture Volume", NULL, "Mic 2 Volume"},
626
627 /* ALC Mixer */
628 {"ALC Mixer", "Line Capture Switch", "Line Mixer"},
629 {"ALC Mixer", "Mic2 Capture Switch", "Mic 2 Volume"},
630 {"ALC Mixer", "Mic1 Capture Switch", "Mic 1 Volume"},
631 {"ALC Mixer", "Rx Capture Switch", "Rx Mixer"},
632
633 /* Line Left Mux */
634 {"Line Left Mux", "Line 1", "LINE1"},
635 {"Line Left Mux", "Rx Mix", "Rx Mixer"},
636
637 /* Line Right Mux */
638 {"Line Right Mux", "Line 2", "LINE2"},
639 {"Line Right Mux", "Rx Mix", "Rx Mixer"},
640
641 /* Line Mono Mux */
642 {"Line Mono Mux", "Line Mix", "Line Mixer"},
643 {"Line Mono Mux", "Rx Mix", "Rx Mixer"},
644
645 /* Line Mixer/Mux */
646 {"Line Mixer", "Line 1 + 2", "LINE1"},
647 {"Line Mixer", "Line 1 - 2", "LINE1"},
648 {"Line Mixer", "Line 1 + 2", "LINE2"},
649 {"Line Mixer", "Line 1 - 2", "LINE2"},
650 {"Line Mixer", "Line 1", "LINE1"},
651 {"Line Mixer", "Line 2", "LINE2"},
652
653 /* Rx Mixer/Mux */
654 {"Rx Mixer", "RXP - RXN", "RXP"},
655 {"Rx Mixer", "RXP + RXN", "RXP"},
656 {"Rx Mixer", "RXP - RXN", "RXN"},
657 {"Rx Mixer", "RXP + RXN", "RXN"},
658 {"Rx Mixer", "RXP", "RXP"},
659 {"Rx Mixer", "RXN", "RXN"},
660
661 /* Mic 1 Volume */
662 {"Mic 1 Volume", NULL, "MIC1N"},
663 {"Mic 1 Volume", NULL, "Mic Selection Mux"},
664
665 /* Mic 2 Volume */
666 {"Mic 2 Volume", NULL, "MIC2N"},
667 {"Mic 2 Volume", NULL, "MIC2"},
668
669 /* Mic Selector Mux */
670 {"Mic Selection Mux", "Mic 1", "MIC1"},
671 {"Mic Selection Mux", "Mic 2", "MIC2N"},
672 {"Mic Selection Mux", "Mic 3", "MIC2"},
673
674 /* ACOP */
675 {"ACOP", NULL, "ALC Mixer"},
676
677 /* terminator */
678 {NULL, NULL, NULL},
679};
680
681static int wm8753_add_widgets(struct snd_soc_codec *codec)
682{
683 int i;
684
685 for (i = 0; i < ARRAY_SIZE(wm8753_dapm_widgets); i++)
686 snd_soc_dapm_new_control(codec, &wm8753_dapm_widgets[i]);
687
688 /* set up the WM8753 audio map */
689 for (i = 0; audio_map[i][0] != NULL; i++) {
690 snd_soc_dapm_connect_input(codec, audio_map[i][0],
691 audio_map[i][1], audio_map[i][2]);
692 }
693
694 snd_soc_dapm_new_widgets(codec);
695 return 0;
696}
697
698/* PLL divisors */
699struct _pll_div {
700 u32 div2:1;
701 u32 n:4;
702 u32 k:24;
703};
704
705/* The size in bits of the pll divide multiplied by 10
706 * to allow rounding later */
707#define FIXED_PLL_SIZE ((1 << 22) * 10)
708
709static void pll_factors(struct _pll_div *pll_div, unsigned int target,
710 unsigned int source)
711{
712 u64 Kpart;
713 unsigned int K, Ndiv, Nmod;
714
715 Ndiv = target / source;
716 if (Ndiv < 6) {
717 source >>= 1;
718 pll_div->div2 = 1;
719 Ndiv = target / source;
720 } else
721 pll_div->div2 = 0;
722
723 if ((Ndiv < 6) || (Ndiv > 12))
724 printk(KERN_WARNING
725 "WM8753 N value outwith recommended range! N = %d\n",Ndiv);
726
727 pll_div->n = Ndiv;
728 Nmod = target % source;
729 Kpart = FIXED_PLL_SIZE * (long long)Nmod;
730
731 do_div(Kpart, source);
732
733 K = Kpart & 0xFFFFFFFF;
734
735 /* Check if we need to round */
736 if ((K % 10) >= 5)
737 K += 5;
738
739 /* Move down to proper range now rounding is done */
740 K /= 10;
741
742 pll_div->k = K;
743}
744
745static int wm8753_set_dai_pll(struct snd_soc_codec_dai *codec_dai,
746 int pll_id, unsigned int freq_in, unsigned int freq_out)
747{
748 u16 reg, enable;
749 int offset;
750 struct snd_soc_codec *codec = codec_dai->codec;
751
752 if (pll_id < WM8753_PLL1 || pll_id > WM8753_PLL2)
753 return -ENODEV;
754
755 if (pll_id == WM8753_PLL1) {
756 offset = 0;
757 enable = 0x10;
758 reg = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0xffef;
759 } else {
760 offset = 4;
761 enable = 0x8;
762 reg = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0xfff7;
763 }
764
765 if (!freq_in || !freq_out) {
766 /* disable PLL */
767 wm8753_write(codec, WM8753_PLL1CTL1 + offset, 0x0026);
768 wm8753_write(codec, WM8753_CLOCK, reg);
769 return 0;
770 } else {
771 u16 value = 0;
772 struct _pll_div pll_div;
773
774 pll_factors(&pll_div, freq_out * 8, freq_in);
775
776 /* set up N and K PLL divisor ratios */
777 /* bits 8:5 = PLL_N, bits 3:0 = PLL_K[21:18] */
778 value = (pll_div.n << 5) + ((pll_div.k & 0x3c0000) >> 18);
779 wm8753_write(codec, WM8753_PLL1CTL2 + offset, value);
780
781 /* bits 8:0 = PLL_K[17:9] */
782 value = (pll_div.k & 0x03fe00) >> 9;
783 wm8753_write(codec, WM8753_PLL1CTL3 + offset, value);
784
785 /* bits 8:0 = PLL_K[8:0] */
786 value = pll_div.k & 0x0001ff;
787 wm8753_write(codec, WM8753_PLL1CTL4 + offset, value);
788
789 /* set PLL as input and enable */
790 wm8753_write(codec, WM8753_PLL1CTL1 + offset, 0x0027 |
791 (pll_div.div2 << 3));
792 wm8753_write(codec, WM8753_CLOCK, reg | enable);
793 }
794 return 0;
795}
796
797struct _coeff_div {
798 u32 mclk;
799 u32 rate;
800 u8 sr:5;
801 u8 usb:1;
802};
803
804/* codec hifi mclk (after PLL) clock divider coefficients */
805static const struct _coeff_div coeff_div[] = {
806 /* 8k */
807 {12288000, 8000, 0x6, 0x0},
808 {11289600, 8000, 0x16, 0x0},
809 {18432000, 8000, 0x7, 0x0},
810 {16934400, 8000, 0x17, 0x0},
811 {12000000, 8000, 0x6, 0x1},
812
813 /* 11.025k */
814 {11289600, 11025, 0x18, 0x0},
815 {16934400, 11025, 0x19, 0x0},
816 {12000000, 11025, 0x19, 0x1},
817
818 /* 16k */
819 {12288000, 16000, 0xa, 0x0},
820 {18432000, 16000, 0xb, 0x0},
821 {12000000, 16000, 0xa, 0x1},
822
823 /* 22.05k */
824 {11289600, 22050, 0x1a, 0x0},
825 {16934400, 22050, 0x1b, 0x0},
826 {12000000, 22050, 0x1b, 0x1},
827
828 /* 32k */
829 {12288000, 32000, 0xc, 0x0},
830 {18432000, 32000, 0xd, 0x0},
831 {12000000, 32000, 0xa, 0x1},
832
833 /* 44.1k */
834 {11289600, 44100, 0x10, 0x0},
835 {16934400, 44100, 0x11, 0x0},
836 {12000000, 44100, 0x11, 0x1},
837
838 /* 48k */
839 {12288000, 48000, 0x0, 0x0},
840 {18432000, 48000, 0x1, 0x0},
841 {12000000, 48000, 0x0, 0x1},
842
843 /* 88.2k */
844 {11289600, 88200, 0x1e, 0x0},
845 {16934400, 88200, 0x1f, 0x0},
846 {12000000, 88200, 0x1f, 0x1},
847
848 /* 96k */
849 {12288000, 96000, 0xe, 0x0},
850 {18432000, 96000, 0xf, 0x0},
851 {12000000, 96000, 0xe, 0x1},
852};
853
854static int get_coeff(int mclk, int rate)
855{
856 int i;
857
858 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
859 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
860 return i;
861 }
862 return -EINVAL;
863}
864
865/*
866 * Clock after PLL and dividers
867 */
868static int wm8753_set_dai_sysclk(struct snd_soc_codec_dai *codec_dai,
869 int clk_id, unsigned int freq, int dir)
870{
871 struct snd_soc_codec *codec = codec_dai->codec;
872 struct wm8753_priv *wm8753 = codec->private_data;
873
874 switch (freq) {
875 case 11289600:
876 case 12000000:
877 case 12288000:
878 case 16934400:
879 case 18432000:
880 if (clk_id == WM8753_MCLK) {
881 wm8753->sysclk = freq;
882 return 0;
883 } else if (clk_id == WM8753_PCMCLK) {
884 wm8753->pcmclk = freq;
885 return 0;
886 }
887 break;
888 }
889 return -EINVAL;
890}
891
892/*
893 * Set's ADC and Voice DAC format.
894 */
895static int wm8753_vdac_adc_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
896 unsigned int fmt)
897{
898 struct snd_soc_codec *codec = codec_dai->codec;
899 u16 voice = wm8753_read_reg_cache(codec, WM8753_PCM) & 0x01ec;
900
901 /* interface format */
902 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
903 case SND_SOC_DAIFMT_I2S:
904 voice |= 0x0002;
905 break;
906 case SND_SOC_DAIFMT_RIGHT_J:
907 break;
908 case SND_SOC_DAIFMT_LEFT_J:
909 voice |= 0x0001;
910 break;
911 case SND_SOC_DAIFMT_DSP_A:
912 voice |= 0x0003;
913 break;
914 case SND_SOC_DAIFMT_DSP_B:
915 voice |= 0x0013;
916 break;
917 default:
918 return -EINVAL;
919 }
920
921 wm8753_write(codec, WM8753_PCM, voice);
922 return 0;
923}
924
925/*
926 * Set PCM DAI bit size and sample rate.
927 */
928static int wm8753_pcm_hw_params(struct snd_pcm_substream *substream,
929 struct snd_pcm_hw_params *params)
930{
931 struct snd_soc_pcm_runtime *rtd = substream->private_data;
932 struct snd_soc_device *socdev = rtd->socdev;
933 struct snd_soc_codec *codec = socdev->codec;
934 struct wm8753_priv *wm8753 = codec->private_data;
935 u16 voice = wm8753_read_reg_cache(codec, WM8753_PCM) & 0x01f3;
936 u16 srate = wm8753_read_reg_cache(codec, WM8753_SRATE1) & 0x017f;
937
938 /* bit size */
939 switch (params_format(params)) {
940 case SNDRV_PCM_FORMAT_S16_LE:
941 break;
942 case SNDRV_PCM_FORMAT_S20_3LE:
943 voice |= 0x0004;
944 break;
945 case SNDRV_PCM_FORMAT_S24_LE:
946 voice |= 0x0008;
947 break;
948 case SNDRV_PCM_FORMAT_S32_LE:
949 voice |= 0x000c;
950 break;
951 }
952
953 /* sample rate */
954 if (params_rate(params) * 384 == wm8753->pcmclk)
955 srate |= 0x80;
956 wm8753_write(codec, WM8753_SRATE1, srate);
957
958 wm8753_write(codec, WM8753_PCM, voice);
959 return 0;
960}
961
962/*
963 * Set's PCM dai fmt and BCLK.
964 */
965static int wm8753_pcm_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
966 unsigned int fmt)
967{
968 struct snd_soc_codec *codec = codec_dai->codec;
969 u16 voice, ioctl;
970
971 voice = wm8753_read_reg_cache(codec, WM8753_PCM) & 0x011f;
972 ioctl = wm8753_read_reg_cache(codec, WM8753_IOCTL) & 0x015d;
973
974 /* set master/slave audio interface */
975 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
976 case SND_SOC_DAIFMT_CBS_CFS:
977 break;
978 case SND_SOC_DAIFMT_CBM_CFM:
979 ioctl |= 0x2;
980 case SND_SOC_DAIFMT_CBM_CFS:
981 voice |= 0x0040;
982 break;
983 default:
984 return -EINVAL;
985 }
986
987 /* clock inversion */
988 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
989 case SND_SOC_DAIFMT_DSP_A:
990 case SND_SOC_DAIFMT_DSP_B:
991 /* frame inversion not valid for DSP modes */
992 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
993 case SND_SOC_DAIFMT_NB_NF:
994 break;
995 case SND_SOC_DAIFMT_IB_NF:
996 voice |= 0x0080;
997 break;
998 default:
999 return -EINVAL;
1000 }
1001 break;
1002 case SND_SOC_DAIFMT_I2S:
1003 case SND_SOC_DAIFMT_RIGHT_J:
1004 case SND_SOC_DAIFMT_LEFT_J:
1005 voice &= ~0x0010;
1006 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1007 case SND_SOC_DAIFMT_NB_NF:
1008 break;
1009 case SND_SOC_DAIFMT_IB_IF:
1010 voice |= 0x0090;
1011 break;
1012 case SND_SOC_DAIFMT_IB_NF:
1013 voice |= 0x0080;
1014 break;
1015 case SND_SOC_DAIFMT_NB_IF:
1016 voice |= 0x0010;
1017 break;
1018 default:
1019 return -EINVAL;
1020 }
1021 break;
1022 default:
1023 return -EINVAL;
1024 }
1025
1026 wm8753_write(codec, WM8753_PCM, voice);
1027 wm8753_write(codec, WM8753_IOCTL, ioctl);
1028 return 0;
1029}
1030
1031static int wm8753_set_dai_clkdiv(struct snd_soc_codec_dai *codec_dai,
1032 int div_id, int div)
1033{
1034 struct snd_soc_codec *codec = codec_dai->codec;
1035 u16 reg;
1036
1037 switch (div_id) {
1038 case WM8753_PCMDIV:
1039 reg = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0x003f;
1040 wm8753_write(codec, WM8753_CLOCK, reg | div);
1041 break;
1042 case WM8753_BCLKDIV:
1043 reg = wm8753_read_reg_cache(codec, WM8753_SRATE2) & 0x01c7;
1044 wm8753_write(codec, WM8753_SRATE2, reg | div);
1045 break;
1046 case WM8753_VXCLKDIV:
1047 reg = wm8753_read_reg_cache(codec, WM8753_SRATE2) & 0x003f;
1048 wm8753_write(codec, WM8753_SRATE2, reg | div);
1049 break;
1050 default:
1051 return -EINVAL;
1052 }
1053 return 0;
1054}
1055
1056/*
1057 * Set's HiFi DAC format.
1058 */
1059static int wm8753_hdac_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
1060 unsigned int fmt)
1061{
1062 struct snd_soc_codec *codec = codec_dai->codec;
1063 u16 hifi = wm8753_read_reg_cache(codec, WM8753_HIFI) & 0x01e0;
1064
1065 /* interface format */
1066 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1067 case SND_SOC_DAIFMT_I2S:
1068 hifi |= 0x0002;
1069 break;
1070 case SND_SOC_DAIFMT_RIGHT_J:
1071 break;
1072 case SND_SOC_DAIFMT_LEFT_J:
1073 hifi |= 0x0001;
1074 break;
1075 case SND_SOC_DAIFMT_DSP_A:
1076 hifi |= 0x0003;
1077 break;
1078 case SND_SOC_DAIFMT_DSP_B:
1079 hifi |= 0x0013;
1080 break;
1081 default:
1082 return -EINVAL;
1083 }
1084
1085 wm8753_write(codec, WM8753_HIFI, hifi);
1086 return 0;
1087}
1088
1089/*
1090 * Set's I2S DAI format.
1091 */
1092static int wm8753_i2s_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
1093 unsigned int fmt)
1094{
1095 struct snd_soc_codec *codec = codec_dai->codec;
1096 u16 ioctl, hifi;
1097
1098 hifi = wm8753_read_reg_cache(codec, WM8753_HIFI) & 0x011f;
1099 ioctl = wm8753_read_reg_cache(codec, WM8753_IOCTL) & 0x00ae;
1100
1101 /* set master/slave audio interface */
1102 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1103 case SND_SOC_DAIFMT_CBS_CFS:
1104 break;
1105 case SND_SOC_DAIFMT_CBM_CFM:
1106 ioctl |= 0x1;
1107 case SND_SOC_DAIFMT_CBM_CFS:
1108 hifi |= 0x0040;
1109 break;
1110 default:
1111 return -EINVAL;
1112 }
1113
1114 /* clock inversion */
1115 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1116 case SND_SOC_DAIFMT_DSP_A:
1117 case SND_SOC_DAIFMT_DSP_B:
1118 /* frame inversion not valid for DSP modes */
1119 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1120 case SND_SOC_DAIFMT_NB_NF:
1121 break;
1122 case SND_SOC_DAIFMT_IB_NF:
1123 hifi |= 0x0080;
1124 break;
1125 default:
1126 return -EINVAL;
1127 }
1128 break;
1129 case SND_SOC_DAIFMT_I2S:
1130 case SND_SOC_DAIFMT_RIGHT_J:
1131 case SND_SOC_DAIFMT_LEFT_J:
1132 hifi &= ~0x0010;
1133 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1134 case SND_SOC_DAIFMT_NB_NF:
1135 break;
1136 case SND_SOC_DAIFMT_IB_IF:
1137 hifi |= 0x0090;
1138 break;
1139 case SND_SOC_DAIFMT_IB_NF:
1140 hifi |= 0x0080;
1141 break;
1142 case SND_SOC_DAIFMT_NB_IF:
1143 hifi |= 0x0010;
1144 break;
1145 default:
1146 return -EINVAL;
1147 }
1148 break;
1149 default:
1150 return -EINVAL;
1151 }
1152
1153 wm8753_write(codec, WM8753_HIFI, hifi);
1154 wm8753_write(codec, WM8753_IOCTL, ioctl);
1155 return 0;
1156}
1157
1158/*
1159 * Set PCM DAI bit size and sample rate.
1160 */
1161static int wm8753_i2s_hw_params(struct snd_pcm_substream *substream,
1162 struct snd_pcm_hw_params *params)
1163{
1164 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1165 struct snd_soc_device *socdev = rtd->socdev;
1166 struct snd_soc_codec *codec = socdev->codec;
1167 struct wm8753_priv *wm8753 = codec->private_data;
1168 u16 srate = wm8753_read_reg_cache(codec, WM8753_SRATE1) & 0x01c0;
1169 u16 hifi = wm8753_read_reg_cache(codec, WM8753_HIFI) & 0x01f3;
1170 int coeff;
1171
1172 /* is digital filter coefficient valid ? */
1173 coeff = get_coeff(wm8753->sysclk, params_rate(params));
1174 if (coeff < 0) {
1175 printk(KERN_ERR "wm8753 invalid MCLK or rate\n");
1176 return coeff;
1177 }
1178 wm8753_write(codec, WM8753_SRATE1, srate | (coeff_div[coeff].sr << 1) |
1179 coeff_div[coeff].usb);
1180
1181 /* bit size */
1182 switch (params_format(params)) {
1183 case SNDRV_PCM_FORMAT_S16_LE:
1184 break;
1185 case SNDRV_PCM_FORMAT_S20_3LE:
1186 hifi |= 0x0004;
1187 break;
1188 case SNDRV_PCM_FORMAT_S24_LE:
1189 hifi |= 0x0008;
1190 break;
1191 case SNDRV_PCM_FORMAT_S32_LE:
1192 hifi |= 0x000c;
1193 break;
1194 }
1195
1196 wm8753_write(codec, WM8753_HIFI, hifi);
1197 return 0;
1198}
1199
1200static int wm8753_mode1v_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
1201 unsigned int fmt)
1202{
1203 struct snd_soc_codec *codec = codec_dai->codec;
1204 u16 clock;
1205
1206 /* set clk source as pcmclk */
1207 clock = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0xfffb;
1208 wm8753_write(codec, WM8753_CLOCK, clock);
1209
1210 if (wm8753_vdac_adc_set_dai_fmt(codec_dai, fmt) < 0)
1211 return -EINVAL;
1212 return wm8753_pcm_set_dai_fmt(codec_dai, fmt);
1213}
1214
1215static int wm8753_mode1h_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
1216 unsigned int fmt)
1217{
1218 if (wm8753_hdac_set_dai_fmt(codec_dai, fmt) < 0)
1219 return -EINVAL;
1220 return wm8753_i2s_set_dai_fmt(codec_dai, fmt);
1221}
1222
1223static int wm8753_mode2_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
1224 unsigned int fmt)
1225{
1226 struct snd_soc_codec *codec = codec_dai->codec;
1227 u16 clock;
1228
1229 /* set clk source as pcmclk */
1230 clock = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0xfffb;
1231 wm8753_write(codec, WM8753_CLOCK, clock);
1232
1233 if (wm8753_vdac_adc_set_dai_fmt(codec_dai, fmt) < 0)
1234 return -EINVAL;
1235 return wm8753_i2s_set_dai_fmt(codec_dai, fmt);
1236}
1237
1238static int wm8753_mode3_4_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
1239 unsigned int fmt)
1240{
1241 struct snd_soc_codec *codec = codec_dai->codec;
1242 u16 clock;
1243
1244 /* set clk source as mclk */
1245 clock = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0xfffb;
1246 wm8753_write(codec, WM8753_CLOCK, clock | 0x4);
1247
1248 if (wm8753_hdac_set_dai_fmt(codec_dai, fmt) < 0)
1249 return -EINVAL;
1250 if (wm8753_vdac_adc_set_dai_fmt(codec_dai, fmt) < 0)
1251 return -EINVAL;
1252 return wm8753_i2s_set_dai_fmt(codec_dai, fmt);
1253}
1254
1255static int wm8753_mute(struct snd_soc_codec_dai *dai, int mute)
1256{
1257 struct snd_soc_codec *codec = dai->codec;
1258 u16 mute_reg = wm8753_read_reg_cache(codec, WM8753_DAC) & 0xfff7;
1259
1260 /* the digital mute covers the HiFi and Voice DAC's on the WM8753.
1261 * make sure we check if they are not both active when we mute */
1262 if (mute && dai->id == 1) {
1263 if (!wm8753_dai[WM8753_DAI_VOICE].playback.active ||
1264 !wm8753_dai[WM8753_DAI_HIFI].playback.active)
1265 wm8753_write(codec, WM8753_DAC, mute_reg | 0x8);
1266 } else {
1267 if (mute)
1268 wm8753_write(codec, WM8753_DAC, mute_reg | 0x8);
1269 else
1270 wm8753_write(codec, WM8753_DAC, mute_reg);
1271 }
1272
1273 return 0;
1274}
1275
1276static int wm8753_dapm_event(struct snd_soc_codec *codec, int event)
1277{
1278 u16 pwr_reg = wm8753_read_reg_cache(codec, WM8753_PWR1) & 0xfe3e;
1279
1280 switch (event) {
1281 case SNDRV_CTL_POWER_D0: /* full On */
1282 /* set vmid to 50k and unmute dac */
1283 wm8753_write(codec, WM8753_PWR1, pwr_reg | 0x00c0);
1284 break;
1285 case SNDRV_CTL_POWER_D1: /* partial On */
1286 case SNDRV_CTL_POWER_D2: /* partial On */
1287 /* set vmid to 5k for quick power up */
1288 wm8753_write(codec, WM8753_PWR1, pwr_reg | 0x01c1);
1289 break;
1290 case SNDRV_CTL_POWER_D3hot: /* Off, with power */
1291 /* mute dac and set vmid to 500k, enable VREF */
1292 wm8753_write(codec, WM8753_PWR1, pwr_reg | 0x0141);
1293 break;
1294 case SNDRV_CTL_POWER_D3cold: /* Off, without power */
1295 wm8753_write(codec, WM8753_PWR1, 0x0001);
1296 break;
1297 }
1298 codec->dapm_state = event;
1299 return 0;
1300}
1301
1302#define WM8753_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
1303 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_44100 | \
1304 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
1305
1306#define WM8753_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
1307 SNDRV_PCM_FMTBIT_S24_LE)
1308
1309/*
1310 * The WM8753 supports upto 4 different and mutually exclusive DAI
1311 * configurations. This gives 2 PCM's available for use, hifi and voice.
1312 * NOTE: The Voice PCM cannot play or capture audio to the CPU as it's DAI
1313 * is connected between the wm8753 and a BT codec or GSM modem.
1314 *
1315 * 1. Voice over PCM DAI - HIFI DAC over HIFI DAI
1316 * 2. Voice over HIFI DAI - HIFI disabled
1317 * 3. Voice disabled - HIFI over HIFI
1318 * 4. Voice disabled - HIFI over HIFI, uses voice DAI LRC for capture
1319 */
1320static const struct snd_soc_codec_dai wm8753_all_dai[] = {
1321/* DAI HiFi mode 1 */
1322{ .name = "WM8753 HiFi",
1323 .id = 1,
1324 .playback = {
1325 .stream_name = "HiFi Playback",
1326 .channels_min = 1,
1327 .channels_max = 2,
1328 .rates = WM8753_RATES,
1329 .formats = WM8753_FORMATS,},
1330 .capture = { /* dummy for fast DAI switching */
1331 .stream_name = "Capture",
1332 .channels_min = 1,
1333 .channels_max = 2,
1334 .rates = WM8753_RATES,
1335 .formats = WM8753_FORMATS,},
1336 .ops = {
1337 .hw_params = wm8753_i2s_hw_params,},
1338 .dai_ops = {
1339 .digital_mute = wm8753_mute,
1340 .set_fmt = wm8753_mode1h_set_dai_fmt,
1341 .set_clkdiv = wm8753_set_dai_clkdiv,
1342 .set_pll = wm8753_set_dai_pll,
1343 .set_sysclk = wm8753_set_dai_sysclk,
1344 },
1345},
1346/* DAI Voice mode 1 */
1347{ .name = "WM8753 Voice",
1348 .id = 1,
1349 .playback = {
1350 .stream_name = "Voice Playback",
1351 .channels_min = 1,
1352 .channels_max = 1,
1353 .rates = WM8753_RATES,
1354 .formats = WM8753_FORMATS,},
1355 .capture = {
1356 .stream_name = "Capture",
1357 .channels_min = 1,
1358 .channels_max = 2,
1359 .rates = WM8753_RATES,
1360 .formats = WM8753_FORMATS,},
1361 .ops = {
1362 .hw_params = wm8753_pcm_hw_params,},
1363 .dai_ops = {
1364 .digital_mute = wm8753_mute,
1365 .set_fmt = wm8753_mode1v_set_dai_fmt,
1366 .set_clkdiv = wm8753_set_dai_clkdiv,
1367 .set_pll = wm8753_set_dai_pll,
1368 .set_sysclk = wm8753_set_dai_sysclk,
1369 },
1370},
1371/* DAI HiFi mode 2 - dummy */
1372{ .name = "WM8753 HiFi",
1373 .id = 2,
1374},
1375/* DAI Voice mode 2 */
1376{ .name = "WM8753 Voice",
1377 .id = 2,
1378 .playback = {
1379 .stream_name = "Voice Playback",
1380 .channels_min = 1,
1381 .channels_max = 1,
1382 .rates = WM8753_RATES,
1383 .formats = WM8753_FORMATS,},
1384 .capture = {
1385 .stream_name = "Capture",
1386 .channels_min = 1,
1387 .channels_max = 2,
1388 .rates = WM8753_RATES,
1389 .formats = WM8753_FORMATS,},
1390 .ops = {
1391 .hw_params = wm8753_pcm_hw_params,},
1392 .dai_ops = {
1393 .digital_mute = wm8753_mute,
1394 .set_fmt = wm8753_mode2_set_dai_fmt,
1395 .set_clkdiv = wm8753_set_dai_clkdiv,
1396 .set_pll = wm8753_set_dai_pll,
1397 .set_sysclk = wm8753_set_dai_sysclk,
1398 },
1399},
1400/* DAI HiFi mode 3 */
1401{ .name = "WM8753 HiFi",
1402 .id = 3,
1403 .playback = {
1404 .stream_name = "HiFi Playback",
1405 .channels_min = 1,
1406 .channels_max = 2,
1407 .rates = WM8753_RATES,
1408 .formats = WM8753_FORMATS,},
1409 .capture = {
1410 .stream_name = "Capture",
1411 .channels_min = 1,
1412 .channels_max = 2,
1413 .rates = WM8753_RATES,
1414 .formats = WM8753_FORMATS,},
1415 .ops = {
1416 .hw_params = wm8753_i2s_hw_params,},
1417 .dai_ops = {
1418 .digital_mute = wm8753_mute,
1419 .set_fmt = wm8753_mode3_4_set_dai_fmt,
1420 .set_clkdiv = wm8753_set_dai_clkdiv,
1421 .set_pll = wm8753_set_dai_pll,
1422 .set_sysclk = wm8753_set_dai_sysclk,
1423 },
1424},
1425/* DAI Voice mode 3 - dummy */
1426{ .name = "WM8753 Voice",
1427 .id = 3,
1428},
1429/* DAI HiFi mode 4 */
1430{ .name = "WM8753 HiFi",
1431 .id = 4,
1432 .playback = {
1433 .stream_name = "HiFi Playback",
1434 .channels_min = 1,
1435 .channels_max = 2,
1436 .rates = WM8753_RATES,
1437 .formats = WM8753_FORMATS,},
1438 .capture = {
1439 .stream_name = "Capture",
1440 .channels_min = 1,
1441 .channels_max = 2,
1442 .rates = WM8753_RATES,
1443 .formats = WM8753_FORMATS,},
1444 .ops = {
1445 .hw_params = wm8753_i2s_hw_params,},
1446 .dai_ops = {
1447 .digital_mute = wm8753_mute,
1448 .set_fmt = wm8753_mode3_4_set_dai_fmt,
1449 .set_clkdiv = wm8753_set_dai_clkdiv,
1450 .set_pll = wm8753_set_dai_pll,
1451 .set_sysclk = wm8753_set_dai_sysclk,
1452 },
1453},
1454/* DAI Voice mode 4 - dummy */
1455{ .name = "WM8753 Voice",
1456 .id = 4,
1457},
1458};
1459
1460struct snd_soc_codec_dai wm8753_dai[2];
1461EXPORT_SYMBOL_GPL(wm8753_dai);
1462
1463static void wm8753_set_dai_mode(struct snd_soc_codec *codec, unsigned int mode)
1464{
1465 if (mode < 4) {
1466 int playback_active, capture_active, codec_active, pop_wait;
1467 void *private_data;
1468
1469 playback_active = wm8753_dai[0].playback.active;
1470 capture_active = wm8753_dai[0].capture.active;
1471 codec_active = wm8753_dai[0].active;
1472 private_data = wm8753_dai[0].private_data;
1473 pop_wait = wm8753_dai[0].pop_wait;
1474 wm8753_dai[0] = wm8753_all_dai[mode << 1];
1475 wm8753_dai[0].playback.active = playback_active;
1476 wm8753_dai[0].capture.active = capture_active;
1477 wm8753_dai[0].active = codec_active;
1478 wm8753_dai[0].private_data = private_data;
1479 wm8753_dai[0].pop_wait = pop_wait;
1480
1481 playback_active = wm8753_dai[1].playback.active;
1482 capture_active = wm8753_dai[1].capture.active;
1483 codec_active = wm8753_dai[1].active;
1484 private_data = wm8753_dai[1].private_data;
1485 pop_wait = wm8753_dai[1].pop_wait;
1486 wm8753_dai[1] = wm8753_all_dai[(mode << 1) + 1];
1487 wm8753_dai[1].playback.active = playback_active;
1488 wm8753_dai[1].capture.active = capture_active;
1489 wm8753_dai[1].active = codec_active;
1490 wm8753_dai[1].private_data = private_data;
1491 wm8753_dai[1].pop_wait = pop_wait;
1492 }
1493 wm8753_dai[0].codec = codec;
1494 wm8753_dai[1].codec = codec;
1495}
1496
1497static void wm8753_work(struct work_struct *work)
1498{
1499 struct snd_soc_codec *codec =
1500 container_of(work, struct snd_soc_codec, delayed_work.work);
1501 wm8753_dapm_event(codec, codec->dapm_state);
1502}
1503
1504static int wm8753_suspend(struct platform_device *pdev, pm_message_t state)
1505{
1506 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1507 struct snd_soc_codec *codec = socdev->codec;
1508
1509 /* we only need to suspend if we are a valid card */
1510 if(!codec->card)
1511 return 0;
1512
1513 wm8753_dapm_event(codec, SNDRV_CTL_POWER_D3cold);
1514 return 0;
1515}
1516
1517static int wm8753_resume(struct platform_device *pdev)
1518{
1519 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1520 struct snd_soc_codec *codec = socdev->codec;
1521 int i;
1522 u8 data[2];
1523 u16 *cache = codec->reg_cache;
1524
1525 /* we only need to resume if we are a valid card */
1526 if(!codec->card)
1527 return 0;
1528
1529 /* Sync reg_cache with the hardware */
1530 for (i = 0; i < ARRAY_SIZE(wm8753_reg); i++) {
1531 if (i + 1 == WM8753_RESET)
1532 continue;
1533 data[0] = ((i + 1) << 1) | ((cache[i] >> 8) & 0x0001);
1534 data[1] = cache[i] & 0x00ff;
1535 codec->hw_write(codec->control_data, data, 2);
1536 }
1537
1538 wm8753_dapm_event(codec, SNDRV_CTL_POWER_D3hot);
1539
1540 /* charge wm8753 caps */
1541 if (codec->suspend_dapm_state == SNDRV_CTL_POWER_D0) {
1542 wm8753_dapm_event(codec, SNDRV_CTL_POWER_D2);
1543 codec->dapm_state = SNDRV_CTL_POWER_D0;
1544 schedule_delayed_work(&codec->delayed_work,
1545 msecs_to_jiffies(caps_charge));
1546 }
1547
1548 return 0;
1549}
1550
1551/*
1552 * initialise the WM8753 driver
1553 * register the mixer and dsp interfaces with the kernel
1554 */
1555static int wm8753_init(struct snd_soc_device *socdev)
1556{
1557 struct snd_soc_codec *codec = socdev->codec;
1558 int reg, ret = 0;
1559
1560 codec->name = "WM8753";
1561 codec->owner = THIS_MODULE;
1562 codec->read = wm8753_read_reg_cache;
1563 codec->write = wm8753_write;
1564 codec->dapm_event = wm8753_dapm_event;
1565 codec->dai = wm8753_dai;
1566 codec->num_dai = 2;
1567 codec->reg_cache_size = sizeof(wm8753_reg);
1568 codec->reg_cache = kmemdup(wm8753_reg, sizeof(wm8753_reg), GFP_KERNEL);
1569
1570 if (codec->reg_cache == NULL)
1571 return -ENOMEM;
1572
1573 wm8753_set_dai_mode(codec, 0);
1574
1575 wm8753_reset(codec);
1576
1577 /* register pcms */
1578 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
1579 if (ret < 0) {
1580 printk(KERN_ERR "wm8753: failed to create pcms\n");
1581 goto pcm_err;
1582 }
1583
1584 /* charge output caps */
1585 wm8753_dapm_event(codec, SNDRV_CTL_POWER_D2);
1586 codec->dapm_state = SNDRV_CTL_POWER_D3hot;
1587 schedule_delayed_work(&codec->delayed_work,
1588 msecs_to_jiffies(caps_charge));
1589
1590 /* set the update bits */
1591 reg = wm8753_read_reg_cache(codec, WM8753_LDAC);
1592 wm8753_write(codec, WM8753_LDAC, reg | 0x0100);
1593 reg = wm8753_read_reg_cache(codec, WM8753_RDAC);
1594 wm8753_write(codec, WM8753_RDAC, reg | 0x0100);
1595 reg = wm8753_read_reg_cache(codec, WM8753_LADC);
1596 wm8753_write(codec, WM8753_LADC, reg | 0x0100);
1597 reg = wm8753_read_reg_cache(codec, WM8753_RADC);
1598 wm8753_write(codec, WM8753_RADC, reg | 0x0100);
1599 reg = wm8753_read_reg_cache(codec, WM8753_LOUT1V);
1600 wm8753_write(codec, WM8753_LOUT1V, reg | 0x0100);
1601 reg = wm8753_read_reg_cache(codec, WM8753_ROUT1V);
1602 wm8753_write(codec, WM8753_ROUT1V, reg | 0x0100);
1603 reg = wm8753_read_reg_cache(codec, WM8753_LOUT2V);
1604 wm8753_write(codec, WM8753_LOUT2V, reg | 0x0100);
1605 reg = wm8753_read_reg_cache(codec, WM8753_ROUT2V);
1606 wm8753_write(codec, WM8753_ROUT2V, reg | 0x0100);
1607 reg = wm8753_read_reg_cache(codec, WM8753_LINVOL);
1608 wm8753_write(codec, WM8753_LINVOL, reg | 0x0100);
1609 reg = wm8753_read_reg_cache(codec, WM8753_RINVOL);
1610 wm8753_write(codec, WM8753_RINVOL, reg | 0x0100);
1611
1612 wm8753_add_controls(codec);
1613 wm8753_add_widgets(codec);
1614 ret = snd_soc_register_card(socdev);
1615 if (ret < 0) {
1616 printk(KERN_ERR "wm8753: failed to register card\n");
1617 goto card_err;
1618 }
1619 return ret;
1620
1621card_err:
1622 snd_soc_free_pcms(socdev);
1623 snd_soc_dapm_free(socdev);
1624pcm_err:
1625 kfree(codec->reg_cache);
1626 return ret;
1627}
1628
1629/* If the i2c layer weren't so broken, we could pass this kind of data
1630 around */
1631static struct snd_soc_device *wm8753_socdev;
1632
1633#if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE)
1634
1635/*
1636 * WM8753 2 wire address is determined by GPIO5
1637 * state during powerup.
1638 * low = 0x1a
1639 * high = 0x1b
1640 */
1641static unsigned short normal_i2c[] = { 0, I2C_CLIENT_END };
1642
1643/* Magic definition of all other variables and things */
1644I2C_CLIENT_INSMOD;
1645
1646static struct i2c_driver wm8753_i2c_driver;
1647static struct i2c_client client_template;
1648
1649static int wm8753_codec_probe(struct i2c_adapter *adap, int addr, int kind)
1650{
1651 struct snd_soc_device *socdev = wm8753_socdev;
1652 struct wm8753_setup_data *setup = socdev->codec_data;
1653 struct snd_soc_codec *codec = socdev->codec;
1654 struct i2c_client *i2c;
1655 int ret;
1656
1657 if (addr != setup->i2c_address)
1658 return -ENODEV;
1659
1660 client_template.adapter = adap;
1661 client_template.addr = addr;
1662
1663 i2c = kmemdup(&client_template, sizeof(client_template), GFP_KERNEL);
1664 if (i2c == NULL){
1665 kfree(codec);
1666 return -ENOMEM;
1667 }
1668 i2c_set_clientdata(i2c, codec);
1669 codec->control_data = i2c;
1670
1671 ret = i2c_attach_client(i2c);
1672 if (ret < 0) {
1673 err("failed to attach codec at addr %x\n", addr);
1674 goto err;
1675 }
1676
1677 ret = wm8753_init(socdev);
1678 if (ret < 0) {
1679 err("failed to initialise WM8753\n");
1680 goto err;
1681 }
1682
1683 return ret;
1684
1685err:
1686 kfree(codec);
1687 kfree(i2c);
1688 return ret;
1689}
1690
1691static int wm8753_i2c_detach(struct i2c_client *client)
1692{
1693 struct snd_soc_codec *codec = i2c_get_clientdata(client);
1694 i2c_detach_client(client);
1695 kfree(codec->reg_cache);
1696 kfree(client);
1697 return 0;
1698}
1699
1700static int wm8753_i2c_attach(struct i2c_adapter *adap)
1701{
1702 return i2c_probe(adap, &addr_data, wm8753_codec_probe);
1703}
1704
1705/* corgi i2c codec control layer */
1706static struct i2c_driver wm8753_i2c_driver = {
1707 .driver = {
1708 .name = "WM8753 I2C Codec",
1709 .owner = THIS_MODULE,
1710 },
1711 .id = I2C_DRIVERID_WM8753,
1712 .attach_adapter = wm8753_i2c_attach,
1713 .detach_client = wm8753_i2c_detach,
1714 .command = NULL,
1715};
1716
1717static struct i2c_client client_template = {
1718 .name = "WM8753",
1719 .driver = &wm8753_i2c_driver,
1720};
1721#endif
1722
1723static int wm8753_probe(struct platform_device *pdev)
1724{
1725 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1726 struct wm8753_setup_data *setup;
1727 struct snd_soc_codec *codec;
1728 struct wm8753_priv *wm8753;
1729 int ret = 0;
1730
1731 info("WM8753 Audio Codec %s", WM8753_VERSION);
1732
1733 setup = socdev->codec_data;
1734 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
1735 if (codec == NULL)
1736 return -ENOMEM;
1737
1738 wm8753 = kzalloc(sizeof(struct wm8753_priv), GFP_KERNEL);
1739 if (wm8753 == NULL) {
1740 kfree(codec);
1741 return -ENOMEM;
1742 }
1743
1744 codec->private_data = wm8753;
1745 socdev->codec = codec;
1746 mutex_init(&codec->mutex);
1747 INIT_LIST_HEAD(&codec->dapm_widgets);
1748 INIT_LIST_HEAD(&codec->dapm_paths);
1749 wm8753_socdev = socdev;
1750 INIT_DELAYED_WORK(&codec->delayed_work, wm8753_work);
1751
1752#if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE)
1753 if (setup->i2c_address) {
1754 normal_i2c[0] = setup->i2c_address;
1755 codec->hw_write = (hw_write_t)i2c_master_send;
1756 ret = i2c_add_driver(&wm8753_i2c_driver);
1757 if (ret != 0)
1758 printk(KERN_ERR "can't add i2c driver");
1759 }
1760#else
1761 /* Add other interfaces here */
1762#endif
1763 return ret;
1764}
1765
1766/*
1767 * This function forces any delayed work to be queued and run.
1768 */
1769static int run_delayed_work(struct delayed_work *dwork)
1770{
1771 int ret;
1772
1773 /* cancel any work waiting to be queued. */
1774 ret = cancel_delayed_work(dwork);
1775
1776 /* if there was any work waiting then we run it now and
1777 * wait for it's completion */
1778 if (ret) {
1779 schedule_delayed_work(dwork, 0);
1780 flush_scheduled_work();
1781 }
1782 return ret;
1783}
1784
1785/* power down chip */
1786static int wm8753_remove(struct platform_device *pdev)
1787{
1788 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1789 struct snd_soc_codec *codec = socdev->codec;
1790
1791 if (codec->control_data)
1792 wm8753_dapm_event(codec, SNDRV_CTL_POWER_D3cold);
1793 run_delayed_work(&codec->delayed_work);
1794 snd_soc_free_pcms(socdev);
1795 snd_soc_dapm_free(socdev);
1796#if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE)
1797 i2c_del_driver(&wm8753_i2c_driver);
1798#endif
1799 kfree(codec->private_data);
1800 kfree(codec);
1801
1802 return 0;
1803}
1804
1805struct snd_soc_codec_device soc_codec_dev_wm8753 = {
1806 .probe = wm8753_probe,
1807 .remove = wm8753_remove,
1808 .suspend = wm8753_suspend,
1809 .resume = wm8753_resume,
1810};
1811
1812EXPORT_SYMBOL_GPL(soc_codec_dev_wm8753);
1813
1814MODULE_DESCRIPTION("ASoC WM8753 driver");
1815MODULE_AUTHOR("Liam Girdwood");
1816MODULE_LICENSE("GPL");