RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / sound / aoa / soundbus / i2sbus / i2sbus-pcm.c
blobc6b42f9bdbc9e92eefb3bc3426bb970f297ebd2d
1 /*
2 * i2sbus driver -- pcm routines
4 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
6 * GPL v2, can be found in COPYING.
7 */
9 #include <asm/io.h>
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"
18 #include "i2sbus.h"
20 static inline void get_pcm_info(struct i2sbus_dev *i2sdev, int in,
21 struct pcm_info **pi, struct pcm_info **other)
23 if (in) {
24 if (pi)
25 *pi = &i2sdev->in;
26 if (other)
27 *other = &i2sdev->out;
28 } else {
29 if (pi)
30 *pi = &i2sdev->out;
31 if (other)
32 *other = &i2sdev->in;
36 static int clock_and_divisors(int mclk, int sclk, int rate, int *out)
38 /* sclk must be derived from mclk! */
39 if (mclk % sclk)
40 return -1;
41 /* derive sclk register value */
42 if (i2s_sf_sclkdiv(mclk / sclk, out))
43 return -1;
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;
48 return 0;
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;
54 return 0;
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;
60 return 0;
63 return -1;
66 #define CHECK_RATE(rate) \
67 do { if (rates & SNDRV_PCM_RATE_ ##rate) { \
68 int dummy; \
69 if (clock_and_divisors(sysclock_factor, \
70 bus_factor, rate, &dummy)) \
71 rates &= ~SNDRV_PCM_RATE_ ##rate; \
72 } } while (0)
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;
81 u64 formats = 0;
82 unsigned int rates = 0;
83 struct transfer_info v;
84 int result = 0;
85 int bus_factor = 0, sysclock_factor = 0;
86 int found_this;
88 mutex_lock(&i2sdev->lock);
90 get_pcm_info(i2sdev, in, &pi, &other);
92 hw = &pi->substream->runtime->hw;
93 sdev = &i2sdev->sound;
95 if (pi->active) {
96 /* alsa messed up */
97 result = -EBUSY;
98 goto out_unlock;
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) {
107 v = *ti;
108 if (ti->transfer_in == in
109 && cii->codec->usable(cii, ti, &v)) {
110 if (masks_inited) {
111 formats &= v.formats;
112 rates &= v.rates;
113 } else {
114 formats = v.formats;
115 rates = v.rates;
116 masks_inited = 1;
119 ti++;
122 if (!masks_inited || !bus_factor || !sysclock_factor) {
123 result = -ENODEV;
124 goto out_unlock;
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;
131 CHECK_RATE(5512);
132 CHECK_RATE(8000);
133 CHECK_RATE(11025);
134 CHECK_RATE(16000);
135 CHECK_RATE(22050);
136 CHECK_RATE(32000);
137 CHECK_RATE(44100);
138 CHECK_RATE(48000);
139 CHECK_RATE(64000);
140 CHECK_RATE(88200);
141 CHECK_RATE(96000);
142 CHECK_RATE(176400);
143 CHECK_RATE(192000);
144 hw->rates = rates;
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). */
172 hw->rate_min = 5512;
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.
181 if (other->active) {
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;
195 hw->periods_min = 3;
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);
200 if (err) {
201 result = err;
202 /* unwind */
203 found_this = 0;
204 list_for_each_entry_reverse(rev,
205 &sdev->codec_list, list) {
206 if (found_this && rev->codec->close) {
207 rev->codec->close(rev,
208 pi->substream);
210 if (rev == cii)
211 found_this = 1;
213 goto out_unlock;
218 out_unlock:
219 mutex_unlock(&i2sdev->lock);
220 return result;
223 #undef CHECK_RATE
225 static int i2sbus_pcm_close(struct i2sbus_dev *i2sdev, int in)
227 struct codec_info_item *cii;
228 struct pcm_info *pi;
229 int err = 0, tmp;
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);
238 if (tmp)
239 err = tmp;
243 pi->substream = NULL;
244 pi->active = 0;
245 mutex_unlock(&i2sdev->lock);
246 return err;
249 static void i2sbus_wait_for_stop(struct i2sbus_dev *i2sdev,
250 struct pcm_info *pi)
252 unsigned long flags;
253 struct completion done;
254 long timeout;
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;
264 if (timeout == 0) {
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;
270 timeout = 10;
271 while (in_le32(&pi->dbdma->status) & ACTIVE) {
272 if (--timeout <= 0)
273 break;
274 udelay(1);
278 spin_unlock_irqrestore(&i2sdev->low_lock, flags);
281 #ifdef CONFIG_PM
282 void i2sbus_wait_for_stop_both(struct i2sbus_dev *i2sdev)
284 struct pcm_info *pi;
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);
291 #endif
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);
302 struct pcm_info *pi;
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);
308 return 0;
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;
329 dma_addr_t offset;
330 struct bus_info bi;
331 struct codec_info_item *cii;
332 int sfr = 0; /* serial format register */
333 int dws = 0; /* data word sizes reg */
334 int input_16bit;
335 struct pcm_info *pi, *other;
336 int cnt;
337 int result = 0;
338 unsigned int cmd, stopaddr;
340 mutex_lock(&i2sdev->lock);
342 get_pcm_info(i2sdev, in, &pi, &other);
344 if (pi->dbdma_ring.running) {
345 result = -EBUSY;
346 goto out_unlock;
348 if (pi->dbdma_ring.stopping)
349 i2sbus_wait_for_stop(i2sdev, pi);
351 if (!pi->substream || !pi->substream->runtime) {
352 result = -EINVAL;
353 goto out_unlock;
356 runtime = pi->substream->runtime;
357 pi->active = 1;
358 if (other->active &&
359 ((i2sdev->format != runtime->format)
360 || (i2sdev->rate != runtime->rate))) {
361 result = -EINVAL;
362 goto out_unlock;
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);
402 command++;
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) {
409 /* 16 bit formats */
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
413 * do more here!! */
414 bi.bus_factor = 0;
415 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
416 bi.bus_factor = cii->codec->bus_factor;
417 break;
419 if (!bi.bus_factor) {
420 result = -ENODEV;
421 goto out_unlock;
423 input_16bit = 1;
424 break;
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! */
429 bi.bus_factor = 64;
430 input_16bit = 0;
431 break;
432 default:
433 result = -EINVAL;
434 goto out_unlock;
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;
439 break;
442 if (clock_and_divisors(bi.sysclock_factor,
443 bi.bus_factor,
444 runtime->rate,
445 &sfr) < 0) {
446 result = -EINVAL;
447 goto out_unlock;
449 switch (bi.bus_factor) {
450 case 32:
451 sfr |= I2S_SF_SERIAL_FORMAT_I2S_32X;
452 break;
453 case 64:
454 sfr |= I2S_SF_SERIAL_FORMAT_I2S_64X;
455 break;
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) {
461 int err = 0;
462 if (cii->codec->prepare)
463 err = cii->codec->prepare(cii, &bi, pi->substream);
464 if (err) {
465 result = err;
466 goto out_unlock;
469 /* codecs are fine with it, so set our clocks */
470 if (input_16bit)
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;
474 else
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)
483 goto out_unlock;
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);
498 msleep(1);
500 /* wait for clock stopped. This can apparently take a while... */
501 cnt = 100;
502 while (cnt-- &&
503 !(in_le32(&i2sdev->intfregs->intr_ctl) & I2S_PENDING_CLOCKS_STOPPED)) {
504 msleep(5);
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);
515 msleep(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);
521 out_unlock:
522 mutex_unlock(&i2sdev->lock);
523 return result;
526 #ifdef CONFIG_PM
527 void i2sbus_pcm_prepare_both(struct i2sbus_dev *i2sdev)
529 i2sbus_pcm_prepare(i2sdev, 0);
530 i2sbus_pcm_prepare(i2sdev, 1);
532 #endif
534 static int i2sbus_pcm_trigger(struct i2sbus_dev *i2sdev, int in, int cmd)
536 struct codec_info_item *cii;
537 struct pcm_info *pi;
538 int result = 0;
539 unsigned long flags;
541 spin_lock_irqsave(&i2sdev->low_lock, flags);
543 get_pcm_info(i2sdev, in, &pi, NULL);
545 switch (cmd) {
546 case SNDRV_PCM_TRIGGER_START:
547 case SNDRV_PCM_TRIGGER_RESUME:
548 if (pi->dbdma_ring.running) {
549 result = -EALREADY;
550 goto out_unlock;
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? */
562 udelay(10);
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);
586 /* off you go! */
587 break;
589 case SNDRV_PCM_TRIGGER_STOP:
590 case SNDRV_PCM_TRIGGER_SUSPEND:
591 if (!pi->dbdma_ring.running) {
592 result = -EALREADY;
593 goto out_unlock;
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);
604 break;
605 default:
606 result = -EINVAL;
607 goto out_unlock;
610 out_unlock:
611 spin_unlock_irqrestore(&i2sdev->low_lock, flags);
612 return result;
615 static snd_pcm_uframes_t i2sbus_pcm_pointer(struct i2sbus_dev *i2sdev, int in)
617 struct pcm_info *pi;
618 u32 fc;
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;
627 return fc;
630 static inline void handle_interrupt(struct i2sbus_dev *i2sdev, int in)
632 struct pcm_info *pi;
633 u32 fc, nframes;
634 u32 status;
635 int timeout, i;
636 int dma_stopped = 0;
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)
642 goto out_unlock;
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.
653 dma_stopped = 1;
654 pi->dbdma_ring.cmds[i].xfer_status = 0;
656 if (++i >= runtime->periods) {
657 i = 0;
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;
672 if (dma_stopped) {
673 timeout = 1000;
674 for (;;) {
675 status = in_le32(&pi->dbdma->status);
676 if (!(status & ACTIVE) && (!in || (status & 0x80)))
677 break;
678 if (--timeout <= 0) {
679 printk(KERN_ERR "i2sbus: timed out "
680 "waiting for DMA to stop!\n");
681 break;
683 udelay(1);
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)
695 goto out_unlock;
696 spin_unlock(&i2sdev->low_lock);
697 /* may call _trigger again, hence needs to be unlocked */
698 snd_pcm_period_elapsed(pi->substream);
699 return;
701 out_unlock:
702 spin_unlock(&i2sdev->low_lock);
705 irqreturn_t i2sbus_tx_intr(int irq, void *devid)
707 handle_interrupt((struct i2sbus_dev *)devid, 0);
708 return IRQ_HANDLED;
711 irqreturn_t i2sbus_rx_intr(int irq, void *devid)
713 handle_interrupt((struct i2sbus_dev *)devid, 1);
714 return IRQ_HANDLED;
717 static int i2sbus_playback_open(struct snd_pcm_substream *substream)
719 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
721 if (!i2sdev)
722 return -EINVAL;
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);
730 int err;
732 if (!i2sdev)
733 return -EINVAL;
734 if (i2sdev->out.substream != substream)
735 return -EINVAL;
736 err = i2sbus_pcm_close(i2sdev, 0);
737 if (!err)
738 i2sdev->out.substream = NULL;
739 return err;
742 static int i2sbus_playback_prepare(struct snd_pcm_substream *substream)
744 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
746 if (!i2sdev)
747 return -EINVAL;
748 if (i2sdev->out.substream != substream)
749 return -EINVAL;
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);
757 if (!i2sdev)
758 return -EINVAL;
759 if (i2sdev->out.substream != substream)
760 return -EINVAL;
761 return i2sbus_pcm_trigger(i2sdev, 0, cmd);
764 static snd_pcm_uframes_t i2sbus_playback_pointer(struct snd_pcm_substream
765 *substream)
767 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
769 if (!i2sdev)
770 return -EINVAL;
771 if (i2sdev->out.substream != substream)
772 return 0;
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);
791 if (!i2sdev)
792 return -EINVAL;
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);
800 int err;
802 if (!i2sdev)
803 return -EINVAL;
804 if (i2sdev->in.substream != substream)
805 return -EINVAL;
806 err = i2sbus_pcm_close(i2sdev, 1);
807 if (!err)
808 i2sdev->in.substream = NULL;
809 return err;
812 static int i2sbus_record_prepare(struct snd_pcm_substream *substream)
814 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
816 if (!i2sdev)
817 return -EINVAL;
818 if (i2sdev->in.substream != substream)
819 return -EINVAL;
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);
827 if (!i2sdev)
828 return -EINVAL;
829 if (i2sdev->in.substream != substream)
830 return -EINVAL;
831 return i2sbus_pcm_trigger(i2sdev, 1, cmd);
834 static snd_pcm_uframes_t i2sbus_record_pointer(struct snd_pcm_substream
835 *substream)
837 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
839 if (!i2sdev)
840 return -EINVAL;
841 if (i2sdev->in.substream != substream)
842 return 0;
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");
867 list_del(&p->list);
868 module_put(p->codec->owner);
869 kfree(p);
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");
886 return -EINVAL;
889 list_for_each_entry(cii, &dev->codec_list, list) {
890 if (cii->codec_data == data)
891 return -EALREADY;
894 if (!ci->transfers || !ci->transfers->formats
895 || !ci->transfers->rates || !ci->usable)
896 return -EINVAL;
898 /* we currently code the i2s transfer on the clock, and support only
899 * 32 and 64 */
900 if (ci->bus_factor != 32 && ci->bus_factor != 64)
901 return -EINVAL;
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) {
908 printk(KERN_DEBUG
909 "cannot yet handle multiple different sysclocks!\n");
910 return -EINVAL;
912 if (cii->codec->bus_factor != ci->bus_factor) {
913 printk(KERN_DEBUG
914 "cannot yet handle multiple different bus clocks!\n");
915 return -EINVAL;
919 tmp = ci->transfers;
920 while (tmp->formats && tmp->rates) {
921 if (tmp->transfer_in)
922 in = 1;
923 else
924 out = 1;
925 tmp++;
928 cii = kzalloc(sizeof(struct codec_info_item), GFP_KERNEL);
929 if (!cii) {
930 printk(KERN_DEBUG "i2sbus: failed to allocate cii\n");
931 return -ENOMEM;
934 /* use the private data to point to the codec info */
935 cii->sdev = soundbus_dev_get(dev);
936 cii->codec = ci;
937 cii->codec_data = data;
939 if (!cii->sdev) {
940 printk(KERN_DEBUG
941 "i2sbus: failed to get soundbus dev reference\n");
942 err = -ENODEV;
943 goto out_free_cii;
946 if (!try_module_get(THIS_MODULE)) {
947 printk(KERN_DEBUG "i2sbus: failed to get module reference!\n");
948 err = -EBUSY;
949 goto out_put_sdev;
952 if (!try_module_get(ci->owner)) {
953 printk(KERN_DEBUG
954 "i2sbus: failed to get module reference to codec owner!\n");
955 err = -EBUSY;
956 goto out_put_this_module;
959 if (!dev->pcm) {
960 err = snd_pcm_new(card, dev->pcmname, dev->pcmid, 0, 0,
961 &dev->pcm);
962 if (err) {
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. */
971 out = in = 1;
973 if (!i2sdev->out.created && out) {
974 if (dev->pcm->card != card) {
975 /* eh? */
976 printk(KERN_ERR
977 "Can't attach same bus to different cards!\n");
978 err = -EINVAL;
979 goto out_put_ci_module;
981 err = snd_pcm_new_stream(dev->pcm, SNDRV_PCM_STREAM_PLAYBACK, 1);
982 if (err)
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) {
991 printk(KERN_ERR
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);
996 if (err)
997 goto out_put_ci_module;
998 snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_CAPTURE,
999 &i2sbus_record_ops);
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);
1010 if (err) {
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);
1026 return 0;
1027 out_put_ci_module:
1028 module_put(ci->owner);
1029 out_put_this_module:
1030 module_put(THIS_MODULE);
1031 out_put_sdev:
1032 soundbus_dev_put(dev);
1033 out_free_cii:
1034 kfree(cii);
1035 return err;
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) {
1044 cii = i;
1045 break;
1048 if (cii) {
1049 list_del(&cii->list);
1050 module_put(cii->codec->owner);
1051 kfree(cii);
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);