Linux Audio

Check our new training course

Loading...
v3.15
 
   1/*
   2 * Driver for AT73C213 16-bit stereo DAC connected to Atmel SSC
   3 *
   4 * Copyright (C) 2006-2007 Atmel Norway
   5 *
   6 * This program is free software; you can redistribute it and/or modify it
   7 * under the terms of the GNU General Public License version 2 as published by
   8 * the Free Software Foundation.
   9 */
  10
  11/*#define DEBUG*/
  12
  13#include <linux/clk.h>
  14#include <linux/err.h>
  15#include <linux/delay.h>
  16#include <linux/device.h>
  17#include <linux/dma-mapping.h>
  18#include <linux/init.h>
  19#include <linux/interrupt.h>
  20#include <linux/module.h>
  21#include <linux/mutex.h>
  22#include <linux/platform_device.h>
  23#include <linux/io.h>
  24
  25#include <sound/initval.h>
  26#include <sound/control.h>
  27#include <sound/core.h>
  28#include <sound/pcm.h>
  29
  30#include <linux/atmel-ssc.h>
  31
  32#include <linux/spi/spi.h>
  33#include <linux/spi/at73c213.h>
  34
  35#include "at73c213.h"
  36
  37#define BITRATE_MIN	 8000 /* Hardware limit? */
  38#define BITRATE_TARGET	CONFIG_SND_AT73C213_TARGET_BITRATE
  39#define BITRATE_MAX	50000 /* Hardware limit. */
  40
  41/* Initial (hardware reset) AT73C213 register values. */
  42static u8 snd_at73c213_original_image[18] =
  43{
  44	0x00,	/* 00 - CTRL    */
  45	0x05,	/* 01 - LLIG    */
  46	0x05,	/* 02 - RLIG    */
  47	0x08,	/* 03 - LPMG    */
  48	0x08,	/* 04 - RPMG    */
  49	0x00,	/* 05 - LLOG    */
  50	0x00,	/* 06 - RLOG    */
  51	0x22,	/* 07 - OLC     */
  52	0x09,	/* 08 - MC      */
  53	0x00,	/* 09 - CSFC    */
  54	0x00,	/* 0A - MISC    */
  55	0x00,	/* 0B -         */
  56	0x00,	/* 0C - PRECH   */
  57	0x05,	/* 0D - AUXG    */
  58	0x00,	/* 0E -         */
  59	0x00,	/* 0F -         */
  60	0x00,	/* 10 - RST     */
  61	0x00,	/* 11 - PA_CTRL */
  62};
  63
  64struct snd_at73c213 {
  65	struct snd_card			*card;
  66	struct snd_pcm			*pcm;
  67	struct snd_pcm_substream	*substream;
  68	struct at73c213_board_info	*board;
  69	int				irq;
  70	int				period;
  71	unsigned long			bitrate;
  72	struct ssc_device		*ssc;
  73	struct spi_device		*spi;
  74	u8				spi_wbuffer[2];
  75	u8				spi_rbuffer[2];
  76	/* Image of the SPI registers in AT73C213. */
  77	u8				reg_image[18];
  78	/* Protect SSC registers against concurrent access. */
  79	spinlock_t			lock;
  80	/* Protect mixer registers against concurrent access. */
  81	struct mutex			mixer_lock;
  82};
  83
  84#define get_chip(card) ((struct snd_at73c213 *)card->private_data)
  85
  86static int
  87snd_at73c213_write_reg(struct snd_at73c213 *chip, u8 reg, u8 val)
  88{
  89	struct spi_message msg;
  90	struct spi_transfer msg_xfer = {
  91		.len		= 2,
  92		.cs_change	= 0,
  93	};
  94	int retval;
  95
  96	spi_message_init(&msg);
  97
  98	chip->spi_wbuffer[0] = reg;
  99	chip->spi_wbuffer[1] = val;
 100
 101	msg_xfer.tx_buf = chip->spi_wbuffer;
 102	msg_xfer.rx_buf = chip->spi_rbuffer;
 103	spi_message_add_tail(&msg_xfer, &msg);
 104
 105	retval = spi_sync(chip->spi, &msg);
 106
 107	if (!retval)
 108		chip->reg_image[reg] = val;
 109
 110	return retval;
 111}
 112
 113static struct snd_pcm_hardware snd_at73c213_playback_hw = {
 114	.info		= SNDRV_PCM_INFO_INTERLEAVED |
 115			  SNDRV_PCM_INFO_BLOCK_TRANSFER,
 116	.formats	= SNDRV_PCM_FMTBIT_S16_BE,
 117	.rates		= SNDRV_PCM_RATE_CONTINUOUS,
 118	.rate_min	= 8000,  /* Replaced by chip->bitrate later. */
 119	.rate_max	= 50000, /* Replaced by chip->bitrate later. */
 120	.channels_min	= 1,
 121	.channels_max	= 2,
 122	.buffer_bytes_max = 64 * 1024 - 1,
 123	.period_bytes_min = 512,
 124	.period_bytes_max = 64 * 1024 - 1,
 125	.periods_min	= 4,
 126	.periods_max	= 1024,
 127};
 128
 129/*
 130 * Calculate and set bitrate and divisions.
 131 */
 132static int snd_at73c213_set_bitrate(struct snd_at73c213 *chip)
 133{
 134	unsigned long ssc_rate = clk_get_rate(chip->ssc->clk);
 135	unsigned long dac_rate_new, ssc_div;
 136	int status;
 137	unsigned long ssc_div_max, ssc_div_min;
 138	int max_tries;
 139
 140	/*
 141	 * We connect two clocks here, picking divisors so the I2S clocks
 142	 * out data at the same rate the DAC clocks it in ... and as close
 143	 * as practical to the desired target rate.
 144	 *
 145	 * The DAC master clock (MCLK) is programmable, and is either 256
 146	 * or (not here) 384 times the I2S output clock (BCLK).
 147	 */
 148
 149	/* SSC clock / (bitrate * stereo * 16-bit). */
 150	ssc_div = ssc_rate / (BITRATE_TARGET * 2 * 16);
 151	ssc_div_min = ssc_rate / (BITRATE_MAX * 2 * 16);
 152	ssc_div_max = ssc_rate / (BITRATE_MIN * 2 * 16);
 153	max_tries = (ssc_div_max - ssc_div_min) / 2;
 154
 155	if (max_tries < 1)
 156		max_tries = 1;
 157
 158	/* ssc_div must be even. */
 159	ssc_div = (ssc_div + 1) & ~1UL;
 160
 161	if ((ssc_rate / (ssc_div * 2 * 16)) < BITRATE_MIN) {
 162		ssc_div -= 2;
 163		if ((ssc_rate / (ssc_div * 2 * 16)) > BITRATE_MAX)
 164			return -ENXIO;
 165	}
 166
 167	/* Search for a possible bitrate. */
 168	do {
 169		/* SSC clock / (ssc divider * 16-bit * stereo). */
 170		if ((ssc_rate / (ssc_div * 2 * 16)) < BITRATE_MIN)
 171			return -ENXIO;
 172
 173		/* 256 / (2 * 16) = 8 */
 174		dac_rate_new = 8 * (ssc_rate / ssc_div);
 175
 176		status = clk_round_rate(chip->board->dac_clk, dac_rate_new);
 177		if (status <= 0)
 178			return status;
 179
 180		/* Ignore difference smaller than 256 Hz. */
 181		if ((status/256) == (dac_rate_new/256))
 182			goto set_rate;
 183
 184		ssc_div += 2;
 185	} while (--max_tries);
 186
 187	/* Not able to find a valid bitrate. */
 188	return -ENXIO;
 189
 190set_rate:
 191	status = clk_set_rate(chip->board->dac_clk, status);
 192	if (status < 0)
 193		return status;
 194
 195	/* Set divider in SSC device. */
 196	ssc_writel(chip->ssc->regs, CMR, ssc_div/2);
 197
 198	/* SSC clock / (ssc divider * 16-bit * stereo). */
 199	chip->bitrate = ssc_rate / (ssc_div * 16 * 2);
 200
 201	dev_info(&chip->spi->dev,
 202			"at73c213: supported bitrate is %lu (%lu divider)\n",
 203			chip->bitrate, ssc_div);
 204
 205	return 0;
 206}
 207
 208static int snd_at73c213_pcm_open(struct snd_pcm_substream *substream)
 209{
 210	struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
 211	struct snd_pcm_runtime *runtime = substream->runtime;
 212	int err;
 213
 214	/* ensure buffer_size is a multiple of period_size */
 215	err = snd_pcm_hw_constraint_integer(runtime,
 216					SNDRV_PCM_HW_PARAM_PERIODS);
 217	if (err < 0)
 218		return err;
 219	snd_at73c213_playback_hw.rate_min = chip->bitrate;
 220	snd_at73c213_playback_hw.rate_max = chip->bitrate;
 221	runtime->hw = snd_at73c213_playback_hw;
 222	chip->substream = substream;
 223
 
 
 224	return 0;
 225}
 226
 227static int snd_at73c213_pcm_close(struct snd_pcm_substream *substream)
 228{
 229	struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
 230	chip->substream = NULL;
 
 231	return 0;
 232}
 233
 234static int snd_at73c213_pcm_hw_params(struct snd_pcm_substream *substream,
 235				 struct snd_pcm_hw_params *hw_params)
 236{
 237	struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
 238	int channels = params_channels(hw_params);
 239	int val;
 240
 241	val = ssc_readl(chip->ssc->regs, TFMR);
 242	val = SSC_BFINS(TFMR_DATNB, channels - 1, val);
 243	ssc_writel(chip->ssc->regs, TFMR, val);
 244
 245	return snd_pcm_lib_malloc_pages(substream,
 246					params_buffer_bytes(hw_params));
 247}
 248
 249static int snd_at73c213_pcm_hw_free(struct snd_pcm_substream *substream)
 250{
 251	return snd_pcm_lib_free_pages(substream);
 252}
 253
 254static int snd_at73c213_pcm_prepare(struct snd_pcm_substream *substream)
 255{
 256	struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
 257	struct snd_pcm_runtime *runtime = substream->runtime;
 258	int block_size;
 259
 260	block_size = frames_to_bytes(runtime, runtime->period_size);
 261
 262	chip->period = 0;
 263
 264	ssc_writel(chip->ssc->regs, PDC_TPR,
 265			(long)runtime->dma_addr);
 266	ssc_writel(chip->ssc->regs, PDC_TCR,
 267			runtime->period_size * runtime->channels);
 268	ssc_writel(chip->ssc->regs, PDC_TNPR,
 269			(long)runtime->dma_addr + block_size);
 270	ssc_writel(chip->ssc->regs, PDC_TNCR,
 271			runtime->period_size * runtime->channels);
 272
 273	return 0;
 274}
 275
 276static int snd_at73c213_pcm_trigger(struct snd_pcm_substream *substream,
 277				   int cmd)
 278{
 279	struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
 280	int retval = 0;
 281
 282	spin_lock(&chip->lock);
 283
 284	switch (cmd) {
 285	case SNDRV_PCM_TRIGGER_START:
 286		ssc_writel(chip->ssc->regs, IER, SSC_BIT(IER_ENDTX));
 287		ssc_writel(chip->ssc->regs, PDC_PTCR, SSC_BIT(PDC_PTCR_TXTEN));
 288		break;
 289	case SNDRV_PCM_TRIGGER_STOP:
 290		ssc_writel(chip->ssc->regs, PDC_PTCR, SSC_BIT(PDC_PTCR_TXTDIS));
 291		ssc_writel(chip->ssc->regs, IDR, SSC_BIT(IDR_ENDTX));
 292		break;
 293	default:
 294		dev_dbg(&chip->spi->dev, "spurious command %x\n", cmd);
 295		retval = -EINVAL;
 296		break;
 297	}
 298
 299	spin_unlock(&chip->lock);
 300
 301	return retval;
 302}
 303
 304static snd_pcm_uframes_t
 305snd_at73c213_pcm_pointer(struct snd_pcm_substream *substream)
 306{
 307	struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
 308	struct snd_pcm_runtime *runtime = substream->runtime;
 309	snd_pcm_uframes_t pos;
 310	unsigned long bytes;
 311
 312	bytes = ssc_readl(chip->ssc->regs, PDC_TPR)
 313		- (unsigned long)runtime->dma_addr;
 314
 315	pos = bytes_to_frames(runtime, bytes);
 316	if (pos >= runtime->buffer_size)
 317		pos -= runtime->buffer_size;
 318
 319	return pos;
 320}
 321
 322static struct snd_pcm_ops at73c213_playback_ops = {
 323	.open		= snd_at73c213_pcm_open,
 324	.close		= snd_at73c213_pcm_close,
 325	.ioctl		= snd_pcm_lib_ioctl,
 326	.hw_params	= snd_at73c213_pcm_hw_params,
 327	.hw_free	= snd_at73c213_pcm_hw_free,
 328	.prepare	= snd_at73c213_pcm_prepare,
 329	.trigger	= snd_at73c213_pcm_trigger,
 330	.pointer	= snd_at73c213_pcm_pointer,
 331};
 332
 333static int snd_at73c213_pcm_new(struct snd_at73c213 *chip, int device)
 334{
 335	struct snd_pcm *pcm;
 336	int retval;
 337
 338	retval = snd_pcm_new(chip->card, chip->card->shortname,
 339			device, 1, 0, &pcm);
 340	if (retval < 0)
 341		goto out;
 342
 343	pcm->private_data = chip;
 344	pcm->info_flags = SNDRV_PCM_INFO_BLOCK_TRANSFER;
 345	strcpy(pcm->name, "at73c213");
 346	chip->pcm = pcm;
 347
 348	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &at73c213_playback_ops);
 349
 350	retval = snd_pcm_lib_preallocate_pages_for_all(chip->pcm,
 351			SNDRV_DMA_TYPE_DEV, &chip->ssc->pdev->dev,
 352			64 * 1024, 64 * 1024);
 353out:
 354	return retval;
 355}
 356
 357static irqreturn_t snd_at73c213_interrupt(int irq, void *dev_id)
 358{
 359	struct snd_at73c213 *chip = dev_id;
 360	struct snd_pcm_runtime *runtime = chip->substream->runtime;
 361	u32 status;
 362	int offset;
 363	int block_size;
 364	int next_period;
 365	int retval = IRQ_NONE;
 366
 367	spin_lock(&chip->lock);
 368
 369	block_size = frames_to_bytes(runtime, runtime->period_size);
 370	status = ssc_readl(chip->ssc->regs, IMR);
 371
 372	if (status & SSC_BIT(IMR_ENDTX)) {
 373		chip->period++;
 374		if (chip->period == runtime->periods)
 375			chip->period = 0;
 376		next_period = chip->period + 1;
 377		if (next_period == runtime->periods)
 378			next_period = 0;
 379
 380		offset = block_size * next_period;
 381
 382		ssc_writel(chip->ssc->regs, PDC_TNPR,
 383				(long)runtime->dma_addr + offset);
 384		ssc_writel(chip->ssc->regs, PDC_TNCR,
 385				runtime->period_size * runtime->channels);
 386		retval = IRQ_HANDLED;
 387	}
 388
 389	ssc_readl(chip->ssc->regs, IMR);
 390	spin_unlock(&chip->lock);
 391
 392	if (status & SSC_BIT(IMR_ENDTX))
 393		snd_pcm_period_elapsed(chip->substream);
 394
 395	return retval;
 396}
 397
 398/*
 399 * Mixer functions.
 400 */
 401static int snd_at73c213_mono_get(struct snd_kcontrol *kcontrol,
 402				 struct snd_ctl_elem_value *ucontrol)
 403{
 404	struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
 405	int reg = kcontrol->private_value & 0xff;
 406	int shift = (kcontrol->private_value >> 8) & 0xff;
 407	int mask = (kcontrol->private_value >> 16) & 0xff;
 408	int invert = (kcontrol->private_value >> 24) & 0xff;
 409
 410	mutex_lock(&chip->mixer_lock);
 411
 412	ucontrol->value.integer.value[0] =
 413		(chip->reg_image[reg] >> shift) & mask;
 414
 415	if (invert)
 416		ucontrol->value.integer.value[0] =
 417			mask - ucontrol->value.integer.value[0];
 418
 419	mutex_unlock(&chip->mixer_lock);
 420
 421	return 0;
 422}
 423
 424static int snd_at73c213_mono_put(struct snd_kcontrol *kcontrol,
 425				 struct snd_ctl_elem_value *ucontrol)
 426{
 427	struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
 428	int reg = kcontrol->private_value & 0xff;
 429	int shift = (kcontrol->private_value >> 8) & 0xff;
 430	int mask = (kcontrol->private_value >> 16) & 0xff;
 431	int invert = (kcontrol->private_value >> 24) & 0xff;
 432	int change, retval;
 433	unsigned short val;
 434
 435	val = (ucontrol->value.integer.value[0] & mask);
 436	if (invert)
 437		val = mask - val;
 438	val <<= shift;
 439
 440	mutex_lock(&chip->mixer_lock);
 441
 442	val = (chip->reg_image[reg] & ~(mask << shift)) | val;
 443	change = val != chip->reg_image[reg];
 444	retval = snd_at73c213_write_reg(chip, reg, val);
 445
 446	mutex_unlock(&chip->mixer_lock);
 447
 448	if (retval)
 449		return retval;
 450
 451	return change;
 452}
 453
 454static int snd_at73c213_stereo_info(struct snd_kcontrol *kcontrol,
 455				  struct snd_ctl_elem_info *uinfo)
 456{
 457	int mask = (kcontrol->private_value >> 24) & 0xff;
 458
 459	if (mask == 1)
 460		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
 461	else
 462		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
 463
 464	uinfo->count = 2;
 465	uinfo->value.integer.min = 0;
 466	uinfo->value.integer.max = mask;
 467
 468	return 0;
 469}
 470
 471static int snd_at73c213_stereo_get(struct snd_kcontrol *kcontrol,
 472				 struct snd_ctl_elem_value *ucontrol)
 473{
 474	struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
 475	int left_reg = kcontrol->private_value & 0xff;
 476	int right_reg = (kcontrol->private_value >> 8) & 0xff;
 477	int shift_left = (kcontrol->private_value >> 16) & 0x07;
 478	int shift_right = (kcontrol->private_value >> 19) & 0x07;
 479	int mask = (kcontrol->private_value >> 24) & 0xff;
 480	int invert = (kcontrol->private_value >> 22) & 1;
 481
 482	mutex_lock(&chip->mixer_lock);
 483
 484	ucontrol->value.integer.value[0] =
 485		(chip->reg_image[left_reg] >> shift_left) & mask;
 486	ucontrol->value.integer.value[1] =
 487		(chip->reg_image[right_reg] >> shift_right) & mask;
 488
 489	if (invert) {
 490		ucontrol->value.integer.value[0] =
 491			mask - ucontrol->value.integer.value[0];
 492		ucontrol->value.integer.value[1] =
 493			mask - ucontrol->value.integer.value[1];
 494	}
 495
 496	mutex_unlock(&chip->mixer_lock);
 497
 498	return 0;
 499}
 500
 501static int snd_at73c213_stereo_put(struct snd_kcontrol *kcontrol,
 502				 struct snd_ctl_elem_value *ucontrol)
 503{
 504	struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
 505	int left_reg = kcontrol->private_value & 0xff;
 506	int right_reg = (kcontrol->private_value >> 8) & 0xff;
 507	int shift_left = (kcontrol->private_value >> 16) & 0x07;
 508	int shift_right = (kcontrol->private_value >> 19) & 0x07;
 509	int mask = (kcontrol->private_value >> 24) & 0xff;
 510	int invert = (kcontrol->private_value >> 22) & 1;
 511	int change, retval;
 512	unsigned short val1, val2;
 513
 514	val1 = ucontrol->value.integer.value[0] & mask;
 515	val2 = ucontrol->value.integer.value[1] & mask;
 516	if (invert) {
 517		val1 = mask - val1;
 518		val2 = mask - val2;
 519	}
 520	val1 <<= shift_left;
 521	val2 <<= shift_right;
 522
 523	mutex_lock(&chip->mixer_lock);
 524
 525	val1 = (chip->reg_image[left_reg] & ~(mask << shift_left)) | val1;
 526	val2 = (chip->reg_image[right_reg] & ~(mask << shift_right)) | val2;
 527	change = val1 != chip->reg_image[left_reg]
 528		|| val2 != chip->reg_image[right_reg];
 529	retval = snd_at73c213_write_reg(chip, left_reg, val1);
 530	if (retval) {
 531		mutex_unlock(&chip->mixer_lock);
 532		goto out;
 533	}
 534	retval = snd_at73c213_write_reg(chip, right_reg, val2);
 535	if (retval) {
 536		mutex_unlock(&chip->mixer_lock);
 537		goto out;
 538	}
 539
 540	mutex_unlock(&chip->mixer_lock);
 541
 542	return change;
 543
 544out:
 545	return retval;
 546}
 547
 548#define snd_at73c213_mono_switch_info	snd_ctl_boolean_mono_info
 549
 550static int snd_at73c213_mono_switch_get(struct snd_kcontrol *kcontrol,
 551				 struct snd_ctl_elem_value *ucontrol)
 552{
 553	struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
 554	int reg = kcontrol->private_value & 0xff;
 555	int shift = (kcontrol->private_value >> 8) & 0xff;
 556	int invert = (kcontrol->private_value >> 24) & 0xff;
 557
 558	mutex_lock(&chip->mixer_lock);
 559
 560	ucontrol->value.integer.value[0] =
 561		(chip->reg_image[reg] >> shift) & 0x01;
 562
 563	if (invert)
 564		ucontrol->value.integer.value[0] =
 565			0x01 - ucontrol->value.integer.value[0];
 566
 567	mutex_unlock(&chip->mixer_lock);
 568
 569	return 0;
 570}
 571
 572static int snd_at73c213_mono_switch_put(struct snd_kcontrol *kcontrol,
 573				 struct snd_ctl_elem_value *ucontrol)
 574{
 575	struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
 576	int reg = kcontrol->private_value & 0xff;
 577	int shift = (kcontrol->private_value >> 8) & 0xff;
 578	int mask = (kcontrol->private_value >> 16) & 0xff;
 579	int invert = (kcontrol->private_value >> 24) & 0xff;
 580	int change, retval;
 581	unsigned short val;
 582
 583	if (ucontrol->value.integer.value[0])
 584		val = mask;
 585	else
 586		val = 0;
 587
 588	if (invert)
 589		val = mask - val;
 590	val <<= shift;
 591
 592	mutex_lock(&chip->mixer_lock);
 593
 594	val |= (chip->reg_image[reg] & ~(mask << shift));
 595	change = val != chip->reg_image[reg];
 596
 597	retval = snd_at73c213_write_reg(chip, reg, val);
 598
 599	mutex_unlock(&chip->mixer_lock);
 600
 601	if (retval)
 602		return retval;
 603
 604	return change;
 605}
 606
 607static int snd_at73c213_pa_volume_info(struct snd_kcontrol *kcontrol,
 608				  struct snd_ctl_elem_info *uinfo)
 609{
 610	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
 611	uinfo->count = 1;
 612	uinfo->value.integer.min = 0;
 613	uinfo->value.integer.max = ((kcontrol->private_value >> 16) & 0xff) - 1;
 614
 615	return 0;
 616}
 617
 618static int snd_at73c213_line_capture_volume_info(
 619		struct snd_kcontrol *kcontrol,
 620		struct snd_ctl_elem_info *uinfo)
 621{
 622	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
 623	uinfo->count = 2;
 624	/* When inverted will give values 0x10001 => 0. */
 625	uinfo->value.integer.min = 14;
 626	uinfo->value.integer.max = 31;
 627
 628	return 0;
 629}
 630
 631static int snd_at73c213_aux_capture_volume_info(
 632		struct snd_kcontrol *kcontrol,
 633		struct snd_ctl_elem_info *uinfo)
 634{
 635	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
 636	uinfo->count = 1;
 637	/* When inverted will give values 0x10001 => 0. */
 638	uinfo->value.integer.min = 14;
 639	uinfo->value.integer.max = 31;
 640
 641	return 0;
 642}
 643
 644#define AT73C213_MONO_SWITCH(xname, xindex, reg, shift, mask, invert)	\
 645{									\
 646	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,				\
 647	.name = xname,							\
 648	.index = xindex,						\
 649	.info = snd_at73c213_mono_switch_info,				\
 650	.get = snd_at73c213_mono_switch_get,				\
 651	.put = snd_at73c213_mono_switch_put,				\
 652	.private_value = (reg | (shift << 8) | (mask << 16) | (invert << 24)) \
 653}
 654
 655#define AT73C213_STEREO(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
 656{									\
 657	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,				\
 658	.name = xname,							\
 659	.index = xindex,						\
 660	.info = snd_at73c213_stereo_info,				\
 661	.get = snd_at73c213_stereo_get,					\
 662	.put = snd_at73c213_stereo_put,					\
 663	.private_value = (left_reg | (right_reg << 8)			\
 664			| (shift_left << 16) | (shift_right << 19)	\
 665			| (mask << 24) | (invert << 22))		\
 666}
 667
 668static struct snd_kcontrol_new snd_at73c213_controls[] = {
 669AT73C213_STEREO("Master Playback Volume", 0, DAC_LMPG, DAC_RMPG, 0, 0, 0x1f, 1),
 670AT73C213_STEREO("Master Playback Switch", 0, DAC_LMPG, DAC_RMPG, 5, 5, 1, 1),
 671AT73C213_STEREO("PCM Playback Volume", 0, DAC_LLOG, DAC_RLOG, 0, 0, 0x1f, 1),
 672AT73C213_STEREO("PCM Playback Switch", 0, DAC_LLOG, DAC_RLOG, 5, 5, 1, 1),
 673AT73C213_MONO_SWITCH("Mono PA Playback Switch", 0, DAC_CTRL, DAC_CTRL_ONPADRV,
 674		     0x01, 0),
 675{
 676	.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
 677	.name	= "PA Playback Volume",
 678	.index	= 0,
 679	.info	= snd_at73c213_pa_volume_info,
 680	.get	= snd_at73c213_mono_get,
 681	.put	= snd_at73c213_mono_put,
 682	.private_value	= PA_CTRL | (PA_CTRL_APAGAIN << 8) | \
 683		(0x0f << 16) | (1 << 24),
 684},
 685AT73C213_MONO_SWITCH("PA High Gain Playback Switch", 0, PA_CTRL, PA_CTRL_APALP,
 686		     0x01, 1),
 687AT73C213_MONO_SWITCH("PA Playback Switch", 0, PA_CTRL, PA_CTRL_APAON, 0x01, 0),
 688{
 689	.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
 690	.name	= "Aux Capture Volume",
 691	.index	= 0,
 692	.info	= snd_at73c213_aux_capture_volume_info,
 693	.get	= snd_at73c213_mono_get,
 694	.put	= snd_at73c213_mono_put,
 695	.private_value	= DAC_AUXG | (0 << 8) | (0x1f << 16) | (1 << 24),
 696},
 697AT73C213_MONO_SWITCH("Aux Capture Switch", 0, DAC_CTRL, DAC_CTRL_ONAUXIN,
 698		     0x01, 0),
 699{
 700	.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
 701	.name	= "Line Capture Volume",
 702	.index	= 0,
 703	.info	= snd_at73c213_line_capture_volume_info,
 704	.get	= snd_at73c213_stereo_get,
 705	.put	= snd_at73c213_stereo_put,
 706	.private_value	= DAC_LLIG | (DAC_RLIG << 8) | (0 << 16) | (0 << 19)
 707		| (0x1f << 24) | (1 << 22),
 708},
 709AT73C213_MONO_SWITCH("Line Capture Switch", 0, DAC_CTRL, 0, 0x03, 0),
 710};
 711
 712static int snd_at73c213_mixer(struct snd_at73c213 *chip)
 713{
 714	struct snd_card *card;
 715	int errval, idx;
 716
 717	if (chip == NULL || chip->pcm == NULL)
 718		return -EINVAL;
 719
 720	card = chip->card;
 721
 722	strcpy(card->mixername, chip->pcm->name);
 723
 724	for (idx = 0; idx < ARRAY_SIZE(snd_at73c213_controls); idx++) {
 725		errval = snd_ctl_add(card,
 726				snd_ctl_new1(&snd_at73c213_controls[idx],
 727					chip));
 728		if (errval < 0)
 729			goto cleanup;
 730	}
 731
 732	return 0;
 733
 734cleanup:
 735	for (idx = 1; idx < ARRAY_SIZE(snd_at73c213_controls) + 1; idx++) {
 736		struct snd_kcontrol *kctl;
 737		kctl = snd_ctl_find_numid(card, idx);
 738		if (kctl)
 739			snd_ctl_remove(card, kctl);
 740	}
 741	return errval;
 742}
 743
 744/*
 745 * Device functions
 746 */
 747static int snd_at73c213_ssc_init(struct snd_at73c213 *chip)
 748{
 749	/*
 750	 * Continuous clock output.
 751	 * Starts on falling TF.
 752	 * Delay 1 cycle (1 bit).
 753	 * Periode is 16 bit (16 - 1).
 754	 */
 755	ssc_writel(chip->ssc->regs, TCMR,
 756			SSC_BF(TCMR_CKO, 1)
 757			| SSC_BF(TCMR_START, 4)
 758			| SSC_BF(TCMR_STTDLY, 1)
 759			| SSC_BF(TCMR_PERIOD, 16 - 1));
 760	/*
 761	 * Data length is 16 bit (16 - 1).
 762	 * Transmit MSB first.
 763	 * Transmit 2 words each transfer.
 764	 * Frame sync length is 16 bit (16 - 1).
 765	 * Frame starts on negative pulse.
 766	 */
 767	ssc_writel(chip->ssc->regs, TFMR,
 768			SSC_BF(TFMR_DATLEN, 16 - 1)
 769			| SSC_BIT(TFMR_MSBF)
 770			| SSC_BF(TFMR_DATNB, 1)
 771			| SSC_BF(TFMR_FSLEN, 16 - 1)
 772			| SSC_BF(TFMR_FSOS, 1));
 773
 774	return 0;
 775}
 776
 777static int snd_at73c213_chip_init(struct snd_at73c213 *chip)
 778{
 779	int retval;
 780	unsigned char dac_ctrl = 0;
 781
 782	retval = snd_at73c213_set_bitrate(chip);
 783	if (retval)
 784		goto out;
 785
 786	/* Enable DAC master clock. */
 787	clk_enable(chip->board->dac_clk);
 788
 789	/* Initialize at73c213 on SPI bus. */
 790	retval = snd_at73c213_write_reg(chip, DAC_RST, 0x04);
 791	if (retval)
 792		goto out_clk;
 793	msleep(1);
 794	retval = snd_at73c213_write_reg(chip, DAC_RST, 0x03);
 795	if (retval)
 796		goto out_clk;
 797
 798	/* Precharge everything. */
 799	retval = snd_at73c213_write_reg(chip, DAC_PRECH, 0xff);
 800	if (retval)
 801		goto out_clk;
 802	retval = snd_at73c213_write_reg(chip, PA_CTRL, (1<<PA_CTRL_APAPRECH));
 803	if (retval)
 804		goto out_clk;
 805	retval = snd_at73c213_write_reg(chip, DAC_CTRL,
 806			(1<<DAC_CTRL_ONLNOL) | (1<<DAC_CTRL_ONLNOR));
 807	if (retval)
 808		goto out_clk;
 809
 810	msleep(50);
 811
 812	/* Stop precharging PA. */
 813	retval = snd_at73c213_write_reg(chip, PA_CTRL,
 814			(1<<PA_CTRL_APALP) | 0x0f);
 815	if (retval)
 816		goto out_clk;
 817
 818	msleep(450);
 819
 820	/* Stop precharging DAC, turn on master power. */
 821	retval = snd_at73c213_write_reg(chip, DAC_PRECH, (1<<DAC_PRECH_ONMSTR));
 822	if (retval)
 823		goto out_clk;
 824
 825	msleep(1);
 826
 827	/* Turn on DAC. */
 828	dac_ctrl = (1<<DAC_CTRL_ONDACL) | (1<<DAC_CTRL_ONDACR)
 829		| (1<<DAC_CTRL_ONLNOL) | (1<<DAC_CTRL_ONLNOR);
 830
 831	retval = snd_at73c213_write_reg(chip, DAC_CTRL, dac_ctrl);
 832	if (retval)
 833		goto out_clk;
 834
 835	/* Mute sound. */
 836	retval = snd_at73c213_write_reg(chip, DAC_LMPG, 0x3f);
 837	if (retval)
 838		goto out_clk;
 839	retval = snd_at73c213_write_reg(chip, DAC_RMPG, 0x3f);
 840	if (retval)
 841		goto out_clk;
 842	retval = snd_at73c213_write_reg(chip, DAC_LLOG, 0x3f);
 843	if (retval)
 844		goto out_clk;
 845	retval = snd_at73c213_write_reg(chip, DAC_RLOG, 0x3f);
 846	if (retval)
 847		goto out_clk;
 848	retval = snd_at73c213_write_reg(chip, DAC_LLIG, 0x11);
 849	if (retval)
 850		goto out_clk;
 851	retval = snd_at73c213_write_reg(chip, DAC_RLIG, 0x11);
 852	if (retval)
 853		goto out_clk;
 854	retval = snd_at73c213_write_reg(chip, DAC_AUXG, 0x11);
 855	if (retval)
 856		goto out_clk;
 857
 858	/* Enable I2S device, i.e. clock output. */
 859	ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXEN));
 860
 861	goto out;
 862
 863out_clk:
 864	clk_disable(chip->board->dac_clk);
 865out:
 866	return retval;
 867}
 868
 869static int snd_at73c213_dev_free(struct snd_device *device)
 870{
 871	struct snd_at73c213 *chip = device->device_data;
 872
 873	ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXDIS));
 874	if (chip->irq >= 0) {
 875		free_irq(chip->irq, chip);
 876		chip->irq = -1;
 877	}
 878
 879	return 0;
 880}
 881
 882static int snd_at73c213_dev_init(struct snd_card *card,
 883				 struct spi_device *spi)
 884{
 885	static struct snd_device_ops ops = {
 886		.dev_free	= snd_at73c213_dev_free,
 887	};
 888	struct snd_at73c213 *chip = get_chip(card);
 889	int irq, retval;
 890
 891	irq = chip->ssc->irq;
 892	if (irq < 0)
 893		return irq;
 894
 895	spin_lock_init(&chip->lock);
 896	mutex_init(&chip->mixer_lock);
 897	chip->card = card;
 898	chip->irq = -1;
 899
 
 
 900	retval = request_irq(irq, snd_at73c213_interrupt, 0, "at73c213", chip);
 901	if (retval) {
 902		dev_dbg(&chip->spi->dev, "unable to request irq %d\n", irq);
 903		goto out;
 904	}
 905	chip->irq = irq;
 906
 907	memcpy(&chip->reg_image, &snd_at73c213_original_image,
 908			sizeof(snd_at73c213_original_image));
 909
 910	retval = snd_at73c213_ssc_init(chip);
 911	if (retval)
 912		goto out_irq;
 913
 914	retval = snd_at73c213_chip_init(chip);
 915	if (retval)
 916		goto out_irq;
 917
 918	retval = snd_at73c213_pcm_new(chip, 0);
 919	if (retval)
 920		goto out_irq;
 921
 922	retval = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
 923	if (retval)
 924		goto out_irq;
 925
 926	retval = snd_at73c213_mixer(chip);
 927	if (retval)
 928		goto out_snd_dev;
 929
 930	goto out;
 931
 932out_snd_dev:
 933	snd_device_free(card, chip);
 934out_irq:
 935	free_irq(chip->irq, chip);
 936	chip->irq = -1;
 937out:
 
 
 938	return retval;
 939}
 940
 941static int snd_at73c213_probe(struct spi_device *spi)
 942{
 943	struct snd_card			*card;
 944	struct snd_at73c213		*chip;
 945	struct at73c213_board_info	*board;
 946	int				retval;
 947	char				id[16];
 948
 949	board = spi->dev.platform_data;
 950	if (!board) {
 951		dev_dbg(&spi->dev, "no platform_data\n");
 952		return -ENXIO;
 953	}
 954
 955	if (!board->dac_clk) {
 956		dev_dbg(&spi->dev, "no DAC clk\n");
 957		return -ENXIO;
 958	}
 959
 960	if (IS_ERR(board->dac_clk)) {
 961		dev_dbg(&spi->dev, "no DAC clk\n");
 962		return PTR_ERR(board->dac_clk);
 963	}
 964
 965	/* Allocate "card" using some unused identifiers. */
 966	snprintf(id, sizeof id, "at73c213_%d", board->ssc_id);
 967	retval = snd_card_new(&spi->dev, -1, id, THIS_MODULE,
 968			      sizeof(struct snd_at73c213), &card);
 969	if (retval < 0)
 970		goto out;
 971
 972	chip = card->private_data;
 973	chip->spi = spi;
 974	chip->board = board;
 975
 976	chip->ssc = ssc_request(board->ssc_id);
 977	if (IS_ERR(chip->ssc)) {
 978		dev_dbg(&spi->dev, "could not get ssc%d device\n",
 979				board->ssc_id);
 980		retval = PTR_ERR(chip->ssc);
 981		goto out_card;
 982	}
 983
 984	retval = snd_at73c213_dev_init(card, spi);
 985	if (retval)
 986		goto out_ssc;
 987
 988	strcpy(card->driver, "at73c213");
 989	strcpy(card->shortname, board->shortname);
 990	sprintf(card->longname, "%s on irq %d", card->shortname, chip->irq);
 991
 992	retval = snd_card_register(card);
 993	if (retval)
 994		goto out_ssc;
 995
 996	dev_set_drvdata(&spi->dev, card);
 997
 998	goto out;
 999
1000out_ssc:
1001	ssc_free(chip->ssc);
1002out_card:
1003	snd_card_free(card);
1004out:
1005	return retval;
1006}
1007
1008static int snd_at73c213_remove(struct spi_device *spi)
1009{
1010	struct snd_card *card = dev_get_drvdata(&spi->dev);
1011	struct snd_at73c213 *chip = card->private_data;
1012	int retval;
1013
1014	/* Stop playback. */
 
1015	ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXDIS));
 
1016
1017	/* Mute sound. */
1018	retval = snd_at73c213_write_reg(chip, DAC_LMPG, 0x3f);
1019	if (retval)
1020		goto out;
1021	retval = snd_at73c213_write_reg(chip, DAC_RMPG, 0x3f);
1022	if (retval)
1023		goto out;
1024	retval = snd_at73c213_write_reg(chip, DAC_LLOG, 0x3f);
1025	if (retval)
1026		goto out;
1027	retval = snd_at73c213_write_reg(chip, DAC_RLOG, 0x3f);
1028	if (retval)
1029		goto out;
1030	retval = snd_at73c213_write_reg(chip, DAC_LLIG, 0x11);
1031	if (retval)
1032		goto out;
1033	retval = snd_at73c213_write_reg(chip, DAC_RLIG, 0x11);
1034	if (retval)
1035		goto out;
1036	retval = snd_at73c213_write_reg(chip, DAC_AUXG, 0x11);
1037	if (retval)
1038		goto out;
1039
1040	/* Turn off PA. */
1041	retval = snd_at73c213_write_reg(chip, PA_CTRL,
1042					chip->reg_image[PA_CTRL] | 0x0f);
1043	if (retval)
1044		goto out;
1045	msleep(10);
1046	retval = snd_at73c213_write_reg(chip, PA_CTRL,
1047					(1 << PA_CTRL_APALP) | 0x0f);
1048	if (retval)
1049		goto out;
1050
1051	/* Turn off external DAC. */
1052	retval = snd_at73c213_write_reg(chip, DAC_CTRL, 0x0c);
1053	if (retval)
1054		goto out;
1055	msleep(2);
1056	retval = snd_at73c213_write_reg(chip, DAC_CTRL, 0x00);
1057	if (retval)
1058		goto out;
1059
1060	/* Turn off master power. */
1061	retval = snd_at73c213_write_reg(chip, DAC_PRECH, 0x00);
1062	if (retval)
1063		goto out;
1064
1065out:
1066	/* Stop DAC master clock. */
1067	clk_disable(chip->board->dac_clk);
1068
1069	ssc_free(chip->ssc);
1070	snd_card_free(card);
1071
1072	return 0;
1073}
1074
1075#ifdef CONFIG_PM_SLEEP
1076
1077static int snd_at73c213_suspend(struct device *dev)
1078{
1079	struct snd_card *card = dev_get_drvdata(dev);
1080	struct snd_at73c213 *chip = card->private_data;
1081
1082	ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXDIS));
 
1083	clk_disable(chip->board->dac_clk);
1084
1085	return 0;
1086}
1087
1088static int snd_at73c213_resume(struct device *dev)
1089{
1090	struct snd_card *card = dev_get_drvdata(dev);
1091	struct snd_at73c213 *chip = card->private_data;
1092
1093	clk_enable(chip->board->dac_clk);
 
1094	ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXEN));
1095
1096	return 0;
1097}
1098
1099static SIMPLE_DEV_PM_OPS(at73c213_pm_ops, snd_at73c213_suspend,
1100		snd_at73c213_resume);
1101#define AT73C213_PM_OPS (&at73c213_pm_ops)
1102
1103#else
1104#define AT73C213_PM_OPS NULL
1105#endif
1106
1107static struct spi_driver at73c213_driver = {
1108	.driver		= {
1109		.name	= "at73c213",
1110		.pm	= AT73C213_PM_OPS,
1111	},
1112	.probe		= snd_at73c213_probe,
1113	.remove		= snd_at73c213_remove,
1114};
1115
1116module_spi_driver(at73c213_driver);
1117
1118MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>");
1119MODULE_DESCRIPTION("Sound driver for AT73C213 with Atmel SSC");
1120MODULE_LICENSE("GPL");
v5.4
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Driver for AT73C213 16-bit stereo DAC connected to Atmel SSC
   4 *
   5 * Copyright (C) 2006-2007 Atmel Norway
 
 
 
 
   6 */
   7
   8/*#define DEBUG*/
   9
  10#include <linux/clk.h>
  11#include <linux/err.h>
  12#include <linux/delay.h>
  13#include <linux/device.h>
  14#include <linux/dma-mapping.h>
  15#include <linux/init.h>
  16#include <linux/interrupt.h>
  17#include <linux/module.h>
  18#include <linux/mutex.h>
  19#include <linux/platform_device.h>
  20#include <linux/io.h>
  21
  22#include <sound/initval.h>
  23#include <sound/control.h>
  24#include <sound/core.h>
  25#include <sound/pcm.h>
  26
  27#include <linux/atmel-ssc.h>
  28
  29#include <linux/spi/spi.h>
  30#include <linux/spi/at73c213.h>
  31
  32#include "at73c213.h"
  33
  34#define BITRATE_MIN	 8000 /* Hardware limit? */
  35#define BITRATE_TARGET	CONFIG_SND_AT73C213_TARGET_BITRATE
  36#define BITRATE_MAX	50000 /* Hardware limit. */
  37
  38/* Initial (hardware reset) AT73C213 register values. */
  39static u8 snd_at73c213_original_image[18] =
  40{
  41	0x00,	/* 00 - CTRL    */
  42	0x05,	/* 01 - LLIG    */
  43	0x05,	/* 02 - RLIG    */
  44	0x08,	/* 03 - LPMG    */
  45	0x08,	/* 04 - RPMG    */
  46	0x00,	/* 05 - LLOG    */
  47	0x00,	/* 06 - RLOG    */
  48	0x22,	/* 07 - OLC     */
  49	0x09,	/* 08 - MC      */
  50	0x00,	/* 09 - CSFC    */
  51	0x00,	/* 0A - MISC    */
  52	0x00,	/* 0B -         */
  53	0x00,	/* 0C - PRECH   */
  54	0x05,	/* 0D - AUXG    */
  55	0x00,	/* 0E -         */
  56	0x00,	/* 0F -         */
  57	0x00,	/* 10 - RST     */
  58	0x00,	/* 11 - PA_CTRL */
  59};
  60
  61struct snd_at73c213 {
  62	struct snd_card			*card;
  63	struct snd_pcm			*pcm;
  64	struct snd_pcm_substream	*substream;
  65	struct at73c213_board_info	*board;
  66	int				irq;
  67	int				period;
  68	unsigned long			bitrate;
  69	struct ssc_device		*ssc;
  70	struct spi_device		*spi;
  71	u8				spi_wbuffer[2];
  72	u8				spi_rbuffer[2];
  73	/* Image of the SPI registers in AT73C213. */
  74	u8				reg_image[18];
  75	/* Protect SSC registers against concurrent access. */
  76	spinlock_t			lock;
  77	/* Protect mixer registers against concurrent access. */
  78	struct mutex			mixer_lock;
  79};
  80
  81#define get_chip(card) ((struct snd_at73c213 *)card->private_data)
  82
  83static int
  84snd_at73c213_write_reg(struct snd_at73c213 *chip, u8 reg, u8 val)
  85{
  86	struct spi_message msg;
  87	struct spi_transfer msg_xfer = {
  88		.len		= 2,
  89		.cs_change	= 0,
  90	};
  91	int retval;
  92
  93	spi_message_init(&msg);
  94
  95	chip->spi_wbuffer[0] = reg;
  96	chip->spi_wbuffer[1] = val;
  97
  98	msg_xfer.tx_buf = chip->spi_wbuffer;
  99	msg_xfer.rx_buf = chip->spi_rbuffer;
 100	spi_message_add_tail(&msg_xfer, &msg);
 101
 102	retval = spi_sync(chip->spi, &msg);
 103
 104	if (!retval)
 105		chip->reg_image[reg] = val;
 106
 107	return retval;
 108}
 109
 110static struct snd_pcm_hardware snd_at73c213_playback_hw = {
 111	.info		= SNDRV_PCM_INFO_INTERLEAVED |
 112			  SNDRV_PCM_INFO_BLOCK_TRANSFER,
 113	.formats	= SNDRV_PCM_FMTBIT_S16_BE,
 114	.rates		= SNDRV_PCM_RATE_CONTINUOUS,
 115	.rate_min	= 8000,  /* Replaced by chip->bitrate later. */
 116	.rate_max	= 50000, /* Replaced by chip->bitrate later. */
 117	.channels_min	= 1,
 118	.channels_max	= 2,
 119	.buffer_bytes_max = 64 * 1024 - 1,
 120	.period_bytes_min = 512,
 121	.period_bytes_max = 64 * 1024 - 1,
 122	.periods_min	= 4,
 123	.periods_max	= 1024,
 124};
 125
 126/*
 127 * Calculate and set bitrate and divisions.
 128 */
 129static int snd_at73c213_set_bitrate(struct snd_at73c213 *chip)
 130{
 131	unsigned long ssc_rate = clk_get_rate(chip->ssc->clk);
 132	unsigned long dac_rate_new, ssc_div;
 133	int status;
 134	unsigned long ssc_div_max, ssc_div_min;
 135	int max_tries;
 136
 137	/*
 138	 * We connect two clocks here, picking divisors so the I2S clocks
 139	 * out data at the same rate the DAC clocks it in ... and as close
 140	 * as practical to the desired target rate.
 141	 *
 142	 * The DAC master clock (MCLK) is programmable, and is either 256
 143	 * or (not here) 384 times the I2S output clock (BCLK).
 144	 */
 145
 146	/* SSC clock / (bitrate * stereo * 16-bit). */
 147	ssc_div = ssc_rate / (BITRATE_TARGET * 2 * 16);
 148	ssc_div_min = ssc_rate / (BITRATE_MAX * 2 * 16);
 149	ssc_div_max = ssc_rate / (BITRATE_MIN * 2 * 16);
 150	max_tries = (ssc_div_max - ssc_div_min) / 2;
 151
 152	if (max_tries < 1)
 153		max_tries = 1;
 154
 155	/* ssc_div must be even. */
 156	ssc_div = (ssc_div + 1) & ~1UL;
 157
 158	if ((ssc_rate / (ssc_div * 2 * 16)) < BITRATE_MIN) {
 159		ssc_div -= 2;
 160		if ((ssc_rate / (ssc_div * 2 * 16)) > BITRATE_MAX)
 161			return -ENXIO;
 162	}
 163
 164	/* Search for a possible bitrate. */
 165	do {
 166		/* SSC clock / (ssc divider * 16-bit * stereo). */
 167		if ((ssc_rate / (ssc_div * 2 * 16)) < BITRATE_MIN)
 168			return -ENXIO;
 169
 170		/* 256 / (2 * 16) = 8 */
 171		dac_rate_new = 8 * (ssc_rate / ssc_div);
 172
 173		status = clk_round_rate(chip->board->dac_clk, dac_rate_new);
 174		if (status <= 0)
 175			return status;
 176
 177		/* Ignore difference smaller than 256 Hz. */
 178		if ((status/256) == (dac_rate_new/256))
 179			goto set_rate;
 180
 181		ssc_div += 2;
 182	} while (--max_tries);
 183
 184	/* Not able to find a valid bitrate. */
 185	return -ENXIO;
 186
 187set_rate:
 188	status = clk_set_rate(chip->board->dac_clk, status);
 189	if (status < 0)
 190		return status;
 191
 192	/* Set divider in SSC device. */
 193	ssc_writel(chip->ssc->regs, CMR, ssc_div/2);
 194
 195	/* SSC clock / (ssc divider * 16-bit * stereo). */
 196	chip->bitrate = ssc_rate / (ssc_div * 16 * 2);
 197
 198	dev_info(&chip->spi->dev,
 199			"at73c213: supported bitrate is %lu (%lu divider)\n",
 200			chip->bitrate, ssc_div);
 201
 202	return 0;
 203}
 204
 205static int snd_at73c213_pcm_open(struct snd_pcm_substream *substream)
 206{
 207	struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
 208	struct snd_pcm_runtime *runtime = substream->runtime;
 209	int err;
 210
 211	/* ensure buffer_size is a multiple of period_size */
 212	err = snd_pcm_hw_constraint_integer(runtime,
 213					SNDRV_PCM_HW_PARAM_PERIODS);
 214	if (err < 0)
 215		return err;
 216	snd_at73c213_playback_hw.rate_min = chip->bitrate;
 217	snd_at73c213_playback_hw.rate_max = chip->bitrate;
 218	runtime->hw = snd_at73c213_playback_hw;
 219	chip->substream = substream;
 220
 221	clk_enable(chip->ssc->clk);
 222
 223	return 0;
 224}
 225
 226static int snd_at73c213_pcm_close(struct snd_pcm_substream *substream)
 227{
 228	struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
 229	chip->substream = NULL;
 230	clk_disable(chip->ssc->clk);
 231	return 0;
 232}
 233
 234static int snd_at73c213_pcm_hw_params(struct snd_pcm_substream *substream,
 235				 struct snd_pcm_hw_params *hw_params)
 236{
 237	struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
 238	int channels = params_channels(hw_params);
 239	int val;
 240
 241	val = ssc_readl(chip->ssc->regs, TFMR);
 242	val = SSC_BFINS(TFMR_DATNB, channels - 1, val);
 243	ssc_writel(chip->ssc->regs, TFMR, val);
 244
 245	return snd_pcm_lib_malloc_pages(substream,
 246					params_buffer_bytes(hw_params));
 247}
 248
 249static int snd_at73c213_pcm_hw_free(struct snd_pcm_substream *substream)
 250{
 251	return snd_pcm_lib_free_pages(substream);
 252}
 253
 254static int snd_at73c213_pcm_prepare(struct snd_pcm_substream *substream)
 255{
 256	struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
 257	struct snd_pcm_runtime *runtime = substream->runtime;
 258	int block_size;
 259
 260	block_size = frames_to_bytes(runtime, runtime->period_size);
 261
 262	chip->period = 0;
 263
 264	ssc_writel(chip->ssc->regs, PDC_TPR,
 265			(long)runtime->dma_addr);
 266	ssc_writel(chip->ssc->regs, PDC_TCR,
 267			runtime->period_size * runtime->channels);
 268	ssc_writel(chip->ssc->regs, PDC_TNPR,
 269			(long)runtime->dma_addr + block_size);
 270	ssc_writel(chip->ssc->regs, PDC_TNCR,
 271			runtime->period_size * runtime->channels);
 272
 273	return 0;
 274}
 275
 276static int snd_at73c213_pcm_trigger(struct snd_pcm_substream *substream,
 277				   int cmd)
 278{
 279	struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
 280	int retval = 0;
 281
 282	spin_lock(&chip->lock);
 283
 284	switch (cmd) {
 285	case SNDRV_PCM_TRIGGER_START:
 286		ssc_writel(chip->ssc->regs, IER, SSC_BIT(IER_ENDTX));
 287		ssc_writel(chip->ssc->regs, PDC_PTCR, SSC_BIT(PDC_PTCR_TXTEN));
 288		break;
 289	case SNDRV_PCM_TRIGGER_STOP:
 290		ssc_writel(chip->ssc->regs, PDC_PTCR, SSC_BIT(PDC_PTCR_TXTDIS));
 291		ssc_writel(chip->ssc->regs, IDR, SSC_BIT(IDR_ENDTX));
 292		break;
 293	default:
 294		dev_dbg(&chip->spi->dev, "spurious command %x\n", cmd);
 295		retval = -EINVAL;
 296		break;
 297	}
 298
 299	spin_unlock(&chip->lock);
 300
 301	return retval;
 302}
 303
 304static snd_pcm_uframes_t
 305snd_at73c213_pcm_pointer(struct snd_pcm_substream *substream)
 306{
 307	struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
 308	struct snd_pcm_runtime *runtime = substream->runtime;
 309	snd_pcm_uframes_t pos;
 310	unsigned long bytes;
 311
 312	bytes = ssc_readl(chip->ssc->regs, PDC_TPR)
 313		- (unsigned long)runtime->dma_addr;
 314
 315	pos = bytes_to_frames(runtime, bytes);
 316	if (pos >= runtime->buffer_size)
 317		pos -= runtime->buffer_size;
 318
 319	return pos;
 320}
 321
 322static const struct snd_pcm_ops at73c213_playback_ops = {
 323	.open		= snd_at73c213_pcm_open,
 324	.close		= snd_at73c213_pcm_close,
 325	.ioctl		= snd_pcm_lib_ioctl,
 326	.hw_params	= snd_at73c213_pcm_hw_params,
 327	.hw_free	= snd_at73c213_pcm_hw_free,
 328	.prepare	= snd_at73c213_pcm_prepare,
 329	.trigger	= snd_at73c213_pcm_trigger,
 330	.pointer	= snd_at73c213_pcm_pointer,
 331};
 332
 333static int snd_at73c213_pcm_new(struct snd_at73c213 *chip, int device)
 334{
 335	struct snd_pcm *pcm;
 336	int retval;
 337
 338	retval = snd_pcm_new(chip->card, chip->card->shortname,
 339			device, 1, 0, &pcm);
 340	if (retval < 0)
 341		goto out;
 342
 343	pcm->private_data = chip;
 344	pcm->info_flags = SNDRV_PCM_INFO_BLOCK_TRANSFER;
 345	strcpy(pcm->name, "at73c213");
 346	chip->pcm = pcm;
 347
 348	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &at73c213_playback_ops);
 349
 350	snd_pcm_lib_preallocate_pages_for_all(chip->pcm,
 351			SNDRV_DMA_TYPE_DEV, &chip->ssc->pdev->dev,
 352			64 * 1024, 64 * 1024);
 353out:
 354	return retval;
 355}
 356
 357static irqreturn_t snd_at73c213_interrupt(int irq, void *dev_id)
 358{
 359	struct snd_at73c213 *chip = dev_id;
 360	struct snd_pcm_runtime *runtime = chip->substream->runtime;
 361	u32 status;
 362	int offset;
 363	int block_size;
 364	int next_period;
 365	int retval = IRQ_NONE;
 366
 367	spin_lock(&chip->lock);
 368
 369	block_size = frames_to_bytes(runtime, runtime->period_size);
 370	status = ssc_readl(chip->ssc->regs, IMR);
 371
 372	if (status & SSC_BIT(IMR_ENDTX)) {
 373		chip->period++;
 374		if (chip->period == runtime->periods)
 375			chip->period = 0;
 376		next_period = chip->period + 1;
 377		if (next_period == runtime->periods)
 378			next_period = 0;
 379
 380		offset = block_size * next_period;
 381
 382		ssc_writel(chip->ssc->regs, PDC_TNPR,
 383				(long)runtime->dma_addr + offset);
 384		ssc_writel(chip->ssc->regs, PDC_TNCR,
 385				runtime->period_size * runtime->channels);
 386		retval = IRQ_HANDLED;
 387	}
 388
 389	ssc_readl(chip->ssc->regs, IMR);
 390	spin_unlock(&chip->lock);
 391
 392	if (status & SSC_BIT(IMR_ENDTX))
 393		snd_pcm_period_elapsed(chip->substream);
 394
 395	return retval;
 396}
 397
 398/*
 399 * Mixer functions.
 400 */
 401static int snd_at73c213_mono_get(struct snd_kcontrol *kcontrol,
 402				 struct snd_ctl_elem_value *ucontrol)
 403{
 404	struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
 405	int reg = kcontrol->private_value & 0xff;
 406	int shift = (kcontrol->private_value >> 8) & 0xff;
 407	int mask = (kcontrol->private_value >> 16) & 0xff;
 408	int invert = (kcontrol->private_value >> 24) & 0xff;
 409
 410	mutex_lock(&chip->mixer_lock);
 411
 412	ucontrol->value.integer.value[0] =
 413		(chip->reg_image[reg] >> shift) & mask;
 414
 415	if (invert)
 416		ucontrol->value.integer.value[0] =
 417			mask - ucontrol->value.integer.value[0];
 418
 419	mutex_unlock(&chip->mixer_lock);
 420
 421	return 0;
 422}
 423
 424static int snd_at73c213_mono_put(struct snd_kcontrol *kcontrol,
 425				 struct snd_ctl_elem_value *ucontrol)
 426{
 427	struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
 428	int reg = kcontrol->private_value & 0xff;
 429	int shift = (kcontrol->private_value >> 8) & 0xff;
 430	int mask = (kcontrol->private_value >> 16) & 0xff;
 431	int invert = (kcontrol->private_value >> 24) & 0xff;
 432	int change, retval;
 433	unsigned short val;
 434
 435	val = (ucontrol->value.integer.value[0] & mask);
 436	if (invert)
 437		val = mask - val;
 438	val <<= shift;
 439
 440	mutex_lock(&chip->mixer_lock);
 441
 442	val = (chip->reg_image[reg] & ~(mask << shift)) | val;
 443	change = val != chip->reg_image[reg];
 444	retval = snd_at73c213_write_reg(chip, reg, val);
 445
 446	mutex_unlock(&chip->mixer_lock);
 447
 448	if (retval)
 449		return retval;
 450
 451	return change;
 452}
 453
 454static int snd_at73c213_stereo_info(struct snd_kcontrol *kcontrol,
 455				  struct snd_ctl_elem_info *uinfo)
 456{
 457	int mask = (kcontrol->private_value >> 24) & 0xff;
 458
 459	if (mask == 1)
 460		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
 461	else
 462		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
 463
 464	uinfo->count = 2;
 465	uinfo->value.integer.min = 0;
 466	uinfo->value.integer.max = mask;
 467
 468	return 0;
 469}
 470
 471static int snd_at73c213_stereo_get(struct snd_kcontrol *kcontrol,
 472				 struct snd_ctl_elem_value *ucontrol)
 473{
 474	struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
 475	int left_reg = kcontrol->private_value & 0xff;
 476	int right_reg = (kcontrol->private_value >> 8) & 0xff;
 477	int shift_left = (kcontrol->private_value >> 16) & 0x07;
 478	int shift_right = (kcontrol->private_value >> 19) & 0x07;
 479	int mask = (kcontrol->private_value >> 24) & 0xff;
 480	int invert = (kcontrol->private_value >> 22) & 1;
 481
 482	mutex_lock(&chip->mixer_lock);
 483
 484	ucontrol->value.integer.value[0] =
 485		(chip->reg_image[left_reg] >> shift_left) & mask;
 486	ucontrol->value.integer.value[1] =
 487		(chip->reg_image[right_reg] >> shift_right) & mask;
 488
 489	if (invert) {
 490		ucontrol->value.integer.value[0] =
 491			mask - ucontrol->value.integer.value[0];
 492		ucontrol->value.integer.value[1] =
 493			mask - ucontrol->value.integer.value[1];
 494	}
 495
 496	mutex_unlock(&chip->mixer_lock);
 497
 498	return 0;
 499}
 500
 501static int snd_at73c213_stereo_put(struct snd_kcontrol *kcontrol,
 502				 struct snd_ctl_elem_value *ucontrol)
 503{
 504	struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
 505	int left_reg = kcontrol->private_value & 0xff;
 506	int right_reg = (kcontrol->private_value >> 8) & 0xff;
 507	int shift_left = (kcontrol->private_value >> 16) & 0x07;
 508	int shift_right = (kcontrol->private_value >> 19) & 0x07;
 509	int mask = (kcontrol->private_value >> 24) & 0xff;
 510	int invert = (kcontrol->private_value >> 22) & 1;
 511	int change, retval;
 512	unsigned short val1, val2;
 513
 514	val1 = ucontrol->value.integer.value[0] & mask;
 515	val2 = ucontrol->value.integer.value[1] & mask;
 516	if (invert) {
 517		val1 = mask - val1;
 518		val2 = mask - val2;
 519	}
 520	val1 <<= shift_left;
 521	val2 <<= shift_right;
 522
 523	mutex_lock(&chip->mixer_lock);
 524
 525	val1 = (chip->reg_image[left_reg] & ~(mask << shift_left)) | val1;
 526	val2 = (chip->reg_image[right_reg] & ~(mask << shift_right)) | val2;
 527	change = val1 != chip->reg_image[left_reg]
 528		|| val2 != chip->reg_image[right_reg];
 529	retval = snd_at73c213_write_reg(chip, left_reg, val1);
 530	if (retval) {
 531		mutex_unlock(&chip->mixer_lock);
 532		goto out;
 533	}
 534	retval = snd_at73c213_write_reg(chip, right_reg, val2);
 535	if (retval) {
 536		mutex_unlock(&chip->mixer_lock);
 537		goto out;
 538	}
 539
 540	mutex_unlock(&chip->mixer_lock);
 541
 542	return change;
 543
 544out:
 545	return retval;
 546}
 547
 548#define snd_at73c213_mono_switch_info	snd_ctl_boolean_mono_info
 549
 550static int snd_at73c213_mono_switch_get(struct snd_kcontrol *kcontrol,
 551				 struct snd_ctl_elem_value *ucontrol)
 552{
 553	struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
 554	int reg = kcontrol->private_value & 0xff;
 555	int shift = (kcontrol->private_value >> 8) & 0xff;
 556	int invert = (kcontrol->private_value >> 24) & 0xff;
 557
 558	mutex_lock(&chip->mixer_lock);
 559
 560	ucontrol->value.integer.value[0] =
 561		(chip->reg_image[reg] >> shift) & 0x01;
 562
 563	if (invert)
 564		ucontrol->value.integer.value[0] =
 565			0x01 - ucontrol->value.integer.value[0];
 566
 567	mutex_unlock(&chip->mixer_lock);
 568
 569	return 0;
 570}
 571
 572static int snd_at73c213_mono_switch_put(struct snd_kcontrol *kcontrol,
 573				 struct snd_ctl_elem_value *ucontrol)
 574{
 575	struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
 576	int reg = kcontrol->private_value & 0xff;
 577	int shift = (kcontrol->private_value >> 8) & 0xff;
 578	int mask = (kcontrol->private_value >> 16) & 0xff;
 579	int invert = (kcontrol->private_value >> 24) & 0xff;
 580	int change, retval;
 581	unsigned short val;
 582
 583	if (ucontrol->value.integer.value[0])
 584		val = mask;
 585	else
 586		val = 0;
 587
 588	if (invert)
 589		val = mask - val;
 590	val <<= shift;
 591
 592	mutex_lock(&chip->mixer_lock);
 593
 594	val |= (chip->reg_image[reg] & ~(mask << shift));
 595	change = val != chip->reg_image[reg];
 596
 597	retval = snd_at73c213_write_reg(chip, reg, val);
 598
 599	mutex_unlock(&chip->mixer_lock);
 600
 601	if (retval)
 602		return retval;
 603
 604	return change;
 605}
 606
 607static int snd_at73c213_pa_volume_info(struct snd_kcontrol *kcontrol,
 608				  struct snd_ctl_elem_info *uinfo)
 609{
 610	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
 611	uinfo->count = 1;
 612	uinfo->value.integer.min = 0;
 613	uinfo->value.integer.max = ((kcontrol->private_value >> 16) & 0xff) - 1;
 614
 615	return 0;
 616}
 617
 618static int snd_at73c213_line_capture_volume_info(
 619		struct snd_kcontrol *kcontrol,
 620		struct snd_ctl_elem_info *uinfo)
 621{
 622	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
 623	uinfo->count = 2;
 624	/* When inverted will give values 0x10001 => 0. */
 625	uinfo->value.integer.min = 14;
 626	uinfo->value.integer.max = 31;
 627
 628	return 0;
 629}
 630
 631static int snd_at73c213_aux_capture_volume_info(
 632		struct snd_kcontrol *kcontrol,
 633		struct snd_ctl_elem_info *uinfo)
 634{
 635	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
 636	uinfo->count = 1;
 637	/* When inverted will give values 0x10001 => 0. */
 638	uinfo->value.integer.min = 14;
 639	uinfo->value.integer.max = 31;
 640
 641	return 0;
 642}
 643
 644#define AT73C213_MONO_SWITCH(xname, xindex, reg, shift, mask, invert)	\
 645{									\
 646	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,				\
 647	.name = xname,							\
 648	.index = xindex,						\
 649	.info = snd_at73c213_mono_switch_info,				\
 650	.get = snd_at73c213_mono_switch_get,				\
 651	.put = snd_at73c213_mono_switch_put,				\
 652	.private_value = (reg | (shift << 8) | (mask << 16) | (invert << 24)) \
 653}
 654
 655#define AT73C213_STEREO(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
 656{									\
 657	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,				\
 658	.name = xname,							\
 659	.index = xindex,						\
 660	.info = snd_at73c213_stereo_info,				\
 661	.get = snd_at73c213_stereo_get,					\
 662	.put = snd_at73c213_stereo_put,					\
 663	.private_value = (left_reg | (right_reg << 8)			\
 664			| (shift_left << 16) | (shift_right << 19)	\
 665			| (mask << 24) | (invert << 22))		\
 666}
 667
 668static struct snd_kcontrol_new snd_at73c213_controls[] = {
 669AT73C213_STEREO("Master Playback Volume", 0, DAC_LMPG, DAC_RMPG, 0, 0, 0x1f, 1),
 670AT73C213_STEREO("Master Playback Switch", 0, DAC_LMPG, DAC_RMPG, 5, 5, 1, 1),
 671AT73C213_STEREO("PCM Playback Volume", 0, DAC_LLOG, DAC_RLOG, 0, 0, 0x1f, 1),
 672AT73C213_STEREO("PCM Playback Switch", 0, DAC_LLOG, DAC_RLOG, 5, 5, 1, 1),
 673AT73C213_MONO_SWITCH("Mono PA Playback Switch", 0, DAC_CTRL, DAC_CTRL_ONPADRV,
 674		     0x01, 0),
 675{
 676	.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
 677	.name	= "PA Playback Volume",
 678	.index	= 0,
 679	.info	= snd_at73c213_pa_volume_info,
 680	.get	= snd_at73c213_mono_get,
 681	.put	= snd_at73c213_mono_put,
 682	.private_value	= PA_CTRL | (PA_CTRL_APAGAIN << 8) | \
 683		(0x0f << 16) | (1 << 24),
 684},
 685AT73C213_MONO_SWITCH("PA High Gain Playback Switch", 0, PA_CTRL, PA_CTRL_APALP,
 686		     0x01, 1),
 687AT73C213_MONO_SWITCH("PA Playback Switch", 0, PA_CTRL, PA_CTRL_APAON, 0x01, 0),
 688{
 689	.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
 690	.name	= "Aux Capture Volume",
 691	.index	= 0,
 692	.info	= snd_at73c213_aux_capture_volume_info,
 693	.get	= snd_at73c213_mono_get,
 694	.put	= snd_at73c213_mono_put,
 695	.private_value	= DAC_AUXG | (0 << 8) | (0x1f << 16) | (1 << 24),
 696},
 697AT73C213_MONO_SWITCH("Aux Capture Switch", 0, DAC_CTRL, DAC_CTRL_ONAUXIN,
 698		     0x01, 0),
 699{
 700	.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
 701	.name	= "Line Capture Volume",
 702	.index	= 0,
 703	.info	= snd_at73c213_line_capture_volume_info,
 704	.get	= snd_at73c213_stereo_get,
 705	.put	= snd_at73c213_stereo_put,
 706	.private_value	= DAC_LLIG | (DAC_RLIG << 8) | (0 << 16) | (0 << 19)
 707		| (0x1f << 24) | (1 << 22),
 708},
 709AT73C213_MONO_SWITCH("Line Capture Switch", 0, DAC_CTRL, 0, 0x03, 0),
 710};
 711
 712static int snd_at73c213_mixer(struct snd_at73c213 *chip)
 713{
 714	struct snd_card *card;
 715	int errval, idx;
 716
 717	if (chip == NULL || chip->pcm == NULL)
 718		return -EINVAL;
 719
 720	card = chip->card;
 721
 722	strcpy(card->mixername, chip->pcm->name);
 723
 724	for (idx = 0; idx < ARRAY_SIZE(snd_at73c213_controls); idx++) {
 725		errval = snd_ctl_add(card,
 726				snd_ctl_new1(&snd_at73c213_controls[idx],
 727					chip));
 728		if (errval < 0)
 729			goto cleanup;
 730	}
 731
 732	return 0;
 733
 734cleanup:
 735	for (idx = 1; idx < ARRAY_SIZE(snd_at73c213_controls) + 1; idx++) {
 736		struct snd_kcontrol *kctl;
 737		kctl = snd_ctl_find_numid(card, idx);
 738		if (kctl)
 739			snd_ctl_remove(card, kctl);
 740	}
 741	return errval;
 742}
 743
 744/*
 745 * Device functions
 746 */
 747static int snd_at73c213_ssc_init(struct snd_at73c213 *chip)
 748{
 749	/*
 750	 * Continuous clock output.
 751	 * Starts on falling TF.
 752	 * Delay 1 cycle (1 bit).
 753	 * Periode is 16 bit (16 - 1).
 754	 */
 755	ssc_writel(chip->ssc->regs, TCMR,
 756			SSC_BF(TCMR_CKO, 1)
 757			| SSC_BF(TCMR_START, 4)
 758			| SSC_BF(TCMR_STTDLY, 1)
 759			| SSC_BF(TCMR_PERIOD, 16 - 1));
 760	/*
 761	 * Data length is 16 bit (16 - 1).
 762	 * Transmit MSB first.
 763	 * Transmit 2 words each transfer.
 764	 * Frame sync length is 16 bit (16 - 1).
 765	 * Frame starts on negative pulse.
 766	 */
 767	ssc_writel(chip->ssc->regs, TFMR,
 768			SSC_BF(TFMR_DATLEN, 16 - 1)
 769			| SSC_BIT(TFMR_MSBF)
 770			| SSC_BF(TFMR_DATNB, 1)
 771			| SSC_BF(TFMR_FSLEN, 16 - 1)
 772			| SSC_BF(TFMR_FSOS, 1));
 773
 774	return 0;
 775}
 776
 777static int snd_at73c213_chip_init(struct snd_at73c213 *chip)
 778{
 779	int retval;
 780	unsigned char dac_ctrl = 0;
 781
 782	retval = snd_at73c213_set_bitrate(chip);
 783	if (retval)
 784		goto out;
 785
 786	/* Enable DAC master clock. */
 787	clk_enable(chip->board->dac_clk);
 788
 789	/* Initialize at73c213 on SPI bus. */
 790	retval = snd_at73c213_write_reg(chip, DAC_RST, 0x04);
 791	if (retval)
 792		goto out_clk;
 793	msleep(1);
 794	retval = snd_at73c213_write_reg(chip, DAC_RST, 0x03);
 795	if (retval)
 796		goto out_clk;
 797
 798	/* Precharge everything. */
 799	retval = snd_at73c213_write_reg(chip, DAC_PRECH, 0xff);
 800	if (retval)
 801		goto out_clk;
 802	retval = snd_at73c213_write_reg(chip, PA_CTRL, (1<<PA_CTRL_APAPRECH));
 803	if (retval)
 804		goto out_clk;
 805	retval = snd_at73c213_write_reg(chip, DAC_CTRL,
 806			(1<<DAC_CTRL_ONLNOL) | (1<<DAC_CTRL_ONLNOR));
 807	if (retval)
 808		goto out_clk;
 809
 810	msleep(50);
 811
 812	/* Stop precharging PA. */
 813	retval = snd_at73c213_write_reg(chip, PA_CTRL,
 814			(1<<PA_CTRL_APALP) | 0x0f);
 815	if (retval)
 816		goto out_clk;
 817
 818	msleep(450);
 819
 820	/* Stop precharging DAC, turn on master power. */
 821	retval = snd_at73c213_write_reg(chip, DAC_PRECH, (1<<DAC_PRECH_ONMSTR));
 822	if (retval)
 823		goto out_clk;
 824
 825	msleep(1);
 826
 827	/* Turn on DAC. */
 828	dac_ctrl = (1<<DAC_CTRL_ONDACL) | (1<<DAC_CTRL_ONDACR)
 829		| (1<<DAC_CTRL_ONLNOL) | (1<<DAC_CTRL_ONLNOR);
 830
 831	retval = snd_at73c213_write_reg(chip, DAC_CTRL, dac_ctrl);
 832	if (retval)
 833		goto out_clk;
 834
 835	/* Mute sound. */
 836	retval = snd_at73c213_write_reg(chip, DAC_LMPG, 0x3f);
 837	if (retval)
 838		goto out_clk;
 839	retval = snd_at73c213_write_reg(chip, DAC_RMPG, 0x3f);
 840	if (retval)
 841		goto out_clk;
 842	retval = snd_at73c213_write_reg(chip, DAC_LLOG, 0x3f);
 843	if (retval)
 844		goto out_clk;
 845	retval = snd_at73c213_write_reg(chip, DAC_RLOG, 0x3f);
 846	if (retval)
 847		goto out_clk;
 848	retval = snd_at73c213_write_reg(chip, DAC_LLIG, 0x11);
 849	if (retval)
 850		goto out_clk;
 851	retval = snd_at73c213_write_reg(chip, DAC_RLIG, 0x11);
 852	if (retval)
 853		goto out_clk;
 854	retval = snd_at73c213_write_reg(chip, DAC_AUXG, 0x11);
 855	if (retval)
 856		goto out_clk;
 857
 858	/* Enable I2S device, i.e. clock output. */
 859	ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXEN));
 860
 861	goto out;
 862
 863out_clk:
 864	clk_disable(chip->board->dac_clk);
 865out:
 866	return retval;
 867}
 868
 869static int snd_at73c213_dev_free(struct snd_device *device)
 870{
 871	struct snd_at73c213 *chip = device->device_data;
 872
 873	ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXDIS));
 874	if (chip->irq >= 0) {
 875		free_irq(chip->irq, chip);
 876		chip->irq = -1;
 877	}
 878
 879	return 0;
 880}
 881
 882static int snd_at73c213_dev_init(struct snd_card *card,
 883				 struct spi_device *spi)
 884{
 885	static struct snd_device_ops ops = {
 886		.dev_free	= snd_at73c213_dev_free,
 887	};
 888	struct snd_at73c213 *chip = get_chip(card);
 889	int irq, retval;
 890
 891	irq = chip->ssc->irq;
 892	if (irq < 0)
 893		return irq;
 894
 895	spin_lock_init(&chip->lock);
 896	mutex_init(&chip->mixer_lock);
 897	chip->card = card;
 898	chip->irq = -1;
 899
 900	clk_enable(chip->ssc->clk);
 901
 902	retval = request_irq(irq, snd_at73c213_interrupt, 0, "at73c213", chip);
 903	if (retval) {
 904		dev_dbg(&chip->spi->dev, "unable to request irq %d\n", irq);
 905		goto out;
 906	}
 907	chip->irq = irq;
 908
 909	memcpy(&chip->reg_image, &snd_at73c213_original_image,
 910			sizeof(snd_at73c213_original_image));
 911
 912	retval = snd_at73c213_ssc_init(chip);
 913	if (retval)
 914		goto out_irq;
 915
 916	retval = snd_at73c213_chip_init(chip);
 917	if (retval)
 918		goto out_irq;
 919
 920	retval = snd_at73c213_pcm_new(chip, 0);
 921	if (retval)
 922		goto out_irq;
 923
 924	retval = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
 925	if (retval)
 926		goto out_irq;
 927
 928	retval = snd_at73c213_mixer(chip);
 929	if (retval)
 930		goto out_snd_dev;
 931
 932	goto out;
 933
 934out_snd_dev:
 935	snd_device_free(card, chip);
 936out_irq:
 937	free_irq(chip->irq, chip);
 938	chip->irq = -1;
 939out:
 940	clk_disable(chip->ssc->clk);
 941
 942	return retval;
 943}
 944
 945static int snd_at73c213_probe(struct spi_device *spi)
 946{
 947	struct snd_card			*card;
 948	struct snd_at73c213		*chip;
 949	struct at73c213_board_info	*board;
 950	int				retval;
 951	char				id[16];
 952
 953	board = spi->dev.platform_data;
 954	if (!board) {
 955		dev_dbg(&spi->dev, "no platform_data\n");
 956		return -ENXIO;
 957	}
 958
 959	if (!board->dac_clk) {
 960		dev_dbg(&spi->dev, "no DAC clk\n");
 961		return -ENXIO;
 962	}
 963
 964	if (IS_ERR(board->dac_clk)) {
 965		dev_dbg(&spi->dev, "no DAC clk\n");
 966		return PTR_ERR(board->dac_clk);
 967	}
 968
 969	/* Allocate "card" using some unused identifiers. */
 970	snprintf(id, sizeof id, "at73c213_%d", board->ssc_id);
 971	retval = snd_card_new(&spi->dev, -1, id, THIS_MODULE,
 972			      sizeof(struct snd_at73c213), &card);
 973	if (retval < 0)
 974		goto out;
 975
 976	chip = card->private_data;
 977	chip->spi = spi;
 978	chip->board = board;
 979
 980	chip->ssc = ssc_request(board->ssc_id);
 981	if (IS_ERR(chip->ssc)) {
 982		dev_dbg(&spi->dev, "could not get ssc%d device\n",
 983				board->ssc_id);
 984		retval = PTR_ERR(chip->ssc);
 985		goto out_card;
 986	}
 987
 988	retval = snd_at73c213_dev_init(card, spi);
 989	if (retval)
 990		goto out_ssc;
 991
 992	strcpy(card->driver, "at73c213");
 993	strcpy(card->shortname, board->shortname);
 994	sprintf(card->longname, "%s on irq %d", card->shortname, chip->irq);
 995
 996	retval = snd_card_register(card);
 997	if (retval)
 998		goto out_ssc;
 999
1000	dev_set_drvdata(&spi->dev, card);
1001
1002	goto out;
1003
1004out_ssc:
1005	ssc_free(chip->ssc);
1006out_card:
1007	snd_card_free(card);
1008out:
1009	return retval;
1010}
1011
1012static int snd_at73c213_remove(struct spi_device *spi)
1013{
1014	struct snd_card *card = dev_get_drvdata(&spi->dev);
1015	struct snd_at73c213 *chip = card->private_data;
1016	int retval;
1017
1018	/* Stop playback. */
1019	clk_enable(chip->ssc->clk);
1020	ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXDIS));
1021	clk_disable(chip->ssc->clk);
1022
1023	/* Mute sound. */
1024	retval = snd_at73c213_write_reg(chip, DAC_LMPG, 0x3f);
1025	if (retval)
1026		goto out;
1027	retval = snd_at73c213_write_reg(chip, DAC_RMPG, 0x3f);
1028	if (retval)
1029		goto out;
1030	retval = snd_at73c213_write_reg(chip, DAC_LLOG, 0x3f);
1031	if (retval)
1032		goto out;
1033	retval = snd_at73c213_write_reg(chip, DAC_RLOG, 0x3f);
1034	if (retval)
1035		goto out;
1036	retval = snd_at73c213_write_reg(chip, DAC_LLIG, 0x11);
1037	if (retval)
1038		goto out;
1039	retval = snd_at73c213_write_reg(chip, DAC_RLIG, 0x11);
1040	if (retval)
1041		goto out;
1042	retval = snd_at73c213_write_reg(chip, DAC_AUXG, 0x11);
1043	if (retval)
1044		goto out;
1045
1046	/* Turn off PA. */
1047	retval = snd_at73c213_write_reg(chip, PA_CTRL,
1048					chip->reg_image[PA_CTRL] | 0x0f);
1049	if (retval)
1050		goto out;
1051	msleep(10);
1052	retval = snd_at73c213_write_reg(chip, PA_CTRL,
1053					(1 << PA_CTRL_APALP) | 0x0f);
1054	if (retval)
1055		goto out;
1056
1057	/* Turn off external DAC. */
1058	retval = snd_at73c213_write_reg(chip, DAC_CTRL, 0x0c);
1059	if (retval)
1060		goto out;
1061	msleep(2);
1062	retval = snd_at73c213_write_reg(chip, DAC_CTRL, 0x00);
1063	if (retval)
1064		goto out;
1065
1066	/* Turn off master power. */
1067	retval = snd_at73c213_write_reg(chip, DAC_PRECH, 0x00);
1068	if (retval)
1069		goto out;
1070
1071out:
1072	/* Stop DAC master clock. */
1073	clk_disable(chip->board->dac_clk);
1074
1075	ssc_free(chip->ssc);
1076	snd_card_free(card);
1077
1078	return 0;
1079}
1080
1081#ifdef CONFIG_PM_SLEEP
1082
1083static int snd_at73c213_suspend(struct device *dev)
1084{
1085	struct snd_card *card = dev_get_drvdata(dev);
1086	struct snd_at73c213 *chip = card->private_data;
1087
1088	ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXDIS));
1089	clk_disable(chip->ssc->clk);
1090	clk_disable(chip->board->dac_clk);
1091
1092	return 0;
1093}
1094
1095static int snd_at73c213_resume(struct device *dev)
1096{
1097	struct snd_card *card = dev_get_drvdata(dev);
1098	struct snd_at73c213 *chip = card->private_data;
1099
1100	clk_enable(chip->board->dac_clk);
1101	clk_enable(chip->ssc->clk);
1102	ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXEN));
1103
1104	return 0;
1105}
1106
1107static SIMPLE_DEV_PM_OPS(at73c213_pm_ops, snd_at73c213_suspend,
1108		snd_at73c213_resume);
1109#define AT73C213_PM_OPS (&at73c213_pm_ops)
1110
1111#else
1112#define AT73C213_PM_OPS NULL
1113#endif
1114
1115static struct spi_driver at73c213_driver = {
1116	.driver		= {
1117		.name	= "at73c213",
1118		.pm	= AT73C213_PM_OPS,
1119	},
1120	.probe		= snd_at73c213_probe,
1121	.remove		= snd_at73c213_remove,
1122};
1123
1124module_spi_driver(at73c213_driver);
1125
1126MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>");
1127MODULE_DESCRIPTION("Sound driver for AT73C213 with Atmel SSC");
1128MODULE_LICENSE("GPL");