2 * i2sbus driver -- pcm routines
4 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
6 * GPL v2, can be found in COPYING.
10 #include <linux/delay.h>
11 /* So apparently there's a reason for requiring driver.h
12 * to be included first, even if I don't know it... */
13 #include <sound/driver.h>
14 #include <sound/core.h>
15 #include <asm/macio.h>
16 #include <linux/pci.h>
17 #include "../soundbus.h"
20 static inline void get_pcm_info(struct i2sbus_dev
*i2sdev
, int in
,
21 struct pcm_info
**pi
, struct pcm_info
**other
)
27 *other
= &i2sdev
->out
;
36 static int clock_and_divisors(int mclk
, int sclk
, int rate
, int *out
)
38 /* sclk must be derived from mclk! */
41 /* derive sclk register value */
42 if (i2s_sf_sclkdiv(mclk
/ sclk
, out
))
45 if (I2S_CLOCK_SPEED_18MHz
% (rate
* mclk
) == 0) {
46 if (!i2s_sf_mclkdiv(I2S_CLOCK_SPEED_18MHz
/ (rate
* mclk
), out
)) {
47 *out
|= I2S_SF_CLOCK_SOURCE_18MHz
;
51 if (I2S_CLOCK_SPEED_45MHz
% (rate
* mclk
) == 0) {
52 if (!i2s_sf_mclkdiv(I2S_CLOCK_SPEED_45MHz
/ (rate
* mclk
), out
)) {
53 *out
|= I2S_SF_CLOCK_SOURCE_45MHz
;
57 if (I2S_CLOCK_SPEED_49MHz
% (rate
* mclk
) == 0) {
58 if (!i2s_sf_mclkdiv(I2S_CLOCK_SPEED_49MHz
/ (rate
* mclk
), out
)) {
59 *out
|= I2S_SF_CLOCK_SOURCE_49MHz
;
66 #define CHECK_RATE(rate) \
67 do { if (rates & SNDRV_PCM_RATE_ ##rate) { \
69 if (clock_and_divisors(sysclock_factor, \
70 bus_factor, rate, &dummy)) \
71 rates &= ~SNDRV_PCM_RATE_ ##rate; \
74 static int i2sbus_pcm_open(struct i2sbus_dev
*i2sdev
, int in
)
76 struct pcm_info
*pi
, *other
;
77 struct soundbus_dev
*sdev
;
78 int masks_inited
= 0, err
;
79 struct codec_info_item
*cii
, *rev
;
80 struct snd_pcm_hardware
*hw
;
82 unsigned int rates
= 0;
83 struct transfer_info v
;
85 int bus_factor
= 0, sysclock_factor
= 0;
88 mutex_lock(&i2sdev
->lock
);
90 get_pcm_info(i2sdev
, in
, &pi
, &other
);
92 hw
= &pi
->substream
->runtime
->hw
;
93 sdev
= &i2sdev
->sound
;
101 /* we now need to assign the hw */
102 list_for_each_entry(cii
, &sdev
->codec_list
, list
) {
103 struct transfer_info
*ti
= cii
->codec
->transfers
;
104 bus_factor
= cii
->codec
->bus_factor
;
105 sysclock_factor
= cii
->codec
->sysclock_factor
;
106 while (ti
->formats
&& ti
->rates
) {
108 if (ti
->transfer_in
== in
109 && cii
->codec
->usable(cii
, ti
, &v
)) {
111 formats
&= v
.formats
;
122 if (!masks_inited
|| !bus_factor
|| !sysclock_factor
) {
126 /* bus dependent stuff */
127 hw
->info
= SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_MMAP_VALID
|
128 SNDRV_PCM_INFO_INTERLEAVED
| SNDRV_PCM_INFO_RESUME
;
145 /* well. the codec might want 24 bits only, and we'll
146 * ever only transfer 24 bits, but they are top-aligned!
147 * So for alsa, we claim that we're doing full 32 bit
148 * while in reality we'll ignore the lower 8 bits of
149 * that when doing playback (they're transferred as 0
150 * as far as I know, no codecs we have are 32-bit capable
151 * so I can't really test) and when doing recording we'll
152 * always have those lower 8 bits recorded as 0 */
153 if (formats
& SNDRV_PCM_FMTBIT_S24_BE
)
154 formats
|= SNDRV_PCM_FMTBIT_S32_BE
;
155 if (formats
& SNDRV_PCM_FMTBIT_U24_BE
)
156 formats
|= SNDRV_PCM_FMTBIT_U32_BE
;
157 /* now mask off what we can support. I suppose we could
158 * also support S24_3LE and some similar formats, but I
159 * doubt there's a codec that would be able to use that,
160 * so we don't support it here. */
161 hw
->formats
= formats
& (SNDRV_PCM_FMTBIT_S16_BE
|
162 SNDRV_PCM_FMTBIT_U16_BE
|
163 SNDRV_PCM_FMTBIT_S32_BE
|
164 SNDRV_PCM_FMTBIT_U32_BE
);
166 /* we need to set the highest and lowest rate possible.
167 * These are the highest and lowest rates alsa can
168 * support properly in its bitfield.
169 * Below, we'll use that to restrict to the rate
170 * currently in use (if any). */
172 hw
->rate_max
= 192000;
173 /* if the other stream is active, then we can only
174 * support what it is currently using.
175 * FIXME: I lied. This comment is wrong. We can support
176 * anything that works with the same serial format, ie.
177 * when recording 24 bit sound we can well play 16 bit
178 * sound at the same time iff using the same transfer mode.
181 /* FIXME: is this guaranteed by the alsa api? */
182 hw
->formats
&= (1ULL << i2sdev
->format
);
183 /* see above, restrict rates to the one we already have */
184 hw
->rate_min
= i2sdev
->rate
;
185 hw
->rate_max
= i2sdev
->rate
;
188 hw
->channels_min
= 2;
189 hw
->channels_max
= 2;
190 /* these are somewhat arbitrary */
191 hw
->buffer_bytes_max
= 131072;
192 hw
->period_bytes_min
= 256;
193 hw
->period_bytes_max
= 16384;
195 hw
->periods_max
= MAX_DBDMA_COMMANDS
;
196 list_for_each_entry(cii
, &sdev
->codec_list
, list
) {
197 if (cii
->codec
->open
) {
198 err
= cii
->codec
->open(cii
, pi
->substream
);
203 list_for_each_entry_reverse(rev
,
204 &sdev
->codec_list
, list
) {
205 if (found_this
&& rev
->codec
->close
) {
206 rev
->codec
->close(rev
,
218 mutex_unlock(&i2sdev
->lock
);
224 static int i2sbus_pcm_close(struct i2sbus_dev
*i2sdev
, int in
)
226 struct codec_info_item
*cii
;
230 mutex_lock(&i2sdev
->lock
);
232 get_pcm_info(i2sdev
, in
, &pi
, NULL
);
234 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
) {
235 if (cii
->codec
->close
) {
236 tmp
= cii
->codec
->close(cii
, pi
->substream
);
242 pi
->substream
= NULL
;
244 mutex_unlock(&i2sdev
->lock
);
248 static int i2sbus_hw_params(struct snd_pcm_substream
*substream
,
249 struct snd_pcm_hw_params
*params
)
251 return snd_pcm_lib_malloc_pages(substream
, params_buffer_bytes(params
));
254 static int i2sbus_hw_free(struct snd_pcm_substream
*substream
)
256 snd_pcm_lib_free_pages(substream
);
260 static int i2sbus_pcm_prepare(struct i2sbus_dev
*i2sdev
, int in
)
262 /* whee. Hard work now. The user has selected a bitrate
263 * and bit format, so now we have to program our
264 * I2S controller appropriately. */
265 struct snd_pcm_runtime
*runtime
;
266 struct dbdma_cmd
*command
;
270 struct codec_info_item
*cii
;
271 int sfr
= 0; /* serial format register */
272 int dws
= 0; /* data word sizes reg */
274 struct pcm_info
*pi
, *other
;
278 mutex_lock(&i2sdev
->lock
);
280 get_pcm_info(i2sdev
, in
, &pi
, &other
);
282 if (pi
->dbdma_ring
.running
) {
287 runtime
= pi
->substream
->runtime
;
290 ((i2sdev
->format
!= runtime
->format
)
291 || (i2sdev
->rate
!= runtime
->rate
))) {
296 i2sdev
->format
= runtime
->format
;
297 i2sdev
->rate
= runtime
->rate
;
299 periodsize
= snd_pcm_lib_period_bytes(pi
->substream
);
300 pi
->current_period
= 0;
302 /* generate dbdma command ring first */
303 command
= pi
->dbdma_ring
.cmds
;
304 offset
= runtime
->dma_addr
;
305 for (i
= 0; i
< pi
->substream
->runtime
->periods
;
306 i
++, command
++, offset
+= periodsize
) {
307 memset(command
, 0, sizeof(struct dbdma_cmd
));
309 cpu_to_le16((in
? INPUT_MORE
: OUTPUT_MORE
) | INTR_ALWAYS
);
310 command
->phy_addr
= cpu_to_le32(offset
);
311 command
->req_count
= cpu_to_le16(periodsize
);
312 command
->xfer_status
= cpu_to_le16(0);
314 /* last one branches back to first */
316 command
->command
|= cpu_to_le16(BR_ALWAYS
);
317 command
->cmd_dep
= cpu_to_le32(pi
->dbdma_ring
.bus_cmd_start
);
319 /* ok, let's set the serial format and stuff */
320 switch (runtime
->format
) {
322 case SNDRV_PCM_FORMAT_S16_BE
:
323 case SNDRV_PCM_FORMAT_U16_BE
:
324 /* FIXME: if we add different bus factors we need to
327 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
) {
328 bi
.bus_factor
= cii
->codec
->bus_factor
;
331 if (!bi
.bus_factor
) {
337 case SNDRV_PCM_FORMAT_S32_BE
:
338 case SNDRV_PCM_FORMAT_U32_BE
:
339 /* force 64x bus speed, otherwise the data cannot be
340 * transferred quickly enough! */
348 /* we assume all sysclocks are the same! */
349 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
) {
350 bi
.sysclock_factor
= cii
->codec
->sysclock_factor
;
354 if (clock_and_divisors(bi
.sysclock_factor
,
361 switch (bi
.bus_factor
) {
363 sfr
|= I2S_SF_SERIAL_FORMAT_I2S_32X
;
366 sfr
|= I2S_SF_SERIAL_FORMAT_I2S_64X
;
369 /* FIXME: THIS ASSUMES MASTER ALL THE TIME */
370 sfr
|= I2S_SF_SCLK_MASTER
;
372 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
) {
374 if (cii
->codec
->prepare
)
375 err
= cii
->codec
->prepare(cii
, &bi
, pi
->substream
);
381 /* codecs are fine with it, so set our clocks */
383 dws
= (2 << I2S_DWS_NUM_CHANNELS_IN_SHIFT
) |
384 (2 << I2S_DWS_NUM_CHANNELS_OUT_SHIFT
) |
385 I2S_DWS_DATA_IN_16BIT
| I2S_DWS_DATA_OUT_16BIT
;
387 dws
= (2 << I2S_DWS_NUM_CHANNELS_IN_SHIFT
) |
388 (2 << I2S_DWS_NUM_CHANNELS_OUT_SHIFT
) |
389 I2S_DWS_DATA_IN_24BIT
| I2S_DWS_DATA_OUT_24BIT
;
391 /* early exit if already programmed correctly */
392 /* not locking these is fine since we touch them only in this function */
393 if (in_le32(&i2sdev
->intfregs
->serial_format
) == sfr
394 && in_le32(&i2sdev
->intfregs
->data_word_sizes
) == dws
)
397 /* let's notify the codecs about clocks going away.
398 * For now we only do mastering on the i2s cell... */
399 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
)
400 if (cii
->codec
->switch_clock
)
401 cii
->codec
->switch_clock(cii
, CLOCK_SWITCH_PREPARE_SLAVE
);
403 i2sbus_control_enable(i2sdev
->control
, i2sdev
);
404 i2sbus_control_cell(i2sdev
->control
, i2sdev
, 1);
406 out_le32(&i2sdev
->intfregs
->intr_ctl
, I2S_PENDING_CLOCKS_STOPPED
);
408 i2sbus_control_clock(i2sdev
->control
, i2sdev
, 0);
412 /* wait for clock stopped. This can apparently take a while... */
415 !(in_le32(&i2sdev
->intfregs
->intr_ctl
) & I2S_PENDING_CLOCKS_STOPPED
)) {
418 out_le32(&i2sdev
->intfregs
->intr_ctl
, I2S_PENDING_CLOCKS_STOPPED
);
420 /* not locking these is fine since we touch them only in this function */
421 out_le32(&i2sdev
->intfregs
->serial_format
, sfr
);
422 out_le32(&i2sdev
->intfregs
->data_word_sizes
, dws
);
424 i2sbus_control_enable(i2sdev
->control
, i2sdev
);
425 i2sbus_control_cell(i2sdev
->control
, i2sdev
, 1);
426 i2sbus_control_clock(i2sdev
->control
, i2sdev
, 1);
429 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
)
430 if (cii
->codec
->switch_clock
)
431 cii
->codec
->switch_clock(cii
, CLOCK_SWITCH_SLAVE
);
434 mutex_unlock(&i2sdev
->lock
);
438 static struct dbdma_cmd STOP_CMD
= {
439 .command
= __constant_cpu_to_le16(DBDMA_STOP
),
442 static int i2sbus_pcm_trigger(struct i2sbus_dev
*i2sdev
, int in
, int cmd
)
444 struct codec_info_item
*cii
;
447 struct dbdma_cmd tmp
;
451 spin_lock_irqsave(&i2sdev
->low_lock
, flags
);
453 get_pcm_info(i2sdev
, in
, &pi
, NULL
);
456 case SNDRV_PCM_TRIGGER_START
:
457 case SNDRV_PCM_TRIGGER_RESUME
:
458 if (pi
->dbdma_ring
.running
) {
462 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
)
463 if (cii
->codec
->start
)
464 cii
->codec
->start(cii
, pi
->substream
);
465 pi
->dbdma_ring
.running
= 1;
467 /* reset dma engine */
468 out_le32(&pi
->dbdma
->control
,
469 0 | (RUN
| PAUSE
| FLUSH
| WAKE
) << 16);
471 while (in_le32(&pi
->dbdma
->status
) & RUN
&& timeout
--)
475 "i2sbus: error waiting for dma reset\n");
480 /* write dma command buffer address to the dbdma chip */
481 out_le32(&pi
->dbdma
->cmdptr
, pi
->dbdma_ring
.bus_cmd_start
);
484 (void)in_le32(&pi
->dbdma
->status
);
486 /* change first command to STOP */
487 tmp
= *pi
->dbdma_ring
.cmds
;
488 *pi
->dbdma_ring
.cmds
= STOP_CMD
;
490 /* set running state, remember that the first command is STOP */
491 out_le32(&pi
->dbdma
->control
, RUN
| (RUN
<< 16));
493 /* wait for STOP to be executed */
494 while (in_le32(&pi
->dbdma
->status
) & ACTIVE
&& timeout
--)
497 printk(KERN_ERR
"i2sbus: error waiting for dma stop\n");
501 /* again, write dma command buffer address to the dbdma chip,
502 * this time of the first real command */
503 *pi
->dbdma_ring
.cmds
= tmp
;
504 out_le32(&pi
->dbdma
->cmdptr
, pi
->dbdma_ring
.bus_cmd_start
);
507 (void)in_le32(&pi
->dbdma
->status
);
509 /* reset dma engine again */
510 out_le32(&pi
->dbdma
->control
,
511 0 | (RUN
| PAUSE
| FLUSH
| WAKE
) << 16);
513 while (in_le32(&pi
->dbdma
->status
) & RUN
&& timeout
--)
517 "i2sbus: error waiting for dma reset\n");
522 /* wake up the chip with the next descriptor */
523 out_le32(&pi
->dbdma
->control
,
524 (RUN
| WAKE
) | ((RUN
| WAKE
) << 16));
525 /* get the frame count */
526 pi
->frame_count
= in_le32(&i2sdev
->intfregs
->frame_count
);
530 case SNDRV_PCM_TRIGGER_STOP
:
531 case SNDRV_PCM_TRIGGER_SUSPEND
:
532 if (!pi
->dbdma_ring
.running
) {
537 /* turn off all relevant bits */
538 out_le32(&pi
->dbdma
->control
,
539 (RUN
| WAKE
| FLUSH
| PAUSE
) << 16);
541 /* FIXME: move to own function */
543 while ((in_le32(&pi
->dbdma
->status
) & RUN
)
548 "i2sbus: timed out turning "
549 "off dbdma engine!\n");
552 pi
->dbdma_ring
.running
= 0;
553 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
)
554 if (cii
->codec
->stop
)
555 cii
->codec
->stop(cii
, pi
->substream
);
563 spin_unlock_irqrestore(&i2sdev
->low_lock
, flags
);
567 static snd_pcm_uframes_t
i2sbus_pcm_pointer(struct i2sbus_dev
*i2sdev
, int in
)
572 get_pcm_info(i2sdev
, in
, &pi
, NULL
);
574 fc
= in_le32(&i2sdev
->intfregs
->frame_count
);
575 fc
= fc
- pi
->frame_count
;
577 return (bytes_to_frames(pi
->substream
->runtime
,
579 snd_pcm_lib_period_bytes(pi
->substream
))
580 + fc
) % pi
->substream
->runtime
->buffer_size
;
583 static inline void handle_interrupt(struct i2sbus_dev
*i2sdev
, int in
)
589 spin_lock(&i2sdev
->low_lock
);
590 get_pcm_info(i2sdev
, in
, &pi
, NULL
);
592 if (!pi
->dbdma_ring
.running
) {
593 /* there was still an interrupt pending
594 * while we stopped. or maybe another
595 * processor (not the one that was stopping
596 * the DMA engine) was spinning above
597 * waiting for the lock. */
601 fc
= in_le32(&i2sdev
->intfregs
->frame_count
);
602 /* a counter overflow does not change the calculation. */
603 delta
= fc
- pi
->frame_count
;
605 /* update current_period */
606 while (delta
>= pi
->substream
->runtime
->period_size
) {
607 pi
->current_period
++;
608 delta
= delta
- pi
->substream
->runtime
->period_size
;
611 if (unlikely(delta
)) {
612 /* Some interrupt came late, so check the dbdma.
613 * This special case exists to syncronize the frame_count with
614 * the dbdma transfer, but is hit every once in a while. */
617 period
= (in_le32(&pi
->dbdma
->cmdptr
)
618 - pi
->dbdma_ring
.bus_cmd_start
)
619 / sizeof(struct dbdma_cmd
);
620 pi
->current_period
= pi
->current_period
621 % pi
->substream
->runtime
->periods
;
623 while (pi
->current_period
!= period
) {
624 pi
->current_period
++;
625 pi
->current_period
%= pi
->substream
->runtime
->periods
;
626 /* Set delta to zero, as the frame_count value is too
627 * high (otherwise the code path will not be executed).
628 * This corrects the fact that the frame_count is too
629 * low at the beginning due to buffering. */
634 pi
->frame_count
= fc
- delta
;
635 pi
->current_period
%= pi
->substream
->runtime
->periods
;
637 spin_unlock(&i2sdev
->low_lock
);
638 /* may call _trigger again, hence needs to be unlocked */
639 snd_pcm_period_elapsed(pi
->substream
);
642 spin_unlock(&i2sdev
->low_lock
);
645 irqreturn_t
i2sbus_tx_intr(int irq
, void *devid
)
647 handle_interrupt((struct i2sbus_dev
*)devid
, 0);
651 irqreturn_t
i2sbus_rx_intr(int irq
, void *devid
)
653 handle_interrupt((struct i2sbus_dev
*)devid
, 1);
657 static int i2sbus_playback_open(struct snd_pcm_substream
*substream
)
659 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
663 i2sdev
->out
.substream
= substream
;
664 return i2sbus_pcm_open(i2sdev
, 0);
667 static int i2sbus_playback_close(struct snd_pcm_substream
*substream
)
669 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
674 if (i2sdev
->out
.substream
!= substream
)
676 err
= i2sbus_pcm_close(i2sdev
, 0);
678 i2sdev
->out
.substream
= NULL
;
682 static int i2sbus_playback_prepare(struct snd_pcm_substream
*substream
)
684 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
688 if (i2sdev
->out
.substream
!= substream
)
690 return i2sbus_pcm_prepare(i2sdev
, 0);
693 static int i2sbus_playback_trigger(struct snd_pcm_substream
*substream
, int cmd
)
695 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
699 if (i2sdev
->out
.substream
!= substream
)
701 return i2sbus_pcm_trigger(i2sdev
, 0, cmd
);
704 static snd_pcm_uframes_t
i2sbus_playback_pointer(struct snd_pcm_substream
707 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
711 if (i2sdev
->out
.substream
!= substream
)
713 return i2sbus_pcm_pointer(i2sdev
, 0);
716 static struct snd_pcm_ops i2sbus_playback_ops
= {
717 .open
= i2sbus_playback_open
,
718 .close
= i2sbus_playback_close
,
719 .ioctl
= snd_pcm_lib_ioctl
,
720 .hw_params
= i2sbus_hw_params
,
721 .hw_free
= i2sbus_hw_free
,
722 .prepare
= i2sbus_playback_prepare
,
723 .trigger
= i2sbus_playback_trigger
,
724 .pointer
= i2sbus_playback_pointer
,
727 static int i2sbus_record_open(struct snd_pcm_substream
*substream
)
729 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
733 i2sdev
->in
.substream
= substream
;
734 return i2sbus_pcm_open(i2sdev
, 1);
737 static int i2sbus_record_close(struct snd_pcm_substream
*substream
)
739 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
744 if (i2sdev
->in
.substream
!= substream
)
746 err
= i2sbus_pcm_close(i2sdev
, 1);
748 i2sdev
->in
.substream
= NULL
;
752 static int i2sbus_record_prepare(struct snd_pcm_substream
*substream
)
754 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
758 if (i2sdev
->in
.substream
!= substream
)
760 return i2sbus_pcm_prepare(i2sdev
, 1);
763 static int i2sbus_record_trigger(struct snd_pcm_substream
*substream
, int cmd
)
765 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
769 if (i2sdev
->in
.substream
!= substream
)
771 return i2sbus_pcm_trigger(i2sdev
, 1, cmd
);
774 static snd_pcm_uframes_t
i2sbus_record_pointer(struct snd_pcm_substream
777 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
781 if (i2sdev
->in
.substream
!= substream
)
783 return i2sbus_pcm_pointer(i2sdev
, 1);
786 static struct snd_pcm_ops i2sbus_record_ops
= {
787 .open
= i2sbus_record_open
,
788 .close
= i2sbus_record_close
,
789 .ioctl
= snd_pcm_lib_ioctl
,
790 .hw_params
= i2sbus_hw_params
,
791 .hw_free
= i2sbus_hw_free
,
792 .prepare
= i2sbus_record_prepare
,
793 .trigger
= i2sbus_record_trigger
,
794 .pointer
= i2sbus_record_pointer
,
797 static void i2sbus_private_free(struct snd_pcm
*pcm
)
799 struct i2sbus_dev
*i2sdev
= snd_pcm_chip(pcm
);
800 struct codec_info_item
*p
, *tmp
;
802 i2sdev
->sound
.pcm
= NULL
;
803 i2sdev
->out
.created
= 0;
804 i2sdev
->in
.created
= 0;
805 list_for_each_entry_safe(p
, tmp
, &i2sdev
->sound
.codec_list
, list
) {
806 printk(KERN_ERR
"i2sbus: a codec didn't unregister!\n");
808 module_put(p
->codec
->owner
);
811 soundbus_dev_put(&i2sdev
->sound
);
812 module_put(THIS_MODULE
);
815 /* FIXME: this function needs an error handling strategy with labels */
817 i2sbus_attach_codec(struct soundbus_dev
*dev
, struct snd_card
*card
,
818 struct codec_info
*ci
, void *data
)
820 int err
, in
= 0, out
= 0;
821 struct transfer_info
*tmp
;
822 struct i2sbus_dev
*i2sdev
= soundbus_dev_to_i2sbus_dev(dev
);
823 struct codec_info_item
*cii
;
825 if (!dev
->pcmname
|| dev
->pcmid
== -1) {
826 printk(KERN_ERR
"i2sbus: pcm name and id must be set!\n");
830 list_for_each_entry(cii
, &dev
->codec_list
, list
) {
831 if (cii
->codec_data
== data
)
835 if (!ci
->transfers
|| !ci
->transfers
->formats
836 || !ci
->transfers
->rates
|| !ci
->usable
)
839 /* we currently code the i2s transfer on the clock, and support only
841 if (ci
->bus_factor
!= 32 && ci
->bus_factor
!= 64)
844 /* If you want to fix this, you need to keep track of what transport infos
845 * are to be used, which codecs they belong to, and then fix all the
846 * sysclock/busclock stuff above to depend on which is usable */
847 list_for_each_entry(cii
, &dev
->codec_list
, list
) {
848 if (cii
->codec
->sysclock_factor
!= ci
->sysclock_factor
) {
850 "cannot yet handle multiple different sysclocks!\n");
853 if (cii
->codec
->bus_factor
!= ci
->bus_factor
) {
855 "cannot yet handle multiple different bus clocks!\n");
861 while (tmp
->formats
&& tmp
->rates
) {
862 if (tmp
->transfer_in
)
869 cii
= kzalloc(sizeof(struct codec_info_item
), GFP_KERNEL
);
871 printk(KERN_DEBUG
"i2sbus: failed to allocate cii\n");
875 /* use the private data to point to the codec info */
876 cii
->sdev
= soundbus_dev_get(dev
);
878 cii
->codec_data
= data
;
882 "i2sbus: failed to get soundbus dev reference\n");
887 if (!try_module_get(THIS_MODULE
)) {
888 printk(KERN_DEBUG
"i2sbus: failed to get module reference!\n");
889 soundbus_dev_put(dev
);
894 if (!try_module_get(ci
->owner
)) {
896 "i2sbus: failed to get module reference to codec owner!\n");
897 module_put(THIS_MODULE
);
898 soundbus_dev_put(dev
);
904 err
= snd_pcm_new(card
,
911 printk(KERN_DEBUG
"i2sbus: failed to create pcm\n");
913 module_put(ci
->owner
);
914 soundbus_dev_put(dev
);
915 module_put(THIS_MODULE
);
920 /* ALSA yet again sucks.
921 * If it is ever fixed, remove this line. See below. */
924 if (!i2sdev
->out
.created
&& out
) {
925 if (dev
->pcm
->card
!= card
) {
928 "Can't attach same bus to different cards!\n");
929 module_put(ci
->owner
);
931 soundbus_dev_put(dev
);
932 module_put(THIS_MODULE
);
936 snd_pcm_new_stream(dev
->pcm
, SNDRV_PCM_STREAM_PLAYBACK
, 1))) {
937 module_put(ci
->owner
);
939 soundbus_dev_put(dev
);
940 module_put(THIS_MODULE
);
943 snd_pcm_set_ops(dev
->pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
944 &i2sbus_playback_ops
);
945 i2sdev
->out
.created
= 1;
948 if (!i2sdev
->in
.created
&& in
) {
949 if (dev
->pcm
->card
!= card
) {
951 "Can't attach same bus to different cards!\n");
952 module_put(ci
->owner
);
954 soundbus_dev_put(dev
);
955 module_put(THIS_MODULE
);
959 snd_pcm_new_stream(dev
->pcm
, SNDRV_PCM_STREAM_CAPTURE
, 1))) {
960 module_put(ci
->owner
);
962 soundbus_dev_put(dev
);
963 module_put(THIS_MODULE
);
966 snd_pcm_set_ops(dev
->pcm
, SNDRV_PCM_STREAM_CAPTURE
,
968 i2sdev
->in
.created
= 1;
971 /* so we have to register the pcm after adding any substream
972 * to it because alsa doesn't create the devices for the
973 * substreams when we add them later.
974 * Therefore, force in and out on both busses (above) and
975 * register the pcm now instead of just after creating it.
977 err
= snd_device_register(card
, dev
->pcm
);
979 printk(KERN_ERR
"i2sbus: error registering new pcm\n");
980 module_put(ci
->owner
);
982 soundbus_dev_put(dev
);
983 module_put(THIS_MODULE
);
986 /* no errors any more, so let's add this to our list */
987 list_add(&cii
->list
, &dev
->codec_list
);
989 dev
->pcm
->private_data
= i2sdev
;
990 dev
->pcm
->private_free
= i2sbus_private_free
;
992 /* well, we really should support scatter/gather DMA */
993 snd_pcm_lib_preallocate_pages_for_all(
994 dev
->pcm
, SNDRV_DMA_TYPE_DEV
,
995 snd_dma_pci_data(macio_get_pci_dev(i2sdev
->macio
)),
996 64 * 1024, 64 * 1024);
1001 void i2sbus_detach_codec(struct soundbus_dev
*dev
, void *data
)
1003 struct codec_info_item
*cii
= NULL
, *i
;
1005 list_for_each_entry(i
, &dev
->codec_list
, list
) {
1006 if (i
->codec_data
== data
) {
1012 list_del(&cii
->list
);
1013 module_put(cii
->codec
->owner
);
1016 /* no more codecs, but still a pcm? */
1017 if (list_empty(&dev
->codec_list
) && dev
->pcm
) {
1018 /* the actual cleanup is done by the callback above! */
1019 snd_device_free(dev
->pcm
->card
, dev
->pcm
);