[ALSA] add number of periods constraint to snd-aoa
[pandora-kernel.git] / sound / aoa / soundbus / i2sbus / i2sbus-pcm.c
1 /*
2  * i2sbus driver -- pcm routines
3  *
4  * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
5  *
6  * GPL v2, can be found in COPYING.
7  */
8
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"
19
20 static inline void get_pcm_info(struct i2sbus_dev *i2sdev, int in,
21                                 struct pcm_info **pi, struct pcm_info **other)
22 {
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;
33         }
34 }
35
36 static int clock_and_divisors(int mclk, int sclk, int rate, int *out)
37 {
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;
44
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;
49                 }
50         }
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;
55                 }
56         }
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;
61                 }
62         }
63         return -1;
64 }
65
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)
73
74 static int i2sbus_pcm_open(struct i2sbus_dev *i2sdev, int in)
75 {
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;
87
88         mutex_lock(&i2sdev->lock);
89
90         get_pcm_info(i2sdev, in, &pi, &other);
91
92         hw = &pi->substream->runtime->hw;
93         sdev = &i2sdev->sound;
94
95         if (pi->active) {
96                 /* alsa messed up */
97                 result = -EBUSY;
98                 goto out_unlock;
99         }
100
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;
117                                 }
118                         }
119                         ti++;
120                 }
121         }
122         if (!masks_inited || !bus_factor || !sysclock_factor) {
123                 result = -ENODEV;
124                 goto out_unlock;
125         }
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;
130
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;
145
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);
166
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.
180          */
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;
187         }
188
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         err = snd_pcm_hw_constraint_integer(pi->substream->runtime,
198                                             SNDRV_PCM_HW_PARAM_PERIODS);
199         if (err < 0) {
200                 result = err;
201                 goto out_unlock;
202         }
203         list_for_each_entry(cii, &sdev->codec_list, list) {
204                 if (cii->codec->open) {
205                         err = cii->codec->open(cii, pi->substream);
206                         if (err) {
207                                 result = err;
208                                 /* unwind */
209                                 found_this = 0;
210                                 list_for_each_entry_reverse(rev,
211                                     &sdev->codec_list, list) {
212                                         if (found_this && rev->codec->close) {
213                                                 rev->codec->close(rev,
214                                                                 pi->substream);
215                                         }
216                                         if (rev == cii)
217                                                 found_this = 1;
218                                 }
219                                 goto out_unlock;
220                         }
221                 }
222         }
223
224  out_unlock:
225         mutex_unlock(&i2sdev->lock);
226         return result;
227 }
228
229 #undef CHECK_RATE
230
231 static int i2sbus_pcm_close(struct i2sbus_dev *i2sdev, int in)
232 {
233         struct codec_info_item *cii;
234         struct pcm_info *pi;
235         int err = 0, tmp;
236
237         mutex_lock(&i2sdev->lock);
238
239         get_pcm_info(i2sdev, in, &pi, NULL);
240
241         list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
242                 if (cii->codec->close) {
243                         tmp = cii->codec->close(cii, pi->substream);
244                         if (tmp)
245                                 err = tmp;
246                 }
247         }
248
249         pi->substream = NULL;
250         pi->active = 0;
251         mutex_unlock(&i2sdev->lock);
252         return err;
253 }
254
255 static void i2sbus_wait_for_stop(struct i2sbus_dev *i2sdev,
256                                  struct pcm_info *pi)
257 {
258         unsigned long flags;
259         struct completion done;
260         long timeout;
261
262         spin_lock_irqsave(&i2sdev->low_lock, flags);
263         if (pi->dbdma_ring.stopping) {
264                 init_completion(&done);
265                 pi->stop_completion = &done;
266                 spin_unlock_irqrestore(&i2sdev->low_lock, flags);
267                 timeout = wait_for_completion_timeout(&done, HZ);
268                 spin_lock_irqsave(&i2sdev->low_lock, flags);
269                 pi->stop_completion = NULL;
270                 if (timeout == 0) {
271                         /* timeout expired, stop dbdma forcefully */
272                         printk(KERN_ERR "i2sbus_wait_for_stop: timed out\n");
273                         /* make sure RUN, PAUSE and S0 bits are cleared */
274                         out_le32(&pi->dbdma->control, (RUN | PAUSE | 1) << 16);
275                         pi->dbdma_ring.stopping = 0;
276                         timeout = 10;
277                         while (in_le32(&pi->dbdma->status) & ACTIVE) {
278                                 if (--timeout <= 0)
279                                         break;
280                                 udelay(1);
281                         }
282                 }
283         }
284         spin_unlock_irqrestore(&i2sdev->low_lock, flags);
285 }
286
287 #ifdef CONFIG_PM
288 void i2sbus_wait_for_stop_both(struct i2sbus_dev *i2sdev)
289 {
290         struct pcm_info *pi;
291
292         get_pcm_info(i2sdev, 0, &pi, NULL);
293         i2sbus_wait_for_stop(i2sdev, pi);
294         get_pcm_info(i2sdev, 1, &pi, NULL);
295         i2sbus_wait_for_stop(i2sdev, pi);
296 }
297 #endif
298
299 static int i2sbus_hw_params(struct snd_pcm_substream *substream,
300                             struct snd_pcm_hw_params *params)
301 {
302         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
303 }
304
305 static inline int i2sbus_hw_free(struct snd_pcm_substream *substream, int in)
306 {
307         struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
308         struct pcm_info *pi;
309
310         get_pcm_info(i2sdev, in, &pi, NULL);
311         if (pi->dbdma_ring.stopping)
312                 i2sbus_wait_for_stop(i2sdev, pi);
313         snd_pcm_lib_free_pages(substream);
314         return 0;
315 }
316
317 static int i2sbus_playback_hw_free(struct snd_pcm_substream *substream)
318 {
319         return i2sbus_hw_free(substream, 0);
320 }
321
322 static int i2sbus_record_hw_free(struct snd_pcm_substream *substream)
323 {
324         return i2sbus_hw_free(substream, 1);
325 }
326
327 static int i2sbus_pcm_prepare(struct i2sbus_dev *i2sdev, int in)
328 {
329         /* whee. Hard work now. The user has selected a bitrate
330          * and bit format, so now we have to program our
331          * I2S controller appropriately. */
332         struct snd_pcm_runtime *runtime;
333         struct dbdma_cmd *command;
334         int i, periodsize, nperiods;
335         dma_addr_t offset;
336         struct bus_info bi;
337         struct codec_info_item *cii;
338         int sfr = 0;            /* serial format register */
339         int dws = 0;            /* data word sizes reg */
340         int input_16bit;
341         struct pcm_info *pi, *other;
342         int cnt;
343         int result = 0;
344         unsigned int cmd, stopaddr;
345
346         mutex_lock(&i2sdev->lock);
347
348         get_pcm_info(i2sdev, in, &pi, &other);
349
350         if (pi->dbdma_ring.running) {
351                 result = -EBUSY;
352                 goto out_unlock;
353         }
354         if (pi->dbdma_ring.stopping)
355                 i2sbus_wait_for_stop(i2sdev, pi);
356
357         if (!pi->substream || !pi->substream->runtime) {
358                 result = -EINVAL;
359                 goto out_unlock;
360         }
361
362         runtime = pi->substream->runtime;
363         pi->active = 1;
364         if (other->active &&
365             ((i2sdev->format != runtime->format)
366              || (i2sdev->rate != runtime->rate))) {
367                 result = -EINVAL;
368                 goto out_unlock;
369         }
370
371         i2sdev->format = runtime->format;
372         i2sdev->rate = runtime->rate;
373
374         periodsize = snd_pcm_lib_period_bytes(pi->substream);
375         nperiods = pi->substream->runtime->periods;
376         pi->current_period = 0;
377
378         /* generate dbdma command ring first */
379         command = pi->dbdma_ring.cmds;
380         memset(command, 0, (nperiods + 2) * sizeof(struct dbdma_cmd));
381
382         /* commands to DMA to/from the ring */
383         /*
384          * For input, we need to do a graceful stop; if we abort
385          * the DMA, we end up with leftover bytes that corrupt
386          * the next recording.  To do this we set the S0 status
387          * bit and wait for the DMA controller to stop.  Each
388          * command has a branch condition to
389          * make it branch to a stop command if S0 is set.
390          * On input we also need to wait for the S7 bit to be
391          * set before turning off the DMA controller.
392          * In fact we do the graceful stop for output as well.
393          */
394         offset = runtime->dma_addr;
395         cmd = (in? INPUT_MORE: OUTPUT_MORE) | BR_IFSET | INTR_ALWAYS;
396         stopaddr = pi->dbdma_ring.bus_cmd_start +
397                 (nperiods + 1) * sizeof(struct dbdma_cmd);
398         for (i = 0; i < nperiods; i++, command++, offset += periodsize) {
399                 command->command = cpu_to_le16(cmd);
400                 command->cmd_dep = cpu_to_le32(stopaddr);
401                 command->phy_addr = cpu_to_le32(offset);
402                 command->req_count = cpu_to_le16(periodsize);
403         }
404
405         /* branch back to beginning of ring */
406         command->command = cpu_to_le16(DBDMA_NOP | BR_ALWAYS);
407         command->cmd_dep = cpu_to_le32(pi->dbdma_ring.bus_cmd_start);
408         command++;
409
410         /* set stop command */
411         command->command = cpu_to_le16(DBDMA_STOP);
412
413         /* ok, let's set the serial format and stuff */
414         switch (runtime->format) {
415         /* 16 bit formats */
416         case SNDRV_PCM_FORMAT_S16_BE:
417         case SNDRV_PCM_FORMAT_U16_BE:
418                 /* FIXME: if we add different bus factors we need to
419                  * do more here!! */
420                 bi.bus_factor = 0;
421                 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
422                         bi.bus_factor = cii->codec->bus_factor;
423                         break;
424                 }
425                 if (!bi.bus_factor) {
426                         result = -ENODEV;
427                         goto out_unlock;
428                 }
429                 input_16bit = 1;
430                 break;
431         case SNDRV_PCM_FORMAT_S32_BE:
432         case SNDRV_PCM_FORMAT_U32_BE:
433                 /* force 64x bus speed, otherwise the data cannot be
434                  * transferred quickly enough! */
435                 bi.bus_factor = 64;
436                 input_16bit = 0;
437                 break;
438         default:
439                 result = -EINVAL;
440                 goto out_unlock;
441         }
442         /* we assume all sysclocks are the same! */
443         list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
444                 bi.sysclock_factor = cii->codec->sysclock_factor;
445                 break;
446         }
447
448         if (clock_and_divisors(bi.sysclock_factor,
449                                bi.bus_factor,
450                                runtime->rate,
451                                &sfr) < 0) {
452                 result = -EINVAL;
453                 goto out_unlock;
454         }
455         switch (bi.bus_factor) {
456         case 32:
457                 sfr |= I2S_SF_SERIAL_FORMAT_I2S_32X;
458                 break;
459         case 64:
460                 sfr |= I2S_SF_SERIAL_FORMAT_I2S_64X;
461                 break;
462         }
463         /* FIXME: THIS ASSUMES MASTER ALL THE TIME */
464         sfr |= I2S_SF_SCLK_MASTER;
465
466         list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
467                 int err = 0;
468                 if (cii->codec->prepare)
469                         err = cii->codec->prepare(cii, &bi, pi->substream);
470                 if (err) {
471                         result = err;
472                         goto out_unlock;
473                 }
474         }
475         /* codecs are fine with it, so set our clocks */
476         if (input_16bit)
477                 dws =   (2 << I2S_DWS_NUM_CHANNELS_IN_SHIFT) |
478                         (2 << I2S_DWS_NUM_CHANNELS_OUT_SHIFT) |
479                         I2S_DWS_DATA_IN_16BIT | I2S_DWS_DATA_OUT_16BIT;
480         else
481                 dws =   (2 << I2S_DWS_NUM_CHANNELS_IN_SHIFT) |
482                         (2 << I2S_DWS_NUM_CHANNELS_OUT_SHIFT) |
483                         I2S_DWS_DATA_IN_24BIT | I2S_DWS_DATA_OUT_24BIT;
484
485         /* early exit if already programmed correctly */
486         /* not locking these is fine since we touch them only in this function */
487         if (in_le32(&i2sdev->intfregs->serial_format) == sfr
488          && in_le32(&i2sdev->intfregs->data_word_sizes) == dws)
489                 goto out_unlock;
490
491         /* let's notify the codecs about clocks going away.
492          * For now we only do mastering on the i2s cell... */
493         list_for_each_entry(cii, &i2sdev->sound.codec_list, list)
494                 if (cii->codec->switch_clock)
495                         cii->codec->switch_clock(cii, CLOCK_SWITCH_PREPARE_SLAVE);
496
497         i2sbus_control_enable(i2sdev->control, i2sdev);
498         i2sbus_control_cell(i2sdev->control, i2sdev, 1);
499
500         out_le32(&i2sdev->intfregs->intr_ctl, I2S_PENDING_CLOCKS_STOPPED);
501
502         i2sbus_control_clock(i2sdev->control, i2sdev, 0);
503
504         msleep(1);
505
506         /* wait for clock stopped. This can apparently take a while... */
507         cnt = 100;
508         while (cnt-- &&
509             !(in_le32(&i2sdev->intfregs->intr_ctl) & I2S_PENDING_CLOCKS_STOPPED)) {
510                 msleep(5);
511         }
512         out_le32(&i2sdev->intfregs->intr_ctl, I2S_PENDING_CLOCKS_STOPPED);
513
514         /* not locking these is fine since we touch them only in this function */
515         out_le32(&i2sdev->intfregs->serial_format, sfr);
516         out_le32(&i2sdev->intfregs->data_word_sizes, dws);
517
518         i2sbus_control_enable(i2sdev->control, i2sdev);
519         i2sbus_control_cell(i2sdev->control, i2sdev, 1);
520         i2sbus_control_clock(i2sdev->control, i2sdev, 1);
521         msleep(1);
522
523         list_for_each_entry(cii, &i2sdev->sound.codec_list, list)
524                 if (cii->codec->switch_clock)
525                         cii->codec->switch_clock(cii, CLOCK_SWITCH_SLAVE);
526
527  out_unlock:
528         mutex_unlock(&i2sdev->lock);
529         return result;
530 }
531
532 #ifdef CONFIG_PM
533 void i2sbus_pcm_prepare_both(struct i2sbus_dev *i2sdev)
534 {
535         i2sbus_pcm_prepare(i2sdev, 0);
536         i2sbus_pcm_prepare(i2sdev, 1);
537 }
538 #endif
539
540 static int i2sbus_pcm_trigger(struct i2sbus_dev *i2sdev, int in, int cmd)
541 {
542         struct codec_info_item *cii;
543         struct pcm_info *pi;
544         int result = 0;
545         unsigned long flags;
546
547         spin_lock_irqsave(&i2sdev->low_lock, flags);
548
549         get_pcm_info(i2sdev, in, &pi, NULL);
550
551         switch (cmd) {
552         case SNDRV_PCM_TRIGGER_START:
553         case SNDRV_PCM_TRIGGER_RESUME:
554                 if (pi->dbdma_ring.running) {
555                         result = -EALREADY;
556                         goto out_unlock;
557                 }
558                 list_for_each_entry(cii, &i2sdev->sound.codec_list, list)
559                         if (cii->codec->start)
560                                 cii->codec->start(cii, pi->substream);
561                 pi->dbdma_ring.running = 1;
562
563                 if (pi->dbdma_ring.stopping) {
564                         /* Clear the S0 bit, then see if we stopped yet */
565                         out_le32(&pi->dbdma->control, 1 << 16);
566                         if (in_le32(&pi->dbdma->status) & ACTIVE) {
567                                 /* possible race here? */
568                                 udelay(10);
569                                 if (in_le32(&pi->dbdma->status) & ACTIVE) {
570                                         pi->dbdma_ring.stopping = 0;
571                                         goto out_unlock; /* keep running */
572                                 }
573                         }
574                 }
575
576                 /* make sure RUN, PAUSE and S0 bits are cleared */
577                 out_le32(&pi->dbdma->control, (RUN | PAUSE | 1) << 16);
578
579                 /* set branch condition select register */
580                 out_le32(&pi->dbdma->br_sel, (1 << 16) | 1);
581
582                 /* write dma command buffer address to the dbdma chip */
583                 out_le32(&pi->dbdma->cmdptr, pi->dbdma_ring.bus_cmd_start);
584
585                 /* initialize the frame count and current period */
586                 pi->current_period = 0;
587                 pi->frame_count = in_le32(&i2sdev->intfregs->frame_count);
588
589                 /* set the DMA controller running */
590                 out_le32(&pi->dbdma->control, (RUN << 16) | RUN);
591
592                 /* off you go! */
593                 break;
594
595         case SNDRV_PCM_TRIGGER_STOP:
596         case SNDRV_PCM_TRIGGER_SUSPEND:
597                 if (!pi->dbdma_ring.running) {
598                         result = -EALREADY;
599                         goto out_unlock;
600                 }
601                 pi->dbdma_ring.running = 0;
602
603                 /* Set the S0 bit to make the DMA branch to the stop cmd */
604                 out_le32(&pi->dbdma->control, (1 << 16) | 1);
605                 pi->dbdma_ring.stopping = 1;
606
607                 list_for_each_entry(cii, &i2sdev->sound.codec_list, list)
608                         if (cii->codec->stop)
609                                 cii->codec->stop(cii, pi->substream);
610                 break;
611         default:
612                 result = -EINVAL;
613                 goto out_unlock;
614         }
615
616  out_unlock:
617         spin_unlock_irqrestore(&i2sdev->low_lock, flags);
618         return result;
619 }
620
621 static snd_pcm_uframes_t i2sbus_pcm_pointer(struct i2sbus_dev *i2sdev, int in)
622 {
623         struct pcm_info *pi;
624         u32 fc;
625
626         get_pcm_info(i2sdev, in, &pi, NULL);
627
628         fc = in_le32(&i2sdev->intfregs->frame_count);
629         fc = fc - pi->frame_count;
630
631         if (fc >= pi->substream->runtime->buffer_size)
632                 fc %= pi->substream->runtime->buffer_size;
633         return fc;
634 }
635
636 static inline void handle_interrupt(struct i2sbus_dev *i2sdev, int in)
637 {
638         struct pcm_info *pi;
639         u32 fc, nframes;
640         u32 status;
641         int timeout, i;
642         int dma_stopped = 0;
643         struct snd_pcm_runtime *runtime;
644
645         spin_lock(&i2sdev->low_lock);
646         get_pcm_info(i2sdev, in, &pi, NULL);
647         if (!pi->dbdma_ring.running && !pi->dbdma_ring.stopping)
648                 goto out_unlock;
649
650         i = pi->current_period;
651         runtime = pi->substream->runtime;
652         while (pi->dbdma_ring.cmds[i].xfer_status) {
653                 if (le16_to_cpu(pi->dbdma_ring.cmds[i].xfer_status) & BT)
654                         /*
655                          * BT is the branch taken bit.  If it took a branch
656                          * it is because we set the S0 bit to make it
657                          * branch to the stop command.
658                          */
659                         dma_stopped = 1;
660                 pi->dbdma_ring.cmds[i].xfer_status = 0;
661
662                 if (++i >= runtime->periods) {
663                         i = 0;
664                         pi->frame_count += runtime->buffer_size;
665                 }
666                 pi->current_period = i;
667
668                 /*
669                  * Check the frame count.  The DMA tends to get a bit
670                  * ahead of the frame counter, which confuses the core.
671                  */
672                 fc = in_le32(&i2sdev->intfregs->frame_count);
673                 nframes = i * runtime->period_size;
674                 if (fc < pi->frame_count + nframes)
675                         pi->frame_count = fc - nframes;
676         }
677
678         if (dma_stopped) {
679                 timeout = 1000;
680                 for (;;) {
681                         status = in_le32(&pi->dbdma->status);
682                         if (!(status & ACTIVE) && (!in || (status & 0x80)))
683                                 break;
684                         if (--timeout <= 0) {
685                                 printk(KERN_ERR "i2sbus: timed out "
686                                        "waiting for DMA to stop!\n");
687                                 break;
688                         }
689                         udelay(1);
690                 }
691
692                 /* Turn off DMA controller, clear S0 bit */
693                 out_le32(&pi->dbdma->control, (RUN | PAUSE | 1) << 16);
694
695                 pi->dbdma_ring.stopping = 0;
696                 if (pi->stop_completion)
697                         complete(pi->stop_completion);
698         }
699
700         if (!pi->dbdma_ring.running)
701                 goto out_unlock;
702         spin_unlock(&i2sdev->low_lock);
703         /* may call _trigger again, hence needs to be unlocked */
704         snd_pcm_period_elapsed(pi->substream);
705         return;
706
707  out_unlock:
708         spin_unlock(&i2sdev->low_lock);
709 }
710
711 irqreturn_t i2sbus_tx_intr(int irq, void *devid)
712 {
713         handle_interrupt((struct i2sbus_dev *)devid, 0);
714         return IRQ_HANDLED;
715 }
716
717 irqreturn_t i2sbus_rx_intr(int irq, void *devid)
718 {
719         handle_interrupt((struct i2sbus_dev *)devid, 1);
720         return IRQ_HANDLED;
721 }
722
723 static int i2sbus_playback_open(struct snd_pcm_substream *substream)
724 {
725         struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
726
727         if (!i2sdev)
728                 return -EINVAL;
729         i2sdev->out.substream = substream;
730         return i2sbus_pcm_open(i2sdev, 0);
731 }
732
733 static int i2sbus_playback_close(struct snd_pcm_substream *substream)
734 {
735         struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
736         int err;
737
738         if (!i2sdev)
739                 return -EINVAL;
740         if (i2sdev->out.substream != substream)
741                 return -EINVAL;
742         err = i2sbus_pcm_close(i2sdev, 0);
743         if (!err)
744                 i2sdev->out.substream = NULL;
745         return err;
746 }
747
748 static int i2sbus_playback_prepare(struct snd_pcm_substream *substream)
749 {
750         struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
751
752         if (!i2sdev)
753                 return -EINVAL;
754         if (i2sdev->out.substream != substream)
755                 return -EINVAL;
756         return i2sbus_pcm_prepare(i2sdev, 0);
757 }
758
759 static int i2sbus_playback_trigger(struct snd_pcm_substream *substream, int cmd)
760 {
761         struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
762
763         if (!i2sdev)
764                 return -EINVAL;
765         if (i2sdev->out.substream != substream)
766                 return -EINVAL;
767         return i2sbus_pcm_trigger(i2sdev, 0, cmd);
768 }
769
770 static snd_pcm_uframes_t i2sbus_playback_pointer(struct snd_pcm_substream
771                                                  *substream)
772 {
773         struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
774
775         if (!i2sdev)
776                 return -EINVAL;
777         if (i2sdev->out.substream != substream)
778                 return 0;
779         return i2sbus_pcm_pointer(i2sdev, 0);
780 }
781
782 static struct snd_pcm_ops i2sbus_playback_ops = {
783         .open =         i2sbus_playback_open,
784         .close =        i2sbus_playback_close,
785         .ioctl =        snd_pcm_lib_ioctl,
786         .hw_params =    i2sbus_hw_params,
787         .hw_free =      i2sbus_playback_hw_free,
788         .prepare =      i2sbus_playback_prepare,
789         .trigger =      i2sbus_playback_trigger,
790         .pointer =      i2sbus_playback_pointer,
791 };
792
793 static int i2sbus_record_open(struct snd_pcm_substream *substream)
794 {
795         struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
796
797         if (!i2sdev)
798                 return -EINVAL;
799         i2sdev->in.substream = substream;
800         return i2sbus_pcm_open(i2sdev, 1);
801 }
802
803 static int i2sbus_record_close(struct snd_pcm_substream *substream)
804 {
805         struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
806         int err;
807
808         if (!i2sdev)
809                 return -EINVAL;
810         if (i2sdev->in.substream != substream)
811                 return -EINVAL;
812         err = i2sbus_pcm_close(i2sdev, 1);
813         if (!err)
814                 i2sdev->in.substream = NULL;
815         return err;
816 }
817
818 static int i2sbus_record_prepare(struct snd_pcm_substream *substream)
819 {
820         struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
821
822         if (!i2sdev)
823                 return -EINVAL;
824         if (i2sdev->in.substream != substream)
825                 return -EINVAL;
826         return i2sbus_pcm_prepare(i2sdev, 1);
827 }
828
829 static int i2sbus_record_trigger(struct snd_pcm_substream *substream, int cmd)
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 -EINVAL;
837         return i2sbus_pcm_trigger(i2sdev, 1, cmd);
838 }
839
840 static snd_pcm_uframes_t i2sbus_record_pointer(struct snd_pcm_substream
841                                                *substream)
842 {
843         struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
844
845         if (!i2sdev)
846                 return -EINVAL;
847         if (i2sdev->in.substream != substream)
848                 return 0;
849         return i2sbus_pcm_pointer(i2sdev, 1);
850 }
851
852 static struct snd_pcm_ops i2sbus_record_ops = {
853         .open =         i2sbus_record_open,
854         .close =        i2sbus_record_close,
855         .ioctl =        snd_pcm_lib_ioctl,
856         .hw_params =    i2sbus_hw_params,
857         .hw_free =      i2sbus_record_hw_free,
858         .prepare =      i2sbus_record_prepare,
859         .trigger =      i2sbus_record_trigger,
860         .pointer =      i2sbus_record_pointer,
861 };
862
863 static void i2sbus_private_free(struct snd_pcm *pcm)
864 {
865         struct i2sbus_dev *i2sdev = snd_pcm_chip(pcm);
866         struct codec_info_item *p, *tmp;
867
868         i2sdev->sound.pcm = NULL;
869         i2sdev->out.created = 0;
870         i2sdev->in.created = 0;
871         list_for_each_entry_safe(p, tmp, &i2sdev->sound.codec_list, list) {
872                 printk(KERN_ERR "i2sbus: a codec didn't unregister!\n");
873                 list_del(&p->list);
874                 module_put(p->codec->owner);
875                 kfree(p);
876         }
877         soundbus_dev_put(&i2sdev->sound);
878         module_put(THIS_MODULE);
879 }
880
881 int
882 i2sbus_attach_codec(struct soundbus_dev *dev, struct snd_card *card,
883                     struct codec_info *ci, void *data)
884 {
885         int err, in = 0, out = 0;
886         struct transfer_info *tmp;
887         struct i2sbus_dev *i2sdev = soundbus_dev_to_i2sbus_dev(dev);
888         struct codec_info_item *cii;
889
890         if (!dev->pcmname || dev->pcmid == -1) {
891                 printk(KERN_ERR "i2sbus: pcm name and id must be set!\n");
892                 return -EINVAL;
893         }
894
895         list_for_each_entry(cii, &dev->codec_list, list) {
896                 if (cii->codec_data == data)
897                         return -EALREADY;
898         }
899
900         if (!ci->transfers || !ci->transfers->formats
901             || !ci->transfers->rates || !ci->usable)
902                 return -EINVAL;
903
904         /* we currently code the i2s transfer on the clock, and support only
905          * 32 and 64 */
906         if (ci->bus_factor != 32 && ci->bus_factor != 64)
907                 return -EINVAL;
908
909         /* If you want to fix this, you need to keep track of what transport infos
910          * are to be used, which codecs they belong to, and then fix all the
911          * sysclock/busclock stuff above to depend on which is usable */
912         list_for_each_entry(cii, &dev->codec_list, list) {
913                 if (cii->codec->sysclock_factor != ci->sysclock_factor) {
914                         printk(KERN_DEBUG
915                                "cannot yet handle multiple different sysclocks!\n");
916                         return -EINVAL;
917                 }
918                 if (cii->codec->bus_factor != ci->bus_factor) {
919                         printk(KERN_DEBUG
920                                "cannot yet handle multiple different bus clocks!\n");
921                         return -EINVAL;
922                 }
923         }
924
925         tmp = ci->transfers;
926         while (tmp->formats && tmp->rates) {
927                 if (tmp->transfer_in)
928                         in = 1;
929                 else
930                         out = 1;
931                 tmp++;
932         }
933
934         cii = kzalloc(sizeof(struct codec_info_item), GFP_KERNEL);
935         if (!cii) {
936                 printk(KERN_DEBUG "i2sbus: failed to allocate cii\n");
937                 return -ENOMEM;
938         }
939
940         /* use the private data to point to the codec info */
941         cii->sdev = soundbus_dev_get(dev);
942         cii->codec = ci;
943         cii->codec_data = data;
944
945         if (!cii->sdev) {
946                 printk(KERN_DEBUG
947                        "i2sbus: failed to get soundbus dev reference\n");
948                 err = -ENODEV;
949                 goto out_free_cii;
950         }
951
952         if (!try_module_get(THIS_MODULE)) {
953                 printk(KERN_DEBUG "i2sbus: failed to get module reference!\n");
954                 err = -EBUSY;
955                 goto out_put_sdev;
956         }
957
958         if (!try_module_get(ci->owner)) {
959                 printk(KERN_DEBUG
960                        "i2sbus: failed to get module reference to codec owner!\n");
961                 err = -EBUSY;
962                 goto out_put_this_module;
963         }
964
965         if (!dev->pcm) {
966                 err = snd_pcm_new(card, dev->pcmname, dev->pcmid, 0, 0,
967                                   &dev->pcm);
968                 if (err) {
969                         printk(KERN_DEBUG "i2sbus: failed to create pcm\n");
970                         goto out_put_ci_module;
971                 }
972                 dev->pcm->dev = &dev->ofdev.dev;
973         }
974
975         /* ALSA yet again sucks.
976          * If it is ever fixed, remove this line. See below. */
977         out = in = 1;
978
979         if (!i2sdev->out.created && out) {
980                 if (dev->pcm->card != card) {
981                         /* eh? */
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_PLAYBACK, 1);
988                 if (err)
989                         goto out_put_ci_module;
990                 snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_PLAYBACK,
991                                 &i2sbus_playback_ops);
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                         goto out_put_ci_module;
1000                 }
1001                 err = snd_pcm_new_stream(dev->pcm, SNDRV_PCM_STREAM_CAPTURE, 1);
1002                 if (err)
1003                         goto out_put_ci_module;
1004                 snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_CAPTURE,
1005                                 &i2sbus_record_ops);
1006                 i2sdev->in.created = 1;
1007         }
1008
1009         /* so we have to register the pcm after adding any substream
1010          * to it because alsa doesn't create the devices for the
1011          * substreams when we add them later.
1012          * Therefore, force in and out on both busses (above) and
1013          * register the pcm now instead of just after creating it.
1014          */
1015         err = snd_device_register(card, dev->pcm);
1016         if (err) {
1017                 printk(KERN_ERR "i2sbus: error registering new pcm\n");
1018                 goto out_put_ci_module;
1019         }
1020         /* no errors any more, so let's add this to our list */
1021         list_add(&cii->list, &dev->codec_list);
1022
1023         dev->pcm->private_data = i2sdev;
1024         dev->pcm->private_free = i2sbus_private_free;
1025
1026         /* well, we really should support scatter/gather DMA */
1027         snd_pcm_lib_preallocate_pages_for_all(
1028                 dev->pcm, SNDRV_DMA_TYPE_DEV,
1029                 snd_dma_pci_data(macio_get_pci_dev(i2sdev->macio)),
1030                 64 * 1024, 64 * 1024);
1031
1032         return 0;
1033  out_put_ci_module:
1034         module_put(ci->owner);
1035  out_put_this_module:
1036         module_put(THIS_MODULE);
1037  out_put_sdev:
1038         soundbus_dev_put(dev);
1039  out_free_cii:
1040         kfree(cii);
1041         return err;
1042 }
1043
1044 void i2sbus_detach_codec(struct soundbus_dev *dev, void *data)
1045 {
1046         struct codec_info_item *cii = NULL, *i;
1047
1048         list_for_each_entry(i, &dev->codec_list, list) {
1049                 if (i->codec_data == data) {
1050                         cii = i;
1051                         break;
1052                 }
1053         }
1054         if (cii) {
1055                 list_del(&cii->list);
1056                 module_put(cii->codec->owner);
1057                 kfree(cii);
1058         }
1059         /* no more codecs, but still a pcm? */
1060         if (list_empty(&dev->codec_list) && dev->pcm) {
1061                 /* the actual cleanup is done by the callback above! */
1062                 snd_device_free(dev->pcm->card, dev->pcm);
1063         }
1064 }