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
;
72 struct ssc_device
*ssc
;
73 struct spi_device
*spi
;
76 /* Image of the SPI registers in AT73C213. */
78 /* Protect SSC registers against concurrent access. */
80 /* Protect mixer registers against concurrent access. */
81 struct mutex mixer_lock
;
84 #define get_chip(card) ((struct snd_at73c213 *)card->private_data)
87 snd_at73c213_write_reg(struct snd_at73c213
*chip
, u8 reg
, u8 val
)
89 struct spi_message msg
;
90 struct spi_transfer msg_xfer
= {
96 spi_message_init(&msg
);
98 chip
->spi_wbuffer
[0] = reg
;
99 chip
->spi_wbuffer
[1] = val
;
101 msg_xfer
.tx_buf
= chip
->spi_wbuffer
;
102 msg_xfer
.rx_buf
= chip
->spi_rbuffer
;
103 spi_message_add_tail(&msg_xfer
, &msg
);
105 retval
= spi_sync(chip
->spi
, &msg
);
108 chip
->reg_image
[reg
] = val
;
113 static struct snd_pcm_hardware snd_at73c213_playback_hw
= {
114 .info
= SNDRV_PCM_INFO_INTERLEAVED
|
115 SNDRV_PCM_INFO_BLOCK_TRANSFER
,
116 .formats
= SNDRV_PCM_FMTBIT_S16_BE
,
117 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
118 .rate_min
= 8000, /* Replaced by chip->bitrate later. */
119 .rate_max
= 50000, /* Replaced by chip->bitrate later. */
122 .buffer_bytes_max
= 64 * 1024 - 1,
123 .period_bytes_min
= 512,
124 .period_bytes_max
= 64 * 1024 - 1,
130 * Calculate and set bitrate and divisions.
132 static int snd_at73c213_set_bitrate(struct snd_at73c213
*chip
)
134 unsigned long ssc_rate
= clk_get_rate(chip
->ssc
->clk
);
135 unsigned long dac_rate_new
, ssc_div
;
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 even. */
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
;
214 /* ensure buffer_size is a multiple of period_size */
215 err
= snd_pcm_hw_constraint_integer(runtime
,
216 SNDRV_PCM_HW_PARAM_PERIODS
);
219 snd_at73c213_playback_hw
.rate_min
= chip
->bitrate
;
220 snd_at73c213_playback_hw
.rate_max
= chip
->bitrate
;
221 runtime
->hw
= snd_at73c213_playback_hw
;
222 chip
->substream
= substream
;
224 clk_enable(chip
->ssc
->clk
);
229 static int snd_at73c213_pcm_close(struct snd_pcm_substream
*substream
)
231 struct snd_at73c213
*chip
= snd_pcm_substream_chip(substream
);
232 chip
->substream
= NULL
;
233 clk_disable(chip
->ssc
->clk
);
237 static int snd_at73c213_pcm_hw_params(struct snd_pcm_substream
*substream
,
238 struct snd_pcm_hw_params
*hw_params
)
240 struct snd_at73c213
*chip
= snd_pcm_substream_chip(substream
);
241 int channels
= params_channels(hw_params
);
244 val
= ssc_readl(chip
->ssc
->regs
, TFMR
);
245 val
= SSC_BFINS(TFMR_DATNB
, channels
- 1, val
);
246 ssc_writel(chip
->ssc
->regs
, TFMR
, val
);
248 return snd_pcm_lib_malloc_pages(substream
,
249 params_buffer_bytes(hw_params
));
252 static int snd_at73c213_pcm_hw_free(struct snd_pcm_substream
*substream
)
254 return snd_pcm_lib_free_pages(substream
);
257 static int snd_at73c213_pcm_prepare(struct snd_pcm_substream
*substream
)
259 struct snd_at73c213
*chip
= snd_pcm_substream_chip(substream
);
260 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
263 block_size
= frames_to_bytes(runtime
, runtime
->period_size
);
267 ssc_writel(chip
->ssc
->regs
, PDC_TPR
,
268 (long)runtime
->dma_addr
);
269 ssc_writel(chip
->ssc
->regs
, PDC_TCR
,
270 runtime
->period_size
* runtime
->channels
);
271 ssc_writel(chip
->ssc
->regs
, PDC_TNPR
,
272 (long)runtime
->dma_addr
+ block_size
);
273 ssc_writel(chip
->ssc
->regs
, PDC_TNCR
,
274 runtime
->period_size
* runtime
->channels
);
279 static int snd_at73c213_pcm_trigger(struct snd_pcm_substream
*substream
,
282 struct snd_at73c213
*chip
= snd_pcm_substream_chip(substream
);
285 spin_lock(&chip
->lock
);
288 case SNDRV_PCM_TRIGGER_START
:
289 ssc_writel(chip
->ssc
->regs
, IER
, SSC_BIT(IER_ENDTX
));
290 ssc_writel(chip
->ssc
->regs
, PDC_PTCR
, SSC_BIT(PDC_PTCR_TXTEN
));
292 case SNDRV_PCM_TRIGGER_STOP
:
293 ssc_writel(chip
->ssc
->regs
, PDC_PTCR
, SSC_BIT(PDC_PTCR_TXTDIS
));
294 ssc_writel(chip
->ssc
->regs
, IDR
, SSC_BIT(IDR_ENDTX
));
297 dev_dbg(&chip
->spi
->dev
, "spurious command %x\n", cmd
);
302 spin_unlock(&chip
->lock
);
307 static snd_pcm_uframes_t
308 snd_at73c213_pcm_pointer(struct snd_pcm_substream
*substream
)
310 struct snd_at73c213
*chip
= snd_pcm_substream_chip(substream
);
311 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
312 snd_pcm_uframes_t pos
;
315 bytes
= ssc_readl(chip
->ssc
->regs
, PDC_TPR
)
316 - (unsigned long)runtime
->dma_addr
;
318 pos
= bytes_to_frames(runtime
, bytes
);
319 if (pos
>= runtime
->buffer_size
)
320 pos
-= runtime
->buffer_size
;
325 static const struct snd_pcm_ops at73c213_playback_ops
= {
326 .open
= snd_at73c213_pcm_open
,
327 .close
= snd_at73c213_pcm_close
,
328 .ioctl
= snd_pcm_lib_ioctl
,
329 .hw_params
= snd_at73c213_pcm_hw_params
,
330 .hw_free
= snd_at73c213_pcm_hw_free
,
331 .prepare
= snd_at73c213_pcm_prepare
,
332 .trigger
= snd_at73c213_pcm_trigger
,
333 .pointer
= snd_at73c213_pcm_pointer
,
336 static int snd_at73c213_pcm_new(struct snd_at73c213
*chip
, int device
)
341 retval
= snd_pcm_new(chip
->card
, chip
->card
->shortname
,
346 pcm
->private_data
= chip
;
347 pcm
->info_flags
= SNDRV_PCM_INFO_BLOCK_TRANSFER
;
348 strcpy(pcm
->name
, "at73c213");
351 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &at73c213_playback_ops
);
353 retval
= snd_pcm_lib_preallocate_pages_for_all(chip
->pcm
,
354 SNDRV_DMA_TYPE_DEV
, &chip
->ssc
->pdev
->dev
,
355 64 * 1024, 64 * 1024);
360 static irqreturn_t
snd_at73c213_interrupt(int irq
, void *dev_id
)
362 struct snd_at73c213
*chip
= dev_id
;
363 struct snd_pcm_runtime
*runtime
= chip
->substream
->runtime
;
368 int retval
= IRQ_NONE
;
370 spin_lock(&chip
->lock
);
372 block_size
= frames_to_bytes(runtime
, runtime
->period_size
);
373 status
= ssc_readl(chip
->ssc
->regs
, IMR
);
375 if (status
& SSC_BIT(IMR_ENDTX
)) {
377 if (chip
->period
== runtime
->periods
)
379 next_period
= chip
->period
+ 1;
380 if (next_period
== runtime
->periods
)
383 offset
= block_size
* next_period
;
385 ssc_writel(chip
->ssc
->regs
, PDC_TNPR
,
386 (long)runtime
->dma_addr
+ offset
);
387 ssc_writel(chip
->ssc
->regs
, PDC_TNCR
,
388 runtime
->period_size
* runtime
->channels
);
389 retval
= IRQ_HANDLED
;
392 ssc_readl(chip
->ssc
->regs
, IMR
);
393 spin_unlock(&chip
->lock
);
395 if (status
& SSC_BIT(IMR_ENDTX
))
396 snd_pcm_period_elapsed(chip
->substream
);
404 static int snd_at73c213_mono_get(struct snd_kcontrol
*kcontrol
,
405 struct snd_ctl_elem_value
*ucontrol
)
407 struct snd_at73c213
*chip
= snd_kcontrol_chip(kcontrol
);
408 int reg
= kcontrol
->private_value
& 0xff;
409 int shift
= (kcontrol
->private_value
>> 8) & 0xff;
410 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
411 int invert
= (kcontrol
->private_value
>> 24) & 0xff;
413 mutex_lock(&chip
->mixer_lock
);
415 ucontrol
->value
.integer
.value
[0] =
416 (chip
->reg_image
[reg
] >> shift
) & mask
;
419 ucontrol
->value
.integer
.value
[0] =
420 mask
- ucontrol
->value
.integer
.value
[0];
422 mutex_unlock(&chip
->mixer_lock
);
427 static int snd_at73c213_mono_put(struct snd_kcontrol
*kcontrol
,
428 struct snd_ctl_elem_value
*ucontrol
)
430 struct snd_at73c213
*chip
= snd_kcontrol_chip(kcontrol
);
431 int reg
= kcontrol
->private_value
& 0xff;
432 int shift
= (kcontrol
->private_value
>> 8) & 0xff;
433 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
434 int invert
= (kcontrol
->private_value
>> 24) & 0xff;
438 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
443 mutex_lock(&chip
->mixer_lock
);
445 val
= (chip
->reg_image
[reg
] & ~(mask
<< shift
)) | val
;
446 change
= val
!= chip
->reg_image
[reg
];
447 retval
= snd_at73c213_write_reg(chip
, reg
, val
);
449 mutex_unlock(&chip
->mixer_lock
);
457 static int snd_at73c213_stereo_info(struct snd_kcontrol
*kcontrol
,
458 struct snd_ctl_elem_info
*uinfo
)
460 int mask
= (kcontrol
->private_value
>> 24) & 0xff;
463 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
465 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
468 uinfo
->value
.integer
.min
= 0;
469 uinfo
->value
.integer
.max
= mask
;
474 static int snd_at73c213_stereo_get(struct snd_kcontrol
*kcontrol
,
475 struct snd_ctl_elem_value
*ucontrol
)
477 struct snd_at73c213
*chip
= snd_kcontrol_chip(kcontrol
);
478 int left_reg
= kcontrol
->private_value
& 0xff;
479 int right_reg
= (kcontrol
->private_value
>> 8) & 0xff;
480 int shift_left
= (kcontrol
->private_value
>> 16) & 0x07;
481 int shift_right
= (kcontrol
->private_value
>> 19) & 0x07;
482 int mask
= (kcontrol
->private_value
>> 24) & 0xff;
483 int invert
= (kcontrol
->private_value
>> 22) & 1;
485 mutex_lock(&chip
->mixer_lock
);
487 ucontrol
->value
.integer
.value
[0] =
488 (chip
->reg_image
[left_reg
] >> shift_left
) & mask
;
489 ucontrol
->value
.integer
.value
[1] =
490 (chip
->reg_image
[right_reg
] >> shift_right
) & mask
;
493 ucontrol
->value
.integer
.value
[0] =
494 mask
- ucontrol
->value
.integer
.value
[0];
495 ucontrol
->value
.integer
.value
[1] =
496 mask
- ucontrol
->value
.integer
.value
[1];
499 mutex_unlock(&chip
->mixer_lock
);
504 static int snd_at73c213_stereo_put(struct snd_kcontrol
*kcontrol
,
505 struct snd_ctl_elem_value
*ucontrol
)
507 struct snd_at73c213
*chip
= snd_kcontrol_chip(kcontrol
);
508 int left_reg
= kcontrol
->private_value
& 0xff;
509 int right_reg
= (kcontrol
->private_value
>> 8) & 0xff;
510 int shift_left
= (kcontrol
->private_value
>> 16) & 0x07;
511 int shift_right
= (kcontrol
->private_value
>> 19) & 0x07;
512 int mask
= (kcontrol
->private_value
>> 24) & 0xff;
513 int invert
= (kcontrol
->private_value
>> 22) & 1;
515 unsigned short val1
, val2
;
517 val1
= ucontrol
->value
.integer
.value
[0] & mask
;
518 val2
= ucontrol
->value
.integer
.value
[1] & mask
;
524 val2
<<= shift_right
;
526 mutex_lock(&chip
->mixer_lock
);
528 val1
= (chip
->reg_image
[left_reg
] & ~(mask
<< shift_left
)) | val1
;
529 val2
= (chip
->reg_image
[right_reg
] & ~(mask
<< shift_right
)) | val2
;
530 change
= val1
!= chip
->reg_image
[left_reg
]
531 || val2
!= chip
->reg_image
[right_reg
];
532 retval
= snd_at73c213_write_reg(chip
, left_reg
, val1
);
534 mutex_unlock(&chip
->mixer_lock
);
537 retval
= snd_at73c213_write_reg(chip
, right_reg
, val2
);
539 mutex_unlock(&chip
->mixer_lock
);
543 mutex_unlock(&chip
->mixer_lock
);
551 #define snd_at73c213_mono_switch_info snd_ctl_boolean_mono_info
553 static int snd_at73c213_mono_switch_get(struct snd_kcontrol
*kcontrol
,
554 struct snd_ctl_elem_value
*ucontrol
)
556 struct snd_at73c213
*chip
= snd_kcontrol_chip(kcontrol
);
557 int reg
= kcontrol
->private_value
& 0xff;
558 int shift
= (kcontrol
->private_value
>> 8) & 0xff;
559 int invert
= (kcontrol
->private_value
>> 24) & 0xff;
561 mutex_lock(&chip
->mixer_lock
);
563 ucontrol
->value
.integer
.value
[0] =
564 (chip
->reg_image
[reg
] >> shift
) & 0x01;
567 ucontrol
->value
.integer
.value
[0] =
568 0x01 - ucontrol
->value
.integer
.value
[0];
570 mutex_unlock(&chip
->mixer_lock
);
575 static int snd_at73c213_mono_switch_put(struct snd_kcontrol
*kcontrol
,
576 struct snd_ctl_elem_value
*ucontrol
)
578 struct snd_at73c213
*chip
= snd_kcontrol_chip(kcontrol
);
579 int reg
= kcontrol
->private_value
& 0xff;
580 int shift
= (kcontrol
->private_value
>> 8) & 0xff;
581 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
582 int invert
= (kcontrol
->private_value
>> 24) & 0xff;
586 if (ucontrol
->value
.integer
.value
[0])
595 mutex_lock(&chip
->mixer_lock
);
597 val
|= (chip
->reg_image
[reg
] & ~(mask
<< shift
));
598 change
= val
!= chip
->reg_image
[reg
];
600 retval
= snd_at73c213_write_reg(chip
, reg
, val
);
602 mutex_unlock(&chip
->mixer_lock
);
610 static int snd_at73c213_pa_volume_info(struct snd_kcontrol
*kcontrol
,
611 struct snd_ctl_elem_info
*uinfo
)
613 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
615 uinfo
->value
.integer
.min
= 0;
616 uinfo
->value
.integer
.max
= ((kcontrol
->private_value
>> 16) & 0xff) - 1;
621 static int snd_at73c213_line_capture_volume_info(
622 struct snd_kcontrol
*kcontrol
,
623 struct snd_ctl_elem_info
*uinfo
)
625 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
627 /* When inverted will give values 0x10001 => 0. */
628 uinfo
->value
.integer
.min
= 14;
629 uinfo
->value
.integer
.max
= 31;
634 static int snd_at73c213_aux_capture_volume_info(
635 struct snd_kcontrol
*kcontrol
,
636 struct snd_ctl_elem_info
*uinfo
)
638 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
640 /* When inverted will give values 0x10001 => 0. */
641 uinfo
->value
.integer
.min
= 14;
642 uinfo
->value
.integer
.max
= 31;
647 #define AT73C213_MONO_SWITCH(xname, xindex, reg, shift, mask, invert) \
649 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
652 .info = snd_at73c213_mono_switch_info, \
653 .get = snd_at73c213_mono_switch_get, \
654 .put = snd_at73c213_mono_switch_put, \
655 .private_value = (reg | (shift << 8) | (mask << 16) | (invert << 24)) \
658 #define AT73C213_STEREO(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
660 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
663 .info = snd_at73c213_stereo_info, \
664 .get = snd_at73c213_stereo_get, \
665 .put = snd_at73c213_stereo_put, \
666 .private_value = (left_reg | (right_reg << 8) \
667 | (shift_left << 16) | (shift_right << 19) \
668 | (mask << 24) | (invert << 22)) \
671 static struct snd_kcontrol_new snd_at73c213_controls
[] = {
672 AT73C213_STEREO("Master Playback Volume", 0, DAC_LMPG
, DAC_RMPG
, 0, 0, 0x1f, 1),
673 AT73C213_STEREO("Master Playback Switch", 0, DAC_LMPG
, DAC_RMPG
, 5, 5, 1, 1),
674 AT73C213_STEREO("PCM Playback Volume", 0, DAC_LLOG
, DAC_RLOG
, 0, 0, 0x1f, 1),
675 AT73C213_STEREO("PCM Playback Switch", 0, DAC_LLOG
, DAC_RLOG
, 5, 5, 1, 1),
676 AT73C213_MONO_SWITCH("Mono PA Playback Switch", 0, DAC_CTRL
, DAC_CTRL_ONPADRV
,
679 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
680 .name
= "PA Playback Volume",
682 .info
= snd_at73c213_pa_volume_info
,
683 .get
= snd_at73c213_mono_get
,
684 .put
= snd_at73c213_mono_put
,
685 .private_value
= PA_CTRL
| (PA_CTRL_APAGAIN
<< 8) | \
686 (0x0f << 16) | (1 << 24),
688 AT73C213_MONO_SWITCH("PA High Gain Playback Switch", 0, PA_CTRL
, PA_CTRL_APALP
,
690 AT73C213_MONO_SWITCH("PA Playback Switch", 0, PA_CTRL
, PA_CTRL_APAON
, 0x01, 0),
692 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
693 .name
= "Aux Capture Volume",
695 .info
= snd_at73c213_aux_capture_volume_info
,
696 .get
= snd_at73c213_mono_get
,
697 .put
= snd_at73c213_mono_put
,
698 .private_value
= DAC_AUXG
| (0 << 8) | (0x1f << 16) | (1 << 24),
700 AT73C213_MONO_SWITCH("Aux Capture Switch", 0, DAC_CTRL
, DAC_CTRL_ONAUXIN
,
703 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
704 .name
= "Line Capture Volume",
706 .info
= snd_at73c213_line_capture_volume_info
,
707 .get
= snd_at73c213_stereo_get
,
708 .put
= snd_at73c213_stereo_put
,
709 .private_value
= DAC_LLIG
| (DAC_RLIG
<< 8) | (0 << 16) | (0 << 19)
710 | (0x1f << 24) | (1 << 22),
712 AT73C213_MONO_SWITCH("Line Capture Switch", 0, DAC_CTRL
, 0, 0x03, 0),
715 static int snd_at73c213_mixer(struct snd_at73c213
*chip
)
717 struct snd_card
*card
;
720 if (chip
== NULL
|| chip
->pcm
== NULL
)
725 strcpy(card
->mixername
, chip
->pcm
->name
);
727 for (idx
= 0; idx
< ARRAY_SIZE(snd_at73c213_controls
); idx
++) {
728 errval
= snd_ctl_add(card
,
729 snd_ctl_new1(&snd_at73c213_controls
[idx
],
738 for (idx
= 1; idx
< ARRAY_SIZE(snd_at73c213_controls
) + 1; idx
++) {
739 struct snd_kcontrol
*kctl
;
740 kctl
= snd_ctl_find_numid(card
, idx
);
742 snd_ctl_remove(card
, kctl
);
750 static int snd_at73c213_ssc_init(struct snd_at73c213
*chip
)
753 * Continuous clock output.
754 * Starts on falling TF.
755 * Delay 1 cycle (1 bit).
756 * Periode is 16 bit (16 - 1).
758 ssc_writel(chip
->ssc
->regs
, TCMR
,
760 | SSC_BF(TCMR_START
, 4)
761 | SSC_BF(TCMR_STTDLY
, 1)
762 | SSC_BF(TCMR_PERIOD
, 16 - 1));
764 * Data length is 16 bit (16 - 1).
765 * Transmit MSB first.
766 * Transmit 2 words each transfer.
767 * Frame sync length is 16 bit (16 - 1).
768 * Frame starts on negative pulse.
770 ssc_writel(chip
->ssc
->regs
, TFMR
,
771 SSC_BF(TFMR_DATLEN
, 16 - 1)
773 | SSC_BF(TFMR_DATNB
, 1)
774 | SSC_BF(TFMR_FSLEN
, 16 - 1)
775 | SSC_BF(TFMR_FSOS
, 1));
780 static int snd_at73c213_chip_init(struct snd_at73c213
*chip
)
783 unsigned char dac_ctrl
= 0;
785 retval
= snd_at73c213_set_bitrate(chip
);
789 /* Enable DAC master clock. */
790 clk_enable(chip
->board
->dac_clk
);
792 /* Initialize at73c213 on SPI bus. */
793 retval
= snd_at73c213_write_reg(chip
, DAC_RST
, 0x04);
797 retval
= snd_at73c213_write_reg(chip
, DAC_RST
, 0x03);
801 /* Precharge everything. */
802 retval
= snd_at73c213_write_reg(chip
, DAC_PRECH
, 0xff);
805 retval
= snd_at73c213_write_reg(chip
, PA_CTRL
, (1<<PA_CTRL_APAPRECH
));
808 retval
= snd_at73c213_write_reg(chip
, DAC_CTRL
,
809 (1<<DAC_CTRL_ONLNOL
) | (1<<DAC_CTRL_ONLNOR
));
815 /* Stop precharging PA. */
816 retval
= snd_at73c213_write_reg(chip
, PA_CTRL
,
817 (1<<PA_CTRL_APALP
) | 0x0f);
823 /* Stop precharging DAC, turn on master power. */
824 retval
= snd_at73c213_write_reg(chip
, DAC_PRECH
, (1<<DAC_PRECH_ONMSTR
));
831 dac_ctrl
= (1<<DAC_CTRL_ONDACL
) | (1<<DAC_CTRL_ONDACR
)
832 | (1<<DAC_CTRL_ONLNOL
) | (1<<DAC_CTRL_ONLNOR
);
834 retval
= snd_at73c213_write_reg(chip
, DAC_CTRL
, dac_ctrl
);
839 retval
= snd_at73c213_write_reg(chip
, DAC_LMPG
, 0x3f);
842 retval
= snd_at73c213_write_reg(chip
, DAC_RMPG
, 0x3f);
845 retval
= snd_at73c213_write_reg(chip
, DAC_LLOG
, 0x3f);
848 retval
= snd_at73c213_write_reg(chip
, DAC_RLOG
, 0x3f);
851 retval
= snd_at73c213_write_reg(chip
, DAC_LLIG
, 0x11);
854 retval
= snd_at73c213_write_reg(chip
, DAC_RLIG
, 0x11);
857 retval
= snd_at73c213_write_reg(chip
, DAC_AUXG
, 0x11);
861 /* Enable I2S device, i.e. clock output. */
862 ssc_writel(chip
->ssc
->regs
, CR
, SSC_BIT(CR_TXEN
));
867 clk_disable(chip
->board
->dac_clk
);
872 static int snd_at73c213_dev_free(struct snd_device
*device
)
874 struct snd_at73c213
*chip
= device
->device_data
;
876 ssc_writel(chip
->ssc
->regs
, CR
, SSC_BIT(CR_TXDIS
));
877 if (chip
->irq
>= 0) {
878 free_irq(chip
->irq
, chip
);
885 static int snd_at73c213_dev_init(struct snd_card
*card
,
886 struct spi_device
*spi
)
888 static struct snd_device_ops ops
= {
889 .dev_free
= snd_at73c213_dev_free
,
891 struct snd_at73c213
*chip
= get_chip(card
);
894 irq
= chip
->ssc
->irq
;
898 spin_lock_init(&chip
->lock
);
899 mutex_init(&chip
->mixer_lock
);
903 clk_enable(chip
->ssc
->clk
);
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
);
938 snd_device_free(card
, chip
);
940 free_irq(chip
->irq
, chip
);
943 clk_disable(chip
->ssc
->clk
);
948 static int 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
);
972 /* Allocate "card" using some unused identifiers. */
973 snprintf(id
, sizeof id
, "at73c213_%d", board
->ssc_id
);
974 retval
= snd_card_new(&spi
->dev
, -1, id
, THIS_MODULE
,
975 sizeof(struct snd_at73c213
), &card
);
979 chip
= card
->private_data
;
983 chip
->ssc
= ssc_request(board
->ssc_id
);
984 if (IS_ERR(chip
->ssc
)) {
985 dev_dbg(&spi
->dev
, "could not get ssc%d device\n",
987 retval
= PTR_ERR(chip
->ssc
);
991 retval
= snd_at73c213_dev_init(card
, spi
);
995 strcpy(card
->driver
, "at73c213");
996 strcpy(card
->shortname
, board
->shortname
);
997 sprintf(card
->longname
, "%s on irq %d", card
->shortname
, chip
->irq
);
999 retval
= snd_card_register(card
);
1003 dev_set_drvdata(&spi
->dev
, card
);
1008 ssc_free(chip
->ssc
);
1010 snd_card_free(card
);
1015 static int snd_at73c213_remove(struct spi_device
*spi
)
1017 struct snd_card
*card
= dev_get_drvdata(&spi
->dev
);
1018 struct snd_at73c213
*chip
= card
->private_data
;
1021 /* Stop playback. */
1022 clk_enable(chip
->ssc
->clk
);
1023 ssc_writel(chip
->ssc
->regs
, CR
, SSC_BIT(CR_TXDIS
));
1024 clk_disable(chip
->ssc
->clk
);
1027 retval
= snd_at73c213_write_reg(chip
, DAC_LMPG
, 0x3f);
1030 retval
= snd_at73c213_write_reg(chip
, DAC_RMPG
, 0x3f);
1033 retval
= snd_at73c213_write_reg(chip
, DAC_LLOG
, 0x3f);
1036 retval
= snd_at73c213_write_reg(chip
, DAC_RLOG
, 0x3f);
1039 retval
= snd_at73c213_write_reg(chip
, DAC_LLIG
, 0x11);
1042 retval
= snd_at73c213_write_reg(chip
, DAC_RLIG
, 0x11);
1045 retval
= snd_at73c213_write_reg(chip
, DAC_AUXG
, 0x11);
1050 retval
= snd_at73c213_write_reg(chip
, PA_CTRL
,
1051 chip
->reg_image
[PA_CTRL
] | 0x0f);
1055 retval
= snd_at73c213_write_reg(chip
, PA_CTRL
,
1056 (1 << PA_CTRL_APALP
) | 0x0f);
1060 /* Turn off external DAC. */
1061 retval
= snd_at73c213_write_reg(chip
, DAC_CTRL
, 0x0c);
1065 retval
= snd_at73c213_write_reg(chip
, DAC_CTRL
, 0x00);
1069 /* Turn off master power. */
1070 retval
= snd_at73c213_write_reg(chip
, DAC_PRECH
, 0x00);
1075 /* Stop DAC master clock. */
1076 clk_disable(chip
->board
->dac_clk
);
1078 ssc_free(chip
->ssc
);
1079 snd_card_free(card
);
1084 #ifdef CONFIG_PM_SLEEP
1086 static int snd_at73c213_suspend(struct device
*dev
)
1088 struct snd_card
*card
= dev_get_drvdata(dev
);
1089 struct snd_at73c213
*chip
= card
->private_data
;
1091 ssc_writel(chip
->ssc
->regs
, CR
, SSC_BIT(CR_TXDIS
));
1092 clk_disable(chip
->ssc
->clk
);
1093 clk_disable(chip
->board
->dac_clk
);
1098 static int snd_at73c213_resume(struct device
*dev
)
1100 struct snd_card
*card
= dev_get_drvdata(dev
);
1101 struct snd_at73c213
*chip
= card
->private_data
;
1103 clk_enable(chip
->board
->dac_clk
);
1104 clk_enable(chip
->ssc
->clk
);
1105 ssc_writel(chip
->ssc
->regs
, CR
, SSC_BIT(CR_TXEN
));
1110 static SIMPLE_DEV_PM_OPS(at73c213_pm_ops
, snd_at73c213_suspend
,
1111 snd_at73c213_resume
);
1112 #define AT73C213_PM_OPS (&at73c213_pm_ops)
1115 #define AT73C213_PM_OPS NULL
1118 static struct spi_driver at73c213_driver
= {
1121 .pm
= AT73C213_PM_OPS
,
1123 .probe
= snd_at73c213_probe
,
1124 .remove
= snd_at73c213_remove
,
1127 module_spi_driver(at73c213_driver
);
1129 MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>");
1130 MODULE_DESCRIPTION("Sound driver for AT73C213 with Atmel SSC");
1131 MODULE_LICENSE("GPL");