1
2
3
4
5
6
7
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"
17
18static inline void get_pcm_info(struct i2sbus_dev *i2sdev, int in,
19 struct pcm_info **pi, struct pcm_info **other)
20{
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;
31 }
32}
33
34static int clock_and_divisors(int mclk, int sclk, int rate, int *out)
35{
36
37 if (mclk % sclk)
38 return -1;
39
40 if (i2s_sf_sclkdiv(mclk / sclk, out))
41 return -1;
42
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;
47 }
48 }
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;
53 }
54 }
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;
59 }
60 }
61 return -1;
62}
63
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)
71
72static int i2sbus_pcm_open(struct i2sbus_dev *i2sdev, int in)
73{
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;
85
86 mutex_lock(&i2sdev->lock);
87
88 get_pcm_info(i2sdev, in, &pi, &other);
89
90 hw = &pi->substream->runtime->hw;
91 sdev = &i2sdev->sound;
92
93 if (pi->active) {
94
95 result = -EBUSY;
96 goto out_unlock;
97 }
98
99
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;
115 }
116 }
117 ti++;
118 }
119 }
120 if (!masks_inited || !bus_factor || !sysclock_factor) {
121 result = -ENODEV;
122 goto out_unlock;
123 }
124
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;
128
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;
143
144
145
146
147
148
149
150
151
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
157
158
159
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);
164
165
166
167
168
169
170 hw->rate_min = 5512;
171 hw->rate_max = 192000;
172
173
174
175
176
177
178
179 if (other->active) {
180
181 hw->formats &= pcm_format_to_bits(i2sdev->format);
182
183 hw->rate_min = i2sdev->rate;
184 hw->rate_max = i2sdev->rate;
185 }
186
187 hw->channels_min = 2;
188 hw->channels_max = 2;
189
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;
200 }
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
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);
213 }
214 if (rev == cii)
215 found_this = 1;
216 }
217 goto out_unlock;
218 }
219 }
220 }
221
222 out_unlock:
223 mutex_unlock(&i2sdev->lock);
224 return result;
225}
226
227#undef CHECK_RATE
228
229static int i2sbus_pcm_close(struct i2sbus_dev *i2sdev, int in)
230{
231 struct codec_info_item *cii;
232 struct pcm_info *pi;
233 int err = 0, tmp;
234
235 mutex_lock(&i2sdev->lock);
236
237 get_pcm_info(i2sdev, in, &pi, NULL);
238
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;
244 }
245 }
246
247 pi->substream = NULL;
248 pi->active = 0;
249 mutex_unlock(&i2sdev->lock);
250 return err;
251}
252
253static void i2sbus_wait_for_stop(struct i2sbus_dev *i2sdev,
254 struct pcm_info *pi)
255{
256 unsigned long flags;
257 DECLARE_COMPLETION_ONSTACK(done);
258 long timeout;
259
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
269 printk(KERN_ERR "i2sbus_wait_for_stop: timed out\n");
270
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);
278 }
279 }
280 }
281 spin_unlock_irqrestore(&i2sdev->low_lock, flags);
282}
283
284#ifdef CONFIG_PM
285void i2sbus_wait_for_stop_both(struct i2sbus_dev *i2sdev)
286{
287 struct pcm_info *pi;
288
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);
293}
294#endif
295
296static inline int i2sbus_hw_free(struct snd_pcm_substream *substream, int in)
297{
298 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
299 struct pcm_info *pi;
300
301 get_pcm_info(i2sdev, in, &pi, NULL);
302 if (pi->dbdma_ring.stopping)
303 i2sbus_wait_for_stop(i2sdev, pi);
304 return 0;
305}
306
307static int i2sbus_playback_hw_free(struct snd_pcm_substream *substream)
308{
309 return i2sbus_hw_free(substream, 0);
310}
311
312static int i2sbus_record_hw_free(struct snd_pcm_substream *substream)
313{
314 return i2sbus_hw_free(substream, 1);
315}
316
317static int i2sbus_pcm_prepare(struct i2sbus_dev *i2sdev, int in)
318{
319
320
321
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;
329 int dws = 0;
330 int input_16bit;
331 struct pcm_info *pi, *other;
332 int cnt;
333 int result = 0;
334 unsigned int cmd, stopaddr;
335
336 mutex_lock(&i2sdev->lock);
337
338 get_pcm_info(i2sdev, in, &pi, &other);
339
340 if (pi->dbdma_ring.running) {
341 result = -EBUSY;
342 goto out_unlock;
343 }
344 if (pi->dbdma_ring.stopping)
345 i2sbus_wait_for_stop(i2sdev, pi);
346
347 if (!pi->substream || !pi->substream->runtime) {
348 result = -EINVAL;
349 goto out_unlock;
350 }
351
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;
359 }
360
361 i2sdev->format = runtime->format;
362 i2sdev->rate = runtime->rate;
363
364 periodsize = snd_pcm_lib_period_bytes(pi->substream);
365 nperiods = pi->substream->runtime->periods;
366 pi->current_period = 0;
367
368
369 command = pi->dbdma_ring.cmds;
370 memset(command, 0, (nperiods + 2) * sizeof(struct dbdma_cmd));
371
372
373
374
375
376
377
378
379
380
381
382
383
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);
393 }
394
395
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++;
399
400
401 command->command = cpu_to_le16(DBDMA_STOP);
402
403
404 switch (runtime->format) {
405
406 case SNDRV_PCM_FORMAT_S16_BE:
407 case SNDRV_PCM_FORMAT_U16_BE:
408
409
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;
414 }
415 if (!bi.bus_factor) {
416 result = -ENODEV;
417 goto out_unlock;
418 }
419 input_16bit = 1;
420 break;
421 case SNDRV_PCM_FORMAT_S32_BE:
422 case SNDRV_PCM_FORMAT_U32_BE:
423
424
425 bi.bus_factor = 64;
426 input_16bit = 0;
427 break;
428 default:
429 result = -EINVAL;
430 goto out_unlock;
431 }
432
433 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
434 bi.sysclock_factor = cii->codec->sysclock_factor;
435 break;
436 }
437
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;
444 }
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;
452 }
453
454 sfr |= I2S_SF_SCLK_MASTER;
455
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;
463 }
464 }
465
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;
474
475
476
477 if (in_le32(&i2sdev->intfregs->serial_format) == sfr
478 && in_le32(&i2sdev->intfregs->data_word_sizes) == dws)
479 goto out_unlock;
480
481
482
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);
486
487 i2sbus_control_enable(i2sdev->control, i2sdev);
488 i2sbus_control_cell(i2sdev->control, i2sdev, 1);
489
490 out_le32(&i2sdev->intfregs->intr_ctl, I2S_PENDING_CLOCKS_STOPPED);
491
492 i2sbus_control_clock(i2sdev->control, i2sdev, 0);
493
494 msleep(1);
495
496
497 cnt = 100;
498 while (cnt-- &&
499 !(in_le32(&i2sdev->intfregs->intr_ctl) & I2S_PENDING_CLOCKS_STOPPED)) {
500 msleep(5);
501 }
502 out_le32(&i2sdev->intfregs->intr_ctl, I2S_PENDING_CLOCKS_STOPPED);
503
504
505 out_le32(&i2sdev->intfregs->serial_format, sfr);
506 out_le32(&i2sdev->intfregs->data_word_sizes, dws);
507
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);
512
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);
516
517 out_unlock:
518 mutex_unlock(&i2sdev->lock);
519 return result;
520}
521
522#ifdef CONFIG_PM
523void i2sbus_pcm_prepare_both(struct i2sbus_dev *i2sdev)
524{
525 i2sbus_pcm_prepare(i2sdev, 0);
526 i2sbus_pcm_prepare(i2sdev, 1);
527}
528#endif
529
530static int i2sbus_pcm_trigger(struct i2sbus_dev *i2sdev, int in, int cmd)
531{
532 struct codec_info_item *cii;
533 struct pcm_info *pi;
534 int result = 0;
535 unsigned long flags;
536
537 spin_lock_irqsave(&i2sdev->low_lock, flags);
538
539 get_pcm_info(i2sdev, in, &pi, NULL);
540
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;
547 }
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;
552
553 if (pi->dbdma_ring.stopping) {
554
555 out_le32(&pi->dbdma->control, 1 << 16);
556 if (in_le32(&pi->dbdma->status) & ACTIVE) {
557
558 udelay(10);
559 if (in_le32(&pi->dbdma->status) & ACTIVE) {
560 pi->dbdma_ring.stopping = 0;
561 goto out_unlock;
562 }
563 }
564 }
565
566
567 out_le32(&pi->dbdma->control, (RUN | PAUSE | 1) << 16);
568
569
570 out_le32(&pi->dbdma->br_sel, (1 << 16) | 1);
571
572
573 out_le32(&pi->dbdma->cmdptr, pi->dbdma_ring.bus_cmd_start);
574
575
576 pi->current_period = 0;
577 pi->frame_count = in_le32(&i2sdev->intfregs->frame_count);
578
579
580 out_le32(&pi->dbdma->control, (RUN << 16) | RUN);
581
582
583 break;
584
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;
590 }
591 pi->dbdma_ring.running = 0;
592
593
594 out_le32(&pi->dbdma->control, (1 << 16) | 1);
595 pi->dbdma_ring.stopping = 1;
596
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;
604 }
605
606 out_unlock:
607 spin_unlock_irqrestore(&i2sdev->low_lock, flags);
608 return result;
609}
610
611static snd_pcm_uframes_t i2sbus_pcm_pointer(struct i2sbus_dev *i2sdev, int in)
612{
613 struct pcm_info *pi;
614 u32 fc;
615
616 get_pcm_info(i2sdev, in, &pi, NULL);
617
618 fc = in_le32(&i2sdev->intfregs->frame_count);
619 fc = fc - pi->frame_count;
620
621 if (fc >= pi->substream->runtime->buffer_size)
622 fc %= pi->substream->runtime->buffer_size;
623 return fc;
624}
625
626static inline void handle_interrupt(struct i2sbus_dev *i2sdev, int in)
627{
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;
634
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;
639
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)
644
645
646
647
648
649 dma_stopped = 1;
650 pi->dbdma_ring.cmds[i].xfer_status = 0;
651
652 if (++i >= runtime->periods) {
653 i = 0;
654 pi->frame_count += runtime->buffer_size;
655 }
656 pi->current_period = i;
657
658
659
660
661
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;
666 }
667
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;
678 }
679 udelay(1);
680 }
681
682
683 out_le32(&pi->dbdma->control, (RUN | PAUSE | 1) << 16);
684
685 pi->dbdma_ring.stopping = 0;
686 if (pi->stop_completion)
687 complete(pi->stop_completion);
688 }
689
690 if (!pi->dbdma_ring.running)
691 goto out_unlock;
692 spin_unlock(&i2sdev->low_lock);
693
694 snd_pcm_period_elapsed(pi->substream);
695 return;
696
697 out_unlock:
698 spin_unlock(&i2sdev->low_lock);
699}
700
701irqreturn_t i2sbus_tx_intr(int irq, void *devid)
702{
703 handle_interrupt((struct i2sbus_dev *)devid, 0);
704 return IRQ_HANDLED;
705}
706
707irqreturn_t i2sbus_rx_intr(int irq, void *devid)
708{
709 handle_interrupt((struct i2sbus_dev *)devid, 1);
710 return IRQ_HANDLED;
711}
712
713static int i2sbus_playback_open(struct snd_pcm_substream *substream)
714{
715 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
716
717 if (!i2sdev)
718 return -EINVAL;
719 i2sdev->out.substream = substream;
720 return i2sbus_pcm_open(i2sdev, 0);
721}
722
723static int i2sbus_playback_close(struct snd_pcm_substream *substream)
724{
725 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
726 int err;
727
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;
736}
737
738static int i2sbus_playback_prepare(struct snd_pcm_substream *substream)
739{
740 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
741
742 if (!i2sdev)
743 return -EINVAL;
744 if (i2sdev->out.substream != substream)
745 return -EINVAL;
746 return i2sbus_pcm_prepare(i2sdev, 0);
747}
748
749static int i2sbus_playback_trigger(struct snd_pcm_substream *substream, int cmd)
750{
751 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
752
753 if (!i2sdev)
754 return -EINVAL;
755 if (i2sdev->out.substream != substream)
756 return -EINVAL;
757 return i2sbus_pcm_trigger(i2sdev, 0, cmd);
758}
759
760static snd_pcm_uframes_t i2sbus_playback_pointer(struct snd_pcm_substream
761 *substream)
762{
763 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
764
765 if (!i2sdev)
766 return -EINVAL;
767 if (i2sdev->out.substream != substream)
768 return 0;
769 return i2sbus_pcm_pointer(i2sdev, 0);
770}
771
772static 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,
779};
780
781static int i2sbus_record_open(struct snd_pcm_substream *substream)
782{
783 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
784
785 if (!i2sdev)
786 return -EINVAL;
787 i2sdev->in.substream = substream;
788 return i2sbus_pcm_open(i2sdev, 1);
789}
790
791static int i2sbus_record_close(struct snd_pcm_substream *substream)
792{
793 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
794 int err;
795
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;
804}
805
806static int i2sbus_record_prepare(struct snd_pcm_substream *substream)
807{
808 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
809
810 if (!i2sdev)
811 return -EINVAL;
812 if (i2sdev->in.substream != substream)
813 return -EINVAL;
814 return i2sbus_pcm_prepare(i2sdev, 1);
815}
816
817static int i2sbus_record_trigger(struct snd_pcm_substream *substream, int cmd)
818{
819 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
820
821 if (!i2sdev)
822 return -EINVAL;
823 if (i2sdev->in.substream != substream)
824 return -EINVAL;
825 return i2sbus_pcm_trigger(i2sdev, 1, cmd);
826}
827
828static snd_pcm_uframes_t i2sbus_record_pointer(struct snd_pcm_substream
829 *substream)
830{
831 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
832
833 if (!i2sdev)
834 return -EINVAL;
835 if (i2sdev->in.substream != substream)
836 return 0;
837 return i2sbus_pcm_pointer(i2sdev, 1);
838}
839
840static 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,
847};
848
849static void i2sbus_private_free(struct snd_pcm *pcm)
850{
851 struct i2sbus_dev *i2sdev = snd_pcm_chip(pcm);
852 struct codec_info_item *p, *tmp;
853
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);
862 }
863 soundbus_dev_put(&i2sdev->sound);
864 module_put(THIS_MODULE);
865}
866
867int
868i2sbus_attach_codec(struct soundbus_dev *dev, struct snd_card *card,
869 struct codec_info *ci, void *data)
870{
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;
875
876 if (!dev->pcmname || dev->pcmid == -1) {
877 printk(KERN_ERR "i2sbus: pcm name and id must be set!\n");
878 return -EINVAL;
879 }
880
881 list_for_each_entry(cii, &dev->codec_list, list) {
882 if (cii->codec_data == data)
883 return -EALREADY;
884 }
885
886 if (!ci->transfers || !ci->transfers->formats
887 || !ci->transfers->rates || !ci->usable)
888 return -EINVAL;
889
890
891
892 if (ci->bus_factor != 32 && ci->bus_factor != 64)
893 return -EINVAL;
894
895
896
897
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;
903 }
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;
908 }
909 }
910
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++;
918 }
919
920 cii = kzalloc(sizeof(struct codec_info_item), GFP_KERNEL);
921 if (!cii)
922 return -ENOMEM;
923
924
925 cii->sdev = soundbus_dev_get(dev);
926 cii->codec = ci;
927 cii->codec_data = data;
928
929 if (!cii->sdev) {
930 printk(KERN_DEBUG
931 "i2sbus: failed to get soundbus dev reference\n");
932 err = -ENODEV;
933 goto out_free_cii;
934 }
935
936 if (!try_module_get(THIS_MODULE)) {
937 printk(KERN_DEBUG "i2sbus: failed to get module reference!\n");
938 err = -EBUSY;
939 goto out_put_sdev;
940 }
941
942 if (!try_module_get(ci->owner)) {
943 printk(KERN_DEBUG
944 "i2sbus: failed to get module reference to codec owner!\n");
945 err = -EBUSY;
946 goto out_put_this_module;
947 }
948
949 if (!dev->pcm) {
950 err = snd_pcm_new(card, dev->pcmname, dev->pcmid, 0, 0,
951 &dev->pcm);
952 if (err) {
953 printk(KERN_DEBUG "i2sbus: failed to create pcm\n");
954 goto out_put_ci_module;
955 }
956 }
957
958
959
960 out = in = 1;
961
962 if (!i2sdev->out.created && out) {
963 if (dev->pcm->card != card) {
964
965 printk(KERN_ERR
966 "Can't attach same bus to different cards!\n");
967 err = -EINVAL;
968 goto out_put_ci_module;
969 }
970 err = snd_pcm_new_stream(dev->pcm, SNDRV_PCM_STREAM_PLAYBACK, 1);
971 if (err)
972 goto out_put_ci_module;
973 snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_PLAYBACK,
974 &i2sbus_playback_ops);
975 dev->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].dev.parent =
976 &dev->ofdev.dev;
977 i2sdev->out.created = 1;
978 }
979
980 if (!i2sdev->in.created && in) {
981 if (dev->pcm->card != card) {
982 printk(KERN_ERR
983 "Can't attach same bus to different cards!\n");
984 err = -EINVAL;
985 goto out_put_ci_module;
986 }
987 err = snd_pcm_new_stream(dev->pcm, SNDRV_PCM_STREAM_CAPTURE, 1);
988 if (err)
989 goto out_put_ci_module;
990 snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_CAPTURE,
991 &i2sbus_record_ops);
992 dev->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].dev.parent =
993 &dev->ofdev.dev;
994 i2sdev->in.created = 1;
995 }
996
997
998
999
1000
1001
1002
1003 err = snd_device_register(card, dev->pcm);
1004 if (err) {
1005 printk(KERN_ERR "i2sbus: error registering new pcm\n");
1006 goto out_put_ci_module;
1007 }
1008
1009 list_add(&cii->list, &dev->codec_list);
1010
1011 dev->pcm->private_data = i2sdev;
1012 dev->pcm->private_free = i2sbus_private_free;
1013
1014
1015 snd_pcm_set_managed_buffer_all(
1016 dev->pcm, SNDRV_DMA_TYPE_DEV,
1017 &macio_get_pci_dev(i2sdev->macio)->dev,
1018 64 * 1024, 64 * 1024);
1019
1020 return 0;
1021 out_put_ci_module:
1022 module_put(ci->owner);
1023 out_put_this_module:
1024 module_put(THIS_MODULE);
1025 out_put_sdev:
1026 soundbus_dev_put(dev);
1027 out_free_cii:
1028 kfree(cii);
1029 return err;
1030}
1031
1032void i2sbus_detach_codec(struct soundbus_dev *dev, void *data)
1033{
1034 struct codec_info_item *cii = NULL, *i;
1035
1036 list_for_each_entry(i, &dev->codec_list, list) {
1037 if (i->codec_data == data) {
1038 cii = i;
1039 break;
1040 }
1041 }
1042 if (cii) {
1043 list_del(&cii->list);
1044 module_put(cii->codec->owner);
1045 kfree(cii);
1046 }
1047
1048 if (list_empty(&dev->codec_list) && dev->pcm) {
1049
1050 snd_device_free(dev->pcm->card, dev->pcm);
1051 }
1052}
1053