xref: /openbmc/linux/sound/soc/codecs/wm9090.c (revision 2359ccdd)
1 /*
2  * ALSA SoC WM9090 driver
3  *
4  * Copyright 2009-12 Wolfson Microelectronics
5  *
6  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  */
22 
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/device.h>
26 #include <linux/i2c.h>
27 #include <linux/delay.h>
28 #include <linux/regmap.h>
29 #include <linux/slab.h>
30 #include <sound/initval.h>
31 #include <sound/soc.h>
32 #include <sound/tlv.h>
33 #include <sound/wm9090.h>
34 
35 #include "wm9090.h"
36 
37 static const struct reg_default wm9090_reg_defaults[] = {
38 	{ 1,  0x0006 },     /* R1   - Power Management (1) */
39 	{ 2,  0x6000 },     /* R2   - Power Management (2) */
40 	{ 3,  0x0000 },     /* R3   - Power Management (3) */
41 	{ 6,  0x01C0 },     /* R6   - Clocking 1 */
42 	{ 22, 0x0003 },     /* R22  - IN1 Line Control */
43 	{ 23, 0x0003 },     /* R23  - IN2 Line Control */
44 	{ 24, 0x0083 },     /* R24  - IN1 Line Input A Volume */
45 	{ 25, 0x0083 },     /* R25  - IN1  Line Input B Volume */
46 	{ 26, 0x0083 },     /* R26  - IN2 Line Input A Volume */
47 	{ 27, 0x0083 },     /* R27  - IN2 Line Input B Volume */
48 	{ 28, 0x002D },     /* R28  - Left Output Volume */
49 	{ 29, 0x002D },     /* R29  - Right Output Volume */
50 	{ 34, 0x0100 },     /* R34  - SPKMIXL Attenuation */
51 	{ 35, 0x0010 },     /* R36  - SPKOUT Mixers */
52 	{ 37, 0x0140 },     /* R37  - ClassD3 */
53 	{ 38, 0x0039 },     /* R38  - Speaker Volume Left */
54 	{ 45, 0x0000 },     /* R45  - Output Mixer1 */
55 	{ 46, 0x0000 },     /* R46  - Output Mixer2 */
56 	{ 47, 0x0100 },     /* R47  - Output Mixer3 */
57 	{ 48, 0x0100 },     /* R48  - Output Mixer4 */
58 	{ 54, 0x0000 },     /* R54  - Speaker Mixer */
59 	{ 57, 0x000D },     /* R57  - AntiPOP2 */
60 	{ 70, 0x0000 },     /* R70  - Write Sequencer 0 */
61 	{ 71, 0x0000 },     /* R71  - Write Sequencer 1 */
62 	{ 72, 0x0000 },     /* R72  - Write Sequencer 2 */
63 	{ 73, 0x0000 },     /* R73  - Write Sequencer 3 */
64 	{ 74, 0x0000 },     /* R74  - Write Sequencer 4 */
65 	{ 75, 0x0000 },     /* R75  - Write Sequencer 5 */
66 	{ 76, 0x1F25 },     /* R76  - Charge Pump 1 */
67 	{ 85, 0x054A },     /* R85  - DC Servo 1 */
68 	{ 87, 0x0000 },     /* R87  - DC Servo 3 */
69 	{ 96, 0x0100 },     /* R96  - Analogue HP 0 */
70 	{ 98, 0x8640 },     /* R98  - AGC Control 0 */
71 	{ 99, 0xC000 },     /* R99  - AGC Control 1 */
72 	{ 100, 0x0200 },     /* R100 - AGC Control 2 */
73 };
74 
75 /* This struct is used to save the context */
76 struct wm9090_priv {
77 	struct wm9090_platform_data pdata;
78 	struct regmap *regmap;
79 };
80 
81 static bool wm9090_volatile(struct device *dev, unsigned int reg)
82 {
83 	switch (reg) {
84 	case WM9090_SOFTWARE_RESET:
85 	case WM9090_DC_SERVO_0:
86 	case WM9090_DC_SERVO_READBACK_0:
87 	case WM9090_DC_SERVO_READBACK_1:
88 	case WM9090_DC_SERVO_READBACK_2:
89 		return true;
90 
91 	default:
92 		return false;
93 	}
94 }
95 
96 static bool wm9090_readable(struct device *dev, unsigned int reg)
97 {
98 	switch (reg) {
99 	case WM9090_SOFTWARE_RESET:
100 	case WM9090_POWER_MANAGEMENT_1:
101 	case WM9090_POWER_MANAGEMENT_2:
102 	case WM9090_POWER_MANAGEMENT_3:
103 	case WM9090_CLOCKING_1:
104 	case WM9090_IN1_LINE_CONTROL:
105 	case WM9090_IN2_LINE_CONTROL:
106 	case WM9090_IN1_LINE_INPUT_A_VOLUME:
107 	case WM9090_IN1_LINE_INPUT_B_VOLUME:
108 	case WM9090_IN2_LINE_INPUT_A_VOLUME:
109 	case WM9090_IN2_LINE_INPUT_B_VOLUME:
110 	case WM9090_LEFT_OUTPUT_VOLUME:
111 	case WM9090_RIGHT_OUTPUT_VOLUME:
112 	case WM9090_SPKMIXL_ATTENUATION:
113 	case WM9090_SPKOUT_MIXERS:
114 	case WM9090_CLASSD3:
115 	case WM9090_SPEAKER_VOLUME_LEFT:
116 	case WM9090_OUTPUT_MIXER1:
117 	case WM9090_OUTPUT_MIXER2:
118 	case WM9090_OUTPUT_MIXER3:
119 	case WM9090_OUTPUT_MIXER4:
120 	case WM9090_SPEAKER_MIXER:
121 	case WM9090_ANTIPOP2:
122 	case WM9090_WRITE_SEQUENCER_0:
123 	case WM9090_WRITE_SEQUENCER_1:
124 	case WM9090_WRITE_SEQUENCER_2:
125 	case WM9090_WRITE_SEQUENCER_3:
126 	case WM9090_WRITE_SEQUENCER_4:
127 	case WM9090_WRITE_SEQUENCER_5:
128 	case WM9090_CHARGE_PUMP_1:
129 	case WM9090_DC_SERVO_0:
130 	case WM9090_DC_SERVO_1:
131 	case WM9090_DC_SERVO_3:
132 	case WM9090_DC_SERVO_READBACK_0:
133 	case WM9090_DC_SERVO_READBACK_1:
134 	case WM9090_DC_SERVO_READBACK_2:
135 	case WM9090_ANALOGUE_HP_0:
136 	case WM9090_AGC_CONTROL_0:
137 	case WM9090_AGC_CONTROL_1:
138 	case WM9090_AGC_CONTROL_2:
139 		return true;
140 
141 	default:
142 		return false;
143 	}
144 }
145 
146 static void wait_for_dc_servo(struct snd_soc_component *component)
147 {
148 	unsigned int reg;
149 	int count = 0;
150 
151 	dev_dbg(component->dev, "Waiting for DC servo...\n");
152 	do {
153 		count++;
154 		msleep(1);
155 		reg = snd_soc_component_read32(component, WM9090_DC_SERVO_READBACK_0);
156 		dev_dbg(component->dev, "DC servo status: %x\n", reg);
157 	} while ((reg & WM9090_DCS_CAL_COMPLETE_MASK)
158 		 != WM9090_DCS_CAL_COMPLETE_MASK && count < 1000);
159 
160 	if ((reg & WM9090_DCS_CAL_COMPLETE_MASK)
161 	    != WM9090_DCS_CAL_COMPLETE_MASK)
162 		dev_err(component->dev, "Timed out waiting for DC Servo\n");
163 }
164 
165 static const DECLARE_TLV_DB_RANGE(in_tlv,
166 	0, 0, TLV_DB_SCALE_ITEM(-600, 0, 0),
167 	1, 3, TLV_DB_SCALE_ITEM(-350, 350, 0),
168 	4, 6, TLV_DB_SCALE_ITEM(600, 600, 0)
169 );
170 static const DECLARE_TLV_DB_RANGE(mix_tlv,
171 	0, 2, TLV_DB_SCALE_ITEM(-1200, 300, 0),
172 	3, 3, TLV_DB_SCALE_ITEM(0, 0, 0)
173 );
174 static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
175 static const DECLARE_TLV_DB_RANGE(spkboost_tlv,
176 	0, 6, TLV_DB_SCALE_ITEM(0, 150, 0),
177 	7, 7, TLV_DB_SCALE_ITEM(1200, 0, 0)
178 );
179 
180 static const struct snd_kcontrol_new wm9090_controls[] = {
181 SOC_SINGLE_TLV("IN1A Volume", WM9090_IN1_LINE_INPUT_A_VOLUME, 0, 6, 0,
182 	       in_tlv),
183 SOC_SINGLE("IN1A Switch", WM9090_IN1_LINE_INPUT_A_VOLUME, 7, 1, 1),
184 SOC_SINGLE("IN1A ZC Switch", WM9090_IN1_LINE_INPUT_A_VOLUME, 6, 1, 0),
185 
186 SOC_SINGLE_TLV("IN2A Volume", WM9090_IN2_LINE_INPUT_A_VOLUME, 0, 6, 0,
187 	       in_tlv),
188 SOC_SINGLE("IN2A Switch", WM9090_IN2_LINE_INPUT_A_VOLUME, 7, 1, 1),
189 SOC_SINGLE("IN2A ZC Switch", WM9090_IN2_LINE_INPUT_A_VOLUME, 6, 1, 0),
190 
191 SOC_SINGLE("MIXOUTL Switch", WM9090_OUTPUT_MIXER3, 8, 1, 1),
192 SOC_SINGLE_TLV("MIXOUTL IN1A Volume", WM9090_OUTPUT_MIXER3, 6, 3, 1,
193 	       mix_tlv),
194 SOC_SINGLE_TLV("MIXOUTL IN2A Volume", WM9090_OUTPUT_MIXER3, 2, 3, 1,
195 	       mix_tlv),
196 
197 SOC_SINGLE("MIXOUTR Switch", WM9090_OUTPUT_MIXER4, 8, 1, 1),
198 SOC_SINGLE_TLV("MIXOUTR IN1A Volume", WM9090_OUTPUT_MIXER4, 6, 3, 1,
199 	       mix_tlv),
200 SOC_SINGLE_TLV("MIXOUTR IN2A Volume", WM9090_OUTPUT_MIXER4, 2, 3, 1,
201 	       mix_tlv),
202 
203 SOC_SINGLE("SPKMIX Switch", WM9090_SPKMIXL_ATTENUATION, 8, 1, 1),
204 SOC_SINGLE_TLV("SPKMIX IN1A Volume", WM9090_SPKMIXL_ATTENUATION, 6, 3, 1,
205 	       mix_tlv),
206 SOC_SINGLE_TLV("SPKMIX IN2A Volume", WM9090_SPKMIXL_ATTENUATION, 2, 3, 1,
207 	       mix_tlv),
208 
209 SOC_DOUBLE_R_TLV("Headphone Volume", WM9090_LEFT_OUTPUT_VOLUME,
210 		 WM9090_RIGHT_OUTPUT_VOLUME, 0, 63, 0, out_tlv),
211 SOC_DOUBLE_R("Headphone Switch", WM9090_LEFT_OUTPUT_VOLUME,
212 	     WM9090_RIGHT_OUTPUT_VOLUME, 6, 1, 1),
213 SOC_DOUBLE_R("Headphone ZC Switch", WM9090_LEFT_OUTPUT_VOLUME,
214 	     WM9090_RIGHT_OUTPUT_VOLUME, 7, 1, 0),
215 
216 SOC_SINGLE_TLV("Speaker Volume", WM9090_SPEAKER_VOLUME_LEFT, 0, 63, 0,
217 	       out_tlv),
218 SOC_SINGLE("Speaker Switch", WM9090_SPEAKER_VOLUME_LEFT, 6, 1, 1),
219 SOC_SINGLE("Speaker ZC Switch", WM9090_SPEAKER_VOLUME_LEFT, 7, 1, 0),
220 SOC_SINGLE_TLV("Speaker Boost Volume", WM9090_CLASSD3, 3, 7, 0, spkboost_tlv),
221 };
222 
223 static const struct snd_kcontrol_new wm9090_in1_se_controls[] = {
224 SOC_SINGLE_TLV("IN1B Volume", WM9090_IN1_LINE_INPUT_B_VOLUME, 0, 6, 0,
225 	       in_tlv),
226 SOC_SINGLE("IN1B Switch", WM9090_IN1_LINE_INPUT_B_VOLUME, 7, 1, 1),
227 SOC_SINGLE("IN1B ZC Switch", WM9090_IN1_LINE_INPUT_B_VOLUME, 6, 1, 0),
228 
229 SOC_SINGLE_TLV("SPKMIX IN1B Volume", WM9090_SPKMIXL_ATTENUATION, 4, 3, 1,
230 	       mix_tlv),
231 SOC_SINGLE_TLV("MIXOUTL IN1B Volume", WM9090_OUTPUT_MIXER3, 4, 3, 1,
232 	       mix_tlv),
233 SOC_SINGLE_TLV("MIXOUTR IN1B Volume", WM9090_OUTPUT_MIXER4, 4, 3, 1,
234 	       mix_tlv),
235 };
236 
237 static const struct snd_kcontrol_new wm9090_in2_se_controls[] = {
238 SOC_SINGLE_TLV("IN2B Volume", WM9090_IN2_LINE_INPUT_B_VOLUME, 0, 6, 0,
239 	       in_tlv),
240 SOC_SINGLE("IN2B Switch", WM9090_IN2_LINE_INPUT_B_VOLUME, 7, 1, 1),
241 SOC_SINGLE("IN2B ZC Switch", WM9090_IN2_LINE_INPUT_B_VOLUME, 6, 1, 0),
242 
243 SOC_SINGLE_TLV("SPKMIX IN2B Volume", WM9090_SPKMIXL_ATTENUATION, 0, 3, 1,
244 	       mix_tlv),
245 SOC_SINGLE_TLV("MIXOUTL IN2B Volume", WM9090_OUTPUT_MIXER3, 0, 3, 1,
246 	       mix_tlv),
247 SOC_SINGLE_TLV("MIXOUTR IN2B Volume", WM9090_OUTPUT_MIXER4, 0, 3, 1,
248 	       mix_tlv),
249 };
250 
251 static int hp_ev(struct snd_soc_dapm_widget *w,
252 		 struct snd_kcontrol *kcontrol, int event)
253 {
254 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
255 	unsigned int reg = snd_soc_component_read32(component, WM9090_ANALOGUE_HP_0);
256 
257 	switch (event) {
258 	case SND_SOC_DAPM_POST_PMU:
259 		snd_soc_component_update_bits(component, WM9090_CHARGE_PUMP_1,
260 				    WM9090_CP_ENA, WM9090_CP_ENA);
261 
262 		msleep(5);
263 
264 		snd_soc_component_update_bits(component, WM9090_POWER_MANAGEMENT_1,
265 				    WM9090_HPOUT1L_ENA | WM9090_HPOUT1R_ENA,
266 				    WM9090_HPOUT1L_ENA | WM9090_HPOUT1R_ENA);
267 
268 		reg |= WM9090_HPOUT1L_DLY | WM9090_HPOUT1R_DLY;
269 		snd_soc_component_write(component, WM9090_ANALOGUE_HP_0, reg);
270 
271 		/* Start the DC servo.  We don't currently use the
272 		 * ability to save the state since we don't have full
273 		 * control of the analogue paths and they can change
274 		 * DC offsets; see the WM8904 driver for an example of
275 		 * doing so.
276 		 */
277 		snd_soc_component_write(component, WM9090_DC_SERVO_0,
278 			      WM9090_DCS_ENA_CHAN_0 |
279 			      WM9090_DCS_ENA_CHAN_1 |
280 			      WM9090_DCS_TRIG_STARTUP_1 |
281 			      WM9090_DCS_TRIG_STARTUP_0);
282 		wait_for_dc_servo(component);
283 
284 		reg |= WM9090_HPOUT1R_OUTP | WM9090_HPOUT1R_RMV_SHORT |
285 			WM9090_HPOUT1L_OUTP | WM9090_HPOUT1L_RMV_SHORT;
286 		snd_soc_component_write(component, WM9090_ANALOGUE_HP_0, reg);
287 		break;
288 
289 	case SND_SOC_DAPM_PRE_PMD:
290 		reg &= ~(WM9090_HPOUT1L_RMV_SHORT |
291 			 WM9090_HPOUT1L_DLY |
292 			 WM9090_HPOUT1L_OUTP |
293 			 WM9090_HPOUT1R_RMV_SHORT |
294 			 WM9090_HPOUT1R_DLY |
295 			 WM9090_HPOUT1R_OUTP);
296 
297 		snd_soc_component_write(component, WM9090_ANALOGUE_HP_0, reg);
298 
299 		snd_soc_component_write(component, WM9090_DC_SERVO_0, 0);
300 
301 		snd_soc_component_update_bits(component, WM9090_POWER_MANAGEMENT_1,
302 				    WM9090_HPOUT1L_ENA | WM9090_HPOUT1R_ENA,
303 				    0);
304 
305 		snd_soc_component_update_bits(component, WM9090_CHARGE_PUMP_1,
306 				    WM9090_CP_ENA, 0);
307 		break;
308 	}
309 
310 	return 0;
311 }
312 
313 static const struct snd_kcontrol_new spkmix[] = {
314 SOC_DAPM_SINGLE("IN1A Switch", WM9090_SPEAKER_MIXER, 6, 1, 0),
315 SOC_DAPM_SINGLE("IN1B Switch", WM9090_SPEAKER_MIXER, 4, 1, 0),
316 SOC_DAPM_SINGLE("IN2A Switch", WM9090_SPEAKER_MIXER, 2, 1, 0),
317 SOC_DAPM_SINGLE("IN2B Switch", WM9090_SPEAKER_MIXER, 0, 1, 0),
318 };
319 
320 static const struct snd_kcontrol_new spkout[] = {
321 SOC_DAPM_SINGLE("Mixer Switch", WM9090_SPKOUT_MIXERS, 4, 1, 0),
322 };
323 
324 static const struct snd_kcontrol_new mixoutl[] = {
325 SOC_DAPM_SINGLE("IN1A Switch", WM9090_OUTPUT_MIXER1, 6, 1, 0),
326 SOC_DAPM_SINGLE("IN1B Switch", WM9090_OUTPUT_MIXER1, 4, 1, 0),
327 SOC_DAPM_SINGLE("IN2A Switch", WM9090_OUTPUT_MIXER1, 2, 1, 0),
328 SOC_DAPM_SINGLE("IN2B Switch", WM9090_OUTPUT_MIXER1, 0, 1, 0),
329 };
330 
331 static const struct snd_kcontrol_new mixoutr[] = {
332 SOC_DAPM_SINGLE("IN1A Switch", WM9090_OUTPUT_MIXER2, 6, 1, 0),
333 SOC_DAPM_SINGLE("IN1B Switch", WM9090_OUTPUT_MIXER2, 4, 1, 0),
334 SOC_DAPM_SINGLE("IN2A Switch", WM9090_OUTPUT_MIXER2, 2, 1, 0),
335 SOC_DAPM_SINGLE("IN2B Switch", WM9090_OUTPUT_MIXER2, 0, 1, 0),
336 };
337 
338 static const struct snd_soc_dapm_widget wm9090_dapm_widgets[] = {
339 SND_SOC_DAPM_INPUT("IN1+"),
340 SND_SOC_DAPM_INPUT("IN1-"),
341 SND_SOC_DAPM_INPUT("IN2+"),
342 SND_SOC_DAPM_INPUT("IN2-"),
343 
344 SND_SOC_DAPM_SUPPLY("OSC", WM9090_POWER_MANAGEMENT_1, 3, 0, NULL, 0),
345 
346 SND_SOC_DAPM_PGA("IN1A PGA", WM9090_POWER_MANAGEMENT_2, 7, 0, NULL, 0),
347 SND_SOC_DAPM_PGA("IN1B PGA", WM9090_POWER_MANAGEMENT_2, 6, 0, NULL, 0),
348 SND_SOC_DAPM_PGA("IN2A PGA", WM9090_POWER_MANAGEMENT_2, 5, 0, NULL, 0),
349 SND_SOC_DAPM_PGA("IN2B PGA", WM9090_POWER_MANAGEMENT_2, 4, 0, NULL, 0),
350 
351 SND_SOC_DAPM_MIXER("SPKMIX", WM9090_POWER_MANAGEMENT_3, 3, 0,
352 		   spkmix, ARRAY_SIZE(spkmix)),
353 SND_SOC_DAPM_MIXER("MIXOUTL", WM9090_POWER_MANAGEMENT_3, 5, 0,
354 		   mixoutl, ARRAY_SIZE(mixoutl)),
355 SND_SOC_DAPM_MIXER("MIXOUTR", WM9090_POWER_MANAGEMENT_3, 4, 0,
356 		   mixoutr, ARRAY_SIZE(mixoutr)),
357 
358 SND_SOC_DAPM_PGA_E("HP PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
359 		   hp_ev, SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
360 
361 SND_SOC_DAPM_PGA("SPKPGA", WM9090_POWER_MANAGEMENT_3, 8, 0, NULL, 0),
362 SND_SOC_DAPM_MIXER("SPKOUT", WM9090_POWER_MANAGEMENT_1, 12, 0,
363 		   spkout, ARRAY_SIZE(spkout)),
364 
365 SND_SOC_DAPM_OUTPUT("HPR"),
366 SND_SOC_DAPM_OUTPUT("HPL"),
367 SND_SOC_DAPM_OUTPUT("Speaker"),
368 };
369 
370 static const struct snd_soc_dapm_route audio_map[] = {
371 	{ "IN1A PGA", NULL, "IN1+" },
372 	{ "IN2A PGA", NULL, "IN2+" },
373 
374 	{ "SPKMIX", "IN1A Switch", "IN1A PGA" },
375 	{ "SPKMIX", "IN2A Switch", "IN2A PGA" },
376 
377 	{ "MIXOUTL", "IN1A Switch", "IN1A PGA" },
378 	{ "MIXOUTL", "IN2A Switch", "IN2A PGA" },
379 
380 	{ "MIXOUTR", "IN1A Switch", "IN1A PGA" },
381 	{ "MIXOUTR", "IN2A Switch", "IN2A PGA" },
382 
383 	{ "HP PGA", NULL, "OSC" },
384 	{ "HP PGA", NULL, "MIXOUTL" },
385 	{ "HP PGA", NULL, "MIXOUTR" },
386 
387 	{ "HPL", NULL, "HP PGA" },
388 	{ "HPR", NULL, "HP PGA" },
389 
390 	{ "SPKPGA", NULL, "OSC" },
391 	{ "SPKPGA", NULL, "SPKMIX" },
392 
393 	{ "SPKOUT", "Mixer Switch", "SPKPGA" },
394 
395 	{ "Speaker", NULL, "SPKOUT" },
396 };
397 
398 static const struct snd_soc_dapm_route audio_map_in1_se[] = {
399 	{ "IN1B PGA", NULL, "IN1-" },
400 
401 	{ "SPKMIX", "IN1B Switch", "IN1B PGA" },
402 	{ "MIXOUTL", "IN1B Switch", "IN1B PGA" },
403 	{ "MIXOUTR", "IN1B Switch", "IN1B PGA" },
404 };
405 
406 static const struct snd_soc_dapm_route audio_map_in1_diff[] = {
407 	{ "IN1A PGA", NULL, "IN1-" },
408 };
409 
410 static const struct snd_soc_dapm_route audio_map_in2_se[] = {
411 	{ "IN2B PGA", NULL, "IN2-" },
412 
413 	{ "SPKMIX", "IN2B Switch", "IN2B PGA" },
414 	{ "MIXOUTL", "IN2B Switch", "IN2B PGA" },
415 	{ "MIXOUTR", "IN2B Switch", "IN2B PGA" },
416 };
417 
418 static const struct snd_soc_dapm_route audio_map_in2_diff[] = {
419 	{ "IN2A PGA", NULL, "IN2-" },
420 };
421 
422 static int wm9090_add_controls(struct snd_soc_component *component)
423 {
424 	struct wm9090_priv *wm9090 = snd_soc_component_get_drvdata(component);
425 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
426 	int i;
427 
428 	snd_soc_dapm_new_controls(dapm, wm9090_dapm_widgets,
429 				  ARRAY_SIZE(wm9090_dapm_widgets));
430 
431 	snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
432 
433 	snd_soc_add_component_controls(component, wm9090_controls,
434 			     ARRAY_SIZE(wm9090_controls));
435 
436 	if (wm9090->pdata.lin1_diff) {
437 		snd_soc_dapm_add_routes(dapm, audio_map_in1_diff,
438 					ARRAY_SIZE(audio_map_in1_diff));
439 	} else {
440 		snd_soc_dapm_add_routes(dapm, audio_map_in1_se,
441 					ARRAY_SIZE(audio_map_in1_se));
442 		snd_soc_add_component_controls(component, wm9090_in1_se_controls,
443 				     ARRAY_SIZE(wm9090_in1_se_controls));
444 	}
445 
446 	if (wm9090->pdata.lin2_diff) {
447 		snd_soc_dapm_add_routes(dapm, audio_map_in2_diff,
448 					ARRAY_SIZE(audio_map_in2_diff));
449 	} else {
450 		snd_soc_dapm_add_routes(dapm, audio_map_in2_se,
451 					ARRAY_SIZE(audio_map_in2_se));
452 		snd_soc_add_component_controls(component, wm9090_in2_se_controls,
453 				     ARRAY_SIZE(wm9090_in2_se_controls));
454 	}
455 
456 	if (wm9090->pdata.agc_ena) {
457 		for (i = 0; i < ARRAY_SIZE(wm9090->pdata.agc); i++)
458 			snd_soc_component_write(component, WM9090_AGC_CONTROL_0 + i,
459 				      wm9090->pdata.agc[i]);
460 		snd_soc_component_update_bits(component, WM9090_POWER_MANAGEMENT_3,
461 				    WM9090_AGC_ENA, WM9090_AGC_ENA);
462 	} else {
463 		snd_soc_component_update_bits(component, WM9090_POWER_MANAGEMENT_3,
464 				    WM9090_AGC_ENA, 0);
465 	}
466 
467 	return 0;
468 
469 }
470 
471 /*
472  * The machine driver should call this from their set_bias_level; if there
473  * isn't one then this can just be set as the set_bias_level function.
474  */
475 static int wm9090_set_bias_level(struct snd_soc_component *component,
476 				 enum snd_soc_bias_level level)
477 {
478 	struct wm9090_priv *wm9090 = snd_soc_component_get_drvdata(component);
479 
480 	switch (level) {
481 	case SND_SOC_BIAS_ON:
482 		break;
483 
484 	case SND_SOC_BIAS_PREPARE:
485 		snd_soc_component_update_bits(component, WM9090_ANTIPOP2, WM9090_VMID_ENA,
486 				    WM9090_VMID_ENA);
487 		snd_soc_component_update_bits(component, WM9090_POWER_MANAGEMENT_1,
488 				    WM9090_BIAS_ENA |
489 				    WM9090_VMID_RES_MASK,
490 				    WM9090_BIAS_ENA |
491 				    1 << WM9090_VMID_RES_SHIFT);
492 		msleep(1);  /* Probably an overestimate */
493 		break;
494 
495 	case SND_SOC_BIAS_STANDBY:
496 		if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
497 			/* Restore the register cache */
498 			regcache_sync(wm9090->regmap);
499 		}
500 
501 		/* We keep VMID off during standby since the combination of
502 		 * ground referenced outputs and class D speaker mean that
503 		 * latency is not an issue.
504 		 */
505 		snd_soc_component_update_bits(component, WM9090_POWER_MANAGEMENT_1,
506 				    WM9090_BIAS_ENA | WM9090_VMID_RES_MASK, 0);
507 		snd_soc_component_update_bits(component, WM9090_ANTIPOP2,
508 				    WM9090_VMID_ENA, 0);
509 		break;
510 
511 	case SND_SOC_BIAS_OFF:
512 		break;
513 	}
514 
515 	return 0;
516 }
517 
518 static int wm9090_probe(struct snd_soc_component *component)
519 {
520 	/* Configure some defaults; they will be written out when we
521 	 * bring the bias up.
522 	 */
523 	snd_soc_component_update_bits(component, WM9090_IN1_LINE_INPUT_A_VOLUME,
524 			    WM9090_IN1_VU | WM9090_IN1A_ZC,
525 			    WM9090_IN1_VU | WM9090_IN1A_ZC);
526 	snd_soc_component_update_bits(component, WM9090_IN1_LINE_INPUT_B_VOLUME,
527 			    WM9090_IN1_VU | WM9090_IN1B_ZC,
528 			    WM9090_IN1_VU | WM9090_IN1B_ZC);
529 	snd_soc_component_update_bits(component, WM9090_IN2_LINE_INPUT_A_VOLUME,
530 			    WM9090_IN2_VU | WM9090_IN2A_ZC,
531 			    WM9090_IN2_VU | WM9090_IN2A_ZC);
532 	snd_soc_component_update_bits(component, WM9090_IN2_LINE_INPUT_B_VOLUME,
533 			    WM9090_IN2_VU | WM9090_IN2B_ZC,
534 			    WM9090_IN2_VU | WM9090_IN2B_ZC);
535 	snd_soc_component_update_bits(component, WM9090_SPEAKER_VOLUME_LEFT,
536 			    WM9090_SPKOUT_VU | WM9090_SPKOUTL_ZC,
537 			    WM9090_SPKOUT_VU | WM9090_SPKOUTL_ZC);
538 	snd_soc_component_update_bits(component, WM9090_LEFT_OUTPUT_VOLUME,
539 			    WM9090_HPOUT1_VU | WM9090_HPOUT1L_ZC,
540 			    WM9090_HPOUT1_VU | WM9090_HPOUT1L_ZC);
541 	snd_soc_component_update_bits(component, WM9090_RIGHT_OUTPUT_VOLUME,
542 			    WM9090_HPOUT1_VU | WM9090_HPOUT1R_ZC,
543 			    WM9090_HPOUT1_VU | WM9090_HPOUT1R_ZC);
544 
545 	snd_soc_component_update_bits(component, WM9090_CLOCKING_1,
546 			    WM9090_TOCLK_ENA, WM9090_TOCLK_ENA);
547 
548 	wm9090_add_controls(component);
549 
550 	return 0;
551 }
552 
553 static const struct snd_soc_component_driver soc_component_dev_wm9090 = {
554 	.probe			= wm9090_probe,
555 	.set_bias_level		= wm9090_set_bias_level,
556 	.suspend_bias_off	= 1,
557 	.idle_bias_on		= 1,
558 	.use_pmdown_time	= 1,
559 	.endianness		= 1,
560 	.non_legacy_dai_naming	= 1,
561 };
562 
563 static const struct regmap_config wm9090_regmap = {
564 	.reg_bits = 8,
565 	.val_bits = 16,
566 
567 	.max_register = WM9090_MAX_REGISTER,
568 	.volatile_reg = wm9090_volatile,
569 	.readable_reg = wm9090_readable,
570 
571 	.cache_type = REGCACHE_RBTREE,
572 	.reg_defaults = wm9090_reg_defaults,
573 	.num_reg_defaults = ARRAY_SIZE(wm9090_reg_defaults),
574 };
575 
576 
577 static int wm9090_i2c_probe(struct i2c_client *i2c,
578 			    const struct i2c_device_id *id)
579 {
580 	struct wm9090_priv *wm9090;
581 	unsigned int reg;
582 	int ret;
583 
584 	wm9090 = devm_kzalloc(&i2c->dev, sizeof(*wm9090), GFP_KERNEL);
585 	if (!wm9090)
586 		return -ENOMEM;
587 
588 	wm9090->regmap = devm_regmap_init_i2c(i2c, &wm9090_regmap);
589 	if (IS_ERR(wm9090->regmap)) {
590 		ret = PTR_ERR(wm9090->regmap);
591 		dev_err(&i2c->dev, "Failed to allocate regmap: %d\n", ret);
592 		return ret;
593 	}
594 
595 	ret = regmap_read(wm9090->regmap, WM9090_SOFTWARE_RESET, &reg);
596 	if (ret < 0)
597 		return ret;
598 
599 	if (reg != 0x9093) {
600 		dev_err(&i2c->dev, "Device is not a WM9090, ID=%x\n", reg);
601 		return -ENODEV;
602 	}
603 
604 	ret = regmap_write(wm9090->regmap, WM9090_SOFTWARE_RESET, 0);
605 	if (ret < 0)
606 		return ret;
607 
608 	if (i2c->dev.platform_data)
609 		memcpy(&wm9090->pdata, i2c->dev.platform_data,
610 		       sizeof(wm9090->pdata));
611 
612 	i2c_set_clientdata(i2c, wm9090);
613 
614 	ret =  devm_snd_soc_register_component(&i2c->dev,
615 			&soc_component_dev_wm9090,  NULL, 0);
616 	if (ret != 0) {
617 		dev_err(&i2c->dev, "Failed to register CODEC: %d\n", ret);
618 		return ret;
619 	}
620 
621 	return 0;
622 }
623 
624 static const struct i2c_device_id wm9090_id[] = {
625 	{ "wm9090", 0 },
626 	{ "wm9093", 0 },
627 	{ }
628 };
629 MODULE_DEVICE_TABLE(i2c, wm9090_id);
630 
631 static struct i2c_driver wm9090_i2c_driver = {
632 	.driver = {
633 		.name = "wm9090",
634 	},
635 	.probe = wm9090_i2c_probe,
636 	.id_table = wm9090_id,
637 };
638 
639 module_i2c_driver(wm9090_i2c_driver);
640 
641 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
642 MODULE_DESCRIPTION("WM9090 ASoC driver");
643 MODULE_LICENSE("GPL");
644