blob: 55b9a25cd1b382abc9e44c2010df21f3f56cd12b [file] [log] [blame]
Mark Brownf2644a22009-04-07 19:20:14 +01001/*
2 * wm8960.c -- WM8960 ALSA SoC Audio driver
3 *
4 * Author: Liam Girdwood
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/init.h>
14#include <linux/delay.h>
15#include <linux/pm.h>
16#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Mark Brownf2644a22009-04-07 19:20:14 +010018#include <sound/core.h>
19#include <sound/pcm.h>
20#include <sound/pcm_params.h>
21#include <sound/soc.h>
Mark Brownf2644a22009-04-07 19:20:14 +010022#include <sound/initval.h>
23#include <sound/tlv.h>
Mark Brownb6877a42010-03-03 11:43:38 +000024#include <sound/wm8960.h>
Mark Brownf2644a22009-04-07 19:20:14 +010025
26#include "wm8960.h"
27
28#define AUDIO_NAME "wm8960"
29
Mark Brownf2644a22009-04-07 19:20:14 +010030/* R25 - Power 1 */
Mark Brown913d7b42010-03-03 13:47:03 +000031#define WM8960_VMID_MASK 0x180
Mark Brownf2644a22009-04-07 19:20:14 +010032#define WM8960_VREF 0x40
33
Mark Brown913d7b42010-03-03 13:47:03 +000034/* R26 - Power 2 */
35#define WM8960_PWR2_LOUT1 0x40
36#define WM8960_PWR2_ROUT1 0x20
37#define WM8960_PWR2_OUT3 0x02
38
Mark Brownf2644a22009-04-07 19:20:14 +010039/* R28 - Anti-pop 1 */
40#define WM8960_POBCTRL 0x80
41#define WM8960_BUFDCOPEN 0x10
42#define WM8960_BUFIOEN 0x08
43#define WM8960_SOFT_ST 0x04
44#define WM8960_HPSTBY 0x01
45
46/* R29 - Anti-pop 2 */
47#define WM8960_DISOP 0x40
Mark Brown913d7b42010-03-03 13:47:03 +000048#define WM8960_DRES_MASK 0x30
Mark Brownf2644a22009-04-07 19:20:14 +010049
50/*
51 * wm8960 register cache
52 * We can't read the WM8960 register space when we are
53 * using 2 wire for device control, so we cache them instead.
54 */
55static const u16 wm8960_reg[WM8960_CACHEREGNUM] = {
56 0x0097, 0x0097, 0x0000, 0x0000,
57 0x0000, 0x0008, 0x0000, 0x000a,
58 0x01c0, 0x0000, 0x00ff, 0x00ff,
59 0x0000, 0x0000, 0x0000, 0x0000,
60 0x0000, 0x007b, 0x0100, 0x0032,
61 0x0000, 0x00c3, 0x00c3, 0x01c0,
62 0x0000, 0x0000, 0x0000, 0x0000,
63 0x0000, 0x0000, 0x0000, 0x0000,
64 0x0100, 0x0100, 0x0050, 0x0050,
65 0x0050, 0x0050, 0x0000, 0x0000,
66 0x0000, 0x0000, 0x0040, 0x0000,
67 0x0000, 0x0050, 0x0050, 0x0000,
68 0x0002, 0x0037, 0x004d, 0x0080,
69 0x0008, 0x0031, 0x0026, 0x00e9,
70};
71
72struct wm8960_priv {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000073 enum snd_soc_control_type control_type;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000074 int (*set_bias_level)(struct snd_soc_codec *,
75 enum snd_soc_bias_level level);
Mark Brown913d7b42010-03-03 13:47:03 +000076 struct snd_soc_dapm_widget *lout1;
77 struct snd_soc_dapm_widget *rout1;
78 struct snd_soc_dapm_widget *out3;
Mark Brownafd6d362010-07-05 13:58:16 +090079 bool deemph;
80 int playback_fs;
Mark Brownf2644a22009-04-07 19:20:14 +010081};
82
Mark Brown17a52fd2009-07-05 17:24:50 +010083#define wm8960_reset(c) snd_soc_write(c, WM8960_RESET, 0)
Mark Brownf2644a22009-04-07 19:20:14 +010084
85/* enumerated controls */
Mark Brownf2644a22009-04-07 19:20:14 +010086static const char *wm8960_polarity[] = {"No Inversion", "Left Inverted",
87 "Right Inverted", "Stereo Inversion"};
88static const char *wm8960_3d_upper_cutoff[] = {"High", "Low"};
89static const char *wm8960_3d_lower_cutoff[] = {"Low", "High"};
90static const char *wm8960_alcfunc[] = {"Off", "Right", "Left", "Stereo"};
91static const char *wm8960_alcmode[] = {"ALC", "Limiter"};
92
93static const struct soc_enum wm8960_enum[] = {
Mark Brownf2644a22009-04-07 19:20:14 +010094 SOC_ENUM_SINGLE(WM8960_DACCTL1, 5, 4, wm8960_polarity),
95 SOC_ENUM_SINGLE(WM8960_DACCTL2, 5, 4, wm8960_polarity),
96 SOC_ENUM_SINGLE(WM8960_3D, 6, 2, wm8960_3d_upper_cutoff),
97 SOC_ENUM_SINGLE(WM8960_3D, 5, 2, wm8960_3d_lower_cutoff),
98 SOC_ENUM_SINGLE(WM8960_ALC1, 7, 4, wm8960_alcfunc),
99 SOC_ENUM_SINGLE(WM8960_ALC3, 8, 2, wm8960_alcmode),
100};
101
Mark Brownafd6d362010-07-05 13:58:16 +0900102static const int deemph_settings[] = { 0, 32000, 44100, 48000 };
103
104static int wm8960_set_deemph(struct snd_soc_codec *codec)
105{
106 struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
107 int val, i, best;
108
109 /* If we're using deemphasis select the nearest available sample
110 * rate.
111 */
112 if (wm8960->deemph) {
113 best = 1;
114 for (i = 2; i < ARRAY_SIZE(deemph_settings); i++) {
115 if (abs(deemph_settings[i] - wm8960->playback_fs) <
116 abs(deemph_settings[best] - wm8960->playback_fs))
117 best = i;
118 }
119
120 val = best << 1;
121 } else {
122 val = 0;
123 }
124
125 dev_dbg(codec->dev, "Set deemphasis %d\n", val);
126
127 return snd_soc_update_bits(codec, WM8960_DACCTL1,
128 0x6, val);
129}
130
131static int wm8960_get_deemph(struct snd_kcontrol *kcontrol,
132 struct snd_ctl_elem_value *ucontrol)
133{
134 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
135 struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
136
Dmitry Artamonow3f343f82010-12-08 23:36:17 +0300137 ucontrol->value.enumerated.item[0] = wm8960->deemph;
138 return 0;
Mark Brownafd6d362010-07-05 13:58:16 +0900139}
140
141static int wm8960_put_deemph(struct snd_kcontrol *kcontrol,
142 struct snd_ctl_elem_value *ucontrol)
143{
144 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
145 struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
146 int deemph = ucontrol->value.enumerated.item[0];
147
148 if (deemph > 1)
149 return -EINVAL;
150
151 wm8960->deemph = deemph;
152
153 return wm8960_set_deemph(codec);
154}
155
Mark Brownf2644a22009-04-07 19:20:14 +0100156static const DECLARE_TLV_DB_SCALE(adc_tlv, -9700, 50, 0);
157static const DECLARE_TLV_DB_SCALE(dac_tlv, -12700, 50, 1);
158static const DECLARE_TLV_DB_SCALE(bypass_tlv, -2100, 300, 0);
159static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
160
161static const struct snd_kcontrol_new wm8960_snd_controls[] = {
162SOC_DOUBLE_R_TLV("Capture Volume", WM8960_LINVOL, WM8960_RINVOL,
163 0, 63, 0, adc_tlv),
164SOC_DOUBLE_R("Capture Volume ZC Switch", WM8960_LINVOL, WM8960_RINVOL,
165 6, 1, 0),
166SOC_DOUBLE_R("Capture Switch", WM8960_LINVOL, WM8960_RINVOL,
167 7, 1, 0),
168
169SOC_DOUBLE_R_TLV("Playback Volume", WM8960_LDAC, WM8960_RDAC,
170 0, 255, 0, dac_tlv),
171
172SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8960_LOUT1, WM8960_ROUT1,
173 0, 127, 0, out_tlv),
174SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8960_LOUT1, WM8960_ROUT1,
175 7, 1, 0),
176
177SOC_DOUBLE_R_TLV("Speaker Playback Volume", WM8960_LOUT2, WM8960_ROUT2,
178 0, 127, 0, out_tlv),
179SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8960_LOUT2, WM8960_ROUT2,
180 7, 1, 0),
181SOC_SINGLE("Speaker DC Volume", WM8960_CLASSD3, 3, 5, 0),
182SOC_SINGLE("Speaker AC Volume", WM8960_CLASSD3, 0, 5, 0),
183
184SOC_SINGLE("PCM Playback -6dB Switch", WM8960_DACCTL1, 7, 1, 0),
Mark Brown4faaa8d2010-07-05 13:54:32 +0900185SOC_ENUM("ADC Polarity", wm8960_enum[0]),
Mark Brownf2644a22009-04-07 19:20:14 +0100186SOC_SINGLE("ADC High Pass Filter Switch", WM8960_DACCTL1, 0, 1, 0),
187
188SOC_ENUM("DAC Polarity", wm8960_enum[2]),
Mark Brownafd6d362010-07-05 13:58:16 +0900189SOC_SINGLE_BOOL_EXT("DAC Deemphasis Switch", 0,
190 wm8960_get_deemph, wm8960_put_deemph),
Mark Brownf2644a22009-04-07 19:20:14 +0100191
Mark Brown4faaa8d2010-07-05 13:54:32 +0900192SOC_ENUM("3D Filter Upper Cut-Off", wm8960_enum[2]),
193SOC_ENUM("3D Filter Lower Cut-Off", wm8960_enum[3]),
Mark Brownf2644a22009-04-07 19:20:14 +0100194SOC_SINGLE("3D Volume", WM8960_3D, 1, 15, 0),
195SOC_SINGLE("3D Switch", WM8960_3D, 0, 1, 0),
196
Mark Brown4faaa8d2010-07-05 13:54:32 +0900197SOC_ENUM("ALC Function", wm8960_enum[4]),
Mark Brownf2644a22009-04-07 19:20:14 +0100198SOC_SINGLE("ALC Max Gain", WM8960_ALC1, 4, 7, 0),
199SOC_SINGLE("ALC Target", WM8960_ALC1, 0, 15, 1),
200SOC_SINGLE("ALC Min Gain", WM8960_ALC2, 4, 7, 0),
201SOC_SINGLE("ALC Hold Time", WM8960_ALC2, 0, 15, 0),
Mark Brown4faaa8d2010-07-05 13:54:32 +0900202SOC_ENUM("ALC Mode", wm8960_enum[5]),
Mark Brownf2644a22009-04-07 19:20:14 +0100203SOC_SINGLE("ALC Decay", WM8960_ALC3, 4, 15, 0),
204SOC_SINGLE("ALC Attack", WM8960_ALC3, 0, 15, 0),
205
206SOC_SINGLE("Noise Gate Threshold", WM8960_NOISEG, 3, 31, 0),
207SOC_SINGLE("Noise Gate Switch", WM8960_NOISEG, 0, 1, 0),
208
209SOC_DOUBLE_R("ADC PCM Capture Volume", WM8960_LINPATH, WM8960_RINPATH,
210 0, 127, 0),
211
212SOC_SINGLE_TLV("Left Output Mixer Boost Bypass Volume",
213 WM8960_BYPASS1, 4, 7, 1, bypass_tlv),
214SOC_SINGLE_TLV("Left Output Mixer LINPUT3 Volume",
215 WM8960_LOUTMIX, 4, 7, 1, bypass_tlv),
216SOC_SINGLE_TLV("Right Output Mixer Boost Bypass Volume",
217 WM8960_BYPASS2, 4, 7, 1, bypass_tlv),
218SOC_SINGLE_TLV("Right Output Mixer RINPUT3 Volume",
219 WM8960_ROUTMIX, 4, 7, 1, bypass_tlv),
220};
221
222static const struct snd_kcontrol_new wm8960_lin_boost[] = {
223SOC_DAPM_SINGLE("LINPUT2 Switch", WM8960_LINPATH, 6, 1, 0),
224SOC_DAPM_SINGLE("LINPUT3 Switch", WM8960_LINPATH, 7, 1, 0),
225SOC_DAPM_SINGLE("LINPUT1 Switch", WM8960_LINPATH, 8, 1, 0),
226};
227
228static const struct snd_kcontrol_new wm8960_lin[] = {
229SOC_DAPM_SINGLE("Boost Switch", WM8960_LINPATH, 3, 1, 0),
230};
231
232static const struct snd_kcontrol_new wm8960_rin_boost[] = {
233SOC_DAPM_SINGLE("RINPUT2 Switch", WM8960_RINPATH, 6, 1, 0),
234SOC_DAPM_SINGLE("RINPUT3 Switch", WM8960_RINPATH, 7, 1, 0),
235SOC_DAPM_SINGLE("RINPUT1 Switch", WM8960_RINPATH, 8, 1, 0),
236};
237
238static const struct snd_kcontrol_new wm8960_rin[] = {
239SOC_DAPM_SINGLE("Boost Switch", WM8960_RINPATH, 3, 1, 0),
240};
241
242static const struct snd_kcontrol_new wm8960_loutput_mixer[] = {
243SOC_DAPM_SINGLE("PCM Playback Switch", WM8960_LOUTMIX, 8, 1, 0),
244SOC_DAPM_SINGLE("LINPUT3 Switch", WM8960_LOUTMIX, 7, 1, 0),
245SOC_DAPM_SINGLE("Boost Bypass Switch", WM8960_BYPASS1, 7, 1, 0),
246};
247
248static const struct snd_kcontrol_new wm8960_routput_mixer[] = {
249SOC_DAPM_SINGLE("PCM Playback Switch", WM8960_ROUTMIX, 8, 1, 0),
250SOC_DAPM_SINGLE("RINPUT3 Switch", WM8960_ROUTMIX, 7, 1, 0),
251SOC_DAPM_SINGLE("Boost Bypass Switch", WM8960_BYPASS2, 7, 1, 0),
252};
253
254static const struct snd_kcontrol_new wm8960_mono_out[] = {
255SOC_DAPM_SINGLE("Left Switch", WM8960_MONOMIX1, 7, 1, 0),
256SOC_DAPM_SINGLE("Right Switch", WM8960_MONOMIX2, 7, 1, 0),
257};
258
259static const struct snd_soc_dapm_widget wm8960_dapm_widgets[] = {
260SND_SOC_DAPM_INPUT("LINPUT1"),
261SND_SOC_DAPM_INPUT("RINPUT1"),
262SND_SOC_DAPM_INPUT("LINPUT2"),
263SND_SOC_DAPM_INPUT("RINPUT2"),
264SND_SOC_DAPM_INPUT("LINPUT3"),
265SND_SOC_DAPM_INPUT("RINPUT3"),
266
Mark Brown187774c2011-10-27 09:46:17 +0200267SND_SOC_DAPM_SUPPLY("MICB", WM8960_POWER1, 1, 0, NULL, 0),
Mark Brownf2644a22009-04-07 19:20:14 +0100268
269SND_SOC_DAPM_MIXER("Left Boost Mixer", WM8960_POWER1, 5, 0,
270 wm8960_lin_boost, ARRAY_SIZE(wm8960_lin_boost)),
271SND_SOC_DAPM_MIXER("Right Boost Mixer", WM8960_POWER1, 4, 0,
272 wm8960_rin_boost, ARRAY_SIZE(wm8960_rin_boost)),
273
274SND_SOC_DAPM_MIXER("Left Input Mixer", WM8960_POWER3, 5, 0,
275 wm8960_lin, ARRAY_SIZE(wm8960_lin)),
276SND_SOC_DAPM_MIXER("Right Input Mixer", WM8960_POWER3, 4, 0,
277 wm8960_rin, ARRAY_SIZE(wm8960_rin)),
278
279SND_SOC_DAPM_ADC("Left ADC", "Capture", WM8960_POWER2, 3, 0),
280SND_SOC_DAPM_ADC("Right ADC", "Capture", WM8960_POWER2, 2, 0),
281
282SND_SOC_DAPM_DAC("Left DAC", "Playback", WM8960_POWER2, 8, 0),
283SND_SOC_DAPM_DAC("Right DAC", "Playback", WM8960_POWER2, 7, 0),
284
285SND_SOC_DAPM_MIXER("Left Output Mixer", WM8960_POWER3, 3, 0,
286 &wm8960_loutput_mixer[0],
287 ARRAY_SIZE(wm8960_loutput_mixer)),
288SND_SOC_DAPM_MIXER("Right Output Mixer", WM8960_POWER3, 2, 0,
289 &wm8960_routput_mixer[0],
290 ARRAY_SIZE(wm8960_routput_mixer)),
291
Mark Brownf2644a22009-04-07 19:20:14 +0100292SND_SOC_DAPM_PGA("LOUT1 PGA", WM8960_POWER2, 6, 0, NULL, 0),
293SND_SOC_DAPM_PGA("ROUT1 PGA", WM8960_POWER2, 5, 0, NULL, 0),
294
295SND_SOC_DAPM_PGA("Left Speaker PGA", WM8960_POWER2, 4, 0, NULL, 0),
296SND_SOC_DAPM_PGA("Right Speaker PGA", WM8960_POWER2, 3, 0, NULL, 0),
297
298SND_SOC_DAPM_PGA("Right Speaker Output", WM8960_CLASSD1, 7, 0, NULL, 0),
299SND_SOC_DAPM_PGA("Left Speaker Output", WM8960_CLASSD1, 6, 0, NULL, 0),
300
301SND_SOC_DAPM_OUTPUT("SPK_LP"),
302SND_SOC_DAPM_OUTPUT("SPK_LN"),
303SND_SOC_DAPM_OUTPUT("HP_L"),
304SND_SOC_DAPM_OUTPUT("HP_R"),
305SND_SOC_DAPM_OUTPUT("SPK_RP"),
306SND_SOC_DAPM_OUTPUT("SPK_RN"),
307SND_SOC_DAPM_OUTPUT("OUT3"),
308};
309
Mark Brown913d7b42010-03-03 13:47:03 +0000310static const struct snd_soc_dapm_widget wm8960_dapm_widgets_out3[] = {
311SND_SOC_DAPM_MIXER("Mono Output Mixer", WM8960_POWER2, 1, 0,
312 &wm8960_mono_out[0],
313 ARRAY_SIZE(wm8960_mono_out)),
314};
315
316/* Represent OUT3 as a PGA so that it gets turned on with LOUT1/ROUT1 */
317static const struct snd_soc_dapm_widget wm8960_dapm_widgets_capless[] = {
318SND_SOC_DAPM_PGA("OUT3 VMID", WM8960_POWER2, 1, 0, NULL, 0),
319};
320
Mark Brownf2644a22009-04-07 19:20:14 +0100321static const struct snd_soc_dapm_route audio_paths[] = {
322 { "Left Boost Mixer", "LINPUT1 Switch", "LINPUT1" },
323 { "Left Boost Mixer", "LINPUT2 Switch", "LINPUT2" },
324 { "Left Boost Mixer", "LINPUT3 Switch", "LINPUT3" },
325
326 { "Left Input Mixer", "Boost Switch", "Left Boost Mixer", },
327 { "Left Input Mixer", NULL, "LINPUT1", }, /* Really Boost Switch */
328 { "Left Input Mixer", NULL, "LINPUT2" },
329 { "Left Input Mixer", NULL, "LINPUT3" },
330
331 { "Right Boost Mixer", "RINPUT1 Switch", "RINPUT1" },
332 { "Right Boost Mixer", "RINPUT2 Switch", "RINPUT2" },
333 { "Right Boost Mixer", "RINPUT3 Switch", "RINPUT3" },
334
335 { "Right Input Mixer", "Boost Switch", "Right Boost Mixer", },
336 { "Right Input Mixer", NULL, "RINPUT1", }, /* Really Boost Switch */
337 { "Right Input Mixer", NULL, "RINPUT2" },
338 { "Right Input Mixer", NULL, "LINPUT3" },
339
340 { "Left ADC", NULL, "Left Input Mixer" },
341 { "Right ADC", NULL, "Right Input Mixer" },
342
343 { "Left Output Mixer", "LINPUT3 Switch", "LINPUT3" },
344 { "Left Output Mixer", "Boost Bypass Switch", "Left Boost Mixer"} ,
345 { "Left Output Mixer", "PCM Playback Switch", "Left DAC" },
346
347 { "Right Output Mixer", "RINPUT3 Switch", "RINPUT3" },
348 { "Right Output Mixer", "Boost Bypass Switch", "Right Boost Mixer" } ,
349 { "Right Output Mixer", "PCM Playback Switch", "Right DAC" },
350
Mark Brownf2644a22009-04-07 19:20:14 +0100351 { "LOUT1 PGA", NULL, "Left Output Mixer" },
352 { "ROUT1 PGA", NULL, "Right Output Mixer" },
353
354 { "HP_L", NULL, "LOUT1 PGA" },
355 { "HP_R", NULL, "ROUT1 PGA" },
356
357 { "Left Speaker PGA", NULL, "Left Output Mixer" },
358 { "Right Speaker PGA", NULL, "Right Output Mixer" },
359
360 { "Left Speaker Output", NULL, "Left Speaker PGA" },
361 { "Right Speaker Output", NULL, "Right Speaker PGA" },
362
363 { "SPK_LN", NULL, "Left Speaker Output" },
364 { "SPK_LP", NULL, "Left Speaker Output" },
365 { "SPK_RN", NULL, "Right Speaker Output" },
366 { "SPK_RP", NULL, "Right Speaker Output" },
Mark Brown913d7b42010-03-03 13:47:03 +0000367};
368
369static const struct snd_soc_dapm_route audio_paths_out3[] = {
370 { "Mono Output Mixer", "Left Switch", "Left Output Mixer" },
371 { "Mono Output Mixer", "Right Switch", "Right Output Mixer" },
Mark Brownf2644a22009-04-07 19:20:14 +0100372
373 { "OUT3", NULL, "Mono Output Mixer", }
374};
375
Mark Brown913d7b42010-03-03 13:47:03 +0000376static const struct snd_soc_dapm_route audio_paths_capless[] = {
377 { "HP_L", NULL, "OUT3 VMID" },
378 { "HP_R", NULL, "OUT3 VMID" },
379
380 { "OUT3 VMID", NULL, "Left Output Mixer" },
381 { "OUT3 VMID", NULL, "Right Output Mixer" },
382};
383
Mark Brownf2644a22009-04-07 19:20:14 +0100384static int wm8960_add_widgets(struct snd_soc_codec *codec)
385{
Mark Brown913d7b42010-03-03 13:47:03 +0000386 struct wm8960_data *pdata = codec->dev->platform_data;
Mark Brownb2c812e2010-04-14 15:35:19 +0900387 struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200388 struct snd_soc_dapm_context *dapm = &codec->dapm;
Mark Brown913d7b42010-03-03 13:47:03 +0000389 struct snd_soc_dapm_widget *w;
390
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200391 snd_soc_dapm_new_controls(dapm, wm8960_dapm_widgets,
Mark Brownf2644a22009-04-07 19:20:14 +0100392 ARRAY_SIZE(wm8960_dapm_widgets));
393
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200394 snd_soc_dapm_add_routes(dapm, audio_paths, ARRAY_SIZE(audio_paths));
Mark Brownf2644a22009-04-07 19:20:14 +0100395
Mark Brown913d7b42010-03-03 13:47:03 +0000396 /* In capless mode OUT3 is used to provide VMID for the
397 * headphone outputs, otherwise it is used as a mono mixer.
398 */
399 if (pdata && pdata->capless) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200400 snd_soc_dapm_new_controls(dapm, wm8960_dapm_widgets_capless,
Mark Brown913d7b42010-03-03 13:47:03 +0000401 ARRAY_SIZE(wm8960_dapm_widgets_capless));
402
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200403 snd_soc_dapm_add_routes(dapm, audio_paths_capless,
Mark Brown913d7b42010-03-03 13:47:03 +0000404 ARRAY_SIZE(audio_paths_capless));
405 } else {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200406 snd_soc_dapm_new_controls(dapm, wm8960_dapm_widgets_out3,
Mark Brown913d7b42010-03-03 13:47:03 +0000407 ARRAY_SIZE(wm8960_dapm_widgets_out3));
408
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200409 snd_soc_dapm_add_routes(dapm, audio_paths_out3,
Mark Brown913d7b42010-03-03 13:47:03 +0000410 ARRAY_SIZE(audio_paths_out3));
411 }
412
413 /* We need to power up the headphone output stage out of
414 * sequence for capless mode. To save scanning the widget
415 * list each time to find the desired power state do so now
416 * and save the result.
417 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +0200418 list_for_each_entry(w, &codec->card->widgets, list) {
419 if (w->dapm != &codec->dapm)
420 continue;
Mark Brown913d7b42010-03-03 13:47:03 +0000421 if (strcmp(w->name, "LOUT1 PGA") == 0)
422 wm8960->lout1 = w;
423 if (strcmp(w->name, "ROUT1 PGA") == 0)
424 wm8960->rout1 = w;
425 if (strcmp(w->name, "OUT3 VMID") == 0)
426 wm8960->out3 = w;
427 }
428
Mark Brownf2644a22009-04-07 19:20:14 +0100429 return 0;
430}
431
432static int wm8960_set_dai_fmt(struct snd_soc_dai *codec_dai,
433 unsigned int fmt)
434{
435 struct snd_soc_codec *codec = codec_dai->codec;
436 u16 iface = 0;
437
438 /* set master/slave audio interface */
439 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
440 case SND_SOC_DAIFMT_CBM_CFM:
441 iface |= 0x0040;
442 break;
443 case SND_SOC_DAIFMT_CBS_CFS:
444 break;
445 default:
446 return -EINVAL;
447 }
448
449 /* interface format */
450 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
451 case SND_SOC_DAIFMT_I2S:
452 iface |= 0x0002;
453 break;
454 case SND_SOC_DAIFMT_RIGHT_J:
455 break;
456 case SND_SOC_DAIFMT_LEFT_J:
457 iface |= 0x0001;
458 break;
459 case SND_SOC_DAIFMT_DSP_A:
460 iface |= 0x0003;
461 break;
462 case SND_SOC_DAIFMT_DSP_B:
463 iface |= 0x0013;
464 break;
465 default:
466 return -EINVAL;
467 }
468
469 /* clock inversion */
470 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
471 case SND_SOC_DAIFMT_NB_NF:
472 break;
473 case SND_SOC_DAIFMT_IB_IF:
474 iface |= 0x0090;
475 break;
476 case SND_SOC_DAIFMT_IB_NF:
477 iface |= 0x0080;
478 break;
479 case SND_SOC_DAIFMT_NB_IF:
480 iface |= 0x0010;
481 break;
482 default:
483 return -EINVAL;
484 }
485
486 /* set iface */
Mark Brown17a52fd2009-07-05 17:24:50 +0100487 snd_soc_write(codec, WM8960_IFACE1, iface);
Mark Brownf2644a22009-04-07 19:20:14 +0100488 return 0;
489}
490
Mark Browndb059c02010-07-05 23:54:51 +0900491static struct {
492 int rate;
493 unsigned int val;
494} alc_rates[] = {
495 { 48000, 0 },
496 { 44100, 0 },
497 { 32000, 1 },
498 { 22050, 2 },
499 { 24000, 2 },
500 { 16000, 3 },
501 { 11250, 4 },
502 { 12000, 4 },
503 { 8000, 5 },
504};
505
Mark Brownf2644a22009-04-07 19:20:14 +0100506static int wm8960_hw_params(struct snd_pcm_substream *substream,
507 struct snd_pcm_hw_params *params,
508 struct snd_soc_dai *dai)
509{
510 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000511 struct snd_soc_codec *codec = rtd->codec;
Mark Brownafd6d362010-07-05 13:58:16 +0900512 struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
Mark Brown17a52fd2009-07-05 17:24:50 +0100513 u16 iface = snd_soc_read(codec, WM8960_IFACE1) & 0xfff3;
Mark Browndb059c02010-07-05 23:54:51 +0900514 int i;
Mark Brownf2644a22009-04-07 19:20:14 +0100515
516 /* bit size */
517 switch (params_format(params)) {
518 case SNDRV_PCM_FORMAT_S16_LE:
519 break;
520 case SNDRV_PCM_FORMAT_S20_3LE:
521 iface |= 0x0004;
522 break;
523 case SNDRV_PCM_FORMAT_S24_LE:
524 iface |= 0x0008;
525 break;
526 }
527
Mark Brownafd6d362010-07-05 13:58:16 +0900528 /* Update filters for the new rate */
529 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
530 wm8960->playback_fs = params_rate(params);
531 wm8960_set_deemph(codec);
Mark Browndb059c02010-07-05 23:54:51 +0900532 } else {
533 for (i = 0; i < ARRAY_SIZE(alc_rates); i++)
534 if (alc_rates[i].rate == params_rate(params))
535 snd_soc_update_bits(codec,
536 WM8960_ADDCTL3, 0x7,
537 alc_rates[i].val);
Mark Brownafd6d362010-07-05 13:58:16 +0900538 }
539
Mark Brownf2644a22009-04-07 19:20:14 +0100540 /* set iface */
Mark Brown17a52fd2009-07-05 17:24:50 +0100541 snd_soc_write(codec, WM8960_IFACE1, iface);
Mark Brownf2644a22009-04-07 19:20:14 +0100542 return 0;
543}
544
545static int wm8960_mute(struct snd_soc_dai *dai, int mute)
546{
547 struct snd_soc_codec *codec = dai->codec;
Mark Brown17a52fd2009-07-05 17:24:50 +0100548 u16 mute_reg = snd_soc_read(codec, WM8960_DACCTL1) & 0xfff7;
Mark Brownf2644a22009-04-07 19:20:14 +0100549
550 if (mute)
Mark Brown17a52fd2009-07-05 17:24:50 +0100551 snd_soc_write(codec, WM8960_DACCTL1, mute_reg | 0x8);
Mark Brownf2644a22009-04-07 19:20:14 +0100552 else
Mark Brown17a52fd2009-07-05 17:24:50 +0100553 snd_soc_write(codec, WM8960_DACCTL1, mute_reg);
Mark Brownf2644a22009-04-07 19:20:14 +0100554 return 0;
555}
556
Mark Brown913d7b42010-03-03 13:47:03 +0000557static int wm8960_set_bias_level_out3(struct snd_soc_codec *codec,
558 enum snd_soc_bias_level level)
Mark Brownf2644a22009-04-07 19:20:14 +0100559{
Mark Brownf2644a22009-04-07 19:20:14 +0100560 u16 reg;
561
562 switch (level) {
563 case SND_SOC_BIAS_ON:
564 break;
565
566 case SND_SOC_BIAS_PREPARE:
567 /* Set VMID to 2x50k */
Mark Brown17a52fd2009-07-05 17:24:50 +0100568 reg = snd_soc_read(codec, WM8960_POWER1);
Mark Brownf2644a22009-04-07 19:20:14 +0100569 reg &= ~0x180;
570 reg |= 0x80;
Mark Brown17a52fd2009-07-05 17:24:50 +0100571 snd_soc_write(codec, WM8960_POWER1, reg);
Mark Brownf2644a22009-04-07 19:20:14 +0100572 break;
573
574 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200575 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
Axel Linbc45df22011-10-07 21:50:23 +0800576 snd_soc_cache_sync(codec);
577
Mark Brownf2644a22009-04-07 19:20:14 +0100578 /* Enable anti-pop features */
Mark Brown17a52fd2009-07-05 17:24:50 +0100579 snd_soc_write(codec, WM8960_APOP1,
Mark Brown913d7b42010-03-03 13:47:03 +0000580 WM8960_POBCTRL | WM8960_SOFT_ST |
581 WM8960_BUFDCOPEN | WM8960_BUFIOEN);
Mark Brownf2644a22009-04-07 19:20:14 +0100582
583 /* Enable & ramp VMID at 2x50k */
Mark Brown17a52fd2009-07-05 17:24:50 +0100584 reg = snd_soc_read(codec, WM8960_POWER1);
Mark Brownf2644a22009-04-07 19:20:14 +0100585 reg |= 0x80;
Mark Brown17a52fd2009-07-05 17:24:50 +0100586 snd_soc_write(codec, WM8960_POWER1, reg);
Mark Brownf2644a22009-04-07 19:20:14 +0100587 msleep(100);
588
589 /* Enable VREF */
Mark Brown17a52fd2009-07-05 17:24:50 +0100590 snd_soc_write(codec, WM8960_POWER1, reg | WM8960_VREF);
Mark Brownf2644a22009-04-07 19:20:14 +0100591
592 /* Disable anti-pop features */
Mark Brown17a52fd2009-07-05 17:24:50 +0100593 snd_soc_write(codec, WM8960_APOP1, WM8960_BUFIOEN);
Mark Brownf2644a22009-04-07 19:20:14 +0100594 }
595
596 /* Set VMID to 2x250k */
Mark Brown17a52fd2009-07-05 17:24:50 +0100597 reg = snd_soc_read(codec, WM8960_POWER1);
Mark Brownf2644a22009-04-07 19:20:14 +0100598 reg &= ~0x180;
599 reg |= 0x100;
Mark Brown17a52fd2009-07-05 17:24:50 +0100600 snd_soc_write(codec, WM8960_POWER1, reg);
Mark Brownf2644a22009-04-07 19:20:14 +0100601 break;
602
603 case SND_SOC_BIAS_OFF:
604 /* Enable anti-pop features */
Mark Brown17a52fd2009-07-05 17:24:50 +0100605 snd_soc_write(codec, WM8960_APOP1,
Mark Brownf2644a22009-04-07 19:20:14 +0100606 WM8960_POBCTRL | WM8960_SOFT_ST |
607 WM8960_BUFDCOPEN | WM8960_BUFIOEN);
608
609 /* Disable VMID and VREF, let them discharge */
Mark Brown17a52fd2009-07-05 17:24:50 +0100610 snd_soc_write(codec, WM8960_POWER1, 0);
Mark Brownf2644a22009-04-07 19:20:14 +0100611 msleep(600);
Mark Brown913d7b42010-03-03 13:47:03 +0000612 break;
613 }
Mark Brownf2644a22009-04-07 19:20:14 +0100614
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200615 codec->dapm.bias_level = level;
Mark Brown913d7b42010-03-03 13:47:03 +0000616
617 return 0;
618}
619
620static int wm8960_set_bias_level_capless(struct snd_soc_codec *codec,
621 enum snd_soc_bias_level level)
622{
Mark Brownb2c812e2010-04-14 15:35:19 +0900623 struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
Mark Brown913d7b42010-03-03 13:47:03 +0000624 int reg;
625
626 switch (level) {
627 case SND_SOC_BIAS_ON:
628 break;
629
630 case SND_SOC_BIAS_PREPARE:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200631 switch (codec->dapm.bias_level) {
Mark Brown913d7b42010-03-03 13:47:03 +0000632 case SND_SOC_BIAS_STANDBY:
633 /* Enable anti pop mode */
634 snd_soc_update_bits(codec, WM8960_APOP1,
635 WM8960_POBCTRL | WM8960_SOFT_ST |
636 WM8960_BUFDCOPEN,
637 WM8960_POBCTRL | WM8960_SOFT_ST |
638 WM8960_BUFDCOPEN);
639
640 /* Enable LOUT1, ROUT1 and OUT3 if they're enabled */
641 reg = 0;
642 if (wm8960->lout1 && wm8960->lout1->power)
643 reg |= WM8960_PWR2_LOUT1;
644 if (wm8960->rout1 && wm8960->rout1->power)
645 reg |= WM8960_PWR2_ROUT1;
646 if (wm8960->out3 && wm8960->out3->power)
647 reg |= WM8960_PWR2_OUT3;
648 snd_soc_update_bits(codec, WM8960_POWER2,
649 WM8960_PWR2_LOUT1 |
650 WM8960_PWR2_ROUT1 |
651 WM8960_PWR2_OUT3, reg);
652
653 /* Enable VMID at 2*50k */
654 snd_soc_update_bits(codec, WM8960_POWER1,
655 WM8960_VMID_MASK, 0x80);
656
657 /* Ramp */
658 msleep(100);
659
660 /* Enable VREF */
661 snd_soc_update_bits(codec, WM8960_POWER1,
662 WM8960_VREF, WM8960_VREF);
663
664 msleep(100);
665 break;
666
667 case SND_SOC_BIAS_ON:
668 /* Enable anti-pop mode */
669 snd_soc_update_bits(codec, WM8960_APOP1,
670 WM8960_POBCTRL | WM8960_SOFT_ST |
671 WM8960_BUFDCOPEN,
672 WM8960_POBCTRL | WM8960_SOFT_ST |
673 WM8960_BUFDCOPEN);
674
675 /* Disable VMID and VREF */
676 snd_soc_update_bits(codec, WM8960_POWER1,
677 WM8960_VREF | WM8960_VMID_MASK, 0);
678 break;
679
Axel Linbc45df22011-10-07 21:50:23 +0800680 case SND_SOC_BIAS_OFF:
681 snd_soc_cache_sync(codec);
682 break;
Mark Brown913d7b42010-03-03 13:47:03 +0000683 default:
684 break;
685 }
686 break;
687
688 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200689 switch (codec->dapm.bias_level) {
Mark Brown913d7b42010-03-03 13:47:03 +0000690 case SND_SOC_BIAS_PREPARE:
691 /* Disable HP discharge */
692 snd_soc_update_bits(codec, WM8960_APOP2,
693 WM8960_DISOP | WM8960_DRES_MASK,
694 0);
695
696 /* Disable anti-pop features */
697 snd_soc_update_bits(codec, WM8960_APOP1,
698 WM8960_POBCTRL | WM8960_SOFT_ST |
699 WM8960_BUFDCOPEN,
700 WM8960_POBCTRL | WM8960_SOFT_ST |
701 WM8960_BUFDCOPEN);
702 break;
703
704 default:
705 break;
706 }
707 break;
708
709 case SND_SOC_BIAS_OFF:
Mark Brownf2644a22009-04-07 19:20:14 +0100710 break;
711 }
712
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200713 codec->dapm.bias_level = level;
Mark Brownf2644a22009-04-07 19:20:14 +0100714
715 return 0;
716}
717
718/* PLL divisors */
719struct _pll_div {
720 u32 pre_div:1;
721 u32 n:4;
722 u32 k:24;
723};
724
725/* The size in bits of the pll divide multiplied by 10
726 * to allow rounding later */
727#define FIXED_PLL_SIZE ((1 << 24) * 10)
728
729static int pll_factors(unsigned int source, unsigned int target,
730 struct _pll_div *pll_div)
731{
732 unsigned long long Kpart;
733 unsigned int K, Ndiv, Nmod;
734
735 pr_debug("WM8960 PLL: setting %dHz->%dHz\n", source, target);
736
737 /* Scale up target to PLL operating frequency */
738 target *= 4;
739
740 Ndiv = target / source;
741 if (Ndiv < 6) {
742 source >>= 1;
743 pll_div->pre_div = 1;
744 Ndiv = target / source;
745 } else
746 pll_div->pre_div = 0;
747
748 if ((Ndiv < 6) || (Ndiv > 12)) {
749 pr_err("WM8960 PLL: Unsupported N=%d\n", Ndiv);
750 return -EINVAL;
751 }
752
753 pll_div->n = Ndiv;
754 Nmod = target % source;
755 Kpart = FIXED_PLL_SIZE * (long long)Nmod;
756
757 do_div(Kpart, source);
758
759 K = Kpart & 0xFFFFFFFF;
760
761 /* Check if we need to round */
762 if ((K % 10) >= 5)
763 K += 5;
764
765 /* Move down to proper range now rounding is done */
766 K /= 10;
767
768 pll_div->k = K;
769
770 pr_debug("WM8960 PLL: N=%x K=%x pre_div=%d\n",
771 pll_div->n, pll_div->k, pll_div->pre_div);
772
773 return 0;
774}
775
Mark Brown85488032009-09-05 18:52:16 +0100776static int wm8960_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
777 int source, unsigned int freq_in, unsigned int freq_out)
Mark Brownf2644a22009-04-07 19:20:14 +0100778{
779 struct snd_soc_codec *codec = codec_dai->codec;
780 u16 reg;
781 static struct _pll_div pll_div;
782 int ret;
783
784 if (freq_in && freq_out) {
785 ret = pll_factors(freq_in, freq_out, &pll_div);
786 if (ret != 0)
787 return ret;
788 }
789
790 /* Disable the PLL: even if we are changing the frequency the
791 * PLL needs to be disabled while we do so. */
Mark Brown17a52fd2009-07-05 17:24:50 +0100792 snd_soc_write(codec, WM8960_CLOCK1,
793 snd_soc_read(codec, WM8960_CLOCK1) & ~1);
794 snd_soc_write(codec, WM8960_POWER2,
795 snd_soc_read(codec, WM8960_POWER2) & ~1);
Mark Brownf2644a22009-04-07 19:20:14 +0100796
797 if (!freq_in || !freq_out)
798 return 0;
799
Mark Brown17a52fd2009-07-05 17:24:50 +0100800 reg = snd_soc_read(codec, WM8960_PLL1) & ~0x3f;
Mark Brownf2644a22009-04-07 19:20:14 +0100801 reg |= pll_div.pre_div << 4;
802 reg |= pll_div.n;
803
804 if (pll_div.k) {
805 reg |= 0x20;
806
Mark Brown17a52fd2009-07-05 17:24:50 +0100807 snd_soc_write(codec, WM8960_PLL2, (pll_div.k >> 18) & 0x3f);
808 snd_soc_write(codec, WM8960_PLL3, (pll_div.k >> 9) & 0x1ff);
809 snd_soc_write(codec, WM8960_PLL4, pll_div.k & 0x1ff);
Mark Brownf2644a22009-04-07 19:20:14 +0100810 }
Mark Brown17a52fd2009-07-05 17:24:50 +0100811 snd_soc_write(codec, WM8960_PLL1, reg);
Mark Brownf2644a22009-04-07 19:20:14 +0100812
813 /* Turn it on */
Mark Brown17a52fd2009-07-05 17:24:50 +0100814 snd_soc_write(codec, WM8960_POWER2,
815 snd_soc_read(codec, WM8960_POWER2) | 1);
Mark Brownf2644a22009-04-07 19:20:14 +0100816 msleep(250);
Mark Brown17a52fd2009-07-05 17:24:50 +0100817 snd_soc_write(codec, WM8960_CLOCK1,
818 snd_soc_read(codec, WM8960_CLOCK1) | 1);
Mark Brownf2644a22009-04-07 19:20:14 +0100819
820 return 0;
821}
822
823static int wm8960_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
824 int div_id, int div)
825{
826 struct snd_soc_codec *codec = codec_dai->codec;
827 u16 reg;
828
829 switch (div_id) {
Mark Brownf2644a22009-04-07 19:20:14 +0100830 case WM8960_SYSCLKDIV:
Mark Brown17a52fd2009-07-05 17:24:50 +0100831 reg = snd_soc_read(codec, WM8960_CLOCK1) & 0x1f9;
832 snd_soc_write(codec, WM8960_CLOCK1, reg | div);
Mark Brownf2644a22009-04-07 19:20:14 +0100833 break;
834 case WM8960_DACDIV:
Mark Brown17a52fd2009-07-05 17:24:50 +0100835 reg = snd_soc_read(codec, WM8960_CLOCK1) & 0x1c7;
836 snd_soc_write(codec, WM8960_CLOCK1, reg | div);
Mark Brownf2644a22009-04-07 19:20:14 +0100837 break;
838 case WM8960_OPCLKDIV:
Mark Brown17a52fd2009-07-05 17:24:50 +0100839 reg = snd_soc_read(codec, WM8960_PLL1) & 0x03f;
840 snd_soc_write(codec, WM8960_PLL1, reg | div);
Mark Brownf2644a22009-04-07 19:20:14 +0100841 break;
842 case WM8960_DCLKDIV:
Mark Brown17a52fd2009-07-05 17:24:50 +0100843 reg = snd_soc_read(codec, WM8960_CLOCK2) & 0x03f;
844 snd_soc_write(codec, WM8960_CLOCK2, reg | div);
Mark Brownf2644a22009-04-07 19:20:14 +0100845 break;
846 case WM8960_TOCLKSEL:
Mark Brown17a52fd2009-07-05 17:24:50 +0100847 reg = snd_soc_read(codec, WM8960_ADDCTL1) & 0x1fd;
848 snd_soc_write(codec, WM8960_ADDCTL1, reg | div);
Mark Brownf2644a22009-04-07 19:20:14 +0100849 break;
850 default:
851 return -EINVAL;
852 }
853
854 return 0;
855}
856
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000857static int wm8960_set_bias_level(struct snd_soc_codec *codec,
858 enum snd_soc_bias_level level)
859{
860 struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
861
862 return wm8960->set_bias_level(codec, level);
863}
864
Mark Brownf2644a22009-04-07 19:20:14 +0100865#define WM8960_RATES SNDRV_PCM_RATE_8000_48000
866
867#define WM8960_FORMATS \
868 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
869 SNDRV_PCM_FMTBIT_S24_LE)
870
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100871static const struct snd_soc_dai_ops wm8960_dai_ops = {
Mark Brownf2644a22009-04-07 19:20:14 +0100872 .hw_params = wm8960_hw_params,
873 .digital_mute = wm8960_mute,
874 .set_fmt = wm8960_set_dai_fmt,
875 .set_clkdiv = wm8960_set_dai_clkdiv,
876 .set_pll = wm8960_set_dai_pll,
877};
878
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000879static struct snd_soc_dai_driver wm8960_dai = {
880 .name = "wm8960-hifi",
Mark Brownf2644a22009-04-07 19:20:14 +0100881 .playback = {
882 .stream_name = "Playback",
883 .channels_min = 1,
884 .channels_max = 2,
885 .rates = WM8960_RATES,
886 .formats = WM8960_FORMATS,},
887 .capture = {
888 .stream_name = "Capture",
889 .channels_min = 1,
890 .channels_max = 2,
891 .rates = WM8960_RATES,
892 .formats = WM8960_FORMATS,},
893 .ops = &wm8960_dai_ops,
894 .symmetric_rates = 1,
895};
Mark Brownf2644a22009-04-07 19:20:14 +0100896
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100897static int wm8960_suspend(struct snd_soc_codec *codec)
Mark Brownf2644a22009-04-07 19:20:14 +0100898{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000899 struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
Mark Brownf2644a22009-04-07 19:20:14 +0100900
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000901 wm8960->set_bias_level(codec, SND_SOC_BIAS_OFF);
Mark Brownf2644a22009-04-07 19:20:14 +0100902 return 0;
903}
904
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000905static int wm8960_resume(struct snd_soc_codec *codec)
Mark Brownf2644a22009-04-07 19:20:14 +0100906{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000907 struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
Mark Brownf2644a22009-04-07 19:20:14 +0100908
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000909 wm8960->set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Mark Brownf2644a22009-04-07 19:20:14 +0100910 return 0;
911}
912
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000913static int wm8960_probe(struct snd_soc_codec *codec)
Mark Brownf2644a22009-04-07 19:20:14 +0100914{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000915 struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
916 struct wm8960_data *pdata = dev_get_platdata(codec->dev);
Mark Brownf2644a22009-04-07 19:20:14 +0100917 int ret;
918 u16 reg;
919
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000920 wm8960->set_bias_level = wm8960_set_bias_level_out3;
Mark Brown913d7b42010-03-03 13:47:03 +0000921
Mark Brownf2644a22009-04-07 19:20:14 +0100922 if (!pdata) {
923 dev_warn(codec->dev, "No platform data supplied\n");
924 } else {
925 if (pdata->dres > WM8960_DRES_MAX) {
926 dev_err(codec->dev, "Invalid DRES: %d\n", pdata->dres);
927 pdata->dres = 0;
928 }
Mark Brown913d7b42010-03-03 13:47:03 +0000929
930 if (pdata->capless)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000931 wm8960->set_bias_level = wm8960_set_bias_level_capless;
Mark Brownf2644a22009-04-07 19:20:14 +0100932 }
933
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000934 ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8960->control_type);
Mark Brown17a52fd2009-07-05 17:24:50 +0100935 if (ret < 0) {
936 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000937 return ret;
Mark Brown17a52fd2009-07-05 17:24:50 +0100938 }
939
Mark Brownf2644a22009-04-07 19:20:14 +0100940 ret = wm8960_reset(codec);
941 if (ret < 0) {
942 dev_err(codec->dev, "Failed to issue reset\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000943 return ret;
Mark Brownf2644a22009-04-07 19:20:14 +0100944 }
945
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000946 wm8960->set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Mark Brownf2644a22009-04-07 19:20:14 +0100947
948 /* Latch the update bits */
Mark Brown17a52fd2009-07-05 17:24:50 +0100949 reg = snd_soc_read(codec, WM8960_LINVOL);
950 snd_soc_write(codec, WM8960_LINVOL, reg | 0x100);
951 reg = snd_soc_read(codec, WM8960_RINVOL);
952 snd_soc_write(codec, WM8960_RINVOL, reg | 0x100);
953 reg = snd_soc_read(codec, WM8960_LADC);
954 snd_soc_write(codec, WM8960_LADC, reg | 0x100);
955 reg = snd_soc_read(codec, WM8960_RADC);
956 snd_soc_write(codec, WM8960_RADC, reg | 0x100);
957 reg = snd_soc_read(codec, WM8960_LDAC);
958 snd_soc_write(codec, WM8960_LDAC, reg | 0x100);
959 reg = snd_soc_read(codec, WM8960_RDAC);
960 snd_soc_write(codec, WM8960_RDAC, reg | 0x100);
961 reg = snd_soc_read(codec, WM8960_LOUT1);
962 snd_soc_write(codec, WM8960_LOUT1, reg | 0x100);
963 reg = snd_soc_read(codec, WM8960_ROUT1);
964 snd_soc_write(codec, WM8960_ROUT1, reg | 0x100);
965 reg = snd_soc_read(codec, WM8960_LOUT2);
966 snd_soc_write(codec, WM8960_LOUT2, reg | 0x100);
967 reg = snd_soc_read(codec, WM8960_ROUT2);
968 snd_soc_write(codec, WM8960_ROUT2, reg | 0x100);
Mark Brownf2644a22009-04-07 19:20:14 +0100969
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000970 snd_soc_add_controls(codec, wm8960_snd_controls,
971 ARRAY_SIZE(wm8960_snd_controls));
972 wm8960_add_widgets(codec);
Mark Brownf2644a22009-04-07 19:20:14 +0100973
974 return 0;
975}
976
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000977/* power down chip */
978static int wm8960_remove(struct snd_soc_codec *codec)
Mark Brownf2644a22009-04-07 19:20:14 +0100979{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000980 struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
981
982 wm8960->set_bias_level(codec, SND_SOC_BIAS_OFF);
983 return 0;
Mark Brownf2644a22009-04-07 19:20:14 +0100984}
985
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000986static struct snd_soc_codec_driver soc_codec_dev_wm8960 = {
987 .probe = wm8960_probe,
988 .remove = wm8960_remove,
989 .suspend = wm8960_suspend,
990 .resume = wm8960_resume,
991 .set_bias_level = wm8960_set_bias_level,
992 .reg_cache_size = ARRAY_SIZE(wm8960_reg),
993 .reg_word_size = sizeof(u16),
994 .reg_cache_default = wm8960_reg,
995};
996
997#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Mark Brownf2644a22009-04-07 19:20:14 +0100998static __devinit int wm8960_i2c_probe(struct i2c_client *i2c,
999 const struct i2c_device_id *id)
1000{
1001 struct wm8960_priv *wm8960;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001002 int ret;
Mark Brownf2644a22009-04-07 19:20:14 +01001003
1004 wm8960 = kzalloc(sizeof(struct wm8960_priv), GFP_KERNEL);
1005 if (wm8960 == NULL)
1006 return -ENOMEM;
1007
Mark Brownf2644a22009-04-07 19:20:14 +01001008 i2c_set_clientdata(i2c, wm8960);
Lars-Peter Clausen7f984b52010-12-28 21:08:57 +01001009 wm8960->control_type = SND_SOC_I2C;
Mark Brownf2644a22009-04-07 19:20:14 +01001010
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001011 ret = snd_soc_register_codec(&i2c->dev,
1012 &soc_codec_dev_wm8960, &wm8960_dai, 1);
1013 if (ret < 0)
1014 kfree(wm8960);
1015 return ret;
Mark Brownf2644a22009-04-07 19:20:14 +01001016}
1017
1018static __devexit int wm8960_i2c_remove(struct i2c_client *client)
1019{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001020 snd_soc_unregister_codec(&client->dev);
1021 kfree(i2c_get_clientdata(client));
Mark Brownf2644a22009-04-07 19:20:14 +01001022 return 0;
1023}
1024
1025static const struct i2c_device_id wm8960_i2c_id[] = {
1026 { "wm8960", 0 },
1027 { }
1028};
1029MODULE_DEVICE_TABLE(i2c, wm8960_i2c_id);
1030
1031static struct i2c_driver wm8960_i2c_driver = {
1032 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001033 .name = "wm8960-codec",
Mark Brownf2644a22009-04-07 19:20:14 +01001034 .owner = THIS_MODULE,
1035 },
1036 .probe = wm8960_i2c_probe,
1037 .remove = __devexit_p(wm8960_i2c_remove),
Mark Brownf2644a22009-04-07 19:20:14 +01001038 .id_table = wm8960_i2c_id,
1039};
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001040#endif
Mark Brownf2644a22009-04-07 19:20:14 +01001041
1042static int __init wm8960_modinit(void)
1043{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001044 int ret = 0;
1045#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Mark Brownf2644a22009-04-07 19:20:14 +01001046 ret = i2c_add_driver(&wm8960_i2c_driver);
1047 if (ret != 0) {
1048 printk(KERN_ERR "Failed to register WM8960 I2C driver: %d\n",
1049 ret);
1050 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001051#endif
Mark Brownf2644a22009-04-07 19:20:14 +01001052 return ret;
1053}
1054module_init(wm8960_modinit);
1055
1056static void __exit wm8960_exit(void)
1057{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001058#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Mark Brownf2644a22009-04-07 19:20:14 +01001059 i2c_del_driver(&wm8960_i2c_driver);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001060#endif
Mark Brownf2644a22009-04-07 19:20:14 +01001061}
1062module_exit(wm8960_exit);
1063
Mark Brownf2644a22009-04-07 19:20:14 +01001064MODULE_DESCRIPTION("ASoC WM8960 driver");
1065MODULE_AUTHOR("Liam Girdwood");
1066MODULE_LICENSE("GPL");