blob: 4ea64d6e68e2bfd8a6cb8c8984b334c2b9f8f227 [file] [log] [blame]
Mark Brown74dc55e2009-06-09 09:55:51 +01001/*
2 * wm8961.c -- WM8961 ALSA SoC Audio driver
3 *
Mark Brown656baae2012-05-23 12:39:07 +01004 * Copyright 2009-10 Wolfson Microelectronics, plc
5 *
Mark Brown74dc55e2009-06-09 09:55:51 +01006 * Author: Mark Brown
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 * Currently unimplemented features:
13 * - ALC
14 */
15
16#include <linux/module.h>
17#include <linux/moduleparam.h>
18#include <linux/init.h>
19#include <linux/delay.h>
20#include <linux/pm.h>
21#include <linux/i2c.h>
Mark Brown35ecf7c2012-09-13 12:53:59 +080022#include <linux/regmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Mark Brown74dc55e2009-06-09 09:55:51 +010024#include <sound/core.h>
25#include <sound/pcm.h>
26#include <sound/pcm_params.h>
27#include <sound/soc.h>
Mark Brown74dc55e2009-06-09 09:55:51 +010028#include <sound/initval.h>
29#include <sound/tlv.h>
30
31#include "wm8961.h"
32
33#define WM8961_MAX_REGISTER 0xFC
34
Mark Brown35ecf7c2012-09-13 12:53:59 +080035static const struct reg_default wm8961_reg_defaults[] = {
36 { 0, 0x009F }, /* R0 - Left Input volume */
37 { 1, 0x009F }, /* R1 - Right Input volume */
38 { 2, 0x0000 }, /* R2 - LOUT1 volume */
39 { 3, 0x0000 }, /* R3 - ROUT1 volume */
40 { 4, 0x0020 }, /* R4 - Clocking1 */
41 { 5, 0x0008 }, /* R5 - ADC & DAC Control 1 */
42 { 6, 0x0000 }, /* R6 - ADC & DAC Control 2 */
43 { 7, 0x000A }, /* R7 - Audio Interface 0 */
44 { 8, 0x01F4 }, /* R8 - Clocking2 */
45 { 9, 0x0000 }, /* R9 - Audio Interface 1 */
46 { 10, 0x00FF }, /* R10 - Left DAC volume */
47 { 11, 0x00FF }, /* R11 - Right DAC volume */
48
49 { 14, 0x0040 }, /* R14 - Audio Interface 2 */
50
51 { 17, 0x007B }, /* R17 - ALC1 */
52 { 18, 0x0000 }, /* R18 - ALC2 */
53 { 19, 0x0032 }, /* R19 - ALC3 */
54 { 20, 0x0000 }, /* R20 - Noise Gate */
55 { 21, 0x00C0 }, /* R21 - Left ADC volume */
56 { 22, 0x00C0 }, /* R22 - Right ADC volume */
57 { 23, 0x0120 }, /* R23 - Additional control(1) */
58 { 24, 0x0000 }, /* R24 - Additional control(2) */
59 { 25, 0x0000 }, /* R25 - Pwr Mgmt (1) */
60 { 26, 0x0000 }, /* R26 - Pwr Mgmt (2) */
61 { 27, 0x0000 }, /* R27 - Additional Control (3) */
62 { 28, 0x0000 }, /* R28 - Anti-pop */
63
64 { 30, 0x005F }, /* R30 - Clocking 3 */
65
66 { 32, 0x0000 }, /* R32 - ADCL signal path */
67 { 33, 0x0000 }, /* R33 - ADCR signal path */
68
69 { 40, 0x0000 }, /* R40 - LOUT2 volume */
70 { 41, 0x0000 }, /* R41 - ROUT2 volume */
71
72 { 47, 0x0000 }, /* R47 - Pwr Mgmt (3) */
73 { 48, 0x0023 }, /* R48 - Additional Control (4) */
74 { 49, 0x0000 }, /* R49 - Class D Control 1 */
75
76 { 51, 0x0003 }, /* R51 - Class D Control 2 */
77
78 { 56, 0x0106 }, /* R56 - Clocking 4 */
79 { 57, 0x0000 }, /* R57 - DSP Sidetone 0 */
80 { 58, 0x0000 }, /* R58 - DSP Sidetone 1 */
81
82 { 60, 0x0000 }, /* R60 - DC Servo 0 */
83 { 61, 0x0000 }, /* R61 - DC Servo 1 */
84
85 { 63, 0x015E }, /* R63 - DC Servo 3 */
86
87 { 65, 0x0010 }, /* R65 - DC Servo 5 */
88
89 { 68, 0x0003 }, /* R68 - Analogue PGA Bias */
90 { 69, 0x0000 }, /* R69 - Analogue HP 0 */
91
92 { 71, 0x01FB }, /* R71 - Analogue HP 2 */
93 { 72, 0x0000 }, /* R72 - Charge Pump 1 */
94
95 { 82, 0x0000 }, /* R82 - Charge Pump B */
96
97 { 87, 0x0000 }, /* R87 - Write Sequencer 1 */
98 { 88, 0x0000 }, /* R88 - Write Sequencer 2 */
99 { 89, 0x0000 }, /* R89 - Write Sequencer 3 */
100 { 90, 0x0000 }, /* R90 - Write Sequencer 4 */
101 { 91, 0x0000 }, /* R91 - Write Sequencer 5 */
102 { 92, 0x0000 }, /* R92 - Write Sequencer 6 */
103 { 93, 0x0000 }, /* R93 - Write Sequencer 7 */
104
105 { 252, 0x0001 }, /* R252 - General test 1 */
Mark Brown74dc55e2009-06-09 09:55:51 +0100106};
107
108struct wm8961_priv {
Mark Brown35ecf7c2012-09-13 12:53:59 +0800109 struct regmap *regmap;
Mark Brown74dc55e2009-06-09 09:55:51 +0100110 int sysclk;
Mark Brown74dc55e2009-06-09 09:55:51 +0100111};
112
Mark Brown35ecf7c2012-09-13 12:53:59 +0800113static bool wm8961_volatile(struct device *dev, unsigned int reg)
Mark Brown74dc55e2009-06-09 09:55:51 +0100114{
115 switch (reg) {
Mark Brown8d50e442009-07-10 23:12:01 +0100116 case WM8961_SOFTWARE_RESET:
Mark Brown74dc55e2009-06-09 09:55:51 +0100117 case WM8961_WRITE_SEQUENCER_7:
118 case WM8961_DC_SERVO_1:
Mark Brown35ecf7c2012-09-13 12:53:59 +0800119 return true;
Mark Brown74dc55e2009-06-09 09:55:51 +0100120
121 default:
Mark Brown35ecf7c2012-09-13 12:53:59 +0800122 return false;
123 }
124}
125
126static bool wm8961_readable(struct device *dev, unsigned int reg)
127{
128 switch (reg) {
129 case WM8961_LEFT_INPUT_VOLUME:
130 case WM8961_RIGHT_INPUT_VOLUME:
131 case WM8961_LOUT1_VOLUME:
132 case WM8961_ROUT1_VOLUME:
133 case WM8961_CLOCKING1:
134 case WM8961_ADC_DAC_CONTROL_1:
135 case WM8961_ADC_DAC_CONTROL_2:
136 case WM8961_AUDIO_INTERFACE_0:
137 case WM8961_CLOCKING2:
138 case WM8961_AUDIO_INTERFACE_1:
139 case WM8961_LEFT_DAC_VOLUME:
140 case WM8961_RIGHT_DAC_VOLUME:
141 case WM8961_AUDIO_INTERFACE_2:
142 case WM8961_SOFTWARE_RESET:
143 case WM8961_ALC1:
144 case WM8961_ALC2:
145 case WM8961_ALC3:
146 case WM8961_NOISE_GATE:
147 case WM8961_LEFT_ADC_VOLUME:
148 case WM8961_RIGHT_ADC_VOLUME:
149 case WM8961_ADDITIONAL_CONTROL_1:
150 case WM8961_ADDITIONAL_CONTROL_2:
151 case WM8961_PWR_MGMT_1:
152 case WM8961_PWR_MGMT_2:
153 case WM8961_ADDITIONAL_CONTROL_3:
154 case WM8961_ANTI_POP:
155 case WM8961_CLOCKING_3:
156 case WM8961_ADCL_SIGNAL_PATH:
157 case WM8961_ADCR_SIGNAL_PATH:
158 case WM8961_LOUT2_VOLUME:
159 case WM8961_ROUT2_VOLUME:
160 case WM8961_PWR_MGMT_3:
161 case WM8961_ADDITIONAL_CONTROL_4:
162 case WM8961_CLASS_D_CONTROL_1:
163 case WM8961_CLASS_D_CONTROL_2:
164 case WM8961_CLOCKING_4:
165 case WM8961_DSP_SIDETONE_0:
166 case WM8961_DSP_SIDETONE_1:
167 case WM8961_DC_SERVO_0:
168 case WM8961_DC_SERVO_1:
169 case WM8961_DC_SERVO_3:
170 case WM8961_DC_SERVO_5:
171 case WM8961_ANALOGUE_PGA_BIAS:
172 case WM8961_ANALOGUE_HP_0:
173 case WM8961_ANALOGUE_HP_2:
174 case WM8961_CHARGE_PUMP_1:
175 case WM8961_CHARGE_PUMP_B:
176 case WM8961_WRITE_SEQUENCER_1:
177 case WM8961_WRITE_SEQUENCER_2:
178 case WM8961_WRITE_SEQUENCER_3:
179 case WM8961_WRITE_SEQUENCER_4:
180 case WM8961_WRITE_SEQUENCER_5:
181 case WM8961_WRITE_SEQUENCER_6:
182 case WM8961_WRITE_SEQUENCER_7:
183 case WM8961_GENERAL_TEST_1:
184 return true;
185 default:
186 return false;
Mark Brown74dc55e2009-06-09 09:55:51 +0100187 }
188}
189
Mark Brown74dc55e2009-06-09 09:55:51 +0100190static int wm8961_reset(struct snd_soc_codec *codec)
191{
Mark Brown8d50e442009-07-10 23:12:01 +0100192 return snd_soc_write(codec, WM8961_SOFTWARE_RESET, 0);
Mark Brown74dc55e2009-06-09 09:55:51 +0100193}
194
195/*
196 * The headphone output supports special anti-pop sequences giving
197 * silent power up and power down.
198 */
199static int wm8961_hp_event(struct snd_soc_dapm_widget *w,
200 struct snd_kcontrol *kcontrol, int event)
201{
202 struct snd_soc_codec *codec = w->codec;
Mark Brown8d50e442009-07-10 23:12:01 +0100203 u16 hp_reg = snd_soc_read(codec, WM8961_ANALOGUE_HP_0);
204 u16 cp_reg = snd_soc_read(codec, WM8961_CHARGE_PUMP_1);
205 u16 pwr_reg = snd_soc_read(codec, WM8961_PWR_MGMT_2);
206 u16 dcs_reg = snd_soc_read(codec, WM8961_DC_SERVO_1);
Mark Brown74dc55e2009-06-09 09:55:51 +0100207 int timeout = 500;
208
209 if (event & SND_SOC_DAPM_POST_PMU) {
210 /* Make sure the output is shorted */
211 hp_reg &= ~(WM8961_HPR_RMV_SHORT | WM8961_HPL_RMV_SHORT);
Mark Brown8d50e442009-07-10 23:12:01 +0100212 snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100213
214 /* Enable the charge pump */
215 cp_reg |= WM8961_CP_ENA;
Mark Brown8d50e442009-07-10 23:12:01 +0100216 snd_soc_write(codec, WM8961_CHARGE_PUMP_1, cp_reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100217 mdelay(5);
218
219 /* Enable the PGA */
220 pwr_reg |= WM8961_LOUT1_PGA | WM8961_ROUT1_PGA;
Mark Brown8d50e442009-07-10 23:12:01 +0100221 snd_soc_write(codec, WM8961_PWR_MGMT_2, pwr_reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100222
223 /* Enable the amplifier */
224 hp_reg |= WM8961_HPR_ENA | WM8961_HPL_ENA;
Mark Brown8d50e442009-07-10 23:12:01 +0100225 snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100226
227 /* Second stage enable */
228 hp_reg |= WM8961_HPR_ENA_DLY | WM8961_HPL_ENA_DLY;
Mark Brown8d50e442009-07-10 23:12:01 +0100229 snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100230
231 /* Enable the DC servo & trigger startup */
232 dcs_reg |=
233 WM8961_DCS_ENA_CHAN_HPR | WM8961_DCS_TRIG_STARTUP_HPR |
234 WM8961_DCS_ENA_CHAN_HPL | WM8961_DCS_TRIG_STARTUP_HPL;
235 dev_dbg(codec->dev, "Enabling DC servo\n");
236
Mark Brown8d50e442009-07-10 23:12:01 +0100237 snd_soc_write(codec, WM8961_DC_SERVO_1, dcs_reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100238 do {
239 msleep(1);
Mark Brown8d50e442009-07-10 23:12:01 +0100240 dcs_reg = snd_soc_read(codec, WM8961_DC_SERVO_1);
Mark Brown74dc55e2009-06-09 09:55:51 +0100241 } while (--timeout &&
242 dcs_reg & (WM8961_DCS_TRIG_STARTUP_HPR |
243 WM8961_DCS_TRIG_STARTUP_HPL));
244 if (dcs_reg & (WM8961_DCS_TRIG_STARTUP_HPR |
245 WM8961_DCS_TRIG_STARTUP_HPL))
246 dev_err(codec->dev, "DC servo timed out\n");
247 else
248 dev_dbg(codec->dev, "DC servo startup complete\n");
249
250 /* Enable the output stage */
251 hp_reg |= WM8961_HPR_ENA_OUTP | WM8961_HPL_ENA_OUTP;
Mark Brown8d50e442009-07-10 23:12:01 +0100252 snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100253
254 /* Remove the short on the output stage */
255 hp_reg |= WM8961_HPR_RMV_SHORT | WM8961_HPL_RMV_SHORT;
Mark Brown8d50e442009-07-10 23:12:01 +0100256 snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100257 }
258
259 if (event & SND_SOC_DAPM_PRE_PMD) {
260 /* Short the output */
261 hp_reg &= ~(WM8961_HPR_RMV_SHORT | WM8961_HPL_RMV_SHORT);
Mark Brown8d50e442009-07-10 23:12:01 +0100262 snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100263
264 /* Disable the output stage */
265 hp_reg &= ~(WM8961_HPR_ENA_OUTP | WM8961_HPL_ENA_OUTP);
Mark Brown8d50e442009-07-10 23:12:01 +0100266 snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100267
268 /* Disable DC offset cancellation */
269 dcs_reg &= ~(WM8961_DCS_ENA_CHAN_HPR |
270 WM8961_DCS_ENA_CHAN_HPL);
Mark Brown8d50e442009-07-10 23:12:01 +0100271 snd_soc_write(codec, WM8961_DC_SERVO_1, dcs_reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100272
273 /* Finish up */
274 hp_reg &= ~(WM8961_HPR_ENA_DLY | WM8961_HPR_ENA |
275 WM8961_HPL_ENA_DLY | WM8961_HPL_ENA);
Mark Brown8d50e442009-07-10 23:12:01 +0100276 snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100277
278 /* Disable the PGA */
279 pwr_reg &= ~(WM8961_LOUT1_PGA | WM8961_ROUT1_PGA);
Mark Brown8d50e442009-07-10 23:12:01 +0100280 snd_soc_write(codec, WM8961_PWR_MGMT_2, pwr_reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100281
282 /* Disable the charge pump */
283 dev_dbg(codec->dev, "Disabling charge pump\n");
Mark Brown8d50e442009-07-10 23:12:01 +0100284 snd_soc_write(codec, WM8961_CHARGE_PUMP_1,
Mark Brown74dc55e2009-06-09 09:55:51 +0100285 cp_reg & ~WM8961_CP_ENA);
286 }
287
288 return 0;
289}
290
291static int wm8961_spk_event(struct snd_soc_dapm_widget *w,
292 struct snd_kcontrol *kcontrol, int event)
293{
294 struct snd_soc_codec *codec = w->codec;
Mark Brown8d50e442009-07-10 23:12:01 +0100295 u16 pwr_reg = snd_soc_read(codec, WM8961_PWR_MGMT_2);
296 u16 spk_reg = snd_soc_read(codec, WM8961_CLASS_D_CONTROL_1);
Mark Brown74dc55e2009-06-09 09:55:51 +0100297
298 if (event & SND_SOC_DAPM_POST_PMU) {
299 /* Enable the PGA */
300 pwr_reg |= WM8961_SPKL_PGA | WM8961_SPKR_PGA;
Mark Brown8d50e442009-07-10 23:12:01 +0100301 snd_soc_write(codec, WM8961_PWR_MGMT_2, pwr_reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100302
303 /* Enable the amplifier */
304 spk_reg |= WM8961_SPKL_ENA | WM8961_SPKR_ENA;
Mark Brown8d50e442009-07-10 23:12:01 +0100305 snd_soc_write(codec, WM8961_CLASS_D_CONTROL_1, spk_reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100306 }
307
308 if (event & SND_SOC_DAPM_PRE_PMD) {
Axel Lin7fcadfd2011-12-09 18:43:20 +0800309 /* Disable the amplifier */
Mark Brown74dc55e2009-06-09 09:55:51 +0100310 spk_reg &= ~(WM8961_SPKL_ENA | WM8961_SPKR_ENA);
Mark Brown8d50e442009-07-10 23:12:01 +0100311 snd_soc_write(codec, WM8961_CLASS_D_CONTROL_1, spk_reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100312
Axel Lin7fcadfd2011-12-09 18:43:20 +0800313 /* Disable the PGA */
Mark Brown74dc55e2009-06-09 09:55:51 +0100314 pwr_reg &= ~(WM8961_SPKL_PGA | WM8961_SPKR_PGA);
Mark Brown8d50e442009-07-10 23:12:01 +0100315 snd_soc_write(codec, WM8961_PWR_MGMT_2, pwr_reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100316 }
317
318 return 0;
319}
320
321static const char *adc_hpf_text[] = {
322 "Hi-fi", "Voice 1", "Voice 2", "Voice 3",
323};
324
325static const struct soc_enum adc_hpf =
326 SOC_ENUM_SINGLE(WM8961_ADC_DAC_CONTROL_2, 7, 4, adc_hpf_text);
327
328static const char *dac_deemph_text[] = {
329 "None", "32kHz", "44.1kHz", "48kHz",
330};
331
332static const struct soc_enum dac_deemph =
333 SOC_ENUM_SINGLE(WM8961_ADC_DAC_CONTROL_1, 1, 4, dac_deemph_text);
334
335static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
336static const DECLARE_TLV_DB_SCALE(hp_sec_tlv, -700, 100, 0);
337static const DECLARE_TLV_DB_SCALE(adc_tlv, -7200, 75, 1);
338static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -3600, 300, 0);
339static unsigned int boost_tlv[] = {
340 TLV_DB_RANGE_HEAD(4),
341 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
342 1, 1, TLV_DB_SCALE_ITEM(13, 0, 0),
343 2, 2, TLV_DB_SCALE_ITEM(20, 0, 0),
344 3, 3, TLV_DB_SCALE_ITEM(29, 0, 0),
345};
346static const DECLARE_TLV_DB_SCALE(pga_tlv, -2325, 75, 0);
347
348static const struct snd_kcontrol_new wm8961_snd_controls[] = {
349SOC_DOUBLE_R_TLV("Headphone Volume", WM8961_LOUT1_VOLUME, WM8961_ROUT1_VOLUME,
350 0, 127, 0, out_tlv),
351SOC_DOUBLE_TLV("Headphone Secondary Volume", WM8961_ANALOGUE_HP_2,
352 6, 3, 7, 0, hp_sec_tlv),
353SOC_DOUBLE_R("Headphone ZC Switch", WM8961_LOUT1_VOLUME, WM8961_ROUT1_VOLUME,
354 7, 1, 0),
355
356SOC_DOUBLE_R_TLV("Speaker Volume", WM8961_LOUT2_VOLUME, WM8961_ROUT2_VOLUME,
357 0, 127, 0, out_tlv),
358SOC_DOUBLE_R("Speaker ZC Switch", WM8961_LOUT2_VOLUME, WM8961_ROUT2_VOLUME,
359 7, 1, 0),
360SOC_SINGLE("Speaker AC Gain", WM8961_CLASS_D_CONTROL_2, 0, 7, 0),
361
362SOC_SINGLE("DAC x128 OSR Switch", WM8961_ADC_DAC_CONTROL_2, 0, 1, 0),
363SOC_ENUM("DAC Deemphasis", dac_deemph),
364SOC_SINGLE("DAC Soft Mute Switch", WM8961_ADC_DAC_CONTROL_2, 3, 1, 0),
365
366SOC_DOUBLE_R_TLV("Sidetone Volume", WM8961_DSP_SIDETONE_0,
367 WM8961_DSP_SIDETONE_1, 4, 12, 0, sidetone_tlv),
368
369SOC_SINGLE("ADC High Pass Filter Switch", WM8961_ADC_DAC_CONTROL_1, 0, 1, 0),
370SOC_ENUM("ADC High Pass Filter Mode", adc_hpf),
371
372SOC_DOUBLE_R_TLV("Capture Volume",
373 WM8961_LEFT_ADC_VOLUME, WM8961_RIGHT_ADC_VOLUME,
374 1, 119, 0, adc_tlv),
375SOC_DOUBLE_R_TLV("Capture Boost Volume",
376 WM8961_ADCL_SIGNAL_PATH, WM8961_ADCR_SIGNAL_PATH,
377 4, 3, 0, boost_tlv),
378SOC_DOUBLE_R_TLV("Capture PGA Volume",
379 WM8961_LEFT_INPUT_VOLUME, WM8961_RIGHT_INPUT_VOLUME,
380 0, 62, 0, pga_tlv),
381SOC_DOUBLE_R("Capture PGA ZC Switch",
382 WM8961_LEFT_INPUT_VOLUME, WM8961_RIGHT_INPUT_VOLUME,
383 6, 1, 1),
384SOC_DOUBLE_R("Capture PGA Switch",
385 WM8961_LEFT_INPUT_VOLUME, WM8961_RIGHT_INPUT_VOLUME,
386 7, 1, 1),
387};
388
389static const char *sidetone_text[] = {
390 "None", "Left", "Right"
391};
392
393static const struct soc_enum dacl_sidetone =
394 SOC_ENUM_SINGLE(WM8961_DSP_SIDETONE_0, 2, 3, sidetone_text);
395
396static const struct soc_enum dacr_sidetone =
397 SOC_ENUM_SINGLE(WM8961_DSP_SIDETONE_1, 2, 3, sidetone_text);
398
399static const struct snd_kcontrol_new dacl_mux =
400 SOC_DAPM_ENUM("DACL Sidetone", dacl_sidetone);
401
402static const struct snd_kcontrol_new dacr_mux =
403 SOC_DAPM_ENUM("DACR Sidetone", dacr_sidetone);
404
405static const struct snd_soc_dapm_widget wm8961_dapm_widgets[] = {
406SND_SOC_DAPM_INPUT("LINPUT"),
407SND_SOC_DAPM_INPUT("RINPUT"),
408
409SND_SOC_DAPM_SUPPLY("CLK_DSP", WM8961_CLOCKING2, 4, 0, NULL, 0),
410
411SND_SOC_DAPM_PGA("Left Input", WM8961_PWR_MGMT_1, 5, 0, NULL, 0),
412SND_SOC_DAPM_PGA("Right Input", WM8961_PWR_MGMT_1, 4, 0, NULL, 0),
413
414SND_SOC_DAPM_ADC("ADCL", "HiFi Capture", WM8961_PWR_MGMT_1, 3, 0),
415SND_SOC_DAPM_ADC("ADCR", "HiFi Capture", WM8961_PWR_MGMT_1, 2, 0),
416
Mark Brown20abf082011-10-27 09:46:32 +0200417SND_SOC_DAPM_SUPPLY("MICBIAS", WM8961_PWR_MGMT_1, 1, 0, NULL, 0),
Mark Brown74dc55e2009-06-09 09:55:51 +0100418
419SND_SOC_DAPM_MUX("DACL Sidetone", SND_SOC_NOPM, 0, 0, &dacl_mux),
420SND_SOC_DAPM_MUX("DACR Sidetone", SND_SOC_NOPM, 0, 0, &dacr_mux),
421
422SND_SOC_DAPM_DAC("DACL", "HiFi Playback", WM8961_PWR_MGMT_2, 8, 0),
423SND_SOC_DAPM_DAC("DACR", "HiFi Playback", WM8961_PWR_MGMT_2, 7, 0),
424
425/* Handle as a mono path for DCS */
426SND_SOC_DAPM_PGA_E("Headphone Output", SND_SOC_NOPM,
427 4, 0, NULL, 0, wm8961_hp_event,
428 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
429SND_SOC_DAPM_PGA_E("Speaker Output", SND_SOC_NOPM,
430 4, 0, NULL, 0, wm8961_spk_event,
431 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
432
433SND_SOC_DAPM_OUTPUT("HP_L"),
434SND_SOC_DAPM_OUTPUT("HP_R"),
435SND_SOC_DAPM_OUTPUT("SPK_LN"),
436SND_SOC_DAPM_OUTPUT("SPK_LP"),
437SND_SOC_DAPM_OUTPUT("SPK_RN"),
438SND_SOC_DAPM_OUTPUT("SPK_RP"),
439};
440
441
442static const struct snd_soc_dapm_route audio_paths[] = {
443 { "DACL", NULL, "CLK_DSP" },
444 { "DACL", NULL, "DACL Sidetone" },
445 { "DACR", NULL, "CLK_DSP" },
446 { "DACR", NULL, "DACR Sidetone" },
447
448 { "DACL Sidetone", "Left", "ADCL" },
449 { "DACL Sidetone", "Right", "ADCR" },
450
451 { "DACR Sidetone", "Left", "ADCL" },
452 { "DACR Sidetone", "Right", "ADCR" },
453
454 { "HP_L", NULL, "Headphone Output" },
455 { "HP_R", NULL, "Headphone Output" },
456 { "Headphone Output", NULL, "DACL" },
457 { "Headphone Output", NULL, "DACR" },
458
459 { "SPK_LN", NULL, "Speaker Output" },
460 { "SPK_LP", NULL, "Speaker Output" },
461 { "SPK_RN", NULL, "Speaker Output" },
462 { "SPK_RP", NULL, "Speaker Output" },
463
464 { "Speaker Output", NULL, "DACL" },
465 { "Speaker Output", NULL, "DACR" },
466
467 { "ADCL", NULL, "Left Input" },
468 { "ADCL", NULL, "CLK_DSP" },
469 { "ADCR", NULL, "Right Input" },
470 { "ADCR", NULL, "CLK_DSP" },
471
472 { "Left Input", NULL, "LINPUT" },
473 { "Right Input", NULL, "RINPUT" },
474
475};
476
477/* Values for CLK_SYS_RATE */
478static struct {
479 int ratio;
480 u16 val;
481} wm8961_clk_sys_ratio[] = {
482 { 64, 0 },
483 { 128, 1 },
484 { 192, 2 },
485 { 256, 3 },
486 { 384, 4 },
487 { 512, 5 },
488 { 768, 6 },
489 { 1024, 7 },
490 { 1408, 8 },
491 { 1536, 9 },
492};
493
494/* Values for SAMPLE_RATE */
495static struct {
496 int rate;
497 u16 val;
498} wm8961_srate[] = {
499 { 48000, 0 },
500 { 44100, 0 },
501 { 32000, 1 },
502 { 22050, 2 },
503 { 24000, 2 },
504 { 16000, 3 },
505 { 11250, 4 },
506 { 12000, 4 },
507 { 8000, 5 },
508};
509
510static int wm8961_hw_params(struct snd_pcm_substream *substream,
511 struct snd_pcm_hw_params *params,
512 struct snd_soc_dai *dai)
513{
514 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900515 struct wm8961_priv *wm8961 = snd_soc_codec_get_drvdata(codec);
Mark Brown74dc55e2009-06-09 09:55:51 +0100516 int i, best, target, fs;
517 u16 reg;
518
519 fs = params_rate(params);
520
521 if (!wm8961->sysclk) {
522 dev_err(codec->dev, "MCLK has not been specified\n");
523 return -EINVAL;
524 }
525
526 /* Find the closest sample rate for the filters */
527 best = 0;
528 for (i = 0; i < ARRAY_SIZE(wm8961_srate); i++) {
529 if (abs(wm8961_srate[i].rate - fs) <
530 abs(wm8961_srate[best].rate - fs))
531 best = i;
532 }
Mark Brown8d50e442009-07-10 23:12:01 +0100533 reg = snd_soc_read(codec, WM8961_ADDITIONAL_CONTROL_3);
Mark Brown74dc55e2009-06-09 09:55:51 +0100534 reg &= ~WM8961_SAMPLE_RATE_MASK;
535 reg |= wm8961_srate[best].val;
Mark Brown8d50e442009-07-10 23:12:01 +0100536 snd_soc_write(codec, WM8961_ADDITIONAL_CONTROL_3, reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100537 dev_dbg(codec->dev, "Selected SRATE %dHz for %dHz\n",
538 wm8961_srate[best].rate, fs);
539
540 /* Select a CLK_SYS/fs ratio equal to or higher than required */
541 target = wm8961->sysclk / fs;
542
543 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && target < 64) {
544 dev_err(codec->dev,
545 "SYSCLK must be at least 64*fs for DAC\n");
546 return -EINVAL;
547 }
548 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE && target < 256) {
549 dev_err(codec->dev,
550 "SYSCLK must be at least 256*fs for ADC\n");
551 return -EINVAL;
552 }
553
554 for (i = 0; i < ARRAY_SIZE(wm8961_clk_sys_ratio); i++) {
555 if (wm8961_clk_sys_ratio[i].ratio >= target)
556 break;
557 }
558 if (i == ARRAY_SIZE(wm8961_clk_sys_ratio)) {
559 dev_err(codec->dev, "Unable to generate CLK_SYS_RATE\n");
560 return -EINVAL;
561 }
562 dev_dbg(codec->dev, "Selected CLK_SYS_RATE of %d for %d/%d=%d\n",
563 wm8961_clk_sys_ratio[i].ratio, wm8961->sysclk, fs,
564 wm8961->sysclk / fs);
565
Mark Brown8d50e442009-07-10 23:12:01 +0100566 reg = snd_soc_read(codec, WM8961_CLOCKING_4);
Mark Brown74dc55e2009-06-09 09:55:51 +0100567 reg &= ~WM8961_CLK_SYS_RATE_MASK;
568 reg |= wm8961_clk_sys_ratio[i].val << WM8961_CLK_SYS_RATE_SHIFT;
Mark Brown8d50e442009-07-10 23:12:01 +0100569 snd_soc_write(codec, WM8961_CLOCKING_4, reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100570
Mark Brown8d50e442009-07-10 23:12:01 +0100571 reg = snd_soc_read(codec, WM8961_AUDIO_INTERFACE_0);
Mark Brown74dc55e2009-06-09 09:55:51 +0100572 reg &= ~WM8961_WL_MASK;
573 switch (params_format(params)) {
574 case SNDRV_PCM_FORMAT_S16_LE:
575 break;
576 case SNDRV_PCM_FORMAT_S20_3LE:
577 reg |= 1 << WM8961_WL_SHIFT;
578 break;
579 case SNDRV_PCM_FORMAT_S24_LE:
580 reg |= 2 << WM8961_WL_SHIFT;
581 break;
582 case SNDRV_PCM_FORMAT_S32_LE:
583 reg |= 3 << WM8961_WL_SHIFT;
584 break;
585 default:
586 return -EINVAL;
587 }
Mark Brown8d50e442009-07-10 23:12:01 +0100588 snd_soc_write(codec, WM8961_AUDIO_INTERFACE_0, reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100589
590 /* Sloping stop-band filter is recommended for <= 24kHz */
Mark Brown8d50e442009-07-10 23:12:01 +0100591 reg = snd_soc_read(codec, WM8961_ADC_DAC_CONTROL_2);
Mark Brown74dc55e2009-06-09 09:55:51 +0100592 if (fs <= 24000)
593 reg |= WM8961_DACSLOPE;
594 else
Axel Lin08b1a3842010-11-24 10:20:33 +0800595 reg &= ~WM8961_DACSLOPE;
Mark Brown8d50e442009-07-10 23:12:01 +0100596 snd_soc_write(codec, WM8961_ADC_DAC_CONTROL_2, reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100597
598 return 0;
599}
600
601static int wm8961_set_sysclk(struct snd_soc_dai *dai, int clk_id,
602 unsigned int freq,
603 int dir)
604{
605 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900606 struct wm8961_priv *wm8961 = snd_soc_codec_get_drvdata(codec);
Mark Brown8d50e442009-07-10 23:12:01 +0100607 u16 reg = snd_soc_read(codec, WM8961_CLOCKING1);
Mark Brown74dc55e2009-06-09 09:55:51 +0100608
609 if (freq > 33000000) {
610 dev_err(codec->dev, "MCLK must be <33MHz\n");
611 return -EINVAL;
612 }
613
614 if (freq > 16500000) {
615 dev_dbg(codec->dev, "Using MCLK/2 for %dHz MCLK\n", freq);
616 reg |= WM8961_MCLKDIV;
617 freq /= 2;
618 } else {
619 dev_dbg(codec->dev, "Using MCLK/1 for %dHz MCLK\n", freq);
Axel Lin2f7dcee2010-11-24 10:21:54 +0800620 reg &= ~WM8961_MCLKDIV;
Mark Brown74dc55e2009-06-09 09:55:51 +0100621 }
622
Mark Brown8d50e442009-07-10 23:12:01 +0100623 snd_soc_write(codec, WM8961_CLOCKING1, reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100624
625 wm8961->sysclk = freq;
626
627 return 0;
628}
629
630static int wm8961_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
631{
632 struct snd_soc_codec *codec = dai->codec;
Mark Brown8d50e442009-07-10 23:12:01 +0100633 u16 aif = snd_soc_read(codec, WM8961_AUDIO_INTERFACE_0);
Mark Brown74dc55e2009-06-09 09:55:51 +0100634
635 aif &= ~(WM8961_BCLKINV | WM8961_LRP |
636 WM8961_MS | WM8961_FORMAT_MASK);
637
638 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
639 case SND_SOC_DAIFMT_CBM_CFM:
640 aif |= WM8961_MS;
641 break;
642 case SND_SOC_DAIFMT_CBS_CFS:
643 break;
644 default:
645 return -EINVAL;
646 }
647
648 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
649 case SND_SOC_DAIFMT_RIGHT_J:
650 break;
651
652 case SND_SOC_DAIFMT_LEFT_J:
653 aif |= 1;
654 break;
655
656 case SND_SOC_DAIFMT_I2S:
657 aif |= 2;
658 break;
659
660 case SND_SOC_DAIFMT_DSP_B:
661 aif |= WM8961_LRP;
662 case SND_SOC_DAIFMT_DSP_A:
663 aif |= 3;
664 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
665 case SND_SOC_DAIFMT_NB_NF:
666 case SND_SOC_DAIFMT_IB_NF:
667 break;
668 default:
669 return -EINVAL;
670 }
671 break;
672
673 default:
674 return -EINVAL;
675 }
676
677 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
678 case SND_SOC_DAIFMT_NB_NF:
679 break;
680 case SND_SOC_DAIFMT_NB_IF:
681 aif |= WM8961_LRP;
682 break;
683 case SND_SOC_DAIFMT_IB_NF:
684 aif |= WM8961_BCLKINV;
685 break;
686 case SND_SOC_DAIFMT_IB_IF:
687 aif |= WM8961_BCLKINV | WM8961_LRP;
688 break;
689 default:
690 return -EINVAL;
691 }
692
Mark Brown8d50e442009-07-10 23:12:01 +0100693 return snd_soc_write(codec, WM8961_AUDIO_INTERFACE_0, aif);
Mark Brown74dc55e2009-06-09 09:55:51 +0100694}
695
696static int wm8961_set_tristate(struct snd_soc_dai *dai, int tristate)
697{
698 struct snd_soc_codec *codec = dai->codec;
Mark Brown8d50e442009-07-10 23:12:01 +0100699 u16 reg = snd_soc_read(codec, WM8961_ADDITIONAL_CONTROL_2);
Mark Brown74dc55e2009-06-09 09:55:51 +0100700
701 if (tristate)
702 reg |= WM8961_TRIS;
703 else
704 reg &= ~WM8961_TRIS;
705
Mark Brown8d50e442009-07-10 23:12:01 +0100706 return snd_soc_write(codec, WM8961_ADDITIONAL_CONTROL_2, reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100707}
708
709static int wm8961_digital_mute(struct snd_soc_dai *dai, int mute)
710{
711 struct snd_soc_codec *codec = dai->codec;
Mark Brown8d50e442009-07-10 23:12:01 +0100712 u16 reg = snd_soc_read(codec, WM8961_ADC_DAC_CONTROL_1);
Mark Brown74dc55e2009-06-09 09:55:51 +0100713
714 if (mute)
715 reg |= WM8961_DACMU;
716 else
717 reg &= ~WM8961_DACMU;
718
719 msleep(17);
720
Mark Brown8d50e442009-07-10 23:12:01 +0100721 return snd_soc_write(codec, WM8961_ADC_DAC_CONTROL_1, reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100722}
723
724static int wm8961_set_clkdiv(struct snd_soc_dai *dai, int div_id, int div)
725{
726 struct snd_soc_codec *codec = dai->codec;
727 u16 reg;
728
729 switch (div_id) {
730 case WM8961_BCLK:
Mark Brown8d50e442009-07-10 23:12:01 +0100731 reg = snd_soc_read(codec, WM8961_CLOCKING2);
Mark Brown74dc55e2009-06-09 09:55:51 +0100732 reg &= ~WM8961_BCLKDIV_MASK;
733 reg |= div;
Mark Brown8d50e442009-07-10 23:12:01 +0100734 snd_soc_write(codec, WM8961_CLOCKING2, reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100735 break;
736
737 case WM8961_LRCLK:
Mark Brown8d50e442009-07-10 23:12:01 +0100738 reg = snd_soc_read(codec, WM8961_AUDIO_INTERFACE_2);
Mark Brown74dc55e2009-06-09 09:55:51 +0100739 reg &= ~WM8961_LRCLK_RATE_MASK;
740 reg |= div;
Mark Brown8d50e442009-07-10 23:12:01 +0100741 snd_soc_write(codec, WM8961_AUDIO_INTERFACE_2, reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100742 break;
743
744 default:
745 return -EINVAL;
746 }
747
748 return 0;
749}
750
751static int wm8961_set_bias_level(struct snd_soc_codec *codec,
752 enum snd_soc_bias_level level)
753{
754 u16 reg;
755
756 /* This is all slightly unusual since we have no bypass paths
757 * and the output amplifier structure means we can just slam
758 * the biases straight up rather than having to ramp them
759 * slowly.
760 */
761 switch (level) {
762 case SND_SOC_BIAS_ON:
763 break;
764
765 case SND_SOC_BIAS_PREPARE:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200766 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY) {
Mark Brown74dc55e2009-06-09 09:55:51 +0100767 /* Enable bias generation */
Mark Brown8d50e442009-07-10 23:12:01 +0100768 reg = snd_soc_read(codec, WM8961_ANTI_POP);
Mark Brown74dc55e2009-06-09 09:55:51 +0100769 reg |= WM8961_BUFIOEN | WM8961_BUFDCOPEN;
Mark Brown8d50e442009-07-10 23:12:01 +0100770 snd_soc_write(codec, WM8961_ANTI_POP, reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100771
772 /* VMID=2*50k, VREF */
Mark Brown8d50e442009-07-10 23:12:01 +0100773 reg = snd_soc_read(codec, WM8961_PWR_MGMT_1);
Mark Brown74dc55e2009-06-09 09:55:51 +0100774 reg &= ~WM8961_VMIDSEL_MASK;
775 reg |= (1 << WM8961_VMIDSEL_SHIFT) | WM8961_VREF;
Mark Brown8d50e442009-07-10 23:12:01 +0100776 snd_soc_write(codec, WM8961_PWR_MGMT_1, reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100777 }
778 break;
779
780 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200781 if (codec->dapm.bias_level == SND_SOC_BIAS_PREPARE) {
Mark Brown74dc55e2009-06-09 09:55:51 +0100782 /* VREF off */
Mark Brown8d50e442009-07-10 23:12:01 +0100783 reg = snd_soc_read(codec, WM8961_PWR_MGMT_1);
Mark Brown74dc55e2009-06-09 09:55:51 +0100784 reg &= ~WM8961_VREF;
Mark Brown8d50e442009-07-10 23:12:01 +0100785 snd_soc_write(codec, WM8961_PWR_MGMT_1, reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100786
787 /* Bias generation off */
Mark Brown8d50e442009-07-10 23:12:01 +0100788 reg = snd_soc_read(codec, WM8961_ANTI_POP);
Mark Brown74dc55e2009-06-09 09:55:51 +0100789 reg &= ~(WM8961_BUFIOEN | WM8961_BUFDCOPEN);
Mark Brown8d50e442009-07-10 23:12:01 +0100790 snd_soc_write(codec, WM8961_ANTI_POP, reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100791
792 /* VMID off */
Mark Brown8d50e442009-07-10 23:12:01 +0100793 reg = snd_soc_read(codec, WM8961_PWR_MGMT_1);
Mark Brown74dc55e2009-06-09 09:55:51 +0100794 reg &= ~WM8961_VMIDSEL_MASK;
Mark Brown8d50e442009-07-10 23:12:01 +0100795 snd_soc_write(codec, WM8961_PWR_MGMT_1, reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100796 }
797 break;
798
799 case SND_SOC_BIAS_OFF:
800 break;
801 }
802
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200803 codec->dapm.bias_level = level;
Mark Brown74dc55e2009-06-09 09:55:51 +0100804
805 return 0;
806}
807
808
809#define WM8961_RATES SNDRV_PCM_RATE_8000_48000
810
811#define WM8961_FORMATS \
812 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
813 SNDRV_PCM_FMTBIT_S24_LE)
814
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100815static const struct snd_soc_dai_ops wm8961_dai_ops = {
Mark Brown74dc55e2009-06-09 09:55:51 +0100816 .hw_params = wm8961_hw_params,
817 .set_sysclk = wm8961_set_sysclk,
818 .set_fmt = wm8961_set_fmt,
819 .digital_mute = wm8961_digital_mute,
820 .set_tristate = wm8961_set_tristate,
821 .set_clkdiv = wm8961_set_clkdiv,
822};
823
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000824static struct snd_soc_dai_driver wm8961_dai = {
825 .name = "wm8961-hifi",
Mark Brown74dc55e2009-06-09 09:55:51 +0100826 .playback = {
827 .stream_name = "HiFi Playback",
828 .channels_min = 1,
829 .channels_max = 2,
830 .rates = WM8961_RATES,
831 .formats = WM8961_FORMATS,},
832 .capture = {
833 .stream_name = "HiFi Capture",
834 .channels_min = 1,
835 .channels_max = 2,
836 .rates = WM8961_RATES,
837 .formats = WM8961_FORMATS,},
838 .ops = &wm8961_dai_ops,
839};
Mark Brown74dc55e2009-06-09 09:55:51 +0100840
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000841static int wm8961_probe(struct snd_soc_codec *codec)
Mark Brown74dc55e2009-06-09 09:55:51 +0100842{
Mark Brown35ecf7c2012-09-13 12:53:59 +0800843 struct wm8961_priv *wm8961 = snd_soc_codec_get_drvdata(codec);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200844 struct snd_soc_dapm_context *dapm = &codec->dapm;
Mark Brown74dc55e2009-06-09 09:55:51 +0100845 int ret = 0;
Mark Brown74dc55e2009-06-09 09:55:51 +0100846 u16 reg;
847
Mark Brown35ecf7c2012-09-13 12:53:59 +0800848 ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_REGMAP);
Mark Brown8d50e442009-07-10 23:12:01 +0100849 if (ret != 0) {
850 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000851 return ret;
Mark Brown8d50e442009-07-10 23:12:01 +0100852 }
853
854 reg = snd_soc_read(codec, WM8961_SOFTWARE_RESET);
Mark Brown74dc55e2009-06-09 09:55:51 +0100855 if (reg != 0x1801) {
856 dev_err(codec->dev, "Device is not a WM8961: ID=0x%x\n", reg);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000857 return -EINVAL;
Mark Brown74dc55e2009-06-09 09:55:51 +0100858 }
859
Mark Brown8d50e442009-07-10 23:12:01 +0100860 /* This isn't volatile - readback doesn't correspond to write */
Mark Brown35ecf7c2012-09-13 12:53:59 +0800861 regcache_cache_bypass(wm8961->regmap, true);
Axel Lin370f4642011-10-14 09:39:14 +0800862 reg = snd_soc_read(codec, WM8961_RIGHT_INPUT_VOLUME);
Mark Brown35ecf7c2012-09-13 12:53:59 +0800863 regcache_cache_bypass(wm8961->regmap, false);
Mark Brown74dc55e2009-06-09 09:55:51 +0100864 dev_info(codec->dev, "WM8961 family %d revision %c\n",
865 (reg & WM8961_DEVICE_ID_MASK) >> WM8961_DEVICE_ID_SHIFT,
866 ((reg & WM8961_CHIP_REV_MASK) >> WM8961_CHIP_REV_SHIFT)
867 + 'A');
868
869 ret = wm8961_reset(codec);
870 if (ret < 0) {
871 dev_err(codec->dev, "Failed to issue reset\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000872 return ret;
Mark Brown74dc55e2009-06-09 09:55:51 +0100873 }
874
875 /* Enable class W */
Mark Brown8d50e442009-07-10 23:12:01 +0100876 reg = snd_soc_read(codec, WM8961_CHARGE_PUMP_B);
Mark Brown74dc55e2009-06-09 09:55:51 +0100877 reg |= WM8961_CP_DYN_PWR_MASK;
Mark Brown8d50e442009-07-10 23:12:01 +0100878 snd_soc_write(codec, WM8961_CHARGE_PUMP_B, reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100879
880 /* Latch volume update bits (right channel only, we always
881 * write both out) and default ZC on. */
Mark Brown8d50e442009-07-10 23:12:01 +0100882 reg = snd_soc_read(codec, WM8961_ROUT1_VOLUME);
883 snd_soc_write(codec, WM8961_ROUT1_VOLUME,
Mark Brown74dc55e2009-06-09 09:55:51 +0100884 reg | WM8961_LO1ZC | WM8961_OUT1VU);
Mark Brown8d50e442009-07-10 23:12:01 +0100885 snd_soc_write(codec, WM8961_LOUT1_VOLUME, reg | WM8961_LO1ZC);
886 reg = snd_soc_read(codec, WM8961_ROUT2_VOLUME);
887 snd_soc_write(codec, WM8961_ROUT2_VOLUME,
Mark Brown74dc55e2009-06-09 09:55:51 +0100888 reg | WM8961_SPKRZC | WM8961_SPKVU);
Mark Brown8d50e442009-07-10 23:12:01 +0100889 snd_soc_write(codec, WM8961_LOUT2_VOLUME, reg | WM8961_SPKLZC);
Mark Brown74dc55e2009-06-09 09:55:51 +0100890
Mark Brown8d50e442009-07-10 23:12:01 +0100891 reg = snd_soc_read(codec, WM8961_RIGHT_ADC_VOLUME);
892 snd_soc_write(codec, WM8961_RIGHT_ADC_VOLUME, reg | WM8961_ADCVU);
893 reg = snd_soc_read(codec, WM8961_RIGHT_INPUT_VOLUME);
894 snd_soc_write(codec, WM8961_RIGHT_INPUT_VOLUME, reg | WM8961_IPVU);
Mark Brown74dc55e2009-06-09 09:55:51 +0100895
896 /* Use soft mute by default */
Mark Brown8d50e442009-07-10 23:12:01 +0100897 reg = snd_soc_read(codec, WM8961_ADC_DAC_CONTROL_2);
Mark Brown74dc55e2009-06-09 09:55:51 +0100898 reg |= WM8961_DACSMM;
Mark Brown8d50e442009-07-10 23:12:01 +0100899 snd_soc_write(codec, WM8961_ADC_DAC_CONTROL_2, reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100900
901 /* Use automatic clocking mode by default; for now this is all
902 * we support.
903 */
Mark Brown8d50e442009-07-10 23:12:01 +0100904 reg = snd_soc_read(codec, WM8961_CLOCKING_3);
Mark Brown74dc55e2009-06-09 09:55:51 +0100905 reg &= ~WM8961_MANUAL_MODE;
Mark Brown8d50e442009-07-10 23:12:01 +0100906 snd_soc_write(codec, WM8961_CLOCKING_3, reg);
Mark Brown74dc55e2009-06-09 09:55:51 +0100907
908 wm8961_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
909
Liam Girdwood022658b2012-02-03 17:43:09 +0000910 snd_soc_add_codec_controls(codec, wm8961_snd_controls,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000911 ARRAY_SIZE(wm8961_snd_controls));
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200912 snd_soc_dapm_new_controls(dapm, wm8961_dapm_widgets,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000913 ARRAY_SIZE(wm8961_dapm_widgets));
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200914 snd_soc_dapm_add_routes(dapm, audio_paths, ARRAY_SIZE(audio_paths));
Mark Brown74dc55e2009-06-09 09:55:51 +0100915
916 return 0;
Mark Brown74dc55e2009-06-09 09:55:51 +0100917}
918
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000919static int wm8961_remove(struct snd_soc_codec *codec)
Mark Brown74dc55e2009-06-09 09:55:51 +0100920{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000921 wm8961_set_bias_level(codec, SND_SOC_BIAS_OFF);
922 return 0;
Mark Brown74dc55e2009-06-09 09:55:51 +0100923}
924
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000925#ifdef CONFIG_PM
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100926static int wm8961_suspend(struct snd_soc_codec *codec)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000927{
928 wm8961_set_bias_level(codec, SND_SOC_BIAS_OFF);
929
930 return 0;
931}
932
933static int wm8961_resume(struct snd_soc_codec *codec)
934{
Mark Brown202a51a2011-12-16 07:57:11 +0100935 snd_soc_cache_sync(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000936
937 wm8961_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
938
939 return 0;
940}
941#else
942#define wm8961_suspend NULL
943#define wm8961_resume NULL
944#endif
945
946static struct snd_soc_codec_driver soc_codec_dev_wm8961 = {
947 .probe = wm8961_probe,
948 .remove = wm8961_remove,
949 .suspend = wm8961_suspend,
950 .resume = wm8961_resume,
951 .set_bias_level = wm8961_set_bias_level,
Mark Brown35ecf7c2012-09-13 12:53:59 +0800952};
953
954static const struct regmap_config wm8961_regmap = {
955 .reg_bits = 8,
956 .val_bits = 16,
957 .max_register = WM8961_MAX_REGISTER,
958
959 .reg_defaults = wm8961_reg_defaults,
960 .num_reg_defaults = ARRAY_SIZE(wm8961_reg_defaults),
961 .cache_type = REGCACHE_RBTREE,
962
963 .volatile_reg = wm8961_volatile,
964 .readable_reg = wm8961_readable,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000965};
966
Mark Brown74dc55e2009-06-09 09:55:51 +0100967static __devinit int wm8961_i2c_probe(struct i2c_client *i2c,
968 const struct i2c_device_id *id)
969{
970 struct wm8961_priv *wm8961;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000971 int ret;
Mark Brown74dc55e2009-06-09 09:55:51 +0100972
Mark Brown2ec2a902011-12-16 07:56:02 +0100973 wm8961 = devm_kzalloc(&i2c->dev, sizeof(struct wm8961_priv),
974 GFP_KERNEL);
Mark Brown74dc55e2009-06-09 09:55:51 +0100975 if (wm8961 == NULL)
976 return -ENOMEM;
977
Mark Brown35ecf7c2012-09-13 12:53:59 +0800978 wm8961->regmap = devm_regmap_init_i2c(i2c, &wm8961_regmap);
979 if (IS_ERR(wm8961->regmap))
980 return PTR_ERR(wm8961->regmap);
981
Mark Brown74dc55e2009-06-09 09:55:51 +0100982 i2c_set_clientdata(i2c, wm8961);
Mark Brown74dc55e2009-06-09 09:55:51 +0100983
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000984 ret = snd_soc_register_codec(&i2c->dev,
985 &soc_codec_dev_wm8961, &wm8961_dai, 1);
Mark Brown2ec2a902011-12-16 07:56:02 +0100986
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000987 return ret;
Mark Brown74dc55e2009-06-09 09:55:51 +0100988}
989
990static __devexit int wm8961_i2c_remove(struct i2c_client *client)
991{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000992 snd_soc_unregister_codec(&client->dev);
Mark Brown2ec2a902011-12-16 07:56:02 +0100993
Mark Brown74dc55e2009-06-09 09:55:51 +0100994 return 0;
995}
996
997static const struct i2c_device_id wm8961_i2c_id[] = {
998 { "wm8961", 0 },
999 { }
1000};
1001MODULE_DEVICE_TABLE(i2c, wm8961_i2c_id);
1002
1003static struct i2c_driver wm8961_i2c_driver = {
1004 .driver = {
Mark Brown091edcc2011-12-02 22:08:49 +00001005 .name = "wm8961",
Mark Brown74dc55e2009-06-09 09:55:51 +01001006 .owner = THIS_MODULE,
1007 },
1008 .probe = wm8961_i2c_probe,
1009 .remove = __devexit_p(wm8961_i2c_remove),
Mark Brown74dc55e2009-06-09 09:55:51 +01001010 .id_table = wm8961_i2c_id,
1011};
1012
Sachin Kamat8b08eb22012-08-06 17:25:56 +05301013module_i2c_driver(wm8961_i2c_driver);
Mark Brown74dc55e2009-06-09 09:55:51 +01001014
Mark Brown74dc55e2009-06-09 09:55:51 +01001015MODULE_DESCRIPTION("ASoC WM8961 driver");
1016MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1017MODULE_LICENSE("GPL");