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
;
215 /* ensure buffer_size is a multiple of period_size */
216 err
= snd_pcm_hw_constraint_integer(runtime
,
217 SNDRV_PCM_HW_PARAM_PERIODS
);
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
;
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
;
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
);
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
;
261 block_size
= frames_to_bytes(runtime
, runtime
->period_size
);
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
);
277 static int snd_at73c213_pcm_trigger(struct snd_pcm_substream
*substream
,
280 struct snd_at73c213
*chip
= snd_pcm_substream_chip(substream
);
283 spin_lock(&chip
->lock
);
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
));
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
));
295 dev_dbg(&chip
->spi
->dev
, "spurious command %x\n", cmd
);
300 spin_unlock(&chip
->lock
);
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
;
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
;
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
)
339 retval
= snd_pcm_new(chip
->card
, chip
->card
->shortname
,
344 pcm
->private_data
= chip
;
345 pcm
->info_flags
= SNDRV_PCM_INFO_BLOCK_TRANSFER
;
346 strcpy(pcm
->name
, "at73c213");
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);
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
;
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
)) {
375 if (chip
->period
== runtime
->periods
)
377 next_period
= chip
->period
+ 1;
378 if (next_period
== runtime
->periods
)
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
);
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
;
417 ucontrol
->value
.integer
.value
[0] =
418 mask
- ucontrol
->value
.integer
.value
[0];
420 mutex_unlock(&chip
->mixer_lock
);
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;
436 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
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
);
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;
461 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
463 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
466 uinfo
->value
.integer
.min
= 0;
467 uinfo
->value
.integer
.max
= mask
;
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
;
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
);
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;
513 unsigned short val1
, val2
;
515 val1
= ucontrol
->value
.integer
.value
[0] & mask
;
516 val2
= ucontrol
->value
.integer
.value
[1] & mask
;
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
);
532 mutex_unlock(&chip
->mixer_lock
);
535 retval
= snd_at73c213_write_reg(chip
, right_reg
, val2
);
537 mutex_unlock(&chip
->mixer_lock
);
541 mutex_unlock(&chip
->mixer_lock
);
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;
565 ucontrol
->value
.integer
.value
[0] =
566 0x01 - ucontrol
->value
.integer
.value
[0];
568 mutex_unlock(&chip
->mixer_lock
);
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;
584 if (ucontrol
->value
.integer
.value
[0])
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
);
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
;
613 uinfo
->value
.integer
.min
= 0;
614 uinfo
->value
.integer
.max
= ((kcontrol
->private_value
>> 16) & 0xff) - 1;
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
;
625 /* When inverted will give values 0x10001 => 0. */
626 uinfo
->value
.integer
.min
= 14;
627 uinfo
->value
.integer
.max
= 31;
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
;
638 /* When inverted will give values 0x10001 => 0. */
639 uinfo
->value
.integer
.min
= 14;
640 uinfo
->value
.integer
.max
= 31;
645 #define AT73C213_MONO_SWITCH(xname, xindex, reg, shift, mask, invert) \
647 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
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, \
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
,
677 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
678 .name
= "PA Playback Volume",
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
,
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",
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
,
701 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
702 .name
= "Line Capture Volume",
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
;
718 if (chip
== NULL
|| chip
->pcm
== NULL
)
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
],
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
);
740 snd_ctl_remove(card
, kctl
);
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
,
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)
771 | SSC_BF(TFMR_DATNB
, 1)
772 | SSC_BF(TFMR_FSLEN
, 16 - 1)
773 | SSC_BF(TFMR_FSOS
, 1));
778 static int __devinit
snd_at73c213_chip_init(struct snd_at73c213
*chip
)
781 unsigned char dac_ctrl
= 0;
783 retval
= snd_at73c213_set_bitrate(chip
);
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);
795 retval
= snd_at73c213_write_reg(chip
, DAC_RST
, 0x03);
799 /* Precharge everything. */
800 retval
= snd_at73c213_write_reg(chip
, DAC_PRECH
, 0xff);
803 retval
= snd_at73c213_write_reg(chip
, PA_CTRL
, (1<<PA_CTRL_APAPRECH
));
806 retval
= snd_at73c213_write_reg(chip
, DAC_CTRL
,
807 (1<<DAC_CTRL_ONLNOL
) | (1<<DAC_CTRL_ONLNOR
));
813 /* Stop precharging PA. */
814 retval
= snd_at73c213_write_reg(chip
, PA_CTRL
,
815 (1<<PA_CTRL_APALP
) | 0x0f);
821 /* Stop precharging DAC, turn on master power. */
822 retval
= snd_at73c213_write_reg(chip
, DAC_PRECH
, (1<<DAC_PRECH_ONMSTR
));
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
);
837 retval
= snd_at73c213_write_reg(chip
, DAC_LMPG
, 0x3f);
840 retval
= snd_at73c213_write_reg(chip
, DAC_RMPG
, 0x3f);
843 retval
= snd_at73c213_write_reg(chip
, DAC_LLOG
, 0x3f);
846 retval
= snd_at73c213_write_reg(chip
, DAC_RLOG
, 0x3f);
849 retval
= snd_at73c213_write_reg(chip
, DAC_LLIG
, 0x11);
852 retval
= snd_at73c213_write_reg(chip
, DAC_RLIG
, 0x11);
855 retval
= snd_at73c213_write_reg(chip
, DAC_AUXG
, 0x11);
859 /* Enable I2S device, i.e. clock output. */
860 ssc_writel(chip
->ssc
->regs
, CR
, SSC_BIT(CR_TXEN
));
865 clk_disable(chip
->board
->dac_clk
);
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
);
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
);
892 irq
= chip
->ssc
->irq
;
896 spin_lock_init(&chip
->lock
);
897 mutex_init(&chip
->mixer_lock
);
901 retval
= request_irq(irq
, snd_at73c213_interrupt
, 0, "at73c213", chip
);
903 dev_dbg(&chip
->spi
->dev
, "unable to request irq %d\n", irq
);
908 memcpy(&chip
->reg_image
, &snd_at73c213_original_image
,
909 sizeof(snd_at73c213_original_image
));
911 retval
= snd_at73c213_ssc_init(chip
);
915 retval
= snd_at73c213_chip_init(chip
);
919 retval
= snd_at73c213_pcm_new(chip
, 0);
923 retval
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, chip
, &ops
);
927 retval
= snd_at73c213_mixer(chip
);
931 snd_card_set_dev(card
, &spi
->dev
);
936 snd_device_free(card
, chip
);
938 free_irq(chip
->irq
, chip
);
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
;
952 board
= spi
->dev
.platform_data
;
954 dev_dbg(&spi
->dev
, "no platform_data\n");
958 if (!board
->dac_clk
) {
959 dev_dbg(&spi
->dev
, "no DAC clk\n");
963 if (IS_ERR(board
->dac_clk
)) {
964 dev_dbg(&spi
->dev
, "no DAC clk\n");
965 return PTR_ERR(board
->dac_clk
);
970 /* Allocate "card" using some unused identifiers. */
971 snprintf(id
, sizeof id
, "at73c213_%d", board
->ssc_id
);
972 card
= snd_card_new(-1, id
, THIS_MODULE
, sizeof(struct snd_at73c213
));
976 chip
= card
->private_data
;
980 chip
->ssc
= ssc_request(board
->ssc_id
);
981 if (IS_ERR(chip
->ssc
)) {
982 dev_dbg(&spi
->dev
, "could not get ssc%d device\n",
984 retval
= PTR_ERR(chip
->ssc
);
988 retval
= snd_at73c213_dev_init(card
, spi
);
992 strcpy(card
->driver
, "at73c213");
993 strcpy(card
->shortname
, board
->shortname
);
994 sprintf(card
->longname
, "%s on irq %d", card
->shortname
, chip
->irq
);
996 retval
= snd_card_register(card
);
1000 dev_set_drvdata(&spi
->dev
, card
);
1005 ssc_free(chip
->ssc
);
1007 snd_card_free(card
);
1012 static int __devexit
snd_at73c213_remove(struct spi_device
*spi
)
1014 struct snd_card
*card
= dev_get_drvdata(&spi
->dev
);
1015 struct snd_at73c213
*chip
= card
->private_data
;
1018 /* Stop playback. */
1019 ssc_writel(chip
->ssc
->regs
, CR
, SSC_BIT(CR_TXDIS
));
1022 retval
= snd_at73c213_write_reg(chip
, DAC_LMPG
, 0x3f);
1025 retval
= snd_at73c213_write_reg(chip
, DAC_RMPG
, 0x3f);
1028 retval
= snd_at73c213_write_reg(chip
, DAC_LLOG
, 0x3f);
1031 retval
= snd_at73c213_write_reg(chip
, DAC_RLOG
, 0x3f);
1034 retval
= snd_at73c213_write_reg(chip
, DAC_LLIG
, 0x11);
1037 retval
= snd_at73c213_write_reg(chip
, DAC_RLIG
, 0x11);
1040 retval
= snd_at73c213_write_reg(chip
, DAC_AUXG
, 0x11);
1045 retval
= snd_at73c213_write_reg(chip
, PA_CTRL
,
1046 chip
->reg_image
[PA_CTRL
] | 0x0f);
1050 retval
= snd_at73c213_write_reg(chip
, PA_CTRL
,
1051 (1 << PA_CTRL_APALP
) | 0x0f);
1055 /* Turn off external DAC. */
1056 retval
= snd_at73c213_write_reg(chip
, DAC_CTRL
, 0x0c);
1060 retval
= snd_at73c213_write_reg(chip
, DAC_CTRL
, 0x00);
1064 /* Turn off master power. */
1065 retval
= snd_at73c213_write_reg(chip
, DAC_PRECH
, 0x00);
1070 /* Stop DAC master clock. */
1071 clk_disable(chip
->board
->dac_clk
);
1073 ssc_free(chip
->ssc
);
1074 snd_card_free(card
);
1075 dev_set_drvdata(&spi
->dev
, NULL
);
1081 static int snd_at73c213_suspend(struct spi_device
*spi
, pm_message_t msg
)
1083 struct snd_card
*card
= dev_get_drvdata(&spi
->dev
);
1084 struct snd_at73c213
*chip
= card
->private_data
;
1086 ssc_writel(chip
->ssc
->regs
, CR
, SSC_BIT(CR_TXDIS
));
1087 clk_disable(chip
->board
->dac_clk
);
1092 static int snd_at73c213_resume(struct spi_device
*spi
)
1094 struct snd_card
*card
= dev_get_drvdata(&spi
->dev
);
1095 struct snd_at73c213
*chip
= card
->private_data
;
1097 clk_enable(chip
->board
->dac_clk
);
1098 ssc_writel(chip
->ssc
->regs
, CR
, SSC_BIT(CR_TXEN
));
1103 #define snd_at73c213_suspend NULL
1104 #define snd_at73c213_resume NULL
1107 static struct spi_driver at73c213_driver
= {
1111 .probe
= snd_at73c213_probe
,
1112 .suspend
= snd_at73c213_suspend
,
1113 .resume
= snd_at73c213_resume
,
1114 .remove
= __devexit_p(snd_at73c213_remove
),
1117 static int __init
at73c213_init(void)
1119 return spi_register_driver(&at73c213_driver
);
1121 module_init(at73c213_init
);
1123 static void __exit
at73c213_exit(void)
1125 spi_unregister_driver(&at73c213_driver
);
1127 module_exit(at73c213_exit
);
1129 MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
1130 MODULE_DESCRIPTION("Sound driver for AT73C213 with Atmel SSC");
1131 MODULE_LICENSE("GPL");