blob: 620793e5166608bdeffe4d5bad70f5cfbfee9dc9 [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>
Mark Brownf1c0a022008-08-26 13:05:27 +010032#include <sound/initval.h>
Mark Brown8abd16a2010-03-15 18:25:26 +000033#include <sound/wm8903.h>
Mark Brownf1c0a022008-08-26 13:05:27 +010034
35#include "wm8903.h"
36
Mark Brownf1c0a022008-08-26 13:05:27 +010037/* Register defaults at reset */
38static u16 wm8903_reg_defaults[] = {
39 0x8903, /* R0 - SW Reset and ID */
40 0x0000, /* R1 - Revision Number */
41 0x0000, /* R2 */
42 0x0000, /* R3 */
43 0x0018, /* R4 - Bias Control 0 */
44 0x0000, /* R5 - VMID Control 0 */
45 0x0000, /* R6 - Mic Bias Control 0 */
46 0x0000, /* R7 */
47 0x0001, /* R8 - Analogue DAC 0 */
48 0x0000, /* R9 */
49 0x0001, /* R10 - Analogue ADC 0 */
50 0x0000, /* R11 */
51 0x0000, /* R12 - Power Management 0 */
52 0x0000, /* R13 - Power Management 1 */
53 0x0000, /* R14 - Power Management 2 */
54 0x0000, /* R15 - Power Management 3 */
55 0x0000, /* R16 - Power Management 4 */
56 0x0000, /* R17 - Power Management 5 */
57 0x0000, /* R18 - Power Management 6 */
58 0x0000, /* R19 */
59 0x0400, /* R20 - Clock Rates 0 */
60 0x0D07, /* R21 - Clock Rates 1 */
61 0x0000, /* R22 - Clock Rates 2 */
62 0x0000, /* R23 */
63 0x0050, /* R24 - Audio Interface 0 */
64 0x0242, /* R25 - Audio Interface 1 */
65 0x0008, /* R26 - Audio Interface 2 */
66 0x0022, /* R27 - Audio Interface 3 */
67 0x0000, /* R28 */
68 0x0000, /* R29 */
69 0x00C0, /* R30 - DAC Digital Volume Left */
70 0x00C0, /* R31 - DAC Digital Volume Right */
71 0x0000, /* R32 - DAC Digital 0 */
72 0x0000, /* R33 - DAC Digital 1 */
73 0x0000, /* R34 */
74 0x0000, /* R35 */
75 0x00C0, /* R36 - ADC Digital Volume Left */
76 0x00C0, /* R37 - ADC Digital Volume Right */
77 0x0000, /* R38 - ADC Digital 0 */
78 0x0073, /* R39 - Digital Microphone 0 */
79 0x09BF, /* R40 - DRC 0 */
80 0x3241, /* R41 - DRC 1 */
81 0x0020, /* R42 - DRC 2 */
82 0x0000, /* R43 - DRC 3 */
83 0x0085, /* R44 - Analogue Left Input 0 */
84 0x0085, /* R45 - Analogue Right Input 0 */
85 0x0044, /* R46 - Analogue Left Input 1 */
86 0x0044, /* R47 - Analogue Right Input 1 */
87 0x0000, /* R48 */
88 0x0000, /* R49 */
89 0x0008, /* R50 - Analogue Left Mix 0 */
90 0x0004, /* R51 - Analogue Right Mix 0 */
91 0x0000, /* R52 - Analogue Spk Mix Left 0 */
92 0x0000, /* R53 - Analogue Spk Mix Left 1 */
93 0x0000, /* R54 - Analogue Spk Mix Right 0 */
94 0x0000, /* R55 - Analogue Spk Mix Right 1 */
95 0x0000, /* R56 */
96 0x002D, /* R57 - Analogue OUT1 Left */
97 0x002D, /* R58 - Analogue OUT1 Right */
98 0x0039, /* R59 - Analogue OUT2 Left */
99 0x0039, /* R60 - Analogue OUT2 Right */
100 0x0100, /* R61 */
101 0x0139, /* R62 - Analogue OUT3 Left */
102 0x0139, /* R63 - Analogue OUT3 Right */
103 0x0000, /* R64 */
104 0x0000, /* R65 - Analogue SPK Output Control 0 */
105 0x0000, /* R66 */
106 0x0010, /* R67 - DC Servo 0 */
107 0x0100, /* R68 */
108 0x00A4, /* R69 - DC Servo 2 */
109 0x0807, /* R70 */
110 0x0000, /* R71 */
111 0x0000, /* R72 */
112 0x0000, /* R73 */
113 0x0000, /* R74 */
114 0x0000, /* R75 */
115 0x0000, /* R76 */
116 0x0000, /* R77 */
117 0x0000, /* R78 */
118 0x000E, /* R79 */
119 0x0000, /* R80 */
120 0x0000, /* R81 */
121 0x0000, /* R82 */
122 0x0000, /* R83 */
123 0x0000, /* R84 */
124 0x0000, /* R85 */
125 0x0000, /* R86 */
126 0x0006, /* R87 */
127 0x0000, /* R88 */
128 0x0000, /* R89 */
129 0x0000, /* R90 - Analogue HP 0 */
130 0x0060, /* R91 */
131 0x0000, /* R92 */
132 0x0000, /* R93 */
133 0x0000, /* R94 - Analogue Lineout 0 */
134 0x0060, /* R95 */
135 0x0000, /* R96 */
136 0x0000, /* R97 */
137 0x0000, /* R98 - Charge Pump 0 */
138 0x1F25, /* R99 */
139 0x2B19, /* R100 */
140 0x01C0, /* R101 */
141 0x01EF, /* R102 */
142 0x2B00, /* R103 */
143 0x0000, /* R104 - Class W 0 */
144 0x01C0, /* R105 */
145 0x1C10, /* R106 */
146 0x0000, /* R107 */
147 0x0000, /* R108 - Write Sequencer 0 */
148 0x0000, /* R109 - Write Sequencer 1 */
149 0x0000, /* R110 - Write Sequencer 2 */
150 0x0000, /* R111 - Write Sequencer 3 */
151 0x0000, /* R112 - Write Sequencer 4 */
152 0x0000, /* R113 */
153 0x0000, /* R114 - Control Interface */
154 0x0000, /* R115 */
155 0x00A8, /* R116 - GPIO Control 1 */
156 0x00A8, /* R117 - GPIO Control 2 */
157 0x00A8, /* R118 - GPIO Control 3 */
158 0x0220, /* R119 - GPIO Control 4 */
159 0x01A0, /* R120 - GPIO Control 5 */
160 0x0000, /* R121 - Interrupt Status 1 */
161 0xFFFF, /* R122 - Interrupt Status 1 Mask */
162 0x0000, /* R123 - Interrupt Polarity 1 */
163 0x0000, /* R124 */
164 0x0003, /* R125 */
165 0x0000, /* R126 - Interrupt Control */
166 0x0000, /* R127 */
167 0x0005, /* R128 */
168 0x0000, /* R129 - Control Interface Test 1 */
169 0x0000, /* R130 */
170 0x0000, /* R131 */
171 0x0000, /* R132 */
172 0x0000, /* R133 */
173 0x0000, /* R134 */
174 0x03FF, /* R135 */
175 0x0007, /* R136 */
176 0x0040, /* R137 */
177 0x0000, /* R138 */
178 0x0000, /* R139 */
179 0x0000, /* R140 */
180 0x0000, /* R141 */
181 0x0000, /* R142 */
182 0x0000, /* R143 */
183 0x0000, /* R144 */
184 0x0000, /* R145 */
185 0x0000, /* R146 */
186 0x0000, /* R147 */
187 0x4000, /* R148 */
188 0x6810, /* R149 - Charge Pump Test 1 */
189 0x0004, /* R150 */
190 0x0000, /* R151 */
191 0x0000, /* R152 */
192 0x0000, /* R153 */
193 0x0000, /* R154 */
194 0x0000, /* R155 */
195 0x0000, /* R156 */
196 0x0000, /* R157 */
197 0x0000, /* R158 */
198 0x0000, /* R159 */
199 0x0000, /* R160 */
200 0x0000, /* R161 */
201 0x0000, /* R162 */
202 0x0000, /* R163 */
203 0x0028, /* R164 - Clock Rate Test 4 */
204 0x0004, /* R165 */
205 0x0000, /* R166 */
206 0x0060, /* R167 */
207 0x0000, /* R168 */
208 0x0000, /* R169 */
209 0x0000, /* R170 */
210 0x0000, /* R171 */
211 0x0000, /* R172 - Analogue Output Bias 0 */
212};
213
Mark Brownd58d5d52008-12-10 18:36:42 +0000214struct wm8903_priv {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000215
Mark Brownd58d5d52008-12-10 18:36:42 +0000216 u16 reg_cache[ARRAY_SIZE(wm8903_reg_defaults)];
217
218 int sysclk;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000219 int irq;
Mark Brownd58d5d52008-12-10 18:36:42 +0000220
221 /* Reference counts */
Mark Brownd58d5d52008-12-10 18:36:42 +0000222 int class_w_users;
223 int playback_active;
224 int capture_active;
225
Mark Brown8abd16a2010-03-15 18:25:26 +0000226 struct completion wseq;
227
Mark Brown72453872010-03-15 21:22:58 +0000228 struct snd_soc_jack *mic_jack;
229 int mic_det;
230 int mic_short;
231 int mic_last_report;
232 int mic_delay;
233
Mark Brownd58d5d52008-12-10 18:36:42 +0000234 struct snd_pcm_substream *master_substream;
235 struct snd_pcm_substream *slave_substream;
236};
237
Mark Brown8d50e442009-07-10 23:12:01 +0100238static int wm8903_volatile_register(unsigned int reg)
Mark Brownf1c0a022008-08-26 13:05:27 +0100239{
240 switch (reg) {
241 case WM8903_SW_RESET_AND_ID:
242 case WM8903_REVISION_NUMBER:
243 case WM8903_INTERRUPT_STATUS_1:
244 case WM8903_WRITE_SEQUENCER_4:
Mark Brown8d50e442009-07-10 23:12:01 +0100245 return 1;
Mark Brownf1c0a022008-08-26 13:05:27 +0100246
247 default:
Mark Brownf1c0a022008-08-26 13:05:27 +0100248 return 0;
Mark Brown8d50e442009-07-10 23:12:01 +0100249 }
Mark Brownf1c0a022008-08-26 13:05:27 +0100250}
251
252static int wm8903_run_sequence(struct snd_soc_codec *codec, unsigned int start)
253{
254 u16 reg[5];
Mark Brownb2c812e2010-04-14 15:35:19 +0900255 struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
Mark Brownf1c0a022008-08-26 13:05:27 +0100256
257 BUG_ON(start > 48);
258
Mark Brown37f88e82010-03-15 18:14:34 +0000259 /* Enable the sequencer if it's not already on */
Mark Brown8d50e442009-07-10 23:12:01 +0100260 reg[0] = snd_soc_read(codec, WM8903_WRITE_SEQUENCER_0);
Mark Brown37f88e82010-03-15 18:14:34 +0000261 snd_soc_write(codec, WM8903_WRITE_SEQUENCER_0,
262 reg[0] | WM8903_WSEQ_ENA);
Mark Brownf1c0a022008-08-26 13:05:27 +0100263
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000264 dev_dbg(codec->dev, "Starting sequence at %d\n", start);
Mark Brownf1c0a022008-08-26 13:05:27 +0100265
Mark Brown8d50e442009-07-10 23:12:01 +0100266 snd_soc_write(codec, WM8903_WRITE_SEQUENCER_3,
Mark Brownf1c0a022008-08-26 13:05:27 +0100267 start | WM8903_WSEQ_START);
268
269 /* Wait for it to complete. If we have the interrupt wired up then
Mark Brown8abd16a2010-03-15 18:25:26 +0000270 * that will break us out of the poll early.
Mark Brownf1c0a022008-08-26 13:05:27 +0100271 */
272 do {
Mark Brown8abd16a2010-03-15 18:25:26 +0000273 wait_for_completion_timeout(&wm8903->wseq,
274 msecs_to_jiffies(10));
Mark Brownf1c0a022008-08-26 13:05:27 +0100275
Mark Brown8d50e442009-07-10 23:12:01 +0100276 reg[4] = snd_soc_read(codec, WM8903_WRITE_SEQUENCER_4);
Mark Brownf1c0a022008-08-26 13:05:27 +0100277 } while (reg[4] & WM8903_WSEQ_BUSY);
278
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000279 dev_dbg(codec->dev, "Sequence complete\n");
Mark Brownf1c0a022008-08-26 13:05:27 +0100280
Mark Brown37f88e82010-03-15 18:14:34 +0000281 /* Disable the sequencer again if we enabled it */
282 snd_soc_write(codec, WM8903_WRITE_SEQUENCER_0, reg[0]);
Mark Brownf1c0a022008-08-26 13:05:27 +0100283
284 return 0;
285}
286
287static void wm8903_sync_reg_cache(struct snd_soc_codec *codec, u16 *cache)
288{
289 int i;
290
291 /* There really ought to be something better we can do here :/ */
292 for (i = 0; i < ARRAY_SIZE(wm8903_reg_defaults); i++)
Mark Brown8d50e442009-07-10 23:12:01 +0100293 cache[i] = codec->hw_read(codec, i);
Mark Brownf1c0a022008-08-26 13:05:27 +0100294}
295
296static void wm8903_reset(struct snd_soc_codec *codec)
297{
Mark Brown8d50e442009-07-10 23:12:01 +0100298 snd_soc_write(codec, WM8903_SW_RESET_AND_ID, 0);
Mark Brownd58d5d52008-12-10 18:36:42 +0000299 memcpy(codec->reg_cache, wm8903_reg_defaults,
300 sizeof(wm8903_reg_defaults));
Mark Brownf1c0a022008-08-26 13:05:27 +0100301}
302
303#define WM8903_OUTPUT_SHORT 0x8
304#define WM8903_OUTPUT_OUT 0x4
305#define WM8903_OUTPUT_INT 0x2
306#define WM8903_OUTPUT_IN 0x1
307
Mark Brown42768a12009-04-22 18:39:39 +0100308static int wm8903_cp_event(struct snd_soc_dapm_widget *w,
309 struct snd_kcontrol *kcontrol, int event)
310{
311 WARN_ON(event != SND_SOC_DAPM_POST_PMU);
312 mdelay(4);
313
314 return 0;
315}
316
Mark Brownf1c0a022008-08-26 13:05:27 +0100317/*
318 * Event for headphone and line out amplifier power changes. Special
319 * power up/down sequences are required in order to maximise pop/click
320 * performance.
321 */
322static int wm8903_output_event(struct snd_soc_dapm_widget *w,
323 struct snd_kcontrol *kcontrol, int event)
324{
325 struct snd_soc_codec *codec = w->codec;
Mark Brownf1c0a022008-08-26 13:05:27 +0100326 u16 val;
Takashi Iwai0bc286e2008-12-01 19:59:35 +0100327 u16 reg;
Mark Brownd7d5c542009-04-22 21:03:50 +0100328 u16 dcs_reg;
329 u16 dcs_bit;
Takashi Iwai0bc286e2008-12-01 19:59:35 +0100330 int shift;
Mark Brownf1c0a022008-08-26 13:05:27 +0100331
332 switch (w->reg) {
333 case WM8903_POWER_MANAGEMENT_2:
334 reg = WM8903_ANALOGUE_HP_0;
Mark Brownd7d5c542009-04-22 21:03:50 +0100335 dcs_bit = 0 + w->shift;
Mark Brownf1c0a022008-08-26 13:05:27 +0100336 break;
337 case WM8903_POWER_MANAGEMENT_3:
338 reg = WM8903_ANALOGUE_LINEOUT_0;
Mark Brownd7d5c542009-04-22 21:03:50 +0100339 dcs_bit = 2 + w->shift;
Mark Brownf1c0a022008-08-26 13:05:27 +0100340 break;
341 default:
342 BUG();
Mark Brown1e297a12008-12-10 11:08:33 +0000343 return -EINVAL; /* Spurious warning from some compilers */
Mark Brownf1c0a022008-08-26 13:05:27 +0100344 }
345
346 switch (w->shift) {
347 case 0:
348 shift = 0;
349 break;
350 case 1:
351 shift = 4;
352 break;
353 default:
354 BUG();
Mark Brown1e297a12008-12-10 11:08:33 +0000355 return -EINVAL; /* Spurious warning from some compilers */
Mark Brownf1c0a022008-08-26 13:05:27 +0100356 }
357
358 if (event & SND_SOC_DAPM_PRE_PMU) {
Mark Brown8d50e442009-07-10 23:12:01 +0100359 val = snd_soc_read(codec, reg);
Mark Brownf1c0a022008-08-26 13:05:27 +0100360
361 /* Short the output */
362 val &= ~(WM8903_OUTPUT_SHORT << shift);
Mark Brown8d50e442009-07-10 23:12:01 +0100363 snd_soc_write(codec, reg, val);
Mark Brownf1c0a022008-08-26 13:05:27 +0100364 }
365
366 if (event & SND_SOC_DAPM_POST_PMU) {
Mark Brown8d50e442009-07-10 23:12:01 +0100367 val = snd_soc_read(codec, reg);
Mark Brownf1c0a022008-08-26 13:05:27 +0100368
369 val |= (WM8903_OUTPUT_IN << shift);
Mark Brown8d50e442009-07-10 23:12:01 +0100370 snd_soc_write(codec, reg, val);
Mark Brownf1c0a022008-08-26 13:05:27 +0100371
372 val |= (WM8903_OUTPUT_INT << shift);
Mark Brown8d50e442009-07-10 23:12:01 +0100373 snd_soc_write(codec, reg, val);
Mark Brownf1c0a022008-08-26 13:05:27 +0100374
375 /* Turn on the output ENA_OUTP */
376 val |= (WM8903_OUTPUT_OUT << shift);
Mark Brown8d50e442009-07-10 23:12:01 +0100377 snd_soc_write(codec, reg, val);
Mark Brownf1c0a022008-08-26 13:05:27 +0100378
Mark Brownd7d5c542009-04-22 21:03:50 +0100379 /* Enable the DC servo */
Mark Brown8d50e442009-07-10 23:12:01 +0100380 dcs_reg = snd_soc_read(codec, WM8903_DC_SERVO_0);
Mark Brownd7d5c542009-04-22 21:03:50 +0100381 dcs_reg |= dcs_bit;
Mark Brown8d50e442009-07-10 23:12:01 +0100382 snd_soc_write(codec, WM8903_DC_SERVO_0, dcs_reg);
Mark Brownd7d5c542009-04-22 21:03:50 +0100383
Mark Brownf1c0a022008-08-26 13:05:27 +0100384 /* Remove the short */
385 val |= (WM8903_OUTPUT_SHORT << shift);
Mark Brown8d50e442009-07-10 23:12:01 +0100386 snd_soc_write(codec, reg, val);
Mark Brownf1c0a022008-08-26 13:05:27 +0100387 }
388
389 if (event & SND_SOC_DAPM_PRE_PMD) {
Mark Brown8d50e442009-07-10 23:12:01 +0100390 val = snd_soc_read(codec, reg);
Mark Brownf1c0a022008-08-26 13:05:27 +0100391
392 /* Short the output */
393 val &= ~(WM8903_OUTPUT_SHORT << shift);
Mark Brown8d50e442009-07-10 23:12:01 +0100394 snd_soc_write(codec, reg, val);
Mark Brownf1c0a022008-08-26 13:05:27 +0100395
Mark Brownd7d5c542009-04-22 21:03:50 +0100396 /* Disable the DC servo */
Mark Brown8d50e442009-07-10 23:12:01 +0100397 dcs_reg = snd_soc_read(codec, WM8903_DC_SERVO_0);
Mark Brownd7d5c542009-04-22 21:03:50 +0100398 dcs_reg &= ~dcs_bit;
Mark Brown8d50e442009-07-10 23:12:01 +0100399 snd_soc_write(codec, WM8903_DC_SERVO_0, dcs_reg);
Mark Brownd7d5c542009-04-22 21:03:50 +0100400
Mark Brownf1c0a022008-08-26 13:05:27 +0100401 /* Then disable the intermediate and output stages */
402 val &= ~((WM8903_OUTPUT_OUT | WM8903_OUTPUT_INT |
403 WM8903_OUTPUT_IN) << shift);
Mark Brown8d50e442009-07-10 23:12:01 +0100404 snd_soc_write(codec, reg, val);
Mark Brownf1c0a022008-08-26 13:05:27 +0100405 }
406
Mark Brownf1c0a022008-08-26 13:05:27 +0100407 return 0;
408}
409
410/*
411 * When used with DAC outputs only the WM8903 charge pump supports
412 * operation in class W mode, providing very low power consumption
413 * when used with digital sources. Enable and disable this mode
414 * automatically depending on the mixer configuration.
415 *
416 * All the relevant controls are simple switches.
417 */
418static int wm8903_class_w_put(struct snd_kcontrol *kcontrol,
419 struct snd_ctl_elem_value *ucontrol)
420{
421 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
422 struct snd_soc_codec *codec = widget->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900423 struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
Mark Brownf1c0a022008-08-26 13:05:27 +0100424 u16 reg;
425 int ret;
426
Mark Brown8d50e442009-07-10 23:12:01 +0100427 reg = snd_soc_read(codec, WM8903_CLASS_W_0);
Mark Brownf1c0a022008-08-26 13:05:27 +0100428
429 /* Turn it off if we're about to enable bypass */
430 if (ucontrol->value.integer.value[0]) {
431 if (wm8903->class_w_users == 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000432 dev_dbg(codec->dev, "Disabling Class W\n");
Mark Brown8d50e442009-07-10 23:12:01 +0100433 snd_soc_write(codec, WM8903_CLASS_W_0, reg &
Mark Brownf1c0a022008-08-26 13:05:27 +0100434 ~(WM8903_CP_DYN_FREQ | WM8903_CP_DYN_V));
435 }
436 wm8903->class_w_users++;
437 }
438
439 /* Implement the change */
440 ret = snd_soc_dapm_put_volsw(kcontrol, ucontrol);
441
442 /* If we've just disabled the last bypass path turn Class W on */
443 if (!ucontrol->value.integer.value[0]) {
444 if (wm8903->class_w_users == 1) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000445 dev_dbg(codec->dev, "Enabling Class W\n");
Mark Brown8d50e442009-07-10 23:12:01 +0100446 snd_soc_write(codec, WM8903_CLASS_W_0, reg |
Mark Brownf1c0a022008-08-26 13:05:27 +0100447 WM8903_CP_DYN_FREQ | WM8903_CP_DYN_V);
448 }
449 wm8903->class_w_users--;
450 }
451
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000452 dev_dbg(codec->dev, "Bypass use count now %d\n",
Mark Brownf1c0a022008-08-26 13:05:27 +0100453 wm8903->class_w_users);
454
455 return ret;
456}
457
458#define SOC_DAPM_SINGLE_W(xname, reg, shift, max, invert) \
459{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
460 .info = snd_soc_info_volsw, \
461 .get = snd_soc_dapm_get_volsw, .put = wm8903_class_w_put, \
462 .private_value = SOC_SINGLE_VALUE(reg, shift, max, invert) }
463
464
465/* ALSA can only do steps of .01dB */
466static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
467
Mark Brown291ce182009-04-22 21:36:14 +0100468static const DECLARE_TLV_DB_SCALE(digital_sidetone_tlv, -3600, 300, 0);
Mark Brownf1c0a022008-08-26 13:05:27 +0100469static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
470
471static const DECLARE_TLV_DB_SCALE(drc_tlv_thresh, 0, 75, 0);
472static const DECLARE_TLV_DB_SCALE(drc_tlv_amp, -2250, 75, 0);
473static const DECLARE_TLV_DB_SCALE(drc_tlv_min, 0, 600, 0);
474static const DECLARE_TLV_DB_SCALE(drc_tlv_max, 1200, 600, 0);
475static const DECLARE_TLV_DB_SCALE(drc_tlv_startup, -300, 50, 0);
476
477static const char *drc_slope_text[] = {
478 "1", "1/2", "1/4", "1/8", "1/16", "0"
479};
480
481static const struct soc_enum drc_slope_r0 =
482 SOC_ENUM_SINGLE(WM8903_DRC_2, 3, 6, drc_slope_text);
483
484static const struct soc_enum drc_slope_r1 =
485 SOC_ENUM_SINGLE(WM8903_DRC_2, 0, 6, drc_slope_text);
486
487static const char *drc_attack_text[] = {
488 "instantaneous",
489 "363us", "762us", "1.45ms", "2.9ms", "5.8ms", "11.6ms", "23.2ms",
490 "46.4ms", "92.8ms", "185.6ms"
491};
492
493static const struct soc_enum drc_attack =
494 SOC_ENUM_SINGLE(WM8903_DRC_1, 12, 11, drc_attack_text);
495
496static const char *drc_decay_text[] = {
497 "186ms", "372ms", "743ms", "1.49s", "2.97s", "5.94s", "11.89s",
498 "23.87s", "47.56s"
499};
500
501static const struct soc_enum drc_decay =
502 SOC_ENUM_SINGLE(WM8903_DRC_1, 8, 9, drc_decay_text);
503
504static const char *drc_ff_delay_text[] = {
505 "5 samples", "9 samples"
506};
507
508static const struct soc_enum drc_ff_delay =
509 SOC_ENUM_SINGLE(WM8903_DRC_0, 5, 2, drc_ff_delay_text);
510
511static const char *drc_qr_decay_text[] = {
512 "0.725ms", "1.45ms", "5.8ms"
513};
514
515static const struct soc_enum drc_qr_decay =
516 SOC_ENUM_SINGLE(WM8903_DRC_1, 4, 3, drc_qr_decay_text);
517
518static const char *drc_smoothing_text[] = {
519 "Low", "Medium", "High"
520};
521
522static const struct soc_enum drc_smoothing =
523 SOC_ENUM_SINGLE(WM8903_DRC_0, 11, 3, drc_smoothing_text);
524
525static const char *soft_mute_text[] = {
526 "Fast (fs/2)", "Slow (fs/32)"
527};
528
529static const struct soc_enum soft_mute =
530 SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_1, 10, 2, soft_mute_text);
531
532static const char *mute_mode_text[] = {
533 "Hard", "Soft"
534};
535
536static const struct soc_enum mute_mode =
537 SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_1, 9, 2, mute_mode_text);
538
539static const char *dac_deemphasis_text[] = {
540 "Disabled", "32kHz", "44.1kHz", "48kHz"
541};
542
543static const struct soc_enum dac_deemphasis =
544 SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_1, 1, 4, dac_deemphasis_text);
545
546static const char *companding_text[] = {
547 "ulaw", "alaw"
548};
549
550static const struct soc_enum dac_companding =
551 SOC_ENUM_SINGLE(WM8903_AUDIO_INTERFACE_0, 0, 2, companding_text);
552
553static const struct soc_enum adc_companding =
554 SOC_ENUM_SINGLE(WM8903_AUDIO_INTERFACE_0, 2, 2, companding_text);
555
556static const char *input_mode_text[] = {
557 "Single-Ended", "Differential Line", "Differential Mic"
558};
559
560static const struct soc_enum linput_mode_enum =
561 SOC_ENUM_SINGLE(WM8903_ANALOGUE_LEFT_INPUT_1, 0, 3, input_mode_text);
562
563static const struct soc_enum rinput_mode_enum =
564 SOC_ENUM_SINGLE(WM8903_ANALOGUE_RIGHT_INPUT_1, 0, 3, input_mode_text);
565
566static const char *linput_mux_text[] = {
567 "IN1L", "IN2L", "IN3L"
568};
569
570static const struct soc_enum linput_enum =
571 SOC_ENUM_SINGLE(WM8903_ANALOGUE_LEFT_INPUT_1, 2, 3, linput_mux_text);
572
573static const struct soc_enum linput_inv_enum =
574 SOC_ENUM_SINGLE(WM8903_ANALOGUE_LEFT_INPUT_1, 4, 3, linput_mux_text);
575
576static const char *rinput_mux_text[] = {
577 "IN1R", "IN2R", "IN3R"
578};
579
580static const struct soc_enum rinput_enum =
581 SOC_ENUM_SINGLE(WM8903_ANALOGUE_RIGHT_INPUT_1, 2, 3, rinput_mux_text);
582
583static const struct soc_enum rinput_inv_enum =
584 SOC_ENUM_SINGLE(WM8903_ANALOGUE_RIGHT_INPUT_1, 4, 3, rinput_mux_text);
585
586
Mark Brown291ce182009-04-22 21:36:14 +0100587static const char *sidetone_text[] = {
588 "None", "Left", "Right"
589};
590
591static const struct soc_enum lsidetone_enum =
592 SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_0, 2, 3, sidetone_text);
593
594static const struct soc_enum rsidetone_enum =
595 SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_0, 0, 3, sidetone_text);
596
Mark Brownf1c0a022008-08-26 13:05:27 +0100597static const struct snd_kcontrol_new wm8903_snd_controls[] = {
598
599/* Input PGAs - No TLV since the scale depends on PGA mode */
600SOC_SINGLE("Left Input PGA Switch", WM8903_ANALOGUE_LEFT_INPUT_0,
Mark Brown57159522008-09-24 10:47:02 +0100601 7, 1, 1),
Mark Brownf1c0a022008-08-26 13:05:27 +0100602SOC_SINGLE("Left Input PGA Volume", WM8903_ANALOGUE_LEFT_INPUT_0,
603 0, 31, 0),
604SOC_SINGLE("Left Input PGA Common Mode Switch", WM8903_ANALOGUE_LEFT_INPUT_1,
605 6, 1, 0),
606
607SOC_SINGLE("Right Input PGA Switch", WM8903_ANALOGUE_RIGHT_INPUT_0,
Mark Brown57159522008-09-24 10:47:02 +0100608 7, 1, 1),
Mark Brownf1c0a022008-08-26 13:05:27 +0100609SOC_SINGLE("Right Input PGA Volume", WM8903_ANALOGUE_RIGHT_INPUT_0,
610 0, 31, 0),
611SOC_SINGLE("Right Input PGA Common Mode Switch", WM8903_ANALOGUE_RIGHT_INPUT_1,
612 6, 1, 0),
613
614/* ADCs */
615SOC_SINGLE("DRC Switch", WM8903_DRC_0, 15, 1, 0),
616SOC_ENUM("DRC Compressor Slope R0", drc_slope_r0),
617SOC_ENUM("DRC Compressor Slope R1", drc_slope_r1),
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200618SOC_SINGLE_TLV("DRC Compressor Threshold Volume", WM8903_DRC_3, 5, 124, 1,
Mark Brownf1c0a022008-08-26 13:05:27 +0100619 drc_tlv_thresh),
620SOC_SINGLE_TLV("DRC Volume", WM8903_DRC_3, 0, 30, 1, drc_tlv_amp),
621SOC_SINGLE_TLV("DRC Minimum Gain Volume", WM8903_DRC_1, 2, 3, 1, drc_tlv_min),
622SOC_SINGLE_TLV("DRC Maximum Gain Volume", WM8903_DRC_1, 0, 3, 0, drc_tlv_max),
623SOC_ENUM("DRC Attack Rate", drc_attack),
624SOC_ENUM("DRC Decay Rate", drc_decay),
625SOC_ENUM("DRC FF Delay", drc_ff_delay),
626SOC_SINGLE("DRC Anticlip Switch", WM8903_DRC_0, 1, 1, 0),
627SOC_SINGLE("DRC QR Switch", WM8903_DRC_0, 2, 1, 0),
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200628SOC_SINGLE_TLV("DRC QR Threshold Volume", WM8903_DRC_0, 6, 3, 0, drc_tlv_max),
Mark Brownf1c0a022008-08-26 13:05:27 +0100629SOC_ENUM("DRC QR Decay Rate", drc_qr_decay),
630SOC_SINGLE("DRC Smoothing Switch", WM8903_DRC_0, 3, 1, 0),
631SOC_SINGLE("DRC Smoothing Hysteresis Switch", WM8903_DRC_0, 0, 1, 0),
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200632SOC_ENUM("DRC Smoothing Threshold", drc_smoothing),
Mark Brownf1c0a022008-08-26 13:05:27 +0100633SOC_SINGLE_TLV("DRC Startup Volume", WM8903_DRC_0, 6, 18, 0, drc_tlv_startup),
634
635SOC_DOUBLE_R_TLV("Digital Capture Volume", WM8903_ADC_DIGITAL_VOLUME_LEFT,
636 WM8903_ADC_DIGITAL_VOLUME_RIGHT, 1, 96, 0, digital_tlv),
637SOC_ENUM("ADC Companding Mode", adc_companding),
638SOC_SINGLE("ADC Companding Switch", WM8903_AUDIO_INTERFACE_0, 3, 1, 0),
639
Mark Brown291ce182009-04-22 21:36:14 +0100640SOC_DOUBLE_TLV("Digital Sidetone Volume", WM8903_DAC_DIGITAL_0, 4, 8,
641 12, 0, digital_sidetone_tlv),
642
Mark Brownf1c0a022008-08-26 13:05:27 +0100643/* DAC */
644SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8903_DAC_DIGITAL_VOLUME_LEFT,
645 WM8903_DAC_DIGITAL_VOLUME_RIGHT, 1, 120, 0, digital_tlv),
646SOC_ENUM("DAC Soft Mute Rate", soft_mute),
647SOC_ENUM("DAC Mute Mode", mute_mode),
648SOC_SINGLE("DAC Mono Switch", WM8903_DAC_DIGITAL_1, 12, 1, 0),
649SOC_ENUM("DAC De-emphasis", dac_deemphasis),
Mark Brownf1c0a022008-08-26 13:05:27 +0100650SOC_ENUM("DAC Companding Mode", dac_companding),
651SOC_SINGLE("DAC Companding Switch", WM8903_AUDIO_INTERFACE_0, 1, 1, 0),
652
653/* Headphones */
654SOC_DOUBLE_R("Headphone Switch",
655 WM8903_ANALOGUE_OUT1_LEFT, WM8903_ANALOGUE_OUT1_RIGHT,
656 8, 1, 1),
657SOC_DOUBLE_R("Headphone ZC Switch",
658 WM8903_ANALOGUE_OUT1_LEFT, WM8903_ANALOGUE_OUT1_RIGHT,
659 6, 1, 0),
660SOC_DOUBLE_R_TLV("Headphone Volume",
661 WM8903_ANALOGUE_OUT1_LEFT, WM8903_ANALOGUE_OUT1_RIGHT,
662 0, 63, 0, out_tlv),
663
664/* Line out */
665SOC_DOUBLE_R("Line Out Switch",
666 WM8903_ANALOGUE_OUT2_LEFT, WM8903_ANALOGUE_OUT2_RIGHT,
667 8, 1, 1),
668SOC_DOUBLE_R("Line Out ZC Switch",
669 WM8903_ANALOGUE_OUT2_LEFT, WM8903_ANALOGUE_OUT2_RIGHT,
670 6, 1, 0),
671SOC_DOUBLE_R_TLV("Line Out Volume",
672 WM8903_ANALOGUE_OUT2_LEFT, WM8903_ANALOGUE_OUT2_RIGHT,
673 0, 63, 0, out_tlv),
674
675/* Speaker */
676SOC_DOUBLE_R("Speaker Switch",
677 WM8903_ANALOGUE_OUT3_LEFT, WM8903_ANALOGUE_OUT3_RIGHT, 8, 1, 1),
678SOC_DOUBLE_R("Speaker ZC Switch",
679 WM8903_ANALOGUE_OUT3_LEFT, WM8903_ANALOGUE_OUT3_RIGHT, 6, 1, 0),
680SOC_DOUBLE_R_TLV("Speaker Volume",
681 WM8903_ANALOGUE_OUT3_LEFT, WM8903_ANALOGUE_OUT3_RIGHT,
682 0, 63, 0, out_tlv),
683};
684
Mark Brownf1c0a022008-08-26 13:05:27 +0100685static const struct snd_kcontrol_new linput_mode_mux =
686 SOC_DAPM_ENUM("Left Input Mode Mux", linput_mode_enum);
687
688static const struct snd_kcontrol_new rinput_mode_mux =
689 SOC_DAPM_ENUM("Right Input Mode Mux", rinput_mode_enum);
690
691static const struct snd_kcontrol_new linput_mux =
692 SOC_DAPM_ENUM("Left Input Mux", linput_enum);
693
694static const struct snd_kcontrol_new linput_inv_mux =
695 SOC_DAPM_ENUM("Left Inverting Input Mux", linput_inv_enum);
696
697static const struct snd_kcontrol_new rinput_mux =
698 SOC_DAPM_ENUM("Right Input Mux", rinput_enum);
699
700static const struct snd_kcontrol_new rinput_inv_mux =
701 SOC_DAPM_ENUM("Right Inverting Input Mux", rinput_inv_enum);
702
Mark Brown291ce182009-04-22 21:36:14 +0100703static const struct snd_kcontrol_new lsidetone_mux =
704 SOC_DAPM_ENUM("DACL Sidetone Mux", lsidetone_enum);
705
706static const struct snd_kcontrol_new rsidetone_mux =
707 SOC_DAPM_ENUM("DACR Sidetone Mux", rsidetone_enum);
708
Mark Brownf1c0a022008-08-26 13:05:27 +0100709static const struct snd_kcontrol_new left_output_mixer[] = {
710SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_LEFT_MIX_0, 3, 1, 0),
711SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_LEFT_MIX_0, 2, 1, 0),
712SOC_DAPM_SINGLE_W("Left Bypass Switch", WM8903_ANALOGUE_LEFT_MIX_0, 1, 1, 0),
Mark Brown4b4fffd2008-12-03 11:21:08 +0000713SOC_DAPM_SINGLE_W("Right Bypass Switch", WM8903_ANALOGUE_LEFT_MIX_0, 0, 1, 0),
Mark Brownf1c0a022008-08-26 13:05:27 +0100714};
715
716static const struct snd_kcontrol_new right_output_mixer[] = {
717SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 3, 1, 0),
718SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 2, 1, 0),
719SOC_DAPM_SINGLE_W("Left Bypass Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 1, 1, 0),
Mark Brown4b4fffd2008-12-03 11:21:08 +0000720SOC_DAPM_SINGLE_W("Right Bypass Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 0, 1, 0),
Mark Brownf1c0a022008-08-26 13:05:27 +0100721};
722
723static const struct snd_kcontrol_new left_speaker_mixer[] = {
724SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0, 3, 1, 0),
725SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0, 2, 1, 0),
726SOC_DAPM_SINGLE("Left Bypass Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0, 1, 1, 0),
727SOC_DAPM_SINGLE("Right Bypass Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0,
Mark Brown4b4fffd2008-12-03 11:21:08 +0000728 0, 1, 0),
Mark Brownf1c0a022008-08-26 13:05:27 +0100729};
730
731static const struct snd_kcontrol_new right_speaker_mixer[] = {
732SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0, 3, 1, 0),
733SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0, 2, 1, 0),
734SOC_DAPM_SINGLE("Left Bypass Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0,
735 1, 1, 0),
736SOC_DAPM_SINGLE("Right Bypass Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0,
Mark Brown4b4fffd2008-12-03 11:21:08 +0000737 0, 1, 0),
Mark Brownf1c0a022008-08-26 13:05:27 +0100738};
739
740static const struct snd_soc_dapm_widget wm8903_dapm_widgets[] = {
741SND_SOC_DAPM_INPUT("IN1L"),
742SND_SOC_DAPM_INPUT("IN1R"),
743SND_SOC_DAPM_INPUT("IN2L"),
744SND_SOC_DAPM_INPUT("IN2R"),
745SND_SOC_DAPM_INPUT("IN3L"),
746SND_SOC_DAPM_INPUT("IN3R"),
747
748SND_SOC_DAPM_OUTPUT("HPOUTL"),
749SND_SOC_DAPM_OUTPUT("HPOUTR"),
750SND_SOC_DAPM_OUTPUT("LINEOUTL"),
751SND_SOC_DAPM_OUTPUT("LINEOUTR"),
752SND_SOC_DAPM_OUTPUT("LOP"),
753SND_SOC_DAPM_OUTPUT("LON"),
754SND_SOC_DAPM_OUTPUT("ROP"),
755SND_SOC_DAPM_OUTPUT("RON"),
756
757SND_SOC_DAPM_MICBIAS("Mic Bias", WM8903_MIC_BIAS_CONTROL_0, 0, 0),
758
759SND_SOC_DAPM_MUX("Left Input Mux", SND_SOC_NOPM, 0, 0, &linput_mux),
760SND_SOC_DAPM_MUX("Left Input Inverting Mux", SND_SOC_NOPM, 0, 0,
761 &linput_inv_mux),
762SND_SOC_DAPM_MUX("Left Input Mode Mux", SND_SOC_NOPM, 0, 0, &linput_mode_mux),
763
764SND_SOC_DAPM_MUX("Right Input Mux", SND_SOC_NOPM, 0, 0, &rinput_mux),
765SND_SOC_DAPM_MUX("Right Input Inverting Mux", SND_SOC_NOPM, 0, 0,
766 &rinput_inv_mux),
767SND_SOC_DAPM_MUX("Right Input Mode Mux", SND_SOC_NOPM, 0, 0, &rinput_mode_mux),
768
769SND_SOC_DAPM_PGA("Left Input PGA", WM8903_POWER_MANAGEMENT_0, 1, 0, NULL, 0),
770SND_SOC_DAPM_PGA("Right Input PGA", WM8903_POWER_MANAGEMENT_0, 0, 0, NULL, 0),
771
772SND_SOC_DAPM_ADC("ADCL", "Left HiFi Capture", WM8903_POWER_MANAGEMENT_6, 1, 0),
773SND_SOC_DAPM_ADC("ADCR", "Right HiFi Capture", WM8903_POWER_MANAGEMENT_6, 0, 0),
774
Mark Brown291ce182009-04-22 21:36:14 +0100775SND_SOC_DAPM_MUX("DACL Sidetone", SND_SOC_NOPM, 0, 0, &lsidetone_mux),
776SND_SOC_DAPM_MUX("DACR Sidetone", SND_SOC_NOPM, 0, 0, &rsidetone_mux),
777
Mark Brownf1c0a022008-08-26 13:05:27 +0100778SND_SOC_DAPM_DAC("DACL", "Left Playback", WM8903_POWER_MANAGEMENT_6, 3, 0),
779SND_SOC_DAPM_DAC("DACR", "Right Playback", WM8903_POWER_MANAGEMENT_6, 2, 0),
780
781SND_SOC_DAPM_MIXER("Left Output Mixer", WM8903_POWER_MANAGEMENT_1, 1, 0,
782 left_output_mixer, ARRAY_SIZE(left_output_mixer)),
783SND_SOC_DAPM_MIXER("Right Output Mixer", WM8903_POWER_MANAGEMENT_1, 0, 0,
784 right_output_mixer, ARRAY_SIZE(right_output_mixer)),
785
786SND_SOC_DAPM_MIXER("Left Speaker Mixer", WM8903_POWER_MANAGEMENT_4, 1, 0,
787 left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer)),
788SND_SOC_DAPM_MIXER("Right Speaker Mixer", WM8903_POWER_MANAGEMENT_4, 0, 0,
789 right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer)),
790
791SND_SOC_DAPM_PGA_E("Left Headphone Output PGA", WM8903_POWER_MANAGEMENT_2,
792 1, 0, NULL, 0, wm8903_output_event,
793 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
Mark Brown42768a12009-04-22 18:39:39 +0100794 SND_SOC_DAPM_PRE_PMD),
Mark Brownf1c0a022008-08-26 13:05:27 +0100795SND_SOC_DAPM_PGA_E("Right Headphone Output PGA", WM8903_POWER_MANAGEMENT_2,
796 0, 0, NULL, 0, wm8903_output_event,
797 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
Mark Brown42768a12009-04-22 18:39:39 +0100798 SND_SOC_DAPM_PRE_PMD),
Mark Brownf1c0a022008-08-26 13:05:27 +0100799
800SND_SOC_DAPM_PGA_E("Left Line Output PGA", WM8903_POWER_MANAGEMENT_3, 1, 0,
801 NULL, 0, wm8903_output_event,
802 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
Mark Brown42768a12009-04-22 18:39:39 +0100803 SND_SOC_DAPM_PRE_PMD),
Mark Brownf1c0a022008-08-26 13:05:27 +0100804SND_SOC_DAPM_PGA_E("Right Line Output PGA", WM8903_POWER_MANAGEMENT_3, 0, 0,
805 NULL, 0, wm8903_output_event,
806 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
Mark Brown42768a12009-04-22 18:39:39 +0100807 SND_SOC_DAPM_PRE_PMD),
Mark Brownf1c0a022008-08-26 13:05:27 +0100808
809SND_SOC_DAPM_PGA("Left Speaker PGA", WM8903_POWER_MANAGEMENT_5, 1, 0,
810 NULL, 0),
811SND_SOC_DAPM_PGA("Right Speaker PGA", WM8903_POWER_MANAGEMENT_5, 0, 0,
812 NULL, 0),
813
Mark Brown42768a12009-04-22 18:39:39 +0100814SND_SOC_DAPM_SUPPLY("Charge Pump", WM8903_CHARGE_PUMP_0, 0, 0,
815 wm8903_cp_event, SND_SOC_DAPM_POST_PMU),
Mark Brownc2aef4f2009-04-22 20:04:44 +0100816SND_SOC_DAPM_SUPPLY("CLK_DSP", WM8903_CLOCK_RATES_2, 1, 0, NULL, 0),
Mark Brownf1c0a022008-08-26 13:05:27 +0100817};
818
819static const struct snd_soc_dapm_route intercon[] = {
820
821 { "Left Input Mux", "IN1L", "IN1L" },
822 { "Left Input Mux", "IN2L", "IN2L" },
823 { "Left Input Mux", "IN3L", "IN3L" },
824
825 { "Left Input Inverting Mux", "IN1L", "IN1L" },
826 { "Left Input Inverting Mux", "IN2L", "IN2L" },
827 { "Left Input Inverting Mux", "IN3L", "IN3L" },
828
829 { "Right Input Mux", "IN1R", "IN1R" },
830 { "Right Input Mux", "IN2R", "IN2R" },
831 { "Right Input Mux", "IN3R", "IN3R" },
832
833 { "Right Input Inverting Mux", "IN1R", "IN1R" },
834 { "Right Input Inverting Mux", "IN2R", "IN2R" },
835 { "Right Input Inverting Mux", "IN3R", "IN3R" },
836
837 { "Left Input Mode Mux", "Single-Ended", "Left Input Inverting Mux" },
838 { "Left Input Mode Mux", "Differential Line",
839 "Left Input Mux" },
840 { "Left Input Mode Mux", "Differential Line",
841 "Left Input Inverting Mux" },
842 { "Left Input Mode Mux", "Differential Mic",
843 "Left Input Mux" },
844 { "Left Input Mode Mux", "Differential Mic",
845 "Left Input Inverting Mux" },
846
847 { "Right Input Mode Mux", "Single-Ended",
848 "Right Input Inverting Mux" },
849 { "Right Input Mode Mux", "Differential Line",
850 "Right Input Mux" },
851 { "Right Input Mode Mux", "Differential Line",
852 "Right Input Inverting Mux" },
853 { "Right Input Mode Mux", "Differential Mic",
854 "Right Input Mux" },
855 { "Right Input Mode Mux", "Differential Mic",
856 "Right Input Inverting Mux" },
857
858 { "Left Input PGA", NULL, "Left Input Mode Mux" },
859 { "Right Input PGA", NULL, "Right Input Mode Mux" },
860
861 { "ADCL", NULL, "Left Input PGA" },
Mark Brownc2aef4f2009-04-22 20:04:44 +0100862 { "ADCL", NULL, "CLK_DSP" },
Mark Brownf1c0a022008-08-26 13:05:27 +0100863 { "ADCR", NULL, "Right Input PGA" },
Mark Brownc2aef4f2009-04-22 20:04:44 +0100864 { "ADCR", NULL, "CLK_DSP" },
865
Mark Brown291ce182009-04-22 21:36:14 +0100866 { "DACL Sidetone", "Left", "ADCL" },
867 { "DACL Sidetone", "Right", "ADCR" },
868 { "DACR Sidetone", "Left", "ADCL" },
869 { "DACR Sidetone", "Right", "ADCR" },
870
871 { "DACL", NULL, "DACL Sidetone" },
Mark Brownc2aef4f2009-04-22 20:04:44 +0100872 { "DACL", NULL, "CLK_DSP" },
Mark Brown291ce182009-04-22 21:36:14 +0100873 { "DACR", NULL, "DACR Sidetone" },
Mark Brownc2aef4f2009-04-22 20:04:44 +0100874 { "DACR", NULL, "CLK_DSP" },
Mark Brownf1c0a022008-08-26 13:05:27 +0100875
876 { "Left Output Mixer", "Left Bypass Switch", "Left Input PGA" },
877 { "Left Output Mixer", "Right Bypass Switch", "Right Input PGA" },
878 { "Left Output Mixer", "DACL Switch", "DACL" },
879 { "Left Output Mixer", "DACR Switch", "DACR" },
880
881 { "Right Output Mixer", "Left Bypass Switch", "Left Input PGA" },
882 { "Right Output Mixer", "Right Bypass Switch", "Right Input PGA" },
883 { "Right Output Mixer", "DACL Switch", "DACL" },
884 { "Right Output Mixer", "DACR Switch", "DACR" },
885
886 { "Left Speaker Mixer", "Left Bypass Switch", "Left Input PGA" },
887 { "Left Speaker Mixer", "Right Bypass Switch", "Right Input PGA" },
888 { "Left Speaker Mixer", "DACL Switch", "DACL" },
889 { "Left Speaker Mixer", "DACR Switch", "DACR" },
890
891 { "Right Speaker Mixer", "Left Bypass Switch", "Left Input PGA" },
892 { "Right Speaker Mixer", "Right Bypass Switch", "Right Input PGA" },
893 { "Right Speaker Mixer", "DACL Switch", "DACL" },
894 { "Right Speaker Mixer", "DACR Switch", "DACR" },
895
896 { "Left Line Output PGA", NULL, "Left Output Mixer" },
897 { "Right Line Output PGA", NULL, "Right Output Mixer" },
898
899 { "Left Headphone Output PGA", NULL, "Left Output Mixer" },
900 { "Right Headphone Output PGA", NULL, "Right Output Mixer" },
901
902 { "Left Speaker PGA", NULL, "Left Speaker Mixer" },
903 { "Right Speaker PGA", NULL, "Right Speaker Mixer" },
904
905 { "HPOUTL", NULL, "Left Headphone Output PGA" },
906 { "HPOUTR", NULL, "Right Headphone Output PGA" },
907
908 { "LINEOUTL", NULL, "Left Line Output PGA" },
909 { "LINEOUTR", NULL, "Right Line Output PGA" },
910
911 { "LOP", NULL, "Left Speaker PGA" },
912 { "LON", NULL, "Left Speaker PGA" },
913
914 { "ROP", NULL, "Right Speaker PGA" },
915 { "RON", NULL, "Right Speaker PGA" },
Mark Brown42768a12009-04-22 18:39:39 +0100916
917 { "Left Headphone Output PGA", NULL, "Charge Pump" },
918 { "Right Headphone Output PGA", NULL, "Charge Pump" },
919 { "Left Line Output PGA", NULL, "Charge Pump" },
920 { "Right Line Output PGA", NULL, "Charge Pump" },
Mark Brownf1c0a022008-08-26 13:05:27 +0100921};
922
923static int wm8903_add_widgets(struct snd_soc_codec *codec)
924{
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200925 struct snd_soc_dapm_context *dapm = &codec->dapm;
Mark Brownf1c0a022008-08-26 13:05:27 +0100926
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200927 snd_soc_dapm_new_controls(dapm, wm8903_dapm_widgets,
928 ARRAY_SIZE(wm8903_dapm_widgets));
929 snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
Mark Brownf1c0a022008-08-26 13:05:27 +0100930
Mark Brownf1c0a022008-08-26 13:05:27 +0100931 return 0;
932}
933
934static int wm8903_set_bias_level(struct snd_soc_codec *codec,
935 enum snd_soc_bias_level level)
936{
Mark Brownf1c0a022008-08-26 13:05:27 +0100937 u16 reg, reg2;
938
939 switch (level) {
940 case SND_SOC_BIAS_ON:
941 case SND_SOC_BIAS_PREPARE:
Mark Brown8d50e442009-07-10 23:12:01 +0100942 reg = snd_soc_read(codec, WM8903_VMID_CONTROL_0);
Mark Brownf1c0a022008-08-26 13:05:27 +0100943 reg &= ~(WM8903_VMID_RES_MASK);
944 reg |= WM8903_VMID_RES_50K;
Mark Brown8d50e442009-07-10 23:12:01 +0100945 snd_soc_write(codec, WM8903_VMID_CONTROL_0, reg);
Mark Brownf1c0a022008-08-26 13:05:27 +0100946 break;
947
948 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200949 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
Mark Brown8d50e442009-07-10 23:12:01 +0100950 snd_soc_write(codec, WM8903_CLOCK_RATES_2,
Mark Brown3b1228a2008-12-10 19:27:10 +0000951 WM8903_CLK_SYS_ENA);
952
Mark Brown4dbfe802009-04-22 20:32:40 +0100953 /* Change DC servo dither level in startup sequence */
Mark Brown8d50e442009-07-10 23:12:01 +0100954 snd_soc_write(codec, WM8903_WRITE_SEQUENCER_0, 0x11);
955 snd_soc_write(codec, WM8903_WRITE_SEQUENCER_1, 0x1257);
956 snd_soc_write(codec, WM8903_WRITE_SEQUENCER_2, 0x2);
Mark Brown4dbfe802009-04-22 20:32:40 +0100957
Mark Brownf1c0a022008-08-26 13:05:27 +0100958 wm8903_run_sequence(codec, 0);
959 wm8903_sync_reg_cache(codec, codec->reg_cache);
960
961 /* Enable low impedence charge pump output */
Mark Brown8d50e442009-07-10 23:12:01 +0100962 reg = snd_soc_read(codec,
Mark Brownf1c0a022008-08-26 13:05:27 +0100963 WM8903_CONTROL_INTERFACE_TEST_1);
Mark Brown8d50e442009-07-10 23:12:01 +0100964 snd_soc_write(codec, WM8903_CONTROL_INTERFACE_TEST_1,
Mark Brownf1c0a022008-08-26 13:05:27 +0100965 reg | WM8903_TEST_KEY);
Mark Brown8d50e442009-07-10 23:12:01 +0100966 reg2 = snd_soc_read(codec, WM8903_CHARGE_PUMP_TEST_1);
967 snd_soc_write(codec, WM8903_CHARGE_PUMP_TEST_1,
Mark Brownf1c0a022008-08-26 13:05:27 +0100968 reg2 | WM8903_CP_SW_KELVIN_MODE_MASK);
Mark Brown8d50e442009-07-10 23:12:01 +0100969 snd_soc_write(codec, WM8903_CONTROL_INTERFACE_TEST_1,
Mark Brownf1c0a022008-08-26 13:05:27 +0100970 reg);
971
972 /* By default no bypass paths are enabled so
973 * enable Class W support.
974 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000975 dev_dbg(codec->dev, "Enabling Class W\n");
Mark Brown8d50e442009-07-10 23:12:01 +0100976 snd_soc_write(codec, WM8903_CLASS_W_0, reg |
Mark Brownf1c0a022008-08-26 13:05:27 +0100977 WM8903_CP_DYN_FREQ | WM8903_CP_DYN_V);
978 }
979
Mark Brown8d50e442009-07-10 23:12:01 +0100980 reg = snd_soc_read(codec, WM8903_VMID_CONTROL_0);
Mark Brownf1c0a022008-08-26 13:05:27 +0100981 reg &= ~(WM8903_VMID_RES_MASK);
982 reg |= WM8903_VMID_RES_250K;
Mark Brown8d50e442009-07-10 23:12:01 +0100983 snd_soc_write(codec, WM8903_VMID_CONTROL_0, reg);
Mark Brownf1c0a022008-08-26 13:05:27 +0100984 break;
985
986 case SND_SOC_BIAS_OFF:
987 wm8903_run_sequence(codec, 32);
Mark Brown8d50e442009-07-10 23:12:01 +0100988 reg = snd_soc_read(codec, WM8903_CLOCK_RATES_2);
Mark Brown3b1228a2008-12-10 19:27:10 +0000989 reg &= ~WM8903_CLK_SYS_ENA;
Mark Brown8d50e442009-07-10 23:12:01 +0100990 snd_soc_write(codec, WM8903_CLOCK_RATES_2, reg);
Mark Brownf1c0a022008-08-26 13:05:27 +0100991 break;
992 }
993
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200994 codec->dapm.bias_level = level;
Mark Brownf1c0a022008-08-26 13:05:27 +0100995
996 return 0;
997}
998
999static int wm8903_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1000 int clk_id, unsigned int freq, int dir)
1001{
1002 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001003 struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
Mark Brownf1c0a022008-08-26 13:05:27 +01001004
1005 wm8903->sysclk = freq;
1006
1007 return 0;
1008}
1009
1010static int wm8903_set_dai_fmt(struct snd_soc_dai *codec_dai,
1011 unsigned int fmt)
1012{
1013 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brown8d50e442009-07-10 23:12:01 +01001014 u16 aif1 = snd_soc_read(codec, WM8903_AUDIO_INTERFACE_1);
Mark Brownf1c0a022008-08-26 13:05:27 +01001015
1016 aif1 &= ~(WM8903_LRCLK_DIR | WM8903_BCLK_DIR | WM8903_AIF_FMT_MASK |
1017 WM8903_AIF_LRCLK_INV | WM8903_AIF_BCLK_INV);
1018
1019 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1020 case SND_SOC_DAIFMT_CBS_CFS:
1021 break;
1022 case SND_SOC_DAIFMT_CBS_CFM:
1023 aif1 |= WM8903_LRCLK_DIR;
1024 break;
1025 case SND_SOC_DAIFMT_CBM_CFM:
1026 aif1 |= WM8903_LRCLK_DIR | WM8903_BCLK_DIR;
1027 break;
1028 case SND_SOC_DAIFMT_CBM_CFS:
1029 aif1 |= WM8903_BCLK_DIR;
1030 break;
1031 default:
1032 return -EINVAL;
1033 }
1034
1035 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1036 case SND_SOC_DAIFMT_DSP_A:
1037 aif1 |= 0x3;
1038 break;
1039 case SND_SOC_DAIFMT_DSP_B:
1040 aif1 |= 0x3 | WM8903_AIF_LRCLK_INV;
1041 break;
1042 case SND_SOC_DAIFMT_I2S:
1043 aif1 |= 0x2;
1044 break;
1045 case SND_SOC_DAIFMT_RIGHT_J:
1046 aif1 |= 0x1;
1047 break;
1048 case SND_SOC_DAIFMT_LEFT_J:
1049 break;
1050 default:
1051 return -EINVAL;
1052 }
1053
1054 /* Clock inversion */
1055 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1056 case SND_SOC_DAIFMT_DSP_A:
1057 case SND_SOC_DAIFMT_DSP_B:
1058 /* frame inversion not valid for DSP modes */
1059 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1060 case SND_SOC_DAIFMT_NB_NF:
1061 break;
1062 case SND_SOC_DAIFMT_IB_NF:
1063 aif1 |= WM8903_AIF_BCLK_INV;
1064 break;
1065 default:
1066 return -EINVAL;
1067 }
1068 break;
1069 case SND_SOC_DAIFMT_I2S:
1070 case SND_SOC_DAIFMT_RIGHT_J:
1071 case SND_SOC_DAIFMT_LEFT_J:
1072 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1073 case SND_SOC_DAIFMT_NB_NF:
1074 break;
1075 case SND_SOC_DAIFMT_IB_IF:
1076 aif1 |= WM8903_AIF_BCLK_INV | WM8903_AIF_LRCLK_INV;
1077 break;
1078 case SND_SOC_DAIFMT_IB_NF:
1079 aif1 |= WM8903_AIF_BCLK_INV;
1080 break;
1081 case SND_SOC_DAIFMT_NB_IF:
1082 aif1 |= WM8903_AIF_LRCLK_INV;
1083 break;
1084 default:
1085 return -EINVAL;
1086 }
1087 break;
1088 default:
1089 return -EINVAL;
1090 }
1091
Mark Brown8d50e442009-07-10 23:12:01 +01001092 snd_soc_write(codec, WM8903_AUDIO_INTERFACE_1, aif1);
Mark Brownf1c0a022008-08-26 13:05:27 +01001093
1094 return 0;
1095}
1096
1097static int wm8903_digital_mute(struct snd_soc_dai *codec_dai, int mute)
1098{
1099 struct snd_soc_codec *codec = codec_dai->codec;
1100 u16 reg;
1101
Mark Brown8d50e442009-07-10 23:12:01 +01001102 reg = snd_soc_read(codec, WM8903_DAC_DIGITAL_1);
Mark Brownf1c0a022008-08-26 13:05:27 +01001103
1104 if (mute)
1105 reg |= WM8903_DAC_MUTE;
1106 else
1107 reg &= ~WM8903_DAC_MUTE;
1108
Mark Brown8d50e442009-07-10 23:12:01 +01001109 snd_soc_write(codec, WM8903_DAC_DIGITAL_1, reg);
Mark Brownf1c0a022008-08-26 13:05:27 +01001110
1111 return 0;
1112}
1113
1114/* Lookup table for CLK_SYS/fs ratio. 256fs or more is recommended
1115 * for optimal performance so we list the lower rates first and match
1116 * on the last match we find. */
1117static struct {
1118 int div;
1119 int rate;
1120 int mode;
1121 int mclk_div;
1122} clk_sys_ratios[] = {
1123 { 64, 0x0, 0x0, 1 },
1124 { 68, 0x0, 0x1, 1 },
1125 { 125, 0x0, 0x2, 1 },
1126 { 128, 0x1, 0x0, 1 },
1127 { 136, 0x1, 0x1, 1 },
1128 { 192, 0x2, 0x0, 1 },
1129 { 204, 0x2, 0x1, 1 },
1130
1131 { 64, 0x0, 0x0, 2 },
1132 { 68, 0x0, 0x1, 2 },
1133 { 125, 0x0, 0x2, 2 },
1134 { 128, 0x1, 0x0, 2 },
1135 { 136, 0x1, 0x1, 2 },
1136 { 192, 0x2, 0x0, 2 },
1137 { 204, 0x2, 0x1, 2 },
1138
1139 { 250, 0x2, 0x2, 1 },
1140 { 256, 0x3, 0x0, 1 },
1141 { 272, 0x3, 0x1, 1 },
1142 { 384, 0x4, 0x0, 1 },
1143 { 408, 0x4, 0x1, 1 },
1144 { 375, 0x4, 0x2, 1 },
1145 { 512, 0x5, 0x0, 1 },
1146 { 544, 0x5, 0x1, 1 },
1147 { 500, 0x5, 0x2, 1 },
1148 { 768, 0x6, 0x0, 1 },
1149 { 816, 0x6, 0x1, 1 },
1150 { 750, 0x6, 0x2, 1 },
1151 { 1024, 0x7, 0x0, 1 },
1152 { 1088, 0x7, 0x1, 1 },
1153 { 1000, 0x7, 0x2, 1 },
1154 { 1408, 0x8, 0x0, 1 },
1155 { 1496, 0x8, 0x1, 1 },
1156 { 1536, 0x9, 0x0, 1 },
1157 { 1632, 0x9, 0x1, 1 },
1158 { 1500, 0x9, 0x2, 1 },
1159
1160 { 250, 0x2, 0x2, 2 },
1161 { 256, 0x3, 0x0, 2 },
1162 { 272, 0x3, 0x1, 2 },
1163 { 384, 0x4, 0x0, 2 },
1164 { 408, 0x4, 0x1, 2 },
1165 { 375, 0x4, 0x2, 2 },
1166 { 512, 0x5, 0x0, 2 },
1167 { 544, 0x5, 0x1, 2 },
1168 { 500, 0x5, 0x2, 2 },
1169 { 768, 0x6, 0x0, 2 },
1170 { 816, 0x6, 0x1, 2 },
1171 { 750, 0x6, 0x2, 2 },
1172 { 1024, 0x7, 0x0, 2 },
1173 { 1088, 0x7, 0x1, 2 },
1174 { 1000, 0x7, 0x2, 2 },
1175 { 1408, 0x8, 0x0, 2 },
1176 { 1496, 0x8, 0x1, 2 },
1177 { 1536, 0x9, 0x0, 2 },
1178 { 1632, 0x9, 0x1, 2 },
1179 { 1500, 0x9, 0x2, 2 },
1180};
1181
1182/* CLK_SYS/BCLK ratios - multiplied by 10 due to .5s */
1183static struct {
1184 int ratio;
1185 int div;
1186} bclk_divs[] = {
1187 { 10, 0 },
Mark Brownf1c0a022008-08-26 13:05:27 +01001188 { 20, 2 },
1189 { 30, 3 },
1190 { 40, 4 },
1191 { 50, 5 },
Mark Brownf1c0a022008-08-26 13:05:27 +01001192 { 60, 7 },
1193 { 80, 8 },
1194 { 100, 9 },
Mark Brownf1c0a022008-08-26 13:05:27 +01001195 { 120, 11 },
1196 { 160, 12 },
1197 { 200, 13 },
1198 { 220, 14 },
1199 { 240, 15 },
Mark Brownf1c0a022008-08-26 13:05:27 +01001200 { 300, 17 },
1201 { 320, 18 },
1202 { 440, 19 },
1203 { 480, 20 },
1204};
1205
1206/* Sample rates for DSP */
1207static struct {
1208 int rate;
1209 int value;
1210} sample_rates[] = {
1211 { 8000, 0 },
1212 { 11025, 1 },
1213 { 12000, 2 },
1214 { 16000, 3 },
1215 { 22050, 4 },
1216 { 24000, 5 },
1217 { 32000, 6 },
1218 { 44100, 7 },
1219 { 48000, 8 },
1220 { 88200, 9 },
1221 { 96000, 10 },
1222 { 0, 0 },
1223};
1224
Mark Browndee89c42008-11-18 22:11:38 +00001225static int wm8903_startup(struct snd_pcm_substream *substream,
1226 struct snd_soc_dai *dai)
Mark Brownf1c0a022008-08-26 13:05:27 +01001227{
1228 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001229 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001230 struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
Mark Brownf1c0a022008-08-26 13:05:27 +01001231 struct snd_pcm_runtime *master_runtime;
1232
1233 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1234 wm8903->playback_active++;
1235 else
1236 wm8903->capture_active++;
1237
1238 /* The DAI has shared clocks so if we already have a playback or
1239 * capture going then constrain this substream to match it.
1240 */
1241 if (wm8903->master_substream) {
1242 master_runtime = wm8903->master_substream->runtime;
1243
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001244 dev_dbg(codec->dev, "Constraining to %d bits\n",
Mark Brown727fb902009-04-22 21:06:14 +01001245 master_runtime->sample_bits);
Mark Brownf1c0a022008-08-26 13:05:27 +01001246
1247 snd_pcm_hw_constraint_minmax(substream->runtime,
1248 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1249 master_runtime->sample_bits,
1250 master_runtime->sample_bits);
1251
1252 wm8903->slave_substream = substream;
1253 } else
1254 wm8903->master_substream = substream;
1255
1256 return 0;
1257}
1258
Mark Browndee89c42008-11-18 22:11:38 +00001259static void wm8903_shutdown(struct snd_pcm_substream *substream,
1260 struct snd_soc_dai *dai)
Mark Brownf1c0a022008-08-26 13:05:27 +01001261{
1262 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001263 struct snd_soc_codec *codec = rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001264 struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
Mark Brownf1c0a022008-08-26 13:05:27 +01001265
1266 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1267 wm8903->playback_active--;
1268 else
1269 wm8903->capture_active--;
1270
1271 if (wm8903->master_substream == substream)
1272 wm8903->master_substream = wm8903->slave_substream;
1273
1274 wm8903->slave_substream = NULL;
1275}
1276
1277static int wm8903_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +00001278 struct snd_pcm_hw_params *params,
1279 struct snd_soc_dai *dai)
Mark Brownf1c0a022008-08-26 13:05:27 +01001280{
1281 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001282 struct snd_soc_codec *codec =rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001283 struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
Mark Brownf1c0a022008-08-26 13:05:27 +01001284 int fs = params_rate(params);
1285 int bclk;
1286 int bclk_div;
1287 int i;
1288 int dsp_config;
1289 int clk_config;
1290 int best_val;
1291 int cur_val;
1292 int clk_sys;
1293
Mark Brown8d50e442009-07-10 23:12:01 +01001294 u16 aif1 = snd_soc_read(codec, WM8903_AUDIO_INTERFACE_1);
1295 u16 aif2 = snd_soc_read(codec, WM8903_AUDIO_INTERFACE_2);
1296 u16 aif3 = snd_soc_read(codec, WM8903_AUDIO_INTERFACE_3);
1297 u16 clock0 = snd_soc_read(codec, WM8903_CLOCK_RATES_0);
1298 u16 clock1 = snd_soc_read(codec, WM8903_CLOCK_RATES_1);
1299 u16 dac_digital1 = snd_soc_read(codec, WM8903_DAC_DIGITAL_1);
Mark Brownf1c0a022008-08-26 13:05:27 +01001300
1301 if (substream == wm8903->slave_substream) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001302 dev_dbg(codec->dev, "Ignoring hw_params for slave substream\n");
Mark Brownf1c0a022008-08-26 13:05:27 +01001303 return 0;
1304 }
1305
Mark Brown9e792612009-06-12 17:27:07 +01001306 /* Enable sloping stopband filter for low sample rates */
1307 if (fs <= 24000)
1308 dac_digital1 |= WM8903_DAC_SB_FILT;
1309 else
1310 dac_digital1 &= ~WM8903_DAC_SB_FILT;
1311
Mark Brownf1c0a022008-08-26 13:05:27 +01001312 /* Configure sample rate logic for DSP - choose nearest rate */
1313 dsp_config = 0;
1314 best_val = abs(sample_rates[dsp_config].rate - fs);
1315 for (i = 1; i < ARRAY_SIZE(sample_rates); i++) {
1316 cur_val = abs(sample_rates[i].rate - fs);
1317 if (cur_val <= best_val) {
1318 dsp_config = i;
1319 best_val = cur_val;
1320 }
1321 }
1322
1323 /* Constraints should stop us hitting this but let's make sure */
1324 if (wm8903->capture_active)
1325 switch (sample_rates[dsp_config].rate) {
1326 case 88200:
1327 case 96000:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001328 dev_err(codec->dev, "%dHz unsupported by ADC\n",
Mark Brownf1c0a022008-08-26 13:05:27 +01001329 fs);
1330 return -EINVAL;
1331
1332 default:
1333 break;
1334 }
1335
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001336 dev_dbg(codec->dev, "DSP fs = %dHz\n", sample_rates[dsp_config].rate);
Mark Brownf1c0a022008-08-26 13:05:27 +01001337 clock1 &= ~WM8903_SAMPLE_RATE_MASK;
1338 clock1 |= sample_rates[dsp_config].value;
1339
1340 aif1 &= ~WM8903_AIF_WL_MASK;
1341 bclk = 2 * fs;
1342 switch (params_format(params)) {
1343 case SNDRV_PCM_FORMAT_S16_LE:
1344 bclk *= 16;
1345 break;
1346 case SNDRV_PCM_FORMAT_S20_3LE:
1347 bclk *= 20;
1348 aif1 |= 0x4;
1349 break;
1350 case SNDRV_PCM_FORMAT_S24_LE:
1351 bclk *= 24;
1352 aif1 |= 0x8;
1353 break;
1354 case SNDRV_PCM_FORMAT_S32_LE:
1355 bclk *= 32;
1356 aif1 |= 0xc;
1357 break;
1358 default:
1359 return -EINVAL;
1360 }
1361
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001362 dev_dbg(codec->dev, "MCLK = %dHz, target sample rate = %dHz\n",
Mark Brownf1c0a022008-08-26 13:05:27 +01001363 wm8903->sysclk, fs);
1364
1365 /* We may not have an MCLK which allows us to generate exactly
1366 * the clock we want, particularly with USB derived inputs, so
1367 * approximate.
1368 */
1369 clk_config = 0;
1370 best_val = abs((wm8903->sysclk /
1371 (clk_sys_ratios[0].mclk_div *
1372 clk_sys_ratios[0].div)) - fs);
1373 for (i = 1; i < ARRAY_SIZE(clk_sys_ratios); i++) {
1374 cur_val = abs((wm8903->sysclk /
1375 (clk_sys_ratios[i].mclk_div *
1376 clk_sys_ratios[i].div)) - fs);
1377
1378 if (cur_val <= best_val) {
1379 clk_config = i;
1380 best_val = cur_val;
1381 }
1382 }
1383
1384 if (clk_sys_ratios[clk_config].mclk_div == 2) {
1385 clock0 |= WM8903_MCLKDIV2;
1386 clk_sys = wm8903->sysclk / 2;
1387 } else {
1388 clock0 &= ~WM8903_MCLKDIV2;
1389 clk_sys = wm8903->sysclk;
1390 }
1391
1392 clock1 &= ~(WM8903_CLK_SYS_RATE_MASK |
1393 WM8903_CLK_SYS_MODE_MASK);
1394 clock1 |= clk_sys_ratios[clk_config].rate << WM8903_CLK_SYS_RATE_SHIFT;
1395 clock1 |= clk_sys_ratios[clk_config].mode << WM8903_CLK_SYS_MODE_SHIFT;
1396
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001397 dev_dbg(codec->dev, "CLK_SYS_RATE=%x, CLK_SYS_MODE=%x div=%d\n",
Mark Brownf1c0a022008-08-26 13:05:27 +01001398 clk_sys_ratios[clk_config].rate,
1399 clk_sys_ratios[clk_config].mode,
1400 clk_sys_ratios[clk_config].div);
1401
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001402 dev_dbg(codec->dev, "Actual CLK_SYS = %dHz\n", clk_sys);
Mark Brownf1c0a022008-08-26 13:05:27 +01001403
1404 /* We may not get quite the right frequency if using
1405 * approximate clocks so look for the closest match that is
1406 * higher than the target (we need to ensure that there enough
1407 * BCLKs to clock out the samples).
1408 */
1409 bclk_div = 0;
1410 best_val = ((clk_sys * 10) / bclk_divs[0].ratio) - bclk;
1411 i = 1;
1412 while (i < ARRAY_SIZE(bclk_divs)) {
1413 cur_val = ((clk_sys * 10) / bclk_divs[i].ratio) - bclk;
1414 if (cur_val < 0) /* BCLK table is sorted */
1415 break;
1416 bclk_div = i;
1417 best_val = cur_val;
1418 i++;
1419 }
1420
1421 aif2 &= ~WM8903_BCLK_DIV_MASK;
1422 aif3 &= ~WM8903_LRCLK_RATE_MASK;
1423
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001424 dev_dbg(codec->dev, "BCLK ratio %d for %dHz - actual BCLK = %dHz\n",
Mark Brownf1c0a022008-08-26 13:05:27 +01001425 bclk_divs[bclk_div].ratio / 10, bclk,
1426 (clk_sys * 10) / bclk_divs[bclk_div].ratio);
1427
1428 aif2 |= bclk_divs[bclk_div].div;
1429 aif3 |= bclk / fs;
1430
Mark Brown8d50e442009-07-10 23:12:01 +01001431 snd_soc_write(codec, WM8903_CLOCK_RATES_0, clock0);
1432 snd_soc_write(codec, WM8903_CLOCK_RATES_1, clock1);
1433 snd_soc_write(codec, WM8903_AUDIO_INTERFACE_1, aif1);
1434 snd_soc_write(codec, WM8903_AUDIO_INTERFACE_2, aif2);
1435 snd_soc_write(codec, WM8903_AUDIO_INTERFACE_3, aif3);
1436 snd_soc_write(codec, WM8903_DAC_DIGITAL_1, dac_digital1);
Mark Brownf1c0a022008-08-26 13:05:27 +01001437
1438 return 0;
1439}
1440
Mark Brown72453872010-03-15 21:22:58 +00001441/**
1442 * wm8903_mic_detect - Enable microphone detection via the WM8903 IRQ
1443 *
1444 * @codec: WM8903 codec
1445 * @jack: jack to report detection events on
1446 * @det: value to report for presence detection
1447 * @shrt: value to report for short detection
1448 *
1449 * Enable microphone detection via IRQ on the WM8903. If GPIOs are
1450 * being used to bring out signals to the processor then only platform
1451 * data configuration is needed for WM8903 and processor GPIOs should
1452 * be configured using snd_soc_jack_add_gpios() instead.
1453 *
1454 * The current threasholds for detection should be configured using
1455 * micdet_cfg in the platform data. Using this function will force on
1456 * the microphone bias for the device.
1457 */
1458int wm8903_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack,
1459 int det, int shrt)
1460{
Mark Brownb2c812e2010-04-14 15:35:19 +09001461 struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
Mark Brown69266862010-03-22 16:37:01 +00001462 int irq_mask = WM8903_MICDET_EINT | WM8903_MICSHRT_EINT;
Mark Brown72453872010-03-15 21:22:58 +00001463
1464 dev_dbg(codec->dev, "Enabling microphone detection: %x %x\n",
1465 det, shrt);
1466
1467 /* Store the configuration */
1468 wm8903->mic_jack = jack;
1469 wm8903->mic_det = det;
1470 wm8903->mic_short = shrt;
1471
1472 /* Enable interrupts we've got a report configured for */
1473 if (det)
1474 irq_mask &= ~WM8903_MICDET_EINT;
1475 if (shrt)
1476 irq_mask &= ~WM8903_MICSHRT_EINT;
1477
1478 snd_soc_update_bits(codec, WM8903_INTERRUPT_STATUS_1_MASK,
1479 WM8903_MICDET_EINT | WM8903_MICSHRT_EINT,
1480 irq_mask);
1481
Mark Brown69266862010-03-22 16:37:01 +00001482 if (det && shrt) {
1483 /* Enable mic detection, this may not have been set through
1484 * platform data (eg, if the defaults are OK). */
1485 snd_soc_update_bits(codec, WM8903_WRITE_SEQUENCER_0,
1486 WM8903_WSEQ_ENA, WM8903_WSEQ_ENA);
1487 snd_soc_update_bits(codec, WM8903_MIC_BIAS_CONTROL_0,
1488 WM8903_MICDET_ENA, WM8903_MICDET_ENA);
1489 } else {
1490 snd_soc_update_bits(codec, WM8903_MIC_BIAS_CONTROL_0,
1491 WM8903_MICDET_ENA, 0);
1492 }
Mark Brown72453872010-03-15 21:22:58 +00001493
1494 return 0;
1495}
1496EXPORT_SYMBOL_GPL(wm8903_mic_detect);
1497
Mark Brown8abd16a2010-03-15 18:25:26 +00001498static irqreturn_t wm8903_irq(int irq, void *data)
1499{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001500 struct snd_soc_codec *codec = data;
1501 struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
Mark Brown72453872010-03-15 21:22:58 +00001502 int mic_report;
1503 int int_pol;
1504 int int_val = 0;
1505 int mask = ~snd_soc_read(codec, WM8903_INTERRUPT_STATUS_1_MASK);
Mark Brown8abd16a2010-03-15 18:25:26 +00001506
Mark Brown72453872010-03-15 21:22:58 +00001507 int_val = snd_soc_read(codec, WM8903_INTERRUPT_STATUS_1) & mask;
Mark Brown8abd16a2010-03-15 18:25:26 +00001508
Mark Brown72453872010-03-15 21:22:58 +00001509 if (int_val & WM8903_WSEQ_BUSY_EINT) {
Mark Brown8abd16a2010-03-15 18:25:26 +00001510 dev_dbg(codec->dev, "Write sequencer done\n");
1511 complete(&wm8903->wseq);
1512 }
1513
Mark Brown72453872010-03-15 21:22:58 +00001514 /*
1515 * The rest is microphone jack detection. We need to manually
1516 * invert the polarity of the interrupt after each event - to
1517 * simplify the code keep track of the last state we reported
1518 * and just invert the relevant bits in both the report and
1519 * the polarity register.
1520 */
1521 mic_report = wm8903->mic_last_report;
1522 int_pol = snd_soc_read(codec, WM8903_INTERRUPT_POLARITY_1);
1523
1524 if (int_val & WM8903_MICSHRT_EINT) {
1525 dev_dbg(codec->dev, "Microphone short (pol=%x)\n", int_pol);
1526
1527 mic_report ^= wm8903->mic_short;
1528 int_pol ^= WM8903_MICSHRT_INV;
1529 }
1530
1531 if (int_val & WM8903_MICDET_EINT) {
1532 dev_dbg(codec->dev, "Microphone detect (pol=%x)\n", int_pol);
1533
1534 mic_report ^= wm8903->mic_det;
1535 int_pol ^= WM8903_MICDET_INV;
1536
1537 msleep(wm8903->mic_delay);
1538 }
1539
1540 snd_soc_update_bits(codec, WM8903_INTERRUPT_POLARITY_1,
1541 WM8903_MICSHRT_INV | WM8903_MICDET_INV, int_pol);
1542
1543 snd_soc_jack_report(wm8903->mic_jack, mic_report,
1544 wm8903->mic_short | wm8903->mic_det);
1545
1546 wm8903->mic_last_report = mic_report;
1547
Mark Brown8abd16a2010-03-15 18:25:26 +00001548 return IRQ_HANDLED;
1549}
1550
Mark Brownf1c0a022008-08-26 13:05:27 +01001551#define WM8903_PLAYBACK_RATES (SNDRV_PCM_RATE_8000 |\
1552 SNDRV_PCM_RATE_11025 | \
1553 SNDRV_PCM_RATE_16000 | \
1554 SNDRV_PCM_RATE_22050 | \
1555 SNDRV_PCM_RATE_32000 | \
1556 SNDRV_PCM_RATE_44100 | \
1557 SNDRV_PCM_RATE_48000 | \
1558 SNDRV_PCM_RATE_88200 | \
1559 SNDRV_PCM_RATE_96000)
1560
1561#define WM8903_CAPTURE_RATES (SNDRV_PCM_RATE_8000 |\
1562 SNDRV_PCM_RATE_11025 | \
1563 SNDRV_PCM_RATE_16000 | \
1564 SNDRV_PCM_RATE_22050 | \
1565 SNDRV_PCM_RATE_32000 | \
1566 SNDRV_PCM_RATE_44100 | \
1567 SNDRV_PCM_RATE_48000)
1568
1569#define WM8903_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
1570 SNDRV_PCM_FMTBIT_S20_3LE |\
1571 SNDRV_PCM_FMTBIT_S24_LE)
1572
Eric Miao6335d052009-03-03 09:41:00 +08001573static struct snd_soc_dai_ops wm8903_dai_ops = {
1574 .startup = wm8903_startup,
1575 .shutdown = wm8903_shutdown,
1576 .hw_params = wm8903_hw_params,
1577 .digital_mute = wm8903_digital_mute,
1578 .set_fmt = wm8903_set_dai_fmt,
1579 .set_sysclk = wm8903_set_dai_sysclk,
1580};
1581
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001582static struct snd_soc_dai_driver wm8903_dai = {
1583 .name = "wm8903-hifi",
Mark Brownf1c0a022008-08-26 13:05:27 +01001584 .playback = {
1585 .stream_name = "Playback",
1586 .channels_min = 2,
1587 .channels_max = 2,
1588 .rates = WM8903_PLAYBACK_RATES,
1589 .formats = WM8903_FORMATS,
1590 },
1591 .capture = {
1592 .stream_name = "Capture",
1593 .channels_min = 2,
1594 .channels_max = 2,
1595 .rates = WM8903_CAPTURE_RATES,
1596 .formats = WM8903_FORMATS,
1597 },
Eric Miao6335d052009-03-03 09:41:00 +08001598 .ops = &wm8903_dai_ops,
Mark Brown0d960e82009-04-16 10:08:39 +01001599 .symmetric_rates = 1,
Mark Brownf1c0a022008-08-26 13:05:27 +01001600};
Mark Brownf1c0a022008-08-26 13:05:27 +01001601
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001602static int wm8903_suspend(struct snd_soc_codec *codec, pm_message_t state)
Mark Brownf1c0a022008-08-26 13:05:27 +01001603{
Mark Brownf1c0a022008-08-26 13:05:27 +01001604 wm8903_set_bias_level(codec, SND_SOC_BIAS_OFF);
1605
1606 return 0;
1607}
1608
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001609static int wm8903_resume(struct snd_soc_codec *codec)
Mark Brownf1c0a022008-08-26 13:05:27 +01001610{
Mark Brownf1c0a022008-08-26 13:05:27 +01001611 int i;
1612 u16 *reg_cache = codec->reg_cache;
Guennadi Liakhovetski40aa7032010-01-22 18:00:03 +01001613 u16 *tmp_cache = kmemdup(reg_cache, sizeof(wm8903_reg_defaults),
Mark Brownf1c0a022008-08-26 13:05:27 +01001614 GFP_KERNEL);
1615
1616 /* Bring the codec back up to standby first to minimise pop/clicks */
1617 wm8903_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Mark Brownf1c0a022008-08-26 13:05:27 +01001618
1619 /* Sync back everything else */
1620 if (tmp_cache) {
1621 for (i = 2; i < ARRAY_SIZE(wm8903_reg_defaults); i++)
1622 if (tmp_cache[i] != reg_cache[i])
Mark Brown8d50e442009-07-10 23:12:01 +01001623 snd_soc_write(codec, i, tmp_cache[i]);
Guennadi Liakhovetski40aa7032010-01-22 18:00:03 +01001624 kfree(tmp_cache);
Mark Brownf1c0a022008-08-26 13:05:27 +01001625 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001626 dev_err(codec->dev, "Failed to allocate temporary cache\n");
Mark Brownf1c0a022008-08-26 13:05:27 +01001627 }
1628
1629 return 0;
1630}
1631
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001632static int wm8903_probe(struct snd_soc_codec *codec)
Mark Brownf1c0a022008-08-26 13:05:27 +01001633{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001634 struct wm8903_platform_data *pdata = dev_get_platdata(codec->dev);
1635 struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
Mark Brown73b34ea2010-03-15 17:46:02 +00001636 int ret, i;
Mark Brown8abd16a2010-03-15 18:25:26 +00001637 int trigger, irq_pol;
Mark Brownf1c0a022008-08-26 13:05:27 +01001638 u16 val;
1639
Mark Brown8abd16a2010-03-15 18:25:26 +00001640 init_completion(&wm8903->wseq);
Mark Brownd58d5d52008-12-10 18:36:42 +00001641
Mark Brown8d50e442009-07-10 23:12:01 +01001642 ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_I2C);
1643 if (ret != 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001644 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
1645 return ret;
Mark Brown8d50e442009-07-10 23:12:01 +01001646 }
1647
1648 val = snd_soc_read(codec, WM8903_SW_RESET_AND_ID);
Mark Brownf1c0a022008-08-26 13:05:27 +01001649 if (val != wm8903_reg_defaults[WM8903_SW_RESET_AND_ID]) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001650 dev_err(codec->dev,
Mark Brownf1c0a022008-08-26 13:05:27 +01001651 "Device with ID register %x is not a WM8903\n", val);
1652 return -ENODEV;
1653 }
1654
Mark Brown8d50e442009-07-10 23:12:01 +01001655 val = snd_soc_read(codec, WM8903_REVISION_NUMBER);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001656 dev_info(codec->dev, "WM8903 revision %d\n",
Mark Brownf1c0a022008-08-26 13:05:27 +01001657 val & WM8903_CHIP_REV_MASK);
1658
1659 wm8903_reset(codec);
1660
Mark Brown37f88e82010-03-15 18:14:34 +00001661 /* Set up GPIOs and microphone detection */
Mark Brown73b34ea2010-03-15 17:46:02 +00001662 if (pdata) {
1663 for (i = 0; i < ARRAY_SIZE(pdata->gpio_cfg); i++) {
1664 if (!pdata->gpio_cfg[i])
1665 continue;
1666
1667 snd_soc_write(codec, WM8903_GPIO_CONTROL_1 + i,
1668 pdata->gpio_cfg[i] & 0xffff);
1669 }
Mark Brown37f88e82010-03-15 18:14:34 +00001670
1671 snd_soc_write(codec, WM8903_MIC_BIAS_CONTROL_0,
1672 pdata->micdet_cfg);
1673
1674 /* Microphone detection needs the WSEQ clock */
1675 if (pdata->micdet_cfg)
1676 snd_soc_update_bits(codec, WM8903_WRITE_SEQUENCER_0,
1677 WM8903_WSEQ_ENA, WM8903_WSEQ_ENA);
1678
1679 wm8903->mic_delay = pdata->micdet_delay;
Mark Brown73b34ea2010-03-15 17:46:02 +00001680 }
Mark Brown8abd16a2010-03-15 18:25:26 +00001681
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001682 if (wm8903->irq) {
Mark Brown8abd16a2010-03-15 18:25:26 +00001683 if (pdata && pdata->irq_active_low) {
1684 trigger = IRQF_TRIGGER_LOW;
1685 irq_pol = WM8903_IRQ_POL;
1686 } else {
1687 trigger = IRQF_TRIGGER_HIGH;
1688 irq_pol = 0;
1689 }
1690
1691 snd_soc_update_bits(codec, WM8903_INTERRUPT_CONTROL,
1692 WM8903_IRQ_POL, irq_pol);
1693
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001694 ret = request_threaded_irq(wm8903->irq, NULL, wm8903_irq,
Mark Brown8abd16a2010-03-15 18:25:26 +00001695 trigger | IRQF_ONESHOT,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001696 "wm8903", codec);
Mark Brown8abd16a2010-03-15 18:25:26 +00001697 if (ret != 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001698 dev_err(codec->dev, "Failed to request IRQ: %d\n",
Mark Brown8abd16a2010-03-15 18:25:26 +00001699 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001700 return ret;
Mark Brown8abd16a2010-03-15 18:25:26 +00001701 }
1702
1703 /* Enable write sequencer interrupts */
1704 snd_soc_update_bits(codec, WM8903_INTERRUPT_STATUS_1_MASK,
1705 WM8903_IM_WSEQ_BUSY_EINT, 0);
1706 }
Mark Brown73b34ea2010-03-15 17:46:02 +00001707
Mark Brownf1c0a022008-08-26 13:05:27 +01001708 /* power on device */
1709 wm8903_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1710
1711 /* Latch volume update bits */
Mark Brown8d50e442009-07-10 23:12:01 +01001712 val = snd_soc_read(codec, WM8903_ADC_DIGITAL_VOLUME_LEFT);
Mark Brownf1c0a022008-08-26 13:05:27 +01001713 val |= WM8903_ADCVU;
Mark Brown8d50e442009-07-10 23:12:01 +01001714 snd_soc_write(codec, WM8903_ADC_DIGITAL_VOLUME_LEFT, val);
1715 snd_soc_write(codec, WM8903_ADC_DIGITAL_VOLUME_RIGHT, val);
Mark Brownf1c0a022008-08-26 13:05:27 +01001716
Mark Brown8d50e442009-07-10 23:12:01 +01001717 val = snd_soc_read(codec, WM8903_DAC_DIGITAL_VOLUME_LEFT);
Mark Brownf1c0a022008-08-26 13:05:27 +01001718 val |= WM8903_DACVU;
Mark Brown8d50e442009-07-10 23:12:01 +01001719 snd_soc_write(codec, WM8903_DAC_DIGITAL_VOLUME_LEFT, val);
1720 snd_soc_write(codec, WM8903_DAC_DIGITAL_VOLUME_RIGHT, val);
Mark Brownf1c0a022008-08-26 13:05:27 +01001721
Mark Brown8d50e442009-07-10 23:12:01 +01001722 val = snd_soc_read(codec, WM8903_ANALOGUE_OUT1_LEFT);
Mark Brownf1c0a022008-08-26 13:05:27 +01001723 val |= WM8903_HPOUTVU;
Mark Brown8d50e442009-07-10 23:12:01 +01001724 snd_soc_write(codec, WM8903_ANALOGUE_OUT1_LEFT, val);
1725 snd_soc_write(codec, WM8903_ANALOGUE_OUT1_RIGHT, val);
Mark Brownf1c0a022008-08-26 13:05:27 +01001726
Mark Brown8d50e442009-07-10 23:12:01 +01001727 val = snd_soc_read(codec, WM8903_ANALOGUE_OUT2_LEFT);
Mark Brownf1c0a022008-08-26 13:05:27 +01001728 val |= WM8903_LINEOUTVU;
Mark Brown8d50e442009-07-10 23:12:01 +01001729 snd_soc_write(codec, WM8903_ANALOGUE_OUT2_LEFT, val);
1730 snd_soc_write(codec, WM8903_ANALOGUE_OUT2_RIGHT, val);
Mark Brownf1c0a022008-08-26 13:05:27 +01001731
Mark Brown8d50e442009-07-10 23:12:01 +01001732 val = snd_soc_read(codec, WM8903_ANALOGUE_OUT3_LEFT);
Mark Brownf1c0a022008-08-26 13:05:27 +01001733 val |= WM8903_SPKVU;
Mark Brown8d50e442009-07-10 23:12:01 +01001734 snd_soc_write(codec, WM8903_ANALOGUE_OUT3_LEFT, val);
1735 snd_soc_write(codec, WM8903_ANALOGUE_OUT3_RIGHT, val);
Mark Brownf1c0a022008-08-26 13:05:27 +01001736
1737 /* Enable DAC soft mute by default */
Mark Brown8d50e442009-07-10 23:12:01 +01001738 val = snd_soc_read(codec, WM8903_DAC_DIGITAL_1);
Mark Brownf1c0a022008-08-26 13:05:27 +01001739 val |= WM8903_DAC_MUTEMODE;
Mark Brown8d50e442009-07-10 23:12:01 +01001740 snd_soc_write(codec, WM8903_DAC_DIGITAL_1, val);
Mark Brownf1c0a022008-08-26 13:05:27 +01001741
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001742 snd_soc_add_controls(codec, wm8903_snd_controls,
1743 ARRAY_SIZE(wm8903_snd_controls));
1744 wm8903_add_widgets(codec);
Mark Brownf1c0a022008-08-26 13:05:27 +01001745
1746 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001747}
Mark Brownf1c0a022008-08-26 13:05:27 +01001748
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001749/* power down chip */
1750static int wm8903_remove(struct snd_soc_codec *codec)
1751{
1752 wm8903_set_bias_level(codec, SND_SOC_BIAS_OFF);
1753 return 0;
1754}
1755
1756static struct snd_soc_codec_driver soc_codec_dev_wm8903 = {
1757 .probe = wm8903_probe,
1758 .remove = wm8903_remove,
1759 .suspend = wm8903_suspend,
1760 .resume = wm8903_resume,
1761 .set_bias_level = wm8903_set_bias_level,
1762 .reg_cache_size = ARRAY_SIZE(wm8903_reg_defaults),
1763 .reg_word_size = sizeof(u16),
1764 .reg_cache_default = wm8903_reg_defaults,
1765 .volatile_register = wm8903_volatile_register,
1766};
1767
1768#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1769static __devinit int wm8903_i2c_probe(struct i2c_client *i2c,
1770 const struct i2c_device_id *id)
1771{
1772 struct wm8903_priv *wm8903;
1773 int ret;
1774
1775 wm8903 = kzalloc(sizeof(struct wm8903_priv), GFP_KERNEL);
1776 if (wm8903 == NULL)
1777 return -ENOMEM;
1778
1779 i2c_set_clientdata(i2c, wm8903);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001780 wm8903->irq = i2c->irq;
1781
1782 ret = snd_soc_register_codec(&i2c->dev,
1783 &soc_codec_dev_wm8903, &wm8903_dai, 1);
1784 if (ret < 0)
1785 kfree(wm8903);
Mark Brownf1c0a022008-08-26 13:05:27 +01001786 return ret;
1787}
1788
Mark Brownc6f29812009-02-18 21:25:40 +00001789static __devexit int wm8903_i2c_remove(struct i2c_client *client)
Mark Brownf1c0a022008-08-26 13:05:27 +01001790{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001791 snd_soc_unregister_codec(&client->dev);
1792 kfree(i2c_get_clientdata(client));
Mark Brownf1c0a022008-08-26 13:05:27 +01001793 return 0;
1794}
1795
Mark Brownf1c0a022008-08-26 13:05:27 +01001796static const struct i2c_device_id wm8903_i2c_id[] = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001797 { "wm8903", 0 },
1798 { }
Mark Brownf1c0a022008-08-26 13:05:27 +01001799};
1800MODULE_DEVICE_TABLE(i2c, wm8903_i2c_id);
1801
1802static struct i2c_driver wm8903_i2c_driver = {
1803 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001804 .name = "wm8903-codec",
Mark Brownf1c0a022008-08-26 13:05:27 +01001805 .owner = THIS_MODULE,
1806 },
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001807 .probe = wm8903_i2c_probe,
1808 .remove = __devexit_p(wm8903_i2c_remove),
Mark Brownf1c0a022008-08-26 13:05:27 +01001809 .id_table = wm8903_i2c_id,
1810};
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001811#endif
Mark Brownf1c0a022008-08-26 13:05:27 +01001812
Takashi Iwaic9b3a402008-12-10 07:47:22 +01001813static int __init wm8903_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +00001814{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001815 int ret = 0;
1816#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1817 ret = i2c_add_driver(&wm8903_i2c_driver);
1818 if (ret != 0) {
1819 printk(KERN_ERR "Failed to register wm8903 I2C driver: %d\n",
1820 ret);
1821 }
1822#endif
1823 return ret;
Mark Brown64089b82008-12-08 19:17:58 +00001824}
1825module_init(wm8903_modinit);
1826
1827static void __exit wm8903_exit(void)
1828{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001829#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Mark Brownd58d5d52008-12-10 18:36:42 +00001830 i2c_del_driver(&wm8903_i2c_driver);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001831#endif
Mark Brown64089b82008-12-08 19:17:58 +00001832}
1833module_exit(wm8903_exit);
1834
Mark Brownf1c0a022008-08-26 13:05:27 +01001835MODULE_DESCRIPTION("ASoC WM8903 driver");
1836MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.cm>");
1837MODULE_LICENSE("GPL");