wm8974.c 21.7 KB
Newer Older
M
Mark Brown 已提交
1 2 3
/*
 * wm8974.c  --  WM8974 ALSA Soc Audio driver
 *
M
Mark Brown 已提交
4
 * Copyright 2006-2009 Wolfson Microelectronics PLC.
M
Mark Brown 已提交
5
 *
6
 * Author: Liam Girdwood <linux@wolfsonmicro.com>
M
Mark Brown 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/pm.h>
#include <linux/i2c.h>
#include <linux/platform_device.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <sound/soc-dapm.h>
#include <sound/initval.h>
M
Mark Brown 已提交
27
#include <sound/tlv.h>
M
Mark Brown 已提交
28 29 30 31

#include "wm8974.h"

static const u16 wm8974_reg[WM8974_CACHEREGNUM] = {
M
Mark Brown 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
	0x0000, 0x0000, 0x0000, 0x0000,
	0x0050, 0x0000, 0x0140, 0x0000,
	0x0000, 0x0000, 0x0000, 0x00ff,
	0x0000, 0x0000, 0x0100, 0x00ff,
	0x0000, 0x0000, 0x012c, 0x002c,
	0x002c, 0x002c, 0x002c, 0x0000,
	0x0032, 0x0000, 0x0000, 0x0000,
	0x0000, 0x0000, 0x0000, 0x0000,
	0x0038, 0x000b, 0x0032, 0x0000,
	0x0008, 0x000c, 0x0093, 0x00e9,
	0x0000, 0x0000, 0x0000, 0x0000,
	0x0003, 0x0010, 0x0000, 0x0000,
	0x0000, 0x0002, 0x0000, 0x0000,
	0x0000, 0x0000, 0x0039, 0x0000,
	0x0000,
M
Mark Brown 已提交
47 48
};

49 50 51
#define WM8974_POWER1_BIASEN  0x08
#define WM8974_POWER1_BUFIOEN 0x10

52 53 54 55 56 57 58
struct wm8974_priv {
	struct snd_soc_codec codec;
	u16 reg_cache[WM8974_CACHEREGNUM];
};

static struct snd_soc_codec *wm8974_codec;

59
#define wm8974_reset(c)	snd_soc_write(c, WM8974_RESET, 0)
M
Mark Brown 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

static const char *wm8974_companding[] = {"Off", "NC", "u-law", "A-law" };
static const char *wm8974_deemp[] = {"None", "32kHz", "44.1kHz", "48kHz" };
static const char *wm8974_eqmode[] = {"Capture", "Playback" };
static const char *wm8974_bw[] = {"Narrow", "Wide" };
static const char *wm8974_eq1[] = {"80Hz", "105Hz", "135Hz", "175Hz" };
static const char *wm8974_eq2[] = {"230Hz", "300Hz", "385Hz", "500Hz" };
static const char *wm8974_eq3[] = {"650Hz", "850Hz", "1.1kHz", "1.4kHz" };
static const char *wm8974_eq4[] = {"1.8kHz", "2.4kHz", "3.2kHz", "4.1kHz" };
static const char *wm8974_eq5[] = {"5.3kHz", "6.9kHz", "9kHz", "11.7kHz" };
static const char *wm8974_alc[] = {"ALC", "Limiter" };

static const struct soc_enum wm8974_enum[] = {
	SOC_ENUM_SINGLE(WM8974_COMP, 1, 4, wm8974_companding), /* adc */
	SOC_ENUM_SINGLE(WM8974_COMP, 3, 4, wm8974_companding), /* dac */
	SOC_ENUM_SINGLE(WM8974_DAC,  4, 4, wm8974_deemp),
	SOC_ENUM_SINGLE(WM8974_EQ1,  8, 2, wm8974_eqmode),

	SOC_ENUM_SINGLE(WM8974_EQ1,  5, 4, wm8974_eq1),
	SOC_ENUM_SINGLE(WM8974_EQ2,  8, 2, wm8974_bw),
	SOC_ENUM_SINGLE(WM8974_EQ2,  5, 4, wm8974_eq2),
	SOC_ENUM_SINGLE(WM8974_EQ3,  8, 2, wm8974_bw),

	SOC_ENUM_SINGLE(WM8974_EQ3,  5, 4, wm8974_eq3),
	SOC_ENUM_SINGLE(WM8974_EQ4,  8, 2, wm8974_bw),
	SOC_ENUM_SINGLE(WM8974_EQ4,  5, 4, wm8974_eq4),
	SOC_ENUM_SINGLE(WM8974_EQ5,  8, 2, wm8974_bw),

	SOC_ENUM_SINGLE(WM8974_EQ5,  5, 4, wm8974_eq5),
	SOC_ENUM_SINGLE(WM8974_ALC3,  8, 2, wm8974_alc),
};

M
Mark Brown 已提交
92 93 94 95 96
static const char *wm8974_auxmode_text[] = { "Buffer", "Mixer" };

static const struct soc_enum wm8974_auxmode =
	SOC_ENUM_SINGLE(WM8974_INPUT,  3, 2, wm8974_auxmode_text);

M
Mark Brown 已提交
97 98 99 100 101
static const DECLARE_TLV_DB_SCALE(digital_tlv, -12750, 50, 1);
static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
static const DECLARE_TLV_DB_SCALE(inpga_tlv, -1200, 75, 0);
static const DECLARE_TLV_DB_SCALE(spk_tlv, -5700, 100, 0);

M
Mark Brown 已提交
102 103 104 105 106 107 108 109 110 111
static const struct snd_kcontrol_new wm8974_snd_controls[] = {

SOC_SINGLE("Digital Loopback Switch", WM8974_COMP, 0, 1, 0),

SOC_ENUM("DAC Companding", wm8974_enum[1]),
SOC_ENUM("ADC Companding", wm8974_enum[0]),

SOC_ENUM("Playback De-emphasis", wm8974_enum[2]),
SOC_SINGLE("DAC Inversion Switch", WM8974_DAC, 0, 1, 0),

M
Mark Brown 已提交
112
SOC_SINGLE_TLV("PCM Volume", WM8974_DACVOL, 0, 255, 0, digital_tlv),
M
Mark Brown 已提交
113 114 115

SOC_SINGLE("High Pass Filter Switch", WM8974_ADC, 8, 1, 0),
SOC_SINGLE("High Pass Cut Off", WM8974_ADC, 4, 7, 0),
116
SOC_SINGLE("ADC Inversion Switch", WM8974_ADC, 0, 1, 0),
M
Mark Brown 已提交
117

M
Mark Brown 已提交
118
SOC_SINGLE_TLV("Capture Volume", WM8974_ADCVOL,  0, 255, 0, digital_tlv),
M
Mark Brown 已提交
119 120 121

SOC_ENUM("Equaliser Function", wm8974_enum[3]),
SOC_ENUM("EQ1 Cut Off", wm8974_enum[4]),
M
Mark Brown 已提交
122
SOC_SINGLE_TLV("EQ1 Volume", WM8974_EQ1,  0, 24, 1, eq_tlv),
M
Mark Brown 已提交
123 124 125

SOC_ENUM("Equaliser EQ2 Bandwith", wm8974_enum[5]),
SOC_ENUM("EQ2 Cut Off", wm8974_enum[6]),
M
Mark Brown 已提交
126
SOC_SINGLE_TLV("EQ2 Volume", WM8974_EQ2,  0, 24, 1, eq_tlv),
M
Mark Brown 已提交
127 128 129

SOC_ENUM("Equaliser EQ3 Bandwith", wm8974_enum[7]),
SOC_ENUM("EQ3 Cut Off", wm8974_enum[8]),
M
Mark Brown 已提交
130
SOC_SINGLE_TLV("EQ3 Volume", WM8974_EQ3,  0, 24, 1, eq_tlv),
M
Mark Brown 已提交
131 132 133

SOC_ENUM("Equaliser EQ4 Bandwith", wm8974_enum[9]),
SOC_ENUM("EQ4 Cut Off", wm8974_enum[10]),
M
Mark Brown 已提交
134
SOC_SINGLE_TLV("EQ4 Volume", WM8974_EQ4,  0, 24, 1, eq_tlv),
M
Mark Brown 已提交
135 136 137

SOC_ENUM("Equaliser EQ5 Bandwith", wm8974_enum[11]),
SOC_ENUM("EQ5 Cut Off", wm8974_enum[12]),
M
Mark Brown 已提交
138
SOC_SINGLE_TLV("EQ5 Volume", WM8974_EQ5,  0, 24, 1, eq_tlv),
M
Mark Brown 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

SOC_SINGLE("DAC Playback Limiter Switch", WM8974_DACLIM1,  8, 1, 0),
SOC_SINGLE("DAC Playback Limiter Decay", WM8974_DACLIM1,  4, 15, 0),
SOC_SINGLE("DAC Playback Limiter Attack", WM8974_DACLIM1,  0, 15, 0),

SOC_SINGLE("DAC Playback Limiter Threshold", WM8974_DACLIM2,  4, 7, 0),
SOC_SINGLE("DAC Playback Limiter Boost", WM8974_DACLIM2,  0, 15, 0),

SOC_SINGLE("ALC Enable Switch", WM8974_ALC1,  8, 1, 0),
SOC_SINGLE("ALC Capture Max Gain", WM8974_ALC1,  3, 7, 0),
SOC_SINGLE("ALC Capture Min Gain", WM8974_ALC1,  0, 7, 0),

SOC_SINGLE("ALC Capture ZC Switch", WM8974_ALC2,  8, 1, 0),
SOC_SINGLE("ALC Capture Hold", WM8974_ALC2,  4, 7, 0),
SOC_SINGLE("ALC Capture Target", WM8974_ALC2,  0, 15, 0),

SOC_ENUM("ALC Capture Mode", wm8974_enum[13]),
SOC_SINGLE("ALC Capture Decay", WM8974_ALC3,  4, 15, 0),
SOC_SINGLE("ALC Capture Attack", WM8974_ALC3,  0, 15, 0),

SOC_SINGLE("ALC Capture Noise Gate Switch", WM8974_NGATE,  3, 1, 0),
SOC_SINGLE("ALC Capture Noise Gate Threshold", WM8974_NGATE,  0, 7, 0),

SOC_SINGLE("Capture PGA ZC Switch", WM8974_INPPGA,  7, 1, 0),
M
Mark Brown 已提交
163
SOC_SINGLE_TLV("Capture PGA Volume", WM8974_INPPGA,  0, 63, 0, inpga_tlv),
M
Mark Brown 已提交
164 165 166

SOC_SINGLE("Speaker Playback ZC Switch", WM8974_SPKVOL,  7, 1, 0),
SOC_SINGLE("Speaker Playback Switch", WM8974_SPKVOL,  6, 1, 1),
M
Mark Brown 已提交
167 168 169
SOC_SINGLE_TLV("Speaker Playback Volume", WM8974_SPKVOL,  0, 63, 0, spk_tlv),

SOC_ENUM("Aux Mode", wm8974_auxmode),
M
Mark Brown 已提交
170 171

SOC_SINGLE("Capture Boost(+20dB)", WM8974_ADCBOOST,  8, 1, 0),
M
Mark Brown 已提交
172
SOC_SINGLE("Mono Playback Switch", WM8974_MONOMIX, 6, 1, 1),
M
Mark Brown 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185
};

/* Speaker Output Mixer */
static const struct snd_kcontrol_new wm8974_speaker_mixer_controls[] = {
SOC_DAPM_SINGLE("Line Bypass Switch", WM8974_SPKMIX, 1, 1, 0),
SOC_DAPM_SINGLE("Aux Playback Switch", WM8974_SPKMIX, 5, 1, 0),
SOC_DAPM_SINGLE("PCM Playback Switch", WM8974_SPKMIX, 0, 1, 1),
};

/* Mono Output Mixer */
static const struct snd_kcontrol_new wm8974_mono_mixer_controls[] = {
SOC_DAPM_SINGLE("Line Bypass Switch", WM8974_MONOMIX, 1, 1, 0),
SOC_DAPM_SINGLE("Aux Playback Switch", WM8974_MONOMIX, 2, 1, 0),
M
Mark Brown 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198
SOC_DAPM_SINGLE("PCM Playback Switch", WM8974_MONOMIX, 0, 1, 0),
};

/* Boost mixer */
static const struct snd_kcontrol_new wm8974_boost_mixer[] = {
SOC_DAPM_SINGLE("Aux Switch", WM8974_INPPGA, 6, 1, 0),
};

/* Input PGA */
static const struct snd_kcontrol_new wm8974_inpga[] = {
SOC_DAPM_SINGLE("Aux Switch", WM8974_INPUT, 2, 1, 0),
SOC_DAPM_SINGLE("MicN Switch", WM8974_INPUT, 1, 1, 0),
SOC_DAPM_SINGLE("MicP Switch", WM8974_INPUT, 0, 1, 0),
M
Mark Brown 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
};

/* AUX Input boost vol */
static const struct snd_kcontrol_new wm8974_aux_boost_controls =
SOC_DAPM_SINGLE("Aux Volume", WM8974_ADCBOOST, 0, 7, 0);

/* Mic Input boost vol */
static const struct snd_kcontrol_new wm8974_mic_boost_controls =
SOC_DAPM_SINGLE("Mic Volume", WM8974_ADCBOOST, 4, 7, 0);

static const struct snd_soc_dapm_widget wm8974_dapm_widgets[] = {
SND_SOC_DAPM_MIXER("Speaker Mixer", WM8974_POWER3, 2, 0,
	&wm8974_speaker_mixer_controls[0],
	ARRAY_SIZE(wm8974_speaker_mixer_controls)),
SND_SOC_DAPM_MIXER("Mono Mixer", WM8974_POWER3, 3, 0,
	&wm8974_mono_mixer_controls[0],
	ARRAY_SIZE(wm8974_mono_mixer_controls)),
SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8974_POWER3, 0, 0),
M
Mark Brown 已提交
217
SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8974_POWER2, 0, 0),
M
Mark Brown 已提交
218 219 220 221 222
SND_SOC_DAPM_PGA("Aux Input", WM8974_POWER1, 6, 0, NULL, 0),
SND_SOC_DAPM_PGA("SpkN Out", WM8974_POWER3, 5, 0, NULL, 0),
SND_SOC_DAPM_PGA("SpkP Out", WM8974_POWER3, 6, 0, NULL, 0),
SND_SOC_DAPM_PGA("Mono Out", WM8974_POWER3, 7, 0, NULL, 0),

M
Mark Brown 已提交
223 224 225 226
SND_SOC_DAPM_MIXER("Input PGA", WM8974_POWER2, 2, 0, wm8974_inpga,
		   ARRAY_SIZE(wm8974_inpga)),
SND_SOC_DAPM_MIXER("Boost Mixer", WM8974_POWER2, 4, 0,
		   wm8974_boost_mixer, ARRAY_SIZE(wm8974_boost_mixer)),
M
Mark Brown 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257

SND_SOC_DAPM_MICBIAS("Mic Bias", WM8974_POWER1, 4, 0),

SND_SOC_DAPM_INPUT("MICN"),
SND_SOC_DAPM_INPUT("MICP"),
SND_SOC_DAPM_INPUT("AUX"),
SND_SOC_DAPM_OUTPUT("MONOOUT"),
SND_SOC_DAPM_OUTPUT("SPKOUTP"),
SND_SOC_DAPM_OUTPUT("SPKOUTN"),
};

static const struct snd_soc_dapm_route audio_map[] = {
	/* Mono output mixer */
	{"Mono Mixer", "PCM Playback Switch", "DAC"},
	{"Mono Mixer", "Aux Playback Switch", "Aux Input"},
	{"Mono Mixer", "Line Bypass Switch", "Boost Mixer"},

	/* Speaker output mixer */
	{"Speaker Mixer", "PCM Playback Switch", "DAC"},
	{"Speaker Mixer", "Aux Playback Switch", "Aux Input"},
	{"Speaker Mixer", "Line Bypass Switch", "Boost Mixer"},

	/* Outputs */
	{"Mono Out", NULL, "Mono Mixer"},
	{"MONOOUT", NULL, "Mono Out"},
	{"SpkN Out", NULL, "Speaker Mixer"},
	{"SpkP Out", NULL, "Speaker Mixer"},
	{"SPKOUTN", NULL, "SpkN Out"},
	{"SPKOUTP", NULL, "SpkP Out"},

	/* Boost Mixer */
M
Mark Brown 已提交
258 259 260 261 262 263 264 265 266
	{"ADC", NULL, "Boost Mixer"},
	{"Boost Mixer", "Aux Switch", "Aux Input"},
	{"Boost Mixer", NULL, "Input PGA"},
	{"Boost Mixer", NULL, "MICP"},

	/* Input PGA */
	{"Input PGA", "Aux Switch", "Aux Input"},
	{"Input PGA", "MicN Switch", "MICN"},
	{"Input PGA", "MicP Switch", "MICP"},
M
Mark Brown 已提交
267 268

	/* Inputs */
M
Mark Brown 已提交
269
	{"Aux Input", NULL, "AUX"},
M
Mark Brown 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282 283
};

static int wm8974_add_widgets(struct snd_soc_codec *codec)
{
	snd_soc_dapm_new_controls(codec, wm8974_dapm_widgets,
				  ARRAY_SIZE(wm8974_dapm_widgets));

	snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));

	snd_soc_dapm_new_widgets(codec);
	return 0;
}

struct pll_ {
284
	unsigned int pre_div:4; /* prescale - 1 */
M
Mark Brown 已提交
285 286 287 288
	unsigned int n:4;
	unsigned int k;
};

289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
static struct pll_ pll_div;

/* The size in bits of the pll divide multiplied by 10
 * to allow rounding later */
#define FIXED_PLL_SIZE ((1 << 24) * 10)

static void pll_factors(unsigned int target, unsigned int source)
{
	unsigned long long Kpart;
	unsigned int K, Ndiv, Nmod;

	Ndiv = target / source;
	if (Ndiv < 6) {
		source >>= 1;
		pll_div.pre_div = 1;
		Ndiv = target / source;
	} else
		pll_div.pre_div = 0;

	if ((Ndiv < 6) || (Ndiv > 12))
		printk(KERN_WARNING
M
Mark Brown 已提交
310
			"WM8974 N value %u outwith recommended range!\n",
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
			Ndiv);

	pll_div.n = Ndiv;
	Nmod = target % source;
	Kpart = FIXED_PLL_SIZE * (long long)Nmod;

	do_div(Kpart, source);

	K = Kpart & 0xFFFFFFFF;

	/* Check if we need to round */
	if ((K % 10) >= 5)
		K += 5;

	/* Move down to proper range now rounding is done */
	K /= 10;

	pll_div.k = K;
}
M
Mark Brown 已提交
330 331 332 333 334 335 336

static int wm8974_set_dai_pll(struct snd_soc_dai *codec_dai,
		int pll_id, unsigned int freq_in, unsigned int freq_out)
{
	struct snd_soc_codec *codec = codec_dai->codec;
	u16 reg;

M
Mark Brown 已提交
337
	if (freq_in == 0 || freq_out == 0) {
338
		/* Clock CODEC directly from MCLK */
339 340
		reg = snd_soc_read(codec, WM8974_CLOCK);
		snd_soc_write(codec, WM8974_CLOCK, reg & 0x0ff);
341 342

		/* Turn off PLL */
343 344
		reg = snd_soc_read(codec, WM8974_POWER1);
		snd_soc_write(codec, WM8974_POWER1, reg & 0x1df);
M
Mark Brown 已提交
345 346 347
		return 0;
	}

348 349
	pll_factors(freq_out*4, freq_in);

350 351 352 353 354 355
	snd_soc_write(codec, WM8974_PLLN, (pll_div.pre_div << 4) | pll_div.n);
	snd_soc_write(codec, WM8974_PLLK1, pll_div.k >> 18);
	snd_soc_write(codec, WM8974_PLLK2, (pll_div.k >> 9) & 0x1ff);
	snd_soc_write(codec, WM8974_PLLK3, pll_div.k & 0x1ff);
	reg = snd_soc_read(codec, WM8974_POWER1);
	snd_soc_write(codec, WM8974_POWER1, reg | 0x020);
M
Mark Brown 已提交
356

357
	/* Run CODEC from PLL instead of MCLK */
358 359
	reg = snd_soc_read(codec, WM8974_CLOCK);
	snd_soc_write(codec, WM8974_CLOCK, reg | 0x100);
360 361

	return 0;
M
Mark Brown 已提交
362 363 364 365 366 367 368 369 370 371 372 373 374
}

/*
 * Configure WM8974 clock dividers.
 */
static int wm8974_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
		int div_id, int div)
{
	struct snd_soc_codec *codec = codec_dai->codec;
	u16 reg;

	switch (div_id) {
	case WM8974_OPCLKDIV:
375 376
		reg = snd_soc_read(codec, WM8974_GPIO) & 0x1cf;
		snd_soc_write(codec, WM8974_GPIO, reg | div);
M
Mark Brown 已提交
377 378
		break;
	case WM8974_MCLKDIV:
379 380
		reg = snd_soc_read(codec, WM8974_CLOCK) & 0x11f;
		snd_soc_write(codec, WM8974_CLOCK, reg | div);
M
Mark Brown 已提交
381 382
		break;
	case WM8974_ADCCLK:
383 384
		reg = snd_soc_read(codec, WM8974_ADC) & 0x1f7;
		snd_soc_write(codec, WM8974_ADC, reg | div);
M
Mark Brown 已提交
385 386
		break;
	case WM8974_DACCLK:
387 388
		reg = snd_soc_read(codec, WM8974_DAC) & 0x1f7;
		snd_soc_write(codec, WM8974_DAC, reg | div);
M
Mark Brown 已提交
389 390
		break;
	case WM8974_BCLKDIV:
391 392
		reg = snd_soc_read(codec, WM8974_CLOCK) & 0x1e3;
		snd_soc_write(codec, WM8974_CLOCK, reg | div);
M
Mark Brown 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static int wm8974_set_dai_fmt(struct snd_soc_dai *codec_dai,
		unsigned int fmt)
{
	struct snd_soc_codec *codec = codec_dai->codec;
	u16 iface = 0;
406
	u16 clk = snd_soc_read(codec, WM8974_CLOCK) & 0x1fe;
M
Mark Brown 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452

	/* set master/slave audio interface */
	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
	case SND_SOC_DAIFMT_CBM_CFM:
		clk |= 0x0001;
		break;
	case SND_SOC_DAIFMT_CBS_CFS:
		break;
	default:
		return -EINVAL;
	}

	/* interface format */
	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
	case SND_SOC_DAIFMT_I2S:
		iface |= 0x0010;
		break;
	case SND_SOC_DAIFMT_RIGHT_J:
		break;
	case SND_SOC_DAIFMT_LEFT_J:
		iface |= 0x0008;
		break;
	case SND_SOC_DAIFMT_DSP_A:
		iface |= 0x00018;
		break;
	default:
		return -EINVAL;
	}

	/* clock inversion */
	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
	case SND_SOC_DAIFMT_NB_NF:
		break;
	case SND_SOC_DAIFMT_IB_IF:
		iface |= 0x0180;
		break;
	case SND_SOC_DAIFMT_IB_NF:
		iface |= 0x0100;
		break;
	case SND_SOC_DAIFMT_NB_IF:
		iface |= 0x0080;
		break;
	default:
		return -EINVAL;
	}

453 454
	snd_soc_write(codec, WM8974_IFACE, iface);
	snd_soc_write(codec, WM8974_CLOCK, clk);
M
Mark Brown 已提交
455 456 457 458 459 460 461 462
	return 0;
}

static int wm8974_pcm_hw_params(struct snd_pcm_substream *substream,
				struct snd_pcm_hw_params *params,
				struct snd_soc_dai *dai)
{
	struct snd_soc_codec *codec = dai->codec;
463 464
	u16 iface = snd_soc_read(codec, WM8974_IFACE) & 0x19f;
	u16 adn = snd_soc_read(codec, WM8974_ADD) & 0x1f1;
M
Mark Brown 已提交
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498

	/* bit size */
	switch (params_format(params)) {
	case SNDRV_PCM_FORMAT_S16_LE:
		break;
	case SNDRV_PCM_FORMAT_S20_3LE:
		iface |= 0x0020;
		break;
	case SNDRV_PCM_FORMAT_S24_LE:
		iface |= 0x0040;
		break;
	case SNDRV_PCM_FORMAT_S32_LE:
		iface |= 0x0060;
		break;
	}

	/* filter coefficient */
	switch (params_rate(params)) {
	case SNDRV_PCM_RATE_8000:
		adn |= 0x5 << 1;
		break;
	case SNDRV_PCM_RATE_11025:
		adn |= 0x4 << 1;
		break;
	case SNDRV_PCM_RATE_16000:
		adn |= 0x3 << 1;
		break;
	case SNDRV_PCM_RATE_22050:
		adn |= 0x2 << 1;
		break;
	case SNDRV_PCM_RATE_32000:
		adn |= 0x1 << 1;
		break;
	case SNDRV_PCM_RATE_44100:
M
Mark Brown 已提交
499
	case SNDRV_PCM_RATE_48000:
M
Mark Brown 已提交
500 501 502
		break;
	}

503 504
	snd_soc_write(codec, WM8974_IFACE, iface);
	snd_soc_write(codec, WM8974_ADD, adn);
M
Mark Brown 已提交
505 506 507 508 509 510
	return 0;
}

static int wm8974_mute(struct snd_soc_dai *dai, int mute)
{
	struct snd_soc_codec *codec = dai->codec;
511
	u16 mute_reg = snd_soc_read(codec, WM8974_DAC) & 0xffbf;
M
Mark Brown 已提交
512

M
Mark Brown 已提交
513
	if (mute)
514
		snd_soc_write(codec, WM8974_DAC, mute_reg | 0x40);
M
Mark Brown 已提交
515
	else
516
		snd_soc_write(codec, WM8974_DAC, mute_reg);
M
Mark Brown 已提交
517 518 519 520 521 522 523
	return 0;
}

/* liam need to make this lower power with dapm */
static int wm8974_set_bias_level(struct snd_soc_codec *codec,
	enum snd_soc_bias_level level)
{
524
	u16 power1 = snd_soc_read(codec, WM8974_POWER1) & ~0x3;
525

M
Mark Brown 已提交
526 527 528
	switch (level) {
	case SND_SOC_BIAS_ON:
	case SND_SOC_BIAS_PREPARE:
529
		power1 |= 0x1;  /* VMID 50k */
530
		snd_soc_write(codec, WM8974_POWER1, power1);
M
Mark Brown 已提交
531
		break;
532

M
Mark Brown 已提交
533
	case SND_SOC_BIAS_STANDBY:
534 535 536 537
		power1 |= WM8974_POWER1_BIASEN | WM8974_POWER1_BUFIOEN;

		if (codec->bias_level == SND_SOC_BIAS_OFF) {
			/* Initial cap charge at VMID 5k */
538
			snd_soc_write(codec, WM8974_POWER1, power1 | 0x3);
539 540 541 542
			mdelay(100);
		}

		power1 |= 0x2;  /* VMID 500k */
543
		snd_soc_write(codec, WM8974_POWER1, power1);
M
Mark Brown 已提交
544
		break;
545

M
Mark Brown 已提交
546
	case SND_SOC_BIAS_OFF:
547 548 549
		snd_soc_write(codec, WM8974_POWER1, 0);
		snd_soc_write(codec, WM8974_POWER2, 0);
		snd_soc_write(codec, WM8974_POWER3, 0);
M
Mark Brown 已提交
550 551
		break;
	}
552

M
Mark Brown 已提交
553 554 555 556
	codec->bias_level = level;
	return 0;
}

M
Mark Brown 已提交
557
#define WM8974_RATES (SNDRV_PCM_RATE_8000_48000)
M
Mark Brown 已提交
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574

#define WM8974_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
	SNDRV_PCM_FMTBIT_S24_LE)

static struct snd_soc_dai_ops wm8974_ops = {
	.hw_params = wm8974_pcm_hw_params,
	.digital_mute = wm8974_mute,
	.set_fmt = wm8974_set_dai_fmt,
	.set_clkdiv = wm8974_set_dai_clkdiv,
	.set_pll = wm8974_set_dai_pll,
};

struct snd_soc_dai wm8974_dai = {
	.name = "WM8974 HiFi",
	.playback = {
		.stream_name = "Playback",
		.channels_min = 1,
M
Mark Brown 已提交
575
		.channels_max = 2,   /* Only 1 channel of data */
M
Mark Brown 已提交
576 577 578 579 580
		.rates = WM8974_RATES,
		.formats = WM8974_FORMATS,},
	.capture = {
		.stream_name = "Capture",
		.channels_min = 1,
M
Mark Brown 已提交
581
		.channels_max = 2,   /* Only 1 channel of data */
M
Mark Brown 已提交
582 583 584
		.rates = WM8974_RATES,
		.formats = WM8974_FORMATS,},
	.ops = &wm8974_ops,
585
	.symmetric_rates = 1,
M
Mark Brown 已提交
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
};
EXPORT_SYMBOL_GPL(wm8974_dai);

static int wm8974_suspend(struct platform_device *pdev, pm_message_t state)
{
	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
	struct snd_soc_codec *codec = socdev->card->codec;

	wm8974_set_bias_level(codec, SND_SOC_BIAS_OFF);
	return 0;
}

static int wm8974_resume(struct platform_device *pdev)
{
	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
	struct snd_soc_codec *codec = socdev->card->codec;
	int i;
	u8 data[2];
	u16 *cache = codec->reg_cache;

	/* Sync reg_cache with the hardware */
	for (i = 0; i < ARRAY_SIZE(wm8974_reg); i++) {
		data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
		data[1] = cache[i] & 0x00ff;
		codec->hw_write(codec->control_data, data, 2);
	}
	wm8974_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
	wm8974_set_bias_level(codec, codec->suspend_bias_level);
	return 0;
}

617
static int wm8974_probe(struct platform_device *pdev)
M
Mark Brown 已提交
618
{
619 620
	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
	struct snd_soc_codec *codec;
M
Mark Brown 已提交
621 622
	int ret = 0;

623 624 625 626
	if (wm8974_codec == NULL) {
		dev_err(&pdev->dev, "Codec device not registered\n");
		return -ENODEV;
	}
M
Mark Brown 已提交
627

628 629
	socdev->card->codec = wm8974_codec;
	codec = wm8974_codec;
M
Mark Brown 已提交
630 631 632

	/* register pcms */
	ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
M
Mark Brown 已提交
633
	if (ret < 0) {
634
		dev_err(codec->dev, "failed to create pcms: %d\n", ret);
M
Mark Brown 已提交
635 636 637
		goto pcm_err;
	}

638 639
	snd_soc_add_controls(codec, wm8974_snd_controls,
			     ARRAY_SIZE(wm8974_snd_controls));
M
Mark Brown 已提交
640 641 642
	wm8974_add_widgets(codec);
	ret = snd_soc_init_card(socdev);
	if (ret < 0) {
643
		dev_err(codec->dev, "failed to register card: %d\n", ret);
M
Mark Brown 已提交
644 645
		goto card_err;
	}
646

M
Mark Brown 已提交
647 648 649 650 651 652 653 654 655
	return ret;

card_err:
	snd_soc_free_pcms(socdev);
	snd_soc_dapm_free(socdev);
pcm_err:
	return ret;
}

656 657 658 659
/* power down chip */
static int wm8974_remove(struct platform_device *pdev)
{
	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
M
Mark Brown 已提交
660

661 662
	snd_soc_free_pcms(socdev);
	snd_soc_dapm_free(socdev);
M
Mark Brown 已提交
663

664 665
	return 0;
}
M
Mark Brown 已提交
666

667 668 669 670 671 672 673
struct snd_soc_codec_device soc_codec_dev_wm8974 = {
	.probe = 	wm8974_probe,
	.remove = 	wm8974_remove,
	.suspend = 	wm8974_suspend,
	.resume =	wm8974_resume,
};
EXPORT_SYMBOL_GPL(soc_codec_dev_wm8974);
M
Mark Brown 已提交
674

675
static __devinit int wm8974_register(struct wm8974_priv *wm8974)
M
Mark Brown 已提交
676 677
{
	int ret;
678
	struct snd_soc_codec *codec = &wm8974->codec;
M
Mark Brown 已提交
679

680 681 682 683
	if (wm8974_codec) {
		dev_err(codec->dev, "Another WM8974 is registered\n");
		return -EINVAL;
	}
M
Mark Brown 已提交
684

685 686 687
	mutex_init(&codec->mutex);
	INIT_LIST_HEAD(&codec->dapm_widgets);
	INIT_LIST_HEAD(&codec->dapm_paths);
M
Mark Brown 已提交
688

689 690 691 692 693 694 695 696 697
	codec->private_data = wm8974;
	codec->name = "WM8974";
	codec->owner = THIS_MODULE;
	codec->bias_level = SND_SOC_BIAS_OFF;
	codec->set_bias_level = wm8974_set_bias_level;
	codec->dai = &wm8974_dai;
	codec->num_dai = 1;
	codec->reg_cache_size = WM8974_CACHEREGNUM;
	codec->reg_cache = &wm8974->reg_cache;
M
Mark Brown 已提交
698

699 700 701 702 703 704
	ret = snd_soc_codec_set_cache_io(codec, 7, 9, SND_SOC_I2C);
	if (ret < 0) {
		dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
		goto err;
	}

705 706 707
	memcpy(codec->reg_cache, wm8974_reg, sizeof(wm8974_reg));

	ret = wm8974_reset(codec);
M
Mark Brown 已提交
708
	if (ret < 0) {
709
		dev_err(codec->dev, "Failed to issue reset\n");
710
		goto err;
M
Mark Brown 已提交
711 712
	}

713 714 715 716 717 718 719 720 721
	wm8974_dai.dev = codec->dev;

	wm8974_set_bias_level(codec, SND_SOC_BIAS_STANDBY);

	wm8974_codec = codec;

	ret = snd_soc_register_codec(codec);
	if (ret != 0) {
		dev_err(codec->dev, "Failed to register codec: %d\n", ret);
722
		goto err;
M
Mark Brown 已提交
723 724
	}

725 726 727
	ret = snd_soc_register_dai(&wm8974_dai);
	if (ret != 0) {
		dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
728
		goto err_codec;
729
	}
M
Mark Brown 已提交
730 731

	return 0;
732 733 734 735 736 737

err_codec:
	snd_soc_unregister_codec(codec);
err:
	kfree(wm8974);
	return ret;
M
Mark Brown 已提交
738 739
}

740
static __devexit void wm8974_unregister(struct wm8974_priv *wm8974)
M
Mark Brown 已提交
741
{
742 743 744 745 746
	wm8974_set_bias_level(&wm8974->codec, SND_SOC_BIAS_OFF);
	snd_soc_unregister_dai(&wm8974_dai);
	snd_soc_unregister_codec(&wm8974->codec);
	kfree(wm8974);
	wm8974_codec = NULL;
M
Mark Brown 已提交
747 748
}

749 750
static __devinit int wm8974_i2c_probe(struct i2c_client *i2c,
				      const struct i2c_device_id *id)
M
Mark Brown 已提交
751
{
752
	struct wm8974_priv *wm8974;
M
Mark Brown 已提交
753 754
	struct snd_soc_codec *codec;

755 756
	wm8974 = kzalloc(sizeof(struct wm8974_priv), GFP_KERNEL);
	if (wm8974 == NULL)
M
Mark Brown 已提交
757 758
		return -ENOMEM;

759 760
	codec = &wm8974->codec;
	codec->hw_write = (hw_write_t)i2c_master_send;
M
Mark Brown 已提交
761

762 763
	i2c_set_clientdata(i2c, wm8974);
	codec->control_data = i2c;
M
Mark Brown 已提交
764

765
	codec->dev = &i2c->dev;
M
Mark Brown 已提交
766

767 768
	return wm8974_register(wm8974);
}
M
Mark Brown 已提交
769

770 771 772 773
static __devexit int wm8974_i2c_remove(struct i2c_client *client)
{
	struct wm8974_priv *wm8974 = i2c_get_clientdata(client);
	wm8974_unregister(wm8974);
M
Mark Brown 已提交
774 775 776
	return 0;
}

777 778 779 780 781 782 783 784
static const struct i2c_device_id wm8974_i2c_id[] = {
	{ "wm8974", 0 },
	{ }
};
MODULE_DEVICE_TABLE(i2c, wm8974_i2c_id);

static struct i2c_driver wm8974_i2c_driver = {
	.driver = {
M
Mark Brown 已提交
785
		.name = "WM8974",
786 787 788 789 790
		.owner = THIS_MODULE,
	},
	.probe =    wm8974_i2c_probe,
	.remove =   __devexit_p(wm8974_i2c_remove),
	.id_table = wm8974_i2c_id,
M
Mark Brown 已提交
791 792 793 794
};

static int __init wm8974_modinit(void)
{
795
	return i2c_add_driver(&wm8974_i2c_driver);
M
Mark Brown 已提交
796 797 798 799 800
}
module_init(wm8974_modinit);

static void __exit wm8974_exit(void)
{
801
	i2c_del_driver(&wm8974_i2c_driver);
M
Mark Brown 已提交
802 803 804 805 806 807
}
module_exit(wm8974_exit);

MODULE_DESCRIPTION("ASoC WM8974 driver");
MODULE_AUTHOR("Liam Girdwood");
MODULE_LICENSE("GPL");