WIP FPC-III support
[linux/fpc-iii.git] / sound / aoa / soundbus / i2sbus / pcm.c
blob1c8e8131a7162c1cb7d8ac159c9e6c79164fa75c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * i2sbus driver -- pcm routines
5 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
6 */
8 #include <linux/io.h>
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"
16 #include "i2sbus.h"
18 static inline void get_pcm_info(struct i2sbus_dev *i2sdev, int in,
19 struct pcm_info **pi, struct pcm_info **other)
21 if (in) {
22 if (pi)
23 *pi = &i2sdev->in;
24 if (other)
25 *other = &i2sdev->out;
26 } else {
27 if (pi)
28 *pi = &i2sdev->out;
29 if (other)
30 *other = &i2sdev->in;
34 static int clock_and_divisors(int mclk, int sclk, int rate, int *out)
36 /* sclk must be derived from mclk! */
37 if (mclk % sclk)
38 return -1;
39 /* derive sclk register value */
40 if (i2s_sf_sclkdiv(mclk / sclk, out))
41 return -1;
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;
46 return 0;
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;
52 return 0;
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;
58 return 0;
61 return -1;
64 #define CHECK_RATE(rate) \
65 do { if (rates & SNDRV_PCM_RATE_ ##rate) { \
66 int dummy; \
67 if (clock_and_divisors(sysclock_factor, \
68 bus_factor, rate, &dummy)) \
69 rates &= ~SNDRV_PCM_RATE_ ##rate; \
70 } } while (0)
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;
79 u64 formats = 0;
80 unsigned int rates = 0;
81 struct transfer_info v;
82 int result = 0;
83 int bus_factor = 0, sysclock_factor = 0;
84 int found_this;
86 mutex_lock(&i2sdev->lock);
88 get_pcm_info(i2sdev, in, &pi, &other);
90 hw = &pi->substream->runtime->hw;
91 sdev = &i2sdev->sound;
93 if (pi->active) {
94 /* alsa messed up */
95 result = -EBUSY;
96 goto out_unlock;
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) {
105 v = *ti;
106 if (ti->transfer_in == in
107 && cii->codec->usable(cii, ti, &v)) {
108 if (masks_inited) {
109 formats &= v.formats;
110 rates &= v.rates;
111 } else {
112 formats = v.formats;
113 rates = v.rates;
114 masks_inited = 1;
117 ti++;
120 if (!masks_inited || !bus_factor || !sysclock_factor) {
121 result = -ENODEV;
122 goto out_unlock;
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;
129 CHECK_RATE(5512);
130 CHECK_RATE(8000);
131 CHECK_RATE(11025);
132 CHECK_RATE(16000);
133 CHECK_RATE(22050);
134 CHECK_RATE(32000);
135 CHECK_RATE(44100);
136 CHECK_RATE(48000);
137 CHECK_RATE(64000);
138 CHECK_RATE(88200);
139 CHECK_RATE(96000);
140 CHECK_RATE(176400);
141 CHECK_RATE(192000);
142 hw->rates = rates;
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). */
170 hw->rate_min = 5512;
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.
179 if (other->active) {
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;
193 hw->periods_min = 3;
194 hw->periods_max = MAX_DBDMA_COMMANDS;
195 err = snd_pcm_hw_constraint_integer(pi->substream->runtime,
196 SNDRV_PCM_HW_PARAM_PERIODS);
197 if (err < 0) {
198 result = err;
199 goto out_unlock;
201 list_for_each_entry(cii, &sdev->codec_list, list) {
202 if (cii->codec->open) {
203 err = cii->codec->open(cii, pi->substream);
204 if (err) {
205 result = err;
206 /* unwind */
207 found_this = 0;
208 list_for_each_entry_reverse(rev,
209 &sdev->codec_list, list) {
210 if (found_this && rev->codec->close) {
211 rev->codec->close(rev,
212 pi->substream);
214 if (rev == cii)
215 found_this = 1;
217 goto out_unlock;
222 out_unlock:
223 mutex_unlock(&i2sdev->lock);
224 return result;
227 #undef CHECK_RATE
229 static int i2sbus_pcm_close(struct i2sbus_dev *i2sdev, int in)
231 struct codec_info_item *cii;
232 struct pcm_info *pi;
233 int err = 0, tmp;
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);
242 if (tmp)
243 err = tmp;
247 pi->substream = NULL;
248 pi->active = 0;
249 mutex_unlock(&i2sdev->lock);
250 return err;
253 static void i2sbus_wait_for_stop(struct i2sbus_dev *i2sdev,
254 struct pcm_info *pi)
256 unsigned long flags;
257 DECLARE_COMPLETION_ONSTACK(done);
258 long timeout;
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;
267 if (timeout == 0) {
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;
273 timeout = 10;
274 while (in_le32(&pi->dbdma->status) & ACTIVE) {
275 if (--timeout <= 0)
276 break;
277 udelay(1);
281 spin_unlock_irqrestore(&i2sdev->low_lock, flags);
284 #ifdef CONFIG_PM
285 void i2sbus_wait_for_stop_both(struct i2sbus_dev *i2sdev)
287 struct pcm_info *pi;
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);
294 #endif
296 static inline int i2sbus_hw_free(struct snd_pcm_substream *substream, int in)
298 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
299 struct pcm_info *pi;
301 get_pcm_info(i2sdev, in, &pi, NULL);
302 if (pi->dbdma_ring.stopping)
303 i2sbus_wait_for_stop(i2sdev, pi);
304 return 0;
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;
325 dma_addr_t offset;
326 struct bus_info bi;
327 struct codec_info_item *cii;
328 int sfr = 0; /* serial format register */
329 int dws = 0; /* data word sizes reg */
330 int input_16bit;
331 struct pcm_info *pi, *other;
332 int cnt;
333 int result = 0;
334 unsigned int cmd, stopaddr;
336 mutex_lock(&i2sdev->lock);
338 get_pcm_info(i2sdev, in, &pi, &other);
340 if (pi->dbdma_ring.running) {
341 result = -EBUSY;
342 goto out_unlock;
344 if (pi->dbdma_ring.stopping)
345 i2sbus_wait_for_stop(i2sdev, pi);
347 if (!pi->substream || !pi->substream->runtime) {
348 result = -EINVAL;
349 goto out_unlock;
352 runtime = pi->substream->runtime;
353 pi->active = 1;
354 if (other->active &&
355 ((i2sdev->format != runtime->format)
356 || (i2sdev->rate != runtime->rate))) {
357 result = -EINVAL;
358 goto out_unlock;
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);
398 command++;
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) {
405 /* 16 bit formats */
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
409 * do more here!! */
410 bi.bus_factor = 0;
411 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
412 bi.bus_factor = cii->codec->bus_factor;
413 break;
415 if (!bi.bus_factor) {
416 result = -ENODEV;
417 goto out_unlock;
419 input_16bit = 1;
420 break;
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! */
425 bi.bus_factor = 64;
426 input_16bit = 0;
427 break;
428 default:
429 result = -EINVAL;
430 goto out_unlock;
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;
435 break;
438 if (clock_and_divisors(bi.sysclock_factor,
439 bi.bus_factor,
440 runtime->rate,
441 &sfr) < 0) {
442 result = -EINVAL;
443 goto out_unlock;
445 switch (bi.bus_factor) {
446 case 32:
447 sfr |= I2S_SF_SERIAL_FORMAT_I2S_32X;
448 break;
449 case 64:
450 sfr |= I2S_SF_SERIAL_FORMAT_I2S_64X;
451 break;
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) {
457 int err = 0;
458 if (cii->codec->prepare)
459 err = cii->codec->prepare(cii, &bi, pi->substream);
460 if (err) {
461 result = err;
462 goto out_unlock;
465 /* codecs are fine with it, so set our clocks */
466 if (input_16bit)
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;
470 else
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)
479 goto out_unlock;
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);
494 msleep(1);
496 /* wait for clock stopped. This can apparently take a while... */
497 cnt = 100;
498 while (cnt-- &&
499 !(in_le32(&i2sdev->intfregs->intr_ctl) & I2S_PENDING_CLOCKS_STOPPED)) {
500 msleep(5);
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);
511 msleep(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);
517 out_unlock:
518 mutex_unlock(&i2sdev->lock);
519 return result;
522 #ifdef CONFIG_PM
523 void i2sbus_pcm_prepare_both(struct i2sbus_dev *i2sdev)
525 i2sbus_pcm_prepare(i2sdev, 0);
526 i2sbus_pcm_prepare(i2sdev, 1);
528 #endif
530 static int i2sbus_pcm_trigger(struct i2sbus_dev *i2sdev, int in, int cmd)
532 struct codec_info_item *cii;
533 struct pcm_info *pi;
534 int result = 0;
535 unsigned long flags;
537 spin_lock_irqsave(&i2sdev->low_lock, flags);
539 get_pcm_info(i2sdev, in, &pi, NULL);
541 switch (cmd) {
542 case SNDRV_PCM_TRIGGER_START:
543 case SNDRV_PCM_TRIGGER_RESUME:
544 if (pi->dbdma_ring.running) {
545 result = -EALREADY;
546 goto out_unlock;
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? */
558 udelay(10);
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);
582 /* off you go! */
583 break;
585 case SNDRV_PCM_TRIGGER_STOP:
586 case SNDRV_PCM_TRIGGER_SUSPEND:
587 if (!pi->dbdma_ring.running) {
588 result = -EALREADY;
589 goto out_unlock;
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);
600 break;
601 default:
602 result = -EINVAL;
603 goto out_unlock;
606 out_unlock:
607 spin_unlock_irqrestore(&i2sdev->low_lock, flags);
608 return result;
611 static snd_pcm_uframes_t i2sbus_pcm_pointer(struct i2sbus_dev *i2sdev, int in)
613 struct pcm_info *pi;
614 u32 fc;
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;
623 return fc;
626 static inline void handle_interrupt(struct i2sbus_dev *i2sdev, int in)
628 struct pcm_info *pi;
629 u32 fc, nframes;
630 u32 status;
631 int timeout, i;
632 int dma_stopped = 0;
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)
638 goto out_unlock;
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.
649 dma_stopped = 1;
650 pi->dbdma_ring.cmds[i].xfer_status = 0;
652 if (++i >= runtime->periods) {
653 i = 0;
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;
668 if (dma_stopped) {
669 timeout = 1000;
670 for (;;) {
671 status = in_le32(&pi->dbdma->status);
672 if (!(status & ACTIVE) && (!in || (status & 0x80)))
673 break;
674 if (--timeout <= 0) {
675 printk(KERN_ERR "i2sbus: timed out "
676 "waiting for DMA to stop!\n");
677 break;
679 udelay(1);
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)
691 goto out_unlock;
692 spin_unlock(&i2sdev->low_lock);
693 /* may call _trigger again, hence needs to be unlocked */
694 snd_pcm_period_elapsed(pi->substream);
695 return;
697 out_unlock:
698 spin_unlock(&i2sdev->low_lock);
701 irqreturn_t i2sbus_tx_intr(int irq, void *devid)
703 handle_interrupt((struct i2sbus_dev *)devid, 0);
704 return IRQ_HANDLED;
707 irqreturn_t i2sbus_rx_intr(int irq, void *devid)
709 handle_interrupt((struct i2sbus_dev *)devid, 1);
710 return IRQ_HANDLED;
713 static int i2sbus_playback_open(struct snd_pcm_substream *substream)
715 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
717 if (!i2sdev)
718 return -EINVAL;
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);
726 int err;
728 if (!i2sdev)
729 return -EINVAL;
730 if (i2sdev->out.substream != substream)
731 return -EINVAL;
732 err = i2sbus_pcm_close(i2sdev, 0);
733 if (!err)
734 i2sdev->out.substream = NULL;
735 return err;
738 static int i2sbus_playback_prepare(struct snd_pcm_substream *substream)
740 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
742 if (!i2sdev)
743 return -EINVAL;
744 if (i2sdev->out.substream != substream)
745 return -EINVAL;
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);
753 if (!i2sdev)
754 return -EINVAL;
755 if (i2sdev->out.substream != substream)
756 return -EINVAL;
757 return i2sbus_pcm_trigger(i2sdev, 0, cmd);
760 static snd_pcm_uframes_t i2sbus_playback_pointer(struct snd_pcm_substream
761 *substream)
763 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
765 if (!i2sdev)
766 return -EINVAL;
767 if (i2sdev->out.substream != substream)
768 return 0;
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);
785 if (!i2sdev)
786 return -EINVAL;
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);
794 int err;
796 if (!i2sdev)
797 return -EINVAL;
798 if (i2sdev->in.substream != substream)
799 return -EINVAL;
800 err = i2sbus_pcm_close(i2sdev, 1);
801 if (!err)
802 i2sdev->in.substream = NULL;
803 return err;
806 static int i2sbus_record_prepare(struct snd_pcm_substream *substream)
808 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
810 if (!i2sdev)
811 return -EINVAL;
812 if (i2sdev->in.substream != substream)
813 return -EINVAL;
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);
821 if (!i2sdev)
822 return -EINVAL;
823 if (i2sdev->in.substream != substream)
824 return -EINVAL;
825 return i2sbus_pcm_trigger(i2sdev, 1, cmd);
828 static snd_pcm_uframes_t i2sbus_record_pointer(struct snd_pcm_substream
829 *substream)
831 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
833 if (!i2sdev)
834 return -EINVAL;
835 if (i2sdev->in.substream != substream)
836 return 0;
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");
859 list_del(&p->list);
860 module_put(p->codec->owner);
861 kfree(p);
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");
878 return -EINVAL;
881 list_for_each_entry(cii, &dev->codec_list, list) {
882 if (cii->codec_data == data)
883 return -EALREADY;
886 if (!ci->transfers || !ci->transfers->formats
887 || !ci->transfers->rates || !ci->usable)
888 return -EINVAL;
890 /* we currently code the i2s transfer on the clock, and support only
891 * 32 and 64 */
892 if (ci->bus_factor != 32 && ci->bus_factor != 64)
893 return -EINVAL;
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) {
900 printk(KERN_DEBUG
901 "cannot yet handle multiple different sysclocks!\n");
902 return -EINVAL;
904 if (cii->codec->bus_factor != ci->bus_factor) {
905 printk(KERN_DEBUG
906 "cannot yet handle multiple different bus clocks!\n");
907 return -EINVAL;
911 tmp = ci->transfers;
912 while (tmp->formats && tmp->rates) {
913 if (tmp->transfer_in)
914 in = 1;
915 else
916 out = 1;
917 tmp++;
920 cii = kzalloc(sizeof(struct codec_info_item), GFP_KERNEL);
921 if (!cii) {
922 printk(KERN_DEBUG "i2sbus: failed to allocate cii\n");
923 return -ENOMEM;
926 /* use the private data to point to the codec info */
927 cii->sdev = soundbus_dev_get(dev);
928 cii->codec = ci;
929 cii->codec_data = data;
931 if (!cii->sdev) {
932 printk(KERN_DEBUG
933 "i2sbus: failed to get soundbus dev reference\n");
934 err = -ENODEV;
935 goto out_free_cii;
938 if (!try_module_get(THIS_MODULE)) {
939 printk(KERN_DEBUG "i2sbus: failed to get module reference!\n");
940 err = -EBUSY;
941 goto out_put_sdev;
944 if (!try_module_get(ci->owner)) {
945 printk(KERN_DEBUG
946 "i2sbus: failed to get module reference to codec owner!\n");
947 err = -EBUSY;
948 goto out_put_this_module;
951 if (!dev->pcm) {
952 err = snd_pcm_new(card, dev->pcmname, dev->pcmid, 0, 0,
953 &dev->pcm);
954 if (err) {
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. */
962 out = in = 1;
964 if (!i2sdev->out.created && out) {
965 if (dev->pcm->card != card) {
966 /* eh? */
967 printk(KERN_ERR
968 "Can't attach same bus to different cards!\n");
969 err = -EINVAL;
970 goto out_put_ci_module;
972 err = snd_pcm_new_stream(dev->pcm, SNDRV_PCM_STREAM_PLAYBACK, 1);
973 if (err)
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 =
978 &dev->ofdev.dev;
979 i2sdev->out.created = 1;
982 if (!i2sdev->in.created && in) {
983 if (dev->pcm->card != card) {
984 printk(KERN_ERR
985 "Can't attach same bus to different cards!\n");
986 err = -EINVAL;
987 goto out_put_ci_module;
989 err = snd_pcm_new_stream(dev->pcm, SNDRV_PCM_STREAM_CAPTURE, 1);
990 if (err)
991 goto out_put_ci_module;
992 snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_CAPTURE,
993 &i2sbus_record_ops);
994 dev->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].dev.parent =
995 &dev->ofdev.dev;
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);
1006 if (err) {
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);
1022 return 0;
1023 out_put_ci_module:
1024 module_put(ci->owner);
1025 out_put_this_module:
1026 module_put(THIS_MODULE);
1027 out_put_sdev:
1028 soundbus_dev_put(dev);
1029 out_free_cii:
1030 kfree(cii);
1031 return err;
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) {
1040 cii = i;
1041 break;
1044 if (cii) {
1045 list_del(&cii->list);
1046 module_put(cii->codec->owner);
1047 kfree(cii);
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);