ad193x.c 13.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * AD193X Audio Codec driver supporting AD1936/7/8/9
 *
 * Copyright 2010 Analog Devices Inc.
 *
 * Licensed under the GPL-2 or later.
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/i2c.h>
#include <linux/spi/spi.h>
15
#include <linux/slab.h>
16 17 18 19 20 21 22 23 24 25
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/initval.h>
#include <sound/soc.h>
#include <sound/tlv.h>
#include "ad193x.h"

/* codec private data */
struct ad193x_priv {
26
	struct regmap *regmap;
27
	int sysclk;
28 29 30 31 32
};

/*
 * AD193X volume/mute/de-emphasis etc. controls
 */
33
static const char * const ad193x_deemp[] = {"None", "48kHz", "44.1kHz", "32kHz"};
34 35 36 37

static const struct soc_enum ad193x_deemp_enum =
	SOC_ENUM_SINGLE(AD193X_DAC_CTRL2, 1, 4, ad193x_deemp);

38 39
static const DECLARE_TLV_DB_MINMAX(adau193x_tlv, -9563, 0);

40 41
static const struct snd_kcontrol_new ad193x_snd_controls[] = {
	/* DAC volume control */
42 43 44 45 46 47 48 49
	SOC_DOUBLE_R_TLV("DAC1 Volume", AD193X_DAC_L1_VOL,
			AD193X_DAC_R1_VOL, 0, 0xFF, 1, adau193x_tlv),
	SOC_DOUBLE_R_TLV("DAC2 Volume", AD193X_DAC_L2_VOL,
			AD193X_DAC_R2_VOL, 0, 0xFF, 1, adau193x_tlv),
	SOC_DOUBLE_R_TLV("DAC3 Volume", AD193X_DAC_L3_VOL,
			AD193X_DAC_R3_VOL, 0, 0xFF, 1, adau193x_tlv),
	SOC_DOUBLE_R_TLV("DAC4 Volume", AD193X_DAC_L4_VOL,
			AD193X_DAC_R4_VOL, 0, 0xFF, 1, adau193x_tlv),
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

	/* ADC switch control */
	SOC_DOUBLE("ADC1 Switch", AD193X_ADC_CTRL0, AD193X_ADCL1_MUTE,
		AD193X_ADCR1_MUTE, 1, 1),
	SOC_DOUBLE("ADC2 Switch", AD193X_ADC_CTRL0, AD193X_ADCL2_MUTE,
		AD193X_ADCR2_MUTE, 1, 1),

	/* DAC switch control */
	SOC_DOUBLE("DAC1 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL1_MUTE,
		AD193X_DACR1_MUTE, 1, 1),
	SOC_DOUBLE("DAC2 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL2_MUTE,
		AD193X_DACR2_MUTE, 1, 1),
	SOC_DOUBLE("DAC3 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL3_MUTE,
		AD193X_DACR3_MUTE, 1, 1),
	SOC_DOUBLE("DAC4 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL4_MUTE,
		AD193X_DACR4_MUTE, 1, 1),

	/* ADC high-pass filter */
	SOC_SINGLE("ADC High Pass Filter Switch", AD193X_ADC_CTRL0,
			AD193X_ADC_HIGHPASS_FILTER, 1, 0),

	/* DAC de-emphasis */
	SOC_ENUM("Playback Deemphasis", ad193x_deemp_enum),
};

static const struct snd_soc_dapm_widget ad193x_dapm_widgets[] = {
	SND_SOC_DAPM_DAC("DAC", "Playback", AD193X_DAC_CTRL0, 0, 1),
	SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0),
	SND_SOC_DAPM_SUPPLY("PLL_PWR", AD193X_PLL_CLK_CTRL0, 0, 1, NULL, 0),
	SND_SOC_DAPM_SUPPLY("ADC_PWR", AD193X_ADC_CTRL0, 0, 1, NULL, 0),
80
	SND_SOC_DAPM_SUPPLY("SYSCLK", AD193X_PLL_CLK_CTRL0, 7, 0, NULL, 0),
81 82 83 84 85 86 87 88 89
	SND_SOC_DAPM_OUTPUT("DAC1OUT"),
	SND_SOC_DAPM_OUTPUT("DAC2OUT"),
	SND_SOC_DAPM_OUTPUT("DAC3OUT"),
	SND_SOC_DAPM_OUTPUT("DAC4OUT"),
	SND_SOC_DAPM_INPUT("ADC1IN"),
	SND_SOC_DAPM_INPUT("ADC2IN"),
};

static const struct snd_soc_dapm_route audio_paths[] = {
90 91
	{ "DAC", NULL, "SYSCLK" },
	{ "ADC", NULL, "SYSCLK" },
92 93
	{ "DAC", NULL, "ADC_PWR" },
	{ "ADC", NULL, "ADC_PWR" },
94 95 96 97 98 99
	{ "DAC1OUT", NULL, "DAC" },
	{ "DAC2OUT", NULL, "DAC" },
	{ "DAC3OUT", NULL, "DAC" },
	{ "DAC4OUT", NULL, "DAC" },
	{ "ADC", NULL, "ADC1IN" },
	{ "ADC", NULL, "ADC2IN" },
100
	{ "SYSCLK", NULL, "PLL_PWR" },
101 102 103 104 105 106 107 108
};

/*
 * DAI ops entries
 */

static int ad193x_mute(struct snd_soc_dai *dai, int mute)
{
109
	struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(dai->codec);
110

111
	if (mute)
112
		regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL2,
113 114 115
				    AD193X_DAC_MASTER_MUTE,
				    AD193X_DAC_MASTER_MUTE);
	else
116
		regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL2,
117
				    AD193X_DAC_MASTER_MUTE, 0);
118 119 120 121 122 123 124

	return 0;
}

static int ad193x_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
			       unsigned int rx_mask, int slots, int width)
{
125
	struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(dai->codec);
126
	unsigned int channels;
127 128 129

	switch (slots) {
	case 2:
130
		channels = AD193X_2_CHANNELS;
131 132
		break;
	case 4:
133
		channels = AD193X_4_CHANNELS;
134 135
		break;
	case 8:
136
		channels = AD193X_8_CHANNELS;
137 138
		break;
	case 16:
139
		channels = AD193X_16_CHANNELS;
140 141 142 143 144
		break;
	default:
		return -EINVAL;
	}

145 146 147 148
	regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL1,
		AD193X_DAC_CHAN_MASK, channels << AD193X_DAC_CHAN_SHFT);
	regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL2,
		AD193X_ADC_CHAN_MASK, channels << AD193X_ADC_CHAN_SHFT);
149 150 151 152 153 154 155

	return 0;
}

static int ad193x_set_dai_fmt(struct snd_soc_dai *codec_dai,
		unsigned int fmt)
{
156
	struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(codec_dai->codec);
157 158 159
	unsigned int adc_serfmt = 0;
	unsigned int adc_fmt = 0;
	unsigned int dac_fmt = 0;
160 161 162 163 164 165

	/* At present, the driver only support AUX ADC mode(SND_SOC_DAIFMT_I2S
	 * with TDM) and ADC&DAC TDM mode(SND_SOC_DAIFMT_DSP_A)
	 */
	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
	case SND_SOC_DAIFMT_I2S:
166
		adc_serfmt |= AD193X_ADC_SERFMT_TDM;
167 168
		break;
	case SND_SOC_DAIFMT_DSP_A:
169
		adc_serfmt |= AD193X_ADC_SERFMT_AUX;
170 171 172 173 174 175 176 177 178
		break;
	default:
		return -EINVAL;
	}

	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
	case SND_SOC_DAIFMT_NB_NF: /* normal bit clock + frame */
		break;
	case SND_SOC_DAIFMT_NB_IF: /* normal bclk + invert frm */
179 180
		adc_fmt |= AD193X_ADC_LEFT_HIGH;
		dac_fmt |= AD193X_DAC_LEFT_HIGH;
181 182
		break;
	case SND_SOC_DAIFMT_IB_NF: /* invert bclk + normal frm */
183 184
		adc_fmt |= AD193X_ADC_BCLK_INV;
		dac_fmt |= AD193X_DAC_BCLK_INV;
185 186
		break;
	case SND_SOC_DAIFMT_IB_IF: /* invert bclk + frm */
187 188 189 190
		adc_fmt |= AD193X_ADC_LEFT_HIGH;
		adc_fmt |= AD193X_ADC_BCLK_INV;
		dac_fmt |= AD193X_DAC_LEFT_HIGH;
		dac_fmt |= AD193X_DAC_BCLK_INV;
191 192 193 194 195 196 197
		break;
	default:
		return -EINVAL;
	}

	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
	case SND_SOC_DAIFMT_CBM_CFM: /* codec clk & frm master */
198 199 200 201
		adc_fmt |= AD193X_ADC_LCR_MASTER;
		adc_fmt |= AD193X_ADC_BCLK_MASTER;
		dac_fmt |= AD193X_DAC_LCR_MASTER;
		dac_fmt |= AD193X_DAC_BCLK_MASTER;
202 203
		break;
	case SND_SOC_DAIFMT_CBS_CFM: /* codec clk slave & frm master */
204 205
		adc_fmt |= AD193X_ADC_LCR_MASTER;
		dac_fmt |= AD193X_DAC_LCR_MASTER;
206 207
		break;
	case SND_SOC_DAIFMT_CBM_CFS: /* codec clk master & frame slave */
208 209
		adc_fmt |= AD193X_ADC_BCLK_MASTER;
		dac_fmt |= AD193X_DAC_BCLK_MASTER;
210 211 212 213 214 215 216
		break;
	case SND_SOC_DAIFMT_CBS_CFS: /* codec clk & frm slave */
		break;
	default:
		return -EINVAL;
	}

217 218 219 220 221 222
	regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL1,
		AD193X_ADC_SERFMT_MASK, adc_serfmt);
	regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL2,
		AD193X_ADC_FMT_MASK, adc_fmt);
	regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL1,
		AD193X_DAC_FMT_MASK, dac_fmt);
223 224 225 226

	return 0;
}

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
static int ad193x_set_dai_sysclk(struct snd_soc_dai *codec_dai,
		int clk_id, unsigned int freq, int dir)
{
	struct snd_soc_codec *codec = codec_dai->codec;
	struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(codec);
	switch (freq) {
	case 12288000:
	case 18432000:
	case 24576000:
	case 36864000:
		ad193x->sysclk = freq;
		return 0;
	}
	return -EINVAL;
}

243 244 245 246
static int ad193x_hw_params(struct snd_pcm_substream *substream,
		struct snd_pcm_hw_params *params,
		struct snd_soc_dai *dai)
{
247
	int word_len = 0, master_rate = 0;
248
	struct snd_soc_codec *codec = dai->codec;
249
	struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(codec);
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

	/* bit size */
	switch (params_format(params)) {
	case SNDRV_PCM_FORMAT_S16_LE:
		word_len = 3;
		break;
	case SNDRV_PCM_FORMAT_S20_3LE:
		word_len = 1;
		break;
	case SNDRV_PCM_FORMAT_S24_LE:
	case SNDRV_PCM_FORMAT_S32_LE:
		word_len = 0;
		break;
	}

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
	switch (ad193x->sysclk) {
	case 12288000:
		master_rate = AD193X_PLL_INPUT_256;
		break;
	case 18432000:
		master_rate = AD193X_PLL_INPUT_384;
		break;
	case 24576000:
		master_rate = AD193X_PLL_INPUT_512;
		break;
	case 36864000:
		master_rate = AD193X_PLL_INPUT_768;
		break;
	}

280
	regmap_update_bits(ad193x->regmap, AD193X_PLL_CLK_CTRL0,
281
			    AD193X_PLL_INPUT_MASK, master_rate);
282

283
	regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL2,
284 285
			    AD193X_DAC_WORD_LEN_MASK,
			    word_len << AD193X_DAC_WORD_LEN_SHFT);
286

287
	regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL1,
288
			    AD193X_ADC_WORD_LEN_MASK, word_len);
289 290 291 292

	return 0;
}

293
static const struct snd_soc_dai_ops ad193x_dai_ops = {
294 295 296
	.hw_params = ad193x_hw_params,
	.digital_mute = ad193x_mute,
	.set_tdm_slot = ad193x_set_tdm_slot,
297
	.set_sysclk	= ad193x_set_dai_sysclk,
298 299 300 301
	.set_fmt = ad193x_set_dai_fmt,
};

/* codec DAI instance */
302 303
static struct snd_soc_dai_driver ad193x_dai = {
	.name = "ad193x-hifi",
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
	.playback = {
		.stream_name = "Playback",
		.channels_min = 2,
		.channels_max = 8,
		.rates = SNDRV_PCM_RATE_48000,
		.formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE |
			SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE,
	},
	.capture = {
		.stream_name = "Capture",
		.channels_min = 2,
		.channels_max = 4,
		.rates = SNDRV_PCM_RATE_48000,
		.formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE |
			SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE,
	},
	.ops = &ad193x_dai_ops,
};

323
static int ad193x_probe(struct snd_soc_codec *codec)
324
{
325 326
	struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(codec);
	int ret;
327

328 329
	codec->control_data = ad193x->regmap;
	ret = snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
330
	if (ret < 0) {
331
		dev_err(codec->dev, "failed to set cache I/O: %d\n", ret);
332
		return ret;
333 334
	}

335
	/* default setting for ad193x */
336

337
	/* unmute dac channels */
338
	regmap_write(ad193x->regmap, AD193X_DAC_CHNL_MUTE, 0x0);
339
	/* de-emphasis: 48kHz, powedown dac */
340
	regmap_write(ad193x->regmap, AD193X_DAC_CTRL2, 0x1A);
341
	/* powerdown dac, dac in tdm mode */
342
	regmap_write(ad193x->regmap, AD193X_DAC_CTRL0, 0x41);
343
	/* high-pass filter enable */
344
	regmap_write(ad193x->regmap, AD193X_ADC_CTRL0, 0x3);
345
	/* sata delay=1, adc aux mode */
346
	regmap_write(ad193x->regmap, AD193X_ADC_CTRL1, 0x43);
347
	/* pll input: mclki/xi */
348 349
	regmap_write(ad193x->regmap, AD193X_PLL_CLK_CTRL0, 0x99); /* mclk=24.576Mhz: 0x9D; mclk=12.288Mhz: 0x99 */
	regmap_write(ad193x->regmap, AD193X_PLL_CLK_CTRL1, 0x04);
350 351 352 353

	return ret;
}

354
static struct snd_soc_codec_driver soc_codec_dev_ad193x = {
355
	.probe = 	ad193x_probe,
356 357 358 359 360 361
	.controls = ad193x_snd_controls,
	.num_controls = ARRAY_SIZE(ad193x_snd_controls),
	.dapm_widgets = ad193x_dapm_widgets,
	.num_dapm_widgets = ARRAY_SIZE(ad193x_dapm_widgets),
	.dapm_routes = audio_paths,
	.num_dapm_routes = ARRAY_SIZE(audio_paths),
362 363
};

364 365 366 367 368
static bool adau193x_reg_volatile(struct device *dev, unsigned int reg)
{
	return false;
}

369
#if defined(CONFIG_SPI_MASTER)
370 371 372 373 374 375

static const struct regmap_config ad193x_spi_regmap_config = {
	.val_bits = 8,
	.reg_bits = 16,
	.read_flag_mask = 0x09,
	.write_flag_mask = 0x08,
376 377 378

	.max_register = AD193X_NUM_REGS - 1,
	.volatile_reg = adau193x_reg_volatile,
379 380
};

381 382
static int __devinit ad193x_spi_probe(struct spi_device *spi)
{
383 384
	struct ad193x_priv *ad193x;

A
Axel Lin 已提交
385 386
	ad193x = devm_kzalloc(&spi->dev, sizeof(struct ad193x_priv),
			      GFP_KERNEL);
387 388 389
	if (ad193x == NULL)
		return -ENOMEM;

390 391 392
	ad193x->regmap = devm_regmap_init_spi(spi, &ad193x_spi_regmap_config);
	if (IS_ERR(ad193x->regmap))
		return PTR_ERR(ad193x->regmap);
393

394 395
	spi_set_drvdata(spi, ad193x);

396 397
	return snd_soc_register_codec(&spi->dev, &soc_codec_dev_ad193x,
			&ad193x_dai, 1);
398 399 400 401
}

static int __devexit ad193x_spi_remove(struct spi_device *spi)
{
402 403
	snd_soc_unregister_codec(&spi->dev);
	return 0;
404 405 406 407
}

static struct spi_driver ad193x_spi_driver = {
	.driver = {
M
Mike Frysinger 已提交
408
		.name	= "ad193x",
409 410 411 412 413 414 415 416
		.owner	= THIS_MODULE,
	},
	.probe		= ad193x_spi_probe,
	.remove		= __devexit_p(ad193x_spi_remove),
};
#endif

#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
417 418 419 420

static const struct regmap_config ad193x_i2c_regmap_config = {
	.val_bits = 8,
	.reg_bits = 8,
421 422 423

	.max_register = AD193X_NUM_REGS - 1,
	.volatile_reg = adau193x_reg_volatile,
424 425
};

426 427 428 429 430 431 432 433 434 435
static const struct i2c_device_id ad193x_id[] = {
	{ "ad1936", 0 },
	{ "ad1937", 0 },
	{ }
};
MODULE_DEVICE_TABLE(i2c, ad193x_id);

static int __devinit ad193x_i2c_probe(struct i2c_client *client,
		const struct i2c_device_id *id)
{
436 437
	struct ad193x_priv *ad193x;

A
Axel Lin 已提交
438 439
	ad193x = devm_kzalloc(&client->dev, sizeof(struct ad193x_priv),
			      GFP_KERNEL);
440 441 442
	if (ad193x == NULL)
		return -ENOMEM;

443 444 445
	ad193x->regmap = devm_regmap_init_i2c(client, &ad193x_i2c_regmap_config);
	if (IS_ERR(ad193x->regmap))
		return PTR_ERR(ad193x->regmap);
446

447 448
	i2c_set_clientdata(client, ad193x);

449 450
	return snd_soc_register_codec(&client->dev, &soc_codec_dev_ad193x,
			&ad193x_dai, 1);
451 452 453 454
}

static int __devexit ad193x_i2c_remove(struct i2c_client *client)
{
455 456
	snd_soc_unregister_codec(&client->dev);
	return 0;
457 458 459 460
}

static struct i2c_driver ad193x_i2c_driver = {
	.driver = {
M
Mike Frysinger 已提交
461
		.name = "ad193x",
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 497 498 499 500 501 502 503 504 505 506
	},
	.probe    = ad193x_i2c_probe,
	.remove   = __devexit_p(ad193x_i2c_remove),
	.id_table = ad193x_id,
};
#endif

static int __init ad193x_modinit(void)
{
	int ret;

#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
	ret =  i2c_add_driver(&ad193x_i2c_driver);
	if (ret != 0) {
		printk(KERN_ERR "Failed to register AD193X I2C driver: %d\n",
				ret);
	}
#endif

#if defined(CONFIG_SPI_MASTER)
	ret = spi_register_driver(&ad193x_spi_driver);
	if (ret != 0) {
		printk(KERN_ERR "Failed to register AD193X SPI driver: %d\n",
				ret);
	}
#endif
	return ret;
}
module_init(ad193x_modinit);

static void __exit ad193x_modexit(void)
{
#if defined(CONFIG_SPI_MASTER)
	spi_unregister_driver(&ad193x_spi_driver);
#endif

#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
	i2c_del_driver(&ad193x_i2c_driver);
#endif
}
module_exit(ad193x_modexit);

MODULE_DESCRIPTION("ASoC ad193x driver");
MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
MODULE_LICENSE("GPL");