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 <linux/module.h>
16 #include "../soundbus.h"
19 static inline void get_pcm_info(struct i2sbus_dev
*i2sdev
, int in
,
20 struct pcm_info
**pi
, struct pcm_info
**other
)
26 *other
= &i2sdev
->out
;
35 static int clock_and_divisors(int mclk
, int sclk
, int rate
, int *out
)
37 /* sclk must be derived from mclk! */
40 /* derive sclk register value */
41 if (i2s_sf_sclkdiv(mclk
/ sclk
, out
))
44 if (I2S_CLOCK_SPEED_18MHz
% (rate
* mclk
) == 0) {
45 if (!i2s_sf_mclkdiv(I2S_CLOCK_SPEED_18MHz
/ (rate
* mclk
), out
)) {
46 *out
|= I2S_SF_CLOCK_SOURCE_18MHz
;
50 if (I2S_CLOCK_SPEED_45MHz
% (rate
* mclk
) == 0) {
51 if (!i2s_sf_mclkdiv(I2S_CLOCK_SPEED_45MHz
/ (rate
* mclk
), out
)) {
52 *out
|= I2S_SF_CLOCK_SOURCE_45MHz
;
56 if (I2S_CLOCK_SPEED_49MHz
% (rate
* mclk
) == 0) {
57 if (!i2s_sf_mclkdiv(I2S_CLOCK_SPEED_49MHz
/ (rate
* mclk
), out
)) {
58 *out
|= I2S_SF_CLOCK_SOURCE_49MHz
;
65 #define CHECK_RATE(rate) \
66 do { if (rates & SNDRV_PCM_RATE_ ##rate) { \
68 if (clock_and_divisors(sysclock_factor, \
69 bus_factor, rate, &dummy)) \
70 rates &= ~SNDRV_PCM_RATE_ ##rate; \
73 static int i2sbus_pcm_open(struct i2sbus_dev
*i2sdev
, int in
)
75 struct pcm_info
*pi
, *other
;
76 struct soundbus_dev
*sdev
;
77 int masks_inited
= 0, err
;
78 struct codec_info_item
*cii
, *rev
;
79 struct snd_pcm_hardware
*hw
;
81 unsigned int rates
= 0;
82 struct transfer_info v
;
84 int bus_factor
= 0, sysclock_factor
= 0;
87 mutex_lock(&i2sdev
->lock
);
89 get_pcm_info(i2sdev
, in
, &pi
, &other
);
91 hw
= &pi
->substream
->runtime
->hw
;
92 sdev
= &i2sdev
->sound
;
100 /* we now need to assign the hw */
101 list_for_each_entry(cii
, &sdev
->codec_list
, list
) {
102 struct transfer_info
*ti
= cii
->codec
->transfers
;
103 bus_factor
= cii
->codec
->bus_factor
;
104 sysclock_factor
= cii
->codec
->sysclock_factor
;
105 while (ti
->formats
&& ti
->rates
) {
107 if (ti
->transfer_in
== in
108 && cii
->codec
->usable(cii
, ti
, &v
)) {
110 formats
&= v
.formats
;
121 if (!masks_inited
|| !bus_factor
|| !sysclock_factor
) {
125 /* bus dependent stuff */
126 hw
->info
= SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_MMAP_VALID
|
127 SNDRV_PCM_INFO_INTERLEAVED
| SNDRV_PCM_INFO_RESUME
|
128 SNDRV_PCM_INFO_JOINT_DUPLEX
;
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
&= pcm_format_to_bits(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 err
= snd_pcm_hw_constraint_integer(pi
->substream
->runtime
,
197 SNDRV_PCM_HW_PARAM_PERIODS
);
202 list_for_each_entry(cii
, &sdev
->codec_list
, list
) {
203 if (cii
->codec
->open
) {
204 err
= cii
->codec
->open(cii
, pi
->substream
);
209 list_for_each_entry_reverse(rev
,
210 &sdev
->codec_list
, list
) {
211 if (found_this
&& rev
->codec
->close
) {
212 rev
->codec
->close(rev
,
224 mutex_unlock(&i2sdev
->lock
);
230 static int i2sbus_pcm_close(struct i2sbus_dev
*i2sdev
, int in
)
232 struct codec_info_item
*cii
;
236 mutex_lock(&i2sdev
->lock
);
238 get_pcm_info(i2sdev
, in
, &pi
, NULL
);
240 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
) {
241 if (cii
->codec
->close
) {
242 tmp
= cii
->codec
->close(cii
, pi
->substream
);
248 pi
->substream
= NULL
;
250 mutex_unlock(&i2sdev
->lock
);
254 static void i2sbus_wait_for_stop(struct i2sbus_dev
*i2sdev
,
258 struct completion done
;
261 spin_lock_irqsave(&i2sdev
->low_lock
, flags
);
262 if (pi
->dbdma_ring
.stopping
) {
263 init_completion(&done
);
264 pi
->stop_completion
= &done
;
265 spin_unlock_irqrestore(&i2sdev
->low_lock
, flags
);
266 timeout
= wait_for_completion_timeout(&done
, HZ
);
267 spin_lock_irqsave(&i2sdev
->low_lock
, flags
);
268 pi
->stop_completion
= NULL
;
270 /* timeout expired, stop dbdma forcefully */
271 printk(KERN_ERR
"i2sbus_wait_for_stop: timed out\n");
272 /* make sure RUN, PAUSE and S0 bits are cleared */
273 out_le32(&pi
->dbdma
->control
, (RUN
| PAUSE
| 1) << 16);
274 pi
->dbdma_ring
.stopping
= 0;
276 while (in_le32(&pi
->dbdma
->status
) & ACTIVE
) {
283 spin_unlock_irqrestore(&i2sdev
->low_lock
, flags
);
287 void i2sbus_wait_for_stop_both(struct i2sbus_dev
*i2sdev
)
291 get_pcm_info(i2sdev
, 0, &pi
, NULL
);
292 i2sbus_wait_for_stop(i2sdev
, pi
);
293 get_pcm_info(i2sdev
, 1, &pi
, NULL
);
294 i2sbus_wait_for_stop(i2sdev
, pi
);
298 static int i2sbus_hw_params(struct snd_pcm_substream
*substream
,
299 struct snd_pcm_hw_params
*params
)
301 return snd_pcm_lib_malloc_pages(substream
, params_buffer_bytes(params
));
304 static inline int i2sbus_hw_free(struct snd_pcm_substream
*substream
, int in
)
306 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
309 get_pcm_info(i2sdev
, in
, &pi
, NULL
);
310 if (pi
->dbdma_ring
.stopping
)
311 i2sbus_wait_for_stop(i2sdev
, pi
);
312 snd_pcm_lib_free_pages(substream
);
316 static int i2sbus_playback_hw_free(struct snd_pcm_substream
*substream
)
318 return i2sbus_hw_free(substream
, 0);
321 static int i2sbus_record_hw_free(struct snd_pcm_substream
*substream
)
323 return i2sbus_hw_free(substream
, 1);
326 static int i2sbus_pcm_prepare(struct i2sbus_dev
*i2sdev
, int in
)
328 /* whee. Hard work now. The user has selected a bitrate
329 * and bit format, so now we have to program our
330 * I2S controller appropriately. */
331 struct snd_pcm_runtime
*runtime
;
332 struct dbdma_cmd
*command
;
333 int i
, periodsize
, nperiods
;
336 struct codec_info_item
*cii
;
337 int sfr
= 0; /* serial format register */
338 int dws
= 0; /* data word sizes reg */
340 struct pcm_info
*pi
, *other
;
343 unsigned int cmd
, stopaddr
;
345 mutex_lock(&i2sdev
->lock
);
347 get_pcm_info(i2sdev
, in
, &pi
, &other
);
349 if (pi
->dbdma_ring
.running
) {
353 if (pi
->dbdma_ring
.stopping
)
354 i2sbus_wait_for_stop(i2sdev
, pi
);
356 if (!pi
->substream
|| !pi
->substream
->runtime
) {
361 runtime
= pi
->substream
->runtime
;
364 ((i2sdev
->format
!= runtime
->format
)
365 || (i2sdev
->rate
!= runtime
->rate
))) {
370 i2sdev
->format
= runtime
->format
;
371 i2sdev
->rate
= runtime
->rate
;
373 periodsize
= snd_pcm_lib_period_bytes(pi
->substream
);
374 nperiods
= pi
->substream
->runtime
->periods
;
375 pi
->current_period
= 0;
377 /* generate dbdma command ring first */
378 command
= pi
->dbdma_ring
.cmds
;
379 memset(command
, 0, (nperiods
+ 2) * sizeof(struct dbdma_cmd
));
381 /* commands to DMA to/from the ring */
383 * For input, we need to do a graceful stop; if we abort
384 * the DMA, we end up with leftover bytes that corrupt
385 * the next recording. To do this we set the S0 status
386 * bit and wait for the DMA controller to stop. Each
387 * command has a branch condition to
388 * make it branch to a stop command if S0 is set.
389 * On input we also need to wait for the S7 bit to be
390 * set before turning off the DMA controller.
391 * In fact we do the graceful stop for output as well.
393 offset
= runtime
->dma_addr
;
394 cmd
= (in
? INPUT_MORE
: OUTPUT_MORE
) | BR_IFSET
| INTR_ALWAYS
;
395 stopaddr
= pi
->dbdma_ring
.bus_cmd_start
+
396 (nperiods
+ 1) * sizeof(struct dbdma_cmd
);
397 for (i
= 0; i
< nperiods
; i
++, command
++, offset
+= periodsize
) {
398 command
->command
= cpu_to_le16(cmd
);
399 command
->cmd_dep
= cpu_to_le32(stopaddr
);
400 command
->phy_addr
= cpu_to_le32(offset
);
401 command
->req_count
= cpu_to_le16(periodsize
);
404 /* branch back to beginning of ring */
405 command
->command
= cpu_to_le16(DBDMA_NOP
| BR_ALWAYS
);
406 command
->cmd_dep
= cpu_to_le32(pi
->dbdma_ring
.bus_cmd_start
);
409 /* set stop command */
410 command
->command
= cpu_to_le16(DBDMA_STOP
);
412 /* ok, let's set the serial format and stuff */
413 switch (runtime
->format
) {
415 case SNDRV_PCM_FORMAT_S16_BE
:
416 case SNDRV_PCM_FORMAT_U16_BE
:
417 /* FIXME: if we add different bus factors we need to
420 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
) {
421 bi
.bus_factor
= cii
->codec
->bus_factor
;
424 if (!bi
.bus_factor
) {
430 case SNDRV_PCM_FORMAT_S32_BE
:
431 case SNDRV_PCM_FORMAT_U32_BE
:
432 /* force 64x bus speed, otherwise the data cannot be
433 * transferred quickly enough! */
441 /* we assume all sysclocks are the same! */
442 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
) {
443 bi
.sysclock_factor
= cii
->codec
->sysclock_factor
;
447 if (clock_and_divisors(bi
.sysclock_factor
,
454 switch (bi
.bus_factor
) {
456 sfr
|= I2S_SF_SERIAL_FORMAT_I2S_32X
;
459 sfr
|= I2S_SF_SERIAL_FORMAT_I2S_64X
;
462 /* FIXME: THIS ASSUMES MASTER ALL THE TIME */
463 sfr
|= I2S_SF_SCLK_MASTER
;
465 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
) {
467 if (cii
->codec
->prepare
)
468 err
= cii
->codec
->prepare(cii
, &bi
, pi
->substream
);
474 /* codecs are fine with it, so set our clocks */
476 dws
= (2 << I2S_DWS_NUM_CHANNELS_IN_SHIFT
) |
477 (2 << I2S_DWS_NUM_CHANNELS_OUT_SHIFT
) |
478 I2S_DWS_DATA_IN_16BIT
| I2S_DWS_DATA_OUT_16BIT
;
480 dws
= (2 << I2S_DWS_NUM_CHANNELS_IN_SHIFT
) |
481 (2 << I2S_DWS_NUM_CHANNELS_OUT_SHIFT
) |
482 I2S_DWS_DATA_IN_24BIT
| I2S_DWS_DATA_OUT_24BIT
;
484 /* early exit if already programmed correctly */
485 /* not locking these is fine since we touch them only in this function */
486 if (in_le32(&i2sdev
->intfregs
->serial_format
) == sfr
487 && in_le32(&i2sdev
->intfregs
->data_word_sizes
) == dws
)
490 /* let's notify the codecs about clocks going away.
491 * For now we only do mastering on the i2s cell... */
492 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
)
493 if (cii
->codec
->switch_clock
)
494 cii
->codec
->switch_clock(cii
, CLOCK_SWITCH_PREPARE_SLAVE
);
496 i2sbus_control_enable(i2sdev
->control
, i2sdev
);
497 i2sbus_control_cell(i2sdev
->control
, i2sdev
, 1);
499 out_le32(&i2sdev
->intfregs
->intr_ctl
, I2S_PENDING_CLOCKS_STOPPED
);
501 i2sbus_control_clock(i2sdev
->control
, i2sdev
, 0);
505 /* wait for clock stopped. This can apparently take a while... */
508 !(in_le32(&i2sdev
->intfregs
->intr_ctl
) & I2S_PENDING_CLOCKS_STOPPED
)) {
511 out_le32(&i2sdev
->intfregs
->intr_ctl
, I2S_PENDING_CLOCKS_STOPPED
);
513 /* not locking these is fine since we touch them only in this function */
514 out_le32(&i2sdev
->intfregs
->serial_format
, sfr
);
515 out_le32(&i2sdev
->intfregs
->data_word_sizes
, dws
);
517 i2sbus_control_enable(i2sdev
->control
, i2sdev
);
518 i2sbus_control_cell(i2sdev
->control
, i2sdev
, 1);
519 i2sbus_control_clock(i2sdev
->control
, i2sdev
, 1);
522 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
)
523 if (cii
->codec
->switch_clock
)
524 cii
->codec
->switch_clock(cii
, CLOCK_SWITCH_SLAVE
);
527 mutex_unlock(&i2sdev
->lock
);
532 void i2sbus_pcm_prepare_both(struct i2sbus_dev
*i2sdev
)
534 i2sbus_pcm_prepare(i2sdev
, 0);
535 i2sbus_pcm_prepare(i2sdev
, 1);
539 static int i2sbus_pcm_trigger(struct i2sbus_dev
*i2sdev
, int in
, int cmd
)
541 struct codec_info_item
*cii
;
546 spin_lock_irqsave(&i2sdev
->low_lock
, flags
);
548 get_pcm_info(i2sdev
, in
, &pi
, NULL
);
551 case SNDRV_PCM_TRIGGER_START
:
552 case SNDRV_PCM_TRIGGER_RESUME
:
553 if (pi
->dbdma_ring
.running
) {
557 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
)
558 if (cii
->codec
->start
)
559 cii
->codec
->start(cii
, pi
->substream
);
560 pi
->dbdma_ring
.running
= 1;
562 if (pi
->dbdma_ring
.stopping
) {
563 /* Clear the S0 bit, then see if we stopped yet */
564 out_le32(&pi
->dbdma
->control
, 1 << 16);
565 if (in_le32(&pi
->dbdma
->status
) & ACTIVE
) {
566 /* possible race here? */
568 if (in_le32(&pi
->dbdma
->status
) & ACTIVE
) {
569 pi
->dbdma_ring
.stopping
= 0;
570 goto out_unlock
; /* keep running */
575 /* make sure RUN, PAUSE and S0 bits are cleared */
576 out_le32(&pi
->dbdma
->control
, (RUN
| PAUSE
| 1) << 16);
578 /* set branch condition select register */
579 out_le32(&pi
->dbdma
->br_sel
, (1 << 16) | 1);
581 /* write dma command buffer address to the dbdma chip */
582 out_le32(&pi
->dbdma
->cmdptr
, pi
->dbdma_ring
.bus_cmd_start
);
584 /* initialize the frame count and current period */
585 pi
->current_period
= 0;
586 pi
->frame_count
= in_le32(&i2sdev
->intfregs
->frame_count
);
588 /* set the DMA controller running */
589 out_le32(&pi
->dbdma
->control
, (RUN
<< 16) | RUN
);
594 case SNDRV_PCM_TRIGGER_STOP
:
595 case SNDRV_PCM_TRIGGER_SUSPEND
:
596 if (!pi
->dbdma_ring
.running
) {
600 pi
->dbdma_ring
.running
= 0;
602 /* Set the S0 bit to make the DMA branch to the stop cmd */
603 out_le32(&pi
->dbdma
->control
, (1 << 16) | 1);
604 pi
->dbdma_ring
.stopping
= 1;
606 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
)
607 if (cii
->codec
->stop
)
608 cii
->codec
->stop(cii
, pi
->substream
);
616 spin_unlock_irqrestore(&i2sdev
->low_lock
, flags
);
620 static snd_pcm_uframes_t
i2sbus_pcm_pointer(struct i2sbus_dev
*i2sdev
, int in
)
625 get_pcm_info(i2sdev
, in
, &pi
, NULL
);
627 fc
= in_le32(&i2sdev
->intfregs
->frame_count
);
628 fc
= fc
- pi
->frame_count
;
630 if (fc
>= pi
->substream
->runtime
->buffer_size
)
631 fc
%= pi
->substream
->runtime
->buffer_size
;
635 static inline void handle_interrupt(struct i2sbus_dev
*i2sdev
, int in
)
642 struct snd_pcm_runtime
*runtime
;
644 spin_lock(&i2sdev
->low_lock
);
645 get_pcm_info(i2sdev
, in
, &pi
, NULL
);
646 if (!pi
->dbdma_ring
.running
&& !pi
->dbdma_ring
.stopping
)
649 i
= pi
->current_period
;
650 runtime
= pi
->substream
->runtime
;
651 while (pi
->dbdma_ring
.cmds
[i
].xfer_status
) {
652 if (le16_to_cpu(pi
->dbdma_ring
.cmds
[i
].xfer_status
) & BT
)
654 * BT is the branch taken bit. If it took a branch
655 * it is because we set the S0 bit to make it
656 * branch to the stop command.
659 pi
->dbdma_ring
.cmds
[i
].xfer_status
= 0;
661 if (++i
>= runtime
->periods
) {
663 pi
->frame_count
+= runtime
->buffer_size
;
665 pi
->current_period
= i
;
668 * Check the frame count. The DMA tends to get a bit
669 * ahead of the frame counter, which confuses the core.
671 fc
= in_le32(&i2sdev
->intfregs
->frame_count
);
672 nframes
= i
* runtime
->period_size
;
673 if (fc
< pi
->frame_count
+ nframes
)
674 pi
->frame_count
= fc
- nframes
;
680 status
= in_le32(&pi
->dbdma
->status
);
681 if (!(status
& ACTIVE
) && (!in
|| (status
& 0x80)))
683 if (--timeout
<= 0) {
684 printk(KERN_ERR
"i2sbus: timed out "
685 "waiting for DMA to stop!\n");
691 /* Turn off DMA controller, clear S0 bit */
692 out_le32(&pi
->dbdma
->control
, (RUN
| PAUSE
| 1) << 16);
694 pi
->dbdma_ring
.stopping
= 0;
695 if (pi
->stop_completion
)
696 complete(pi
->stop_completion
);
699 if (!pi
->dbdma_ring
.running
)
701 spin_unlock(&i2sdev
->low_lock
);
702 /* may call _trigger again, hence needs to be unlocked */
703 snd_pcm_period_elapsed(pi
->substream
);
707 spin_unlock(&i2sdev
->low_lock
);
710 irqreturn_t
i2sbus_tx_intr(int irq
, void *devid
)
712 handle_interrupt((struct i2sbus_dev
*)devid
, 0);
716 irqreturn_t
i2sbus_rx_intr(int irq
, void *devid
)
718 handle_interrupt((struct i2sbus_dev
*)devid
, 1);
722 static int i2sbus_playback_open(struct snd_pcm_substream
*substream
)
724 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
728 i2sdev
->out
.substream
= substream
;
729 return i2sbus_pcm_open(i2sdev
, 0);
732 static int i2sbus_playback_close(struct snd_pcm_substream
*substream
)
734 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
739 if (i2sdev
->out
.substream
!= substream
)
741 err
= i2sbus_pcm_close(i2sdev
, 0);
743 i2sdev
->out
.substream
= NULL
;
747 static int i2sbus_playback_prepare(struct snd_pcm_substream
*substream
)
749 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
753 if (i2sdev
->out
.substream
!= substream
)
755 return i2sbus_pcm_prepare(i2sdev
, 0);
758 static int i2sbus_playback_trigger(struct snd_pcm_substream
*substream
, int cmd
)
760 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
764 if (i2sdev
->out
.substream
!= substream
)
766 return i2sbus_pcm_trigger(i2sdev
, 0, cmd
);
769 static snd_pcm_uframes_t
i2sbus_playback_pointer(struct snd_pcm_substream
772 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
776 if (i2sdev
->out
.substream
!= substream
)
778 return i2sbus_pcm_pointer(i2sdev
, 0);
781 static const struct snd_pcm_ops i2sbus_playback_ops
= {
782 .open
= i2sbus_playback_open
,
783 .close
= i2sbus_playback_close
,
784 .ioctl
= snd_pcm_lib_ioctl
,
785 .hw_params
= i2sbus_hw_params
,
786 .hw_free
= i2sbus_playback_hw_free
,
787 .prepare
= i2sbus_playback_prepare
,
788 .trigger
= i2sbus_playback_trigger
,
789 .pointer
= i2sbus_playback_pointer
,
792 static int i2sbus_record_open(struct snd_pcm_substream
*substream
)
794 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
798 i2sdev
->in
.substream
= substream
;
799 return i2sbus_pcm_open(i2sdev
, 1);
802 static int i2sbus_record_close(struct snd_pcm_substream
*substream
)
804 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
809 if (i2sdev
->in
.substream
!= substream
)
811 err
= i2sbus_pcm_close(i2sdev
, 1);
813 i2sdev
->in
.substream
= NULL
;
817 static int i2sbus_record_prepare(struct snd_pcm_substream
*substream
)
819 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
823 if (i2sdev
->in
.substream
!= substream
)
825 return i2sbus_pcm_prepare(i2sdev
, 1);
828 static int i2sbus_record_trigger(struct snd_pcm_substream
*substream
, int cmd
)
830 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
834 if (i2sdev
->in
.substream
!= substream
)
836 return i2sbus_pcm_trigger(i2sdev
, 1, cmd
);
839 static snd_pcm_uframes_t
i2sbus_record_pointer(struct snd_pcm_substream
842 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
846 if (i2sdev
->in
.substream
!= substream
)
848 return i2sbus_pcm_pointer(i2sdev
, 1);
851 static const struct snd_pcm_ops i2sbus_record_ops
= {
852 .open
= i2sbus_record_open
,
853 .close
= i2sbus_record_close
,
854 .ioctl
= snd_pcm_lib_ioctl
,
855 .hw_params
= i2sbus_hw_params
,
856 .hw_free
= i2sbus_record_hw_free
,
857 .prepare
= i2sbus_record_prepare
,
858 .trigger
= i2sbus_record_trigger
,
859 .pointer
= i2sbus_record_pointer
,
862 static void i2sbus_private_free(struct snd_pcm
*pcm
)
864 struct i2sbus_dev
*i2sdev
= snd_pcm_chip(pcm
);
865 struct codec_info_item
*p
, *tmp
;
867 i2sdev
->sound
.pcm
= NULL
;
868 i2sdev
->out
.created
= 0;
869 i2sdev
->in
.created
= 0;
870 list_for_each_entry_safe(p
, tmp
, &i2sdev
->sound
.codec_list
, list
) {
871 printk(KERN_ERR
"i2sbus: a codec didn't unregister!\n");
873 module_put(p
->codec
->owner
);
876 soundbus_dev_put(&i2sdev
->sound
);
877 module_put(THIS_MODULE
);
881 i2sbus_attach_codec(struct soundbus_dev
*dev
, struct snd_card
*card
,
882 struct codec_info
*ci
, void *data
)
884 int err
, in
= 0, out
= 0;
885 struct transfer_info
*tmp
;
886 struct i2sbus_dev
*i2sdev
= soundbus_dev_to_i2sbus_dev(dev
);
887 struct codec_info_item
*cii
;
889 if (!dev
->pcmname
|| dev
->pcmid
== -1) {
890 printk(KERN_ERR
"i2sbus: pcm name and id must be set!\n");
894 list_for_each_entry(cii
, &dev
->codec_list
, list
) {
895 if (cii
->codec_data
== data
)
899 if (!ci
->transfers
|| !ci
->transfers
->formats
900 || !ci
->transfers
->rates
|| !ci
->usable
)
903 /* we currently code the i2s transfer on the clock, and support only
905 if (ci
->bus_factor
!= 32 && ci
->bus_factor
!= 64)
908 /* If you want to fix this, you need to keep track of what transport infos
909 * are to be used, which codecs they belong to, and then fix all the
910 * sysclock/busclock stuff above to depend on which is usable */
911 list_for_each_entry(cii
, &dev
->codec_list
, list
) {
912 if (cii
->codec
->sysclock_factor
!= ci
->sysclock_factor
) {
914 "cannot yet handle multiple different sysclocks!\n");
917 if (cii
->codec
->bus_factor
!= ci
->bus_factor
) {
919 "cannot yet handle multiple different bus clocks!\n");
925 while (tmp
->formats
&& tmp
->rates
) {
926 if (tmp
->transfer_in
)
933 cii
= kzalloc(sizeof(struct codec_info_item
), GFP_KERNEL
);
935 printk(KERN_DEBUG
"i2sbus: failed to allocate cii\n");
939 /* use the private data to point to the codec info */
940 cii
->sdev
= soundbus_dev_get(dev
);
942 cii
->codec_data
= data
;
946 "i2sbus: failed to get soundbus dev reference\n");
951 if (!try_module_get(THIS_MODULE
)) {
952 printk(KERN_DEBUG
"i2sbus: failed to get module reference!\n");
957 if (!try_module_get(ci
->owner
)) {
959 "i2sbus: failed to get module reference to codec owner!\n");
961 goto out_put_this_module
;
965 err
= snd_pcm_new(card
, dev
->pcmname
, dev
->pcmid
, 0, 0,
968 printk(KERN_DEBUG
"i2sbus: failed to create pcm\n");
969 goto out_put_ci_module
;
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 dev
->pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].dev
.parent
=
992 i2sdev
->out
.created
= 1;
995 if (!i2sdev
->in
.created
&& in
) {
996 if (dev
->pcm
->card
!= card
) {
998 "Can't attach same bus to different cards!\n");
1000 goto out_put_ci_module
;
1002 err
= snd_pcm_new_stream(dev
->pcm
, SNDRV_PCM_STREAM_CAPTURE
, 1);
1004 goto out_put_ci_module
;
1005 snd_pcm_set_ops(dev
->pcm
, SNDRV_PCM_STREAM_CAPTURE
,
1006 &i2sbus_record_ops
);
1007 dev
->pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].dev
.parent
=
1009 i2sdev
->in
.created
= 1;
1012 /* so we have to register the pcm after adding any substream
1013 * to it because alsa doesn't create the devices for the
1014 * substreams when we add them later.
1015 * Therefore, force in and out on both busses (above) and
1016 * register the pcm now instead of just after creating it.
1018 err
= snd_device_register(card
, dev
->pcm
);
1020 printk(KERN_ERR
"i2sbus: error registering new pcm\n");
1021 goto out_put_ci_module
;
1023 /* no errors any more, so let's add this to our list */
1024 list_add(&cii
->list
, &dev
->codec_list
);
1026 dev
->pcm
->private_data
= i2sdev
;
1027 dev
->pcm
->private_free
= i2sbus_private_free
;
1029 /* well, we really should support scatter/gather DMA */
1030 snd_pcm_lib_preallocate_pages_for_all(
1031 dev
->pcm
, SNDRV_DMA_TYPE_DEV
,
1032 snd_dma_pci_data(macio_get_pci_dev(i2sdev
->macio
)),
1033 64 * 1024, 64 * 1024);
1037 module_put(ci
->owner
);
1038 out_put_this_module
:
1039 module_put(THIS_MODULE
);
1041 soundbus_dev_put(dev
);
1047 void i2sbus_detach_codec(struct soundbus_dev
*dev
, void *data
)
1049 struct codec_info_item
*cii
= NULL
, *i
;
1051 list_for_each_entry(i
, &dev
->codec_list
, list
) {
1052 if (i
->codec_data
== data
) {
1058 list_del(&cii
->list
);
1059 module_put(cii
->codec
->owner
);
1062 /* no more codecs, but still a pcm? */
1063 if (list_empty(&dev
->codec_list
) && dev
->pcm
) {
1064 /* the actual cleanup is done by the callback above! */
1065 snd_device_free(dev
->pcm
->card
, dev
->pcm
);