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.
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>
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>
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] =
56 0x00, /* 0C - PRECH */
61 0x00, /* 11 - PA_CTRL */
65 struct snd_card
*card
;
67 struct snd_pcm_substream
*substream
;
68 struct at73c213_board_info
*board
;
71 unsigned long bitrate
;
73 struct ssc_device
*ssc
;
74 struct spi_device
*spi
;
77 /* Image of the SPI registers in AT73C213. */
79 /* Protect SSC registers against concurrent access. */
81 /* Protect mixer registers against concurrent access. */
82 struct mutex mixer_lock
;
85 #define get_chip(card) ((struct snd_at73c213 *)card->private_data)
88 snd_at73c213_write_reg(struct snd_at73c213
*chip
, u8 reg
, u8 val
)
90 struct spi_message msg
;
91 struct spi_transfer msg_xfer
= {
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
);
109 chip
->reg_image
[reg
] = val
;
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. */
123 .buffer_bytes_max
= 64 * 1024 - 1,
124 .period_bytes_min
= 512,
125 .period_bytes_max
= 64 * 1024 - 1,
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
, status
;
137 unsigned long ssc_div_max
, ssc_div_min
;
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.
145 * The DAC master clock (MCLK) is programmable, and is either 256
146 * or (not here) 384 times the I2S output clock (BCLK).
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;
158 /* ssc_div must be a power of 2. */
159 ssc_div
= (ssc_div
+ 1) & ~1UL;
161 if ((ssc_rate
/ (ssc_div
* 2 * 16)) < BITRATE_MIN
) {
163 if ((ssc_rate
/ (ssc_div
* 2 * 16)) > BITRATE_MAX
)
167 /* Search for a possible bitrate. */
169 /* SSC clock / (ssc divider * 16-bit * stereo). */
170 if ((ssc_rate
/ (ssc_div
* 2 * 16)) < BITRATE_MIN
)
173 /* 256 / (2 * 16) = 8 */
174 dac_rate_new
= 8 * (ssc_rate
/ ssc_div
);
176 status
= clk_round_rate(chip
->board
->dac_clk
, dac_rate_new
);
180 /* Ignore difference smaller than 256 Hz. */
181 if ((status
/256) == (dac_rate_new
/256))
185 } while (--max_tries
);
187 /* Not able to find a valid bitrate. */
191 status
= clk_set_rate(chip
->board
->dac_clk
, status
);
195 /* Set divider in SSC device. */
196 ssc_writel(chip
->ssc
->regs
, CMR
, ssc_div
/2);
198 /* SSC clock / (ssc divider * 16-bit * stereo). */
199 chip
->bitrate
= ssc_rate
/ (ssc_div
* 16 * 2);
201 dev_info(&chip
->spi
->dev
,
202 "at73c213: supported bitrate is %lu (%lu divider)\n",
203 chip
->bitrate
, ssc_div
);
208 static int snd_at73c213_pcm_open(struct snd_pcm_substream
*substream
)
210 struct snd_at73c213
*chip
= snd_pcm_substream_chip(substream
);
211 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
213 snd_at73c213_playback_hw
.rate_min
= chip
->bitrate
;
214 snd_at73c213_playback_hw
.rate_max
= chip
->bitrate
;
215 runtime
->hw
= snd_at73c213_playback_hw
;
216 chip
->substream
= substream
;
221 static int snd_at73c213_pcm_close(struct snd_pcm_substream
*substream
)
223 struct snd_at73c213
*chip
= snd_pcm_substream_chip(substream
);
224 chip
->substream
= NULL
;
228 static int snd_at73c213_pcm_hw_params(struct snd_pcm_substream
*substream
,
229 struct snd_pcm_hw_params
*hw_params
)
231 return snd_pcm_lib_malloc_pages(substream
,
232 params_buffer_bytes(hw_params
));
235 static int snd_at73c213_pcm_hw_free(struct snd_pcm_substream
*substream
)
237 return snd_pcm_lib_free_pages(substream
);
240 static int snd_at73c213_pcm_prepare(struct snd_pcm_substream
*substream
)
242 struct snd_at73c213
*chip
= snd_pcm_substream_chip(substream
);
243 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
246 block_size
= frames_to_bytes(runtime
, runtime
->period_size
);
250 ssc_writel(chip
->ssc
->regs
, PDC_TPR
,
251 (long)runtime
->dma_addr
);
252 ssc_writel(chip
->ssc
->regs
, PDC_TCR
, runtime
->period_size
* 2);
253 ssc_writel(chip
->ssc
->regs
, PDC_TNPR
,
254 (long)runtime
->dma_addr
+ block_size
);
255 ssc_writel(chip
->ssc
->regs
, PDC_TNCR
, runtime
->period_size
* 2);
260 static int snd_at73c213_pcm_trigger(struct snd_pcm_substream
*substream
,
263 struct snd_at73c213
*chip
= snd_pcm_substream_chip(substream
);
266 spin_lock(&chip
->lock
);
269 case SNDRV_PCM_TRIGGER_START
:
270 ssc_writel(chip
->ssc
->regs
, IER
, SSC_BIT(IER_ENDTX
));
271 ssc_writel(chip
->ssc
->regs
, PDC_PTCR
, SSC_BIT(PDC_PTCR_TXTEN
));
273 case SNDRV_PCM_TRIGGER_STOP
:
274 ssc_writel(chip
->ssc
->regs
, PDC_PTCR
, SSC_BIT(PDC_PTCR_TXTDIS
));
275 ssc_writel(chip
->ssc
->regs
, IDR
, SSC_BIT(IDR_ENDTX
));
278 dev_dbg(&chip
->spi
->dev
, "spurious command %x\n", cmd
);
283 spin_unlock(&chip
->lock
);
288 static snd_pcm_uframes_t
289 snd_at73c213_pcm_pointer(struct snd_pcm_substream
*substream
)
291 struct snd_at73c213
*chip
= snd_pcm_substream_chip(substream
);
292 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
293 snd_pcm_uframes_t pos
;
296 bytes
= ssc_readl(chip
->ssc
->regs
, PDC_TPR
)
297 - (unsigned long)runtime
->dma_addr
;
299 pos
= bytes_to_frames(runtime
, bytes
);
300 if (pos
>= runtime
->buffer_size
)
301 pos
-= runtime
->buffer_size
;
306 static struct snd_pcm_ops at73c213_playback_ops
= {
307 .open
= snd_at73c213_pcm_open
,
308 .close
= snd_at73c213_pcm_close
,
309 .ioctl
= snd_pcm_lib_ioctl
,
310 .hw_params
= snd_at73c213_pcm_hw_params
,
311 .hw_free
= snd_at73c213_pcm_hw_free
,
312 .prepare
= snd_at73c213_pcm_prepare
,
313 .trigger
= snd_at73c213_pcm_trigger
,
314 .pointer
= snd_at73c213_pcm_pointer
,
317 static void snd_at73c213_pcm_free(struct snd_pcm
*pcm
)
319 struct snd_at73c213
*chip
= snd_pcm_chip(pcm
);
321 snd_pcm_lib_preallocate_free_for_all(chip
->pcm
);
326 static int __devinit
snd_at73c213_pcm_new(struct snd_at73c213
*chip
, int device
)
331 retval
= snd_pcm_new(chip
->card
, chip
->card
->shortname
,
336 pcm
->private_data
= chip
;
337 pcm
->private_free
= snd_at73c213_pcm_free
;
338 pcm
->info_flags
= SNDRV_PCM_INFO_BLOCK_TRANSFER
;
339 strcpy(pcm
->name
, "at73c213");
342 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &at73c213_playback_ops
);
344 retval
= snd_pcm_lib_preallocate_pages_for_all(chip
->pcm
,
345 SNDRV_DMA_TYPE_DEV
, &chip
->ssc
->pdev
->dev
,
346 64 * 1024, 64 * 1024);
351 static irqreturn_t
snd_at73c213_interrupt(int irq
, void *dev_id
)
353 struct snd_at73c213
*chip
= dev_id
;
354 struct snd_pcm_runtime
*runtime
= chip
->substream
->runtime
;
359 int retval
= IRQ_NONE
;
361 spin_lock(&chip
->lock
);
363 block_size
= frames_to_bytes(runtime
, runtime
->period_size
);
364 status
= ssc_readl(chip
->ssc
->regs
, IMR
);
366 if (status
& SSC_BIT(IMR_ENDTX
)) {
368 if (chip
->period
== runtime
->periods
)
370 next_period
= chip
->period
+ 1;
371 if (next_period
== runtime
->periods
)
374 offset
= block_size
* next_period
;
376 ssc_writel(chip
->ssc
->regs
, PDC_TNPR
,
377 (long)runtime
->dma_addr
+ offset
);
378 ssc_writel(chip
->ssc
->regs
, PDC_TNCR
, runtime
->period_size
* 2);
379 retval
= IRQ_HANDLED
;
382 ssc_readl(chip
->ssc
->regs
, IMR
);
383 spin_unlock(&chip
->lock
);
385 if (status
& SSC_BIT(IMR_ENDTX
))
386 snd_pcm_period_elapsed(chip
->substream
);
394 static int snd_at73c213_mono_get(struct snd_kcontrol
*kcontrol
,
395 struct snd_ctl_elem_value
*ucontrol
)
397 struct snd_at73c213
*chip
= snd_kcontrol_chip(kcontrol
);
398 int reg
= kcontrol
->private_value
& 0xff;
399 int shift
= (kcontrol
->private_value
>> 8) & 0xff;
400 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
401 int invert
= (kcontrol
->private_value
>> 24) & 0xff;
403 mutex_lock(&chip
->mixer_lock
);
405 ucontrol
->value
.integer
.value
[0] =
406 (chip
->reg_image
[reg
] >> shift
) & mask
;
409 ucontrol
->value
.integer
.value
[0] =
410 mask
- ucontrol
->value
.integer
.value
[0];
412 mutex_unlock(&chip
->mixer_lock
);
417 static int snd_at73c213_mono_put(struct snd_kcontrol
*kcontrol
,
418 struct snd_ctl_elem_value
*ucontrol
)
420 struct snd_at73c213
*chip
= snd_kcontrol_chip(kcontrol
);
421 int reg
= kcontrol
->private_value
& 0xff;
422 int shift
= (kcontrol
->private_value
>> 8) & 0xff;
423 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
424 int invert
= (kcontrol
->private_value
>> 24) & 0xff;
428 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
433 mutex_lock(&chip
->mixer_lock
);
435 val
= (chip
->reg_image
[reg
] & ~(mask
<< shift
)) | val
;
436 change
= val
!= chip
->reg_image
[reg
];
437 retval
= snd_at73c213_write_reg(chip
, reg
, val
);
439 mutex_unlock(&chip
->mixer_lock
);
447 static int snd_at73c213_stereo_info(struct snd_kcontrol
*kcontrol
,
448 struct snd_ctl_elem_info
*uinfo
)
450 int mask
= (kcontrol
->private_value
>> 24) & 0xff;
453 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
455 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
458 uinfo
->value
.integer
.min
= 0;
459 uinfo
->value
.integer
.max
= mask
;
464 static int snd_at73c213_stereo_get(struct snd_kcontrol
*kcontrol
,
465 struct snd_ctl_elem_value
*ucontrol
)
467 struct snd_at73c213
*chip
= snd_kcontrol_chip(kcontrol
);
468 int left_reg
= kcontrol
->private_value
& 0xff;
469 int right_reg
= (kcontrol
->private_value
>> 8) & 0xff;
470 int shift_left
= (kcontrol
->private_value
>> 16) & 0x07;
471 int shift_right
= (kcontrol
->private_value
>> 19) & 0x07;
472 int mask
= (kcontrol
->private_value
>> 24) & 0xff;
473 int invert
= (kcontrol
->private_value
>> 22) & 1;
475 mutex_lock(&chip
->mixer_lock
);
477 ucontrol
->value
.integer
.value
[0] =
478 (chip
->reg_image
[left_reg
] >> shift_left
) & mask
;
479 ucontrol
->value
.integer
.value
[1] =
480 (chip
->reg_image
[right_reg
] >> shift_right
) & mask
;
483 ucontrol
->value
.integer
.value
[0] =
484 mask
- ucontrol
->value
.integer
.value
[0];
485 ucontrol
->value
.integer
.value
[1] =
486 mask
- ucontrol
->value
.integer
.value
[1];
489 mutex_unlock(&chip
->mixer_lock
);
494 static int snd_at73c213_stereo_put(struct snd_kcontrol
*kcontrol
,
495 struct snd_ctl_elem_value
*ucontrol
)
497 struct snd_at73c213
*chip
= snd_kcontrol_chip(kcontrol
);
498 int left_reg
= kcontrol
->private_value
& 0xff;
499 int right_reg
= (kcontrol
->private_value
>> 8) & 0xff;
500 int shift_left
= (kcontrol
->private_value
>> 16) & 0x07;
501 int shift_right
= (kcontrol
->private_value
>> 19) & 0x07;
502 int mask
= (kcontrol
->private_value
>> 24) & 0xff;
503 int invert
= (kcontrol
->private_value
>> 22) & 1;
505 unsigned short val1
, val2
;
507 val1
= ucontrol
->value
.integer
.value
[0] & mask
;
508 val2
= ucontrol
->value
.integer
.value
[1] & mask
;
514 val2
<<= shift_right
;
516 mutex_lock(&chip
->mixer_lock
);
518 val1
= (chip
->reg_image
[left_reg
] & ~(mask
<< shift_left
)) | val1
;
519 val2
= (chip
->reg_image
[right_reg
] & ~(mask
<< shift_right
)) | val2
;
520 change
= val1
!= chip
->reg_image
[left_reg
]
521 || val2
!= chip
->reg_image
[right_reg
];
522 retval
= snd_at73c213_write_reg(chip
, left_reg
, val1
);
524 mutex_unlock(&chip
->mixer_lock
);
527 retval
= snd_at73c213_write_reg(chip
, right_reg
, val2
);
529 mutex_unlock(&chip
->mixer_lock
);
533 mutex_unlock(&chip
->mixer_lock
);
541 #define snd_at73c213_mono_switch_info snd_ctl_boolean_mono_info
543 static int snd_at73c213_mono_switch_get(struct snd_kcontrol
*kcontrol
,
544 struct snd_ctl_elem_value
*ucontrol
)
546 struct snd_at73c213
*chip
= snd_kcontrol_chip(kcontrol
);
547 int reg
= kcontrol
->private_value
& 0xff;
548 int shift
= (kcontrol
->private_value
>> 8) & 0xff;
549 int invert
= (kcontrol
->private_value
>> 24) & 0xff;
551 mutex_lock(&chip
->mixer_lock
);
553 ucontrol
->value
.integer
.value
[0] =
554 (chip
->reg_image
[reg
] >> shift
) & 0x01;
557 ucontrol
->value
.integer
.value
[0] =
558 0x01 - ucontrol
->value
.integer
.value
[0];
560 mutex_unlock(&chip
->mixer_lock
);
565 static int snd_at73c213_mono_switch_put(struct snd_kcontrol
*kcontrol
,
566 struct snd_ctl_elem_value
*ucontrol
)
568 struct snd_at73c213
*chip
= snd_kcontrol_chip(kcontrol
);
569 int reg
= kcontrol
->private_value
& 0xff;
570 int shift
= (kcontrol
->private_value
>> 8) & 0xff;
571 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
572 int invert
= (kcontrol
->private_value
>> 24) & 0xff;
576 if (ucontrol
->value
.integer
.value
[0])
585 mutex_lock(&chip
->mixer_lock
);
587 val
|= (chip
->reg_image
[reg
] & ~(mask
<< shift
));
588 change
= val
!= chip
->reg_image
[reg
];
590 retval
= snd_at73c213_write_reg(chip
, reg
, val
);
592 mutex_unlock(&chip
->mixer_lock
);
600 static int snd_at73c213_pa_volume_info(struct snd_kcontrol
*kcontrol
,
601 struct snd_ctl_elem_info
*uinfo
)
603 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
605 uinfo
->value
.integer
.min
= 0;
606 uinfo
->value
.integer
.max
= ((kcontrol
->private_value
>> 16) & 0xff) - 1;
611 static int snd_at73c213_line_capture_volume_info(
612 struct snd_kcontrol
*kcontrol
,
613 struct snd_ctl_elem_info
*uinfo
)
615 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
617 /* When inverted will give values 0x10001 => 0. */
618 uinfo
->value
.integer
.min
= 14;
619 uinfo
->value
.integer
.max
= 31;
624 static int snd_at73c213_aux_capture_volume_info(
625 struct snd_kcontrol
*kcontrol
,
626 struct snd_ctl_elem_info
*uinfo
)
628 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
630 /* When inverted will give values 0x10001 => 0. */
631 uinfo
->value
.integer
.min
= 14;
632 uinfo
->value
.integer
.max
= 31;
637 #define AT73C213_MONO_SWITCH(xname, xindex, reg, shift, mask, invert) \
639 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
642 .info = snd_at73c213_mono_switch_info, \
643 .get = snd_at73c213_mono_switch_get, \
644 .put = snd_at73c213_mono_switch_put, \
645 .private_value = (reg | (shift << 8) | (mask << 16) | (invert << 24)) \
648 #define AT73C213_STEREO(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
650 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
653 .info = snd_at73c213_stereo_info, \
654 .get = snd_at73c213_stereo_get, \
655 .put = snd_at73c213_stereo_put, \
656 .private_value = (left_reg | (right_reg << 8) \
657 | (shift_left << 16) | (shift_right << 19) \
658 | (mask << 24) | (invert << 22)) \
661 static struct snd_kcontrol_new snd_at73c213_controls
[] __devinitdata
= {
662 AT73C213_STEREO("Master Playback Volume", 0, DAC_LMPG
, DAC_RMPG
, 0, 0, 0x1f, 1),
663 AT73C213_STEREO("Master Playback Switch", 0, DAC_LMPG
, DAC_RMPG
, 5, 5, 1, 1),
664 AT73C213_STEREO("PCM Playback Volume", 0, DAC_LLOG
, DAC_RLOG
, 0, 0, 0x1f, 1),
665 AT73C213_STEREO("PCM Playback Switch", 0, DAC_LLOG
, DAC_RLOG
, 5, 5, 1, 1),
666 AT73C213_MONO_SWITCH("Mono PA Playback Switch", 0, DAC_CTRL
, DAC_CTRL_ONPADRV
,
669 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
670 .name
= "PA Playback Volume",
672 .info
= snd_at73c213_pa_volume_info
,
673 .get
= snd_at73c213_mono_get
,
674 .put
= snd_at73c213_mono_put
,
675 .private_value
= PA_CTRL
| (PA_CTRL_APAGAIN
<< 8) | \
676 (0x0f << 16) | (1 << 24),
678 AT73C213_MONO_SWITCH("PA High Gain Playback Switch", 0, PA_CTRL
, PA_CTRL_APALP
,
680 AT73C213_MONO_SWITCH("PA Playback Switch", 0, PA_CTRL
, PA_CTRL_APAON
, 0x01, 0),
682 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
683 .name
= "Aux Capture Volume",
685 .info
= snd_at73c213_aux_capture_volume_info
,
686 .get
= snd_at73c213_mono_get
,
687 .put
= snd_at73c213_mono_put
,
688 .private_value
= DAC_AUXG
| (0 << 8) | (0x1f << 16) | (1 << 24),
690 AT73C213_MONO_SWITCH("Aux Capture Switch", 0, DAC_CTRL
, DAC_CTRL_ONAUXIN
,
693 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
694 .name
= "Line Capture Volume",
696 .info
= snd_at73c213_line_capture_volume_info
,
697 .get
= snd_at73c213_stereo_get
,
698 .put
= snd_at73c213_stereo_put
,
699 .private_value
= DAC_LLIG
| (DAC_RLIG
<< 8) | (0 << 16) | (0 << 19)
700 | (0x1f << 24) | (1 << 22),
702 AT73C213_MONO_SWITCH("Line Capture Switch", 0, DAC_CTRL
, 0, 0x03, 0),
705 static int __devinit
snd_at73c213_mixer(struct snd_at73c213
*chip
)
707 struct snd_card
*card
;
710 if (chip
== NULL
|| chip
->pcm
== NULL
)
715 strcpy(card
->mixername
, chip
->pcm
->name
);
717 for (idx
= 0; idx
< ARRAY_SIZE(snd_at73c213_controls
); idx
++) {
718 errval
= snd_ctl_add(card
,
719 snd_ctl_new1(&snd_at73c213_controls
[idx
],
728 for (idx
= 1; idx
< ARRAY_SIZE(snd_at73c213_controls
) + 1; idx
++) {
729 struct snd_kcontrol
*kctl
;
730 kctl
= snd_ctl_find_numid(card
, idx
);
732 snd_ctl_remove(card
, kctl
);
740 static int snd_at73c213_ssc_init(struct snd_at73c213
*chip
)
743 * Continuous clock output.
744 * Starts on falling TF.
745 * Delay 1 cycle (1 bit).
746 * Periode is 16 bit (16 - 1).
748 ssc_writel(chip
->ssc
->regs
, TCMR
,
750 | SSC_BF(TCMR_START
, 4)
751 | SSC_BF(TCMR_STTDLY
, 1)
752 | SSC_BF(TCMR_PERIOD
, 16 - 1));
754 * Data length is 16 bit (16 - 1).
755 * Transmit MSB first.
756 * Transmit 2 words each transfer.
757 * Frame sync length is 16 bit (16 - 1).
758 * Frame starts on negative pulse.
760 ssc_writel(chip
->ssc
->regs
, TFMR
,
761 SSC_BF(TFMR_DATLEN
, 16 - 1)
763 | SSC_BF(TFMR_DATNB
, 1)
764 | SSC_BF(TFMR_FSLEN
, 16 - 1)
765 | SSC_BF(TFMR_FSOS
, 1));
770 static int snd_at73c213_chip_init(struct snd_at73c213
*chip
)
773 unsigned char dac_ctrl
= 0;
775 retval
= snd_at73c213_set_bitrate(chip
);
779 /* Enable DAC master clock. */
780 clk_enable(chip
->board
->dac_clk
);
782 /* Initialize at73c213 on SPI bus. */
783 retval
= snd_at73c213_write_reg(chip
, DAC_RST
, 0x04);
787 retval
= snd_at73c213_write_reg(chip
, DAC_RST
, 0x03);
791 /* Precharge everything. */
792 retval
= snd_at73c213_write_reg(chip
, DAC_PRECH
, 0xff);
795 retval
= snd_at73c213_write_reg(chip
, PA_CTRL
, (1<<PA_CTRL_APAPRECH
));
798 retval
= snd_at73c213_write_reg(chip
, DAC_CTRL
,
799 (1<<DAC_CTRL_ONLNOL
) | (1<<DAC_CTRL_ONLNOR
));
805 /* Stop precharging PA. */
806 retval
= snd_at73c213_write_reg(chip
, PA_CTRL
,
807 (1<<PA_CTRL_APALP
) | 0x0f);
813 /* Stop precharging DAC, turn on master power. */
814 retval
= snd_at73c213_write_reg(chip
, DAC_PRECH
, (1<<DAC_PRECH_ONMSTR
));
821 dac_ctrl
= (1<<DAC_CTRL_ONDACL
) | (1<<DAC_CTRL_ONDACR
)
822 | (1<<DAC_CTRL_ONLNOL
) | (1<<DAC_CTRL_ONLNOR
);
824 retval
= snd_at73c213_write_reg(chip
, DAC_CTRL
, dac_ctrl
);
829 retval
= snd_at73c213_write_reg(chip
, DAC_LMPG
, 0x3f);
832 retval
= snd_at73c213_write_reg(chip
, DAC_RMPG
, 0x3f);
835 retval
= snd_at73c213_write_reg(chip
, DAC_LLOG
, 0x3f);
838 retval
= snd_at73c213_write_reg(chip
, DAC_RLOG
, 0x3f);
841 retval
= snd_at73c213_write_reg(chip
, DAC_LLIG
, 0x11);
844 retval
= snd_at73c213_write_reg(chip
, DAC_RLIG
, 0x11);
847 retval
= snd_at73c213_write_reg(chip
, DAC_AUXG
, 0x11);
851 /* Enable I2S device, i.e. clock output. */
852 ssc_writel(chip
->ssc
->regs
, CR
, SSC_BIT(CR_TXEN
));
857 clk_disable(chip
->board
->dac_clk
);
862 static int snd_at73c213_dev_free(struct snd_device
*device
)
864 struct snd_at73c213
*chip
= device
->device_data
;
866 ssc_writel(chip
->ssc
->regs
, CR
, SSC_BIT(CR_TXDIS
));
867 if (chip
->irq
>= 0) {
868 free_irq(chip
->irq
, chip
);
875 static int __devinit
snd_at73c213_dev_init(struct snd_card
*card
,
876 struct spi_device
*spi
)
878 static struct snd_device_ops ops
= {
879 .dev_free
= snd_at73c213_dev_free
,
881 struct snd_at73c213
*chip
= get_chip(card
);
884 irq
= chip
->ssc
->irq
;
888 spin_lock_init(&chip
->lock
);
889 mutex_init(&chip
->mixer_lock
);
893 retval
= request_irq(irq
, snd_at73c213_interrupt
, 0, "at73c213", chip
);
895 dev_dbg(&chip
->spi
->dev
, "unable to request irq %d\n", irq
);
900 memcpy(&chip
->reg_image
, &snd_at73c213_original_image
,
901 sizeof(snd_at73c213_original_image
));
903 retval
= snd_at73c213_ssc_init(chip
);
907 retval
= snd_at73c213_chip_init(chip
);
911 retval
= snd_at73c213_pcm_new(chip
, 0);
915 retval
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, chip
, &ops
);
919 retval
= snd_at73c213_mixer(chip
);
923 snd_card_set_dev(card
, &spi
->dev
);
928 snd_device_free(card
, chip
);
930 free_irq(chip
->irq
, chip
);
936 static int snd_at73c213_probe(struct spi_device
*spi
)
938 struct snd_card
*card
;
939 struct snd_at73c213
*chip
;
940 struct at73c213_board_info
*board
;
944 board
= spi
->dev
.platform_data
;
946 dev_dbg(&spi
->dev
, "no platform_data\n");
950 if (!board
->dac_clk
) {
951 dev_dbg(&spi
->dev
, "no DAC clk\n");
955 if (IS_ERR(board
->dac_clk
)) {
956 dev_dbg(&spi
->dev
, "no DAC clk\n");
957 return PTR_ERR(board
->dac_clk
);
962 /* Allocate "card" using some unused identifiers. */
963 snprintf(id
, sizeof id
, "at73c213_%d", board
->ssc_id
);
964 card
= snd_card_new(-1, id
, THIS_MODULE
, sizeof(struct snd_at73c213
));
968 chip
= card
->private_data
;
972 chip
->ssc
= ssc_request(board
->ssc_id
);
973 if (IS_ERR(chip
->ssc
)) {
974 dev_dbg(&spi
->dev
, "could not get ssc%d device\n",
976 retval
= PTR_ERR(chip
->ssc
);
980 retval
= snd_at73c213_dev_init(card
, spi
);
984 strcpy(card
->driver
, "at73c213");
985 strcpy(card
->shortname
, board
->shortname
);
986 sprintf(card
->longname
, "%s on irq %d", card
->shortname
, chip
->irq
);
988 retval
= snd_card_register(card
);
992 dev_set_drvdata(&spi
->dev
, card
);
1004 static int __devexit
snd_at73c213_remove(struct spi_device
*spi
)
1006 struct snd_card
*card
= dev_get_drvdata(&spi
->dev
);
1007 struct snd_at73c213
*chip
= card
->private_data
;
1010 /* Stop playback. */
1011 ssc_writel(chip
->ssc
->regs
, CR
, SSC_BIT(CR_TXDIS
));
1014 retval
= snd_at73c213_write_reg(chip
, DAC_LMPG
, 0x3f);
1017 retval
= snd_at73c213_write_reg(chip
, DAC_RMPG
, 0x3f);
1020 retval
= snd_at73c213_write_reg(chip
, DAC_LLOG
, 0x3f);
1023 retval
= snd_at73c213_write_reg(chip
, DAC_RLOG
, 0x3f);
1026 retval
= snd_at73c213_write_reg(chip
, DAC_LLIG
, 0x11);
1029 retval
= snd_at73c213_write_reg(chip
, DAC_RLIG
, 0x11);
1032 retval
= snd_at73c213_write_reg(chip
, DAC_AUXG
, 0x11);
1037 retval
= snd_at73c213_write_reg(chip
, PA_CTRL
,
1038 chip
->reg_image
[PA_CTRL
] | 0x0f);
1042 retval
= snd_at73c213_write_reg(chip
, PA_CTRL
,
1043 (1 << PA_CTRL_APALP
) | 0x0f);
1047 /* Turn off external DAC. */
1048 retval
= snd_at73c213_write_reg(chip
, DAC_CTRL
, 0x0c);
1052 retval
= snd_at73c213_write_reg(chip
, DAC_CTRL
, 0x00);
1056 /* Turn off master power. */
1057 retval
= snd_at73c213_write_reg(chip
, DAC_PRECH
, 0x00);
1062 /* Stop DAC master clock. */
1063 clk_disable(chip
->board
->dac_clk
);
1065 ssc_free(chip
->ssc
);
1066 snd_card_free(card
);
1067 dev_set_drvdata(&spi
->dev
, NULL
);
1073 static int snd_at73c213_suspend(struct spi_device
*spi
, pm_message_t msg
)
1075 struct snd_card
*card
= dev_get_drvdata(&spi
->dev
);
1076 struct snd_at73c213
*chip
= card
->private_data
;
1078 ssc_writel(chip
->ssc
->regs
, CR
, SSC_BIT(CR_TXDIS
));
1079 clk_disable(chip
->board
->dac_clk
);
1084 static int snd_at73c213_resume(struct spi_device
*spi
)
1086 struct snd_card
*card
= dev_get_drvdata(&spi
->dev
);
1087 struct snd_at73c213
*chip
= card
->private_data
;
1089 clk_enable(chip
->board
->dac_clk
);
1090 ssc_writel(chip
->ssc
->regs
, CR
, SSC_BIT(CR_TXEN
));
1095 #define snd_at73c213_suspend NULL
1096 #define snd_at73c213_resume NULL
1099 static struct spi_driver at73c213_driver
= {
1103 .probe
= snd_at73c213_probe
,
1104 .suspend
= snd_at73c213_suspend
,
1105 .resume
= snd_at73c213_resume
,
1106 .remove
= __devexit_p(snd_at73c213_remove
),
1109 static int __init
at73c213_init(void)
1111 return spi_register_driver(&at73c213_driver
);
1113 module_init(at73c213_init
);
1115 static void __exit
at73c213_exit(void)
1117 spi_unregister_driver(&at73c213_driver
);
1119 module_exit(at73c213_exit
);
1121 MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
1122 MODULE_DESCRIPTION("Sound driver for AT73C213 with Atmel SSC");
1123 MODULE_LICENSE("GPL");