blob: 5ad677ce80da46baa8789c2d3072c3205474bb2b [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.
Liam Girdwoodd3311242008-10-12 13:17:36 +01005 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood1f53aee2007-04-16 19:17:44 +02006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
12 * Notes:
13 * The WM8753 is a low power, high quality stereo codec with integrated PCM
14 * codec designed for portable digital telephony applications.
15 *
16 * Dual DAI:-
17 *
18 * This driver support 2 DAI PCM's. This makes the default PCM available for
19 * HiFi audio (e.g. MP3, ogg) playback/capture and the other PCM available for
20 * voice.
21 *
22 * Please note that the voice PCM can be connected directly to a Bluetooth
23 * codec or GSM modem and thus cannot be read or written to, although it is
24 * available to be configured with snd_hw_params(), etc and kcontrols in the
25 * normal alsa manner.
26 *
27 * Fast DAI switching:-
28 *
29 * The driver can now fast switch between the DAI configurations via a
30 * an alsa kcontrol. This allows the PCM to remain open.
31 *
32 */
33
34#include <linux/module.h>
35#include <linux/moduleparam.h>
Liam Girdwood1f53aee2007-04-16 19:17:44 +020036#include <linux/kernel.h>
37#include <linux/init.h>
38#include <linux/delay.h>
39#include <linux/pm.h>
40#include <linux/i2c.h>
41#include <linux/platform_device.h>
Mark Browndd0c0c82008-10-06 16:54:34 +010042#include <linux/spi/spi.h>
Liam Girdwood1f53aee2007-04-16 19:17:44 +020043#include <sound/core.h>
44#include <sound/pcm.h>
45#include <sound/pcm_params.h>
46#include <sound/soc.h>
47#include <sound/soc-dapm.h>
48#include <sound/initval.h>
Liam Girdwood2d6a4ac2008-01-10 14:43:48 +010049#include <sound/tlv.h>
Liam Girdwood1f53aee2007-04-16 19:17:44 +020050#include <asm/div64.h>
51
52#include "wm8753.h"
53
Liam Girdwood1f53aee2007-04-16 19:17:44 +020054static int caps_charge = 2000;
55module_param(caps_charge, int, 0);
56MODULE_PARM_DESC(caps_charge, "WM8753 cap charge time (msecs)");
57
58static void wm8753_set_dai_mode(struct snd_soc_codec *codec,
59 unsigned int mode);
60
Liam Girdwood1f53aee2007-04-16 19:17:44 +020061/*
62 * wm8753 register cache
63 * We can't read the WM8753 register space when we
64 * are using 2 wire for device control, so we cache them instead.
65 */
66static const u16 wm8753_reg[] = {
67 0x0008, 0x0000, 0x000a, 0x000a,
68 0x0033, 0x0000, 0x0007, 0x00ff,
69 0x00ff, 0x000f, 0x000f, 0x007b,
70 0x0000, 0x0032, 0x0000, 0x00c3,
71 0x00c3, 0x00c0, 0x0000, 0x0000,
72 0x0000, 0x0000, 0x0000, 0x0000,
73 0x0000, 0x0000, 0x0000, 0x0000,
74 0x0000, 0x0000, 0x0000, 0x0055,
75 0x0005, 0x0050, 0x0055, 0x0050,
76 0x0055, 0x0050, 0x0055, 0x0079,
77 0x0079, 0x0079, 0x0079, 0x0079,
78 0x0000, 0x0000, 0x0000, 0x0000,
79 0x0097, 0x0097, 0x0000, 0x0004,
80 0x0000, 0x0083, 0x0024, 0x01ba,
81 0x0000, 0x0083, 0x0024, 0x01ba,
Lars-Peter Clausen637a9352009-07-03 01:04:16 +020082 0x0000, 0x0000, 0x0000
Liam Girdwood1f53aee2007-04-16 19:17:44 +020083};
84
Mark Brownc2bac162009-02-24 23:33:12 +000085/* codec private data */
86struct wm8753_priv {
87 unsigned int sysclk;
88 unsigned int pcmclk;
89 struct snd_soc_codec codec;
90 u16 reg_cache[ARRAY_SIZE(wm8753_reg)];
91};
92
Liam Girdwood1f53aee2007-04-16 19:17:44 +020093/*
94 * read wm8753 register cache
95 */
96static inline unsigned int wm8753_read_reg_cache(struct snd_soc_codec *codec,
97 unsigned int reg)
98{
99 u16 *cache = codec->reg_cache;
Ian Molton91432e92009-01-17 17:44:23 +0000100 if (reg < 1 || reg >= (ARRAY_SIZE(wm8753_reg) + 1))
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200101 return -1;
102 return cache[reg - 1];
103}
104
105/*
106 * write wm8753 register cache
107 */
108static inline void wm8753_write_reg_cache(struct snd_soc_codec *codec,
109 unsigned int reg, unsigned int value)
110{
111 u16 *cache = codec->reg_cache;
Ian Molton91432e92009-01-17 17:44:23 +0000112 if (reg < 1 || reg >= (ARRAY_SIZE(wm8753_reg) + 1))
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200113 return;
114 cache[reg - 1] = value;
115}
116
117/*
118 * write to the WM8753 register space
119 */
120static int wm8753_write(struct snd_soc_codec *codec, unsigned int reg,
121 unsigned int value)
122{
123 u8 data[2];
124
125 /* data is
126 * D15..D9 WM8753 register offset
127 * D8...D0 register data
128 */
129 data[0] = (reg << 1) | ((value >> 8) & 0x0001);
130 data[1] = value & 0x00ff;
131
Mark Brown60fc6842008-04-30 17:18:43 +0200132 wm8753_write_reg_cache(codec, reg, value);
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200133 if (codec->hw_write(codec->control_data, data, 2) == 2)
134 return 0;
135 else
136 return -EIO;
137}
138
139#define wm8753_reset(c) wm8753_write(c, WM8753_RESET, 0)
140
141/*
142 * WM8753 Controls
143 */
144static const char *wm8753_base[] = {"Linear Control", "Adaptive Boost"};
145static const char *wm8753_base_filter[] =
146 {"130Hz @ 48kHz", "200Hz @ 48kHz", "100Hz @ 16kHz", "400Hz @ 48kHz",
147 "100Hz @ 8kHz", "200Hz @ 8kHz"};
148static const char *wm8753_treble[] = {"8kHz", "4kHz"};
149static const char *wm8753_alc_func[] = {"Off", "Right", "Left", "Stereo"};
150static const char *wm8753_ng_type[] = {"Constant PGA Gain", "Mute ADC Output"};
151static const char *wm8753_3d_func[] = {"Capture", "Playback"};
152static const char *wm8753_3d_uc[] = {"2.2kHz", "1.5kHz"};
153static const char *wm8753_3d_lc[] = {"200Hz", "500Hz"};
154static const char *wm8753_deemp[] = {"None", "32kHz", "44.1kHz", "48kHz"};
155static const char *wm8753_mono_mix[] = {"Stereo", "Left", "Right", "Mono"};
156static const char *wm8753_dac_phase[] = {"Non Inverted", "Inverted"};
157static const char *wm8753_line_mix[] = {"Line 1 + 2", "Line 1 - 2",
158 "Line 1", "Line 2"};
159static const char *wm8753_mono_mux[] = {"Line Mix", "Rx Mix"};
160static const char *wm8753_right_mux[] = {"Line 2", "Rx Mix"};
161static const char *wm8753_left_mux[] = {"Line 1", "Rx Mix"};
162static const char *wm8753_rxmsel[] = {"RXP - RXN", "RXP + RXN", "RXP", "RXN"};
163static const char *wm8753_sidetone_mux[] = {"Left PGA", "Mic 1", "Mic 2",
164 "Right PGA"};
165static const char *wm8753_mono2_src[] = {"Inverted Mono 1", "Left", "Right",
166 "Left + Right"};
167static const char *wm8753_out3[] = {"VREF", "ROUT2", "Left + Right"};
168static const char *wm8753_out4[] = {"VREF", "Capture ST", "LOUT2"};
169static const char *wm8753_radcsel[] = {"PGA", "Line or RXP-RXN", "Sidetone"};
170static const char *wm8753_ladcsel[] = {"PGA", "Line or RXP-RXN", "Line"};
171static const char *wm8753_mono_adc[] = {"Stereo", "Analogue Mix Left",
172 "Analogue Mix Right", "Digital Mono Mix"};
173static const char *wm8753_adc_hp[] = {"3.4Hz @ 48kHz", "82Hz @ 16k",
174 "82Hz @ 8kHz", "170Hz @ 8kHz"};
175static const char *wm8753_adc_filter[] = {"HiFi", "Voice"};
176static const char *wm8753_mic_sel[] = {"Mic 1", "Mic 2", "Mic 3"};
177static const char *wm8753_dai_mode[] = {"DAI 0", "DAI 1", "DAI 2", "DAI 3"};
178static const char *wm8753_dat_sel[] = {"Stereo", "Left ADC", "Right ADC",
179 "Channel Swap"};
Graeme Gregoryae092c92008-03-03 17:19:45 +0100180static const char *wm8753_rout2_phase[] = {"Non Inverted", "Inverted"};
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200181
182static const struct soc_enum wm8753_enum[] = {
183SOC_ENUM_SINGLE(WM8753_BASS, 7, 2, wm8753_base),
184SOC_ENUM_SINGLE(WM8753_BASS, 4, 6, wm8753_base_filter),
185SOC_ENUM_SINGLE(WM8753_TREBLE, 6, 2, wm8753_treble),
186SOC_ENUM_SINGLE(WM8753_ALC1, 7, 4, wm8753_alc_func),
187SOC_ENUM_SINGLE(WM8753_NGATE, 1, 2, wm8753_ng_type),
188SOC_ENUM_SINGLE(WM8753_3D, 7, 2, wm8753_3d_func),
189SOC_ENUM_SINGLE(WM8753_3D, 6, 2, wm8753_3d_uc),
190SOC_ENUM_SINGLE(WM8753_3D, 5, 2, wm8753_3d_lc),
191SOC_ENUM_SINGLE(WM8753_DAC, 1, 4, wm8753_deemp),
192SOC_ENUM_SINGLE(WM8753_DAC, 4, 4, wm8753_mono_mix),
193SOC_ENUM_SINGLE(WM8753_DAC, 6, 2, wm8753_dac_phase),
194SOC_ENUM_SINGLE(WM8753_INCTL1, 3, 4, wm8753_line_mix),
195SOC_ENUM_SINGLE(WM8753_INCTL1, 2, 2, wm8753_mono_mux),
196SOC_ENUM_SINGLE(WM8753_INCTL1, 1, 2, wm8753_right_mux),
197SOC_ENUM_SINGLE(WM8753_INCTL1, 0, 2, wm8753_left_mux),
198SOC_ENUM_SINGLE(WM8753_INCTL2, 6, 4, wm8753_rxmsel),
199SOC_ENUM_SINGLE(WM8753_INCTL2, 4, 4, wm8753_sidetone_mux),
200SOC_ENUM_SINGLE(WM8753_OUTCTL, 7, 4, wm8753_mono2_src),
201SOC_ENUM_SINGLE(WM8753_OUTCTL, 0, 3, wm8753_out3),
202SOC_ENUM_SINGLE(WM8753_ADCTL2, 7, 3, wm8753_out4),
203SOC_ENUM_SINGLE(WM8753_ADCIN, 2, 3, wm8753_radcsel),
204SOC_ENUM_SINGLE(WM8753_ADCIN, 0, 3, wm8753_ladcsel),
205SOC_ENUM_SINGLE(WM8753_ADCIN, 4, 4, wm8753_mono_adc),
206SOC_ENUM_SINGLE(WM8753_ADC, 2, 4, wm8753_adc_hp),
207SOC_ENUM_SINGLE(WM8753_ADC, 4, 2, wm8753_adc_filter),
208SOC_ENUM_SINGLE(WM8753_MICBIAS, 6, 3, wm8753_mic_sel),
209SOC_ENUM_SINGLE(WM8753_IOCTL, 2, 4, wm8753_dai_mode),
210SOC_ENUM_SINGLE(WM8753_ADC, 7, 4, wm8753_dat_sel),
Graeme Gregoryae092c92008-03-03 17:19:45 +0100211SOC_ENUM_SINGLE(WM8753_OUTCTL, 2, 2, wm8753_rout2_phase),
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200212};
213
214
215static int wm8753_get_dai(struct snd_kcontrol *kcontrol,
216 struct snd_ctl_elem_value *ucontrol)
217{
218 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
219 int mode = wm8753_read_reg_cache(codec, WM8753_IOCTL);
220
221 ucontrol->value.integer.value[0] = (mode & 0xc) >> 2;
222 return 0;
223}
224
225static int wm8753_set_dai(struct snd_kcontrol *kcontrol,
226 struct snd_ctl_elem_value *ucontrol)
227{
228 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
229 int mode = wm8753_read_reg_cache(codec, WM8753_IOCTL);
230
Mark Brown60fc6842008-04-30 17:18:43 +0200231 if (((mode & 0xc) >> 2) == ucontrol->value.integer.value[0])
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200232 return 0;
233
234 mode &= 0xfff3;
235 mode |= (ucontrol->value.integer.value[0] << 2);
236
237 wm8753_write(codec, WM8753_IOCTL, mode);
238 wm8753_set_dai_mode(codec, ucontrol->value.integer.value[0]);
239 return 1;
240}
241
Mike Montour2cc8c602008-06-11 13:47:12 +0100242static const DECLARE_TLV_DB_SCALE(rec_mix_tlv, -1500, 300, 0);
243static const DECLARE_TLV_DB_SCALE(mic_preamp_tlv, 1200, 600, 0);
244static const DECLARE_TLV_DB_SCALE(adc_tlv, -9750, 50, 1);
245static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
246static const unsigned int out_tlv[] = {
247 TLV_DB_RANGE_HEAD(2),
248 /* 0000000 - 0101111 = "Analogue mute" */
249 0, 48, TLV_DB_SCALE_ITEM(-25500, 0, 0),
250 48, 127, TLV_DB_SCALE_ITEM(-7300, 100, 0),
251};
252static const DECLARE_TLV_DB_SCALE(mix_tlv, -1500, 300, 0);
253static const DECLARE_TLV_DB_SCALE(voice_mix_tlv, -1200, 300, 0);
254static const DECLARE_TLV_DB_SCALE(pga_tlv, -1725, 75, 0);
Liam Girdwood2d6a4ac2008-01-10 14:43:48 +0100255
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200256static const struct snd_kcontrol_new wm8753_snd_controls[] = {
Mike Montour2cc8c602008-06-11 13:47:12 +0100257SOC_DOUBLE_R_TLV("PCM Volume", WM8753_LDAC, WM8753_RDAC, 0, 255, 0, dac_tlv),
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200258
Mike Montour2cc8c602008-06-11 13:47:12 +0100259SOC_DOUBLE_R_TLV("ADC Capture Volume", WM8753_LADC, WM8753_RADC, 0, 255, 0,
260 adc_tlv),
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200261
Mike Montour2cc8c602008-06-11 13:47:12 +0100262SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8753_LOUT1V, WM8753_ROUT1V,
263 0, 127, 0, out_tlv),
264SOC_DOUBLE_R_TLV("Speaker Playback Volume", WM8753_LOUT2V, WM8753_ROUT2V, 0,
265 127, 0, out_tlv),
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200266
Mike Montour2cc8c602008-06-11 13:47:12 +0100267SOC_SINGLE_TLV("Mono Playback Volume", WM8753_MOUTV, 0, 127, 0, out_tlv),
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200268
Mike Montour2cc8c602008-06-11 13:47:12 +0100269SOC_DOUBLE_R_TLV("Bypass Playback Volume", WM8753_LOUTM1, WM8753_ROUTM1, 4, 7,
270 1, mix_tlv),
271SOC_DOUBLE_R_TLV("Sidetone Playback Volume", WM8753_LOUTM2, WM8753_ROUTM2, 4,
272 7, 1, mix_tlv),
273SOC_DOUBLE_R_TLV("Voice Playback Volume", WM8753_LOUTM2, WM8753_ROUTM2, 0, 7,
274 1, voice_mix_tlv),
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200275
Mike Montour2cc8c602008-06-11 13:47:12 +0100276SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8753_LOUT1V, WM8753_ROUT1V, 7,
277 1, 0),
278SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8753_LOUT2V, WM8753_ROUT2V, 7,
279 1, 0),
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200280
Mike Montour2cc8c602008-06-11 13:47:12 +0100281SOC_SINGLE_TLV("Mono Bypass Playback Volume", WM8753_MOUTM1, 4, 7, 1, mix_tlv),
282SOC_SINGLE_TLV("Mono Sidetone Playback Volume", WM8753_MOUTM2, 4, 7, 1,
283 mix_tlv),
284SOC_SINGLE_TLV("Mono Voice Playback Volume", WM8753_MOUTM2, 0, 7, 1,
285 voice_mix_tlv),
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200286SOC_SINGLE("Mono Playback ZC Switch", WM8753_MOUTV, 7, 1, 0),
287
288SOC_ENUM("Bass Boost", wm8753_enum[0]),
289SOC_ENUM("Bass Filter", wm8753_enum[1]),
290SOC_SINGLE("Bass Volume", WM8753_BASS, 0, 15, 1),
291
292SOC_SINGLE("Treble Volume", WM8753_TREBLE, 0, 15, 1),
293SOC_ENUM("Treble Cut-off", wm8753_enum[2]),
294
Mike Montour2cc8c602008-06-11 13:47:12 +0100295SOC_DOUBLE_TLV("Sidetone Capture Volume", WM8753_RECMIX1, 0, 4, 7, 1,
296 rec_mix_tlv),
297SOC_SINGLE_TLV("Voice Sidetone Capture Volume", WM8753_RECMIX2, 0, 7, 1,
298 rec_mix_tlv),
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200299
Mike Montour2cc8c602008-06-11 13:47:12 +0100300SOC_DOUBLE_R_TLV("Capture Volume", WM8753_LINVOL, WM8753_RINVOL, 0, 63, 0,
301 pga_tlv),
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200302SOC_DOUBLE_R("Capture ZC Switch", WM8753_LINVOL, WM8753_RINVOL, 6, 1, 0),
303SOC_DOUBLE_R("Capture Switch", WM8753_LINVOL, WM8753_RINVOL, 7, 1, 1),
304
305SOC_ENUM("Capture Filter Select", wm8753_enum[23]),
306SOC_ENUM("Capture Filter Cut-off", wm8753_enum[24]),
307SOC_SINGLE("Capture Filter Switch", WM8753_ADC, 0, 1, 1),
308
309SOC_SINGLE("ALC Capture Target Volume", WM8753_ALC1, 0, 7, 0),
310SOC_SINGLE("ALC Capture Max Volume", WM8753_ALC1, 4, 7, 0),
311SOC_ENUM("ALC Capture Function", wm8753_enum[3]),
312SOC_SINGLE("ALC Capture ZC Switch", WM8753_ALC2, 8, 1, 0),
313SOC_SINGLE("ALC Capture Hold Time", WM8753_ALC2, 0, 15, 1),
314SOC_SINGLE("ALC Capture Decay Time", WM8753_ALC3, 4, 15, 1),
315SOC_SINGLE("ALC Capture Attack Time", WM8753_ALC3, 0, 15, 0),
316SOC_SINGLE("ALC Capture NG Threshold", WM8753_NGATE, 3, 31, 0),
317SOC_ENUM("ALC Capture NG Type", wm8753_enum[4]),
318SOC_SINGLE("ALC Capture NG Switch", WM8753_NGATE, 0, 1, 0),
319
320SOC_ENUM("3D Function", wm8753_enum[5]),
321SOC_ENUM("3D Upper Cut-off", wm8753_enum[6]),
322SOC_ENUM("3D Lower Cut-off", wm8753_enum[7]),
323SOC_SINGLE("3D Volume", WM8753_3D, 1, 15, 0),
324SOC_SINGLE("3D Switch", WM8753_3D, 0, 1, 0),
325
326SOC_SINGLE("Capture 6dB Attenuate", WM8753_ADCTL1, 2, 1, 0),
327SOC_SINGLE("Playback 6dB Attenuate", WM8753_ADCTL1, 1, 1, 0),
328
329SOC_ENUM("De-emphasis", wm8753_enum[8]),
330SOC_ENUM("Playback Mono Mix", wm8753_enum[9]),
331SOC_ENUM("Playback Phase", wm8753_enum[10]),
332
Mike Montour2cc8c602008-06-11 13:47:12 +0100333SOC_SINGLE_TLV("Mic2 Capture Volume", WM8753_INCTL1, 7, 3, 0, mic_preamp_tlv),
334SOC_SINGLE_TLV("Mic1 Capture Volume", WM8753_INCTL1, 5, 3, 0, mic_preamp_tlv),
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200335
336SOC_ENUM_EXT("DAI Mode", wm8753_enum[26], wm8753_get_dai, wm8753_set_dai),
337
338SOC_ENUM("ADC Data Select", wm8753_enum[27]),
Graeme Gregoryae092c92008-03-03 17:19:45 +0100339SOC_ENUM("ROUT2 Phase", wm8753_enum[28]),
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200340};
341
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200342/*
343 * _DAPM_ Controls
344 */
345
346/* Left Mixer */
347static const struct snd_kcontrol_new wm8753_left_mixer_controls[] = {
348SOC_DAPM_SINGLE("Voice Playback Switch", WM8753_LOUTM2, 8, 1, 0),
349SOC_DAPM_SINGLE("Sidetone Playback Switch", WM8753_LOUTM2, 7, 1, 0),
350SOC_DAPM_SINGLE("Left Playback Switch", WM8753_LOUTM1, 8, 1, 0),
351SOC_DAPM_SINGLE("Bypass Playback Switch", WM8753_LOUTM1, 7, 1, 0),
352};
353
354/* Right mixer */
355static const struct snd_kcontrol_new wm8753_right_mixer_controls[] = {
356SOC_DAPM_SINGLE("Voice Playback Switch", WM8753_ROUTM2, 8, 1, 0),
357SOC_DAPM_SINGLE("Sidetone Playback Switch", WM8753_ROUTM2, 7, 1, 0),
358SOC_DAPM_SINGLE("Right Playback Switch", WM8753_ROUTM1, 8, 1, 0),
359SOC_DAPM_SINGLE("Bypass Playback Switch", WM8753_ROUTM1, 7, 1, 0),
360};
361
362/* Mono mixer */
363static const struct snd_kcontrol_new wm8753_mono_mixer_controls[] = {
364SOC_DAPM_SINGLE("Left Playback Switch", WM8753_MOUTM1, 8, 1, 0),
365SOC_DAPM_SINGLE("Right Playback Switch", WM8753_MOUTM2, 8, 1, 0),
366SOC_DAPM_SINGLE("Voice Playback Switch", WM8753_MOUTM2, 3, 1, 0),
367SOC_DAPM_SINGLE("Sidetone Playback Switch", WM8753_MOUTM2, 7, 1, 0),
368SOC_DAPM_SINGLE("Bypass Playback Switch", WM8753_MOUTM1, 7, 1, 0),
369};
370
371/* Mono 2 Mux */
372static const struct snd_kcontrol_new wm8753_mono2_controls =
373SOC_DAPM_ENUM("Route", wm8753_enum[17]);
374
375/* Out 3 Mux */
376static const struct snd_kcontrol_new wm8753_out3_controls =
377SOC_DAPM_ENUM("Route", wm8753_enum[18]);
378
379/* Out 4 Mux */
380static const struct snd_kcontrol_new wm8753_out4_controls =
381SOC_DAPM_ENUM("Route", wm8753_enum[19]);
382
383/* ADC Mono Mix */
384static const struct snd_kcontrol_new wm8753_adc_mono_controls =
385SOC_DAPM_ENUM("Route", wm8753_enum[22]);
386
387/* Record mixer */
388static const struct snd_kcontrol_new wm8753_record_mixer_controls[] = {
389SOC_DAPM_SINGLE("Voice Capture Switch", WM8753_RECMIX2, 3, 1, 0),
390SOC_DAPM_SINGLE("Left Capture Switch", WM8753_RECMIX1, 3, 1, 0),
391SOC_DAPM_SINGLE("Right Capture Switch", WM8753_RECMIX1, 7, 1, 0),
392};
393
394/* Left ADC mux */
395static const struct snd_kcontrol_new wm8753_adc_left_controls =
396SOC_DAPM_ENUM("Route", wm8753_enum[21]);
397
398/* Right ADC mux */
399static const struct snd_kcontrol_new wm8753_adc_right_controls =
400SOC_DAPM_ENUM("Route", wm8753_enum[20]);
401
402/* MIC mux */
403static const struct snd_kcontrol_new wm8753_mic_mux_controls =
404SOC_DAPM_ENUM("Route", wm8753_enum[16]);
405
406/* ALC mixer */
407static const struct snd_kcontrol_new wm8753_alc_mixer_controls[] = {
408SOC_DAPM_SINGLE("Line Capture Switch", WM8753_INCTL2, 3, 1, 0),
409SOC_DAPM_SINGLE("Mic2 Capture Switch", WM8753_INCTL2, 2, 1, 0),
410SOC_DAPM_SINGLE("Mic1 Capture Switch", WM8753_INCTL2, 1, 1, 0),
411SOC_DAPM_SINGLE("Rx Capture Switch", WM8753_INCTL2, 0, 1, 0),
412};
413
414/* Left Line mux */
415static const struct snd_kcontrol_new wm8753_line_left_controls =
416SOC_DAPM_ENUM("Route", wm8753_enum[14]);
417
418/* Right Line mux */
419static const struct snd_kcontrol_new wm8753_line_right_controls =
420SOC_DAPM_ENUM("Route", wm8753_enum[13]);
421
422/* Mono Line mux */
423static const struct snd_kcontrol_new wm8753_line_mono_controls =
424SOC_DAPM_ENUM("Route", wm8753_enum[12]);
425
426/* Line mux and mixer */
427static const struct snd_kcontrol_new wm8753_line_mux_mix_controls =
428SOC_DAPM_ENUM("Route", wm8753_enum[11]);
429
430/* Rx mux and mixer */
431static const struct snd_kcontrol_new wm8753_rx_mux_mix_controls =
432SOC_DAPM_ENUM("Route", wm8753_enum[15]);
433
434/* Mic Selector Mux */
435static const struct snd_kcontrol_new wm8753_mic_sel_mux_controls =
436SOC_DAPM_ENUM("Route", wm8753_enum[25]);
437
438static const struct snd_soc_dapm_widget wm8753_dapm_widgets[] = {
439SND_SOC_DAPM_MICBIAS("Mic Bias", WM8753_PWR1, 5, 0),
440SND_SOC_DAPM_MIXER("Left Mixer", WM8753_PWR4, 0, 0,
441 &wm8753_left_mixer_controls[0], ARRAY_SIZE(wm8753_left_mixer_controls)),
442SND_SOC_DAPM_PGA("Left Out 1", WM8753_PWR3, 8, 0, NULL, 0),
443SND_SOC_DAPM_PGA("Left Out 2", WM8753_PWR3, 6, 0, NULL, 0),
444SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback", WM8753_PWR1, 3, 0),
445SND_SOC_DAPM_OUTPUT("LOUT1"),
446SND_SOC_DAPM_OUTPUT("LOUT2"),
447SND_SOC_DAPM_MIXER("Right Mixer", WM8753_PWR4, 1, 0,
448 &wm8753_right_mixer_controls[0], ARRAY_SIZE(wm8753_right_mixer_controls)),
449SND_SOC_DAPM_PGA("Right Out 1", WM8753_PWR3, 7, 0, NULL, 0),
450SND_SOC_DAPM_PGA("Right Out 2", WM8753_PWR3, 5, 0, NULL, 0),
451SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback", WM8753_PWR1, 2, 0),
452SND_SOC_DAPM_OUTPUT("ROUT1"),
453SND_SOC_DAPM_OUTPUT("ROUT2"),
454SND_SOC_DAPM_MIXER("Mono Mixer", WM8753_PWR4, 2, 0,
455 &wm8753_mono_mixer_controls[0], ARRAY_SIZE(wm8753_mono_mixer_controls)),
456SND_SOC_DAPM_PGA("Mono Out 1", WM8753_PWR3, 2, 0, NULL, 0),
457SND_SOC_DAPM_PGA("Mono Out 2", WM8753_PWR3, 1, 0, NULL, 0),
458SND_SOC_DAPM_DAC("Voice DAC", "Voice Playback", WM8753_PWR1, 4, 0),
459SND_SOC_DAPM_OUTPUT("MONO1"),
460SND_SOC_DAPM_MUX("Mono 2 Mux", SND_SOC_NOPM, 0, 0, &wm8753_mono2_controls),
461SND_SOC_DAPM_OUTPUT("MONO2"),
462SND_SOC_DAPM_MIXER("Out3 Left + Right", -1, 0, 0, NULL, 0),
463SND_SOC_DAPM_MUX("Out3 Mux", SND_SOC_NOPM, 0, 0, &wm8753_out3_controls),
464SND_SOC_DAPM_PGA("Out 3", WM8753_PWR3, 4, 0, NULL, 0),
465SND_SOC_DAPM_OUTPUT("OUT3"),
466SND_SOC_DAPM_MUX("Out4 Mux", SND_SOC_NOPM, 0, 0, &wm8753_out4_controls),
467SND_SOC_DAPM_PGA("Out 4", WM8753_PWR3, 3, 0, NULL, 0),
468SND_SOC_DAPM_OUTPUT("OUT4"),
469SND_SOC_DAPM_MIXER("Playback Mixer", WM8753_PWR4, 3, 0,
470 &wm8753_record_mixer_controls[0],
471 ARRAY_SIZE(wm8753_record_mixer_controls)),
472SND_SOC_DAPM_ADC("Left ADC", "Left Capture", WM8753_PWR2, 3, 0),
473SND_SOC_DAPM_ADC("Right ADC", "Right Capture", WM8753_PWR2, 2, 0),
474SND_SOC_DAPM_MUX("Capture Left Mixer", SND_SOC_NOPM, 0, 0,
475 &wm8753_adc_mono_controls),
476SND_SOC_DAPM_MUX("Capture Right Mixer", SND_SOC_NOPM, 0, 0,
477 &wm8753_adc_mono_controls),
478SND_SOC_DAPM_MUX("Capture Left Mux", SND_SOC_NOPM, 0, 0,
479 &wm8753_adc_left_controls),
480SND_SOC_DAPM_MUX("Capture Right Mux", SND_SOC_NOPM, 0, 0,
481 &wm8753_adc_right_controls),
482SND_SOC_DAPM_MUX("Mic Sidetone Mux", SND_SOC_NOPM, 0, 0,
483 &wm8753_mic_mux_controls),
484SND_SOC_DAPM_PGA("Left Capture Volume", WM8753_PWR2, 5, 0, NULL, 0),
485SND_SOC_DAPM_PGA("Right Capture Volume", WM8753_PWR2, 4, 0, NULL, 0),
486SND_SOC_DAPM_MIXER("ALC Mixer", WM8753_PWR2, 6, 0,
487 &wm8753_alc_mixer_controls[0], ARRAY_SIZE(wm8753_alc_mixer_controls)),
488SND_SOC_DAPM_MUX("Line Left Mux", SND_SOC_NOPM, 0, 0,
489 &wm8753_line_left_controls),
490SND_SOC_DAPM_MUX("Line Right Mux", SND_SOC_NOPM, 0, 0,
491 &wm8753_line_right_controls),
492SND_SOC_DAPM_MUX("Line Mono Mux", SND_SOC_NOPM, 0, 0,
493 &wm8753_line_mono_controls),
494SND_SOC_DAPM_MUX("Line Mixer", WM8753_PWR2, 0, 0,
495 &wm8753_line_mux_mix_controls),
496SND_SOC_DAPM_MUX("Rx Mixer", WM8753_PWR2, 1, 0,
497 &wm8753_rx_mux_mix_controls),
498SND_SOC_DAPM_PGA("Mic 1 Volume", WM8753_PWR2, 8, 0, NULL, 0),
499SND_SOC_DAPM_PGA("Mic 2 Volume", WM8753_PWR2, 7, 0, NULL, 0),
500SND_SOC_DAPM_MUX("Mic Selection Mux", SND_SOC_NOPM, 0, 0,
501 &wm8753_mic_sel_mux_controls),
502SND_SOC_DAPM_INPUT("LINE1"),
503SND_SOC_DAPM_INPUT("LINE2"),
504SND_SOC_DAPM_INPUT("RXP"),
505SND_SOC_DAPM_INPUT("RXN"),
506SND_SOC_DAPM_INPUT("ACIN"),
507SND_SOC_DAPM_OUTPUT("ACOP"),
508SND_SOC_DAPM_INPUT("MIC1N"),
509SND_SOC_DAPM_INPUT("MIC1"),
510SND_SOC_DAPM_INPUT("MIC2N"),
511SND_SOC_DAPM_INPUT("MIC2"),
512SND_SOC_DAPM_VMID("VREF"),
513};
514
Mark Browna65f0562008-05-13 14:54:43 +0200515static const struct snd_soc_dapm_route audio_map[] = {
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200516 /* left mixer */
517 {"Left Mixer", "Left Playback Switch", "Left DAC"},
518 {"Left Mixer", "Voice Playback Switch", "Voice DAC"},
519 {"Left Mixer", "Sidetone Playback Switch", "Mic Sidetone Mux"},
520 {"Left Mixer", "Bypass Playback Switch", "Line Left Mux"},
521
522 /* right mixer */
523 {"Right Mixer", "Right Playback Switch", "Right DAC"},
524 {"Right Mixer", "Voice Playback Switch", "Voice DAC"},
525 {"Right Mixer", "Sidetone Playback Switch", "Mic Sidetone Mux"},
526 {"Right Mixer", "Bypass Playback Switch", "Line Right Mux"},
527
528 /* mono mixer */
529 {"Mono Mixer", "Voice Playback Switch", "Voice DAC"},
530 {"Mono Mixer", "Left Playback Switch", "Left DAC"},
531 {"Mono Mixer", "Right Playback Switch", "Right DAC"},
532 {"Mono Mixer", "Sidetone Playback Switch", "Mic Sidetone Mux"},
533 {"Mono Mixer", "Bypass Playback Switch", "Line Mono Mux"},
534
535 /* left out */
536 {"Left Out 1", NULL, "Left Mixer"},
537 {"Left Out 2", NULL, "Left Mixer"},
538 {"LOUT1", NULL, "Left Out 1"},
539 {"LOUT2", NULL, "Left Out 2"},
540
541 /* right out */
542 {"Right Out 1", NULL, "Right Mixer"},
543 {"Right Out 2", NULL, "Right Mixer"},
544 {"ROUT1", NULL, "Right Out 1"},
545 {"ROUT2", NULL, "Right Out 2"},
546
547 /* mono 1 out */
548 {"Mono Out 1", NULL, "Mono Mixer"},
549 {"MONO1", NULL, "Mono Out 1"},
550
551 /* mono 2 out */
552 {"Mono 2 Mux", "Left + Right", "Out3 Left + Right"},
553 {"Mono 2 Mux", "Inverted Mono 1", "MONO1"},
554 {"Mono 2 Mux", "Left", "Left Mixer"},
555 {"Mono 2 Mux", "Right", "Right Mixer"},
556 {"Mono Out 2", NULL, "Mono 2 Mux"},
557 {"MONO2", NULL, "Mono Out 2"},
558
559 /* out 3 */
560 {"Out3 Left + Right", NULL, "Left Mixer"},
561 {"Out3 Left + Right", NULL, "Right Mixer"},
562 {"Out3 Mux", "VREF", "VREF"},
563 {"Out3 Mux", "Left + Right", "Out3 Left + Right"},
564 {"Out3 Mux", "ROUT2", "ROUT2"},
565 {"Out 3", NULL, "Out3 Mux"},
566 {"OUT3", NULL, "Out 3"},
567
568 /* out 4 */
569 {"Out4 Mux", "VREF", "VREF"},
Rob Sims40373142008-10-01 21:47:31 +0200570 {"Out4 Mux", "Capture ST", "Playback Mixer"},
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200571 {"Out4 Mux", "LOUT2", "LOUT2"},
572 {"Out 4", NULL, "Out4 Mux"},
573 {"OUT4", NULL, "Out 4"},
574
575 /* record mixer */
576 {"Playback Mixer", "Left Capture Switch", "Left Mixer"},
577 {"Playback Mixer", "Voice Capture Switch", "Mono Mixer"},
578 {"Playback Mixer", "Right Capture Switch", "Right Mixer"},
579
580 /* Mic/SideTone Mux */
581 {"Mic Sidetone Mux", "Left PGA", "Left Capture Volume"},
582 {"Mic Sidetone Mux", "Right PGA", "Right Capture Volume"},
583 {"Mic Sidetone Mux", "Mic 1", "Mic 1 Volume"},
584 {"Mic Sidetone Mux", "Mic 2", "Mic 2 Volume"},
585
586 /* Capture Left Mux */
587 {"Capture Left Mux", "PGA", "Left Capture Volume"},
588 {"Capture Left Mux", "Line or RXP-RXN", "Line Left Mux"},
589 {"Capture Left Mux", "Line", "LINE1"},
590
591 /* Capture Right Mux */
592 {"Capture Right Mux", "PGA", "Right Capture Volume"},
593 {"Capture Right Mux", "Line or RXP-RXN", "Line Right Mux"},
Rob Sims40373142008-10-01 21:47:31 +0200594 {"Capture Right Mux", "Sidetone", "Playback Mixer"},
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200595
596 /* Mono Capture mixer-mux */
597 {"Capture Right Mixer", "Stereo", "Capture Right Mux"},
Phil Vandry877ae702009-09-21 11:36:08 -0400598 {"Capture Left Mixer", "Stereo", "Capture Left Mux"},
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200599 {"Capture Left Mixer", "Analogue Mix Left", "Capture Left Mux"},
600 {"Capture Left Mixer", "Analogue Mix Left", "Capture Right Mux"},
601 {"Capture Right Mixer", "Analogue Mix Right", "Capture Left Mux"},
602 {"Capture Right Mixer", "Analogue Mix Right", "Capture Right Mux"},
603 {"Capture Left Mixer", "Digital Mono Mix", "Capture Left Mux"},
604 {"Capture Left Mixer", "Digital Mono Mix", "Capture Right Mux"},
605 {"Capture Right Mixer", "Digital Mono Mix", "Capture Left Mux"},
606 {"Capture Right Mixer", "Digital Mono Mix", "Capture Right Mux"},
607
608 /* ADC */
609 {"Left ADC", NULL, "Capture Left Mixer"},
610 {"Right ADC", NULL, "Capture Right Mixer"},
611
612 /* Left Capture Volume */
613 {"Left Capture Volume", NULL, "ACIN"},
614
615 /* Right Capture Volume */
616 {"Right Capture Volume", NULL, "Mic 2 Volume"},
617
618 /* ALC Mixer */
619 {"ALC Mixer", "Line Capture Switch", "Line Mixer"},
620 {"ALC Mixer", "Mic2 Capture Switch", "Mic 2 Volume"},
621 {"ALC Mixer", "Mic1 Capture Switch", "Mic 1 Volume"},
622 {"ALC Mixer", "Rx Capture Switch", "Rx Mixer"},
623
624 /* Line Left Mux */
625 {"Line Left Mux", "Line 1", "LINE1"},
626 {"Line Left Mux", "Rx Mix", "Rx Mixer"},
627
628 /* Line Right Mux */
629 {"Line Right Mux", "Line 2", "LINE2"},
630 {"Line Right Mux", "Rx Mix", "Rx Mixer"},
631
632 /* Line Mono Mux */
633 {"Line Mono Mux", "Line Mix", "Line Mixer"},
634 {"Line Mono Mux", "Rx Mix", "Rx Mixer"},
635
636 /* Line Mixer/Mux */
637 {"Line Mixer", "Line 1 + 2", "LINE1"},
638 {"Line Mixer", "Line 1 - 2", "LINE1"},
639 {"Line Mixer", "Line 1 + 2", "LINE2"},
640 {"Line Mixer", "Line 1 - 2", "LINE2"},
641 {"Line Mixer", "Line 1", "LINE1"},
642 {"Line Mixer", "Line 2", "LINE2"},
643
644 /* Rx Mixer/Mux */
645 {"Rx Mixer", "RXP - RXN", "RXP"},
646 {"Rx Mixer", "RXP + RXN", "RXP"},
647 {"Rx Mixer", "RXP - RXN", "RXN"},
648 {"Rx Mixer", "RXP + RXN", "RXN"},
649 {"Rx Mixer", "RXP", "RXP"},
650 {"Rx Mixer", "RXN", "RXN"},
651
652 /* Mic 1 Volume */
653 {"Mic 1 Volume", NULL, "MIC1N"},
654 {"Mic 1 Volume", NULL, "Mic Selection Mux"},
655
656 /* Mic 2 Volume */
657 {"Mic 2 Volume", NULL, "MIC2N"},
658 {"Mic 2 Volume", NULL, "MIC2"},
659
660 /* Mic Selector Mux */
661 {"Mic Selection Mux", "Mic 1", "MIC1"},
662 {"Mic Selection Mux", "Mic 2", "MIC2N"},
663 {"Mic Selection Mux", "Mic 3", "MIC2"},
664
665 /* ACOP */
666 {"ACOP", NULL, "ALC Mixer"},
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200667};
668
669static int wm8753_add_widgets(struct snd_soc_codec *codec)
670{
Mark Browna65f0562008-05-13 14:54:43 +0200671 snd_soc_dapm_new_controls(codec, wm8753_dapm_widgets,
672 ARRAY_SIZE(wm8753_dapm_widgets));
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200673
Mark Browna65f0562008-05-13 14:54:43 +0200674 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200675
676 snd_soc_dapm_new_widgets(codec);
677 return 0;
678}
679
680/* PLL divisors */
681struct _pll_div {
682 u32 div2:1;
683 u32 n:4;
684 u32 k:24;
685};
686
687/* The size in bits of the pll divide multiplied by 10
688 * to allow rounding later */
689#define FIXED_PLL_SIZE ((1 << 22) * 10)
690
691static void pll_factors(struct _pll_div *pll_div, unsigned int target,
692 unsigned int source)
693{
694 u64 Kpart;
695 unsigned int K, Ndiv, Nmod;
696
697 Ndiv = target / source;
698 if (Ndiv < 6) {
699 source >>= 1;
700 pll_div->div2 = 1;
701 Ndiv = target / source;
702 } else
703 pll_div->div2 = 0;
704
705 if ((Ndiv < 6) || (Ndiv > 12))
706 printk(KERN_WARNING
Roel Kluin449bd542009-05-27 17:08:39 -0700707 "wm8753: unsupported N = %u\n", Ndiv);
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200708
709 pll_div->n = Ndiv;
710 Nmod = target % source;
711 Kpart = FIXED_PLL_SIZE * (long long)Nmod;
712
713 do_div(Kpart, source);
714
715 K = Kpart & 0xFFFFFFFF;
716
717 /* Check if we need to round */
718 if ((K % 10) >= 5)
719 K += 5;
720
721 /* Move down to proper range now rounding is done */
722 K /= 10;
723
724 pll_div->k = K;
725}
726
Liam Girdwoode550e172008-07-07 16:07:52 +0100727static int wm8753_set_dai_pll(struct snd_soc_dai *codec_dai,
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200728 int pll_id, unsigned int freq_in, unsigned int freq_out)
729{
730 u16 reg, enable;
731 int offset;
732 struct snd_soc_codec *codec = codec_dai->codec;
733
734 if (pll_id < WM8753_PLL1 || pll_id > WM8753_PLL2)
735 return -ENODEV;
736
737 if (pll_id == WM8753_PLL1) {
738 offset = 0;
739 enable = 0x10;
740 reg = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0xffef;
741 } else {
742 offset = 4;
743 enable = 0x8;
744 reg = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0xfff7;
745 }
746
747 if (!freq_in || !freq_out) {
748 /* disable PLL */
749 wm8753_write(codec, WM8753_PLL1CTL1 + offset, 0x0026);
750 wm8753_write(codec, WM8753_CLOCK, reg);
751 return 0;
752 } else {
753 u16 value = 0;
754 struct _pll_div pll_div;
755
756 pll_factors(&pll_div, freq_out * 8, freq_in);
757
758 /* set up N and K PLL divisor ratios */
759 /* bits 8:5 = PLL_N, bits 3:0 = PLL_K[21:18] */
760 value = (pll_div.n << 5) + ((pll_div.k & 0x3c0000) >> 18);
761 wm8753_write(codec, WM8753_PLL1CTL2 + offset, value);
762
763 /* bits 8:0 = PLL_K[17:9] */
764 value = (pll_div.k & 0x03fe00) >> 9;
765 wm8753_write(codec, WM8753_PLL1CTL3 + offset, value);
766
767 /* bits 8:0 = PLL_K[8:0] */
768 value = pll_div.k & 0x0001ff;
769 wm8753_write(codec, WM8753_PLL1CTL4 + offset, value);
770
771 /* set PLL as input and enable */
772 wm8753_write(codec, WM8753_PLL1CTL1 + offset, 0x0027 |
773 (pll_div.div2 << 3));
774 wm8753_write(codec, WM8753_CLOCK, reg | enable);
775 }
776 return 0;
777}
778
779struct _coeff_div {
780 u32 mclk;
781 u32 rate;
782 u8 sr:5;
783 u8 usb:1;
784};
785
786/* codec hifi mclk (after PLL) clock divider coefficients */
787static const struct _coeff_div coeff_div[] = {
788 /* 8k */
789 {12288000, 8000, 0x6, 0x0},
790 {11289600, 8000, 0x16, 0x0},
791 {18432000, 8000, 0x7, 0x0},
792 {16934400, 8000, 0x17, 0x0},
793 {12000000, 8000, 0x6, 0x1},
794
795 /* 11.025k */
796 {11289600, 11025, 0x18, 0x0},
797 {16934400, 11025, 0x19, 0x0},
798 {12000000, 11025, 0x19, 0x1},
799
800 /* 16k */
801 {12288000, 16000, 0xa, 0x0},
802 {18432000, 16000, 0xb, 0x0},
803 {12000000, 16000, 0xa, 0x1},
804
805 /* 22.05k */
806 {11289600, 22050, 0x1a, 0x0},
807 {16934400, 22050, 0x1b, 0x0},
808 {12000000, 22050, 0x1b, 0x1},
809
810 /* 32k */
811 {12288000, 32000, 0xc, 0x0},
812 {18432000, 32000, 0xd, 0x0},
813 {12000000, 32000, 0xa, 0x1},
814
815 /* 44.1k */
816 {11289600, 44100, 0x10, 0x0},
817 {16934400, 44100, 0x11, 0x0},
818 {12000000, 44100, 0x11, 0x1},
819
820 /* 48k */
821 {12288000, 48000, 0x0, 0x0},
822 {18432000, 48000, 0x1, 0x0},
823 {12000000, 48000, 0x0, 0x1},
824
825 /* 88.2k */
826 {11289600, 88200, 0x1e, 0x0},
827 {16934400, 88200, 0x1f, 0x0},
828 {12000000, 88200, 0x1f, 0x1},
829
830 /* 96k */
831 {12288000, 96000, 0xe, 0x0},
832 {18432000, 96000, 0xf, 0x0},
833 {12000000, 96000, 0xe, 0x1},
834};
835
836static int get_coeff(int mclk, int rate)
837{
838 int i;
839
840 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
841 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
842 return i;
843 }
844 return -EINVAL;
845}
846
847/*
848 * Clock after PLL and dividers
849 */
Liam Girdwoode550e172008-07-07 16:07:52 +0100850static int wm8753_set_dai_sysclk(struct snd_soc_dai *codec_dai,
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200851 int clk_id, unsigned int freq, int dir)
852{
853 struct snd_soc_codec *codec = codec_dai->codec;
854 struct wm8753_priv *wm8753 = codec->private_data;
855
856 switch (freq) {
857 case 11289600:
858 case 12000000:
859 case 12288000:
860 case 16934400:
861 case 18432000:
862 if (clk_id == WM8753_MCLK) {
863 wm8753->sysclk = freq;
864 return 0;
865 } else if (clk_id == WM8753_PCMCLK) {
866 wm8753->pcmclk = freq;
867 return 0;
868 }
869 break;
870 }
871 return -EINVAL;
872}
873
874/*
875 * Set's ADC and Voice DAC format.
876 */
Liam Girdwoode550e172008-07-07 16:07:52 +0100877static int wm8753_vdac_adc_set_dai_fmt(struct snd_soc_dai *codec_dai,
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200878 unsigned int fmt)
879{
880 struct snd_soc_codec *codec = codec_dai->codec;
881 u16 voice = wm8753_read_reg_cache(codec, WM8753_PCM) & 0x01ec;
882
883 /* interface format */
884 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
885 case SND_SOC_DAIFMT_I2S:
886 voice |= 0x0002;
887 break;
888 case SND_SOC_DAIFMT_RIGHT_J:
889 break;
890 case SND_SOC_DAIFMT_LEFT_J:
891 voice |= 0x0001;
892 break;
893 case SND_SOC_DAIFMT_DSP_A:
894 voice |= 0x0003;
895 break;
896 case SND_SOC_DAIFMT_DSP_B:
897 voice |= 0x0013;
898 break;
899 default:
900 return -EINVAL;
901 }
902
903 wm8753_write(codec, WM8753_PCM, voice);
904 return 0;
905}
906
907/*
908 * Set PCM DAI bit size and sample rate.
909 */
910static int wm8753_pcm_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000911 struct snd_pcm_hw_params *params,
912 struct snd_soc_dai *dai)
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200913{
914 struct snd_soc_pcm_runtime *rtd = substream->private_data;
915 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000916 struct snd_soc_codec *codec = socdev->card->codec;
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200917 struct wm8753_priv *wm8753 = codec->private_data;
918 u16 voice = wm8753_read_reg_cache(codec, WM8753_PCM) & 0x01f3;
919 u16 srate = wm8753_read_reg_cache(codec, WM8753_SRATE1) & 0x017f;
920
921 /* bit size */
922 switch (params_format(params)) {
923 case SNDRV_PCM_FORMAT_S16_LE:
924 break;
925 case SNDRV_PCM_FORMAT_S20_3LE:
926 voice |= 0x0004;
927 break;
928 case SNDRV_PCM_FORMAT_S24_LE:
929 voice |= 0x0008;
930 break;
931 case SNDRV_PCM_FORMAT_S32_LE:
932 voice |= 0x000c;
933 break;
934 }
935
936 /* sample rate */
937 if (params_rate(params) * 384 == wm8753->pcmclk)
938 srate |= 0x80;
939 wm8753_write(codec, WM8753_SRATE1, srate);
940
941 wm8753_write(codec, WM8753_PCM, voice);
942 return 0;
943}
944
945/*
946 * Set's PCM dai fmt and BCLK.
947 */
Liam Girdwoode550e172008-07-07 16:07:52 +0100948static int wm8753_pcm_set_dai_fmt(struct snd_soc_dai *codec_dai,
Liam Girdwood1f53aee2007-04-16 19:17:44 +0200949 unsigned int fmt)
950{
951 struct snd_soc_codec *codec = codec_dai->codec;
952 u16 voice, ioctl;
953
954 voice = wm8753_read_reg_cache(codec, WM8753_PCM) & 0x011f;
955 ioctl = wm8753_read_reg_cache(codec, WM8753_IOCTL) & 0x015d;
956
957 /* set master/slave audio interface */
958 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
959 case SND_SOC_DAIFMT_CBS_CFS:
960 break;
961 case SND_SOC_DAIFMT_CBM_CFM:
962 ioctl |= 0x2;
963 case SND_SOC_DAIFMT_CBM_CFS:
964 voice |= 0x0040;
965 break;
966 default:
967 return -EINVAL;
968 }
969
970 /* clock inversion */
971 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
972 case SND_SOC_DAIFMT_DSP_A:
973 case SND_SOC_DAIFMT_DSP_B:
974 /* frame inversion not valid for DSP modes */
975 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
976 case SND_SOC_DAIFMT_NB_NF:
977 break;
978 case SND_SOC_DAIFMT_IB_NF:
979 voice |= 0x0080;
980 break;
981 default:
982 return -EINVAL;
983 }
984 break;
985 case SND_SOC_DAIFMT_I2S:
986 case SND_SOC_DAIFMT_RIGHT_J:
987 case SND_SOC_DAIFMT_LEFT_J:
988 voice &= ~0x0010;
989 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
990 case SND_SOC_DAIFMT_NB_NF:
991 break;
992 case SND_SOC_DAIFMT_IB_IF:
993 voice |= 0x0090;
994 break;
995 case SND_SOC_DAIFMT_IB_NF:
996 voice |= 0x0080;
997 break;
998 case SND_SOC_DAIFMT_NB_IF:
999 voice |= 0x0010;
1000 break;
1001 default:
1002 return -EINVAL;
1003 }
1004 break;
1005 default:
1006 return -EINVAL;
1007 }
1008
1009 wm8753_write(codec, WM8753_PCM, voice);
1010 wm8753_write(codec, WM8753_IOCTL, ioctl);
1011 return 0;
1012}
1013
Liam Girdwoode550e172008-07-07 16:07:52 +01001014static int wm8753_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001015 int div_id, int div)
1016{
1017 struct snd_soc_codec *codec = codec_dai->codec;
1018 u16 reg;
1019
1020 switch (div_id) {
1021 case WM8753_PCMDIV:
1022 reg = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0x003f;
1023 wm8753_write(codec, WM8753_CLOCK, reg | div);
1024 break;
1025 case WM8753_BCLKDIV:
1026 reg = wm8753_read_reg_cache(codec, WM8753_SRATE2) & 0x01c7;
1027 wm8753_write(codec, WM8753_SRATE2, reg | div);
1028 break;
1029 case WM8753_VXCLKDIV:
1030 reg = wm8753_read_reg_cache(codec, WM8753_SRATE2) & 0x003f;
1031 wm8753_write(codec, WM8753_SRATE2, reg | div);
1032 break;
1033 default:
1034 return -EINVAL;
1035 }
1036 return 0;
1037}
1038
1039/*
1040 * Set's HiFi DAC format.
1041 */
Liam Girdwoode550e172008-07-07 16:07:52 +01001042static int wm8753_hdac_set_dai_fmt(struct snd_soc_dai *codec_dai,
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001043 unsigned int fmt)
1044{
1045 struct snd_soc_codec *codec = codec_dai->codec;
1046 u16 hifi = wm8753_read_reg_cache(codec, WM8753_HIFI) & 0x01e0;
1047
1048 /* interface format */
1049 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1050 case SND_SOC_DAIFMT_I2S:
1051 hifi |= 0x0002;
1052 break;
1053 case SND_SOC_DAIFMT_RIGHT_J:
1054 break;
1055 case SND_SOC_DAIFMT_LEFT_J:
1056 hifi |= 0x0001;
1057 break;
1058 case SND_SOC_DAIFMT_DSP_A:
1059 hifi |= 0x0003;
1060 break;
1061 case SND_SOC_DAIFMT_DSP_B:
1062 hifi |= 0x0013;
1063 break;
1064 default:
1065 return -EINVAL;
1066 }
1067
1068 wm8753_write(codec, WM8753_HIFI, hifi);
1069 return 0;
1070}
1071
1072/*
1073 * Set's I2S DAI format.
1074 */
Liam Girdwoode550e172008-07-07 16:07:52 +01001075static int wm8753_i2s_set_dai_fmt(struct snd_soc_dai *codec_dai,
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001076 unsigned int fmt)
1077{
1078 struct snd_soc_codec *codec = codec_dai->codec;
1079 u16 ioctl, hifi;
1080
1081 hifi = wm8753_read_reg_cache(codec, WM8753_HIFI) & 0x011f;
1082 ioctl = wm8753_read_reg_cache(codec, WM8753_IOCTL) & 0x00ae;
1083
1084 /* set master/slave audio interface */
1085 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1086 case SND_SOC_DAIFMT_CBS_CFS:
1087 break;
1088 case SND_SOC_DAIFMT_CBM_CFM:
1089 ioctl |= 0x1;
1090 case SND_SOC_DAIFMT_CBM_CFS:
1091 hifi |= 0x0040;
1092 break;
1093 default:
1094 return -EINVAL;
1095 }
1096
1097 /* clock inversion */
1098 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1099 case SND_SOC_DAIFMT_DSP_A:
1100 case SND_SOC_DAIFMT_DSP_B:
1101 /* frame inversion not valid for DSP modes */
1102 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1103 case SND_SOC_DAIFMT_NB_NF:
1104 break;
1105 case SND_SOC_DAIFMT_IB_NF:
1106 hifi |= 0x0080;
1107 break;
1108 default:
1109 return -EINVAL;
1110 }
1111 break;
1112 case SND_SOC_DAIFMT_I2S:
1113 case SND_SOC_DAIFMT_RIGHT_J:
1114 case SND_SOC_DAIFMT_LEFT_J:
1115 hifi &= ~0x0010;
1116 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1117 case SND_SOC_DAIFMT_NB_NF:
1118 break;
1119 case SND_SOC_DAIFMT_IB_IF:
1120 hifi |= 0x0090;
1121 break;
1122 case SND_SOC_DAIFMT_IB_NF:
1123 hifi |= 0x0080;
1124 break;
1125 case SND_SOC_DAIFMT_NB_IF:
1126 hifi |= 0x0010;
1127 break;
1128 default:
1129 return -EINVAL;
1130 }
1131 break;
1132 default:
1133 return -EINVAL;
1134 }
1135
1136 wm8753_write(codec, WM8753_HIFI, hifi);
1137 wm8753_write(codec, WM8753_IOCTL, ioctl);
1138 return 0;
1139}
1140
1141/*
1142 * Set PCM DAI bit size and sample rate.
1143 */
1144static int wm8753_i2s_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +00001145 struct snd_pcm_hw_params *params,
1146 struct snd_soc_dai *dai)
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001147{
1148 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1149 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown6627a652009-01-23 22:55:23 +00001150 struct snd_soc_codec *codec = socdev->card->codec;
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001151 struct wm8753_priv *wm8753 = codec->private_data;
1152 u16 srate = wm8753_read_reg_cache(codec, WM8753_SRATE1) & 0x01c0;
1153 u16 hifi = wm8753_read_reg_cache(codec, WM8753_HIFI) & 0x01f3;
1154 int coeff;
1155
1156 /* is digital filter coefficient valid ? */
1157 coeff = get_coeff(wm8753->sysclk, params_rate(params));
1158 if (coeff < 0) {
1159 printk(KERN_ERR "wm8753 invalid MCLK or rate\n");
1160 return coeff;
1161 }
1162 wm8753_write(codec, WM8753_SRATE1, srate | (coeff_div[coeff].sr << 1) |
1163 coeff_div[coeff].usb);
1164
1165 /* bit size */
1166 switch (params_format(params)) {
1167 case SNDRV_PCM_FORMAT_S16_LE:
1168 break;
1169 case SNDRV_PCM_FORMAT_S20_3LE:
1170 hifi |= 0x0004;
1171 break;
1172 case SNDRV_PCM_FORMAT_S24_LE:
1173 hifi |= 0x0008;
1174 break;
1175 case SNDRV_PCM_FORMAT_S32_LE:
1176 hifi |= 0x000c;
1177 break;
1178 }
1179
1180 wm8753_write(codec, WM8753_HIFI, hifi);
1181 return 0;
1182}
1183
Liam Girdwoode550e172008-07-07 16:07:52 +01001184static int wm8753_mode1v_set_dai_fmt(struct snd_soc_dai *codec_dai,
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001185 unsigned int fmt)
1186{
1187 struct snd_soc_codec *codec = codec_dai->codec;
1188 u16 clock;
1189
1190 /* set clk source as pcmclk */
1191 clock = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0xfffb;
1192 wm8753_write(codec, WM8753_CLOCK, clock);
1193
1194 if (wm8753_vdac_adc_set_dai_fmt(codec_dai, fmt) < 0)
1195 return -EINVAL;
1196 return wm8753_pcm_set_dai_fmt(codec_dai, fmt);
1197}
1198
Liam Girdwoode550e172008-07-07 16:07:52 +01001199static int wm8753_mode1h_set_dai_fmt(struct snd_soc_dai *codec_dai,
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001200 unsigned int fmt)
1201{
1202 if (wm8753_hdac_set_dai_fmt(codec_dai, fmt) < 0)
1203 return -EINVAL;
1204 return wm8753_i2s_set_dai_fmt(codec_dai, fmt);
1205}
1206
Liam Girdwoode550e172008-07-07 16:07:52 +01001207static int wm8753_mode2_set_dai_fmt(struct snd_soc_dai *codec_dai,
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001208 unsigned int fmt)
1209{
1210 struct snd_soc_codec *codec = codec_dai->codec;
1211 u16 clock;
1212
1213 /* set clk source as pcmclk */
1214 clock = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0xfffb;
1215 wm8753_write(codec, WM8753_CLOCK, clock);
1216
1217 if (wm8753_vdac_adc_set_dai_fmt(codec_dai, fmt) < 0)
1218 return -EINVAL;
1219 return wm8753_i2s_set_dai_fmt(codec_dai, fmt);
1220}
1221
Liam Girdwoode550e172008-07-07 16:07:52 +01001222static int wm8753_mode3_4_set_dai_fmt(struct snd_soc_dai *codec_dai,
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001223 unsigned int fmt)
1224{
1225 struct snd_soc_codec *codec = codec_dai->codec;
1226 u16 clock;
1227
1228 /* set clk source as mclk */
1229 clock = wm8753_read_reg_cache(codec, WM8753_CLOCK) & 0xfffb;
1230 wm8753_write(codec, WM8753_CLOCK, clock | 0x4);
1231
1232 if (wm8753_hdac_set_dai_fmt(codec_dai, fmt) < 0)
1233 return -EINVAL;
1234 if (wm8753_vdac_adc_set_dai_fmt(codec_dai, fmt) < 0)
1235 return -EINVAL;
1236 return wm8753_i2s_set_dai_fmt(codec_dai, fmt);
1237}
1238
Liam Girdwoode550e172008-07-07 16:07:52 +01001239static int wm8753_mute(struct snd_soc_dai *dai, int mute)
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001240{
1241 struct snd_soc_codec *codec = dai->codec;
1242 u16 mute_reg = wm8753_read_reg_cache(codec, WM8753_DAC) & 0xfff7;
1243
1244 /* the digital mute covers the HiFi and Voice DAC's on the WM8753.
1245 * make sure we check if they are not both active when we mute */
1246 if (mute && dai->id == 1) {
1247 if (!wm8753_dai[WM8753_DAI_VOICE].playback.active ||
1248 !wm8753_dai[WM8753_DAI_HIFI].playback.active)
1249 wm8753_write(codec, WM8753_DAC, mute_reg | 0x8);
1250 } else {
1251 if (mute)
1252 wm8753_write(codec, WM8753_DAC, mute_reg | 0x8);
1253 else
1254 wm8753_write(codec, WM8753_DAC, mute_reg);
1255 }
1256
1257 return 0;
1258}
1259
Mark Brown0be98982008-05-19 12:31:28 +02001260static int wm8753_set_bias_level(struct snd_soc_codec *codec,
1261 enum snd_soc_bias_level level)
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001262{
1263 u16 pwr_reg = wm8753_read_reg_cache(codec, WM8753_PWR1) & 0xfe3e;
1264
Mark Brown0be98982008-05-19 12:31:28 +02001265 switch (level) {
1266 case SND_SOC_BIAS_ON:
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001267 /* set vmid to 50k and unmute dac */
1268 wm8753_write(codec, WM8753_PWR1, pwr_reg | 0x00c0);
1269 break;
Mark Brown0be98982008-05-19 12:31:28 +02001270 case SND_SOC_BIAS_PREPARE:
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001271 /* set vmid to 5k for quick power up */
1272 wm8753_write(codec, WM8753_PWR1, pwr_reg | 0x01c1);
1273 break;
Mark Brown0be98982008-05-19 12:31:28 +02001274 case SND_SOC_BIAS_STANDBY:
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001275 /* mute dac and set vmid to 500k, enable VREF */
1276 wm8753_write(codec, WM8753_PWR1, pwr_reg | 0x0141);
1277 break;
Mark Brown0be98982008-05-19 12:31:28 +02001278 case SND_SOC_BIAS_OFF:
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001279 wm8753_write(codec, WM8753_PWR1, 0x0001);
1280 break;
1281 }
Mark Brown0be98982008-05-19 12:31:28 +02001282 codec->bias_level = level;
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001283 return 0;
1284}
1285
1286#define WM8753_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
Mark Brown60fc6842008-04-30 17:18:43 +02001287 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
1288 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
1289 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001290
1291#define WM8753_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
1292 SNDRV_PCM_FMTBIT_S24_LE)
1293
1294/*
1295 * The WM8753 supports upto 4 different and mutually exclusive DAI
1296 * configurations. This gives 2 PCM's available for use, hifi and voice.
1297 * NOTE: The Voice PCM cannot play or capture audio to the CPU as it's DAI
1298 * is connected between the wm8753 and a BT codec or GSM modem.
1299 *
1300 * 1. Voice over PCM DAI - HIFI DAC over HIFI DAI
1301 * 2. Voice over HIFI DAI - HIFI disabled
1302 * 3. Voice disabled - HIFI over HIFI
1303 * 4. Voice disabled - HIFI over HIFI, uses voice DAI LRC for capture
1304 */
Eric Miao6335d052009-03-03 09:41:00 +08001305static struct snd_soc_dai_ops wm8753_dai_ops_hifi_mode1 = {
1306 .hw_params = wm8753_i2s_hw_params,
1307 .digital_mute = wm8753_mute,
1308 .set_fmt = wm8753_mode1h_set_dai_fmt,
1309 .set_clkdiv = wm8753_set_dai_clkdiv,
1310 .set_pll = wm8753_set_dai_pll,
1311 .set_sysclk = wm8753_set_dai_sysclk,
1312};
1313
1314static struct snd_soc_dai_ops wm8753_dai_ops_voice_mode1 = {
1315 .hw_params = wm8753_pcm_hw_params,
1316 .digital_mute = wm8753_mute,
1317 .set_fmt = wm8753_mode1v_set_dai_fmt,
1318 .set_clkdiv = wm8753_set_dai_clkdiv,
1319 .set_pll = wm8753_set_dai_pll,
1320 .set_sysclk = wm8753_set_dai_sysclk,
1321};
1322
1323static struct snd_soc_dai_ops wm8753_dai_ops_voice_mode2 = {
1324 .hw_params = wm8753_pcm_hw_params,
1325 .digital_mute = wm8753_mute,
1326 .set_fmt = wm8753_mode2_set_dai_fmt,
1327 .set_clkdiv = wm8753_set_dai_clkdiv,
1328 .set_pll = wm8753_set_dai_pll,
1329 .set_sysclk = wm8753_set_dai_sysclk,
1330};
1331
1332static struct snd_soc_dai_ops wm8753_dai_ops_hifi_mode3 = {
1333 .hw_params = wm8753_i2s_hw_params,
1334 .digital_mute = wm8753_mute,
1335 .set_fmt = wm8753_mode3_4_set_dai_fmt,
1336 .set_clkdiv = wm8753_set_dai_clkdiv,
1337 .set_pll = wm8753_set_dai_pll,
1338 .set_sysclk = wm8753_set_dai_sysclk,
1339};
1340
1341static struct snd_soc_dai_ops wm8753_dai_ops_hifi_mode4 = {
1342 .hw_params = wm8753_i2s_hw_params,
1343 .digital_mute = wm8753_mute,
1344 .set_fmt = wm8753_mode3_4_set_dai_fmt,
1345 .set_clkdiv = wm8753_set_dai_clkdiv,
1346 .set_pll = wm8753_set_dai_pll,
1347 .set_sysclk = wm8753_set_dai_sysclk,
1348};
1349
Liam Girdwoode550e172008-07-07 16:07:52 +01001350static const struct snd_soc_dai wm8753_all_dai[] = {
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001351/* DAI HiFi mode 1 */
1352{ .name = "WM8753 HiFi",
1353 .id = 1,
1354 .playback = {
1355 .stream_name = "HiFi Playback",
1356 .channels_min = 1,
1357 .channels_max = 2,
1358 .rates = WM8753_RATES,
Mark Browndee89c42008-11-18 22:11:38 +00001359 .formats = WM8753_FORMATS},
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001360 .capture = { /* dummy for fast DAI switching */
1361 .stream_name = "Capture",
1362 .channels_min = 1,
1363 .channels_max = 2,
1364 .rates = WM8753_RATES,
Mark Browndee89c42008-11-18 22:11:38 +00001365 .formats = WM8753_FORMATS},
Eric Miao6335d052009-03-03 09:41:00 +08001366 .ops = &wm8753_dai_ops_hifi_mode1,
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001367},
1368/* DAI Voice mode 1 */
1369{ .name = "WM8753 Voice",
1370 .id = 1,
1371 .playback = {
1372 .stream_name = "Voice Playback",
1373 .channels_min = 1,
1374 .channels_max = 1,
1375 .rates = WM8753_RATES,
1376 .formats = WM8753_FORMATS,},
1377 .capture = {
1378 .stream_name = "Capture",
1379 .channels_min = 1,
1380 .channels_max = 2,
1381 .rates = WM8753_RATES,
1382 .formats = WM8753_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +08001383 .ops = &wm8753_dai_ops_voice_mode1,
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001384},
1385/* DAI HiFi mode 2 - dummy */
1386{ .name = "WM8753 HiFi",
1387 .id = 2,
1388},
1389/* DAI Voice mode 2 */
1390{ .name = "WM8753 Voice",
1391 .id = 2,
1392 .playback = {
1393 .stream_name = "Voice Playback",
1394 .channels_min = 1,
1395 .channels_max = 1,
1396 .rates = WM8753_RATES,
1397 .formats = WM8753_FORMATS,},
1398 .capture = {
1399 .stream_name = "Capture",
1400 .channels_min = 1,
1401 .channels_max = 2,
1402 .rates = WM8753_RATES,
1403 .formats = WM8753_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +08001404 .ops = &wm8753_dai_ops_voice_mode2,
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001405},
1406/* DAI HiFi mode 3 */
1407{ .name = "WM8753 HiFi",
1408 .id = 3,
1409 .playback = {
1410 .stream_name = "HiFi Playback",
1411 .channels_min = 1,
1412 .channels_max = 2,
1413 .rates = WM8753_RATES,
1414 .formats = WM8753_FORMATS,},
1415 .capture = {
1416 .stream_name = "Capture",
1417 .channels_min = 1,
1418 .channels_max = 2,
1419 .rates = WM8753_RATES,
1420 .formats = WM8753_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +08001421 .ops = &wm8753_dai_ops_hifi_mode3,
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001422},
1423/* DAI Voice mode 3 - dummy */
1424{ .name = "WM8753 Voice",
1425 .id = 3,
1426},
1427/* DAI HiFi mode 4 */
1428{ .name = "WM8753 HiFi",
1429 .id = 4,
1430 .playback = {
1431 .stream_name = "HiFi Playback",
1432 .channels_min = 1,
1433 .channels_max = 2,
1434 .rates = WM8753_RATES,
1435 .formats = WM8753_FORMATS,},
1436 .capture = {
1437 .stream_name = "Capture",
1438 .channels_min = 1,
1439 .channels_max = 2,
1440 .rates = WM8753_RATES,
1441 .formats = WM8753_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +08001442 .ops = &wm8753_dai_ops_hifi_mode4,
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001443},
1444/* DAI Voice mode 4 - dummy */
1445{ .name = "WM8753 Voice",
1446 .id = 4,
1447},
1448};
1449
Mark Brown9e70c1f2009-01-29 13:08:20 +00001450struct snd_soc_dai wm8753_dai[] = {
1451 {
1452 .name = "WM8753 DAI 0",
1453 },
1454 {
1455 .name = "WM8753 DAI 1",
1456 },
1457};
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001458EXPORT_SYMBOL_GPL(wm8753_dai);
1459
1460static void wm8753_set_dai_mode(struct snd_soc_codec *codec, unsigned int mode)
1461{
1462 if (mode < 4) {
1463 int playback_active, capture_active, codec_active, pop_wait;
1464 void *private_data;
Paul Fertser31b59cf2009-02-16 02:49:41 +03001465 struct list_head list;
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001466
1467 playback_active = wm8753_dai[0].playback.active;
1468 capture_active = wm8753_dai[0].capture.active;
1469 codec_active = wm8753_dai[0].active;
1470 private_data = wm8753_dai[0].private_data;
1471 pop_wait = wm8753_dai[0].pop_wait;
Paul Fertser31b59cf2009-02-16 02:49:41 +03001472 list = wm8753_dai[0].list;
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001473 wm8753_dai[0] = wm8753_all_dai[mode << 1];
1474 wm8753_dai[0].playback.active = playback_active;
1475 wm8753_dai[0].capture.active = capture_active;
1476 wm8753_dai[0].active = codec_active;
1477 wm8753_dai[0].private_data = private_data;
1478 wm8753_dai[0].pop_wait = pop_wait;
Paul Fertser31b59cf2009-02-16 02:49:41 +03001479 wm8753_dai[0].list = list;
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001480
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;
Paul Fertser31b59cf2009-02-16 02:49:41 +03001486 list = wm8753_dai[1].list;
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001487 wm8753_dai[1] = wm8753_all_dai[(mode << 1) + 1];
1488 wm8753_dai[1].playback.active = playback_active;
1489 wm8753_dai[1].capture.active = capture_active;
1490 wm8753_dai[1].active = codec_active;
1491 wm8753_dai[1].private_data = private_data;
1492 wm8753_dai[1].pop_wait = pop_wait;
Paul Fertser31b59cf2009-02-16 02:49:41 +03001493 wm8753_dai[1].list = list;
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001494 }
1495 wm8753_dai[0].codec = codec;
1496 wm8753_dai[1].codec = codec;
1497}
1498
1499static void wm8753_work(struct work_struct *work)
1500{
1501 struct snd_soc_codec *codec =
1502 container_of(work, struct snd_soc_codec, delayed_work.work);
Mark Brown0be98982008-05-19 12:31:28 +02001503 wm8753_set_bias_level(codec, codec->bias_level);
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001504}
1505
1506static int wm8753_suspend(struct platform_device *pdev, pm_message_t state)
1507{
1508 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +00001509 struct snd_soc_codec *codec = socdev->card->codec;
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001510
1511 /* we only need to suspend if we are a valid card */
Mark Brown60fc6842008-04-30 17:18:43 +02001512 if (!codec->card)
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001513 return 0;
Mark Brown60fc6842008-04-30 17:18:43 +02001514
Mark Brown0be98982008-05-19 12:31:28 +02001515 wm8753_set_bias_level(codec, SND_SOC_BIAS_OFF);
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001516 return 0;
1517}
1518
1519static int wm8753_resume(struct platform_device *pdev)
1520{
1521 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +00001522 struct snd_soc_codec *codec = socdev->card->codec;
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001523 int i;
1524 u8 data[2];
1525 u16 *cache = codec->reg_cache;
1526
1527 /* we only need to resume if we are a valid card */
Mark Brown60fc6842008-04-30 17:18:43 +02001528 if (!codec->card)
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001529 return 0;
1530
1531 /* Sync reg_cache with the hardware */
1532 for (i = 0; i < ARRAY_SIZE(wm8753_reg); i++) {
1533 if (i + 1 == WM8753_RESET)
1534 continue;
Mark Browne611bd82009-02-22 20:04:41 +00001535
1536 /* No point in writing hardware default values back */
1537 if (cache[i] == wm8753_reg[i])
1538 continue;
1539
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001540 data[0] = ((i + 1) << 1) | ((cache[i] >> 8) & 0x0001);
1541 data[1] = cache[i] & 0x00ff;
1542 codec->hw_write(codec->control_data, data, 2);
1543 }
1544
Mark Brown0be98982008-05-19 12:31:28 +02001545 wm8753_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001546
1547 /* charge wm8753 caps */
Mark Brown0be98982008-05-19 12:31:28 +02001548 if (codec->suspend_bias_level == SND_SOC_BIAS_ON) {
1549 wm8753_set_bias_level(codec, SND_SOC_BIAS_PREPARE);
1550 codec->bias_level = SND_SOC_BIAS_ON;
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001551 schedule_delayed_work(&codec->delayed_work,
1552 msecs_to_jiffies(caps_charge));
1553 }
1554
1555 return 0;
1556}
1557
Mark Brownc2bac162009-02-24 23:33:12 +00001558static struct snd_soc_codec *wm8753_codec;
1559
1560static int wm8753_probe(struct platform_device *pdev)
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001561{
Mark Brownc2bac162009-02-24 23:33:12 +00001562 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1563 struct snd_soc_codec *codec;
1564 int ret = 0;
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001565
Mark Brownc2bac162009-02-24 23:33:12 +00001566 if (!wm8753_codec) {
1567 dev_err(&pdev->dev, "WM8753 codec not yet registered\n");
1568 return -EINVAL;
1569 }
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001570
Mark Brownc2bac162009-02-24 23:33:12 +00001571 socdev->card->codec = wm8753_codec;
1572 codec = wm8753_codec;
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001573
1574 wm8753_set_dai_mode(codec, 0);
1575
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001576 /* register pcms */
1577 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
1578 if (ret < 0) {
1579 printk(KERN_ERR "wm8753: failed to create pcms\n");
1580 goto pcm_err;
1581 }
1582
Takashi Iwai6d564342009-02-26 11:29:58 +01001583 snd_soc_add_controls(codec, wm8753_snd_controls,
1584 ARRAY_SIZE(wm8753_snd_controls));
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001585 wm8753_add_widgets(codec);
Mark Brown968a6022008-11-28 11:49:07 +00001586 ret = snd_soc_init_card(socdev);
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001587 if (ret < 0) {
Mark Brown60fc6842008-04-30 17:18:43 +02001588 printk(KERN_ERR "wm8753: failed to register card\n");
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001589 goto card_err;
Mark Brown60fc6842008-04-30 17:18:43 +02001590 }
1591
Mark Brownc2bac162009-02-24 23:33:12 +00001592 return 0;
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001593
1594card_err:
1595 snd_soc_free_pcms(socdev);
1596 snd_soc_dapm_free(socdev);
Mark Brownc2bac162009-02-24 23:33:12 +00001597
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001598pcm_err:
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001599 return ret;
1600}
1601
1602/*
1603 * This function forces any delayed work to be queued and run.
1604 */
1605static int run_delayed_work(struct delayed_work *dwork)
1606{
1607 int ret;
1608
1609 /* cancel any work waiting to be queued. */
1610 ret = cancel_delayed_work(dwork);
1611
1612 /* if there was any work waiting then we run it now and
1613 * wait for it's completion */
1614 if (ret) {
1615 schedule_delayed_work(dwork, 0);
1616 flush_scheduled_work();
1617 }
1618 return ret;
1619}
1620
1621/* power down chip */
1622static int wm8753_remove(struct platform_device *pdev)
1623{
1624 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001625
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001626 snd_soc_free_pcms(socdev);
1627 snd_soc_dapm_free(socdev);
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001628
1629 return 0;
1630}
1631
1632struct snd_soc_codec_device soc_codec_dev_wm8753 = {
1633 .probe = wm8753_probe,
1634 .remove = wm8753_remove,
1635 .suspend = wm8753_suspend,
1636 .resume = wm8753_resume,
1637};
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001638EXPORT_SYMBOL_GPL(soc_codec_dev_wm8753);
1639
Mark Brownc2bac162009-02-24 23:33:12 +00001640static int wm8753_register(struct wm8753_priv *wm8753)
1641{
1642 int ret, i;
1643 struct snd_soc_codec *codec = &wm8753->codec;
1644 u16 reg;
1645
1646 if (wm8753_codec) {
1647 dev_err(codec->dev, "Multiple WM8753 devices not supported\n");
1648 ret = -EINVAL;
1649 goto err;
1650 }
1651
1652 mutex_init(&codec->mutex);
1653 INIT_LIST_HEAD(&codec->dapm_widgets);
1654 INIT_LIST_HEAD(&codec->dapm_paths);
1655
1656 codec->name = "WM8753";
1657 codec->owner = THIS_MODULE;
1658 codec->read = wm8753_read_reg_cache;
1659 codec->write = wm8753_write;
1660 codec->bias_level = SND_SOC_BIAS_STANDBY;
1661 codec->set_bias_level = wm8753_set_bias_level;
1662 codec->dai = wm8753_dai;
1663 codec->num_dai = 2;
Lars-Peter Clausen637a9352009-07-03 01:04:16 +02001664 codec->reg_cache_size = ARRAY_SIZE(wm8753->reg_cache) + 1;
Mark Brownc2bac162009-02-24 23:33:12 +00001665 codec->reg_cache = &wm8753->reg_cache;
1666 codec->private_data = wm8753;
1667
Mark Brown1df892c2009-07-03 10:33:39 +01001668 memcpy(codec->reg_cache, wm8753_reg, sizeof(wm8753->reg_cache));
Mark Brownc2bac162009-02-24 23:33:12 +00001669 INIT_DELAYED_WORK(&codec->delayed_work, wm8753_work);
1670
1671 ret = wm8753_reset(codec);
1672 if (ret < 0) {
1673 dev_err(codec->dev, "Failed to issue reset\n");
1674 goto err;
1675 }
1676
1677 /* charge output caps */
1678 wm8753_set_bias_level(codec, SND_SOC_BIAS_PREPARE);
1679 schedule_delayed_work(&codec->delayed_work,
1680 msecs_to_jiffies(caps_charge));
1681
1682 /* set the update bits */
1683 reg = wm8753_read_reg_cache(codec, WM8753_LDAC);
1684 wm8753_write(codec, WM8753_LDAC, reg | 0x0100);
1685 reg = wm8753_read_reg_cache(codec, WM8753_RDAC);
1686 wm8753_write(codec, WM8753_RDAC, reg | 0x0100);
1687 reg = wm8753_read_reg_cache(codec, WM8753_LADC);
1688 wm8753_write(codec, WM8753_LADC, reg | 0x0100);
1689 reg = wm8753_read_reg_cache(codec, WM8753_RADC);
1690 wm8753_write(codec, WM8753_RADC, reg | 0x0100);
1691 reg = wm8753_read_reg_cache(codec, WM8753_LOUT1V);
1692 wm8753_write(codec, WM8753_LOUT1V, reg | 0x0100);
1693 reg = wm8753_read_reg_cache(codec, WM8753_ROUT1V);
1694 wm8753_write(codec, WM8753_ROUT1V, reg | 0x0100);
1695 reg = wm8753_read_reg_cache(codec, WM8753_LOUT2V);
1696 wm8753_write(codec, WM8753_LOUT2V, reg | 0x0100);
1697 reg = wm8753_read_reg_cache(codec, WM8753_ROUT2V);
1698 wm8753_write(codec, WM8753_ROUT2V, reg | 0x0100);
1699 reg = wm8753_read_reg_cache(codec, WM8753_LINVOL);
1700 wm8753_write(codec, WM8753_LINVOL, reg | 0x0100);
1701 reg = wm8753_read_reg_cache(codec, WM8753_RINVOL);
1702 wm8753_write(codec, WM8753_RINVOL, reg | 0x0100);
1703
1704 wm8753_codec = codec;
1705
1706 for (i = 0; i < ARRAY_SIZE(wm8753_dai); i++)
1707 wm8753_dai[i].dev = codec->dev;
1708
1709 ret = snd_soc_register_codec(codec);
1710 if (ret != 0) {
1711 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
1712 goto err;
1713 }
1714
1715 ret = snd_soc_register_dais(&wm8753_dai[0], ARRAY_SIZE(wm8753_dai));
1716 if (ret != 0) {
1717 dev_err(codec->dev, "Failed to register DAIs: %d\n", ret);
1718 goto err_codec;
1719 }
1720
1721 return 0;
1722
1723err_codec:
1724 run_delayed_work(&codec->delayed_work);
1725 snd_soc_unregister_codec(codec);
1726err:
1727 kfree(wm8753);
1728 return ret;
1729}
1730
1731static void wm8753_unregister(struct wm8753_priv *wm8753)
1732{
1733 wm8753_set_bias_level(&wm8753->codec, SND_SOC_BIAS_OFF);
1734 run_delayed_work(&wm8753->codec.delayed_work);
1735 snd_soc_unregister_dais(&wm8753_dai[0], ARRAY_SIZE(wm8753_dai));
1736 snd_soc_unregister_codec(&wm8753->codec);
1737 kfree(wm8753);
1738 wm8753_codec = NULL;
1739}
1740
Mark Brown69e169d2009-02-22 14:39:03 +00001741#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1742
1743static int wm8753_i2c_probe(struct i2c_client *i2c,
1744 const struct i2c_device_id *id)
1745{
Mark Brownc2bac162009-02-24 23:33:12 +00001746 struct snd_soc_codec *codec;
1747 struct wm8753_priv *wm8753;
Mark Brown69e169d2009-02-22 14:39:03 +00001748
Mark Brownc2bac162009-02-24 23:33:12 +00001749 wm8753 = kzalloc(sizeof(struct wm8753_priv), GFP_KERNEL);
1750 if (wm8753 == NULL)
1751 return -ENOMEM;
Mark Brown69e169d2009-02-22 14:39:03 +00001752
Mark Brownc2bac162009-02-24 23:33:12 +00001753 codec = &wm8753->codec;
1754 codec->hw_write = (hw_write_t)i2c_master_send;
1755 codec->control_data = i2c;
1756 i2c_set_clientdata(i2c, wm8753);
Mark Brown69e169d2009-02-22 14:39:03 +00001757
Mark Brownc2bac162009-02-24 23:33:12 +00001758 codec->dev = &i2c->dev;
1759
1760 return wm8753_register(wm8753);
Mark Brown69e169d2009-02-22 14:39:03 +00001761}
1762
1763static int wm8753_i2c_remove(struct i2c_client *client)
1764{
Mark Brownc2bac162009-02-24 23:33:12 +00001765 struct wm8753_priv *wm8753 = i2c_get_clientdata(client);
1766 wm8753_unregister(wm8753);
1767 return 0;
Mark Brown69e169d2009-02-22 14:39:03 +00001768}
1769
Mark Brownb3b50b32009-06-13 22:30:18 +01001770#ifdef CONFIG_PM
1771static int wm8753_i2c_suspend(struct i2c_client *client, pm_message_t msg)
1772{
1773 return snd_soc_suspend_device(&client->dev);
1774}
1775
1776static int wm8753_i2c_resume(struct i2c_client *client)
1777{
1778 return snd_soc_resume_device(&client->dev);
1779}
1780#else
1781#define wm8753_i2c_suspend NULL
1782#define wm8753_i2c_resume NULL
1783#endif
1784
Mark Brown69e169d2009-02-22 14:39:03 +00001785static const struct i2c_device_id wm8753_i2c_id[] = {
1786 { "wm8753", 0 },
1787 { }
1788};
1789MODULE_DEVICE_TABLE(i2c, wm8753_i2c_id);
1790
1791static struct i2c_driver wm8753_i2c_driver = {
1792 .driver = {
Mark Brownc2bac162009-02-24 23:33:12 +00001793 .name = "wm8753",
Mark Brown69e169d2009-02-22 14:39:03 +00001794 .owner = THIS_MODULE,
1795 },
1796 .probe = wm8753_i2c_probe,
1797 .remove = wm8753_i2c_remove,
Mark Brownb3b50b32009-06-13 22:30:18 +01001798 .suspend = wm8753_i2c_suspend,
1799 .resume = wm8753_i2c_resume,
Mark Brown69e169d2009-02-22 14:39:03 +00001800 .id_table = wm8753_i2c_id,
1801};
1802#endif
1803
1804#if defined(CONFIG_SPI_MASTER)
1805static int wm8753_spi_write(struct spi_device *spi, const char *data, int len)
1806{
1807 struct spi_transfer t;
1808 struct spi_message m;
1809 u8 msg[2];
1810
1811 if (len <= 0)
1812 return 0;
1813
1814 msg[0] = data[0];
1815 msg[1] = data[1];
1816
1817 spi_message_init(&m);
1818 memset(&t, 0, (sizeof t));
1819
1820 t.tx_buf = &msg[0];
1821 t.len = len;
1822
1823 spi_message_add_tail(&t, &m);
1824 spi_sync(spi, &m);
1825
1826 return len;
1827}
1828
1829static int __devinit wm8753_spi_probe(struct spi_device *spi)
1830{
Mark Brownc2bac162009-02-24 23:33:12 +00001831 struct snd_soc_codec *codec;
1832 struct wm8753_priv *wm8753;
Mark Brown69e169d2009-02-22 14:39:03 +00001833
Mark Brownc2bac162009-02-24 23:33:12 +00001834 wm8753 = kzalloc(sizeof(struct wm8753_priv), GFP_KERNEL);
1835 if (wm8753 == NULL)
1836 return -ENOMEM;
1837
1838 codec = &wm8753->codec;
Mark Brown69e169d2009-02-22 14:39:03 +00001839 codec->control_data = spi;
Mark Brownc2bac162009-02-24 23:33:12 +00001840 codec->hw_write = (hw_write_t)wm8753_spi_write;
1841 codec->dev = &spi->dev;
Mark Brown69e169d2009-02-22 14:39:03 +00001842
Greg Kroah-Hartmanae31c1f2009-05-04 12:40:54 -07001843 dev_set_drvdata(&spi->dev, wm8753);
Mark Brown69e169d2009-02-22 14:39:03 +00001844
Mark Brownc2bac162009-02-24 23:33:12 +00001845 return wm8753_register(wm8753);
Mark Brown69e169d2009-02-22 14:39:03 +00001846}
1847
1848static int __devexit wm8753_spi_remove(struct spi_device *spi)
1849{
Greg Kroah-Hartmanae31c1f2009-05-04 12:40:54 -07001850 struct wm8753_priv *wm8753 = dev_get_drvdata(&spi->dev);
Mark Brownc2bac162009-02-24 23:33:12 +00001851 wm8753_unregister(wm8753);
Mark Brown69e169d2009-02-22 14:39:03 +00001852 return 0;
1853}
1854
Mark Brownb3b50b32009-06-13 22:30:18 +01001855#ifdef CONFIG_PM
1856static int wm8753_spi_suspend(struct spi_device *spi, pm_message_t msg)
1857{
1858 return snd_soc_suspend_device(&spi->dev);
1859}
1860
1861static int wm8753_spi_resume(struct spi_device *spi)
1862{
1863 return snd_soc_resume_device(&spi->dev);
1864}
1865
1866#else
1867#define wm8753_spi_suspend NULL
1868#define wm8753_spi_resume NULL
1869#endif
1870
Mark Brown69e169d2009-02-22 14:39:03 +00001871static struct spi_driver wm8753_spi_driver = {
1872 .driver = {
1873 .name = "wm8753",
1874 .bus = &spi_bus_type,
1875 .owner = THIS_MODULE,
1876 },
1877 .probe = wm8753_spi_probe,
1878 .remove = __devexit_p(wm8753_spi_remove),
Mark Brownb3b50b32009-06-13 22:30:18 +01001879 .suspend = wm8753_spi_suspend,
1880 .resume = wm8753_spi_resume,
Mark Brown69e169d2009-02-22 14:39:03 +00001881};
1882#endif
1883
Takashi Iwaic9b3a402008-12-10 07:47:22 +01001884static int __init wm8753_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +00001885{
Mark Brownc2bac162009-02-24 23:33:12 +00001886 int ret;
1887#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1888 ret = i2c_add_driver(&wm8753_i2c_driver);
1889 if (ret != 0)
1890 pr_err("Failed to register WM8753 I2C driver: %d\n", ret);
1891#endif
1892#if defined(CONFIG_SPI_MASTER)
1893 ret = spi_register_driver(&wm8753_spi_driver);
1894 if (ret != 0)
1895 pr_err("Failed to register WM8753 SPI driver: %d\n", ret);
1896#endif
1897 return 0;
Mark Brown64089b82008-12-08 19:17:58 +00001898}
1899module_init(wm8753_modinit);
1900
1901static void __exit wm8753_exit(void)
1902{
Mark Brownc2bac162009-02-24 23:33:12 +00001903#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1904 i2c_del_driver(&wm8753_i2c_driver);
1905#endif
1906#if defined(CONFIG_SPI_MASTER)
1907 spi_unregister_driver(&wm8753_spi_driver);
1908#endif
Mark Brown64089b82008-12-08 19:17:58 +00001909}
1910module_exit(wm8753_exit);
1911
Liam Girdwood1f53aee2007-04-16 19:17:44 +02001912MODULE_DESCRIPTION("ASoC WM8753 driver");
1913MODULE_AUTHOR("Liam Girdwood");
1914MODULE_LICENSE("GPL");