1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34#include <linux/module.h>
35#include <linux/moduleparam.h>
36#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/of_device.h>
42#include <linux/regmap.h>
43#include <linux/spi/spi.h>
44#include <linux/slab.h>
45#include <sound/core.h>
46#include <sound/pcm.h>
47#include <sound/pcm_params.h>
48#include <sound/soc.h>
49#include <sound/initval.h>
50#include <sound/tlv.h>
51#include <asm/div64.h>
52
53#include "wm8753.h"
54
55static int caps_charge = 2000;
56module_param(caps_charge, int, 0);
57MODULE_PARM_DESC(caps_charge, "WM8753 cap charge time (msecs)");
58
59static int wm8753_hifi_write_dai_fmt(struct snd_soc_codec *codec,
60 unsigned int fmt);
61static int wm8753_voice_write_dai_fmt(struct snd_soc_codec *codec,
62 unsigned int fmt);
63
64
65
66
67
68
69static const struct reg_default wm8753_reg_defaults[] = {
70 { 0x00, 0x0000 },
71 { 0x01, 0x0008 },
72 { 0x02, 0x0000 },
73 { 0x03, 0x000a },
74 { 0x04, 0x000a },
75 { 0x05, 0x0033 },
76 { 0x06, 0x0000 },
77 { 0x07, 0x0007 },
78 { 0x08, 0x00ff },
79 { 0x09, 0x00ff },
80 { 0x0a, 0x000f },
81 { 0x0b, 0x000f },
82 { 0x0c, 0x007b },
83 { 0x0d, 0x0000 },
84 { 0x0e, 0x0032 },
85 { 0x0f, 0x0000 },
86 { 0x10, 0x00c3 },
87 { 0x11, 0x00c3 },
88 { 0x12, 0x00c0 },
89 { 0x13, 0x0000 },
90 { 0x14, 0x0000 },
91 { 0x15, 0x0000 },
92 { 0x16, 0x0000 },
93 { 0x17, 0x0000 },
94 { 0x18, 0x0000 },
95 { 0x19, 0x0000 },
96 { 0x1a, 0x0000 },
97 { 0x1b, 0x0000 },
98 { 0x1c, 0x0000 },
99 { 0x1d, 0x0000 },
100 { 0x1e, 0x0000 },
101 { 0x1f, 0x0000 },
102 { 0x20, 0x0055 },
103 { 0x21, 0x0005 },
104 { 0x22, 0x0050 },
105 { 0x23, 0x0055 },
106 { 0x24, 0x0050 },
107 { 0x25, 0x0055 },
108 { 0x26, 0x0050 },
109 { 0x27, 0x0055 },
110 { 0x28, 0x0079 },
111 { 0x29, 0x0079 },
112 { 0x2a, 0x0079 },
113 { 0x2b, 0x0079 },
114 { 0x2c, 0x0079 },
115 { 0x2d, 0x0000 },
116 { 0x2e, 0x0000 },
117 { 0x2f, 0x0000 },
118 { 0x30, 0x0000 },
119 { 0x31, 0x0097 },
120 { 0x32, 0x0097 },
121 { 0x33, 0x0000 },
122 { 0x34, 0x0004 },
123 { 0x35, 0x0000 },
124 { 0x36, 0x0083 },
125 { 0x37, 0x0024 },
126 { 0x38, 0x01ba },
127 { 0x39, 0x0000 },
128 { 0x3a, 0x0083 },
129 { 0x3b, 0x0024 },
130 { 0x3c, 0x01ba },
131 { 0x3d, 0x0000 },
132 { 0x3e, 0x0000 },
133 { 0x3f, 0x0000 },
134};
135
136static bool wm8753_volatile(struct device *dev, unsigned int reg)
137{
138 return reg == WM8753_RESET;
139}
140
141static bool wm8753_writeable(struct device *dev, unsigned int reg)
142{
143 return reg <= WM8753_ADCTL2;
144}
145
146
147struct wm8753_priv {
148 struct regmap *regmap;
149 unsigned int sysclk;
150 unsigned int pcmclk;
151
152 unsigned int voice_fmt;
153 unsigned int hifi_fmt;
154
155 int dai_func;
156};
157
158#define wm8753_reset(c) snd_soc_write(c, WM8753_RESET, 0)
159
160
161
162
163static const char *wm8753_base[] = {"Linear Control", "Adaptive Boost"};
164static const char *wm8753_base_filter[] =
165 {"130Hz @ 48kHz", "200Hz @ 48kHz", "100Hz @ 16kHz", "400Hz @ 48kHz",
166 "100Hz @ 8kHz", "200Hz @ 8kHz"};
167static const char *wm8753_treble[] = {"8kHz", "4kHz"};
168static const char *wm8753_alc_func[] = {"Off", "Right", "Left", "Stereo"};
169static const char *wm8753_ng_type[] = {"Constant PGA Gain", "Mute ADC Output"};
170static const char *wm8753_3d_func[] = {"Capture", "Playback"};
171static const char *wm8753_3d_uc[] = {"2.2kHz", "1.5kHz"};
172static const char *wm8753_3d_lc[] = {"200Hz", "500Hz"};
173static const char *wm8753_deemp[] = {"None", "32kHz", "44.1kHz", "48kHz"};
174static const char *wm8753_mono_mix[] = {"Stereo", "Left", "Right", "Mono"};
175static const char *wm8753_dac_phase[] = {"Non Inverted", "Inverted"};
176static const char *wm8753_line_mix[] = {"Line 1 + 2", "Line 1 - 2",
177 "Line 1", "Line 2"};
178static const char *wm8753_mono_mux[] = {"Line Mix", "Rx Mix"};
179static const char *wm8753_right_mux[] = {"Line 2", "Rx Mix"};
180static const char *wm8753_left_mux[] = {"Line 1", "Rx Mix"};
181static const char *wm8753_rxmsel[] = {"RXP - RXN", "RXP + RXN", "RXP", "RXN"};
182static const char *wm8753_sidetone_mux[] = {"Left PGA", "Mic 1", "Mic 2",
183 "Right PGA"};
184static const char *wm8753_mono2_src[] = {"Inverted Mono 1", "Left", "Right",
185 "Left + Right"};
186static const char *wm8753_out3[] = {"VREF", "ROUT2", "Left + Right"};
187static const char *wm8753_out4[] = {"VREF", "Capture ST", "LOUT2"};
188static const char *wm8753_radcsel[] = {"PGA", "Line or RXP-RXN", "Sidetone"};
189static const char *wm8753_ladcsel[] = {"PGA", "Line or RXP-RXN", "Line"};
190static const char *wm8753_mono_adc[] = {"Stereo", "Analogue Mix Left",
191 "Analogue Mix Right", "Digital Mono Mix"};
192static const char *wm8753_adc_hp[] = {"3.4Hz @ 48kHz", "82Hz @ 16k",
193 "82Hz @ 8kHz", "170Hz @ 8kHz"};
194static const char *wm8753_adc_filter[] = {"HiFi", "Voice"};
195static const char *wm8753_mic_sel[] = {"Mic 1", "Mic 2", "Mic 3"};
196static const char *wm8753_dai_mode[] = {"DAI 0", "DAI 1", "DAI 2", "DAI 3"};
197static const char *wm8753_dat_sel[] = {"Stereo", "Left ADC", "Right ADC",
198 "Channel Swap"};
199static const char *wm8753_rout2_phase[] = {"Non Inverted", "Inverted"};
200
201static const struct soc_enum wm8753_enum[] = {
202SOC_ENUM_SINGLE(WM8753_BASS, 7, 2, wm8753_base),
203SOC_ENUM_SINGLE(WM8753_BASS, 4, 6, wm8753_base_filter),
204SOC_ENUM_SINGLE(WM8753_TREBLE, 6, 2, wm8753_treble),
205SOC_ENUM_SINGLE(WM8753_ALC1, 7, 4, wm8753_alc_func),
206SOC_ENUM_SINGLE(WM8753_NGATE, 1, 2, wm8753_ng_type),
207SOC_ENUM_SINGLE(WM8753_3D, 7, 2, wm8753_3d_func),
208SOC_ENUM_SINGLE(WM8753_3D, 6, 2, wm8753_3d_uc),
209SOC_ENUM_SINGLE(WM8753_3D, 5, 2, wm8753_3d_lc),
210SOC_ENUM_SINGLE(WM8753_DAC, 1, 4, wm8753_deemp),
211SOC_ENUM_SINGLE(WM8753_DAC, 4, 4, wm8753_mono_mix),
212SOC_ENUM_SINGLE(WM8753_DAC, 6, 2, wm8753_dac_phase),
213SOC_ENUM_SINGLE(WM8753_INCTL1, 3, 4, wm8753_line_mix),
214SOC_ENUM_SINGLE(WM8753_INCTL1, 2, 2, wm8753_mono_mux),
215SOC_ENUM_SINGLE(WM8753_INCTL1, 1, 2, wm8753_right_mux),
216SOC_ENUM_SINGLE(WM8753_INCTL1, 0, 2, wm8753_left_mux),
217SOC_ENUM_SINGLE(WM8753_INCTL2, 6, 4, wm8753_rxmsel),
218SOC_ENUM_SINGLE(WM8753_INCTL2, 4, 4, wm8753_sidetone_mux),
219SOC_ENUM_SINGLE(WM8753_OUTCTL, 7, 4, wm8753_mono2_src),
220SOC_ENUM_SINGLE(WM8753_OUTCTL, 0, 3, wm8753_out3),
221SOC_ENUM_SINGLE(WM8753_ADCTL2, 7, 3, wm8753_out4),
222SOC_ENUM_SINGLE(WM8753_ADCIN, 2, 3, wm8753_radcsel),
223SOC_ENUM_SINGLE(WM8753_ADCIN, 0, 3, wm8753_ladcsel),
224SOC_ENUM_SINGLE(WM8753_ADCIN, 4, 4, wm8753_mono_adc),
225SOC_ENUM_SINGLE(WM8753_ADC, 2, 4, wm8753_adc_hp),
226SOC_ENUM_SINGLE(WM8753_ADC, 4, 2, wm8753_adc_filter),
227SOC_ENUM_SINGLE(WM8753_MICBIAS, 6, 3, wm8753_mic_sel),
228SOC_ENUM_SINGLE(WM8753_IOCTL, 2, 4, wm8753_dai_mode),
229SOC_ENUM_SINGLE(WM8753_ADC, 7, 4, wm8753_dat_sel),
230SOC_ENUM_SINGLE(WM8753_OUTCTL, 2, 2, wm8753_rout2_phase),
231};
232
233
234static int wm8753_get_dai(struct snd_kcontrol *kcontrol,
235 struct snd_ctl_elem_value *ucontrol)
236{
237 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
238 struct wm8753_priv *wm8753 = snd_soc_codec_get_drvdata(codec);
239
240 ucontrol->value.integer.value[0] = wm8753->dai_func;
241 return 0;
242}
243
244static int wm8753_set_dai(struct snd_kcontrol *kcontrol,
245 struct snd_ctl_elem_value *ucontrol)
246{
247 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
248 struct wm8753_priv *wm8753 = snd_soc_codec_get_drvdata(codec);
249 u16 ioctl;
250
251 if (wm8753->dai_func == ucontrol->value.integer.value[0])
252 return 0;
253
254 if (codec->active)
255 return -EBUSY;
256
257 ioctl = snd_soc_read(codec, WM8753_IOCTL);
258
259 wm8753->dai_func = ucontrol->value.integer.value[0];
260
261 if (((ioctl >> 2) & 0x3) == wm8753->dai_func)
262 return 1;
263
264 ioctl = (ioctl & 0x1f3) | (wm8753->dai_func << 2);
265 snd_soc_write(codec, WM8753_IOCTL, ioctl);
266
267
268 wm8753_hifi_write_dai_fmt(codec, wm8753->hifi_fmt);
269 wm8753_voice_write_dai_fmt(codec, wm8753->voice_fmt);
270
271 return 1;
272}
273
274static const DECLARE_TLV_DB_SCALE(rec_mix_tlv, -1500, 300, 0);
275static const DECLARE_TLV_DB_SCALE(mic_preamp_tlv, 1200, 600, 0);
276static const DECLARE_TLV_DB_SCALE(adc_tlv, -9750, 50, 1);
277static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
278static const unsigned int out_tlv[] = {
279 TLV_DB_RANGE_HEAD(2),
280
281 0, 48, TLV_DB_SCALE_ITEM(-25500, 0, 0),
282 48, 127, TLV_DB_SCALE_ITEM(-7300, 100, 0),
283};
284static const DECLARE_TLV_DB_SCALE(mix_tlv, -1500, 300, 0);
285static const DECLARE_TLV_DB_SCALE(voice_mix_tlv, -1200, 300, 0);
286static const DECLARE_TLV_DB_SCALE(pga_tlv, -1725, 75, 0);
287
288static const struct snd_kcontrol_new wm8753_snd_controls[] = {
289SOC_DOUBLE_R_TLV("PCM Volume", WM8753_LDAC, WM8753_RDAC, 0, 255, 0, dac_tlv),
290
291SOC_DOUBLE_R_TLV("ADC Capture Volume", WM8753_LADC, WM8753_RADC, 0, 255, 0,
292 adc_tlv),
293
294SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8753_LOUT1V, WM8753_ROUT1V,
295 0, 127, 0, out_tlv),
296SOC_DOUBLE_R_TLV("Speaker Playback Volume", WM8753_LOUT2V, WM8753_ROUT2V, 0,
297 127, 0, out_tlv),
298
299SOC_SINGLE_TLV("Mono Playback Volume", WM8753_MOUTV, 0, 127, 0, out_tlv),
300
301SOC_DOUBLE_R_TLV("Bypass Playback Volume", WM8753_LOUTM1, WM8753_ROUTM1, 4, 7,
302 1, mix_tlv),
303SOC_DOUBLE_R_TLV("Sidetone Playback Volume", WM8753_LOUTM2, WM8753_ROUTM2, 4,
304 7, 1, mix_tlv),
305SOC_DOUBLE_R_TLV("Voice Playback Volume", WM8753_LOUTM2, WM8753_ROUTM2, 0, 7,
306 1, voice_mix_tlv),
307
308SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8753_LOUT1V, WM8753_ROUT1V, 7,
309 1, 0),
310SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8753_LOUT2V, WM8753_ROUT2V, 7,
311 1, 0),
312
313SOC_SINGLE_TLV("Mono Bypass Playback Volume", WM8753_MOUTM1, 4, 7, 1, mix_tlv),
314SOC_SINGLE_TLV("Mono Sidetone Playback Volume", WM8753_MOUTM2, 4, 7, 1,
315 mix_tlv),
316SOC_SINGLE_TLV("Mono Voice Playback Volume", WM8753_MOUTM2, 0, 7, 1,
317 voice_mix_tlv),
318SOC_SINGLE("Mono Playback ZC Switch", WM8753_MOUTV, 7, 1, 0),
319
320SOC_ENUM("Bass Boost", wm8753_enum[0]),
321SOC_ENUM("Bass Filter", wm8753_enum[1]),
322SOC_SINGLE("Bass Volume", WM8753_BASS, 0, 15, 1),
323
324SOC_SINGLE("Treble Volume", WM8753_TREBLE, 0, 15, 1),
325SOC_ENUM("Treble Cut-off", wm8753_enum[2]),
326
327SOC_DOUBLE_TLV("Sidetone Capture Volume", WM8753_RECMIX1, 0, 4, 7, 1,
328 rec_mix_tlv),
329SOC_SINGLE_TLV("Voice Sidetone Capture Volume", WM8753_RECMIX2, 0, 7, 1,
330 rec_mix_tlv),
331
332SOC_DOUBLE_R_TLV("Capture Volume", WM8753_LINVOL, WM8753_RINVOL, 0, 63, 0,
333 pga_tlv),
334SOC_DOUBLE_R("Capture ZC Switch", WM8753_LINVOL, WM8753_RINVOL, 6, 1, 0),
335SOC_DOUBLE_R("Capture Switch", WM8753_LINVOL, WM8753_RINVOL, 7, 1, 1),
336
337SOC_ENUM("Capture Filter Select", wm8753_enum[23]),
338SOC_ENUM("Capture Filter Cut-off", wm8753_enum[24]),
339SOC_SINGLE("Capture Filter Switch", WM8753_ADC, 0, 1, 1),
340
341SOC_SINGLE("ALC Capture Target Volume", WM8753_ALC1, 0, 7, 0),
342SOC_SINGLE("ALC Capture Max Volume", WM8753_ALC1, 4, 7, 0),
343SOC_ENUM("ALC Capture Function", wm8753_enum[3]),
344SOC_SINGLE("ALC Capture ZC Switch", WM8753_ALC2, 8, 1, 0),
345SOC_SINGLE("ALC Capture Hold Time", WM8753_ALC2, 0, 15, 1),
346SOC_SINGLE("ALC Capture Decay Time", WM8753_ALC3, 4, 15, 1),
347SOC_SINGLE("ALC Capture Attack Time", WM8753_ALC3, 0, 15, 0),
348SOC_SINGLE("ALC Capture NG Threshold", WM8753_NGATE, 3, 31, 0),
349SOC_ENUM("ALC Capture NG Type", wm8753_enum[4]),
350SOC_SINGLE("ALC Capture NG Switch", WM8753_NGATE, 0, 1, 0),
351
352SOC_ENUM("3D Function", wm8753_enum[5]),
353SOC_ENUM("3D Upper Cut-off", wm8753_enum[6]),
354SOC_ENUM("3D Lower Cut-off", wm8753_enum[7]),
355SOC_SINGLE("3D Volume", WM8753_3D, 1, 15, 0),
356SOC_SINGLE("3D Switch", WM8753_3D, 0, 1, 0),
357
358SOC_SINGLE("Capture 6dB Attenuate", WM8753_ADCTL1, 2, 1, 0),
359SOC_SINGLE("Playback 6dB Attenuate", WM8753_ADCTL1, 1, 1, 0),
360
361SOC_ENUM("De-emphasis", wm8753_enum[8]),
362SOC_ENUM("Playback Mono Mix", wm8753_enum[9]),
363SOC_ENUM("Playback Phase", wm8753_enum[10]),
364
365SOC_SINGLE_TLV("Mic2 Capture Volume", WM8753_INCTL1, 7, 3, 0, mic_preamp_tlv),
366SOC_SINGLE_TLV("Mic1 Capture Volume", WM8753_INCTL1, 5, 3, 0, mic_preamp_tlv),
367
368SOC_ENUM_EXT("DAI Mode", wm8753_enum[26], wm8753_get_dai, wm8753_set_dai),
369
370SOC_ENUM("ADC Data Select", wm8753_enum[27]),
371SOC_ENUM("ROUT2 Phase", wm8753_enum[28]),
372};
373
374
375
376
377
378
379static const struct snd_kcontrol_new wm8753_left_mixer_controls[] = {
380SOC_DAPM_SINGLE("Voice Playback Switch", WM8753_LOUTM2, 8, 1, 0),
381SOC_DAPM_SINGLE("Sidetone Playback Switch", WM8753_LOUTM2, 7, 1, 0),
382SOC_DAPM_SINGLE("Left Playback Switch", WM8753_LOUTM1, 8, 1, 0),
383SOC_DAPM_SINGLE("Bypass Playback Switch", WM8753_LOUTM1, 7, 1, 0),
384};
385
386
387static const struct snd_kcontrol_new wm8753_right_mixer_controls[] = {
388SOC_DAPM_SINGLE("Voice Playback Switch", WM8753_ROUTM2, 8, 1, 0),
389SOC_DAPM_SINGLE("Sidetone Playback Switch", WM8753_ROUTM2, 7, 1, 0),
390SOC_DAPM_SINGLE("Right Playback Switch", WM8753_ROUTM1, 8, 1, 0),
391SOC_DAPM_SINGLE("Bypass Playback Switch", WM8753_ROUTM1, 7, 1, 0),
392};
393
394
395static const struct snd_kcontrol_new wm8753_mono_mixer_controls[] = {
396SOC_DAPM_SINGLE("Left Playback Switch", WM8753_MOUTM1, 8, 1, 0),
397SOC_DAPM_SINGLE("Right Playback Switch", WM8753_MOUTM2, 8, 1, 0),
398SOC_DAPM_SINGLE("Voice Playback Switch", WM8753_MOUTM2, 3, 1, 0),
399SOC_DAPM_SINGLE("Sidetone Playback Switch", WM8753_MOUTM2, 7, 1, 0),
400SOC_DAPM_SINGLE("Bypass Playback Switch", WM8753_MOUTM1, 7, 1, 0),
401};
402
403
404static const struct snd_kcontrol_new wm8753_mono2_controls =
405SOC_DAPM_ENUM("Route", wm8753_enum[17]);
406
407
408static const struct snd_kcontrol_new wm8753_out3_controls =
409SOC_DAPM_ENUM("Route", wm8753_enum[18]);
410
411
412static const struct snd_kcontrol_new wm8753_out4_controls =
413SOC_DAPM_ENUM("Route", wm8753_enum[19]);
414
415
416static const struct snd_kcontrol_new wm8753_adc_mono_controls =
417SOC_DAPM_ENUM("Route", wm8753_enum[22]);
418
419
420static const struct snd_kcontrol_new wm8753_record_mixer_controls[] = {
421SOC_DAPM_SINGLE("Voice Capture Switch", WM8753_RECMIX2, 3, 1, 0),
422SOC_DAPM_SINGLE("Left Capture Switch", WM8753_RECMIX1, 3, 1, 0),
423SOC_DAPM_SINGLE("Right Capture Switch", WM8753_RECMIX1, 7, 1, 0),
424};
425
426
427static const struct snd_kcontrol_new wm8753_adc_left_controls =
428SOC_DAPM_ENUM("Route", wm8753_enum[21]);
429
430
431static const struct snd_kcontrol_new wm8753_adc_right_controls =
432SOC_DAPM_ENUM("Route", wm8753_enum[20]);
433
434
435static const struct snd_kcontrol_new wm8753_mic_mux_controls =
436SOC_DAPM_ENUM("Route", wm8753_enum[16]);
437
438
439static const struct snd_kcontrol_new wm8753_alc_mixer_controls[] = {
440SOC_DAPM_SINGLE("Line Capture Switch", WM8753_INCTL2, 3, 1, 0),
441SOC_DAPM_SINGLE("Mic2 Capture Switch", WM8753_INCTL2, 2, 1, 0),
442SOC_DAPM_SINGLE("Mic1 Capture Switch", WM8753_INCTL2, 1, 1, 0),
443SOC_DAPM_SINGLE("Rx Capture Switch", WM8753_INCTL2, 0, 1, 0),
444};
445
446
447static const struct snd_kcontrol_new wm8753_line_left_controls =
448SOC_DAPM_ENUM("Route", wm8753_enum[14]);
449
450
451static const struct snd_kcontrol_new wm8753_line_right_controls =
452SOC_DAPM_ENUM("Route", wm8753_enum[13]);
453
454
455static const struct snd_kcontrol_new wm8753_line_mono_controls =
456SOC_DAPM_ENUM("Route", wm8753_enum[12]);
457
458
459static const struct snd_kcontrol_new wm8753_line_mux_mix_controls =
460SOC_DAPM_ENUM("Route", wm8753_enum[11]);
461
462
463static const struct snd_kcontrol_new wm8753_rx_mux_mix_controls =
464SOC_DAPM_ENUM("Route", wm8753_enum[15]);
465
466
467static const struct snd_kcontrol_new wm8753_mic_sel_mux_controls =
468SOC_DAPM_ENUM("Route", wm8753_enum[25]);
469
470static const struct snd_soc_dapm_widget wm8753_dapm_widgets[] = {
471SND_SOC_DAPM_MICBIAS("Mic Bias", WM8753_PWR1, 5, 0),
472SND_SOC_DAPM_MIXER("Left Mixer", WM8753_PWR4, 0, 0,
473 &wm8753_left_mixer_controls[0], ARRAY_SIZE(wm8753_left_mixer_controls)),
474SND_SOC_DAPM_PGA("Left Out 1", WM8753_PWR3, 8, 0, NULL, 0),
475SND_SOC_DAPM_PGA("Left Out 2", WM8753_PWR3, 6, 0, NULL, 0),
476SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback", WM8753_PWR1, 3, 0),
477SND_SOC_DAPM_OUTPUT("LOUT1"),
478SND_SOC_DAPM_OUTPUT("LOUT2"),
479SND_SOC_DAPM_MIXER("Right Mixer", WM8753_PWR4, 1, 0,
480 &wm8753_right_mixer_controls[0], ARRAY_SIZE(wm8753_right_mixer_controls)),
481SND_SOC_DAPM_PGA("Right Out 1", WM8753_PWR3, 7, 0, NULL, 0),
482SND_SOC_DAPM_PGA("Right Out 2", WM8753_PWR3, 5, 0, NULL, 0),
483SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback", WM8753_PWR1, 2, 0),
484SND_SOC_DAPM_OUTPUT("ROUT1"),
485SND_SOC_DAPM_OUTPUT("ROUT2"),
486SND_SOC_DAPM_MIXER("Mono Mixer", WM8753_PWR4, 2, 0,
487 &wm8753_mono_mixer_controls[0], ARRAY_SIZE(wm8753_mono_mixer_controls)),
488SND_SOC_DAPM_PGA("Mono Out 1", WM8753_PWR3, 2, 0, NULL, 0),
489SND_SOC_DAPM_PGA("Mono Out 2", WM8753_PWR3, 1, 0, NULL, 0),
490SND_SOC_DAPM_DAC("Voice DAC", "Voice Playback", WM8753_PWR1, 4, 0),
491SND_SOC_DAPM_OUTPUT("MONO1"),
492SND_SOC_DAPM_MUX("Mono 2 Mux", SND_SOC_NOPM, 0, 0, &wm8753_mono2_controls),
493SND_SOC_DAPM_OUTPUT("MONO2"),
494SND_SOC_DAPM_MIXER("Out3 Left + Right", -1, 0, 0, NULL, 0),
495SND_SOC_DAPM_MUX("Out3 Mux", SND_SOC_NOPM, 0, 0, &wm8753_out3_controls),
496SND_SOC_DAPM_PGA("Out 3", WM8753_PWR3, 4, 0, NULL, 0),
497SND_SOC_DAPM_OUTPUT("OUT3"),
498SND_SOC_DAPM_MUX("Out4 Mux", SND_SOC_NOPM, 0, 0, &wm8753_out4_controls),
499SND_SOC_DAPM_PGA("Out 4", WM8753_PWR3, 3, 0, NULL, 0),
500SND_SOC_DAPM_OUTPUT("OUT4"),
501SND_SOC_DAPM_MIXER("Playback Mixer", WM8753_PWR4, 3, 0,
502 &wm8753_record_mixer_controls[0],
503 ARRAY_SIZE(wm8753_record_mixer_controls)),
504SND_SOC_DAPM_ADC("Left ADC", "Left Capture", WM8753_PWR2, 3, 0),
505SND_SOC_DAPM_ADC("Right ADC", "Right Capture", WM8753_PWR2, 2, 0),
506SND_SOC_DAPM_MUX("Capture Left Mixer", SND_SOC_NOPM, 0, 0,
507 &wm8753_adc_mono_controls),
508SND_SOC_DAPM_MUX("Capture Right Mixer", SND_SOC_NOPM, 0, 0,
509 &wm8753_adc_mono_controls),
510SND_SOC_DAPM_MUX("Capture Left Mux", SND_SOC_NOPM, 0, 0,
511 &wm8753_adc_left_controls),
512SND_SOC_DAPM_MUX("Capture Right Mux", SND_SOC_NOPM, 0, 0,
513 &wm8753_adc_right_controls),
514SND_SOC_DAPM_MUX("Mic Sidetone Mux", SND_SOC_NOPM, 0, 0,
515 &wm8753_mic_mux_controls),
516SND_SOC_DAPM_PGA("Left Capture Volume", WM8753_PWR2, 5, 0, NULL, 0),
517SND_SOC_DAPM_PGA("Right Capture Volume", WM8753_PWR2, 4, 0, NULL, 0),
518SND_SOC_DAPM_MIXER("ALC Mixer", WM8753_PWR2, 6, 0,
519 &wm8753_alc_mixer_controls[0], ARRAY_SIZE(wm8753_alc_mixer_controls)),
520SND_SOC_DAPM_MUX("Line Left Mux", SND_SOC_NOPM, 0, 0,
521 &wm8753_line_left_controls),
522SND_SOC_DAPM_MUX("Line Right Mux", SND_SOC_NOPM, 0, 0,
523 &wm8753_line_right_controls),
524SND_SOC_DAPM_MUX("Line Mono Mux", SND_SOC_NOPM, 0, 0,
525 &wm8753_line_mono_controls),
526SND_SOC_DAPM_MUX("Line Mixer", WM8753_PWR2, 0, 0,
527 &wm8753_line_mux_mix_controls),
528SND_SOC_DAPM_MUX("Rx Mixer", WM8753_PWR2, 1, 0,
529 &wm8753_rx_mux_mix_controls),
530SND_SOC_DAPM_PGA("Mic 1 Volume", WM8753_PWR2, 8, 0, NULL, 0),
531SND_SOC_DAPM_PGA("Mic 2 Volume", WM8753_PWR2, 7, 0, NULL, 0),
532SND_SOC_DAPM_MUX("Mic Selection Mux", SND_SOC_NOPM, 0, 0,
533 &wm8753_mic_sel_mux_controls),
534SND_SOC_DAPM_INPUT("LINE1"),
535SND_SOC_DAPM_INPUT("LINE2"),
536SND_SOC_DAPM_INPUT("RXP"),
537SND_SOC_DAPM_INPUT("RXN"),
538SND_SOC_DAPM_INPUT("ACIN"),
539SND_SOC_DAPM_OUTPUT("ACOP"),
540SND_SOC_DAPM_INPUT("MIC1N"),
541SND_SOC_DAPM_INPUT("MIC1"),
542SND_SOC_DAPM_INPUT("MIC2N"),
543SND_SOC_DAPM_INPUT("MIC2"),
544SND_SOC_DAPM_VMID("VREF"),
545};
546
547static const struct snd_soc_dapm_route wm8753_dapm_routes[] = {
548
549 {"Left Mixer", "Left Playback Switch", "Left DAC"},
550 {"Left Mixer", "Voice Playback Switch", "Voice DAC"},
551 {"Left Mixer", "Sidetone Playback Switch", "Mic Sidetone Mux"},
552 {"Left Mixer", "Bypass Playback Switch", "Line Left Mux"},
553
554
555 {"Right Mixer", "Right Playback Switch", "Right DAC"},
556 {"Right Mixer", "Voice Playback Switch", "Voice DAC"},
557 {"Right Mixer", "Sidetone Playback Switch", "Mic Sidetone Mux"},
558 {"Right Mixer", "Bypass Playback Switch", "Line Right Mux"},
559
560
561 {"Mono Mixer", "Voice Playback Switch", "Voice DAC"},
562 {"Mono Mixer", "Left Playback Switch", "Left DAC"},
563 {"Mono Mixer", "Right Playback Switch", "Right DAC"},
564 {"Mono Mixer", "Sidetone Playback Switch", "Mic Sidetone Mux"},
565 {"Mono Mixer", "Bypass Playback Switch", "Line Mono Mux"},
566
567
568 {"Left Out 1", NULL, "Left Mixer"},
569 {"Left Out 2", NULL, "Left Mixer"},
570 {"LOUT1", NULL, "Left Out 1"},
571 {"LOUT2", NULL, "Left Out 2"},
572
573
574 {"Right Out 1", NULL, "Right Mixer"},
575 {"Right Out 2", NULL, "Right Mixer"},
576 {"ROUT1", NULL, "Right Out 1"},
577 {"ROUT2", NULL, "Right Out 2"},
578
579
580 {"Mono Out 1", NULL, "Mono Mixer"},
581 {"MONO1", NULL, "Mono Out 1"},
582
583
584 {"Mono 2 Mux", "Left + Right", "Out3 Left + Right"},
585 {"Mono 2 Mux", "Inverted Mono 1", "MONO1"},
586 {"Mono 2 Mux", "Left", "Left Mixer"},
587 {"Mono 2 Mux", "Right", "Right Mixer"},
588 {"Mono Out 2", NULL, "Mono 2 Mux"},
589 {"MONO2", NULL, "Mono Out 2"},
590
591
592 {"Out3 Left + Right", NULL, "Left Mixer"},
593 {"Out3 Left + Right", NULL, "Right Mixer"},
594 {"Out3 Mux", "VREF", "VREF"},
595 {"Out3 Mux", "Left + Right", "Out3 Left + Right"},
596 {"Out3 Mux", "ROUT2", "ROUT2"},
597 {"Out 3", NULL, "Out3 Mux"},
598 {"OUT3", NULL, "Out 3"},
599
600
601 {"Out4 Mux", "VREF", "VREF"},
602 {"Out4 Mux", "Capture ST", "Playback Mixer"},
603 {"Out4 Mux", "LOUT2", "LOUT2"},
604 {"Out 4", NULL, "Out4 Mux"},
605 {"OUT4", NULL, "Out 4"},
606
607
608 {"Playback Mixer", "Left Capture Switch", "Left Mixer"},
609 {"Playback Mixer", "Voice Capture Switch", "Mono Mixer"},
610 {"Playback Mixer", "Right Capture Switch", "Right Mixer"},
611
612
613 {"Mic Sidetone Mux", "Left PGA", "Left Capture Volume"},
614 {"Mic Sidetone Mux", "Right PGA", "Right Capture Volume"},
615 {"Mic Sidetone Mux", "Mic 1", "Mic 1 Volume"},
616 {"Mic Sidetone Mux", "Mic 2", "Mic 2 Volume"},
617
618
619 {"Capture Left Mux", "PGA", "Left Capture Volume"},
620 {"Capture Left Mux", "Line or RXP-RXN", "Line Left Mux"},
621 {"Capture Left Mux", "Line", "LINE1"},
622
623
624 {"Capture Right Mux", "PGA", "Right Capture Volume"},
625 {"Capture Right Mux", "Line or RXP-RXN", "Line Right Mux"},
626 {"Capture Right Mux", "Sidetone", "Playback Mixer"},
627
628
629 {"Capture Right Mixer", "Stereo", "Capture Right Mux"},
630 {"Capture Left Mixer", "Stereo", "Capture Left Mux"},
631 {"Capture Left Mixer", "Analogue Mix Left", "Capture Left Mux"},
632 {"Capture Left Mixer", "Analogue Mix Left", "Capture Right Mux"},
633 {"Capture Right Mixer", "Analogue Mix Right", "Capture Left Mux"},
634 {"Capture Right Mixer", "Analogue Mix Right", "Capture Right Mux"},
635 {"Capture Left Mixer", "Digital Mono Mix", "Capture Left Mux"},
636 {"Capture Left Mixer", "Digital Mono Mix", "Capture Right Mux"},
637 {"Capture Right Mixer", "Digital Mono Mix", "Capture Left Mux"},
638 {"Capture Right Mixer", "Digital Mono Mix", "Capture Right Mux"},
639
640
641 {"Left ADC", NULL, "Capture Left Mixer"},
642 {"Right ADC", NULL, "Capture Right Mixer"},
643
644
645 {"Left Capture Volume", NULL, "ACIN"},
646
647
648 {"Right Capture Volume", NULL, "Mic 2 Volume"},
649
650
651 {"ALC Mixer", "Line Capture Switch", "Line Mixer"},
652 {"ALC Mixer", "Mic2 Capture Switch", "Mic 2 Volume"},
653 {"ALC Mixer", "Mic1 Capture Switch", "Mic 1 Volume"},
654 {"ALC Mixer", "Rx Capture Switch", "Rx Mixer"},
655
656
657 {"Line Left Mux", "Line 1", "LINE1"},
658 {"Line Left Mux", "Rx Mix", "Rx Mixer"},
659
660
661 {"Line Right Mux", "Line 2", "LINE2"},
662 {"Line Right Mux", "Rx Mix", "Rx Mixer"},
663
664
665 {"Line Mono Mux", "Line Mix", "Line Mixer"},
666 {"Line Mono Mux", "Rx Mix", "Rx Mixer"},
667
668
669 {"Line Mixer", "Line 1 + 2", "LINE1"},
670 {"Line Mixer", "Line 1 - 2", "LINE1"},
671 {"Line Mixer", "Line 1 + 2", "LINE2"},
672 {"Line Mixer", "Line 1 - 2", "LINE2"},
673 {"Line Mixer", "Line 1", "LINE1"},
674 {"Line Mixer", "Line 2", "LINE2"},
675
676
677 {"Rx Mixer", "RXP - RXN", "RXP"},
678 {"Rx Mixer", "RXP + RXN", "RXP"},
679 {"Rx Mixer", "RXP - RXN", "RXN"},
680 {"Rx Mixer", "RXP + RXN", "RXN"},
681 {"Rx Mixer", "RXP", "RXP"},
682 {"Rx Mixer", "RXN", "RXN"},
683
684
685 {"Mic 1 Volume", NULL, "MIC1N"},
686 {"Mic 1 Volume", NULL, "Mic Selection Mux"},
687
688
689 {"Mic 2 Volume", NULL, "MIC2N"},
690 {"Mic 2 Volume", NULL, "MIC2"},
691
692
693 {"Mic Selection Mux", "Mic 1", "MIC1"},
694 {"Mic Selection Mux", "Mic 2", "MIC2N"},
695 {"Mic Selection Mux", "Mic 3", "MIC2"},
696
697
698 {"ACOP", NULL, "ALC Mixer"},
699};
700
701
702struct _pll_div {
703 u32 div2:1;
704 u32 n:4;
705 u32 k:24;
706};
707
708
709
710#define FIXED_PLL_SIZE ((1 << 22) * 10)
711
712static void pll_factors(struct _pll_div *pll_div, unsigned int target,
713 unsigned int source)
714{
715 u64 Kpart;
716 unsigned int K, Ndiv, Nmod;
717
718 Ndiv = target / source;
719 if (Ndiv < 6) {
720 source >>= 1;
721 pll_div->div2 = 1;
722 Ndiv = target / source;
723 } else
724 pll_div->div2 = 0;
725
726 if ((Ndiv < 6) || (Ndiv > 12))
727 printk(KERN_WARNING
728 "wm8753: unsupported N = %u\n", Ndiv);
729
730 pll_div->n = Ndiv;
731 Nmod = target % source;
732 Kpart = FIXED_PLL_SIZE * (long long)Nmod;
733
734 do_div(Kpart, source);
735
736 K = Kpart & 0xFFFFFFFF;
737
738
739 if ((K % 10) >= 5)
740 K += 5;
741
742
743 K /= 10;
744
745 pll_div->k = K;
746}
747
748static int wm8753_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
749 int source, unsigned int freq_in, unsigned int freq_out)
750{
751 u16 reg, enable;
752 int offset;
753 struct snd_soc_codec *codec = codec_dai->codec;
754
755 if (pll_id < WM8753_PLL1 || pll_id > WM8753_PLL2)
756 return -ENODEV;
757
758 if (pll_id == WM8753_PLL1) {
759 offset = 0;
760 enable = 0x10;
761 reg = snd_soc_read(codec, WM8753_CLOCK) & 0xffef;
762 } else {
763 offset = 4;
764 enable = 0x8;
765 reg = snd_soc_read(codec, WM8753_CLOCK) & 0xfff7;
766 }
767
768 if (!freq_in || !freq_out) {
769
770 snd_soc_write(codec, WM8753_PLL1CTL1 + offset, 0x0026);
771 snd_soc_write(codec, WM8753_CLOCK, reg);
772 return 0;
773 } else {
774 u16 value = 0;
775 struct _pll_div pll_div;
776
777 pll_factors(&pll_div, freq_out * 8, freq_in);
778
779
780
781 value = (pll_div.n << 5) + ((pll_div.k & 0x3c0000) >> 18);
782 snd_soc_write(codec, WM8753_PLL1CTL2 + offset, value);
783
784
785 value = (pll_div.k & 0x03fe00) >> 9;
786 snd_soc_write(codec, WM8753_PLL1CTL3 + offset, value);
787
788
789 value = pll_div.k & 0x0001ff;
790 snd_soc_write(codec, WM8753_PLL1CTL4 + offset, value);
791
792
793 snd_soc_write(codec, WM8753_PLL1CTL1 + offset, 0x0027 |
794 (pll_div.div2 << 3));
795 snd_soc_write(codec, WM8753_CLOCK, reg | enable);
796 }
797 return 0;
798}
799
800struct _coeff_div {
801 u32 mclk;
802 u32 rate;
803 u8 sr:5;
804 u8 usb:1;
805};
806
807
808static const struct _coeff_div coeff_div[] = {
809
810 {12288000, 8000, 0x6, 0x0},
811 {11289600, 8000, 0x16, 0x0},
812 {18432000, 8000, 0x7, 0x0},
813 {16934400, 8000, 0x17, 0x0},
814 {12000000, 8000, 0x6, 0x1},
815
816
817 {11289600, 11025, 0x18, 0x0},
818 {16934400, 11025, 0x19, 0x0},
819 {12000000, 11025, 0x19, 0x1},
820
821
822 {12288000, 16000, 0xa, 0x0},
823 {18432000, 16000, 0xb, 0x0},
824 {12000000, 16000, 0xa, 0x1},
825
826
827 {11289600, 22050, 0x1a, 0x0},
828 {16934400, 22050, 0x1b, 0x0},
829 {12000000, 22050, 0x1b, 0x1},
830
831
832 {12288000, 32000, 0xc, 0x0},
833 {18432000, 32000, 0xd, 0x0},
834 {12000000, 32000, 0xa, 0x1},
835
836
837 {11289600, 44100, 0x10, 0x0},
838 {16934400, 44100, 0x11, 0x0},
839 {12000000, 44100, 0x11, 0x1},
840
841
842 {12288000, 48000, 0x0, 0x0},
843 {18432000, 48000, 0x1, 0x0},
844 {12000000, 48000, 0x0, 0x1},
845
846
847 {11289600, 88200, 0x1e, 0x0},
848 {16934400, 88200, 0x1f, 0x0},
849 {12000000, 88200, 0x1f, 0x1},
850
851
852 {12288000, 96000, 0xe, 0x0},
853 {18432000, 96000, 0xf, 0x0},
854 {12000000, 96000, 0xe, 0x1},
855};
856
857static int get_coeff(int mclk, int rate)
858{
859 int i;
860
861 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
862 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
863 return i;
864 }
865 return -EINVAL;
866}
867
868
869
870
871static int wm8753_set_dai_sysclk(struct snd_soc_dai *codec_dai,
872 int clk_id, unsigned int freq, int dir)
873{
874 struct snd_soc_codec *codec = codec_dai->codec;
875 struct wm8753_priv *wm8753 = snd_soc_codec_get_drvdata(codec);
876
877 switch (freq) {
878 case 11289600:
879 case 12000000:
880 case 12288000:
881 case 16934400:
882 case 18432000:
883 if (clk_id == WM8753_MCLK) {
884 wm8753->sysclk = freq;
885 return 0;
886 } else if (clk_id == WM8753_PCMCLK) {
887 wm8753->pcmclk = freq;
888 return 0;
889 }
890 break;
891 }
892 return -EINVAL;
893}
894
895
896
897
898static int wm8753_vdac_adc_set_dai_fmt(struct snd_soc_codec *codec,
899 unsigned int fmt)
900{
901 u16 voice = snd_soc_read(codec, WM8753_PCM) & 0x01ec;
902
903
904 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
905 case SND_SOC_DAIFMT_I2S:
906 voice |= 0x0002;
907 break;
908 case SND_SOC_DAIFMT_RIGHT_J:
909 break;
910 case SND_SOC_DAIFMT_LEFT_J:
911 voice |= 0x0001;
912 break;
913 case SND_SOC_DAIFMT_DSP_A:
914 voice |= 0x0003;
915 break;
916 case SND_SOC_DAIFMT_DSP_B:
917 voice |= 0x0013;
918 break;
919 default:
920 return -EINVAL;
921 }
922
923 snd_soc_write(codec, WM8753_PCM, voice);
924 return 0;
925}
926
927
928
929
930static int wm8753_pcm_hw_params(struct snd_pcm_substream *substream,
931 struct snd_pcm_hw_params *params,
932 struct snd_soc_dai *dai)
933{
934 struct snd_soc_codec *codec = dai->codec;
935 struct wm8753_priv *wm8753 = snd_soc_codec_get_drvdata(codec);
936 u16 voice = snd_soc_read(codec, WM8753_PCM) & 0x01f3;
937 u16 srate = snd_soc_read(codec, WM8753_SRATE1) & 0x017f;
938
939
940 switch (params_format(params)) {
941 case SNDRV_PCM_FORMAT_S16_LE:
942 break;
943 case SNDRV_PCM_FORMAT_S20_3LE:
944 voice |= 0x0004;
945 break;
946 case SNDRV_PCM_FORMAT_S24_LE:
947 voice |= 0x0008;
948 break;
949 case SNDRV_PCM_FORMAT_S32_LE:
950 voice |= 0x000c;
951 break;
952 }
953
954
955 if (params_rate(params) * 384 == wm8753->pcmclk)
956 srate |= 0x80;
957 snd_soc_write(codec, WM8753_SRATE1, srate);
958
959 snd_soc_write(codec, WM8753_PCM, voice);
960 return 0;
961}
962
963
964
965
966static int wm8753_pcm_set_dai_fmt(struct snd_soc_codec *codec,
967 unsigned int fmt)
968{
969 u16 voice, ioctl;
970
971 voice = snd_soc_read(codec, WM8753_PCM) & 0x011f;
972 ioctl = snd_soc_read(codec, WM8753_IOCTL) & 0x015d;
973
974
975 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
976 case SND_SOC_DAIFMT_CBS_CFS:
977 break;
978 case SND_SOC_DAIFMT_CBM_CFM:
979 ioctl |= 0x2;
980 case SND_SOC_DAIFMT_CBM_CFS:
981 voice |= 0x0040;
982 break;
983 default:
984 return -EINVAL;
985 }
986
987
988 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
989 case SND_SOC_DAIFMT_DSP_A:
990 case SND_SOC_DAIFMT_DSP_B:
991
992 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
993 case SND_SOC_DAIFMT_NB_NF:
994 break;
995 case SND_SOC_DAIFMT_IB_NF:
996 voice |= 0x0080;
997 break;
998 default:
999 return -EINVAL;
1000 }
1001 break;
1002 case SND_SOC_DAIFMT_I2S:
1003 case SND_SOC_DAIFMT_RIGHT_J:
1004 case SND_SOC_DAIFMT_LEFT_J:
1005 voice &= ~0x0010;
1006 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1007 case SND_SOC_DAIFMT_NB_NF:
1008 break;
1009 case SND_SOC_DAIFMT_IB_IF:
1010 voice |= 0x0090;
1011 break;
1012 case SND_SOC_DAIFMT_IB_NF:
1013 voice |= 0x0080;
1014 break;
1015 case SND_SOC_DAIFMT_NB_IF:
1016 voice |= 0x0010;
1017 break;
1018 default:
1019 return -EINVAL;
1020 }
1021 break;
1022 default:
1023 return -EINVAL;
1024 }
1025
1026 snd_soc_write(codec, WM8753_PCM, voice);
1027 snd_soc_write(codec, WM8753_IOCTL, ioctl);
1028 return 0;
1029}
1030
1031static int wm8753_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
1032 int div_id, int div)
1033{
1034 struct snd_soc_codec *codec = codec_dai->codec;
1035 u16 reg;
1036
1037 switch (div_id) {
1038 case WM8753_PCMDIV:
1039 reg = snd_soc_read(codec, WM8753_CLOCK) & 0x003f;
1040 snd_soc_write(codec, WM8753_CLOCK, reg | div);
1041 break;
1042 case WM8753_BCLKDIV:
1043 reg = snd_soc_read(codec, WM8753_SRATE2) & 0x01c7;
1044 snd_soc_write(codec, WM8753_SRATE2, reg | div);
1045 break;
1046 case WM8753_VXCLKDIV:
1047 reg = snd_soc_read(codec, WM8753_SRATE2) & 0x003f;
1048 snd_soc_write(codec, WM8753_SRATE2, reg | div);
1049 break;
1050 default:
1051 return -EINVAL;
1052 }
1053 return 0;
1054}
1055
1056
1057
1058
1059static int wm8753_hdac_set_dai_fmt(struct snd_soc_codec *codec,
1060 unsigned int fmt)
1061{
1062 u16 hifi = snd_soc_read(codec, WM8753_HIFI) & 0x01e0;
1063
1064
1065 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1066 case SND_SOC_DAIFMT_I2S:
1067 hifi |= 0x0002;
1068 break;
1069 case SND_SOC_DAIFMT_RIGHT_J:
1070 break;
1071 case SND_SOC_DAIFMT_LEFT_J:
1072 hifi |= 0x0001;
1073 break;
1074 case SND_SOC_DAIFMT_DSP_A:
1075 hifi |= 0x0003;
1076 break;
1077 case SND_SOC_DAIFMT_DSP_B:
1078 hifi |= 0x0013;
1079 break;
1080 default:
1081 return -EINVAL;
1082 }
1083
1084 snd_soc_write(codec, WM8753_HIFI, hifi);
1085 return 0;
1086}
1087
1088
1089
1090
1091static int wm8753_i2s_set_dai_fmt(struct snd_soc_codec *codec,
1092 unsigned int fmt)
1093{
1094 u16 ioctl, hifi;
1095
1096 hifi = snd_soc_read(codec, WM8753_HIFI) & 0x011f;
1097 ioctl = snd_soc_read(codec, WM8753_IOCTL) & 0x00ae;
1098
1099
1100 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1101 case SND_SOC_DAIFMT_CBS_CFS:
1102 break;
1103 case SND_SOC_DAIFMT_CBM_CFM:
1104 ioctl |= 0x1;
1105 case SND_SOC_DAIFMT_CBM_CFS:
1106 hifi |= 0x0040;
1107 break;
1108 default:
1109 return -EINVAL;
1110 }
1111
1112
1113 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1114 case SND_SOC_DAIFMT_DSP_A:
1115 case SND_SOC_DAIFMT_DSP_B:
1116
1117 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1118 case SND_SOC_DAIFMT_NB_NF:
1119 break;
1120 case SND_SOC_DAIFMT_IB_NF:
1121 hifi |= 0x0080;
1122 break;
1123 default:
1124 return -EINVAL;
1125 }
1126 break;
1127 case SND_SOC_DAIFMT_I2S:
1128 case SND_SOC_DAIFMT_RIGHT_J:
1129 case SND_SOC_DAIFMT_LEFT_J:
1130 hifi &= ~0x0010;
1131 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1132 case SND_SOC_DAIFMT_NB_NF:
1133 break;
1134 case SND_SOC_DAIFMT_IB_IF:
1135 hifi |= 0x0090;
1136 break;
1137 case SND_SOC_DAIFMT_IB_NF:
1138 hifi |= 0x0080;
1139 break;
1140 case SND_SOC_DAIFMT_NB_IF:
1141 hifi |= 0x0010;
1142 break;
1143 default:
1144 return -EINVAL;
1145 }
1146 break;
1147 default:
1148 return -EINVAL;
1149 }
1150
1151 snd_soc_write(codec, WM8753_HIFI, hifi);
1152 snd_soc_write(codec, WM8753_IOCTL, ioctl);
1153 return 0;
1154}
1155
1156
1157
1158
1159static int wm8753_i2s_hw_params(struct snd_pcm_substream *substream,
1160 struct snd_pcm_hw_params *params,
1161 struct snd_soc_dai *dai)
1162{
1163 struct snd_soc_codec *codec = dai->codec;
1164 struct wm8753_priv *wm8753 = snd_soc_codec_get_drvdata(codec);
1165 u16 srate = snd_soc_read(codec, WM8753_SRATE1) & 0x01c0;
1166 u16 hifi = snd_soc_read(codec, WM8753_HIFI) & 0x01f3;
1167 int coeff;
1168
1169
1170 coeff = get_coeff(wm8753->sysclk, params_rate(params));
1171 if (coeff < 0) {
1172 printk(KERN_ERR "wm8753 invalid MCLK or rate\n");
1173 return coeff;
1174 }
1175 snd_soc_write(codec, WM8753_SRATE1, srate | (coeff_div[coeff].sr << 1) |
1176 coeff_div[coeff].usb);
1177
1178
1179 switch (params_format(params)) {
1180 case SNDRV_PCM_FORMAT_S16_LE:
1181 break;
1182 case SNDRV_PCM_FORMAT_S20_3LE:
1183 hifi |= 0x0004;
1184 break;
1185 case SNDRV_PCM_FORMAT_S24_LE:
1186 hifi |= 0x0008;
1187 break;
1188 case SNDRV_PCM_FORMAT_S32_LE:
1189 hifi |= 0x000c;
1190 break;
1191 }
1192
1193 snd_soc_write(codec, WM8753_HIFI, hifi);
1194 return 0;
1195}
1196
1197static int wm8753_mode1v_set_dai_fmt(struct snd_soc_codec *codec,
1198 unsigned int fmt)
1199{
1200 u16 clock;
1201
1202
1203 clock = snd_soc_read(codec, WM8753_CLOCK) & 0xfffb;
1204 snd_soc_write(codec, WM8753_CLOCK, clock);
1205
1206 return wm8753_vdac_adc_set_dai_fmt(codec, fmt);
1207}
1208
1209static int wm8753_mode1h_set_dai_fmt(struct snd_soc_codec *codec,
1210 unsigned int fmt)
1211{
1212 return wm8753_hdac_set_dai_fmt(codec, fmt);
1213}
1214
1215static int wm8753_mode2_set_dai_fmt(struct snd_soc_codec *codec,
1216 unsigned int fmt)
1217{
1218 u16 clock;
1219
1220
1221 clock = snd_soc_read(codec, WM8753_CLOCK) & 0xfffb;
1222 snd_soc_write(codec, WM8753_CLOCK, clock);
1223
1224 return wm8753_vdac_adc_set_dai_fmt(codec, fmt);
1225}
1226
1227static int wm8753_mode3_4_set_dai_fmt(struct snd_soc_codec *codec,
1228 unsigned int fmt)
1229{
1230 u16 clock;
1231
1232
1233 clock = snd_soc_read(codec, WM8753_CLOCK) & 0xfffb;
1234 snd_soc_write(codec, WM8753_CLOCK, clock | 0x4);
1235
1236 if (wm8753_hdac_set_dai_fmt(codec, fmt) < 0)
1237 return -EINVAL;
1238 return wm8753_vdac_adc_set_dai_fmt(codec, fmt);
1239}
1240
1241static int wm8753_hifi_write_dai_fmt(struct snd_soc_codec *codec,
1242 unsigned int fmt)
1243{
1244 struct wm8753_priv *wm8753 = snd_soc_codec_get_drvdata(codec);
1245 int ret = 0;
1246
1247 switch (wm8753->dai_func) {
1248 case 0:
1249 ret = wm8753_mode1h_set_dai_fmt(codec, fmt);
1250 break;
1251 case 1:
1252 ret = wm8753_mode2_set_dai_fmt(codec, fmt);
1253 break;
1254 case 2:
1255 case 3:
1256 ret = wm8753_mode3_4_set_dai_fmt(codec, fmt);
1257 break;
1258 default:
1259 break;
1260 }
1261 if (ret)
1262 return ret;
1263
1264 return wm8753_i2s_set_dai_fmt(codec, fmt);
1265}
1266
1267static int wm8753_hifi_set_dai_fmt(struct snd_soc_dai *codec_dai,
1268 unsigned int fmt)
1269{
1270 struct snd_soc_codec *codec = codec_dai->codec;
1271 struct wm8753_priv *wm8753 = snd_soc_codec_get_drvdata(codec);
1272
1273 wm8753->hifi_fmt = fmt;
1274
1275 return wm8753_hifi_write_dai_fmt(codec, fmt);
1276};
1277
1278static int wm8753_voice_write_dai_fmt(struct snd_soc_codec *codec,
1279 unsigned int fmt)
1280{
1281 struct wm8753_priv *wm8753 = snd_soc_codec_get_drvdata(codec);
1282 int ret = 0;
1283
1284 if (wm8753->dai_func != 0)
1285 return 0;
1286
1287 ret = wm8753_mode1v_set_dai_fmt(codec, fmt);
1288 if (ret)
1289 return ret;
1290 ret = wm8753_pcm_set_dai_fmt(codec, fmt);
1291 if (ret)
1292 return ret;
1293
1294 return 0;
1295};
1296
1297static int wm8753_voice_set_dai_fmt(struct snd_soc_dai *codec_dai,
1298 unsigned int fmt)
1299{
1300 struct snd_soc_codec *codec = codec_dai->codec;
1301 struct wm8753_priv *wm8753 = snd_soc_codec_get_drvdata(codec);
1302
1303 wm8753->voice_fmt = fmt;
1304
1305 return wm8753_voice_write_dai_fmt(codec, fmt);
1306};
1307
1308static int wm8753_mute(struct snd_soc_dai *dai, int mute)
1309{
1310 struct snd_soc_codec *codec = dai->codec;
1311 u16 mute_reg = snd_soc_read(codec, WM8753_DAC) & 0xfff7;
1312 struct wm8753_priv *wm8753 = snd_soc_codec_get_drvdata(codec);
1313
1314
1315
1316 if (mute && wm8753->dai_func == 1) {
1317 if (!codec->active)
1318 snd_soc_write(codec, WM8753_DAC, mute_reg | 0x8);
1319 } else {
1320 if (mute)
1321 snd_soc_write(codec, WM8753_DAC, mute_reg | 0x8);
1322 else
1323 snd_soc_write(codec, WM8753_DAC, mute_reg);
1324 }
1325
1326 return 0;
1327}
1328
1329static int wm8753_set_bias_level(struct snd_soc_codec *codec,
1330 enum snd_soc_bias_level level)
1331{
1332 u16 pwr_reg = snd_soc_read(codec, WM8753_PWR1) & 0xfe3e;
1333
1334 switch (level) {
1335 case SND_SOC_BIAS_ON:
1336
1337 snd_soc_write(codec, WM8753_PWR1, pwr_reg | 0x00c0);
1338 break;
1339 case SND_SOC_BIAS_PREPARE:
1340
1341 snd_soc_write(codec, WM8753_PWR1, pwr_reg | 0x01c1);
1342 break;
1343 case SND_SOC_BIAS_STANDBY:
1344
1345 snd_soc_write(codec, WM8753_PWR1, pwr_reg | 0x0141);
1346 break;
1347 case SND_SOC_BIAS_OFF:
1348 snd_soc_write(codec, WM8753_PWR1, 0x0001);
1349 break;
1350 }
1351 codec->dapm.bias_level = level;
1352 return 0;
1353}
1354
1355#define WM8753_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
1356 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
1357 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
1358 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
1359
1360#define WM8753_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
1361 SNDRV_PCM_FMTBIT_S24_LE)
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374static const struct snd_soc_dai_ops wm8753_dai_ops_hifi_mode = {
1375 .hw_params = wm8753_i2s_hw_params,
1376 .digital_mute = wm8753_mute,
1377 .set_fmt = wm8753_hifi_set_dai_fmt,
1378 .set_clkdiv = wm8753_set_dai_clkdiv,
1379 .set_pll = wm8753_set_dai_pll,
1380 .set_sysclk = wm8753_set_dai_sysclk,
1381};
1382
1383static const struct snd_soc_dai_ops wm8753_dai_ops_voice_mode = {
1384 .hw_params = wm8753_pcm_hw_params,
1385 .digital_mute = wm8753_mute,
1386 .set_fmt = wm8753_voice_set_dai_fmt,
1387 .set_clkdiv = wm8753_set_dai_clkdiv,
1388 .set_pll = wm8753_set_dai_pll,
1389 .set_sysclk = wm8753_set_dai_sysclk,
1390};
1391
1392static struct snd_soc_dai_driver wm8753_dai[] = {
1393
1394{ .name = "wm8753-hifi",
1395 .playback = {
1396 .stream_name = "HiFi Playback",
1397 .channels_min = 1,
1398 .channels_max = 2,
1399 .rates = WM8753_RATES,
1400 .formats = WM8753_FORMATS
1401 },
1402 .capture = {
1403 .stream_name = "Capture",
1404 .channels_min = 1,
1405 .channels_max = 2,
1406 .rates = WM8753_RATES,
1407 .formats = WM8753_FORMATS
1408 },
1409 .ops = &wm8753_dai_ops_hifi_mode,
1410},
1411
1412{ .name = "wm8753-voice",
1413 .playback = {
1414 .stream_name = "Voice Playback",
1415 .channels_min = 1,
1416 .channels_max = 1,
1417 .rates = WM8753_RATES,
1418 .formats = WM8753_FORMATS,
1419 },
1420 .capture = {
1421 .stream_name = "Capture",
1422 .channels_min = 1,
1423 .channels_max = 2,
1424 .rates = WM8753_RATES,
1425 .formats = WM8753_FORMATS,
1426 },
1427 .ops = &wm8753_dai_ops_voice_mode,
1428},
1429};
1430
1431static void wm8753_work(struct work_struct *work)
1432{
1433 struct snd_soc_dapm_context *dapm =
1434 container_of(work, struct snd_soc_dapm_context,
1435 delayed_work.work);
1436 struct snd_soc_codec *codec = dapm->codec;
1437 wm8753_set_bias_level(codec, dapm->bias_level);
1438}
1439
1440static int wm8753_suspend(struct snd_soc_codec *codec)
1441{
1442 wm8753_set_bias_level(codec, SND_SOC_BIAS_OFF);
1443 codec->cache_sync = 1;
1444 return 0;
1445}
1446
1447static int wm8753_resume(struct snd_soc_codec *codec)
1448{
1449 struct wm8753_priv *wm8753 = snd_soc_codec_get_drvdata(codec);
1450
1451 regcache_sync(wm8753->regmap);
1452
1453 wm8753_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1454
1455
1456 if (codec->dapm.suspend_bias_level == SND_SOC_BIAS_ON) {
1457 wm8753_set_bias_level(codec, SND_SOC_BIAS_PREPARE);
1458 codec->dapm.bias_level = SND_SOC_BIAS_ON;
1459 schedule_delayed_work(&codec->dapm.delayed_work,
1460 msecs_to_jiffies(caps_charge));
1461 }
1462
1463 return 0;
1464}
1465
1466static int wm8753_probe(struct snd_soc_codec *codec)
1467{
1468 struct wm8753_priv *wm8753 = snd_soc_codec_get_drvdata(codec);
1469 int ret;
1470
1471 INIT_DELAYED_WORK(&codec->dapm.delayed_work, wm8753_work);
1472
1473 codec->control_data = wm8753->regmap;
1474 ret = snd_soc_codec_set_cache_io(codec, 7, 9, SND_SOC_REGMAP);
1475 if (ret < 0) {
1476 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
1477 return ret;
1478 }
1479
1480 ret = wm8753_reset(codec);
1481 if (ret < 0) {
1482 dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
1483 return ret;
1484 }
1485
1486 wm8753_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1487 wm8753->dai_func = 0;
1488
1489
1490 wm8753_set_bias_level(codec, SND_SOC_BIAS_PREPARE);
1491 schedule_delayed_work(&codec->dapm.delayed_work,
1492 msecs_to_jiffies(caps_charge));
1493
1494
1495 snd_soc_update_bits(codec, WM8753_LDAC, 0x0100, 0x0100);
1496 snd_soc_update_bits(codec, WM8753_RDAC, 0x0100, 0x0100);
1497 snd_soc_update_bits(codec, WM8753_LADC, 0x0100, 0x0100);
1498 snd_soc_update_bits(codec, WM8753_RADC, 0x0100, 0x0100);
1499 snd_soc_update_bits(codec, WM8753_LOUT1V, 0x0100, 0x0100);
1500 snd_soc_update_bits(codec, WM8753_ROUT1V, 0x0100, 0x0100);
1501 snd_soc_update_bits(codec, WM8753_LOUT2V, 0x0100, 0x0100);
1502 snd_soc_update_bits(codec, WM8753_ROUT2V, 0x0100, 0x0100);
1503 snd_soc_update_bits(codec, WM8753_LINVOL, 0x0100, 0x0100);
1504 snd_soc_update_bits(codec, WM8753_RINVOL, 0x0100, 0x0100);
1505
1506 return 0;
1507}
1508
1509
1510static int wm8753_remove(struct snd_soc_codec *codec)
1511{
1512 flush_delayed_work(&codec->dapm.delayed_work);
1513 wm8753_set_bias_level(codec, SND_SOC_BIAS_OFF);
1514
1515 return 0;
1516}
1517
1518static struct snd_soc_codec_driver soc_codec_dev_wm8753 = {
1519 .probe = wm8753_probe,
1520 .remove = wm8753_remove,
1521 .suspend = wm8753_suspend,
1522 .resume = wm8753_resume,
1523 .set_bias_level = wm8753_set_bias_level,
1524
1525 .controls = wm8753_snd_controls,
1526 .num_controls = ARRAY_SIZE(wm8753_snd_controls),
1527 .dapm_widgets = wm8753_dapm_widgets,
1528 .num_dapm_widgets = ARRAY_SIZE(wm8753_dapm_widgets),
1529 .dapm_routes = wm8753_dapm_routes,
1530 .num_dapm_routes = ARRAY_SIZE(wm8753_dapm_routes),
1531};
1532
1533static const struct of_device_id wm8753_of_match[] = {
1534 { .compatible = "wlf,wm8753", },
1535 { }
1536};
1537MODULE_DEVICE_TABLE(of, wm8753_of_match);
1538
1539static const struct regmap_config wm8753_regmap = {
1540 .reg_bits = 7,
1541 .val_bits = 9,
1542
1543 .max_register = WM8753_ADCTL2,
1544 .writeable_reg = wm8753_writeable,
1545 .volatile_reg = wm8753_volatile,
1546
1547 .cache_type = REGCACHE_RBTREE,
1548 .reg_defaults = wm8753_reg_defaults,
1549 .num_reg_defaults = ARRAY_SIZE(wm8753_reg_defaults),
1550};
1551
1552#if defined(CONFIG_SPI_MASTER)
1553static int wm8753_spi_probe(struct spi_device *spi)
1554{
1555 struct wm8753_priv *wm8753;
1556 int ret;
1557
1558 wm8753 = devm_kzalloc(&spi->dev, sizeof(struct wm8753_priv),
1559 GFP_KERNEL);
1560 if (wm8753 == NULL)
1561 return -ENOMEM;
1562
1563 spi_set_drvdata(spi, wm8753);
1564
1565 wm8753->regmap = devm_regmap_init_spi(spi, &wm8753_regmap);
1566 if (IS_ERR(wm8753->regmap)) {
1567 ret = PTR_ERR(wm8753->regmap);
1568 dev_err(&spi->dev, "Failed to allocate register map: %d\n",
1569 ret);
1570 return ret;
1571 }
1572
1573 ret = snd_soc_register_codec(&spi->dev, &soc_codec_dev_wm8753,
1574 wm8753_dai, ARRAY_SIZE(wm8753_dai));
1575 if (ret != 0)
1576 dev_err(&spi->dev, "Failed to register CODEC: %d\n", ret);
1577
1578 return ret;
1579}
1580
1581static int wm8753_spi_remove(struct spi_device *spi)
1582{
1583 snd_soc_unregister_codec(&spi->dev);
1584 return 0;
1585}
1586
1587static struct spi_driver wm8753_spi_driver = {
1588 .driver = {
1589 .name = "wm8753",
1590 .owner = THIS_MODULE,
1591 .of_match_table = wm8753_of_match,
1592 },
1593 .probe = wm8753_spi_probe,
1594 .remove = wm8753_spi_remove,
1595};
1596#endif
1597
1598#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1599static int wm8753_i2c_probe(struct i2c_client *i2c,
1600 const struct i2c_device_id *id)
1601{
1602 struct wm8753_priv *wm8753;
1603 int ret;
1604
1605 wm8753 = devm_kzalloc(&i2c->dev, sizeof(struct wm8753_priv),
1606 GFP_KERNEL);
1607 if (wm8753 == NULL)
1608 return -ENOMEM;
1609
1610 i2c_set_clientdata(i2c, wm8753);
1611
1612 wm8753->regmap = devm_regmap_init_i2c(i2c, &wm8753_regmap);
1613 if (IS_ERR(wm8753->regmap)) {
1614 ret = PTR_ERR(wm8753->regmap);
1615 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
1616 ret);
1617 return ret;
1618 }
1619
1620 ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_wm8753,
1621 wm8753_dai, ARRAY_SIZE(wm8753_dai));
1622 if (ret != 0)
1623 dev_err(&i2c->dev, "Failed to register CODEC: %d\n", ret);
1624
1625 return ret;
1626}
1627
1628static int wm8753_i2c_remove(struct i2c_client *client)
1629{
1630 snd_soc_unregister_codec(&client->dev);
1631 return 0;
1632}
1633
1634static const struct i2c_device_id wm8753_i2c_id[] = {
1635 { "wm8753", 0 },
1636 { }
1637};
1638MODULE_DEVICE_TABLE(i2c, wm8753_i2c_id);
1639
1640static struct i2c_driver wm8753_i2c_driver = {
1641 .driver = {
1642 .name = "wm8753",
1643 .owner = THIS_MODULE,
1644 .of_match_table = wm8753_of_match,
1645 },
1646 .probe = wm8753_i2c_probe,
1647 .remove = wm8753_i2c_remove,
1648 .id_table = wm8753_i2c_id,
1649};
1650#endif
1651
1652static int __init wm8753_modinit(void)
1653{
1654 int ret = 0;
1655#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1656 ret = i2c_add_driver(&wm8753_i2c_driver);
1657 if (ret != 0) {
1658 printk(KERN_ERR "Failed to register wm8753 I2C driver: %d\n",
1659 ret);
1660 }
1661#endif
1662#if defined(CONFIG_SPI_MASTER)
1663 ret = spi_register_driver(&wm8753_spi_driver);
1664 if (ret != 0) {
1665 printk(KERN_ERR "Failed to register wm8753 SPI driver: %d\n",
1666 ret);
1667 }
1668#endif
1669 return ret;
1670}
1671module_init(wm8753_modinit);
1672
1673static void __exit wm8753_exit(void)
1674{
1675#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1676 i2c_del_driver(&wm8753_i2c_driver);
1677#endif
1678#if defined(CONFIG_SPI_MASTER)
1679 spi_unregister_driver(&wm8753_spi_driver);
1680#endif
1681}
1682module_exit(wm8753_exit);
1683
1684MODULE_DESCRIPTION("ASoC WM8753 driver");
1685MODULE_AUTHOR("Liam Girdwood");
1686MODULE_LICENSE("GPL");
1687