wm8900.c 38.1 KB
Newer Older
M
Mark Brown 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * wm8900.c  --  WM8900 ALSA Soc Audio driver
 *
 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
 *
 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
 *
 * 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.
 *
 * TODO:
 *  - Tristating.
 *  - TDM.
 *  - Jack detect.
 *  - FLL source configuration, currently only MCLK is supported.
 */

#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>
26
#include <linux/regmap.h>
27
#include <linux/spi/spi.h>
28
#include <linux/slab.h>
M
Mark Brown 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <sound/initval.h>
#include <sound/tlv.h>

#include "wm8900.h"

/* WM8900 register space */
#define WM8900_REG_RESET	0x0
#define WM8900_REG_ID		0x0
#define WM8900_REG_POWER1	0x1
#define WM8900_REG_POWER2	0x2
#define WM8900_REG_POWER3	0x3
#define WM8900_REG_AUDIO1	0x4
#define WM8900_REG_AUDIO2	0x5
#define WM8900_REG_CLOCKING1    0x6
#define WM8900_REG_CLOCKING2    0x7
#define WM8900_REG_AUDIO3       0x8
#define WM8900_REG_AUDIO4       0x9
#define WM8900_REG_DACCTRL      0xa
#define WM8900_REG_LDAC_DV      0xb
#define WM8900_REG_RDAC_DV      0xc
#define WM8900_REG_SIDETONE     0xd
#define WM8900_REG_ADCCTRL      0xe
#define WM8900_REG_LADC_DV	0xf
#define WM8900_REG_RADC_DV      0x10
#define WM8900_REG_GPIO         0x12
#define WM8900_REG_INCTL	0x15
#define WM8900_REG_LINVOL	0x16
#define WM8900_REG_RINVOL	0x17
#define WM8900_REG_INBOOSTMIX1  0x18
#define WM8900_REG_INBOOSTMIX2  0x19
#define WM8900_REG_ADCPATH	0x1a
#define WM8900_REG_AUXBOOST	0x1b
#define WM8900_REG_ADDCTL       0x1e
#define WM8900_REG_FLLCTL1      0x24
#define WM8900_REG_FLLCTL2      0x25
#define WM8900_REG_FLLCTL3      0x26
#define WM8900_REG_FLLCTL4      0x27
#define WM8900_REG_FLLCTL5      0x28
#define WM8900_REG_FLLCTL6      0x29
#define WM8900_REG_LOUTMIXCTL1  0x2c
#define WM8900_REG_ROUTMIXCTL1  0x2d
#define WM8900_REG_BYPASS1	0x2e
#define WM8900_REG_BYPASS2	0x2f
#define WM8900_REG_AUXOUT_CTL   0x30
#define WM8900_REG_LOUT1CTL     0x33
#define WM8900_REG_ROUT1CTL     0x34
#define WM8900_REG_LOUT2CTL	0x35
#define WM8900_REG_ROUT2CTL	0x36
#define WM8900_REG_HPCTL1	0x3a
#define WM8900_REG_OUTBIASCTL   0x73

#define WM8900_MAXREG		0x80

#define WM8900_REG_ADDCTL_OUT1_DIS    0x80
#define WM8900_REG_ADDCTL_OUT2_DIS    0x40
#define WM8900_REG_ADDCTL_VMID_DIS    0x20
#define WM8900_REG_ADDCTL_BIAS_SRC    0x10
#define WM8900_REG_ADDCTL_VMID_SOFTST 0x04
#define WM8900_REG_ADDCTL_TEMP_SD     0x02

#define WM8900_REG_GPIO_TEMP_ENA   0x2

#define WM8900_REG_POWER1_STARTUP_BIAS_ENA 0x0100
#define WM8900_REG_POWER1_BIAS_ENA         0x0008
#define WM8900_REG_POWER1_VMID_BUF_ENA     0x0004
#define WM8900_REG_POWER1_FLL_ENA          0x0040

#define WM8900_REG_POWER2_SYSCLK_ENA  0x8000
#define WM8900_REG_POWER2_ADCL_ENA    0x0002
#define WM8900_REG_POWER2_ADCR_ENA    0x0001

#define WM8900_REG_POWER3_DACL_ENA    0x0002
#define WM8900_REG_POWER3_DACR_ENA    0x0001

#define WM8900_REG_AUDIO1_AIF_FMT_MASK 0x0018
#define WM8900_REG_AUDIO1_LRCLK_INV    0x0080
#define WM8900_REG_AUDIO1_BCLK_INV     0x0100

#define WM8900_REG_CLOCKING1_BCLK_DIR   0x1
#define WM8900_REG_CLOCKING1_MCLK_SRC   0x100
A
Axel Lin 已提交
113 114
#define WM8900_REG_CLOCKING1_BCLK_MASK  0x01e
#define WM8900_REG_CLOCKING1_OPCLK_MASK 0x7000
M
Mark Brown 已提交
115 116 117 118 119

#define WM8900_REG_CLOCKING2_ADC_CLKDIV 0xe0
#define WM8900_REG_CLOCKING2_DAC_CLKDIV 0x1c

#define WM8900_REG_DACCTRL_MUTE          0x004
120
#define WM8900_REG_DACCTRL_DAC_SB_FILT   0x100
M
Mark Brown 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
#define WM8900_REG_DACCTRL_AIF_LRCLKRATE 0x400

#define WM8900_REG_AUDIO3_ADCLRC_DIR    0x0800

#define WM8900_REG_AUDIO4_DACLRC_DIR    0x0800

#define WM8900_REG_FLLCTL1_OSC_ENA    0x100

#define WM8900_REG_FLLCTL6_FLL_SLOW_LOCK_REF 0x100

#define WM8900_REG_HPCTL1_HP_IPSTAGE_ENA 0x80
#define WM8900_REG_HPCTL1_HP_OPSTAGE_ENA 0x40
#define WM8900_REG_HPCTL1_HP_CLAMP_IP    0x20
#define WM8900_REG_HPCTL1_HP_CLAMP_OP    0x10
#define WM8900_REG_HPCTL1_HP_SHORT       0x08
#define WM8900_REG_HPCTL1_HP_SHORT2      0x04

A
Axel Lin 已提交
138
#define WM8900_LRC_MASK 0x03ff
M
Mark Brown 已提交
139 140

struct wm8900_priv {
141
	struct regmap *regmap;
142

M
Mark Brown 已提交
143 144 145 146 147 148 149 150
	u32 fll_in; /* FLL input frequency */
	u32 fll_out; /* FLL output frequency */
};

/*
 * wm8900 register cache.  We can't read the entire register space and we
 * have slow control buses so we cache the registers.
 */
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 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
static const struct reg_default wm8900_reg_defaults[] = {
	{  1, 0x0000 },
	{  2, 0xc000 },
	{  3, 0x0000 },
	{  4, 0x4050 },
	{  5, 0x4000 },
	{  6, 0x0008 },
	{  7, 0x0000 },
	{  8, 0x0040 },
	{  9, 0x0040 },
	{ 10, 0x1004 },
	{ 11, 0x00c0 },
	{ 12, 0x00c0 },
	{ 13, 0x0000 },
	{ 14, 0x0100 },
	{ 15, 0x00c0 },
	{ 16, 0x00c0 },
	{ 17, 0x0000 },
	{ 18, 0xb001 },
	{ 19, 0x0000 },
	{ 20, 0x0000 },
	{ 21, 0x0044 },
	{ 22, 0x004c },
	{ 23, 0x004c },
	{ 24, 0x0044 },
	{ 25, 0x0044 },
	{ 26, 0x0000 },
	{ 27, 0x0044 },
	{ 28, 0x0000 },
	{ 29, 0x0000 },
	{ 30, 0x0002 },
	{ 31, 0x0000 },
	{ 32, 0x0000 },
	{ 33, 0x0000 },
	{ 34, 0x0000 },
	{ 35, 0x0000 },
	{ 36, 0x0008 },
	{ 37, 0x0000 },
	{ 38, 0x0000 },
	{ 39, 0x0008 },
	{ 40, 0x0097 },
	{ 41, 0x0100 },
	{ 42, 0x0000 },
	{ 43, 0x0000 },
	{ 44, 0x0050 },
	{ 45, 0x0050 },
	{ 46, 0x0055 },
	{ 47, 0x0055 },
	{ 48, 0x0055 },
	{ 49, 0x0000 },
	{ 50, 0x0000 },
	{ 51, 0x0079 },
	{ 52, 0x0079 },
	{ 53, 0x0079 },
	{ 54, 0x0079 },
	{ 55, 0x0000 },
M
Mark Brown 已提交
207 208
};

209
static bool wm8900_volatile_register(struct device *dev, unsigned int reg)
M
Mark Brown 已提交
210 211 212
{
	switch (reg) {
	case WM8900_REG_ID:
213
		return true;
M
Mark Brown 已提交
214
	default:
215
		return false;
M
Mark Brown 已提交
216 217 218 219 220
	}
}

static void wm8900_reset(struct snd_soc_codec *codec)
{
221
	snd_soc_write(codec, WM8900_REG_RESET, 0);
M
Mark Brown 已提交
222 223 224 225 226
}

static int wm8900_hp_event(struct snd_soc_dapm_widget *w,
			   struct snd_kcontrol *kcontrol, int event)
{
227
	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
228
	u16 hpctl1 = snd_soc_read(codec, WM8900_REG_HPCTL1);
M
Mark Brown 已提交
229 230 231 232 233 234

	switch (event) {
	case SND_SOC_DAPM_PRE_PMU:
		/* Clamp headphone outputs */
		hpctl1 = WM8900_REG_HPCTL1_HP_CLAMP_IP |
			WM8900_REG_HPCTL1_HP_CLAMP_OP;
235
		snd_soc_write(codec, WM8900_REG_HPCTL1, hpctl1);
M
Mark Brown 已提交
236 237 238 239 240 241 242 243
		break;

	case SND_SOC_DAPM_POST_PMU:
		/* Enable the input stage */
		hpctl1 &= ~WM8900_REG_HPCTL1_HP_CLAMP_IP;
		hpctl1 |= WM8900_REG_HPCTL1_HP_SHORT |
			WM8900_REG_HPCTL1_HP_SHORT2 |
			WM8900_REG_HPCTL1_HP_IPSTAGE_ENA;
244
		snd_soc_write(codec, WM8900_REG_HPCTL1, hpctl1);
M
Mark Brown 已提交
245 246 247 248 249 250

		msleep(400);

		/* Enable the output stage */
		hpctl1 &= ~WM8900_REG_HPCTL1_HP_CLAMP_OP;
		hpctl1 |= WM8900_REG_HPCTL1_HP_OPSTAGE_ENA;
251
		snd_soc_write(codec, WM8900_REG_HPCTL1, hpctl1);
M
Mark Brown 已提交
252 253 254

		/* Remove the shorts */
		hpctl1 &= ~WM8900_REG_HPCTL1_HP_SHORT2;
255
		snd_soc_write(codec, WM8900_REG_HPCTL1, hpctl1);
M
Mark Brown 已提交
256
		hpctl1 &= ~WM8900_REG_HPCTL1_HP_SHORT;
257
		snd_soc_write(codec, WM8900_REG_HPCTL1, hpctl1);
M
Mark Brown 已提交
258 259 260 261 262
		break;

	case SND_SOC_DAPM_PRE_PMD:
		/* Short the output */
		hpctl1 |= WM8900_REG_HPCTL1_HP_SHORT;
263
		snd_soc_write(codec, WM8900_REG_HPCTL1, hpctl1);
M
Mark Brown 已提交
264 265 266

		/* Disable the output stage */
		hpctl1 &= ~WM8900_REG_HPCTL1_HP_OPSTAGE_ENA;
267
		snd_soc_write(codec, WM8900_REG_HPCTL1, hpctl1);
M
Mark Brown 已提交
268 269 270 271 272

		/* Clamp the outputs and power down input */
		hpctl1 |= WM8900_REG_HPCTL1_HP_CLAMP_IP |
			WM8900_REG_HPCTL1_HP_CLAMP_OP;
		hpctl1 &= ~WM8900_REG_HPCTL1_HP_IPSTAGE_ENA;
273
		snd_soc_write(codec, WM8900_REG_HPCTL1, hpctl1);
M
Mark Brown 已提交
274 275 276 277
		break;

	case SND_SOC_DAPM_POST_PMD:
		/* Disable everything */
278
		snd_soc_write(codec, WM8900_REG_HPCTL1, 0);
M
Mark Brown 已提交
279 280 281
		break;

	default:
282 283
		WARN(1, "Invalid event %d\n", event);
		break;
M
Mark Brown 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
	}

	return 0;
}

static const DECLARE_TLV_DB_SCALE(out_pga_tlv, -5700, 100, 0);

static const DECLARE_TLV_DB_SCALE(out_mix_tlv, -1500, 300, 0);

static const DECLARE_TLV_DB_SCALE(in_boost_tlv, -1200, 600, 0);

static const DECLARE_TLV_DB_SCALE(in_pga_tlv, -1200, 100, 0);

static const DECLARE_TLV_DB_SCALE(dac_boost_tlv, 0, 600, 0);

static const DECLARE_TLV_DB_SCALE(dac_tlv, -7200, 75, 1);

static const DECLARE_TLV_DB_SCALE(adc_svol_tlv, -3600, 300, 0);

static const DECLARE_TLV_DB_SCALE(adc_tlv, -7200, 75, 1);

static const char *mic_bias_level_txt[] = { "0.9*AVDD", "0.65*AVDD" };

307 308
static SOC_ENUM_SINGLE_DECL(mic_bias_level,
			    WM8900_REG_INCTL, 8, mic_bias_level_txt);
M
Mark Brown 已提交
309 310 311

static const char *dac_mute_rate_txt[] = { "Fast", "Slow" };

312 313
static SOC_ENUM_SINGLE_DECL(dac_mute_rate,
			    WM8900_REG_DACCTRL, 7, dac_mute_rate_txt);
M
Mark Brown 已提交
314 315 316 317 318

static const char *dac_deemphasis_txt[] = {
	"Disabled", "32kHz", "44.1kHz", "48kHz"
};

319 320
static SOC_ENUM_SINGLE_DECL(dac_deemphasis,
			    WM8900_REG_DACCTRL, 4, dac_deemphasis_txt);
M
Mark Brown 已提交
321 322 323 324 325

static const char *adc_hpf_cut_txt[] = {
	"Hi-fi mode", "Voice mode 1", "Voice mode 2", "Voice mode 3"
};

326 327
static SOC_ENUM_SINGLE_DECL(adc_hpf_cut,
			    WM8900_REG_ADCCTRL, 5, adc_hpf_cut_txt);
M
Mark Brown 已提交
328 329 330 331 332

static const char *lr_txt[] = {
	"Left", "Right"
};

333 334
static SOC_ENUM_SINGLE_DECL(aifl_src,
			    WM8900_REG_AUDIO1, 15, lr_txt);
M
Mark Brown 已提交
335

336 337
static SOC_ENUM_SINGLE_DECL(aifr_src,
			    WM8900_REG_AUDIO1, 14, lr_txt);
M
Mark Brown 已提交
338

339 340
static SOC_ENUM_SINGLE_DECL(dacl_src,
			    WM8900_REG_AUDIO2, 15, lr_txt);
M
Mark Brown 已提交
341

342 343
static SOC_ENUM_SINGLE_DECL(dacr_src,
			    WM8900_REG_AUDIO2, 14, lr_txt);
M
Mark Brown 已提交
344 345 346 347 348

static const char *sidetone_txt[] = {
	"Disabled", "Left ADC", "Right ADC"
};

349 350
static SOC_ENUM_SINGLE_DECL(dacl_sidetone,
			    WM8900_REG_SIDETONE, 2, sidetone_txt);
M
Mark Brown 已提交
351

352 353
static SOC_ENUM_SINGLE_DECL(dacr_sidetone,
			    WM8900_REG_SIDETONE, 0, sidetone_txt);
M
Mark Brown 已提交
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 398 399 400 401 402 403 404 405 406 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 453 454 455 456 457 458 459 460 461 462 463 464 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

static const struct snd_kcontrol_new wm8900_snd_controls[] = {
SOC_ENUM("Mic Bias Level", mic_bias_level),

SOC_SINGLE_TLV("Left Input PGA Volume", WM8900_REG_LINVOL, 0, 31, 0,
	       in_pga_tlv),
SOC_SINGLE("Left Input PGA Switch", WM8900_REG_LINVOL, 6, 1, 1),
SOC_SINGLE("Left Input PGA ZC Switch", WM8900_REG_LINVOL, 7, 1, 0),

SOC_SINGLE_TLV("Right Input PGA Volume", WM8900_REG_RINVOL, 0, 31, 0,
	       in_pga_tlv),
SOC_SINGLE("Right Input PGA Switch", WM8900_REG_RINVOL, 6, 1, 1),
SOC_SINGLE("Right Input PGA ZC Switch", WM8900_REG_RINVOL, 7, 1, 0),

SOC_SINGLE("DAC Soft Mute Switch", WM8900_REG_DACCTRL, 6, 1, 1),
SOC_ENUM("DAC Mute Rate", dac_mute_rate),
SOC_SINGLE("DAC Mono Switch", WM8900_REG_DACCTRL, 9, 1, 0),
SOC_ENUM("DAC Deemphasis", dac_deemphasis),
SOC_SINGLE("DAC Sigma-Delta Modulator Clock Switch", WM8900_REG_DACCTRL,
	   12, 1, 0),

SOC_SINGLE("ADC HPF Switch", WM8900_REG_ADCCTRL, 8, 1, 0),
SOC_ENUM("ADC HPF Cut-Off", adc_hpf_cut),
SOC_DOUBLE("ADC Invert Switch", WM8900_REG_ADCCTRL, 1, 0, 1, 0),
SOC_SINGLE_TLV("Left ADC Sidetone Volume", WM8900_REG_SIDETONE, 9, 12, 0,
	       adc_svol_tlv),
SOC_SINGLE_TLV("Right ADC Sidetone Volume", WM8900_REG_SIDETONE, 5, 12, 0,
	       adc_svol_tlv),
SOC_ENUM("Left Digital Audio Source", aifl_src),
SOC_ENUM("Right Digital Audio Source", aifr_src),

SOC_SINGLE_TLV("DAC Input Boost Volume", WM8900_REG_AUDIO2, 10, 4, 0,
	       dac_boost_tlv),
SOC_ENUM("Left DAC Source", dacl_src),
SOC_ENUM("Right DAC Source", dacr_src),
SOC_ENUM("Left DAC Sidetone", dacl_sidetone),
SOC_ENUM("Right DAC Sidetone", dacr_sidetone),
SOC_DOUBLE("DAC Invert Switch", WM8900_REG_DACCTRL, 1, 0, 1, 0),

SOC_DOUBLE_R_TLV("Digital Playback Volume",
		 WM8900_REG_LDAC_DV, WM8900_REG_RDAC_DV,
		 1, 96, 0, dac_tlv),
SOC_DOUBLE_R_TLV("Digital Capture Volume",
		 WM8900_REG_LADC_DV, WM8900_REG_RADC_DV, 1, 119, 0, adc_tlv),

SOC_SINGLE_TLV("LINPUT3 Bypass Volume", WM8900_REG_LOUTMIXCTL1, 4, 7, 0,
	       out_mix_tlv),
SOC_SINGLE_TLV("RINPUT3 Bypass Volume", WM8900_REG_ROUTMIXCTL1, 4, 7, 0,
	       out_mix_tlv),
SOC_SINGLE_TLV("Left AUX Bypass Volume", WM8900_REG_AUXOUT_CTL, 4, 7, 0,
	       out_mix_tlv),
SOC_SINGLE_TLV("Right AUX Bypass Volume", WM8900_REG_AUXOUT_CTL, 0, 7, 0,
	       out_mix_tlv),

SOC_SINGLE_TLV("LeftIn to RightOut Mixer Volume", WM8900_REG_BYPASS1, 0, 7, 0,
	       out_mix_tlv),
SOC_SINGLE_TLV("LeftIn to LeftOut Mixer Volume", WM8900_REG_BYPASS1, 4, 7, 0,
	       out_mix_tlv),
SOC_SINGLE_TLV("RightIn to LeftOut Mixer Volume", WM8900_REG_BYPASS2, 0, 7, 0,
	       out_mix_tlv),
SOC_SINGLE_TLV("RightIn to RightOut Mixer Volume", WM8900_REG_BYPASS2, 4, 7, 0,
	       out_mix_tlv),

SOC_SINGLE_TLV("IN2L Boost Volume", WM8900_REG_INBOOSTMIX1, 0, 3, 0,
	       in_boost_tlv),
SOC_SINGLE_TLV("IN3L Boost Volume", WM8900_REG_INBOOSTMIX1, 4, 3, 0,
	       in_boost_tlv),
SOC_SINGLE_TLV("IN2R Boost Volume", WM8900_REG_INBOOSTMIX2, 0, 3, 0,
	       in_boost_tlv),
SOC_SINGLE_TLV("IN3R Boost Volume", WM8900_REG_INBOOSTMIX2, 4, 3, 0,
	       in_boost_tlv),
SOC_SINGLE_TLV("Left AUX Boost Volume", WM8900_REG_AUXBOOST, 4, 3, 0,
	       in_boost_tlv),
SOC_SINGLE_TLV("Right AUX Boost Volume", WM8900_REG_AUXBOOST, 0, 3, 0,
	       in_boost_tlv),

SOC_DOUBLE_R_TLV("LINEOUT1 Volume", WM8900_REG_LOUT1CTL, WM8900_REG_ROUT1CTL,
	       0, 63, 0, out_pga_tlv),
SOC_DOUBLE_R("LINEOUT1 Switch", WM8900_REG_LOUT1CTL, WM8900_REG_ROUT1CTL,
	     6, 1, 1),
SOC_DOUBLE_R("LINEOUT1 ZC Switch", WM8900_REG_LOUT1CTL, WM8900_REG_ROUT1CTL,
	     7, 1, 0),

SOC_DOUBLE_R_TLV("LINEOUT2 Volume",
		 WM8900_REG_LOUT2CTL, WM8900_REG_ROUT2CTL,
		 0, 63, 0, out_pga_tlv),
SOC_DOUBLE_R("LINEOUT2 Switch",
	     WM8900_REG_LOUT2CTL, WM8900_REG_ROUT2CTL, 6, 1, 1),
SOC_DOUBLE_R("LINEOUT2 ZC Switch",
	     WM8900_REG_LOUT2CTL, WM8900_REG_ROUT2CTL, 7, 1, 0),
SOC_SINGLE("LINEOUT2 LP -12dB", WM8900_REG_LOUTMIXCTL1,
	   0, 1, 1),

};

static const struct snd_kcontrol_new wm8900_dapm_loutput2_control =
SOC_DAPM_SINGLE("LINEOUT2L Switch", WM8900_REG_POWER3, 6, 1, 0);

static const struct snd_kcontrol_new wm8900_dapm_routput2_control =
SOC_DAPM_SINGLE("LINEOUT2R Switch", WM8900_REG_POWER3, 5, 1, 0);

static const struct snd_kcontrol_new wm8900_loutmix_controls[] = {
SOC_DAPM_SINGLE("LINPUT3 Bypass Switch", WM8900_REG_LOUTMIXCTL1, 7, 1, 0),
SOC_DAPM_SINGLE("AUX Bypass Switch", WM8900_REG_AUXOUT_CTL, 7, 1, 0),
SOC_DAPM_SINGLE("Left Input Mixer Switch", WM8900_REG_BYPASS1, 7, 1, 0),
SOC_DAPM_SINGLE("Right Input Mixer Switch", WM8900_REG_BYPASS2, 3, 1, 0),
SOC_DAPM_SINGLE("DACL Switch", WM8900_REG_LOUTMIXCTL1, 8, 1, 0),
};

static const struct snd_kcontrol_new wm8900_routmix_controls[] = {
SOC_DAPM_SINGLE("RINPUT3 Bypass Switch", WM8900_REG_ROUTMIXCTL1, 7, 1, 0),
SOC_DAPM_SINGLE("AUX Bypass Switch", WM8900_REG_AUXOUT_CTL, 3, 1, 0),
SOC_DAPM_SINGLE("Left Input Mixer Switch", WM8900_REG_BYPASS1, 3, 1, 0),
SOC_DAPM_SINGLE("Right Input Mixer Switch", WM8900_REG_BYPASS2, 7, 1, 0),
SOC_DAPM_SINGLE("DACR Switch", WM8900_REG_ROUTMIXCTL1, 8, 1, 0),
};

static const struct snd_kcontrol_new wm8900_linmix_controls[] = {
SOC_DAPM_SINGLE("LINPUT2 Switch", WM8900_REG_INBOOSTMIX1, 2, 1, 1),
SOC_DAPM_SINGLE("LINPUT3 Switch", WM8900_REG_INBOOSTMIX1, 6, 1, 1),
SOC_DAPM_SINGLE("AUX Switch", WM8900_REG_AUXBOOST, 6, 1, 1),
SOC_DAPM_SINGLE("Input PGA Switch", WM8900_REG_ADCPATH, 6, 1, 0),
};

static const struct snd_kcontrol_new wm8900_rinmix_controls[] = {
SOC_DAPM_SINGLE("RINPUT2 Switch", WM8900_REG_INBOOSTMIX2, 2, 1, 1),
SOC_DAPM_SINGLE("RINPUT3 Switch", WM8900_REG_INBOOSTMIX2, 6, 1, 1),
SOC_DAPM_SINGLE("AUX Switch", WM8900_REG_AUXBOOST, 2, 1, 1),
SOC_DAPM_SINGLE("Input PGA Switch", WM8900_REG_ADCPATH, 2, 1, 0),
};

static const struct snd_kcontrol_new wm8900_linpga_controls[] = {
SOC_DAPM_SINGLE("LINPUT1 Switch", WM8900_REG_INCTL, 6, 1, 0),
SOC_DAPM_SINGLE("LINPUT2 Switch", WM8900_REG_INCTL, 5, 1, 0),
SOC_DAPM_SINGLE("LINPUT3 Switch", WM8900_REG_INCTL, 4, 1, 0),
};

static const struct snd_kcontrol_new wm8900_rinpga_controls[] = {
SOC_DAPM_SINGLE("RINPUT1 Switch", WM8900_REG_INCTL, 2, 1, 0),
SOC_DAPM_SINGLE("RINPUT2 Switch", WM8900_REG_INCTL, 1, 1, 0),
SOC_DAPM_SINGLE("RINPUT3 Switch", WM8900_REG_INCTL, 0, 1, 0),
};

497
static const char *wm8900_lp_mux[] = { "Disabled", "Enabled" };
M
Mark Brown 已提交
498

499 500
static SOC_ENUM_SINGLE_DECL(wm8900_lineout2_lp_mux,
			    WM8900_REG_LOUTMIXCTL1, 1, wm8900_lp_mux);
M
Mark Brown 已提交
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539

static const struct snd_kcontrol_new wm8900_lineout2_lp =
SOC_DAPM_ENUM("Route", wm8900_lineout2_lp_mux);

static const struct snd_soc_dapm_widget wm8900_dapm_widgets[] = {

/* Externally visible pins */
SND_SOC_DAPM_OUTPUT("LINEOUT1L"),
SND_SOC_DAPM_OUTPUT("LINEOUT1R"),
SND_SOC_DAPM_OUTPUT("LINEOUT2L"),
SND_SOC_DAPM_OUTPUT("LINEOUT2R"),
SND_SOC_DAPM_OUTPUT("HP_L"),
SND_SOC_DAPM_OUTPUT("HP_R"),

SND_SOC_DAPM_INPUT("RINPUT1"),
SND_SOC_DAPM_INPUT("LINPUT1"),
SND_SOC_DAPM_INPUT("RINPUT2"),
SND_SOC_DAPM_INPUT("LINPUT2"),
SND_SOC_DAPM_INPUT("RINPUT3"),
SND_SOC_DAPM_INPUT("LINPUT3"),
SND_SOC_DAPM_INPUT("AUX"),

SND_SOC_DAPM_VMID("VMID"),

/* Input */
SND_SOC_DAPM_MIXER("Left Input PGA", WM8900_REG_POWER2, 3, 0,
		   wm8900_linpga_controls,
		   ARRAY_SIZE(wm8900_linpga_controls)),
SND_SOC_DAPM_MIXER("Right Input PGA", WM8900_REG_POWER2, 2, 0,
		   wm8900_rinpga_controls,
		   ARRAY_SIZE(wm8900_rinpga_controls)),

SND_SOC_DAPM_MIXER("Left Input Mixer", WM8900_REG_POWER2, 5, 0,
		   wm8900_linmix_controls,
		   ARRAY_SIZE(wm8900_linmix_controls)),
SND_SOC_DAPM_MIXER("Right Input Mixer", WM8900_REG_POWER2, 4, 0,
		   wm8900_rinmix_controls,
		   ARRAY_SIZE(wm8900_rinmix_controls)),

540
SND_SOC_DAPM_SUPPLY("Mic Bias", WM8900_REG_POWER1, 4, 0, NULL, 0),
M
Mark Brown 已提交
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569

SND_SOC_DAPM_ADC("ADCL", "Left HiFi Capture", WM8900_REG_POWER2, 1, 0),
SND_SOC_DAPM_ADC("ADCR", "Right HiFi Capture", WM8900_REG_POWER2, 0, 0),

/* Output */
SND_SOC_DAPM_DAC("DACL", "Left HiFi Playback", WM8900_REG_POWER3, 1, 0),
SND_SOC_DAPM_DAC("DACR", "Right HiFi Playback", WM8900_REG_POWER3, 0, 0),

SND_SOC_DAPM_PGA_E("Headphone Amplifier", WM8900_REG_POWER3, 7, 0, NULL, 0,
		   wm8900_hp_event,
		   SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
		   SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),

SND_SOC_DAPM_PGA("LINEOUT1L PGA", WM8900_REG_POWER2, 8, 0, NULL, 0),
SND_SOC_DAPM_PGA("LINEOUT1R PGA", WM8900_REG_POWER2, 7, 0, NULL, 0),

SND_SOC_DAPM_MUX("LINEOUT2 LP", SND_SOC_NOPM, 0, 0, &wm8900_lineout2_lp),
SND_SOC_DAPM_PGA("LINEOUT2L PGA", WM8900_REG_POWER3, 6, 0, NULL, 0),
SND_SOC_DAPM_PGA("LINEOUT2R PGA", WM8900_REG_POWER3, 5, 0, NULL, 0),

SND_SOC_DAPM_MIXER("Left Output Mixer", WM8900_REG_POWER3, 3, 0,
		   wm8900_loutmix_controls,
		   ARRAY_SIZE(wm8900_loutmix_controls)),
SND_SOC_DAPM_MIXER("Right Output Mixer", WM8900_REG_POWER3, 2, 0,
		   wm8900_routmix_controls,
		   ARRAY_SIZE(wm8900_routmix_controls)),
};

/* Target, Path, Source */
570
static const struct snd_soc_dapm_route wm8900_dapm_routes[] = {
M
Mark Brown 已提交
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 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 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
/* Inputs */
{"Left Input PGA", "LINPUT1 Switch", "LINPUT1"},
{"Left Input PGA", "LINPUT2 Switch", "LINPUT2"},
{"Left Input PGA", "LINPUT3 Switch", "LINPUT3"},

{"Right Input PGA", "RINPUT1 Switch", "RINPUT1"},
{"Right Input PGA", "RINPUT2 Switch", "RINPUT2"},
{"Right Input PGA", "RINPUT3 Switch", "RINPUT3"},

{"Left Input Mixer", "LINPUT2 Switch", "LINPUT2"},
{"Left Input Mixer", "LINPUT3 Switch", "LINPUT3"},
{"Left Input Mixer", "AUX Switch", "AUX"},
{"Left Input Mixer", "Input PGA Switch", "Left Input PGA"},

{"Right Input Mixer", "RINPUT2 Switch", "RINPUT2"},
{"Right Input Mixer", "RINPUT3 Switch", "RINPUT3"},
{"Right Input Mixer", "AUX Switch", "AUX"},
{"Right Input Mixer", "Input PGA Switch", "Right Input PGA"},

{"ADCL", NULL, "Left Input Mixer"},
{"ADCR", NULL, "Right Input Mixer"},

/* Outputs */
{"LINEOUT1L", NULL, "LINEOUT1L PGA"},
{"LINEOUT1L PGA", NULL, "Left Output Mixer"},
{"LINEOUT1R", NULL, "LINEOUT1R PGA"},
{"LINEOUT1R PGA", NULL, "Right Output Mixer"},

{"LINEOUT2L PGA", NULL, "Left Output Mixer"},
{"LINEOUT2 LP", "Disabled", "LINEOUT2L PGA"},
{"LINEOUT2 LP", "Enabled", "Left Output Mixer"},
{"LINEOUT2L", NULL, "LINEOUT2 LP"},

{"LINEOUT2R PGA", NULL, "Right Output Mixer"},
{"LINEOUT2 LP", "Disabled", "LINEOUT2R PGA"},
{"LINEOUT2 LP", "Enabled", "Right Output Mixer"},
{"LINEOUT2R", NULL, "LINEOUT2 LP"},

{"Left Output Mixer", "LINPUT3 Bypass Switch", "LINPUT3"},
{"Left Output Mixer", "AUX Bypass Switch", "AUX"},
{"Left Output Mixer", "Left Input Mixer Switch", "Left Input Mixer"},
{"Left Output Mixer", "Right Input Mixer Switch", "Right Input Mixer"},
{"Left Output Mixer", "DACL Switch", "DACL"},

{"Right Output Mixer", "RINPUT3 Bypass Switch", "RINPUT3"},
{"Right Output Mixer", "AUX Bypass Switch", "AUX"},
{"Right Output Mixer", "Left Input Mixer Switch", "Left Input Mixer"},
{"Right Output Mixer", "Right Input Mixer Switch", "Right Input Mixer"},
{"Right Output Mixer", "DACR Switch", "DACR"},

/* Note that the headphone output stage needs to be connected
 * externally to LINEOUT2 via DC blocking capacitors.  Other
 * configurations are not supported.
 *
 * Note also that left and right headphone paths are treated as a
 * mono path.
 */
{"Headphone Amplifier", NULL, "LINEOUT2 LP"},
{"Headphone Amplifier", NULL, "LINEOUT2 LP"},
{"HP_L", NULL, "Headphone Amplifier"},
{"HP_R", NULL, "Headphone Amplifier"},
};

static int wm8900_hw_params(struct snd_pcm_substream *substream,
635 636
	struct snd_pcm_hw_params *params,
	struct snd_soc_dai *dai)
M
Mark Brown 已提交
637
{
638
	struct snd_soc_codec *codec = dai->codec;
M
Mark Brown 已提交
639 640
	u16 reg;

641
	reg = snd_soc_read(codec, WM8900_REG_AUDIO1) & ~0x60;
M
Mark Brown 已提交
642

643 644
	switch (params_width(params)) {
	case 16:
M
Mark Brown 已提交
645
		break;
646
	case 20:
M
Mark Brown 已提交
647 648
		reg |= 0x20;
		break;
649
	case 24:
M
Mark Brown 已提交
650 651
		reg |= 0x40;
		break;
652
	case 32:
M
Mark Brown 已提交
653 654 655 656 657 658
		reg |= 0x60;
		break;
	default:
		return -EINVAL;
	}

659
	snd_soc_write(codec, WM8900_REG_AUDIO1, reg);
M
Mark Brown 已提交
660

661
	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
662
		reg = snd_soc_read(codec, WM8900_REG_DACCTRL);
663 664 665 666 667 668

		if (params_rate(params) <= 24000)
			reg |= WM8900_REG_DACCTRL_DAC_SB_FILT;
		else
			reg &= ~WM8900_REG_DACCTRL_DAC_SB_FILT;

669
		snd_soc_write(codec, WM8900_REG_DACCTRL, reg);
670 671
	}

M
Mark Brown 已提交
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
	return 0;
}

/* FLL divisors */
struct _fll_div {
	u16 fll_ratio;
	u16 fllclk_div;
	u16 fll_slow_lock_ref;
	u16 n;
	u16 k;
};

/* The size in bits of the FLL divide multiplied by 10
 * to allow rounding later */
#define FIXED_FLL_SIZE ((1 << 16) * 10)

static int fll_factors(struct _fll_div *fll_div, unsigned int Fref,
		       unsigned int Fout)
{
	u64 Kpart;
	unsigned int K, Ndiv, Nmod, target;
	unsigned int div;

695 696
	if (WARN_ON(!Fout))
		return -EINVAL;
M
Mark Brown 已提交
697 698 699 700 701 702 703 704 705 706 707

	/* The FLL must run at 90-100MHz which is then scaled down to
	 * the output value by FLLCLK_DIV. */
	target = Fout;
	div = 1;
	while (target < 90000000) {
		div *= 2;
		target *= 2;
	}

	if (target > 100000000)
708 709
		printk(KERN_WARNING "wm8900: FLL rate %u out of range, Fref=%u"
		       " Fout=%u\n", target, Fref, Fout);
M
Mark Brown 已提交
710 711
	if (div > 32) {
		printk(KERN_ERR "wm8900: Invalid FLL division rate %u, "
712
		       "Fref=%u, Fout=%u, target=%u\n",
M
Mark Brown 已提交
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
		       div, Fref, Fout, target);
		return -EINVAL;
	}

	fll_div->fllclk_div = div >> 2;

	if (Fref < 48000)
		fll_div->fll_slow_lock_ref = 1;
	else
		fll_div->fll_slow_lock_ref = 0;

	Ndiv = target / Fref;

	if (Fref < 1000000)
		fll_div->fll_ratio = 8;
	else
		fll_div->fll_ratio = 1;

	fll_div->n = Ndiv / fll_div->fll_ratio;
	Nmod = (target / fll_div->fll_ratio) % Fref;

	/* Calculate fractional part - scale up so we can round. */
	Kpart = FIXED_FLL_SIZE * (long long)Nmod;

	do_div(Kpart, Fref);

	K = Kpart & 0xFFFFFFFF;

	if ((K % 10) >= 5)
		K += 5;

	/* Move down to proper range now rounding is done */
	fll_div->k = K / 10;

747 748 749
	if (WARN_ON(target != Fout * (fll_div->fllclk_div << 2)) ||
	    WARN_ON(!K && target != Fref * fll_div->fll_ratio * fll_div->n))
		return -EINVAL;
M
Mark Brown 已提交
750 751 752 753 754 755 756

	return 0;
}

static int wm8900_set_fll(struct snd_soc_codec *codec,
	int fll_id, unsigned int freq_in, unsigned int freq_out)
{
757
	struct wm8900_priv *wm8900 = snd_soc_codec_get_drvdata(codec);
M
Mark Brown 已提交
758 759 760 761 762 763
	struct _fll_div fll_div;

	if (wm8900->fll_in == freq_in && wm8900->fll_out == freq_out)
		return 0;

	/* The digital side should be disabled during any change. */
764 765
	snd_soc_update_bits(codec, WM8900_REG_POWER1,
			    WM8900_REG_POWER1_FLL_ENA, 0);
M
Mark Brown 已提交
766 767 768

	/* Disable the FLL? */
	if (!freq_in || !freq_out) {
769 770 771 772
		snd_soc_update_bits(codec, WM8900_REG_CLOCKING1,
				    WM8900_REG_CLOCKING1_MCLK_SRC, 0);
		snd_soc_update_bits(codec, WM8900_REG_FLLCTL1,
				    WM8900_REG_FLLCTL1_OSC_ENA, 0);
M
Mark Brown 已提交
773 774 775 776 777 778 779 780 781 782 783 784 785 786
		wm8900->fll_in = freq_in;
		wm8900->fll_out = freq_out;

		return 0;
	}

	if (fll_factors(&fll_div, freq_in, freq_out) != 0)
		goto reenable;

	wm8900->fll_in = freq_in;
	wm8900->fll_out = freq_out;

	/* The osclilator *MUST* be enabled before we enable the
	 * digital circuit. */
787
	snd_soc_write(codec, WM8900_REG_FLLCTL1,
M
Mark Brown 已提交
788 789
		     fll_div.fll_ratio | WM8900_REG_FLLCTL1_OSC_ENA);

790 791
	snd_soc_write(codec, WM8900_REG_FLLCTL4, fll_div.n >> 5);
	snd_soc_write(codec, WM8900_REG_FLLCTL5,
M
Mark Brown 已提交
792 793 794
		     (fll_div.fllclk_div << 6) | (fll_div.n & 0x1f));

	if (fll_div.k) {
795
		snd_soc_write(codec, WM8900_REG_FLLCTL2,
M
Mark Brown 已提交
796
			     (fll_div.k >> 8) | 0x100);
797
		snd_soc_write(codec, WM8900_REG_FLLCTL3, fll_div.k & 0xff);
M
Mark Brown 已提交
798
	} else
799
		snd_soc_write(codec, WM8900_REG_FLLCTL2, 0);
M
Mark Brown 已提交
800 801

	if (fll_div.fll_slow_lock_ref)
802
		snd_soc_write(codec, WM8900_REG_FLLCTL6,
M
Mark Brown 已提交
803 804
			     WM8900_REG_FLLCTL6_FLL_SLOW_LOCK_REF);
	else
805
		snd_soc_write(codec, WM8900_REG_FLLCTL6, 0);
M
Mark Brown 已提交
806

807 808 809
	snd_soc_update_bits(codec, WM8900_REG_POWER1,
			    WM8900_REG_POWER1_FLL_ENA,
			    WM8900_REG_POWER1_FLL_ENA);
M
Mark Brown 已提交
810 811

reenable:
812 813 814
	snd_soc_update_bits(codec, WM8900_REG_CLOCKING1,
			    WM8900_REG_CLOCKING1_MCLK_SRC,
			    WM8900_REG_CLOCKING1_MCLK_SRC);
M
Mark Brown 已提交
815 816 817
	return 0;
}

818 819
static int wm8900_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
		int source, unsigned int freq_in, unsigned int freq_out)
M
Mark Brown 已提交
820 821 822 823 824 825 826 827 828 829 830
{
	return wm8900_set_fll(codec_dai->codec, pll_id, freq_in, freq_out);
}

static int wm8900_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
				 int div_id, int div)
{
	struct snd_soc_codec *codec = codec_dai->codec;

	switch (div_id) {
	case WM8900_BCLK_DIV:
831 832
		snd_soc_update_bits(codec, WM8900_REG_CLOCKING1,
				    WM8900_REG_CLOCKING1_BCLK_MASK, div);
M
Mark Brown 已提交
833 834
		break;
	case WM8900_OPCLK_DIV:
835 836
		snd_soc_update_bits(codec, WM8900_REG_CLOCKING1,
				    WM8900_REG_CLOCKING1_OPCLK_MASK, div);
M
Mark Brown 已提交
837 838
		break;
	case WM8900_DAC_LRCLK:
839 840
		snd_soc_update_bits(codec, WM8900_REG_AUDIO4,
				    WM8900_LRC_MASK, div);
M
Mark Brown 已提交
841 842
		break;
	case WM8900_ADC_LRCLK:
843 844
		snd_soc_update_bits(codec, WM8900_REG_AUDIO3,
				    WM8900_LRC_MASK, div);
M
Mark Brown 已提交
845 846
		break;
	case WM8900_DAC_CLKDIV:
847 848
		snd_soc_update_bits(codec, WM8900_REG_CLOCKING2,
				    WM8900_REG_CLOCKING2_DAC_CLKDIV, div);
M
Mark Brown 已提交
849 850
		break;
	case WM8900_ADC_CLKDIV:
851 852
		snd_soc_update_bits(codec, WM8900_REG_CLOCKING2,
				    WM8900_REG_CLOCKING2_ADC_CLKDIV, div);
M
Mark Brown 已提交
853 854
		break;
	case WM8900_LRCLK_MODE:
855 856
		snd_soc_update_bits(codec, WM8900_REG_DACCTRL,
				    WM8900_REG_DACCTRL_AIF_LRCLKRATE, div);
M
Mark Brown 已提交
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
		break;
	default:
		return -EINVAL;
	}

	return 0;
}


static int wm8900_set_dai_fmt(struct snd_soc_dai *codec_dai,
			      unsigned int fmt)
{
	struct snd_soc_codec *codec = codec_dai->codec;
	unsigned int clocking1, aif1, aif3, aif4;

872 873 874 875
	clocking1 = snd_soc_read(codec, WM8900_REG_CLOCKING1);
	aif1 = snd_soc_read(codec, WM8900_REG_AUDIO1);
	aif3 = snd_soc_read(codec, WM8900_REG_AUDIO3);
	aif4 = snd_soc_read(codec, WM8900_REG_AUDIO4);
M
Mark Brown 已提交
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970

	/* set master/slave audio interface */
	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
	case SND_SOC_DAIFMT_CBS_CFS:
		clocking1 &= ~WM8900_REG_CLOCKING1_BCLK_DIR;
		aif3 &= ~WM8900_REG_AUDIO3_ADCLRC_DIR;
		aif4 &= ~WM8900_REG_AUDIO4_DACLRC_DIR;
		break;
	case SND_SOC_DAIFMT_CBS_CFM:
		clocking1 &= ~WM8900_REG_CLOCKING1_BCLK_DIR;
		aif3 |= WM8900_REG_AUDIO3_ADCLRC_DIR;
		aif4 |= WM8900_REG_AUDIO4_DACLRC_DIR;
		break;
	case SND_SOC_DAIFMT_CBM_CFM:
		clocking1 |= WM8900_REG_CLOCKING1_BCLK_DIR;
		aif3 |= WM8900_REG_AUDIO3_ADCLRC_DIR;
		aif4 |= WM8900_REG_AUDIO4_DACLRC_DIR;
		break;
	case SND_SOC_DAIFMT_CBM_CFS:
		clocking1 |= WM8900_REG_CLOCKING1_BCLK_DIR;
		aif3 &= ~WM8900_REG_AUDIO3_ADCLRC_DIR;
		aif4 &= ~WM8900_REG_AUDIO4_DACLRC_DIR;
		break;
	default:
		return -EINVAL;
	}

	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
	case SND_SOC_DAIFMT_DSP_A:
		aif1 |= WM8900_REG_AUDIO1_AIF_FMT_MASK;
		aif1 &= ~WM8900_REG_AUDIO1_LRCLK_INV;
		break;
	case SND_SOC_DAIFMT_DSP_B:
		aif1 |= WM8900_REG_AUDIO1_AIF_FMT_MASK;
		aif1 |= WM8900_REG_AUDIO1_LRCLK_INV;
		break;
	case SND_SOC_DAIFMT_I2S:
		aif1 &= ~WM8900_REG_AUDIO1_AIF_FMT_MASK;
		aif1 |= 0x10;
		break;
	case SND_SOC_DAIFMT_RIGHT_J:
		aif1 &= ~WM8900_REG_AUDIO1_AIF_FMT_MASK;
		break;
	case SND_SOC_DAIFMT_LEFT_J:
		aif1 &= ~WM8900_REG_AUDIO1_AIF_FMT_MASK;
		aif1 |= 0x8;
		break;
	default:
		return -EINVAL;
	}

	/* Clock inversion */
	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
	case SND_SOC_DAIFMT_DSP_A:
	case SND_SOC_DAIFMT_DSP_B:
		/* frame inversion not valid for DSP modes */
		switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
		case SND_SOC_DAIFMT_NB_NF:
			aif1 &= ~WM8900_REG_AUDIO1_BCLK_INV;
			break;
		case SND_SOC_DAIFMT_IB_NF:
			aif1 |= WM8900_REG_AUDIO1_BCLK_INV;
			break;
		default:
			return -EINVAL;
		}
		break;
	case SND_SOC_DAIFMT_I2S:
	case SND_SOC_DAIFMT_RIGHT_J:
	case SND_SOC_DAIFMT_LEFT_J:
		switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
		case SND_SOC_DAIFMT_NB_NF:
			aif1 &= ~WM8900_REG_AUDIO1_BCLK_INV;
			aif1 &= ~WM8900_REG_AUDIO1_LRCLK_INV;
			break;
		case SND_SOC_DAIFMT_IB_IF:
			aif1 |= WM8900_REG_AUDIO1_BCLK_INV;
			aif1 |= WM8900_REG_AUDIO1_LRCLK_INV;
			break;
		case SND_SOC_DAIFMT_IB_NF:
			aif1 |= WM8900_REG_AUDIO1_BCLK_INV;
			aif1 &= ~WM8900_REG_AUDIO1_LRCLK_INV;
			break;
		case SND_SOC_DAIFMT_NB_IF:
			aif1 &= ~WM8900_REG_AUDIO1_BCLK_INV;
			aif1 |= WM8900_REG_AUDIO1_LRCLK_INV;
			break;
		default:
			return -EINVAL;
		}
		break;
	default:
		return -EINVAL;
	}

971 972 973 974
	snd_soc_write(codec, WM8900_REG_CLOCKING1, clocking1);
	snd_soc_write(codec, WM8900_REG_AUDIO1, aif1);
	snd_soc_write(codec, WM8900_REG_AUDIO3, aif3);
	snd_soc_write(codec, WM8900_REG_AUDIO4, aif4);
M
Mark Brown 已提交
975 976 977 978 979 980 981 982 983

	return 0;
}

static int wm8900_digital_mute(struct snd_soc_dai *codec_dai, int mute)
{
	struct snd_soc_codec *codec = codec_dai->codec;
	u16 reg;

984
	reg = snd_soc_read(codec, WM8900_REG_DACCTRL);
M
Mark Brown 已提交
985 986 987 988 989 990

	if (mute)
		reg |= WM8900_REG_DACCTRL_MUTE;
	else
		reg &= ~WM8900_REG_DACCTRL_MUTE;

991
	snd_soc_write(codec, WM8900_REG_DACCTRL, reg);
M
Mark Brown 已提交
992 993 994 995 996 997 998 999 1000 1001 1002 1003

	return 0;
}

#define WM8900_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 WM8900_PCM_FORMATS \
	(SNDRV_PCM_FORMAT_S16_LE | SNDRV_PCM_FORMAT_S20_3LE | \
	 SNDRV_PCM_FORMAT_S24_LE)

1004
static const struct snd_soc_dai_ops wm8900_dai_ops = {
1005 1006 1007 1008 1009 1010 1011
	.hw_params	= wm8900_hw_params,
	.set_clkdiv	= wm8900_set_dai_clkdiv,
	.set_pll	= wm8900_set_dai_pll,
	.set_fmt	= wm8900_set_dai_fmt,
	.digital_mute	= wm8900_digital_mute,
};

1012 1013
static struct snd_soc_dai_driver wm8900_dai = {
	.name = "wm8900-hifi",
M
Mark Brown 已提交
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
	.playback = {
		.stream_name = "HiFi Playback",
		.channels_min = 1,
		.channels_max = 2,
		.rates = WM8900_RATES,
		.formats = WM8900_PCM_FORMATS,
	},
	.capture = {
		.stream_name = "HiFi Capture",
		.channels_min = 1,
		.channels_max = 2,
		.rates = WM8900_RATES,
		.formats = WM8900_PCM_FORMATS,
	 },
1028
	.ops = &wm8900_dai_ops,
M
Mark Brown 已提交
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
};

static int wm8900_set_bias_level(struct snd_soc_codec *codec,
				 enum snd_soc_bias_level level)
{
	u16 reg;

	switch (level) {
	case SND_SOC_BIAS_ON:
		/* Enable thermal shutdown */
1039 1040 1041 1042 1043 1044
		snd_soc_update_bits(codec, WM8900_REG_GPIO,
				    WM8900_REG_GPIO_TEMP_ENA,
				    WM8900_REG_GPIO_TEMP_ENA);
		snd_soc_update_bits(codec, WM8900_REG_ADDCTL,
				    WM8900_REG_ADDCTL_TEMP_SD,
				    WM8900_REG_ADDCTL_TEMP_SD);
M
Mark Brown 已提交
1045 1046 1047 1048 1049 1050 1051
		break;

	case SND_SOC_BIAS_PREPARE:
		break;

	case SND_SOC_BIAS_STANDBY:
		/* Charge capacitors if initial power up */
L
Liam Girdwood 已提交
1052
		if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
M
Mark Brown 已提交
1053
			/* STARTUP_BIAS_ENA on */
1054
			snd_soc_write(codec, WM8900_REG_POWER1,
M
Mark Brown 已提交
1055 1056 1057
				     WM8900_REG_POWER1_STARTUP_BIAS_ENA);

			/* Startup bias mode */
1058
			snd_soc_write(codec, WM8900_REG_ADDCTL,
M
Mark Brown 已提交
1059 1060 1061 1062
				     WM8900_REG_ADDCTL_BIAS_SRC |
				     WM8900_REG_ADDCTL_VMID_SOFTST);

			/* VMID 2x50k */
1063
			snd_soc_write(codec, WM8900_REG_POWER1,
M
Mark Brown 已提交
1064 1065 1066 1067 1068 1069
				     WM8900_REG_POWER1_STARTUP_BIAS_ENA | 0x1);

			/* Allow capacitors to charge */
			schedule_timeout_interruptible(msecs_to_jiffies(400));

			/* Enable bias */
1070
			snd_soc_write(codec, WM8900_REG_POWER1,
M
Mark Brown 已提交
1071 1072 1073
				     WM8900_REG_POWER1_STARTUP_BIAS_ENA |
				     WM8900_REG_POWER1_BIAS_ENA | 0x1);

1074
			snd_soc_write(codec, WM8900_REG_ADDCTL, 0);
M
Mark Brown 已提交
1075

1076
			snd_soc_write(codec, WM8900_REG_POWER1,
M
Mark Brown 已提交
1077 1078 1079
				     WM8900_REG_POWER1_BIAS_ENA | 0x1);
		}

1080 1081
		reg = snd_soc_read(codec, WM8900_REG_POWER1);
		snd_soc_write(codec, WM8900_REG_POWER1,
M
Mark Brown 已提交
1082 1083
			     (reg & WM8900_REG_POWER1_FLL_ENA) |
			     WM8900_REG_POWER1_BIAS_ENA | 0x1);
1084
		snd_soc_write(codec, WM8900_REG_POWER2,
M
Mark Brown 已提交
1085
			     WM8900_REG_POWER2_SYSCLK_ENA);
1086
		snd_soc_write(codec, WM8900_REG_POWER3, 0);
M
Mark Brown 已提交
1087 1088 1089 1090
		break;

	case SND_SOC_BIAS_OFF:
		/* Startup bias enable */
1091 1092
		reg = snd_soc_read(codec, WM8900_REG_POWER1);
		snd_soc_write(codec, WM8900_REG_POWER1,
M
Mark Brown 已提交
1093
			     reg & WM8900_REG_POWER1_STARTUP_BIAS_ENA);
1094
		snd_soc_write(codec, WM8900_REG_ADDCTL,
M
Mark Brown 已提交
1095 1096 1097 1098
			     WM8900_REG_ADDCTL_BIAS_SRC |
			     WM8900_REG_ADDCTL_VMID_SOFTST);

		/* Discharge caps */
1099
		snd_soc_write(codec, WM8900_REG_POWER1,
M
Mark Brown 已提交
1100 1101 1102 1103
			     WM8900_REG_POWER1_STARTUP_BIAS_ENA);
		schedule_timeout_interruptible(msecs_to_jiffies(500));

		/* Remove clamp */
1104
		snd_soc_write(codec, WM8900_REG_HPCTL1, 0);
M
Mark Brown 已提交
1105 1106

		/* Power down */
1107 1108 1109 1110
		snd_soc_write(codec, WM8900_REG_ADDCTL, 0);
		snd_soc_write(codec, WM8900_REG_POWER1, 0);
		snd_soc_write(codec, WM8900_REG_POWER2, 0);
		snd_soc_write(codec, WM8900_REG_POWER3, 0);
M
Mark Brown 已提交
1111 1112 1113 1114 1115

		/* Need to let things settle before stopping the clock
		 * to ensure that restart works, see "Stopping the
		 * master clock" in the datasheet. */
		schedule_timeout_interruptible(msecs_to_jiffies(1));
1116
		snd_soc_write(codec, WM8900_REG_POWER2,
M
Mark Brown 已提交
1117 1118 1119
			     WM8900_REG_POWER2_SYSCLK_ENA);
		break;
	}
L
Liam Girdwood 已提交
1120
	codec->dapm.bias_level = level;
M
Mark Brown 已提交
1121 1122 1123
	return 0;
}

1124
static int wm8900_suspend(struct snd_soc_codec *codec)
M
Mark Brown 已提交
1125
{
1126
	struct wm8900_priv *wm8900 = snd_soc_codec_get_drvdata(codec);
M
Mark Brown 已提交
1127 1128 1129 1130 1131 1132 1133
	int fll_out = wm8900->fll_out;
	int fll_in  = wm8900->fll_in;
	int ret;

	/* Stop the FLL in an orderly fashion */
	ret = wm8900_set_fll(codec, 0, 0, 0);
	if (ret != 0) {
1134
		dev_err(codec->dev, "Failed to stop FLL\n");
M
Mark Brown 已提交
1135 1136 1137 1138 1139 1140
		return ret;
	}

	wm8900->fll_out = fll_out;
	wm8900->fll_in = fll_in;

1141
	snd_soc_codec_force_bias_level(codec, SND_SOC_BIAS_OFF);
M
Mark Brown 已提交
1142 1143 1144 1145

	return 0;
}

1146
static int wm8900_resume(struct snd_soc_codec *codec)
M
Mark Brown 已提交
1147
{
1148
	struct wm8900_priv *wm8900 = snd_soc_codec_get_drvdata(codec);
1149
	int ret;
M
Mark Brown 已提交
1150 1151

	wm8900_reset(codec);
1152 1153 1154 1155 1156 1157 1158

	ret = regcache_sync(wm8900->regmap);
	if (ret != 0) {
		dev_err(codec->dev, "Failed to restore cache: %d\n", ret);
		return ret;
	}

1159
	snd_soc_codec_force_bias_level(codec, SND_SOC_BIAS_STANDBY);
M
Mark Brown 已提交
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170

	/* Restart the FLL? */
	if (wm8900->fll_out) {
		int fll_out = wm8900->fll_out;
		int fll_in  = wm8900->fll_in;

		wm8900->fll_in = 0;
		wm8900->fll_out = 0;

		ret = wm8900_set_fll(codec, 0, fll_in, fll_out);
		if (ret != 0) {
1171
			dev_err(codec->dev, "Failed to restart FLL\n");
M
Mark Brown 已提交
1172 1173 1174 1175 1176 1177 1178
			return ret;
		}
	}

	return 0;
}

1179
static int wm8900_probe(struct snd_soc_codec *codec)
M
Mark Brown 已提交
1180
{
1181
	int reg;
1182 1183

	reg = snd_soc_read(codec, WM8900_REG_ID);
M
Mark Brown 已提交
1184
	if (reg != 0x8900) {
1185 1186
		dev_err(codec->dev, "Device is not a WM8900 - ID %x\n", reg);
		return -ENODEV;
M
Mark Brown 已提交
1187 1188 1189 1190
	}

	wm8900_reset(codec);

1191
	/* Turn the chip on */
1192
	snd_soc_codec_force_bias_level(codec, SND_SOC_BIAS_STANDBY);
1193

M
Mark Brown 已提交
1194
	/* Latch the volume update bits */
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
	snd_soc_update_bits(codec, WM8900_REG_LINVOL, 0x100, 0x100);
	snd_soc_update_bits(codec, WM8900_REG_RINVOL, 0x100, 0x100);
	snd_soc_update_bits(codec, WM8900_REG_LOUT1CTL, 0x100, 0x100);
	snd_soc_update_bits(codec, WM8900_REG_ROUT1CTL, 0x100, 0x100);
	snd_soc_update_bits(codec, WM8900_REG_LOUT2CTL, 0x100, 0x100);
	snd_soc_update_bits(codec, WM8900_REG_ROUT2CTL, 0x100, 0x100);
	snd_soc_update_bits(codec, WM8900_REG_LDAC_DV, 0x100, 0x100);
	snd_soc_update_bits(codec, WM8900_REG_RDAC_DV, 0x100, 0x100);
	snd_soc_update_bits(codec, WM8900_REG_LADC_DV, 0x100, 0x100);
	snd_soc_update_bits(codec, WM8900_REG_RADC_DV, 0x100, 0x100);
M
Mark Brown 已提交
1205 1206

	/* Set the DAC and mixer output bias */
1207
	snd_soc_write(codec, WM8900_REG_OUTBIASCTL, 0x81);
M
Mark Brown 已提交
1208

1209 1210
	return 0;
}
M
Mark Brown 已提交
1211

1212 1213 1214 1215 1216
static struct snd_soc_codec_driver soc_codec_dev_wm8900 = {
	.probe =	wm8900_probe,
	.suspend =	wm8900_suspend,
	.resume =	wm8900_resume,
	.set_bias_level = wm8900_set_bias_level,
1217 1218 1219 1220 1221 1222 1223

	.controls = wm8900_snd_controls,
	.num_controls = ARRAY_SIZE(wm8900_snd_controls),
	.dapm_widgets = wm8900_dapm_widgets,
	.num_dapm_widgets = ARRAY_SIZE(wm8900_dapm_widgets),
	.dapm_routes = wm8900_dapm_routes,
	.num_dapm_routes = ARRAY_SIZE(wm8900_dapm_routes),
1224
};
M
Mark Brown 已提交
1225

1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
static const struct regmap_config wm8900_regmap = {
	.reg_bits = 8,
	.val_bits = 16,
	.max_register = WM8900_MAXREG,

	.reg_defaults = wm8900_reg_defaults,
	.num_reg_defaults = ARRAY_SIZE(wm8900_reg_defaults),
	.cache_type = REGCACHE_RBTREE,

	.volatile_reg = wm8900_volatile_register,
};

1238
#if defined(CONFIG_SPI_MASTER)
1239
static int wm8900_spi_probe(struct spi_device *spi)
1240 1241 1242 1243
{
	struct wm8900_priv *wm8900;
	int ret;

1244 1245
	wm8900 = devm_kzalloc(&spi->dev, sizeof(struct wm8900_priv),
			      GFP_KERNEL);
1246 1247
	if (wm8900 == NULL)
		return -ENOMEM;
M
Mark Brown 已提交
1248

1249 1250 1251 1252
	wm8900->regmap = devm_regmap_init_spi(spi, &wm8900_regmap);
	if (IS_ERR(wm8900->regmap))
		return PTR_ERR(wm8900->regmap);

1253 1254 1255 1256
	spi_set_drvdata(spi, wm8900);

	ret = snd_soc_register_codec(&spi->dev,
			&soc_codec_dev_wm8900, &wm8900_dai, 1);
1257

1258
	return ret;
M
Mark Brown 已提交
1259 1260
}

1261
static int wm8900_spi_remove(struct spi_device *spi)
M
Mark Brown 已提交
1262
{
1263 1264 1265
	snd_soc_unregister_codec(&spi->dev);
	return 0;
}
1266

1267 1268
static struct spi_driver wm8900_spi_driver = {
	.driver = {
1269
		.name	= "wm8900",
1270 1271 1272
		.owner	= THIS_MODULE,
	},
	.probe		= wm8900_spi_probe,
1273
	.remove		= wm8900_spi_remove,
1274 1275 1276
};
#endif /* CONFIG_SPI_MASTER */

1277
#if IS_ENABLED(CONFIG_I2C)
1278 1279
static int wm8900_i2c_probe(struct i2c_client *i2c,
			    const struct i2c_device_id *id)
1280 1281 1282 1283
{
	struct wm8900_priv *wm8900;
	int ret;

1284 1285
	wm8900 = devm_kzalloc(&i2c->dev, sizeof(struct wm8900_priv),
			      GFP_KERNEL);
1286 1287 1288
	if (wm8900 == NULL)
		return -ENOMEM;

1289 1290 1291 1292
	wm8900->regmap = devm_regmap_init_i2c(i2c, &wm8900_regmap);
	if (IS_ERR(wm8900->regmap))
		return PTR_ERR(wm8900->regmap);

1293
	i2c_set_clientdata(i2c, wm8900);
1294

1295 1296
	ret =  snd_soc_register_codec(&i2c->dev,
			&soc_codec_dev_wm8900, &wm8900_dai, 1);
1297

1298 1299
	return ret;
}
1300

1301
static int wm8900_i2c_remove(struct i2c_client *client)
1302 1303
{
	snd_soc_unregister_codec(&client->dev);
M
Mark Brown 已提交
1304 1305 1306
	return 0;
}

1307 1308 1309 1310 1311
static const struct i2c_device_id wm8900_i2c_id[] = {
	{ "wm8900", 0 },
	{ }
};
MODULE_DEVICE_TABLE(i2c, wm8900_i2c_id);
M
Mark Brown 已提交
1312 1313 1314

static struct i2c_driver wm8900_i2c_driver = {
	.driver = {
1315
		.name = "wm8900",
M
Mark Brown 已提交
1316 1317
		.owner = THIS_MODULE,
	},
1318
	.probe =    wm8900_i2c_probe,
1319
	.remove =   wm8900_i2c_remove,
1320
	.id_table = wm8900_i2c_id,
M
Mark Brown 已提交
1321
};
1322
#endif
M
Mark Brown 已提交
1323

1324
static int __init wm8900_modinit(void)
M
Mark Brown 已提交
1325 1326
{
	int ret = 0;
1327
#if IS_ENABLED(CONFIG_I2C)
1328 1329 1330 1331
	ret = i2c_add_driver(&wm8900_i2c_driver);
	if (ret != 0) {
		printk(KERN_ERR "Failed to register wm8900 I2C driver: %d\n",
		       ret);
1332
	}
1333 1334 1335 1336 1337 1338
#endif
#if defined(CONFIG_SPI_MASTER)
	ret = spi_register_driver(&wm8900_spi_driver);
	if (ret != 0) {
		printk(KERN_ERR "Failed to register wm8900 SPI driver: %d\n",
		       ret);
1339
	}
1340
#endif
M
Mark Brown 已提交
1341 1342
	return ret;
}
M
Mark Brown 已提交
1343 1344 1345 1346
module_init(wm8900_modinit);

static void __exit wm8900_exit(void)
{
1347
#if IS_ENABLED(CONFIG_I2C)
1348
	i2c_del_driver(&wm8900_i2c_driver);
1349 1350 1351 1352
#endif
#if defined(CONFIG_SPI_MASTER)
	spi_unregister_driver(&wm8900_spi_driver);
#endif
M
Mark Brown 已提交
1353 1354 1355
}
module_exit(wm8900_exit);

M
Mark Brown 已提交
1356 1357 1358
MODULE_DESCRIPTION("ASoC WM8900 driver");
MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfonmicro.com>");
MODULE_LICENSE("GPL");