ad1816a_lib.c 30 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
    ad1816a.c - lowlevel code for Analog Devices AD1816A chip.
    Copyright (C) 1999-2000 by Massimo Piccioni <dafastidio@libero.it>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
*/

#include <linux/delay.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/ioport.h>
25
#include <linux/io.h>
Linus Torvalds's avatar
Linus Torvalds committed
26
#include <sound/core.h>
27
#include <sound/tlv.h>
Linus Torvalds's avatar
Linus Torvalds committed
28 29 30 31
#include <sound/ad1816a.h>

#include <asm/dma.h>

32
static inline int snd_ad1816a_busy_wait(struct snd_ad1816a *chip)
Linus Torvalds's avatar
Linus Torvalds committed
33 34 35 36 37 38 39
{
	int timeout;

	for (timeout = 1000; timeout-- > 0; udelay(10))
		if (inb(AD1816A_REG(AD1816A_CHIP_STATUS)) & AD1816A_READY)
			return 0;

40
	snd_printk(KERN_WARNING "chip busy.\n");
Linus Torvalds's avatar
Linus Torvalds committed
41 42 43
	return -EBUSY;
}

44
static inline unsigned char snd_ad1816a_in(struct snd_ad1816a *chip, unsigned char reg)
Linus Torvalds's avatar
Linus Torvalds committed
45 46 47 48 49
{
	snd_ad1816a_busy_wait(chip);
	return inb(AD1816A_REG(reg));
}

50
static inline void snd_ad1816a_out(struct snd_ad1816a *chip, unsigned char reg,
Linus Torvalds's avatar
Linus Torvalds committed
51 52 53 54 55 56
			    unsigned char value)
{
	snd_ad1816a_busy_wait(chip);
	outb(value, AD1816A_REG(reg));
}

57
static inline void snd_ad1816a_out_mask(struct snd_ad1816a *chip, unsigned char reg,
Linus Torvalds's avatar
Linus Torvalds committed
58 59 60 61 62 63
				 unsigned char mask, unsigned char value)
{
	snd_ad1816a_out(chip, reg,
		(value & mask) | (snd_ad1816a_in(chip, reg) & ~mask));
}

64
static unsigned short snd_ad1816a_read(struct snd_ad1816a *chip, unsigned char reg)
Linus Torvalds's avatar
Linus Torvalds committed
65 66 67 68 69 70
{
	snd_ad1816a_out(chip, AD1816A_INDIR_ADDR, reg & 0x3f);
	return snd_ad1816a_in(chip, AD1816A_INDIR_DATA_LOW) |
		(snd_ad1816a_in(chip, AD1816A_INDIR_DATA_HIGH) << 8);
}

71
static void snd_ad1816a_write(struct snd_ad1816a *chip, unsigned char reg,
Linus Torvalds's avatar
Linus Torvalds committed
72 73 74 75 76 77 78
			      unsigned short value)
{
	snd_ad1816a_out(chip, AD1816A_INDIR_ADDR, reg & 0x3f);
	snd_ad1816a_out(chip, AD1816A_INDIR_DATA_LOW, value & 0xff);
	snd_ad1816a_out(chip, AD1816A_INDIR_DATA_HIGH, (value >> 8) & 0xff);
}

79
static void snd_ad1816a_write_mask(struct snd_ad1816a *chip, unsigned char reg,
Linus Torvalds's avatar
Linus Torvalds committed
80 81 82 83 84 85 86
				   unsigned short mask, unsigned short value)
{
	snd_ad1816a_write(chip, reg,
		(value & mask) | (snd_ad1816a_read(chip, reg) & ~mask));
}


87
static unsigned char snd_ad1816a_get_format(struct snd_ad1816a *chip,
Linus Torvalds's avatar
Linus Torvalds committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
					    unsigned int format, int channels)
{
	unsigned char retval = AD1816A_FMT_LINEAR_8;

	switch (format) {
	case SNDRV_PCM_FORMAT_MU_LAW:
		retval = AD1816A_FMT_ULAW_8;
		break;
	case SNDRV_PCM_FORMAT_A_LAW:
		retval = AD1816A_FMT_ALAW_8;
		break;
	case SNDRV_PCM_FORMAT_S16_LE:
		retval = AD1816A_FMT_LINEAR_16_LIT;
		break;
	case SNDRV_PCM_FORMAT_S16_BE:
		retval = AD1816A_FMT_LINEAR_16_BIG;
	}
	return (channels > 1) ? (retval | AD1816A_FMT_STEREO) : retval;
}

108
static int snd_ad1816a_open(struct snd_ad1816a *chip, unsigned int mode)
Linus Torvalds's avatar
Linus Torvalds committed
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 143
{
	unsigned long flags;

	spin_lock_irqsave(&chip->lock, flags);

	if (chip->mode & mode) {
		spin_unlock_irqrestore(&chip->lock, flags);
		return -EAGAIN;
	}

	switch ((mode &= AD1816A_MODE_OPEN)) {
	case AD1816A_MODE_PLAYBACK:
		snd_ad1816a_out_mask(chip, AD1816A_INTERRUPT_STATUS,
			AD1816A_PLAYBACK_IRQ_PENDING, 0x00);
		snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE,
			AD1816A_PLAYBACK_IRQ_ENABLE, 0xffff);
		break;
	case AD1816A_MODE_CAPTURE:
		snd_ad1816a_out_mask(chip, AD1816A_INTERRUPT_STATUS,
			AD1816A_CAPTURE_IRQ_PENDING, 0x00);
		snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE,
			AD1816A_CAPTURE_IRQ_ENABLE, 0xffff);
		break;
	case AD1816A_MODE_TIMER:
		snd_ad1816a_out_mask(chip, AD1816A_INTERRUPT_STATUS,
			AD1816A_TIMER_IRQ_PENDING, 0x00);
		snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE,
			AD1816A_TIMER_IRQ_ENABLE, 0xffff);
	}
	chip->mode |= mode;

	spin_unlock_irqrestore(&chip->lock, flags);
	return 0;
}

144
static void snd_ad1816a_close(struct snd_ad1816a *chip, unsigned int mode)
Linus Torvalds's avatar
Linus Torvalds committed
145 146 147 148 149 150 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
{
	unsigned long flags;

	spin_lock_irqsave(&chip->lock, flags);

	switch ((mode &= AD1816A_MODE_OPEN)) {
	case AD1816A_MODE_PLAYBACK:
		snd_ad1816a_out_mask(chip, AD1816A_INTERRUPT_STATUS,
			AD1816A_PLAYBACK_IRQ_PENDING, 0x00);
		snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE,
			AD1816A_PLAYBACK_IRQ_ENABLE, 0x0000);
		break;
	case AD1816A_MODE_CAPTURE:
		snd_ad1816a_out_mask(chip, AD1816A_INTERRUPT_STATUS,
			AD1816A_CAPTURE_IRQ_PENDING, 0x00);
		snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE,
			AD1816A_CAPTURE_IRQ_ENABLE, 0x0000);
		break;
	case AD1816A_MODE_TIMER:
		snd_ad1816a_out_mask(chip, AD1816A_INTERRUPT_STATUS,
			AD1816A_TIMER_IRQ_PENDING, 0x00);
		snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE,
			AD1816A_TIMER_IRQ_ENABLE, 0x0000);
	}
	if (!((chip->mode &= ~mode) & AD1816A_MODE_OPEN))
		chip->mode = 0;

	spin_unlock_irqrestore(&chip->lock, flags);
}


176
static int snd_ad1816a_trigger(struct snd_ad1816a *chip, unsigned char what,
177
			       int channel, int cmd, int iscapture)
Linus Torvalds's avatar
Linus Torvalds committed
178 179 180 181 182 183 184 185
{
	int error = 0;

	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
	case SNDRV_PCM_TRIGGER_STOP:
		spin_lock(&chip->lock);
		cmd = (cmd == SNDRV_PCM_TRIGGER_START) ? 0xff: 0x00;
186 187 188 189 190
		/* if (what & AD1816A_PLAYBACK_ENABLE) */
		/* That is not valid, because playback and capture enable
		 * are the same bit pattern, just to different addresses
		 */
		if (! iscapture)
Linus Torvalds's avatar
Linus Torvalds committed
191 192
			snd_ad1816a_out_mask(chip, AD1816A_PLAYBACK_CONFIG,
				AD1816A_PLAYBACK_ENABLE, cmd);
193
		else
Linus Torvalds's avatar
Linus Torvalds committed
194 195 196 197 198
			snd_ad1816a_out_mask(chip, AD1816A_CAPTURE_CONFIG,
				AD1816A_CAPTURE_ENABLE, cmd);
		spin_unlock(&chip->lock);
		break;
	default:
199
		snd_printk(KERN_WARNING "invalid trigger mode 0x%x.\n", what);
Linus Torvalds's avatar
Linus Torvalds committed
200 201 202 203 204 205
		error = -EINVAL;
	}

	return error;
}

206
static int snd_ad1816a_playback_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds's avatar
Linus Torvalds committed
207
{
208
	struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
209
	return snd_ad1816a_trigger(chip, AD1816A_PLAYBACK_ENABLE,
210
				   SNDRV_PCM_STREAM_PLAYBACK, cmd, 0);
Linus Torvalds's avatar
Linus Torvalds committed
211 212
}

213
static int snd_ad1816a_capture_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds's avatar
Linus Torvalds committed
214
{
215
	struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
216
	return snd_ad1816a_trigger(chip, AD1816A_CAPTURE_ENABLE,
217
				   SNDRV_PCM_STREAM_CAPTURE, cmd, 1);
Linus Torvalds's avatar
Linus Torvalds committed
218 219
}

220 221
static int snd_ad1816a_hw_params(struct snd_pcm_substream *substream,
				 struct snd_pcm_hw_params *hw_params)
Linus Torvalds's avatar
Linus Torvalds committed
222 223 224 225
{
	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
}

226
static int snd_ad1816a_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
227 228 229 230
{
	return snd_pcm_lib_free_pages(substream);
}

231
static int snd_ad1816a_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
232
{
233
	struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
234
	unsigned long flags;
235
	struct snd_pcm_runtime *runtime = substream->runtime;
236
	unsigned int size, rate;
Linus Torvalds's avatar
Linus Torvalds committed
237 238 239 240 241 242 243 244 245 246

	spin_lock_irqsave(&chip->lock, flags);

	chip->p_dma_size = size = snd_pcm_lib_buffer_bytes(substream);
	snd_ad1816a_out_mask(chip, AD1816A_PLAYBACK_CONFIG,
		AD1816A_PLAYBACK_ENABLE | AD1816A_PLAYBACK_PIO, 0x00);

	snd_dma_program(chip->dma1, runtime->dma_addr, size,
			DMA_MODE_WRITE | DMA_AUTOINIT);

247 248 249 250
	rate = runtime->rate;
	if (chip->clock_freq)
		rate = (rate * 33000) / chip->clock_freq;
	snd_ad1816a_write(chip, AD1816A_PLAYBACK_SAMPLE_RATE, rate);
Linus Torvalds's avatar
Linus Torvalds committed
251 252 253 254 255 256 257 258 259 260 261 262
	snd_ad1816a_out_mask(chip, AD1816A_PLAYBACK_CONFIG,
		AD1816A_FMT_ALL | AD1816A_FMT_STEREO,
		snd_ad1816a_get_format(chip, runtime->format,
			runtime->channels));

	snd_ad1816a_write(chip, AD1816A_PLAYBACK_BASE_COUNT,
		snd_pcm_lib_period_bytes(substream) / 4 - 1);

	spin_unlock_irqrestore(&chip->lock, flags);
	return 0;
}

263
static int snd_ad1816a_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
264
{
265
	struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
266
	unsigned long flags;
267
	struct snd_pcm_runtime *runtime = substream->runtime;
268
	unsigned int size, rate;
Linus Torvalds's avatar
Linus Torvalds committed
269 270 271 272 273 274 275 276 277 278

	spin_lock_irqsave(&chip->lock, flags);

	chip->c_dma_size = size = snd_pcm_lib_buffer_bytes(substream);
	snd_ad1816a_out_mask(chip, AD1816A_CAPTURE_CONFIG,
		AD1816A_CAPTURE_ENABLE | AD1816A_CAPTURE_PIO, 0x00);

	snd_dma_program(chip->dma2, runtime->dma_addr, size,
			DMA_MODE_READ | DMA_AUTOINIT);

279 280 281 282
	rate = runtime->rate;
	if (chip->clock_freq)
		rate = (rate * 33000) / chip->clock_freq;
	snd_ad1816a_write(chip, AD1816A_CAPTURE_SAMPLE_RATE, rate);
Linus Torvalds's avatar
Linus Torvalds committed
283 284 285 286 287 288 289 290 291 292 293 294 295
	snd_ad1816a_out_mask(chip, AD1816A_CAPTURE_CONFIG,
		AD1816A_FMT_ALL | AD1816A_FMT_STEREO,
		snd_ad1816a_get_format(chip, runtime->format,
			runtime->channels));

	snd_ad1816a_write(chip, AD1816A_CAPTURE_BASE_COUNT,
		snd_pcm_lib_period_bytes(substream) / 4 - 1);

	spin_unlock_irqrestore(&chip->lock, flags);
	return 0;
}


296
static snd_pcm_uframes_t snd_ad1816a_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
297
{
298
	struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
299 300 301 302 303 304 305
	size_t ptr;
	if (!(chip->mode & AD1816A_MODE_PLAYBACK))
		return 0;
	ptr = snd_dma_pointer(chip->dma1, chip->p_dma_size);
	return bytes_to_frames(substream->runtime, ptr);
}

306
static snd_pcm_uframes_t snd_ad1816a_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
307
{
308
	struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
309 310 311 312 313 314 315 316
	size_t ptr;
	if (!(chip->mode & AD1816A_MODE_CAPTURE))
		return 0;
	ptr = snd_dma_pointer(chip->dma2, chip->c_dma_size);
	return bytes_to_frames(substream->runtime, ptr);
}


317
static irqreturn_t snd_ad1816a_interrupt(int irq, void *dev_id)
Linus Torvalds's avatar
Linus Torvalds committed
318
{
319
	struct snd_ad1816a *chip = dev_id;
Linus Torvalds's avatar
Linus Torvalds committed
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
	unsigned char status;

	spin_lock(&chip->lock);
	status = snd_ad1816a_in(chip, AD1816A_INTERRUPT_STATUS);
	spin_unlock(&chip->lock);

	if ((status & AD1816A_PLAYBACK_IRQ_PENDING) && chip->playback_substream)
		snd_pcm_period_elapsed(chip->playback_substream);

	if ((status & AD1816A_CAPTURE_IRQ_PENDING) && chip->capture_substream)
		snd_pcm_period_elapsed(chip->capture_substream);

	if ((status & AD1816A_TIMER_IRQ_PENDING) && chip->timer)
		snd_timer_interrupt(chip->timer, chip->timer->sticks);

	spin_lock(&chip->lock);
	snd_ad1816a_out(chip, AD1816A_INTERRUPT_STATUS, 0x00);
	spin_unlock(&chip->lock);
	return IRQ_HANDLED;
}


342
static struct snd_pcm_hardware snd_ad1816a_playback = {
Linus Torvalds's avatar
Linus Torvalds committed
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
				 SNDRV_PCM_INFO_MMAP_VALID),
	.formats =		(SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW |
				 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
				 SNDRV_PCM_FMTBIT_S16_BE),
	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
	.rate_min =		4000,
	.rate_max =		55200,
	.channels_min =		1,
	.channels_max =		2,
	.buffer_bytes_max =	(128*1024),
	.period_bytes_min =	64,
	.period_bytes_max =	(128*1024),
	.periods_min =		1,
	.periods_max =		1024,
	.fifo_size =		0,
};

361
static struct snd_pcm_hardware snd_ad1816a_capture = {
Linus Torvalds's avatar
Linus Torvalds committed
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
				 SNDRV_PCM_INFO_MMAP_VALID),
	.formats =		(SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW |
				 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
				 SNDRV_PCM_FMTBIT_S16_BE),
	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
	.rate_min =		4000,
	.rate_max =		55200,
	.channels_min =		1,
	.channels_max =		2,
	.buffer_bytes_max =	(128*1024),
	.period_bytes_min =	64,
	.period_bytes_max =	(128*1024),
	.periods_min =		1,
	.periods_max =		1024,
	.fifo_size =		0,
};

380
static int snd_ad1816a_timer_close(struct snd_timer *timer)
Linus Torvalds's avatar
Linus Torvalds committed
381
{
382
	struct snd_ad1816a *chip = snd_timer_chip(timer);
Linus Torvalds's avatar
Linus Torvalds committed
383 384 385 386
	snd_ad1816a_close(chip, AD1816A_MODE_TIMER);
	return 0;
}

387
static int snd_ad1816a_timer_open(struct snd_timer *timer)
Linus Torvalds's avatar
Linus Torvalds committed
388
{
389
	struct snd_ad1816a *chip = snd_timer_chip(timer);
Linus Torvalds's avatar
Linus Torvalds committed
390 391 392 393
	snd_ad1816a_open(chip, AD1816A_MODE_TIMER);
	return 0;
}

394
static unsigned long snd_ad1816a_timer_resolution(struct snd_timer *timer)
Linus Torvalds's avatar
Linus Torvalds committed
395
{
396 397
	if (snd_BUG_ON(!timer))
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
398 399 400 401

	return 10000;
}

402
static int snd_ad1816a_timer_start(struct snd_timer *timer)
Linus Torvalds's avatar
Linus Torvalds committed
403 404 405
{
	unsigned short bits;
	unsigned long flags;
406
	struct snd_ad1816a *chip = snd_timer_chip(timer);
Linus Torvalds's avatar
Linus Torvalds committed
407 408 409 410 411 412 413 414 415 416 417 418 419 420
	spin_lock_irqsave(&chip->lock, flags);
	bits = snd_ad1816a_read(chip, AD1816A_INTERRUPT_ENABLE);

	if (!(bits & AD1816A_TIMER_ENABLE)) {
		snd_ad1816a_write(chip, AD1816A_TIMER_BASE_COUNT,
			timer->sticks & 0xffff);

		snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE,
			AD1816A_TIMER_ENABLE, 0xffff);
	}
	spin_unlock_irqrestore(&chip->lock, flags);
	return 0;
}

421
static int snd_ad1816a_timer_stop(struct snd_timer *timer)
Linus Torvalds's avatar
Linus Torvalds committed
422 423
{
	unsigned long flags;
424
	struct snd_ad1816a *chip = snd_timer_chip(timer);
Linus Torvalds's avatar
Linus Torvalds committed
425 426 427 428 429 430 431 432 433
	spin_lock_irqsave(&chip->lock, flags);

	snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE,
		AD1816A_TIMER_ENABLE, 0x0000);

	spin_unlock_irqrestore(&chip->lock, flags);
	return 0;
}

434
static struct snd_timer_hardware snd_ad1816a_timer_table = {
Linus Torvalds's avatar
Linus Torvalds committed
435 436 437 438 439 440 441 442 443 444
	.flags =	SNDRV_TIMER_HW_AUTO,
	.resolution =	10000,
	.ticks =	65535,
	.open =		snd_ad1816a_timer_open,
	.close =	snd_ad1816a_timer_close,
	.c_resolution =	snd_ad1816a_timer_resolution,
	.start =	snd_ad1816a_timer_start,
	.stop =		snd_ad1816a_timer_stop,
};

445
static int snd_ad1816a_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
446
{
447 448
	struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
449 450 451 452 453 454 455 456 457 458 459
	int error;

	if ((error = snd_ad1816a_open(chip, AD1816A_MODE_PLAYBACK)) < 0)
		return error;
	runtime->hw = snd_ad1816a_playback;
	snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.buffer_bytes_max);
	snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.period_bytes_max);
	chip->playback_substream = substream;
	return 0;
}

460
static int snd_ad1816a_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
461
{
462 463
	struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
464 465 466 467 468 469 470 471 472 473 474
	int error;

	if ((error = snd_ad1816a_open(chip, AD1816A_MODE_CAPTURE)) < 0)
		return error;
	runtime->hw = snd_ad1816a_capture;
	snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.buffer_bytes_max);
	snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.period_bytes_max);
	chip->capture_substream = substream;
	return 0;
}

475
static int snd_ad1816a_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
476
{
477
	struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
478 479 480 481 482 483

	chip->playback_substream = NULL;
	snd_ad1816a_close(chip, AD1816A_MODE_PLAYBACK);
	return 0;
}

484
static int snd_ad1816a_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
485
{
486
	struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
487 488 489 490 491 492 493

	chip->capture_substream = NULL;
	snd_ad1816a_close(chip, AD1816A_MODE_CAPTURE);
	return 0;
}


494
static void snd_ad1816a_init(struct snd_ad1816a *chip)
Linus Torvalds's avatar
Linus Torvalds committed
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
{
	unsigned long flags;

	spin_lock_irqsave(&chip->lock, flags);

	snd_ad1816a_out(chip, AD1816A_INTERRUPT_STATUS, 0x00);
	snd_ad1816a_out_mask(chip, AD1816A_PLAYBACK_CONFIG,
		AD1816A_PLAYBACK_ENABLE | AD1816A_PLAYBACK_PIO, 0x00);
	snd_ad1816a_out_mask(chip, AD1816A_CAPTURE_CONFIG,
		AD1816A_CAPTURE_ENABLE | AD1816A_CAPTURE_PIO, 0x00);
	snd_ad1816a_write(chip, AD1816A_INTERRUPT_ENABLE, 0x0000);
	snd_ad1816a_write_mask(chip, AD1816A_CHIP_CONFIG,
		AD1816A_CAPTURE_NOT_EQUAL | AD1816A_WSS_ENABLE, 0xffff);
	snd_ad1816a_write(chip, AD1816A_DSP_CONFIG, 0x0000);
	snd_ad1816a_write(chip, AD1816A_POWERDOWN_CTRL, 0x0000);

	spin_unlock_irqrestore(&chip->lock, flags);
}

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
#ifdef CONFIG_PM
void snd_ad1816a_suspend(struct snd_ad1816a *chip)
{
	int reg;
	unsigned long flags;

	snd_pcm_suspend_all(chip->pcm);
	spin_lock_irqsave(&chip->lock, flags);
	for (reg = 0; reg < 48; reg++)
		chip->image[reg] = snd_ad1816a_read(chip, reg);
	spin_unlock_irqrestore(&chip->lock, flags);
}

void snd_ad1816a_resume(struct snd_ad1816a *chip)
{
	int reg;
	unsigned long flags;

	snd_ad1816a_init(chip);
	spin_lock_irqsave(&chip->lock, flags);
	for (reg = 0; reg < 48; reg++)
		snd_ad1816a_write(chip, reg, chip->image[reg]);
	spin_unlock_irqrestore(&chip->lock, flags);
}
#endif

540
static int snd_ad1816a_probe(struct snd_ad1816a *chip)
Linus Torvalds's avatar
Linus Torvalds committed
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
{
	unsigned long flags;

	spin_lock_irqsave(&chip->lock, flags);

	switch (chip->version = snd_ad1816a_read(chip, AD1816A_VERSION_ID)) {
	case 0:
		chip->hardware = AD1816A_HW_AD1815;
		break;
	case 1:
		chip->hardware = AD1816A_HW_AD18MAX10;
		break;
	case 3:
		chip->hardware = AD1816A_HW_AD1816A;
		break;
	default:
		chip->hardware = AD1816A_HW_AUTO;
	}

	spin_unlock_irqrestore(&chip->lock, flags);
	return 0;
}

564
static int snd_ad1816a_free(struct snd_ad1816a *chip)
Linus Torvalds's avatar
Linus Torvalds committed
565
{
566
	release_and_free_resource(chip->res_port);
Linus Torvalds's avatar
Linus Torvalds committed
567 568 569 570 571 572 573 574 575 576 577 578 579
	if (chip->irq >= 0)
		free_irq(chip->irq, (void *) chip);
	if (chip->dma1 >= 0) {
		snd_dma_disable(chip->dma1);
		free_dma(chip->dma1);
	}
	if (chip->dma2 >= 0) {
		snd_dma_disable(chip->dma2);
		free_dma(chip->dma2);
	}
	return 0;
}

580
static int snd_ad1816a_dev_free(struct snd_device *device)
Linus Torvalds's avatar
Linus Torvalds committed
581
{
582
	struct snd_ad1816a *chip = device->device_data;
Linus Torvalds's avatar
Linus Torvalds committed
583 584 585
	return snd_ad1816a_free(chip);
}

586
static const char *snd_ad1816a_chip_id(struct snd_ad1816a *chip)
Linus Torvalds's avatar
Linus Torvalds committed
587 588 589 590 591 592
{
	switch (chip->hardware) {
	case AD1816A_HW_AD1816A: return "AD1816A";
	case AD1816A_HW_AD1815:	return "AD1815";
	case AD1816A_HW_AD18MAX10: return "AD18max10";
	default:
593
		snd_printk(KERN_WARNING "Unknown chip version %d:%d.\n",
Linus Torvalds's avatar
Linus Torvalds committed
594 595 596 597 598
			chip->version, chip->hardware);
		return "AD1816A - unknown";
	}
}

599 600 601
int snd_ad1816a_create(struct snd_card *card,
		       unsigned long port, int irq, int dma1, int dma2,
		       struct snd_ad1816a *chip)
Linus Torvalds's avatar
Linus Torvalds committed
602
{
603
        static struct snd_device_ops ops = {
Linus Torvalds's avatar
Linus Torvalds committed
604 605 606 607 608 609 610 611 612 613 614 615 616
		.dev_free =	snd_ad1816a_dev_free,
	};
	int error;

	chip->irq = -1;
	chip->dma1 = -1;
	chip->dma2 = -1;

	if ((chip->res_port = request_region(port, 16, "AD1816A")) == NULL) {
		snd_printk(KERN_ERR "ad1816a: can't grab port 0x%lx\n", port);
		snd_ad1816a_free(chip);
		return -EBUSY;
	}
Yong Zhang's avatar
Yong Zhang committed
617
	if (request_irq(irq, snd_ad1816a_interrupt, 0, "AD1816A", (void *) chip)) {
Linus Torvalds's avatar
Linus Torvalds committed
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
		snd_printk(KERN_ERR "ad1816a: can't grab IRQ %d\n", irq);
		snd_ad1816a_free(chip);
		return -EBUSY;
	}
	chip->irq = irq;
	if (request_dma(dma1, "AD1816A - 1")) {
		snd_printk(KERN_ERR "ad1816a: can't grab DMA1 %d\n", dma1);
		snd_ad1816a_free(chip);
		return -EBUSY;
	}
	chip->dma1 = dma1;
	if (request_dma(dma2, "AD1816A - 2")) {
		snd_printk(KERN_ERR "ad1816a: can't grab DMA2 %d\n", dma2);
		snd_ad1816a_free(chip);
		return -EBUSY;
	}
	chip->dma2 = dma2;

	chip->card = card;
	chip->port = port;
	spin_lock_init(&chip->lock);

	if ((error = snd_ad1816a_probe(chip))) {
		snd_ad1816a_free(chip);
		return error;
	}

	snd_ad1816a_init(chip);

	/* Register device */
	if ((error = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
		snd_ad1816a_free(chip);
		return error;
	}

	return 0;
}

656
static struct snd_pcm_ops snd_ad1816a_playback_ops = {
Linus Torvalds's avatar
Linus Torvalds committed
657 658 659 660 661 662 663 664 665 666
	.open =		snd_ad1816a_playback_open,
	.close =	snd_ad1816a_playback_close,
	.ioctl =	snd_pcm_lib_ioctl,
	.hw_params =	snd_ad1816a_hw_params,
	.hw_free =	snd_ad1816a_hw_free,
	.prepare =	snd_ad1816a_playback_prepare,
	.trigger =	snd_ad1816a_playback_trigger,
	.pointer =	snd_ad1816a_playback_pointer,
};

667
static struct snd_pcm_ops snd_ad1816a_capture_ops = {
Linus Torvalds's avatar
Linus Torvalds committed
668 669 670 671 672 673 674 675 676 677
	.open =		snd_ad1816a_capture_open,
	.close =	snd_ad1816a_capture_close,
	.ioctl =	snd_pcm_lib_ioctl,
	.hw_params =	snd_ad1816a_hw_params,
	.hw_free =	snd_ad1816a_hw_free,
	.prepare =	snd_ad1816a_capture_prepare,
	.trigger =	snd_ad1816a_capture_trigger,
	.pointer =	snd_ad1816a_capture_pointer,
};

678
int snd_ad1816a_pcm(struct snd_ad1816a *chip, int device)
Linus Torvalds's avatar
Linus Torvalds committed
679 680
{
	int error;
681
	struct snd_pcm *pcm;
Linus Torvalds's avatar
Linus Torvalds committed
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702

	if ((error = snd_pcm_new(chip->card, "AD1816A", device, 1, 1, &pcm)))
		return error;

	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ad1816a_playback_ops);
	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ad1816a_capture_ops);

	pcm->private_data = chip;
	pcm->info_flags = (chip->dma1 == chip->dma2 ) ? SNDRV_PCM_INFO_JOINT_DUPLEX : 0;

	strcpy(pcm->name, snd_ad1816a_chip_id(chip));
	snd_ad1816a_init(chip);

	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
					      snd_dma_isa_data(),
					      64*1024, chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);

	chip->pcm = pcm;
	return 0;
}

703
int snd_ad1816a_timer(struct snd_ad1816a *chip, int device)
Linus Torvalds's avatar
Linus Torvalds committed
704
{
705 706
	struct snd_timer *timer;
	struct snd_timer_id tid;
Linus Torvalds's avatar
Linus Torvalds committed
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
	int error;

	tid.dev_class = SNDRV_TIMER_CLASS_CARD;
	tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
	tid.card = chip->card->number;
	tid.device = device;
	tid.subdevice = 0;
	if ((error = snd_timer_new(chip->card, "AD1816A", &tid, &timer)) < 0)
		return error;
	strcpy(timer->name, snd_ad1816a_chip_id(chip));
	timer->private_data = chip;
	chip->timer = timer;
	timer->hw = snd_ad1816a_timer_table;
	return 0;
}

/*
 *
 */

727
static int snd_ad1816a_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
728
{
729
	static const char * const texts[8] = {
Linus Torvalds's avatar
Linus Torvalds committed
730 731 732 733
		"Line", "Mix", "CD", "Synth", "Video",
		"Mic", "Phone",
	};

734
	return snd_ctl_enum_info(uinfo, 2, 7, texts);
Linus Torvalds's avatar
Linus Torvalds committed
735 736
}

737
static int snd_ad1816a_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
738
{
739
	struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
740 741 742 743 744 745 746 747 748 749 750
	unsigned long flags;
	unsigned short val;
	
	spin_lock_irqsave(&chip->lock, flags);
	val = snd_ad1816a_read(chip, AD1816A_ADC_SOURCE_SEL);
	spin_unlock_irqrestore(&chip->lock, flags);
	ucontrol->value.enumerated.item[0] = (val >> 12) & 7;
	ucontrol->value.enumerated.item[1] = (val >> 4) & 7;
	return 0;
}

751
static int snd_ad1816a_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
752
{
753
	struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
	unsigned long flags;
	unsigned short val;
	int change;
	
	if (ucontrol->value.enumerated.item[0] > 6 ||
	    ucontrol->value.enumerated.item[1] > 6)
		return -EINVAL;
	val = (ucontrol->value.enumerated.item[0] << 12) |
	      (ucontrol->value.enumerated.item[1] << 4);
	spin_lock_irqsave(&chip->lock, flags);
	change = snd_ad1816a_read(chip, AD1816A_ADC_SOURCE_SEL) != val;
	snd_ad1816a_write(chip, AD1816A_ADC_SOURCE_SEL, val);
	spin_unlock_irqrestore(&chip->lock, flags);
	return change;
}

770 771 772 773 774 775 776
#define AD1816A_SINGLE_TLV(xname, reg, shift, mask, invert, xtlv)	\
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
  .name = xname, .info = snd_ad1816a_info_single, \
  .get = snd_ad1816a_get_single, .put = snd_ad1816a_put_single, \
  .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24), \
  .tlv = { .p = (xtlv) } }
Linus Torvalds's avatar
Linus Torvalds committed
777 778 779 780 781
#define AD1816A_SINGLE(xname, reg, shift, mask, invert) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ad1816a_info_single, \
  .get = snd_ad1816a_get_single, .put = snd_ad1816a_put_single, \
  .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }

782
static int snd_ad1816a_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
783 784 785 786 787 788 789 790 791 792
{
	int mask = (kcontrol->private_value >> 16) & 0xff;

	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 1;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = mask;
	return 0;
}

793
static int snd_ad1816a_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
794
{
795
	struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
796 797 798 799 800 801 802 803 804 805 806 807 808 809
	unsigned long flags;
	int reg = kcontrol->private_value & 0xff;
	int shift = (kcontrol->private_value >> 8) & 0xff;
	int mask = (kcontrol->private_value >> 16) & 0xff;
	int invert = (kcontrol->private_value >> 24) & 0xff;
	
	spin_lock_irqsave(&chip->lock, flags);
	ucontrol->value.integer.value[0] = (snd_ad1816a_read(chip, reg) >> shift) & mask;
	spin_unlock_irqrestore(&chip->lock, flags);
	if (invert)
		ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
	return 0;
}

810
static int snd_ad1816a_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
811
{
812
	struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
	unsigned long flags;
	int reg = kcontrol->private_value & 0xff;
	int shift = (kcontrol->private_value >> 8) & 0xff;
	int mask = (kcontrol->private_value >> 16) & 0xff;
	int invert = (kcontrol->private_value >> 24) & 0xff;
	int change;
	unsigned short old_val, val;
	
	val = (ucontrol->value.integer.value[0] & mask);
	if (invert)
		val = mask - val;
	val <<= shift;
	spin_lock_irqsave(&chip->lock, flags);
	old_val = snd_ad1816a_read(chip, reg);
	val = (old_val & ~(mask << shift)) | val;
	change = val != old_val;
	snd_ad1816a_write(chip, reg, val);
	spin_unlock_irqrestore(&chip->lock, flags);
	return change;
}

834 835 836 837 838 839 840 841
#define AD1816A_DOUBLE_TLV(xname, reg, shift_left, shift_right, mask, invert, xtlv) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
  .name = xname, .info = snd_ad1816a_info_double,		\
  .get = snd_ad1816a_get_double, .put = snd_ad1816a_put_double, \
  .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24), \
  .tlv = { .p = (xtlv) } }

Linus Torvalds's avatar
Linus Torvalds committed
842 843 844 845 846
#define AD1816A_DOUBLE(xname, reg, shift_left, shift_right, mask, invert) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ad1816a_info_double, \
  .get = snd_ad1816a_get_double, .put = snd_ad1816a_put_double, \
  .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24) }

847
static int snd_ad1816a_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
848 849 850 851 852 853 854 855 856 857
{
	int mask = (kcontrol->private_value >> 16) & 0xff;

	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 2;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = mask;
	return 0;
}

858
static int snd_ad1816a_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
859
{
860
	struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
	unsigned long flags;
	int reg = kcontrol->private_value & 0xff;
	int shift_left = (kcontrol->private_value >> 8) & 0x0f;
	int shift_right = (kcontrol->private_value >> 12) & 0x0f;
	int mask = (kcontrol->private_value >> 16) & 0xff;
	int invert = (kcontrol->private_value >> 24) & 0xff;
	unsigned short val;
	
	spin_lock_irqsave(&chip->lock, flags);
	val = snd_ad1816a_read(chip, reg);
	ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
	ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
	spin_unlock_irqrestore(&chip->lock, flags);
	if (invert) {
		ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
		ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
	}
	return 0;
}

881
static int snd_ad1816a_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
882
{
883
	struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
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
	unsigned long flags;
	int reg = kcontrol->private_value & 0xff;
	int shift_left = (kcontrol->private_value >> 8) & 0x0f;
	int shift_right = (kcontrol->private_value >> 12) & 0x0f;
	int mask = (kcontrol->private_value >> 16) & 0xff;
	int invert = (kcontrol->private_value >> 24) & 0xff;
	int change;
	unsigned short old_val, val1, val2;
	
	val1 = ucontrol->value.integer.value[0] & mask;
	val2 = ucontrol->value.integer.value[1] & mask;
	if (invert) {
		val1 = mask - val1;
		val2 = mask - val2;
	}
	val1 <<= shift_left;
	val2 <<= shift_right;
	spin_lock_irqsave(&chip->lock, flags);
	old_val = snd_ad1816a_read(chip, reg);
	val1 = (old_val & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
	change = val1 != old_val;
	snd_ad1816a_write(chip, reg, val1);
	spin_unlock_irqrestore(&chip->lock, flags);
	return change;
}

910 911 912 913 914
static const DECLARE_TLV_DB_SCALE(db_scale_4bit, -4500, 300, 0);
static const DECLARE_TLV_DB_SCALE(db_scale_5bit, -4650, 150, 0);
static const DECLARE_TLV_DB_SCALE(db_scale_6bit, -9450, 150, 0);
static const DECLARE_TLV_DB_SCALE(db_scale_5bit_12db_max, -3450, 150, 0);
static const DECLARE_TLV_DB_SCALE(db_scale_rec_gain, 0, 150, 0);
915

916
static struct snd_kcontrol_new snd_ad1816a_controls[] = {
Linus Torvalds's avatar
Linus Torvalds committed
917
AD1816A_DOUBLE("Master Playback Switch", AD1816A_MASTER_ATT, 15, 7, 1, 1),
918 919
AD1816A_DOUBLE_TLV("Master Playback Volume", AD1816A_MASTER_ATT, 8, 0, 31, 1,
		   db_scale_5bit),
Linus Torvalds's avatar
Linus Torvalds committed
920
AD1816A_DOUBLE("PCM Playback Switch", AD1816A_VOICE_ATT, 15, 7, 1, 1),
921 922
AD1816A_DOUBLE_TLV("PCM Playback Volume", AD1816A_VOICE_ATT, 8, 0, 63, 1,
		   db_scale_6bit),
Linus Torvalds's avatar
Linus Torvalds committed
923
AD1816A_DOUBLE("Line Playback Switch", AD1816A_LINE_GAIN_ATT, 15, 7, 1, 1),
924 925
AD1816A_DOUBLE_TLV("Line Playback Volume", AD1816A_LINE_GAIN_ATT, 8, 0, 31, 1,
		   db_scale_5bit_12db_max),
Linus Torvalds's avatar
Linus Torvalds committed
926
AD1816A_DOUBLE("CD Playback Switch", AD1816A_CD_GAIN_ATT, 15, 7, 1, 1),
927 928
AD1816A_DOUBLE_TLV("CD Playback Volume", AD1816A_CD_GAIN_ATT, 8, 0, 31, 1,
		   db_scale_5bit_12db_max),
Linus Torvalds's avatar
Linus Torvalds committed
929
AD1816A_DOUBLE("Synth Playback Switch", AD1816A_SYNTH_GAIN_ATT, 15, 7, 1, 1),
930 931
AD1816A_DOUBLE_TLV("Synth Playback Volume", AD1816A_SYNTH_GAIN_ATT, 8, 0, 31, 1,
		   db_scale_5bit_12db_max),
Linus Torvalds's avatar
Linus Torvalds committed
932
AD1816A_DOUBLE("FM Playback Switch", AD1816A_FM_ATT, 15, 7, 1, 1),
933 934
AD1816A_DOUBLE_TLV("FM Playback Volume", AD1816A_FM_ATT, 8, 0, 63, 1,
		   db_scale_6bit),
Linus Torvalds's avatar
Linus Torvalds committed
935
AD1816A_SINGLE("Mic Playback Switch", AD1816A_MIC_GAIN_ATT, 15, 1, 1),
936 937
AD1816A_SINGLE_TLV("Mic Playback Volume", AD1816A_MIC_GAIN_ATT, 8, 31, 1,
		   db_scale_5bit_12db_max),
Linus Torvalds's avatar
Linus Torvalds committed
938 939
AD1816A_SINGLE("Mic Boost", AD1816A_MIC_GAIN_ATT, 14, 1, 0),
AD1816A_DOUBLE("Video Playback Switch", AD1816A_VID_GAIN_ATT, 15, 7, 1, 1),
940 941
AD1816A_DOUBLE_TLV("Video Playback Volume", AD1816A_VID_GAIN_ATT, 8, 0, 31, 1,
		   db_scale_5bit_12db_max),
Linus Torvalds's avatar
Linus Torvalds committed
942
AD1816A_SINGLE("Phone Capture Switch", AD1816A_PHONE_IN_GAIN_ATT, 15, 1, 1),
943 944
AD1816A_SINGLE_TLV("Phone Capture Volume", AD1816A_PHONE_IN_GAIN_ATT, 0, 15, 1,
		   db_scale_4bit),
Linus Torvalds's avatar
Linus Torvalds committed
945
AD1816A_SINGLE("Phone Playback Switch", AD1816A_PHONE_OUT_ATT, 7, 1, 1),
946 947
AD1816A_SINGLE_TLV("Phone Playback Volume", AD1816A_PHONE_OUT_ATT, 0, 31, 1,
		   db_scale_5bit),
Linus Torvalds's avatar
Linus Torvalds committed
948 949 950 951 952 953 954 955
{
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "Capture Source",
	.info = snd_ad1816a_info_mux,
	.get = snd_ad1816a_get_mux,
	.put = snd_ad1816a_put_mux,
},
AD1816A_DOUBLE("Capture Switch", AD1816A_ADC_PGA, 15, 7, 1, 1),
956 957
AD1816A_DOUBLE_TLV("Capture Volume", AD1816A_ADC_PGA, 8, 0, 15, 0,
		   db_scale_rec_gain),
Linus Torvalds's avatar
Linus Torvalds committed
958 959 960 961
AD1816A_SINGLE("3D Control - Switch", AD1816A_3D_PHAT_CTRL, 15, 1, 1),
AD1816A_SINGLE("3D Control - Level", AD1816A_3D_PHAT_CTRL, 0, 15, 0),
};
                                        
962
int snd_ad1816a_mixer(struct snd_ad1816a *chip)
Linus Torvalds's avatar
Linus Torvalds committed
963
{
964
	struct snd_card *card;
Linus Torvalds's avatar
Linus Torvalds committed
965 966 967
	unsigned int idx;
	int err;

968 969
	if (snd_BUG_ON(!chip || !chip->card))
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
970 971 972 973 974 975 976 977 978 979 980

	card = chip->card;

	strcpy(card->mixername, snd_ad1816a_chip_id(chip));

	for (idx = 0; idx < ARRAY_SIZE(snd_ad1816a_controls); idx++) {
		if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_ad1816a_controls[idx], chip))) < 0)
			return err;
	}
	return 0;
}