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 <sound/core.h>
12 #include <asm/macio.h>
13 #include <linux/pci.h>
14 #include "../soundbus.h"
17 static inline void get_pcm_info(struct i2sbus_dev
*i2sdev
, int in
,
18 struct pcm_info
**pi
, struct pcm_info
**other
)
24 *other
= &i2sdev
->out
;
33 static int clock_and_divisors(int mclk
, int sclk
, int rate
, int *out
)
35 /* sclk must be derived from mclk! */
38 /* derive sclk register value */
39 if (i2s_sf_sclkdiv(mclk
/ sclk
, out
))
42 if (I2S_CLOCK_SPEED_18MHz
% (rate
* mclk
) == 0) {
43 if (!i2s_sf_mclkdiv(I2S_CLOCK_SPEED_18MHz
/ (rate
* mclk
), out
)) {
44 *out
|= I2S_SF_CLOCK_SOURCE_18MHz
;
48 if (I2S_CLOCK_SPEED_45MHz
% (rate
* mclk
) == 0) {
49 if (!i2s_sf_mclkdiv(I2S_CLOCK_SPEED_45MHz
/ (rate
* mclk
), out
)) {
50 *out
|= I2S_SF_CLOCK_SOURCE_45MHz
;
54 if (I2S_CLOCK_SPEED_49MHz
% (rate
* mclk
) == 0) {
55 if (!i2s_sf_mclkdiv(I2S_CLOCK_SPEED_49MHz
/ (rate
* mclk
), out
)) {
56 *out
|= I2S_SF_CLOCK_SOURCE_49MHz
;
63 #define CHECK_RATE(rate) \
64 do { if (rates & SNDRV_PCM_RATE_ ##rate) { \
66 if (clock_and_divisors(sysclock_factor, \
67 bus_factor, rate, &dummy)) \
68 rates &= ~SNDRV_PCM_RATE_ ##rate; \
71 static int i2sbus_pcm_open(struct i2sbus_dev
*i2sdev
, int in
)
73 struct pcm_info
*pi
, *other
;
74 struct soundbus_dev
*sdev
;
75 int masks_inited
= 0, err
;
76 struct codec_info_item
*cii
, *rev
;
77 struct snd_pcm_hardware
*hw
;
79 unsigned int rates
= 0;
80 struct transfer_info v
;
82 int bus_factor
= 0, sysclock_factor
= 0;
85 mutex_lock(&i2sdev
->lock
);
87 get_pcm_info(i2sdev
, in
, &pi
, &other
);
89 hw
= &pi
->substream
->runtime
->hw
;
90 sdev
= &i2sdev
->sound
;
98 /* we now need to assign the hw */
99 list_for_each_entry(cii
, &sdev
->codec_list
, list
) {
100 struct transfer_info
*ti
= cii
->codec
->transfers
;
101 bus_factor
= cii
->codec
->bus_factor
;
102 sysclock_factor
= cii
->codec
->sysclock_factor
;
103 while (ti
->formats
&& ti
->rates
) {
105 if (ti
->transfer_in
== in
106 && cii
->codec
->usable(cii
, ti
, &v
)) {
108 formats
&= v
.formats
;
119 if (!masks_inited
|| !bus_factor
|| !sysclock_factor
) {
123 /* bus dependent stuff */
124 hw
->info
= SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_MMAP_VALID
|
125 SNDRV_PCM_INFO_INTERLEAVED
| SNDRV_PCM_INFO_RESUME
|
126 SNDRV_PCM_INFO_JOINT_DUPLEX
;
143 /* well. the codec might want 24 bits only, and we'll
144 * ever only transfer 24 bits, but they are top-aligned!
145 * So for alsa, we claim that we're doing full 32 bit
146 * while in reality we'll ignore the lower 8 bits of
147 * that when doing playback (they're transferred as 0
148 * as far as I know, no codecs we have are 32-bit capable
149 * so I can't really test) and when doing recording we'll
150 * always have those lower 8 bits recorded as 0 */
151 if (formats
& SNDRV_PCM_FMTBIT_S24_BE
)
152 formats
|= SNDRV_PCM_FMTBIT_S32_BE
;
153 if (formats
& SNDRV_PCM_FMTBIT_U24_BE
)
154 formats
|= SNDRV_PCM_FMTBIT_U32_BE
;
155 /* now mask off what we can support. I suppose we could
156 * also support S24_3LE and some similar formats, but I
157 * doubt there's a codec that would be able to use that,
158 * so we don't support it here. */
159 hw
->formats
= formats
& (SNDRV_PCM_FMTBIT_S16_BE
|
160 SNDRV_PCM_FMTBIT_U16_BE
|
161 SNDRV_PCM_FMTBIT_S32_BE
|
162 SNDRV_PCM_FMTBIT_U32_BE
);
164 /* we need to set the highest and lowest rate possible.
165 * These are the highest and lowest rates alsa can
166 * support properly in its bitfield.
167 * Below, we'll use that to restrict to the rate
168 * currently in use (if any). */
170 hw
->rate_max
= 192000;
171 /* if the other stream is active, then we can only
172 * support what it is currently using.
173 * FIXME: I lied. This comment is wrong. We can support
174 * anything that works with the same serial format, ie.
175 * when recording 24 bit sound we can well play 16 bit
176 * sound at the same time iff using the same transfer mode.
179 /* FIXME: is this guaranteed by the alsa api? */
180 hw
->formats
&= (1ULL << i2sdev
->format
);
181 /* see above, restrict rates to the one we already have */
182 hw
->rate_min
= i2sdev
->rate
;
183 hw
->rate_max
= i2sdev
->rate
;
186 hw
->channels_min
= 2;
187 hw
->channels_max
= 2;
188 /* these are somewhat arbitrary */
189 hw
->buffer_bytes_max
= 131072;
190 hw
->period_bytes_min
= 256;
191 hw
->period_bytes_max
= 16384;
193 hw
->periods_max
= MAX_DBDMA_COMMANDS
;
194 err
= snd_pcm_hw_constraint_integer(pi
->substream
->runtime
,
195 SNDRV_PCM_HW_PARAM_PERIODS
);
200 list_for_each_entry(cii
, &sdev
->codec_list
, list
) {
201 if (cii
->codec
->open
) {
202 err
= cii
->codec
->open(cii
, pi
->substream
);
207 list_for_each_entry_reverse(rev
,
208 &sdev
->codec_list
, list
) {
209 if (found_this
&& rev
->codec
->close
) {
210 rev
->codec
->close(rev
,
222 mutex_unlock(&i2sdev
->lock
);
228 static int i2sbus_pcm_close(struct i2sbus_dev
*i2sdev
, int in
)
230 struct codec_info_item
*cii
;
234 mutex_lock(&i2sdev
->lock
);
236 get_pcm_info(i2sdev
, in
, &pi
, NULL
);
238 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
) {
239 if (cii
->codec
->close
) {
240 tmp
= cii
->codec
->close(cii
, pi
->substream
);
246 pi
->substream
= NULL
;
248 mutex_unlock(&i2sdev
->lock
);
252 static void i2sbus_wait_for_stop(struct i2sbus_dev
*i2sdev
,
256 struct completion done
;
259 spin_lock_irqsave(&i2sdev
->low_lock
, flags
);
260 if (pi
->dbdma_ring
.stopping
) {
261 init_completion(&done
);
262 pi
->stop_completion
= &done
;
263 spin_unlock_irqrestore(&i2sdev
->low_lock
, flags
);
264 timeout
= wait_for_completion_timeout(&done
, HZ
);
265 spin_lock_irqsave(&i2sdev
->low_lock
, flags
);
266 pi
->stop_completion
= NULL
;
268 /* timeout expired, stop dbdma forcefully */
269 printk(KERN_ERR
"i2sbus_wait_for_stop: timed out\n");
270 /* make sure RUN, PAUSE and S0 bits are cleared */
271 out_le32(&pi
->dbdma
->control
, (RUN
| PAUSE
| 1) << 16);
272 pi
->dbdma_ring
.stopping
= 0;
274 while (in_le32(&pi
->dbdma
->status
) & ACTIVE
) {
281 spin_unlock_irqrestore(&i2sdev
->low_lock
, flags
);
285 void i2sbus_wait_for_stop_both(struct i2sbus_dev
*i2sdev
)
289 get_pcm_info(i2sdev
, 0, &pi
, NULL
);
290 i2sbus_wait_for_stop(i2sdev
, pi
);
291 get_pcm_info(i2sdev
, 1, &pi
, NULL
);
292 i2sbus_wait_for_stop(i2sdev
, pi
);
296 static int i2sbus_hw_params(struct snd_pcm_substream
*substream
,
297 struct snd_pcm_hw_params
*params
)
299 return snd_pcm_lib_malloc_pages(substream
, params_buffer_bytes(params
));
302 static inline int i2sbus_hw_free(struct snd_pcm_substream
*substream
, int in
)
304 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
307 get_pcm_info(i2sdev
, in
, &pi
, NULL
);
308 if (pi
->dbdma_ring
.stopping
)
309 i2sbus_wait_for_stop(i2sdev
, pi
);
310 snd_pcm_lib_free_pages(substream
);
314 static int i2sbus_playback_hw_free(struct snd_pcm_substream
*substream
)
316 return i2sbus_hw_free(substream
, 0);
319 static int i2sbus_record_hw_free(struct snd_pcm_substream
*substream
)
321 return i2sbus_hw_free(substream
, 1);
324 static int i2sbus_pcm_prepare(struct i2sbus_dev
*i2sdev
, int in
)
326 /* whee. Hard work now. The user has selected a bitrate
327 * and bit format, so now we have to program our
328 * I2S controller appropriately. */
329 struct snd_pcm_runtime
*runtime
;
330 struct dbdma_cmd
*command
;
331 int i
, periodsize
, nperiods
;
334 struct codec_info_item
*cii
;
335 int sfr
= 0; /* serial format register */
336 int dws
= 0; /* data word sizes reg */
338 struct pcm_info
*pi
, *other
;
341 unsigned int cmd
, stopaddr
;
343 mutex_lock(&i2sdev
->lock
);
345 get_pcm_info(i2sdev
, in
, &pi
, &other
);
347 if (pi
->dbdma_ring
.running
) {
351 if (pi
->dbdma_ring
.stopping
)
352 i2sbus_wait_for_stop(i2sdev
, pi
);
354 if (!pi
->substream
|| !pi
->substream
->runtime
) {
359 runtime
= pi
->substream
->runtime
;
362 ((i2sdev
->format
!= runtime
->format
)
363 || (i2sdev
->rate
!= runtime
->rate
))) {
368 i2sdev
->format
= runtime
->format
;
369 i2sdev
->rate
= runtime
->rate
;
371 periodsize
= snd_pcm_lib_period_bytes(pi
->substream
);
372 nperiods
= pi
->substream
->runtime
->periods
;
373 pi
->current_period
= 0;
375 /* generate dbdma command ring first */
376 command
= pi
->dbdma_ring
.cmds
;
377 memset(command
, 0, (nperiods
+ 2) * sizeof(struct dbdma_cmd
));
379 /* commands to DMA to/from the ring */
381 * For input, we need to do a graceful stop; if we abort
382 * the DMA, we end up with leftover bytes that corrupt
383 * the next recording. To do this we set the S0 status
384 * bit and wait for the DMA controller to stop. Each
385 * command has a branch condition to
386 * make it branch to a stop command if S0 is set.
387 * On input we also need to wait for the S7 bit to be
388 * set before turning off the DMA controller.
389 * In fact we do the graceful stop for output as well.
391 offset
= runtime
->dma_addr
;
392 cmd
= (in
? INPUT_MORE
: OUTPUT_MORE
) | BR_IFSET
| INTR_ALWAYS
;
393 stopaddr
= pi
->dbdma_ring
.bus_cmd_start
+
394 (nperiods
+ 1) * sizeof(struct dbdma_cmd
);
395 for (i
= 0; i
< nperiods
; i
++, command
++, offset
+= periodsize
) {
396 command
->command
= cpu_to_le16(cmd
);
397 command
->cmd_dep
= cpu_to_le32(stopaddr
);
398 command
->phy_addr
= cpu_to_le32(offset
);
399 command
->req_count
= cpu_to_le16(periodsize
);
402 /* branch back to beginning of ring */
403 command
->command
= cpu_to_le16(DBDMA_NOP
| BR_ALWAYS
);
404 command
->cmd_dep
= cpu_to_le32(pi
->dbdma_ring
.bus_cmd_start
);
407 /* set stop command */
408 command
->command
= cpu_to_le16(DBDMA_STOP
);
410 /* ok, let's set the serial format and stuff */
411 switch (runtime
->format
) {
413 case SNDRV_PCM_FORMAT_S16_BE
:
414 case SNDRV_PCM_FORMAT_U16_BE
:
415 /* FIXME: if we add different bus factors we need to
418 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
) {
419 bi
.bus_factor
= cii
->codec
->bus_factor
;
422 if (!bi
.bus_factor
) {
428 case SNDRV_PCM_FORMAT_S32_BE
:
429 case SNDRV_PCM_FORMAT_U32_BE
:
430 /* force 64x bus speed, otherwise the data cannot be
431 * transferred quickly enough! */
439 /* we assume all sysclocks are the same! */
440 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
) {
441 bi
.sysclock_factor
= cii
->codec
->sysclock_factor
;
445 if (clock_and_divisors(bi
.sysclock_factor
,
452 switch (bi
.bus_factor
) {
454 sfr
|= I2S_SF_SERIAL_FORMAT_I2S_32X
;
457 sfr
|= I2S_SF_SERIAL_FORMAT_I2S_64X
;
460 /* FIXME: THIS ASSUMES MASTER ALL THE TIME */
461 sfr
|= I2S_SF_SCLK_MASTER
;
463 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
) {
465 if (cii
->codec
->prepare
)
466 err
= cii
->codec
->prepare(cii
, &bi
, pi
->substream
);
472 /* codecs are fine with it, so set our clocks */
474 dws
= (2 << I2S_DWS_NUM_CHANNELS_IN_SHIFT
) |
475 (2 << I2S_DWS_NUM_CHANNELS_OUT_SHIFT
) |
476 I2S_DWS_DATA_IN_16BIT
| I2S_DWS_DATA_OUT_16BIT
;
478 dws
= (2 << I2S_DWS_NUM_CHANNELS_IN_SHIFT
) |
479 (2 << I2S_DWS_NUM_CHANNELS_OUT_SHIFT
) |
480 I2S_DWS_DATA_IN_24BIT
| I2S_DWS_DATA_OUT_24BIT
;
482 /* early exit if already programmed correctly */
483 /* not locking these is fine since we touch them only in this function */
484 if (in_le32(&i2sdev
->intfregs
->serial_format
) == sfr
485 && in_le32(&i2sdev
->intfregs
->data_word_sizes
) == dws
)
488 /* let's notify the codecs about clocks going away.
489 * For now we only do mastering on the i2s cell... */
490 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
)
491 if (cii
->codec
->switch_clock
)
492 cii
->codec
->switch_clock(cii
, CLOCK_SWITCH_PREPARE_SLAVE
);
494 i2sbus_control_enable(i2sdev
->control
, i2sdev
);
495 i2sbus_control_cell(i2sdev
->control
, i2sdev
, 1);
497 out_le32(&i2sdev
->intfregs
->intr_ctl
, I2S_PENDING_CLOCKS_STOPPED
);
499 i2sbus_control_clock(i2sdev
->control
, i2sdev
, 0);
503 /* wait for clock stopped. This can apparently take a while... */
506 !(in_le32(&i2sdev
->intfregs
->intr_ctl
) & I2S_PENDING_CLOCKS_STOPPED
)) {
509 out_le32(&i2sdev
->intfregs
->intr_ctl
, I2S_PENDING_CLOCKS_STOPPED
);
511 /* not locking these is fine since we touch them only in this function */
512 out_le32(&i2sdev
->intfregs
->serial_format
, sfr
);
513 out_le32(&i2sdev
->intfregs
->data_word_sizes
, dws
);
515 i2sbus_control_enable(i2sdev
->control
, i2sdev
);
516 i2sbus_control_cell(i2sdev
->control
, i2sdev
, 1);
517 i2sbus_control_clock(i2sdev
->control
, i2sdev
, 1);
520 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
)
521 if (cii
->codec
->switch_clock
)
522 cii
->codec
->switch_clock(cii
, CLOCK_SWITCH_SLAVE
);
525 mutex_unlock(&i2sdev
->lock
);
530 void i2sbus_pcm_prepare_both(struct i2sbus_dev
*i2sdev
)
532 i2sbus_pcm_prepare(i2sdev
, 0);
533 i2sbus_pcm_prepare(i2sdev
, 1);
537 static int i2sbus_pcm_trigger(struct i2sbus_dev
*i2sdev
, int in
, int cmd
)
539 struct codec_info_item
*cii
;
544 spin_lock_irqsave(&i2sdev
->low_lock
, flags
);
546 get_pcm_info(i2sdev
, in
, &pi
, NULL
);
549 case SNDRV_PCM_TRIGGER_START
:
550 case SNDRV_PCM_TRIGGER_RESUME
:
551 if (pi
->dbdma_ring
.running
) {
555 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
)
556 if (cii
->codec
->start
)
557 cii
->codec
->start(cii
, pi
->substream
);
558 pi
->dbdma_ring
.running
= 1;
560 if (pi
->dbdma_ring
.stopping
) {
561 /* Clear the S0 bit, then see if we stopped yet */
562 out_le32(&pi
->dbdma
->control
, 1 << 16);
563 if (in_le32(&pi
->dbdma
->status
) & ACTIVE
) {
564 /* possible race here? */
566 if (in_le32(&pi
->dbdma
->status
) & ACTIVE
) {
567 pi
->dbdma_ring
.stopping
= 0;
568 goto out_unlock
; /* keep running */
573 /* make sure RUN, PAUSE and S0 bits are cleared */
574 out_le32(&pi
->dbdma
->control
, (RUN
| PAUSE
| 1) << 16);
576 /* set branch condition select register */
577 out_le32(&pi
->dbdma
->br_sel
, (1 << 16) | 1);
579 /* write dma command buffer address to the dbdma chip */
580 out_le32(&pi
->dbdma
->cmdptr
, pi
->dbdma_ring
.bus_cmd_start
);
582 /* initialize the frame count and current period */
583 pi
->current_period
= 0;
584 pi
->frame_count
= in_le32(&i2sdev
->intfregs
->frame_count
);
586 /* set the DMA controller running */
587 out_le32(&pi
->dbdma
->control
, (RUN
<< 16) | RUN
);
592 case SNDRV_PCM_TRIGGER_STOP
:
593 case SNDRV_PCM_TRIGGER_SUSPEND
:
594 if (!pi
->dbdma_ring
.running
) {
598 pi
->dbdma_ring
.running
= 0;
600 /* Set the S0 bit to make the DMA branch to the stop cmd */
601 out_le32(&pi
->dbdma
->control
, (1 << 16) | 1);
602 pi
->dbdma_ring
.stopping
= 1;
604 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
)
605 if (cii
->codec
->stop
)
606 cii
->codec
->stop(cii
, pi
->substream
);
614 spin_unlock_irqrestore(&i2sdev
->low_lock
, flags
);
618 static snd_pcm_uframes_t
i2sbus_pcm_pointer(struct i2sbus_dev
*i2sdev
, int in
)
623 get_pcm_info(i2sdev
, in
, &pi
, NULL
);
625 fc
= in_le32(&i2sdev
->intfregs
->frame_count
);
626 fc
= fc
- pi
->frame_count
;
628 if (fc
>= pi
->substream
->runtime
->buffer_size
)
629 fc
%= pi
->substream
->runtime
->buffer_size
;
633 static inline void handle_interrupt(struct i2sbus_dev
*i2sdev
, int in
)
640 struct snd_pcm_runtime
*runtime
;
642 spin_lock(&i2sdev
->low_lock
);
643 get_pcm_info(i2sdev
, in
, &pi
, NULL
);
644 if (!pi
->dbdma_ring
.running
&& !pi
->dbdma_ring
.stopping
)
647 i
= pi
->current_period
;
648 runtime
= pi
->substream
->runtime
;
649 while (pi
->dbdma_ring
.cmds
[i
].xfer_status
) {
650 if (le16_to_cpu(pi
->dbdma_ring
.cmds
[i
].xfer_status
) & BT
)
652 * BT is the branch taken bit. If it took a branch
653 * it is because we set the S0 bit to make it
654 * branch to the stop command.
657 pi
->dbdma_ring
.cmds
[i
].xfer_status
= 0;
659 if (++i
>= runtime
->periods
) {
661 pi
->frame_count
+= runtime
->buffer_size
;
663 pi
->current_period
= i
;
666 * Check the frame count. The DMA tends to get a bit
667 * ahead of the frame counter, which confuses the core.
669 fc
= in_le32(&i2sdev
->intfregs
->frame_count
);
670 nframes
= i
* runtime
->period_size
;
671 if (fc
< pi
->frame_count
+ nframes
)
672 pi
->frame_count
= fc
- nframes
;
678 status
= in_le32(&pi
->dbdma
->status
);
679 if (!(status
& ACTIVE
) && (!in
|| (status
& 0x80)))
681 if (--timeout
<= 0) {
682 printk(KERN_ERR
"i2sbus: timed out "
683 "waiting for DMA to stop!\n");
689 /* Turn off DMA controller, clear S0 bit */
690 out_le32(&pi
->dbdma
->control
, (RUN
| PAUSE
| 1) << 16);
692 pi
->dbdma_ring
.stopping
= 0;
693 if (pi
->stop_completion
)
694 complete(pi
->stop_completion
);
697 if (!pi
->dbdma_ring
.running
)
699 spin_unlock(&i2sdev
->low_lock
);
700 /* may call _trigger again, hence needs to be unlocked */
701 snd_pcm_period_elapsed(pi
->substream
);
705 spin_unlock(&i2sdev
->low_lock
);
708 irqreturn_t
i2sbus_tx_intr(int irq
, void *devid
)
710 handle_interrupt((struct i2sbus_dev
*)devid
, 0);
714 irqreturn_t
i2sbus_rx_intr(int irq
, void *devid
)
716 handle_interrupt((struct i2sbus_dev
*)devid
, 1);
720 static int i2sbus_playback_open(struct snd_pcm_substream
*substream
)
722 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
726 i2sdev
->out
.substream
= substream
;
727 return i2sbus_pcm_open(i2sdev
, 0);
730 static int i2sbus_playback_close(struct snd_pcm_substream
*substream
)
732 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
737 if (i2sdev
->out
.substream
!= substream
)
739 err
= i2sbus_pcm_close(i2sdev
, 0);
741 i2sdev
->out
.substream
= NULL
;
745 static int i2sbus_playback_prepare(struct snd_pcm_substream
*substream
)
747 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
751 if (i2sdev
->out
.substream
!= substream
)
753 return i2sbus_pcm_prepare(i2sdev
, 0);
756 static int i2sbus_playback_trigger(struct snd_pcm_substream
*substream
, int cmd
)
758 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
762 if (i2sdev
->out
.substream
!= substream
)
764 return i2sbus_pcm_trigger(i2sdev
, 0, cmd
);
767 static snd_pcm_uframes_t
i2sbus_playback_pointer(struct snd_pcm_substream
770 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
774 if (i2sdev
->out
.substream
!= substream
)
776 return i2sbus_pcm_pointer(i2sdev
, 0);
779 static struct snd_pcm_ops i2sbus_playback_ops
= {
780 .open
= i2sbus_playback_open
,
781 .close
= i2sbus_playback_close
,
782 .ioctl
= snd_pcm_lib_ioctl
,
783 .hw_params
= i2sbus_hw_params
,
784 .hw_free
= i2sbus_playback_hw_free
,
785 .prepare
= i2sbus_playback_prepare
,
786 .trigger
= i2sbus_playback_trigger
,
787 .pointer
= i2sbus_playback_pointer
,
790 static int i2sbus_record_open(struct snd_pcm_substream
*substream
)
792 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
796 i2sdev
->in
.substream
= substream
;
797 return i2sbus_pcm_open(i2sdev
, 1);
800 static int i2sbus_record_close(struct snd_pcm_substream
*substream
)
802 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
807 if (i2sdev
->in
.substream
!= substream
)
809 err
= i2sbus_pcm_close(i2sdev
, 1);
811 i2sdev
->in
.substream
= NULL
;
815 static int i2sbus_record_prepare(struct snd_pcm_substream
*substream
)
817 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
821 if (i2sdev
->in
.substream
!= substream
)
823 return i2sbus_pcm_prepare(i2sdev
, 1);
826 static int i2sbus_record_trigger(struct snd_pcm_substream
*substream
, int cmd
)
828 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
832 if (i2sdev
->in
.substream
!= substream
)
834 return i2sbus_pcm_trigger(i2sdev
, 1, cmd
);
837 static snd_pcm_uframes_t
i2sbus_record_pointer(struct snd_pcm_substream
840 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
844 if (i2sdev
->in
.substream
!= substream
)
846 return i2sbus_pcm_pointer(i2sdev
, 1);
849 static struct snd_pcm_ops i2sbus_record_ops
= {
850 .open
= i2sbus_record_open
,
851 .close
= i2sbus_record_close
,
852 .ioctl
= snd_pcm_lib_ioctl
,
853 .hw_params
= i2sbus_hw_params
,
854 .hw_free
= i2sbus_record_hw_free
,
855 .prepare
= i2sbus_record_prepare
,
856 .trigger
= i2sbus_record_trigger
,
857 .pointer
= i2sbus_record_pointer
,
860 static void i2sbus_private_free(struct snd_pcm
*pcm
)
862 struct i2sbus_dev
*i2sdev
= snd_pcm_chip(pcm
);
863 struct codec_info_item
*p
, *tmp
;
865 i2sdev
->sound
.pcm
= NULL
;
866 i2sdev
->out
.created
= 0;
867 i2sdev
->in
.created
= 0;
868 list_for_each_entry_safe(p
, tmp
, &i2sdev
->sound
.codec_list
, list
) {
869 printk(KERN_ERR
"i2sbus: a codec didn't unregister!\n");
871 module_put(p
->codec
->owner
);
874 soundbus_dev_put(&i2sdev
->sound
);
875 module_put(THIS_MODULE
);
879 i2sbus_attach_codec(struct soundbus_dev
*dev
, struct snd_card
*card
,
880 struct codec_info
*ci
, void *data
)
882 int err
, in
= 0, out
= 0;
883 struct transfer_info
*tmp
;
884 struct i2sbus_dev
*i2sdev
= soundbus_dev_to_i2sbus_dev(dev
);
885 struct codec_info_item
*cii
;
887 if (!dev
->pcmname
|| dev
->pcmid
== -1) {
888 printk(KERN_ERR
"i2sbus: pcm name and id must be set!\n");
892 list_for_each_entry(cii
, &dev
->codec_list
, list
) {
893 if (cii
->codec_data
== data
)
897 if (!ci
->transfers
|| !ci
->transfers
->formats
898 || !ci
->transfers
->rates
|| !ci
->usable
)
901 /* we currently code the i2s transfer on the clock, and support only
903 if (ci
->bus_factor
!= 32 && ci
->bus_factor
!= 64)
906 /* If you want to fix this, you need to keep track of what transport infos
907 * are to be used, which codecs they belong to, and then fix all the
908 * sysclock/busclock stuff above to depend on which is usable */
909 list_for_each_entry(cii
, &dev
->codec_list
, list
) {
910 if (cii
->codec
->sysclock_factor
!= ci
->sysclock_factor
) {
912 "cannot yet handle multiple different sysclocks!\n");
915 if (cii
->codec
->bus_factor
!= ci
->bus_factor
) {
917 "cannot yet handle multiple different bus clocks!\n");
923 while (tmp
->formats
&& tmp
->rates
) {
924 if (tmp
->transfer_in
)
931 cii
= kzalloc(sizeof(struct codec_info_item
), GFP_KERNEL
);
933 printk(KERN_DEBUG
"i2sbus: failed to allocate cii\n");
937 /* use the private data to point to the codec info */
938 cii
->sdev
= soundbus_dev_get(dev
);
940 cii
->codec_data
= data
;
944 "i2sbus: failed to get soundbus dev reference\n");
949 if (!try_module_get(THIS_MODULE
)) {
950 printk(KERN_DEBUG
"i2sbus: failed to get module reference!\n");
955 if (!try_module_get(ci
->owner
)) {
957 "i2sbus: failed to get module reference to codec owner!\n");
959 goto out_put_this_module
;
963 err
= snd_pcm_new(card
, dev
->pcmname
, dev
->pcmid
, 0, 0,
966 printk(KERN_DEBUG
"i2sbus: failed to create pcm\n");
967 goto out_put_ci_module
;
969 dev
->pcm
->dev
= &dev
->ofdev
.dev
;
972 /* ALSA yet again sucks.
973 * If it is ever fixed, remove this line. See below. */
976 if (!i2sdev
->out
.created
&& out
) {
977 if (dev
->pcm
->card
!= card
) {
980 "Can't attach same bus to different cards!\n");
982 goto out_put_ci_module
;
984 err
= snd_pcm_new_stream(dev
->pcm
, SNDRV_PCM_STREAM_PLAYBACK
, 1);
986 goto out_put_ci_module
;
987 snd_pcm_set_ops(dev
->pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
988 &i2sbus_playback_ops
);
989 i2sdev
->out
.created
= 1;
992 if (!i2sdev
->in
.created
&& in
) {
993 if (dev
->pcm
->card
!= card
) {
995 "Can't attach same bus to different cards!\n");
997 goto out_put_ci_module
;
999 err
= snd_pcm_new_stream(dev
->pcm
, SNDRV_PCM_STREAM_CAPTURE
, 1);
1001 goto out_put_ci_module
;
1002 snd_pcm_set_ops(dev
->pcm
, SNDRV_PCM_STREAM_CAPTURE
,
1003 &i2sbus_record_ops
);
1004 i2sdev
->in
.created
= 1;
1007 /* so we have to register the pcm after adding any substream
1008 * to it because alsa doesn't create the devices for the
1009 * substreams when we add them later.
1010 * Therefore, force in and out on both busses (above) and
1011 * register the pcm now instead of just after creating it.
1013 err
= snd_device_register(card
, dev
->pcm
);
1015 printk(KERN_ERR
"i2sbus: error registering new pcm\n");
1016 goto out_put_ci_module
;
1018 /* no errors any more, so let's add this to our list */
1019 list_add(&cii
->list
, &dev
->codec_list
);
1021 dev
->pcm
->private_data
= i2sdev
;
1022 dev
->pcm
->private_free
= i2sbus_private_free
;
1024 /* well, we really should support scatter/gather DMA */
1025 snd_pcm_lib_preallocate_pages_for_all(
1026 dev
->pcm
, SNDRV_DMA_TYPE_DEV
,
1027 snd_dma_pci_data(macio_get_pci_dev(i2sdev
->macio
)),
1028 64 * 1024, 64 * 1024);
1032 module_put(ci
->owner
);
1033 out_put_this_module
:
1034 module_put(THIS_MODULE
);
1036 soundbus_dev_put(dev
);
1042 void i2sbus_detach_codec(struct soundbus_dev
*dev
, void *data
)
1044 struct codec_info_item
*cii
= NULL
, *i
;
1046 list_for_each_entry(i
, &dev
->codec_list
, list
) {
1047 if (i
->codec_data
== data
) {
1053 list_del(&cii
->list
);
1054 module_put(cii
->codec
->owner
);
1057 /* no more codecs, but still a pcm? */
1058 if (list_empty(&dev
->codec_list
) && dev
->pcm
) {
1059 /* the actual cleanup is done by the callback above! */
1060 snd_device_free(dev
->pcm
->card
, dev
->pcm
);