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