xref: /linux/sound/soc/codecs/wm9090.c (revision 39b8eab7e7fe429d8d57f18c0ebdb7c25df55f5c)
1*39b8eab7SMark Brown /*
2*39b8eab7SMark Brown  * ALSA SoC WM9090 driver
3*39b8eab7SMark Brown  *
4*39b8eab7SMark Brown  * Copyright 2009, 2010 Wolfson Microelectronics
5*39b8eab7SMark Brown  *
6*39b8eab7SMark Brown  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7*39b8eab7SMark Brown  *
8*39b8eab7SMark Brown  * This program is free software; you can redistribute it and/or
9*39b8eab7SMark Brown  * modify it under the terms of the GNU General Public License
10*39b8eab7SMark Brown  * version 2 as published by the Free Software Foundation.
11*39b8eab7SMark Brown  *
12*39b8eab7SMark Brown  * This program is distributed in the hope that it will be useful, but
13*39b8eab7SMark Brown  * WITHOUT ANY WARRANTY; without even the implied warranty of
14*39b8eab7SMark Brown  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15*39b8eab7SMark Brown  * General Public License for more details.
16*39b8eab7SMark Brown  *
17*39b8eab7SMark Brown  * You should have received a copy of the GNU General Public License
18*39b8eab7SMark Brown  * along with this program; if not, write to the Free Software
19*39b8eab7SMark Brown  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20*39b8eab7SMark Brown  * 02110-1301 USA
21*39b8eab7SMark Brown  */
22*39b8eab7SMark Brown 
23*39b8eab7SMark Brown #include <linux/module.h>
24*39b8eab7SMark Brown #include <linux/errno.h>
25*39b8eab7SMark Brown #include <linux/device.h>
26*39b8eab7SMark Brown #include <linux/i2c.h>
27*39b8eab7SMark Brown #include <linux/delay.h>
28*39b8eab7SMark Brown #include <linux/slab.h>
29*39b8eab7SMark Brown #include <sound/initval.h>
30*39b8eab7SMark Brown #include <sound/soc.h>
31*39b8eab7SMark Brown #include <sound/soc-dapm.h>
32*39b8eab7SMark Brown #include <sound/tlv.h>
33*39b8eab7SMark Brown #include <sound/wm9090.h>
34*39b8eab7SMark Brown 
35*39b8eab7SMark Brown #include "wm9090.h"
36*39b8eab7SMark Brown 
37*39b8eab7SMark Brown static struct snd_soc_codec *wm9090_codec;
38*39b8eab7SMark Brown 
39*39b8eab7SMark Brown static const u16 wm9090_reg_defaults[] = {
40*39b8eab7SMark Brown 	0x9093,     /* R0   - Software Reset */
41*39b8eab7SMark Brown 	0x0006,     /* R1   - Power Management (1) */
42*39b8eab7SMark Brown 	0x6000,     /* R2   - Power Management (2) */
43*39b8eab7SMark Brown 	0x0000,     /* R3   - Power Management (3) */
44*39b8eab7SMark Brown 	0x0000,     /* R4 */
45*39b8eab7SMark Brown 	0x0000,     /* R5 */
46*39b8eab7SMark Brown 	0x01C0,     /* R6   - Clocking 1 */
47*39b8eab7SMark Brown 	0x0000,     /* R7 */
48*39b8eab7SMark Brown 	0x0000,     /* R8 */
49*39b8eab7SMark Brown 	0x0000,     /* R9 */
50*39b8eab7SMark Brown 	0x0000,     /* R10 */
51*39b8eab7SMark Brown 	0x0000,     /* R11 */
52*39b8eab7SMark Brown 	0x0000,     /* R12 */
53*39b8eab7SMark Brown 	0x0000,     /* R13 */
54*39b8eab7SMark Brown 	0x0000,     /* R14 */
55*39b8eab7SMark Brown 	0x0000,     /* R15 */
56*39b8eab7SMark Brown 	0x0000,     /* R16 */
57*39b8eab7SMark Brown 	0x0000,     /* R17 */
58*39b8eab7SMark Brown 	0x0000,     /* R18 */
59*39b8eab7SMark Brown 	0x0000,     /* R19 */
60*39b8eab7SMark Brown 	0x0000,     /* R20 */
61*39b8eab7SMark Brown 	0x0000,     /* R21 */
62*39b8eab7SMark Brown 	0x0003,     /* R22  - IN1 Line Control */
63*39b8eab7SMark Brown 	0x0003,     /* R23  - IN2 Line Control */
64*39b8eab7SMark Brown 	0x0083,     /* R24  - IN1 Line Input A Volume */
65*39b8eab7SMark Brown 	0x0083,     /* R25  - IN1  Line Input B Volume */
66*39b8eab7SMark Brown 	0x0083,     /* R26  - IN2 Line Input A Volume */
67*39b8eab7SMark Brown 	0x0083,     /* R27  - IN2 Line Input B Volume */
68*39b8eab7SMark Brown 	0x002D,     /* R28  - Left Output Volume */
69*39b8eab7SMark Brown 	0x002D,     /* R29  - Right Output Volume */
70*39b8eab7SMark Brown 	0x0000,     /* R30 */
71*39b8eab7SMark Brown 	0x0000,     /* R31 */
72*39b8eab7SMark Brown 	0x0000,     /* R32 */
73*39b8eab7SMark Brown 	0x0000,     /* R33 */
74*39b8eab7SMark Brown 	0x0100,     /* R34  - SPKMIXL Attenuation */
75*39b8eab7SMark Brown 	0x0000,     /* R35 */
76*39b8eab7SMark Brown 	0x0010,     /* R36  - SPKOUT Mixers */
77*39b8eab7SMark Brown 	0x0140,     /* R37  - ClassD3 */
78*39b8eab7SMark Brown 	0x0039,     /* R38  - Speaker Volume Left */
79*39b8eab7SMark Brown 	0x0000,     /* R39 */
80*39b8eab7SMark Brown 	0x0000,     /* R40 */
81*39b8eab7SMark Brown 	0x0000,     /* R41 */
82*39b8eab7SMark Brown 	0x0000,     /* R42 */
83*39b8eab7SMark Brown 	0x0000,     /* R43 */
84*39b8eab7SMark Brown 	0x0000,     /* R44 */
85*39b8eab7SMark Brown 	0x0000,     /* R45  - Output Mixer1 */
86*39b8eab7SMark Brown 	0x0000,     /* R46  - Output Mixer2 */
87*39b8eab7SMark Brown 	0x0100,     /* R47  - Output Mixer3 */
88*39b8eab7SMark Brown 	0x0100,     /* R48  - Output Mixer4 */
89*39b8eab7SMark Brown 	0x0000,     /* R49 */
90*39b8eab7SMark Brown 	0x0000,     /* R50 */
91*39b8eab7SMark Brown 	0x0000,     /* R51 */
92*39b8eab7SMark Brown 	0x0000,     /* R52 */
93*39b8eab7SMark Brown 	0x0000,     /* R53 */
94*39b8eab7SMark Brown 	0x0000,     /* R54  - Speaker Mixer */
95*39b8eab7SMark Brown 	0x0000,     /* R55 */
96*39b8eab7SMark Brown 	0x0000,     /* R56 */
97*39b8eab7SMark Brown 	0x000D,     /* R57  - AntiPOP2 */
98*39b8eab7SMark Brown 	0x0000,     /* R58 */
99*39b8eab7SMark Brown 	0x0000,     /* R59 */
100*39b8eab7SMark Brown 	0x0000,     /* R60 */
101*39b8eab7SMark Brown 	0x0000,     /* R61 */
102*39b8eab7SMark Brown 	0x0000,     /* R62 */
103*39b8eab7SMark Brown 	0x0000,     /* R63 */
104*39b8eab7SMark Brown 	0x0000,     /* R64 */
105*39b8eab7SMark Brown 	0x0000,     /* R65 */
106*39b8eab7SMark Brown 	0x0000,     /* R66 */
107*39b8eab7SMark Brown 	0x0000,     /* R67 */
108*39b8eab7SMark Brown 	0x0000,     /* R68 */
109*39b8eab7SMark Brown 	0x0000,     /* R69 */
110*39b8eab7SMark Brown 	0x0000,     /* R70  - Write Sequencer 0 */
111*39b8eab7SMark Brown 	0x0000,     /* R71  - Write Sequencer 1 */
112*39b8eab7SMark Brown 	0x0000,     /* R72  - Write Sequencer 2 */
113*39b8eab7SMark Brown 	0x0000,     /* R73  - Write Sequencer 3 */
114*39b8eab7SMark Brown 	0x0000,     /* R74  - Write Sequencer 4 */
115*39b8eab7SMark Brown 	0x0000,     /* R75  - Write Sequencer 5 */
116*39b8eab7SMark Brown 	0x1F25,     /* R76  - Charge Pump 1 */
117*39b8eab7SMark Brown 	0x0000,     /* R77 */
118*39b8eab7SMark Brown 	0x0000,     /* R78 */
119*39b8eab7SMark Brown 	0x0000,     /* R79 */
120*39b8eab7SMark Brown 	0x0000,     /* R80 */
121*39b8eab7SMark Brown 	0x0000,     /* R81 */
122*39b8eab7SMark Brown 	0x0000,     /* R82 */
123*39b8eab7SMark Brown 	0x0000,     /* R83 */
124*39b8eab7SMark Brown 	0x0000,     /* R84  - DC Servo 0 */
125*39b8eab7SMark Brown 	0x054A,     /* R85  - DC Servo 1 */
126*39b8eab7SMark Brown 	0x0000,     /* R86 */
127*39b8eab7SMark Brown 	0x0000,     /* R87  - DC Servo 3 */
128*39b8eab7SMark Brown 	0x0000,     /* R88  - DC Servo Readback 0 */
129*39b8eab7SMark Brown 	0x0000,     /* R89  - DC Servo Readback 1 */
130*39b8eab7SMark Brown 	0x0000,     /* R90  - DC Servo Readback 2 */
131*39b8eab7SMark Brown 	0x0000,     /* R91 */
132*39b8eab7SMark Brown 	0x0000,     /* R92 */
133*39b8eab7SMark Brown 	0x0000,     /* R93 */
134*39b8eab7SMark Brown 	0x0000,     /* R94 */
135*39b8eab7SMark Brown 	0x0000,     /* R95 */
136*39b8eab7SMark Brown 	0x0100,     /* R96  - Analogue HP 0 */
137*39b8eab7SMark Brown 	0x0000,     /* R97 */
138*39b8eab7SMark Brown 	0x8640,     /* R98  - AGC Control 0 */
139*39b8eab7SMark Brown 	0xC000,     /* R99  - AGC Control 1 */
140*39b8eab7SMark Brown 	0x0200,     /* R100 - AGC Control 2 */
141*39b8eab7SMark Brown };
142*39b8eab7SMark Brown 
143*39b8eab7SMark Brown /* This struct is used to save the context */
144*39b8eab7SMark Brown struct wm9090_priv {
145*39b8eab7SMark Brown 	/* We're not really registering as a CODEC since ASoC core
146*39b8eab7SMark Brown 	 * does not yet support multiple CODECs but having the CODEC
147*39b8eab7SMark Brown 	 * structure means we can reuse some of the ASoC core
148*39b8eab7SMark Brown 	 * features.
149*39b8eab7SMark Brown 	 */
150*39b8eab7SMark Brown 	struct snd_soc_codec codec;
151*39b8eab7SMark Brown 	struct mutex mutex;
152*39b8eab7SMark Brown 	u16 reg_cache[WM9090_MAX_REGISTER + 1];
153*39b8eab7SMark Brown 	struct wm9090_platform_data pdata;
154*39b8eab7SMark Brown };
155*39b8eab7SMark Brown 
156*39b8eab7SMark Brown static int wm9090_volatile(unsigned int reg)
157*39b8eab7SMark Brown {
158*39b8eab7SMark Brown 	switch (reg) {
159*39b8eab7SMark Brown 	case WM9090_SOFTWARE_RESET:
160*39b8eab7SMark Brown 	case WM9090_DC_SERVO_0:
161*39b8eab7SMark Brown 	case WM9090_DC_SERVO_READBACK_0:
162*39b8eab7SMark Brown 	case WM9090_DC_SERVO_READBACK_1:
163*39b8eab7SMark Brown 	case WM9090_DC_SERVO_READBACK_2:
164*39b8eab7SMark Brown 		return 1;
165*39b8eab7SMark Brown 
166*39b8eab7SMark Brown 	default:
167*39b8eab7SMark Brown 		return 0;
168*39b8eab7SMark Brown 	}
169*39b8eab7SMark Brown }
170*39b8eab7SMark Brown 
171*39b8eab7SMark Brown static void wait_for_dc_servo(struct snd_soc_codec *codec)
172*39b8eab7SMark Brown {
173*39b8eab7SMark Brown 	unsigned int reg;
174*39b8eab7SMark Brown 	int count = 0;
175*39b8eab7SMark Brown 
176*39b8eab7SMark Brown 	dev_dbg(codec->dev, "Waiting for DC servo...\n");
177*39b8eab7SMark Brown 	do {
178*39b8eab7SMark Brown 		count++;
179*39b8eab7SMark Brown 		msleep(1);
180*39b8eab7SMark Brown 		reg = snd_soc_read(codec, WM9090_DC_SERVO_READBACK_0);
181*39b8eab7SMark Brown 		dev_dbg(codec->dev, "DC servo status: %x\n", reg);
182*39b8eab7SMark Brown 	} while ((reg & WM9090_DCS_CAL_COMPLETE_MASK)
183*39b8eab7SMark Brown 		 != WM9090_DCS_CAL_COMPLETE_MASK && count < 1000);
184*39b8eab7SMark Brown 
185*39b8eab7SMark Brown 	if ((reg & WM9090_DCS_CAL_COMPLETE_MASK)
186*39b8eab7SMark Brown 	    != WM9090_DCS_CAL_COMPLETE_MASK)
187*39b8eab7SMark Brown 		dev_err(codec->dev, "Timed out waiting for DC Servo\n");
188*39b8eab7SMark Brown }
189*39b8eab7SMark Brown 
190*39b8eab7SMark Brown static const unsigned int in_tlv[] = {
191*39b8eab7SMark Brown 	TLV_DB_RANGE_HEAD(6),
192*39b8eab7SMark Brown 	0, 0, TLV_DB_SCALE_ITEM(-600, 0, 0),
193*39b8eab7SMark Brown 	1, 3, TLV_DB_SCALE_ITEM(-350, 350, 0),
194*39b8eab7SMark Brown 	4, 6, TLV_DB_SCALE_ITEM(600, 600, 0),
195*39b8eab7SMark Brown };
196*39b8eab7SMark Brown static const unsigned int mix_tlv[] = {
197*39b8eab7SMark Brown 	TLV_DB_RANGE_HEAD(4),
198*39b8eab7SMark Brown 	0, 2, TLV_DB_SCALE_ITEM(-1200, 300, 0),
199*39b8eab7SMark Brown 	3, 3, TLV_DB_SCALE_ITEM(0, 0, 0),
200*39b8eab7SMark Brown };
201*39b8eab7SMark Brown static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
202*39b8eab7SMark Brown static const unsigned int spkboost_tlv[] = {
203*39b8eab7SMark Brown 	TLV_DB_RANGE_HEAD(7),
204*39b8eab7SMark Brown 	0, 6, TLV_DB_SCALE_ITEM(0, 150, 0),
205*39b8eab7SMark Brown 	7, 7, TLV_DB_SCALE_ITEM(1200, 0, 0),
206*39b8eab7SMark Brown };
207*39b8eab7SMark Brown 
208*39b8eab7SMark Brown static const struct snd_kcontrol_new wm9090_controls[] = {
209*39b8eab7SMark Brown SOC_SINGLE_TLV("IN1A Volume", WM9090_IN1_LINE_INPUT_A_VOLUME, 0, 6, 0,
210*39b8eab7SMark Brown 	       in_tlv),
211*39b8eab7SMark Brown SOC_SINGLE("IN1A Switch", WM9090_IN1_LINE_INPUT_A_VOLUME, 7, 1, 1),
212*39b8eab7SMark Brown SOC_SINGLE("IN1A ZC Switch", WM9090_IN1_LINE_INPUT_A_VOLUME, 6, 1, 0),
213*39b8eab7SMark Brown 
214*39b8eab7SMark Brown SOC_SINGLE_TLV("IN2A Volume", WM9090_IN2_LINE_INPUT_A_VOLUME, 0, 6, 0,
215*39b8eab7SMark Brown 	       in_tlv),
216*39b8eab7SMark Brown SOC_SINGLE("IN2A Switch", WM9090_IN2_LINE_INPUT_A_VOLUME, 7, 1, 1),
217*39b8eab7SMark Brown SOC_SINGLE("IN2A ZC Switch", WM9090_IN2_LINE_INPUT_A_VOLUME, 6, 1, 0),
218*39b8eab7SMark Brown 
219*39b8eab7SMark Brown SOC_SINGLE("MIXOUTL Switch", WM9090_OUTPUT_MIXER3, 8, 1, 1),
220*39b8eab7SMark Brown SOC_SINGLE_TLV("MIXOUTL IN1A Volume", WM9090_OUTPUT_MIXER3, 6, 3, 1,
221*39b8eab7SMark Brown 	       mix_tlv),
222*39b8eab7SMark Brown SOC_SINGLE_TLV("MIXOUTL IN2A Volume", WM9090_OUTPUT_MIXER3, 2, 3, 1,
223*39b8eab7SMark Brown 	       mix_tlv),
224*39b8eab7SMark Brown 
225*39b8eab7SMark Brown SOC_SINGLE("MIXOUTR Switch", WM9090_OUTPUT_MIXER4, 8, 1, 1),
226*39b8eab7SMark Brown SOC_SINGLE_TLV("MIXOUTR IN1A Volume", WM9090_OUTPUT_MIXER4, 6, 3, 1,
227*39b8eab7SMark Brown 	       mix_tlv),
228*39b8eab7SMark Brown SOC_SINGLE_TLV("MIXOUTR IN2A Volume", WM9090_OUTPUT_MIXER4, 2, 3, 1,
229*39b8eab7SMark Brown 	       mix_tlv),
230*39b8eab7SMark Brown 
231*39b8eab7SMark Brown SOC_SINGLE("SPKMIX Switch", WM9090_SPKMIXL_ATTENUATION, 8, 1, 1),
232*39b8eab7SMark Brown SOC_SINGLE_TLV("SPKMIX IN1A Volume", WM9090_SPKMIXL_ATTENUATION, 6, 3, 1,
233*39b8eab7SMark Brown 	       mix_tlv),
234*39b8eab7SMark Brown SOC_SINGLE_TLV("SPKMIX IN2A Volume", WM9090_SPKMIXL_ATTENUATION, 2, 3, 1,
235*39b8eab7SMark Brown 	       mix_tlv),
236*39b8eab7SMark Brown 
237*39b8eab7SMark Brown SOC_DOUBLE_R_TLV("Headphone Volume", WM9090_LEFT_OUTPUT_VOLUME,
238*39b8eab7SMark Brown 		 WM9090_RIGHT_OUTPUT_VOLUME, 0, 63, 0, out_tlv),
239*39b8eab7SMark Brown SOC_DOUBLE_R("Headphone Switch", WM9090_LEFT_OUTPUT_VOLUME,
240*39b8eab7SMark Brown 	     WM9090_RIGHT_OUTPUT_VOLUME, 6, 1, 1),
241*39b8eab7SMark Brown SOC_DOUBLE_R("Headphone ZC Switch", WM9090_LEFT_OUTPUT_VOLUME,
242*39b8eab7SMark Brown 	     WM9090_RIGHT_OUTPUT_VOLUME, 7, 1, 0),
243*39b8eab7SMark Brown 
244*39b8eab7SMark Brown SOC_SINGLE_TLV("Speaker Volume", WM9090_SPEAKER_VOLUME_LEFT, 0, 63, 0,
245*39b8eab7SMark Brown 	       out_tlv),
246*39b8eab7SMark Brown SOC_SINGLE("Speaker Switch", WM9090_SPEAKER_VOLUME_LEFT, 6, 1, 1),
247*39b8eab7SMark Brown SOC_SINGLE("Speaker ZC Switch", WM9090_SPEAKER_VOLUME_LEFT, 7, 1, 0),
248*39b8eab7SMark Brown SOC_SINGLE_TLV("Speaker Boost Volume", WM9090_CLASSD3, 3, 7, 0, spkboost_tlv),
249*39b8eab7SMark Brown };
250*39b8eab7SMark Brown 
251*39b8eab7SMark Brown static const struct snd_kcontrol_new wm9090_in1_se_controls[] = {
252*39b8eab7SMark Brown SOC_SINGLE_TLV("IN1B Volume", WM9090_IN1_LINE_INPUT_B_VOLUME, 0, 6, 0,
253*39b8eab7SMark Brown 	       in_tlv),
254*39b8eab7SMark Brown SOC_SINGLE("IN1B Switch", WM9090_IN1_LINE_INPUT_B_VOLUME, 7, 1, 1),
255*39b8eab7SMark Brown SOC_SINGLE("IN1B ZC Switch", WM9090_IN1_LINE_INPUT_B_VOLUME, 6, 1, 0),
256*39b8eab7SMark Brown 
257*39b8eab7SMark Brown SOC_SINGLE_TLV("SPKMIX IN1B Volume", WM9090_SPKMIXL_ATTENUATION, 4, 3, 1,
258*39b8eab7SMark Brown 	       mix_tlv),
259*39b8eab7SMark Brown SOC_SINGLE_TLV("MIXOUTL IN1B Volume", WM9090_OUTPUT_MIXER3, 4, 3, 1,
260*39b8eab7SMark Brown 	       mix_tlv),
261*39b8eab7SMark Brown SOC_SINGLE_TLV("MIXOUTR IN1B Volume", WM9090_OUTPUT_MIXER4, 4, 3, 1,
262*39b8eab7SMark Brown 	       mix_tlv),
263*39b8eab7SMark Brown };
264*39b8eab7SMark Brown 
265*39b8eab7SMark Brown static const struct snd_kcontrol_new wm9090_in2_se_controls[] = {
266*39b8eab7SMark Brown SOC_SINGLE_TLV("IN2B Volume", WM9090_IN2_LINE_INPUT_B_VOLUME, 0, 6, 0,
267*39b8eab7SMark Brown 	       in_tlv),
268*39b8eab7SMark Brown SOC_SINGLE("IN2B Switch", WM9090_IN2_LINE_INPUT_B_VOLUME, 7, 1, 1),
269*39b8eab7SMark Brown SOC_SINGLE("IN2B ZC Switch", WM9090_IN2_LINE_INPUT_B_VOLUME, 6, 1, 0),
270*39b8eab7SMark Brown 
271*39b8eab7SMark Brown SOC_SINGLE_TLV("SPKMIX IN2B Volume", WM9090_SPKMIXL_ATTENUATION, 0, 3, 1,
272*39b8eab7SMark Brown 	       mix_tlv),
273*39b8eab7SMark Brown SOC_SINGLE_TLV("MIXOUTL IN2B Volume", WM9090_OUTPUT_MIXER3, 0, 3, 1,
274*39b8eab7SMark Brown 	       mix_tlv),
275*39b8eab7SMark Brown SOC_SINGLE_TLV("MIXOUTR IN2B Volume", WM9090_OUTPUT_MIXER4, 0, 3, 1,
276*39b8eab7SMark Brown 	       mix_tlv),
277*39b8eab7SMark Brown };
278*39b8eab7SMark Brown 
279*39b8eab7SMark Brown static int hp_ev(struct snd_soc_dapm_widget *w,
280*39b8eab7SMark Brown 		 struct snd_kcontrol *kcontrol, int event)
281*39b8eab7SMark Brown {
282*39b8eab7SMark Brown 	struct snd_soc_codec *codec = w->codec;
283*39b8eab7SMark Brown 	unsigned int reg = snd_soc_read(codec, WM9090_ANALOGUE_HP_0);
284*39b8eab7SMark Brown 
285*39b8eab7SMark Brown 	switch (event) {
286*39b8eab7SMark Brown 	case SND_SOC_DAPM_POST_PMU:
287*39b8eab7SMark Brown 		snd_soc_update_bits(codec, WM9090_CHARGE_PUMP_1,
288*39b8eab7SMark Brown 				    WM9090_CP_ENA, WM9090_CP_ENA);
289*39b8eab7SMark Brown 
290*39b8eab7SMark Brown 		msleep(5);
291*39b8eab7SMark Brown 
292*39b8eab7SMark Brown 		snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_1,
293*39b8eab7SMark Brown 				    WM9090_HPOUT1L_ENA | WM9090_HPOUT1R_ENA,
294*39b8eab7SMark Brown 				    WM9090_HPOUT1L_ENA | WM9090_HPOUT1R_ENA);
295*39b8eab7SMark Brown 
296*39b8eab7SMark Brown 		reg |= WM9090_HPOUT1L_DLY | WM9090_HPOUT1R_DLY;
297*39b8eab7SMark Brown 		snd_soc_write(codec, WM9090_ANALOGUE_HP_0, reg);
298*39b8eab7SMark Brown 
299*39b8eab7SMark Brown 		/* Start the DC servo.  We don't currently use the
300*39b8eab7SMark Brown 		 * ability to save the state since we don't have full
301*39b8eab7SMark Brown 		 * control of the analogue paths and they can change
302*39b8eab7SMark Brown 		 * DC offsets; see the WM8904 driver for an example of
303*39b8eab7SMark Brown 		 * doing so.
304*39b8eab7SMark Brown 		 */
305*39b8eab7SMark Brown 		snd_soc_write(codec, WM9090_DC_SERVO_0,
306*39b8eab7SMark Brown 			      WM9090_DCS_ENA_CHAN_0 |
307*39b8eab7SMark Brown 			      WM9090_DCS_ENA_CHAN_1 |
308*39b8eab7SMark Brown 			      WM9090_DCS_TRIG_STARTUP_1 |
309*39b8eab7SMark Brown 			      WM9090_DCS_TRIG_STARTUP_0);
310*39b8eab7SMark Brown 		wait_for_dc_servo(codec);
311*39b8eab7SMark Brown 
312*39b8eab7SMark Brown 		reg |= WM9090_HPOUT1R_OUTP | WM9090_HPOUT1R_RMV_SHORT |
313*39b8eab7SMark Brown 			WM9090_HPOUT1L_OUTP | WM9090_HPOUT1L_RMV_SHORT;
314*39b8eab7SMark Brown 		snd_soc_write(codec, WM9090_ANALOGUE_HP_0, reg);
315*39b8eab7SMark Brown 		break;
316*39b8eab7SMark Brown 
317*39b8eab7SMark Brown 	case SND_SOC_DAPM_PRE_PMD:
318*39b8eab7SMark Brown 		reg &= ~(WM9090_HPOUT1L_RMV_SHORT |
319*39b8eab7SMark Brown 			 WM9090_HPOUT1L_DLY |
320*39b8eab7SMark Brown 			 WM9090_HPOUT1L_OUTP |
321*39b8eab7SMark Brown 			 WM9090_HPOUT1R_RMV_SHORT |
322*39b8eab7SMark Brown 			 WM9090_HPOUT1R_DLY |
323*39b8eab7SMark Brown 			 WM9090_HPOUT1R_OUTP);
324*39b8eab7SMark Brown 
325*39b8eab7SMark Brown 		snd_soc_write(codec, WM9090_ANALOGUE_HP_0, reg);
326*39b8eab7SMark Brown 
327*39b8eab7SMark Brown 		snd_soc_write(codec, WM9090_DC_SERVO_0, 0);
328*39b8eab7SMark Brown 
329*39b8eab7SMark Brown 		snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_1,
330*39b8eab7SMark Brown 				    WM9090_HPOUT1L_ENA | WM9090_HPOUT1R_ENA,
331*39b8eab7SMark Brown 				    0);
332*39b8eab7SMark Brown 
333*39b8eab7SMark Brown 		snd_soc_update_bits(codec, WM9090_CHARGE_PUMP_1,
334*39b8eab7SMark Brown 				    WM9090_CP_ENA, 0);
335*39b8eab7SMark Brown 		break;
336*39b8eab7SMark Brown 	}
337*39b8eab7SMark Brown 
338*39b8eab7SMark Brown 	return 0;
339*39b8eab7SMark Brown }
340*39b8eab7SMark Brown 
341*39b8eab7SMark Brown static const struct snd_kcontrol_new spkmix[] = {
342*39b8eab7SMark Brown SOC_DAPM_SINGLE("IN1A Switch", WM9090_SPEAKER_MIXER, 6, 1, 0),
343*39b8eab7SMark Brown SOC_DAPM_SINGLE("IN1B Switch", WM9090_SPEAKER_MIXER, 4, 1, 0),
344*39b8eab7SMark Brown SOC_DAPM_SINGLE("IN2A Switch", WM9090_SPEAKER_MIXER, 2, 1, 0),
345*39b8eab7SMark Brown SOC_DAPM_SINGLE("IN2B Switch", WM9090_SPEAKER_MIXER, 0, 1, 0),
346*39b8eab7SMark Brown };
347*39b8eab7SMark Brown 
348*39b8eab7SMark Brown static const struct snd_kcontrol_new spkout[] = {
349*39b8eab7SMark Brown SOC_DAPM_SINGLE("Mixer Switch", WM9090_SPKOUT_MIXERS, 4, 1, 0),
350*39b8eab7SMark Brown };
351*39b8eab7SMark Brown 
352*39b8eab7SMark Brown static const struct snd_kcontrol_new mixoutl[] = {
353*39b8eab7SMark Brown SOC_DAPM_SINGLE("IN1A Switch", WM9090_OUTPUT_MIXER1, 6, 1, 0),
354*39b8eab7SMark Brown SOC_DAPM_SINGLE("IN1B Switch", WM9090_OUTPUT_MIXER1, 4, 1, 0),
355*39b8eab7SMark Brown SOC_DAPM_SINGLE("IN2A Switch", WM9090_OUTPUT_MIXER1, 2, 1, 0),
356*39b8eab7SMark Brown SOC_DAPM_SINGLE("IN2B Switch", WM9090_OUTPUT_MIXER1, 0, 1, 0),
357*39b8eab7SMark Brown };
358*39b8eab7SMark Brown 
359*39b8eab7SMark Brown static const struct snd_kcontrol_new mixoutr[] = {
360*39b8eab7SMark Brown SOC_DAPM_SINGLE("IN1A Switch", WM9090_OUTPUT_MIXER2, 6, 1, 0),
361*39b8eab7SMark Brown SOC_DAPM_SINGLE("IN1B Switch", WM9090_OUTPUT_MIXER2, 4, 1, 0),
362*39b8eab7SMark Brown SOC_DAPM_SINGLE("IN2A Switch", WM9090_OUTPUT_MIXER2, 2, 1, 0),
363*39b8eab7SMark Brown SOC_DAPM_SINGLE("IN2B Switch", WM9090_OUTPUT_MIXER2, 0, 1, 0),
364*39b8eab7SMark Brown };
365*39b8eab7SMark Brown 
366*39b8eab7SMark Brown static const struct snd_soc_dapm_widget wm9090_dapm_widgets[] = {
367*39b8eab7SMark Brown SND_SOC_DAPM_INPUT("IN1+"),
368*39b8eab7SMark Brown SND_SOC_DAPM_INPUT("IN1-"),
369*39b8eab7SMark Brown SND_SOC_DAPM_INPUT("IN2+"),
370*39b8eab7SMark Brown SND_SOC_DAPM_INPUT("IN2-"),
371*39b8eab7SMark Brown 
372*39b8eab7SMark Brown SND_SOC_DAPM_SUPPLY("OSC", WM9090_POWER_MANAGEMENT_1, 3, 0, NULL, 0),
373*39b8eab7SMark Brown 
374*39b8eab7SMark Brown SND_SOC_DAPM_PGA("IN1A PGA", WM9090_POWER_MANAGEMENT_2, 7, 0, NULL, 0),
375*39b8eab7SMark Brown SND_SOC_DAPM_PGA("IN1B PGA", WM9090_POWER_MANAGEMENT_2, 6, 0, NULL, 0),
376*39b8eab7SMark Brown SND_SOC_DAPM_PGA("IN2A PGA", WM9090_POWER_MANAGEMENT_2, 5, 0, NULL, 0),
377*39b8eab7SMark Brown SND_SOC_DAPM_PGA("IN2B PGA", WM9090_POWER_MANAGEMENT_2, 4, 0, NULL, 0),
378*39b8eab7SMark Brown 
379*39b8eab7SMark Brown SND_SOC_DAPM_MIXER("SPKMIX", WM9090_POWER_MANAGEMENT_3, 3, 0,
380*39b8eab7SMark Brown 		   spkmix, ARRAY_SIZE(spkmix)),
381*39b8eab7SMark Brown SND_SOC_DAPM_MIXER("MIXOUTL", WM9090_POWER_MANAGEMENT_3, 5, 0,
382*39b8eab7SMark Brown 		   mixoutl, ARRAY_SIZE(mixoutl)),
383*39b8eab7SMark Brown SND_SOC_DAPM_MIXER("MIXOUTR", WM9090_POWER_MANAGEMENT_3, 4, 0,
384*39b8eab7SMark Brown 		   mixoutr, ARRAY_SIZE(mixoutr)),
385*39b8eab7SMark Brown 
386*39b8eab7SMark Brown SND_SOC_DAPM_PGA_E("HP PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
387*39b8eab7SMark Brown 		   hp_ev, SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
388*39b8eab7SMark Brown 
389*39b8eab7SMark Brown SND_SOC_DAPM_PGA("SPKPGA", WM9090_POWER_MANAGEMENT_3, 8, 0, NULL, 0),
390*39b8eab7SMark Brown SND_SOC_DAPM_MIXER("SPKOUT", WM9090_POWER_MANAGEMENT_1, 12, 0,
391*39b8eab7SMark Brown 		   spkout, ARRAY_SIZE(spkout)),
392*39b8eab7SMark Brown 
393*39b8eab7SMark Brown SND_SOC_DAPM_OUTPUT("HPR"),
394*39b8eab7SMark Brown SND_SOC_DAPM_OUTPUT("HPL"),
395*39b8eab7SMark Brown SND_SOC_DAPM_OUTPUT("Speaker"),
396*39b8eab7SMark Brown };
397*39b8eab7SMark Brown 
398*39b8eab7SMark Brown static const struct snd_soc_dapm_route audio_map[] = {
399*39b8eab7SMark Brown 	{ "IN1A PGA", NULL, "IN1+" },
400*39b8eab7SMark Brown 	{ "IN2A PGA", NULL, "IN2+" },
401*39b8eab7SMark Brown 
402*39b8eab7SMark Brown 	{ "SPKMIX", "IN1A Switch", "IN1A PGA" },
403*39b8eab7SMark Brown 	{ "SPKMIX", "IN2A Switch", "IN2A PGA" },
404*39b8eab7SMark Brown 
405*39b8eab7SMark Brown 	{ "MIXOUTL", "IN1A Switch", "IN1A PGA" },
406*39b8eab7SMark Brown 	{ "MIXOUTL", "IN2A Switch", "IN2A PGA" },
407*39b8eab7SMark Brown 
408*39b8eab7SMark Brown 	{ "MIXOUTR", "IN1A Switch", "IN1A PGA" },
409*39b8eab7SMark Brown 	{ "MIXOUTR", "IN2A Switch", "IN2A PGA" },
410*39b8eab7SMark Brown 
411*39b8eab7SMark Brown 	{ "HP PGA", NULL, "OSC" },
412*39b8eab7SMark Brown 	{ "HP PGA", NULL, "MIXOUTL" },
413*39b8eab7SMark Brown 	{ "HP PGA", NULL, "MIXOUTR" },
414*39b8eab7SMark Brown 
415*39b8eab7SMark Brown 	{ "HPL", NULL, "HP PGA" },
416*39b8eab7SMark Brown 	{ "HPR", NULL, "HP PGA" },
417*39b8eab7SMark Brown 
418*39b8eab7SMark Brown 	{ "SPKPGA", NULL, "OSC" },
419*39b8eab7SMark Brown 	{ "SPKPGA", NULL, "SPKMIX" },
420*39b8eab7SMark Brown 
421*39b8eab7SMark Brown 	{ "SPKOUT", "Mixer Switch", "SPKPGA" },
422*39b8eab7SMark Brown 
423*39b8eab7SMark Brown 	{ "Speaker", NULL, "SPKOUT" },
424*39b8eab7SMark Brown };
425*39b8eab7SMark Brown 
426*39b8eab7SMark Brown static const struct snd_soc_dapm_route audio_map_in1_se[] = {
427*39b8eab7SMark Brown 	{ "IN1B PGA", NULL, "IN1-" },
428*39b8eab7SMark Brown 
429*39b8eab7SMark Brown 	{ "SPKMIX", "IN1B Switch", "IN1B PGA" },
430*39b8eab7SMark Brown 	{ "MIXOUTL", "IN1B Switch", "IN1B PGA" },
431*39b8eab7SMark Brown 	{ "MIXOUTR", "IN1B Switch", "IN1B PGA" },
432*39b8eab7SMark Brown };
433*39b8eab7SMark Brown 
434*39b8eab7SMark Brown static const struct snd_soc_dapm_route audio_map_in1_diff[] = {
435*39b8eab7SMark Brown 	{ "IN1A PGA", NULL, "IN1-" },
436*39b8eab7SMark Brown };
437*39b8eab7SMark Brown 
438*39b8eab7SMark Brown static const struct snd_soc_dapm_route audio_map_in2_se[] = {
439*39b8eab7SMark Brown 	{ "IN2B PGA", NULL, "IN2-" },
440*39b8eab7SMark Brown 
441*39b8eab7SMark Brown 	{ "SPKMIX", "IN2B Switch", "IN2B PGA" },
442*39b8eab7SMark Brown 	{ "MIXOUTL", "IN2B Switch", "IN2B PGA" },
443*39b8eab7SMark Brown 	{ "MIXOUTR", "IN2B Switch", "IN2B PGA" },
444*39b8eab7SMark Brown };
445*39b8eab7SMark Brown 
446*39b8eab7SMark Brown static const struct snd_soc_dapm_route audio_map_in2_diff[] = {
447*39b8eab7SMark Brown 	{ "IN2A PGA", NULL, "IN2-" },
448*39b8eab7SMark Brown };
449*39b8eab7SMark Brown 
450*39b8eab7SMark Brown static int wm9090_add_controls(struct snd_soc_codec *codec)
451*39b8eab7SMark Brown {
452*39b8eab7SMark Brown 	struct wm9090_priv *wm9090 = snd_soc_codec_get_drvdata(codec);
453*39b8eab7SMark Brown 	int i;
454*39b8eab7SMark Brown 
455*39b8eab7SMark Brown 	snd_soc_dapm_new_controls(codec, wm9090_dapm_widgets,
456*39b8eab7SMark Brown 				  ARRAY_SIZE(wm9090_dapm_widgets));
457*39b8eab7SMark Brown 
458*39b8eab7SMark Brown 	snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
459*39b8eab7SMark Brown 
460*39b8eab7SMark Brown 	snd_soc_add_controls(codec, wm9090_controls,
461*39b8eab7SMark Brown 			     ARRAY_SIZE(wm9090_controls));
462*39b8eab7SMark Brown 
463*39b8eab7SMark Brown 	if (wm9090->pdata.lin1_diff) {
464*39b8eab7SMark Brown 		snd_soc_dapm_add_routes(codec, audio_map_in1_diff,
465*39b8eab7SMark Brown 					ARRAY_SIZE(audio_map_in1_diff));
466*39b8eab7SMark Brown 	} else {
467*39b8eab7SMark Brown 		snd_soc_dapm_add_routes(codec, audio_map_in1_se,
468*39b8eab7SMark Brown 					ARRAY_SIZE(audio_map_in1_se));
469*39b8eab7SMark Brown 		snd_soc_add_controls(codec, wm9090_in1_se_controls,
470*39b8eab7SMark Brown 				     ARRAY_SIZE(wm9090_in1_se_controls));
471*39b8eab7SMark Brown 	}
472*39b8eab7SMark Brown 
473*39b8eab7SMark Brown 	if (wm9090->pdata.lin2_diff) {
474*39b8eab7SMark Brown 		snd_soc_dapm_add_routes(codec, audio_map_in2_diff,
475*39b8eab7SMark Brown 					ARRAY_SIZE(audio_map_in2_diff));
476*39b8eab7SMark Brown 	} else {
477*39b8eab7SMark Brown 		snd_soc_dapm_add_routes(codec, audio_map_in2_se,
478*39b8eab7SMark Brown 					ARRAY_SIZE(audio_map_in2_se));
479*39b8eab7SMark Brown 		snd_soc_add_controls(codec, wm9090_in2_se_controls,
480*39b8eab7SMark Brown 				     ARRAY_SIZE(wm9090_in2_se_controls));
481*39b8eab7SMark Brown 	}
482*39b8eab7SMark Brown 
483*39b8eab7SMark Brown 	if (wm9090->pdata.agc_ena) {
484*39b8eab7SMark Brown 		for (i = 0; i < ARRAY_SIZE(wm9090->pdata.agc); i++)
485*39b8eab7SMark Brown 			snd_soc_write(codec, WM9090_AGC_CONTROL_0 + i,
486*39b8eab7SMark Brown 				      wm9090->pdata.agc[i]);
487*39b8eab7SMark Brown 		snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_3,
488*39b8eab7SMark Brown 				    WM9090_AGC_ENA, WM9090_AGC_ENA);
489*39b8eab7SMark Brown 	} else {
490*39b8eab7SMark Brown 		snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_3,
491*39b8eab7SMark Brown 				    WM9090_AGC_ENA, 0);
492*39b8eab7SMark Brown 	}
493*39b8eab7SMark Brown 
494*39b8eab7SMark Brown 	return 0;
495*39b8eab7SMark Brown 
496*39b8eab7SMark Brown }
497*39b8eab7SMark Brown 
498*39b8eab7SMark Brown /*
499*39b8eab7SMark Brown  * The machine driver should call this from their set_bias_level; if there
500*39b8eab7SMark Brown  * isn't one then this can just be set as the set_bias_level function.
501*39b8eab7SMark Brown  */
502*39b8eab7SMark Brown static int wm9090_set_bias_level(struct snd_soc_codec *codec,
503*39b8eab7SMark Brown 				 enum snd_soc_bias_level level)
504*39b8eab7SMark Brown {
505*39b8eab7SMark Brown 	u16 *reg_cache = codec->reg_cache;
506*39b8eab7SMark Brown 	int i, ret;
507*39b8eab7SMark Brown 
508*39b8eab7SMark Brown 	switch (level) {
509*39b8eab7SMark Brown 	case SND_SOC_BIAS_ON:
510*39b8eab7SMark Brown 		break;
511*39b8eab7SMark Brown 
512*39b8eab7SMark Brown 	case SND_SOC_BIAS_PREPARE:
513*39b8eab7SMark Brown 		snd_soc_update_bits(codec, WM9090_ANTIPOP2, WM9090_VMID_ENA,
514*39b8eab7SMark Brown 				    WM9090_VMID_ENA);
515*39b8eab7SMark Brown 		snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_1,
516*39b8eab7SMark Brown 				    WM9090_BIAS_ENA |
517*39b8eab7SMark Brown 				    WM9090_VMID_RES_MASK,
518*39b8eab7SMark Brown 				    WM9090_BIAS_ENA |
519*39b8eab7SMark Brown 				    1 << WM9090_VMID_RES_SHIFT);
520*39b8eab7SMark Brown 		msleep(1);  /* Probably an overestimate */
521*39b8eab7SMark Brown 		break;
522*39b8eab7SMark Brown 
523*39b8eab7SMark Brown 	case SND_SOC_BIAS_STANDBY:
524*39b8eab7SMark Brown 		if (codec->bias_level == SND_SOC_BIAS_OFF) {
525*39b8eab7SMark Brown 			/* Restore the register cache */
526*39b8eab7SMark Brown 			for (i = 1; i < codec->reg_cache_size; i++) {
527*39b8eab7SMark Brown 				if (reg_cache[i] == wm9090_reg_defaults[i])
528*39b8eab7SMark Brown 					continue;
529*39b8eab7SMark Brown 				if (wm9090_volatile(i))
530*39b8eab7SMark Brown 					continue;
531*39b8eab7SMark Brown 
532*39b8eab7SMark Brown 				ret = snd_soc_write(codec, i, reg_cache[i]);
533*39b8eab7SMark Brown 				if (ret != 0)
534*39b8eab7SMark Brown 					dev_warn(codec->dev,
535*39b8eab7SMark Brown 						 "Failed to restore register %d: %d\n",
536*39b8eab7SMark Brown 						 i, ret);
537*39b8eab7SMark Brown 			}
538*39b8eab7SMark Brown 		}
539*39b8eab7SMark Brown 
540*39b8eab7SMark Brown 		/* We keep VMID off during standby since the combination of
541*39b8eab7SMark Brown 		 * ground referenced outputs and class D speaker mean that
542*39b8eab7SMark Brown 		 * latency is not an issue.
543*39b8eab7SMark Brown 		 */
544*39b8eab7SMark Brown 		snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_1,
545*39b8eab7SMark Brown 				    WM9090_BIAS_ENA | WM9090_VMID_RES_MASK, 0);
546*39b8eab7SMark Brown 		snd_soc_update_bits(codec, WM9090_ANTIPOP2,
547*39b8eab7SMark Brown 				    WM9090_VMID_ENA, 0);
548*39b8eab7SMark Brown 		break;
549*39b8eab7SMark Brown 
550*39b8eab7SMark Brown 	case SND_SOC_BIAS_OFF:
551*39b8eab7SMark Brown 		break;
552*39b8eab7SMark Brown 	}
553*39b8eab7SMark Brown 
554*39b8eab7SMark Brown 	codec->bias_level = level;
555*39b8eab7SMark Brown 
556*39b8eab7SMark Brown 	return 0;
557*39b8eab7SMark Brown }
558*39b8eab7SMark Brown 
559*39b8eab7SMark Brown static int wm9090_probe(struct platform_device *pdev)
560*39b8eab7SMark Brown {
561*39b8eab7SMark Brown 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
562*39b8eab7SMark Brown 	struct snd_soc_codec *codec;
563*39b8eab7SMark Brown 	int ret = 0;
564*39b8eab7SMark Brown 
565*39b8eab7SMark Brown 	if (wm9090_codec == NULL) {
566*39b8eab7SMark Brown 		dev_err(&pdev->dev, "Codec device not registered\n");
567*39b8eab7SMark Brown 		return -ENODEV;
568*39b8eab7SMark Brown 	}
569*39b8eab7SMark Brown 
570*39b8eab7SMark Brown 	socdev->card->codec = wm9090_codec;
571*39b8eab7SMark Brown 	codec = wm9090_codec;
572*39b8eab7SMark Brown 
573*39b8eab7SMark Brown 	/* register pcms */
574*39b8eab7SMark Brown 	ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
575*39b8eab7SMark Brown 	if (ret < 0) {
576*39b8eab7SMark Brown 		dev_err(codec->dev, "failed to create pcms: %d\n", ret);
577*39b8eab7SMark Brown 		goto pcm_err;
578*39b8eab7SMark Brown 	}
579*39b8eab7SMark Brown 
580*39b8eab7SMark Brown 	wm9090_add_controls(codec);
581*39b8eab7SMark Brown 
582*39b8eab7SMark Brown 	return 0;
583*39b8eab7SMark Brown 
584*39b8eab7SMark Brown pcm_err:
585*39b8eab7SMark Brown 	return ret;
586*39b8eab7SMark Brown }
587*39b8eab7SMark Brown 
588*39b8eab7SMark Brown #ifdef CONFIG_PM
589*39b8eab7SMark Brown static int wm9090_suspend(struct platform_device *pdev, pm_message_t state)
590*39b8eab7SMark Brown {
591*39b8eab7SMark Brown 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
592*39b8eab7SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
593*39b8eab7SMark Brown 
594*39b8eab7SMark Brown 	wm9090_set_bias_level(codec, SND_SOC_BIAS_OFF);
595*39b8eab7SMark Brown 
596*39b8eab7SMark Brown 	return 0;
597*39b8eab7SMark Brown }
598*39b8eab7SMark Brown 
599*39b8eab7SMark Brown static int wm9090_resume(struct platform_device *pdev)
600*39b8eab7SMark Brown {
601*39b8eab7SMark Brown 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
602*39b8eab7SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
603*39b8eab7SMark Brown 
604*39b8eab7SMark Brown 	wm9090_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
605*39b8eab7SMark Brown 
606*39b8eab7SMark Brown 	return 0;
607*39b8eab7SMark Brown }
608*39b8eab7SMark Brown #else
609*39b8eab7SMark Brown #define wm9090_suspend NULL
610*39b8eab7SMark Brown #define wm9090_resume NULL
611*39b8eab7SMark Brown #endif
612*39b8eab7SMark Brown 
613*39b8eab7SMark Brown static int wm9090_remove(struct platform_device *pdev)
614*39b8eab7SMark Brown {
615*39b8eab7SMark Brown 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
616*39b8eab7SMark Brown 
617*39b8eab7SMark Brown 	snd_soc_free_pcms(socdev);
618*39b8eab7SMark Brown 	snd_soc_dapm_free(socdev);
619*39b8eab7SMark Brown 
620*39b8eab7SMark Brown 	return 0;
621*39b8eab7SMark Brown }
622*39b8eab7SMark Brown 
623*39b8eab7SMark Brown struct snd_soc_codec_device soc_codec_dev_wm9090 = {
624*39b8eab7SMark Brown 	.probe = 	wm9090_probe,
625*39b8eab7SMark Brown 	.remove = 	wm9090_remove,
626*39b8eab7SMark Brown 	.suspend = 	wm9090_suspend,
627*39b8eab7SMark Brown 	.resume =	wm9090_resume,
628*39b8eab7SMark Brown };
629*39b8eab7SMark Brown EXPORT_SYMBOL_GPL(soc_codec_dev_wm9090);
630*39b8eab7SMark Brown 
631*39b8eab7SMark Brown static int wm9090_i2c_probe(struct i2c_client *i2c,
632*39b8eab7SMark Brown 			    const struct i2c_device_id *id)
633*39b8eab7SMark Brown {
634*39b8eab7SMark Brown 	struct wm9090_priv *wm9090;
635*39b8eab7SMark Brown 	struct snd_soc_codec *codec;
636*39b8eab7SMark Brown 	int ret;
637*39b8eab7SMark Brown 
638*39b8eab7SMark Brown 	wm9090 = kzalloc(sizeof(*wm9090), GFP_KERNEL);
639*39b8eab7SMark Brown 	if (wm9090 == NULL) {
640*39b8eab7SMark Brown 		dev_err(&i2c->dev, "Can not allocate memory\n");
641*39b8eab7SMark Brown 		return -ENOMEM;
642*39b8eab7SMark Brown 	}
643*39b8eab7SMark Brown 	codec = &wm9090->codec;
644*39b8eab7SMark Brown 
645*39b8eab7SMark Brown 	if (i2c->dev.platform_data)
646*39b8eab7SMark Brown 		memcpy(&wm9090->pdata, i2c->dev.platform_data,
647*39b8eab7SMark Brown 		       sizeof(wm9090->pdata));
648*39b8eab7SMark Brown 
649*39b8eab7SMark Brown 	wm9090_codec = codec;
650*39b8eab7SMark Brown 
651*39b8eab7SMark Brown 	i2c_set_clientdata(i2c, wm9090);
652*39b8eab7SMark Brown 
653*39b8eab7SMark Brown 	mutex_init(&codec->mutex);
654*39b8eab7SMark Brown 	INIT_LIST_HEAD(&codec->dapm_widgets);
655*39b8eab7SMark Brown 	INIT_LIST_HEAD(&codec->dapm_paths);
656*39b8eab7SMark Brown 
657*39b8eab7SMark Brown 	codec->control_data = i2c;
658*39b8eab7SMark Brown 	snd_soc_codec_set_drvdata(codec, wm9090);
659*39b8eab7SMark Brown 	codec->dev = &i2c->dev;
660*39b8eab7SMark Brown 	codec->name = "WM9090";
661*39b8eab7SMark Brown 	codec->owner = THIS_MODULE;
662*39b8eab7SMark Brown 	codec->bias_level = SND_SOC_BIAS_OFF;
663*39b8eab7SMark Brown 	codec->set_bias_level = wm9090_set_bias_level,
664*39b8eab7SMark Brown 	codec->reg_cache_size = WM9090_MAX_REGISTER + 1;
665*39b8eab7SMark Brown 	codec->reg_cache = &wm9090->reg_cache;
666*39b8eab7SMark Brown 	codec->volatile_register = wm9090_volatile;
667*39b8eab7SMark Brown 
668*39b8eab7SMark Brown 	ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_I2C);
669*39b8eab7SMark Brown 	if (ret != 0) {
670*39b8eab7SMark Brown 		dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
671*39b8eab7SMark Brown 		goto err;
672*39b8eab7SMark Brown 	}
673*39b8eab7SMark Brown 
674*39b8eab7SMark Brown 	memcpy(&wm9090->reg_cache, wm9090_reg_defaults,
675*39b8eab7SMark Brown 	       sizeof(wm9090->reg_cache));
676*39b8eab7SMark Brown 
677*39b8eab7SMark Brown 	ret = snd_soc_read(codec, WM9090_SOFTWARE_RESET);
678*39b8eab7SMark Brown 	if (ret < 0)
679*39b8eab7SMark Brown 		goto err;
680*39b8eab7SMark Brown 	if (ret != wm9090_reg_defaults[WM9090_SOFTWARE_RESET]) {
681*39b8eab7SMark Brown 		dev_err(&i2c->dev, "Device is not a WM9090, ID=%x\n", ret);
682*39b8eab7SMark Brown 		ret = -EINVAL;
683*39b8eab7SMark Brown 		goto err;
684*39b8eab7SMark Brown 	}
685*39b8eab7SMark Brown 
686*39b8eab7SMark Brown 	ret = snd_soc_write(codec, WM9090_SOFTWARE_RESET, 0);
687*39b8eab7SMark Brown 	if (ret < 0)
688*39b8eab7SMark Brown 		goto err;
689*39b8eab7SMark Brown 
690*39b8eab7SMark Brown 	/* Configure some defaults; they will be written out when we
691*39b8eab7SMark Brown 	 * bring the bias up.
692*39b8eab7SMark Brown 	 */
693*39b8eab7SMark Brown 	wm9090->reg_cache[WM9090_IN1_LINE_INPUT_A_VOLUME] |= WM9090_IN1_VU
694*39b8eab7SMark Brown 		| WM9090_IN1A_ZC;
695*39b8eab7SMark Brown 	wm9090->reg_cache[WM9090_IN1_LINE_INPUT_B_VOLUME] |= WM9090_IN1_VU
696*39b8eab7SMark Brown 		| WM9090_IN1B_ZC;
697*39b8eab7SMark Brown 	wm9090->reg_cache[WM9090_IN2_LINE_INPUT_A_VOLUME] |= WM9090_IN2_VU
698*39b8eab7SMark Brown 		| WM9090_IN2A_ZC;
699*39b8eab7SMark Brown 	wm9090->reg_cache[WM9090_IN2_LINE_INPUT_B_VOLUME] |= WM9090_IN2_VU
700*39b8eab7SMark Brown 		| WM9090_IN2B_ZC;
701*39b8eab7SMark Brown 	wm9090->reg_cache[WM9090_SPEAKER_VOLUME_LEFT] |=
702*39b8eab7SMark Brown 		WM9090_SPKOUT_VU | WM9090_SPKOUTL_ZC;
703*39b8eab7SMark Brown 	wm9090->reg_cache[WM9090_LEFT_OUTPUT_VOLUME] |=
704*39b8eab7SMark Brown 		WM9090_HPOUT1_VU | WM9090_HPOUT1L_ZC;
705*39b8eab7SMark Brown 	wm9090->reg_cache[WM9090_RIGHT_OUTPUT_VOLUME] |=
706*39b8eab7SMark Brown 		WM9090_HPOUT1_VU | WM9090_HPOUT1R_ZC;
707*39b8eab7SMark Brown 
708*39b8eab7SMark Brown 	wm9090->reg_cache[WM9090_CLOCKING_1] |= WM9090_TOCLK_ENA;
709*39b8eab7SMark Brown 
710*39b8eab7SMark Brown 	wm9090_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
711*39b8eab7SMark Brown 
712*39b8eab7SMark Brown 	ret = snd_soc_register_codec(codec);
713*39b8eab7SMark Brown 	if (ret != 0) {
714*39b8eab7SMark Brown 		dev_err(&i2c->dev, "Failed to register CODEC: %d\n", ret);
715*39b8eab7SMark Brown 		goto err_bias;
716*39b8eab7SMark Brown 	}
717*39b8eab7SMark Brown 
718*39b8eab7SMark Brown 	return 0;
719*39b8eab7SMark Brown 
720*39b8eab7SMark Brown err_bias:
721*39b8eab7SMark Brown 	wm9090_set_bias_level(codec, SND_SOC_BIAS_OFF);
722*39b8eab7SMark Brown err:
723*39b8eab7SMark Brown 	kfree(wm9090);
724*39b8eab7SMark Brown 	i2c_set_clientdata(i2c, NULL);
725*39b8eab7SMark Brown 	wm9090_codec = NULL;
726*39b8eab7SMark Brown 
727*39b8eab7SMark Brown 	return ret;
728*39b8eab7SMark Brown }
729*39b8eab7SMark Brown 
730*39b8eab7SMark Brown static int wm9090_i2c_remove(struct i2c_client *i2c)
731*39b8eab7SMark Brown {
732*39b8eab7SMark Brown 	struct wm9090_priv *wm9090 = i2c_get_clientdata(i2c);
733*39b8eab7SMark Brown 	struct snd_soc_codec *codec = &wm9090->codec;
734*39b8eab7SMark Brown 
735*39b8eab7SMark Brown 	snd_soc_unregister_codec(codec);
736*39b8eab7SMark Brown 	wm9090_set_bias_level(codec, SND_SOC_BIAS_OFF);
737*39b8eab7SMark Brown 	kfree(wm9090);
738*39b8eab7SMark Brown 	wm9090_codec = NULL;
739*39b8eab7SMark Brown 
740*39b8eab7SMark Brown 	return 0;
741*39b8eab7SMark Brown }
742*39b8eab7SMark Brown 
743*39b8eab7SMark Brown static const struct i2c_device_id wm9090_id[] = {
744*39b8eab7SMark Brown 	{ "wm9090", 0 },
745*39b8eab7SMark Brown 	{ }
746*39b8eab7SMark Brown };
747*39b8eab7SMark Brown MODULE_DEVICE_TABLE(i2c, wm9090_id);
748*39b8eab7SMark Brown 
749*39b8eab7SMark Brown static struct i2c_driver wm9090_i2c_driver = {
750*39b8eab7SMark Brown 	.driver = {
751*39b8eab7SMark Brown 		.name = "wm9090",
752*39b8eab7SMark Brown 		.owner = THIS_MODULE,
753*39b8eab7SMark Brown 	},
754*39b8eab7SMark Brown 	.probe = wm9090_i2c_probe,
755*39b8eab7SMark Brown 	.remove = __devexit_p(wm9090_i2c_remove),
756*39b8eab7SMark Brown 	.id_table = wm9090_id,
757*39b8eab7SMark Brown };
758*39b8eab7SMark Brown 
759*39b8eab7SMark Brown static int __init wm9090_init(void)
760*39b8eab7SMark Brown {
761*39b8eab7SMark Brown 	return i2c_add_driver(&wm9090_i2c_driver);
762*39b8eab7SMark Brown }
763*39b8eab7SMark Brown module_init(wm9090_init);
764*39b8eab7SMark Brown 
765*39b8eab7SMark Brown static void __exit wm9090_exit(void)
766*39b8eab7SMark Brown {
767*39b8eab7SMark Brown 	i2c_del_driver(&wm9090_i2c_driver);
768*39b8eab7SMark Brown }
769*39b8eab7SMark Brown module_exit(wm9090_exit);
770*39b8eab7SMark Brown 
771*39b8eab7SMark Brown MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
772*39b8eab7SMark Brown MODULE_DESCRIPTION("WM9090 ASoC driver");
773*39b8eab7SMark Brown MODULE_LICENSE("GPL");
774