1
2
3
4
5
6
7
8
9#include <linux/io.h>
10#include <linux/delay.h>
11#include <linux/slab.h>
12#include <sound/core.h>
13#include <asm/macio.h>
14#include <linux/pci.h>
15#include <linux/module.h>
16#include "../soundbus.h"
17#include "i2sbus.h"
18
19static inline void get_pcm_info(struct i2sbus_dev *i2sdev, int in,
20 struct pcm_info **pi, struct pcm_info **other)
21{
22 if (in) {
23 if (pi)
24 *pi = &i2sdev->in;
25 if (other)
26 *other = &i2sdev->out;
27 } else {
28 if (pi)
29 *pi = &i2sdev->out;
30 if (other)
31 *other = &i2sdev->in;
32 }
33}
34
35static int clock_and_divisors(int mclk, int sclk, int rate, int *out)
36{
37
38 if (mclk % sclk)
39 return -1;
40
41 if (i2s_sf_sclkdiv(mclk / sclk, out))
42 return -1;
43
44 if (I2S_CLOCK_SPEED_18MHz % (rate * mclk) == 0) {
45 if (!i2s_sf_mclkdiv(I2S_CLOCK_SPEED_18MHz / (rate * mclk), out)) {
46 *out |= I2S_SF_CLOCK_SOURCE_18MHz;
47 return 0;
48 }
49 }
50 if (I2S_CLOCK_SPEED_45MHz % (rate * mclk) == 0) {
51 if (!i2s_sf_mclkdiv(I2S_CLOCK_SPEED_45MHz / (rate * mclk), out)) {
52 *out |= I2S_SF_CLOCK_SOURCE_45MHz;
53 return 0;
54 }
55 }
56 if (I2S_CLOCK_SPEED_49MHz % (rate * mclk) == 0) {
57 if (!i2s_sf_mclkdiv(I2S_CLOCK_SPEED_49MHz / (rate * mclk), out)) {
58 *out |= I2S_SF_CLOCK_SOURCE_49MHz;
59 return 0;
60 }
61 }
62 return -1;
63}
64
65#define CHECK_RATE(rate) \
66 do { if (rates & SNDRV_PCM_RATE_ ##rate) { \
67 int dummy; \
68 if (clock_and_divisors(sysclock_factor, \
69 bus_factor, rate, &dummy)) \
70 rates &= ~SNDRV_PCM_RATE_ ##rate; \
71 } } while (0)
72
73static int i2sbus_pcm_open(struct i2sbus_dev *i2sdev, int in)
74{
75 struct pcm_info *pi, *other;
76 struct soundbus_dev *sdev;
77 int masks_inited = 0, err;
78 struct codec_info_item *cii, *rev;
79 struct snd_pcm_hardware *hw;
80 u64 formats = 0;
81 unsigned int rates = 0;
82 struct transfer_info v;
83 int result = 0;
84 int bus_factor = 0, sysclock_factor = 0;
85 int found_this;
86
87 mutex_lock(&i2sdev->lock);
88
89 get_pcm_info(i2sdev, in, &pi, &other);
90
91 hw = &pi->substream->runtime->hw;
92 sdev = &i2sdev->sound;
93
94 if (pi->active) {
95
96 result = -EBUSY;
97 goto out_unlock;
98 }
99
100
101 list_for_each_entry(cii, &sdev->codec_list, list) {
102 struct transfer_info *ti = cii->codec->transfers;
103 bus_factor = cii->codec->bus_factor;
104 sysclock_factor = cii->codec->sysclock_factor;
105 while (ti->formats && ti->rates) {
106 v = *ti;
107 if (ti->transfer_in == in
108 && cii->codec->usable(cii, ti, &v)) {
109 if (masks_inited) {
110 formats &= v.formats;
111 rates &= v.rates;
112 } else {
113 formats = v.formats;
114 rates = v.rates;
115 masks_inited = 1;
116 }
117 }
118 ti++;
119 }
120 }
121 if (!masks_inited || !bus_factor || !sysclock_factor) {
122 result = -ENODEV;
123 goto out_unlock;
124 }
125
126 hw->info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
127 SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_RESUME |
128 SNDRV_PCM_INFO_JOINT_DUPLEX;
129
130 CHECK_RATE(5512);
131 CHECK_RATE(8000);
132 CHECK_RATE(11025);
133 CHECK_RATE(16000);
134 CHECK_RATE(22050);
135 CHECK_RATE(32000);
136 CHECK_RATE(44100);
137 CHECK_RATE(48000);
138 CHECK_RATE(64000);
139 CHECK_RATE(88200);
140 CHECK_RATE(96000);
141 CHECK_RATE(176400);
142 CHECK_RATE(192000);
143 hw->rates = rates;
144
145
146
147
148
149
150
151
152
153 if (formats & SNDRV_PCM_FMTBIT_S24_BE)
154 formats |= SNDRV_PCM_FMTBIT_S32_BE;
155 if (formats & SNDRV_PCM_FMTBIT_U24_BE)
156 formats |= SNDRV_PCM_FMTBIT_U32_BE;
157
158
159
160
161 hw->formats = formats & (SNDRV_PCM_FMTBIT_S16_BE |
162 SNDRV_PCM_FMTBIT_U16_BE |
163 SNDRV_PCM_FMTBIT_S32_BE |
164 SNDRV_PCM_FMTBIT_U32_BE);
165
166
167
168
169
170
171 hw->rate_min = 5512;
172 hw->rate_max = 192000;
173
174
175
176
177
178
179
180 if (other->active) {
181
182 hw->formats &= pcm_format_to_bits(i2sdev->format);
183
184 hw->rate_min = i2sdev->rate;
185 hw->rate_max = i2sdev->rate;
186 }
187
188 hw->channels_min = 2;
189 hw->channels_max = 2;
190
191 hw->buffer_bytes_max = 131072;
192 hw->period_bytes_min = 256;
193 hw->period_bytes_max = 16384;
194 hw->periods_min = 3;
195 hw->periods_max = MAX_DBDMA_COMMANDS;
196 err = snd_pcm_hw_constraint_integer(pi->substream->runtime,
197 SNDRV_PCM_HW_PARAM_PERIODS);
198 if (err < 0) {
199 result = err;
200 goto out_unlock;
201 }
202 list_for_each_entry(cii, &sdev->codec_list, list) {
203 if (cii->codec->open) {
204 err = cii->codec->open(cii, pi->substream);
205 if (err) {
206 result = err;
207
208 found_this = 0;
209 list_for_each_entry_reverse(rev,
210 &sdev->codec_list, list) {
211 if (found_this && rev->codec->close) {
212 rev->codec->close(rev,
213 pi->substream);
214 }
215 if (rev == cii)
216 found_this = 1;
217 }
218 goto out_unlock;
219 }
220 }
221 }
222
223 out_unlock:
224 mutex_unlock(&i2sdev->lock);
225 return result;
226}
227
228#undef CHECK_RATE
229
230static int i2sbus_pcm_close(struct i2sbus_dev *i2sdev, int in)
231{
232 struct codec_info_item *cii;
233 struct pcm_info *pi;
234 int err = 0, tmp;
235
236 mutex_lock(&i2sdev->lock);
237
238 get_pcm_info(i2sdev, in, &pi, NULL);
239
240 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
241 if (cii->codec->close) {
242 tmp = cii->codec->close(cii, pi->substream);
243 if (tmp)
244 err = tmp;
245 }
246 }
247
248 pi->substream = NULL;
249 pi->active = 0;
250 mutex_unlock(&i2sdev->lock);
251 return err;
252}
253
254static void i2sbus_wait_for_stop(struct i2sbus_dev *i2sdev,
255 struct pcm_info *pi)
256{
257 unsigned long flags;
258 struct completion done;
259 long timeout;
260
261 spin_lock_irqsave(&i2sdev->low_lock, flags);
262 if (pi->dbdma_ring.stopping) {
263 init_completion(&done);
264 pi->stop_completion = &done;
265 spin_unlock_irqrestore(&i2sdev->low_lock, flags);
266 timeout = wait_for_completion_timeout(&done, HZ);
267 spin_lock_irqsave(&i2sdev->low_lock, flags);
268 pi->stop_completion = NULL;
269 if (timeout == 0) {
270
271 printk(KERN_ERR "i2sbus_wait_for_stop: timed out\n");
272
273 out_le32(&pi->dbdma->control, (RUN | PAUSE | 1) << 16);
274 pi->dbdma_ring.stopping = 0;
275 timeout = 10;
276 while (in_le32(&pi->dbdma->status) & ACTIVE) {
277 if (--timeout <= 0)
278 break;
279 udelay(1);
280 }
281 }
282 }
283 spin_unlock_irqrestore(&i2sdev->low_lock, flags);
284}
285
286#ifdef CONFIG_PM
287void i2sbus_wait_for_stop_both(struct i2sbus_dev *i2sdev)
288{
289 struct pcm_info *pi;
290
291 get_pcm_info(i2sdev, 0, &pi, NULL);
292 i2sbus_wait_for_stop(i2sdev, pi);
293 get_pcm_info(i2sdev, 1, &pi, NULL);
294 i2sbus_wait_for_stop(i2sdev, pi);
295}
296#endif
297
298static int i2sbus_hw_params(struct snd_pcm_substream *substream,
299 struct snd_pcm_hw_params *params)
300{
301 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
302}
303
304static inline int i2sbus_hw_free(struct snd_pcm_substream *substream, int in)
305{
306 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
307 struct pcm_info *pi;
308
309 get_pcm_info(i2sdev, in, &pi, NULL);
310 if (pi->dbdma_ring.stopping)
311 i2sbus_wait_for_stop(i2sdev, pi);
312 snd_pcm_lib_free_pages(substream);
313 return 0;
314}
315
316static int i2sbus_playback_hw_free(struct snd_pcm_substream *substream)
317{
318 return i2sbus_hw_free(substream, 0);
319}
320
321static int i2sbus_record_hw_free(struct snd_pcm_substream *substream)
322{
323 return i2sbus_hw_free(substream, 1);
324}
325
326static int i2sbus_pcm_prepare(struct i2sbus_dev *i2sdev, int in)
327{
328
329
330
331 struct snd_pcm_runtime *runtime;
332 struct dbdma_cmd *command;
333 int i, periodsize, nperiods;
334 dma_addr_t offset;
335 struct bus_info bi;
336 struct codec_info_item *cii;
337 int sfr = 0;
338 int dws = 0;
339 int input_16bit;
340 struct pcm_info *pi, *other;
341 int cnt;
342 int result = 0;
343 unsigned int cmd, stopaddr;
344
345 mutex_lock(&i2sdev->lock);
346
347 get_pcm_info(i2sdev, in, &pi, &other);
348
349 if (pi->dbdma_ring.running) {
350 result = -EBUSY;
351 goto out_unlock;
352 }
353 if (pi->dbdma_ring.stopping)
354 i2sbus_wait_for_stop(i2sdev, pi);
355
356 if (!pi->substream || !pi->substream->runtime) {
357 result = -EINVAL;
358 goto out_unlock;
359 }
360
361 runtime = pi->substream->runtime;
362 pi->active = 1;
363 if (other->active &&
364 ((i2sdev->format != runtime->format)
365 || (i2sdev->rate != runtime->rate))) {
366 result = -EINVAL;
367 goto out_unlock;
368 }
369
370 i2sdev->format = runtime->format;
371 i2sdev->rate = runtime->rate;
372
373 periodsize = snd_pcm_lib_period_bytes(pi->substream);
374 nperiods = pi->substream->runtime->periods;
375 pi->current_period = 0;
376
377
378 command = pi->dbdma_ring.cmds;
379 memset(command, 0, (nperiods + 2) * sizeof(struct dbdma_cmd));
380
381
382
383
384
385
386
387
388
389
390
391
392
393 offset = runtime->dma_addr;
394 cmd = (in? INPUT_MORE: OUTPUT_MORE) | BR_IFSET | INTR_ALWAYS;
395 stopaddr = pi->dbdma_ring.bus_cmd_start +
396 (nperiods + 1) * sizeof(struct dbdma_cmd);
397 for (i = 0; i < nperiods; i++, command++, offset += periodsize) {
398 command->command = cpu_to_le16(cmd);
399 command->cmd_dep = cpu_to_le32(stopaddr);
400 command->phy_addr = cpu_to_le32(offset);
401 command->req_count = cpu_to_le16(periodsize);
402 }
403
404
405 command->command = cpu_to_le16(DBDMA_NOP | BR_ALWAYS);
406 command->cmd_dep = cpu_to_le32(pi->dbdma_ring.bus_cmd_start);
407 command++;
408
409
410 command->command = cpu_to_le16(DBDMA_STOP);
411
412
413 switch (runtime->format) {
414
415 case SNDRV_PCM_FORMAT_S16_BE:
416 case SNDRV_PCM_FORMAT_U16_BE:
417
418
419 bi.bus_factor = 0;
420 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
421 bi.bus_factor = cii->codec->bus_factor;
422 break;
423 }
424 if (!bi.bus_factor) {
425 result = -ENODEV;
426 goto out_unlock;
427 }
428 input_16bit = 1;
429 break;
430 case SNDRV_PCM_FORMAT_S32_BE:
431 case SNDRV_PCM_FORMAT_U32_BE:
432
433
434 bi.bus_factor = 64;
435 input_16bit = 0;
436 break;
437 default:
438 result = -EINVAL;
439 goto out_unlock;
440 }
441
442 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
443 bi.sysclock_factor = cii->codec->sysclock_factor;
444 break;
445 }
446
447 if (clock_and_divisors(bi.sysclock_factor,
448 bi.bus_factor,
449 runtime->rate,
450 &sfr) < 0) {
451 result = -EINVAL;
452 goto out_unlock;
453 }
454 switch (bi.bus_factor) {
455 case 32:
456 sfr |= I2S_SF_SERIAL_FORMAT_I2S_32X;
457 break;
458 case 64:
459 sfr |= I2S_SF_SERIAL_FORMAT_I2S_64X;
460 break;
461 }
462
463 sfr |= I2S_SF_SCLK_MASTER;
464
465 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
466 int err = 0;
467 if (cii->codec->prepare)
468 err = cii->codec->prepare(cii, &bi, pi->substream);
469 if (err) {
470 result = err;
471 goto out_unlock;
472 }
473 }
474
475 if (input_16bit)
476 dws = (2 << I2S_DWS_NUM_CHANNELS_IN_SHIFT) |
477 (2 << I2S_DWS_NUM_CHANNELS_OUT_SHIFT) |
478 I2S_DWS_DATA_IN_16BIT | I2S_DWS_DATA_OUT_16BIT;
479 else
480 dws = (2 << I2S_DWS_NUM_CHANNELS_IN_SHIFT) |
481 (2 << I2S_DWS_NUM_CHANNELS_OUT_SHIFT) |
482 I2S_DWS_DATA_IN_24BIT | I2S_DWS_DATA_OUT_24BIT;
483
484
485
486 if (in_le32(&i2sdev->intfregs->serial_format) == sfr
487 && in_le32(&i2sdev->intfregs->data_word_sizes) == dws)
488 goto out_unlock;
489
490
491
492 list_for_each_entry(cii, &i2sdev->sound.codec_list, list)
493 if (cii->codec->switch_clock)
494 cii->codec->switch_clock(cii, CLOCK_SWITCH_PREPARE_SLAVE);
495
496 i2sbus_control_enable(i2sdev->control, i2sdev);
497 i2sbus_control_cell(i2sdev->control, i2sdev, 1);
498
499 out_le32(&i2sdev->intfregs->intr_ctl, I2S_PENDING_CLOCKS_STOPPED);
500
501 i2sbus_control_clock(i2sdev->control, i2sdev, 0);
502
503 msleep(1);
504
505
506 cnt = 100;
507 while (cnt-- &&
508 !(in_le32(&i2sdev->intfregs->intr_ctl) & I2S_PENDING_CLOCKS_STOPPED)) {
509 msleep(5);
510 }
511 out_le32(&i2sdev->intfregs->intr_ctl, I2S_PENDING_CLOCKS_STOPPED);
512
513
514 out_le32(&i2sdev->intfregs->serial_format, sfr);
515 out_le32(&i2sdev->intfregs->data_word_sizes, dws);
516
517 i2sbus_control_enable(i2sdev->control, i2sdev);
518 i2sbus_control_cell(i2sdev->control, i2sdev, 1);
519 i2sbus_control_clock(i2sdev->control, i2sdev, 1);
520 msleep(1);
521
522 list_for_each_entry(cii, &i2sdev->sound.codec_list, list)
523 if (cii->codec->switch_clock)
524 cii->codec->switch_clock(cii, CLOCK_SWITCH_SLAVE);
525
526 out_unlock:
527 mutex_unlock(&i2sdev->lock);
528 return result;
529}
530
531#ifdef CONFIG_PM
532void i2sbus_pcm_prepare_both(struct i2sbus_dev *i2sdev)
533{
534 i2sbus_pcm_prepare(i2sdev, 0);
535 i2sbus_pcm_prepare(i2sdev, 1);
536}
537#endif
538
539static int i2sbus_pcm_trigger(struct i2sbus_dev *i2sdev, int in, int cmd)
540{
541 struct codec_info_item *cii;
542 struct pcm_info *pi;
543 int result = 0;
544 unsigned long flags;
545
546 spin_lock_irqsave(&i2sdev->low_lock, flags);
547
548 get_pcm_info(i2sdev, in, &pi, NULL);
549
550 switch (cmd) {
551 case SNDRV_PCM_TRIGGER_START:
552 case SNDRV_PCM_TRIGGER_RESUME:
553 if (pi->dbdma_ring.running) {
554 result = -EALREADY;
555 goto out_unlock;
556 }
557 list_for_each_entry(cii, &i2sdev->sound.codec_list, list)
558 if (cii->codec->start)
559 cii->codec->start(cii, pi->substream);
560 pi->dbdma_ring.running = 1;
561
562 if (pi->dbdma_ring.stopping) {
563
564 out_le32(&pi->dbdma->control, 1 << 16);
565 if (in_le32(&pi->dbdma->status) & ACTIVE) {
566
567 udelay(10);
568 if (in_le32(&pi->dbdma->status) & ACTIVE) {
569 pi->dbdma_ring.stopping = 0;
570 goto out_unlock;
571 }
572 }
573 }
574
575
576 out_le32(&pi->dbdma->control, (RUN | PAUSE | 1) << 16);
577
578
579 out_le32(&pi->dbdma->br_sel, (1 << 16) | 1);
580
581
582 out_le32(&pi->dbdma->cmdptr, pi->dbdma_ring.bus_cmd_start);
583
584
585 pi->current_period = 0;
586 pi->frame_count = in_le32(&i2sdev->intfregs->frame_count);
587
588
589 out_le32(&pi->dbdma->control, (RUN << 16) | RUN);
590
591
592 break;
593
594 case SNDRV_PCM_TRIGGER_STOP:
595 case SNDRV_PCM_TRIGGER_SUSPEND:
596 if (!pi->dbdma_ring.running) {
597 result = -EALREADY;
598 goto out_unlock;
599 }
600 pi->dbdma_ring.running = 0;
601
602
603 out_le32(&pi->dbdma->control, (1 << 16) | 1);
604 pi->dbdma_ring.stopping = 1;
605
606 list_for_each_entry(cii, &i2sdev->sound.codec_list, list)
607 if (cii->codec->stop)
608 cii->codec->stop(cii, pi->substream);
609 break;
610 default:
611 result = -EINVAL;
612 goto out_unlock;
613 }
614
615 out_unlock:
616 spin_unlock_irqrestore(&i2sdev->low_lock, flags);
617 return result;
618}
619
620static snd_pcm_uframes_t i2sbus_pcm_pointer(struct i2sbus_dev *i2sdev, int in)
621{
622 struct pcm_info *pi;
623 u32 fc;
624
625 get_pcm_info(i2sdev, in, &pi, NULL);
626
627 fc = in_le32(&i2sdev->intfregs->frame_count);
628 fc = fc - pi->frame_count;
629
630 if (fc >= pi->substream->runtime->buffer_size)
631 fc %= pi->substream->runtime->buffer_size;
632 return fc;
633}
634
635static inline void handle_interrupt(struct i2sbus_dev *i2sdev, int in)
636{
637 struct pcm_info *pi;
638 u32 fc, nframes;
639 u32 status;
640 int timeout, i;
641 int dma_stopped = 0;
642 struct snd_pcm_runtime *runtime;
643
644 spin_lock(&i2sdev->low_lock);
645 get_pcm_info(i2sdev, in, &pi, NULL);
646 if (!pi->dbdma_ring.running && !pi->dbdma_ring.stopping)
647 goto out_unlock;
648
649 i = pi->current_period;
650 runtime = pi->substream->runtime;
651 while (pi->dbdma_ring.cmds[i].xfer_status) {
652 if (le16_to_cpu(pi->dbdma_ring.cmds[i].xfer_status) & BT)
653
654
655
656
657
658 dma_stopped = 1;
659 pi->dbdma_ring.cmds[i].xfer_status = 0;
660
661 if (++i >= runtime->periods) {
662 i = 0;
663 pi->frame_count += runtime->buffer_size;
664 }
665 pi->current_period = i;
666
667
668
669
670
671 fc = in_le32(&i2sdev->intfregs->frame_count);
672 nframes = i * runtime->period_size;
673 if (fc < pi->frame_count + nframes)
674 pi->frame_count = fc - nframes;
675 }
676
677 if (dma_stopped) {
678 timeout = 1000;
679 for (;;) {
680 status = in_le32(&pi->dbdma->status);
681 if (!(status & ACTIVE) && (!in || (status & 0x80)))
682 break;
683 if (--timeout <= 0) {
684 printk(KERN_ERR "i2sbus: timed out "
685 "waiting for DMA to stop!\n");
686 break;
687 }
688 udelay(1);
689 }
690
691
692 out_le32(&pi->dbdma->control, (RUN | PAUSE | 1) << 16);
693
694 pi->dbdma_ring.stopping = 0;
695 if (pi->stop_completion)
696 complete(pi->stop_completion);
697 }
698
699 if (!pi->dbdma_ring.running)
700 goto out_unlock;
701 spin_unlock(&i2sdev->low_lock);
702
703 snd_pcm_period_elapsed(pi->substream);
704 return;
705
706 out_unlock:
707 spin_unlock(&i2sdev->low_lock);
708}
709
710irqreturn_t i2sbus_tx_intr(int irq, void *devid)
711{
712 handle_interrupt((struct i2sbus_dev *)devid, 0);
713 return IRQ_HANDLED;
714}
715
716irqreturn_t i2sbus_rx_intr(int irq, void *devid)
717{
718 handle_interrupt((struct i2sbus_dev *)devid, 1);
719 return IRQ_HANDLED;
720}
721
722static int i2sbus_playback_open(struct snd_pcm_substream *substream)
723{
724 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
725
726 if (!i2sdev)
727 return -EINVAL;
728 i2sdev->out.substream = substream;
729 return i2sbus_pcm_open(i2sdev, 0);
730}
731
732static int i2sbus_playback_close(struct snd_pcm_substream *substream)
733{
734 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
735 int err;
736
737 if (!i2sdev)
738 return -EINVAL;
739 if (i2sdev->out.substream != substream)
740 return -EINVAL;
741 err = i2sbus_pcm_close(i2sdev, 0);
742 if (!err)
743 i2sdev->out.substream = NULL;
744 return err;
745}
746
747static int i2sbus_playback_prepare(struct snd_pcm_substream *substream)
748{
749 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
750
751 if (!i2sdev)
752 return -EINVAL;
753 if (i2sdev->out.substream != substream)
754 return -EINVAL;
755 return i2sbus_pcm_prepare(i2sdev, 0);
756}
757
758static int i2sbus_playback_trigger(struct snd_pcm_substream *substream, int cmd)
759{
760 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
761
762 if (!i2sdev)
763 return -EINVAL;
764 if (i2sdev->out.substream != substream)
765 return -EINVAL;
766 return i2sbus_pcm_trigger(i2sdev, 0, cmd);
767}
768
769static snd_pcm_uframes_t i2sbus_playback_pointer(struct snd_pcm_substream
770 *substream)
771{
772 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
773
774 if (!i2sdev)
775 return -EINVAL;
776 if (i2sdev->out.substream != substream)
777 return 0;
778 return i2sbus_pcm_pointer(i2sdev, 0);
779}
780
781static struct snd_pcm_ops i2sbus_playback_ops = {
782 .open = i2sbus_playback_open,
783 .close = i2sbus_playback_close,
784 .ioctl = snd_pcm_lib_ioctl,
785 .hw_params = i2sbus_hw_params,
786 .hw_free = i2sbus_playback_hw_free,
787 .prepare = i2sbus_playback_prepare,
788 .trigger = i2sbus_playback_trigger,
789 .pointer = i2sbus_playback_pointer,
790};
791
792static int i2sbus_record_open(struct snd_pcm_substream *substream)
793{
794 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
795
796 if (!i2sdev)
797 return -EINVAL;
798 i2sdev->in.substream = substream;
799 return i2sbus_pcm_open(i2sdev, 1);
800}
801
802static int i2sbus_record_close(struct snd_pcm_substream *substream)
803{
804 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
805 int err;
806
807 if (!i2sdev)
808 return -EINVAL;
809 if (i2sdev->in.substream != substream)
810 return -EINVAL;
811 err = i2sbus_pcm_close(i2sdev, 1);
812 if (!err)
813 i2sdev->in.substream = NULL;
814 return err;
815}
816
817static int i2sbus_record_prepare(struct snd_pcm_substream *substream)
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_prepare(i2sdev, 1);
826}
827
828static int i2sbus_record_trigger(struct snd_pcm_substream *substream, int cmd)
829{
830 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
831
832 if (!i2sdev)
833 return -EINVAL;
834 if (i2sdev->in.substream != substream)
835 return -EINVAL;
836 return i2sbus_pcm_trigger(i2sdev, 1, cmd);
837}
838
839static snd_pcm_uframes_t i2sbus_record_pointer(struct snd_pcm_substream
840 *substream)
841{
842 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
843
844 if (!i2sdev)
845 return -EINVAL;
846 if (i2sdev->in.substream != substream)
847 return 0;
848 return i2sbus_pcm_pointer(i2sdev, 1);
849}
850
851static struct snd_pcm_ops i2sbus_record_ops = {
852 .open = i2sbus_record_open,
853 .close = i2sbus_record_close,
854 .ioctl = snd_pcm_lib_ioctl,
855 .hw_params = i2sbus_hw_params,
856 .hw_free = i2sbus_record_hw_free,
857 .prepare = i2sbus_record_prepare,
858 .trigger = i2sbus_record_trigger,
859 .pointer = i2sbus_record_pointer,
860};
861
862static void i2sbus_private_free(struct snd_pcm *pcm)
863{
864 struct i2sbus_dev *i2sdev = snd_pcm_chip(pcm);
865 struct codec_info_item *p, *tmp;
866
867 i2sdev->sound.pcm = NULL;
868 i2sdev->out.created = 0;
869 i2sdev->in.created = 0;
870 list_for_each_entry_safe(p, tmp, &i2sdev->sound.codec_list, list) {
871 printk(KERN_ERR "i2sbus: a codec didn't unregister!\n");
872 list_del(&p->list);
873 module_put(p->codec->owner);
874 kfree(p);
875 }
876 soundbus_dev_put(&i2sdev->sound);
877 module_put(THIS_MODULE);
878}
879
880int
881i2sbus_attach_codec(struct soundbus_dev *dev, struct snd_card *card,
882 struct codec_info *ci, void *data)
883{
884 int err, in = 0, out = 0;
885 struct transfer_info *tmp;
886 struct i2sbus_dev *i2sdev = soundbus_dev_to_i2sbus_dev(dev);
887 struct codec_info_item *cii;
888
889 if (!dev->pcmname || dev->pcmid == -1) {
890 printk(KERN_ERR "i2sbus: pcm name and id must be set!\n");
891 return -EINVAL;
892 }
893
894 list_for_each_entry(cii, &dev->codec_list, list) {
895 if (cii->codec_data == data)
896 return -EALREADY;
897 }
898
899 if (!ci->transfers || !ci->transfers->formats
900 || !ci->transfers->rates || !ci->usable)
901 return -EINVAL;
902
903
904
905 if (ci->bus_factor != 32 && ci->bus_factor != 64)
906 return -EINVAL;
907
908
909
910
911 list_for_each_entry(cii, &dev->codec_list, list) {
912 if (cii->codec->sysclock_factor != ci->sysclock_factor) {
913 printk(KERN_DEBUG
914 "cannot yet handle multiple different sysclocks!\n");
915 return -EINVAL;
916 }
917 if (cii->codec->bus_factor != ci->bus_factor) {
918 printk(KERN_DEBUG
919 "cannot yet handle multiple different bus clocks!\n");
920 return -EINVAL;
921 }
922 }
923
924 tmp = ci->transfers;
925 while (tmp->formats && tmp->rates) {
926 if (tmp->transfer_in)
927 in = 1;
928 else
929 out = 1;
930 tmp++;
931 }
932
933 cii = kzalloc(sizeof(struct codec_info_item), GFP_KERNEL);
934 if (!cii) {
935 printk(KERN_DEBUG "i2sbus: failed to allocate cii\n");
936 return -ENOMEM;
937 }
938
939
940 cii->sdev = soundbus_dev_get(dev);
941 cii->codec = ci;
942 cii->codec_data = data;
943
944 if (!cii->sdev) {
945 printk(KERN_DEBUG
946 "i2sbus: failed to get soundbus dev reference\n");
947 err = -ENODEV;
948 goto out_free_cii;
949 }
950
951 if (!try_module_get(THIS_MODULE)) {
952 printk(KERN_DEBUG "i2sbus: failed to get module reference!\n");
953 err = -EBUSY;
954 goto out_put_sdev;
955 }
956
957 if (!try_module_get(ci->owner)) {
958 printk(KERN_DEBUG
959 "i2sbus: failed to get module reference to codec owner!\n");
960 err = -EBUSY;
961 goto out_put_this_module;
962 }
963
964 if (!dev->pcm) {
965 err = snd_pcm_new(card, dev->pcmname, dev->pcmid, 0, 0,
966 &dev->pcm);
967 if (err) {
968 printk(KERN_DEBUG "i2sbus: failed to create pcm\n");
969 goto out_put_ci_module;
970 }
971 }
972
973
974
975 out = in = 1;
976
977 if (!i2sdev->out.created && out) {
978 if (dev->pcm->card != card) {
979
980 printk(KERN_ERR
981 "Can't attach same bus to different cards!\n");
982 err = -EINVAL;
983 goto out_put_ci_module;
984 }
985 err = snd_pcm_new_stream(dev->pcm, SNDRV_PCM_STREAM_PLAYBACK, 1);
986 if (err)
987 goto out_put_ci_module;
988 snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_PLAYBACK,
989 &i2sbus_playback_ops);
990 dev->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].dev.parent =
991 &dev->ofdev.dev;
992 i2sdev->out.created = 1;
993 }
994
995 if (!i2sdev->in.created && in) {
996 if (dev->pcm->card != card) {
997 printk(KERN_ERR
998 "Can't attach same bus to different cards!\n");
999 err = -EINVAL;
1000 goto out_put_ci_module;
1001 }
1002 err = snd_pcm_new_stream(dev->pcm, SNDRV_PCM_STREAM_CAPTURE, 1);
1003 if (err)
1004 goto out_put_ci_module;
1005 snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_CAPTURE,
1006 &i2sbus_record_ops);
1007 dev->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].dev.parent =
1008 &dev->ofdev.dev;
1009 i2sdev->in.created = 1;
1010 }
1011
1012
1013
1014
1015
1016
1017
1018 err = snd_device_register(card, dev->pcm);
1019 if (err) {
1020 printk(KERN_ERR "i2sbus: error registering new pcm\n");
1021 goto out_put_ci_module;
1022 }
1023
1024 list_add(&cii->list, &dev->codec_list);
1025
1026 dev->pcm->private_data = i2sdev;
1027 dev->pcm->private_free = i2sbus_private_free;
1028
1029
1030 snd_pcm_lib_preallocate_pages_for_all(
1031 dev->pcm, SNDRV_DMA_TYPE_DEV,
1032 snd_dma_pci_data(macio_get_pci_dev(i2sdev->macio)),
1033 64 * 1024, 64 * 1024);
1034
1035 return 0;
1036 out_put_ci_module:
1037 module_put(ci->owner);
1038 out_put_this_module:
1039 module_put(THIS_MODULE);
1040 out_put_sdev:
1041 soundbus_dev_put(dev);
1042 out_free_cii:
1043 kfree(cii);
1044 return err;
1045}
1046
1047void i2sbus_detach_codec(struct soundbus_dev *dev, void *data)
1048{
1049 struct codec_info_item *cii = NULL, *i;
1050
1051 list_for_each_entry(i, &dev->codec_list, list) {
1052 if (i->codec_data == data) {
1053 cii = i;
1054 break;
1055 }
1056 }
1057 if (cii) {
1058 list_del(&cii->list);
1059 module_put(cii->codec->owner);
1060 kfree(cii);
1061 }
1062
1063 if (list_empty(&dev->codec_list) && dev->pcm) {
1064
1065 snd_device_free(dev->pcm->card, dev->pcm);
1066 }
1067}
1068