1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * i2sbus driver -- pcm routines
4 *
5 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
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
get_pcm_info(struct i2sbus_dev * i2sdev,int in,struct pcm_info ** pi,struct pcm_info ** other)18 static 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
clock_and_divisors(int mclk,int sclk,int rate,int * out)34 static int clock_and_divisors(int mclk, int sclk, int rate, int *out)
35 {
36 /* sclk must be derived from mclk! */
37 if (mclk % sclk)
38 return -1;
39 /* derive sclk register value */
40 if (i2s_sf_sclkdiv(mclk / sclk, out))
41 return -1;
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
i2sbus_pcm_open(struct i2sbus_dev * i2sdev,int in)72 static 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 /* alsa messed up */
95 result = -EBUSY;
96 goto out_unlock;
97 }
98
99 /* we now need to assign the hw */
100 list_for_each_entry(cii, &sdev->codec_list, list) {
101 struct transfer_info *ti = cii->codec->transfers;
102 bus_factor = cii->codec->bus_factor;
103 sysclock_factor = cii->codec->sysclock_factor;
104 while (ti->formats && ti->rates) {
105 v = *ti;
106 if (ti->transfer_in == in
107 && cii->codec->usable(cii, ti, &v)) {
108 if (masks_inited) {
109 formats &= v.formats;
110 rates &= v.rates;
111 } else {
112 formats = v.formats;
113 rates = v.rates;
114 masks_inited = 1;
115 }
116 }
117 ti++;
118 }
119 }
120 if (!masks_inited || !bus_factor || !sysclock_factor) {
121 result = -ENODEV;
122 goto out_unlock;
123 }
124 /* bus dependent stuff */
125 hw->info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
126 SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_RESUME |
127 SNDRV_PCM_INFO_JOINT_DUPLEX;
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 /* well. the codec might want 24 bits only, and we'll
145 * ever only transfer 24 bits, but they are top-aligned!
146 * So for alsa, we claim that we're doing full 32 bit
147 * while in reality we'll ignore the lower 8 bits of
148 * that when doing playback (they're transferred as 0
149 * as far as I know, no codecs we have are 32-bit capable
150 * so I can't really test) and when doing recording we'll
151 * always have those lower 8 bits recorded as 0 */
152 if (formats & SNDRV_PCM_FMTBIT_S24_BE)
153 formats |= SNDRV_PCM_FMTBIT_S32_BE;
154 if (formats & SNDRV_PCM_FMTBIT_U24_BE)
155 formats |= SNDRV_PCM_FMTBIT_U32_BE;
156 /* now mask off what we can support. I suppose we could
157 * also support S24_3LE and some similar formats, but I
158 * doubt there's a codec that would be able to use that,
159 * so we don't support it here. */
160 hw->formats = formats & (SNDRV_PCM_FMTBIT_S16_BE |
161 SNDRV_PCM_FMTBIT_U16_BE |
162 SNDRV_PCM_FMTBIT_S32_BE |
163 SNDRV_PCM_FMTBIT_U32_BE);
164
165 /* we need to set the highest and lowest rate possible.
166 * These are the highest and lowest rates alsa can
167 * support properly in its bitfield.
168 * Below, we'll use that to restrict to the rate
169 * currently in use (if any). */
170 hw->rate_min = 5512;
171 hw->rate_max = 192000;
172 /* if the other stream is active, then we can only
173 * support what it is currently using.
174 * FIXME: I lied. This comment is wrong. We can support
175 * anything that works with the same serial format, ie.
176 * when recording 24 bit sound we can well play 16 bit
177 * sound at the same time iff using the same transfer mode.
178 */
179 if (other->active) {
180 /* FIXME: is this guaranteed by the alsa api? */
181 hw->formats &= pcm_format_to_bits(i2sdev->format);
182 /* see above, restrict rates to the one we already have */
183 hw->rate_min = i2sdev->rate;
184 hw->rate_max = i2sdev->rate;
185 }
186
187 hw->channels_min = 2;
188 hw->channels_max = 2;
189 /* these are somewhat arbitrary */
190 hw->buffer_bytes_max = 131072;
191 hw->period_bytes_min = 256;
192 hw->period_bytes_max = 16384;
193 hw->periods_min = 3;
194 hw->periods_max = MAX_DBDMA_COMMANDS;
195 err = snd_pcm_hw_constraint_integer(pi->substream->runtime,
196 SNDRV_PCM_HW_PARAM_PERIODS);
197 if (err < 0) {
198 result = err;
199 goto out_unlock;
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 /* unwind */
207 found_this = 0;
208 list_for_each_entry_reverse(rev,
209 &sdev->codec_list, list) {
210 if (found_this && rev->codec->close) {
211 rev->codec->close(rev,
212 pi->substream);
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
i2sbus_pcm_close(struct i2sbus_dev * i2sdev,int in)229 static 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
i2sbus_wait_for_stop(struct i2sbus_dev * i2sdev,struct pcm_info * pi)253 static 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 /* timeout expired, stop dbdma forcefully */
269 printk(KERN_ERR "i2sbus_wait_for_stop: timed out\n");
270 /* make sure RUN, PAUSE and S0 bits are cleared */
271 out_le32(&pi->dbdma->control, (RUN | PAUSE | 1) << 16);
272 pi->dbdma_ring.stopping = 0;
273 timeout = 10;
274 while (in_le32(&pi->dbdma->status) & ACTIVE) {
275 if (--timeout <= 0)
276 break;
277 udelay(1);
278 }
279 }
280 }
281 spin_unlock_irqrestore(&i2sdev->low_lock, flags);
282 }
283
284 #ifdef CONFIG_PM
i2sbus_wait_for_stop_both(struct i2sbus_dev * i2sdev)285 void 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
i2sbus_hw_free(struct snd_pcm_substream * substream,int in)296 static 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
i2sbus_playback_hw_free(struct snd_pcm_substream * substream)307 static int i2sbus_playback_hw_free(struct snd_pcm_substream *substream)
308 {
309 return i2sbus_hw_free(substream, 0);
310 }
311
i2sbus_record_hw_free(struct snd_pcm_substream * substream)312 static int i2sbus_record_hw_free(struct snd_pcm_substream *substream)
313 {
314 return i2sbus_hw_free(substream, 1);
315 }
316
i2sbus_pcm_prepare(struct i2sbus_dev * i2sdev,int in)317 static int i2sbus_pcm_prepare(struct i2sbus_dev *i2sdev, int in)
318 {
319 /* whee. Hard work now. The user has selected a bitrate
320 * and bit format, so now we have to program our
321 * I2S controller appropriately. */
322 struct snd_pcm_runtime *runtime;
323 struct dbdma_cmd *command;
324 int i, periodsize, nperiods;
325 dma_addr_t offset;
326 struct bus_info bi;
327 struct codec_info_item *cii;
328 int sfr = 0; /* serial format register */
329 int dws = 0; /* data word sizes reg */
330 int input_16bit;
331 struct pcm_info *pi, *other;
332 int cnt;
333 int result = 0;
334 unsigned int cmd, stopaddr;
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 /* generate dbdma command ring first */
369 command = pi->dbdma_ring.cmds;
370 memset(command, 0, (nperiods + 2) * sizeof(struct dbdma_cmd));
371
372 /* commands to DMA to/from the ring */
373 /*
374 * For input, we need to do a graceful stop; if we abort
375 * the DMA, we end up with leftover bytes that corrupt
376 * the next recording. To do this we set the S0 status
377 * bit and wait for the DMA controller to stop. Each
378 * command has a branch condition to
379 * make it branch to a stop command if S0 is set.
380 * On input we also need to wait for the S7 bit to be
381 * set before turning off the DMA controller.
382 * In fact we do the graceful stop for output as well.
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 /* branch back to beginning of ring */
396 command->command = cpu_to_le16(DBDMA_NOP | BR_ALWAYS);
397 command->cmd_dep = cpu_to_le32(pi->dbdma_ring.bus_cmd_start);
398 command++;
399
400 /* set stop command */
401 command->command = cpu_to_le16(DBDMA_STOP);
402
403 /* ok, let's set the serial format and stuff */
404 switch (runtime->format) {
405 /* 16 bit formats */
406 case SNDRV_PCM_FORMAT_S16_BE:
407 case SNDRV_PCM_FORMAT_U16_BE:
408 /* FIXME: if we add different bus factors we need to
409 * do more here!! */
410 bi.bus_factor = 0;
411 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
412 bi.bus_factor = cii->codec->bus_factor;
413 break;
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 /* force 64x bus speed, otherwise the data cannot be
424 * transferred quickly enough! */
425 bi.bus_factor = 64;
426 input_16bit = 0;
427 break;
428 default:
429 result = -EINVAL;
430 goto out_unlock;
431 }
432 /* we assume all sysclocks are the same! */
433 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
434 bi.sysclock_factor = cii->codec->sysclock_factor;
435 break;
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 /* FIXME: THIS ASSUMES MASTER ALL THE TIME */
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 /* codecs are fine with it, so set our clocks */
466 if (input_16bit)
467 dws = (2 << I2S_DWS_NUM_CHANNELS_IN_SHIFT) |
468 (2 << I2S_DWS_NUM_CHANNELS_OUT_SHIFT) |
469 I2S_DWS_DATA_IN_16BIT | I2S_DWS_DATA_OUT_16BIT;
470 else
471 dws = (2 << I2S_DWS_NUM_CHANNELS_IN_SHIFT) |
472 (2 << I2S_DWS_NUM_CHANNELS_OUT_SHIFT) |
473 I2S_DWS_DATA_IN_24BIT | I2S_DWS_DATA_OUT_24BIT;
474
475 /* early exit if already programmed correctly */
476 /* not locking these is fine since we touch them only in this function */
477 if (in_le32(&i2sdev->intfregs->serial_format) == sfr
478 && in_le32(&i2sdev->intfregs->data_word_sizes) == dws)
479 goto out_unlock;
480
481 /* let's notify the codecs about clocks going away.
482 * For now we only do mastering on the i2s cell... */
483 list_for_each_entry(cii, &i2sdev->sound.codec_list, list)
484 if (cii->codec->switch_clock)
485 cii->codec->switch_clock(cii, CLOCK_SWITCH_PREPARE_SLAVE);
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 /* wait for clock stopped. This can apparently take a while... */
497 cnt = 100;
498 while (cnt-- &&
499 !(in_le32(&i2sdev->intfregs->intr_ctl) & I2S_PENDING_CLOCKS_STOPPED)) {
500 msleep(5);
501 }
502 out_le32(&i2sdev->intfregs->intr_ctl, I2S_PENDING_CLOCKS_STOPPED);
503
504 /* not locking these is fine since we touch them only in this function */
505 out_le32(&i2sdev->intfregs->serial_format, sfr);
506 out_le32(&i2sdev->intfregs->data_word_sizes, dws);
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
i2sbus_pcm_prepare_both(struct i2sbus_dev * i2sdev)523 void 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
i2sbus_pcm_trigger(struct i2sbus_dev * i2sdev,int in,int cmd)530 static 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 /* Clear the S0 bit, then see if we stopped yet */
555 out_le32(&pi->dbdma->control, 1 << 16);
556 if (in_le32(&pi->dbdma->status) & ACTIVE) {
557 /* possible race here? */
558 udelay(10);
559 if (in_le32(&pi->dbdma->status) & ACTIVE) {
560 pi->dbdma_ring.stopping = 0;
561 goto out_unlock; /* keep running */
562 }
563 }
564 }
565
566 /* make sure RUN, PAUSE and S0 bits are cleared */
567 out_le32(&pi->dbdma->control, (RUN | PAUSE | 1) << 16);
568
569 /* set branch condition select register */
570 out_le32(&pi->dbdma->br_sel, (1 << 16) | 1);
571
572 /* write dma command buffer address to the dbdma chip */
573 out_le32(&pi->dbdma->cmdptr, pi->dbdma_ring.bus_cmd_start);
574
575 /* initialize the frame count and current period */
576 pi->current_period = 0;
577 pi->frame_count = in_le32(&i2sdev->intfregs->frame_count);
578
579 /* set the DMA controller running */
580 out_le32(&pi->dbdma->control, (RUN << 16) | RUN);
581
582 /* off you go! */
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 /* Set the S0 bit to make the DMA branch to the stop cmd */
594 out_le32(&pi->dbdma->control, (1 << 16) | 1);
595 pi->dbdma_ring.stopping = 1;
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
i2sbus_pcm_pointer(struct i2sbus_dev * i2sdev,int in)611 static 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
handle_interrupt(struct i2sbus_dev * i2sdev,int in)626 static 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 * BT is the branch taken bit. If it took a branch
646 * it is because we set the S0 bit to make it
647 * branch to the stop command.
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 * Check the frame count. The DMA tends to get a bit
660 * ahead of the frame counter, which confuses the core.
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 /* Turn off DMA controller, clear S0 bit */
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 /* may call _trigger again, hence needs to be unlocked */
694 snd_pcm_period_elapsed(pi->substream);
695 return;
696
697 out_unlock:
698 spin_unlock(&i2sdev->low_lock);
699 }
700
i2sbus_tx_intr(int irq,void * devid)701 irqreturn_t i2sbus_tx_intr(int irq, void *devid)
702 {
703 handle_interrupt((struct i2sbus_dev *)devid, 0);
704 return IRQ_HANDLED;
705 }
706
i2sbus_rx_intr(int irq,void * devid)707 irqreturn_t i2sbus_rx_intr(int irq, void *devid)
708 {
709 handle_interrupt((struct i2sbus_dev *)devid, 1);
710 return IRQ_HANDLED;
711 }
712
i2sbus_playback_open(struct snd_pcm_substream * substream)713 static 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
i2sbus_playback_close(struct snd_pcm_substream * substream)723 static 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
i2sbus_playback_prepare(struct snd_pcm_substream * substream)738 static 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
i2sbus_playback_trigger(struct snd_pcm_substream * substream,int cmd)749 static 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
i2sbus_playback_pointer(struct snd_pcm_substream * substream)760 static 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
772 static const struct snd_pcm_ops i2sbus_playback_ops = {
773 .open = i2sbus_playback_open,
774 .close = i2sbus_playback_close,
775 .hw_free = i2sbus_playback_hw_free,
776 .prepare = i2sbus_playback_prepare,
777 .trigger = i2sbus_playback_trigger,
778 .pointer = i2sbus_playback_pointer,
779 };
780
i2sbus_record_open(struct snd_pcm_substream * substream)781 static 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
i2sbus_record_close(struct snd_pcm_substream * substream)791 static 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
i2sbus_record_prepare(struct snd_pcm_substream * substream)806 static 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
i2sbus_record_trigger(struct snd_pcm_substream * substream,int cmd)817 static 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
i2sbus_record_pointer(struct snd_pcm_substream * substream)828 static 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
840 static const struct snd_pcm_ops i2sbus_record_ops = {
841 .open = i2sbus_record_open,
842 .close = i2sbus_record_close,
843 .hw_free = i2sbus_record_hw_free,
844 .prepare = i2sbus_record_prepare,
845 .trigger = i2sbus_record_trigger,
846 .pointer = i2sbus_record_pointer,
847 };
848
i2sbus_private_free(struct snd_pcm * pcm)849 static 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
867 int
i2sbus_attach_codec(struct soundbus_dev * dev,struct snd_card * card,struct codec_info * ci,void * data)868 i2sbus_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 /* we currently code the i2s transfer on the clock, and support only
891 * 32 and 64 */
892 if (ci->bus_factor != 32 && ci->bus_factor != 64)
893 return -EINVAL;
894
895 /* If you want to fix this, you need to keep track of what transport infos
896 * are to be used, which codecs they belong to, and then fix all the
897 * sysclock/busclock stuff above to depend on which is usable */
898 list_for_each_entry(cii, &dev->codec_list, list) {
899 if (cii->codec->sysclock_factor != ci->sysclock_factor) {
900 printk(KERN_DEBUG
901 "cannot yet handle multiple different sysclocks!\n");
902 return -EINVAL;
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 /* use the private data to point to the codec info */
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 /* ALSA yet again sucks.
959 * If it is ever fixed, remove this line. See below. */
960 out = in = 1;
961
962 if (!i2sdev->out.created && out) {
963 if (dev->pcm->card != card) {
964 /* eh? */
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 /* so we have to register the pcm after adding any substream
998 * to it because alsa doesn't create the devices for the
999 * substreams when we add them later.
1000 * Therefore, force in and out on both busses (above) and
1001 * register the pcm now instead of just after creating it.
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 /* no errors any more, so let's add this to our list */
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 /* well, we really should support scatter/gather DMA */
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
i2sbus_detach_codec(struct soundbus_dev * dev,void * data)1032 void 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 /* no more codecs, but still a pcm? */
1048 if (list_empty(&dev->codec_list) && dev->pcm) {
1049 /* the actual cleanup is done by the callback above! */
1050 snd_device_free(dev->pcm->card, dev->pcm);
1051 }
1052 }
1053