GNU Linux-libre 4.19.286-gnu1
[releases.git] / sound / soc / codecs / wm8960.c
1 /*
2  * wm8960.c  --  WM8960 ALSA SoC Audio driver
3  *
4  * Copyright 2007-11 Wolfson Microelectronics, plc
5  *
6  * Author: Liam Girdwood
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/init.h>
16 #include <linux/delay.h>
17 #include <linux/pm.h>
18 #include <linux/clk.h>
19 #include <linux/i2c.h>
20 #include <linux/slab.h>
21 #include <sound/core.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
24 #include <sound/soc.h>
25 #include <sound/initval.h>
26 #include <sound/tlv.h>
27 #include <sound/wm8960.h>
28
29 #include "wm8960.h"
30
31 /* R25 - Power 1 */
32 #define WM8960_VMID_MASK 0x180
33 #define WM8960_VREF      0x40
34
35 /* R26 - Power 2 */
36 #define WM8960_PWR2_LOUT1       0x40
37 #define WM8960_PWR2_ROUT1       0x20
38 #define WM8960_PWR2_OUT3        0x02
39
40 /* R28 - Anti-pop 1 */
41 #define WM8960_POBCTRL   0x80
42 #define WM8960_BUFDCOPEN 0x10
43 #define WM8960_BUFIOEN   0x08
44 #define WM8960_SOFT_ST   0x04
45 #define WM8960_HPSTBY    0x01
46
47 /* R29 - Anti-pop 2 */
48 #define WM8960_DISOP     0x40
49 #define WM8960_DRES_MASK 0x30
50
51 static bool is_pll_freq_available(unsigned int source, unsigned int target);
52 static int wm8960_set_pll(struct snd_soc_component *component,
53                 unsigned int freq_in, unsigned int freq_out);
54 /*
55  * wm8960 register cache
56  * We can't read the WM8960 register space when we are
57  * using 2 wire for device control, so we cache them instead.
58  */
59 static const struct reg_default wm8960_reg_defaults[] = {
60         {  0x0, 0x00a7 },
61         {  0x1, 0x00a7 },
62         {  0x2, 0x0000 },
63         {  0x3, 0x0000 },
64         {  0x4, 0x0000 },
65         {  0x5, 0x0008 },
66         {  0x6, 0x0000 },
67         {  0x7, 0x000a },
68         {  0x8, 0x01c0 },
69         {  0x9, 0x0000 },
70         {  0xa, 0x00ff },
71         {  0xb, 0x00ff },
72
73         { 0x10, 0x0000 },
74         { 0x11, 0x007b },
75         { 0x12, 0x0100 },
76         { 0x13, 0x0032 },
77         { 0x14, 0x0000 },
78         { 0x15, 0x00c3 },
79         { 0x16, 0x00c3 },
80         { 0x17, 0x01c0 },
81         { 0x18, 0x0000 },
82         { 0x19, 0x0000 },
83         { 0x1a, 0x0000 },
84         { 0x1b, 0x0000 },
85         { 0x1c, 0x0000 },
86         { 0x1d, 0x0000 },
87
88         { 0x20, 0x0100 },
89         { 0x21, 0x0100 },
90         { 0x22, 0x0050 },
91
92         { 0x25, 0x0050 },
93         { 0x26, 0x0000 },
94         { 0x27, 0x0000 },
95         { 0x28, 0x0000 },
96         { 0x29, 0x0000 },
97         { 0x2a, 0x0040 },
98         { 0x2b, 0x0000 },
99         { 0x2c, 0x0000 },
100         { 0x2d, 0x0050 },
101         { 0x2e, 0x0050 },
102         { 0x2f, 0x0000 },
103         { 0x30, 0x0002 },
104         { 0x31, 0x0037 },
105
106         { 0x33, 0x0080 },
107         { 0x34, 0x0008 },
108         { 0x35, 0x0031 },
109         { 0x36, 0x0026 },
110         { 0x37, 0x00e9 },
111 };
112
113 static bool wm8960_volatile(struct device *dev, unsigned int reg)
114 {
115         switch (reg) {
116         case WM8960_RESET:
117                 return true;
118         default:
119                 return false;
120         }
121 }
122
123 struct wm8960_priv {
124         struct clk *mclk;
125         struct regmap *regmap;
126         int (*set_bias_level)(struct snd_soc_component *,
127                               enum snd_soc_bias_level level);
128         struct snd_soc_dapm_widget *lout1;
129         struct snd_soc_dapm_widget *rout1;
130         struct snd_soc_dapm_widget *out3;
131         bool deemph;
132         int lrclk;
133         int bclk;
134         int sysclk;
135         int clk_id;
136         int freq_in;
137         bool is_stream_in_use[2];
138         struct wm8960_data pdata;
139 };
140
141 #define wm8960_reset(c) regmap_write(c, WM8960_RESET, 0)
142
143 /* enumerated controls */
144 static const char *wm8960_polarity[] = {"No Inversion", "Left Inverted",
145         "Right Inverted", "Stereo Inversion"};
146 static const char *wm8960_3d_upper_cutoff[] = {"High", "Low"};
147 static const char *wm8960_3d_lower_cutoff[] = {"Low", "High"};
148 static const char *wm8960_alcfunc[] = {"Off", "Right", "Left", "Stereo"};
149 static const char *wm8960_alcmode[] = {"ALC", "Limiter"};
150 static const char *wm8960_adc_data_output_sel[] = {
151         "Left Data = Left ADC;  Right Data = Right ADC",
152         "Left Data = Left ADC;  Right Data = Left ADC",
153         "Left Data = Right ADC; Right Data = Right ADC",
154         "Left Data = Right ADC; Right Data = Left ADC",
155 };
156 static const char *wm8960_dmonomix[] = {"Stereo", "Mono"};
157
158 static const struct soc_enum wm8960_enum[] = {
159         SOC_ENUM_SINGLE(WM8960_DACCTL1, 5, 4, wm8960_polarity),
160         SOC_ENUM_SINGLE(WM8960_DACCTL2, 5, 4, wm8960_polarity),
161         SOC_ENUM_SINGLE(WM8960_3D, 6, 2, wm8960_3d_upper_cutoff),
162         SOC_ENUM_SINGLE(WM8960_3D, 5, 2, wm8960_3d_lower_cutoff),
163         SOC_ENUM_SINGLE(WM8960_ALC1, 7, 4, wm8960_alcfunc),
164         SOC_ENUM_SINGLE(WM8960_ALC3, 8, 2, wm8960_alcmode),
165         SOC_ENUM_SINGLE(WM8960_ADDCTL1, 2, 4, wm8960_adc_data_output_sel),
166         SOC_ENUM_SINGLE(WM8960_ADDCTL1, 4, 2, wm8960_dmonomix),
167 };
168
169 static const int deemph_settings[] = { 0, 32000, 44100, 48000 };
170
171 static int wm8960_set_deemph(struct snd_soc_component *component)
172 {
173         struct wm8960_priv *wm8960 = snd_soc_component_get_drvdata(component);
174         int val, i, best;
175
176         /* If we're using deemphasis select the nearest available sample
177          * rate.
178          */
179         if (wm8960->deemph) {
180                 best = 1;
181                 for (i = 2; i < ARRAY_SIZE(deemph_settings); i++) {
182                         if (abs(deemph_settings[i] - wm8960->lrclk) <
183                             abs(deemph_settings[best] - wm8960->lrclk))
184                                 best = i;
185                 }
186
187                 val = best << 1;
188         } else {
189                 val = 0;
190         }
191
192         dev_dbg(component->dev, "Set deemphasis %d\n", val);
193
194         return snd_soc_component_update_bits(component, WM8960_DACCTL1,
195                                    0x6, val);
196 }
197
198 static int wm8960_get_deemph(struct snd_kcontrol *kcontrol,
199                              struct snd_ctl_elem_value *ucontrol)
200 {
201         struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
202         struct wm8960_priv *wm8960 = snd_soc_component_get_drvdata(component);
203
204         ucontrol->value.integer.value[0] = wm8960->deemph;
205         return 0;
206 }
207
208 static int wm8960_put_deemph(struct snd_kcontrol *kcontrol,
209                              struct snd_ctl_elem_value *ucontrol)
210 {
211         struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
212         struct wm8960_priv *wm8960 = snd_soc_component_get_drvdata(component);
213         unsigned int deemph = ucontrol->value.integer.value[0];
214
215         if (deemph > 1)
216                 return -EINVAL;
217
218         wm8960->deemph = deemph;
219
220         return wm8960_set_deemph(component);
221 }
222
223 static const DECLARE_TLV_DB_SCALE(adc_tlv, -9750, 50, 1);
224 static const DECLARE_TLV_DB_SCALE(inpga_tlv, -1725, 75, 0);
225 static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
226 static const DECLARE_TLV_DB_SCALE(bypass_tlv, -2100, 300, 0);
227 static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
228 static const DECLARE_TLV_DB_SCALE(lineinboost_tlv, -1500, 300, 1);
229 static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(micboost_tlv,
230         0, 1, TLV_DB_SCALE_ITEM(0, 1300, 0),
231         2, 3, TLV_DB_SCALE_ITEM(2000, 900, 0),
232 );
233
234 static const struct snd_kcontrol_new wm8960_snd_controls[] = {
235 SOC_DOUBLE_R_TLV("Capture Volume", WM8960_LINVOL, WM8960_RINVOL,
236                  0, 63, 0, inpga_tlv),
237 SOC_DOUBLE_R("Capture Volume ZC Switch", WM8960_LINVOL, WM8960_RINVOL,
238         6, 1, 0),
239 SOC_DOUBLE_R("Capture Switch", WM8960_LINVOL, WM8960_RINVOL,
240         7, 1, 1),
241
242 SOC_SINGLE_TLV("Left Input Boost Mixer LINPUT3 Volume",
243                WM8960_INBMIX1, 4, 7, 0, lineinboost_tlv),
244 SOC_SINGLE_TLV("Left Input Boost Mixer LINPUT2 Volume",
245                WM8960_INBMIX1, 1, 7, 0, lineinboost_tlv),
246 SOC_SINGLE_TLV("Right Input Boost Mixer RINPUT3 Volume",
247                WM8960_INBMIX2, 4, 7, 0, lineinboost_tlv),
248 SOC_SINGLE_TLV("Right Input Boost Mixer RINPUT2 Volume",
249                WM8960_INBMIX2, 1, 7, 0, lineinboost_tlv),
250 SOC_SINGLE_TLV("Right Input Boost Mixer RINPUT1 Volume",
251                 WM8960_RINPATH, 4, 3, 0, micboost_tlv),
252 SOC_SINGLE_TLV("Left Input Boost Mixer LINPUT1 Volume",
253                 WM8960_LINPATH, 4, 3, 0, micboost_tlv),
254
255 SOC_DOUBLE_R_TLV("Playback Volume", WM8960_LDAC, WM8960_RDAC,
256                  0, 255, 0, dac_tlv),
257
258 SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8960_LOUT1, WM8960_ROUT1,
259                  0, 127, 0, out_tlv),
260 SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8960_LOUT1, WM8960_ROUT1,
261         7, 1, 0),
262
263 SOC_DOUBLE_R_TLV("Speaker Playback Volume", WM8960_LOUT2, WM8960_ROUT2,
264                  0, 127, 0, out_tlv),
265 SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8960_LOUT2, WM8960_ROUT2,
266         7, 1, 0),
267 SOC_SINGLE("Speaker DC Volume", WM8960_CLASSD3, 3, 5, 0),
268 SOC_SINGLE("Speaker AC Volume", WM8960_CLASSD3, 0, 5, 0),
269
270 SOC_SINGLE("PCM Playback -6dB Switch", WM8960_DACCTL1, 7, 1, 0),
271 SOC_ENUM("ADC Polarity", wm8960_enum[0]),
272 SOC_SINGLE("ADC High Pass Filter Switch", WM8960_DACCTL1, 0, 1, 0),
273
274 SOC_ENUM("DAC Polarity", wm8960_enum[1]),
275 SOC_SINGLE_BOOL_EXT("DAC Deemphasis Switch", 0,
276                     wm8960_get_deemph, wm8960_put_deemph),
277
278 SOC_ENUM("3D Filter Upper Cut-Off", wm8960_enum[2]),
279 SOC_ENUM("3D Filter Lower Cut-Off", wm8960_enum[3]),
280 SOC_SINGLE("3D Volume", WM8960_3D, 1, 15, 0),
281 SOC_SINGLE("3D Switch", WM8960_3D, 0, 1, 0),
282
283 SOC_ENUM("ALC Function", wm8960_enum[4]),
284 SOC_SINGLE("ALC Max Gain", WM8960_ALC1, 4, 7, 0),
285 SOC_SINGLE("ALC Target", WM8960_ALC1, 0, 15, 1),
286 SOC_SINGLE("ALC Min Gain", WM8960_ALC2, 4, 7, 0),
287 SOC_SINGLE("ALC Hold Time", WM8960_ALC2, 0, 15, 0),
288 SOC_ENUM("ALC Mode", wm8960_enum[5]),
289 SOC_SINGLE("ALC Decay", WM8960_ALC3, 4, 15, 0),
290 SOC_SINGLE("ALC Attack", WM8960_ALC3, 0, 15, 0),
291
292 SOC_SINGLE("Noise Gate Threshold", WM8960_NOISEG, 3, 31, 0),
293 SOC_SINGLE("Noise Gate Switch", WM8960_NOISEG, 0, 1, 0),
294
295 SOC_DOUBLE_R_TLV("ADC PCM Capture Volume", WM8960_LADC, WM8960_RADC,
296         0, 255, 0, adc_tlv),
297
298 SOC_SINGLE_TLV("Left Output Mixer Boost Bypass Volume",
299                WM8960_BYPASS1, 4, 7, 1, bypass_tlv),
300 SOC_SINGLE_TLV("Left Output Mixer LINPUT3 Volume",
301                WM8960_LOUTMIX, 4, 7, 1, bypass_tlv),
302 SOC_SINGLE_TLV("Right Output Mixer Boost Bypass Volume",
303                WM8960_BYPASS2, 4, 7, 1, bypass_tlv),
304 SOC_SINGLE_TLV("Right Output Mixer RINPUT3 Volume",
305                WM8960_ROUTMIX, 4, 7, 1, bypass_tlv),
306
307 SOC_ENUM("ADC Data Output Select", wm8960_enum[6]),
308 SOC_ENUM("DAC Mono Mix", wm8960_enum[7]),
309 };
310
311 static const struct snd_kcontrol_new wm8960_lin_boost[] = {
312 SOC_DAPM_SINGLE("LINPUT2 Switch", WM8960_LINPATH, 6, 1, 0),
313 SOC_DAPM_SINGLE("LINPUT3 Switch", WM8960_LINPATH, 7, 1, 0),
314 SOC_DAPM_SINGLE("LINPUT1 Switch", WM8960_LINPATH, 8, 1, 0),
315 };
316
317 static const struct snd_kcontrol_new wm8960_lin[] = {
318 SOC_DAPM_SINGLE("Boost Switch", WM8960_LINPATH, 3, 1, 0),
319 };
320
321 static const struct snd_kcontrol_new wm8960_rin_boost[] = {
322 SOC_DAPM_SINGLE("RINPUT2 Switch", WM8960_RINPATH, 6, 1, 0),
323 SOC_DAPM_SINGLE("RINPUT3 Switch", WM8960_RINPATH, 7, 1, 0),
324 SOC_DAPM_SINGLE("RINPUT1 Switch", WM8960_RINPATH, 8, 1, 0),
325 };
326
327 static const struct snd_kcontrol_new wm8960_rin[] = {
328 SOC_DAPM_SINGLE("Boost Switch", WM8960_RINPATH, 3, 1, 0),
329 };
330
331 static const struct snd_kcontrol_new wm8960_loutput_mixer[] = {
332 SOC_DAPM_SINGLE("PCM Playback Switch", WM8960_LOUTMIX, 8, 1, 0),
333 SOC_DAPM_SINGLE("LINPUT3 Switch", WM8960_LOUTMIX, 7, 1, 0),
334 SOC_DAPM_SINGLE("Boost Bypass Switch", WM8960_BYPASS1, 7, 1, 0),
335 };
336
337 static const struct snd_kcontrol_new wm8960_routput_mixer[] = {
338 SOC_DAPM_SINGLE("PCM Playback Switch", WM8960_ROUTMIX, 8, 1, 0),
339 SOC_DAPM_SINGLE("RINPUT3 Switch", WM8960_ROUTMIX, 7, 1, 0),
340 SOC_DAPM_SINGLE("Boost Bypass Switch", WM8960_BYPASS2, 7, 1, 0),
341 };
342
343 static const struct snd_kcontrol_new wm8960_mono_out[] = {
344 SOC_DAPM_SINGLE("Left Switch", WM8960_MONOMIX1, 7, 1, 0),
345 SOC_DAPM_SINGLE("Right Switch", WM8960_MONOMIX2, 7, 1, 0),
346 };
347
348 static const struct snd_soc_dapm_widget wm8960_dapm_widgets[] = {
349 SND_SOC_DAPM_INPUT("LINPUT1"),
350 SND_SOC_DAPM_INPUT("RINPUT1"),
351 SND_SOC_DAPM_INPUT("LINPUT2"),
352 SND_SOC_DAPM_INPUT("RINPUT2"),
353 SND_SOC_DAPM_INPUT("LINPUT3"),
354 SND_SOC_DAPM_INPUT("RINPUT3"),
355
356 SND_SOC_DAPM_SUPPLY("MICB", WM8960_POWER1, 1, 0, NULL, 0),
357
358 SND_SOC_DAPM_MIXER("Left Boost Mixer", WM8960_POWER1, 5, 0,
359                    wm8960_lin_boost, ARRAY_SIZE(wm8960_lin_boost)),
360 SND_SOC_DAPM_MIXER("Right Boost Mixer", WM8960_POWER1, 4, 0,
361                    wm8960_rin_boost, ARRAY_SIZE(wm8960_rin_boost)),
362
363 SND_SOC_DAPM_MIXER("Left Input Mixer", WM8960_POWER3, 5, 0,
364                    wm8960_lin, ARRAY_SIZE(wm8960_lin)),
365 SND_SOC_DAPM_MIXER("Right Input Mixer", WM8960_POWER3, 4, 0,
366                    wm8960_rin, ARRAY_SIZE(wm8960_rin)),
367
368 SND_SOC_DAPM_ADC("Left ADC", "Capture", WM8960_POWER1, 3, 0),
369 SND_SOC_DAPM_ADC("Right ADC", "Capture", WM8960_POWER1, 2, 0),
370
371 SND_SOC_DAPM_DAC("Left DAC", "Playback", WM8960_POWER2, 8, 0),
372 SND_SOC_DAPM_DAC("Right DAC", "Playback", WM8960_POWER2, 7, 0),
373
374 SND_SOC_DAPM_MIXER("Left Output Mixer", WM8960_POWER3, 3, 0,
375         &wm8960_loutput_mixer[0],
376         ARRAY_SIZE(wm8960_loutput_mixer)),
377 SND_SOC_DAPM_MIXER("Right Output Mixer", WM8960_POWER3, 2, 0,
378         &wm8960_routput_mixer[0],
379         ARRAY_SIZE(wm8960_routput_mixer)),
380
381 SND_SOC_DAPM_PGA("LOUT1 PGA", WM8960_POWER2, 6, 0, NULL, 0),
382 SND_SOC_DAPM_PGA("ROUT1 PGA", WM8960_POWER2, 5, 0, NULL, 0),
383
384 SND_SOC_DAPM_PGA("Left Speaker PGA", WM8960_POWER2, 4, 0, NULL, 0),
385 SND_SOC_DAPM_PGA("Right Speaker PGA", WM8960_POWER2, 3, 0, NULL, 0),
386
387 SND_SOC_DAPM_PGA("Right Speaker Output", WM8960_CLASSD1, 7, 0, NULL, 0),
388 SND_SOC_DAPM_PGA("Left Speaker Output", WM8960_CLASSD1, 6, 0, NULL, 0),
389
390 SND_SOC_DAPM_OUTPUT("SPK_LP"),
391 SND_SOC_DAPM_OUTPUT("SPK_LN"),
392 SND_SOC_DAPM_OUTPUT("HP_L"),
393 SND_SOC_DAPM_OUTPUT("HP_R"),
394 SND_SOC_DAPM_OUTPUT("SPK_RP"),
395 SND_SOC_DAPM_OUTPUT("SPK_RN"),
396 SND_SOC_DAPM_OUTPUT("OUT3"),
397 };
398
399 static const struct snd_soc_dapm_widget wm8960_dapm_widgets_out3[] = {
400 SND_SOC_DAPM_MIXER("Mono Output Mixer", WM8960_POWER2, 1, 0,
401         &wm8960_mono_out[0],
402         ARRAY_SIZE(wm8960_mono_out)),
403 };
404
405 /* Represent OUT3 as a PGA so that it gets turned on with LOUT1/ROUT1 */
406 static const struct snd_soc_dapm_widget wm8960_dapm_widgets_capless[] = {
407 SND_SOC_DAPM_PGA("OUT3 VMID", WM8960_POWER2, 1, 0, NULL, 0),
408 };
409
410 static const struct snd_soc_dapm_route audio_paths[] = {
411         { "Left Boost Mixer", "LINPUT1 Switch", "LINPUT1" },
412         { "Left Boost Mixer", "LINPUT2 Switch", "LINPUT2" },
413         { "Left Boost Mixer", "LINPUT3 Switch", "LINPUT3" },
414
415         { "Left Input Mixer", "Boost Switch", "Left Boost Mixer" },
416         { "Left Input Mixer", "Boost Switch", "LINPUT1" },  /* Really Boost Switch */
417         { "Left Input Mixer", NULL, "LINPUT2" },
418         { "Left Input Mixer", NULL, "LINPUT3" },
419
420         { "Right Boost Mixer", "RINPUT1 Switch", "RINPUT1" },
421         { "Right Boost Mixer", "RINPUT2 Switch", "RINPUT2" },
422         { "Right Boost Mixer", "RINPUT3 Switch", "RINPUT3" },
423
424         { "Right Input Mixer", "Boost Switch", "Right Boost Mixer" },
425         { "Right Input Mixer", "Boost Switch", "RINPUT1" },  /* Really Boost Switch */
426         { "Right Input Mixer", NULL, "RINPUT2" },
427         { "Right Input Mixer", NULL, "RINPUT3" },
428
429         { "Left ADC", NULL, "Left Input Mixer" },
430         { "Right ADC", NULL, "Right Input Mixer" },
431
432         { "Left Output Mixer", "LINPUT3 Switch", "LINPUT3" },
433         { "Left Output Mixer", "Boost Bypass Switch", "Left Boost Mixer" },
434         { "Left Output Mixer", "PCM Playback Switch", "Left DAC" },
435
436         { "Right Output Mixer", "RINPUT3 Switch", "RINPUT3" },
437         { "Right Output Mixer", "Boost Bypass Switch", "Right Boost Mixer" },
438         { "Right Output Mixer", "PCM Playback Switch", "Right DAC" },
439
440         { "LOUT1 PGA", NULL, "Left Output Mixer" },
441         { "ROUT1 PGA", NULL, "Right Output Mixer" },
442
443         { "HP_L", NULL, "LOUT1 PGA" },
444         { "HP_R", NULL, "ROUT1 PGA" },
445
446         { "Left Speaker PGA", NULL, "Left Output Mixer" },
447         { "Right Speaker PGA", NULL, "Right Output Mixer" },
448
449         { "Left Speaker Output", NULL, "Left Speaker PGA" },
450         { "Right Speaker Output", NULL, "Right Speaker PGA" },
451
452         { "SPK_LN", NULL, "Left Speaker Output" },
453         { "SPK_LP", NULL, "Left Speaker Output" },
454         { "SPK_RN", NULL, "Right Speaker Output" },
455         { "SPK_RP", NULL, "Right Speaker Output" },
456 };
457
458 static const struct snd_soc_dapm_route audio_paths_out3[] = {
459         { "Mono Output Mixer", "Left Switch", "Left Output Mixer" },
460         { "Mono Output Mixer", "Right Switch", "Right Output Mixer" },
461
462         { "OUT3", NULL, "Mono Output Mixer", }
463 };
464
465 static const struct snd_soc_dapm_route audio_paths_capless[] = {
466         { "HP_L", NULL, "OUT3 VMID" },
467         { "HP_R", NULL, "OUT3 VMID" },
468
469         { "OUT3 VMID", NULL, "Left Output Mixer" },
470         { "OUT3 VMID", NULL, "Right Output Mixer" },
471 };
472
473 static int wm8960_add_widgets(struct snd_soc_component *component)
474 {
475         struct wm8960_priv *wm8960 = snd_soc_component_get_drvdata(component);
476         struct wm8960_data *pdata = &wm8960->pdata;
477         struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
478         struct snd_soc_dapm_widget *w;
479
480         snd_soc_dapm_new_controls(dapm, wm8960_dapm_widgets,
481                                   ARRAY_SIZE(wm8960_dapm_widgets));
482
483         snd_soc_dapm_add_routes(dapm, audio_paths, ARRAY_SIZE(audio_paths));
484
485         /* In capless mode OUT3 is used to provide VMID for the
486          * headphone outputs, otherwise it is used as a mono mixer.
487          */
488         if (pdata && pdata->capless) {
489                 snd_soc_dapm_new_controls(dapm, wm8960_dapm_widgets_capless,
490                                           ARRAY_SIZE(wm8960_dapm_widgets_capless));
491
492                 snd_soc_dapm_add_routes(dapm, audio_paths_capless,
493                                         ARRAY_SIZE(audio_paths_capless));
494         } else {
495                 snd_soc_dapm_new_controls(dapm, wm8960_dapm_widgets_out3,
496                                           ARRAY_SIZE(wm8960_dapm_widgets_out3));
497
498                 snd_soc_dapm_add_routes(dapm, audio_paths_out3,
499                                         ARRAY_SIZE(audio_paths_out3));
500         }
501
502         /* We need to power up the headphone output stage out of
503          * sequence for capless mode.  To save scanning the widget
504          * list each time to find the desired power state do so now
505          * and save the result.
506          */
507         list_for_each_entry(w, &component->card->widgets, list) {
508                 if (w->dapm != dapm)
509                         continue;
510                 if (strcmp(w->name, "LOUT1 PGA") == 0)
511                         wm8960->lout1 = w;
512                 if (strcmp(w->name, "ROUT1 PGA") == 0)
513                         wm8960->rout1 = w;
514                 if (strcmp(w->name, "OUT3 VMID") == 0)
515                         wm8960->out3 = w;
516         }
517         
518         return 0;
519 }
520
521 static int wm8960_set_dai_fmt(struct snd_soc_dai *codec_dai,
522                 unsigned int fmt)
523 {
524         struct snd_soc_component *component = codec_dai->component;
525         u16 iface = 0;
526
527         /* set master/slave audio interface */
528         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
529         case SND_SOC_DAIFMT_CBM_CFM:
530                 iface |= 0x0040;
531                 break;
532         case SND_SOC_DAIFMT_CBS_CFS:
533                 break;
534         default:
535                 return -EINVAL;
536         }
537
538         /* interface format */
539         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
540         case SND_SOC_DAIFMT_I2S:
541                 iface |= 0x0002;
542                 break;
543         case SND_SOC_DAIFMT_RIGHT_J:
544                 break;
545         case SND_SOC_DAIFMT_LEFT_J:
546                 iface |= 0x0001;
547                 break;
548         case SND_SOC_DAIFMT_DSP_A:
549                 iface |= 0x0003;
550                 break;
551         case SND_SOC_DAIFMT_DSP_B:
552                 iface |= 0x0013;
553                 break;
554         default:
555                 return -EINVAL;
556         }
557
558         /* clock inversion */
559         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
560         case SND_SOC_DAIFMT_NB_NF:
561                 break;
562         case SND_SOC_DAIFMT_IB_IF:
563                 iface |= 0x0090;
564                 break;
565         case SND_SOC_DAIFMT_IB_NF:
566                 iface |= 0x0080;
567                 break;
568         case SND_SOC_DAIFMT_NB_IF:
569                 iface |= 0x0010;
570                 break;
571         default:
572                 return -EINVAL;
573         }
574
575         /* set iface */
576         snd_soc_component_write(component, WM8960_IFACE1, iface);
577         return 0;
578 }
579
580 static struct {
581         int rate;
582         unsigned int val;
583 } alc_rates[] = {
584         { 48000, 0 },
585         { 44100, 0 },
586         { 32000, 1 },
587         { 22050, 2 },
588         { 24000, 2 },
589         { 16000, 3 },
590         { 11025, 4 },
591         { 12000, 4 },
592         {  8000, 5 },
593 };
594
595 /* -1 for reserved value */
596 static const int sysclk_divs[] = { 1, -1, 2, -1 };
597
598 /* Multiply 256 for internal 256 div */
599 static const int dac_divs[] = { 256, 384, 512, 768, 1024, 1408, 1536 };
600
601 /* Multiply 10 to eliminate decimials */
602 static const int bclk_divs[] = {
603         10, 15, 20, 30, 40, 55, 60, 80, 110,
604         120, 160, 220, 240, 320, 320, 320
605 };
606
607 /**
608  * wm8960_configure_sysclk - checks if there is a sysclk frequency available
609  *      The sysclk must be chosen such that:
610  *              - sysclk     = MCLK / sysclk_divs
611  *              - lrclk      = sysclk / dac_divs
612  *              - 10 * bclk  = sysclk / bclk_divs
613  *
614  *      If we cannot find an exact match for (sysclk, lrclk, bclk)
615  *      triplet, we relax the bclk such that bclk is chosen as the
616  *      closest available frequency greater than expected bclk.
617  *
618  * @wm8960_priv: wm8960 codec private data
619  * @mclk: MCLK used to derive sysclk
620  * @sysclk_idx: sysclk_divs index for found sysclk
621  * @dac_idx: dac_divs index for found lrclk
622  * @bclk_idx: bclk_divs index for found bclk
623  *
624  * Returns:
625  *  -1, in case no sysclk frequency available found
626  * >=0, in case we could derive bclk and lrclk from sysclk using
627  *      (@sysclk_idx, @dac_idx, @bclk_idx) dividers
628  */
629 static
630 int wm8960_configure_sysclk(struct wm8960_priv *wm8960, int mclk,
631                             int *sysclk_idx, int *dac_idx, int *bclk_idx)
632 {
633         int sysclk, bclk, lrclk;
634         int i, j, k;
635         int diff, closest = mclk;
636
637         /* marker for no match */
638         *bclk_idx = -1;
639
640         bclk = wm8960->bclk;
641         lrclk = wm8960->lrclk;
642
643         /* check if the sysclk frequency is available. */
644         for (i = 0; i < ARRAY_SIZE(sysclk_divs); ++i) {
645                 if (sysclk_divs[i] == -1)
646                         continue;
647                 sysclk = mclk / sysclk_divs[i];
648                 for (j = 0; j < ARRAY_SIZE(dac_divs); ++j) {
649                         if (sysclk != dac_divs[j] * lrclk)
650                                 continue;
651                         for (k = 0; k < ARRAY_SIZE(bclk_divs); ++k) {
652                                 diff = sysclk - bclk * bclk_divs[k] / 10;
653                                 if (diff == 0) {
654                                         *sysclk_idx = i;
655                                         *dac_idx = j;
656                                         *bclk_idx = k;
657                                         break;
658                                 }
659                                 if (diff > 0 && closest > diff) {
660                                         *sysclk_idx = i;
661                                         *dac_idx = j;
662                                         *bclk_idx = k;
663                                         closest = diff;
664                                 }
665                         }
666                         if (k != ARRAY_SIZE(bclk_divs))
667                                 break;
668                 }
669                 if (j != ARRAY_SIZE(dac_divs))
670                         break;
671         }
672         return *bclk_idx;
673 }
674
675 /**
676  * wm8960_configure_pll - checks if there is a PLL out frequency available
677  *      The PLL out frequency must be chosen such that:
678  *              - sysclk      = lrclk * dac_divs
679  *              - freq_out    = sysclk * sysclk_divs
680  *              - 10 * sysclk = bclk * bclk_divs
681  *
682  *      If we cannot find an exact match for (sysclk, lrclk, bclk)
683  *      triplet, we relax the bclk such that bclk is chosen as the
684  *      closest available frequency greater than expected bclk.
685  *
686  * @component: component structure
687  * @freq_in: input frequency used to derive freq out via PLL
688  * @sysclk_idx: sysclk_divs index for found sysclk
689  * @dac_idx: dac_divs index for found lrclk
690  * @bclk_idx: bclk_divs index for found bclk
691  *
692  * Returns:
693  * < 0, in case no PLL frequency out available was found
694  * >=0, in case we could derive bclk, lrclk, sysclk from PLL out using
695  *      (@sysclk_idx, @dac_idx, @bclk_idx) dividers
696  */
697 static
698 int wm8960_configure_pll(struct snd_soc_component *component, int freq_in,
699                          int *sysclk_idx, int *dac_idx, int *bclk_idx)
700 {
701         struct wm8960_priv *wm8960 = snd_soc_component_get_drvdata(component);
702         int sysclk, bclk, lrclk, freq_out;
703         int diff, closest, best_freq_out;
704         int i, j, k;
705
706         bclk = wm8960->bclk;
707         lrclk = wm8960->lrclk;
708         closest = freq_in;
709
710         best_freq_out = -EINVAL;
711         *sysclk_idx = *dac_idx = *bclk_idx = -1;
712
713         /*
714          * From Datasheet, the PLL performs best when f2 is between
715          * 90MHz and 100MHz, the desired sysclk output is 11.2896MHz
716          * or 12.288MHz, then sysclkdiv = 2 is the best choice.
717          * So search sysclk_divs from 2 to 1 other than from 1 to 2.
718          */
719         for (i = ARRAY_SIZE(sysclk_divs) - 1; i >= 0; --i) {
720                 if (sysclk_divs[i] == -1)
721                         continue;
722                 for (j = 0; j < ARRAY_SIZE(dac_divs); ++j) {
723                         sysclk = lrclk * dac_divs[j];
724                         freq_out = sysclk * sysclk_divs[i];
725
726                         for (k = 0; k < ARRAY_SIZE(bclk_divs); ++k) {
727                                 if (!is_pll_freq_available(freq_in, freq_out))
728                                         continue;
729
730                                 diff = sysclk - bclk * bclk_divs[k] / 10;
731                                 if (diff == 0) {
732                                         *sysclk_idx = i;
733                                         *dac_idx = j;
734                                         *bclk_idx = k;
735                                         return freq_out;
736                                 }
737                                 if (diff > 0 && closest > diff) {
738                                         *sysclk_idx = i;
739                                         *dac_idx = j;
740                                         *bclk_idx = k;
741                                         closest = diff;
742                                         best_freq_out = freq_out;
743                                 }
744                         }
745                 }
746         }
747
748         return best_freq_out;
749 }
750 static int wm8960_configure_clocking(struct snd_soc_component *component)
751 {
752         struct wm8960_priv *wm8960 = snd_soc_component_get_drvdata(component);
753         int freq_out, freq_in;
754         u16 iface1 = snd_soc_component_read32(component, WM8960_IFACE1);
755         int i, j, k;
756         int ret;
757
758         /*
759          * For Slave mode clocking should still be configured,
760          * so this if statement should be removed, but some platform
761          * may not work if the sysclk is not configured, to avoid such
762          * compatible issue, just add '!wm8960->sysclk' condition in
763          * this if statement.
764          */
765         if (!(iface1 & (1 << 6)) && !wm8960->sysclk) {
766                 dev_warn(component->dev,
767                          "slave mode, but proceeding with no clock configuration\n");
768                 return 0;
769         }
770
771         if (wm8960->clk_id != WM8960_SYSCLK_MCLK && !wm8960->freq_in) {
772                 dev_err(component->dev, "No MCLK configured\n");
773                 return -EINVAL;
774         }
775
776         freq_in = wm8960->freq_in;
777         /*
778          * If it's sysclk auto mode, check if the MCLK can provide sysclk or
779          * not. If MCLK can provide sysclk, using MCLK to provide sysclk
780          * directly. Otherwise, auto select a available pll out frequency
781          * and set PLL.
782          */
783         if (wm8960->clk_id == WM8960_SYSCLK_AUTO) {
784                 /* disable the PLL and using MCLK to provide sysclk */
785                 wm8960_set_pll(component, 0, 0);
786                 freq_out = freq_in;
787         } else if (wm8960->sysclk) {
788                 freq_out = wm8960->sysclk;
789         } else {
790                 dev_err(component->dev, "No SYSCLK configured\n");
791                 return -EINVAL;
792         }
793
794         if (wm8960->clk_id != WM8960_SYSCLK_PLL) {
795                 ret = wm8960_configure_sysclk(wm8960, freq_out, &i, &j, &k);
796                 if (ret >= 0) {
797                         goto configure_clock;
798                 } else if (wm8960->clk_id != WM8960_SYSCLK_AUTO) {
799                         dev_err(component->dev, "failed to configure clock\n");
800                         return -EINVAL;
801                 }
802         }
803
804         freq_out = wm8960_configure_pll(component, freq_in, &i, &j, &k);
805         if (freq_out < 0) {
806                 dev_err(component->dev, "failed to configure clock via PLL\n");
807                 return freq_out;
808         }
809         wm8960_set_pll(component, freq_in, freq_out);
810
811 configure_clock:
812         /* configure sysclk clock */
813         snd_soc_component_update_bits(component, WM8960_CLOCK1, 3 << 1, i << 1);
814
815         /* configure frame clock */
816         snd_soc_component_update_bits(component, WM8960_CLOCK1, 0x7 << 3, j << 3);
817         snd_soc_component_update_bits(component, WM8960_CLOCK1, 0x7 << 6, j << 6);
818
819         /* configure bit clock */
820         snd_soc_component_update_bits(component, WM8960_CLOCK2, 0xf, k);
821
822         return 0;
823 }
824
825 static int wm8960_hw_params(struct snd_pcm_substream *substream,
826                             struct snd_pcm_hw_params *params,
827                             struct snd_soc_dai *dai)
828 {
829         struct snd_soc_component *component = dai->component;
830         struct wm8960_priv *wm8960 = snd_soc_component_get_drvdata(component);
831         u16 iface = snd_soc_component_read32(component, WM8960_IFACE1) & 0xfff3;
832         bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
833         int i;
834
835         wm8960->bclk = snd_soc_params_to_bclk(params);
836         if (params_channels(params) == 1)
837                 wm8960->bclk *= 2;
838
839         /* bit size */
840         switch (params_width(params)) {
841         case 16:
842                 break;
843         case 20:
844                 iface |= 0x0004;
845                 break;
846         case 24:
847                 iface |= 0x0008;
848                 break;
849         case 32:
850                 /* right justify mode does not support 32 word length */
851                 if ((iface & 0x3) != 0) {
852                         iface |= 0x000c;
853                         break;
854                 }
855                 /* fall through */
856         default:
857                 dev_err(component->dev, "unsupported width %d\n",
858                         params_width(params));
859                 return -EINVAL;
860         }
861
862         wm8960->lrclk = params_rate(params);
863         /* Update filters for the new rate */
864         if (tx) {
865                 wm8960_set_deemph(component);
866         } else {
867                 for (i = 0; i < ARRAY_SIZE(alc_rates); i++)
868                         if (alc_rates[i].rate == params_rate(params))
869                                 snd_soc_component_update_bits(component,
870                                                     WM8960_ADDCTL3, 0x7,
871                                                     alc_rates[i].val);
872         }
873
874         /* set iface */
875         snd_soc_component_write(component, WM8960_IFACE1, iface);
876
877         wm8960->is_stream_in_use[tx] = true;
878
879         if (!wm8960->is_stream_in_use[!tx])
880                 return wm8960_configure_clocking(component);
881
882         return 0;
883 }
884
885 static int wm8960_hw_free(struct snd_pcm_substream *substream,
886                 struct snd_soc_dai *dai)
887 {
888         struct snd_soc_component *component = dai->component;
889         struct wm8960_priv *wm8960 = snd_soc_component_get_drvdata(component);
890         bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
891
892         wm8960->is_stream_in_use[tx] = false;
893
894         return 0;
895 }
896
897 static int wm8960_mute(struct snd_soc_dai *dai, int mute)
898 {
899         struct snd_soc_component *component = dai->component;
900
901         if (mute)
902                 snd_soc_component_update_bits(component, WM8960_DACCTL1, 0x8, 0x8);
903         else
904                 snd_soc_component_update_bits(component, WM8960_DACCTL1, 0x8, 0);
905         return 0;
906 }
907
908 static int wm8960_set_bias_level_out3(struct snd_soc_component *component,
909                                       enum snd_soc_bias_level level)
910 {
911         struct wm8960_priv *wm8960 = snd_soc_component_get_drvdata(component);
912         u16 pm2 = snd_soc_component_read32(component, WM8960_POWER2);
913         int ret;
914
915         switch (level) {
916         case SND_SOC_BIAS_ON:
917                 break;
918
919         case SND_SOC_BIAS_PREPARE:
920                 switch (snd_soc_component_get_bias_level(component)) {
921                 case SND_SOC_BIAS_STANDBY:
922                         if (!IS_ERR(wm8960->mclk)) {
923                                 ret = clk_prepare_enable(wm8960->mclk);
924                                 if (ret) {
925                                         dev_err(component->dev,
926                                                 "Failed to enable MCLK: %d\n",
927                                                 ret);
928                                         return ret;
929                                 }
930                         }
931
932                         ret = wm8960_configure_clocking(component);
933                         if (ret)
934                                 return ret;
935
936                         /* Set VMID to 2x50k */
937                         snd_soc_component_update_bits(component, WM8960_POWER1, 0x180, 0x80);
938                         break;
939
940                 case SND_SOC_BIAS_ON:
941                         /*
942                          * If it's sysclk auto mode, and the pll is enabled,
943                          * disable the pll
944                          */
945                         if (wm8960->clk_id == WM8960_SYSCLK_AUTO && (pm2 & 0x1))
946                                 wm8960_set_pll(component, 0, 0);
947
948                         if (!IS_ERR(wm8960->mclk))
949                                 clk_disable_unprepare(wm8960->mclk);
950                         break;
951
952                 default:
953                         break;
954                 }
955
956                 break;
957
958         case SND_SOC_BIAS_STANDBY:
959                 if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
960                         regcache_sync(wm8960->regmap);
961
962                         /* Enable anti-pop features */
963                         snd_soc_component_write(component, WM8960_APOP1,
964                                       WM8960_POBCTRL | WM8960_SOFT_ST |
965                                       WM8960_BUFDCOPEN | WM8960_BUFIOEN);
966
967                         /* Enable & ramp VMID at 2x50k */
968                         snd_soc_component_update_bits(component, WM8960_POWER1, 0x80, 0x80);
969                         msleep(100);
970
971                         /* Enable VREF */
972                         snd_soc_component_update_bits(component, WM8960_POWER1, WM8960_VREF,
973                                             WM8960_VREF);
974
975                         /* Disable anti-pop features */
976                         snd_soc_component_write(component, WM8960_APOP1, WM8960_BUFIOEN);
977                 }
978
979                 /* Set VMID to 2x250k */
980                 snd_soc_component_update_bits(component, WM8960_POWER1, 0x180, 0x100);
981                 break;
982
983         case SND_SOC_BIAS_OFF:
984                 /* Enable anti-pop features */
985                 snd_soc_component_write(component, WM8960_APOP1,
986                              WM8960_POBCTRL | WM8960_SOFT_ST |
987                              WM8960_BUFDCOPEN | WM8960_BUFIOEN);
988
989                 /* Disable VMID and VREF, let them discharge */
990                 snd_soc_component_write(component, WM8960_POWER1, 0);
991                 msleep(600);
992                 break;
993         }
994
995         return 0;
996 }
997
998 static int wm8960_set_bias_level_capless(struct snd_soc_component *component,
999                                          enum snd_soc_bias_level level)
1000 {
1001         struct wm8960_priv *wm8960 = snd_soc_component_get_drvdata(component);
1002         u16 pm2 = snd_soc_component_read32(component, WM8960_POWER2);
1003         int reg, ret;
1004
1005         switch (level) {
1006         case SND_SOC_BIAS_ON:
1007                 break;
1008
1009         case SND_SOC_BIAS_PREPARE:
1010                 switch (snd_soc_component_get_bias_level(component)) {
1011                 case SND_SOC_BIAS_STANDBY:
1012                         /* Enable anti pop mode */
1013                         snd_soc_component_update_bits(component, WM8960_APOP1,
1014                                             WM8960_POBCTRL | WM8960_SOFT_ST |
1015                                             WM8960_BUFDCOPEN,
1016                                             WM8960_POBCTRL | WM8960_SOFT_ST |
1017                                             WM8960_BUFDCOPEN);
1018
1019                         /* Enable LOUT1, ROUT1 and OUT3 if they're enabled */
1020                         reg = 0;
1021                         if (wm8960->lout1 && wm8960->lout1->power)
1022                                 reg |= WM8960_PWR2_LOUT1;
1023                         if (wm8960->rout1 && wm8960->rout1->power)
1024                                 reg |= WM8960_PWR2_ROUT1;
1025                         if (wm8960->out3 && wm8960->out3->power)
1026                                 reg |= WM8960_PWR2_OUT3;
1027                         snd_soc_component_update_bits(component, WM8960_POWER2,
1028                                             WM8960_PWR2_LOUT1 |
1029                                             WM8960_PWR2_ROUT1 |
1030                                             WM8960_PWR2_OUT3, reg);
1031
1032                         /* Enable VMID at 2*50k */
1033                         snd_soc_component_update_bits(component, WM8960_POWER1,
1034                                             WM8960_VMID_MASK, 0x80);
1035
1036                         /* Ramp */
1037                         msleep(100);
1038
1039                         /* Enable VREF */
1040                         snd_soc_component_update_bits(component, WM8960_POWER1,
1041                                             WM8960_VREF, WM8960_VREF);
1042
1043                         msleep(100);
1044
1045                         if (!IS_ERR(wm8960->mclk)) {
1046                                 ret = clk_prepare_enable(wm8960->mclk);
1047                                 if (ret) {
1048                                         dev_err(component->dev,
1049                                                 "Failed to enable MCLK: %d\n",
1050                                                 ret);
1051                                         return ret;
1052                                 }
1053                         }
1054
1055                         ret = wm8960_configure_clocking(component);
1056                         if (ret)
1057                                 return ret;
1058
1059                         break;
1060
1061                 case SND_SOC_BIAS_ON:
1062                         /*
1063                          * If it's sysclk auto mode, and the pll is enabled,
1064                          * disable the pll
1065                          */
1066                         if (wm8960->clk_id == WM8960_SYSCLK_AUTO && (pm2 & 0x1))
1067                                 wm8960_set_pll(component, 0, 0);
1068
1069                         if (!IS_ERR(wm8960->mclk))
1070                                 clk_disable_unprepare(wm8960->mclk);
1071
1072                         /* Enable anti-pop mode */
1073                         snd_soc_component_update_bits(component, WM8960_APOP1,
1074                                             WM8960_POBCTRL | WM8960_SOFT_ST |
1075                                             WM8960_BUFDCOPEN,
1076                                             WM8960_POBCTRL | WM8960_SOFT_ST |
1077                                             WM8960_BUFDCOPEN);
1078
1079                         /* Disable VMID and VREF */
1080                         snd_soc_component_update_bits(component, WM8960_POWER1,
1081                                             WM8960_VREF | WM8960_VMID_MASK, 0);
1082                         break;
1083
1084                 case SND_SOC_BIAS_OFF:
1085                         regcache_sync(wm8960->regmap);
1086                         break;
1087                 default:
1088                         break;
1089                 }
1090                 break;
1091
1092         case SND_SOC_BIAS_STANDBY:
1093                 switch (snd_soc_component_get_bias_level(component)) {
1094                 case SND_SOC_BIAS_PREPARE:
1095                         /* Disable HP discharge */
1096                         snd_soc_component_update_bits(component, WM8960_APOP2,
1097                                             WM8960_DISOP | WM8960_DRES_MASK,
1098                                             0);
1099
1100                         /* Disable anti-pop features */
1101                         snd_soc_component_update_bits(component, WM8960_APOP1,
1102                                             WM8960_POBCTRL | WM8960_SOFT_ST |
1103                                             WM8960_BUFDCOPEN,
1104                                             WM8960_POBCTRL | WM8960_SOFT_ST |
1105                                             WM8960_BUFDCOPEN);
1106                         break;
1107
1108                 default:
1109                         break;
1110                 }
1111                 break;
1112
1113         case SND_SOC_BIAS_OFF:
1114                 break;
1115         }
1116
1117         return 0;
1118 }
1119
1120 /* PLL divisors */
1121 struct _pll_div {
1122         u32 pre_div:1;
1123         u32 n:4;
1124         u32 k:24;
1125 };
1126
1127 static bool is_pll_freq_available(unsigned int source, unsigned int target)
1128 {
1129         unsigned int Ndiv;
1130
1131         if (source == 0 || target == 0)
1132                 return false;
1133
1134         /* Scale up target to PLL operating frequency */
1135         target *= 4;
1136         Ndiv = target / source;
1137
1138         if (Ndiv < 6) {
1139                 source >>= 1;
1140                 Ndiv = target / source;
1141         }
1142
1143         if ((Ndiv < 6) || (Ndiv > 12))
1144                 return false;
1145
1146         return true;
1147 }
1148
1149 /* The size in bits of the pll divide multiplied by 10
1150  * to allow rounding later */
1151 #define FIXED_PLL_SIZE ((1 << 24) * 10)
1152
1153 static int pll_factors(unsigned int source, unsigned int target,
1154                        struct _pll_div *pll_div)
1155 {
1156         unsigned long long Kpart;
1157         unsigned int K, Ndiv, Nmod;
1158
1159         pr_debug("WM8960 PLL: setting %dHz->%dHz\n", source, target);
1160
1161         /* Scale up target to PLL operating frequency */
1162         target *= 4;
1163
1164         Ndiv = target / source;
1165         if (Ndiv < 6) {
1166                 source >>= 1;
1167                 pll_div->pre_div = 1;
1168                 Ndiv = target / source;
1169         } else
1170                 pll_div->pre_div = 0;
1171
1172         if ((Ndiv < 6) || (Ndiv > 12)) {
1173                 pr_err("WM8960 PLL: Unsupported N=%d\n", Ndiv);
1174                 return -EINVAL;
1175         }
1176
1177         pll_div->n = Ndiv;
1178         Nmod = target % source;
1179         Kpart = FIXED_PLL_SIZE * (long long)Nmod;
1180
1181         do_div(Kpart, source);
1182
1183         K = Kpart & 0xFFFFFFFF;
1184
1185         /* Check if we need to round */
1186         if ((K % 10) >= 5)
1187                 K += 5;
1188
1189         /* Move down to proper range now rounding is done */
1190         K /= 10;
1191
1192         pll_div->k = K;
1193
1194         pr_debug("WM8960 PLL: N=%x K=%x pre_div=%d\n",
1195                  pll_div->n, pll_div->k, pll_div->pre_div);
1196
1197         return 0;
1198 }
1199
1200 static int wm8960_set_pll(struct snd_soc_component *component,
1201                 unsigned int freq_in, unsigned int freq_out)
1202 {
1203         u16 reg;
1204         static struct _pll_div pll_div;
1205         int ret;
1206
1207         if (freq_in && freq_out) {
1208                 ret = pll_factors(freq_in, freq_out, &pll_div);
1209                 if (ret != 0)
1210                         return ret;
1211         }
1212
1213         /* Disable the PLL: even if we are changing the frequency the
1214          * PLL needs to be disabled while we do so. */
1215         snd_soc_component_update_bits(component, WM8960_CLOCK1, 0x1, 0);
1216         snd_soc_component_update_bits(component, WM8960_POWER2, 0x1, 0);
1217
1218         if (!freq_in || !freq_out)
1219                 return 0;
1220
1221         reg = snd_soc_component_read32(component, WM8960_PLL1) & ~0x3f;
1222         reg |= pll_div.pre_div << 4;
1223         reg |= pll_div.n;
1224
1225         if (pll_div.k) {
1226                 reg |= 0x20;
1227
1228                 snd_soc_component_write(component, WM8960_PLL2, (pll_div.k >> 16) & 0xff);
1229                 snd_soc_component_write(component, WM8960_PLL3, (pll_div.k >> 8) & 0xff);
1230                 snd_soc_component_write(component, WM8960_PLL4, pll_div.k & 0xff);
1231         }
1232         snd_soc_component_write(component, WM8960_PLL1, reg);
1233
1234         /* Turn it on */
1235         snd_soc_component_update_bits(component, WM8960_POWER2, 0x1, 0x1);
1236         msleep(250);
1237         snd_soc_component_update_bits(component, WM8960_CLOCK1, 0x1, 0x1);
1238
1239         return 0;
1240 }
1241
1242 static int wm8960_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
1243                 int source, unsigned int freq_in, unsigned int freq_out)
1244 {
1245         struct snd_soc_component *component = codec_dai->component;
1246         struct wm8960_priv *wm8960 = snd_soc_component_get_drvdata(component);
1247
1248         wm8960->freq_in = freq_in;
1249
1250         if (pll_id == WM8960_SYSCLK_AUTO)
1251                 return 0;
1252
1253         return wm8960_set_pll(component, freq_in, freq_out);
1254 }
1255
1256 static int wm8960_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
1257                 int div_id, int div)
1258 {
1259         struct snd_soc_component *component = codec_dai->component;
1260         u16 reg;
1261
1262         switch (div_id) {
1263         case WM8960_SYSCLKDIV:
1264                 reg = snd_soc_component_read32(component, WM8960_CLOCK1) & 0x1f9;
1265                 snd_soc_component_write(component, WM8960_CLOCK1, reg | div);
1266                 break;
1267         case WM8960_DACDIV:
1268                 reg = snd_soc_component_read32(component, WM8960_CLOCK1) & 0x1c7;
1269                 snd_soc_component_write(component, WM8960_CLOCK1, reg | div);
1270                 break;
1271         case WM8960_OPCLKDIV:
1272                 reg = snd_soc_component_read32(component, WM8960_PLL1) & 0x03f;
1273                 snd_soc_component_write(component, WM8960_PLL1, reg | div);
1274                 break;
1275         case WM8960_DCLKDIV:
1276                 reg = snd_soc_component_read32(component, WM8960_CLOCK2) & 0x03f;
1277                 snd_soc_component_write(component, WM8960_CLOCK2, reg | div);
1278                 break;
1279         case WM8960_TOCLKSEL:
1280                 reg = snd_soc_component_read32(component, WM8960_ADDCTL1) & 0x1fd;
1281                 snd_soc_component_write(component, WM8960_ADDCTL1, reg | div);
1282                 break;
1283         default:
1284                 return -EINVAL;
1285         }
1286
1287         return 0;
1288 }
1289
1290 static int wm8960_set_bias_level(struct snd_soc_component *component,
1291                                  enum snd_soc_bias_level level)
1292 {
1293         struct wm8960_priv *wm8960 = snd_soc_component_get_drvdata(component);
1294
1295         return wm8960->set_bias_level(component, level);
1296 }
1297
1298 static int wm8960_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id,
1299                                         unsigned int freq, int dir)
1300 {
1301         struct snd_soc_component *component = dai->component;
1302         struct wm8960_priv *wm8960 = snd_soc_component_get_drvdata(component);
1303
1304         switch (clk_id) {
1305         case WM8960_SYSCLK_MCLK:
1306                 snd_soc_component_update_bits(component, WM8960_CLOCK1,
1307                                         0x1, WM8960_SYSCLK_MCLK);
1308                 break;
1309         case WM8960_SYSCLK_PLL:
1310                 snd_soc_component_update_bits(component, WM8960_CLOCK1,
1311                                         0x1, WM8960_SYSCLK_PLL);
1312                 break;
1313         case WM8960_SYSCLK_AUTO:
1314                 break;
1315         default:
1316                 return -EINVAL;
1317         }
1318
1319         wm8960->sysclk = freq;
1320         wm8960->clk_id = clk_id;
1321
1322         return 0;
1323 }
1324
1325 #define WM8960_RATES SNDRV_PCM_RATE_8000_48000
1326
1327 #define WM8960_FORMATS \
1328         (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
1329         SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
1330
1331 static const struct snd_soc_dai_ops wm8960_dai_ops = {
1332         .hw_params = wm8960_hw_params,
1333         .hw_free = wm8960_hw_free,
1334         .digital_mute = wm8960_mute,
1335         .set_fmt = wm8960_set_dai_fmt,
1336         .set_clkdiv = wm8960_set_dai_clkdiv,
1337         .set_pll = wm8960_set_dai_pll,
1338         .set_sysclk = wm8960_set_dai_sysclk,
1339 };
1340
1341 static struct snd_soc_dai_driver wm8960_dai = {
1342         .name = "wm8960-hifi",
1343         .playback = {
1344                 .stream_name = "Playback",
1345                 .channels_min = 1,
1346                 .channels_max = 2,
1347                 .rates = WM8960_RATES,
1348                 .formats = WM8960_FORMATS,},
1349         .capture = {
1350                 .stream_name = "Capture",
1351                 .channels_min = 1,
1352                 .channels_max = 2,
1353                 .rates = WM8960_RATES,
1354                 .formats = WM8960_FORMATS,},
1355         .ops = &wm8960_dai_ops,
1356         .symmetric_rates = 1,
1357 };
1358
1359 static int wm8960_probe(struct snd_soc_component *component)
1360 {
1361         struct wm8960_priv *wm8960 = snd_soc_component_get_drvdata(component);
1362         struct wm8960_data *pdata = &wm8960->pdata;
1363
1364         if (pdata->capless)
1365                 wm8960->set_bias_level = wm8960_set_bias_level_capless;
1366         else
1367                 wm8960->set_bias_level = wm8960_set_bias_level_out3;
1368
1369         snd_soc_add_component_controls(component, wm8960_snd_controls,
1370                                      ARRAY_SIZE(wm8960_snd_controls));
1371         wm8960_add_widgets(component);
1372
1373         return 0;
1374 }
1375
1376 static const struct snd_soc_component_driver soc_component_dev_wm8960 = {
1377         .probe                  = wm8960_probe,
1378         .set_bias_level         = wm8960_set_bias_level,
1379         .suspend_bias_off       = 1,
1380         .idle_bias_on           = 1,
1381         .use_pmdown_time        = 1,
1382         .endianness             = 1,
1383         .non_legacy_dai_naming  = 1,
1384 };
1385
1386 static const struct regmap_config wm8960_regmap = {
1387         .reg_bits = 7,
1388         .val_bits = 9,
1389         .max_register = WM8960_PLL4,
1390
1391         .reg_defaults = wm8960_reg_defaults,
1392         .num_reg_defaults = ARRAY_SIZE(wm8960_reg_defaults),
1393         .cache_type = REGCACHE_RBTREE,
1394
1395         .volatile_reg = wm8960_volatile,
1396 };
1397
1398 static void wm8960_set_pdata_from_of(struct i2c_client *i2c,
1399                                 struct wm8960_data *pdata)
1400 {
1401         const struct device_node *np = i2c->dev.of_node;
1402
1403         if (of_property_read_bool(np, "wlf,capless"))
1404                 pdata->capless = true;
1405
1406         if (of_property_read_bool(np, "wlf,shared-lrclk"))
1407                 pdata->shared_lrclk = true;
1408 }
1409
1410 static int wm8960_i2c_probe(struct i2c_client *i2c,
1411                             const struct i2c_device_id *id)
1412 {
1413         struct wm8960_data *pdata = dev_get_platdata(&i2c->dev);
1414         struct wm8960_priv *wm8960;
1415         int ret;
1416
1417         wm8960 = devm_kzalloc(&i2c->dev, sizeof(struct wm8960_priv),
1418                               GFP_KERNEL);
1419         if (wm8960 == NULL)
1420                 return -ENOMEM;
1421
1422         wm8960->mclk = devm_clk_get(&i2c->dev, "mclk");
1423         if (IS_ERR(wm8960->mclk)) {
1424                 if (PTR_ERR(wm8960->mclk) == -EPROBE_DEFER)
1425                         return -EPROBE_DEFER;
1426         }
1427
1428         wm8960->regmap = devm_regmap_init_i2c(i2c, &wm8960_regmap);
1429         if (IS_ERR(wm8960->regmap))
1430                 return PTR_ERR(wm8960->regmap);
1431
1432         if (pdata)
1433                 memcpy(&wm8960->pdata, pdata, sizeof(struct wm8960_data));
1434         else if (i2c->dev.of_node)
1435                 wm8960_set_pdata_from_of(i2c, &wm8960->pdata);
1436
1437         ret = wm8960_reset(wm8960->regmap);
1438         if (ret != 0) {
1439                 dev_err(&i2c->dev, "Failed to issue reset\n");
1440                 return ret;
1441         }
1442
1443         if (wm8960->pdata.shared_lrclk) {
1444                 ret = regmap_update_bits(wm8960->regmap, WM8960_ADDCTL2,
1445                                          0x4, 0x4);
1446                 if (ret != 0) {
1447                         dev_err(&i2c->dev, "Failed to enable LRCM: %d\n",
1448                                 ret);
1449                         return ret;
1450                 }
1451         }
1452
1453         /* Latch the update bits */
1454         regmap_update_bits(wm8960->regmap, WM8960_LINVOL, 0x100, 0x100);
1455         regmap_update_bits(wm8960->regmap, WM8960_RINVOL, 0x100, 0x100);
1456         regmap_update_bits(wm8960->regmap, WM8960_LADC, 0x100, 0x100);
1457         regmap_update_bits(wm8960->regmap, WM8960_RADC, 0x100, 0x100);
1458         regmap_update_bits(wm8960->regmap, WM8960_LDAC, 0x100, 0x100);
1459         regmap_update_bits(wm8960->regmap, WM8960_RDAC, 0x100, 0x100);
1460         regmap_update_bits(wm8960->regmap, WM8960_LOUT1, 0x100, 0x100);
1461         regmap_update_bits(wm8960->regmap, WM8960_ROUT1, 0x100, 0x100);
1462         regmap_update_bits(wm8960->regmap, WM8960_LOUT2, 0x100, 0x100);
1463         regmap_update_bits(wm8960->regmap, WM8960_ROUT2, 0x100, 0x100);
1464
1465         i2c_set_clientdata(i2c, wm8960);
1466
1467         ret = devm_snd_soc_register_component(&i2c->dev,
1468                         &soc_component_dev_wm8960, &wm8960_dai, 1);
1469
1470         return ret;
1471 }
1472
1473 static int wm8960_i2c_remove(struct i2c_client *client)
1474 {
1475         return 0;
1476 }
1477
1478 static const struct i2c_device_id wm8960_i2c_id[] = {
1479         { "wm8960", 0 },
1480         { }
1481 };
1482 MODULE_DEVICE_TABLE(i2c, wm8960_i2c_id);
1483
1484 static const struct of_device_id wm8960_of_match[] = {
1485        { .compatible = "wlf,wm8960", },
1486        { }
1487 };
1488 MODULE_DEVICE_TABLE(of, wm8960_of_match);
1489
1490 static struct i2c_driver wm8960_i2c_driver = {
1491         .driver = {
1492                 .name = "wm8960",
1493                 .of_match_table = wm8960_of_match,
1494         },
1495         .probe =    wm8960_i2c_probe,
1496         .remove =   wm8960_i2c_remove,
1497         .id_table = wm8960_i2c_id,
1498 };
1499
1500 module_i2c_driver(wm8960_i2c_driver);
1501
1502 MODULE_DESCRIPTION("ASoC WM8960 driver");
1503 MODULE_AUTHOR("Liam Girdwood");
1504 MODULE_LICENSE("GPL");