1 // SPDX-License-Identifier: GPL-2.0-only
3 * i2sbus driver -- pcm routines
5 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
9 #include <linux/delay.h>
10 #include <linux/slab.h>
11 #include <sound/core.h>
12 #include <asm/macio.h>
13 #include <linux/pci.h>
14 #include <linux/module.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
&= pcm_format_to_bits(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 DECLARE_COMPLETION_ONSTACK(done
);
260 spin_lock_irqsave(&i2sdev
->low_lock
, flags
);
261 if (pi
->dbdma_ring
.stopping
) {
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 inline int i2sbus_hw_free(struct snd_pcm_substream
*substream
, int in
)
298 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
301 get_pcm_info(i2sdev
, in
, &pi
, NULL
);
302 if (pi
->dbdma_ring
.stopping
)
303 i2sbus_wait_for_stop(i2sdev
, pi
);
307 static int i2sbus_playback_hw_free(struct snd_pcm_substream
*substream
)
309 return i2sbus_hw_free(substream
, 0);
312 static int i2sbus_record_hw_free(struct snd_pcm_substream
*substream
)
314 return i2sbus_hw_free(substream
, 1);
317 static int i2sbus_pcm_prepare(struct i2sbus_dev
*i2sdev
, int in
)
319 /* whee. Hard work now. The user has selected a bitrate
320 * and bit format, so now we have to program our
321 * I2S controller appropriately. */
322 struct snd_pcm_runtime
*runtime
;
323 struct dbdma_cmd
*command
;
324 int i
, periodsize
, nperiods
;
327 struct codec_info_item
*cii
;
328 int sfr
= 0; /* serial format register */
329 int dws
= 0; /* data word sizes reg */
331 struct pcm_info
*pi
, *other
;
334 unsigned int cmd
, stopaddr
;
336 mutex_lock(&i2sdev
->lock
);
338 get_pcm_info(i2sdev
, in
, &pi
, &other
);
340 if (pi
->dbdma_ring
.running
) {
344 if (pi
->dbdma_ring
.stopping
)
345 i2sbus_wait_for_stop(i2sdev
, pi
);
347 if (!pi
->substream
|| !pi
->substream
->runtime
) {
352 runtime
= pi
->substream
->runtime
;
355 ((i2sdev
->format
!= runtime
->format
)
356 || (i2sdev
->rate
!= runtime
->rate
))) {
361 i2sdev
->format
= runtime
->format
;
362 i2sdev
->rate
= runtime
->rate
;
364 periodsize
= snd_pcm_lib_period_bytes(pi
->substream
);
365 nperiods
= pi
->substream
->runtime
->periods
;
366 pi
->current_period
= 0;
368 /* generate dbdma command ring first */
369 command
= pi
->dbdma_ring
.cmds
;
370 memset(command
, 0, (nperiods
+ 2) * sizeof(struct dbdma_cmd
));
372 /* commands to DMA to/from the ring */
374 * For input, we need to do a graceful stop; if we abort
375 * the DMA, we end up with leftover bytes that corrupt
376 * the next recording. To do this we set the S0 status
377 * bit and wait for the DMA controller to stop. Each
378 * command has a branch condition to
379 * make it branch to a stop command if S0 is set.
380 * On input we also need to wait for the S7 bit to be
381 * set before turning off the DMA controller.
382 * In fact we do the graceful stop for output as well.
384 offset
= runtime
->dma_addr
;
385 cmd
= (in
? INPUT_MORE
: OUTPUT_MORE
) | BR_IFSET
| INTR_ALWAYS
;
386 stopaddr
= pi
->dbdma_ring
.bus_cmd_start
+
387 (nperiods
+ 1) * sizeof(struct dbdma_cmd
);
388 for (i
= 0; i
< nperiods
; i
++, command
++, offset
+= periodsize
) {
389 command
->command
= cpu_to_le16(cmd
);
390 command
->cmd_dep
= cpu_to_le32(stopaddr
);
391 command
->phy_addr
= cpu_to_le32(offset
);
392 command
->req_count
= cpu_to_le16(periodsize
);
395 /* branch back to beginning of ring */
396 command
->command
= cpu_to_le16(DBDMA_NOP
| BR_ALWAYS
);
397 command
->cmd_dep
= cpu_to_le32(pi
->dbdma_ring
.bus_cmd_start
);
400 /* set stop command */
401 command
->command
= cpu_to_le16(DBDMA_STOP
);
403 /* ok, let's set the serial format and stuff */
404 switch (runtime
->format
) {
406 case SNDRV_PCM_FORMAT_S16_BE
:
407 case SNDRV_PCM_FORMAT_U16_BE
:
408 /* FIXME: if we add different bus factors we need to
411 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
) {
412 bi
.bus_factor
= cii
->codec
->bus_factor
;
415 if (!bi
.bus_factor
) {
421 case SNDRV_PCM_FORMAT_S32_BE
:
422 case SNDRV_PCM_FORMAT_U32_BE
:
423 /* force 64x bus speed, otherwise the data cannot be
424 * transferred quickly enough! */
432 /* we assume all sysclocks are the same! */
433 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
) {
434 bi
.sysclock_factor
= cii
->codec
->sysclock_factor
;
438 if (clock_and_divisors(bi
.sysclock_factor
,
445 switch (bi
.bus_factor
) {
447 sfr
|= I2S_SF_SERIAL_FORMAT_I2S_32X
;
450 sfr
|= I2S_SF_SERIAL_FORMAT_I2S_64X
;
453 /* FIXME: THIS ASSUMES MASTER ALL THE TIME */
454 sfr
|= I2S_SF_SCLK_MASTER
;
456 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
) {
458 if (cii
->codec
->prepare
)
459 err
= cii
->codec
->prepare(cii
, &bi
, pi
->substream
);
465 /* codecs are fine with it, so set our clocks */
467 dws
= (2 << I2S_DWS_NUM_CHANNELS_IN_SHIFT
) |
468 (2 << I2S_DWS_NUM_CHANNELS_OUT_SHIFT
) |
469 I2S_DWS_DATA_IN_16BIT
| I2S_DWS_DATA_OUT_16BIT
;
471 dws
= (2 << I2S_DWS_NUM_CHANNELS_IN_SHIFT
) |
472 (2 << I2S_DWS_NUM_CHANNELS_OUT_SHIFT
) |
473 I2S_DWS_DATA_IN_24BIT
| I2S_DWS_DATA_OUT_24BIT
;
475 /* early exit if already programmed correctly */
476 /* not locking these is fine since we touch them only in this function */
477 if (in_le32(&i2sdev
->intfregs
->serial_format
) == sfr
478 && in_le32(&i2sdev
->intfregs
->data_word_sizes
) == dws
)
481 /* let's notify the codecs about clocks going away.
482 * For now we only do mastering on the i2s cell... */
483 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
)
484 if (cii
->codec
->switch_clock
)
485 cii
->codec
->switch_clock(cii
, CLOCK_SWITCH_PREPARE_SLAVE
);
487 i2sbus_control_enable(i2sdev
->control
, i2sdev
);
488 i2sbus_control_cell(i2sdev
->control
, i2sdev
, 1);
490 out_le32(&i2sdev
->intfregs
->intr_ctl
, I2S_PENDING_CLOCKS_STOPPED
);
492 i2sbus_control_clock(i2sdev
->control
, i2sdev
, 0);
496 /* wait for clock stopped. This can apparently take a while... */
499 !(in_le32(&i2sdev
->intfregs
->intr_ctl
) & I2S_PENDING_CLOCKS_STOPPED
)) {
502 out_le32(&i2sdev
->intfregs
->intr_ctl
, I2S_PENDING_CLOCKS_STOPPED
);
504 /* not locking these is fine since we touch them only in this function */
505 out_le32(&i2sdev
->intfregs
->serial_format
, sfr
);
506 out_le32(&i2sdev
->intfregs
->data_word_sizes
, dws
);
508 i2sbus_control_enable(i2sdev
->control
, i2sdev
);
509 i2sbus_control_cell(i2sdev
->control
, i2sdev
, 1);
510 i2sbus_control_clock(i2sdev
->control
, i2sdev
, 1);
513 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
)
514 if (cii
->codec
->switch_clock
)
515 cii
->codec
->switch_clock(cii
, CLOCK_SWITCH_SLAVE
);
518 mutex_unlock(&i2sdev
->lock
);
523 void i2sbus_pcm_prepare_both(struct i2sbus_dev
*i2sdev
)
525 i2sbus_pcm_prepare(i2sdev
, 0);
526 i2sbus_pcm_prepare(i2sdev
, 1);
530 static int i2sbus_pcm_trigger(struct i2sbus_dev
*i2sdev
, int in
, int cmd
)
532 struct codec_info_item
*cii
;
537 spin_lock_irqsave(&i2sdev
->low_lock
, flags
);
539 get_pcm_info(i2sdev
, in
, &pi
, NULL
);
542 case SNDRV_PCM_TRIGGER_START
:
543 case SNDRV_PCM_TRIGGER_RESUME
:
544 if (pi
->dbdma_ring
.running
) {
548 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
)
549 if (cii
->codec
->start
)
550 cii
->codec
->start(cii
, pi
->substream
);
551 pi
->dbdma_ring
.running
= 1;
553 if (pi
->dbdma_ring
.stopping
) {
554 /* Clear the S0 bit, then see if we stopped yet */
555 out_le32(&pi
->dbdma
->control
, 1 << 16);
556 if (in_le32(&pi
->dbdma
->status
) & ACTIVE
) {
557 /* possible race here? */
559 if (in_le32(&pi
->dbdma
->status
) & ACTIVE
) {
560 pi
->dbdma_ring
.stopping
= 0;
561 goto out_unlock
; /* keep running */
566 /* make sure RUN, PAUSE and S0 bits are cleared */
567 out_le32(&pi
->dbdma
->control
, (RUN
| PAUSE
| 1) << 16);
569 /* set branch condition select register */
570 out_le32(&pi
->dbdma
->br_sel
, (1 << 16) | 1);
572 /* write dma command buffer address to the dbdma chip */
573 out_le32(&pi
->dbdma
->cmdptr
, pi
->dbdma_ring
.bus_cmd_start
);
575 /* initialize the frame count and current period */
576 pi
->current_period
= 0;
577 pi
->frame_count
= in_le32(&i2sdev
->intfregs
->frame_count
);
579 /* set the DMA controller running */
580 out_le32(&pi
->dbdma
->control
, (RUN
<< 16) | RUN
);
585 case SNDRV_PCM_TRIGGER_STOP
:
586 case SNDRV_PCM_TRIGGER_SUSPEND
:
587 if (!pi
->dbdma_ring
.running
) {
591 pi
->dbdma_ring
.running
= 0;
593 /* Set the S0 bit to make the DMA branch to the stop cmd */
594 out_le32(&pi
->dbdma
->control
, (1 << 16) | 1);
595 pi
->dbdma_ring
.stopping
= 1;
597 list_for_each_entry(cii
, &i2sdev
->sound
.codec_list
, list
)
598 if (cii
->codec
->stop
)
599 cii
->codec
->stop(cii
, pi
->substream
);
607 spin_unlock_irqrestore(&i2sdev
->low_lock
, flags
);
611 static snd_pcm_uframes_t
i2sbus_pcm_pointer(struct i2sbus_dev
*i2sdev
, int in
)
616 get_pcm_info(i2sdev
, in
, &pi
, NULL
);
618 fc
= in_le32(&i2sdev
->intfregs
->frame_count
);
619 fc
= fc
- pi
->frame_count
;
621 if (fc
>= pi
->substream
->runtime
->buffer_size
)
622 fc
%= pi
->substream
->runtime
->buffer_size
;
626 static inline void handle_interrupt(struct i2sbus_dev
*i2sdev
, int in
)
633 struct snd_pcm_runtime
*runtime
;
635 spin_lock(&i2sdev
->low_lock
);
636 get_pcm_info(i2sdev
, in
, &pi
, NULL
);
637 if (!pi
->dbdma_ring
.running
&& !pi
->dbdma_ring
.stopping
)
640 i
= pi
->current_period
;
641 runtime
= pi
->substream
->runtime
;
642 while (pi
->dbdma_ring
.cmds
[i
].xfer_status
) {
643 if (le16_to_cpu(pi
->dbdma_ring
.cmds
[i
].xfer_status
) & BT
)
645 * BT is the branch taken bit. If it took a branch
646 * it is because we set the S0 bit to make it
647 * branch to the stop command.
650 pi
->dbdma_ring
.cmds
[i
].xfer_status
= 0;
652 if (++i
>= runtime
->periods
) {
654 pi
->frame_count
+= runtime
->buffer_size
;
656 pi
->current_period
= i
;
659 * Check the frame count. The DMA tends to get a bit
660 * ahead of the frame counter, which confuses the core.
662 fc
= in_le32(&i2sdev
->intfregs
->frame_count
);
663 nframes
= i
* runtime
->period_size
;
664 if (fc
< pi
->frame_count
+ nframes
)
665 pi
->frame_count
= fc
- nframes
;
671 status
= in_le32(&pi
->dbdma
->status
);
672 if (!(status
& ACTIVE
) && (!in
|| (status
& 0x80)))
674 if (--timeout
<= 0) {
675 printk(KERN_ERR
"i2sbus: timed out "
676 "waiting for DMA to stop!\n");
682 /* Turn off DMA controller, clear S0 bit */
683 out_le32(&pi
->dbdma
->control
, (RUN
| PAUSE
| 1) << 16);
685 pi
->dbdma_ring
.stopping
= 0;
686 if (pi
->stop_completion
)
687 complete(pi
->stop_completion
);
690 if (!pi
->dbdma_ring
.running
)
692 spin_unlock(&i2sdev
->low_lock
);
693 /* may call _trigger again, hence needs to be unlocked */
694 snd_pcm_period_elapsed(pi
->substream
);
698 spin_unlock(&i2sdev
->low_lock
);
701 irqreturn_t
i2sbus_tx_intr(int irq
, void *devid
)
703 handle_interrupt((struct i2sbus_dev
*)devid
, 0);
707 irqreturn_t
i2sbus_rx_intr(int irq
, void *devid
)
709 handle_interrupt((struct i2sbus_dev
*)devid
, 1);
713 static int i2sbus_playback_open(struct snd_pcm_substream
*substream
)
715 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
719 i2sdev
->out
.substream
= substream
;
720 return i2sbus_pcm_open(i2sdev
, 0);
723 static int i2sbus_playback_close(struct snd_pcm_substream
*substream
)
725 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
730 if (i2sdev
->out
.substream
!= substream
)
732 err
= i2sbus_pcm_close(i2sdev
, 0);
734 i2sdev
->out
.substream
= NULL
;
738 static int i2sbus_playback_prepare(struct snd_pcm_substream
*substream
)
740 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
744 if (i2sdev
->out
.substream
!= substream
)
746 return i2sbus_pcm_prepare(i2sdev
, 0);
749 static int i2sbus_playback_trigger(struct snd_pcm_substream
*substream
, int cmd
)
751 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
755 if (i2sdev
->out
.substream
!= substream
)
757 return i2sbus_pcm_trigger(i2sdev
, 0, cmd
);
760 static snd_pcm_uframes_t
i2sbus_playback_pointer(struct snd_pcm_substream
763 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
767 if (i2sdev
->out
.substream
!= substream
)
769 return i2sbus_pcm_pointer(i2sdev
, 0);
772 static const struct snd_pcm_ops i2sbus_playback_ops
= {
773 .open
= i2sbus_playback_open
,
774 .close
= i2sbus_playback_close
,
775 .hw_free
= i2sbus_playback_hw_free
,
776 .prepare
= i2sbus_playback_prepare
,
777 .trigger
= i2sbus_playback_trigger
,
778 .pointer
= i2sbus_playback_pointer
,
781 static int i2sbus_record_open(struct snd_pcm_substream
*substream
)
783 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
787 i2sdev
->in
.substream
= substream
;
788 return i2sbus_pcm_open(i2sdev
, 1);
791 static int i2sbus_record_close(struct snd_pcm_substream
*substream
)
793 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
798 if (i2sdev
->in
.substream
!= substream
)
800 err
= i2sbus_pcm_close(i2sdev
, 1);
802 i2sdev
->in
.substream
= NULL
;
806 static int i2sbus_record_prepare(struct snd_pcm_substream
*substream
)
808 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
812 if (i2sdev
->in
.substream
!= substream
)
814 return i2sbus_pcm_prepare(i2sdev
, 1);
817 static int i2sbus_record_trigger(struct snd_pcm_substream
*substream
, int cmd
)
819 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
823 if (i2sdev
->in
.substream
!= substream
)
825 return i2sbus_pcm_trigger(i2sdev
, 1, cmd
);
828 static snd_pcm_uframes_t
i2sbus_record_pointer(struct snd_pcm_substream
831 struct i2sbus_dev
*i2sdev
= snd_pcm_substream_chip(substream
);
835 if (i2sdev
->in
.substream
!= substream
)
837 return i2sbus_pcm_pointer(i2sdev
, 1);
840 static const struct snd_pcm_ops i2sbus_record_ops
= {
841 .open
= i2sbus_record_open
,
842 .close
= i2sbus_record_close
,
843 .hw_free
= i2sbus_record_hw_free
,
844 .prepare
= i2sbus_record_prepare
,
845 .trigger
= i2sbus_record_trigger
,
846 .pointer
= i2sbus_record_pointer
,
849 static void i2sbus_private_free(struct snd_pcm
*pcm
)
851 struct i2sbus_dev
*i2sdev
= snd_pcm_chip(pcm
);
852 struct codec_info_item
*p
, *tmp
;
854 i2sdev
->sound
.pcm
= NULL
;
855 i2sdev
->out
.created
= 0;
856 i2sdev
->in
.created
= 0;
857 list_for_each_entry_safe(p
, tmp
, &i2sdev
->sound
.codec_list
, list
) {
858 printk(KERN_ERR
"i2sbus: a codec didn't unregister!\n");
860 module_put(p
->codec
->owner
);
863 soundbus_dev_put(&i2sdev
->sound
);
864 module_put(THIS_MODULE
);
868 i2sbus_attach_codec(struct soundbus_dev
*dev
, struct snd_card
*card
,
869 struct codec_info
*ci
, void *data
)
871 int err
, in
= 0, out
= 0;
872 struct transfer_info
*tmp
;
873 struct i2sbus_dev
*i2sdev
= soundbus_dev_to_i2sbus_dev(dev
);
874 struct codec_info_item
*cii
;
876 if (!dev
->pcmname
|| dev
->pcmid
== -1) {
877 printk(KERN_ERR
"i2sbus: pcm name and id must be set!\n");
881 list_for_each_entry(cii
, &dev
->codec_list
, list
) {
882 if (cii
->codec_data
== data
)
886 if (!ci
->transfers
|| !ci
->transfers
->formats
887 || !ci
->transfers
->rates
|| !ci
->usable
)
890 /* we currently code the i2s transfer on the clock, and support only
892 if (ci
->bus_factor
!= 32 && ci
->bus_factor
!= 64)
895 /* If you want to fix this, you need to keep track of what transport infos
896 * are to be used, which codecs they belong to, and then fix all the
897 * sysclock/busclock stuff above to depend on which is usable */
898 list_for_each_entry(cii
, &dev
->codec_list
, list
) {
899 if (cii
->codec
->sysclock_factor
!= ci
->sysclock_factor
) {
901 "cannot yet handle multiple different sysclocks!\n");
904 if (cii
->codec
->bus_factor
!= ci
->bus_factor
) {
906 "cannot yet handle multiple different bus clocks!\n");
912 while (tmp
->formats
&& tmp
->rates
) {
913 if (tmp
->transfer_in
)
920 cii
= kzalloc(sizeof(struct codec_info_item
), GFP_KERNEL
);
922 printk(KERN_DEBUG
"i2sbus: failed to allocate cii\n");
926 /* use the private data to point to the codec info */
927 cii
->sdev
= soundbus_dev_get(dev
);
929 cii
->codec_data
= data
;
933 "i2sbus: failed to get soundbus dev reference\n");
938 if (!try_module_get(THIS_MODULE
)) {
939 printk(KERN_DEBUG
"i2sbus: failed to get module reference!\n");
944 if (!try_module_get(ci
->owner
)) {
946 "i2sbus: failed to get module reference to codec owner!\n");
948 goto out_put_this_module
;
952 err
= snd_pcm_new(card
, dev
->pcmname
, dev
->pcmid
, 0, 0,
955 printk(KERN_DEBUG
"i2sbus: failed to create pcm\n");
956 goto out_put_ci_module
;
960 /* ALSA yet again sucks.
961 * If it is ever fixed, remove this line. See below. */
964 if (!i2sdev
->out
.created
&& out
) {
965 if (dev
->pcm
->card
!= card
) {
968 "Can't attach same bus to different cards!\n");
970 goto out_put_ci_module
;
972 err
= snd_pcm_new_stream(dev
->pcm
, SNDRV_PCM_STREAM_PLAYBACK
, 1);
974 goto out_put_ci_module
;
975 snd_pcm_set_ops(dev
->pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
976 &i2sbus_playback_ops
);
977 dev
->pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].dev
.parent
=
979 i2sdev
->out
.created
= 1;
982 if (!i2sdev
->in
.created
&& in
) {
983 if (dev
->pcm
->card
!= card
) {
985 "Can't attach same bus to different cards!\n");
987 goto out_put_ci_module
;
989 err
= snd_pcm_new_stream(dev
->pcm
, SNDRV_PCM_STREAM_CAPTURE
, 1);
991 goto out_put_ci_module
;
992 snd_pcm_set_ops(dev
->pcm
, SNDRV_PCM_STREAM_CAPTURE
,
994 dev
->pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].dev
.parent
=
996 i2sdev
->in
.created
= 1;
999 /* so we have to register the pcm after adding any substream
1000 * to it because alsa doesn't create the devices for the
1001 * substreams when we add them later.
1002 * Therefore, force in and out on both busses (above) and
1003 * register the pcm now instead of just after creating it.
1005 err
= snd_device_register(card
, dev
->pcm
);
1007 printk(KERN_ERR
"i2sbus: error registering new pcm\n");
1008 goto out_put_ci_module
;
1010 /* no errors any more, so let's add this to our list */
1011 list_add(&cii
->list
, &dev
->codec_list
);
1013 dev
->pcm
->private_data
= i2sdev
;
1014 dev
->pcm
->private_free
= i2sbus_private_free
;
1016 /* well, we really should support scatter/gather DMA */
1017 snd_pcm_set_managed_buffer_all(
1018 dev
->pcm
, SNDRV_DMA_TYPE_DEV
,
1019 &macio_get_pci_dev(i2sdev
->macio
)->dev
,
1020 64 * 1024, 64 * 1024);
1024 module_put(ci
->owner
);
1025 out_put_this_module
:
1026 module_put(THIS_MODULE
);
1028 soundbus_dev_put(dev
);
1034 void i2sbus_detach_codec(struct soundbus_dev
*dev
, void *data
)
1036 struct codec_info_item
*cii
= NULL
, *i
;
1038 list_for_each_entry(i
, &dev
->codec_list
, list
) {
1039 if (i
->codec_data
== data
) {
1045 list_del(&cii
->list
);
1046 module_put(cii
->codec
->owner
);
1049 /* no more codecs, but still a pcm? */
1050 if (list_empty(&dev
->codec_list
) && dev
->pcm
) {
1051 /* the actual cleanup is done by the callback above! */
1052 snd_device_free(dev
->pcm
->card
, dev
->pcm
);