at91: at91sam9g45 family: identify several chip versions
[linux-2.6/next.git] / sound / spi / at73c213.c
blob4c7b051f9d17705bfc9827113e4fc8f74792e7c7
1 /*
2 * Driver for AT73C213 16-bit stereo DAC connected to Atmel SSC
4 * Copyright (C) 2006-2007 Atmel Norway
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 */
11 /*#define DEBUG*/
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>
25 #include <sound/initval.h>
26 #include <sound/control.h>
27 #include <sound/core.h>
28 #include <sound/pcm.h>
30 #include <linux/atmel-ssc.h>
32 #include <linux/spi/spi.h>
33 #include <linux/spi/at73c213.h>
35 #include "at73c213.h"
37 #define BITRATE_MIN 8000 /* Hardware limit? */
38 #define BITRATE_TARGET CONFIG_SND_AT73C213_TARGET_BITRATE
39 #define BITRATE_MAX 50000 /* Hardware limit. */
41 /* Initial (hardware reset) AT73C213 register values. */
42 static u8 snd_at73c213_original_image[18] =
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 */
64 struct 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 clk *bitclk;
73 struct ssc_device *ssc;
74 struct spi_device *spi;
75 u8 spi_wbuffer[2];
76 u8 spi_rbuffer[2];
77 /* Image of the SPI registers in AT73C213. */
78 u8 reg_image[18];
79 /* Protect SSC registers against concurrent access. */
80 spinlock_t lock;
81 /* Protect mixer registers against concurrent access. */
82 struct mutex mixer_lock;
85 #define get_chip(card) ((struct snd_at73c213 *)card->private_data)
87 static int
88 snd_at73c213_write_reg(struct snd_at73c213 *chip, u8 reg, u8 val)
90 struct spi_message msg;
91 struct spi_transfer msg_xfer = {
92 .len = 2,
93 .cs_change = 0,
95 int retval;
97 spi_message_init(&msg);
99 chip->spi_wbuffer[0] = reg;
100 chip->spi_wbuffer[1] = val;
102 msg_xfer.tx_buf = chip->spi_wbuffer;
103 msg_xfer.rx_buf = chip->spi_rbuffer;
104 spi_message_add_tail(&msg_xfer, &msg);
106 retval = spi_sync(chip->spi, &msg);
108 if (!retval)
109 chip->reg_image[reg] = val;
111 return retval;
114 static struct snd_pcm_hardware snd_at73c213_playback_hw = {
115 .info = SNDRV_PCM_INFO_INTERLEAVED |
116 SNDRV_PCM_INFO_BLOCK_TRANSFER,
117 .formats = SNDRV_PCM_FMTBIT_S16_BE,
118 .rates = SNDRV_PCM_RATE_CONTINUOUS,
119 .rate_min = 8000, /* Replaced by chip->bitrate later. */
120 .rate_max = 50000, /* Replaced by chip->bitrate later. */
121 .channels_min = 1,
122 .channels_max = 2,
123 .buffer_bytes_max = 64 * 1024 - 1,
124 .period_bytes_min = 512,
125 .period_bytes_max = 64 * 1024 - 1,
126 .periods_min = 4,
127 .periods_max = 1024,
131 * Calculate and set bitrate and divisions.
133 static int snd_at73c213_set_bitrate(struct snd_at73c213 *chip)
135 unsigned long ssc_rate = clk_get_rate(chip->ssc->clk);
136 unsigned long dac_rate_new, ssc_div;
137 int status;
138 unsigned long ssc_div_max, ssc_div_min;
139 int max_tries;
142 * We connect two clocks here, picking divisors so the I2S clocks
143 * out data at the same rate the DAC clocks it in ... and as close
144 * as practical to the desired target rate.
146 * The DAC master clock (MCLK) is programmable, and is either 256
147 * or (not here) 384 times the I2S output clock (BCLK).
150 /* SSC clock / (bitrate * stereo * 16-bit). */
151 ssc_div = ssc_rate / (BITRATE_TARGET * 2 * 16);
152 ssc_div_min = ssc_rate / (BITRATE_MAX * 2 * 16);
153 ssc_div_max = ssc_rate / (BITRATE_MIN * 2 * 16);
154 max_tries = (ssc_div_max - ssc_div_min) / 2;
156 if (max_tries < 1)
157 max_tries = 1;
159 /* ssc_div must be a power of 2. */
160 ssc_div = (ssc_div + 1) & ~1UL;
162 if ((ssc_rate / (ssc_div * 2 * 16)) < BITRATE_MIN) {
163 ssc_div -= 2;
164 if ((ssc_rate / (ssc_div * 2 * 16)) > BITRATE_MAX)
165 return -ENXIO;
168 /* Search for a possible bitrate. */
169 do {
170 /* SSC clock / (ssc divider * 16-bit * stereo). */
171 if ((ssc_rate / (ssc_div * 2 * 16)) < BITRATE_MIN)
172 return -ENXIO;
174 /* 256 / (2 * 16) = 8 */
175 dac_rate_new = 8 * (ssc_rate / ssc_div);
177 status = clk_round_rate(chip->board->dac_clk, dac_rate_new);
178 if (status < 0)
179 return status;
181 /* Ignore difference smaller than 256 Hz. */
182 if ((status/256) == (dac_rate_new/256))
183 goto set_rate;
185 ssc_div += 2;
186 } while (--max_tries);
188 /* Not able to find a valid bitrate. */
189 return -ENXIO;
191 set_rate:
192 status = clk_set_rate(chip->board->dac_clk, status);
193 if (status < 0)
194 return status;
196 /* Set divider in SSC device. */
197 ssc_writel(chip->ssc->regs, CMR, ssc_div/2);
199 /* SSC clock / (ssc divider * 16-bit * stereo). */
200 chip->bitrate = ssc_rate / (ssc_div * 16 * 2);
202 dev_info(&chip->spi->dev,
203 "at73c213: supported bitrate is %lu (%lu divider)\n",
204 chip->bitrate, ssc_div);
206 return 0;
209 static int snd_at73c213_pcm_open(struct snd_pcm_substream *substream)
211 struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
212 struct snd_pcm_runtime *runtime = substream->runtime;
213 int err;
215 /* ensure buffer_size is a multiple of period_size */
216 err = snd_pcm_hw_constraint_integer(runtime,
217 SNDRV_PCM_HW_PARAM_PERIODS);
218 if (err < 0)
219 return err;
220 snd_at73c213_playback_hw.rate_min = chip->bitrate;
221 snd_at73c213_playback_hw.rate_max = chip->bitrate;
222 runtime->hw = snd_at73c213_playback_hw;
223 chip->substream = substream;
225 return 0;
228 static int snd_at73c213_pcm_close(struct snd_pcm_substream *substream)
230 struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
231 chip->substream = NULL;
232 return 0;
235 static int snd_at73c213_pcm_hw_params(struct snd_pcm_substream *substream,
236 struct snd_pcm_hw_params *hw_params)
238 struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
239 int channels = params_channels(hw_params);
240 int val;
242 val = ssc_readl(chip->ssc->regs, TFMR);
243 val = SSC_BFINS(TFMR_DATNB, channels - 1, val);
244 ssc_writel(chip->ssc->regs, TFMR, val);
246 return snd_pcm_lib_malloc_pages(substream,
247 params_buffer_bytes(hw_params));
250 static int snd_at73c213_pcm_hw_free(struct snd_pcm_substream *substream)
252 return snd_pcm_lib_free_pages(substream);
255 static int snd_at73c213_pcm_prepare(struct snd_pcm_substream *substream)
257 struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
258 struct snd_pcm_runtime *runtime = substream->runtime;
259 int block_size;
261 block_size = frames_to_bytes(runtime, runtime->period_size);
263 chip->period = 0;
265 ssc_writel(chip->ssc->regs, PDC_TPR,
266 (long)runtime->dma_addr);
267 ssc_writel(chip->ssc->regs, PDC_TCR,
268 runtime->period_size * runtime->channels);
269 ssc_writel(chip->ssc->regs, PDC_TNPR,
270 (long)runtime->dma_addr + block_size);
271 ssc_writel(chip->ssc->regs, PDC_TNCR,
272 runtime->period_size * runtime->channels);
274 return 0;
277 static int snd_at73c213_pcm_trigger(struct snd_pcm_substream *substream,
278 int cmd)
280 struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
281 int retval = 0;
283 spin_lock(&chip->lock);
285 switch (cmd) {
286 case SNDRV_PCM_TRIGGER_START:
287 ssc_writel(chip->ssc->regs, IER, SSC_BIT(IER_ENDTX));
288 ssc_writel(chip->ssc->regs, PDC_PTCR, SSC_BIT(PDC_PTCR_TXTEN));
289 break;
290 case SNDRV_PCM_TRIGGER_STOP:
291 ssc_writel(chip->ssc->regs, PDC_PTCR, SSC_BIT(PDC_PTCR_TXTDIS));
292 ssc_writel(chip->ssc->regs, IDR, SSC_BIT(IDR_ENDTX));
293 break;
294 default:
295 dev_dbg(&chip->spi->dev, "spurious command %x\n", cmd);
296 retval = -EINVAL;
297 break;
300 spin_unlock(&chip->lock);
302 return retval;
305 static snd_pcm_uframes_t
306 snd_at73c213_pcm_pointer(struct snd_pcm_substream *substream)
308 struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
309 struct snd_pcm_runtime *runtime = substream->runtime;
310 snd_pcm_uframes_t pos;
311 unsigned long bytes;
313 bytes = ssc_readl(chip->ssc->regs, PDC_TPR)
314 - (unsigned long)runtime->dma_addr;
316 pos = bytes_to_frames(runtime, bytes);
317 if (pos >= runtime->buffer_size)
318 pos -= runtime->buffer_size;
320 return pos;
323 static struct snd_pcm_ops at73c213_playback_ops = {
324 .open = snd_at73c213_pcm_open,
325 .close = snd_at73c213_pcm_close,
326 .ioctl = snd_pcm_lib_ioctl,
327 .hw_params = snd_at73c213_pcm_hw_params,
328 .hw_free = snd_at73c213_pcm_hw_free,
329 .prepare = snd_at73c213_pcm_prepare,
330 .trigger = snd_at73c213_pcm_trigger,
331 .pointer = snd_at73c213_pcm_pointer,
334 static int __devinit snd_at73c213_pcm_new(struct snd_at73c213 *chip, int device)
336 struct snd_pcm *pcm;
337 int retval;
339 retval = snd_pcm_new(chip->card, chip->card->shortname,
340 device, 1, 0, &pcm);
341 if (retval < 0)
342 goto out;
344 pcm->private_data = chip;
345 pcm->info_flags = SNDRV_PCM_INFO_BLOCK_TRANSFER;
346 strcpy(pcm->name, "at73c213");
347 chip->pcm = pcm;
349 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &at73c213_playback_ops);
351 retval = snd_pcm_lib_preallocate_pages_for_all(chip->pcm,
352 SNDRV_DMA_TYPE_DEV, &chip->ssc->pdev->dev,
353 64 * 1024, 64 * 1024);
354 out:
355 return retval;
358 static irqreturn_t snd_at73c213_interrupt(int irq, void *dev_id)
360 struct snd_at73c213 *chip = dev_id;
361 struct snd_pcm_runtime *runtime = chip->substream->runtime;
362 u32 status;
363 int offset;
364 int block_size;
365 int next_period;
366 int retval = IRQ_NONE;
368 spin_lock(&chip->lock);
370 block_size = frames_to_bytes(runtime, runtime->period_size);
371 status = ssc_readl(chip->ssc->regs, IMR);
373 if (status & SSC_BIT(IMR_ENDTX)) {
374 chip->period++;
375 if (chip->period == runtime->periods)
376 chip->period = 0;
377 next_period = chip->period + 1;
378 if (next_period == runtime->periods)
379 next_period = 0;
381 offset = block_size * next_period;
383 ssc_writel(chip->ssc->regs, PDC_TNPR,
384 (long)runtime->dma_addr + offset);
385 ssc_writel(chip->ssc->regs, PDC_TNCR,
386 runtime->period_size * runtime->channels);
387 retval = IRQ_HANDLED;
390 ssc_readl(chip->ssc->regs, IMR);
391 spin_unlock(&chip->lock);
393 if (status & SSC_BIT(IMR_ENDTX))
394 snd_pcm_period_elapsed(chip->substream);
396 return retval;
400 * Mixer functions.
402 static int snd_at73c213_mono_get(struct snd_kcontrol *kcontrol,
403 struct snd_ctl_elem_value *ucontrol)
405 struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
406 int reg = kcontrol->private_value & 0xff;
407 int shift = (kcontrol->private_value >> 8) & 0xff;
408 int mask = (kcontrol->private_value >> 16) & 0xff;
409 int invert = (kcontrol->private_value >> 24) & 0xff;
411 mutex_lock(&chip->mixer_lock);
413 ucontrol->value.integer.value[0] =
414 (chip->reg_image[reg] >> shift) & mask;
416 if (invert)
417 ucontrol->value.integer.value[0] =
418 mask - ucontrol->value.integer.value[0];
420 mutex_unlock(&chip->mixer_lock);
422 return 0;
425 static int snd_at73c213_mono_put(struct snd_kcontrol *kcontrol,
426 struct snd_ctl_elem_value *ucontrol)
428 struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
429 int reg = kcontrol->private_value & 0xff;
430 int shift = (kcontrol->private_value >> 8) & 0xff;
431 int mask = (kcontrol->private_value >> 16) & 0xff;
432 int invert = (kcontrol->private_value >> 24) & 0xff;
433 int change, retval;
434 unsigned short val;
436 val = (ucontrol->value.integer.value[0] & mask);
437 if (invert)
438 val = mask - val;
439 val <<= shift;
441 mutex_lock(&chip->mixer_lock);
443 val = (chip->reg_image[reg] & ~(mask << shift)) | val;
444 change = val != chip->reg_image[reg];
445 retval = snd_at73c213_write_reg(chip, reg, val);
447 mutex_unlock(&chip->mixer_lock);
449 if (retval)
450 return retval;
452 return change;
455 static int snd_at73c213_stereo_info(struct snd_kcontrol *kcontrol,
456 struct snd_ctl_elem_info *uinfo)
458 int mask = (kcontrol->private_value >> 24) & 0xff;
460 if (mask == 1)
461 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
462 else
463 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
465 uinfo->count = 2;
466 uinfo->value.integer.min = 0;
467 uinfo->value.integer.max = mask;
469 return 0;
472 static int snd_at73c213_stereo_get(struct snd_kcontrol *kcontrol,
473 struct snd_ctl_elem_value *ucontrol)
475 struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
476 int left_reg = kcontrol->private_value & 0xff;
477 int right_reg = (kcontrol->private_value >> 8) & 0xff;
478 int shift_left = (kcontrol->private_value >> 16) & 0x07;
479 int shift_right = (kcontrol->private_value >> 19) & 0x07;
480 int mask = (kcontrol->private_value >> 24) & 0xff;
481 int invert = (kcontrol->private_value >> 22) & 1;
483 mutex_lock(&chip->mixer_lock);
485 ucontrol->value.integer.value[0] =
486 (chip->reg_image[left_reg] >> shift_left) & mask;
487 ucontrol->value.integer.value[1] =
488 (chip->reg_image[right_reg] >> shift_right) & mask;
490 if (invert) {
491 ucontrol->value.integer.value[0] =
492 mask - ucontrol->value.integer.value[0];
493 ucontrol->value.integer.value[1] =
494 mask - ucontrol->value.integer.value[1];
497 mutex_unlock(&chip->mixer_lock);
499 return 0;
502 static int snd_at73c213_stereo_put(struct snd_kcontrol *kcontrol,
503 struct snd_ctl_elem_value *ucontrol)
505 struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
506 int left_reg = kcontrol->private_value & 0xff;
507 int right_reg = (kcontrol->private_value >> 8) & 0xff;
508 int shift_left = (kcontrol->private_value >> 16) & 0x07;
509 int shift_right = (kcontrol->private_value >> 19) & 0x07;
510 int mask = (kcontrol->private_value >> 24) & 0xff;
511 int invert = (kcontrol->private_value >> 22) & 1;
512 int change, retval;
513 unsigned short val1, val2;
515 val1 = ucontrol->value.integer.value[0] & mask;
516 val2 = ucontrol->value.integer.value[1] & mask;
517 if (invert) {
518 val1 = mask - val1;
519 val2 = mask - val2;
521 val1 <<= shift_left;
522 val2 <<= shift_right;
524 mutex_lock(&chip->mixer_lock);
526 val1 = (chip->reg_image[left_reg] & ~(mask << shift_left)) | val1;
527 val2 = (chip->reg_image[right_reg] & ~(mask << shift_right)) | val2;
528 change = val1 != chip->reg_image[left_reg]
529 || val2 != chip->reg_image[right_reg];
530 retval = snd_at73c213_write_reg(chip, left_reg, val1);
531 if (retval) {
532 mutex_unlock(&chip->mixer_lock);
533 goto out;
535 retval = snd_at73c213_write_reg(chip, right_reg, val2);
536 if (retval) {
537 mutex_unlock(&chip->mixer_lock);
538 goto out;
541 mutex_unlock(&chip->mixer_lock);
543 return change;
545 out:
546 return retval;
549 #define snd_at73c213_mono_switch_info snd_ctl_boolean_mono_info
551 static int snd_at73c213_mono_switch_get(struct snd_kcontrol *kcontrol,
552 struct snd_ctl_elem_value *ucontrol)
554 struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
555 int reg = kcontrol->private_value & 0xff;
556 int shift = (kcontrol->private_value >> 8) & 0xff;
557 int invert = (kcontrol->private_value >> 24) & 0xff;
559 mutex_lock(&chip->mixer_lock);
561 ucontrol->value.integer.value[0] =
562 (chip->reg_image[reg] >> shift) & 0x01;
564 if (invert)
565 ucontrol->value.integer.value[0] =
566 0x01 - ucontrol->value.integer.value[0];
568 mutex_unlock(&chip->mixer_lock);
570 return 0;
573 static int snd_at73c213_mono_switch_put(struct snd_kcontrol *kcontrol,
574 struct snd_ctl_elem_value *ucontrol)
576 struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
577 int reg = kcontrol->private_value & 0xff;
578 int shift = (kcontrol->private_value >> 8) & 0xff;
579 int mask = (kcontrol->private_value >> 16) & 0xff;
580 int invert = (kcontrol->private_value >> 24) & 0xff;
581 int change, retval;
582 unsigned short val;
584 if (ucontrol->value.integer.value[0])
585 val = mask;
586 else
587 val = 0;
589 if (invert)
590 val = mask - val;
591 val <<= shift;
593 mutex_lock(&chip->mixer_lock);
595 val |= (chip->reg_image[reg] & ~(mask << shift));
596 change = val != chip->reg_image[reg];
598 retval = snd_at73c213_write_reg(chip, reg, val);
600 mutex_unlock(&chip->mixer_lock);
602 if (retval)
603 return retval;
605 return change;
608 static int snd_at73c213_pa_volume_info(struct snd_kcontrol *kcontrol,
609 struct snd_ctl_elem_info *uinfo)
611 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
612 uinfo->count = 1;
613 uinfo->value.integer.min = 0;
614 uinfo->value.integer.max = ((kcontrol->private_value >> 16) & 0xff) - 1;
616 return 0;
619 static int snd_at73c213_line_capture_volume_info(
620 struct snd_kcontrol *kcontrol,
621 struct snd_ctl_elem_info *uinfo)
623 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
624 uinfo->count = 2;
625 /* When inverted will give values 0x10001 => 0. */
626 uinfo->value.integer.min = 14;
627 uinfo->value.integer.max = 31;
629 return 0;
632 static int snd_at73c213_aux_capture_volume_info(
633 struct snd_kcontrol *kcontrol,
634 struct snd_ctl_elem_info *uinfo)
636 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
637 uinfo->count = 1;
638 /* When inverted will give values 0x10001 => 0. */
639 uinfo->value.integer.min = 14;
640 uinfo->value.integer.max = 31;
642 return 0;
645 #define AT73C213_MONO_SWITCH(xname, xindex, reg, shift, mask, invert) \
647 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
648 .name = xname, \
649 .index = xindex, \
650 .info = snd_at73c213_mono_switch_info, \
651 .get = snd_at73c213_mono_switch_get, \
652 .put = snd_at73c213_mono_switch_put, \
653 .private_value = (reg | (shift << 8) | (mask << 16) | (invert << 24)) \
656 #define AT73C213_STEREO(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
658 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
659 .name = xname, \
660 .index = xindex, \
661 .info = snd_at73c213_stereo_info, \
662 .get = snd_at73c213_stereo_get, \
663 .put = snd_at73c213_stereo_put, \
664 .private_value = (left_reg | (right_reg << 8) \
665 | (shift_left << 16) | (shift_right << 19) \
666 | (mask << 24) | (invert << 22)) \
669 static struct snd_kcontrol_new snd_at73c213_controls[] __devinitdata = {
670 AT73C213_STEREO("Master Playback Volume", 0, DAC_LMPG, DAC_RMPG, 0, 0, 0x1f, 1),
671 AT73C213_STEREO("Master Playback Switch", 0, DAC_LMPG, DAC_RMPG, 5, 5, 1, 1),
672 AT73C213_STEREO("PCM Playback Volume", 0, DAC_LLOG, DAC_RLOG, 0, 0, 0x1f, 1),
673 AT73C213_STEREO("PCM Playback Switch", 0, DAC_LLOG, DAC_RLOG, 5, 5, 1, 1),
674 AT73C213_MONO_SWITCH("Mono PA Playback Switch", 0, DAC_CTRL, DAC_CTRL_ONPADRV,
675 0x01, 0),
677 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
678 .name = "PA Playback Volume",
679 .index = 0,
680 .info = snd_at73c213_pa_volume_info,
681 .get = snd_at73c213_mono_get,
682 .put = snd_at73c213_mono_put,
683 .private_value = PA_CTRL | (PA_CTRL_APAGAIN << 8) | \
684 (0x0f << 16) | (1 << 24),
686 AT73C213_MONO_SWITCH("PA High Gain Playback Switch", 0, PA_CTRL, PA_CTRL_APALP,
687 0x01, 1),
688 AT73C213_MONO_SWITCH("PA Playback Switch", 0, PA_CTRL, PA_CTRL_APAON, 0x01, 0),
690 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
691 .name = "Aux Capture Volume",
692 .index = 0,
693 .info = snd_at73c213_aux_capture_volume_info,
694 .get = snd_at73c213_mono_get,
695 .put = snd_at73c213_mono_put,
696 .private_value = DAC_AUXG | (0 << 8) | (0x1f << 16) | (1 << 24),
698 AT73C213_MONO_SWITCH("Aux Capture Switch", 0, DAC_CTRL, DAC_CTRL_ONAUXIN,
699 0x01, 0),
701 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
702 .name = "Line Capture Volume",
703 .index = 0,
704 .info = snd_at73c213_line_capture_volume_info,
705 .get = snd_at73c213_stereo_get,
706 .put = snd_at73c213_stereo_put,
707 .private_value = DAC_LLIG | (DAC_RLIG << 8) | (0 << 16) | (0 << 19)
708 | (0x1f << 24) | (1 << 22),
710 AT73C213_MONO_SWITCH("Line Capture Switch", 0, DAC_CTRL, 0, 0x03, 0),
713 static int __devinit snd_at73c213_mixer(struct snd_at73c213 *chip)
715 struct snd_card *card;
716 int errval, idx;
718 if (chip == NULL || chip->pcm == NULL)
719 return -EINVAL;
721 card = chip->card;
723 strcpy(card->mixername, chip->pcm->name);
725 for (idx = 0; idx < ARRAY_SIZE(snd_at73c213_controls); idx++) {
726 errval = snd_ctl_add(card,
727 snd_ctl_new1(&snd_at73c213_controls[idx],
728 chip));
729 if (errval < 0)
730 goto cleanup;
733 return 0;
735 cleanup:
736 for (idx = 1; idx < ARRAY_SIZE(snd_at73c213_controls) + 1; idx++) {
737 struct snd_kcontrol *kctl;
738 kctl = snd_ctl_find_numid(card, idx);
739 if (kctl)
740 snd_ctl_remove(card, kctl);
742 return errval;
746 * Device functions
748 static int __devinit snd_at73c213_ssc_init(struct snd_at73c213 *chip)
751 * Continuous clock output.
752 * Starts on falling TF.
753 * Delay 1 cycle (1 bit).
754 * Periode is 16 bit (16 - 1).
756 ssc_writel(chip->ssc->regs, TCMR,
757 SSC_BF(TCMR_CKO, 1)
758 | SSC_BF(TCMR_START, 4)
759 | SSC_BF(TCMR_STTDLY, 1)
760 | SSC_BF(TCMR_PERIOD, 16 - 1));
762 * Data length is 16 bit (16 - 1).
763 * Transmit MSB first.
764 * Transmit 2 words each transfer.
765 * Frame sync length is 16 bit (16 - 1).
766 * Frame starts on negative pulse.
768 ssc_writel(chip->ssc->regs, TFMR,
769 SSC_BF(TFMR_DATLEN, 16 - 1)
770 | SSC_BIT(TFMR_MSBF)
771 | SSC_BF(TFMR_DATNB, 1)
772 | SSC_BF(TFMR_FSLEN, 16 - 1)
773 | SSC_BF(TFMR_FSOS, 1));
775 return 0;
778 static int __devinit snd_at73c213_chip_init(struct snd_at73c213 *chip)
780 int retval;
781 unsigned char dac_ctrl = 0;
783 retval = snd_at73c213_set_bitrate(chip);
784 if (retval)
785 goto out;
787 /* Enable DAC master clock. */
788 clk_enable(chip->board->dac_clk);
790 /* Initialize at73c213 on SPI bus. */
791 retval = snd_at73c213_write_reg(chip, DAC_RST, 0x04);
792 if (retval)
793 goto out_clk;
794 msleep(1);
795 retval = snd_at73c213_write_reg(chip, DAC_RST, 0x03);
796 if (retval)
797 goto out_clk;
799 /* Precharge everything. */
800 retval = snd_at73c213_write_reg(chip, DAC_PRECH, 0xff);
801 if (retval)
802 goto out_clk;
803 retval = snd_at73c213_write_reg(chip, PA_CTRL, (1<<PA_CTRL_APAPRECH));
804 if (retval)
805 goto out_clk;
806 retval = snd_at73c213_write_reg(chip, DAC_CTRL,
807 (1<<DAC_CTRL_ONLNOL) | (1<<DAC_CTRL_ONLNOR));
808 if (retval)
809 goto out_clk;
811 msleep(50);
813 /* Stop precharging PA. */
814 retval = snd_at73c213_write_reg(chip, PA_CTRL,
815 (1<<PA_CTRL_APALP) | 0x0f);
816 if (retval)
817 goto out_clk;
819 msleep(450);
821 /* Stop precharging DAC, turn on master power. */
822 retval = snd_at73c213_write_reg(chip, DAC_PRECH, (1<<DAC_PRECH_ONMSTR));
823 if (retval)
824 goto out_clk;
826 msleep(1);
828 /* Turn on DAC. */
829 dac_ctrl = (1<<DAC_CTRL_ONDACL) | (1<<DAC_CTRL_ONDACR)
830 | (1<<DAC_CTRL_ONLNOL) | (1<<DAC_CTRL_ONLNOR);
832 retval = snd_at73c213_write_reg(chip, DAC_CTRL, dac_ctrl);
833 if (retval)
834 goto out_clk;
836 /* Mute sound. */
837 retval = snd_at73c213_write_reg(chip, DAC_LMPG, 0x3f);
838 if (retval)
839 goto out_clk;
840 retval = snd_at73c213_write_reg(chip, DAC_RMPG, 0x3f);
841 if (retval)
842 goto out_clk;
843 retval = snd_at73c213_write_reg(chip, DAC_LLOG, 0x3f);
844 if (retval)
845 goto out_clk;
846 retval = snd_at73c213_write_reg(chip, DAC_RLOG, 0x3f);
847 if (retval)
848 goto out_clk;
849 retval = snd_at73c213_write_reg(chip, DAC_LLIG, 0x11);
850 if (retval)
851 goto out_clk;
852 retval = snd_at73c213_write_reg(chip, DAC_RLIG, 0x11);
853 if (retval)
854 goto out_clk;
855 retval = snd_at73c213_write_reg(chip, DAC_AUXG, 0x11);
856 if (retval)
857 goto out_clk;
859 /* Enable I2S device, i.e. clock output. */
860 ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXEN));
862 goto out;
864 out_clk:
865 clk_disable(chip->board->dac_clk);
866 out:
867 return retval;
870 static int snd_at73c213_dev_free(struct snd_device *device)
872 struct snd_at73c213 *chip = device->device_data;
874 ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXDIS));
875 if (chip->irq >= 0) {
876 free_irq(chip->irq, chip);
877 chip->irq = -1;
880 return 0;
883 static int __devinit snd_at73c213_dev_init(struct snd_card *card,
884 struct spi_device *spi)
886 static struct snd_device_ops ops = {
887 .dev_free = snd_at73c213_dev_free,
889 struct snd_at73c213 *chip = get_chip(card);
890 int irq, retval;
892 irq = chip->ssc->irq;
893 if (irq < 0)
894 return irq;
896 spin_lock_init(&chip->lock);
897 mutex_init(&chip->mixer_lock);
898 chip->card = card;
899 chip->irq = -1;
901 retval = request_irq(irq, snd_at73c213_interrupt, 0, "at73c213", chip);
902 if (retval) {
903 dev_dbg(&chip->spi->dev, "unable to request irq %d\n", irq);
904 goto out;
906 chip->irq = irq;
908 memcpy(&chip->reg_image, &snd_at73c213_original_image,
909 sizeof(snd_at73c213_original_image));
911 retval = snd_at73c213_ssc_init(chip);
912 if (retval)
913 goto out_irq;
915 retval = snd_at73c213_chip_init(chip);
916 if (retval)
917 goto out_irq;
919 retval = snd_at73c213_pcm_new(chip, 0);
920 if (retval)
921 goto out_irq;
923 retval = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
924 if (retval)
925 goto out_irq;
927 retval = snd_at73c213_mixer(chip);
928 if (retval)
929 goto out_snd_dev;
931 snd_card_set_dev(card, &spi->dev);
933 goto out;
935 out_snd_dev:
936 snd_device_free(card, chip);
937 out_irq:
938 free_irq(chip->irq, chip);
939 chip->irq = -1;
940 out:
941 return retval;
944 static int __devinit snd_at73c213_probe(struct spi_device *spi)
946 struct snd_card *card;
947 struct snd_at73c213 *chip;
948 struct at73c213_board_info *board;
949 int retval;
950 char id[16];
952 board = spi->dev.platform_data;
953 if (!board) {
954 dev_dbg(&spi->dev, "no platform_data\n");
955 return -ENXIO;
958 if (!board->dac_clk) {
959 dev_dbg(&spi->dev, "no DAC clk\n");
960 return -ENXIO;
963 if (IS_ERR(board->dac_clk)) {
964 dev_dbg(&spi->dev, "no DAC clk\n");
965 return PTR_ERR(board->dac_clk);
968 /* Allocate "card" using some unused identifiers. */
969 snprintf(id, sizeof id, "at73c213_%d", board->ssc_id);
970 retval = snd_card_create(-1, id, THIS_MODULE,
971 sizeof(struct snd_at73c213), &card);
972 if (retval < 0)
973 goto out;
975 chip = card->private_data;
976 chip->spi = spi;
977 chip->board = board;
979 chip->ssc = ssc_request(board->ssc_id);
980 if (IS_ERR(chip->ssc)) {
981 dev_dbg(&spi->dev, "could not get ssc%d device\n",
982 board->ssc_id);
983 retval = PTR_ERR(chip->ssc);
984 goto out_card;
987 retval = snd_at73c213_dev_init(card, spi);
988 if (retval)
989 goto out_ssc;
991 strcpy(card->driver, "at73c213");
992 strcpy(card->shortname, board->shortname);
993 sprintf(card->longname, "%s on irq %d", card->shortname, chip->irq);
995 retval = snd_card_register(card);
996 if (retval)
997 goto out_ssc;
999 dev_set_drvdata(&spi->dev, card);
1001 goto out;
1003 out_ssc:
1004 ssc_free(chip->ssc);
1005 out_card:
1006 snd_card_free(card);
1007 out:
1008 return retval;
1011 static int __devexit snd_at73c213_remove(struct spi_device *spi)
1013 struct snd_card *card = dev_get_drvdata(&spi->dev);
1014 struct snd_at73c213 *chip = card->private_data;
1015 int retval;
1017 /* Stop playback. */
1018 ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXDIS));
1020 /* Mute sound. */
1021 retval = snd_at73c213_write_reg(chip, DAC_LMPG, 0x3f);
1022 if (retval)
1023 goto out;
1024 retval = snd_at73c213_write_reg(chip, DAC_RMPG, 0x3f);
1025 if (retval)
1026 goto out;
1027 retval = snd_at73c213_write_reg(chip, DAC_LLOG, 0x3f);
1028 if (retval)
1029 goto out;
1030 retval = snd_at73c213_write_reg(chip, DAC_RLOG, 0x3f);
1031 if (retval)
1032 goto out;
1033 retval = snd_at73c213_write_reg(chip, DAC_LLIG, 0x11);
1034 if (retval)
1035 goto out;
1036 retval = snd_at73c213_write_reg(chip, DAC_RLIG, 0x11);
1037 if (retval)
1038 goto out;
1039 retval = snd_at73c213_write_reg(chip, DAC_AUXG, 0x11);
1040 if (retval)
1041 goto out;
1043 /* Turn off PA. */
1044 retval = snd_at73c213_write_reg(chip, PA_CTRL,
1045 chip->reg_image[PA_CTRL] | 0x0f);
1046 if (retval)
1047 goto out;
1048 msleep(10);
1049 retval = snd_at73c213_write_reg(chip, PA_CTRL,
1050 (1 << PA_CTRL_APALP) | 0x0f);
1051 if (retval)
1052 goto out;
1054 /* Turn off external DAC. */
1055 retval = snd_at73c213_write_reg(chip, DAC_CTRL, 0x0c);
1056 if (retval)
1057 goto out;
1058 msleep(2);
1059 retval = snd_at73c213_write_reg(chip, DAC_CTRL, 0x00);
1060 if (retval)
1061 goto out;
1063 /* Turn off master power. */
1064 retval = snd_at73c213_write_reg(chip, DAC_PRECH, 0x00);
1065 if (retval)
1066 goto out;
1068 out:
1069 /* Stop DAC master clock. */
1070 clk_disable(chip->board->dac_clk);
1072 ssc_free(chip->ssc);
1073 snd_card_free(card);
1074 dev_set_drvdata(&spi->dev, NULL);
1076 return 0;
1079 #ifdef CONFIG_PM
1080 static int snd_at73c213_suspend(struct spi_device *spi, pm_message_t msg)
1082 struct snd_card *card = dev_get_drvdata(&spi->dev);
1083 struct snd_at73c213 *chip = card->private_data;
1085 ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXDIS));
1086 clk_disable(chip->board->dac_clk);
1088 return 0;
1091 static int snd_at73c213_resume(struct spi_device *spi)
1093 struct snd_card *card = dev_get_drvdata(&spi->dev);
1094 struct snd_at73c213 *chip = card->private_data;
1096 clk_enable(chip->board->dac_clk);
1097 ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXEN));
1099 return 0;
1101 #else
1102 #define snd_at73c213_suspend NULL
1103 #define snd_at73c213_resume NULL
1104 #endif
1106 static struct spi_driver at73c213_driver = {
1107 .driver = {
1108 .name = "at73c213",
1110 .probe = snd_at73c213_probe,
1111 .suspend = snd_at73c213_suspend,
1112 .resume = snd_at73c213_resume,
1113 .remove = __devexit_p(snd_at73c213_remove),
1116 static int __init at73c213_init(void)
1118 return spi_register_driver(&at73c213_driver);
1120 module_init(at73c213_init);
1122 static void __exit at73c213_exit(void)
1124 spi_unregister_driver(&at73c213_driver);
1126 module_exit(at73c213_exit);
1128 MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
1129 MODULE_DESCRIPTION("Sound driver for AT73C213 with Atmel SSC");
1130 MODULE_LICENSE("GPL");