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
;
138 unsigned long ssc_div_max
, ssc_div_min
;
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;
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
) {
164 if ((ssc_rate
/ (ssc_div
* 2 * 16)) > BITRATE_MAX
)
168 /* Search for a possible bitrate. */
170 /* SSC clock / (ssc divider * 16-bit * stereo). */
171 if ((ssc_rate
/ (ssc_div
* 2 * 16)) < BITRATE_MIN
)
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
);
181 /* Ignore difference smaller than 256 Hz. */
182 if ((status
/256) == (dac_rate_new
/256))
186 } while (--max_tries
);
188 /* Not able to find a valid bitrate. */
192 status
= clk_set_rate(chip
->board
->dac_clk
, 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
);
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
;
214 snd_at73c213_playback_hw
.rate_min
= chip
->bitrate
;
215 snd_at73c213_playback_hw
.rate_max
= chip
->bitrate
;
216 runtime
->hw
= snd_at73c213_playback_hw
;
217 chip
->substream
= substream
;
222 static int snd_at73c213_pcm_close(struct snd_pcm_substream
*substream
)
224 struct snd_at73c213
*chip
= snd_pcm_substream_chip(substream
);
225 chip
->substream
= NULL
;
229 static int snd_at73c213_pcm_hw_params(struct snd_pcm_substream
*substream
,
230 struct snd_pcm_hw_params
*hw_params
)
232 struct snd_at73c213
*chip
= snd_pcm_substream_chip(substream
);
233 int channels
= params_channels(hw_params
);
236 val
= ssc_readl(chip
->ssc
->regs
, TFMR
);
237 val
= SSC_BFINS(TFMR_DATNB
, channels
- 1, val
);
238 ssc_writel(chip
->ssc
->regs
, TFMR
, val
);
240 return snd_pcm_lib_malloc_pages(substream
,
241 params_buffer_bytes(hw_params
));
244 static int snd_at73c213_pcm_hw_free(struct snd_pcm_substream
*substream
)
246 return snd_pcm_lib_free_pages(substream
);
249 static int snd_at73c213_pcm_prepare(struct snd_pcm_substream
*substream
)
251 struct snd_at73c213
*chip
= snd_pcm_substream_chip(substream
);
252 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
255 block_size
= frames_to_bytes(runtime
, runtime
->period_size
);
259 ssc_writel(chip
->ssc
->regs
, PDC_TPR
,
260 (long)runtime
->dma_addr
);
261 ssc_writel(chip
->ssc
->regs
, PDC_TCR
,
262 runtime
->period_size
* runtime
->channels
);
263 ssc_writel(chip
->ssc
->regs
, PDC_TNPR
,
264 (long)runtime
->dma_addr
+ block_size
);
265 ssc_writel(chip
->ssc
->regs
, PDC_TNCR
,
266 runtime
->period_size
* runtime
->channels
);
271 static int snd_at73c213_pcm_trigger(struct snd_pcm_substream
*substream
,
274 struct snd_at73c213
*chip
= snd_pcm_substream_chip(substream
);
277 spin_lock(&chip
->lock
);
280 case SNDRV_PCM_TRIGGER_START
:
281 ssc_writel(chip
->ssc
->regs
, IER
, SSC_BIT(IER_ENDTX
));
282 ssc_writel(chip
->ssc
->regs
, PDC_PTCR
, SSC_BIT(PDC_PTCR_TXTEN
));
284 case SNDRV_PCM_TRIGGER_STOP
:
285 ssc_writel(chip
->ssc
->regs
, PDC_PTCR
, SSC_BIT(PDC_PTCR_TXTDIS
));
286 ssc_writel(chip
->ssc
->regs
, IDR
, SSC_BIT(IDR_ENDTX
));
289 dev_dbg(&chip
->spi
->dev
, "spurious command %x\n", cmd
);
294 spin_unlock(&chip
->lock
);
299 static snd_pcm_uframes_t
300 snd_at73c213_pcm_pointer(struct snd_pcm_substream
*substream
)
302 struct snd_at73c213
*chip
= snd_pcm_substream_chip(substream
);
303 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
304 snd_pcm_uframes_t pos
;
307 bytes
= ssc_readl(chip
->ssc
->regs
, PDC_TPR
)
308 - (unsigned long)runtime
->dma_addr
;
310 pos
= bytes_to_frames(runtime
, bytes
);
311 if (pos
>= runtime
->buffer_size
)
312 pos
-= runtime
->buffer_size
;
317 static struct snd_pcm_ops at73c213_playback_ops
= {
318 .open
= snd_at73c213_pcm_open
,
319 .close
= snd_at73c213_pcm_close
,
320 .ioctl
= snd_pcm_lib_ioctl
,
321 .hw_params
= snd_at73c213_pcm_hw_params
,
322 .hw_free
= snd_at73c213_pcm_hw_free
,
323 .prepare
= snd_at73c213_pcm_prepare
,
324 .trigger
= snd_at73c213_pcm_trigger
,
325 .pointer
= snd_at73c213_pcm_pointer
,
328 static void snd_at73c213_pcm_free(struct snd_pcm
*pcm
)
330 struct snd_at73c213
*chip
= snd_pcm_chip(pcm
);
332 snd_pcm_lib_preallocate_free_for_all(chip
->pcm
);
337 static int __devinit
snd_at73c213_pcm_new(struct snd_at73c213
*chip
, int device
)
342 retval
= snd_pcm_new(chip
->card
, chip
->card
->shortname
,
347 pcm
->private_data
= chip
;
348 pcm
->private_free
= snd_at73c213_pcm_free
;
349 pcm
->info_flags
= SNDRV_PCM_INFO_BLOCK_TRANSFER
;
350 strcpy(pcm
->name
, "at73c213");
353 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &at73c213_playback_ops
);
355 retval
= snd_pcm_lib_preallocate_pages_for_all(chip
->pcm
,
356 SNDRV_DMA_TYPE_DEV
, &chip
->ssc
->pdev
->dev
,
357 64 * 1024, 64 * 1024);
362 static irqreturn_t
snd_at73c213_interrupt(int irq
, void *dev_id
)
364 struct snd_at73c213
*chip
= dev_id
;
365 struct snd_pcm_runtime
*runtime
= chip
->substream
->runtime
;
370 int retval
= IRQ_NONE
;
372 spin_lock(&chip
->lock
);
374 block_size
= frames_to_bytes(runtime
, runtime
->period_size
);
375 status
= ssc_readl(chip
->ssc
->regs
, IMR
);
377 if (status
& SSC_BIT(IMR_ENDTX
)) {
379 if (chip
->period
== runtime
->periods
)
381 next_period
= chip
->period
+ 1;
382 if (next_period
== runtime
->periods
)
385 offset
= block_size
* next_period
;
387 ssc_writel(chip
->ssc
->regs
, PDC_TNPR
,
388 (long)runtime
->dma_addr
+ offset
);
389 ssc_writel(chip
->ssc
->regs
, PDC_TNCR
,
390 runtime
->period_size
* runtime
->channels
);
391 retval
= IRQ_HANDLED
;
394 ssc_readl(chip
->ssc
->regs
, IMR
);
395 spin_unlock(&chip
->lock
);
397 if (status
& SSC_BIT(IMR_ENDTX
))
398 snd_pcm_period_elapsed(chip
->substream
);
406 static int snd_at73c213_mono_get(struct snd_kcontrol
*kcontrol
,
407 struct snd_ctl_elem_value
*ucontrol
)
409 struct snd_at73c213
*chip
= snd_kcontrol_chip(kcontrol
);
410 int reg
= kcontrol
->private_value
& 0xff;
411 int shift
= (kcontrol
->private_value
>> 8) & 0xff;
412 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
413 int invert
= (kcontrol
->private_value
>> 24) & 0xff;
415 mutex_lock(&chip
->mixer_lock
);
417 ucontrol
->value
.integer
.value
[0] =
418 (chip
->reg_image
[reg
] >> shift
) & mask
;
421 ucontrol
->value
.integer
.value
[0] =
422 mask
- ucontrol
->value
.integer
.value
[0];
424 mutex_unlock(&chip
->mixer_lock
);
429 static int snd_at73c213_mono_put(struct snd_kcontrol
*kcontrol
,
430 struct snd_ctl_elem_value
*ucontrol
)
432 struct snd_at73c213
*chip
= snd_kcontrol_chip(kcontrol
);
433 int reg
= kcontrol
->private_value
& 0xff;
434 int shift
= (kcontrol
->private_value
>> 8) & 0xff;
435 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
436 int invert
= (kcontrol
->private_value
>> 24) & 0xff;
440 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
445 mutex_lock(&chip
->mixer_lock
);
447 val
= (chip
->reg_image
[reg
] & ~(mask
<< shift
)) | val
;
448 change
= val
!= chip
->reg_image
[reg
];
449 retval
= snd_at73c213_write_reg(chip
, reg
, val
);
451 mutex_unlock(&chip
->mixer_lock
);
459 static int snd_at73c213_stereo_info(struct snd_kcontrol
*kcontrol
,
460 struct snd_ctl_elem_info
*uinfo
)
462 int mask
= (kcontrol
->private_value
>> 24) & 0xff;
465 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
467 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
470 uinfo
->value
.integer
.min
= 0;
471 uinfo
->value
.integer
.max
= mask
;
476 static int snd_at73c213_stereo_get(struct snd_kcontrol
*kcontrol
,
477 struct snd_ctl_elem_value
*ucontrol
)
479 struct snd_at73c213
*chip
= snd_kcontrol_chip(kcontrol
);
480 int left_reg
= kcontrol
->private_value
& 0xff;
481 int right_reg
= (kcontrol
->private_value
>> 8) & 0xff;
482 int shift_left
= (kcontrol
->private_value
>> 16) & 0x07;
483 int shift_right
= (kcontrol
->private_value
>> 19) & 0x07;
484 int mask
= (kcontrol
->private_value
>> 24) & 0xff;
485 int invert
= (kcontrol
->private_value
>> 22) & 1;
487 mutex_lock(&chip
->mixer_lock
);
489 ucontrol
->value
.integer
.value
[0] =
490 (chip
->reg_image
[left_reg
] >> shift_left
) & mask
;
491 ucontrol
->value
.integer
.value
[1] =
492 (chip
->reg_image
[right_reg
] >> shift_right
) & mask
;
495 ucontrol
->value
.integer
.value
[0] =
496 mask
- ucontrol
->value
.integer
.value
[0];
497 ucontrol
->value
.integer
.value
[1] =
498 mask
- ucontrol
->value
.integer
.value
[1];
501 mutex_unlock(&chip
->mixer_lock
);
506 static int snd_at73c213_stereo_put(struct snd_kcontrol
*kcontrol
,
507 struct snd_ctl_elem_value
*ucontrol
)
509 struct snd_at73c213
*chip
= snd_kcontrol_chip(kcontrol
);
510 int left_reg
= kcontrol
->private_value
& 0xff;
511 int right_reg
= (kcontrol
->private_value
>> 8) & 0xff;
512 int shift_left
= (kcontrol
->private_value
>> 16) & 0x07;
513 int shift_right
= (kcontrol
->private_value
>> 19) & 0x07;
514 int mask
= (kcontrol
->private_value
>> 24) & 0xff;
515 int invert
= (kcontrol
->private_value
>> 22) & 1;
517 unsigned short val1
, val2
;
519 val1
= ucontrol
->value
.integer
.value
[0] & mask
;
520 val2
= ucontrol
->value
.integer
.value
[1] & mask
;
526 val2
<<= shift_right
;
528 mutex_lock(&chip
->mixer_lock
);
530 val1
= (chip
->reg_image
[left_reg
] & ~(mask
<< shift_left
)) | val1
;
531 val2
= (chip
->reg_image
[right_reg
] & ~(mask
<< shift_right
)) | val2
;
532 change
= val1
!= chip
->reg_image
[left_reg
]
533 || val2
!= chip
->reg_image
[right_reg
];
534 retval
= snd_at73c213_write_reg(chip
, left_reg
, val1
);
536 mutex_unlock(&chip
->mixer_lock
);
539 retval
= snd_at73c213_write_reg(chip
, right_reg
, val2
);
541 mutex_unlock(&chip
->mixer_lock
);
545 mutex_unlock(&chip
->mixer_lock
);
553 #define snd_at73c213_mono_switch_info snd_ctl_boolean_mono_info
555 static int snd_at73c213_mono_switch_get(struct snd_kcontrol
*kcontrol
,
556 struct snd_ctl_elem_value
*ucontrol
)
558 struct snd_at73c213
*chip
= snd_kcontrol_chip(kcontrol
);
559 int reg
= kcontrol
->private_value
& 0xff;
560 int shift
= (kcontrol
->private_value
>> 8) & 0xff;
561 int invert
= (kcontrol
->private_value
>> 24) & 0xff;
563 mutex_lock(&chip
->mixer_lock
);
565 ucontrol
->value
.integer
.value
[0] =
566 (chip
->reg_image
[reg
] >> shift
) & 0x01;
569 ucontrol
->value
.integer
.value
[0] =
570 0x01 - ucontrol
->value
.integer
.value
[0];
572 mutex_unlock(&chip
->mixer_lock
);
577 static int snd_at73c213_mono_switch_put(struct snd_kcontrol
*kcontrol
,
578 struct snd_ctl_elem_value
*ucontrol
)
580 struct snd_at73c213
*chip
= snd_kcontrol_chip(kcontrol
);
581 int reg
= kcontrol
->private_value
& 0xff;
582 int shift
= (kcontrol
->private_value
>> 8) & 0xff;
583 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
584 int invert
= (kcontrol
->private_value
>> 24) & 0xff;
588 if (ucontrol
->value
.integer
.value
[0])
597 mutex_lock(&chip
->mixer_lock
);
599 val
|= (chip
->reg_image
[reg
] & ~(mask
<< shift
));
600 change
= val
!= chip
->reg_image
[reg
];
602 retval
= snd_at73c213_write_reg(chip
, reg
, val
);
604 mutex_unlock(&chip
->mixer_lock
);
612 static int snd_at73c213_pa_volume_info(struct snd_kcontrol
*kcontrol
,
613 struct snd_ctl_elem_info
*uinfo
)
615 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
617 uinfo
->value
.integer
.min
= 0;
618 uinfo
->value
.integer
.max
= ((kcontrol
->private_value
>> 16) & 0xff) - 1;
623 static int snd_at73c213_line_capture_volume_info(
624 struct snd_kcontrol
*kcontrol
,
625 struct snd_ctl_elem_info
*uinfo
)
627 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
629 /* When inverted will give values 0x10001 => 0. */
630 uinfo
->value
.integer
.min
= 14;
631 uinfo
->value
.integer
.max
= 31;
636 static int snd_at73c213_aux_capture_volume_info(
637 struct snd_kcontrol
*kcontrol
,
638 struct snd_ctl_elem_info
*uinfo
)
640 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
642 /* When inverted will give values 0x10001 => 0. */
643 uinfo
->value
.integer
.min
= 14;
644 uinfo
->value
.integer
.max
= 31;
649 #define AT73C213_MONO_SWITCH(xname, xindex, reg, shift, mask, invert) \
651 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
654 .info = snd_at73c213_mono_switch_info, \
655 .get = snd_at73c213_mono_switch_get, \
656 .put = snd_at73c213_mono_switch_put, \
657 .private_value = (reg | (shift << 8) | (mask << 16) | (invert << 24)) \
660 #define AT73C213_STEREO(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
662 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
665 .info = snd_at73c213_stereo_info, \
666 .get = snd_at73c213_stereo_get, \
667 .put = snd_at73c213_stereo_put, \
668 .private_value = (left_reg | (right_reg << 8) \
669 | (shift_left << 16) | (shift_right << 19) \
670 | (mask << 24) | (invert << 22)) \
673 static struct snd_kcontrol_new snd_at73c213_controls
[] __devinitdata
= {
674 AT73C213_STEREO("Master Playback Volume", 0, DAC_LMPG
, DAC_RMPG
, 0, 0, 0x1f, 1),
675 AT73C213_STEREO("Master Playback Switch", 0, DAC_LMPG
, DAC_RMPG
, 5, 5, 1, 1),
676 AT73C213_STEREO("PCM Playback Volume", 0, DAC_LLOG
, DAC_RLOG
, 0, 0, 0x1f, 1),
677 AT73C213_STEREO("PCM Playback Switch", 0, DAC_LLOG
, DAC_RLOG
, 5, 5, 1, 1),
678 AT73C213_MONO_SWITCH("Mono PA Playback Switch", 0, DAC_CTRL
, DAC_CTRL_ONPADRV
,
681 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
682 .name
= "PA Playback Volume",
684 .info
= snd_at73c213_pa_volume_info
,
685 .get
= snd_at73c213_mono_get
,
686 .put
= snd_at73c213_mono_put
,
687 .private_value
= PA_CTRL
| (PA_CTRL_APAGAIN
<< 8) | \
688 (0x0f << 16) | (1 << 24),
690 AT73C213_MONO_SWITCH("PA High Gain Playback Switch", 0, PA_CTRL
, PA_CTRL_APALP
,
692 AT73C213_MONO_SWITCH("PA Playback Switch", 0, PA_CTRL
, PA_CTRL_APAON
, 0x01, 0),
694 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
695 .name
= "Aux Capture Volume",
697 .info
= snd_at73c213_aux_capture_volume_info
,
698 .get
= snd_at73c213_mono_get
,
699 .put
= snd_at73c213_mono_put
,
700 .private_value
= DAC_AUXG
| (0 << 8) | (0x1f << 16) | (1 << 24),
702 AT73C213_MONO_SWITCH("Aux Capture Switch", 0, DAC_CTRL
, DAC_CTRL_ONAUXIN
,
705 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
706 .name
= "Line Capture Volume",
708 .info
= snd_at73c213_line_capture_volume_info
,
709 .get
= snd_at73c213_stereo_get
,
710 .put
= snd_at73c213_stereo_put
,
711 .private_value
= DAC_LLIG
| (DAC_RLIG
<< 8) | (0 << 16) | (0 << 19)
712 | (0x1f << 24) | (1 << 22),
714 AT73C213_MONO_SWITCH("Line Capture Switch", 0, DAC_CTRL
, 0, 0x03, 0),
717 static int __devinit
snd_at73c213_mixer(struct snd_at73c213
*chip
)
719 struct snd_card
*card
;
722 if (chip
== NULL
|| chip
->pcm
== NULL
)
727 strcpy(card
->mixername
, chip
->pcm
->name
);
729 for (idx
= 0; idx
< ARRAY_SIZE(snd_at73c213_controls
); idx
++) {
730 errval
= snd_ctl_add(card
,
731 snd_ctl_new1(&snd_at73c213_controls
[idx
],
740 for (idx
= 1; idx
< ARRAY_SIZE(snd_at73c213_controls
) + 1; idx
++) {
741 struct snd_kcontrol
*kctl
;
742 kctl
= snd_ctl_find_numid(card
, idx
);
744 snd_ctl_remove(card
, kctl
);
752 static int __devinit
snd_at73c213_ssc_init(struct snd_at73c213
*chip
)
755 * Continuous clock output.
756 * Starts on falling TF.
757 * Delay 1 cycle (1 bit).
758 * Periode is 16 bit (16 - 1).
760 ssc_writel(chip
->ssc
->regs
, TCMR
,
762 | SSC_BF(TCMR_START
, 4)
763 | SSC_BF(TCMR_STTDLY
, 1)
764 | SSC_BF(TCMR_PERIOD
, 16 - 1));
766 * Data length is 16 bit (16 - 1).
767 * Transmit MSB first.
768 * Transmit 2 words each transfer.
769 * Frame sync length is 16 bit (16 - 1).
770 * Frame starts on negative pulse.
772 ssc_writel(chip
->ssc
->regs
, TFMR
,
773 SSC_BF(TFMR_DATLEN
, 16 - 1)
775 | SSC_BF(TFMR_DATNB
, 1)
776 | SSC_BF(TFMR_FSLEN
, 16 - 1)
777 | SSC_BF(TFMR_FSOS
, 1));
782 static int __devinit
snd_at73c213_chip_init(struct snd_at73c213
*chip
)
785 unsigned char dac_ctrl
= 0;
787 retval
= snd_at73c213_set_bitrate(chip
);
791 /* Enable DAC master clock. */
792 clk_enable(chip
->board
->dac_clk
);
794 /* Initialize at73c213 on SPI bus. */
795 retval
= snd_at73c213_write_reg(chip
, DAC_RST
, 0x04);
799 retval
= snd_at73c213_write_reg(chip
, DAC_RST
, 0x03);
803 /* Precharge everything. */
804 retval
= snd_at73c213_write_reg(chip
, DAC_PRECH
, 0xff);
807 retval
= snd_at73c213_write_reg(chip
, PA_CTRL
, (1<<PA_CTRL_APAPRECH
));
810 retval
= snd_at73c213_write_reg(chip
, DAC_CTRL
,
811 (1<<DAC_CTRL_ONLNOL
) | (1<<DAC_CTRL_ONLNOR
));
817 /* Stop precharging PA. */
818 retval
= snd_at73c213_write_reg(chip
, PA_CTRL
,
819 (1<<PA_CTRL_APALP
) | 0x0f);
825 /* Stop precharging DAC, turn on master power. */
826 retval
= snd_at73c213_write_reg(chip
, DAC_PRECH
, (1<<DAC_PRECH_ONMSTR
));
833 dac_ctrl
= (1<<DAC_CTRL_ONDACL
) | (1<<DAC_CTRL_ONDACR
)
834 | (1<<DAC_CTRL_ONLNOL
) | (1<<DAC_CTRL_ONLNOR
);
836 retval
= snd_at73c213_write_reg(chip
, DAC_CTRL
, dac_ctrl
);
841 retval
= snd_at73c213_write_reg(chip
, DAC_LMPG
, 0x3f);
844 retval
= snd_at73c213_write_reg(chip
, DAC_RMPG
, 0x3f);
847 retval
= snd_at73c213_write_reg(chip
, DAC_LLOG
, 0x3f);
850 retval
= snd_at73c213_write_reg(chip
, DAC_RLOG
, 0x3f);
853 retval
= snd_at73c213_write_reg(chip
, DAC_LLIG
, 0x11);
856 retval
= snd_at73c213_write_reg(chip
, DAC_RLIG
, 0x11);
859 retval
= snd_at73c213_write_reg(chip
, DAC_AUXG
, 0x11);
863 /* Enable I2S device, i.e. clock output. */
864 ssc_writel(chip
->ssc
->regs
, CR
, SSC_BIT(CR_TXEN
));
869 clk_disable(chip
->board
->dac_clk
);
874 static int snd_at73c213_dev_free(struct snd_device
*device
)
876 struct snd_at73c213
*chip
= device
->device_data
;
878 ssc_writel(chip
->ssc
->regs
, CR
, SSC_BIT(CR_TXDIS
));
879 if (chip
->irq
>= 0) {
880 free_irq(chip
->irq
, chip
);
887 static int __devinit
snd_at73c213_dev_init(struct snd_card
*card
,
888 struct spi_device
*spi
)
890 static struct snd_device_ops ops
= {
891 .dev_free
= snd_at73c213_dev_free
,
893 struct snd_at73c213
*chip
= get_chip(card
);
896 irq
= chip
->ssc
->irq
;
900 spin_lock_init(&chip
->lock
);
901 mutex_init(&chip
->mixer_lock
);
905 retval
= request_irq(irq
, snd_at73c213_interrupt
, 0, "at73c213", chip
);
907 dev_dbg(&chip
->spi
->dev
, "unable to request irq %d\n", irq
);
912 memcpy(&chip
->reg_image
, &snd_at73c213_original_image
,
913 sizeof(snd_at73c213_original_image
));
915 retval
= snd_at73c213_ssc_init(chip
);
919 retval
= snd_at73c213_chip_init(chip
);
923 retval
= snd_at73c213_pcm_new(chip
, 0);
927 retval
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, chip
, &ops
);
931 retval
= snd_at73c213_mixer(chip
);
935 snd_card_set_dev(card
, &spi
->dev
);
940 snd_device_free(card
, chip
);
942 free_irq(chip
->irq
, chip
);
948 static int __devinit
snd_at73c213_probe(struct spi_device
*spi
)
950 struct snd_card
*card
;
951 struct snd_at73c213
*chip
;
952 struct at73c213_board_info
*board
;
956 board
= spi
->dev
.platform_data
;
958 dev_dbg(&spi
->dev
, "no platform_data\n");
962 if (!board
->dac_clk
) {
963 dev_dbg(&spi
->dev
, "no DAC clk\n");
967 if (IS_ERR(board
->dac_clk
)) {
968 dev_dbg(&spi
->dev
, "no DAC clk\n");
969 return PTR_ERR(board
->dac_clk
);
974 /* Allocate "card" using some unused identifiers. */
975 snprintf(id
, sizeof id
, "at73c213_%d", board
->ssc_id
);
976 card
= snd_card_new(-1, id
, THIS_MODULE
, sizeof(struct snd_at73c213
));
980 chip
= card
->private_data
;
984 chip
->ssc
= ssc_request(board
->ssc_id
);
985 if (IS_ERR(chip
->ssc
)) {
986 dev_dbg(&spi
->dev
, "could not get ssc%d device\n",
988 retval
= PTR_ERR(chip
->ssc
);
992 retval
= snd_at73c213_dev_init(card
, spi
);
996 strcpy(card
->driver
, "at73c213");
997 strcpy(card
->shortname
, board
->shortname
);
998 sprintf(card
->longname
, "%s on irq %d", card
->shortname
, chip
->irq
);
1000 retval
= snd_card_register(card
);
1004 dev_set_drvdata(&spi
->dev
, card
);
1009 ssc_free(chip
->ssc
);
1011 snd_card_free(card
);
1016 static int __devexit
snd_at73c213_remove(struct spi_device
*spi
)
1018 struct snd_card
*card
= dev_get_drvdata(&spi
->dev
);
1019 struct snd_at73c213
*chip
= card
->private_data
;
1022 /* Stop playback. */
1023 ssc_writel(chip
->ssc
->regs
, CR
, SSC_BIT(CR_TXDIS
));
1026 retval
= snd_at73c213_write_reg(chip
, DAC_LMPG
, 0x3f);
1029 retval
= snd_at73c213_write_reg(chip
, DAC_RMPG
, 0x3f);
1032 retval
= snd_at73c213_write_reg(chip
, DAC_LLOG
, 0x3f);
1035 retval
= snd_at73c213_write_reg(chip
, DAC_RLOG
, 0x3f);
1038 retval
= snd_at73c213_write_reg(chip
, DAC_LLIG
, 0x11);
1041 retval
= snd_at73c213_write_reg(chip
, DAC_RLIG
, 0x11);
1044 retval
= snd_at73c213_write_reg(chip
, DAC_AUXG
, 0x11);
1049 retval
= snd_at73c213_write_reg(chip
, PA_CTRL
,
1050 chip
->reg_image
[PA_CTRL
] | 0x0f);
1054 retval
= snd_at73c213_write_reg(chip
, PA_CTRL
,
1055 (1 << PA_CTRL_APALP
) | 0x0f);
1059 /* Turn off external DAC. */
1060 retval
= snd_at73c213_write_reg(chip
, DAC_CTRL
, 0x0c);
1064 retval
= snd_at73c213_write_reg(chip
, DAC_CTRL
, 0x00);
1068 /* Turn off master power. */
1069 retval
= snd_at73c213_write_reg(chip
, DAC_PRECH
, 0x00);
1074 /* Stop DAC master clock. */
1075 clk_disable(chip
->board
->dac_clk
);
1077 ssc_free(chip
->ssc
);
1078 snd_card_free(card
);
1079 dev_set_drvdata(&spi
->dev
, NULL
);
1085 static int snd_at73c213_suspend(struct spi_device
*spi
, pm_message_t msg
)
1087 struct snd_card
*card
= dev_get_drvdata(&spi
->dev
);
1088 struct snd_at73c213
*chip
= card
->private_data
;
1090 ssc_writel(chip
->ssc
->regs
, CR
, SSC_BIT(CR_TXDIS
));
1091 clk_disable(chip
->board
->dac_clk
);
1096 static int snd_at73c213_resume(struct spi_device
*spi
)
1098 struct snd_card
*card
= dev_get_drvdata(&spi
->dev
);
1099 struct snd_at73c213
*chip
= card
->private_data
;
1101 clk_enable(chip
->board
->dac_clk
);
1102 ssc_writel(chip
->ssc
->regs
, CR
, SSC_BIT(CR_TXEN
));
1107 #define snd_at73c213_suspend NULL
1108 #define snd_at73c213_resume NULL
1111 static struct spi_driver at73c213_driver
= {
1115 .probe
= snd_at73c213_probe
,
1116 .suspend
= snd_at73c213_suspend
,
1117 .resume
= snd_at73c213_resume
,
1118 .remove
= __devexit_p(snd_at73c213_remove
),
1121 static int __init
at73c213_init(void)
1123 return spi_register_driver(&at73c213_driver
);
1125 module_init(at73c213_init
);
1127 static void __exit
at73c213_exit(void)
1129 spi_unregister_driver(&at73c213_driver
);
1131 module_exit(at73c213_exit
);
1133 MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
1134 MODULE_DESCRIPTION("Sound driver for AT73C213 with Atmel SSC");
1135 MODULE_LICENSE("GPL");