blob: ee084083a49d478f058442c2b854ceedbfd9177e [file] [log] [blame]
Richard Purdieabadfc92006-10-06 18:36:39 +02001/*
2 * wm8750.c -- WM8750 ALSA SoC audio driver
3 *
4 * Copyright 2005 Openedhand Ltd.
5 *
6 * Author: Richard Purdie <richard@openedhand.com>
7 *
8 * Based on WM8753.c
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15#include <linux/module.h>
16#include <linux/moduleparam.h>
17#include <linux/init.h>
18#include <linux/delay.h>
19#include <linux/pm.h>
20#include <linux/i2c.h>
21#include <linux/platform_device.h>
Mark Brown2f3dfaf2008-09-16 12:51:26 +010022#include <linux/spi/spi.h>
Richard Purdieabadfc92006-10-06 18:36:39 +020023#include <sound/core.h>
24#include <sound/pcm.h>
25#include <sound/pcm_params.h>
26#include <sound/soc.h>
27#include <sound/soc-dapm.h>
28#include <sound/initval.h>
29
30#include "wm8750.h"
31
Liam Girdwood4422b602007-02-02 17:15:33 +010032/* codec private data */
33struct wm8750_priv {
34 unsigned int sysclk;
35};
36
Richard Purdieabadfc92006-10-06 18:36:39 +020037/*
38 * wm8750 register cache
39 * We can't read the WM8750 register space when we
40 * are using 2 wire for device control, so we cache them instead.
41 */
42static const u16 wm8750_reg[] = {
43 0x0097, 0x0097, 0x0079, 0x0079, /* 0 */
44 0x0000, 0x0008, 0x0000, 0x000a, /* 4 */
45 0x0000, 0x0000, 0x00ff, 0x00ff, /* 8 */
46 0x000f, 0x000f, 0x0000, 0x0000, /* 12 */
47 0x0000, 0x007b, 0x0000, 0x0032, /* 16 */
48 0x0000, 0x00c3, 0x00c3, 0x00c0, /* 20 */
49 0x0000, 0x0000, 0x0000, 0x0000, /* 24 */
50 0x0000, 0x0000, 0x0000, 0x0000, /* 28 */
51 0x0000, 0x0000, 0x0050, 0x0050, /* 32 */
52 0x0050, 0x0050, 0x0050, 0x0050, /* 36 */
53 0x0079, 0x0079, 0x0079, /* 40 */
54};
55
Mark Brown17a52fd2009-07-05 17:24:50 +010056#define wm8750_reset(c) snd_soc_write(c, WM8750_RESET, 0)
Richard Purdieabadfc92006-10-06 18:36:39 +020057
58/*
59 * WM8750 Controls
60 */
61static const char *wm8750_bass[] = {"Linear Control", "Adaptive Boost"};
62static const char *wm8750_bass_filter[] = { "130Hz @ 48kHz", "200Hz @ 48kHz" };
63static const char *wm8750_treble[] = {"8kHz", "4kHz"};
64static const char *wm8750_3d_lc[] = {"200Hz", "500Hz"};
65static const char *wm8750_3d_uc[] = {"2.2kHz", "1.5kHz"};
66static const char *wm8750_3d_func[] = {"Capture", "Playback"};
67static const char *wm8750_alc_func[] = {"Off", "Right", "Left", "Stereo"};
68static const char *wm8750_ng_type[] = {"Constant PGA Gain",
69 "Mute ADC Output"};
70static const char *wm8750_line_mux[] = {"Line 1", "Line 2", "Line 3", "PGA",
71 "Differential"};
72static const char *wm8750_pga_sel[] = {"Line 1", "Line 2", "Line 3",
73 "Differential"};
74static const char *wm8750_out3[] = {"VREF", "ROUT1 + Vol", "MonoOut",
75 "ROUT1"};
76static const char *wm8750_diff_sel[] = {"Line 1", "Line 2"};
77static const char *wm8750_adcpol[] = {"Normal", "L Invert", "R Invert",
78 "L + R Invert"};
79static const char *wm8750_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
80static const char *wm8750_mono_mux[] = {"Stereo", "Mono (Left)",
81 "Mono (Right)", "Digital Mono"};
82
83static const struct soc_enum wm8750_enum[] = {
84SOC_ENUM_SINGLE(WM8750_BASS, 7, 2, wm8750_bass),
85SOC_ENUM_SINGLE(WM8750_BASS, 6, 2, wm8750_bass_filter),
86SOC_ENUM_SINGLE(WM8750_TREBLE, 6, 2, wm8750_treble),
87SOC_ENUM_SINGLE(WM8750_3D, 5, 2, wm8750_3d_lc),
88SOC_ENUM_SINGLE(WM8750_3D, 6, 2, wm8750_3d_uc),
89SOC_ENUM_SINGLE(WM8750_3D, 7, 2, wm8750_3d_func),
90SOC_ENUM_SINGLE(WM8750_ALC1, 7, 4, wm8750_alc_func),
91SOC_ENUM_SINGLE(WM8750_NGATE, 1, 2, wm8750_ng_type),
92SOC_ENUM_SINGLE(WM8750_LOUTM1, 0, 5, wm8750_line_mux),
93SOC_ENUM_SINGLE(WM8750_ROUTM1, 0, 5, wm8750_line_mux),
94SOC_ENUM_SINGLE(WM8750_LADCIN, 6, 4, wm8750_pga_sel), /* 10 */
95SOC_ENUM_SINGLE(WM8750_RADCIN, 6, 4, wm8750_pga_sel),
96SOC_ENUM_SINGLE(WM8750_ADCTL2, 7, 4, wm8750_out3),
97SOC_ENUM_SINGLE(WM8750_ADCIN, 8, 2, wm8750_diff_sel),
98SOC_ENUM_SINGLE(WM8750_ADCDAC, 5, 4, wm8750_adcpol),
99SOC_ENUM_SINGLE(WM8750_ADCDAC, 1, 4, wm8750_deemph),
100SOC_ENUM_SINGLE(WM8750_ADCIN, 6, 4, wm8750_mono_mux), /* 16 */
101
102};
103
104static const struct snd_kcontrol_new wm8750_snd_controls[] = {
105
106SOC_DOUBLE_R("Capture Volume", WM8750_LINVOL, WM8750_RINVOL, 0, 63, 0),
107SOC_DOUBLE_R("Capture ZC Switch", WM8750_LINVOL, WM8750_RINVOL, 6, 1, 0),
108SOC_DOUBLE_R("Capture Switch", WM8750_LINVOL, WM8750_RINVOL, 7, 1, 1),
109
Liam Girdwoodbd903b62006-11-09 16:35:01 +0100110SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8750_LOUT1V,
Richard Purdieabadfc92006-10-06 18:36:39 +0200111 WM8750_ROUT1V, 7, 1, 0),
Liam Girdwoodbd903b62006-11-09 16:35:01 +0100112SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8750_LOUT2V,
Richard Purdieabadfc92006-10-06 18:36:39 +0200113 WM8750_ROUT2V, 7, 1, 0),
114
115SOC_ENUM("Playback De-emphasis", wm8750_enum[15]),
116
117SOC_ENUM("Capture Polarity", wm8750_enum[14]),
118SOC_SINGLE("Playback 6dB Attenuate", WM8750_ADCDAC, 7, 1, 0),
119SOC_SINGLE("Capture 6dB Attenuate", WM8750_ADCDAC, 8, 1, 0),
120
121SOC_DOUBLE_R("PCM Volume", WM8750_LDAC, WM8750_RDAC, 0, 255, 0),
122
123SOC_ENUM("Bass Boost", wm8750_enum[0]),
124SOC_ENUM("Bass Filter", wm8750_enum[1]),
125SOC_SINGLE("Bass Volume", WM8750_BASS, 0, 15, 1),
126
Stanislav Brabec6a7b8cf2007-11-12 12:11:10 +0100127SOC_SINGLE("Treble Volume", WM8750_TREBLE, 0, 15, 1),
Richard Purdieabadfc92006-10-06 18:36:39 +0200128SOC_ENUM("Treble Cut-off", wm8750_enum[2]),
129
130SOC_SINGLE("3D Switch", WM8750_3D, 0, 1, 0),
131SOC_SINGLE("3D Volume", WM8750_3D, 1, 15, 0),
132SOC_ENUM("3D Lower Cut-off", wm8750_enum[3]),
133SOC_ENUM("3D Upper Cut-off", wm8750_enum[4]),
134SOC_ENUM("3D Mode", wm8750_enum[5]),
135
136SOC_SINGLE("ALC Capture Target Volume", WM8750_ALC1, 0, 7, 0),
137SOC_SINGLE("ALC Capture Max Volume", WM8750_ALC1, 4, 7, 0),
138SOC_ENUM("ALC Capture Function", wm8750_enum[6]),
139SOC_SINGLE("ALC Capture ZC Switch", WM8750_ALC2, 7, 1, 0),
140SOC_SINGLE("ALC Capture Hold Time", WM8750_ALC2, 0, 15, 0),
141SOC_SINGLE("ALC Capture Decay Time", WM8750_ALC3, 4, 15, 0),
142SOC_SINGLE("ALC Capture Attack Time", WM8750_ALC3, 0, 15, 0),
143SOC_SINGLE("ALC Capture NG Threshold", WM8750_NGATE, 3, 31, 0),
144SOC_ENUM("ALC Capture NG Type", wm8750_enum[4]),
145SOC_SINGLE("ALC Capture NG Switch", WM8750_NGATE, 0, 1, 0),
146
147SOC_SINGLE("Left ADC Capture Volume", WM8750_LADC, 0, 255, 0),
148SOC_SINGLE("Right ADC Capture Volume", WM8750_RADC, 0, 255, 0),
149
150SOC_SINGLE("ZC Timeout Switch", WM8750_ADCTL1, 0, 1, 0),
151SOC_SINGLE("Playback Invert Switch", WM8750_ADCTL1, 1, 1, 0),
152
Liam Girdwoodbd903b62006-11-09 16:35:01 +0100153SOC_SINGLE("Right Speaker Playback Invert Switch", WM8750_ADCTL2, 4, 1, 0),
Richard Purdieabadfc92006-10-06 18:36:39 +0200154
155/* Unimplemented */
156/* ADCDAC Bit 0 - ADCHPD */
157/* ADCDAC Bit 4 - HPOR */
158/* ADCTL1 Bit 2,3 - DATSEL */
159/* ADCTL1 Bit 4,5 - DMONOMIX */
160/* ADCTL1 Bit 6,7 - VSEL */
161/* ADCTL2 Bit 2 - LRCM */
162/* ADCTL2 Bit 3 - TRI */
163/* ADCTL3 Bit 5 - HPFLREN */
164/* ADCTL3 Bit 6 - VROI */
165/* ADCTL3 Bit 7,8 - ADCLRM */
166/* ADCIN Bit 4 - LDCM */
167/* ADCIN Bit 5 - RDCM */
168
169SOC_DOUBLE_R("Mic Boost", WM8750_LADCIN, WM8750_RADCIN, 4, 3, 0),
170
171SOC_DOUBLE_R("Bypass Left Playback Volume", WM8750_LOUTM1,
172 WM8750_LOUTM2, 4, 7, 1),
173SOC_DOUBLE_R("Bypass Right Playback Volume", WM8750_ROUTM1,
174 WM8750_ROUTM2, 4, 7, 1),
175SOC_DOUBLE_R("Bypass Mono Playback Volume", WM8750_MOUTM1,
176 WM8750_MOUTM2, 4, 7, 1),
177
178SOC_SINGLE("Mono Playback ZC Switch", WM8750_MOUTV, 7, 1, 0),
179
Liam Girdwoodbd903b62006-11-09 16:35:01 +0100180SOC_DOUBLE_R("Headphone Playback Volume", WM8750_LOUT1V, WM8750_ROUT1V,
181 0, 127, 0),
182SOC_DOUBLE_R("Speaker Playback Volume", WM8750_LOUT2V, WM8750_ROUT2V,
183 0, 127, 0),
Richard Purdieabadfc92006-10-06 18:36:39 +0200184
185SOC_SINGLE("Mono Playback Volume", WM8750_MOUTV, 0, 127, 0),
186
187};
188
Richard Purdieabadfc92006-10-06 18:36:39 +0200189/*
190 * DAPM Controls
191 */
192
193/* Left Mixer */
194static const struct snd_kcontrol_new wm8750_left_mixer_controls[] = {
195SOC_DAPM_SINGLE("Playback Switch", WM8750_LOUTM1, 8, 1, 0),
196SOC_DAPM_SINGLE("Left Bypass Switch", WM8750_LOUTM1, 7, 1, 0),
197SOC_DAPM_SINGLE("Right Playback Switch", WM8750_LOUTM2, 8, 1, 0),
198SOC_DAPM_SINGLE("Right Bypass Switch", WM8750_LOUTM2, 7, 1, 0),
199};
200
201/* Right Mixer */
202static const struct snd_kcontrol_new wm8750_right_mixer_controls[] = {
203SOC_DAPM_SINGLE("Left Playback Switch", WM8750_ROUTM1, 8, 1, 0),
204SOC_DAPM_SINGLE("Left Bypass Switch", WM8750_ROUTM1, 7, 1, 0),
205SOC_DAPM_SINGLE("Playback Switch", WM8750_ROUTM2, 8, 1, 0),
206SOC_DAPM_SINGLE("Right Bypass Switch", WM8750_ROUTM2, 7, 1, 0),
207};
208
209/* Mono Mixer */
210static const struct snd_kcontrol_new wm8750_mono_mixer_controls[] = {
211SOC_DAPM_SINGLE("Left Playback Switch", WM8750_MOUTM1, 8, 1, 0),
212SOC_DAPM_SINGLE("Left Bypass Switch", WM8750_MOUTM1, 7, 1, 0),
213SOC_DAPM_SINGLE("Right Playback Switch", WM8750_MOUTM2, 8, 1, 0),
214SOC_DAPM_SINGLE("Right Bypass Switch", WM8750_MOUTM2, 7, 1, 0),
215};
216
217/* Left Line Mux */
218static const struct snd_kcontrol_new wm8750_left_line_controls =
219SOC_DAPM_ENUM("Route", wm8750_enum[8]);
220
221/* Right Line Mux */
222static const struct snd_kcontrol_new wm8750_right_line_controls =
223SOC_DAPM_ENUM("Route", wm8750_enum[9]);
224
225/* Left PGA Mux */
226static const struct snd_kcontrol_new wm8750_left_pga_controls =
227SOC_DAPM_ENUM("Route", wm8750_enum[10]);
228
229/* Right PGA Mux */
230static const struct snd_kcontrol_new wm8750_right_pga_controls =
231SOC_DAPM_ENUM("Route", wm8750_enum[11]);
232
233/* Out 3 Mux */
234static const struct snd_kcontrol_new wm8750_out3_controls =
235SOC_DAPM_ENUM("Route", wm8750_enum[12]);
236
237/* Differential Mux */
238static const struct snd_kcontrol_new wm8750_diffmux_controls =
239SOC_DAPM_ENUM("Route", wm8750_enum[13]);
240
241/* Mono ADC Mux */
242static const struct snd_kcontrol_new wm8750_monomux_controls =
243SOC_DAPM_ENUM("Route", wm8750_enum[16]);
244
245static const struct snd_soc_dapm_widget wm8750_dapm_widgets[] = {
246 SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM, 0, 0,
247 &wm8750_left_mixer_controls[0],
248 ARRAY_SIZE(wm8750_left_mixer_controls)),
249 SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM, 0, 0,
250 &wm8750_right_mixer_controls[0],
251 ARRAY_SIZE(wm8750_right_mixer_controls)),
252 SND_SOC_DAPM_MIXER("Mono Mixer", WM8750_PWR2, 2, 0,
253 &wm8750_mono_mixer_controls[0],
254 ARRAY_SIZE(wm8750_mono_mixer_controls)),
255
256 SND_SOC_DAPM_PGA("Right Out 2", WM8750_PWR2, 3, 0, NULL, 0),
257 SND_SOC_DAPM_PGA("Left Out 2", WM8750_PWR2, 4, 0, NULL, 0),
258 SND_SOC_DAPM_PGA("Right Out 1", WM8750_PWR2, 5, 0, NULL, 0),
259 SND_SOC_DAPM_PGA("Left Out 1", WM8750_PWR2, 6, 0, NULL, 0),
260 SND_SOC_DAPM_DAC("Right DAC", "Right Playback", WM8750_PWR2, 7, 0),
261 SND_SOC_DAPM_DAC("Left DAC", "Left Playback", WM8750_PWR2, 8, 0),
262
263 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8750_PWR1, 1, 0),
264 SND_SOC_DAPM_ADC("Right ADC", "Right Capture", WM8750_PWR1, 2, 0),
265 SND_SOC_DAPM_ADC("Left ADC", "Left Capture", WM8750_PWR1, 3, 0),
266
267 SND_SOC_DAPM_MUX("Left PGA Mux", WM8750_PWR1, 5, 0,
268 &wm8750_left_pga_controls),
269 SND_SOC_DAPM_MUX("Right PGA Mux", WM8750_PWR1, 4, 0,
270 &wm8750_right_pga_controls),
271 SND_SOC_DAPM_MUX("Left Line Mux", SND_SOC_NOPM, 0, 0,
272 &wm8750_left_line_controls),
273 SND_SOC_DAPM_MUX("Right Line Mux", SND_SOC_NOPM, 0, 0,
274 &wm8750_right_line_controls),
275
276 SND_SOC_DAPM_MUX("Out3 Mux", SND_SOC_NOPM, 0, 0, &wm8750_out3_controls),
277 SND_SOC_DAPM_PGA("Out 3", WM8750_PWR2, 1, 0, NULL, 0),
278 SND_SOC_DAPM_PGA("Mono Out 1", WM8750_PWR2, 2, 0, NULL, 0),
279
280 SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM, 0, 0,
281 &wm8750_diffmux_controls),
282 SND_SOC_DAPM_MUX("Left ADC Mux", SND_SOC_NOPM, 0, 0,
283 &wm8750_monomux_controls),
284 SND_SOC_DAPM_MUX("Right ADC Mux", SND_SOC_NOPM, 0, 0,
285 &wm8750_monomux_controls),
286
287 SND_SOC_DAPM_OUTPUT("LOUT1"),
288 SND_SOC_DAPM_OUTPUT("ROUT1"),
289 SND_SOC_DAPM_OUTPUT("LOUT2"),
290 SND_SOC_DAPM_OUTPUT("ROUT2"),
Dmitry Baryshkov23ba79bd2008-08-09 15:05:28 +0400291 SND_SOC_DAPM_OUTPUT("MONO1"),
Richard Purdieabadfc92006-10-06 18:36:39 +0200292 SND_SOC_DAPM_OUTPUT("OUT3"),
Dmitry Baryshkov04489ee2008-08-12 02:45:31 +0400293 SND_SOC_DAPM_OUTPUT("VREF"),
Richard Purdieabadfc92006-10-06 18:36:39 +0200294
295 SND_SOC_DAPM_INPUT("LINPUT1"),
296 SND_SOC_DAPM_INPUT("LINPUT2"),
297 SND_SOC_DAPM_INPUT("LINPUT3"),
298 SND_SOC_DAPM_INPUT("RINPUT1"),
299 SND_SOC_DAPM_INPUT("RINPUT2"),
300 SND_SOC_DAPM_INPUT("RINPUT3"),
301};
302
Mark Browna65f0562008-05-13 14:54:43 +0200303static const struct snd_soc_dapm_route audio_map[] = {
Richard Purdieabadfc92006-10-06 18:36:39 +0200304 /* left mixer */
305 {"Left Mixer", "Playback Switch", "Left DAC"},
306 {"Left Mixer", "Left Bypass Switch", "Left Line Mux"},
307 {"Left Mixer", "Right Playback Switch", "Right DAC"},
308 {"Left Mixer", "Right Bypass Switch", "Right Line Mux"},
309
310 /* right mixer */
311 {"Right Mixer", "Left Playback Switch", "Left DAC"},
312 {"Right Mixer", "Left Bypass Switch", "Left Line Mux"},
313 {"Right Mixer", "Playback Switch", "Right DAC"},
314 {"Right Mixer", "Right Bypass Switch", "Right Line Mux"},
315
316 /* left out 1 */
317 {"Left Out 1", NULL, "Left Mixer"},
318 {"LOUT1", NULL, "Left Out 1"},
319
320 /* left out 2 */
321 {"Left Out 2", NULL, "Left Mixer"},
322 {"LOUT2", NULL, "Left Out 2"},
323
324 /* right out 1 */
325 {"Right Out 1", NULL, "Right Mixer"},
326 {"ROUT1", NULL, "Right Out 1"},
327
328 /* right out 2 */
329 {"Right Out 2", NULL, "Right Mixer"},
330 {"ROUT2", NULL, "Right Out 2"},
331
332 /* mono mixer */
333 {"Mono Mixer", "Left Playback Switch", "Left DAC"},
334 {"Mono Mixer", "Left Bypass Switch", "Left Line Mux"},
335 {"Mono Mixer", "Right Playback Switch", "Right DAC"},
336 {"Mono Mixer", "Right Bypass Switch", "Right Line Mux"},
337
338 /* mono out */
339 {"Mono Out 1", NULL, "Mono Mixer"},
340 {"MONO1", NULL, "Mono Out 1"},
341
342 /* out 3 */
343 {"Out3 Mux", "VREF", "VREF"},
344 {"Out3 Mux", "ROUT1 + Vol", "ROUT1"},
345 {"Out3 Mux", "ROUT1", "Right Mixer"},
346 {"Out3 Mux", "MonoOut", "MONO1"},
347 {"Out 3", NULL, "Out3 Mux"},
348 {"OUT3", NULL, "Out 3"},
349
350 /* Left Line Mux */
351 {"Left Line Mux", "Line 1", "LINPUT1"},
352 {"Left Line Mux", "Line 2", "LINPUT2"},
353 {"Left Line Mux", "Line 3", "LINPUT3"},
354 {"Left Line Mux", "PGA", "Left PGA Mux"},
355 {"Left Line Mux", "Differential", "Differential Mux"},
356
357 /* Right Line Mux */
358 {"Right Line Mux", "Line 1", "RINPUT1"},
359 {"Right Line Mux", "Line 2", "RINPUT2"},
360 {"Right Line Mux", "Line 3", "RINPUT3"},
361 {"Right Line Mux", "PGA", "Right PGA Mux"},
362 {"Right Line Mux", "Differential", "Differential Mux"},
363
364 /* Left PGA Mux */
365 {"Left PGA Mux", "Line 1", "LINPUT1"},
366 {"Left PGA Mux", "Line 2", "LINPUT2"},
367 {"Left PGA Mux", "Line 3", "LINPUT3"},
368 {"Left PGA Mux", "Differential", "Differential Mux"},
369
370 /* Right PGA Mux */
371 {"Right PGA Mux", "Line 1", "RINPUT1"},
372 {"Right PGA Mux", "Line 2", "RINPUT2"},
373 {"Right PGA Mux", "Line 3", "RINPUT3"},
374 {"Right PGA Mux", "Differential", "Differential Mux"},
375
376 /* Differential Mux */
377 {"Differential Mux", "Line 1", "LINPUT1"},
378 {"Differential Mux", "Line 1", "RINPUT1"},
379 {"Differential Mux", "Line 2", "LINPUT2"},
380 {"Differential Mux", "Line 2", "RINPUT2"},
381
382 /* Left ADC Mux */
383 {"Left ADC Mux", "Stereo", "Left PGA Mux"},
384 {"Left ADC Mux", "Mono (Left)", "Left PGA Mux"},
385 {"Left ADC Mux", "Digital Mono", "Left PGA Mux"},
386
387 /* Right ADC Mux */
388 {"Right ADC Mux", "Stereo", "Right PGA Mux"},
389 {"Right ADC Mux", "Mono (Right)", "Right PGA Mux"},
390 {"Right ADC Mux", "Digital Mono", "Right PGA Mux"},
391
392 /* ADC */
393 {"Left ADC", NULL, "Left ADC Mux"},
394 {"Right ADC", NULL, "Right ADC Mux"},
Richard Purdieabadfc92006-10-06 18:36:39 +0200395};
396
397static int wm8750_add_widgets(struct snd_soc_codec *codec)
398{
Mark Browna65f0562008-05-13 14:54:43 +0200399 snd_soc_dapm_new_controls(codec, wm8750_dapm_widgets,
400 ARRAY_SIZE(wm8750_dapm_widgets));
Richard Purdieabadfc92006-10-06 18:36:39 +0200401
Mark Browna65f0562008-05-13 14:54:43 +0200402 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
Richard Purdieabadfc92006-10-06 18:36:39 +0200403
Richard Purdieabadfc92006-10-06 18:36:39 +0200404 return 0;
405}
406
407struct _coeff_div {
408 u32 mclk;
409 u32 rate;
410 u16 fs;
411 u8 sr:5;
412 u8 usb:1;
413};
414
415/* codec hifi mclk clock divider coefficients */
416static const struct _coeff_div coeff_div[] = {
417 /* 8k */
418 {12288000, 8000, 1536, 0x6, 0x0},
419 {11289600, 8000, 1408, 0x16, 0x0},
420 {18432000, 8000, 2304, 0x7, 0x0},
421 {16934400, 8000, 2112, 0x17, 0x0},
422 {12000000, 8000, 1500, 0x6, 0x1},
423
424 /* 11.025k */
425 {11289600, 11025, 1024, 0x18, 0x0},
426 {16934400, 11025, 1536, 0x19, 0x0},
427 {12000000, 11025, 1088, 0x19, 0x1},
428
429 /* 16k */
430 {12288000, 16000, 768, 0xa, 0x0},
431 {18432000, 16000, 1152, 0xb, 0x0},
432 {12000000, 16000, 750, 0xa, 0x1},
433
434 /* 22.05k */
435 {11289600, 22050, 512, 0x1a, 0x0},
436 {16934400, 22050, 768, 0x1b, 0x0},
437 {12000000, 22050, 544, 0x1b, 0x1},
438
439 /* 32k */
440 {12288000, 32000, 384, 0xc, 0x0},
441 {18432000, 32000, 576, 0xd, 0x0},
442 {12000000, 32000, 375, 0xa, 0x1},
443
444 /* 44.1k */
445 {11289600, 44100, 256, 0x10, 0x0},
446 {16934400, 44100, 384, 0x11, 0x0},
447 {12000000, 44100, 272, 0x11, 0x1},
448
449 /* 48k */
450 {12288000, 48000, 256, 0x0, 0x0},
451 {18432000, 48000, 384, 0x1, 0x0},
452 {12000000, 48000, 250, 0x0, 0x1},
453
454 /* 88.2k */
455 {11289600, 88200, 128, 0x1e, 0x0},
456 {16934400, 88200, 192, 0x1f, 0x0},
457 {12000000, 88200, 136, 0x1f, 0x1},
458
459 /* 96k */
460 {12288000, 96000, 128, 0xe, 0x0},
461 {18432000, 96000, 192, 0xf, 0x0},
462 {12000000, 96000, 125, 0xe, 0x1},
463};
464
465static inline int get_coeff(int mclk, int rate)
466{
467 int i;
468
469 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
470 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
471 return i;
472 }
Liam Girdwooda71a4682006-10-19 20:35:56 +0200473
474 printk(KERN_ERR "wm8750: could not get coeff for mclk %d @ rate %d\n",
475 mclk, rate);
Richard Purdieabadfc92006-10-06 18:36:39 +0200476 return -EINVAL;
477}
478
Liam Girdwoode550e172008-07-07 16:07:52 +0100479static int wm8750_set_dai_sysclk(struct snd_soc_dai *codec_dai,
Liam Girdwood4422b602007-02-02 17:15:33 +0100480 int clk_id, unsigned int freq, int dir)
Richard Purdieabadfc92006-10-06 18:36:39 +0200481{
Liam Girdwood4422b602007-02-02 17:15:33 +0100482 struct snd_soc_codec *codec = codec_dai->codec;
483 struct wm8750_priv *wm8750 = codec->private_data;
484
485 switch (freq) {
486 case 11289600:
487 case 12000000:
488 case 12288000:
489 case 16934400:
490 case 18432000:
491 wm8750->sysclk = freq;
492 return 0;
493 }
494 return -EINVAL;
Richard Purdieabadfc92006-10-06 18:36:39 +0200495}
496
Liam Girdwoode550e172008-07-07 16:07:52 +0100497static int wm8750_set_dai_fmt(struct snd_soc_dai *codec_dai,
Liam Girdwood4422b602007-02-02 17:15:33 +0100498 unsigned int fmt)
Richard Purdieabadfc92006-10-06 18:36:39 +0200499{
Liam Girdwood4422b602007-02-02 17:15:33 +0100500 struct snd_soc_codec *codec = codec_dai->codec;
501 u16 iface = 0;
Richard Purdieabadfc92006-10-06 18:36:39 +0200502
503 /* set master/slave audio interface */
Liam Girdwood4422b602007-02-02 17:15:33 +0100504 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
Richard Purdieabadfc92006-10-06 18:36:39 +0200505 case SND_SOC_DAIFMT_CBM_CFM:
506 iface = 0x0040;
507 break;
508 case SND_SOC_DAIFMT_CBS_CFS:
509 break;
Liam Girdwood4422b602007-02-02 17:15:33 +0100510 default:
511 return -EINVAL;
Richard Purdieabadfc92006-10-06 18:36:39 +0200512 }
513
514 /* interface format */
Liam Girdwood4422b602007-02-02 17:15:33 +0100515 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
Richard Purdieabadfc92006-10-06 18:36:39 +0200516 case SND_SOC_DAIFMT_I2S:
517 iface |= 0x0002;
518 break;
519 case SND_SOC_DAIFMT_RIGHT_J:
520 break;
521 case SND_SOC_DAIFMT_LEFT_J:
522 iface |= 0x0001;
523 break;
524 case SND_SOC_DAIFMT_DSP_A:
525 iface |= 0x0003;
526 break;
527 case SND_SOC_DAIFMT_DSP_B:
528 iface |= 0x0013;
529 break;
Liam Girdwood4422b602007-02-02 17:15:33 +0100530 default:
531 return -EINVAL;
Richard Purdieabadfc92006-10-06 18:36:39 +0200532 }
533
534 /* clock inversion */
Liam Girdwood4422b602007-02-02 17:15:33 +0100535 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
Richard Purdieabadfc92006-10-06 18:36:39 +0200536 case SND_SOC_DAIFMT_NB_NF:
537 break;
538 case SND_SOC_DAIFMT_IB_IF:
539 iface |= 0x0090;
540 break;
541 case SND_SOC_DAIFMT_IB_NF:
542 iface |= 0x0080;
543 break;
544 case SND_SOC_DAIFMT_NB_IF:
545 iface |= 0x0010;
546 break;
Liam Girdwood4422b602007-02-02 17:15:33 +0100547 default:
548 return -EINVAL;
Richard Purdieabadfc92006-10-06 18:36:39 +0200549 }
550
Mark Brown17a52fd2009-07-05 17:24:50 +0100551 snd_soc_write(codec, WM8750_IFACE, iface);
Liam Girdwood4422b602007-02-02 17:15:33 +0100552 return 0;
553}
554
555static int wm8750_pcm_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000556 struct snd_pcm_hw_params *params,
557 struct snd_soc_dai *dai)
Liam Girdwood4422b602007-02-02 17:15:33 +0100558{
559 struct snd_soc_pcm_runtime *rtd = substream->private_data;
560 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000561 struct snd_soc_codec *codec = socdev->card->codec;
Liam Girdwood4422b602007-02-02 17:15:33 +0100562 struct wm8750_priv *wm8750 = codec->private_data;
Mark Brown17a52fd2009-07-05 17:24:50 +0100563 u16 iface = snd_soc_read(codec, WM8750_IFACE) & 0x1f3;
564 u16 srate = snd_soc_read(codec, WM8750_SRATE) & 0x1c0;
Liam Girdwood4422b602007-02-02 17:15:33 +0100565 int coeff = get_coeff(wm8750->sysclk, params_rate(params));
566
567 /* bit size */
568 switch (params_format(params)) {
569 case SNDRV_PCM_FORMAT_S16_LE:
Richard Purdieabadfc92006-10-06 18:36:39 +0200570 break;
Liam Girdwood4422b602007-02-02 17:15:33 +0100571 case SNDRV_PCM_FORMAT_S20_3LE:
572 iface |= 0x0004;
Richard Purdieabadfc92006-10-06 18:36:39 +0200573 break;
Liam Girdwood4422b602007-02-02 17:15:33 +0100574 case SNDRV_PCM_FORMAT_S24_LE:
575 iface |= 0x0008;
Richard Purdieabadfc92006-10-06 18:36:39 +0200576 break;
Liam Girdwood4422b602007-02-02 17:15:33 +0100577 case SNDRV_PCM_FORMAT_S32_LE:
578 iface |= 0x000c;
Richard Purdieabadfc92006-10-06 18:36:39 +0200579 break;
580 }
581
582 /* set iface & srate */
Mark Brown17a52fd2009-07-05 17:24:50 +0100583 snd_soc_write(codec, WM8750_IFACE, iface);
Liam Girdwood4422b602007-02-02 17:15:33 +0100584 if (coeff >= 0)
Mark Brown17a52fd2009-07-05 17:24:50 +0100585 snd_soc_write(codec, WM8750_SRATE, srate |
Liam Girdwood4422b602007-02-02 17:15:33 +0100586 (coeff_div[coeff].sr << 1) | coeff_div[coeff].usb);
Richard Purdieabadfc92006-10-06 18:36:39 +0200587
588 return 0;
589}
590
Liam Girdwoode550e172008-07-07 16:07:52 +0100591static int wm8750_mute(struct snd_soc_dai *dai, int mute)
Richard Purdieabadfc92006-10-06 18:36:39 +0200592{
Liam Girdwood4422b602007-02-02 17:15:33 +0100593 struct snd_soc_codec *codec = dai->codec;
Mark Brown17a52fd2009-07-05 17:24:50 +0100594 u16 mute_reg = snd_soc_read(codec, WM8750_ADCDAC) & 0xfff7;
Liam Girdwood4422b602007-02-02 17:15:33 +0100595
Richard Purdieabadfc92006-10-06 18:36:39 +0200596 if (mute)
Mark Brown17a52fd2009-07-05 17:24:50 +0100597 snd_soc_write(codec, WM8750_ADCDAC, mute_reg | 0x8);
Richard Purdieabadfc92006-10-06 18:36:39 +0200598 else
Mark Brown17a52fd2009-07-05 17:24:50 +0100599 snd_soc_write(codec, WM8750_ADCDAC, mute_reg);
Richard Purdieabadfc92006-10-06 18:36:39 +0200600 return 0;
601}
602
Mark Brown0be98982008-05-19 12:31:28 +0200603static int wm8750_set_bias_level(struct snd_soc_codec *codec,
604 enum snd_soc_bias_level level)
Richard Purdieabadfc92006-10-06 18:36:39 +0200605{
Mark Brown17a52fd2009-07-05 17:24:50 +0100606 u16 pwr_reg = snd_soc_read(codec, WM8750_PWR1) & 0xfe3e;
Richard Purdieabadfc92006-10-06 18:36:39 +0200607
Mark Brown0be98982008-05-19 12:31:28 +0200608 switch (level) {
609 case SND_SOC_BIAS_ON:
Richard Purdieabadfc92006-10-06 18:36:39 +0200610 /* set vmid to 50k and unmute dac */
Mark Brown17a52fd2009-07-05 17:24:50 +0100611 snd_soc_write(codec, WM8750_PWR1, pwr_reg | 0x00c0);
Richard Purdieabadfc92006-10-06 18:36:39 +0200612 break;
Mark Brown0be98982008-05-19 12:31:28 +0200613 case SND_SOC_BIAS_PREPARE:
Richard Purdieabadfc92006-10-06 18:36:39 +0200614 break;
Mark Brown0be98982008-05-19 12:31:28 +0200615 case SND_SOC_BIAS_STANDBY:
Mark Browndd767692010-03-17 11:51:33 +0000616 if (codec->bias_level == SND_SOC_BIAS_OFF) {
617 /* Set VMID to 5k */
618 snd_soc_write(codec, WM8750_PWR1, pwr_reg | 0x01c1);
619
620 /* ...and ramp */
621 msleep(1000);
622 }
623
Richard Purdieabadfc92006-10-06 18:36:39 +0200624 /* mute dac and set vmid to 500k, enable VREF */
Mark Brown17a52fd2009-07-05 17:24:50 +0100625 snd_soc_write(codec, WM8750_PWR1, pwr_reg | 0x0141);
Richard Purdieabadfc92006-10-06 18:36:39 +0200626 break;
Mark Brown0be98982008-05-19 12:31:28 +0200627 case SND_SOC_BIAS_OFF:
Mark Brown17a52fd2009-07-05 17:24:50 +0100628 snd_soc_write(codec, WM8750_PWR1, 0x0001);
Richard Purdieabadfc92006-10-06 18:36:39 +0200629 break;
630 }
Mark Brown0be98982008-05-19 12:31:28 +0200631 codec->bias_level = level;
Richard Purdieabadfc92006-10-06 18:36:39 +0200632 return 0;
633}
634
Liam Girdwood4422b602007-02-02 17:15:33 +0100635#define WM8750_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
Mark Brown42f30302008-04-23 15:17:12 +0200636 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_44100 | \
637 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
Liam Girdwood4422b602007-02-02 17:15:33 +0100638
639#define WM8750_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
640 SNDRV_PCM_FMTBIT_S24_LE)
641
Eric Miao6335d052009-03-03 09:41:00 +0800642static struct snd_soc_dai_ops wm8750_dai_ops = {
643 .hw_params = wm8750_pcm_hw_params,
644 .digital_mute = wm8750_mute,
645 .set_fmt = wm8750_set_dai_fmt,
646 .set_sysclk = wm8750_set_dai_sysclk,
647};
648
Liam Girdwoode550e172008-07-07 16:07:52 +0100649struct snd_soc_dai wm8750_dai = {
Richard Purdieabadfc92006-10-06 18:36:39 +0200650 .name = "WM8750",
651 .playback = {
652 .stream_name = "Playback",
653 .channels_min = 1,
654 .channels_max = 2,
Liam Girdwood4422b602007-02-02 17:15:33 +0100655 .rates = WM8750_RATES,
656 .formats = WM8750_FORMATS,},
Richard Purdieabadfc92006-10-06 18:36:39 +0200657 .capture = {
658 .stream_name = "Capture",
659 .channels_min = 1,
660 .channels_max = 2,
Liam Girdwood4422b602007-02-02 17:15:33 +0100661 .rates = WM8750_RATES,
662 .formats = WM8750_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +0800663 .ops = &wm8750_dai_ops,
Richard Purdieabadfc92006-10-06 18:36:39 +0200664};
665EXPORT_SYMBOL_GPL(wm8750_dai);
666
Richard Purdieabadfc92006-10-06 18:36:39 +0200667static int wm8750_suspend(struct platform_device *pdev, pm_message_t state)
668{
669 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +0000670 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdieabadfc92006-10-06 18:36:39 +0200671
Mark Brown0be98982008-05-19 12:31:28 +0200672 wm8750_set_bias_level(codec, SND_SOC_BIAS_OFF);
Richard Purdieabadfc92006-10-06 18:36:39 +0200673 return 0;
674}
675
676static int wm8750_resume(struct platform_device *pdev)
677{
678 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +0000679 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdieabadfc92006-10-06 18:36:39 +0200680 int i;
681 u8 data[2];
682 u16 *cache = codec->reg_cache;
683
684 /* Sync reg_cache with the hardware */
685 for (i = 0; i < ARRAY_SIZE(wm8750_reg); i++) {
686 if (i == WM8750_RESET)
687 continue;
688 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
689 data[1] = cache[i] & 0x00ff;
690 codec->hw_write(codec->control_data, data, 2);
691 }
692
Mark Brown0be98982008-05-19 12:31:28 +0200693 wm8750_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Richard Purdieabadfc92006-10-06 18:36:39 +0200694
Richard Purdieabadfc92006-10-06 18:36:39 +0200695 return 0;
696}
697
698/*
699 * initialise the WM8750 driver
700 * register the mixer and dsp interfaces with the kernel
701 */
Mark Brown7084a422009-07-10 22:24:27 +0100702static int wm8750_init(struct snd_soc_device *socdev,
703 enum snd_soc_control_type control)
Richard Purdieabadfc92006-10-06 18:36:39 +0200704{
Mark Brown6627a652009-01-23 22:55:23 +0000705 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdieabadfc92006-10-06 18:36:39 +0200706 int reg, ret = 0;
707
708 codec->name = "WM8750";
709 codec->owner = THIS_MODULE;
Mark Brown0be98982008-05-19 12:31:28 +0200710 codec->set_bias_level = wm8750_set_bias_level;
Richard Purdieabadfc92006-10-06 18:36:39 +0200711 codec->dai = &wm8750_dai;
712 codec->num_dai = 1;
Mark Brownd751b232008-06-11 13:47:06 +0100713 codec->reg_cache_size = ARRAY_SIZE(wm8750_reg);
Andrew Morton713fb932007-05-05 12:02:25 +0200714 codec->reg_cache = kmemdup(wm8750_reg, sizeof(wm8750_reg), GFP_KERNEL);
Richard Purdieabadfc92006-10-06 18:36:39 +0200715 if (codec->reg_cache == NULL)
716 return -ENOMEM;
Richard Purdieabadfc92006-10-06 18:36:39 +0200717
Mark Brown7084a422009-07-10 22:24:27 +0100718 ret = snd_soc_codec_set_cache_io(codec, 7, 9, control);
Mark Brown17a52fd2009-07-05 17:24:50 +0100719 if (ret < 0) {
720 printk(KERN_ERR "wm8750: failed to set cache I/O: %d\n", ret);
721 goto err;
722 }
723
724 ret = wm8750_reset(codec);
725 if (ret < 0) {
726 printk(KERN_ERR "wm8750: failed to reset: %d\n", ret);
727 goto err;
728 }
Richard Purdieabadfc92006-10-06 18:36:39 +0200729
730 /* register pcms */
731 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
732 if (ret < 0) {
Liam Girdwoode35115a2007-01-31 10:02:23 +0100733 printk(KERN_ERR "wm8750: failed to create pcms\n");
Mark Brown17a52fd2009-07-05 17:24:50 +0100734 goto err;
Richard Purdieabadfc92006-10-06 18:36:39 +0200735 }
736
737 /* charge output caps */
Mark Browndd767692010-03-17 11:51:33 +0000738 wm8750_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Richard Purdieabadfc92006-10-06 18:36:39 +0200739
740 /* set the update bits */
Mark Brown17a52fd2009-07-05 17:24:50 +0100741 reg = snd_soc_read(codec, WM8750_LDAC);
742 snd_soc_write(codec, WM8750_LDAC, reg | 0x0100);
743 reg = snd_soc_read(codec, WM8750_RDAC);
744 snd_soc_write(codec, WM8750_RDAC, reg | 0x0100);
745 reg = snd_soc_read(codec, WM8750_LOUT1V);
746 snd_soc_write(codec, WM8750_LOUT1V, reg | 0x0100);
747 reg = snd_soc_read(codec, WM8750_ROUT1V);
748 snd_soc_write(codec, WM8750_ROUT1V, reg | 0x0100);
749 reg = snd_soc_read(codec, WM8750_LOUT2V);
750 snd_soc_write(codec, WM8750_LOUT2V, reg | 0x0100);
751 reg = snd_soc_read(codec, WM8750_ROUT2V);
752 snd_soc_write(codec, WM8750_ROUT2V, reg | 0x0100);
753 reg = snd_soc_read(codec, WM8750_LINVOL);
754 snd_soc_write(codec, WM8750_LINVOL, reg | 0x0100);
755 reg = snd_soc_read(codec, WM8750_RINVOL);
756 snd_soc_write(codec, WM8750_RINVOL, reg | 0x0100);
Richard Purdieabadfc92006-10-06 18:36:39 +0200757
Ian Molton3e8e1952009-01-09 00:23:21 +0000758 snd_soc_add_controls(codec, wm8750_snd_controls,
759 ARRAY_SIZE(wm8750_snd_controls));
Richard Purdieabadfc92006-10-06 18:36:39 +0200760 wm8750_add_widgets(codec);
Liam Girdwoode35115a2007-01-31 10:02:23 +0100761 return ret;
Richard Purdieabadfc92006-10-06 18:36:39 +0200762
Mark Brown17a52fd2009-07-05 17:24:50 +0100763err:
Liam Girdwoode35115a2007-01-31 10:02:23 +0100764 kfree(codec->reg_cache);
Richard Purdieabadfc92006-10-06 18:36:39 +0200765 return ret;
766}
767
768/* If the i2c layer weren't so broken, we could pass this kind of data
769 around */
770static struct snd_soc_device *wm8750_socdev;
771
Mark Brown42f30302008-04-23 15:17:12 +0200772#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Richard Purdieabadfc92006-10-06 18:36:39 +0200773
774/*
Mark Brown2f3dfaf2008-09-16 12:51:26 +0100775 * WM8750 2 wire address is determined by GPIO5
Richard Purdieabadfc92006-10-06 18:36:39 +0200776 * state during powerup.
777 * low = 0x1a
778 * high = 0x1b
779 */
Richard Purdieabadfc92006-10-06 18:36:39 +0200780
Jean Delvareee1d0092008-09-01 18:47:00 +0100781static int wm8750_i2c_probe(struct i2c_client *i2c,
782 const struct i2c_device_id *id)
Richard Purdieabadfc92006-10-06 18:36:39 +0200783{
784 struct snd_soc_device *socdev = wm8750_socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000785 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdieabadfc92006-10-06 18:36:39 +0200786 int ret;
787
Richard Purdieabadfc92006-10-06 18:36:39 +0200788 i2c_set_clientdata(i2c, codec);
789 codec->control_data = i2c;
790
Mark Brown7084a422009-07-10 22:24:27 +0100791 ret = wm8750_init(socdev, SND_SOC_I2C);
Jean Delvareee1d0092008-09-01 18:47:00 +0100792 if (ret < 0)
Mark Browna5c95e92008-06-23 14:51:29 +0100793 pr_err("failed to initialise WM8750\n");
Richard Purdieabadfc92006-10-06 18:36:39 +0200794
Richard Purdieabadfc92006-10-06 18:36:39 +0200795 return ret;
796}
797
Jean Delvareee1d0092008-09-01 18:47:00 +0100798static int wm8750_i2c_remove(struct i2c_client *client)
Richard Purdieabadfc92006-10-06 18:36:39 +0200799{
800 struct snd_soc_codec *codec = i2c_get_clientdata(client);
Richard Purdieabadfc92006-10-06 18:36:39 +0200801 kfree(codec->reg_cache);
Richard Purdieabadfc92006-10-06 18:36:39 +0200802 return 0;
803}
804
Jean Delvareee1d0092008-09-01 18:47:00 +0100805static const struct i2c_device_id wm8750_i2c_id[] = {
806 { "wm8750", 0 },
807 { }
808};
809MODULE_DEVICE_TABLE(i2c, wm8750_i2c_id);
Richard Purdieabadfc92006-10-06 18:36:39 +0200810
Richard Purdieabadfc92006-10-06 18:36:39 +0200811static struct i2c_driver wm8750_i2c_driver = {
812 .driver = {
813 .name = "WM8750 I2C Codec",
814 .owner = THIS_MODULE,
815 },
Jean Delvareee1d0092008-09-01 18:47:00 +0100816 .probe = wm8750_i2c_probe,
817 .remove = wm8750_i2c_remove,
818 .id_table = wm8750_i2c_id,
Richard Purdieabadfc92006-10-06 18:36:39 +0200819};
820
Jean Delvareee1d0092008-09-01 18:47:00 +0100821static int wm8750_add_i2c_device(struct platform_device *pdev,
822 const struct wm8750_setup_data *setup)
823{
824 struct i2c_board_info info;
825 struct i2c_adapter *adapter;
826 struct i2c_client *client;
827 int ret;
828
829 ret = i2c_add_driver(&wm8750_i2c_driver);
830 if (ret != 0) {
831 dev_err(&pdev->dev, "can't add i2c driver\n");
832 return ret;
833 }
834
835 memset(&info, 0, sizeof(struct i2c_board_info));
836 info.addr = setup->i2c_address;
837 strlcpy(info.type, "wm8750", I2C_NAME_SIZE);
838
839 adapter = i2c_get_adapter(setup->i2c_bus);
840 if (!adapter) {
841 dev_err(&pdev->dev, "can't get i2c adapter %d\n",
842 setup->i2c_bus);
843 goto err_driver;
844 }
845
846 client = i2c_new_device(adapter, &info);
847 i2c_put_adapter(adapter);
848 if (!client) {
849 dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
850 (unsigned int)info.addr);
851 goto err_driver;
852 }
853
854 return 0;
855
856err_driver:
857 i2c_del_driver(&wm8750_i2c_driver);
858 return -ENODEV;
859}
Richard Purdieabadfc92006-10-06 18:36:39 +0200860#endif
861
Mark Brown2f3dfaf2008-09-16 12:51:26 +0100862#if defined(CONFIG_SPI_MASTER)
863static int __devinit wm8750_spi_probe(struct spi_device *spi)
864{
865 struct snd_soc_device *socdev = wm8750_socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000866 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brown2f3dfaf2008-09-16 12:51:26 +0100867 int ret;
868
869 codec->control_data = spi;
870
Mark Brown7084a422009-07-10 22:24:27 +0100871 ret = wm8750_init(socdev, SND_SOC_SPI);
Mark Brown2f3dfaf2008-09-16 12:51:26 +0100872 if (ret < 0)
873 dev_err(&spi->dev, "failed to initialise WM8750\n");
874
875 return ret;
876}
877
878static int __devexit wm8750_spi_remove(struct spi_device *spi)
879{
880 return 0;
881}
882
883static struct spi_driver wm8750_spi_driver = {
884 .driver = {
885 .name = "wm8750",
886 .bus = &spi_bus_type,
887 .owner = THIS_MODULE,
888 },
889 .probe = wm8750_spi_probe,
890 .remove = __devexit_p(wm8750_spi_remove),
891};
Mark Brown2f3dfaf2008-09-16 12:51:26 +0100892#endif
893
Richard Purdieabadfc92006-10-06 18:36:39 +0200894static int wm8750_probe(struct platform_device *pdev)
895{
896 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
897 struct wm8750_setup_data *setup = socdev->codec_data;
898 struct snd_soc_codec *codec;
Liam Girdwood4422b602007-02-02 17:15:33 +0100899 struct wm8750_priv *wm8750;
Mark Brownb7c9d852008-09-01 18:47:04 +0100900 int ret;
Richard Purdieabadfc92006-10-06 18:36:39 +0200901
Richard Purdieabadfc92006-10-06 18:36:39 +0200902 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
903 if (codec == NULL)
904 return -ENOMEM;
905
Liam Girdwood4422b602007-02-02 17:15:33 +0100906 wm8750 = kzalloc(sizeof(struct wm8750_priv), GFP_KERNEL);
907 if (wm8750 == NULL) {
908 kfree(codec);
909 return -ENOMEM;
910 }
911
912 codec->private_data = wm8750;
Mark Brown6627a652009-01-23 22:55:23 +0000913 socdev->card->codec = codec;
Richard Purdieabadfc92006-10-06 18:36:39 +0200914 mutex_init(&codec->mutex);
915 INIT_LIST_HEAD(&codec->dapm_widgets);
916 INIT_LIST_HEAD(&codec->dapm_paths);
917 wm8750_socdev = socdev;
Mark Brown42f30302008-04-23 15:17:12 +0200918
Mark Brownb7c9d852008-09-01 18:47:04 +0100919 ret = -ENODEV;
920
Mark Brown42f30302008-04-23 15:17:12 +0200921#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Richard Purdieabadfc92006-10-06 18:36:39 +0200922 if (setup->i2c_address) {
Jean Delvareee1d0092008-09-01 18:47:00 +0100923 ret = wm8750_add_i2c_device(pdev, setup);
Richard Purdieabadfc92006-10-06 18:36:39 +0200924 }
Richard Purdieabadfc92006-10-06 18:36:39 +0200925#endif
Mark Brown2f3dfaf2008-09-16 12:51:26 +0100926#if defined(CONFIG_SPI_MASTER)
927 if (setup->spi) {
Mark Brown2f3dfaf2008-09-16 12:51:26 +0100928 ret = spi_register_driver(&wm8750_spi_driver);
929 if (ret != 0)
930 printk(KERN_ERR "can't add spi driver");
931 }
932#endif
Richard Purdieabadfc92006-10-06 18:36:39 +0200933
Jean Delvare3051e412008-08-25 11:49:20 +0100934 if (ret != 0) {
935 kfree(codec->private_data);
936 kfree(codec);
937 }
Richard Purdieabadfc92006-10-06 18:36:39 +0200938 return ret;
939}
940
941/* power down chip */
942static int wm8750_remove(struct platform_device *pdev)
943{
944 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +0000945 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdieabadfc92006-10-06 18:36:39 +0200946
947 if (codec->control_data)
Mark Brown0be98982008-05-19 12:31:28 +0200948 wm8750_set_bias_level(codec, SND_SOC_BIAS_OFF);
Richard Purdieabadfc92006-10-06 18:36:39 +0200949 snd_soc_free_pcms(socdev);
950 snd_soc_dapm_free(socdev);
Mark Brown42f30302008-04-23 15:17:12 +0200951#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Jean Delvareee1d0092008-09-01 18:47:00 +0100952 i2c_unregister_device(codec->control_data);
Richard Purdieabadfc92006-10-06 18:36:39 +0200953 i2c_del_driver(&wm8750_i2c_driver);
954#endif
Mark Brown2f3dfaf2008-09-16 12:51:26 +0100955#if defined(CONFIG_SPI_MASTER)
956 spi_unregister_driver(&wm8750_spi_driver);
957#endif
Liam Girdwood4422b602007-02-02 17:15:33 +0100958 kfree(codec->private_data);
Richard Purdieabadfc92006-10-06 18:36:39 +0200959 kfree(codec);
960
961 return 0;
962}
963
964struct snd_soc_codec_device soc_codec_dev_wm8750 = {
965 .probe = wm8750_probe,
966 .remove = wm8750_remove,
967 .suspend = wm8750_suspend,
968 .resume = wm8750_resume,
969};
Richard Purdieabadfc92006-10-06 18:36:39 +0200970EXPORT_SYMBOL_GPL(soc_codec_dev_wm8750);
971
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100972static int __init wm8750_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +0000973{
974 return snd_soc_register_dai(&wm8750_dai);
975}
976module_init(wm8750_modinit);
977
978static void __exit wm8750_exit(void)
979{
980 snd_soc_unregister_dai(&wm8750_dai);
981}
982module_exit(wm8750_exit);
983
Richard Purdieabadfc92006-10-06 18:36:39 +0200984MODULE_DESCRIPTION("ASoC WM8750 driver");
985MODULE_AUTHOR("Liam Girdwood");
986MODULE_LICENSE("GPL");