blob: 5b89c727601e7cc5db149e8bf8f927bd9d94df2a [file] [log] [blame]
Clemens Ladischd0ce9942007-12-23 19:50:57 +01001/*
2 * C-Media CMI8788 driver - PCM code
3 *
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License, version 2.
9 *
10 * This driver is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this driver; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
Clemens Ladischd0ce9942007-12-23 19:50:57 +010020#include <linux/pci.h>
21#include <sound/control.h>
22#include <sound/core.h>
23#include <sound/pcm.h>
24#include <sound/pcm_params.h>
25#include "oxygen.h"
26
Clemens Ladischc57cccc2008-01-21 08:54:06 +010027static const struct snd_pcm_hardware oxygen_stereo_hardware = {
28 .info = SNDRV_PCM_INFO_MMAP |
29 SNDRV_PCM_INFO_MMAP_VALID |
30 SNDRV_PCM_INFO_INTERLEAVED |
31 SNDRV_PCM_INFO_PAUSE |
32 SNDRV_PCM_INFO_SYNC_START,
33 .formats = SNDRV_PCM_FMTBIT_S16_LE |
34 SNDRV_PCM_FMTBIT_S32_LE,
35 .rates = SNDRV_PCM_RATE_32000 |
36 SNDRV_PCM_RATE_44100 |
37 SNDRV_PCM_RATE_48000 |
38 SNDRV_PCM_RATE_64000 |
39 SNDRV_PCM_RATE_88200 |
40 SNDRV_PCM_RATE_96000 |
41 SNDRV_PCM_RATE_176400 |
42 SNDRV_PCM_RATE_192000,
43 .rate_min = 32000,
44 .rate_max = 192000,
45 .channels_min = 2,
46 .channels_max = 2,
47 .buffer_bytes_max = 256 * 1024,
48 .period_bytes_min = 128,
49 .period_bytes_max = 128 * 1024,
50 .periods_min = 2,
51 .periods_max = 2048,
52};
53static const struct snd_pcm_hardware oxygen_multichannel_hardware = {
54 .info = SNDRV_PCM_INFO_MMAP |
55 SNDRV_PCM_INFO_MMAP_VALID |
56 SNDRV_PCM_INFO_INTERLEAVED |
57 SNDRV_PCM_INFO_PAUSE |
58 SNDRV_PCM_INFO_SYNC_START,
59 .formats = SNDRV_PCM_FMTBIT_S16_LE |
60 SNDRV_PCM_FMTBIT_S32_LE,
61 .rates = SNDRV_PCM_RATE_32000 |
62 SNDRV_PCM_RATE_44100 |
63 SNDRV_PCM_RATE_48000 |
64 SNDRV_PCM_RATE_64000 |
65 SNDRV_PCM_RATE_88200 |
66 SNDRV_PCM_RATE_96000 |
67 SNDRV_PCM_RATE_176400 |
68 SNDRV_PCM_RATE_192000,
69 .rate_min = 32000,
70 .rate_max = 192000,
71 .channels_min = 2,
72 .channels_max = 8,
73 .buffer_bytes_max = 2048 * 1024,
74 .period_bytes_min = 128,
75 .period_bytes_max = 256 * 1024,
76 .periods_min = 2,
77 .periods_max = 16384,
78};
79static const struct snd_pcm_hardware oxygen_ac97_hardware = {
80 .info = SNDRV_PCM_INFO_MMAP |
81 SNDRV_PCM_INFO_MMAP_VALID |
82 SNDRV_PCM_INFO_INTERLEAVED |
83 SNDRV_PCM_INFO_PAUSE |
84 SNDRV_PCM_INFO_SYNC_START,
85 .formats = SNDRV_PCM_FMTBIT_S16_LE,
86 .rates = SNDRV_PCM_RATE_48000,
87 .rate_min = 48000,
88 .rate_max = 48000,
89 .channels_min = 2,
90 .channels_max = 2,
91 .buffer_bytes_max = 256 * 1024,
92 .period_bytes_min = 128,
93 .period_bytes_max = 128 * 1024,
94 .periods_min = 2,
95 .periods_max = 2048,
96};
97
98static const struct snd_pcm_hardware *const oxygen_hardware[PCM_COUNT] = {
99 [PCM_A] = &oxygen_stereo_hardware,
100 [PCM_B] = &oxygen_stereo_hardware,
101 [PCM_C] = &oxygen_stereo_hardware,
102 [PCM_SPDIF] = &oxygen_stereo_hardware,
103 [PCM_MULTICH] = &oxygen_multichannel_hardware,
104 [PCM_AC97] = &oxygen_ac97_hardware,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100105};
106
Clemens Ladisch740eb832008-01-04 09:22:20 +0100107static inline unsigned int
108oxygen_substream_channel(struct snd_pcm_substream *substream)
109{
110 return (unsigned int)(uintptr_t)substream->runtime->private_data;
111}
112
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100113static int oxygen_open(struct snd_pcm_substream *substream,
114 unsigned int channel)
115{
116 struct oxygen *chip = snd_pcm_substream_chip(substream);
117 struct snd_pcm_runtime *runtime = substream->runtime;
118 int err;
119
Clemens Ladisch740eb832008-01-04 09:22:20 +0100120 runtime->private_data = (void *)(uintptr_t)channel;
Clemens Ladischc57cccc2008-01-21 08:54:06 +0100121 runtime->hw = *oxygen_hardware[channel];
Clemens Ladisch976cd622008-01-25 08:37:49 +0100122 switch (channel) {
123 case PCM_C:
Clemens Ladisch33c646e2008-01-24 08:43:16 +0100124 runtime->hw.rates &= ~(SNDRV_PCM_RATE_32000 |
125 SNDRV_PCM_RATE_64000);
126 runtime->hw.rate_min = 44100;
Clemens Ladisch976cd622008-01-25 08:37:49 +0100127 break;
128 case PCM_MULTICH:
129 runtime->hw.channels_max = chip->model->dac_channels;
130 break;
Clemens Ladisch33c646e2008-01-24 08:43:16 +0100131 }
Clemens Ladisch747c6012008-01-16 08:32:53 +0100132 if (chip->model->pcm_hardware_filter)
133 chip->model->pcm_hardware_filter(channel, &runtime->hw);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100134 err = snd_pcm_hw_constraint_step(runtime, 0,
135 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
136 if (err < 0)
137 return err;
138 err = snd_pcm_hw_constraint_step(runtime, 0,
139 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
140 if (err < 0)
141 return err;
142 if (runtime->hw.formats & SNDRV_PCM_FMTBIT_S32_LE) {
143 err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
144 if (err < 0)
145 return err;
146 }
147 if (runtime->hw.channels_max > 2) {
148 err = snd_pcm_hw_constraint_step(runtime, 0,
149 SNDRV_PCM_HW_PARAM_CHANNELS,
150 2);
151 if (err < 0)
152 return err;
153 }
154 snd_pcm_set_sync(substream);
155 chip->streams[channel] = substream;
156
157 mutex_lock(&chip->mutex);
158 chip->pcm_active |= 1 << channel;
159 if (channel == PCM_SPDIF) {
160 chip->spdif_pcm_bits = chip->spdif_bits;
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100161 chip->controls[CONTROL_SPDIF_PCM]->vd[0].access &=
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100162 ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
163 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
164 SNDRV_CTL_EVENT_MASK_INFO,
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100165 &chip->controls[CONTROL_SPDIF_PCM]->id);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100166 }
167 mutex_unlock(&chip->mutex);
168
169 return 0;
170}
171
172static int oxygen_rec_a_open(struct snd_pcm_substream *substream)
173{
174 return oxygen_open(substream, PCM_A);
175}
176
177static int oxygen_rec_b_open(struct snd_pcm_substream *substream)
178{
179 return oxygen_open(substream, PCM_B);
180}
181
182static int oxygen_rec_c_open(struct snd_pcm_substream *substream)
183{
184 return oxygen_open(substream, PCM_C);
185}
186
187static int oxygen_spdif_open(struct snd_pcm_substream *substream)
188{
189 return oxygen_open(substream, PCM_SPDIF);
190}
191
192static int oxygen_multich_open(struct snd_pcm_substream *substream)
193{
194 return oxygen_open(substream, PCM_MULTICH);
195}
196
197static int oxygen_ac97_open(struct snd_pcm_substream *substream)
198{
199 return oxygen_open(substream, PCM_AC97);
200}
201
202static int oxygen_close(struct snd_pcm_substream *substream)
203{
204 struct oxygen *chip = snd_pcm_substream_chip(substream);
Clemens Ladisch740eb832008-01-04 09:22:20 +0100205 unsigned int channel = oxygen_substream_channel(substream);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100206
207 mutex_lock(&chip->mutex);
208 chip->pcm_active &= ~(1 << channel);
209 if (channel == PCM_SPDIF) {
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100210 chip->controls[CONTROL_SPDIF_PCM]->vd[0].access |=
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100211 SNDRV_CTL_ELEM_ACCESS_INACTIVE;
212 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
213 SNDRV_CTL_EVENT_MASK_INFO,
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100214 &chip->controls[CONTROL_SPDIF_PCM]->id);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100215 }
216 if (channel == PCM_SPDIF || channel == PCM_MULTICH)
217 oxygen_update_spdif_source(chip);
218 mutex_unlock(&chip->mutex);
219
220 chip->streams[channel] = NULL;
221 return 0;
222}
223
224static unsigned int oxygen_format(struct snd_pcm_hw_params *hw_params)
225{
226 if (params_format(hw_params) == SNDRV_PCM_FORMAT_S32_LE)
227 return OXYGEN_FORMAT_24;
228 else
229 return OXYGEN_FORMAT_16;
230}
231
232static unsigned int oxygen_rate(struct snd_pcm_hw_params *hw_params)
233{
234 switch (params_rate(hw_params)) {
235 case 32000:
236 return OXYGEN_RATE_32000;
237 case 44100:
238 return OXYGEN_RATE_44100;
239 default: /* 48000 */
240 return OXYGEN_RATE_48000;
241 case 64000:
242 return OXYGEN_RATE_64000;
243 case 88200:
244 return OXYGEN_RATE_88200;
245 case 96000:
246 return OXYGEN_RATE_96000;
247 case 176400:
248 return OXYGEN_RATE_176400;
249 case 192000:
250 return OXYGEN_RATE_192000;
251 }
252}
253
Clemens Ladischc2353a02008-01-18 09:17:53 +0100254static unsigned int oxygen_i2s_mclk(struct snd_pcm_hw_params *hw_params)
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100255{
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100256 if (params_rate(hw_params) <= 96000)
257 return OXYGEN_I2S_MCLK_256;
258 else
259 return OXYGEN_I2S_MCLK_128;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100260}
261
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100262static unsigned int oxygen_i2s_bits(struct snd_pcm_hw_params *hw_params)
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100263{
264 if (params_format(hw_params) == SNDRV_PCM_FORMAT_S32_LE)
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100265 return OXYGEN_I2S_BITS_24;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100266 else
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100267 return OXYGEN_I2S_BITS_16;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100268}
269
270static unsigned int oxygen_play_channels(struct snd_pcm_hw_params *hw_params)
271{
272 switch (params_channels(hw_params)) {
273 default: /* 2 */
274 return OXYGEN_PLAY_CHANNELS_2;
275 case 4:
276 return OXYGEN_PLAY_CHANNELS_4;
277 case 6:
278 return OXYGEN_PLAY_CHANNELS_6;
279 case 8:
280 return OXYGEN_PLAY_CHANNELS_8;
281 }
282}
283
284static const unsigned int channel_base_registers[PCM_COUNT] = {
285 [PCM_A] = OXYGEN_DMA_A_ADDRESS,
286 [PCM_B] = OXYGEN_DMA_B_ADDRESS,
287 [PCM_C] = OXYGEN_DMA_C_ADDRESS,
288 [PCM_SPDIF] = OXYGEN_DMA_SPDIF_ADDRESS,
289 [PCM_MULTICH] = OXYGEN_DMA_MULTICH_ADDRESS,
290 [PCM_AC97] = OXYGEN_DMA_AC97_ADDRESS,
291};
292
293static int oxygen_hw_params(struct snd_pcm_substream *substream,
294 struct snd_pcm_hw_params *hw_params)
295{
296 struct oxygen *chip = snd_pcm_substream_chip(substream);
Clemens Ladisch740eb832008-01-04 09:22:20 +0100297 unsigned int channel = oxygen_substream_channel(substream);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100298 int err;
299
300 err = snd_pcm_lib_malloc_pages(substream,
301 params_buffer_bytes(hw_params));
302 if (err < 0)
303 return err;
304
305 oxygen_write32(chip, channel_base_registers[channel],
306 (u32)substream->runtime->dma_addr);
307 if (channel == PCM_MULTICH) {
308 oxygen_write32(chip, OXYGEN_DMA_MULTICH_COUNT,
309 params_buffer_bytes(hw_params) / 4 - 1);
310 oxygen_write32(chip, OXYGEN_DMA_MULTICH_TCOUNT,
311 params_period_bytes(hw_params) / 4 - 1);
312 } else {
313 oxygen_write16(chip, channel_base_registers[channel] + 4,
314 params_buffer_bytes(hw_params) / 4 - 1);
315 oxygen_write16(chip, channel_base_registers[channel] + 6,
316 params_period_bytes(hw_params) / 4 - 1);
317 }
318 return 0;
319}
320
321static int oxygen_rec_a_hw_params(struct snd_pcm_substream *substream,
322 struct snd_pcm_hw_params *hw_params)
323{
324 struct oxygen *chip = snd_pcm_substream_chip(substream);
325 int err;
326
327 err = oxygen_hw_params(substream, hw_params);
328 if (err < 0)
329 return err;
330
331 spin_lock_irq(&chip->reg_lock);
332 oxygen_write8_masked(chip, OXYGEN_REC_FORMAT,
333 oxygen_format(hw_params) << OXYGEN_REC_FORMAT_A_SHIFT,
334 OXYGEN_REC_FORMAT_A_MASK);
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100335 oxygen_write16_masked(chip, OXYGEN_I2S_A_FORMAT,
336 oxygen_rate(hw_params) |
Clemens Ladischc2353a02008-01-18 09:17:53 +0100337 oxygen_i2s_mclk(hw_params) |
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100338 chip->model->adc_i2s_format |
339 oxygen_i2s_bits(hw_params),
340 OXYGEN_I2S_RATE_MASK |
341 OXYGEN_I2S_FORMAT_MASK |
Clemens Ladischc2353a02008-01-18 09:17:53 +0100342 OXYGEN_I2S_MCLK_MASK |
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100343 OXYGEN_I2S_BITS_MASK);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100344 spin_unlock_irq(&chip->reg_lock);
345
346 mutex_lock(&chip->mutex);
347 chip->model->set_adc_params(chip, hw_params);
348 mutex_unlock(&chip->mutex);
349 return 0;
350}
351
352static int oxygen_rec_b_hw_params(struct snd_pcm_substream *substream,
353 struct snd_pcm_hw_params *hw_params)
354{
355 struct oxygen *chip = snd_pcm_substream_chip(substream);
356 int err;
357
358 err = oxygen_hw_params(substream, hw_params);
359 if (err < 0)
360 return err;
361
362 spin_lock_irq(&chip->reg_lock);
363 oxygen_write8_masked(chip, OXYGEN_REC_FORMAT,
364 oxygen_format(hw_params) << OXYGEN_REC_FORMAT_B_SHIFT,
365 OXYGEN_REC_FORMAT_B_MASK);
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100366 oxygen_write16_masked(chip, OXYGEN_I2S_B_FORMAT,
367 oxygen_rate(hw_params) |
Clemens Ladischc2353a02008-01-18 09:17:53 +0100368 oxygen_i2s_mclk(hw_params) |
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100369 chip->model->adc_i2s_format |
370 oxygen_i2s_bits(hw_params),
371 OXYGEN_I2S_RATE_MASK |
372 OXYGEN_I2S_FORMAT_MASK |
Clemens Ladischc2353a02008-01-18 09:17:53 +0100373 OXYGEN_I2S_MCLK_MASK |
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100374 OXYGEN_I2S_BITS_MASK);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100375 spin_unlock_irq(&chip->reg_lock);
376
377 mutex_lock(&chip->mutex);
378 chip->model->set_adc_params(chip, hw_params);
379 mutex_unlock(&chip->mutex);
380 return 0;
381}
382
383static int oxygen_rec_c_hw_params(struct snd_pcm_substream *substream,
384 struct snd_pcm_hw_params *hw_params)
385{
386 struct oxygen *chip = snd_pcm_substream_chip(substream);
387 int err;
388
389 err = oxygen_hw_params(substream, hw_params);
390 if (err < 0)
391 return err;
392
393 spin_lock_irq(&chip->reg_lock);
394 oxygen_write8_masked(chip, OXYGEN_REC_FORMAT,
395 oxygen_format(hw_params) << OXYGEN_REC_FORMAT_C_SHIFT,
396 OXYGEN_REC_FORMAT_C_MASK);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100397 spin_unlock_irq(&chip->reg_lock);
398 return 0;
399}
400
401static int oxygen_spdif_hw_params(struct snd_pcm_substream *substream,
402 struct snd_pcm_hw_params *hw_params)
403{
404 struct oxygen *chip = snd_pcm_substream_chip(substream);
405 int err;
406
407 err = oxygen_hw_params(substream, hw_params);
408 if (err < 0)
409 return err;
410
411 spin_lock_irq(&chip->reg_lock);
412 oxygen_clear_bits32(chip, OXYGEN_SPDIF_CONTROL,
413 OXYGEN_SPDIF_OUT_ENABLE);
414 oxygen_write8_masked(chip, OXYGEN_PLAY_FORMAT,
415 oxygen_format(hw_params) << OXYGEN_SPDIF_FORMAT_SHIFT,
416 OXYGEN_SPDIF_FORMAT_MASK);
417 oxygen_write32_masked(chip, OXYGEN_SPDIF_CONTROL,
418 oxygen_rate(hw_params) << OXYGEN_SPDIF_OUT_RATE_SHIFT,
419 OXYGEN_SPDIF_OUT_RATE_MASK);
420 oxygen_update_spdif_source(chip);
421 spin_unlock_irq(&chip->reg_lock);
422 return 0;
423}
424
425static int oxygen_multich_hw_params(struct snd_pcm_substream *substream,
426 struct snd_pcm_hw_params *hw_params)
427{
428 struct oxygen *chip = snd_pcm_substream_chip(substream);
429 int err;
430
431 err = oxygen_hw_params(substream, hw_params);
432 if (err < 0)
433 return err;
434
435 spin_lock_irq(&chip->reg_lock);
436 oxygen_write8_masked(chip, OXYGEN_PLAY_CHANNELS,
437 oxygen_play_channels(hw_params),
438 OXYGEN_PLAY_CHANNELS_MASK);
439 oxygen_write8_masked(chip, OXYGEN_PLAY_FORMAT,
440 oxygen_format(hw_params) << OXYGEN_MULTICH_FORMAT_SHIFT,
441 OXYGEN_MULTICH_FORMAT_MASK);
442 oxygen_write16_masked(chip, OXYGEN_I2S_MULTICH_FORMAT,
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100443 oxygen_rate(hw_params) |
444 chip->model->dac_i2s_format |
445 oxygen_i2s_bits(hw_params),
446 OXYGEN_I2S_RATE_MASK |
447 OXYGEN_I2S_FORMAT_MASK |
448 OXYGEN_I2S_BITS_MASK);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100449 oxygen_update_dac_routing(chip);
450 oxygen_update_spdif_source(chip);
451 spin_unlock_irq(&chip->reg_lock);
452
453 mutex_lock(&chip->mutex);
454 chip->model->set_dac_params(chip, hw_params);
455 mutex_unlock(&chip->mutex);
456 return 0;
457}
458
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100459static int oxygen_hw_free(struct snd_pcm_substream *substream)
460{
461 struct oxygen *chip = snd_pcm_substream_chip(substream);
Clemens Ladisch740eb832008-01-04 09:22:20 +0100462 unsigned int channel = oxygen_substream_channel(substream);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100463
464 spin_lock_irq(&chip->reg_lock);
465 chip->interrupt_mask &= ~(1 << channel);
466 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, chip->interrupt_mask);
467 spin_unlock_irq(&chip->reg_lock);
468
469 return snd_pcm_lib_free_pages(substream);
470}
471
472static int oxygen_spdif_hw_free(struct snd_pcm_substream *substream)
473{
474 struct oxygen *chip = snd_pcm_substream_chip(substream);
475
476 spin_lock_irq(&chip->reg_lock);
477 oxygen_clear_bits32(chip, OXYGEN_SPDIF_CONTROL,
478 OXYGEN_SPDIF_OUT_ENABLE);
479 spin_unlock_irq(&chip->reg_lock);
480 return oxygen_hw_free(substream);
481}
482
483static int oxygen_prepare(struct snd_pcm_substream *substream)
484{
485 struct oxygen *chip = snd_pcm_substream_chip(substream);
Clemens Ladisch740eb832008-01-04 09:22:20 +0100486 unsigned int channel = oxygen_substream_channel(substream);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100487 unsigned int channel_mask = 1 << channel;
488
489 spin_lock_irq(&chip->reg_lock);
490 oxygen_set_bits8(chip, OXYGEN_DMA_FLUSH, channel_mask);
491 oxygen_clear_bits8(chip, OXYGEN_DMA_FLUSH, channel_mask);
492
493 chip->interrupt_mask |= channel_mask;
494 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, chip->interrupt_mask);
495 spin_unlock_irq(&chip->reg_lock);
496 return 0;
497}
498
499static int oxygen_trigger(struct snd_pcm_substream *substream, int cmd)
500{
501 struct oxygen *chip = snd_pcm_substream_chip(substream);
502 struct snd_pcm_substream *s;
503 unsigned int mask = 0;
Clemens Ladischdb2396d2008-01-21 08:44:52 +0100504 int pausing;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100505
506 switch (cmd) {
507 case SNDRV_PCM_TRIGGER_STOP:
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100508 case SNDRV_PCM_TRIGGER_START:
Clemens Ladischdb2396d2008-01-21 08:44:52 +0100509 pausing = 0;
510 break;
511 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100512 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Clemens Ladischdb2396d2008-01-21 08:44:52 +0100513 pausing = 1;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100514 break;
515 default:
516 return -EINVAL;
517 }
518
519 snd_pcm_group_for_each_entry(s, substream) {
520 if (snd_pcm_substream_chip(s) == chip) {
Clemens Ladisch740eb832008-01-04 09:22:20 +0100521 mask |= 1 << oxygen_substream_channel(s);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100522 snd_pcm_trigger_done(s, substream);
523 }
524 }
525
526 spin_lock(&chip->reg_lock);
Clemens Ladischdb2396d2008-01-21 08:44:52 +0100527 if (!pausing) {
528 if (cmd == SNDRV_PCM_TRIGGER_START)
529 chip->pcm_running |= mask;
530 else
531 chip->pcm_running &= ~mask;
532 oxygen_write8(chip, OXYGEN_DMA_STATUS, chip->pcm_running);
533 } else {
534 if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH)
535 oxygen_set_bits8(chip, OXYGEN_DMA_PAUSE, mask);
536 else
537 oxygen_clear_bits8(chip, OXYGEN_DMA_PAUSE, mask);
538 }
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100539 spin_unlock(&chip->reg_lock);
540 return 0;
541}
542
543static snd_pcm_uframes_t oxygen_pointer(struct snd_pcm_substream *substream)
544{
545 struct oxygen *chip = snd_pcm_substream_chip(substream);
546 struct snd_pcm_runtime *runtime = substream->runtime;
Clemens Ladisch740eb832008-01-04 09:22:20 +0100547 unsigned int channel = oxygen_substream_channel(substream);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100548 u32 curr_addr;
549
550 /* no spinlock, this read should be atomic */
551 curr_addr = oxygen_read32(chip, channel_base_registers[channel]);
552 return bytes_to_frames(runtime, curr_addr - (u32)runtime->dma_addr);
553}
554
555static struct snd_pcm_ops oxygen_rec_a_ops = {
556 .open = oxygen_rec_a_open,
557 .close = oxygen_close,
558 .ioctl = snd_pcm_lib_ioctl,
559 .hw_params = oxygen_rec_a_hw_params,
560 .hw_free = oxygen_hw_free,
561 .prepare = oxygen_prepare,
562 .trigger = oxygen_trigger,
563 .pointer = oxygen_pointer,
564};
565
566static struct snd_pcm_ops oxygen_rec_b_ops = {
567 .open = oxygen_rec_b_open,
568 .close = oxygen_close,
569 .ioctl = snd_pcm_lib_ioctl,
570 .hw_params = oxygen_rec_b_hw_params,
571 .hw_free = oxygen_hw_free,
572 .prepare = oxygen_prepare,
573 .trigger = oxygen_trigger,
574 .pointer = oxygen_pointer,
575};
576
577static struct snd_pcm_ops oxygen_rec_c_ops = {
578 .open = oxygen_rec_c_open,
579 .close = oxygen_close,
580 .ioctl = snd_pcm_lib_ioctl,
581 .hw_params = oxygen_rec_c_hw_params,
582 .hw_free = oxygen_hw_free,
583 .prepare = oxygen_prepare,
584 .trigger = oxygen_trigger,
585 .pointer = oxygen_pointer,
586};
587
588static struct snd_pcm_ops oxygen_spdif_ops = {
589 .open = oxygen_spdif_open,
590 .close = oxygen_close,
591 .ioctl = snd_pcm_lib_ioctl,
592 .hw_params = oxygen_spdif_hw_params,
593 .hw_free = oxygen_spdif_hw_free,
594 .prepare = oxygen_prepare,
595 .trigger = oxygen_trigger,
596 .pointer = oxygen_pointer,
597};
598
599static struct snd_pcm_ops oxygen_multich_ops = {
600 .open = oxygen_multich_open,
601 .close = oxygen_close,
602 .ioctl = snd_pcm_lib_ioctl,
603 .hw_params = oxygen_multich_hw_params,
604 .hw_free = oxygen_hw_free,
605 .prepare = oxygen_prepare,
606 .trigger = oxygen_trigger,
607 .pointer = oxygen_pointer,
608};
609
610static struct snd_pcm_ops oxygen_ac97_ops = {
611 .open = oxygen_ac97_open,
612 .close = oxygen_close,
613 .ioctl = snd_pcm_lib_ioctl,
Clemens Ladischc2353a02008-01-18 09:17:53 +0100614 .hw_params = oxygen_hw_params,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100615 .hw_free = oxygen_hw_free,
616 .prepare = oxygen_prepare,
617 .trigger = oxygen_trigger,
618 .pointer = oxygen_pointer,
619};
620
621static void oxygen_pcm_free(struct snd_pcm *pcm)
622{
623 snd_pcm_lib_preallocate_free_for_all(pcm);
624}
625
626int __devinit oxygen_pcm_init(struct oxygen *chip)
627{
628 struct snd_pcm *pcm;
Clemens Ladische85e0922008-01-16 08:30:38 +0100629 int outs, ins;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100630 int err;
631
Clemens Ladische85e0922008-01-16 08:30:38 +0100632 outs = 1; /* OXYGEN_CHANNEL_MULTICH is always used */
633 ins = !!(chip->model->used_channels & (OXYGEN_CHANNEL_A |
634 OXYGEN_CHANNEL_B));
635 err = snd_pcm_new(chip->card, "Analog", 0, outs, ins, &pcm);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100636 if (err < 0)
637 return err;
638 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &oxygen_multich_ops);
Clemens Ladische85e0922008-01-16 08:30:38 +0100639 if (chip->model->used_channels & OXYGEN_CHANNEL_A)
640 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
641 &oxygen_rec_a_ops);
642 else if (chip->model->used_channels & OXYGEN_CHANNEL_B)
643 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
644 &oxygen_rec_b_ops);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100645 pcm->private_data = chip;
646 pcm->private_free = oxygen_pcm_free;
647 strcpy(pcm->name, "Analog");
648 snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream,
649 SNDRV_DMA_TYPE_DEV,
650 snd_dma_pci_data(chip->pci),
651 512 * 1024, 2048 * 1024);
Clemens Ladische85e0922008-01-16 08:30:38 +0100652 if (ins)
653 snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
654 SNDRV_DMA_TYPE_DEV,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100655 snd_dma_pci_data(chip->pci),
656 128 * 1024, 256 * 1024);
657
Clemens Ladische85e0922008-01-16 08:30:38 +0100658 outs = !!(chip->model->used_channels & OXYGEN_CHANNEL_SPDIF);
659 ins = !!(chip->model->used_channels & OXYGEN_CHANNEL_C);
660 if (outs | ins) {
661 err = snd_pcm_new(chip->card, "Digital", 1, outs, ins, &pcm);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100662 if (err < 0)
663 return err;
Clemens Ladische85e0922008-01-16 08:30:38 +0100664 if (outs)
665 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
666 &oxygen_spdif_ops);
667 if (ins)
668 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
669 &oxygen_rec_c_ops);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100670 pcm->private_data = chip;
671 pcm->private_free = oxygen_pcm_free;
Clemens Ladische85e0922008-01-16 08:30:38 +0100672 strcpy(pcm->name, "Digital");
673 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
674 snd_dma_pci_data(chip->pci),
675 128 * 1024, 256 * 1024);
676 }
677
678 outs = chip->has_ac97_1 &&
679 (chip->model->used_channels & OXYGEN_CHANNEL_AC97);
680 ins = (chip->model->used_channels & (OXYGEN_CHANNEL_A |
681 OXYGEN_CHANNEL_B))
682 == (OXYGEN_CHANNEL_A | OXYGEN_CHANNEL_B);
683 if (outs | ins) {
684 err = snd_pcm_new(chip->card, ins ? "Analog2" : "AC97",
685 2, outs, ins, &pcm);
686 if (err < 0)
687 return err;
688 if (outs)
689 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
690 &oxygen_ac97_ops);
691 if (ins)
692 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
693 &oxygen_rec_b_ops);
694 pcm->private_data = chip;
695 pcm->private_free = oxygen_pcm_free;
696 strcpy(pcm->name, ins ? "Analog 2" : "Front Panel");
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100697 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
698 snd_dma_pci_data(chip->pci),
699 128 * 1024, 256 * 1024);
700 }
701 return 0;
702}