blob: 053b09c79053e548c1c7c1d8deef5064e7780d29 [file] [log] [blame]
Johannes Bergf3d94782006-06-21 15:42:43 +02001/*
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
Takashi Iwai6cbbfe12015-01-28 16:49:33 +01009#include <linux/io.h>
Johannes Bergf3d94782006-06-21 15:42:43 +020010#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Johannes Bergf3d94782006-06-21 15:42:43 +020012#include <sound/core.h>
13#include <asm/macio.h>
14#include <linux/pci.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040015#include <linux/module.h>
Johannes Bergf3d94782006-06-21 15:42:43 +020016#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 /* sclk must be derived from mclk! */
38 if (mclk % sclk)
39 return -1;
40 /* derive sclk register value */
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 /* alsa messed up */
96 result = -EBUSY;
97 goto out_unlock;
98 }
99
100 /* we now need to assign the hw */
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 /* bus dependent stuff */
126 hw->info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100127 SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_RESUME |
128 SNDRV_PCM_INFO_JOINT_DUPLEX;
Johannes Bergf3d94782006-06-21 15:42:43 +0200129
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 /* well. the codec might want 24 bits only, and we'll
146 * ever only transfer 24 bits, but they are top-aligned!
147 * So for alsa, we claim that we're doing full 32 bit
148 * while in reality we'll ignore the lower 8 bits of
149 * that when doing playback (they're transferred as 0
150 * as far as I know, no codecs we have are 32-bit capable
151 * so I can't really test) and when doing recording we'll
152 * always have those lower 8 bits recorded as 0 */
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 /* now mask off what we can support. I suppose we could
158 * also support S24_3LE and some similar formats, but I
159 * doubt there's a codec that would be able to use that,
160 * so we don't support it here. */
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 /* we need to set the highest and lowest rate possible.
167 * These are the highest and lowest rates alsa can
168 * support properly in its bitfield.
169 * Below, we'll use that to restrict to the rate
170 * currently in use (if any). */
171 hw->rate_min = 5512;
172 hw->rate_max = 192000;
173 /* if the other stream is active, then we can only
174 * support what it is currently using.
175 * FIXME: I lied. This comment is wrong. We can support
176 * anything that works with the same serial format, ie.
177 * when recording 24 bit sound we can well play 16 bit
178 * sound at the same time iff using the same transfer mode.
179 */
180 if (other->active) {
181 /* FIXME: is this guaranteed by the alsa api? */
Eldad Zack74c34ca2013-04-23 01:00:41 +0200182 hw->formats &= pcm_format_to_bits(i2sdev->format);
Johannes Bergf3d94782006-06-21 15:42:43 +0200183 /* see above, restrict rates to the one we already have */
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 /* these are somewhat arbitrary */
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;
Heikki Lindholmdf86d112007-11-23 15:37:48 +0100196 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 }
Johannes Bergf3d94782006-06-21 15:42:43 +0200202 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 /* unwind */
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
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100254static 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 /* timeout expired, stop dbdma forcefully */
271 printk(KERN_ERR "i2sbus_wait_for_stop: timed out\n");
272 /* make sure RUN, PAUSE and S0 bits are cleared */
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
Johannes Bergf3d94782006-06-21 15:42:43 +0200298static 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
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100304static inline int i2sbus_hw_free(struct snd_pcm_substream *substream, int in)
Johannes Bergf3d94782006-06-21 15:42:43 +0200305{
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100306 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);
Johannes Bergf3d94782006-06-21 15:42:43 +0200312 snd_pcm_lib_free_pages(substream);
313 return 0;
314}
315
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100316static 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
Johannes Bergf3d94782006-06-21 15:42:43 +0200326static int i2sbus_pcm_prepare(struct i2sbus_dev *i2sdev, int in)
327{
328 /* whee. Hard work now. The user has selected a bitrate
329 * and bit format, so now we have to program our
330 * I2S controller appropriately. */
331 struct snd_pcm_runtime *runtime;
332 struct dbdma_cmd *command;
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100333 int i, periodsize, nperiods;
Johannes Bergf3d94782006-06-21 15:42:43 +0200334 dma_addr_t offset;
335 struct bus_info bi;
336 struct codec_info_item *cii;
337 int sfr = 0; /* serial format register */
338 int dws = 0; /* data word sizes reg */
339 int input_16bit;
340 struct pcm_info *pi, *other;
341 int cnt;
342 int result = 0;
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100343 unsigned int cmd, stopaddr;
Johannes Bergf3d94782006-06-21 15:42:43 +0200344
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 }
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100353 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 }
Johannes Bergf3d94782006-06-21 15:42:43 +0200360
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);
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100374 nperiods = pi->substream->runtime->periods;
Johannes Bergf3d94782006-06-21 15:42:43 +0200375 pi->current_period = 0;
376
377 /* generate dbdma command ring first */
378 command = pi->dbdma_ring.cmds;
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100379 memset(command, 0, (nperiods + 2) * sizeof(struct dbdma_cmd));
380
381 /* commands to DMA to/from the ring */
382 /*
383 * For input, we need to do a graceful stop; if we abort
384 * the DMA, we end up with leftover bytes that corrupt
385 * the next recording. To do this we set the S0 status
386 * bit and wait for the DMA controller to stop. Each
387 * command has a branch condition to
388 * make it branch to a stop command if S0 is set.
389 * On input we also need to wait for the S7 bit to be
390 * set before turning off the DMA controller.
391 * In fact we do the graceful stop for output as well.
392 */
Johannes Bergf3d94782006-06-21 15:42:43 +0200393 offset = runtime->dma_addr;
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100394 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);
Johannes Bergf3d94782006-06-21 15:42:43 +0200400 command->phy_addr = cpu_to_le32(offset);
401 command->req_count = cpu_to_le16(periodsize);
Johannes Bergf3d94782006-06-21 15:42:43 +0200402 }
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100403
404 /* branch back to beginning of ring */
405 command->command = cpu_to_le16(DBDMA_NOP | BR_ALWAYS);
Johannes Bergf3d94782006-06-21 15:42:43 +0200406 command->cmd_dep = cpu_to_le32(pi->dbdma_ring.bus_cmd_start);
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100407 command++;
408
409 /* set stop command */
410 command->command = cpu_to_le16(DBDMA_STOP);
Johannes Bergf3d94782006-06-21 15:42:43 +0200411
412 /* ok, let's set the serial format and stuff */
413 switch (runtime->format) {
414 /* 16 bit formats */
415 case SNDRV_PCM_FORMAT_S16_BE:
416 case SNDRV_PCM_FORMAT_U16_BE:
417 /* FIXME: if we add different bus factors we need to
418 * do more here!! */
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 /* force 64x bus speed, otherwise the data cannot be
433 * transferred quickly enough! */
434 bi.bus_factor = 64;
435 input_16bit = 0;
436 break;
437 default:
438 result = -EINVAL;
439 goto out_unlock;
440 }
441 /* we assume all sysclocks are the same! */
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 /* FIXME: THIS ASSUMES MASTER ALL THE TIME */
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 /* codecs are fine with it, so set our clocks */
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 /* early exit if already programmed correctly */
485 /* not locking these is fine since we touch them only in this function */
486 if (in_le32(&i2sdev->intfregs->serial_format) == sfr
487 && in_le32(&i2sdev->intfregs->data_word_sizes) == dws)
488 goto out_unlock;
489
490 /* let's notify the codecs about clocks going away.
491 * For now we only do mastering on the i2s cell... */
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 /* wait for clock stopped. This can apparently take a while... */
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 /* not locking these is fine since we touch them only in this function */
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
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100531#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
Johannes Bergf3d94782006-06-21 15:42:43 +0200538
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;
Johannes Bergf3d94782006-06-21 15:42:43 +0200543 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
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100562 if (pi->dbdma_ring.stopping) {
563 /* Clear the S0 bit, then see if we stopped yet */
564 out_le32(&pi->dbdma->control, 1 << 16);
565 if (in_le32(&pi->dbdma->status) & ACTIVE) {
566 /* possible race here? */
567 udelay(10);
568 if (in_le32(&pi->dbdma->status) & ACTIVE) {
569 pi->dbdma_ring.stopping = 0;
570 goto out_unlock; /* keep running */
571 }
572 }
Johannes Bergf3d94782006-06-21 15:42:43 +0200573 }
574
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100575 /* make sure RUN, PAUSE and S0 bits are cleared */
576 out_le32(&pi->dbdma->control, (RUN | PAUSE | 1) << 16);
577
578 /* set branch condition select register */
579 out_le32(&pi->dbdma->br_sel, (1 << 16) | 1);
580
Johannes Bergf3d94782006-06-21 15:42:43 +0200581 /* write dma command buffer address to the dbdma chip */
582 out_le32(&pi->dbdma->cmdptr, pi->dbdma_ring.bus_cmd_start);
Johannes Bergf3d94782006-06-21 15:42:43 +0200583
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100584 /* initialize the frame count and current period */
585 pi->current_period = 0;
Johannes Bergf3d94782006-06-21 15:42:43 +0200586 pi->frame_count = in_le32(&i2sdev->intfregs->frame_count);
587
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100588 /* set the DMA controller running */
589 out_le32(&pi->dbdma->control, (RUN << 16) | RUN);
590
Johannes Bergf3d94782006-06-21 15:42:43 +0200591 /* off you go! */
592 break;
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100593
Johannes Bergf3d94782006-06-21 15:42:43 +0200594 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 }
Johannes Bergf3d94782006-06-21 15:42:43 +0200600 pi->dbdma_ring.running = 0;
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100601
602 /* Set the S0 bit to make the DMA branch to the stop cmd */
603 out_le32(&pi->dbdma->control, (1 << 16) | 1);
604 pi->dbdma_ring.stopping = 1;
605
Johannes Bergf3d94782006-06-21 15:42:43 +0200606 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
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100630 if (fc >= pi->substream->runtime->buffer_size)
631 fc %= pi->substream->runtime->buffer_size;
632 return fc;
Johannes Bergf3d94782006-06-21 15:42:43 +0200633}
634
635static inline void handle_interrupt(struct i2sbus_dev *i2sdev, int in)
636{
637 struct pcm_info *pi;
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100638 u32 fc, nframes;
639 u32 status;
640 int timeout, i;
641 int dma_stopped = 0;
642 struct snd_pcm_runtime *runtime;
Johannes Bergf3d94782006-06-21 15:42:43 +0200643
644 spin_lock(&i2sdev->low_lock);
645 get_pcm_info(i2sdev, in, &pi, NULL);
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100646 if (!pi->dbdma_ring.running && !pi->dbdma_ring.stopping)
Johannes Bergf3d94782006-06-21 15:42:43 +0200647 goto out_unlock;
Johannes Bergf3d94782006-06-21 15:42:43 +0200648
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100649 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 * BT is the branch taken bit. If it took a branch
655 * it is because we set the S0 bit to make it
656 * branch to the stop command.
657 */
658 dma_stopped = 1;
659 pi->dbdma_ring.cmds[i].xfer_status = 0;
Johannes Bergf3d94782006-06-21 15:42:43 +0200660
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100661 if (++i >= runtime->periods) {
662 i = 0;
663 pi->frame_count += runtime->buffer_size;
Johannes Bergf3d94782006-06-21 15:42:43 +0200664 }
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100665 pi->current_period = i;
666
667 /*
668 * Check the frame count. The DMA tends to get a bit
669 * ahead of the frame counter, which confuses the core.
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;
Johannes Bergf3d94782006-06-21 15:42:43 +0200675 }
676
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100677 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 }
Johannes Bergf3d94782006-06-21 15:42:43 +0200690
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100691 /* Turn off DMA controller, clear S0 bit */
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;
Johannes Bergf3d94782006-06-21 15:42:43 +0200701 spin_unlock(&i2sdev->low_lock);
702 /* may call _trigger again, hence needs to be unlocked */
703 snd_pcm_period_elapsed(pi->substream);
704 return;
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100705
Johannes Bergf3d94782006-06-21 15:42:43 +0200706 out_unlock:
707 spin_unlock(&i2sdev->low_lock);
708}
709
David Howells7d12e782006-10-05 14:55:46 +0100710irqreturn_t i2sbus_tx_intr(int irq, void *devid)
Johannes Bergf3d94782006-06-21 15:42:43 +0200711{
712 handle_interrupt((struct i2sbus_dev *)devid, 0);
713 return IRQ_HANDLED;
714}
715
David Howells7d12e782006-10-05 14:55:46 +0100716irqreturn_t i2sbus_rx_intr(int irq, void *devid)
Johannes Bergf3d94782006-06-21 15:42:43 +0200717{
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,
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100786 .hw_free = i2sbus_playback_hw_free,
Johannes Bergf3d94782006-06-21 15:42:43 +0200787 .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,
Paul Mackerras547ac2a2007-02-08 14:25:39 +0100856 .hw_free = i2sbus_record_hw_free,
Johannes Bergf3d94782006-06-21 15:42:43 +0200857 .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
Johannes Bergf3d94782006-06-21 15:42:43 +0200880int
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 /* we currently code the i2s transfer on the clock, and support only
904 * 32 and 64 */
905 if (ci->bus_factor != 32 && ci->bus_factor != 64)
906 return -EINVAL;
907
908 /* If you want to fix this, you need to keep track of what transport infos
909 * are to be used, which codecs they belong to, and then fix all the
910 * sysclock/busclock stuff above to depend on which is usable */
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 /* use the private data to point to the codec info */
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");
Johannes Bergd595ee72006-10-05 15:08:23 +0200947 err = -ENODEV;
948 goto out_free_cii;
Johannes Bergf3d94782006-06-21 15:42:43 +0200949 }
950
951 if (!try_module_get(THIS_MODULE)) {
952 printk(KERN_DEBUG "i2sbus: failed to get module reference!\n");
Johannes Bergd595ee72006-10-05 15:08:23 +0200953 err = -EBUSY;
954 goto out_put_sdev;
Johannes Bergf3d94782006-06-21 15:42:43 +0200955 }
956
957 if (!try_module_get(ci->owner)) {
958 printk(KERN_DEBUG
959 "i2sbus: failed to get module reference to codec owner!\n");
Johannes Bergd595ee72006-10-05 15:08:23 +0200960 err = -EBUSY;
961 goto out_put_this_module;
Johannes Bergf3d94782006-06-21 15:42:43 +0200962 }
963
964 if (!dev->pcm) {
Johannes Berg73e85fe2006-10-05 15:07:23 +0200965 err = snd_pcm_new(card, dev->pcmname, dev->pcmid, 0, 0,
Johannes Bergf3d94782006-06-21 15:42:43 +0200966 &dev->pcm);
967 if (err) {
968 printk(KERN_DEBUG "i2sbus: failed to create pcm\n");
Johannes Bergd595ee72006-10-05 15:08:23 +0200969 goto out_put_ci_module;
Johannes Bergf3d94782006-06-21 15:42:43 +0200970 }
Johannes Bergf3d94782006-06-21 15:42:43 +0200971 }
972
973 /* ALSA yet again sucks.
974 * If it is ever fixed, remove this line. See below. */
975 out = in = 1;
976
977 if (!i2sdev->out.created && out) {
978 if (dev->pcm->card != card) {
979 /* eh? */
980 printk(KERN_ERR
981 "Can't attach same bus to different cards!\n");
Johannes Bergd595ee72006-10-05 15:08:23 +0200982 err = -EINVAL;
983 goto out_put_ci_module;
Johannes Bergf3d94782006-06-21 15:42:43 +0200984 }
Johannes Bergd595ee72006-10-05 15:08:23 +0200985 err = snd_pcm_new_stream(dev->pcm, SNDRV_PCM_STREAM_PLAYBACK, 1);
986 if (err)
987 goto out_put_ci_module;
Johannes Bergf3d94782006-06-21 15:42:43 +0200988 snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_PLAYBACK,
989 &i2sbus_playback_ops);
Takashi Iwaief46c7a2015-01-29 17:32:26 +0100990 dev->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].dev.parent =
991 &dev->ofdev.dev;
Johannes Bergf3d94782006-06-21 15:42:43 +0200992 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");
Takashi Iwai3d3909f2007-11-23 15:41:44 +0100999 err = -EINVAL;
Johannes Bergd595ee72006-10-05 15:08:23 +02001000 goto out_put_ci_module;
Johannes Bergf3d94782006-06-21 15:42:43 +02001001 }
Johannes Bergd595ee72006-10-05 15:08:23 +02001002 err = snd_pcm_new_stream(dev->pcm, SNDRV_PCM_STREAM_CAPTURE, 1);
1003 if (err)
1004 goto out_put_ci_module;
Johannes Bergf3d94782006-06-21 15:42:43 +02001005 snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_CAPTURE,
1006 &i2sbus_record_ops);
Takashi Iwaief46c7a2015-01-29 17:32:26 +01001007 dev->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].dev.parent =
1008 &dev->ofdev.dev;
Johannes Bergf3d94782006-06-21 15:42:43 +02001009 i2sdev->in.created = 1;
1010 }
1011
1012 /* so we have to register the pcm after adding any substream
1013 * to it because alsa doesn't create the devices for the
1014 * substreams when we add them later.
1015 * Therefore, force in and out on both busses (above) and
1016 * register the pcm now instead of just after creating it.
1017 */
1018 err = snd_device_register(card, dev->pcm);
1019 if (err) {
1020 printk(KERN_ERR "i2sbus: error registering new pcm\n");
Johannes Bergd595ee72006-10-05 15:08:23 +02001021 goto out_put_ci_module;
Johannes Bergf3d94782006-06-21 15:42:43 +02001022 }
1023 /* no errors any more, so let's add this to our list */
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 /* well, we really should support scatter/gather DMA */
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;
Johannes Bergd595ee72006-10-05 15:08:23 +02001036 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;
Johannes Bergf3d94782006-06-21 15:42:43 +02001045}
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 /* no more codecs, but still a pcm? */
1063 if (list_empty(&dev->codec_list) && dev->pcm) {
1064 /* the actual cleanup is done by the callback above! */
1065 snd_device_free(dev->pcm->card, dev->pcm);
1066 }
1067}