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 #include <linux/slab.h>
12 #include <sound/core.h>
13 #include <asm/macio.h>
14 #include <linux/pci.h>
15 #include "../soundbus.h"
18 static inline void get_pcm_info(struct i2sbus_dev
*i2sdev
, int in
,
19 struct pcm_info
**pi
, struct pcm_info
**other
)
25 *other
= &i2sdev
->out
;
34 static int clock_and_divisors(int mclk
, int sclk
, int rate
, int *out
)
36 /* sclk must be derived from mclk! */
39 /* derive sclk register value */
40 if (i2s_sf_sclkdiv(mclk
/ sclk
, out
))
43 if (I2S_CLOCK_SPEED_18MHz
% (rate
* mclk
) == 0) {
44 if (!i2s_sf_mclkdiv(I2S_CLOCK_SPEED_18MHz
/ (rate
* mclk
), out
)) {
45 *out
|= I2S_SF_CLOCK_SOURCE_18MHz
;
49 if (I2S_CLOCK_SPEED_45MHz
% (rate
* mclk
) == 0) {
50 if (!i2s_sf_mclkdiv(I2S_CLOCK_SPEED_45MHz
/ (rate
* mclk
), out
)) {
51 *out
|= I2S_SF_CLOCK_SOURCE_45MHz
;
55 if (I2S_CLOCK_SPEED_49MHz
% (rate
* mclk
) == 0) {
56 if (!i2s_sf_mclkdiv(I2S_CLOCK_SPEED_49MHz
/ (rate
* mclk
), out
)) {
57 *out
|= I2S_SF_CLOCK_SOURCE_49MHz
;
64 #define CHECK_RATE(rate) \
65 do { if (rates & SNDRV_PCM_RATE_ ##rate) { \
67 if (clock_and_divisors(sysclock_factor, \
68 bus_factor, rate, &dummy)) \
69 rates &= ~SNDRV_PCM_RATE_ ##rate; \
72 static int i2sbus_pcm_open(struct i2sbus_dev
*i2sdev
, int in
)
74 struct pcm_info
*pi
, *other
;
75 struct soundbus_dev
*sdev
;
76 int masks_inited
= 0, err
;
77 struct codec_info_item
*cii
, *rev
;
78 struct snd_pcm_hardware
*hw
;
80 unsigned int rates
= 0;
81 struct transfer_info v
;
83 int bus_factor
= 0, sysclock_factor
= 0;
86 mutex_lock(&i2sdev
->lock
);
88 get_pcm_info(i2sdev
, in
, &pi
, &other
);
90 hw
= &pi
->substream
->runtime
->hw
;
91 sdev
= &i2sdev
->sound
;
99 /* we now need to assign the hw */
100 list_for_each_entry(cii
, &sdev
->codec_list
, list
) {
101 struct transfer_info
*ti
= cii
->codec
->transfers
;
102 bus_factor
= cii
->codec
->bus_factor
;
103 sysclock_factor
= cii
->codec
->sysclock_factor
;
104 while (ti
->formats
&& ti
->rates
) {
106 if (ti
->transfer_in
== in
107 && cii
->codec
->usable(cii
, ti
, &v
)) {
109 formats
&= v
.formats
;
120 if (!masks_inited
|| !bus_factor
|| !sysclock_factor
) {
124 /* bus dependent stuff */
125 hw
->info
= SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_MMAP_VALID
|
126 SNDRV_PCM_INFO_INTERLEAVED
| SNDRV_PCM_INFO_RESUME
|
127 SNDRV_PCM_INFO_JOINT_DUPLEX
;
144 /* well. the codec might want 24 bits only, and we'll
145 * ever only transfer 24 bits, but they are top-aligned!
146 * So for alsa, we claim that we're doing full 32 bit
147 * while in reality we'll ignore the lower 8 bits of
148 * that when doing playback (they're transferred as 0
149 * as far as I know, no codecs we have are 32-bit capable
150 * so I can't really test) and when doing recording we'll
151 * always have those lower 8 bits recorded as 0 */
152 if (formats
& SNDRV_PCM_FMTBIT_S24_BE
)
153 formats
|= SNDRV_PCM_FMTBIT_S32_BE
;
154 if (formats
& SNDRV_PCM_FMTBIT_U24_BE
)
155 formats
|= SNDRV_PCM_FMTBIT_U32_BE
;
156 /* now mask off what we can support. I suppose we could
157 * also support S24_3LE and some similar formats, but I
158 * doubt there's a codec that would be able to use that,
159 * so we don't support it here. */
160 hw
->formats
= formats
& (SNDRV_PCM_FMTBIT_S16_BE
|
161 SNDRV_PCM_FMTBIT_U16_BE
|
162 SNDRV_PCM_FMTBIT_S32_BE
|
163 SNDRV_PCM_FMTBIT_U32_BE
);
165 /* we need to set the highest and lowest rate possible.
166 * These are the highest and lowest rates alsa can
167 * support properly in its bitfield.
168 * Below, we'll use that to restrict to the rate
169 * currently in use (if any). */
171 hw
->rate_max
= 192000;
172 /* if the other stream is active, then we can only
173 * support what it is currently using.
174 * FIXME: I lied. This comment is wrong. We can support
175 * anything that works with the same serial format, ie.
176 * when recording 24 bit sound we can well play 16 bit
177 * sound at the same time iff using the same transfer mode.
180 /* FIXME: is this guaranteed by the alsa api? */
181 hw
->formats
&= (1ULL << i2sdev
->format
);
182 /* see above, restrict rates to the one we already have */
183 hw
->rate_min
= i2sdev
->rate
;
184 hw
->rate_max
= i2sdev
->rate
;
187 hw
->channels_min
= 2;
188 hw
->channels_max
= 2;
189 /* these are somewhat arbitrary */
190 hw
->buffer_bytes_max
= 131072;
191 hw
->period_bytes_min
= 256;
192 hw
->period_bytes_max
= 16384;
194 hw
->periods_max
= MAX_DBDMA_COMMANDS
;
195 err
= snd_pcm_hw_constraint_integer(pi
->substream
->runtime
,
196 SNDRV_PCM_HW_PARAM_PERIODS
);
201 list_for_each_entry(cii
, &sdev
->codec_list
, list
) {
202 if (cii
->codec
->open
) {
203 err
= cii
->codec
->open(cii
, pi
->substream
);
208 list_for_each_entry_reverse(rev
,
209 &sdev
->codec_list
, list
) {
210 if (found_this
&& rev
->codec
->close
) {
211 rev
->codec
->close(rev
,
223 mutex_unlock(&i2sdev
->lock
);
229 static int i2sbus_pcm_close(struct i2sbus_dev
*i2sdev
, int in
)
231 struct codec_info_item
*cii
;
235 mutex_lock(&i2sdev
->lock
);
237 get_pcm_info(i2sdev
, in
, &pi
, NULL
);
239 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
) {
240 if (cii
->codec
->close
) {
241 tmp
= cii
->codec
->close(cii
, pi
->substream
);
247 pi
->substream
= NULL
;
249 mutex_unlock(&i2sdev
->lock
);
253 static void i2sbus_wait_for_stop(struct i2sbus_dev
*i2sdev
,
257 struct completion done
;
260 spin_lock_irqsave(&i2sdev
->low_lock
, flags
);
261 if (pi
->dbdma_ring
.stopping
) {
262 init_completion(&done
);
263 pi
->stop_completion
= &done
;
264 spin_unlock_irqrestore(&i2sdev
->low_lock
, flags
);
265 timeout
= wait_for_completion_timeout(&done
, HZ
);
266 spin_lock_irqsave(&i2sdev
->low_lock
, flags
);
267 pi
->stop_completion
= NULL
;
269 /* timeout expired, stop dbdma forcefully */
270 printk(KERN_ERR
"i2sbus_wait_for_stop: timed out\n");
271 /* make sure RUN, PAUSE and S0 bits are cleared */
272 out_le32(&pi
->dbdma
->control
, (RUN
| PAUSE
| 1) << 16);
273 pi
->dbdma_ring
.stopping
= 0;
275 while (in_le32(&pi
->dbdma
->status
) & ACTIVE
) {
282 spin_unlock_irqrestore(&i2sdev
->low_lock
, flags
);
286 void i2sbus_wait_for_stop_both(struct i2sbus_dev
*i2sdev
)
290 get_pcm_info(i2sdev
, 0, &pi
, NULL
);
291 i2sbus_wait_for_stop(i2sdev
, pi
);
292 get_pcm_info(i2sdev
, 1, &pi
, NULL
);
293 i2sbus_wait_for_stop(i2sdev
, pi
);
297 static int i2sbus_hw_params(struct snd_pcm_substream
*substream
,
298 struct snd_pcm_hw_params
*params
)
300 return snd_pcm_lib_malloc_pages(substream
, params_buffer_bytes(params
));
303 static inline int i2sbus_hw_free(struct snd_pcm_substream
*substream
, int in
)
305 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
308 get_pcm_info(i2sdev
, in
, &pi
, NULL
);
309 if (pi
->dbdma_ring
.stopping
)
310 i2sbus_wait_for_stop(i2sdev
, pi
);
311 snd_pcm_lib_free_pages(substream
);
315 static int i2sbus_playback_hw_free(struct snd_pcm_substream
*substream
)
317 return i2sbus_hw_free(substream
, 0);
320 static int i2sbus_record_hw_free(struct snd_pcm_substream
*substream
)
322 return i2sbus_hw_free(substream
, 1);
325 static int i2sbus_pcm_prepare(struct i2sbus_dev
*i2sdev
, int in
)
327 /* whee. Hard work now. The user has selected a bitrate
328 * and bit format, so now we have to program our
329 * I2S controller appropriately. */
330 struct snd_pcm_runtime
*runtime
;
331 struct dbdma_cmd
*command
;
332 int i
, periodsize
, nperiods
;
335 struct codec_info_item
*cii
;
336 int sfr
= 0; /* serial format register */
337 int dws
= 0; /* data word sizes reg */
339 struct pcm_info
*pi
, *other
;
342 unsigned int cmd
, stopaddr
;
344 mutex_lock(&i2sdev
->lock
);
346 get_pcm_info(i2sdev
, in
, &pi
, &other
);
348 if (pi
->dbdma_ring
.running
) {
352 if (pi
->dbdma_ring
.stopping
)
353 i2sbus_wait_for_stop(i2sdev
, pi
);
355 if (!pi
->substream
|| !pi
->substream
->runtime
) {
360 runtime
= pi
->substream
->runtime
;
363 ((i2sdev
->format
!= runtime
->format
)
364 || (i2sdev
->rate
!= runtime
->rate
))) {
369 i2sdev
->format
= runtime
->format
;
370 i2sdev
->rate
= runtime
->rate
;
372 periodsize
= snd_pcm_lib_period_bytes(pi
->substream
);
373 nperiods
= pi
->substream
->runtime
->periods
;
374 pi
->current_period
= 0;
376 /* generate dbdma command ring first */
377 command
= pi
->dbdma_ring
.cmds
;
378 memset(command
, 0, (nperiods
+ 2) * sizeof(struct dbdma_cmd
));
380 /* commands to DMA to/from the ring */
382 * For input, we need to do a graceful stop; if we abort
383 * the DMA, we end up with leftover bytes that corrupt
384 * the next recording. To do this we set the S0 status
385 * bit and wait for the DMA controller to stop. Each
386 * command has a branch condition to
387 * make it branch to a stop command if S0 is set.
388 * On input we also need to wait for the S7 bit to be
389 * set before turning off the DMA controller.
390 * In fact we do the graceful stop for output as well.
392 offset
= runtime
->dma_addr
;
393 cmd
= (in
? INPUT_MORE
: OUTPUT_MORE
) | BR_IFSET
| INTR_ALWAYS
;
394 stopaddr
= pi
->dbdma_ring
.bus_cmd_start
+
395 (nperiods
+ 1) * sizeof(struct dbdma_cmd
);
396 for (i
= 0; i
< nperiods
; i
++, command
++, offset
+= periodsize
) {
397 command
->command
= cpu_to_le16(cmd
);
398 command
->cmd_dep
= cpu_to_le32(stopaddr
);
399 command
->phy_addr
= cpu_to_le32(offset
);
400 command
->req_count
= cpu_to_le16(periodsize
);
403 /* branch back to beginning of ring */
404 command
->command
= cpu_to_le16(DBDMA_NOP
| BR_ALWAYS
);
405 command
->cmd_dep
= cpu_to_le32(pi
->dbdma_ring
.bus_cmd_start
);
408 /* set stop command */
409 command
->command
= cpu_to_le16(DBDMA_STOP
);
411 /* ok, let's set the serial format and stuff */
412 switch (runtime
->format
) {
414 case SNDRV_PCM_FORMAT_S16_BE
:
415 case SNDRV_PCM_FORMAT_U16_BE
:
416 /* FIXME: if we add different bus factors we need to
419 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
) {
420 bi
.bus_factor
= cii
->codec
->bus_factor
;
423 if (!bi
.bus_factor
) {
429 case SNDRV_PCM_FORMAT_S32_BE
:
430 case SNDRV_PCM_FORMAT_U32_BE
:
431 /* force 64x bus speed, otherwise the data cannot be
432 * transferred quickly enough! */
440 /* we assume all sysclocks are the same! */
441 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
) {
442 bi
.sysclock_factor
= cii
->codec
->sysclock_factor
;
446 if (clock_and_divisors(bi
.sysclock_factor
,
453 switch (bi
.bus_factor
) {
455 sfr
|= I2S_SF_SERIAL_FORMAT_I2S_32X
;
458 sfr
|= I2S_SF_SERIAL_FORMAT_I2S_64X
;
461 /* FIXME: THIS ASSUMES MASTER ALL THE TIME */
462 sfr
|= I2S_SF_SCLK_MASTER
;
464 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
) {
466 if (cii
->codec
->prepare
)
467 err
= cii
->codec
->prepare(cii
, &bi
, pi
->substream
);
473 /* codecs are fine with it, so set our clocks */
475 dws
= (2 << I2S_DWS_NUM_CHANNELS_IN_SHIFT
) |
476 (2 << I2S_DWS_NUM_CHANNELS_OUT_SHIFT
) |
477 I2S_DWS_DATA_IN_16BIT
| I2S_DWS_DATA_OUT_16BIT
;
479 dws
= (2 << I2S_DWS_NUM_CHANNELS_IN_SHIFT
) |
480 (2 << I2S_DWS_NUM_CHANNELS_OUT_SHIFT
) |
481 I2S_DWS_DATA_IN_24BIT
| I2S_DWS_DATA_OUT_24BIT
;
483 /* early exit if already programmed correctly */
484 /* not locking these is fine since we touch them only in this function */
485 if (in_le32(&i2sdev
->intfregs
->serial_format
) == sfr
486 && in_le32(&i2sdev
->intfregs
->data_word_sizes
) == dws
)
489 /* let's notify the codecs about clocks going away.
490 * For now we only do mastering on the i2s cell... */
491 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
)
492 if (cii
->codec
->switch_clock
)
493 cii
->codec
->switch_clock(cii
, CLOCK_SWITCH_PREPARE_SLAVE
);
495 i2sbus_control_enable(i2sdev
->control
, i2sdev
);
496 i2sbus_control_cell(i2sdev
->control
, i2sdev
, 1);
498 out_le32(&i2sdev
->intfregs
->intr_ctl
, I2S_PENDING_CLOCKS_STOPPED
);
500 i2sbus_control_clock(i2sdev
->control
, i2sdev
, 0);
504 /* wait for clock stopped. This can apparently take a while... */
507 !(in_le32(&i2sdev
->intfregs
->intr_ctl
) & I2S_PENDING_CLOCKS_STOPPED
)) {
510 out_le32(&i2sdev
->intfregs
->intr_ctl
, I2S_PENDING_CLOCKS_STOPPED
);
512 /* not locking these is fine since we touch them only in this function */
513 out_le32(&i2sdev
->intfregs
->serial_format
, sfr
);
514 out_le32(&i2sdev
->intfregs
->data_word_sizes
, dws
);
516 i2sbus_control_enable(i2sdev
->control
, i2sdev
);
517 i2sbus_control_cell(i2sdev
->control
, i2sdev
, 1);
518 i2sbus_control_clock(i2sdev
->control
, i2sdev
, 1);
521 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
)
522 if (cii
->codec
->switch_clock
)
523 cii
->codec
->switch_clock(cii
, CLOCK_SWITCH_SLAVE
);
526 mutex_unlock(&i2sdev
->lock
);
531 void i2sbus_pcm_prepare_both(struct i2sbus_dev
*i2sdev
)
533 i2sbus_pcm_prepare(i2sdev
, 0);
534 i2sbus_pcm_prepare(i2sdev
, 1);
538 static int i2sbus_pcm_trigger(struct i2sbus_dev
*i2sdev
, int in
, int cmd
)
540 struct codec_info_item
*cii
;
545 spin_lock_irqsave(&i2sdev
->low_lock
, flags
);
547 get_pcm_info(i2sdev
, in
, &pi
, NULL
);
550 case SNDRV_PCM_TRIGGER_START
:
551 case SNDRV_PCM_TRIGGER_RESUME
:
552 if (pi
->dbdma_ring
.running
) {
556 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
)
557 if (cii
->codec
->start
)
558 cii
->codec
->start(cii
, pi
->substream
);
559 pi
->dbdma_ring
.running
= 1;
561 if (pi
->dbdma_ring
.stopping
) {
562 /* Clear the S0 bit, then see if we stopped yet */
563 out_le32(&pi
->dbdma
->control
, 1 << 16);
564 if (in_le32(&pi
->dbdma
->status
) & ACTIVE
) {
565 /* possible race here? */
567 if (in_le32(&pi
->dbdma
->status
) & ACTIVE
) {
568 pi
->dbdma_ring
.stopping
= 0;
569 goto out_unlock
; /* keep running */
574 /* make sure RUN, PAUSE and S0 bits are cleared */
575 out_le32(&pi
->dbdma
->control
, (RUN
| PAUSE
| 1) << 16);
577 /* set branch condition select register */
578 out_le32(&pi
->dbdma
->br_sel
, (1 << 16) | 1);
580 /* write dma command buffer address to the dbdma chip */
581 out_le32(&pi
->dbdma
->cmdptr
, pi
->dbdma_ring
.bus_cmd_start
);
583 /* initialize the frame count and current period */
584 pi
->current_period
= 0;
585 pi
->frame_count
= in_le32(&i2sdev
->intfregs
->frame_count
);
587 /* set the DMA controller running */
588 out_le32(&pi
->dbdma
->control
, (RUN
<< 16) | RUN
);
593 case SNDRV_PCM_TRIGGER_STOP
:
594 case SNDRV_PCM_TRIGGER_SUSPEND
:
595 if (!pi
->dbdma_ring
.running
) {
599 pi
->dbdma_ring
.running
= 0;
601 /* Set the S0 bit to make the DMA branch to the stop cmd */
602 out_le32(&pi
->dbdma
->control
, (1 << 16) | 1);
603 pi
->dbdma_ring
.stopping
= 1;
605 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
)
606 if (cii
->codec
->stop
)
607 cii
->codec
->stop(cii
, pi
->substream
);
615 spin_unlock_irqrestore(&i2sdev
->low_lock
, flags
);
619 static snd_pcm_uframes_t
i2sbus_pcm_pointer(struct i2sbus_dev
*i2sdev
, int in
)
624 get_pcm_info(i2sdev
, in
, &pi
, NULL
);
626 fc
= in_le32(&i2sdev
->intfregs
->frame_count
);
627 fc
= fc
- pi
->frame_count
;
629 if (fc
>= pi
->substream
->runtime
->buffer_size
)
630 fc
%= pi
->substream
->runtime
->buffer_size
;
634 static inline void handle_interrupt(struct i2sbus_dev
*i2sdev
, int in
)
641 struct snd_pcm_runtime
*runtime
;
643 spin_lock(&i2sdev
->low_lock
);
644 get_pcm_info(i2sdev
, in
, &pi
, NULL
);
645 if (!pi
->dbdma_ring
.running
&& !pi
->dbdma_ring
.stopping
)
648 i
= pi
->current_period
;
649 runtime
= pi
->substream
->runtime
;
650 while (pi
->dbdma_ring
.cmds
[i
].xfer_status
) {
651 if (le16_to_cpu(pi
->dbdma_ring
.cmds
[i
].xfer_status
) & BT
)
653 * BT is the branch taken bit. If it took a branch
654 * it is because we set the S0 bit to make it
655 * branch to the stop command.
658 pi
->dbdma_ring
.cmds
[i
].xfer_status
= 0;
660 if (++i
>= runtime
->periods
) {
662 pi
->frame_count
+= runtime
->buffer_size
;
664 pi
->current_period
= i
;
667 * Check the frame count. The DMA tends to get a bit
668 * ahead of the frame counter, which confuses the core.
670 fc
= in_le32(&i2sdev
->intfregs
->frame_count
);
671 nframes
= i
* runtime
->period_size
;
672 if (fc
< pi
->frame_count
+ nframes
)
673 pi
->frame_count
= fc
- nframes
;
679 status
= in_le32(&pi
->dbdma
->status
);
680 if (!(status
& ACTIVE
) && (!in
|| (status
& 0x80)))
682 if (--timeout
<= 0) {
683 printk(KERN_ERR
"i2sbus: timed out "
684 "waiting for DMA to stop!\n");
690 /* Turn off DMA controller, clear S0 bit */
691 out_le32(&pi
->dbdma
->control
, (RUN
| PAUSE
| 1) << 16);
693 pi
->dbdma_ring
.stopping
= 0;
694 if (pi
->stop_completion
)
695 complete(pi
->stop_completion
);
698 if (!pi
->dbdma_ring
.running
)
700 spin_unlock(&i2sdev
->low_lock
);
701 /* may call _trigger again, hence needs to be unlocked */
702 snd_pcm_period_elapsed(pi
->substream
);
706 spin_unlock(&i2sdev
->low_lock
);
709 irqreturn_t
i2sbus_tx_intr(int irq
, void *devid
)
711 handle_interrupt((struct i2sbus_dev
*)devid
, 0);
715 irqreturn_t
i2sbus_rx_intr(int irq
, void *devid
)
717 handle_interrupt((struct i2sbus_dev
*)devid
, 1);
721 static int i2sbus_playback_open(struct snd_pcm_substream
*substream
)
723 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
727 i2sdev
->out
.substream
= substream
;
728 return i2sbus_pcm_open(i2sdev
, 0);
731 static int i2sbus_playback_close(struct snd_pcm_substream
*substream
)
733 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
738 if (i2sdev
->out
.substream
!= substream
)
740 err
= i2sbus_pcm_close(i2sdev
, 0);
742 i2sdev
->out
.substream
= NULL
;
746 static int i2sbus_playback_prepare(struct snd_pcm_substream
*substream
)
748 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
752 if (i2sdev
->out
.substream
!= substream
)
754 return i2sbus_pcm_prepare(i2sdev
, 0);
757 static int i2sbus_playback_trigger(struct snd_pcm_substream
*substream
, int cmd
)
759 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
763 if (i2sdev
->out
.substream
!= substream
)
765 return i2sbus_pcm_trigger(i2sdev
, 0, cmd
);
768 static snd_pcm_uframes_t
i2sbus_playback_pointer(struct snd_pcm_substream
771 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
775 if (i2sdev
->out
.substream
!= substream
)
777 return i2sbus_pcm_pointer(i2sdev
, 0);
780 static struct snd_pcm_ops i2sbus_playback_ops
= {
781 .open
= i2sbus_playback_open
,
782 .close
= i2sbus_playback_close
,
783 .ioctl
= snd_pcm_lib_ioctl
,
784 .hw_params
= i2sbus_hw_params
,
785 .hw_free
= i2sbus_playback_hw_free
,
786 .prepare
= i2sbus_playback_prepare
,
787 .trigger
= i2sbus_playback_trigger
,
788 .pointer
= i2sbus_playback_pointer
,
791 static int i2sbus_record_open(struct snd_pcm_substream
*substream
)
793 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
797 i2sdev
->in
.substream
= substream
;
798 return i2sbus_pcm_open(i2sdev
, 1);
801 static int i2sbus_record_close(struct snd_pcm_substream
*substream
)
803 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
808 if (i2sdev
->in
.substream
!= substream
)
810 err
= i2sbus_pcm_close(i2sdev
, 1);
812 i2sdev
->in
.substream
= NULL
;
816 static int i2sbus_record_prepare(struct snd_pcm_substream
*substream
)
818 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
822 if (i2sdev
->in
.substream
!= substream
)
824 return i2sbus_pcm_prepare(i2sdev
, 1);
827 static int i2sbus_record_trigger(struct snd_pcm_substream
*substream
, int cmd
)
829 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
833 if (i2sdev
->in
.substream
!= substream
)
835 return i2sbus_pcm_trigger(i2sdev
, 1, cmd
);
838 static snd_pcm_uframes_t
i2sbus_record_pointer(struct snd_pcm_substream
841 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
845 if (i2sdev
->in
.substream
!= substream
)
847 return i2sbus_pcm_pointer(i2sdev
, 1);
850 static struct snd_pcm_ops i2sbus_record_ops
= {
851 .open
= i2sbus_record_open
,
852 .close
= i2sbus_record_close
,
853 .ioctl
= snd_pcm_lib_ioctl
,
854 .hw_params
= i2sbus_hw_params
,
855 .hw_free
= i2sbus_record_hw_free
,
856 .prepare
= i2sbus_record_prepare
,
857 .trigger
= i2sbus_record_trigger
,
858 .pointer
= i2sbus_record_pointer
,
861 static void i2sbus_private_free(struct snd_pcm
*pcm
)
863 struct i2sbus_dev
*i2sdev
= snd_pcm_chip(pcm
);
864 struct codec_info_item
*p
, *tmp
;
866 i2sdev
->sound
.pcm
= NULL
;
867 i2sdev
->out
.created
= 0;
868 i2sdev
->in
.created
= 0;
869 list_for_each_entry_safe(p
, tmp
, &i2sdev
->sound
.codec_list
, list
) {
870 printk(KERN_ERR
"i2sbus: a codec didn't unregister!\n");
872 module_put(p
->codec
->owner
);
875 soundbus_dev_put(&i2sdev
->sound
);
876 module_put(THIS_MODULE
);
880 i2sbus_attach_codec(struct soundbus_dev
*dev
, struct snd_card
*card
,
881 struct codec_info
*ci
, void *data
)
883 int err
, in
= 0, out
= 0;
884 struct transfer_info
*tmp
;
885 struct i2sbus_dev
*i2sdev
= soundbus_dev_to_i2sbus_dev(dev
);
886 struct codec_info_item
*cii
;
888 if (!dev
->pcmname
|| dev
->pcmid
== -1) {
889 printk(KERN_ERR
"i2sbus: pcm name and id must be set!\n");
893 list_for_each_entry(cii
, &dev
->codec_list
, list
) {
894 if (cii
->codec_data
== data
)
898 if (!ci
->transfers
|| !ci
->transfers
->formats
899 || !ci
->transfers
->rates
|| !ci
->usable
)
902 /* we currently code the i2s transfer on the clock, and support only
904 if (ci
->bus_factor
!= 32 && ci
->bus_factor
!= 64)
907 /* If you want to fix this, you need to keep track of what transport infos
908 * are to be used, which codecs they belong to, and then fix all the
909 * sysclock/busclock stuff above to depend on which is usable */
910 list_for_each_entry(cii
, &dev
->codec_list
, list
) {
911 if (cii
->codec
->sysclock_factor
!= ci
->sysclock_factor
) {
913 "cannot yet handle multiple different sysclocks!\n");
916 if (cii
->codec
->bus_factor
!= ci
->bus_factor
) {
918 "cannot yet handle multiple different bus clocks!\n");
924 while (tmp
->formats
&& tmp
->rates
) {
925 if (tmp
->transfer_in
)
932 cii
= kzalloc(sizeof(struct codec_info_item
), GFP_KERNEL
);
934 printk(KERN_DEBUG
"i2sbus: failed to allocate cii\n");
938 /* use the private data to point to the codec info */
939 cii
->sdev
= soundbus_dev_get(dev
);
941 cii
->codec_data
= data
;
945 "i2sbus: failed to get soundbus dev reference\n");
950 if (!try_module_get(THIS_MODULE
)) {
951 printk(KERN_DEBUG
"i2sbus: failed to get module reference!\n");
956 if (!try_module_get(ci
->owner
)) {
958 "i2sbus: failed to get module reference to codec owner!\n");
960 goto out_put_this_module
;
964 err
= snd_pcm_new(card
, dev
->pcmname
, dev
->pcmid
, 0, 0,
967 printk(KERN_DEBUG
"i2sbus: failed to create pcm\n");
968 goto out_put_ci_module
;
970 dev
->pcm
->dev
= &dev
->ofdev
.dev
;
973 /* ALSA yet again sucks.
974 * If it is ever fixed, remove this line. See below. */
977 if (!i2sdev
->out
.created
&& out
) {
978 if (dev
->pcm
->card
!= card
) {
981 "Can't attach same bus to different cards!\n");
983 goto out_put_ci_module
;
985 err
= snd_pcm_new_stream(dev
->pcm
, SNDRV_PCM_STREAM_PLAYBACK
, 1);
987 goto out_put_ci_module
;
988 snd_pcm_set_ops(dev
->pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
989 &i2sbus_playback_ops
);
990 i2sdev
->out
.created
= 1;
993 if (!i2sdev
->in
.created
&& in
) {
994 if (dev
->pcm
->card
!= card
) {
996 "Can't attach same bus to different cards!\n");
998 goto out_put_ci_module
;
1000 err
= snd_pcm_new_stream(dev
->pcm
, SNDRV_PCM_STREAM_CAPTURE
, 1);
1002 goto out_put_ci_module
;
1003 snd_pcm_set_ops(dev
->pcm
, SNDRV_PCM_STREAM_CAPTURE
,
1004 &i2sbus_record_ops
);
1005 i2sdev
->in
.created
= 1;
1008 /* so we have to register the pcm after adding any substream
1009 * to it because alsa doesn't create the devices for the
1010 * substreams when we add them later.
1011 * Therefore, force in and out on both busses (above) and
1012 * register the pcm now instead of just after creating it.
1014 err
= snd_device_register(card
, dev
->pcm
);
1016 printk(KERN_ERR
"i2sbus: error registering new pcm\n");
1017 goto out_put_ci_module
;
1019 /* no errors any more, so let's add this to our list */
1020 list_add(&cii
->list
, &dev
->codec_list
);
1022 dev
->pcm
->private_data
= i2sdev
;
1023 dev
->pcm
->private_free
= i2sbus_private_free
;
1025 /* well, we really should support scatter/gather DMA */
1026 snd_pcm_lib_preallocate_pages_for_all(
1027 dev
->pcm
, SNDRV_DMA_TYPE_DEV
,
1028 snd_dma_pci_data(macio_get_pci_dev(i2sdev
->macio
)),
1029 64 * 1024, 64 * 1024);
1033 module_put(ci
->owner
);
1034 out_put_this_module
:
1035 module_put(THIS_MODULE
);
1037 soundbus_dev_put(dev
);
1043 void i2sbus_detach_codec(struct soundbus_dev
*dev
, void *data
)
1045 struct codec_info_item
*cii
= NULL
, *i
;
1047 list_for_each_entry(i
, &dev
->codec_list
, list
) {
1048 if (i
->codec_data
== data
) {
1054 list_del(&cii
->list
);
1055 module_put(cii
->codec
->owner
);
1058 /* no more codecs, but still a pcm? */
1059 if (list_empty(&dev
->codec_list
) && dev
->pcm
) {
1060 /* the actual cleanup is done by the callback above! */
1061 snd_device_free(dev
->pcm
->card
, dev
->pcm
);