ad1836.c 10.4 KB
Newer Older
1 2 3
 /*
 * Audio Codec driver supporting:
 *  AD1835A, AD1836, AD1837A, AD1838A, AD1839A
4
 *
5
 * Copyright 2009-2011 Analog Devices Inc.
6
 *
7
 * Licensed under the GPL-2 or later.
8 9 10
 */

#include <linux/init.h>
11
#include <linux/slab.h>
12 13 14 15 16 17 18 19 20 21
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/device.h>
#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 <linux/spi/spi.h>
22 23
#include <linux/regmap.h>

24 25
#include "ad1836.h"

26 27 28 29 30 31
enum ad1836_type {
	AD1835,
	AD1836,
	AD1838,
};

32 33
/* codec private data */
struct ad1836_priv {
34
	enum ad1836_type type;
35
	struct regmap *regmap;
36 37 38 39 40 41 42 43 44 45
};

/*
 * AD1836 volume/mute/de-emphasis etc. controls
 */
static const char *ad1836_deemp[] = {"None", "44.1kHz", "32kHz", "48kHz"};

static const struct soc_enum ad1836_deemp_enum =
	SOC_ENUM_SINGLE(AD1836_DAC_CTRL1, 8, 4, ad1836_deemp);

46 47 48 49 50 51 52 53 54 55 56 57
#define AD1836_DAC_VOLUME(x) \
	SOC_DOUBLE_R("DAC" #x " Playback Volume", AD1836_DAC_L_VOL(x), \
			AD1836_DAC_R_VOL(x), 0, 0x3FF, 0)

#define AD1836_DAC_SWITCH(x) \
	SOC_DOUBLE("DAC" #x " Playback Switch", AD1836_DAC_CTRL2, \
			AD1836_MUTE_LEFT(x), AD1836_MUTE_RIGHT(x), 1, 1)

#define AD1836_ADC_SWITCH(x) \
	SOC_DOUBLE("ADC" #x " Capture Switch", AD1836_ADC_CTRL2, \
		AD1836_MUTE_LEFT(x), AD1836_MUTE_RIGHT(x), 1, 1)

58
static const struct snd_kcontrol_new ad183x_dac_controls[] = {
59
	AD1836_DAC_VOLUME(1),
60
	AD1836_DAC_SWITCH(1),
61
	AD1836_DAC_VOLUME(2),
62
	AD1836_DAC_SWITCH(2),
63
	AD1836_DAC_VOLUME(3),
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
	AD1836_DAC_SWITCH(3),
	AD1836_DAC_VOLUME(4),
	AD1836_DAC_SWITCH(4),
};

static const struct snd_soc_dapm_widget ad183x_dac_dapm_widgets[] = {
	SND_SOC_DAPM_OUTPUT("DAC1OUT"),
	SND_SOC_DAPM_OUTPUT("DAC2OUT"),
	SND_SOC_DAPM_OUTPUT("DAC3OUT"),
	SND_SOC_DAPM_OUTPUT("DAC4OUT"),
};

static const struct snd_soc_dapm_route ad183x_dac_routes[] = {
	{ "DAC1OUT", NULL, "DAC" },
	{ "DAC2OUT", NULL, "DAC" },
	{ "DAC3OUT", NULL, "DAC" },
	{ "DAC4OUT", NULL, "DAC" },
};
82

83
static const struct snd_kcontrol_new ad183x_adc_controls[] = {
84 85
	AD1836_ADC_SWITCH(1),
	AD1836_ADC_SWITCH(2),
86 87
	AD1836_ADC_SWITCH(3),
};
88

89 90 91 92 93 94 95 96 97
static const struct snd_soc_dapm_widget ad183x_adc_dapm_widgets[] = {
	SND_SOC_DAPM_INPUT("ADC1IN"),
	SND_SOC_DAPM_INPUT("ADC2IN"),
};

static const struct snd_soc_dapm_route ad183x_adc_routes[] = {
	{ "ADC", NULL, "ADC1IN" },
	{ "ADC", NULL, "ADC2IN" },
};
98

99
static const struct snd_kcontrol_new ad183x_controls[] = {
100 101 102 103 104 105 106 107
	/* ADC high-pass filter */
	SOC_SINGLE("ADC High Pass Filter Switch", AD1836_ADC_CTRL1,
			AD1836_ADC_HIGHPASS_FILTER, 1, 0),

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

108
static const struct snd_soc_dapm_widget ad183x_dapm_widgets[] = {
109 110 111 112 113 114 115
	SND_SOC_DAPM_DAC("DAC", "Playback", AD1836_DAC_CTRL1,
				AD1836_DAC_POWERDOWN, 1),
	SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0),
	SND_SOC_DAPM_SUPPLY("ADC_PWR", AD1836_ADC_CTRL1,
				AD1836_ADC_POWERDOWN, 1, NULL, 0),
};

116
static const struct snd_soc_dapm_route ad183x_dapm_routes[] = {
117 118 119 120
	{ "DAC", NULL, "ADC_PWR" },
	{ "ADC", NULL, "ADC_PWR" },
};

121 122 123
static const DECLARE_TLV_DB_SCALE(ad1836_in_tlv, 0, 300, 0);

static const struct snd_kcontrol_new ad1836_controls[] = {
124
	SOC_DOUBLE_TLV("ADC2 Capture Volume", AD1836_ADC_CTRL1, 3, 0, 4, 0,
125 126 127
	    ad1836_in_tlv),
};

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
/*
 * DAI ops entries
 */

static int ad1836_set_dai_fmt(struct snd_soc_dai *codec_dai,
		unsigned int fmt)
{
	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
	/* at present, we support adc aux mode to interface with
	 * blackfin sport tdm mode
	 */
	case SND_SOC_DAIFMT_DSP_A:
		break;
	default:
		return -EINVAL;
	}

	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
	case SND_SOC_DAIFMT_IB_IF:
		break;
	default:
		return -EINVAL;
	}

	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
	/* ALCLK,ABCLK are both output, AD1836 can only be master */
	case SND_SOC_DAIFMT_CBM_CFM:
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static int ad1836_hw_params(struct snd_pcm_substream *substream,
		struct snd_pcm_hw_params *params,
		struct snd_soc_dai *dai)
{
167
	struct ad1836_priv *ad1836 = snd_soc_codec_get_drvdata(dai->codec);
168 169 170
	int word_len = 0;

	/* bit size */
171 172
	switch (params_width(params)) {
	case 16:
173
		word_len = AD1836_WORD_LEN_16;
174
		break;
175
	case 20:
176
		word_len = AD1836_WORD_LEN_20;
177
		break;
178 179
	case 24:
	case 32:
180
		word_len = AD1836_WORD_LEN_24;
181
		break;
182 183
	default:
		return -EINVAL;
184 185
	}

186 187
	regmap_update_bits(ad1836->regmap, AD1836_DAC_CTRL1,
		AD1836_DAC_WORD_LEN_MASK,
188
		word_len << AD1836_DAC_WORD_LEN_OFFSET);
189

190 191
	regmap_update_bits(ad1836->regmap, AD1836_ADC_CTRL2,
		AD1836_ADC_WORD_LEN_MASK,
192
		word_len << AD1836_ADC_WORD_OFFSET);
193 194 195 196

	return 0;
}

197
static const struct snd_soc_dai_ops ad1836_dai_ops = {
198 199 200 201
	.hw_params = ad1836_hw_params,
	.set_fmt = ad1836_set_dai_fmt,
};

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
#define AD183X_DAI(_name, num_dacs, num_adcs) \
{ \
	.name = _name "-hifi", \
	.playback = { \
		.stream_name = "Playback", \
		.channels_min = 2, \
		.channels_max = (num_dacs) * 2, \
		.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 = (num_adcs) * 2, \
		.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 = &ad1836_dai_ops, \
}

static struct snd_soc_dai_driver ad183x_dais[] = {
	[AD1835] = AD183X_DAI("ad1835", 4, 1),
	[AD1836] = AD183X_DAI("ad1836", 3, 2),
	[AD1838] = AD183X_DAI("ad1838", 3, 1),
228 229
};

230
#ifdef CONFIG_PM
231
static int ad1836_suspend(struct snd_soc_codec *codec)
232
{
233
	struct ad1836_priv *ad1836 = snd_soc_codec_get_drvdata(codec);
234
	/* reset clock control mode */
235
	return regmap_update_bits(ad1836->regmap, AD1836_ADC_CTRL2,
236 237 238 239 240
		AD1836_ADC_SERFMT_MASK, 0);
}

static int ad1836_resume(struct snd_soc_codec *codec)
{
241
	struct ad1836_priv *ad1836 = snd_soc_codec_get_drvdata(codec);
242
	/* restore clock control mode */
243
	return regmap_update_bits(ad1836->regmap, AD1836_ADC_CTRL2,
244 245 246 247 248 249 250
		AD1836_ADC_SERFMT_MASK, AD1836_ADC_AUX);
}
#else
#define ad1836_suspend NULL
#define ad1836_resume  NULL
#endif

251
static int ad1836_probe(struct snd_soc_codec *codec)
252
{
253
	struct ad1836_priv *ad1836 = snd_soc_codec_get_drvdata(codec);
L
Liam Girdwood 已提交
254
	struct snd_soc_dapm_context *dapm = &codec->dapm;
255
	int num_dacs, num_adcs;
256
	int ret = 0;
257 258 259 260
	int i;

	num_dacs = ad183x_dais[ad1836->type].playback.channels_max / 2;
	num_adcs = ad183x_dais[ad1836->type].capture.channels_max / 2;
261 262 263

	/* default setting for ad1836 */
	/* de-emphasis: 48kHz, power-on dac */
264
	regmap_write(ad1836->regmap, AD1836_DAC_CTRL1, 0x300);
265
	/* unmute dac channels */
266
	regmap_write(ad1836->regmap, AD1836_DAC_CTRL2, 0x0);
267
	/* high-pass filter enable, power-on adc */
268
	regmap_write(ad1836->regmap, AD1836_ADC_CTRL1, 0x100);
269
	/* unmute adc channles, adc aux mode */
270
	regmap_write(ad1836->regmap, AD1836_ADC_CTRL2, 0x180);
271
	/* volume */
272
	for (i = 1; i <= num_dacs; ++i) {
273 274
		regmap_write(ad1836->regmap, AD1836_DAC_L_VOL(i), 0x3FF);
		regmap_write(ad1836->regmap, AD1836_DAC_R_VOL(i), 0x3FF);
275 276 277 278
	}

	if (ad1836->type == AD1836) {
		/* left/right diff:PGA/MUX */
279
		regmap_write(ad1836->regmap, AD1836_ADC_CTRL3, 0x3A);
280
		ret = snd_soc_add_codec_controls(codec, ad1836_controls,
281 282 283
				ARRAY_SIZE(ad1836_controls));
		if (ret)
			return ret;
284
	} else {
285
		regmap_write(ad1836->regmap, AD1836_ADC_CTRL3, 0x00);
286 287
	}

288
	ret = snd_soc_add_codec_controls(codec, ad183x_dac_controls, num_dacs * 2);
289 290 291
	if (ret)
		return ret;

292
	ret = snd_soc_add_codec_controls(codec, ad183x_adc_controls, num_adcs);
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
	if (ret)
		return ret;

	ret = snd_soc_dapm_new_controls(dapm, ad183x_dac_dapm_widgets, num_dacs);
	if (ret)
		return ret;

	ret = snd_soc_dapm_new_controls(dapm, ad183x_adc_dapm_widgets, num_adcs);
	if (ret)
		return ret;

	ret = snd_soc_dapm_add_routes(dapm, ad183x_dac_routes, num_dacs);
	if (ret)
		return ret;

	ret = snd_soc_dapm_add_routes(dapm, ad183x_adc_routes, num_adcs);
	if (ret)
		return ret;
311 312 313 314 315

	return ret;
}

/* power down chip */
316
static int ad1836_remove(struct snd_soc_codec *codec)
317
{
318
	struct ad1836_priv *ad1836 = snd_soc_codec_get_drvdata(codec);
319
	/* reset clock control mode */
320
	return regmap_update_bits(ad1836->regmap, AD1836_ADC_CTRL2,
321
		AD1836_ADC_SERFMT_MASK, 0);
322 323
}

324
static struct snd_soc_codec_driver soc_codec_dev_ad1836 = {
325 326
	.probe = ad1836_probe,
	.remove = ad1836_remove,
327 328
	.suspend = ad1836_suspend,
	.resume = ad1836_resume,
329 330 331 332 333 334 335

	.controls = ad183x_controls,
	.num_controls = ARRAY_SIZE(ad183x_controls),
	.dapm_widgets = ad183x_dapm_widgets,
	.num_dapm_widgets = ARRAY_SIZE(ad183x_dapm_widgets),
	.dapm_routes = ad183x_dapm_routes,
	.num_dapm_routes = ARRAY_SIZE(ad183x_dapm_routes),
336 337
};

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
static const struct reg_default ad1836_reg_defaults[] = {
	{ AD1836_DAC_CTRL1, 0x0000 },
	{ AD1836_DAC_CTRL2, 0x0000 },
	{ AD1836_DAC_L_VOL(0), 0x0000 },
	{ AD1836_DAC_R_VOL(0), 0x0000 },
	{ AD1836_DAC_L_VOL(1), 0x0000 },
	{ AD1836_DAC_R_VOL(1), 0x0000 },
	{ AD1836_DAC_L_VOL(2), 0x0000 },
	{ AD1836_DAC_R_VOL(2), 0x0000 },
	{ AD1836_DAC_L_VOL(3), 0x0000 },
	{ AD1836_DAC_R_VOL(3), 0x0000 },
	{ AD1836_ADC_CTRL1, 0x0000 },
	{ AD1836_ADC_CTRL2, 0x0000 },
	{ AD1836_ADC_CTRL3, 0x0000 },
};

static const struct regmap_config ad1836_regmap_config = {
	.val_bits = 12,
	.reg_bits = 4,
	.read_flag_mask = 0x08,

	.max_register = AD1836_ADC_CTRL3,
	.reg_defaults = ad1836_reg_defaults,
	.num_reg_defaults = ARRAY_SIZE(ad1836_reg_defaults),
	.cache_type = REGCACHE_RBTREE,
};

365
static int ad1836_spi_probe(struct spi_device *spi)
366 367 368 369
{
	struct ad1836_priv *ad1836;
	int ret;

A
Axel Lin 已提交
370 371
	ad1836 = devm_kzalloc(&spi->dev, sizeof(struct ad1836_priv),
			      GFP_KERNEL);
372 373 374
	if (ad1836 == NULL)
		return -ENOMEM;

375 376 377 378
	ad1836->regmap = devm_regmap_init_spi(spi, &ad1836_regmap_config);
	if (IS_ERR(ad1836->regmap))
		return PTR_ERR(ad1836->regmap);

379 380
	ad1836->type = spi_get_device_id(spi)->driver_data;

381 382 383
	spi_set_drvdata(spi, ad1836);

	ret = snd_soc_register_codec(&spi->dev,
384
			&soc_codec_dev_ad1836, &ad183x_dais[ad1836->type], 1);
385 386 387
	return ret;
}

388
static int ad1836_spi_remove(struct spi_device *spi)
389 390 391 392
{
	snd_soc_unregister_codec(&spi->dev);
	return 0;
}
A
Axel Lin 已提交
393

394 395 396 397 398 399 400 401 402
static const struct spi_device_id ad1836_ids[] = {
	{ "ad1835", AD1835 },
	{ "ad1836", AD1836 },
	{ "ad1837", AD1835 },
	{ "ad1838", AD1838 },
	{ "ad1839", AD1838 },
	{ },
};
MODULE_DEVICE_TABLE(spi, ad1836_ids);
403 404 405

static struct spi_driver ad1836_spi_driver = {
	.driver = {
M
Mike Frysinger 已提交
406
		.name	= "ad1836",
407 408 409
		.owner	= THIS_MODULE,
	},
	.probe		= ad1836_spi_probe,
410
	.remove		= ad1836_spi_remove,
411
	.id_table	= ad1836_ids,
412 413
};

414
module_spi_driver(ad1836_spi_driver);
415 416 417 418

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