wm8510.c 18.9 KB
Newer Older
1 2 3 4 5
/*
 * wm8510.c  --  WM8510 ALSA Soc Audio driver
 *
 * Copyright 2006 Wolfson Microelectronics PLC.
 *
6
 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * 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>
21
#include <linux/spi/spi.h>
22
#include <linux/slab.h>
23
#include <linux/of_device.h>
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <sound/initval.h>

#include "wm8510.h"

/*
 * wm8510 register cache
 * We can't read the WM8510 register space when we are
 * using 2 wire for device control, so we cache them instead.
 */
static const u16 wm8510_reg[WM8510_CACHEREGNUM] = {
	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, 0x0001, 0x0000,
	0x0000, 0x0000, 0x0039, 0x0000,
52
	0x0001,
53 54
};

55 56 57
#define WM8510_POWER1_BIASEN  0x08
#define WM8510_POWER1_BUFIOEN 0x10

58
#define wm8510_reset(c)	snd_soc_write(c, WM8510_RESET, 0)
59

60 61 62 63 64
/* codec private data */
struct wm8510_priv {
	enum snd_soc_control_type control_type;
};

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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
static const char *wm8510_companding[] = { "Off", "NC", "u-law", "A-law" };
static const char *wm8510_deemp[] = { "None", "32kHz", "44.1kHz", "48kHz" };
static const char *wm8510_alc[] = { "ALC", "Limiter" };

static const struct soc_enum wm8510_enum[] = {
	SOC_ENUM_SINGLE(WM8510_COMP, 1, 4, wm8510_companding), /* adc */
	SOC_ENUM_SINGLE(WM8510_COMP, 3, 4, wm8510_companding), /* dac */
	SOC_ENUM_SINGLE(WM8510_DAC,  4, 4, wm8510_deemp),
	SOC_ENUM_SINGLE(WM8510_ALC3,  8, 2, wm8510_alc),
};

static const struct snd_kcontrol_new wm8510_snd_controls[] = {

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

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

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

SOC_SINGLE("Master Playback Volume", WM8510_DACVOL, 0, 127, 0),

SOC_SINGLE("High Pass Filter Switch", WM8510_ADC, 8, 1, 0),
SOC_SINGLE("High Pass Cut Off", WM8510_ADC, 4, 7, 0),
SOC_SINGLE("ADC Inversion Switch", WM8510_COMP, 0, 1, 0),

SOC_SINGLE("Capture Volume", WM8510_ADCVOL,  0, 127, 0),

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

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

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

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

SOC_ENUM("ALC Capture Mode", wm8510_enum[3]),
SOC_SINGLE("ALC Capture Decay", WM8510_ALC3,  4, 15, 0),
SOC_SINGLE("ALC Capture Attack", WM8510_ALC3,  0, 15, 0),

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

SOC_SINGLE("Capture PGA ZC Switch", WM8510_INPPGA,  7, 1, 0),
SOC_SINGLE("Capture PGA Volume", WM8510_INPPGA,  0, 63, 0),

SOC_SINGLE("Speaker Playback ZC Switch", WM8510_SPKVOL,  7, 1, 0),
SOC_SINGLE("Speaker Playback Switch", WM8510_SPKVOL,  6, 1, 1),
SOC_SINGLE("Speaker Playback Volume", WM8510_SPKVOL,  0, 63, 0),
SOC_SINGLE("Speaker Boost", WM8510_OUTPUT, 2, 1, 0),

SOC_SINGLE("Capture Boost(+20dB)", WM8510_ADCBOOST,  8, 1, 0),
SOC_SINGLE("Mono Playback Switch", WM8510_MONOMIX, 6, 1, 1),
};

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

/* Mono Output Mixer */
static const struct snd_kcontrol_new wm8510_mono_mixer_controls[] = {
SOC_DAPM_SINGLE("Line Bypass Switch", WM8510_MONOMIX, 1, 1, 0),
SOC_DAPM_SINGLE("Aux Playback Switch", WM8510_MONOMIX, 2, 1, 0),
SOC_DAPM_SINGLE("PCM Playback Switch", WM8510_MONOMIX, 0, 1, 0),
};

static const struct snd_kcontrol_new wm8510_boost_controls[] = {
143
SOC_DAPM_SINGLE("Mic PGA Switch", WM8510_INPPGA,  6, 1, 1),
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
SOC_DAPM_SINGLE("Aux Volume", WM8510_ADCBOOST, 0, 7, 0),
SOC_DAPM_SINGLE("Mic Volume", WM8510_ADCBOOST, 4, 7, 0),
};

static const struct snd_kcontrol_new wm8510_micpga_controls[] = {
SOC_DAPM_SINGLE("MICP Switch", WM8510_INPUT, 0, 1, 0),
SOC_DAPM_SINGLE("MICN Switch", WM8510_INPUT, 1, 1, 0),
SOC_DAPM_SINGLE("AUX Switch", WM8510_INPUT, 2, 1, 0),
};

static const struct snd_soc_dapm_widget wm8510_dapm_widgets[] = {
SND_SOC_DAPM_MIXER("Speaker Mixer", WM8510_POWER3, 2, 0,
	&wm8510_speaker_mixer_controls[0],
	ARRAY_SIZE(wm8510_speaker_mixer_controls)),
SND_SOC_DAPM_MIXER("Mono Mixer", WM8510_POWER3, 3, 0,
	&wm8510_mono_mixer_controls[0],
	ARRAY_SIZE(wm8510_mono_mixer_controls)),
SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8510_POWER3, 0, 0),
SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8510_POWER2, 0, 0),
SND_SOC_DAPM_PGA("Aux Input", WM8510_POWER1, 6, 0, NULL, 0),
SND_SOC_DAPM_PGA("SpkN Out", WM8510_POWER3, 5, 0, NULL, 0),
SND_SOC_DAPM_PGA("SpkP Out", WM8510_POWER3, 6, 0, NULL, 0),
SND_SOC_DAPM_PGA("Mono Out", WM8510_POWER3, 7, 0, NULL, 0),

168 169 170
SND_SOC_DAPM_MIXER("Mic PGA", WM8510_POWER2, 2, 0,
		   &wm8510_micpga_controls[0],
		   ARRAY_SIZE(wm8510_micpga_controls)),
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
SND_SOC_DAPM_MIXER("Boost Mixer", WM8510_POWER2, 4, 0,
	&wm8510_boost_controls[0],
	ARRAY_SIZE(wm8510_boost_controls)),

SND_SOC_DAPM_MICBIAS("Mic Bias", WM8510_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"},

	/* Microphone PGA */
	{"Mic PGA", "MICN Switch", "MICN"},
	{"Mic PGA", "MICP Switch", "MICP"},
	{ "Mic PGA", "AUX Switch", "Aux Input" },

	/* Boost Mixer */
	{"Boost Mixer", "Mic PGA Switch", "Mic PGA"},
	{"Boost Mixer", "Mic Volume", "MICP"},
	{"Boost Mixer", "Aux Volume", "Aux Input"},

	{"ADC", NULL, "Boost Mixer"},
};

static int wm8510_add_widgets(struct snd_soc_codec *codec)
{
219
	struct snd_soc_dapm_context *dapm = &codec->dapm;
220

221 222 223
	snd_soc_dapm_new_controls(dapm, wm8510_dapm_widgets,
				  ARRAY_SIZE(wm8510_dapm_widgets));
	snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
224 225 226 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

	return 0;
}

struct pll_ {
	unsigned int pre_div:4; /* prescale - 1 */
	unsigned int n:4;
	unsigned int k;
};

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
255
			"WM8510 N value %u outwith recommended range!d\n",
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
			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;
}

276 277
static int wm8510_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
		int source, unsigned int freq_in, unsigned int freq_out)
278 279 280 281 282 283
{
	struct snd_soc_codec *codec = codec_dai->codec;
	u16 reg;

	if (freq_in == 0 || freq_out == 0) {
		/* Clock CODEC directly from MCLK */
284 285
		reg = snd_soc_read(codec, WM8510_CLOCK);
		snd_soc_write(codec, WM8510_CLOCK, reg & 0x0ff);
286 287

		/* Turn off PLL */
288 289
		reg = snd_soc_read(codec, WM8510_POWER1);
		snd_soc_write(codec, WM8510_POWER1, reg & 0x1df);
290 291 292
		return 0;
	}

293
	pll_factors(freq_out*4, freq_in);
294

295 296 297 298 299 300
	snd_soc_write(codec, WM8510_PLLN, (pll_div.pre_div << 4) | pll_div.n);
	snd_soc_write(codec, WM8510_PLLK1, pll_div.k >> 18);
	snd_soc_write(codec, WM8510_PLLK2, (pll_div.k >> 9) & 0x1ff);
	snd_soc_write(codec, WM8510_PLLK3, pll_div.k & 0x1ff);
	reg = snd_soc_read(codec, WM8510_POWER1);
	snd_soc_write(codec, WM8510_POWER1, reg | 0x020);
301 302

	/* Run CODEC from PLL instead of MCLK */
303 304
	reg = snd_soc_read(codec, WM8510_CLOCK);
	snd_soc_write(codec, WM8510_CLOCK, reg | 0x100);
305 306 307 308 309 310 311

	return 0;
}

/*
 * Configure WM8510 clock dividers.
 */
312
static int wm8510_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
313 314 315 316 317 318 319
		int div_id, int div)
{
	struct snd_soc_codec *codec = codec_dai->codec;
	u16 reg;

	switch (div_id) {
	case WM8510_OPCLKDIV:
320 321
		reg = snd_soc_read(codec, WM8510_GPIO) & 0x1cf;
		snd_soc_write(codec, WM8510_GPIO, reg | div);
322 323
		break;
	case WM8510_MCLKDIV:
324 325
		reg = snd_soc_read(codec, WM8510_CLOCK) & 0x11f;
		snd_soc_write(codec, WM8510_CLOCK, reg | div);
326 327
		break;
	case WM8510_ADCCLK:
328 329
		reg = snd_soc_read(codec, WM8510_ADC) & 0x1f7;
		snd_soc_write(codec, WM8510_ADC, reg | div);
330 331
		break;
	case WM8510_DACCLK:
332 333
		reg = snd_soc_read(codec, WM8510_DAC) & 0x1f7;
		snd_soc_write(codec, WM8510_DAC, reg | div);
334 335
		break;
	case WM8510_BCLKDIV:
336 337
		reg = snd_soc_read(codec, WM8510_CLOCK) & 0x1e3;
		snd_soc_write(codec, WM8510_CLOCK, reg | div);
338 339 340 341 342 343 344 345
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

346
static int wm8510_set_dai_fmt(struct snd_soc_dai *codec_dai,
347 348 349 350
		unsigned int fmt)
{
	struct snd_soc_codec *codec = codec_dai->codec;
	u16 iface = 0;
351
	u16 clk = snd_soc_read(codec, WM8510_CLOCK) & 0x1fe;
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397

	/* 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;
	}

398 399
	snd_soc_write(codec, WM8510_IFACE, iface);
	snd_soc_write(codec, WM8510_CLOCK, clk);
400 401 402 403
	return 0;
}

static int wm8510_pcm_hw_params(struct snd_pcm_substream *substream,
404 405
				struct snd_pcm_hw_params *params,
				struct snd_soc_dai *dai)
406 407
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
408
	struct snd_soc_codec *codec = rtd->codec;
409 410
	u16 iface = snd_soc_read(codec, WM8510_IFACE) & 0x19f;
	u16 adn = snd_soc_read(codec, WM8510_ADD) & 0x1f1;
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428

	/* 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)) {
429
	case 8000:
430 431
		adn |= 0x5 << 1;
		break;
432
	case 11025:
433 434
		adn |= 0x4 << 1;
		break;
435
	case 16000:
436 437
		adn |= 0x3 << 1;
		break;
438
	case 22050:
439 440
		adn |= 0x2 << 1;
		break;
441
	case 32000:
442 443
		adn |= 0x1 << 1;
		break;
444 445
	case 44100:
	case 48000:
446 447 448
		break;
	}

449 450
	snd_soc_write(codec, WM8510_IFACE, iface);
	snd_soc_write(codec, WM8510_ADD, adn);
451 452 453
	return 0;
}

454
static int wm8510_mute(struct snd_soc_dai *dai, int mute)
455 456
{
	struct snd_soc_codec *codec = dai->codec;
457
	u16 mute_reg = snd_soc_read(codec, WM8510_DAC) & 0xffbf;
458 459

	if (mute)
460
		snd_soc_write(codec, WM8510_DAC, mute_reg | 0x40);
461
	else
462
		snd_soc_write(codec, WM8510_DAC, mute_reg);
463 464 465 466 467 468 469
	return 0;
}

/* liam need to make this lower power with dapm */
static int wm8510_set_bias_level(struct snd_soc_codec *codec,
	enum snd_soc_bias_level level)
{
470
	u16 power1 = snd_soc_read(codec, WM8510_POWER1) & ~0x3;
471 472 473 474

	switch (level) {
	case SND_SOC_BIAS_ON:
	case SND_SOC_BIAS_PREPARE:
475
		power1 |= 0x1;  /* VMID 50k */
476
		snd_soc_write(codec, WM8510_POWER1, power1);
477 478
		break;

479
	case SND_SOC_BIAS_STANDBY:
480 481
		power1 |= WM8510_POWER1_BIASEN | WM8510_POWER1_BUFIOEN;

482
		if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
483
			/* Initial cap charge at VMID 5k */
484
			snd_soc_write(codec, WM8510_POWER1, power1 | 0x3);
485 486 487 488
			mdelay(100);
		}

		power1 |= 0x2;  /* VMID 500k */
489
		snd_soc_write(codec, WM8510_POWER1, power1);
490
		break;
491

492
	case SND_SOC_BIAS_OFF:
493 494 495
		snd_soc_write(codec, WM8510_POWER1, 0);
		snd_soc_write(codec, WM8510_POWER2, 0);
		snd_soc_write(codec, WM8510_POWER3, 0);
496 497
		break;
	}
498

499
	codec->dapm.bias_level = level;
500 501 502 503 504 505 506 507 508 509
	return 0;
}

#define WM8510_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
		SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
		SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)

#define WM8510_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
	SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)

510 511 512 513 514 515 516 517
static struct snd_soc_dai_ops wm8510_dai_ops = {
	.hw_params	= wm8510_pcm_hw_params,
	.digital_mute	= wm8510_mute,
	.set_fmt	= wm8510_set_dai_fmt,
	.set_clkdiv	= wm8510_set_dai_clkdiv,
	.set_pll	= wm8510_set_dai_pll,
};

518 519
static struct snd_soc_dai_driver wm8510_dai = {
	.name = "wm8510-hifi",
520 521 522 523 524 525 526 527 528 529 530 531
	.playback = {
		.stream_name = "Playback",
		.channels_min = 2,
		.channels_max = 2,
		.rates = WM8510_RATES,
		.formats = WM8510_FORMATS,},
	.capture = {
		.stream_name = "Capture",
		.channels_min = 2,
		.channels_max = 2,
		.rates = WM8510_RATES,
		.formats = WM8510_FORMATS,},
532
	.ops = &wm8510_dai_ops,
533
	.symmetric_rates = 1,
534 535
};

536
static int wm8510_suspend(struct snd_soc_codec *codec, pm_message_t state)
537 538 539 540 541
{
	wm8510_set_bias_level(codec, SND_SOC_BIAS_OFF);
	return 0;
}

542
static int wm8510_resume(struct snd_soc_codec *codec)
543 544 545 546 547 548 549 550 551 552 553 554
{
	int i;
	u8 data[2];
	u16 *cache = codec->reg_cache;

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

556 557 558
	return 0;
}

559
static int wm8510_probe(struct snd_soc_codec *codec)
560
{
561 562
	struct wm8510_priv *wm8510 = snd_soc_codec_get_drvdata(codec);
	int ret;
563

564
	ret = snd_soc_codec_set_cache_io(codec, 7, 9,  wm8510->control_type);
565
	if (ret < 0) {
566 567
		printk(KERN_ERR "wm8510: failed to set cache I/O: %d\n", ret);
		return ret;
568 569
	}

570 571 572 573
	wm8510_reset(codec);

	/* power on device */
	wm8510_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
574 575
	snd_soc_add_controls(codec, wm8510_snd_controls,
				ARRAY_SIZE(wm8510_snd_controls));
576
	wm8510_add_widgets(codec);
577

578 579 580
	return ret;
}

581 582
/* power down chip */
static int wm8510_remove(struct snd_soc_codec *codec)
583
{
584
	struct wm8510_priv *wm8510 = snd_soc_codec_get_drvdata(codec);
585

586 587
	wm8510_set_bias_level(codec, SND_SOC_BIAS_OFF);
	kfree(wm8510);
588 589 590
	return 0;
}

591 592 593 594 595 596 597 598 599
static struct snd_soc_codec_driver soc_codec_dev_wm8510 = {
	.probe =	wm8510_probe,
	.remove =	wm8510_remove,
	.suspend =	wm8510_suspend,
	.resume =	wm8510_resume,
	.set_bias_level = wm8510_set_bias_level,
	.reg_cache_size = ARRAY_SIZE(wm8510_reg),
	.reg_word_size = sizeof(u16),
	.reg_cache_default =wm8510_reg,
600 601
};

602 603 604 605 606
static const struct of_device_id wm8510_of_match[] = {
	{ .compatible = "wlf,wm8510" },
	{ },
};

607 608 609
#if defined(CONFIG_SPI_MASTER)
static int __devinit wm8510_spi_probe(struct spi_device *spi)
{
610
	struct wm8510_priv *wm8510;
611 612
	int ret;

613 614 615 616 617 618
	wm8510 = kzalloc(sizeof(struct wm8510_priv), GFP_KERNEL);
	if (wm8510 == NULL)
		return -ENOMEM;

	wm8510->control_type = SND_SOC_SPI;
	spi_set_drvdata(spi, wm8510);
619

620 621
	ret = snd_soc_register_codec(&spi->dev,
			&soc_codec_dev_wm8510, &wm8510_dai, 1);
622
	if (ret < 0)
623
		kfree(wm8510);
624 625 626 627 628
	return ret;
}

static int __devexit wm8510_spi_remove(struct spi_device *spi)
{
629
	snd_soc_unregister_codec(&spi->dev);
630 631 632 633 634 635 636
	return 0;
}

static struct spi_driver wm8510_spi_driver = {
	.driver = {
		.name	= "wm8510",
		.owner	= THIS_MODULE,
637
		.of_match_table = wm8510_of_match,
638 639 640 641 642 643
	},
	.probe		= wm8510_spi_probe,
	.remove		= __devexit_p(wm8510_spi_remove),
};
#endif /* CONFIG_SPI_MASTER */

644 645 646
#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
static __devinit int wm8510_i2c_probe(struct i2c_client *i2c,
				      const struct i2c_device_id *id)
647
{
648 649
	struct wm8510_priv *wm8510;
	int ret;
650

651 652
	wm8510 = kzalloc(sizeof(struct wm8510_priv), GFP_KERNEL);
	if (wm8510 == NULL)
653 654
		return -ENOMEM;

655 656
	i2c_set_clientdata(i2c, wm8510);
	wm8510->control_type = SND_SOC_I2C;
657

658 659 660 661
	ret =  snd_soc_register_codec(&i2c->dev,
			&soc_codec_dev_wm8510, &wm8510_dai, 1);
	if (ret < 0)
		kfree(wm8510);
662 663 664
	return ret;
}

665
static __devexit int wm8510_i2c_remove(struct i2c_client *client)
666
{
667
	snd_soc_unregister_codec(&client->dev);
668 669 670
	return 0;
}

671 672 673 674 675 676 677 678 679 680
static const struct i2c_device_id wm8510_i2c_id[] = {
	{ "wm8510", 0 },
	{ }
};
MODULE_DEVICE_TABLE(i2c, wm8510_i2c_id);

static struct i2c_driver wm8510_i2c_driver = {
	.driver = {
		.name = "wm8510-codec",
		.owner = THIS_MODULE,
681
		.of_match_table = wm8510_of_match,
682 683 684 685
	},
	.probe =    wm8510_i2c_probe,
	.remove =   __devexit_p(wm8510_i2c_remove),
	.id_table = wm8510_i2c_id,
686
};
687
#endif
688

689
static int __init wm8510_modinit(void)
690
{
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
	int ret = 0;
#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
	ret = i2c_add_driver(&wm8510_i2c_driver);
	if (ret != 0) {
		printk(KERN_ERR "Failed to register WM8510 I2C driver: %d\n",
		       ret);
	}
#endif
#if defined(CONFIG_SPI_MASTER)
	ret = spi_register_driver(&wm8510_spi_driver);
	if (ret != 0) {
		printk(KERN_ERR "Failed to register WM8510 SPI driver: %d\n",
		       ret);
	}
#endif
	return ret;
707 708 709 710 711
}
module_init(wm8510_modinit);

static void __exit wm8510_exit(void)
{
712 713 714 715 716 717
#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
	i2c_del_driver(&wm8510_i2c_driver);
#endif
#if defined(CONFIG_SPI_MASTER)
	spi_unregister_driver(&wm8510_spi_driver);
#endif
718 719 720
}
module_exit(wm8510_exit);

721 722 723
MODULE_DESCRIPTION("ASoC WM8510 driver");
MODULE_AUTHOR("Liam Girdwood");
MODULE_LICENSE("GPL");
反馈
建议
客服 返回
顶部