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 list_for_each_entry(cii, &sdev->codec_list, list) {
198 if (cii->codec->open) {
199 err = cii->codec->open(cii, pi->substream);
204 list_for_each_entry_reverse(rev,
205 &sdev->codec_list, list) {
206 if (found_this && rev->codec->close) {
207 rev->codec->close(rev,
219 mutex_unlock(&i2sdev->lock);
225 static int i2sbus_pcm_close(struct i2sbus_dev *i2sdev, int in)
227 struct codec_info_item *cii;
231 mutex_lock(&i2sdev->lock);
233 get_pcm_info(i2sdev, in, &pi, NULL);
235 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
236 if (cii->codec->close) {
237 tmp = cii->codec->close(cii, pi->substream);
243 pi->substream = NULL;
245 mutex_unlock(&i2sdev->lock);
249 static void i2sbus_wait_for_stop(struct i2sbus_dev *i2sdev,
253 struct completion done;
256 spin_lock_irqsave(&i2sdev->low_lock, flags);
257 if (pi->dbdma_ring.stopping) {
258 init_completion(&done);
259 pi->stop_completion = &done;
260 spin_unlock_irqrestore(&i2sdev->low_lock, flags);
261 timeout = wait_for_completion_timeout(&done, HZ);
262 spin_lock_irqsave(&i2sdev->low_lock, flags);
263 pi->stop_completion = NULL;
265 /* timeout expired, stop dbdma forcefully */
266 printk(KERN_ERR "i2sbus_wait_for_stop: timed out\n");
267 /* make sure RUN, PAUSE and S0 bits are cleared */
268 out_le32(&pi->dbdma->control, (RUN | PAUSE | 1) << 16);
269 pi->dbdma_ring.stopping = 0;
271 while (in_le32(&pi->dbdma->status) & ACTIVE) {
278 spin_unlock_irqrestore(&i2sdev->low_lock, flags);
282 void i2sbus_wait_for_stop_both(struct i2sbus_dev *i2sdev)
286 get_pcm_info(i2sdev, 0, &pi, NULL);
287 i2sbus_wait_for_stop(i2sdev, pi);
288 get_pcm_info(i2sdev, 1, &pi, NULL);
289 i2sbus_wait_for_stop(i2sdev, pi);
293 static int i2sbus_hw_params(struct snd_pcm_substream *substream,
294 struct snd_pcm_hw_params *params)
296 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
299 static inline int i2sbus_hw_free(struct snd_pcm_substream *substream, int in)
301 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
304 get_pcm_info(i2sdev, in, &pi, NULL);
305 if (pi->dbdma_ring.stopping)
306 i2sbus_wait_for_stop(i2sdev, pi);
307 snd_pcm_lib_free_pages(substream);
311 static int i2sbus_playback_hw_free(struct snd_pcm_substream *substream)
313 return i2sbus_hw_free(substream, 0);
316 static int i2sbus_record_hw_free(struct snd_pcm_substream *substream)
318 return i2sbus_hw_free(substream, 1);
321 static int i2sbus_pcm_prepare(struct i2sbus_dev *i2sdev, int in)
323 /* whee. Hard work now. The user has selected a bitrate
324 * and bit format, so now we have to program our
325 * I2S controller appropriately. */
326 struct snd_pcm_runtime *runtime;
327 struct dbdma_cmd *command;
328 int i, periodsize, nperiods;
331 struct codec_info_item *cii;
332 int sfr = 0; /* serial format register */
333 int dws = 0; /* data word sizes reg */
335 struct pcm_info *pi, *other;
338 unsigned int cmd, stopaddr;
340 mutex_lock(&i2sdev->lock);
342 get_pcm_info(i2sdev, in, &pi, &other);
344 if (pi->dbdma_ring.running) {
348 if (pi->dbdma_ring.stopping)
349 i2sbus_wait_for_stop(i2sdev, pi);
351 if (!pi->substream || !pi->substream->runtime) {
356 runtime = pi->substream->runtime;
359 ((i2sdev->format != runtime->format)
360 || (i2sdev->rate != runtime->rate))) {
365 i2sdev->format = runtime->format;
366 i2sdev->rate = runtime->rate;
368 periodsize = snd_pcm_lib_period_bytes(pi->substream);
369 nperiods = pi->substream->runtime->periods;
370 pi->current_period = 0;
372 /* generate dbdma command ring first */
373 command = pi->dbdma_ring.cmds;
374 memset(command, 0, (nperiods + 2) * sizeof(struct dbdma_cmd));
376 /* commands to DMA to/from the ring */
378 * For input, we need to do a graceful stop; if we abort
379 * the DMA, we end up with leftover bytes that corrupt
380 * the next recording. To do this we set the S0 status
381 * bit and wait for the DMA controller to stop. Each
382 * command has a branch condition to
383 * make it branch to a stop command if S0 is set.
384 * On input we also need to wait for the S7 bit to be
385 * set before turning off the DMA controller.
386 * In fact we do the graceful stop for output as well.
388 offset = runtime->dma_addr;
389 cmd = (in? INPUT_MORE: OUTPUT_MORE) | BR_IFSET | INTR_ALWAYS;
390 stopaddr = pi->dbdma_ring.bus_cmd_start +
391 (nperiods + 1) * sizeof(struct dbdma_cmd);
392 for (i = 0; i < nperiods; i++, command++, offset += periodsize) {
393 command->command = cpu_to_le16(cmd);
394 command->cmd_dep = cpu_to_le32(stopaddr);
395 command->phy_addr = cpu_to_le32(offset);
396 command->req_count = cpu_to_le16(periodsize);
399 /* branch back to beginning of ring */
400 command->command = cpu_to_le16(DBDMA_NOP | BR_ALWAYS);
401 command->cmd_dep = cpu_to_le32(pi->dbdma_ring.bus_cmd_start);
404 /* set stop command */
405 command->command = cpu_to_le16(DBDMA_STOP);
407 /* ok, let's set the serial format and stuff */
408 switch (runtime->format) {
410 case SNDRV_PCM_FORMAT_S16_BE:
411 case SNDRV_PCM_FORMAT_U16_BE:
412 /* FIXME: if we add different bus factors we need to
415 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
416 bi.bus_factor = cii->codec->bus_factor;
419 if (!bi.bus_factor) {
425 case SNDRV_PCM_FORMAT_S32_BE:
426 case SNDRV_PCM_FORMAT_U32_BE:
427 /* force 64x bus speed, otherwise the data cannot be
428 * transferred quickly enough! */
436 /* we assume all sysclocks are the same! */
437 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
438 bi.sysclock_factor = cii->codec->sysclock_factor;
442 if (clock_and_divisors(bi.sysclock_factor,
449 switch (bi.bus_factor) {
451 sfr |= I2S_SF_SERIAL_FORMAT_I2S_32X;
454 sfr |= I2S_SF_SERIAL_FORMAT_I2S_64X;
457 /* FIXME: THIS ASSUMES MASTER ALL THE TIME */
458 sfr |= I2S_SF_SCLK_MASTER;
460 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
462 if (cii->codec->prepare)
463 err = cii->codec->prepare(cii, &bi, pi->substream);
469 /* codecs are fine with it, so set our clocks */
471 dws = (2 << I2S_DWS_NUM_CHANNELS_IN_SHIFT) |
472 (2 << I2S_DWS_NUM_CHANNELS_OUT_SHIFT) |
473 I2S_DWS_DATA_IN_16BIT | I2S_DWS_DATA_OUT_16BIT;
475 dws = (2 << I2S_DWS_NUM_CHANNELS_IN_SHIFT) |
476 (2 << I2S_DWS_NUM_CHANNELS_OUT_SHIFT) |
477 I2S_DWS_DATA_IN_24BIT | I2S_DWS_DATA_OUT_24BIT;
479 /* early exit if already programmed correctly */
480 /* not locking these is fine since we touch them only in this function */
481 if (in_le32(&i2sdev->intfregs->serial_format) == sfr
482 && in_le32(&i2sdev->intfregs->data_word_sizes) == dws)
485 /* let's notify the codecs about clocks going away.
486 * For now we only do mastering on the i2s cell... */
487 list_for_each_entry(cii, &i2sdev->sound.codec_list, list)
488 if (cii->codec->switch_clock)
489 cii->codec->switch_clock(cii, CLOCK_SWITCH_PREPARE_SLAVE);
491 i2sbus_control_enable(i2sdev->control, i2sdev);
492 i2sbus_control_cell(i2sdev->control, i2sdev, 1);
494 out_le32(&i2sdev->intfregs->intr_ctl, I2S_PENDING_CLOCKS_STOPPED);
496 i2sbus_control_clock(i2sdev->control, i2sdev, 0);
500 /* wait for clock stopped. This can apparently take a while... */
503 !(in_le32(&i2sdev->intfregs->intr_ctl) & I2S_PENDING_CLOCKS_STOPPED)) {
506 out_le32(&i2sdev->intfregs->intr_ctl, I2S_PENDING_CLOCKS_STOPPED);
508 /* not locking these is fine since we touch them only in this function */
509 out_le32(&i2sdev->intfregs->serial_format, sfr);
510 out_le32(&i2sdev->intfregs->data_word_sizes, dws);
512 i2sbus_control_enable(i2sdev->control, i2sdev);
513 i2sbus_control_cell(i2sdev->control, i2sdev, 1);
514 i2sbus_control_clock(i2sdev->control, i2sdev, 1);
517 list_for_each_entry(cii, &i2sdev->sound.codec_list, list)
518 if (cii->codec->switch_clock)
519 cii->codec->switch_clock(cii, CLOCK_SWITCH_SLAVE);
522 mutex_unlock(&i2sdev->lock);
527 void i2sbus_pcm_prepare_both(struct i2sbus_dev *i2sdev)
529 i2sbus_pcm_prepare(i2sdev, 0);
530 i2sbus_pcm_prepare(i2sdev, 1);
534 static int i2sbus_pcm_trigger(struct i2sbus_dev *i2sdev, int in, int cmd)
536 struct codec_info_item *cii;
541 spin_lock_irqsave(&i2sdev->low_lock, flags);
543 get_pcm_info(i2sdev, in, &pi, NULL);
546 case SNDRV_PCM_TRIGGER_START:
547 case SNDRV_PCM_TRIGGER_RESUME:
548 if (pi->dbdma_ring.running) {
552 list_for_each_entry(cii, &i2sdev->sound.codec_list, list)
553 if (cii->codec->start)
554 cii->codec->start(cii, pi->substream);
555 pi->dbdma_ring.running = 1;
557 if (pi->dbdma_ring.stopping) {
558 /* Clear the S0 bit, then see if we stopped yet */
559 out_le32(&pi->dbdma->control, 1 << 16);
560 if (in_le32(&pi->dbdma->status) & ACTIVE) {
561 /* possible race here? */
563 if (in_le32(&pi->dbdma->status) & ACTIVE) {
564 pi->dbdma_ring.stopping = 0;
565 goto out_unlock; /* keep running */
570 /* make sure RUN, PAUSE and S0 bits are cleared */
571 out_le32(&pi->dbdma->control, (RUN | PAUSE | 1) << 16);
573 /* set branch condition select register */
574 out_le32(&pi->dbdma->br_sel, (1 << 16) | 1);
576 /* write dma command buffer address to the dbdma chip */
577 out_le32(&pi->dbdma->cmdptr, pi->dbdma_ring.bus_cmd_start);
579 /* initialize the frame count and current period */
580 pi->current_period = 0;
581 pi->frame_count = in_le32(&i2sdev->intfregs->frame_count);
583 /* set the DMA controller running */
584 out_le32(&pi->dbdma->control, (RUN << 16) | RUN);
589 case SNDRV_PCM_TRIGGER_STOP:
590 case SNDRV_PCM_TRIGGER_SUSPEND:
591 if (!pi->dbdma_ring.running) {
595 pi->dbdma_ring.running = 0;
597 /* Set the S0 bit to make the DMA branch to the stop cmd */
598 out_le32(&pi->dbdma->control, (1 << 16) | 1);
599 pi->dbdma_ring.stopping = 1;
601 list_for_each_entry(cii, &i2sdev->sound.codec_list, list)
602 if (cii->codec->stop)
603 cii->codec->stop(cii, pi->substream);
611 spin_unlock_irqrestore(&i2sdev->low_lock, flags);
615 static snd_pcm_uframes_t i2sbus_pcm_pointer(struct i2sbus_dev *i2sdev, int in)
620 get_pcm_info(i2sdev, in, &pi, NULL);
622 fc = in_le32(&i2sdev->intfregs->frame_count);
623 fc = fc - pi->frame_count;
625 if (fc >= pi->substream->runtime->buffer_size)
626 fc %= pi->substream->runtime->buffer_size;
630 static inline void handle_interrupt(struct i2sbus_dev *i2sdev, int in)
637 struct snd_pcm_runtime *runtime;
639 spin_lock(&i2sdev->low_lock);
640 get_pcm_info(i2sdev, in, &pi, NULL);
641 if (!pi->dbdma_ring.running && !pi->dbdma_ring.stopping)
644 i = pi->current_period;
645 runtime = pi->substream->runtime;
646 while (pi->dbdma_ring.cmds[i].xfer_status) {
647 if (le16_to_cpu(pi->dbdma_ring.cmds[i].xfer_status) & BT)
649 * BT is the branch taken bit. If it took a branch
650 * it is because we set the S0 bit to make it
651 * branch to the stop command.
654 pi->dbdma_ring.cmds[i].xfer_status = 0;
656 if (++i >= runtime->periods) {
658 pi->frame_count += runtime->buffer_size;
660 pi->current_period = i;
663 * Check the frame count. The DMA tends to get a bit
664 * ahead of the frame counter, which confuses the core.
666 fc = in_le32(&i2sdev->intfregs->frame_count);
667 nframes = i * runtime->period_size;
668 if (fc < pi->frame_count + nframes)
669 pi->frame_count = fc - nframes;
675 status = in_le32(&pi->dbdma->status);
676 if (!(status & ACTIVE) && (!in || (status & 0x80)))
678 if (--timeout <= 0) {
679 printk(KERN_ERR "i2sbus: timed out "
680 "waiting for DMA to stop!\n");
686 /* Turn off DMA controller, clear S0 bit */
687 out_le32(&pi->dbdma->control, (RUN | PAUSE | 1) << 16);
689 pi->dbdma_ring.stopping = 0;
690 if (pi->stop_completion)
691 complete(pi->stop_completion);
694 if (!pi->dbdma_ring.running)
696 spin_unlock(&i2sdev->low_lock);
697 /* may call _trigger again, hence needs to be unlocked */
698 snd_pcm_period_elapsed(pi->substream);
702 spin_unlock(&i2sdev->low_lock);
705 irqreturn_t i2sbus_tx_intr(int irq, void *devid)
707 handle_interrupt((struct i2sbus_dev *)devid, 0);
711 irqreturn_t i2sbus_rx_intr(int irq, void *devid)
713 handle_interrupt((struct i2sbus_dev *)devid, 1);
717 static int i2sbus_playback_open(struct snd_pcm_substream *substream)
719 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
723 i2sdev->out.substream = substream;
724 return i2sbus_pcm_open(i2sdev, 0);
727 static int i2sbus_playback_close(struct snd_pcm_substream *substream)
729 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
734 if (i2sdev->out.substream != substream)
736 err = i2sbus_pcm_close(i2sdev, 0);
738 i2sdev->out.substream = NULL;
742 static int i2sbus_playback_prepare(struct snd_pcm_substream *substream)
744 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
748 if (i2sdev->out.substream != substream)
750 return i2sbus_pcm_prepare(i2sdev, 0);
753 static int i2sbus_playback_trigger(struct snd_pcm_substream *substream, int cmd)
755 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
759 if (i2sdev->out.substream != substream)
761 return i2sbus_pcm_trigger(i2sdev, 0, cmd);
764 static snd_pcm_uframes_t i2sbus_playback_pointer(struct snd_pcm_substream
767 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
771 if (i2sdev->out.substream != substream)
773 return i2sbus_pcm_pointer(i2sdev, 0);
776 static struct snd_pcm_ops i2sbus_playback_ops = {
777 .open = i2sbus_playback_open,
778 .close = i2sbus_playback_close,
779 .ioctl = snd_pcm_lib_ioctl,
780 .hw_params = i2sbus_hw_params,
781 .hw_free = i2sbus_playback_hw_free,
782 .prepare = i2sbus_playback_prepare,
783 .trigger = i2sbus_playback_trigger,
784 .pointer = i2sbus_playback_pointer,
787 static int i2sbus_record_open(struct snd_pcm_substream *substream)
789 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
793 i2sdev->in.substream = substream;
794 return i2sbus_pcm_open(i2sdev, 1);
797 static int i2sbus_record_close(struct snd_pcm_substream *substream)
799 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
804 if (i2sdev->in.substream != substream)
806 err = i2sbus_pcm_close(i2sdev, 1);
808 i2sdev->in.substream = NULL;
812 static int i2sbus_record_prepare(struct snd_pcm_substream *substream)
814 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
818 if (i2sdev->in.substream != substream)
820 return i2sbus_pcm_prepare(i2sdev, 1);
823 static int i2sbus_record_trigger(struct snd_pcm_substream *substream, int cmd)
825 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
829 if (i2sdev->in.substream != substream)
831 return i2sbus_pcm_trigger(i2sdev, 1, cmd);
834 static snd_pcm_uframes_t i2sbus_record_pointer(struct snd_pcm_substream
837 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
841 if (i2sdev->in.substream != substream)
843 return i2sbus_pcm_pointer(i2sdev, 1);
846 static struct snd_pcm_ops i2sbus_record_ops = {
847 .open = i2sbus_record_open,
848 .close = i2sbus_record_close,
849 .ioctl = snd_pcm_lib_ioctl,
850 .hw_params = i2sbus_hw_params,
851 .hw_free = i2sbus_record_hw_free,
852 .prepare = i2sbus_record_prepare,
853 .trigger = i2sbus_record_trigger,
854 .pointer = i2sbus_record_pointer,
857 static void i2sbus_private_free(struct snd_pcm *pcm)
859 struct i2sbus_dev *i2sdev = snd_pcm_chip(pcm);
860 struct codec_info_item *p, *tmp;
862 i2sdev->sound.pcm = NULL;
863 i2sdev->out.created = 0;
864 i2sdev->in.created = 0;
865 list_for_each_entry_safe(p, tmp, &i2sdev->sound.codec_list, list) {
866 printk(KERN_ERR "i2sbus: a codec didn't unregister!\n");
868 module_put(p->codec->owner);
871 soundbus_dev_put(&i2sdev->sound);
872 module_put(THIS_MODULE);
876 i2sbus_attach_codec(struct soundbus_dev *dev, struct snd_card *card,
877 struct codec_info *ci, void *data)
879 int err, in = 0, out = 0;
880 struct transfer_info *tmp;
881 struct i2sbus_dev *i2sdev = soundbus_dev_to_i2sbus_dev(dev);
882 struct codec_info_item *cii;
884 if (!dev->pcmname || dev->pcmid == -1) {
885 printk(KERN_ERR "i2sbus: pcm name and id must be set!\n");
889 list_for_each_entry(cii, &dev->codec_list, list) {
890 if (cii->codec_data == data)
894 if (!ci->transfers || !ci->transfers->formats
895 || !ci->transfers->rates || !ci->usable)
898 /* we currently code the i2s transfer on the clock, and support only
900 if (ci->bus_factor != 32 && ci->bus_factor != 64)
903 /* If you want to fix this, you need to keep track of what transport infos
904 * are to be used, which codecs they belong to, and then fix all the
905 * sysclock/busclock stuff above to depend on which is usable */
906 list_for_each_entry(cii, &dev->codec_list, list) {
907 if (cii->codec->sysclock_factor != ci->sysclock_factor) {
909 "cannot yet handle multiple different sysclocks!\n");
912 if (cii->codec->bus_factor != ci->bus_factor) {
914 "cannot yet handle multiple different bus clocks!\n");
920 while (tmp->formats && tmp->rates) {
921 if (tmp->transfer_in)
928 cii = kzalloc(sizeof(struct codec_info_item), GFP_KERNEL);
930 printk(KERN_DEBUG "i2sbus: failed to allocate cii\n");
934 /* use the private data to point to the codec info */
935 cii->sdev = soundbus_dev_get(dev);
937 cii->codec_data = data;
941 "i2sbus: failed to get soundbus dev reference\n");
946 if (!try_module_get(THIS_MODULE)) {
947 printk(KERN_DEBUG "i2sbus: failed to get module reference!\n");
952 if (!try_module_get(ci->owner)) {
954 "i2sbus: failed to get module reference to codec owner!\n");
956 goto out_put_this_module;
960 err = snd_pcm_new(card, dev->pcmname, dev->pcmid, 0, 0,
963 printk(KERN_DEBUG "i2sbus: failed to create pcm\n");
964 goto out_put_ci_module;
966 dev->pcm->dev = &dev->ofdev.dev;
969 /* ALSA yet again sucks.
970 * If it is ever fixed, remove this line. See below. */
973 if (!i2sdev->out.created && out) {
974 if (dev->pcm->card != card) {
977 "Can't attach same bus to different cards!\n");
979 goto out_put_ci_module;
981 err = snd_pcm_new_stream(dev->pcm, SNDRV_PCM_STREAM_PLAYBACK, 1);
983 goto out_put_ci_module;
984 snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_PLAYBACK,
985 &i2sbus_playback_ops);
986 i2sdev->out.created = 1;
989 if (!i2sdev->in.created && in) {
990 if (dev->pcm->card != card) {
992 "Can't attach same bus to different cards!\n");
993 goto out_put_ci_module;
995 err = snd_pcm_new_stream(dev->pcm, SNDRV_PCM_STREAM_CAPTURE, 1);
997 goto out_put_ci_module;
998 snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_CAPTURE,
1000 i2sdev->in.created = 1;
1003 /* so we have to register the pcm after adding any substream
1004 * to it because alsa doesn't create the devices for the
1005 * substreams when we add them later.
1006 * Therefore, force in and out on both busses (above) and
1007 * register the pcm now instead of just after creating it.
1009 err = snd_device_register(card, dev->pcm);
1011 printk(KERN_ERR "i2sbus: error registering new pcm\n");
1012 goto out_put_ci_module;
1014 /* no errors any more, so let's add this to our list */
1015 list_add(&cii->list, &dev->codec_list);
1017 dev->pcm->private_data = i2sdev;
1018 dev->pcm->private_free = i2sbus_private_free;
1020 /* well, we really should support scatter/gather DMA */
1021 snd_pcm_lib_preallocate_pages_for_all(
1022 dev->pcm, SNDRV_DMA_TYPE_DEV,
1023 snd_dma_pci_data(macio_get_pci_dev(i2sdev->macio)),
1024 64 * 1024, 64 * 1024);
1028 module_put(ci->owner);
1029 out_put_this_module:
1030 module_put(THIS_MODULE);
1032 soundbus_dev_put(dev);
1038 void i2sbus_detach_codec(struct soundbus_dev *dev, void *data)
1040 struct codec_info_item *cii = NULL, *i;
1042 list_for_each_entry(i, &dev->codec_list, list) {
1043 if (i->codec_data == data) {
1049 list_del(&cii->list);
1050 module_put(cii->codec->owner);
1053 /* no more codecs, but still a pcm? */
1054 if (list_empty(&dev->codec_list) && dev->pcm) {
1055 /* the actual cleanup is done by the callback above! */
1056 snd_device_free(dev->pcm->card, dev->pcm);