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/platform_device.h>
24 #include <sound/driver.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 registers against concurrent access. */
83 #define get_chip(card) ((struct snd_at73c213 *)card->private_data)
86 snd_at73c213_write_reg(struct snd_at73c213
*chip
, u8 reg
, u8 val
)
88 struct spi_message msg
;
89 struct spi_transfer msg_xfer
= {
95 spi_message_init(&msg
);
97 chip
->spi_wbuffer
[0] = reg
;
98 chip
->spi_wbuffer
[1] = val
;
100 msg_xfer
.tx_buf
= chip
->spi_wbuffer
;
101 msg_xfer
.rx_buf
= chip
->spi_rbuffer
;
102 spi_message_add_tail(&msg_xfer
, &msg
);
104 retval
= spi_sync(chip
->spi
, &msg
);
107 chip
->reg_image
[reg
] = val
;
112 static struct snd_pcm_hardware snd_at73c213_playback_hw
= {
113 .info
= SNDRV_PCM_INFO_INTERLEAVED
|
114 SNDRV_PCM_INFO_BLOCK_TRANSFER
,
115 .formats
= SNDRV_PCM_FMTBIT_S16_BE
,
116 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
117 .rate_min
= 8000, /* Replaced by chip->bitrate later. */
118 .rate_max
= 50000, /* Replaced by chip->bitrate later. */
121 .buffer_bytes_max
= 64 * 1024 - 1,
122 .period_bytes_min
= 512,
123 .period_bytes_max
= 64 * 1024 - 1,
129 * Calculate and set bitrate and divisions.
131 static int snd_at73c213_set_bitrate(struct snd_at73c213
*chip
)
133 unsigned long ssc_rate
= clk_get_rate(chip
->ssc
->clk
);
134 unsigned long dac_rate_new
, ssc_div
, status
;
135 unsigned long ssc_div_max
, ssc_div_min
;
139 * We connect two clocks here, picking divisors so the I2S clocks
140 * out data at the same rate the DAC clocks it in ... and as close
141 * as practical to the desired target rate.
143 * The DAC master clock (MCLK) is programmable, and is either 256
144 * or (not here) 384 times the I2S output clock (BCLK).
147 /* SSC clock / (bitrate * stereo * 16-bit). */
148 ssc_div
= ssc_rate
/ (BITRATE_TARGET
* 2 * 16);
149 ssc_div_min
= ssc_rate
/ (BITRATE_MAX
* 2 * 16);
150 ssc_div_max
= ssc_rate
/ (BITRATE_MIN
* 2 * 16);
151 max_tries
= (ssc_div_max
- ssc_div_min
) / 2;
156 /* ssc_div must be a power of 2. */
157 ssc_div
= (ssc_div
+ 1) & ~1UL;
159 if ((ssc_rate
/ (ssc_div
* 2 * 16)) < BITRATE_MIN
) {
161 if ((ssc_rate
/ (ssc_div
* 2 * 16)) > BITRATE_MAX
)
165 /* Search for a possible bitrate. */
167 /* SSC clock / (ssc divider * 16-bit * stereo). */
168 if ((ssc_rate
/ (ssc_div
* 2 * 16)) < BITRATE_MIN
)
171 /* 256 / (2 * 16) = 8 */
172 dac_rate_new
= 8 * (ssc_rate
/ ssc_div
);
174 status
= clk_round_rate(chip
->board
->dac_clk
, dac_rate_new
);
178 /* Ignore difference smaller than 256 Hz. */
179 if ((status
/256) == (dac_rate_new
/256))
183 } while (--max_tries
);
185 /* Not able to find a valid bitrate. */
189 status
= clk_set_rate(chip
->board
->dac_clk
, status
);
193 /* Set divider in SSC device. */
194 ssc_writel(chip
->ssc
->regs
, CMR
, ssc_div
/2);
196 /* SSC clock / (ssc divider * 16-bit * stereo). */
197 chip
->bitrate
= ssc_rate
/ (ssc_div
* 16 * 2);
199 dev_info(&chip
->spi
->dev
,
200 "at73c213: supported bitrate is %lu (%lu divider)\n",
201 chip
->bitrate
, ssc_div
);
206 static int snd_at73c213_pcm_open(struct snd_pcm_substream
*substream
)
208 struct snd_at73c213
*chip
= snd_pcm_substream_chip(substream
);
209 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
211 snd_at73c213_playback_hw
.rate_min
= chip
->bitrate
;
212 snd_at73c213_playback_hw
.rate_max
= chip
->bitrate
;
213 runtime
->hw
= snd_at73c213_playback_hw
;
214 chip
->substream
= substream
;
219 static int snd_at73c213_pcm_close(struct snd_pcm_substream
*substream
)
221 struct snd_at73c213
*chip
= snd_pcm_substream_chip(substream
);
222 chip
->substream
= NULL
;
226 static int snd_at73c213_pcm_hw_params(struct snd_pcm_substream
*substream
,
227 struct snd_pcm_hw_params
*hw_params
)
229 return snd_pcm_lib_malloc_pages(substream
,
230 params_buffer_bytes(hw_params
));
233 static int snd_at73c213_pcm_hw_free(struct snd_pcm_substream
*substream
)
235 return snd_pcm_lib_free_pages(substream
);
238 static int snd_at73c213_pcm_prepare(struct snd_pcm_substream
*substream
)
240 struct snd_at73c213
*chip
= snd_pcm_substream_chip(substream
);
241 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
244 block_size
= frames_to_bytes(runtime
, runtime
->period_size
);
248 ssc_writel(chip
->ssc
->regs
, PDC_TPR
,
249 (long)runtime
->dma_addr
);
250 ssc_writel(chip
->ssc
->regs
, PDC_TCR
, runtime
->period_size
* 2);
251 ssc_writel(chip
->ssc
->regs
, PDC_TNPR
,
252 (long)runtime
->dma_addr
+ block_size
);
253 ssc_writel(chip
->ssc
->regs
, PDC_TNCR
, runtime
->period_size
* 2);
258 static int snd_at73c213_pcm_trigger(struct snd_pcm_substream
*substream
,
261 struct snd_at73c213
*chip
= snd_pcm_substream_chip(substream
);
264 spin_lock(&chip
->lock
);
267 case SNDRV_PCM_TRIGGER_START
:
268 ssc_writel(chip
->ssc
->regs
, IER
, SSC_BIT(IER_ENDTX
));
269 ssc_writel(chip
->ssc
->regs
, PDC_PTCR
, SSC_BIT(PDC_PTCR_TXTEN
));
271 case SNDRV_PCM_TRIGGER_STOP
:
272 ssc_writel(chip
->ssc
->regs
, PDC_PTCR
, SSC_BIT(PDC_PTCR_TXTDIS
));
273 ssc_writel(chip
->ssc
->regs
, IDR
, SSC_BIT(IDR_ENDTX
));
276 dev_dbg(&chip
->spi
->dev
, "spurious command %x\n", cmd
);
281 spin_unlock(&chip
->lock
);
286 static snd_pcm_uframes_t
287 snd_at73c213_pcm_pointer(struct snd_pcm_substream
*substream
)
289 struct snd_at73c213
*chip
= snd_pcm_substream_chip(substream
);
290 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
291 snd_pcm_uframes_t pos
;
294 bytes
= ssc_readl(chip
->ssc
->regs
, PDC_TPR
)
295 - (unsigned long)runtime
->dma_addr
;
297 pos
= bytes_to_frames(runtime
, bytes
);
298 if (pos
>= runtime
->buffer_size
)
299 pos
-= runtime
->buffer_size
;
304 static struct snd_pcm_ops at73c213_playback_ops
= {
305 .open
= snd_at73c213_pcm_open
,
306 .close
= snd_at73c213_pcm_close
,
307 .ioctl
= snd_pcm_lib_ioctl
,
308 .hw_params
= snd_at73c213_pcm_hw_params
,
309 .hw_free
= snd_at73c213_pcm_hw_free
,
310 .prepare
= snd_at73c213_pcm_prepare
,
311 .trigger
= snd_at73c213_pcm_trigger
,
312 .pointer
= snd_at73c213_pcm_pointer
,
315 static void snd_at73c213_pcm_free(struct snd_pcm
*pcm
)
317 struct snd_at73c213
*chip
= snd_pcm_chip(pcm
);
319 snd_pcm_lib_preallocate_free_for_all(chip
->pcm
);
324 static int __devinit
snd_at73c213_pcm_new(struct snd_at73c213
*chip
, int device
)
329 retval
= snd_pcm_new(chip
->card
, chip
->card
->shortname
,
334 pcm
->private_data
= chip
;
335 pcm
->private_free
= snd_at73c213_pcm_free
;
336 pcm
->info_flags
= SNDRV_PCM_INFO_BLOCK_TRANSFER
;
337 strcpy(pcm
->name
, "at73c213");
340 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &at73c213_playback_ops
);
342 retval
= snd_pcm_lib_preallocate_pages_for_all(chip
->pcm
,
343 SNDRV_DMA_TYPE_DEV
, &chip
->ssc
->pdev
->dev
,
344 64 * 1024, 64 * 1024);
349 static irqreturn_t
snd_at73c213_interrupt(int irq
, void *dev_id
)
351 struct snd_at73c213
*chip
= dev_id
;
352 struct snd_pcm_runtime
*runtime
= chip
->substream
->runtime
;
357 int retval
= IRQ_NONE
;
359 spin_lock(&chip
->lock
);
361 block_size
= frames_to_bytes(runtime
, runtime
->period_size
);
362 status
= ssc_readl(chip
->ssc
->regs
, IMR
);
364 if (status
& SSC_BIT(IMR_ENDTX
)) {
366 if (chip
->period
== runtime
->periods
)
368 next_period
= chip
->period
+ 1;
369 if (next_period
== runtime
->periods
)
372 offset
= block_size
* next_period
;
374 ssc_writel(chip
->ssc
->regs
, PDC_TNPR
,
375 (long)runtime
->dma_addr
+ offset
);
376 ssc_writel(chip
->ssc
->regs
, PDC_TNCR
, runtime
->period_size
* 2);
377 retval
= IRQ_HANDLED
;
380 ssc_readl(chip
->ssc
->regs
, IMR
);
381 spin_unlock(&chip
->lock
);
383 if (status
& SSC_BIT(IMR_ENDTX
))
384 snd_pcm_period_elapsed(chip
->substream
);
392 static int snd_at73c213_mono_get(struct snd_kcontrol
*kcontrol
,
393 struct snd_ctl_elem_value
*ucontrol
)
395 struct snd_at73c213
*chip
= snd_kcontrol_chip(kcontrol
);
396 int reg
= kcontrol
->private_value
& 0xff;
397 int shift
= (kcontrol
->private_value
>> 8) & 0xff;
398 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
399 int invert
= (kcontrol
->private_value
>> 24) & 0xff;
401 spin_lock_irq(&chip
->lock
);
403 ucontrol
->value
.integer
.value
[0] =
404 (chip
->reg_image
[reg
] >> shift
) & mask
;
407 ucontrol
->value
.integer
.value
[0] =
408 mask
- ucontrol
->value
.integer
.value
[0];
410 spin_unlock_irq(&chip
->lock
);
415 static int snd_at73c213_mono_put(struct snd_kcontrol
*kcontrol
,
416 struct snd_ctl_elem_value
*ucontrol
)
418 struct snd_at73c213
*chip
= snd_kcontrol_chip(kcontrol
);
419 int reg
= kcontrol
->private_value
& 0xff;
420 int shift
= (kcontrol
->private_value
>> 8) & 0xff;
421 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
422 int invert
= (kcontrol
->private_value
>> 24) & 0xff;
426 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
431 spin_lock_irq(&chip
->lock
);
433 val
= (chip
->reg_image
[reg
] & ~(mask
<< shift
)) | val
;
434 change
= val
!= chip
->reg_image
[reg
];
435 retval
= snd_at73c213_write_reg(chip
, reg
, val
);
437 spin_unlock_irq(&chip
->lock
);
445 static int snd_at73c213_stereo_info(struct snd_kcontrol
*kcontrol
,
446 struct snd_ctl_elem_info
*uinfo
)
448 int mask
= (kcontrol
->private_value
>> 24) & 0xff;
451 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
453 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
456 uinfo
->value
.integer
.min
= 0;
457 uinfo
->value
.integer
.max
= mask
;
462 static int snd_at73c213_stereo_get(struct snd_kcontrol
*kcontrol
,
463 struct snd_ctl_elem_value
*ucontrol
)
465 struct snd_at73c213
*chip
= snd_kcontrol_chip(kcontrol
);
466 int left_reg
= kcontrol
->private_value
& 0xff;
467 int right_reg
= (kcontrol
->private_value
>> 8) & 0xff;
468 int shift_left
= (kcontrol
->private_value
>> 16) & 0x07;
469 int shift_right
= (kcontrol
->private_value
>> 19) & 0x07;
470 int mask
= (kcontrol
->private_value
>> 24) & 0xff;
471 int invert
= (kcontrol
->private_value
>> 22) & 1;
473 spin_lock_irq(&chip
->lock
);
475 ucontrol
->value
.integer
.value
[0] =
476 (chip
->reg_image
[left_reg
] >> shift_left
) & mask
;
477 ucontrol
->value
.integer
.value
[1] =
478 (chip
->reg_image
[right_reg
] >> shift_right
) & mask
;
481 ucontrol
->value
.integer
.value
[0] =
482 mask
- ucontrol
->value
.integer
.value
[0];
483 ucontrol
->value
.integer
.value
[1] =
484 mask
- ucontrol
->value
.integer
.value
[1];
487 spin_unlock_irq(&chip
->lock
);
492 static int snd_at73c213_stereo_put(struct snd_kcontrol
*kcontrol
,
493 struct snd_ctl_elem_value
*ucontrol
)
495 struct snd_at73c213
*chip
= snd_kcontrol_chip(kcontrol
);
496 int left_reg
= kcontrol
->private_value
& 0xff;
497 int right_reg
= (kcontrol
->private_value
>> 8) & 0xff;
498 int shift_left
= (kcontrol
->private_value
>> 16) & 0x07;
499 int shift_right
= (kcontrol
->private_value
>> 19) & 0x07;
500 int mask
= (kcontrol
->private_value
>> 24) & 0xff;
501 int invert
= (kcontrol
->private_value
>> 22) & 1;
503 unsigned short val1
, val2
;
505 val1
= ucontrol
->value
.integer
.value
[0] & mask
;
506 val2
= ucontrol
->value
.integer
.value
[1] & mask
;
512 val2
<<= shift_right
;
514 spin_lock_irq(&chip
->lock
);
516 val1
= (chip
->reg_image
[left_reg
] & ~(mask
<< shift_left
)) | val1
;
517 val2
= (chip
->reg_image
[right_reg
] & ~(mask
<< shift_right
)) | val2
;
518 change
= val1
!= chip
->reg_image
[left_reg
]
519 || val2
!= chip
->reg_image
[right_reg
];
520 retval
= snd_at73c213_write_reg(chip
, left_reg
, val1
);
522 spin_unlock_irq(&chip
->lock
);
525 retval
= snd_at73c213_write_reg(chip
, right_reg
, val2
);
527 spin_unlock_irq(&chip
->lock
);
531 spin_unlock_irq(&chip
->lock
);
539 static int snd_at73c213_mono_switch_info(struct snd_kcontrol
*kcontrol
,
540 struct snd_ctl_elem_info
*uinfo
)
542 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
544 uinfo
->value
.integer
.min
= 0;
545 uinfo
->value
.integer
.max
= 1;
550 static int snd_at73c213_mono_switch_get(struct snd_kcontrol
*kcontrol
,
551 struct snd_ctl_elem_value
*ucontrol
)
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;
558 spin_lock_irq(&chip
->lock
);
560 ucontrol
->value
.integer
.value
[0] =
561 (chip
->reg_image
[reg
] >> shift
) & 0x01;
564 ucontrol
->value
.integer
.value
[0] =
565 0x01 - ucontrol
->value
.integer
.value
[0];
567 spin_unlock_irq(&chip
->lock
);
572 static int snd_at73c213_mono_switch_put(struct snd_kcontrol
*kcontrol
,
573 struct snd_ctl_elem_value
*ucontrol
)
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;
583 if (ucontrol
->value
.integer
.value
[0])
592 spin_lock_irq(&chip
->lock
);
594 val
|= (chip
->reg_image
[reg
] & ~(mask
<< shift
));
595 change
= val
!= chip
->reg_image
[reg
];
597 retval
= snd_at73c213_write_reg(chip
, reg
, val
);
599 spin_unlock_irq(&chip
->lock
);
607 static int snd_at73c213_pa_volume_info(struct snd_kcontrol
*kcontrol
,
608 struct snd_ctl_elem_info
*uinfo
)
610 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
612 uinfo
->value
.integer
.min
= 0;
613 uinfo
->value
.integer
.max
= ((kcontrol
->private_value
>> 16) & 0xff) - 1;
618 static int snd_at73c213_line_capture_volume_info(
619 struct snd_kcontrol
*kcontrol
,
620 struct snd_ctl_elem_info
*uinfo
)
622 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
624 /* When inverted will give values 0x10001 => 0. */
625 uinfo
->value
.integer
.min
= 14;
626 uinfo
->value
.integer
.max
= 31;
631 static int snd_at73c213_aux_capture_volume_info(
632 struct snd_kcontrol
*kcontrol
,
633 struct snd_ctl_elem_info
*uinfo
)
635 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
637 /* When inverted will give values 0x10001 => 0. */
638 uinfo
->value
.integer
.min
= 14;
639 uinfo
->value
.integer
.max
= 31;
644 #define AT73C213_MONO_SWITCH(xname, xindex, reg, shift, mask, invert) \
646 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
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)) \
655 #define AT73C213_STEREO(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
657 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
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)) \
668 static struct snd_kcontrol_new snd_at73c213_controls
[] __devinitdata
= {
669 AT73C213_STEREO("Master Playback Volume", 0, DAC_LMPG
, DAC_RMPG
, 0, 0, 0x1f, 1),
670 AT73C213_STEREO("Master Playback Switch", 0, DAC_LMPG
, DAC_RMPG
, 5, 5, 1, 1),
671 AT73C213_STEREO("PCM Playback Volume", 0, DAC_LLOG
, DAC_RLOG
, 0, 0, 0x1f, 1),
672 AT73C213_STEREO("PCM Playback Switch", 0, DAC_LLOG
, DAC_RLOG
, 5, 5, 1, 1),
673 AT73C213_MONO_SWITCH("Mono PA Playback Switch", 0, DAC_CTRL
, DAC_CTRL_ONPADRV
,
676 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
677 .name
= "PA Playback Volume",
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),
685 AT73C213_MONO_SWITCH("PA High Gain Playback Switch", 0, PA_CTRL
, PA_CTRL_APALP
,
687 AT73C213_MONO_SWITCH("PA Playback Switch", 0, PA_CTRL
, PA_CTRL_APAON
, 0x01, 0),
689 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
690 .name
= "Aux Capture Volume",
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),
697 AT73C213_MONO_SWITCH("Aux Capture Switch", 0, DAC_CTRL
, DAC_CTRL_ONAUXIN
,
700 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
701 .name
= "Line Capture Volume",
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),
709 AT73C213_MONO_SWITCH("Line Capture Switch", 0, DAC_CTRL
, 0, 0x03, 0),
712 static int __devinit
snd_at73c213_mixer(struct snd_at73c213
*chip
)
714 struct snd_card
*card
;
717 if (chip
== NULL
|| chip
->pcm
== NULL
)
722 strcpy(card
->mixername
, chip
->pcm
->name
);
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
],
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
);
739 snd_ctl_remove(card
, kctl
);
747 static int snd_at73c213_ssc_init(struct snd_at73c213
*chip
)
750 * Continuous clock output.
751 * Starts on falling TF.
752 * Delay 1 cycle (1 bit).
753 * Periode is 16 bit (16 - 1).
755 ssc_writel(chip
->ssc
->regs
, TCMR
,
757 | SSC_BF(TCMR_START
, 4)
758 | SSC_BF(TCMR_STTDLY
, 1)
759 | SSC_BF(TCMR_PERIOD
, 16 - 1));
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.
767 ssc_writel(chip
->ssc
->regs
, TFMR
,
768 SSC_BF(TFMR_DATLEN
, 16 - 1)
770 | SSC_BF(TFMR_DATNB
, 1)
771 | SSC_BF(TFMR_FSLEN
, 16 - 1)
772 | SSC_BF(TFMR_FSOS
, 1));
777 static int snd_at73c213_chip_init(struct snd_at73c213
*chip
)
780 unsigned char dac_ctrl
= 0;
782 retval
= snd_at73c213_set_bitrate(chip
);
786 /* Enable DAC master clock. */
787 clk_enable(chip
->board
->dac_clk
);
789 /* Initialize at73c213 on SPI bus. */
790 retval
= snd_at73c213_write_reg(chip
, DAC_RST
, 0x04);
794 retval
= snd_at73c213_write_reg(chip
, DAC_RST
, 0x03);
798 /* Precharge everything. */
799 retval
= snd_at73c213_write_reg(chip
, DAC_PRECH
, 0xff);
802 retval
= snd_at73c213_write_reg(chip
, PA_CTRL
, (1<<PA_CTRL_APAPRECH
));
805 retval
= snd_at73c213_write_reg(chip
, DAC_CTRL
,
806 (1<<DAC_CTRL_ONLNOL
) | (1<<DAC_CTRL_ONLNOR
));
812 /* Stop precharging PA. */
813 retval
= snd_at73c213_write_reg(chip
, PA_CTRL
,
814 (1<<PA_CTRL_APALP
) | 0x0f);
820 /* Stop precharging DAC, turn on master power. */
821 retval
= snd_at73c213_write_reg(chip
, DAC_PRECH
, (1<<DAC_PRECH_ONMSTR
));
828 dac_ctrl
= (1<<DAC_CTRL_ONDACL
) | (1<<DAC_CTRL_ONDACR
)
829 | (1<<DAC_CTRL_ONLNOL
) | (1<<DAC_CTRL_ONLNOR
);
831 retval
= snd_at73c213_write_reg(chip
, DAC_CTRL
, dac_ctrl
);
836 retval
= snd_at73c213_write_reg(chip
, DAC_LMPG
, 0x3f);
839 retval
= snd_at73c213_write_reg(chip
, DAC_RMPG
, 0x3f);
842 retval
= snd_at73c213_write_reg(chip
, DAC_LLOG
, 0x3f);
845 retval
= snd_at73c213_write_reg(chip
, DAC_RLOG
, 0x3f);
848 retval
= snd_at73c213_write_reg(chip
, DAC_LLIG
, 0x11);
851 retval
= snd_at73c213_write_reg(chip
, DAC_RLIG
, 0x11);
854 retval
= snd_at73c213_write_reg(chip
, DAC_AUXG
, 0x11);
858 /* Enable I2S device, i.e. clock output. */
859 ssc_writel(chip
->ssc
->regs
, CR
, SSC_BIT(CR_TXEN
));
864 clk_disable(chip
->board
->dac_clk
);
869 static int snd_at73c213_dev_free(struct snd_device
*device
)
871 struct snd_at73c213
*chip
= device
->device_data
;
873 ssc_writel(chip
->ssc
->regs
, CR
, SSC_BIT(CR_TXDIS
));
874 if (chip
->irq
>= 0) {
875 free_irq(chip
->irq
, chip
);
882 static int __devinit
snd_at73c213_dev_init(struct snd_card
*card
,
883 struct spi_device
*spi
)
885 static struct snd_device_ops ops
= {
886 .dev_free
= snd_at73c213_dev_free
,
888 struct snd_at73c213
*chip
= get_chip(card
);
891 irq
= chip
->ssc
->irq
;
895 spin_lock_init(&chip
->lock
);
899 retval
= request_irq(irq
, snd_at73c213_interrupt
, 0, "at73c213", chip
);
901 dev_dbg(&chip
->spi
->dev
, "unable to request irq %d\n", irq
);
906 memcpy(&chip
->reg_image
, &snd_at73c213_original_image
,
907 sizeof(snd_at73c213_original_image
));
909 retval
= snd_at73c213_ssc_init(chip
);
913 retval
= snd_at73c213_chip_init(chip
);
917 retval
= snd_at73c213_pcm_new(chip
, 0);
921 retval
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, chip
, &ops
);
925 retval
= snd_at73c213_mixer(chip
);
929 snd_card_set_dev(card
, &spi
->dev
);
934 snd_device_free(card
, chip
);
936 free_irq(chip
->irq
, chip
);
942 static int snd_at73c213_probe(struct spi_device
*spi
)
944 struct snd_card
*card
;
945 struct snd_at73c213
*chip
;
946 struct at73c213_board_info
*board
;
950 board
= spi
->dev
.platform_data
;
952 dev_dbg(&spi
->dev
, "no platform_data\n");
956 if (!board
->dac_clk
) {
957 dev_dbg(&spi
->dev
, "no DAC clk\n");
961 if (IS_ERR(board
->dac_clk
)) {
962 dev_dbg(&spi
->dev
, "no DAC clk\n");
963 return PTR_ERR(board
->dac_clk
);
968 /* Allocate "card" using some unused identifiers. */
969 snprintf(id
, sizeof id
, "at73c213_%d", board
->ssc_id
);
970 card
= snd_card_new(-1, id
, THIS_MODULE
, sizeof(struct snd_at73c213
));
974 chip
= card
->private_data
;
978 chip
->ssc
= ssc_request(board
->ssc_id
);
979 if (IS_ERR(chip
->ssc
)) {
980 dev_dbg(&spi
->dev
, "could not get ssc%d device\n",
982 retval
= PTR_ERR(chip
->ssc
);
986 retval
= snd_at73c213_dev_init(card
, spi
);
990 strcpy(card
->driver
, "at73c213");
991 strcpy(card
->shortname
, board
->shortname
);
992 sprintf(card
->longname
, "%s on irq %d", card
->shortname
, chip
->irq
);
994 retval
= snd_card_register(card
);
998 dev_set_drvdata(&spi
->dev
, card
);
1003 ssc_free(chip
->ssc
);
1005 snd_card_free(card
);
1010 static int __devexit
snd_at73c213_remove(struct spi_device
*spi
)
1012 struct snd_card
*card
= dev_get_drvdata(&spi
->dev
);
1013 struct snd_at73c213
*chip
= card
->private_data
;
1016 /* Stop playback. */
1017 ssc_writel(chip
->ssc
->regs
, CR
, SSC_BIT(CR_TXDIS
));
1020 retval
= snd_at73c213_write_reg(chip
, DAC_LMPG
, 0x3f);
1023 retval
= snd_at73c213_write_reg(chip
, DAC_RMPG
, 0x3f);
1026 retval
= snd_at73c213_write_reg(chip
, DAC_LLOG
, 0x3f);
1029 retval
= snd_at73c213_write_reg(chip
, DAC_RLOG
, 0x3f);
1032 retval
= snd_at73c213_write_reg(chip
, DAC_LLIG
, 0x11);
1035 retval
= snd_at73c213_write_reg(chip
, DAC_RLIG
, 0x11);
1038 retval
= snd_at73c213_write_reg(chip
, DAC_AUXG
, 0x11);
1043 retval
= snd_at73c213_write_reg(chip
, PA_CTRL
,
1044 chip
->reg_image
[PA_CTRL
] | 0x0f);
1048 retval
= snd_at73c213_write_reg(chip
, PA_CTRL
,
1049 (1 << PA_CTRL_APALP
) | 0x0f);
1053 /* Turn off external DAC. */
1054 retval
= snd_at73c213_write_reg(chip
, DAC_CTRL
, 0x0c);
1058 retval
= snd_at73c213_write_reg(chip
, DAC_CTRL
, 0x00);
1062 /* Turn off master power. */
1063 retval
= snd_at73c213_write_reg(chip
, DAC_PRECH
, 0x00);
1068 /* Stop DAC master clock. */
1069 clk_disable(chip
->board
->dac_clk
);
1071 ssc_free(chip
->ssc
);
1072 snd_card_free(card
);
1073 dev_set_drvdata(&spi
->dev
, NULL
);
1079 static int snd_at73c213_suspend(struct spi_device
*spi
, pm_message_t msg
)
1081 struct snd_card
*card
= dev_get_drvdata(&spi
->dev
);
1082 struct snd_at73c213
*chip
= card
->private_data
;
1084 ssc_writel(chip
->ssc
->regs
, CR
, SSC_BIT(CR_TXDIS
));
1085 clk_disable(chip
->board
->dac_clk
);
1090 static int snd_at73c213_resume(struct spi_device
*spi
)
1092 struct snd_card
*card
= dev_get_drvdata(&spi
->dev
);
1093 struct snd_at73c213
*chip
= card
->private_data
;
1095 clk_enable(chip
->board
->dac_clk
);
1096 ssc_writel(chip
->ssc
->regs
, CR
, SSC_BIT(CR_TXEN
));
1101 #define snd_at73c213_suspend NULL
1102 #define snd_at73c213_resume NULL
1105 static struct spi_driver at73c213_driver
= {
1109 .probe
= snd_at73c213_probe
,
1110 .suspend
= snd_at73c213_suspend
,
1111 .resume
= snd_at73c213_resume
,
1112 .remove
= __devexit_p(snd_at73c213_remove
),
1115 static int __init
at73c213_init(void)
1117 return spi_register_driver(&at73c213_driver
);
1119 module_init(at73c213_init
);
1121 static void __exit
at73c213_exit(void)
1123 spi_unregister_driver(&at73c213_driver
);
1125 module_exit(at73c213_exit
);
1127 MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
1128 MODULE_DESCRIPTION("Sound driver for AT73C213 with Atmel SSC");
1129 MODULE_LICENSE("GPL");