blob: f5d73ed72cbd2a3bc9319385249c36ac7a8a3b37 [file] [log] [blame]
Mark Brownf1c0a022008-08-26 13:05:27 +01001/*
2 * wm8903.c -- WM8903 ALSA SoC Audio driver
3 *
4 * Copyright 2008 Wolfson Microelectronics
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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 * TODO:
13 * - TDM mode configuration.
Mark Brownf1c0a022008-08-26 13:05:27 +010014 * - Digital microphone support.
Mark Brownf1c0a022008-08-26 13:05:27 +010015 */
16
17#include <linux/module.h>
18#include <linux/moduleparam.h>
19#include <linux/init.h>
Mark Brown8abd16a2010-03-15 18:25:26 +000020#include <linux/completion.h>
Mark Brownf1c0a022008-08-26 13:05:27 +010021#include <linux/delay.h>
22#include <linux/pm.h>
23#include <linux/i2c.h>
24#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Mark Brownf1c0a022008-08-26 13:05:27 +010026#include <sound/core.h>
Mark Brown72453872010-03-15 21:22:58 +000027#include <sound/jack.h>
Mark Brownf1c0a022008-08-26 13:05:27 +010028#include <sound/pcm.h>
29#include <sound/pcm_params.h>
30#include <sound/tlv.h>
31#include <sound/soc.h>
32#include <sound/soc-dapm.h>
33#include <sound/initval.h>
Mark Brown8abd16a2010-03-15 18:25:26 +000034#include <sound/wm8903.h>
Mark Brownf1c0a022008-08-26 13:05:27 +010035
36#include "wm8903.h"
37
Mark Brownf1c0a022008-08-26 13:05:27 +010038/* Register defaults at reset */
39static u16 wm8903_reg_defaults[] = {
40 0x8903, /* R0 - SW Reset and ID */
41 0x0000, /* R1 - Revision Number */
42 0x0000, /* R2 */
43 0x0000, /* R3 */
44 0x0018, /* R4 - Bias Control 0 */
45 0x0000, /* R5 - VMID Control 0 */
46 0x0000, /* R6 - Mic Bias Control 0 */
47 0x0000, /* R7 */
48 0x0001, /* R8 - Analogue DAC 0 */
49 0x0000, /* R9 */
50 0x0001, /* R10 - Analogue ADC 0 */
51 0x0000, /* R11 */
52 0x0000, /* R12 - Power Management 0 */
53 0x0000, /* R13 - Power Management 1 */
54 0x0000, /* R14 - Power Management 2 */
55 0x0000, /* R15 - Power Management 3 */
56 0x0000, /* R16 - Power Management 4 */
57 0x0000, /* R17 - Power Management 5 */
58 0x0000, /* R18 - Power Management 6 */
59 0x0000, /* R19 */
60 0x0400, /* R20 - Clock Rates 0 */
61 0x0D07, /* R21 - Clock Rates 1 */
62 0x0000, /* R22 - Clock Rates 2 */
63 0x0000, /* R23 */
64 0x0050, /* R24 - Audio Interface 0 */
65 0x0242, /* R25 - Audio Interface 1 */
66 0x0008, /* R26 - Audio Interface 2 */
67 0x0022, /* R27 - Audio Interface 3 */
68 0x0000, /* R28 */
69 0x0000, /* R29 */
70 0x00C0, /* R30 - DAC Digital Volume Left */
71 0x00C0, /* R31 - DAC Digital Volume Right */
72 0x0000, /* R32 - DAC Digital 0 */
73 0x0000, /* R33 - DAC Digital 1 */
74 0x0000, /* R34 */
75 0x0000, /* R35 */
76 0x00C0, /* R36 - ADC Digital Volume Left */
77 0x00C0, /* R37 - ADC Digital Volume Right */
78 0x0000, /* R38 - ADC Digital 0 */
79 0x0073, /* R39 - Digital Microphone 0 */
80 0x09BF, /* R40 - DRC 0 */
81 0x3241, /* R41 - DRC 1 */
82 0x0020, /* R42 - DRC 2 */
83 0x0000, /* R43 - DRC 3 */
84 0x0085, /* R44 - Analogue Left Input 0 */
85 0x0085, /* R45 - Analogue Right Input 0 */
86 0x0044, /* R46 - Analogue Left Input 1 */
87 0x0044, /* R47 - Analogue Right Input 1 */
88 0x0000, /* R48 */
89 0x0000, /* R49 */
90 0x0008, /* R50 - Analogue Left Mix 0 */
91 0x0004, /* R51 - Analogue Right Mix 0 */
92 0x0000, /* R52 - Analogue Spk Mix Left 0 */
93 0x0000, /* R53 - Analogue Spk Mix Left 1 */
94 0x0000, /* R54 - Analogue Spk Mix Right 0 */
95 0x0000, /* R55 - Analogue Spk Mix Right 1 */
96 0x0000, /* R56 */
97 0x002D, /* R57 - Analogue OUT1 Left */
98 0x002D, /* R58 - Analogue OUT1 Right */
99 0x0039, /* R59 - Analogue OUT2 Left */
100 0x0039, /* R60 - Analogue OUT2 Right */
101 0x0100, /* R61 */
102 0x0139, /* R62 - Analogue OUT3 Left */
103 0x0139, /* R63 - Analogue OUT3 Right */
104 0x0000, /* R64 */
105 0x0000, /* R65 - Analogue SPK Output Control 0 */
106 0x0000, /* R66 */
107 0x0010, /* R67 - DC Servo 0 */
108 0x0100, /* R68 */
109 0x00A4, /* R69 - DC Servo 2 */
110 0x0807, /* R70 */
111 0x0000, /* R71 */
112 0x0000, /* R72 */
113 0x0000, /* R73 */
114 0x0000, /* R74 */
115 0x0000, /* R75 */
116 0x0000, /* R76 */
117 0x0000, /* R77 */
118 0x0000, /* R78 */
119 0x000E, /* R79 */
120 0x0000, /* R80 */
121 0x0000, /* R81 */
122 0x0000, /* R82 */
123 0x0000, /* R83 */
124 0x0000, /* R84 */
125 0x0000, /* R85 */
126 0x0000, /* R86 */
127 0x0006, /* R87 */
128 0x0000, /* R88 */
129 0x0000, /* R89 */
130 0x0000, /* R90 - Analogue HP 0 */
131 0x0060, /* R91 */
132 0x0000, /* R92 */
133 0x0000, /* R93 */
134 0x0000, /* R94 - Analogue Lineout 0 */
135 0x0060, /* R95 */
136 0x0000, /* R96 */
137 0x0000, /* R97 */
138 0x0000, /* R98 - Charge Pump 0 */
139 0x1F25, /* R99 */
140 0x2B19, /* R100 */
141 0x01C0, /* R101 */
142 0x01EF, /* R102 */
143 0x2B00, /* R103 */
144 0x0000, /* R104 - Class W 0 */
145 0x01C0, /* R105 */
146 0x1C10, /* R106 */
147 0x0000, /* R107 */
148 0x0000, /* R108 - Write Sequencer 0 */
149 0x0000, /* R109 - Write Sequencer 1 */
150 0x0000, /* R110 - Write Sequencer 2 */
151 0x0000, /* R111 - Write Sequencer 3 */
152 0x0000, /* R112 - Write Sequencer 4 */
153 0x0000, /* R113 */
154 0x0000, /* R114 - Control Interface */
155 0x0000, /* R115 */
156 0x00A8, /* R116 - GPIO Control 1 */
157 0x00A8, /* R117 - GPIO Control 2 */
158 0x00A8, /* R118 - GPIO Control 3 */
159 0x0220, /* R119 - GPIO Control 4 */
160 0x01A0, /* R120 - GPIO Control 5 */
161 0x0000, /* R121 - Interrupt Status 1 */
162 0xFFFF, /* R122 - Interrupt Status 1 Mask */
163 0x0000, /* R123 - Interrupt Polarity 1 */
164 0x0000, /* R124 */
165 0x0003, /* R125 */
166 0x0000, /* R126 - Interrupt Control */
167 0x0000, /* R127 */
168 0x0005, /* R128 */
169 0x0000, /* R129 - Control Interface Test 1 */
170 0x0000, /* R130 */
171 0x0000, /* R131 */
172 0x0000, /* R132 */
173 0x0000, /* R133 */
174 0x0000, /* R134 */
175 0x03FF, /* R135 */
176 0x0007, /* R136 */
177 0x0040, /* R137 */
178 0x0000, /* R138 */
179 0x0000, /* R139 */
180 0x0000, /* R140 */
181 0x0000, /* R141 */
182 0x0000, /* R142 */
183 0x0000, /* R143 */
184 0x0000, /* R144 */
185 0x0000, /* R145 */
186 0x0000, /* R146 */
187 0x0000, /* R147 */
188 0x4000, /* R148 */
189 0x6810, /* R149 - Charge Pump Test 1 */
190 0x0004, /* R150 */
191 0x0000, /* R151 */
192 0x0000, /* R152 */
193 0x0000, /* R153 */
194 0x0000, /* R154 */
195 0x0000, /* R155 */
196 0x0000, /* R156 */
197 0x0000, /* R157 */
198 0x0000, /* R158 */
199 0x0000, /* R159 */
200 0x0000, /* R160 */
201 0x0000, /* R161 */
202 0x0000, /* R162 */
203 0x0000, /* R163 */
204 0x0028, /* R164 - Clock Rate Test 4 */
205 0x0004, /* R165 */
206 0x0000, /* R166 */
207 0x0060, /* R167 */
208 0x0000, /* R168 */
209 0x0000, /* R169 */
210 0x0000, /* R170 */
211 0x0000, /* R171 */
212 0x0000, /* R172 - Analogue Output Bias 0 */
213};
214
Mark Brownd58d5d52008-12-10 18:36:42 +0000215struct wm8903_priv {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000216
Mark Brownd58d5d52008-12-10 18:36:42 +0000217 u16 reg_cache[ARRAY_SIZE(wm8903_reg_defaults)];
218
219 int sysclk;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000220 struct i2c_client *control_data;
221 int irq;
Mark Brownd58d5d52008-12-10 18:36:42 +0000222
223 /* Reference counts */
Mark Brownd58d5d52008-12-10 18:36:42 +0000224 int class_w_users;
225 int playback_active;
226 int capture_active;
227
Mark Brown8abd16a2010-03-15 18:25:26 +0000228 struct completion wseq;
229
Mark Brown72453872010-03-15 21:22:58 +0000230 struct snd_soc_jack *mic_jack;
231 int mic_det;
232 int mic_short;
233 int mic_last_report;
234 int mic_delay;
235
Mark Brownd58d5d52008-12-10 18:36:42 +0000236 struct snd_pcm_substream *master_substream;
237 struct snd_pcm_substream *slave_substream;
238};
239
Mark Brown8d50e442009-07-10 23:12:01 +0100240static int wm8903_volatile_register(unsigned int reg)
Mark Brownf1c0a022008-08-26 13:05:27 +0100241{
242 switch (reg) {
243 case WM8903_SW_RESET_AND_ID:
244 case WM8903_REVISION_NUMBER:
245 case WM8903_INTERRUPT_STATUS_1:
246 case WM8903_WRITE_SEQUENCER_4:
Mark Brown8d50e442009-07-10 23:12:01 +0100247 return 1;
Mark Brownf1c0a022008-08-26 13:05:27 +0100248
249 default:
Mark Brownf1c0a022008-08-26 13:05:27 +0100250 return 0;
Mark Brown8d50e442009-07-10 23:12:01 +0100251 }
Mark Brownf1c0a022008-08-26 13:05:27 +0100252}
253
254static int wm8903_run_sequence(struct snd_soc_codec *codec, unsigned int start)
255{
256 u16 reg[5];
Mark Brownb2c812e2010-04-14 15:35:19 +0900257 struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
Mark Brownf1c0a022008-08-26 13:05:27 +0100258
259 BUG_ON(start > 48);
260
Mark Brown37f88e82010-03-15 18:14:34 +0000261 /* Enable the sequencer if it's not already on */
Mark Brown8d50e442009-07-10 23:12:01 +0100262 reg[0] = snd_soc_read(codec, WM8903_WRITE_SEQUENCER_0);
Mark Brown37f88e82010-03-15 18:14:34 +0000263 snd_soc_write(codec, WM8903_WRITE_SEQUENCER_0,
264 reg[0] | WM8903_WSEQ_ENA);
Mark Brownf1c0a022008-08-26 13:05:27 +0100265
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000266 dev_dbg(codec->dev, "Starting sequence at %d\n", start);
Mark Brownf1c0a022008-08-26 13:05:27 +0100267
Mark Brown8d50e442009-07-10 23:12:01 +0100268 snd_soc_write(codec, WM8903_WRITE_SEQUENCER_3,
Mark Brownf1c0a022008-08-26 13:05:27 +0100269 start | WM8903_WSEQ_START);
270
271 /* Wait for it to complete. If we have the interrupt wired up then
Mark Brown8abd16a2010-03-15 18:25:26 +0000272 * that will break us out of the poll early.
Mark Brownf1c0a022008-08-26 13:05:27 +0100273 */
274 do {
Mark Brown8abd16a2010-03-15 18:25:26 +0000275 wait_for_completion_timeout(&wm8903->wseq,
276 msecs_to_jiffies(10));
Mark Brownf1c0a022008-08-26 13:05:27 +0100277
Mark Brown8d50e442009-07-10 23:12:01 +0100278 reg[4] = snd_soc_read(codec, WM8903_WRITE_SEQUENCER_4);
Mark Brownf1c0a022008-08-26 13:05:27 +0100279 } while (reg[4] & WM8903_WSEQ_BUSY);
280
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000281 dev_dbg(codec->dev, "Sequence complete\n");
Mark Brownf1c0a022008-08-26 13:05:27 +0100282
Mark Brown37f88e82010-03-15 18:14:34 +0000283 /* Disable the sequencer again if we enabled it */
284 snd_soc_write(codec, WM8903_WRITE_SEQUENCER_0, reg[0]);
Mark Brownf1c0a022008-08-26 13:05:27 +0100285
286 return 0;
287}
288
289static void wm8903_sync_reg_cache(struct snd_soc_codec *codec, u16 *cache)
290{
291 int i;
292
293 /* There really ought to be something better we can do here :/ */
294 for (i = 0; i < ARRAY_SIZE(wm8903_reg_defaults); i++)
Mark Brown8d50e442009-07-10 23:12:01 +0100295 cache[i] = codec->hw_read(codec, i);
Mark Brownf1c0a022008-08-26 13:05:27 +0100296}
297
298static void wm8903_reset(struct snd_soc_codec *codec)
299{
Mark Brown8d50e442009-07-10 23:12:01 +0100300 snd_soc_write(codec, WM8903_SW_RESET_AND_ID, 0);
Mark Brownd58d5d52008-12-10 18:36:42 +0000301 memcpy(codec->reg_cache, wm8903_reg_defaults,
302 sizeof(wm8903_reg_defaults));
Mark Brownf1c0a022008-08-26 13:05:27 +0100303}
304
305#define WM8903_OUTPUT_SHORT 0x8
306#define WM8903_OUTPUT_OUT 0x4
307#define WM8903_OUTPUT_INT 0x2
308#define WM8903_OUTPUT_IN 0x1
309
Mark Brown42768a12009-04-22 18:39:39 +0100310static int wm8903_cp_event(struct snd_soc_dapm_widget *w,
311 struct snd_kcontrol *kcontrol, int event)
312{
313 WARN_ON(event != SND_SOC_DAPM_POST_PMU);
314 mdelay(4);
315
316 return 0;
317}
318
Mark Brownf1c0a022008-08-26 13:05:27 +0100319/*
320 * Event for headphone and line out amplifier power changes. Special
321 * power up/down sequences are required in order to maximise pop/click
322 * performance.
323 */
324static int wm8903_output_event(struct snd_soc_dapm_widget *w,
325 struct snd_kcontrol *kcontrol, int event)
326{
327 struct snd_soc_codec *codec = w->codec;
Mark Brownf1c0a022008-08-26 13:05:27 +0100328 u16 val;
Takashi Iwai0bc286e2008-12-01 19:59:35 +0100329 u16 reg;
Mark Brownd7d5c542009-04-22 21:03:50 +0100330 u16 dcs_reg;
331 u16 dcs_bit;
Takashi Iwai0bc286e2008-12-01 19:59:35 +0100332 int shift;
Mark Brownf1c0a022008-08-26 13:05:27 +0100333
334 switch (w->reg) {
335 case WM8903_POWER_MANAGEMENT_2:
336 reg = WM8903_ANALOGUE_HP_0;
Mark Brownd7d5c542009-04-22 21:03:50 +0100337 dcs_bit = 0 + w->shift;
Mark Brownf1c0a022008-08-26 13:05:27 +0100338 break;
339 case WM8903_POWER_MANAGEMENT_3:
340 reg = WM8903_ANALOGUE_LINEOUT_0;
Mark Brownd7d5c542009-04-22 21:03:50 +0100341 dcs_bit = 2 + w->shift;
Mark Brownf1c0a022008-08-26 13:05:27 +0100342 break;
343 default:
344 BUG();
Mark Brown1e297a12008-12-10 11:08:33 +0000345 return -EINVAL; /* Spurious warning from some compilers */
Mark Brownf1c0a022008-08-26 13:05:27 +0100346 }
347
348 switch (w->shift) {
349 case 0:
350 shift = 0;
351 break;
352 case 1:
353 shift = 4;
354 break;
355 default:
356 BUG();
Mark Brown1e297a12008-12-10 11:08:33 +0000357 return -EINVAL; /* Spurious warning from some compilers */
Mark Brownf1c0a022008-08-26 13:05:27 +0100358 }
359
360 if (event & SND_SOC_DAPM_PRE_PMU) {
Mark Brown8d50e442009-07-10 23:12:01 +0100361 val = snd_soc_read(codec, reg);
Mark Brownf1c0a022008-08-26 13:05:27 +0100362
363 /* Short the output */
364 val &= ~(WM8903_OUTPUT_SHORT << shift);
Mark Brown8d50e442009-07-10 23:12:01 +0100365 snd_soc_write(codec, reg, val);
Mark Brownf1c0a022008-08-26 13:05:27 +0100366 }
367
368 if (event & SND_SOC_DAPM_POST_PMU) {
Mark Brown8d50e442009-07-10 23:12:01 +0100369 val = snd_soc_read(codec, reg);
Mark Brownf1c0a022008-08-26 13:05:27 +0100370
371 val |= (WM8903_OUTPUT_IN << shift);
Mark Brown8d50e442009-07-10 23:12:01 +0100372 snd_soc_write(codec, reg, val);
Mark Brownf1c0a022008-08-26 13:05:27 +0100373
374 val |= (WM8903_OUTPUT_INT << shift);
Mark Brown8d50e442009-07-10 23:12:01 +0100375 snd_soc_write(codec, reg, val);
Mark Brownf1c0a022008-08-26 13:05:27 +0100376
377 /* Turn on the output ENA_OUTP */
378 val |= (WM8903_OUTPUT_OUT << shift);
Mark Brown8d50e442009-07-10 23:12:01 +0100379 snd_soc_write(codec, reg, val);
Mark Brownf1c0a022008-08-26 13:05:27 +0100380
Mark Brownd7d5c542009-04-22 21:03:50 +0100381 /* Enable the DC servo */
Mark Brown8d50e442009-07-10 23:12:01 +0100382 dcs_reg = snd_soc_read(codec, WM8903_DC_SERVO_0);
Mark Brownd7d5c542009-04-22 21:03:50 +0100383 dcs_reg |= dcs_bit;
Mark Brown8d50e442009-07-10 23:12:01 +0100384 snd_soc_write(codec, WM8903_DC_SERVO_0, dcs_reg);
Mark Brownd7d5c542009-04-22 21:03:50 +0100385
Mark Brownf1c0a022008-08-26 13:05:27 +0100386 /* Remove the short */
387 val |= (WM8903_OUTPUT_SHORT << shift);
Mark Brown8d50e442009-07-10 23:12:01 +0100388 snd_soc_write(codec, reg, val);
Mark Brownf1c0a022008-08-26 13:05:27 +0100389 }
390
391 if (event & SND_SOC_DAPM_PRE_PMD) {
Mark Brown8d50e442009-07-10 23:12:01 +0100392 val = snd_soc_read(codec, reg);
Mark Brownf1c0a022008-08-26 13:05:27 +0100393
394 /* Short the output */
395 val &= ~(WM8903_OUTPUT_SHORT << shift);
Mark Brown8d50e442009-07-10 23:12:01 +0100396 snd_soc_write(codec, reg, val);
Mark Brownf1c0a022008-08-26 13:05:27 +0100397
Mark Brownd7d5c542009-04-22 21:03:50 +0100398 /* Disable the DC servo */
Mark Brown8d50e442009-07-10 23:12:01 +0100399 dcs_reg = snd_soc_read(codec, WM8903_DC_SERVO_0);
Mark Brownd7d5c542009-04-22 21:03:50 +0100400 dcs_reg &= ~dcs_bit;
Mark Brown8d50e442009-07-10 23:12:01 +0100401 snd_soc_write(codec, WM8903_DC_SERVO_0, dcs_reg);
Mark Brownd7d5c542009-04-22 21:03:50 +0100402
Mark Brownf1c0a022008-08-26 13:05:27 +0100403 /* Then disable the intermediate and output stages */
404 val &= ~((WM8903_OUTPUT_OUT | WM8903_OUTPUT_INT |
405 WM8903_OUTPUT_IN) << shift);
Mark Brown8d50e442009-07-10 23:12:01 +0100406 snd_soc_write(codec, reg, val);
Mark Brownf1c0a022008-08-26 13:05:27 +0100407 }
408
Mark Brownf1c0a022008-08-26 13:05:27 +0100409 return 0;
410}
411
412/*
413 * When used with DAC outputs only the WM8903 charge pump supports
414 * operation in class W mode, providing very low power consumption
415 * when used with digital sources. Enable and disable this mode
416 * automatically depending on the mixer configuration.
417 *
418 * All the relevant controls are simple switches.
419 */
420static int wm8903_class_w_put(struct snd_kcontrol *kcontrol,
421 struct snd_ctl_elem_value *ucontrol)
422{
423 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
424 struct snd_soc_codec *codec = widget->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900425 struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
Mark Brownf1c0a022008-08-26 13:05:27 +0100426 u16 reg;
427 int ret;
428
Mark Brown8d50e442009-07-10 23:12:01 +0100429 reg = snd_soc_read(codec, WM8903_CLASS_W_0);
Mark Brownf1c0a022008-08-26 13:05:27 +0100430
431 /* Turn it off if we're about to enable bypass */
432 if (ucontrol->value.integer.value[0]) {
433 if (wm8903->class_w_users == 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000434 dev_dbg(codec->dev, "Disabling Class W\n");
Mark Brown8d50e442009-07-10 23:12:01 +0100435 snd_soc_write(codec, WM8903_CLASS_W_0, reg &
Mark Brownf1c0a022008-08-26 13:05:27 +0100436 ~(WM8903_CP_DYN_FREQ | WM8903_CP_DYN_V));
437 }
438 wm8903->class_w_users++;
439 }
440
441 /* Implement the change */
442 ret = snd_soc_dapm_put_volsw(kcontrol, ucontrol);
443
444 /* If we've just disabled the last bypass path turn Class W on */
445 if (!ucontrol->value.integer.value[0]) {
446 if (wm8903->class_w_users == 1) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000447 dev_dbg(codec->dev, "Enabling Class W\n");
Mark Brown8d50e442009-07-10 23:12:01 +0100448 snd_soc_write(codec, WM8903_CLASS_W_0, reg |
Mark Brownf1c0a022008-08-26 13:05:27 +0100449 WM8903_CP_DYN_FREQ | WM8903_CP_DYN_V);
450 }
451 wm8903->class_w_users--;
452 }
453
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000454 dev_dbg(codec->dev, "Bypass use count now %d\n",
Mark Brownf1c0a022008-08-26 13:05:27 +0100455 wm8903->class_w_users);
456
457 return ret;
458}
459
460#define SOC_DAPM_SINGLE_W(xname, reg, shift, max, invert) \
461{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
462 .info = snd_soc_info_volsw, \
463 .get = snd_soc_dapm_get_volsw, .put = wm8903_class_w_put, \
464 .private_value = SOC_SINGLE_VALUE(reg, shift, max, invert) }
465
466
467/* ALSA can only do steps of .01dB */
468static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
469
Mark Brown291ce182009-04-22 21:36:14 +0100470static const DECLARE_TLV_DB_SCALE(digital_sidetone_tlv, -3600, 300, 0);
Mark Brownf1c0a022008-08-26 13:05:27 +0100471static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
472
473static const DECLARE_TLV_DB_SCALE(drc_tlv_thresh, 0, 75, 0);
474static const DECLARE_TLV_DB_SCALE(drc_tlv_amp, -2250, 75, 0);
475static const DECLARE_TLV_DB_SCALE(drc_tlv_min, 0, 600, 0);
476static const DECLARE_TLV_DB_SCALE(drc_tlv_max, 1200, 600, 0);
477static const DECLARE_TLV_DB_SCALE(drc_tlv_startup, -300, 50, 0);
478
479static const char *drc_slope_text[] = {
480 "1", "1/2", "1/4", "1/8", "1/16", "0"
481};
482
483static const struct soc_enum drc_slope_r0 =
484 SOC_ENUM_SINGLE(WM8903_DRC_2, 3, 6, drc_slope_text);
485
486static const struct soc_enum drc_slope_r1 =
487 SOC_ENUM_SINGLE(WM8903_DRC_2, 0, 6, drc_slope_text);
488
489static const char *drc_attack_text[] = {
490 "instantaneous",
491 "363us", "762us", "1.45ms", "2.9ms", "5.8ms", "11.6ms", "23.2ms",
492 "46.4ms", "92.8ms", "185.6ms"
493};
494
495static const struct soc_enum drc_attack =
496 SOC_ENUM_SINGLE(WM8903_DRC_1, 12, 11, drc_attack_text);
497
498static const char *drc_decay_text[] = {
499 "186ms", "372ms", "743ms", "1.49s", "2.97s", "5.94s", "11.89s",
500 "23.87s", "47.56s"
501};
502
503static const struct soc_enum drc_decay =
504 SOC_ENUM_SINGLE(WM8903_DRC_1, 8, 9, drc_decay_text);
505
506static const char *drc_ff_delay_text[] = {
507 "5 samples", "9 samples"
508};
509
510static const struct soc_enum drc_ff_delay =
511 SOC_ENUM_SINGLE(WM8903_DRC_0, 5, 2, drc_ff_delay_text);
512
513static const char *drc_qr_decay_text[] = {
514 "0.725ms", "1.45ms", "5.8ms"
515};
516
517static const struct soc_enum drc_qr_decay =
518 SOC_ENUM_SINGLE(WM8903_DRC_1, 4, 3, drc_qr_decay_text);
519
520static const char *drc_smoothing_text[] = {
521 "Low", "Medium", "High"
522};
523
524static const struct soc_enum drc_smoothing =
525 SOC_ENUM_SINGLE(WM8903_DRC_0, 11, 3, drc_smoothing_text);
526
527static const char *soft_mute_text[] = {
528 "Fast (fs/2)", "Slow (fs/32)"
529};
530
531static const struct soc_enum soft_mute =
532 SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_1, 10, 2, soft_mute_text);
533
534static const char *mute_mode_text[] = {
535 "Hard", "Soft"
536};
537
538static const struct soc_enum mute_mode =
539 SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_1, 9, 2, mute_mode_text);
540
541static const char *dac_deemphasis_text[] = {
542 "Disabled", "32kHz", "44.1kHz", "48kHz"
543};
544
545static const struct soc_enum dac_deemphasis =
546 SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_1, 1, 4, dac_deemphasis_text);
547
548static const char *companding_text[] = {
549 "ulaw", "alaw"
550};
551
552static const struct soc_enum dac_companding =
553 SOC_ENUM_SINGLE(WM8903_AUDIO_INTERFACE_0, 0, 2, companding_text);
554
555static const struct soc_enum adc_companding =
556 SOC_ENUM_SINGLE(WM8903_AUDIO_INTERFACE_0, 2, 2, companding_text);
557
558static const char *input_mode_text[] = {
559 "Single-Ended", "Differential Line", "Differential Mic"
560};
561
562static const struct soc_enum linput_mode_enum =
563 SOC_ENUM_SINGLE(WM8903_ANALOGUE_LEFT_INPUT_1, 0, 3, input_mode_text);
564
565static const struct soc_enum rinput_mode_enum =
566 SOC_ENUM_SINGLE(WM8903_ANALOGUE_RIGHT_INPUT_1, 0, 3, input_mode_text);
567
568static const char *linput_mux_text[] = {
569 "IN1L", "IN2L", "IN3L"
570};
571
572static const struct soc_enum linput_enum =
573 SOC_ENUM_SINGLE(WM8903_ANALOGUE_LEFT_INPUT_1, 2, 3, linput_mux_text);
574
575static const struct soc_enum linput_inv_enum =
576 SOC_ENUM_SINGLE(WM8903_ANALOGUE_LEFT_INPUT_1, 4, 3, linput_mux_text);
577
578static const char *rinput_mux_text[] = {
579 "IN1R", "IN2R", "IN3R"
580};
581
582static const struct soc_enum rinput_enum =
583 SOC_ENUM_SINGLE(WM8903_ANALOGUE_RIGHT_INPUT_1, 2, 3, rinput_mux_text);
584
585static const struct soc_enum rinput_inv_enum =
586 SOC_ENUM_SINGLE(WM8903_ANALOGUE_RIGHT_INPUT_1, 4, 3, rinput_mux_text);
587
588
Mark Brown291ce182009-04-22 21:36:14 +0100589static const char *sidetone_text[] = {
590 "None", "Left", "Right"
591};
592
593static const struct soc_enum lsidetone_enum =
594 SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_0, 2, 3, sidetone_text);
595
596static const struct soc_enum rsidetone_enum =
597 SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_0, 0, 3, sidetone_text);
598
Mark Brownf1c0a022008-08-26 13:05:27 +0100599static const struct snd_kcontrol_new wm8903_snd_controls[] = {
600
601/* Input PGAs - No TLV since the scale depends on PGA mode */
602SOC_SINGLE("Left Input PGA Switch", WM8903_ANALOGUE_LEFT_INPUT_0,
Mark Brown57159522008-09-24 10:47:02 +0100603 7, 1, 1),
Mark Brownf1c0a022008-08-26 13:05:27 +0100604SOC_SINGLE("Left Input PGA Volume", WM8903_ANALOGUE_LEFT_INPUT_0,
605 0, 31, 0),
606SOC_SINGLE("Left Input PGA Common Mode Switch", WM8903_ANALOGUE_LEFT_INPUT_1,
607 6, 1, 0),
608
609SOC_SINGLE("Right Input PGA Switch", WM8903_ANALOGUE_RIGHT_INPUT_0,
Mark Brown57159522008-09-24 10:47:02 +0100610 7, 1, 1),
Mark Brownf1c0a022008-08-26 13:05:27 +0100611SOC_SINGLE("Right Input PGA Volume", WM8903_ANALOGUE_RIGHT_INPUT_0,
612 0, 31, 0),
613SOC_SINGLE("Right Input PGA Common Mode Switch", WM8903_ANALOGUE_RIGHT_INPUT_1,
614 6, 1, 0),
615
616/* ADCs */
617SOC_SINGLE("DRC Switch", WM8903_DRC_0, 15, 1, 0),
618SOC_ENUM("DRC Compressor Slope R0", drc_slope_r0),
619SOC_ENUM("DRC Compressor Slope R1", drc_slope_r1),
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200620SOC_SINGLE_TLV("DRC Compressor Threshold Volume", WM8903_DRC_3, 5, 124, 1,
Mark Brownf1c0a022008-08-26 13:05:27 +0100621 drc_tlv_thresh),
622SOC_SINGLE_TLV("DRC Volume", WM8903_DRC_3, 0, 30, 1, drc_tlv_amp),
623SOC_SINGLE_TLV("DRC Minimum Gain Volume", WM8903_DRC_1, 2, 3, 1, drc_tlv_min),
624SOC_SINGLE_TLV("DRC Maximum Gain Volume", WM8903_DRC_1, 0, 3, 0, drc_tlv_max),
625SOC_ENUM("DRC Attack Rate", drc_attack),
626SOC_ENUM("DRC Decay Rate", drc_decay),
627SOC_ENUM("DRC FF Delay", drc_ff_delay),
628SOC_SINGLE("DRC Anticlip Switch", WM8903_DRC_0, 1, 1, 0),
629SOC_SINGLE("DRC QR Switch", WM8903_DRC_0, 2, 1, 0),
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200630SOC_SINGLE_TLV("DRC QR Threshold Volume", WM8903_DRC_0, 6, 3, 0, drc_tlv_max),
Mark Brownf1c0a022008-08-26 13:05:27 +0100631SOC_ENUM("DRC QR Decay Rate", drc_qr_decay),
632SOC_SINGLE("DRC Smoothing Switch", WM8903_DRC_0, 3, 1, 0),
633SOC_SINGLE("DRC Smoothing Hysteresis Switch", WM8903_DRC_0, 0, 1, 0),
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200634SOC_ENUM("DRC Smoothing Threshold", drc_smoothing),
Mark Brownf1c0a022008-08-26 13:05:27 +0100635SOC_SINGLE_TLV("DRC Startup Volume", WM8903_DRC_0, 6, 18, 0, drc_tlv_startup),
636
637SOC_DOUBLE_R_TLV("Digital Capture Volume", WM8903_ADC_DIGITAL_VOLUME_LEFT,
638 WM8903_ADC_DIGITAL_VOLUME_RIGHT, 1, 96, 0, digital_tlv),
639SOC_ENUM("ADC Companding Mode", adc_companding),
640SOC_SINGLE("ADC Companding Switch", WM8903_AUDIO_INTERFACE_0, 3, 1, 0),
641
Mark Brown291ce182009-04-22 21:36:14 +0100642SOC_DOUBLE_TLV("Digital Sidetone Volume", WM8903_DAC_DIGITAL_0, 4, 8,
643 12, 0, digital_sidetone_tlv),
644
Mark Brownf1c0a022008-08-26 13:05:27 +0100645/* DAC */
646SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8903_DAC_DIGITAL_VOLUME_LEFT,
647 WM8903_DAC_DIGITAL_VOLUME_RIGHT, 1, 120, 0, digital_tlv),
648SOC_ENUM("DAC Soft Mute Rate", soft_mute),
649SOC_ENUM("DAC Mute Mode", mute_mode),
650SOC_SINGLE("DAC Mono Switch", WM8903_DAC_DIGITAL_1, 12, 1, 0),
651SOC_ENUM("DAC De-emphasis", dac_deemphasis),
Mark Brownf1c0a022008-08-26 13:05:27 +0100652SOC_ENUM("DAC Companding Mode", dac_companding),
653SOC_SINGLE("DAC Companding Switch", WM8903_AUDIO_INTERFACE_0, 1, 1, 0),
654
655/* Headphones */
656SOC_DOUBLE_R("Headphone Switch",
657 WM8903_ANALOGUE_OUT1_LEFT, WM8903_ANALOGUE_OUT1_RIGHT,
658 8, 1, 1),
659SOC_DOUBLE_R("Headphone ZC Switch",
660 WM8903_ANALOGUE_OUT1_LEFT, WM8903_ANALOGUE_OUT1_RIGHT,
661 6, 1, 0),
662SOC_DOUBLE_R_TLV("Headphone Volume",
663 WM8903_ANALOGUE_OUT1_LEFT, WM8903_ANALOGUE_OUT1_RIGHT,
664 0, 63, 0, out_tlv),
665
666/* Line out */
667SOC_DOUBLE_R("Line Out Switch",
668 WM8903_ANALOGUE_OUT2_LEFT, WM8903_ANALOGUE_OUT2_RIGHT,
669 8, 1, 1),
670SOC_DOUBLE_R("Line Out ZC Switch",
671 WM8903_ANALOGUE_OUT2_LEFT, WM8903_ANALOGUE_OUT2_RIGHT,
672 6, 1, 0),
673SOC_DOUBLE_R_TLV("Line Out Volume",
674 WM8903_ANALOGUE_OUT2_LEFT, WM8903_ANALOGUE_OUT2_RIGHT,
675 0, 63, 0, out_tlv),
676
677/* Speaker */
678SOC_DOUBLE_R("Speaker Switch",
679 WM8903_ANALOGUE_OUT3_LEFT, WM8903_ANALOGUE_OUT3_RIGHT, 8, 1, 1),
680SOC_DOUBLE_R("Speaker ZC Switch",
681 WM8903_ANALOGUE_OUT3_LEFT, WM8903_ANALOGUE_OUT3_RIGHT, 6, 1, 0),
682SOC_DOUBLE_R_TLV("Speaker Volume",
683 WM8903_ANALOGUE_OUT3_LEFT, WM8903_ANALOGUE_OUT3_RIGHT,
684 0, 63, 0, out_tlv),
685};
686
Mark Brownf1c0a022008-08-26 13:05:27 +0100687static const struct snd_kcontrol_new linput_mode_mux =
688 SOC_DAPM_ENUM("Left Input Mode Mux", linput_mode_enum);
689
690static const struct snd_kcontrol_new rinput_mode_mux =
691 SOC_DAPM_ENUM("Right Input Mode Mux", rinput_mode_enum);
692
693static const struct snd_kcontrol_new linput_mux =
694 SOC_DAPM_ENUM("Left Input Mux", linput_enum);
695
696static const struct snd_kcontrol_new linput_inv_mux =
697 SOC_DAPM_ENUM("Left Inverting Input Mux", linput_inv_enum);
698
699static const struct snd_kcontrol_new rinput_mux =
700 SOC_DAPM_ENUM("Right Input Mux", rinput_enum);
701
702static const struct snd_kcontrol_new rinput_inv_mux =
703 SOC_DAPM_ENUM("Right Inverting Input Mux", rinput_inv_enum);
704
Mark Brown291ce182009-04-22 21:36:14 +0100705static const struct snd_kcontrol_new lsidetone_mux =
706 SOC_DAPM_ENUM("DACL Sidetone Mux", lsidetone_enum);
707
708static const struct snd_kcontrol_new rsidetone_mux =
709 SOC_DAPM_ENUM("DACR Sidetone Mux", rsidetone_enum);
710
Mark Brownf1c0a022008-08-26 13:05:27 +0100711static const struct snd_kcontrol_new left_output_mixer[] = {
712SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_LEFT_MIX_0, 3, 1, 0),
713SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_LEFT_MIX_0, 2, 1, 0),
714SOC_DAPM_SINGLE_W("Left Bypass Switch", WM8903_ANALOGUE_LEFT_MIX_0, 1, 1, 0),
Mark Brown4b4fffd2008-12-03 11:21:08 +0000715SOC_DAPM_SINGLE_W("Right Bypass Switch", WM8903_ANALOGUE_LEFT_MIX_0, 0, 1, 0),
Mark Brownf1c0a022008-08-26 13:05:27 +0100716};
717
718static const struct snd_kcontrol_new right_output_mixer[] = {
719SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 3, 1, 0),
720SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 2, 1, 0),
721SOC_DAPM_SINGLE_W("Left Bypass Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 1, 1, 0),
Mark Brown4b4fffd2008-12-03 11:21:08 +0000722SOC_DAPM_SINGLE_W("Right Bypass Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 0, 1, 0),
Mark Brownf1c0a022008-08-26 13:05:27 +0100723};
724
725static const struct snd_kcontrol_new left_speaker_mixer[] = {
726SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0, 3, 1, 0),
727SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0, 2, 1, 0),
728SOC_DAPM_SINGLE("Left Bypass Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0, 1, 1, 0),
729SOC_DAPM_SINGLE("Right Bypass Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0,
Mark Brown4b4fffd2008-12-03 11:21:08 +0000730 0, 1, 0),
Mark Brownf1c0a022008-08-26 13:05:27 +0100731};
732
733static const struct snd_kcontrol_new right_speaker_mixer[] = {
734SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0, 3, 1, 0),
735SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0, 2, 1, 0),
736SOC_DAPM_SINGLE("Left Bypass Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0,
737 1, 1, 0),
738SOC_DAPM_SINGLE("Right Bypass Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0,
Mark Brown4b4fffd2008-12-03 11:21:08 +0000739 0, 1, 0),
Mark Brownf1c0a022008-08-26 13:05:27 +0100740};
741
742static const struct snd_soc_dapm_widget wm8903_dapm_widgets[] = {
743SND_SOC_DAPM_INPUT("IN1L"),
744SND_SOC_DAPM_INPUT("IN1R"),
745SND_SOC_DAPM_INPUT("IN2L"),
746SND_SOC_DAPM_INPUT("IN2R"),
747SND_SOC_DAPM_INPUT("IN3L"),
748SND_SOC_DAPM_INPUT("IN3R"),
749
750SND_SOC_DAPM_OUTPUT("HPOUTL"),
751SND_SOC_DAPM_OUTPUT("HPOUTR"),
752SND_SOC_DAPM_OUTPUT("LINEOUTL"),
753SND_SOC_DAPM_OUTPUT("LINEOUTR"),
754SND_SOC_DAPM_OUTPUT("LOP"),
755SND_SOC_DAPM_OUTPUT("LON"),
756SND_SOC_DAPM_OUTPUT("ROP"),
757SND_SOC_DAPM_OUTPUT("RON"),
758
759SND_SOC_DAPM_MICBIAS("Mic Bias", WM8903_MIC_BIAS_CONTROL_0, 0, 0),
760
761SND_SOC_DAPM_MUX("Left Input Mux", SND_SOC_NOPM, 0, 0, &linput_mux),
762SND_SOC_DAPM_MUX("Left Input Inverting Mux", SND_SOC_NOPM, 0, 0,
763 &linput_inv_mux),
764SND_SOC_DAPM_MUX("Left Input Mode Mux", SND_SOC_NOPM, 0, 0, &linput_mode_mux),
765
766SND_SOC_DAPM_MUX("Right Input Mux", SND_SOC_NOPM, 0, 0, &rinput_mux),
767SND_SOC_DAPM_MUX("Right Input Inverting Mux", SND_SOC_NOPM, 0, 0,
768 &rinput_inv_mux),
769SND_SOC_DAPM_MUX("Right Input Mode Mux", SND_SOC_NOPM, 0, 0, &rinput_mode_mux),
770
771SND_SOC_DAPM_PGA("Left Input PGA", WM8903_POWER_MANAGEMENT_0, 1, 0, NULL, 0),
772SND_SOC_DAPM_PGA("Right Input PGA", WM8903_POWER_MANAGEMENT_0, 0, 0, NULL, 0),
773
774SND_SOC_DAPM_ADC("ADCL", "Left HiFi Capture", WM8903_POWER_MANAGEMENT_6, 1, 0),
775SND_SOC_DAPM_ADC("ADCR", "Right HiFi Capture", WM8903_POWER_MANAGEMENT_6, 0, 0),
776
Mark Brown291ce182009-04-22 21:36:14 +0100777SND_SOC_DAPM_MUX("DACL Sidetone", SND_SOC_NOPM, 0, 0, &lsidetone_mux),
778SND_SOC_DAPM_MUX("DACR Sidetone", SND_SOC_NOPM, 0, 0, &rsidetone_mux),
779
Mark Brownf1c0a022008-08-26 13:05:27 +0100780SND_SOC_DAPM_DAC("DACL", "Left Playback", WM8903_POWER_MANAGEMENT_6, 3, 0),
781SND_SOC_DAPM_DAC("DACR", "Right Playback", WM8903_POWER_MANAGEMENT_6, 2, 0),
782
783SND_SOC_DAPM_MIXER("Left Output Mixer", WM8903_POWER_MANAGEMENT_1, 1, 0,
784 left_output_mixer, ARRAY_SIZE(left_output_mixer)),
785SND_SOC_DAPM_MIXER("Right Output Mixer", WM8903_POWER_MANAGEMENT_1, 0, 0,
786 right_output_mixer, ARRAY_SIZE(right_output_mixer)),
787
788SND_SOC_DAPM_MIXER("Left Speaker Mixer", WM8903_POWER_MANAGEMENT_4, 1, 0,
789 left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer)),
790SND_SOC_DAPM_MIXER("Right Speaker Mixer", WM8903_POWER_MANAGEMENT_4, 0, 0,
791 right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer)),
792
793SND_SOC_DAPM_PGA_E("Left Headphone Output PGA", WM8903_POWER_MANAGEMENT_2,
794 1, 0, NULL, 0, wm8903_output_event,
795 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
Mark Brown42768a12009-04-22 18:39:39 +0100796 SND_SOC_DAPM_PRE_PMD),
Mark Brownf1c0a022008-08-26 13:05:27 +0100797SND_SOC_DAPM_PGA_E("Right Headphone Output PGA", WM8903_POWER_MANAGEMENT_2,
798 0, 0, NULL, 0, wm8903_output_event,
799 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
Mark Brown42768a12009-04-22 18:39:39 +0100800 SND_SOC_DAPM_PRE_PMD),
Mark Brownf1c0a022008-08-26 13:05:27 +0100801
802SND_SOC_DAPM_PGA_E("Left Line Output PGA", WM8903_POWER_MANAGEMENT_3, 1, 0,
803 NULL, 0, wm8903_output_event,
804 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
Mark Brown42768a12009-04-22 18:39:39 +0100805 SND_SOC_DAPM_PRE_PMD),
Mark Brownf1c0a022008-08-26 13:05:27 +0100806SND_SOC_DAPM_PGA_E("Right Line Output PGA", WM8903_POWER_MANAGEMENT_3, 0, 0,
807 NULL, 0, wm8903_output_event,
808 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
Mark Brown42768a12009-04-22 18:39:39 +0100809 SND_SOC_DAPM_PRE_PMD),
Mark Brownf1c0a022008-08-26 13:05:27 +0100810
811SND_SOC_DAPM_PGA("Left Speaker PGA", WM8903_POWER_MANAGEMENT_5, 1, 0,
812 NULL, 0),
813SND_SOC_DAPM_PGA("Right Speaker PGA", WM8903_POWER_MANAGEMENT_5, 0, 0,
814 NULL, 0),
815
Mark Brown42768a12009-04-22 18:39:39 +0100816SND_SOC_DAPM_SUPPLY("Charge Pump", WM8903_CHARGE_PUMP_0, 0, 0,
817 wm8903_cp_event, SND_SOC_DAPM_POST_PMU),
Mark Brownc2aef4f2009-04-22 20:04:44 +0100818SND_SOC_DAPM_SUPPLY("CLK_DSP", WM8903_CLOCK_RATES_2, 1, 0, NULL, 0),
Mark Brownf1c0a022008-08-26 13:05:27 +0100819};
820
821static const struct snd_soc_dapm_route intercon[] = {
822
823 { "Left Input Mux", "IN1L", "IN1L" },
824 { "Left Input Mux", "IN2L", "IN2L" },
825 { "Left Input Mux", "IN3L", "IN3L" },
826
827 { "Left Input Inverting Mux", "IN1L", "IN1L" },
828 { "Left Input Inverting Mux", "IN2L", "IN2L" },
829 { "Left Input Inverting Mux", "IN3L", "IN3L" },
830
831 { "Right Input Mux", "IN1R", "IN1R" },
832 { "Right Input Mux", "IN2R", "IN2R" },
833 { "Right Input Mux", "IN3R", "IN3R" },
834
835 { "Right Input Inverting Mux", "IN1R", "IN1R" },
836 { "Right Input Inverting Mux", "IN2R", "IN2R" },
837 { "Right Input Inverting Mux", "IN3R", "IN3R" },
838
839 { "Left Input Mode Mux", "Single-Ended", "Left Input Inverting Mux" },
840 { "Left Input Mode Mux", "Differential Line",
841 "Left Input Mux" },
842 { "Left Input Mode Mux", "Differential Line",
843 "Left Input Inverting Mux" },
844 { "Left Input Mode Mux", "Differential Mic",
845 "Left Input Mux" },
846 { "Left Input Mode Mux", "Differential Mic",
847 "Left Input Inverting Mux" },
848
849 { "Right Input Mode Mux", "Single-Ended",
850 "Right Input Inverting Mux" },
851 { "Right Input Mode Mux", "Differential Line",
852 "Right Input Mux" },
853 { "Right Input Mode Mux", "Differential Line",
854 "Right Input Inverting Mux" },
855 { "Right Input Mode Mux", "Differential Mic",
856 "Right Input Mux" },
857 { "Right Input Mode Mux", "Differential Mic",
858 "Right Input Inverting Mux" },
859
860 { "Left Input PGA", NULL, "Left Input Mode Mux" },
861 { "Right Input PGA", NULL, "Right Input Mode Mux" },
862
863 { "ADCL", NULL, "Left Input PGA" },
Mark Brownc2aef4f2009-04-22 20:04:44 +0100864 { "ADCL", NULL, "CLK_DSP" },
Mark Brownf1c0a022008-08-26 13:05:27 +0100865 { "ADCR", NULL, "Right Input PGA" },
Mark Brownc2aef4f2009-04-22 20:04:44 +0100866 { "ADCR", NULL, "CLK_DSP" },
867
Mark Brown291ce182009-04-22 21:36:14 +0100868 { "DACL Sidetone", "Left", "ADCL" },
869 { "DACL Sidetone", "Right", "ADCR" },
870 { "DACR Sidetone", "Left", "ADCL" },
871 { "DACR Sidetone", "Right", "ADCR" },
872
873 { "DACL", NULL, "DACL Sidetone" },
Mark Brownc2aef4f2009-04-22 20:04:44 +0100874 { "DACL", NULL, "CLK_DSP" },
Mark Brown291ce182009-04-22 21:36:14 +0100875 { "DACR", NULL, "DACR Sidetone" },
Mark Brownc2aef4f2009-04-22 20:04:44 +0100876 { "DACR", NULL, "CLK_DSP" },
Mark Brownf1c0a022008-08-26 13:05:27 +0100877
878 { "Left Output Mixer", "Left Bypass Switch", "Left Input PGA" },
879 { "Left Output Mixer", "Right Bypass Switch", "Right Input PGA" },
880 { "Left Output Mixer", "DACL Switch", "DACL" },
881 { "Left Output Mixer", "DACR Switch", "DACR" },
882
883 { "Right Output Mixer", "Left Bypass Switch", "Left Input PGA" },
884 { "Right Output Mixer", "Right Bypass Switch", "Right Input PGA" },
885 { "Right Output Mixer", "DACL Switch", "DACL" },
886 { "Right Output Mixer", "DACR Switch", "DACR" },
887
888 { "Left Speaker Mixer", "Left Bypass Switch", "Left Input PGA" },
889 { "Left Speaker Mixer", "Right Bypass Switch", "Right Input PGA" },
890 { "Left Speaker Mixer", "DACL Switch", "DACL" },
891 { "Left Speaker Mixer", "DACR Switch", "DACR" },
892
893 { "Right Speaker Mixer", "Left Bypass Switch", "Left Input PGA" },
894 { "Right Speaker Mixer", "Right Bypass Switch", "Right Input PGA" },
895 { "Right Speaker Mixer", "DACL Switch", "DACL" },
896 { "Right Speaker Mixer", "DACR Switch", "DACR" },
897
898 { "Left Line Output PGA", NULL, "Left Output Mixer" },
899 { "Right Line Output PGA", NULL, "Right Output Mixer" },
900
901 { "Left Headphone Output PGA", NULL, "Left Output Mixer" },
902 { "Right Headphone Output PGA", NULL, "Right Output Mixer" },
903
904 { "Left Speaker PGA", NULL, "Left Speaker Mixer" },
905 { "Right Speaker PGA", NULL, "Right Speaker Mixer" },
906
907 { "HPOUTL", NULL, "Left Headphone Output PGA" },
908 { "HPOUTR", NULL, "Right Headphone Output PGA" },
909
910 { "LINEOUTL", NULL, "Left Line Output PGA" },
911 { "LINEOUTR", NULL, "Right Line Output PGA" },
912
913 { "LOP", NULL, "Left Speaker PGA" },
914 { "LON", NULL, "Left Speaker PGA" },
915
916 { "ROP", NULL, "Right Speaker PGA" },
917 { "RON", NULL, "Right Speaker PGA" },
Mark Brown42768a12009-04-22 18:39:39 +0100918
919 { "Left Headphone Output PGA", NULL, "Charge Pump" },
920 { "Right Headphone Output PGA", NULL, "Charge Pump" },
921 { "Left Line Output PGA", NULL, "Charge Pump" },
922 { "Right Line Output PGA", NULL, "Charge Pump" },
Mark Brownf1c0a022008-08-26 13:05:27 +0100923};
924
925static int wm8903_add_widgets(struct snd_soc_codec *codec)
926{
927 snd_soc_dapm_new_controls(codec, wm8903_dapm_widgets,
928 ARRAY_SIZE(wm8903_dapm_widgets));
929
930 snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
931
Mark Brownf1c0a022008-08-26 13:05:27 +0100932 return 0;
933}
934
935static int wm8903_set_bias_level(struct snd_soc_codec *codec,
936 enum snd_soc_bias_level level)
937{
Mark Brownf1c0a022008-08-26 13:05:27 +0100938 u16 reg, reg2;
939
940 switch (level) {
941 case SND_SOC_BIAS_ON:
942 case SND_SOC_BIAS_PREPARE:
Mark Brown8d50e442009-07-10 23:12:01 +0100943 reg = snd_soc_read(codec, WM8903_VMID_CONTROL_0);
Mark Brownf1c0a022008-08-26 13:05:27 +0100944 reg &= ~(WM8903_VMID_RES_MASK);
945 reg |= WM8903_VMID_RES_50K;
Mark Brown8d50e442009-07-10 23:12:01 +0100946 snd_soc_write(codec, WM8903_VMID_CONTROL_0, reg);
Mark Brownf1c0a022008-08-26 13:05:27 +0100947 break;
948
949 case SND_SOC_BIAS_STANDBY:
950 if (codec->bias_level == SND_SOC_BIAS_OFF) {
Mark Brown8d50e442009-07-10 23:12:01 +0100951 snd_soc_write(codec, WM8903_CLOCK_RATES_2,
Mark Brown3b1228a2008-12-10 19:27:10 +0000952 WM8903_CLK_SYS_ENA);
953
Mark Brown4dbfe802009-04-22 20:32:40 +0100954 /* Change DC servo dither level in startup sequence */
Mark Brown8d50e442009-07-10 23:12:01 +0100955 snd_soc_write(codec, WM8903_WRITE_SEQUENCER_0, 0x11);
956 snd_soc_write(codec, WM8903_WRITE_SEQUENCER_1, 0x1257);
957 snd_soc_write(codec, WM8903_WRITE_SEQUENCER_2, 0x2);
Mark Brown4dbfe802009-04-22 20:32:40 +0100958
Mark Brownf1c0a022008-08-26 13:05:27 +0100959 wm8903_run_sequence(codec, 0);
960 wm8903_sync_reg_cache(codec, codec->reg_cache);
961
962 /* Enable low impedence charge pump output */
Mark Brown8d50e442009-07-10 23:12:01 +0100963 reg = snd_soc_read(codec,
Mark Brownf1c0a022008-08-26 13:05:27 +0100964 WM8903_CONTROL_INTERFACE_TEST_1);
Mark Brown8d50e442009-07-10 23:12:01 +0100965 snd_soc_write(codec, WM8903_CONTROL_INTERFACE_TEST_1,
Mark Brownf1c0a022008-08-26 13:05:27 +0100966 reg | WM8903_TEST_KEY);
Mark Brown8d50e442009-07-10 23:12:01 +0100967 reg2 = snd_soc_read(codec, WM8903_CHARGE_PUMP_TEST_1);
968 snd_soc_write(codec, WM8903_CHARGE_PUMP_TEST_1,
Mark Brownf1c0a022008-08-26 13:05:27 +0100969 reg2 | WM8903_CP_SW_KELVIN_MODE_MASK);
Mark Brown8d50e442009-07-10 23:12:01 +0100970 snd_soc_write(codec, WM8903_CONTROL_INTERFACE_TEST_1,
Mark Brownf1c0a022008-08-26 13:05:27 +0100971 reg);
972
973 /* By default no bypass paths are enabled so
974 * enable Class W support.
975 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000976 dev_dbg(codec->dev, "Enabling Class W\n");
Mark Brown8d50e442009-07-10 23:12:01 +0100977 snd_soc_write(codec, WM8903_CLASS_W_0, reg |
Mark Brownf1c0a022008-08-26 13:05:27 +0100978 WM8903_CP_DYN_FREQ | WM8903_CP_DYN_V);
979 }
980
Mark Brown8d50e442009-07-10 23:12:01 +0100981 reg = snd_soc_read(codec, WM8903_VMID_CONTROL_0);
Mark Brownf1c0a022008-08-26 13:05:27 +0100982 reg &= ~(WM8903_VMID_RES_MASK);
983 reg |= WM8903_VMID_RES_250K;
Mark Brown8d50e442009-07-10 23:12:01 +0100984 snd_soc_write(codec, WM8903_VMID_CONTROL_0, reg);
Mark Brownf1c0a022008-08-26 13:05:27 +0100985 break;
986
987 case SND_SOC_BIAS_OFF:
988 wm8903_run_sequence(codec, 32);
Mark Brown8d50e442009-07-10 23:12:01 +0100989 reg = snd_soc_read(codec, WM8903_CLOCK_RATES_2);
Mark Brown3b1228a2008-12-10 19:27:10 +0000990 reg &= ~WM8903_CLK_SYS_ENA;
Mark Brown8d50e442009-07-10 23:12:01 +0100991 snd_soc_write(codec, WM8903_CLOCK_RATES_2, reg);
Mark Brownf1c0a022008-08-26 13:05:27 +0100992 break;
993 }
994
995 codec->bias_level = level;
996
997 return 0;
998}
999
1000static int wm8903_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1001 int clk_id, unsigned int freq, int dir)
1002{
1003 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001004 struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
Mark Brownf1c0a022008-08-26 13:05:27 +01001005
1006 wm8903->sysclk = freq;
1007
1008 return 0;
1009}
1010
1011static int wm8903_set_dai_fmt(struct snd_soc_dai *codec_dai,
1012 unsigned int fmt)
1013{
1014 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brown8d50e442009-07-10 23:12:01 +01001015 u16 aif1 = snd_soc_read(codec, WM8903_AUDIO_INTERFACE_1);
Mark Brownf1c0a022008-08-26 13:05:27 +01001016
1017 aif1 &= ~(WM8903_LRCLK_DIR | WM8903_BCLK_DIR | WM8903_AIF_FMT_MASK |
1018 WM8903_AIF_LRCLK_INV | WM8903_AIF_BCLK_INV);
1019
1020 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1021 case SND_SOC_DAIFMT_CBS_CFS:
1022 break;
1023 case SND_SOC_DAIFMT_CBS_CFM:
1024 aif1 |= WM8903_LRCLK_DIR;
1025 break;
1026 case SND_SOC_DAIFMT_CBM_CFM:
1027 aif1 |= WM8903_LRCLK_DIR | WM8903_BCLK_DIR;
1028 break;
1029 case SND_SOC_DAIFMT_CBM_CFS:
1030 aif1 |= WM8903_BCLK_DIR;
1031 break;
1032 default:
1033 return -EINVAL;
1034 }
1035
1036 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1037 case SND_SOC_DAIFMT_DSP_A:
1038 aif1 |= 0x3;
1039 break;
1040 case SND_SOC_DAIFMT_DSP_B:
1041 aif1 |= 0x3 | WM8903_AIF_LRCLK_INV;
1042 break;
1043 case SND_SOC_DAIFMT_I2S:
1044 aif1 |= 0x2;
1045 break;
1046 case SND_SOC_DAIFMT_RIGHT_J:
1047 aif1 |= 0x1;
1048 break;
1049 case SND_SOC_DAIFMT_LEFT_J:
1050 break;
1051 default:
1052 return -EINVAL;
1053 }
1054
1055 /* Clock inversion */
1056 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1057 case SND_SOC_DAIFMT_DSP_A:
1058 case SND_SOC_DAIFMT_DSP_B:
1059 /* frame inversion not valid for DSP modes */
1060 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1061 case SND_SOC_DAIFMT_NB_NF:
1062 break;
1063 case SND_SOC_DAIFMT_IB_NF:
1064 aif1 |= WM8903_AIF_BCLK_INV;
1065 break;
1066 default:
1067 return -EINVAL;
1068 }
1069 break;
1070 case SND_SOC_DAIFMT_I2S:
1071 case SND_SOC_DAIFMT_RIGHT_J:
1072 case SND_SOC_DAIFMT_LEFT_J:
1073 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1074 case SND_SOC_DAIFMT_NB_NF:
1075 break;
1076 case SND_SOC_DAIFMT_IB_IF:
1077 aif1 |= WM8903_AIF_BCLK_INV | WM8903_AIF_LRCLK_INV;
1078 break;
1079 case SND_SOC_DAIFMT_IB_NF:
1080 aif1 |= WM8903_AIF_BCLK_INV;
1081 break;
1082 case SND_SOC_DAIFMT_NB_IF:
1083 aif1 |= WM8903_AIF_LRCLK_INV;
1084 break;
1085 default:
1086 return -EINVAL;
1087 }
1088 break;
1089 default:
1090 return -EINVAL;
1091 }
1092
Mark Brown8d50e442009-07-10 23:12:01 +01001093 snd_soc_write(codec, WM8903_AUDIO_INTERFACE_1, aif1);
Mark Brownf1c0a022008-08-26 13:05:27 +01001094
1095 return 0;
1096}
1097
1098static int wm8903_digital_mute(struct snd_soc_dai *codec_dai, int mute)
1099{
1100 struct snd_soc_codec *codec = codec_dai->codec;
1101 u16 reg;
1102
Mark Brown8d50e442009-07-10 23:12:01 +01001103 reg = snd_soc_read(codec, WM8903_DAC_DIGITAL_1);
Mark Brownf1c0a022008-08-26 13:05:27 +01001104
1105 if (mute)
1106 reg |= WM8903_DAC_MUTE;
1107 else
1108 reg &= ~WM8903_DAC_MUTE;
1109
Mark Brown8d50e442009-07-10 23:12:01 +01001110 snd_soc_write(codec, WM8903_DAC_DIGITAL_1, reg);
Mark Brownf1c0a022008-08-26 13:05:27 +01001111
1112 return 0;
1113}
1114
1115/* Lookup table for CLK_SYS/fs ratio. 256fs or more is recommended
1116 * for optimal performance so we list the lower rates first and match
1117 * on the last match we find. */
1118static struct {
1119 int div;
1120 int rate;
1121 int mode;
1122 int mclk_div;
1123} clk_sys_ratios[] = {
1124 { 64, 0x0, 0x0, 1 },
1125 { 68, 0x0, 0x1, 1 },
1126 { 125, 0x0, 0x2, 1 },
1127 { 128, 0x1, 0x0, 1 },
1128 { 136, 0x1, 0x1, 1 },
1129 { 192, 0x2, 0x0, 1 },
1130 { 204, 0x2, 0x1, 1 },
1131
1132 { 64, 0x0, 0x0, 2 },
1133 { 68, 0x0, 0x1, 2 },
1134 { 125, 0x0, 0x2, 2 },
1135 { 128, 0x1, 0x0, 2 },
1136 { 136, 0x1, 0x1, 2 },
1137 { 192, 0x2, 0x0, 2 },
1138 { 204, 0x2, 0x1, 2 },
1139
1140 { 250, 0x2, 0x2, 1 },
1141 { 256, 0x3, 0x0, 1 },
1142 { 272, 0x3, 0x1, 1 },
1143 { 384, 0x4, 0x0, 1 },
1144 { 408, 0x4, 0x1, 1 },
1145 { 375, 0x4, 0x2, 1 },
1146 { 512, 0x5, 0x0, 1 },
1147 { 544, 0x5, 0x1, 1 },
1148 { 500, 0x5, 0x2, 1 },
1149 { 768, 0x6, 0x0, 1 },
1150 { 816, 0x6, 0x1, 1 },
1151 { 750, 0x6, 0x2, 1 },
1152 { 1024, 0x7, 0x0, 1 },
1153 { 1088, 0x7, 0x1, 1 },
1154 { 1000, 0x7, 0x2, 1 },
1155 { 1408, 0x8, 0x0, 1 },
1156 { 1496, 0x8, 0x1, 1 },
1157 { 1536, 0x9, 0x0, 1 },
1158 { 1632, 0x9, 0x1, 1 },
1159 { 1500, 0x9, 0x2, 1 },
1160
1161 { 250, 0x2, 0x2, 2 },
1162 { 256, 0x3, 0x0, 2 },
1163 { 272, 0x3, 0x1, 2 },
1164 { 384, 0x4, 0x0, 2 },
1165 { 408, 0x4, 0x1, 2 },
1166 { 375, 0x4, 0x2, 2 },
1167 { 512, 0x5, 0x0, 2 },
1168 { 544, 0x5, 0x1, 2 },
1169 { 500, 0x5, 0x2, 2 },
1170 { 768, 0x6, 0x0, 2 },
1171 { 816, 0x6, 0x1, 2 },
1172 { 750, 0x6, 0x2, 2 },
1173 { 1024, 0x7, 0x0, 2 },
1174 { 1088, 0x7, 0x1, 2 },
1175 { 1000, 0x7, 0x2, 2 },
1176 { 1408, 0x8, 0x0, 2 },
1177 { 1496, 0x8, 0x1, 2 },
1178 { 1536, 0x9, 0x0, 2 },
1179 { 1632, 0x9, 0x1, 2 },
1180 { 1500, 0x9, 0x2, 2 },
1181};
1182
1183/* CLK_SYS/BCLK ratios - multiplied by 10 due to .5s */
1184static struct {
1185 int ratio;
1186 int div;
1187} bclk_divs[] = {
1188 { 10, 0 },
Mark Brownf1c0a022008-08-26 13:05:27 +01001189 { 20, 2 },
1190 { 30, 3 },
1191 { 40, 4 },
1192 { 50, 5 },
Mark Brownf1c0a022008-08-26 13:05:27 +01001193 { 60, 7 },
1194 { 80, 8 },
1195 { 100, 9 },
Mark Brownf1c0a022008-08-26 13:05:27 +01001196 { 120, 11 },
1197 { 160, 12 },
1198 { 200, 13 },
1199 { 220, 14 },
1200 { 240, 15 },
Mark Brownf1c0a022008-08-26 13:05:27 +01001201 { 300, 17 },
1202 { 320, 18 },
1203 { 440, 19 },
1204 { 480, 20 },
1205};
1206
1207/* Sample rates for DSP */
1208static struct {
1209 int rate;
1210 int value;
1211} sample_rates[] = {
1212 { 8000, 0 },
1213 { 11025, 1 },
1214 { 12000, 2 },
1215 { 16000, 3 },
1216 { 22050, 4 },
1217 { 24000, 5 },
1218 { 32000, 6 },
1219 { 44100, 7 },
1220 { 48000, 8 },
1221 { 88200, 9 },
1222 { 96000, 10 },
1223 { 0, 0 },
1224};
1225
Mark Browndee89c42008-11-18 22:11:38 +00001226static int wm8903_startup(struct snd_pcm_substream *substream,
1227 struct snd_soc_dai *dai)
Mark Brownf1c0a022008-08-26 13:05:27 +01001228{
1229 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001230 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001231 struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
Mark Brownf1c0a022008-08-26 13:05:27 +01001232 struct snd_pcm_runtime *master_runtime;
1233
1234 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1235 wm8903->playback_active++;
1236 else
1237 wm8903->capture_active++;
1238
1239 /* The DAI has shared clocks so if we already have a playback or
1240 * capture going then constrain this substream to match it.
1241 */
1242 if (wm8903->master_substream) {
1243 master_runtime = wm8903->master_substream->runtime;
1244
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001245 dev_dbg(codec->dev, "Constraining to %d bits\n",
Mark Brown727fb902009-04-22 21:06:14 +01001246 master_runtime->sample_bits);
Mark Brownf1c0a022008-08-26 13:05:27 +01001247
1248 snd_pcm_hw_constraint_minmax(substream->runtime,
1249 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1250 master_runtime->sample_bits,
1251 master_runtime->sample_bits);
1252
1253 wm8903->slave_substream = substream;
1254 } else
1255 wm8903->master_substream = substream;
1256
1257 return 0;
1258}
1259
Mark Browndee89c42008-11-18 22:11:38 +00001260static void wm8903_shutdown(struct snd_pcm_substream *substream,
1261 struct snd_soc_dai *dai)
Mark Brownf1c0a022008-08-26 13:05:27 +01001262{
1263 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001264 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001265 struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
Mark Brownf1c0a022008-08-26 13:05:27 +01001266
1267 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1268 wm8903->playback_active--;
1269 else
1270 wm8903->capture_active--;
1271
1272 if (wm8903->master_substream == substream)
1273 wm8903->master_substream = wm8903->slave_substream;
1274
1275 wm8903->slave_substream = NULL;
1276}
1277
1278static int wm8903_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +00001279 struct snd_pcm_hw_params *params,
1280 struct snd_soc_dai *dai)
Mark Brownf1c0a022008-08-26 13:05:27 +01001281{
1282 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001283 struct snd_soc_codec *codec =rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001284 struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
Mark Brownf1c0a022008-08-26 13:05:27 +01001285 int fs = params_rate(params);
1286 int bclk;
1287 int bclk_div;
1288 int i;
1289 int dsp_config;
1290 int clk_config;
1291 int best_val;
1292 int cur_val;
1293 int clk_sys;
1294
Mark Brown8d50e442009-07-10 23:12:01 +01001295 u16 aif1 = snd_soc_read(codec, WM8903_AUDIO_INTERFACE_1);
1296 u16 aif2 = snd_soc_read(codec, WM8903_AUDIO_INTERFACE_2);
1297 u16 aif3 = snd_soc_read(codec, WM8903_AUDIO_INTERFACE_3);
1298 u16 clock0 = snd_soc_read(codec, WM8903_CLOCK_RATES_0);
1299 u16 clock1 = snd_soc_read(codec, WM8903_CLOCK_RATES_1);
1300 u16 dac_digital1 = snd_soc_read(codec, WM8903_DAC_DIGITAL_1);
Mark Brownf1c0a022008-08-26 13:05:27 +01001301
1302 if (substream == wm8903->slave_substream) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001303 dev_dbg(codec->dev, "Ignoring hw_params for slave substream\n");
Mark Brownf1c0a022008-08-26 13:05:27 +01001304 return 0;
1305 }
1306
Mark Brown9e792612009-06-12 17:27:07 +01001307 /* Enable sloping stopband filter for low sample rates */
1308 if (fs <= 24000)
1309 dac_digital1 |= WM8903_DAC_SB_FILT;
1310 else
1311 dac_digital1 &= ~WM8903_DAC_SB_FILT;
1312
Mark Brownf1c0a022008-08-26 13:05:27 +01001313 /* Configure sample rate logic for DSP - choose nearest rate */
1314 dsp_config = 0;
1315 best_val = abs(sample_rates[dsp_config].rate - fs);
1316 for (i = 1; i < ARRAY_SIZE(sample_rates); i++) {
1317 cur_val = abs(sample_rates[i].rate - fs);
1318 if (cur_val <= best_val) {
1319 dsp_config = i;
1320 best_val = cur_val;
1321 }
1322 }
1323
1324 /* Constraints should stop us hitting this but let's make sure */
1325 if (wm8903->capture_active)
1326 switch (sample_rates[dsp_config].rate) {
1327 case 88200:
1328 case 96000:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001329 dev_err(codec->dev, "%dHz unsupported by ADC\n",
Mark Brownf1c0a022008-08-26 13:05:27 +01001330 fs);
1331 return -EINVAL;
1332
1333 default:
1334 break;
1335 }
1336
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001337 dev_dbg(codec->dev, "DSP fs = %dHz\n", sample_rates[dsp_config].rate);
Mark Brownf1c0a022008-08-26 13:05:27 +01001338 clock1 &= ~WM8903_SAMPLE_RATE_MASK;
1339 clock1 |= sample_rates[dsp_config].value;
1340
1341 aif1 &= ~WM8903_AIF_WL_MASK;
1342 bclk = 2 * fs;
1343 switch (params_format(params)) {
1344 case SNDRV_PCM_FORMAT_S16_LE:
1345 bclk *= 16;
1346 break;
1347 case SNDRV_PCM_FORMAT_S20_3LE:
1348 bclk *= 20;
1349 aif1 |= 0x4;
1350 break;
1351 case SNDRV_PCM_FORMAT_S24_LE:
1352 bclk *= 24;
1353 aif1 |= 0x8;
1354 break;
1355 case SNDRV_PCM_FORMAT_S32_LE:
1356 bclk *= 32;
1357 aif1 |= 0xc;
1358 break;
1359 default:
1360 return -EINVAL;
1361 }
1362
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001363 dev_dbg(codec->dev, "MCLK = %dHz, target sample rate = %dHz\n",
Mark Brownf1c0a022008-08-26 13:05:27 +01001364 wm8903->sysclk, fs);
1365
1366 /* We may not have an MCLK which allows us to generate exactly
1367 * the clock we want, particularly with USB derived inputs, so
1368 * approximate.
1369 */
1370 clk_config = 0;
1371 best_val = abs((wm8903->sysclk /
1372 (clk_sys_ratios[0].mclk_div *
1373 clk_sys_ratios[0].div)) - fs);
1374 for (i = 1; i < ARRAY_SIZE(clk_sys_ratios); i++) {
1375 cur_val = abs((wm8903->sysclk /
1376 (clk_sys_ratios[i].mclk_div *
1377 clk_sys_ratios[i].div)) - fs);
1378
1379 if (cur_val <= best_val) {
1380 clk_config = i;
1381 best_val = cur_val;
1382 }
1383 }
1384
1385 if (clk_sys_ratios[clk_config].mclk_div == 2) {
1386 clock0 |= WM8903_MCLKDIV2;
1387 clk_sys = wm8903->sysclk / 2;
1388 } else {
1389 clock0 &= ~WM8903_MCLKDIV2;
1390 clk_sys = wm8903->sysclk;
1391 }
1392
1393 clock1 &= ~(WM8903_CLK_SYS_RATE_MASK |
1394 WM8903_CLK_SYS_MODE_MASK);
1395 clock1 |= clk_sys_ratios[clk_config].rate << WM8903_CLK_SYS_RATE_SHIFT;
1396 clock1 |= clk_sys_ratios[clk_config].mode << WM8903_CLK_SYS_MODE_SHIFT;
1397
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001398 dev_dbg(codec->dev, "CLK_SYS_RATE=%x, CLK_SYS_MODE=%x div=%d\n",
Mark Brownf1c0a022008-08-26 13:05:27 +01001399 clk_sys_ratios[clk_config].rate,
1400 clk_sys_ratios[clk_config].mode,
1401 clk_sys_ratios[clk_config].div);
1402
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001403 dev_dbg(codec->dev, "Actual CLK_SYS = %dHz\n", clk_sys);
Mark Brownf1c0a022008-08-26 13:05:27 +01001404
1405 /* We may not get quite the right frequency if using
1406 * approximate clocks so look for the closest match that is
1407 * higher than the target (we need to ensure that there enough
1408 * BCLKs to clock out the samples).
1409 */
1410 bclk_div = 0;
1411 best_val = ((clk_sys * 10) / bclk_divs[0].ratio) - bclk;
1412 i = 1;
1413 while (i < ARRAY_SIZE(bclk_divs)) {
1414 cur_val = ((clk_sys * 10) / bclk_divs[i].ratio) - bclk;
1415 if (cur_val < 0) /* BCLK table is sorted */
1416 break;
1417 bclk_div = i;
1418 best_val = cur_val;
1419 i++;
1420 }
1421
1422 aif2 &= ~WM8903_BCLK_DIV_MASK;
1423 aif3 &= ~WM8903_LRCLK_RATE_MASK;
1424
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001425 dev_dbg(codec->dev, "BCLK ratio %d for %dHz - actual BCLK = %dHz\n",
Mark Brownf1c0a022008-08-26 13:05:27 +01001426 bclk_divs[bclk_div].ratio / 10, bclk,
1427 (clk_sys * 10) / bclk_divs[bclk_div].ratio);
1428
1429 aif2 |= bclk_divs[bclk_div].div;
1430 aif3 |= bclk / fs;
1431
Mark Brown8d50e442009-07-10 23:12:01 +01001432 snd_soc_write(codec, WM8903_CLOCK_RATES_0, clock0);
1433 snd_soc_write(codec, WM8903_CLOCK_RATES_1, clock1);
1434 snd_soc_write(codec, WM8903_AUDIO_INTERFACE_1, aif1);
1435 snd_soc_write(codec, WM8903_AUDIO_INTERFACE_2, aif2);
1436 snd_soc_write(codec, WM8903_AUDIO_INTERFACE_3, aif3);
1437 snd_soc_write(codec, WM8903_DAC_DIGITAL_1, dac_digital1);
Mark Brownf1c0a022008-08-26 13:05:27 +01001438
1439 return 0;
1440}
1441
Mark Brown72453872010-03-15 21:22:58 +00001442/**
1443 * wm8903_mic_detect - Enable microphone detection via the WM8903 IRQ
1444 *
1445 * @codec: WM8903 codec
1446 * @jack: jack to report detection events on
1447 * @det: value to report for presence detection
1448 * @shrt: value to report for short detection
1449 *
1450 * Enable microphone detection via IRQ on the WM8903. If GPIOs are
1451 * being used to bring out signals to the processor then only platform
1452 * data configuration is needed for WM8903 and processor GPIOs should
1453 * be configured using snd_soc_jack_add_gpios() instead.
1454 *
1455 * The current threasholds for detection should be configured using
1456 * micdet_cfg in the platform data. Using this function will force on
1457 * the microphone bias for the device.
1458 */
1459int wm8903_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack,
1460 int det, int shrt)
1461{
Mark Brownb2c812e2010-04-14 15:35:19 +09001462 struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
Mark Brown69266862010-03-22 16:37:01 +00001463 int irq_mask = WM8903_MICDET_EINT | WM8903_MICSHRT_EINT;
Mark Brown72453872010-03-15 21:22:58 +00001464
1465 dev_dbg(codec->dev, "Enabling microphone detection: %x %x\n",
1466 det, shrt);
1467
1468 /* Store the configuration */
1469 wm8903->mic_jack = jack;
1470 wm8903->mic_det = det;
1471 wm8903->mic_short = shrt;
1472
1473 /* Enable interrupts we've got a report configured for */
1474 if (det)
1475 irq_mask &= ~WM8903_MICDET_EINT;
1476 if (shrt)
1477 irq_mask &= ~WM8903_MICSHRT_EINT;
1478
1479 snd_soc_update_bits(codec, WM8903_INTERRUPT_STATUS_1_MASK,
1480 WM8903_MICDET_EINT | WM8903_MICSHRT_EINT,
1481 irq_mask);
1482
Mark Brown69266862010-03-22 16:37:01 +00001483 if (det && shrt) {
1484 /* Enable mic detection, this may not have been set through
1485 * platform data (eg, if the defaults are OK). */
1486 snd_soc_update_bits(codec, WM8903_WRITE_SEQUENCER_0,
1487 WM8903_WSEQ_ENA, WM8903_WSEQ_ENA);
1488 snd_soc_update_bits(codec, WM8903_MIC_BIAS_CONTROL_0,
1489 WM8903_MICDET_ENA, WM8903_MICDET_ENA);
1490 } else {
1491 snd_soc_update_bits(codec, WM8903_MIC_BIAS_CONTROL_0,
1492 WM8903_MICDET_ENA, 0);
1493 }
Mark Brown72453872010-03-15 21:22:58 +00001494
1495 return 0;
1496}
1497EXPORT_SYMBOL_GPL(wm8903_mic_detect);
1498
Mark Brown8abd16a2010-03-15 18:25:26 +00001499static irqreturn_t wm8903_irq(int irq, void *data)
1500{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001501 struct snd_soc_codec *codec = data;
1502 struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
Mark Brown72453872010-03-15 21:22:58 +00001503 int mic_report;
1504 int int_pol;
1505 int int_val = 0;
1506 int mask = ~snd_soc_read(codec, WM8903_INTERRUPT_STATUS_1_MASK);
Mark Brown8abd16a2010-03-15 18:25:26 +00001507
Mark Brown72453872010-03-15 21:22:58 +00001508 int_val = snd_soc_read(codec, WM8903_INTERRUPT_STATUS_1) & mask;
Mark Brown8abd16a2010-03-15 18:25:26 +00001509
Mark Brown72453872010-03-15 21:22:58 +00001510 if (int_val & WM8903_WSEQ_BUSY_EINT) {
Mark Brown8abd16a2010-03-15 18:25:26 +00001511 dev_dbg(codec->dev, "Write sequencer done\n");
1512 complete(&wm8903->wseq);
1513 }
1514
Mark Brown72453872010-03-15 21:22:58 +00001515 /*
1516 * The rest is microphone jack detection. We need to manually
1517 * invert the polarity of the interrupt after each event - to
1518 * simplify the code keep track of the last state we reported
1519 * and just invert the relevant bits in both the report and
1520 * the polarity register.
1521 */
1522 mic_report = wm8903->mic_last_report;
1523 int_pol = snd_soc_read(codec, WM8903_INTERRUPT_POLARITY_1);
1524
1525 if (int_val & WM8903_MICSHRT_EINT) {
1526 dev_dbg(codec->dev, "Microphone short (pol=%x)\n", int_pol);
1527
1528 mic_report ^= wm8903->mic_short;
1529 int_pol ^= WM8903_MICSHRT_INV;
1530 }
1531
1532 if (int_val & WM8903_MICDET_EINT) {
1533 dev_dbg(codec->dev, "Microphone detect (pol=%x)\n", int_pol);
1534
1535 mic_report ^= wm8903->mic_det;
1536 int_pol ^= WM8903_MICDET_INV;
1537
1538 msleep(wm8903->mic_delay);
1539 }
1540
1541 snd_soc_update_bits(codec, WM8903_INTERRUPT_POLARITY_1,
1542 WM8903_MICSHRT_INV | WM8903_MICDET_INV, int_pol);
1543
1544 snd_soc_jack_report(wm8903->mic_jack, mic_report,
1545 wm8903->mic_short | wm8903->mic_det);
1546
1547 wm8903->mic_last_report = mic_report;
1548
Mark Brown8abd16a2010-03-15 18:25:26 +00001549 return IRQ_HANDLED;
1550}
1551
Mark Brownf1c0a022008-08-26 13:05:27 +01001552#define WM8903_PLAYBACK_RATES (SNDRV_PCM_RATE_8000 |\
1553 SNDRV_PCM_RATE_11025 | \
1554 SNDRV_PCM_RATE_16000 | \
1555 SNDRV_PCM_RATE_22050 | \
1556 SNDRV_PCM_RATE_32000 | \
1557 SNDRV_PCM_RATE_44100 | \
1558 SNDRV_PCM_RATE_48000 | \
1559 SNDRV_PCM_RATE_88200 | \
1560 SNDRV_PCM_RATE_96000)
1561
1562#define WM8903_CAPTURE_RATES (SNDRV_PCM_RATE_8000 |\
1563 SNDRV_PCM_RATE_11025 | \
1564 SNDRV_PCM_RATE_16000 | \
1565 SNDRV_PCM_RATE_22050 | \
1566 SNDRV_PCM_RATE_32000 | \
1567 SNDRV_PCM_RATE_44100 | \
1568 SNDRV_PCM_RATE_48000)
1569
1570#define WM8903_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
1571 SNDRV_PCM_FMTBIT_S20_3LE |\
1572 SNDRV_PCM_FMTBIT_S24_LE)
1573
Eric Miao6335d052009-03-03 09:41:00 +08001574static struct snd_soc_dai_ops wm8903_dai_ops = {
1575 .startup = wm8903_startup,
1576 .shutdown = wm8903_shutdown,
1577 .hw_params = wm8903_hw_params,
1578 .digital_mute = wm8903_digital_mute,
1579 .set_fmt = wm8903_set_dai_fmt,
1580 .set_sysclk = wm8903_set_dai_sysclk,
1581};
1582
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001583static struct snd_soc_dai_driver wm8903_dai = {
1584 .name = "wm8903-hifi",
Mark Brownf1c0a022008-08-26 13:05:27 +01001585 .playback = {
1586 .stream_name = "Playback",
1587 .channels_min = 2,
1588 .channels_max = 2,
1589 .rates = WM8903_PLAYBACK_RATES,
1590 .formats = WM8903_FORMATS,
1591 },
1592 .capture = {
1593 .stream_name = "Capture",
1594 .channels_min = 2,
1595 .channels_max = 2,
1596 .rates = WM8903_CAPTURE_RATES,
1597 .formats = WM8903_FORMATS,
1598 },
Eric Miao6335d052009-03-03 09:41:00 +08001599 .ops = &wm8903_dai_ops,
Mark Brown0d960e82009-04-16 10:08:39 +01001600 .symmetric_rates = 1,
Mark Brownf1c0a022008-08-26 13:05:27 +01001601};
Mark Brownf1c0a022008-08-26 13:05:27 +01001602
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001603static int wm8903_suspend(struct snd_soc_codec *codec, pm_message_t state)
Mark Brownf1c0a022008-08-26 13:05:27 +01001604{
Mark Brownf1c0a022008-08-26 13:05:27 +01001605 wm8903_set_bias_level(codec, SND_SOC_BIAS_OFF);
1606
1607 return 0;
1608}
1609
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001610static int wm8903_resume(struct snd_soc_codec *codec)
Mark Brownf1c0a022008-08-26 13:05:27 +01001611{
Mark Brownf1c0a022008-08-26 13:05:27 +01001612 int i;
1613 u16 *reg_cache = codec->reg_cache;
Guennadi Liakhovetski40aa7032010-01-22 18:00:03 +01001614 u16 *tmp_cache = kmemdup(reg_cache, sizeof(wm8903_reg_defaults),
Mark Brownf1c0a022008-08-26 13:05:27 +01001615 GFP_KERNEL);
1616
1617 /* Bring the codec back up to standby first to minimise pop/clicks */
1618 wm8903_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Mark Brownf1c0a022008-08-26 13:05:27 +01001619
1620 /* Sync back everything else */
1621 if (tmp_cache) {
1622 for (i = 2; i < ARRAY_SIZE(wm8903_reg_defaults); i++)
1623 if (tmp_cache[i] != reg_cache[i])
Mark Brown8d50e442009-07-10 23:12:01 +01001624 snd_soc_write(codec, i, tmp_cache[i]);
Guennadi Liakhovetski40aa7032010-01-22 18:00:03 +01001625 kfree(tmp_cache);
Mark Brownf1c0a022008-08-26 13:05:27 +01001626 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001627 dev_err(codec->dev, "Failed to allocate temporary cache\n");
Mark Brownf1c0a022008-08-26 13:05:27 +01001628 }
1629
1630 return 0;
1631}
1632
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001633static int wm8903_probe(struct snd_soc_codec *codec)
Mark Brownf1c0a022008-08-26 13:05:27 +01001634{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001635 struct wm8903_platform_data *pdata = dev_get_platdata(codec->dev);
1636 struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
Mark Brown73b34ea2010-03-15 17:46:02 +00001637 int ret, i;
Mark Brown8abd16a2010-03-15 18:25:26 +00001638 int trigger, irq_pol;
Mark Brownf1c0a022008-08-26 13:05:27 +01001639 u16 val;
1640
Mark Brown8abd16a2010-03-15 18:25:26 +00001641 init_completion(&wm8903->wseq);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001642 codec->control_data = wm8903->control_data;
Mark Brownd58d5d52008-12-10 18:36:42 +00001643
Mark Brown8d50e442009-07-10 23:12:01 +01001644 ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_I2C);
1645 if (ret != 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001646 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
1647 return ret;
Mark Brown8d50e442009-07-10 23:12:01 +01001648 }
1649
1650 val = snd_soc_read(codec, WM8903_SW_RESET_AND_ID);
Mark Brownf1c0a022008-08-26 13:05:27 +01001651 if (val != wm8903_reg_defaults[WM8903_SW_RESET_AND_ID]) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001652 dev_err(codec->dev,
Mark Brownf1c0a022008-08-26 13:05:27 +01001653 "Device with ID register %x is not a WM8903\n", val);
1654 return -ENODEV;
1655 }
1656
Mark Brown8d50e442009-07-10 23:12:01 +01001657 val = snd_soc_read(codec, WM8903_REVISION_NUMBER);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001658 dev_info(codec->dev, "WM8903 revision %d\n",
Mark Brownf1c0a022008-08-26 13:05:27 +01001659 val & WM8903_CHIP_REV_MASK);
1660
1661 wm8903_reset(codec);
1662
Mark Brown37f88e82010-03-15 18:14:34 +00001663 /* Set up GPIOs and microphone detection */
Mark Brown73b34ea2010-03-15 17:46:02 +00001664 if (pdata) {
1665 for (i = 0; i < ARRAY_SIZE(pdata->gpio_cfg); i++) {
1666 if (!pdata->gpio_cfg[i])
1667 continue;
1668
1669 snd_soc_write(codec, WM8903_GPIO_CONTROL_1 + i,
1670 pdata->gpio_cfg[i] & 0xffff);
1671 }
Mark Brown37f88e82010-03-15 18:14:34 +00001672
1673 snd_soc_write(codec, WM8903_MIC_BIAS_CONTROL_0,
1674 pdata->micdet_cfg);
1675
1676 /* Microphone detection needs the WSEQ clock */
1677 if (pdata->micdet_cfg)
1678 snd_soc_update_bits(codec, WM8903_WRITE_SEQUENCER_0,
1679 WM8903_WSEQ_ENA, WM8903_WSEQ_ENA);
1680
1681 wm8903->mic_delay = pdata->micdet_delay;
Mark Brown73b34ea2010-03-15 17:46:02 +00001682 }
Mark Brown8abd16a2010-03-15 18:25:26 +00001683
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001684 if (wm8903->irq) {
Mark Brown8abd16a2010-03-15 18:25:26 +00001685 if (pdata && pdata->irq_active_low) {
1686 trigger = IRQF_TRIGGER_LOW;
1687 irq_pol = WM8903_IRQ_POL;
1688 } else {
1689 trigger = IRQF_TRIGGER_HIGH;
1690 irq_pol = 0;
1691 }
1692
1693 snd_soc_update_bits(codec, WM8903_INTERRUPT_CONTROL,
1694 WM8903_IRQ_POL, irq_pol);
1695
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001696 ret = request_threaded_irq(wm8903->irq, NULL, wm8903_irq,
Mark Brown8abd16a2010-03-15 18:25:26 +00001697 trigger | IRQF_ONESHOT,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001698 "wm8903", codec);
Mark Brown8abd16a2010-03-15 18:25:26 +00001699 if (ret != 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001700 dev_err(codec->dev, "Failed to request IRQ: %d\n",
Mark Brown8abd16a2010-03-15 18:25:26 +00001701 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001702 return ret;
Mark Brown8abd16a2010-03-15 18:25:26 +00001703 }
1704
1705 /* Enable write sequencer interrupts */
1706 snd_soc_update_bits(codec, WM8903_INTERRUPT_STATUS_1_MASK,
1707 WM8903_IM_WSEQ_BUSY_EINT, 0);
1708 }
Mark Brown73b34ea2010-03-15 17:46:02 +00001709
Mark Brownf1c0a022008-08-26 13:05:27 +01001710 /* power on device */
1711 wm8903_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1712
1713 /* Latch volume update bits */
Mark Brown8d50e442009-07-10 23:12:01 +01001714 val = snd_soc_read(codec, WM8903_ADC_DIGITAL_VOLUME_LEFT);
Mark Brownf1c0a022008-08-26 13:05:27 +01001715 val |= WM8903_ADCVU;
Mark Brown8d50e442009-07-10 23:12:01 +01001716 snd_soc_write(codec, WM8903_ADC_DIGITAL_VOLUME_LEFT, val);
1717 snd_soc_write(codec, WM8903_ADC_DIGITAL_VOLUME_RIGHT, val);
Mark Brownf1c0a022008-08-26 13:05:27 +01001718
Mark Brown8d50e442009-07-10 23:12:01 +01001719 val = snd_soc_read(codec, WM8903_DAC_DIGITAL_VOLUME_LEFT);
Mark Brownf1c0a022008-08-26 13:05:27 +01001720 val |= WM8903_DACVU;
Mark Brown8d50e442009-07-10 23:12:01 +01001721 snd_soc_write(codec, WM8903_DAC_DIGITAL_VOLUME_LEFT, val);
1722 snd_soc_write(codec, WM8903_DAC_DIGITAL_VOLUME_RIGHT, val);
Mark Brownf1c0a022008-08-26 13:05:27 +01001723
Mark Brown8d50e442009-07-10 23:12:01 +01001724 val = snd_soc_read(codec, WM8903_ANALOGUE_OUT1_LEFT);
Mark Brownf1c0a022008-08-26 13:05:27 +01001725 val |= WM8903_HPOUTVU;
Mark Brown8d50e442009-07-10 23:12:01 +01001726 snd_soc_write(codec, WM8903_ANALOGUE_OUT1_LEFT, val);
1727 snd_soc_write(codec, WM8903_ANALOGUE_OUT1_RIGHT, val);
Mark Brownf1c0a022008-08-26 13:05:27 +01001728
Mark Brown8d50e442009-07-10 23:12:01 +01001729 val = snd_soc_read(codec, WM8903_ANALOGUE_OUT2_LEFT);
Mark Brownf1c0a022008-08-26 13:05:27 +01001730 val |= WM8903_LINEOUTVU;
Mark Brown8d50e442009-07-10 23:12:01 +01001731 snd_soc_write(codec, WM8903_ANALOGUE_OUT2_LEFT, val);
1732 snd_soc_write(codec, WM8903_ANALOGUE_OUT2_RIGHT, val);
Mark Brownf1c0a022008-08-26 13:05:27 +01001733
Mark Brown8d50e442009-07-10 23:12:01 +01001734 val = snd_soc_read(codec, WM8903_ANALOGUE_OUT3_LEFT);
Mark Brownf1c0a022008-08-26 13:05:27 +01001735 val |= WM8903_SPKVU;
Mark Brown8d50e442009-07-10 23:12:01 +01001736 snd_soc_write(codec, WM8903_ANALOGUE_OUT3_LEFT, val);
1737 snd_soc_write(codec, WM8903_ANALOGUE_OUT3_RIGHT, val);
Mark Brownf1c0a022008-08-26 13:05:27 +01001738
1739 /* Enable DAC soft mute by default */
Mark Brown8d50e442009-07-10 23:12:01 +01001740 val = snd_soc_read(codec, WM8903_DAC_DIGITAL_1);
Mark Brownf1c0a022008-08-26 13:05:27 +01001741 val |= WM8903_DAC_MUTEMODE;
Mark Brown8d50e442009-07-10 23:12:01 +01001742 snd_soc_write(codec, WM8903_DAC_DIGITAL_1, val);
Mark Brownf1c0a022008-08-26 13:05:27 +01001743
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001744 snd_soc_add_controls(codec, wm8903_snd_controls,
1745 ARRAY_SIZE(wm8903_snd_controls));
1746 wm8903_add_widgets(codec);
Mark Brownf1c0a022008-08-26 13:05:27 +01001747
1748 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001749}
Mark Brownf1c0a022008-08-26 13:05:27 +01001750
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001751/* power down chip */
1752static int wm8903_remove(struct snd_soc_codec *codec)
1753{
1754 wm8903_set_bias_level(codec, SND_SOC_BIAS_OFF);
1755 return 0;
1756}
1757
1758static struct snd_soc_codec_driver soc_codec_dev_wm8903 = {
1759 .probe = wm8903_probe,
1760 .remove = wm8903_remove,
1761 .suspend = wm8903_suspend,
1762 .resume = wm8903_resume,
1763 .set_bias_level = wm8903_set_bias_level,
1764 .reg_cache_size = ARRAY_SIZE(wm8903_reg_defaults),
1765 .reg_word_size = sizeof(u16),
1766 .reg_cache_default = wm8903_reg_defaults,
1767 .volatile_register = wm8903_volatile_register,
1768};
1769
1770#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1771static __devinit int wm8903_i2c_probe(struct i2c_client *i2c,
1772 const struct i2c_device_id *id)
1773{
1774 struct wm8903_priv *wm8903;
1775 int ret;
1776
1777 wm8903 = kzalloc(sizeof(struct wm8903_priv), GFP_KERNEL);
1778 if (wm8903 == NULL)
1779 return -ENOMEM;
1780
1781 i2c_set_clientdata(i2c, wm8903);
1782 wm8903->control_data = i2c;
1783 wm8903->irq = i2c->irq;
1784
1785 ret = snd_soc_register_codec(&i2c->dev,
1786 &soc_codec_dev_wm8903, &wm8903_dai, 1);
1787 if (ret < 0)
1788 kfree(wm8903);
Mark Brownf1c0a022008-08-26 13:05:27 +01001789 return ret;
1790}
1791
Mark Brownc6f29812009-02-18 21:25:40 +00001792static __devexit int wm8903_i2c_remove(struct i2c_client *client)
Mark Brownf1c0a022008-08-26 13:05:27 +01001793{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001794 snd_soc_unregister_codec(&client->dev);
1795 kfree(i2c_get_clientdata(client));
Mark Brownf1c0a022008-08-26 13:05:27 +01001796 return 0;
1797}
1798
Mark Brownf1c0a022008-08-26 13:05:27 +01001799static const struct i2c_device_id wm8903_i2c_id[] = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001800 { "wm8903", 0 },
1801 { }
Mark Brownf1c0a022008-08-26 13:05:27 +01001802};
1803MODULE_DEVICE_TABLE(i2c, wm8903_i2c_id);
1804
1805static struct i2c_driver wm8903_i2c_driver = {
1806 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001807 .name = "wm8903-codec",
Mark Brownf1c0a022008-08-26 13:05:27 +01001808 .owner = THIS_MODULE,
1809 },
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001810 .probe = wm8903_i2c_probe,
1811 .remove = __devexit_p(wm8903_i2c_remove),
Mark Brownf1c0a022008-08-26 13:05:27 +01001812 .id_table = wm8903_i2c_id,
1813};
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001814#endif
Mark Brownf1c0a022008-08-26 13:05:27 +01001815
Takashi Iwaic9b3a402008-12-10 07:47:22 +01001816static int __init wm8903_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +00001817{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001818 int ret = 0;
1819#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1820 ret = i2c_add_driver(&wm8903_i2c_driver);
1821 if (ret != 0) {
1822 printk(KERN_ERR "Failed to register wm8903 I2C driver: %d\n",
1823 ret);
1824 }
1825#endif
1826 return ret;
Mark Brown64089b82008-12-08 19:17:58 +00001827}
1828module_init(wm8903_modinit);
1829
1830static void __exit wm8903_exit(void)
1831{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001832#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Mark Brownd58d5d52008-12-10 18:36:42 +00001833 i2c_del_driver(&wm8903_i2c_driver);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001834#endif
Mark Brown64089b82008-12-08 19:17:58 +00001835}
1836module_exit(wm8903_exit);
1837
Mark Brownf1c0a022008-08-26 13:05:27 +01001838MODULE_DESCRIPTION("ASoC WM8903 driver");
1839MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.cm>");
1840MODULE_LICENSE("GPL");