2 * i2sbus driver -- pcm routines
4 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
6 * GPL v2, can be found in COPYING.
10 #include <linux/delay.h>
11 /* So apparently there's a reason for requiring driver.h
12 * to be included first, even if I don't know it... */
13 #include <sound/driver.h>
14 #include <sound/core.h>
15 #include <asm/macio.h>
16 #include <linux/pci.h>
17 #include "../soundbus.h"
20 static inline void get_pcm_info(struct i2sbus_dev *i2sdev, int in,
21 struct pcm_info **pi, struct pcm_info **other)
27 *other = &i2sdev->out;
36 static int clock_and_divisors(int mclk, int sclk, int rate, int *out)
38 /* sclk must be derived from mclk! */
41 /* derive sclk register value */
42 if (i2s_sf_sclkdiv(mclk / sclk, out))
45 if (I2S_CLOCK_SPEED_18MHz % (rate * mclk) == 0) {
46 if (!i2s_sf_mclkdiv(I2S_CLOCK_SPEED_18MHz / (rate * mclk), out)) {
47 *out |= I2S_SF_CLOCK_SOURCE_18MHz;
51 if (I2S_CLOCK_SPEED_45MHz % (rate * mclk) == 0) {
52 if (!i2s_sf_mclkdiv(I2S_CLOCK_SPEED_45MHz / (rate * mclk), out)) {
53 *out |= I2S_SF_CLOCK_SOURCE_45MHz;
57 if (I2S_CLOCK_SPEED_49MHz % (rate * mclk) == 0) {
58 if (!i2s_sf_mclkdiv(I2S_CLOCK_SPEED_49MHz / (rate * mclk), out)) {
59 *out |= I2S_SF_CLOCK_SOURCE_49MHz;
66 #define CHECK_RATE(rate) \
67 do { if (rates & SNDRV_PCM_RATE_ ##rate) { \
69 if (clock_and_divisors(sysclock_factor, \
70 bus_factor, rate, &dummy)) \
71 rates &= ~SNDRV_PCM_RATE_ ##rate; \
74 static int i2sbus_pcm_open(struct i2sbus_dev *i2sdev, int in)
76 struct pcm_info *pi, *other;
77 struct soundbus_dev *sdev;
78 int masks_inited = 0, err;
79 struct codec_info_item *cii, *rev;
80 struct snd_pcm_hardware *hw;
82 unsigned int rates = 0;
83 struct transfer_info v;
85 int bus_factor = 0, sysclock_factor = 0;
88 mutex_lock(&i2sdev->lock);
90 get_pcm_info(i2sdev, in, &pi, &other);
92 hw = &pi->substream->runtime->hw;
93 sdev = &i2sdev->sound;
101 /* we now need to assign the hw */
102 list_for_each_entry(cii, &sdev->codec_list, list) {
103 struct transfer_info *ti = cii->codec->transfers;
104 bus_factor = cii->codec->bus_factor;
105 sysclock_factor = cii->codec->sysclock_factor;
106 while (ti->formats && ti->rates) {
108 if (ti->transfer_in == in
109 && cii->codec->usable(cii, ti, &v)) {
111 formats &= v.formats;
122 if (!masks_inited || !bus_factor || !sysclock_factor) {
126 /* bus dependent stuff */
127 hw->info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
128 SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_RESUME |
129 SNDRV_PCM_INFO_JOINT_DUPLEX;
146 /* well. the codec might want 24 bits only, and we'll
147 * ever only transfer 24 bits, but they are top-aligned!
148 * So for alsa, we claim that we're doing full 32 bit
149 * while in reality we'll ignore the lower 8 bits of
150 * that when doing playback (they're transferred as 0
151 * as far as I know, no codecs we have are 32-bit capable
152 * so I can't really test) and when doing recording we'll
153 * always have those lower 8 bits recorded as 0 */
154 if (formats & SNDRV_PCM_FMTBIT_S24_BE)
155 formats |= SNDRV_PCM_FMTBIT_S32_BE;
156 if (formats & SNDRV_PCM_FMTBIT_U24_BE)
157 formats |= SNDRV_PCM_FMTBIT_U32_BE;
158 /* now mask off what we can support. I suppose we could
159 * also support S24_3LE and some similar formats, but I
160 * doubt there's a codec that would be able to use that,
161 * so we don't support it here. */
162 hw->formats = formats & (SNDRV_PCM_FMTBIT_S16_BE |
163 SNDRV_PCM_FMTBIT_U16_BE |
164 SNDRV_PCM_FMTBIT_S32_BE |
165 SNDRV_PCM_FMTBIT_U32_BE);
167 /* we need to set the highest and lowest rate possible.
168 * These are the highest and lowest rates alsa can
169 * support properly in its bitfield.
170 * Below, we'll use that to restrict to the rate
171 * currently in use (if any). */
173 hw->rate_max = 192000;
174 /* if the other stream is active, then we can only
175 * support what it is currently using.
176 * FIXME: I lied. This comment is wrong. We can support
177 * anything that works with the same serial format, ie.
178 * when recording 24 bit sound we can well play 16 bit
179 * sound at the same time iff using the same transfer mode.
182 /* FIXME: is this guaranteed by the alsa api? */
183 hw->formats &= (1ULL << i2sdev->format);
184 /* see above, restrict rates to the one we already have */
185 hw->rate_min = i2sdev->rate;
186 hw->rate_max = i2sdev->rate;
189 hw->channels_min = 2;
190 hw->channels_max = 2;
191 /* these are somewhat arbitrary */
192 hw->buffer_bytes_max = 131072;
193 hw->period_bytes_min = 256;
194 hw->period_bytes_max = 16384;
196 hw->periods_max = MAX_DBDMA_COMMANDS;
197 err = snd_pcm_hw_constraint_integer(pi->substream->runtime,
198 SNDRV_PCM_HW_PARAM_PERIODS);
203 list_for_each_entry(cii, &sdev->codec_list, list) {
204 if (cii->codec->open) {
205 err = cii->codec->open(cii, pi->substream);
210 list_for_each_entry_reverse(rev,
211 &sdev->codec_list, list) {
212 if (found_this && rev->codec->close) {
213 rev->codec->close(rev,
225 mutex_unlock(&i2sdev->lock);
231 static int i2sbus_pcm_close(struct i2sbus_dev *i2sdev, int in)
233 struct codec_info_item *cii;
237 mutex_lock(&i2sdev->lock);
239 get_pcm_info(i2sdev, in, &pi, NULL);
241 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
242 if (cii->codec->close) {
243 tmp = cii->codec->close(cii, pi->substream);
249 pi->substream = NULL;
251 mutex_unlock(&i2sdev->lock);
255 static void i2sbus_wait_for_stop(struct i2sbus_dev *i2sdev,
259 struct completion done;
262 spin_lock_irqsave(&i2sdev->low_lock, flags);
263 if (pi->dbdma_ring.stopping) {
264 init_completion(&done);
265 pi->stop_completion = &done;
266 spin_unlock_irqrestore(&i2sdev->low_lock, flags);
267 timeout = wait_for_completion_timeout(&done, HZ);
268 spin_lock_irqsave(&i2sdev->low_lock, flags);
269 pi->stop_completion = NULL;
271 /* timeout expired, stop dbdma forcefully */
272 printk(KERN_ERR "i2sbus_wait_for_stop: timed out\n");
273 /* make sure RUN, PAUSE and S0 bits are cleared */
274 out_le32(&pi->dbdma->control, (RUN | PAUSE | 1) << 16);
275 pi->dbdma_ring.stopping = 0;
277 while (in_le32(&pi->dbdma->status) & ACTIVE) {
284 spin_unlock_irqrestore(&i2sdev->low_lock, flags);
288 void i2sbus_wait_for_stop_both(struct i2sbus_dev *i2sdev)
292 get_pcm_info(i2sdev, 0, &pi, NULL);
293 i2sbus_wait_for_stop(i2sdev, pi);
294 get_pcm_info(i2sdev, 1, &pi, NULL);
295 i2sbus_wait_for_stop(i2sdev, pi);
299 static int i2sbus_hw_params(struct snd_pcm_substream *substream,
300 struct snd_pcm_hw_params *params)
302 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
305 static inline int i2sbus_hw_free(struct snd_pcm_substream *substream, int in)
307 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
310 get_pcm_info(i2sdev, in, &pi, NULL);
311 if (pi->dbdma_ring.stopping)
312 i2sbus_wait_for_stop(i2sdev, pi);
313 snd_pcm_lib_free_pages(substream);
317 static int i2sbus_playback_hw_free(struct snd_pcm_substream *substream)
319 return i2sbus_hw_free(substream, 0);
322 static int i2sbus_record_hw_free(struct snd_pcm_substream *substream)
324 return i2sbus_hw_free(substream, 1);
327 static int i2sbus_pcm_prepare(struct i2sbus_dev *i2sdev, int in)
329 /* whee. Hard work now. The user has selected a bitrate
330 * and bit format, so now we have to program our
331 * I2S controller appropriately. */
332 struct snd_pcm_runtime *runtime;
333 struct dbdma_cmd *command;
334 int i, periodsize, nperiods;
337 struct codec_info_item *cii;
338 int sfr = 0; /* serial format register */
339 int dws = 0; /* data word sizes reg */
341 struct pcm_info *pi, *other;
344 unsigned int cmd, stopaddr;
346 mutex_lock(&i2sdev->lock);
348 get_pcm_info(i2sdev, in, &pi, &other);
350 if (pi->dbdma_ring.running) {
354 if (pi->dbdma_ring.stopping)
355 i2sbus_wait_for_stop(i2sdev, pi);
357 if (!pi->substream || !pi->substream->runtime) {
362 runtime = pi->substream->runtime;
365 ((i2sdev->format != runtime->format)
366 || (i2sdev->rate != runtime->rate))) {
371 i2sdev->format = runtime->format;
372 i2sdev->rate = runtime->rate;
374 periodsize = snd_pcm_lib_period_bytes(pi->substream);
375 nperiods = pi->substream->runtime->periods;
376 pi->current_period = 0;
378 /* generate dbdma command ring first */
379 command = pi->dbdma_ring.cmds;
380 memset(command, 0, (nperiods + 2) * sizeof(struct dbdma_cmd));
382 /* commands to DMA to/from the ring */
384 * For input, we need to do a graceful stop; if we abort
385 * the DMA, we end up with leftover bytes that corrupt
386 * the next recording. To do this we set the S0 status
387 * bit and wait for the DMA controller to stop. Each
388 * command has a branch condition to
389 * make it branch to a stop command if S0 is set.
390 * On input we also need to wait for the S7 bit to be
391 * set before turning off the DMA controller.
392 * In fact we do the graceful stop for output as well.
394 offset = runtime->dma_addr;
395 cmd = (in? INPUT_MORE: OUTPUT_MORE) | BR_IFSET | INTR_ALWAYS;
396 stopaddr = pi->dbdma_ring.bus_cmd_start +
397 (nperiods + 1) * sizeof(struct dbdma_cmd);
398 for (i = 0; i < nperiods; i++, command++, offset += periodsize) {
399 command->command = cpu_to_le16(cmd);
400 command->cmd_dep = cpu_to_le32(stopaddr);
401 command->phy_addr = cpu_to_le32(offset);
402 command->req_count = cpu_to_le16(periodsize);
405 /* branch back to beginning of ring */
406 command->command = cpu_to_le16(DBDMA_NOP | BR_ALWAYS);
407 command->cmd_dep = cpu_to_le32(pi->dbdma_ring.bus_cmd_start);
410 /* set stop command */
411 command->command = cpu_to_le16(DBDMA_STOP);
413 /* ok, let's set the serial format and stuff */
414 switch (runtime->format) {
416 case SNDRV_PCM_FORMAT_S16_BE:
417 case SNDRV_PCM_FORMAT_U16_BE:
418 /* FIXME: if we add different bus factors we need to
421 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
422 bi.bus_factor = cii->codec->bus_factor;
425 if (!bi.bus_factor) {
431 case SNDRV_PCM_FORMAT_S32_BE:
432 case SNDRV_PCM_FORMAT_U32_BE:
433 /* force 64x bus speed, otherwise the data cannot be
434 * transferred quickly enough! */
442 /* we assume all sysclocks are the same! */
443 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
444 bi.sysclock_factor = cii->codec->sysclock_factor;
448 if (clock_and_divisors(bi.sysclock_factor,
455 switch (bi.bus_factor) {
457 sfr |= I2S_SF_SERIAL_FORMAT_I2S_32X;
460 sfr |= I2S_SF_SERIAL_FORMAT_I2S_64X;
463 /* FIXME: THIS ASSUMES MASTER ALL THE TIME */
464 sfr |= I2S_SF_SCLK_MASTER;
466 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
468 if (cii->codec->prepare)
469 err = cii->codec->prepare(cii, &bi, pi->substream);
475 /* codecs are fine with it, so set our clocks */
477 dws = (2 << I2S_DWS_NUM_CHANNELS_IN_SHIFT) |
478 (2 << I2S_DWS_NUM_CHANNELS_OUT_SHIFT) |
479 I2S_DWS_DATA_IN_16BIT | I2S_DWS_DATA_OUT_16BIT;
481 dws = (2 << I2S_DWS_NUM_CHANNELS_IN_SHIFT) |
482 (2 << I2S_DWS_NUM_CHANNELS_OUT_SHIFT) |
483 I2S_DWS_DATA_IN_24BIT | I2S_DWS_DATA_OUT_24BIT;
485 /* early exit if already programmed correctly */
486 /* not locking these is fine since we touch them only in this function */
487 if (in_le32(&i2sdev->intfregs->serial_format) == sfr
488 && in_le32(&i2sdev->intfregs->data_word_sizes) == dws)
491 /* let's notify the codecs about clocks going away.
492 * For now we only do mastering on the i2s cell... */
493 list_for_each_entry(cii, &i2sdev->sound.codec_list, list)
494 if (cii->codec->switch_clock)
495 cii->codec->switch_clock(cii, CLOCK_SWITCH_PREPARE_SLAVE);
497 i2sbus_control_enable(i2sdev->control, i2sdev);
498 i2sbus_control_cell(i2sdev->control, i2sdev, 1);
500 out_le32(&i2sdev->intfregs->intr_ctl, I2S_PENDING_CLOCKS_STOPPED);
502 i2sbus_control_clock(i2sdev->control, i2sdev, 0);
506 /* wait for clock stopped. This can apparently take a while... */
509 !(in_le32(&i2sdev->intfregs->intr_ctl) & I2S_PENDING_CLOCKS_STOPPED)) {
512 out_le32(&i2sdev->intfregs->intr_ctl, I2S_PENDING_CLOCKS_STOPPED);
514 /* not locking these is fine since we touch them only in this function */
515 out_le32(&i2sdev->intfregs->serial_format, sfr);
516 out_le32(&i2sdev->intfregs->data_word_sizes, dws);
518 i2sbus_control_enable(i2sdev->control, i2sdev);
519 i2sbus_control_cell(i2sdev->control, i2sdev, 1);
520 i2sbus_control_clock(i2sdev->control, i2sdev, 1);
523 list_for_each_entry(cii, &i2sdev->sound.codec_list, list)
524 if (cii->codec->switch_clock)
525 cii->codec->switch_clock(cii, CLOCK_SWITCH_SLAVE);
528 mutex_unlock(&i2sdev->lock);
533 void i2sbus_pcm_prepare_both(struct i2sbus_dev *i2sdev)
535 i2sbus_pcm_prepare(i2sdev, 0);
536 i2sbus_pcm_prepare(i2sdev, 1);
540 static int i2sbus_pcm_trigger(struct i2sbus_dev *i2sdev, int in, int cmd)
542 struct codec_info_item *cii;
547 spin_lock_irqsave(&i2sdev->low_lock, flags);
549 get_pcm_info(i2sdev, in, &pi, NULL);
552 case SNDRV_PCM_TRIGGER_START:
553 case SNDRV_PCM_TRIGGER_RESUME:
554 if (pi->dbdma_ring.running) {
558 list_for_each_entry(cii, &i2sdev->sound.codec_list, list)
559 if (cii->codec->start)
560 cii->codec->start(cii, pi->substream);
561 pi->dbdma_ring.running = 1;
563 if (pi->dbdma_ring.stopping) {
564 /* Clear the S0 bit, then see if we stopped yet */
565 out_le32(&pi->dbdma->control, 1 << 16);
566 if (in_le32(&pi->dbdma->status) & ACTIVE) {
567 /* possible race here? */
569 if (in_le32(&pi->dbdma->status) & ACTIVE) {
570 pi->dbdma_ring.stopping = 0;
571 goto out_unlock; /* keep running */
576 /* make sure RUN, PAUSE and S0 bits are cleared */
577 out_le32(&pi->dbdma->control, (RUN | PAUSE | 1) << 16);
579 /* set branch condition select register */
580 out_le32(&pi->dbdma->br_sel, (1 << 16) | 1);
582 /* write dma command buffer address to the dbdma chip */
583 out_le32(&pi->dbdma->cmdptr, pi->dbdma_ring.bus_cmd_start);
585 /* initialize the frame count and current period */
586 pi->current_period = 0;
587 pi->frame_count = in_le32(&i2sdev->intfregs->frame_count);
589 /* set the DMA controller running */
590 out_le32(&pi->dbdma->control, (RUN << 16) | RUN);
595 case SNDRV_PCM_TRIGGER_STOP:
596 case SNDRV_PCM_TRIGGER_SUSPEND:
597 if (!pi->dbdma_ring.running) {
601 pi->dbdma_ring.running = 0;
603 /* Set the S0 bit to make the DMA branch to the stop cmd */
604 out_le32(&pi->dbdma->control, (1 << 16) | 1);
605 pi->dbdma_ring.stopping = 1;
607 list_for_each_entry(cii, &i2sdev->sound.codec_list, list)
608 if (cii->codec->stop)
609 cii->codec->stop(cii, pi->substream);
617 spin_unlock_irqrestore(&i2sdev->low_lock, flags);
621 static snd_pcm_uframes_t i2sbus_pcm_pointer(struct i2sbus_dev *i2sdev, int in)
626 get_pcm_info(i2sdev, in, &pi, NULL);
628 fc = in_le32(&i2sdev->intfregs->frame_count);
629 fc = fc - pi->frame_count;
631 if (fc >= pi->substream->runtime->buffer_size)
632 fc %= pi->substream->runtime->buffer_size;
636 static inline void handle_interrupt(struct i2sbus_dev *i2sdev, int in)
643 struct snd_pcm_runtime *runtime;
645 spin_lock(&i2sdev->low_lock);
646 get_pcm_info(i2sdev, in, &pi, NULL);
647 if (!pi->dbdma_ring.running && !pi->dbdma_ring.stopping)
650 i = pi->current_period;
651 runtime = pi->substream->runtime;
652 while (pi->dbdma_ring.cmds[i].xfer_status) {
653 if (le16_to_cpu(pi->dbdma_ring.cmds[i].xfer_status) & BT)
655 * BT is the branch taken bit. If it took a branch
656 * it is because we set the S0 bit to make it
657 * branch to the stop command.
660 pi->dbdma_ring.cmds[i].xfer_status = 0;
662 if (++i >= runtime->periods) {
664 pi->frame_count += runtime->buffer_size;
666 pi->current_period = i;
669 * Check the frame count. The DMA tends to get a bit
670 * ahead of the frame counter, which confuses the core.
672 fc = in_le32(&i2sdev->intfregs->frame_count);
673 nframes = i * runtime->period_size;
674 if (fc < pi->frame_count + nframes)
675 pi->frame_count = fc - nframes;
681 status = in_le32(&pi->dbdma->status);
682 if (!(status & ACTIVE) && (!in || (status & 0x80)))
684 if (--timeout <= 0) {
685 printk(KERN_ERR "i2sbus: timed out "
686 "waiting for DMA to stop!\n");
692 /* Turn off DMA controller, clear S0 bit */
693 out_le32(&pi->dbdma->control, (RUN | PAUSE | 1) << 16);
695 pi->dbdma_ring.stopping = 0;
696 if (pi->stop_completion)
697 complete(pi->stop_completion);
700 if (!pi->dbdma_ring.running)
702 spin_unlock(&i2sdev->low_lock);
703 /* may call _trigger again, hence needs to be unlocked */
704 snd_pcm_period_elapsed(pi->substream);
708 spin_unlock(&i2sdev->low_lock);
711 irqreturn_t i2sbus_tx_intr(int irq, void *devid)
713 handle_interrupt((struct i2sbus_dev *)devid, 0);
717 irqreturn_t i2sbus_rx_intr(int irq, void *devid)
719 handle_interrupt((struct i2sbus_dev *)devid, 1);
723 static int i2sbus_playback_open(struct snd_pcm_substream *substream)
725 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
729 i2sdev->out.substream = substream;
730 return i2sbus_pcm_open(i2sdev, 0);
733 static int i2sbus_playback_close(struct snd_pcm_substream *substream)
735 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
740 if (i2sdev->out.substream != substream)
742 err = i2sbus_pcm_close(i2sdev, 0);
744 i2sdev->out.substream = NULL;
748 static int i2sbus_playback_prepare(struct snd_pcm_substream *substream)
750 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
754 if (i2sdev->out.substream != substream)
756 return i2sbus_pcm_prepare(i2sdev, 0);
759 static int i2sbus_playback_trigger(struct snd_pcm_substream *substream, int cmd)
761 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
765 if (i2sdev->out.substream != substream)
767 return i2sbus_pcm_trigger(i2sdev, 0, cmd);
770 static snd_pcm_uframes_t i2sbus_playback_pointer(struct snd_pcm_substream
773 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
777 if (i2sdev->out.substream != substream)
779 return i2sbus_pcm_pointer(i2sdev, 0);
782 static struct snd_pcm_ops i2sbus_playback_ops = {
783 .open = i2sbus_playback_open,
784 .close = i2sbus_playback_close,
785 .ioctl = snd_pcm_lib_ioctl,
786 .hw_params = i2sbus_hw_params,
787 .hw_free = i2sbus_playback_hw_free,
788 .prepare = i2sbus_playback_prepare,
789 .trigger = i2sbus_playback_trigger,
790 .pointer = i2sbus_playback_pointer,
793 static int i2sbus_record_open(struct snd_pcm_substream *substream)
795 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
799 i2sdev->in.substream = substream;
800 return i2sbus_pcm_open(i2sdev, 1);
803 static int i2sbus_record_close(struct snd_pcm_substream *substream)
805 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
810 if (i2sdev->in.substream != substream)
812 err = i2sbus_pcm_close(i2sdev, 1);
814 i2sdev->in.substream = NULL;
818 static int i2sbus_record_prepare(struct snd_pcm_substream *substream)
820 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
824 if (i2sdev->in.substream != substream)
826 return i2sbus_pcm_prepare(i2sdev, 1);
829 static int i2sbus_record_trigger(struct snd_pcm_substream *substream, int cmd)
831 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
835 if (i2sdev->in.substream != substream)
837 return i2sbus_pcm_trigger(i2sdev, 1, cmd);
840 static snd_pcm_uframes_t i2sbus_record_pointer(struct snd_pcm_substream
843 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
847 if (i2sdev->in.substream != substream)
849 return i2sbus_pcm_pointer(i2sdev, 1);
852 static struct snd_pcm_ops i2sbus_record_ops = {
853 .open = i2sbus_record_open,
854 .close = i2sbus_record_close,
855 .ioctl = snd_pcm_lib_ioctl,
856 .hw_params = i2sbus_hw_params,
857 .hw_free = i2sbus_record_hw_free,
858 .prepare = i2sbus_record_prepare,
859 .trigger = i2sbus_record_trigger,
860 .pointer = i2sbus_record_pointer,
863 static void i2sbus_private_free(struct snd_pcm *pcm)
865 struct i2sbus_dev *i2sdev = snd_pcm_chip(pcm);
866 struct codec_info_item *p, *tmp;
868 i2sdev->sound.pcm = NULL;
869 i2sdev->out.created = 0;
870 i2sdev->in.created = 0;
871 list_for_each_entry_safe(p, tmp, &i2sdev->sound.codec_list, list) {
872 printk(KERN_ERR "i2sbus: a codec didn't unregister!\n");
874 module_put(p->codec->owner);
877 soundbus_dev_put(&i2sdev->sound);
878 module_put(THIS_MODULE);
882 i2sbus_attach_codec(struct soundbus_dev *dev, struct snd_card *card,
883 struct codec_info *ci, void *data)
885 int err, in = 0, out = 0;
886 struct transfer_info *tmp;
887 struct i2sbus_dev *i2sdev = soundbus_dev_to_i2sbus_dev(dev);
888 struct codec_info_item *cii;
890 if (!dev->pcmname || dev->pcmid == -1) {
891 printk(KERN_ERR "i2sbus: pcm name and id must be set!\n");
895 list_for_each_entry(cii, &dev->codec_list, list) {
896 if (cii->codec_data == data)
900 if (!ci->transfers || !ci->transfers->formats
901 || !ci->transfers->rates || !ci->usable)
904 /* we currently code the i2s transfer on the clock, and support only
906 if (ci->bus_factor != 32 && ci->bus_factor != 64)
909 /* If you want to fix this, you need to keep track of what transport infos
910 * are to be used, which codecs they belong to, and then fix all the
911 * sysclock/busclock stuff above to depend on which is usable */
912 list_for_each_entry(cii, &dev->codec_list, list) {
913 if (cii->codec->sysclock_factor != ci->sysclock_factor) {
915 "cannot yet handle multiple different sysclocks!\n");
918 if (cii->codec->bus_factor != ci->bus_factor) {
920 "cannot yet handle multiple different bus clocks!\n");
926 while (tmp->formats && tmp->rates) {
927 if (tmp->transfer_in)
934 cii = kzalloc(sizeof(struct codec_info_item), GFP_KERNEL);
936 printk(KERN_DEBUG "i2sbus: failed to allocate cii\n");
940 /* use the private data to point to the codec info */
941 cii->sdev = soundbus_dev_get(dev);
943 cii->codec_data = data;
947 "i2sbus: failed to get soundbus dev reference\n");
952 if (!try_module_get(THIS_MODULE)) {
953 printk(KERN_DEBUG "i2sbus: failed to get module reference!\n");
958 if (!try_module_get(ci->owner)) {
960 "i2sbus: failed to get module reference to codec owner!\n");
962 goto out_put_this_module;
966 err = snd_pcm_new(card, dev->pcmname, dev->pcmid, 0, 0,
969 printk(KERN_DEBUG "i2sbus: failed to create pcm\n");
970 goto out_put_ci_module;
972 dev->pcm->dev = &dev->ofdev.dev;
975 /* ALSA yet again sucks.
976 * If it is ever fixed, remove this line. See below. */
979 if (!i2sdev->out.created && out) {
980 if (dev->pcm->card != card) {
983 "Can't attach same bus to different cards!\n");
985 goto out_put_ci_module;
987 err = snd_pcm_new_stream(dev->pcm, SNDRV_PCM_STREAM_PLAYBACK, 1);
989 goto out_put_ci_module;
990 snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_PLAYBACK,
991 &i2sbus_playback_ops);
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");
999 goto out_put_ci_module;
1001 err = snd_pcm_new_stream(dev->pcm, SNDRV_PCM_STREAM_CAPTURE, 1);
1003 goto out_put_ci_module;
1004 snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_CAPTURE,
1005 &i2sbus_record_ops);
1006 i2sdev->in.created = 1;
1009 /* so we have to register the pcm after adding any substream
1010 * to it because alsa doesn't create the devices for the
1011 * substreams when we add them later.
1012 * Therefore, force in and out on both busses (above) and
1013 * register the pcm now instead of just after creating it.
1015 err = snd_device_register(card, dev->pcm);
1017 printk(KERN_ERR "i2sbus: error registering new pcm\n");
1018 goto out_put_ci_module;
1020 /* no errors any more, so let's add this to our list */
1021 list_add(&cii->list, &dev->codec_list);
1023 dev->pcm->private_data = i2sdev;
1024 dev->pcm->private_free = i2sbus_private_free;
1026 /* well, we really should support scatter/gather DMA */
1027 snd_pcm_lib_preallocate_pages_for_all(
1028 dev->pcm, SNDRV_DMA_TYPE_DEV,
1029 snd_dma_pci_data(macio_get_pci_dev(i2sdev->macio)),
1030 64 * 1024, 64 * 1024);
1034 module_put(ci->owner);
1035 out_put_this_module:
1036 module_put(THIS_MODULE);
1038 soundbus_dev_put(dev);
1044 void i2sbus_detach_codec(struct soundbus_dev *dev, void *data)
1046 struct codec_info_item *cii = NULL, *i;
1048 list_for_each_entry(i, &dev->codec_list, list) {
1049 if (i->codec_data == data) {
1055 list_del(&cii->list);
1056 module_put(cii->codec->owner);
1059 /* no more codecs, but still a pcm? */
1060 if (list_empty(&dev->codec_list) && dev->pcm) {
1061 /* the actual cleanup is done by the callback above! */
1062 snd_device_free(dev->pcm->card, dev->pcm);