blob: f9ae403715c623f7cd8a1e2759478ecd97b66839 [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
Mark Brownf2c1fe02010-12-10 19:17:07 +0000221 /* Reference count */
Mark Brownd58d5d52008-12-10 18:36:42 +0000222 int class_w_users;
Mark Brownd58d5d52008-12-10 18:36:42 +0000223
Mark Brown8abd16a2010-03-15 18:25:26 +0000224 struct completion wseq;
225
Mark Brown72453872010-03-15 21:22:58 +0000226 struct snd_soc_jack *mic_jack;
227 int mic_det;
228 int mic_short;
229 int mic_last_report;
230 int mic_delay;
Mark Brownd58d5d52008-12-10 18:36:42 +0000231};
232
Mark Brown8d50e442009-07-10 23:12:01 +0100233static int wm8903_volatile_register(unsigned int reg)
Mark Brownf1c0a022008-08-26 13:05:27 +0100234{
235 switch (reg) {
236 case WM8903_SW_RESET_AND_ID:
237 case WM8903_REVISION_NUMBER:
238 case WM8903_INTERRUPT_STATUS_1:
239 case WM8903_WRITE_SEQUENCER_4:
Mark Brown8d50e442009-07-10 23:12:01 +0100240 return 1;
Mark Brownf1c0a022008-08-26 13:05:27 +0100241
242 default:
Mark Brownf1c0a022008-08-26 13:05:27 +0100243 return 0;
Mark Brown8d50e442009-07-10 23:12:01 +0100244 }
Mark Brownf1c0a022008-08-26 13:05:27 +0100245}
246
247static int wm8903_run_sequence(struct snd_soc_codec *codec, unsigned int start)
248{
249 u16 reg[5];
Mark Brownb2c812e2010-04-14 15:35:19 +0900250 struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
Mark Brownf1c0a022008-08-26 13:05:27 +0100251
252 BUG_ON(start > 48);
253
Mark Brown37f88e82010-03-15 18:14:34 +0000254 /* Enable the sequencer if it's not already on */
Mark Brown8d50e442009-07-10 23:12:01 +0100255 reg[0] = snd_soc_read(codec, WM8903_WRITE_SEQUENCER_0);
Mark Brown37f88e82010-03-15 18:14:34 +0000256 snd_soc_write(codec, WM8903_WRITE_SEQUENCER_0,
257 reg[0] | WM8903_WSEQ_ENA);
Mark Brownf1c0a022008-08-26 13:05:27 +0100258
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000259 dev_dbg(codec->dev, "Starting sequence at %d\n", start);
Mark Brownf1c0a022008-08-26 13:05:27 +0100260
Mark Brown8d50e442009-07-10 23:12:01 +0100261 snd_soc_write(codec, WM8903_WRITE_SEQUENCER_3,
Mark Brownf1c0a022008-08-26 13:05:27 +0100262 start | WM8903_WSEQ_START);
263
264 /* Wait for it to complete. If we have the interrupt wired up then
Mark Brown8abd16a2010-03-15 18:25:26 +0000265 * that will break us out of the poll early.
Mark Brownf1c0a022008-08-26 13:05:27 +0100266 */
267 do {
Mark Brown8abd16a2010-03-15 18:25:26 +0000268 wait_for_completion_timeout(&wm8903->wseq,
269 msecs_to_jiffies(10));
Mark Brownf1c0a022008-08-26 13:05:27 +0100270
Mark Brown8d50e442009-07-10 23:12:01 +0100271 reg[4] = snd_soc_read(codec, WM8903_WRITE_SEQUENCER_4);
Mark Brownf1c0a022008-08-26 13:05:27 +0100272 } while (reg[4] & WM8903_WSEQ_BUSY);
273
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000274 dev_dbg(codec->dev, "Sequence complete\n");
Mark Brownf1c0a022008-08-26 13:05:27 +0100275
Mark Brown37f88e82010-03-15 18:14:34 +0000276 /* Disable the sequencer again if we enabled it */
277 snd_soc_write(codec, WM8903_WRITE_SEQUENCER_0, reg[0]);
Mark Brownf1c0a022008-08-26 13:05:27 +0100278
279 return 0;
280}
281
282static void wm8903_sync_reg_cache(struct snd_soc_codec *codec, u16 *cache)
283{
284 int i;
285
286 /* There really ought to be something better we can do here :/ */
287 for (i = 0; i < ARRAY_SIZE(wm8903_reg_defaults); i++)
Mark Brown8d50e442009-07-10 23:12:01 +0100288 cache[i] = codec->hw_read(codec, i);
Mark Brownf1c0a022008-08-26 13:05:27 +0100289}
290
291static void wm8903_reset(struct snd_soc_codec *codec)
292{
Mark Brown8d50e442009-07-10 23:12:01 +0100293 snd_soc_write(codec, WM8903_SW_RESET_AND_ID, 0);
Mark Brownd58d5d52008-12-10 18:36:42 +0000294 memcpy(codec->reg_cache, wm8903_reg_defaults,
295 sizeof(wm8903_reg_defaults));
Mark Brownf1c0a022008-08-26 13:05:27 +0100296}
297
298#define WM8903_OUTPUT_SHORT 0x8
299#define WM8903_OUTPUT_OUT 0x4
300#define WM8903_OUTPUT_INT 0x2
301#define WM8903_OUTPUT_IN 0x1
302
Mark Brown42768a12009-04-22 18:39:39 +0100303static int wm8903_cp_event(struct snd_soc_dapm_widget *w,
304 struct snd_kcontrol *kcontrol, int event)
305{
306 WARN_ON(event != SND_SOC_DAPM_POST_PMU);
307 mdelay(4);
308
309 return 0;
310}
311
Mark Brownf1c0a022008-08-26 13:05:27 +0100312/*
313 * Event for headphone and line out amplifier power changes. Special
314 * power up/down sequences are required in order to maximise pop/click
315 * performance.
316 */
317static int wm8903_output_event(struct snd_soc_dapm_widget *w,
318 struct snd_kcontrol *kcontrol, int event)
319{
320 struct snd_soc_codec *codec = w->codec;
Mark Brownf1c0a022008-08-26 13:05:27 +0100321 u16 val;
Takashi Iwai0bc286e2008-12-01 19:59:35 +0100322 u16 reg;
Mark Brownd7d5c542009-04-22 21:03:50 +0100323 u16 dcs_reg;
324 u16 dcs_bit;
Takashi Iwai0bc286e2008-12-01 19:59:35 +0100325 int shift;
Mark Brownf1c0a022008-08-26 13:05:27 +0100326
327 switch (w->reg) {
328 case WM8903_POWER_MANAGEMENT_2:
329 reg = WM8903_ANALOGUE_HP_0;
Mark Brownd7d5c542009-04-22 21:03:50 +0100330 dcs_bit = 0 + w->shift;
Mark Brownf1c0a022008-08-26 13:05:27 +0100331 break;
332 case WM8903_POWER_MANAGEMENT_3:
333 reg = WM8903_ANALOGUE_LINEOUT_0;
Mark Brownd7d5c542009-04-22 21:03:50 +0100334 dcs_bit = 2 + w->shift;
Mark Brownf1c0a022008-08-26 13:05:27 +0100335 break;
336 default:
337 BUG();
Mark Brown1e297a12008-12-10 11:08:33 +0000338 return -EINVAL; /* Spurious warning from some compilers */
Mark Brownf1c0a022008-08-26 13:05:27 +0100339 }
340
341 switch (w->shift) {
342 case 0:
343 shift = 0;
344 break;
345 case 1:
346 shift = 4;
347 break;
348 default:
349 BUG();
Mark Brown1e297a12008-12-10 11:08:33 +0000350 return -EINVAL; /* Spurious warning from some compilers */
Mark Brownf1c0a022008-08-26 13:05:27 +0100351 }
352
353 if (event & SND_SOC_DAPM_PRE_PMU) {
Mark Brown8d50e442009-07-10 23:12:01 +0100354 val = snd_soc_read(codec, reg);
Mark Brownf1c0a022008-08-26 13:05:27 +0100355
356 /* Short the output */
357 val &= ~(WM8903_OUTPUT_SHORT << shift);
Mark Brown8d50e442009-07-10 23:12:01 +0100358 snd_soc_write(codec, reg, val);
Mark Brownf1c0a022008-08-26 13:05:27 +0100359 }
360
361 if (event & SND_SOC_DAPM_POST_PMU) {
Mark Brown8d50e442009-07-10 23:12:01 +0100362 val = snd_soc_read(codec, reg);
Mark Brownf1c0a022008-08-26 13:05:27 +0100363
364 val |= (WM8903_OUTPUT_IN << shift);
Mark Brown8d50e442009-07-10 23:12:01 +0100365 snd_soc_write(codec, reg, val);
Mark Brownf1c0a022008-08-26 13:05:27 +0100366
367 val |= (WM8903_OUTPUT_INT << shift);
Mark Brown8d50e442009-07-10 23:12:01 +0100368 snd_soc_write(codec, reg, val);
Mark Brownf1c0a022008-08-26 13:05:27 +0100369
370 /* Turn on the output ENA_OUTP */
371 val |= (WM8903_OUTPUT_OUT << shift);
Mark Brown8d50e442009-07-10 23:12:01 +0100372 snd_soc_write(codec, reg, val);
Mark Brownf1c0a022008-08-26 13:05:27 +0100373
Mark Brownd7d5c542009-04-22 21:03:50 +0100374 /* Enable the DC servo */
Mark Brown8d50e442009-07-10 23:12:01 +0100375 dcs_reg = snd_soc_read(codec, WM8903_DC_SERVO_0);
Mark Brownd7d5c542009-04-22 21:03:50 +0100376 dcs_reg |= dcs_bit;
Mark Brown8d50e442009-07-10 23:12:01 +0100377 snd_soc_write(codec, WM8903_DC_SERVO_0, dcs_reg);
Mark Brownd7d5c542009-04-22 21:03:50 +0100378
Mark Brownf1c0a022008-08-26 13:05:27 +0100379 /* Remove the short */
380 val |= (WM8903_OUTPUT_SHORT << shift);
Mark Brown8d50e442009-07-10 23:12:01 +0100381 snd_soc_write(codec, reg, val);
Mark Brownf1c0a022008-08-26 13:05:27 +0100382 }
383
384 if (event & SND_SOC_DAPM_PRE_PMD) {
Mark Brown8d50e442009-07-10 23:12:01 +0100385 val = snd_soc_read(codec, reg);
Mark Brownf1c0a022008-08-26 13:05:27 +0100386
387 /* Short the output */
388 val &= ~(WM8903_OUTPUT_SHORT << shift);
Mark Brown8d50e442009-07-10 23:12:01 +0100389 snd_soc_write(codec, reg, val);
Mark Brownf1c0a022008-08-26 13:05:27 +0100390
Mark Brownd7d5c542009-04-22 21:03:50 +0100391 /* Disable the DC servo */
Mark Brown8d50e442009-07-10 23:12:01 +0100392 dcs_reg = snd_soc_read(codec, WM8903_DC_SERVO_0);
Mark Brownd7d5c542009-04-22 21:03:50 +0100393 dcs_reg &= ~dcs_bit;
Mark Brown8d50e442009-07-10 23:12:01 +0100394 snd_soc_write(codec, WM8903_DC_SERVO_0, dcs_reg);
Mark Brownd7d5c542009-04-22 21:03:50 +0100395
Mark Brownf1c0a022008-08-26 13:05:27 +0100396 /* Then disable the intermediate and output stages */
397 val &= ~((WM8903_OUTPUT_OUT | WM8903_OUTPUT_INT |
398 WM8903_OUTPUT_IN) << shift);
Mark Brown8d50e442009-07-10 23:12:01 +0100399 snd_soc_write(codec, reg, val);
Mark Brownf1c0a022008-08-26 13:05:27 +0100400 }
401
Mark Brownf1c0a022008-08-26 13:05:27 +0100402 return 0;
403}
404
405/*
406 * When used with DAC outputs only the WM8903 charge pump supports
407 * operation in class W mode, providing very low power consumption
408 * when used with digital sources. Enable and disable this mode
409 * automatically depending on the mixer configuration.
410 *
411 * All the relevant controls are simple switches.
412 */
413static int wm8903_class_w_put(struct snd_kcontrol *kcontrol,
414 struct snd_ctl_elem_value *ucontrol)
415{
416 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
417 struct snd_soc_codec *codec = widget->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900418 struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
Mark Brownf1c0a022008-08-26 13:05:27 +0100419 u16 reg;
420 int ret;
421
Mark Brown8d50e442009-07-10 23:12:01 +0100422 reg = snd_soc_read(codec, WM8903_CLASS_W_0);
Mark Brownf1c0a022008-08-26 13:05:27 +0100423
424 /* Turn it off if we're about to enable bypass */
425 if (ucontrol->value.integer.value[0]) {
426 if (wm8903->class_w_users == 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000427 dev_dbg(codec->dev, "Disabling Class W\n");
Mark Brown8d50e442009-07-10 23:12:01 +0100428 snd_soc_write(codec, WM8903_CLASS_W_0, reg &
Mark Brownf1c0a022008-08-26 13:05:27 +0100429 ~(WM8903_CP_DYN_FREQ | WM8903_CP_DYN_V));
430 }
431 wm8903->class_w_users++;
432 }
433
434 /* Implement the change */
435 ret = snd_soc_dapm_put_volsw(kcontrol, ucontrol);
436
437 /* If we've just disabled the last bypass path turn Class W on */
438 if (!ucontrol->value.integer.value[0]) {
439 if (wm8903->class_w_users == 1) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000440 dev_dbg(codec->dev, "Enabling Class W\n");
Mark Brown8d50e442009-07-10 23:12:01 +0100441 snd_soc_write(codec, WM8903_CLASS_W_0, reg |
Mark Brownf1c0a022008-08-26 13:05:27 +0100442 WM8903_CP_DYN_FREQ | WM8903_CP_DYN_V);
443 }
444 wm8903->class_w_users--;
445 }
446
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000447 dev_dbg(codec->dev, "Bypass use count now %d\n",
Mark Brownf1c0a022008-08-26 13:05:27 +0100448 wm8903->class_w_users);
449
450 return ret;
451}
452
453#define SOC_DAPM_SINGLE_W(xname, reg, shift, max, invert) \
454{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
455 .info = snd_soc_info_volsw, \
456 .get = snd_soc_dapm_get_volsw, .put = wm8903_class_w_put, \
457 .private_value = SOC_SINGLE_VALUE(reg, shift, max, invert) }
458
459
460/* ALSA can only do steps of .01dB */
461static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
462
Mark Brown291ce182009-04-22 21:36:14 +0100463static const DECLARE_TLV_DB_SCALE(digital_sidetone_tlv, -3600, 300, 0);
Mark Brownf1c0a022008-08-26 13:05:27 +0100464static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
465
466static const DECLARE_TLV_DB_SCALE(drc_tlv_thresh, 0, 75, 0);
467static const DECLARE_TLV_DB_SCALE(drc_tlv_amp, -2250, 75, 0);
468static const DECLARE_TLV_DB_SCALE(drc_tlv_min, 0, 600, 0);
469static const DECLARE_TLV_DB_SCALE(drc_tlv_max, 1200, 600, 0);
470static const DECLARE_TLV_DB_SCALE(drc_tlv_startup, -300, 50, 0);
471
Mark Brown460f4aa2010-12-10 18:42:58 +0000472static const char *hpf_mode_text[] = {
473 "Hi-fi", "Voice 1", "Voice 2", "Voice 3"
474};
475
476static const struct soc_enum hpf_mode =
477 SOC_ENUM_SINGLE(WM8903_ADC_DIGITAL_0, 5, 4, hpf_mode_text);
478
Mark Browndcf9ada2010-12-10 19:17:06 +0000479static const char *osr_text[] = {
480 "Low power", "High performance"
481};
482
483static const struct soc_enum adc_osr =
484 SOC_ENUM_SINGLE(WM8903_ANALOGUE_ADC_0, 0, 2, osr_text);
485
486static const struct soc_enum dac_osr =
487 SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_1, 0, 2, osr_text);
488
Mark Brownf1c0a022008-08-26 13:05:27 +0100489static const char *drc_slope_text[] = {
490 "1", "1/2", "1/4", "1/8", "1/16", "0"
491};
492
493static const struct soc_enum drc_slope_r0 =
494 SOC_ENUM_SINGLE(WM8903_DRC_2, 3, 6, drc_slope_text);
495
496static const struct soc_enum drc_slope_r1 =
497 SOC_ENUM_SINGLE(WM8903_DRC_2, 0, 6, drc_slope_text);
498
499static const char *drc_attack_text[] = {
500 "instantaneous",
501 "363us", "762us", "1.45ms", "2.9ms", "5.8ms", "11.6ms", "23.2ms",
502 "46.4ms", "92.8ms", "185.6ms"
503};
504
505static const struct soc_enum drc_attack =
506 SOC_ENUM_SINGLE(WM8903_DRC_1, 12, 11, drc_attack_text);
507
508static const char *drc_decay_text[] = {
509 "186ms", "372ms", "743ms", "1.49s", "2.97s", "5.94s", "11.89s",
510 "23.87s", "47.56s"
511};
512
513static const struct soc_enum drc_decay =
514 SOC_ENUM_SINGLE(WM8903_DRC_1, 8, 9, drc_decay_text);
515
516static const char *drc_ff_delay_text[] = {
517 "5 samples", "9 samples"
518};
519
520static const struct soc_enum drc_ff_delay =
521 SOC_ENUM_SINGLE(WM8903_DRC_0, 5, 2, drc_ff_delay_text);
522
523static const char *drc_qr_decay_text[] = {
524 "0.725ms", "1.45ms", "5.8ms"
525};
526
527static const struct soc_enum drc_qr_decay =
528 SOC_ENUM_SINGLE(WM8903_DRC_1, 4, 3, drc_qr_decay_text);
529
530static const char *drc_smoothing_text[] = {
531 "Low", "Medium", "High"
532};
533
534static const struct soc_enum drc_smoothing =
535 SOC_ENUM_SINGLE(WM8903_DRC_0, 11, 3, drc_smoothing_text);
536
537static const char *soft_mute_text[] = {
538 "Fast (fs/2)", "Slow (fs/32)"
539};
540
541static const struct soc_enum soft_mute =
542 SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_1, 10, 2, soft_mute_text);
543
544static const char *mute_mode_text[] = {
545 "Hard", "Soft"
546};
547
548static const struct soc_enum mute_mode =
549 SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_1, 9, 2, mute_mode_text);
550
551static const char *dac_deemphasis_text[] = {
552 "Disabled", "32kHz", "44.1kHz", "48kHz"
553};
554
555static const struct soc_enum dac_deemphasis =
556 SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_1, 1, 4, dac_deemphasis_text);
557
558static const char *companding_text[] = {
559 "ulaw", "alaw"
560};
561
562static const struct soc_enum dac_companding =
563 SOC_ENUM_SINGLE(WM8903_AUDIO_INTERFACE_0, 0, 2, companding_text);
564
565static const struct soc_enum adc_companding =
566 SOC_ENUM_SINGLE(WM8903_AUDIO_INTERFACE_0, 2, 2, companding_text);
567
568static const char *input_mode_text[] = {
569 "Single-Ended", "Differential Line", "Differential Mic"
570};
571
572static const struct soc_enum linput_mode_enum =
573 SOC_ENUM_SINGLE(WM8903_ANALOGUE_LEFT_INPUT_1, 0, 3, input_mode_text);
574
575static const struct soc_enum rinput_mode_enum =
576 SOC_ENUM_SINGLE(WM8903_ANALOGUE_RIGHT_INPUT_1, 0, 3, input_mode_text);
577
578static const char *linput_mux_text[] = {
579 "IN1L", "IN2L", "IN3L"
580};
581
582static const struct soc_enum linput_enum =
583 SOC_ENUM_SINGLE(WM8903_ANALOGUE_LEFT_INPUT_1, 2, 3, linput_mux_text);
584
585static const struct soc_enum linput_inv_enum =
586 SOC_ENUM_SINGLE(WM8903_ANALOGUE_LEFT_INPUT_1, 4, 3, linput_mux_text);
587
588static const char *rinput_mux_text[] = {
589 "IN1R", "IN2R", "IN3R"
590};
591
592static const struct soc_enum rinput_enum =
593 SOC_ENUM_SINGLE(WM8903_ANALOGUE_RIGHT_INPUT_1, 2, 3, rinput_mux_text);
594
595static const struct soc_enum rinput_inv_enum =
596 SOC_ENUM_SINGLE(WM8903_ANALOGUE_RIGHT_INPUT_1, 4, 3, rinput_mux_text);
597
598
Mark Brown291ce182009-04-22 21:36:14 +0100599static const char *sidetone_text[] = {
600 "None", "Left", "Right"
601};
602
603static const struct soc_enum lsidetone_enum =
604 SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_0, 2, 3, sidetone_text);
605
606static const struct soc_enum rsidetone_enum =
607 SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_0, 0, 3, sidetone_text);
608
Mark Brownf1c0a022008-08-26 13:05:27 +0100609static const struct snd_kcontrol_new wm8903_snd_controls[] = {
610
611/* Input PGAs - No TLV since the scale depends on PGA mode */
612SOC_SINGLE("Left Input PGA Switch", WM8903_ANALOGUE_LEFT_INPUT_0,
Mark Brown57159522008-09-24 10:47:02 +0100613 7, 1, 1),
Mark Brownf1c0a022008-08-26 13:05:27 +0100614SOC_SINGLE("Left Input PGA Volume", WM8903_ANALOGUE_LEFT_INPUT_0,
615 0, 31, 0),
616SOC_SINGLE("Left Input PGA Common Mode Switch", WM8903_ANALOGUE_LEFT_INPUT_1,
617 6, 1, 0),
618
619SOC_SINGLE("Right Input PGA Switch", WM8903_ANALOGUE_RIGHT_INPUT_0,
Mark Brown57159522008-09-24 10:47:02 +0100620 7, 1, 1),
Mark Brownf1c0a022008-08-26 13:05:27 +0100621SOC_SINGLE("Right Input PGA Volume", WM8903_ANALOGUE_RIGHT_INPUT_0,
622 0, 31, 0),
623SOC_SINGLE("Right Input PGA Common Mode Switch", WM8903_ANALOGUE_RIGHT_INPUT_1,
624 6, 1, 0),
625
626/* ADCs */
Mark Browndcf9ada2010-12-10 19:17:06 +0000627SOC_ENUM("ADC OSR", adc_osr),
Mark Brown460f4aa2010-12-10 18:42:58 +0000628SOC_SINGLE("HPF Switch", WM8903_ADC_DIGITAL_0, 4, 1, 0),
629SOC_ENUM("HPF Mode", hpf_mode),
Mark Brownf1c0a022008-08-26 13:05:27 +0100630SOC_SINGLE("DRC Switch", WM8903_DRC_0, 15, 1, 0),
631SOC_ENUM("DRC Compressor Slope R0", drc_slope_r0),
632SOC_ENUM("DRC Compressor Slope R1", drc_slope_r1),
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200633SOC_SINGLE_TLV("DRC Compressor Threshold Volume", WM8903_DRC_3, 5, 124, 1,
Mark Brownf1c0a022008-08-26 13:05:27 +0100634 drc_tlv_thresh),
635SOC_SINGLE_TLV("DRC Volume", WM8903_DRC_3, 0, 30, 1, drc_tlv_amp),
636SOC_SINGLE_TLV("DRC Minimum Gain Volume", WM8903_DRC_1, 2, 3, 1, drc_tlv_min),
637SOC_SINGLE_TLV("DRC Maximum Gain Volume", WM8903_DRC_1, 0, 3, 0, drc_tlv_max),
638SOC_ENUM("DRC Attack Rate", drc_attack),
639SOC_ENUM("DRC Decay Rate", drc_decay),
640SOC_ENUM("DRC FF Delay", drc_ff_delay),
641SOC_SINGLE("DRC Anticlip Switch", WM8903_DRC_0, 1, 1, 0),
642SOC_SINGLE("DRC QR Switch", WM8903_DRC_0, 2, 1, 0),
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200643SOC_SINGLE_TLV("DRC QR Threshold Volume", WM8903_DRC_0, 6, 3, 0, drc_tlv_max),
Mark Brownf1c0a022008-08-26 13:05:27 +0100644SOC_ENUM("DRC QR Decay Rate", drc_qr_decay),
645SOC_SINGLE("DRC Smoothing Switch", WM8903_DRC_0, 3, 1, 0),
646SOC_SINGLE("DRC Smoothing Hysteresis Switch", WM8903_DRC_0, 0, 1, 0),
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200647SOC_ENUM("DRC Smoothing Threshold", drc_smoothing),
Mark Brownf1c0a022008-08-26 13:05:27 +0100648SOC_SINGLE_TLV("DRC Startup Volume", WM8903_DRC_0, 6, 18, 0, drc_tlv_startup),
649
650SOC_DOUBLE_R_TLV("Digital Capture Volume", WM8903_ADC_DIGITAL_VOLUME_LEFT,
651 WM8903_ADC_DIGITAL_VOLUME_RIGHT, 1, 96, 0, digital_tlv),
652SOC_ENUM("ADC Companding Mode", adc_companding),
653SOC_SINGLE("ADC Companding Switch", WM8903_AUDIO_INTERFACE_0, 3, 1, 0),
654
Mark Brown291ce182009-04-22 21:36:14 +0100655SOC_DOUBLE_TLV("Digital Sidetone Volume", WM8903_DAC_DIGITAL_0, 4, 8,
656 12, 0, digital_sidetone_tlv),
657
Mark Brownf1c0a022008-08-26 13:05:27 +0100658/* DAC */
Mark Browndcf9ada2010-12-10 19:17:06 +0000659SOC_ENUM("DAC OSR", dac_osr),
Mark Brownf1c0a022008-08-26 13:05:27 +0100660SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8903_DAC_DIGITAL_VOLUME_LEFT,
661 WM8903_DAC_DIGITAL_VOLUME_RIGHT, 1, 120, 0, digital_tlv),
662SOC_ENUM("DAC Soft Mute Rate", soft_mute),
663SOC_ENUM("DAC Mute Mode", mute_mode),
664SOC_SINGLE("DAC Mono Switch", WM8903_DAC_DIGITAL_1, 12, 1, 0),
665SOC_ENUM("DAC De-emphasis", dac_deemphasis),
Mark Brownf1c0a022008-08-26 13:05:27 +0100666SOC_ENUM("DAC Companding Mode", dac_companding),
667SOC_SINGLE("DAC Companding Switch", WM8903_AUDIO_INTERFACE_0, 1, 1, 0),
668
669/* Headphones */
670SOC_DOUBLE_R("Headphone Switch",
671 WM8903_ANALOGUE_OUT1_LEFT, WM8903_ANALOGUE_OUT1_RIGHT,
672 8, 1, 1),
673SOC_DOUBLE_R("Headphone ZC Switch",
674 WM8903_ANALOGUE_OUT1_LEFT, WM8903_ANALOGUE_OUT1_RIGHT,
675 6, 1, 0),
676SOC_DOUBLE_R_TLV("Headphone Volume",
677 WM8903_ANALOGUE_OUT1_LEFT, WM8903_ANALOGUE_OUT1_RIGHT,
678 0, 63, 0, out_tlv),
679
680/* Line out */
681SOC_DOUBLE_R("Line Out Switch",
682 WM8903_ANALOGUE_OUT2_LEFT, WM8903_ANALOGUE_OUT2_RIGHT,
683 8, 1, 1),
684SOC_DOUBLE_R("Line Out ZC Switch",
685 WM8903_ANALOGUE_OUT2_LEFT, WM8903_ANALOGUE_OUT2_RIGHT,
686 6, 1, 0),
687SOC_DOUBLE_R_TLV("Line Out Volume",
688 WM8903_ANALOGUE_OUT2_LEFT, WM8903_ANALOGUE_OUT2_RIGHT,
689 0, 63, 0, out_tlv),
690
691/* Speaker */
692SOC_DOUBLE_R("Speaker Switch",
693 WM8903_ANALOGUE_OUT3_LEFT, WM8903_ANALOGUE_OUT3_RIGHT, 8, 1, 1),
694SOC_DOUBLE_R("Speaker ZC Switch",
695 WM8903_ANALOGUE_OUT3_LEFT, WM8903_ANALOGUE_OUT3_RIGHT, 6, 1, 0),
696SOC_DOUBLE_R_TLV("Speaker Volume",
697 WM8903_ANALOGUE_OUT3_LEFT, WM8903_ANALOGUE_OUT3_RIGHT,
698 0, 63, 0, out_tlv),
699};
700
Mark Brownf1c0a022008-08-26 13:05:27 +0100701static const struct snd_kcontrol_new linput_mode_mux =
702 SOC_DAPM_ENUM("Left Input Mode Mux", linput_mode_enum);
703
704static const struct snd_kcontrol_new rinput_mode_mux =
705 SOC_DAPM_ENUM("Right Input Mode Mux", rinput_mode_enum);
706
707static const struct snd_kcontrol_new linput_mux =
708 SOC_DAPM_ENUM("Left Input Mux", linput_enum);
709
710static const struct snd_kcontrol_new linput_inv_mux =
711 SOC_DAPM_ENUM("Left Inverting Input Mux", linput_inv_enum);
712
713static const struct snd_kcontrol_new rinput_mux =
714 SOC_DAPM_ENUM("Right Input Mux", rinput_enum);
715
716static const struct snd_kcontrol_new rinput_inv_mux =
717 SOC_DAPM_ENUM("Right Inverting Input Mux", rinput_inv_enum);
718
Mark Brown291ce182009-04-22 21:36:14 +0100719static const struct snd_kcontrol_new lsidetone_mux =
720 SOC_DAPM_ENUM("DACL Sidetone Mux", lsidetone_enum);
721
722static const struct snd_kcontrol_new rsidetone_mux =
723 SOC_DAPM_ENUM("DACR Sidetone Mux", rsidetone_enum);
724
Mark Brownf1c0a022008-08-26 13:05:27 +0100725static const struct snd_kcontrol_new left_output_mixer[] = {
726SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_LEFT_MIX_0, 3, 1, 0),
727SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_LEFT_MIX_0, 2, 1, 0),
728SOC_DAPM_SINGLE_W("Left Bypass Switch", WM8903_ANALOGUE_LEFT_MIX_0, 1, 1, 0),
Mark Brown4b4fffd2008-12-03 11:21:08 +0000729SOC_DAPM_SINGLE_W("Right Bypass Switch", WM8903_ANALOGUE_LEFT_MIX_0, 0, 1, 0),
Mark Brownf1c0a022008-08-26 13:05:27 +0100730};
731
732static const struct snd_kcontrol_new right_output_mixer[] = {
733SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 3, 1, 0),
734SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 2, 1, 0),
735SOC_DAPM_SINGLE_W("Left Bypass Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 1, 1, 0),
Mark Brown4b4fffd2008-12-03 11:21:08 +0000736SOC_DAPM_SINGLE_W("Right Bypass Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 0, 1, 0),
Mark Brownf1c0a022008-08-26 13:05:27 +0100737};
738
739static const struct snd_kcontrol_new left_speaker_mixer[] = {
740SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0, 3, 1, 0),
741SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0, 2, 1, 0),
742SOC_DAPM_SINGLE("Left Bypass Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0, 1, 1, 0),
743SOC_DAPM_SINGLE("Right Bypass Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0,
Mark Brown4b4fffd2008-12-03 11:21:08 +0000744 0, 1, 0),
Mark Brownf1c0a022008-08-26 13:05:27 +0100745};
746
747static const struct snd_kcontrol_new right_speaker_mixer[] = {
748SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0, 3, 1, 0),
749SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0, 2, 1, 0),
750SOC_DAPM_SINGLE("Left Bypass Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0,
751 1, 1, 0),
752SOC_DAPM_SINGLE("Right Bypass Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0,
Mark Brown4b4fffd2008-12-03 11:21:08 +0000753 0, 1, 0),
Mark Brownf1c0a022008-08-26 13:05:27 +0100754};
755
756static const struct snd_soc_dapm_widget wm8903_dapm_widgets[] = {
757SND_SOC_DAPM_INPUT("IN1L"),
758SND_SOC_DAPM_INPUT("IN1R"),
759SND_SOC_DAPM_INPUT("IN2L"),
760SND_SOC_DAPM_INPUT("IN2R"),
761SND_SOC_DAPM_INPUT("IN3L"),
762SND_SOC_DAPM_INPUT("IN3R"),
763
764SND_SOC_DAPM_OUTPUT("HPOUTL"),
765SND_SOC_DAPM_OUTPUT("HPOUTR"),
766SND_SOC_DAPM_OUTPUT("LINEOUTL"),
767SND_SOC_DAPM_OUTPUT("LINEOUTR"),
768SND_SOC_DAPM_OUTPUT("LOP"),
769SND_SOC_DAPM_OUTPUT("LON"),
770SND_SOC_DAPM_OUTPUT("ROP"),
771SND_SOC_DAPM_OUTPUT("RON"),
772
773SND_SOC_DAPM_MICBIAS("Mic Bias", WM8903_MIC_BIAS_CONTROL_0, 0, 0),
774
775SND_SOC_DAPM_MUX("Left Input Mux", SND_SOC_NOPM, 0, 0, &linput_mux),
776SND_SOC_DAPM_MUX("Left Input Inverting Mux", SND_SOC_NOPM, 0, 0,
777 &linput_inv_mux),
778SND_SOC_DAPM_MUX("Left Input Mode Mux", SND_SOC_NOPM, 0, 0, &linput_mode_mux),
779
780SND_SOC_DAPM_MUX("Right Input Mux", SND_SOC_NOPM, 0, 0, &rinput_mux),
781SND_SOC_DAPM_MUX("Right Input Inverting Mux", SND_SOC_NOPM, 0, 0,
782 &rinput_inv_mux),
783SND_SOC_DAPM_MUX("Right Input Mode Mux", SND_SOC_NOPM, 0, 0, &rinput_mode_mux),
784
785SND_SOC_DAPM_PGA("Left Input PGA", WM8903_POWER_MANAGEMENT_0, 1, 0, NULL, 0),
786SND_SOC_DAPM_PGA("Right Input PGA", WM8903_POWER_MANAGEMENT_0, 0, 0, NULL, 0),
787
788SND_SOC_DAPM_ADC("ADCL", "Left HiFi Capture", WM8903_POWER_MANAGEMENT_6, 1, 0),
789SND_SOC_DAPM_ADC("ADCR", "Right HiFi Capture", WM8903_POWER_MANAGEMENT_6, 0, 0),
790
Mark Brown291ce182009-04-22 21:36:14 +0100791SND_SOC_DAPM_MUX("DACL Sidetone", SND_SOC_NOPM, 0, 0, &lsidetone_mux),
792SND_SOC_DAPM_MUX("DACR Sidetone", SND_SOC_NOPM, 0, 0, &rsidetone_mux),
793
Mark Brownf1c0a022008-08-26 13:05:27 +0100794SND_SOC_DAPM_DAC("DACL", "Left Playback", WM8903_POWER_MANAGEMENT_6, 3, 0),
795SND_SOC_DAPM_DAC("DACR", "Right Playback", WM8903_POWER_MANAGEMENT_6, 2, 0),
796
797SND_SOC_DAPM_MIXER("Left Output Mixer", WM8903_POWER_MANAGEMENT_1, 1, 0,
798 left_output_mixer, ARRAY_SIZE(left_output_mixer)),
799SND_SOC_DAPM_MIXER("Right Output Mixer", WM8903_POWER_MANAGEMENT_1, 0, 0,
800 right_output_mixer, ARRAY_SIZE(right_output_mixer)),
801
802SND_SOC_DAPM_MIXER("Left Speaker Mixer", WM8903_POWER_MANAGEMENT_4, 1, 0,
803 left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer)),
804SND_SOC_DAPM_MIXER("Right Speaker Mixer", WM8903_POWER_MANAGEMENT_4, 0, 0,
805 right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer)),
806
807SND_SOC_DAPM_PGA_E("Left Headphone Output PGA", WM8903_POWER_MANAGEMENT_2,
808 1, 0, NULL, 0, wm8903_output_event,
809 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
Mark Brown42768a12009-04-22 18:39:39 +0100810 SND_SOC_DAPM_PRE_PMD),
Mark Brownf1c0a022008-08-26 13:05:27 +0100811SND_SOC_DAPM_PGA_E("Right Headphone Output PGA", WM8903_POWER_MANAGEMENT_2,
812 0, 0, NULL, 0, wm8903_output_event,
813 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
Mark Brown42768a12009-04-22 18:39:39 +0100814 SND_SOC_DAPM_PRE_PMD),
Mark Brownf1c0a022008-08-26 13:05:27 +0100815
816SND_SOC_DAPM_PGA_E("Left Line Output PGA", WM8903_POWER_MANAGEMENT_3, 1, 0,
817 NULL, 0, wm8903_output_event,
818 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
Mark Brown42768a12009-04-22 18:39:39 +0100819 SND_SOC_DAPM_PRE_PMD),
Mark Brownf1c0a022008-08-26 13:05:27 +0100820SND_SOC_DAPM_PGA_E("Right Line Output PGA", WM8903_POWER_MANAGEMENT_3, 0, 0,
821 NULL, 0, wm8903_output_event,
822 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
Mark Brown42768a12009-04-22 18:39:39 +0100823 SND_SOC_DAPM_PRE_PMD),
Mark Brownf1c0a022008-08-26 13:05:27 +0100824
825SND_SOC_DAPM_PGA("Left Speaker PGA", WM8903_POWER_MANAGEMENT_5, 1, 0,
826 NULL, 0),
827SND_SOC_DAPM_PGA("Right Speaker PGA", WM8903_POWER_MANAGEMENT_5, 0, 0,
828 NULL, 0),
829
Mark Brown42768a12009-04-22 18:39:39 +0100830SND_SOC_DAPM_SUPPLY("Charge Pump", WM8903_CHARGE_PUMP_0, 0, 0,
831 wm8903_cp_event, SND_SOC_DAPM_POST_PMU),
Mark Brownc2aef4f2009-04-22 20:04:44 +0100832SND_SOC_DAPM_SUPPLY("CLK_DSP", WM8903_CLOCK_RATES_2, 1, 0, NULL, 0),
Mark Brownf1c0a022008-08-26 13:05:27 +0100833};
834
835static const struct snd_soc_dapm_route intercon[] = {
836
837 { "Left Input Mux", "IN1L", "IN1L" },
838 { "Left Input Mux", "IN2L", "IN2L" },
839 { "Left Input Mux", "IN3L", "IN3L" },
840
841 { "Left Input Inverting Mux", "IN1L", "IN1L" },
842 { "Left Input Inverting Mux", "IN2L", "IN2L" },
843 { "Left Input Inverting Mux", "IN3L", "IN3L" },
844
845 { "Right Input Mux", "IN1R", "IN1R" },
846 { "Right Input Mux", "IN2R", "IN2R" },
847 { "Right Input Mux", "IN3R", "IN3R" },
848
849 { "Right Input Inverting Mux", "IN1R", "IN1R" },
850 { "Right Input Inverting Mux", "IN2R", "IN2R" },
851 { "Right Input Inverting Mux", "IN3R", "IN3R" },
852
853 { "Left Input Mode Mux", "Single-Ended", "Left Input Inverting Mux" },
854 { "Left Input Mode Mux", "Differential Line",
855 "Left Input Mux" },
856 { "Left Input Mode Mux", "Differential Line",
857 "Left Input Inverting Mux" },
858 { "Left Input Mode Mux", "Differential Mic",
859 "Left Input Mux" },
860 { "Left Input Mode Mux", "Differential Mic",
861 "Left Input Inverting Mux" },
862
863 { "Right Input Mode Mux", "Single-Ended",
864 "Right Input Inverting Mux" },
865 { "Right Input Mode Mux", "Differential Line",
866 "Right Input Mux" },
867 { "Right Input Mode Mux", "Differential Line",
868 "Right Input Inverting Mux" },
869 { "Right Input Mode Mux", "Differential Mic",
870 "Right Input Mux" },
871 { "Right Input Mode Mux", "Differential Mic",
872 "Right Input Inverting Mux" },
873
874 { "Left Input PGA", NULL, "Left Input Mode Mux" },
875 { "Right Input PGA", NULL, "Right Input Mode Mux" },
876
877 { "ADCL", NULL, "Left Input PGA" },
Mark Brownc2aef4f2009-04-22 20:04:44 +0100878 { "ADCL", NULL, "CLK_DSP" },
Mark Brownf1c0a022008-08-26 13:05:27 +0100879 { "ADCR", NULL, "Right Input PGA" },
Mark Brownc2aef4f2009-04-22 20:04:44 +0100880 { "ADCR", NULL, "CLK_DSP" },
881
Mark Brown291ce182009-04-22 21:36:14 +0100882 { "DACL Sidetone", "Left", "ADCL" },
883 { "DACL Sidetone", "Right", "ADCR" },
884 { "DACR Sidetone", "Left", "ADCL" },
885 { "DACR Sidetone", "Right", "ADCR" },
886
887 { "DACL", NULL, "DACL Sidetone" },
Mark Brownc2aef4f2009-04-22 20:04:44 +0100888 { "DACL", NULL, "CLK_DSP" },
Mark Brown291ce182009-04-22 21:36:14 +0100889 { "DACR", NULL, "DACR Sidetone" },
Mark Brownc2aef4f2009-04-22 20:04:44 +0100890 { "DACR", NULL, "CLK_DSP" },
Mark Brownf1c0a022008-08-26 13:05:27 +0100891
892 { "Left Output Mixer", "Left Bypass Switch", "Left Input PGA" },
893 { "Left Output Mixer", "Right Bypass Switch", "Right Input PGA" },
894 { "Left Output Mixer", "DACL Switch", "DACL" },
895 { "Left Output Mixer", "DACR Switch", "DACR" },
896
897 { "Right Output Mixer", "Left Bypass Switch", "Left Input PGA" },
898 { "Right Output Mixer", "Right Bypass Switch", "Right Input PGA" },
899 { "Right Output Mixer", "DACL Switch", "DACL" },
900 { "Right Output Mixer", "DACR Switch", "DACR" },
901
902 { "Left Speaker Mixer", "Left Bypass Switch", "Left Input PGA" },
903 { "Left Speaker Mixer", "Right Bypass Switch", "Right Input PGA" },
904 { "Left Speaker Mixer", "DACL Switch", "DACL" },
905 { "Left Speaker Mixer", "DACR Switch", "DACR" },
906
907 { "Right Speaker Mixer", "Left Bypass Switch", "Left Input PGA" },
908 { "Right Speaker Mixer", "Right Bypass Switch", "Right Input PGA" },
909 { "Right Speaker Mixer", "DACL Switch", "DACL" },
910 { "Right Speaker Mixer", "DACR Switch", "DACR" },
911
912 { "Left Line Output PGA", NULL, "Left Output Mixer" },
913 { "Right Line Output PGA", NULL, "Right Output Mixer" },
914
915 { "Left Headphone Output PGA", NULL, "Left Output Mixer" },
916 { "Right Headphone Output PGA", NULL, "Right Output Mixer" },
917
918 { "Left Speaker PGA", NULL, "Left Speaker Mixer" },
919 { "Right Speaker PGA", NULL, "Right Speaker Mixer" },
920
921 { "HPOUTL", NULL, "Left Headphone Output PGA" },
922 { "HPOUTR", NULL, "Right Headphone Output PGA" },
923
924 { "LINEOUTL", NULL, "Left Line Output PGA" },
925 { "LINEOUTR", NULL, "Right Line Output PGA" },
926
927 { "LOP", NULL, "Left Speaker PGA" },
928 { "LON", NULL, "Left Speaker PGA" },
929
930 { "ROP", NULL, "Right Speaker PGA" },
931 { "RON", NULL, "Right Speaker PGA" },
Mark Brown42768a12009-04-22 18:39:39 +0100932
933 { "Left Headphone Output PGA", NULL, "Charge Pump" },
934 { "Right Headphone Output PGA", NULL, "Charge Pump" },
935 { "Left Line Output PGA", NULL, "Charge Pump" },
936 { "Right Line Output PGA", NULL, "Charge Pump" },
Mark Brownf1c0a022008-08-26 13:05:27 +0100937};
938
939static int wm8903_add_widgets(struct snd_soc_codec *codec)
940{
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200941 struct snd_soc_dapm_context *dapm = &codec->dapm;
Mark Brownf1c0a022008-08-26 13:05:27 +0100942
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200943 snd_soc_dapm_new_controls(dapm, wm8903_dapm_widgets,
944 ARRAY_SIZE(wm8903_dapm_widgets));
945 snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
Mark Brownf1c0a022008-08-26 13:05:27 +0100946
Mark Brownf1c0a022008-08-26 13:05:27 +0100947 return 0;
948}
949
950static int wm8903_set_bias_level(struct snd_soc_codec *codec,
951 enum snd_soc_bias_level level)
952{
Mark Brownf1c0a022008-08-26 13:05:27 +0100953 u16 reg, reg2;
954
955 switch (level) {
956 case SND_SOC_BIAS_ON:
957 case SND_SOC_BIAS_PREPARE:
Mark Brown8d50e442009-07-10 23:12:01 +0100958 reg = snd_soc_read(codec, WM8903_VMID_CONTROL_0);
Mark Brownf1c0a022008-08-26 13:05:27 +0100959 reg &= ~(WM8903_VMID_RES_MASK);
960 reg |= WM8903_VMID_RES_50K;
Mark Brown8d50e442009-07-10 23:12:01 +0100961 snd_soc_write(codec, WM8903_VMID_CONTROL_0, reg);
Mark Brownf1c0a022008-08-26 13:05:27 +0100962 break;
963
964 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200965 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
Mark Brown8d50e442009-07-10 23:12:01 +0100966 snd_soc_write(codec, WM8903_CLOCK_RATES_2,
Mark Brown3b1228a2008-12-10 19:27:10 +0000967 WM8903_CLK_SYS_ENA);
968
Mark Brown4dbfe802009-04-22 20:32:40 +0100969 /* Change DC servo dither level in startup sequence */
Mark Brown8d50e442009-07-10 23:12:01 +0100970 snd_soc_write(codec, WM8903_WRITE_SEQUENCER_0, 0x11);
971 snd_soc_write(codec, WM8903_WRITE_SEQUENCER_1, 0x1257);
972 snd_soc_write(codec, WM8903_WRITE_SEQUENCER_2, 0x2);
Mark Brown4dbfe802009-04-22 20:32:40 +0100973
Mark Brownf1c0a022008-08-26 13:05:27 +0100974 wm8903_run_sequence(codec, 0);
975 wm8903_sync_reg_cache(codec, codec->reg_cache);
976
977 /* Enable low impedence charge pump output */
Mark Brown8d50e442009-07-10 23:12:01 +0100978 reg = snd_soc_read(codec,
Mark Brownf1c0a022008-08-26 13:05:27 +0100979 WM8903_CONTROL_INTERFACE_TEST_1);
Mark Brown8d50e442009-07-10 23:12:01 +0100980 snd_soc_write(codec, WM8903_CONTROL_INTERFACE_TEST_1,
Mark Brownf1c0a022008-08-26 13:05:27 +0100981 reg | WM8903_TEST_KEY);
Mark Brown8d50e442009-07-10 23:12:01 +0100982 reg2 = snd_soc_read(codec, WM8903_CHARGE_PUMP_TEST_1);
983 snd_soc_write(codec, WM8903_CHARGE_PUMP_TEST_1,
Mark Brownf1c0a022008-08-26 13:05:27 +0100984 reg2 | WM8903_CP_SW_KELVIN_MODE_MASK);
Mark Brown8d50e442009-07-10 23:12:01 +0100985 snd_soc_write(codec, WM8903_CONTROL_INTERFACE_TEST_1,
Mark Brownf1c0a022008-08-26 13:05:27 +0100986 reg);
987
988 /* By default no bypass paths are enabled so
989 * enable Class W support.
990 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000991 dev_dbg(codec->dev, "Enabling Class W\n");
Mark Brown8d50e442009-07-10 23:12:01 +0100992 snd_soc_write(codec, WM8903_CLASS_W_0, reg |
Mark Brownf1c0a022008-08-26 13:05:27 +0100993 WM8903_CP_DYN_FREQ | WM8903_CP_DYN_V);
994 }
995
Mark Brown8d50e442009-07-10 23:12:01 +0100996 reg = snd_soc_read(codec, WM8903_VMID_CONTROL_0);
Mark Brownf1c0a022008-08-26 13:05:27 +0100997 reg &= ~(WM8903_VMID_RES_MASK);
998 reg |= WM8903_VMID_RES_250K;
Mark Brown8d50e442009-07-10 23:12:01 +0100999 snd_soc_write(codec, WM8903_VMID_CONTROL_0, reg);
Mark Brownf1c0a022008-08-26 13:05:27 +01001000 break;
1001
1002 case SND_SOC_BIAS_OFF:
1003 wm8903_run_sequence(codec, 32);
Mark Brown8d50e442009-07-10 23:12:01 +01001004 reg = snd_soc_read(codec, WM8903_CLOCK_RATES_2);
Mark Brown3b1228a2008-12-10 19:27:10 +00001005 reg &= ~WM8903_CLK_SYS_ENA;
Mark Brown8d50e442009-07-10 23:12:01 +01001006 snd_soc_write(codec, WM8903_CLOCK_RATES_2, reg);
Mark Brownf1c0a022008-08-26 13:05:27 +01001007 break;
1008 }
1009
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001010 codec->dapm.bias_level = level;
Mark Brownf1c0a022008-08-26 13:05:27 +01001011
1012 return 0;
1013}
1014
1015static int wm8903_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1016 int clk_id, unsigned int freq, int dir)
1017{
1018 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001019 struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
Mark Brownf1c0a022008-08-26 13:05:27 +01001020
1021 wm8903->sysclk = freq;
1022
1023 return 0;
1024}
1025
1026static int wm8903_set_dai_fmt(struct snd_soc_dai *codec_dai,
1027 unsigned int fmt)
1028{
1029 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brown8d50e442009-07-10 23:12:01 +01001030 u16 aif1 = snd_soc_read(codec, WM8903_AUDIO_INTERFACE_1);
Mark Brownf1c0a022008-08-26 13:05:27 +01001031
1032 aif1 &= ~(WM8903_LRCLK_DIR | WM8903_BCLK_DIR | WM8903_AIF_FMT_MASK |
1033 WM8903_AIF_LRCLK_INV | WM8903_AIF_BCLK_INV);
1034
1035 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1036 case SND_SOC_DAIFMT_CBS_CFS:
1037 break;
1038 case SND_SOC_DAIFMT_CBS_CFM:
1039 aif1 |= WM8903_LRCLK_DIR;
1040 break;
1041 case SND_SOC_DAIFMT_CBM_CFM:
1042 aif1 |= WM8903_LRCLK_DIR | WM8903_BCLK_DIR;
1043 break;
1044 case SND_SOC_DAIFMT_CBM_CFS:
1045 aif1 |= WM8903_BCLK_DIR;
1046 break;
1047 default:
1048 return -EINVAL;
1049 }
1050
1051 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1052 case SND_SOC_DAIFMT_DSP_A:
1053 aif1 |= 0x3;
1054 break;
1055 case SND_SOC_DAIFMT_DSP_B:
1056 aif1 |= 0x3 | WM8903_AIF_LRCLK_INV;
1057 break;
1058 case SND_SOC_DAIFMT_I2S:
1059 aif1 |= 0x2;
1060 break;
1061 case SND_SOC_DAIFMT_RIGHT_J:
1062 aif1 |= 0x1;
1063 break;
1064 case SND_SOC_DAIFMT_LEFT_J:
1065 break;
1066 default:
1067 return -EINVAL;
1068 }
1069
1070 /* Clock inversion */
1071 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1072 case SND_SOC_DAIFMT_DSP_A:
1073 case SND_SOC_DAIFMT_DSP_B:
1074 /* frame inversion not valid for DSP modes */
1075 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1076 case SND_SOC_DAIFMT_NB_NF:
1077 break;
1078 case SND_SOC_DAIFMT_IB_NF:
1079 aif1 |= WM8903_AIF_BCLK_INV;
1080 break;
1081 default:
1082 return -EINVAL;
1083 }
1084 break;
1085 case SND_SOC_DAIFMT_I2S:
1086 case SND_SOC_DAIFMT_RIGHT_J:
1087 case SND_SOC_DAIFMT_LEFT_J:
1088 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1089 case SND_SOC_DAIFMT_NB_NF:
1090 break;
1091 case SND_SOC_DAIFMT_IB_IF:
1092 aif1 |= WM8903_AIF_BCLK_INV | WM8903_AIF_LRCLK_INV;
1093 break;
1094 case SND_SOC_DAIFMT_IB_NF:
1095 aif1 |= WM8903_AIF_BCLK_INV;
1096 break;
1097 case SND_SOC_DAIFMT_NB_IF:
1098 aif1 |= WM8903_AIF_LRCLK_INV;
1099 break;
1100 default:
1101 return -EINVAL;
1102 }
1103 break;
1104 default:
1105 return -EINVAL;
1106 }
1107
Mark Brown8d50e442009-07-10 23:12:01 +01001108 snd_soc_write(codec, WM8903_AUDIO_INTERFACE_1, aif1);
Mark Brownf1c0a022008-08-26 13:05:27 +01001109
1110 return 0;
1111}
1112
1113static int wm8903_digital_mute(struct snd_soc_dai *codec_dai, int mute)
1114{
1115 struct snd_soc_codec *codec = codec_dai->codec;
1116 u16 reg;
1117
Mark Brown8d50e442009-07-10 23:12:01 +01001118 reg = snd_soc_read(codec, WM8903_DAC_DIGITAL_1);
Mark Brownf1c0a022008-08-26 13:05:27 +01001119
1120 if (mute)
1121 reg |= WM8903_DAC_MUTE;
1122 else
1123 reg &= ~WM8903_DAC_MUTE;
1124
Mark Brown8d50e442009-07-10 23:12:01 +01001125 snd_soc_write(codec, WM8903_DAC_DIGITAL_1, reg);
Mark Brownf1c0a022008-08-26 13:05:27 +01001126
1127 return 0;
1128}
1129
1130/* Lookup table for CLK_SYS/fs ratio. 256fs or more is recommended
1131 * for optimal performance so we list the lower rates first and match
1132 * on the last match we find. */
1133static struct {
1134 int div;
1135 int rate;
1136 int mode;
1137 int mclk_div;
1138} clk_sys_ratios[] = {
1139 { 64, 0x0, 0x0, 1 },
1140 { 68, 0x0, 0x1, 1 },
1141 { 125, 0x0, 0x2, 1 },
1142 { 128, 0x1, 0x0, 1 },
1143 { 136, 0x1, 0x1, 1 },
1144 { 192, 0x2, 0x0, 1 },
1145 { 204, 0x2, 0x1, 1 },
1146
1147 { 64, 0x0, 0x0, 2 },
1148 { 68, 0x0, 0x1, 2 },
1149 { 125, 0x0, 0x2, 2 },
1150 { 128, 0x1, 0x0, 2 },
1151 { 136, 0x1, 0x1, 2 },
1152 { 192, 0x2, 0x0, 2 },
1153 { 204, 0x2, 0x1, 2 },
1154
1155 { 250, 0x2, 0x2, 1 },
1156 { 256, 0x3, 0x0, 1 },
1157 { 272, 0x3, 0x1, 1 },
1158 { 384, 0x4, 0x0, 1 },
1159 { 408, 0x4, 0x1, 1 },
1160 { 375, 0x4, 0x2, 1 },
1161 { 512, 0x5, 0x0, 1 },
1162 { 544, 0x5, 0x1, 1 },
1163 { 500, 0x5, 0x2, 1 },
1164 { 768, 0x6, 0x0, 1 },
1165 { 816, 0x6, 0x1, 1 },
1166 { 750, 0x6, 0x2, 1 },
1167 { 1024, 0x7, 0x0, 1 },
1168 { 1088, 0x7, 0x1, 1 },
1169 { 1000, 0x7, 0x2, 1 },
1170 { 1408, 0x8, 0x0, 1 },
1171 { 1496, 0x8, 0x1, 1 },
1172 { 1536, 0x9, 0x0, 1 },
1173 { 1632, 0x9, 0x1, 1 },
1174 { 1500, 0x9, 0x2, 1 },
1175
1176 { 250, 0x2, 0x2, 2 },
1177 { 256, 0x3, 0x0, 2 },
1178 { 272, 0x3, 0x1, 2 },
1179 { 384, 0x4, 0x0, 2 },
1180 { 408, 0x4, 0x1, 2 },
1181 { 375, 0x4, 0x2, 2 },
1182 { 512, 0x5, 0x0, 2 },
1183 { 544, 0x5, 0x1, 2 },
1184 { 500, 0x5, 0x2, 2 },
1185 { 768, 0x6, 0x0, 2 },
1186 { 816, 0x6, 0x1, 2 },
1187 { 750, 0x6, 0x2, 2 },
1188 { 1024, 0x7, 0x0, 2 },
1189 { 1088, 0x7, 0x1, 2 },
1190 { 1000, 0x7, 0x2, 2 },
1191 { 1408, 0x8, 0x0, 2 },
1192 { 1496, 0x8, 0x1, 2 },
1193 { 1536, 0x9, 0x0, 2 },
1194 { 1632, 0x9, 0x1, 2 },
1195 { 1500, 0x9, 0x2, 2 },
1196};
1197
1198/* CLK_SYS/BCLK ratios - multiplied by 10 due to .5s */
1199static struct {
1200 int ratio;
1201 int div;
1202} bclk_divs[] = {
1203 { 10, 0 },
Mark Brownf1c0a022008-08-26 13:05:27 +01001204 { 20, 2 },
1205 { 30, 3 },
1206 { 40, 4 },
1207 { 50, 5 },
Mark Brownf1c0a022008-08-26 13:05:27 +01001208 { 60, 7 },
1209 { 80, 8 },
1210 { 100, 9 },
Mark Brownf1c0a022008-08-26 13:05:27 +01001211 { 120, 11 },
1212 { 160, 12 },
1213 { 200, 13 },
1214 { 220, 14 },
1215 { 240, 15 },
Mark Brownf1c0a022008-08-26 13:05:27 +01001216 { 300, 17 },
1217 { 320, 18 },
1218 { 440, 19 },
1219 { 480, 20 },
1220};
1221
1222/* Sample rates for DSP */
1223static struct {
1224 int rate;
1225 int value;
1226} sample_rates[] = {
1227 { 8000, 0 },
1228 { 11025, 1 },
1229 { 12000, 2 },
1230 { 16000, 3 },
1231 { 22050, 4 },
1232 { 24000, 5 },
1233 { 32000, 6 },
1234 { 44100, 7 },
1235 { 48000, 8 },
1236 { 88200, 9 },
1237 { 96000, 10 },
1238 { 0, 0 },
1239};
1240
Mark Brownf1c0a022008-08-26 13:05:27 +01001241static int wm8903_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +00001242 struct snd_pcm_hw_params *params,
1243 struct snd_soc_dai *dai)
Mark Brownf1c0a022008-08-26 13:05:27 +01001244{
1245 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001246 struct snd_soc_codec *codec =rtd->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001247 struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
Mark Brownf1c0a022008-08-26 13:05:27 +01001248 int fs = params_rate(params);
1249 int bclk;
1250 int bclk_div;
1251 int i;
1252 int dsp_config;
1253 int clk_config;
1254 int best_val;
1255 int cur_val;
1256 int clk_sys;
1257
Mark Brown8d50e442009-07-10 23:12:01 +01001258 u16 aif1 = snd_soc_read(codec, WM8903_AUDIO_INTERFACE_1);
1259 u16 aif2 = snd_soc_read(codec, WM8903_AUDIO_INTERFACE_2);
1260 u16 aif3 = snd_soc_read(codec, WM8903_AUDIO_INTERFACE_3);
1261 u16 clock0 = snd_soc_read(codec, WM8903_CLOCK_RATES_0);
1262 u16 clock1 = snd_soc_read(codec, WM8903_CLOCK_RATES_1);
1263 u16 dac_digital1 = snd_soc_read(codec, WM8903_DAC_DIGITAL_1);
Mark Brownf1c0a022008-08-26 13:05:27 +01001264
Mark Brown9e792612009-06-12 17:27:07 +01001265 /* Enable sloping stopband filter for low sample rates */
1266 if (fs <= 24000)
1267 dac_digital1 |= WM8903_DAC_SB_FILT;
1268 else
1269 dac_digital1 &= ~WM8903_DAC_SB_FILT;
1270
Mark Brownf1c0a022008-08-26 13:05:27 +01001271 /* Configure sample rate logic for DSP - choose nearest rate */
1272 dsp_config = 0;
1273 best_val = abs(sample_rates[dsp_config].rate - fs);
1274 for (i = 1; i < ARRAY_SIZE(sample_rates); i++) {
1275 cur_val = abs(sample_rates[i].rate - fs);
1276 if (cur_val <= best_val) {
1277 dsp_config = i;
1278 best_val = cur_val;
1279 }
1280 }
1281
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001282 dev_dbg(codec->dev, "DSP fs = %dHz\n", sample_rates[dsp_config].rate);
Mark Brownf1c0a022008-08-26 13:05:27 +01001283 clock1 &= ~WM8903_SAMPLE_RATE_MASK;
1284 clock1 |= sample_rates[dsp_config].value;
1285
1286 aif1 &= ~WM8903_AIF_WL_MASK;
1287 bclk = 2 * fs;
1288 switch (params_format(params)) {
1289 case SNDRV_PCM_FORMAT_S16_LE:
1290 bclk *= 16;
1291 break;
1292 case SNDRV_PCM_FORMAT_S20_3LE:
1293 bclk *= 20;
1294 aif1 |= 0x4;
1295 break;
1296 case SNDRV_PCM_FORMAT_S24_LE:
1297 bclk *= 24;
1298 aif1 |= 0x8;
1299 break;
1300 case SNDRV_PCM_FORMAT_S32_LE:
1301 bclk *= 32;
1302 aif1 |= 0xc;
1303 break;
1304 default:
1305 return -EINVAL;
1306 }
1307
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001308 dev_dbg(codec->dev, "MCLK = %dHz, target sample rate = %dHz\n",
Mark Brownf1c0a022008-08-26 13:05:27 +01001309 wm8903->sysclk, fs);
1310
1311 /* We may not have an MCLK which allows us to generate exactly
1312 * the clock we want, particularly with USB derived inputs, so
1313 * approximate.
1314 */
1315 clk_config = 0;
1316 best_val = abs((wm8903->sysclk /
1317 (clk_sys_ratios[0].mclk_div *
1318 clk_sys_ratios[0].div)) - fs);
1319 for (i = 1; i < ARRAY_SIZE(clk_sys_ratios); i++) {
1320 cur_val = abs((wm8903->sysclk /
1321 (clk_sys_ratios[i].mclk_div *
1322 clk_sys_ratios[i].div)) - fs);
1323
1324 if (cur_val <= best_val) {
1325 clk_config = i;
1326 best_val = cur_val;
1327 }
1328 }
1329
1330 if (clk_sys_ratios[clk_config].mclk_div == 2) {
1331 clock0 |= WM8903_MCLKDIV2;
1332 clk_sys = wm8903->sysclk / 2;
1333 } else {
1334 clock0 &= ~WM8903_MCLKDIV2;
1335 clk_sys = wm8903->sysclk;
1336 }
1337
1338 clock1 &= ~(WM8903_CLK_SYS_RATE_MASK |
1339 WM8903_CLK_SYS_MODE_MASK);
1340 clock1 |= clk_sys_ratios[clk_config].rate << WM8903_CLK_SYS_RATE_SHIFT;
1341 clock1 |= clk_sys_ratios[clk_config].mode << WM8903_CLK_SYS_MODE_SHIFT;
1342
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001343 dev_dbg(codec->dev, "CLK_SYS_RATE=%x, CLK_SYS_MODE=%x div=%d\n",
Mark Brownf1c0a022008-08-26 13:05:27 +01001344 clk_sys_ratios[clk_config].rate,
1345 clk_sys_ratios[clk_config].mode,
1346 clk_sys_ratios[clk_config].div);
1347
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001348 dev_dbg(codec->dev, "Actual CLK_SYS = %dHz\n", clk_sys);
Mark Brownf1c0a022008-08-26 13:05:27 +01001349
1350 /* We may not get quite the right frequency if using
1351 * approximate clocks so look for the closest match that is
1352 * higher than the target (we need to ensure that there enough
1353 * BCLKs to clock out the samples).
1354 */
1355 bclk_div = 0;
1356 best_val = ((clk_sys * 10) / bclk_divs[0].ratio) - bclk;
1357 i = 1;
1358 while (i < ARRAY_SIZE(bclk_divs)) {
1359 cur_val = ((clk_sys * 10) / bclk_divs[i].ratio) - bclk;
1360 if (cur_val < 0) /* BCLK table is sorted */
1361 break;
1362 bclk_div = i;
1363 best_val = cur_val;
1364 i++;
1365 }
1366
1367 aif2 &= ~WM8903_BCLK_DIV_MASK;
1368 aif3 &= ~WM8903_LRCLK_RATE_MASK;
1369
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001370 dev_dbg(codec->dev, "BCLK ratio %d for %dHz - actual BCLK = %dHz\n",
Mark Brownf1c0a022008-08-26 13:05:27 +01001371 bclk_divs[bclk_div].ratio / 10, bclk,
1372 (clk_sys * 10) / bclk_divs[bclk_div].ratio);
1373
1374 aif2 |= bclk_divs[bclk_div].div;
1375 aif3 |= bclk / fs;
1376
Mark Brown8d50e442009-07-10 23:12:01 +01001377 snd_soc_write(codec, WM8903_CLOCK_RATES_0, clock0);
1378 snd_soc_write(codec, WM8903_CLOCK_RATES_1, clock1);
1379 snd_soc_write(codec, WM8903_AUDIO_INTERFACE_1, aif1);
1380 snd_soc_write(codec, WM8903_AUDIO_INTERFACE_2, aif2);
1381 snd_soc_write(codec, WM8903_AUDIO_INTERFACE_3, aif3);
1382 snd_soc_write(codec, WM8903_DAC_DIGITAL_1, dac_digital1);
Mark Brownf1c0a022008-08-26 13:05:27 +01001383
1384 return 0;
1385}
1386
Mark Brown72453872010-03-15 21:22:58 +00001387/**
1388 * wm8903_mic_detect - Enable microphone detection via the WM8903 IRQ
1389 *
1390 * @codec: WM8903 codec
1391 * @jack: jack to report detection events on
1392 * @det: value to report for presence detection
1393 * @shrt: value to report for short detection
1394 *
1395 * Enable microphone detection via IRQ on the WM8903. If GPIOs are
1396 * being used to bring out signals to the processor then only platform
1397 * data configuration is needed for WM8903 and processor GPIOs should
1398 * be configured using snd_soc_jack_add_gpios() instead.
1399 *
1400 * The current threasholds for detection should be configured using
1401 * micdet_cfg in the platform data. Using this function will force on
1402 * the microphone bias for the device.
1403 */
1404int wm8903_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack,
1405 int det, int shrt)
1406{
Mark Brownb2c812e2010-04-14 15:35:19 +09001407 struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
Mark Brown69266862010-03-22 16:37:01 +00001408 int irq_mask = WM8903_MICDET_EINT | WM8903_MICSHRT_EINT;
Mark Brown72453872010-03-15 21:22:58 +00001409
1410 dev_dbg(codec->dev, "Enabling microphone detection: %x %x\n",
1411 det, shrt);
1412
1413 /* Store the configuration */
1414 wm8903->mic_jack = jack;
1415 wm8903->mic_det = det;
1416 wm8903->mic_short = shrt;
1417
1418 /* Enable interrupts we've got a report configured for */
1419 if (det)
1420 irq_mask &= ~WM8903_MICDET_EINT;
1421 if (shrt)
1422 irq_mask &= ~WM8903_MICSHRT_EINT;
1423
1424 snd_soc_update_bits(codec, WM8903_INTERRUPT_STATUS_1_MASK,
1425 WM8903_MICDET_EINT | WM8903_MICSHRT_EINT,
1426 irq_mask);
1427
Mark Brown69266862010-03-22 16:37:01 +00001428 if (det && shrt) {
1429 /* Enable mic detection, this may not have been set through
1430 * platform data (eg, if the defaults are OK). */
1431 snd_soc_update_bits(codec, WM8903_WRITE_SEQUENCER_0,
1432 WM8903_WSEQ_ENA, WM8903_WSEQ_ENA);
1433 snd_soc_update_bits(codec, WM8903_MIC_BIAS_CONTROL_0,
1434 WM8903_MICDET_ENA, WM8903_MICDET_ENA);
1435 } else {
1436 snd_soc_update_bits(codec, WM8903_MIC_BIAS_CONTROL_0,
1437 WM8903_MICDET_ENA, 0);
1438 }
Mark Brown72453872010-03-15 21:22:58 +00001439
1440 return 0;
1441}
1442EXPORT_SYMBOL_GPL(wm8903_mic_detect);
1443
Mark Brown8abd16a2010-03-15 18:25:26 +00001444static irqreturn_t wm8903_irq(int irq, void *data)
1445{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001446 struct snd_soc_codec *codec = data;
1447 struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
Mark Brown72453872010-03-15 21:22:58 +00001448 int mic_report;
1449 int int_pol;
1450 int int_val = 0;
1451 int mask = ~snd_soc_read(codec, WM8903_INTERRUPT_STATUS_1_MASK);
Mark Brown8abd16a2010-03-15 18:25:26 +00001452
Mark Brown72453872010-03-15 21:22:58 +00001453 int_val = snd_soc_read(codec, WM8903_INTERRUPT_STATUS_1) & mask;
Mark Brown8abd16a2010-03-15 18:25:26 +00001454
Mark Brown72453872010-03-15 21:22:58 +00001455 if (int_val & WM8903_WSEQ_BUSY_EINT) {
Mark Brown8abd16a2010-03-15 18:25:26 +00001456 dev_dbg(codec->dev, "Write sequencer done\n");
1457 complete(&wm8903->wseq);
1458 }
1459
Mark Brown72453872010-03-15 21:22:58 +00001460 /*
1461 * The rest is microphone jack detection. We need to manually
1462 * invert the polarity of the interrupt after each event - to
1463 * simplify the code keep track of the last state we reported
1464 * and just invert the relevant bits in both the report and
1465 * the polarity register.
1466 */
1467 mic_report = wm8903->mic_last_report;
1468 int_pol = snd_soc_read(codec, WM8903_INTERRUPT_POLARITY_1);
1469
1470 if (int_val & WM8903_MICSHRT_EINT) {
1471 dev_dbg(codec->dev, "Microphone short (pol=%x)\n", int_pol);
1472
1473 mic_report ^= wm8903->mic_short;
1474 int_pol ^= WM8903_MICSHRT_INV;
1475 }
1476
1477 if (int_val & WM8903_MICDET_EINT) {
1478 dev_dbg(codec->dev, "Microphone detect (pol=%x)\n", int_pol);
1479
1480 mic_report ^= wm8903->mic_det;
1481 int_pol ^= WM8903_MICDET_INV;
1482
1483 msleep(wm8903->mic_delay);
1484 }
1485
1486 snd_soc_update_bits(codec, WM8903_INTERRUPT_POLARITY_1,
1487 WM8903_MICSHRT_INV | WM8903_MICDET_INV, int_pol);
1488
1489 snd_soc_jack_report(wm8903->mic_jack, mic_report,
1490 wm8903->mic_short | wm8903->mic_det);
1491
1492 wm8903->mic_last_report = mic_report;
1493
Mark Brown8abd16a2010-03-15 18:25:26 +00001494 return IRQ_HANDLED;
1495}
1496
Mark Brownf1c0a022008-08-26 13:05:27 +01001497#define WM8903_PLAYBACK_RATES (SNDRV_PCM_RATE_8000 |\
1498 SNDRV_PCM_RATE_11025 | \
1499 SNDRV_PCM_RATE_16000 | \
1500 SNDRV_PCM_RATE_22050 | \
1501 SNDRV_PCM_RATE_32000 | \
1502 SNDRV_PCM_RATE_44100 | \
1503 SNDRV_PCM_RATE_48000 | \
1504 SNDRV_PCM_RATE_88200 | \
1505 SNDRV_PCM_RATE_96000)
1506
1507#define WM8903_CAPTURE_RATES (SNDRV_PCM_RATE_8000 |\
1508 SNDRV_PCM_RATE_11025 | \
1509 SNDRV_PCM_RATE_16000 | \
1510 SNDRV_PCM_RATE_22050 | \
1511 SNDRV_PCM_RATE_32000 | \
1512 SNDRV_PCM_RATE_44100 | \
1513 SNDRV_PCM_RATE_48000)
1514
1515#define WM8903_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
1516 SNDRV_PCM_FMTBIT_S20_3LE |\
1517 SNDRV_PCM_FMTBIT_S24_LE)
1518
Eric Miao6335d052009-03-03 09:41:00 +08001519static struct snd_soc_dai_ops wm8903_dai_ops = {
Eric Miao6335d052009-03-03 09:41:00 +08001520 .hw_params = wm8903_hw_params,
1521 .digital_mute = wm8903_digital_mute,
1522 .set_fmt = wm8903_set_dai_fmt,
1523 .set_sysclk = wm8903_set_dai_sysclk,
1524};
1525
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001526static struct snd_soc_dai_driver wm8903_dai = {
1527 .name = "wm8903-hifi",
Mark Brownf1c0a022008-08-26 13:05:27 +01001528 .playback = {
1529 .stream_name = "Playback",
1530 .channels_min = 2,
1531 .channels_max = 2,
1532 .rates = WM8903_PLAYBACK_RATES,
1533 .formats = WM8903_FORMATS,
1534 },
1535 .capture = {
1536 .stream_name = "Capture",
1537 .channels_min = 2,
1538 .channels_max = 2,
1539 .rates = WM8903_CAPTURE_RATES,
1540 .formats = WM8903_FORMATS,
1541 },
Eric Miao6335d052009-03-03 09:41:00 +08001542 .ops = &wm8903_dai_ops,
Mark Brown0d960e82009-04-16 10:08:39 +01001543 .symmetric_rates = 1,
Mark Brownf1c0a022008-08-26 13:05:27 +01001544};
Mark Brownf1c0a022008-08-26 13:05:27 +01001545
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001546static int wm8903_suspend(struct snd_soc_codec *codec, pm_message_t state)
Mark Brownf1c0a022008-08-26 13:05:27 +01001547{
Mark Brownf1c0a022008-08-26 13:05:27 +01001548 wm8903_set_bias_level(codec, SND_SOC_BIAS_OFF);
1549
1550 return 0;
1551}
1552
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001553static int wm8903_resume(struct snd_soc_codec *codec)
Mark Brownf1c0a022008-08-26 13:05:27 +01001554{
Mark Brownf1c0a022008-08-26 13:05:27 +01001555 int i;
1556 u16 *reg_cache = codec->reg_cache;
Guennadi Liakhovetski40aa7032010-01-22 18:00:03 +01001557 u16 *tmp_cache = kmemdup(reg_cache, sizeof(wm8903_reg_defaults),
Mark Brownf1c0a022008-08-26 13:05:27 +01001558 GFP_KERNEL);
1559
1560 /* Bring the codec back up to standby first to minimise pop/clicks */
1561 wm8903_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Mark Brownf1c0a022008-08-26 13:05:27 +01001562
1563 /* Sync back everything else */
1564 if (tmp_cache) {
1565 for (i = 2; i < ARRAY_SIZE(wm8903_reg_defaults); i++)
1566 if (tmp_cache[i] != reg_cache[i])
Mark Brown8d50e442009-07-10 23:12:01 +01001567 snd_soc_write(codec, i, tmp_cache[i]);
Guennadi Liakhovetski40aa7032010-01-22 18:00:03 +01001568 kfree(tmp_cache);
Mark Brownf1c0a022008-08-26 13:05:27 +01001569 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001570 dev_err(codec->dev, "Failed to allocate temporary cache\n");
Mark Brownf1c0a022008-08-26 13:05:27 +01001571 }
1572
1573 return 0;
1574}
1575
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001576static int wm8903_probe(struct snd_soc_codec *codec)
Mark Brownf1c0a022008-08-26 13:05:27 +01001577{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001578 struct wm8903_platform_data *pdata = dev_get_platdata(codec->dev);
1579 struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
Mark Brown73b34ea2010-03-15 17:46:02 +00001580 int ret, i;
Mark Brown8abd16a2010-03-15 18:25:26 +00001581 int trigger, irq_pol;
Mark Brownf1c0a022008-08-26 13:05:27 +01001582 u16 val;
1583
Mark Brown8abd16a2010-03-15 18:25:26 +00001584 init_completion(&wm8903->wseq);
Mark Brownd58d5d52008-12-10 18:36:42 +00001585
Mark Brown8d50e442009-07-10 23:12:01 +01001586 ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_I2C);
1587 if (ret != 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001588 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
1589 return ret;
Mark Brown8d50e442009-07-10 23:12:01 +01001590 }
1591
1592 val = snd_soc_read(codec, WM8903_SW_RESET_AND_ID);
Mark Brownf1c0a022008-08-26 13:05:27 +01001593 if (val != wm8903_reg_defaults[WM8903_SW_RESET_AND_ID]) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001594 dev_err(codec->dev,
Mark Brownf1c0a022008-08-26 13:05:27 +01001595 "Device with ID register %x is not a WM8903\n", val);
1596 return -ENODEV;
1597 }
1598
Mark Brown8d50e442009-07-10 23:12:01 +01001599 val = snd_soc_read(codec, WM8903_REVISION_NUMBER);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001600 dev_info(codec->dev, "WM8903 revision %d\n",
Mark Brownf1c0a022008-08-26 13:05:27 +01001601 val & WM8903_CHIP_REV_MASK);
1602
1603 wm8903_reset(codec);
1604
Mark Brown37f88e82010-03-15 18:14:34 +00001605 /* Set up GPIOs and microphone detection */
Mark Brown73b34ea2010-03-15 17:46:02 +00001606 if (pdata) {
1607 for (i = 0; i < ARRAY_SIZE(pdata->gpio_cfg); i++) {
1608 if (!pdata->gpio_cfg[i])
1609 continue;
1610
1611 snd_soc_write(codec, WM8903_GPIO_CONTROL_1 + i,
1612 pdata->gpio_cfg[i] & 0xffff);
1613 }
Mark Brown37f88e82010-03-15 18:14:34 +00001614
1615 snd_soc_write(codec, WM8903_MIC_BIAS_CONTROL_0,
1616 pdata->micdet_cfg);
1617
1618 /* Microphone detection needs the WSEQ clock */
1619 if (pdata->micdet_cfg)
1620 snd_soc_update_bits(codec, WM8903_WRITE_SEQUENCER_0,
1621 WM8903_WSEQ_ENA, WM8903_WSEQ_ENA);
1622
1623 wm8903->mic_delay = pdata->micdet_delay;
Mark Brown73b34ea2010-03-15 17:46:02 +00001624 }
Mark Brown8abd16a2010-03-15 18:25:26 +00001625
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001626 if (wm8903->irq) {
Mark Brown8abd16a2010-03-15 18:25:26 +00001627 if (pdata && pdata->irq_active_low) {
1628 trigger = IRQF_TRIGGER_LOW;
1629 irq_pol = WM8903_IRQ_POL;
1630 } else {
1631 trigger = IRQF_TRIGGER_HIGH;
1632 irq_pol = 0;
1633 }
1634
1635 snd_soc_update_bits(codec, WM8903_INTERRUPT_CONTROL,
1636 WM8903_IRQ_POL, irq_pol);
1637
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001638 ret = request_threaded_irq(wm8903->irq, NULL, wm8903_irq,
Mark Brown8abd16a2010-03-15 18:25:26 +00001639 trigger | IRQF_ONESHOT,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001640 "wm8903", codec);
Mark Brown8abd16a2010-03-15 18:25:26 +00001641 if (ret != 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001642 dev_err(codec->dev, "Failed to request IRQ: %d\n",
Mark Brown8abd16a2010-03-15 18:25:26 +00001643 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001644 return ret;
Mark Brown8abd16a2010-03-15 18:25:26 +00001645 }
1646
1647 /* Enable write sequencer interrupts */
1648 snd_soc_update_bits(codec, WM8903_INTERRUPT_STATUS_1_MASK,
1649 WM8903_IM_WSEQ_BUSY_EINT, 0);
1650 }
Mark Brown73b34ea2010-03-15 17:46:02 +00001651
Mark Brownf1c0a022008-08-26 13:05:27 +01001652 /* power on device */
1653 wm8903_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1654
1655 /* Latch volume update bits */
Mark Brown8d50e442009-07-10 23:12:01 +01001656 val = snd_soc_read(codec, WM8903_ADC_DIGITAL_VOLUME_LEFT);
Mark Brownf1c0a022008-08-26 13:05:27 +01001657 val |= WM8903_ADCVU;
Mark Brown8d50e442009-07-10 23:12:01 +01001658 snd_soc_write(codec, WM8903_ADC_DIGITAL_VOLUME_LEFT, val);
1659 snd_soc_write(codec, WM8903_ADC_DIGITAL_VOLUME_RIGHT, val);
Mark Brownf1c0a022008-08-26 13:05:27 +01001660
Mark Brown8d50e442009-07-10 23:12:01 +01001661 val = snd_soc_read(codec, WM8903_DAC_DIGITAL_VOLUME_LEFT);
Mark Brownf1c0a022008-08-26 13:05:27 +01001662 val |= WM8903_DACVU;
Mark Brown8d50e442009-07-10 23:12:01 +01001663 snd_soc_write(codec, WM8903_DAC_DIGITAL_VOLUME_LEFT, val);
1664 snd_soc_write(codec, WM8903_DAC_DIGITAL_VOLUME_RIGHT, val);
Mark Brownf1c0a022008-08-26 13:05:27 +01001665
Mark Brown8d50e442009-07-10 23:12:01 +01001666 val = snd_soc_read(codec, WM8903_ANALOGUE_OUT1_LEFT);
Mark Brownf1c0a022008-08-26 13:05:27 +01001667 val |= WM8903_HPOUTVU;
Mark Brown8d50e442009-07-10 23:12:01 +01001668 snd_soc_write(codec, WM8903_ANALOGUE_OUT1_LEFT, val);
1669 snd_soc_write(codec, WM8903_ANALOGUE_OUT1_RIGHT, val);
Mark Brownf1c0a022008-08-26 13:05:27 +01001670
Mark Brown8d50e442009-07-10 23:12:01 +01001671 val = snd_soc_read(codec, WM8903_ANALOGUE_OUT2_LEFT);
Mark Brownf1c0a022008-08-26 13:05:27 +01001672 val |= WM8903_LINEOUTVU;
Mark Brown8d50e442009-07-10 23:12:01 +01001673 snd_soc_write(codec, WM8903_ANALOGUE_OUT2_LEFT, val);
1674 snd_soc_write(codec, WM8903_ANALOGUE_OUT2_RIGHT, val);
Mark Brownf1c0a022008-08-26 13:05:27 +01001675
Mark Brown8d50e442009-07-10 23:12:01 +01001676 val = snd_soc_read(codec, WM8903_ANALOGUE_OUT3_LEFT);
Mark Brownf1c0a022008-08-26 13:05:27 +01001677 val |= WM8903_SPKVU;
Mark Brown8d50e442009-07-10 23:12:01 +01001678 snd_soc_write(codec, WM8903_ANALOGUE_OUT3_LEFT, val);
1679 snd_soc_write(codec, WM8903_ANALOGUE_OUT3_RIGHT, val);
Mark Brownf1c0a022008-08-26 13:05:27 +01001680
1681 /* Enable DAC soft mute by default */
Mark Brown8d50e442009-07-10 23:12:01 +01001682 val = snd_soc_read(codec, WM8903_DAC_DIGITAL_1);
Mark Brownf1c0a022008-08-26 13:05:27 +01001683 val |= WM8903_DAC_MUTEMODE;
Mark Brown8d50e442009-07-10 23:12:01 +01001684 snd_soc_write(codec, WM8903_DAC_DIGITAL_1, val);
Mark Brownf1c0a022008-08-26 13:05:27 +01001685
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001686 snd_soc_add_controls(codec, wm8903_snd_controls,
1687 ARRAY_SIZE(wm8903_snd_controls));
1688 wm8903_add_widgets(codec);
Mark Brownf1c0a022008-08-26 13:05:27 +01001689
1690 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001691}
Mark Brownf1c0a022008-08-26 13:05:27 +01001692
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001693/* power down chip */
1694static int wm8903_remove(struct snd_soc_codec *codec)
1695{
1696 wm8903_set_bias_level(codec, SND_SOC_BIAS_OFF);
1697 return 0;
1698}
1699
1700static struct snd_soc_codec_driver soc_codec_dev_wm8903 = {
1701 .probe = wm8903_probe,
1702 .remove = wm8903_remove,
1703 .suspend = wm8903_suspend,
1704 .resume = wm8903_resume,
1705 .set_bias_level = wm8903_set_bias_level,
1706 .reg_cache_size = ARRAY_SIZE(wm8903_reg_defaults),
1707 .reg_word_size = sizeof(u16),
1708 .reg_cache_default = wm8903_reg_defaults,
1709 .volatile_register = wm8903_volatile_register,
1710};
1711
1712#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1713static __devinit int wm8903_i2c_probe(struct i2c_client *i2c,
1714 const struct i2c_device_id *id)
1715{
1716 struct wm8903_priv *wm8903;
1717 int ret;
1718
1719 wm8903 = kzalloc(sizeof(struct wm8903_priv), GFP_KERNEL);
1720 if (wm8903 == NULL)
1721 return -ENOMEM;
1722
1723 i2c_set_clientdata(i2c, wm8903);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001724 wm8903->irq = i2c->irq;
1725
1726 ret = snd_soc_register_codec(&i2c->dev,
1727 &soc_codec_dev_wm8903, &wm8903_dai, 1);
1728 if (ret < 0)
1729 kfree(wm8903);
Mark Brownf1c0a022008-08-26 13:05:27 +01001730 return ret;
1731}
1732
Mark Brownc6f29812009-02-18 21:25:40 +00001733static __devexit int wm8903_i2c_remove(struct i2c_client *client)
Mark Brownf1c0a022008-08-26 13:05:27 +01001734{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001735 snd_soc_unregister_codec(&client->dev);
1736 kfree(i2c_get_clientdata(client));
Mark Brownf1c0a022008-08-26 13:05:27 +01001737 return 0;
1738}
1739
Mark Brownf1c0a022008-08-26 13:05:27 +01001740static const struct i2c_device_id wm8903_i2c_id[] = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001741 { "wm8903", 0 },
1742 { }
Mark Brownf1c0a022008-08-26 13:05:27 +01001743};
1744MODULE_DEVICE_TABLE(i2c, wm8903_i2c_id);
1745
1746static struct i2c_driver wm8903_i2c_driver = {
1747 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001748 .name = "wm8903-codec",
Mark Brownf1c0a022008-08-26 13:05:27 +01001749 .owner = THIS_MODULE,
1750 },
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001751 .probe = wm8903_i2c_probe,
1752 .remove = __devexit_p(wm8903_i2c_remove),
Mark Brownf1c0a022008-08-26 13:05:27 +01001753 .id_table = wm8903_i2c_id,
1754};
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001755#endif
Mark Brownf1c0a022008-08-26 13:05:27 +01001756
Takashi Iwaic9b3a402008-12-10 07:47:22 +01001757static int __init wm8903_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +00001758{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001759 int ret = 0;
1760#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1761 ret = i2c_add_driver(&wm8903_i2c_driver);
1762 if (ret != 0) {
1763 printk(KERN_ERR "Failed to register wm8903 I2C driver: %d\n",
1764 ret);
1765 }
1766#endif
1767 return ret;
Mark Brown64089b82008-12-08 19:17:58 +00001768}
1769module_init(wm8903_modinit);
1770
1771static void __exit wm8903_exit(void)
1772{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001773#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Mark Brownd58d5d52008-12-10 18:36:42 +00001774 i2c_del_driver(&wm8903_i2c_driver);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001775#endif
Mark Brown64089b82008-12-08 19:17:58 +00001776}
1777module_exit(wm8903_exit);
1778
Mark Brownf1c0a022008-08-26 13:05:27 +01001779MODULE_DESCRIPTION("ASoC WM8903 driver");
1780MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.cm>");
1781MODULE_LICENSE("GPL");