blob: df1d0cae1dadc5d57e0985c04fd999a2a113e026 [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 Ladischc2353a02008-01-18 09:17:53 +0100256 return params_rate(hw_params) <= 96000
257 ? OXYGEN_I2S_MCLK_256 : OXYGEN_I2S_MCLK_128;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100258}
259
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100260static unsigned int oxygen_i2s_bits(struct snd_pcm_hw_params *hw_params)
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100261{
262 if (params_format(hw_params) == SNDRV_PCM_FORMAT_S32_LE)
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100263 return OXYGEN_I2S_BITS_24;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100264 else
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100265 return OXYGEN_I2S_BITS_16;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100266}
267
268static unsigned int oxygen_play_channels(struct snd_pcm_hw_params *hw_params)
269{
270 switch (params_channels(hw_params)) {
271 default: /* 2 */
272 return OXYGEN_PLAY_CHANNELS_2;
273 case 4:
274 return OXYGEN_PLAY_CHANNELS_4;
275 case 6:
276 return OXYGEN_PLAY_CHANNELS_6;
277 case 8:
278 return OXYGEN_PLAY_CHANNELS_8;
279 }
280}
281
282static const unsigned int channel_base_registers[PCM_COUNT] = {
283 [PCM_A] = OXYGEN_DMA_A_ADDRESS,
284 [PCM_B] = OXYGEN_DMA_B_ADDRESS,
285 [PCM_C] = OXYGEN_DMA_C_ADDRESS,
286 [PCM_SPDIF] = OXYGEN_DMA_SPDIF_ADDRESS,
287 [PCM_MULTICH] = OXYGEN_DMA_MULTICH_ADDRESS,
288 [PCM_AC97] = OXYGEN_DMA_AC97_ADDRESS,
289};
290
291static int oxygen_hw_params(struct snd_pcm_substream *substream,
292 struct snd_pcm_hw_params *hw_params)
293{
294 struct oxygen *chip = snd_pcm_substream_chip(substream);
Clemens Ladisch740eb832008-01-04 09:22:20 +0100295 unsigned int channel = oxygen_substream_channel(substream);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100296 int err;
297
298 err = snd_pcm_lib_malloc_pages(substream,
299 params_buffer_bytes(hw_params));
300 if (err < 0)
301 return err;
302
303 oxygen_write32(chip, channel_base_registers[channel],
304 (u32)substream->runtime->dma_addr);
305 if (channel == PCM_MULTICH) {
306 oxygen_write32(chip, OXYGEN_DMA_MULTICH_COUNT,
307 params_buffer_bytes(hw_params) / 4 - 1);
308 oxygen_write32(chip, OXYGEN_DMA_MULTICH_TCOUNT,
309 params_period_bytes(hw_params) / 4 - 1);
310 } else {
311 oxygen_write16(chip, channel_base_registers[channel] + 4,
312 params_buffer_bytes(hw_params) / 4 - 1);
313 oxygen_write16(chip, channel_base_registers[channel] + 6,
314 params_period_bytes(hw_params) / 4 - 1);
315 }
316 return 0;
317}
318
319static int oxygen_rec_a_hw_params(struct snd_pcm_substream *substream,
320 struct snd_pcm_hw_params *hw_params)
321{
322 struct oxygen *chip = snd_pcm_substream_chip(substream);
323 int err;
324
325 err = oxygen_hw_params(substream, hw_params);
326 if (err < 0)
327 return err;
328
329 spin_lock_irq(&chip->reg_lock);
330 oxygen_write8_masked(chip, OXYGEN_REC_FORMAT,
331 oxygen_format(hw_params) << OXYGEN_REC_FORMAT_A_SHIFT,
332 OXYGEN_REC_FORMAT_A_MASK);
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100333 oxygen_write16_masked(chip, OXYGEN_I2S_A_FORMAT,
334 oxygen_rate(hw_params) |
Clemens Ladischc2353a02008-01-18 09:17:53 +0100335 oxygen_i2s_mclk(hw_params) |
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100336 chip->model->adc_i2s_format |
337 oxygen_i2s_bits(hw_params),
338 OXYGEN_I2S_RATE_MASK |
339 OXYGEN_I2S_FORMAT_MASK |
Clemens Ladischc2353a02008-01-18 09:17:53 +0100340 OXYGEN_I2S_MCLK_MASK |
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100341 OXYGEN_I2S_BITS_MASK);
Clemens Ladischc9946b22008-01-21 08:44:24 +0100342 oxygen_write8_masked(chip, OXYGEN_REC_ROUTING,
343 OXYGEN_REC_A_ROUTE_I2S_ADC_1,
344 OXYGEN_REC_A_ROUTE_MASK);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100345 spin_unlock_irq(&chip->reg_lock);
346
347 mutex_lock(&chip->mutex);
348 chip->model->set_adc_params(chip, hw_params);
349 mutex_unlock(&chip->mutex);
350 return 0;
351}
352
353static int oxygen_rec_b_hw_params(struct snd_pcm_substream *substream,
354 struct snd_pcm_hw_params *hw_params)
355{
356 struct oxygen *chip = snd_pcm_substream_chip(substream);
357 int err;
358
359 err = oxygen_hw_params(substream, hw_params);
360 if (err < 0)
361 return err;
362
363 spin_lock_irq(&chip->reg_lock);
364 oxygen_write8_masked(chip, OXYGEN_REC_FORMAT,
365 oxygen_format(hw_params) << OXYGEN_REC_FORMAT_B_SHIFT,
366 OXYGEN_REC_FORMAT_B_MASK);
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100367 oxygen_write16_masked(chip, OXYGEN_I2S_B_FORMAT,
368 oxygen_rate(hw_params) |
Clemens Ladischc2353a02008-01-18 09:17:53 +0100369 oxygen_i2s_mclk(hw_params) |
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100370 chip->model->adc_i2s_format |
371 oxygen_i2s_bits(hw_params),
372 OXYGEN_I2S_RATE_MASK |
373 OXYGEN_I2S_FORMAT_MASK |
Clemens Ladischc2353a02008-01-18 09:17:53 +0100374 OXYGEN_I2S_MCLK_MASK |
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100375 OXYGEN_I2S_BITS_MASK);
Clemens Ladischc9946b22008-01-21 08:44:24 +0100376 oxygen_write8_masked(chip, OXYGEN_REC_ROUTING,
377 OXYGEN_REC_B_ROUTE_I2S_ADC_2,
378 OXYGEN_REC_B_ROUTE_MASK);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100379 spin_unlock_irq(&chip->reg_lock);
380
381 mutex_lock(&chip->mutex);
382 chip->model->set_adc_params(chip, hw_params);
383 mutex_unlock(&chip->mutex);
384 return 0;
385}
386
387static int oxygen_rec_c_hw_params(struct snd_pcm_substream *substream,
388 struct snd_pcm_hw_params *hw_params)
389{
390 struct oxygen *chip = snd_pcm_substream_chip(substream);
391 int err;
392
393 err = oxygen_hw_params(substream, hw_params);
394 if (err < 0)
395 return err;
396
397 spin_lock_irq(&chip->reg_lock);
398 oxygen_write8_masked(chip, OXYGEN_REC_FORMAT,
399 oxygen_format(hw_params) << OXYGEN_REC_FORMAT_C_SHIFT,
400 OXYGEN_REC_FORMAT_C_MASK);
Clemens Ladischc9946b22008-01-21 08:44:24 +0100401 oxygen_write8_masked(chip, OXYGEN_REC_ROUTING,
402 OXYGEN_REC_C_ROUTE_SPDIF,
403 OXYGEN_REC_C_ROUTE_MASK);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100404 spin_unlock_irq(&chip->reg_lock);
405 return 0;
406}
407
408static int oxygen_spdif_hw_params(struct snd_pcm_substream *substream,
409 struct snd_pcm_hw_params *hw_params)
410{
411 struct oxygen *chip = snd_pcm_substream_chip(substream);
412 int err;
413
414 err = oxygen_hw_params(substream, hw_params);
415 if (err < 0)
416 return err;
417
418 spin_lock_irq(&chip->reg_lock);
419 oxygen_clear_bits32(chip, OXYGEN_SPDIF_CONTROL,
420 OXYGEN_SPDIF_OUT_ENABLE);
421 oxygen_write8_masked(chip, OXYGEN_PLAY_FORMAT,
422 oxygen_format(hw_params) << OXYGEN_SPDIF_FORMAT_SHIFT,
423 OXYGEN_SPDIF_FORMAT_MASK);
424 oxygen_write32_masked(chip, OXYGEN_SPDIF_CONTROL,
425 oxygen_rate(hw_params) << OXYGEN_SPDIF_OUT_RATE_SHIFT,
426 OXYGEN_SPDIF_OUT_RATE_MASK);
427 oxygen_update_spdif_source(chip);
428 spin_unlock_irq(&chip->reg_lock);
429 return 0;
430}
431
432static int oxygen_multich_hw_params(struct snd_pcm_substream *substream,
433 struct snd_pcm_hw_params *hw_params)
434{
435 struct oxygen *chip = snd_pcm_substream_chip(substream);
436 int err;
437
438 err = oxygen_hw_params(substream, hw_params);
439 if (err < 0)
440 return err;
441
442 spin_lock_irq(&chip->reg_lock);
443 oxygen_write8_masked(chip, OXYGEN_PLAY_CHANNELS,
444 oxygen_play_channels(hw_params),
445 OXYGEN_PLAY_CHANNELS_MASK);
446 oxygen_write8_masked(chip, OXYGEN_PLAY_FORMAT,
447 oxygen_format(hw_params) << OXYGEN_MULTICH_FORMAT_SHIFT,
448 OXYGEN_MULTICH_FORMAT_MASK);
449 oxygen_write16_masked(chip, OXYGEN_I2S_MULTICH_FORMAT,
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100450 oxygen_rate(hw_params) |
451 chip->model->dac_i2s_format |
452 oxygen_i2s_bits(hw_params),
453 OXYGEN_I2S_RATE_MASK |
454 OXYGEN_I2S_FORMAT_MASK |
455 OXYGEN_I2S_BITS_MASK);
Clemens Ladischc9946b22008-01-21 08:44:24 +0100456 oxygen_write16_masked(chip, OXYGEN_PLAY_ROUTING,
457 OXYGEN_PLAY_MULTICH_I2S_DAC,
458 OXYGEN_PLAY_MUTE01 | OXYGEN_PLAY_MUTE23 |
459 OXYGEN_PLAY_MUTE45 | OXYGEN_PLAY_MUTE67 |
460 OXYGEN_PLAY_MULTICH_MASK);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100461 oxygen_update_dac_routing(chip);
462 oxygen_update_spdif_source(chip);
463 spin_unlock_irq(&chip->reg_lock);
464
465 mutex_lock(&chip->mutex);
466 chip->model->set_dac_params(chip, hw_params);
467 mutex_unlock(&chip->mutex);
468 return 0;
469}
470
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100471static int oxygen_hw_free(struct snd_pcm_substream *substream)
472{
473 struct oxygen *chip = snd_pcm_substream_chip(substream);
Clemens Ladisch740eb832008-01-04 09:22:20 +0100474 unsigned int channel = oxygen_substream_channel(substream);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100475
476 spin_lock_irq(&chip->reg_lock);
477 chip->interrupt_mask &= ~(1 << channel);
478 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, chip->interrupt_mask);
479 spin_unlock_irq(&chip->reg_lock);
480
481 return snd_pcm_lib_free_pages(substream);
482}
483
484static int oxygen_spdif_hw_free(struct snd_pcm_substream *substream)
485{
486 struct oxygen *chip = snd_pcm_substream_chip(substream);
487
488 spin_lock_irq(&chip->reg_lock);
489 oxygen_clear_bits32(chip, OXYGEN_SPDIF_CONTROL,
490 OXYGEN_SPDIF_OUT_ENABLE);
491 spin_unlock_irq(&chip->reg_lock);
492 return oxygen_hw_free(substream);
493}
494
495static int oxygen_prepare(struct snd_pcm_substream *substream)
496{
497 struct oxygen *chip = snd_pcm_substream_chip(substream);
Clemens Ladisch740eb832008-01-04 09:22:20 +0100498 unsigned int channel = oxygen_substream_channel(substream);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100499 unsigned int channel_mask = 1 << channel;
500
501 spin_lock_irq(&chip->reg_lock);
502 oxygen_set_bits8(chip, OXYGEN_DMA_FLUSH, channel_mask);
503 oxygen_clear_bits8(chip, OXYGEN_DMA_FLUSH, channel_mask);
504
505 chip->interrupt_mask |= channel_mask;
506 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, chip->interrupt_mask);
507 spin_unlock_irq(&chip->reg_lock);
508 return 0;
509}
510
511static int oxygen_trigger(struct snd_pcm_substream *substream, int cmd)
512{
513 struct oxygen *chip = snd_pcm_substream_chip(substream);
514 struct snd_pcm_substream *s;
515 unsigned int mask = 0;
Clemens Ladischdb2396d2008-01-21 08:44:52 +0100516 int pausing;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100517
518 switch (cmd) {
519 case SNDRV_PCM_TRIGGER_STOP:
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100520 case SNDRV_PCM_TRIGGER_START:
Clemens Ladischdb2396d2008-01-21 08:44:52 +0100521 pausing = 0;
522 break;
523 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100524 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Clemens Ladischdb2396d2008-01-21 08:44:52 +0100525 pausing = 1;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100526 break;
527 default:
528 return -EINVAL;
529 }
530
531 snd_pcm_group_for_each_entry(s, substream) {
532 if (snd_pcm_substream_chip(s) == chip) {
Clemens Ladisch740eb832008-01-04 09:22:20 +0100533 mask |= 1 << oxygen_substream_channel(s);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100534 snd_pcm_trigger_done(s, substream);
535 }
536 }
537
538 spin_lock(&chip->reg_lock);
Clemens Ladischdb2396d2008-01-21 08:44:52 +0100539 if (!pausing) {
540 if (cmd == SNDRV_PCM_TRIGGER_START)
541 chip->pcm_running |= mask;
542 else
543 chip->pcm_running &= ~mask;
544 oxygen_write8(chip, OXYGEN_DMA_STATUS, chip->pcm_running);
545 } else {
546 if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH)
547 oxygen_set_bits8(chip, OXYGEN_DMA_PAUSE, mask);
548 else
549 oxygen_clear_bits8(chip, OXYGEN_DMA_PAUSE, mask);
550 }
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100551 spin_unlock(&chip->reg_lock);
552 return 0;
553}
554
555static snd_pcm_uframes_t oxygen_pointer(struct snd_pcm_substream *substream)
556{
557 struct oxygen *chip = snd_pcm_substream_chip(substream);
558 struct snd_pcm_runtime *runtime = substream->runtime;
Clemens Ladisch740eb832008-01-04 09:22:20 +0100559 unsigned int channel = oxygen_substream_channel(substream);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100560 u32 curr_addr;
561
562 /* no spinlock, this read should be atomic */
563 curr_addr = oxygen_read32(chip, channel_base_registers[channel]);
564 return bytes_to_frames(runtime, curr_addr - (u32)runtime->dma_addr);
565}
566
567static struct snd_pcm_ops oxygen_rec_a_ops = {
568 .open = oxygen_rec_a_open,
569 .close = oxygen_close,
570 .ioctl = snd_pcm_lib_ioctl,
571 .hw_params = oxygen_rec_a_hw_params,
572 .hw_free = oxygen_hw_free,
573 .prepare = oxygen_prepare,
574 .trigger = oxygen_trigger,
575 .pointer = oxygen_pointer,
576};
577
578static struct snd_pcm_ops oxygen_rec_b_ops = {
579 .open = oxygen_rec_b_open,
580 .close = oxygen_close,
581 .ioctl = snd_pcm_lib_ioctl,
582 .hw_params = oxygen_rec_b_hw_params,
583 .hw_free = oxygen_hw_free,
584 .prepare = oxygen_prepare,
585 .trigger = oxygen_trigger,
586 .pointer = oxygen_pointer,
587};
588
589static struct snd_pcm_ops oxygen_rec_c_ops = {
590 .open = oxygen_rec_c_open,
591 .close = oxygen_close,
592 .ioctl = snd_pcm_lib_ioctl,
593 .hw_params = oxygen_rec_c_hw_params,
594 .hw_free = oxygen_hw_free,
595 .prepare = oxygen_prepare,
596 .trigger = oxygen_trigger,
597 .pointer = oxygen_pointer,
598};
599
600static struct snd_pcm_ops oxygen_spdif_ops = {
601 .open = oxygen_spdif_open,
602 .close = oxygen_close,
603 .ioctl = snd_pcm_lib_ioctl,
604 .hw_params = oxygen_spdif_hw_params,
605 .hw_free = oxygen_spdif_hw_free,
606 .prepare = oxygen_prepare,
607 .trigger = oxygen_trigger,
608 .pointer = oxygen_pointer,
609};
610
611static struct snd_pcm_ops oxygen_multich_ops = {
612 .open = oxygen_multich_open,
613 .close = oxygen_close,
614 .ioctl = snd_pcm_lib_ioctl,
615 .hw_params = oxygen_multich_hw_params,
616 .hw_free = oxygen_hw_free,
617 .prepare = oxygen_prepare,
618 .trigger = oxygen_trigger,
619 .pointer = oxygen_pointer,
620};
621
622static struct snd_pcm_ops oxygen_ac97_ops = {
623 .open = oxygen_ac97_open,
624 .close = oxygen_close,
625 .ioctl = snd_pcm_lib_ioctl,
Clemens Ladischc2353a02008-01-18 09:17:53 +0100626 .hw_params = oxygen_hw_params,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100627 .hw_free = oxygen_hw_free,
628 .prepare = oxygen_prepare,
629 .trigger = oxygen_trigger,
630 .pointer = oxygen_pointer,
631};
632
633static void oxygen_pcm_free(struct snd_pcm *pcm)
634{
635 snd_pcm_lib_preallocate_free_for_all(pcm);
636}
637
638int __devinit oxygen_pcm_init(struct oxygen *chip)
639{
640 struct snd_pcm *pcm;
Clemens Ladische85e0922008-01-16 08:30:38 +0100641 int outs, ins;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100642 int err;
643
Clemens Ladische85e0922008-01-16 08:30:38 +0100644 outs = 1; /* OXYGEN_CHANNEL_MULTICH is always used */
645 ins = !!(chip->model->used_channels & (OXYGEN_CHANNEL_A |
646 OXYGEN_CHANNEL_B));
647 err = snd_pcm_new(chip->card, "Analog", 0, outs, ins, &pcm);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100648 if (err < 0)
649 return err;
650 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &oxygen_multich_ops);
Clemens Ladische85e0922008-01-16 08:30:38 +0100651 if (chip->model->used_channels & OXYGEN_CHANNEL_A)
652 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
653 &oxygen_rec_a_ops);
654 else if (chip->model->used_channels & OXYGEN_CHANNEL_B)
655 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
656 &oxygen_rec_b_ops);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100657 pcm->private_data = chip;
658 pcm->private_free = oxygen_pcm_free;
659 strcpy(pcm->name, "Analog");
660 snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream,
661 SNDRV_DMA_TYPE_DEV,
662 snd_dma_pci_data(chip->pci),
663 512 * 1024, 2048 * 1024);
Clemens Ladische85e0922008-01-16 08:30:38 +0100664 if (ins)
665 snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
666 SNDRV_DMA_TYPE_DEV,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100667 snd_dma_pci_data(chip->pci),
668 128 * 1024, 256 * 1024);
669
Clemens Ladische85e0922008-01-16 08:30:38 +0100670 outs = !!(chip->model->used_channels & OXYGEN_CHANNEL_SPDIF);
671 ins = !!(chip->model->used_channels & OXYGEN_CHANNEL_C);
672 if (outs | ins) {
673 err = snd_pcm_new(chip->card, "Digital", 1, outs, ins, &pcm);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100674 if (err < 0)
675 return err;
Clemens Ladische85e0922008-01-16 08:30:38 +0100676 if (outs)
677 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
678 &oxygen_spdif_ops);
679 if (ins)
680 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
681 &oxygen_rec_c_ops);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100682 pcm->private_data = chip;
683 pcm->private_free = oxygen_pcm_free;
Clemens Ladische85e0922008-01-16 08:30:38 +0100684 strcpy(pcm->name, "Digital");
685 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
686 snd_dma_pci_data(chip->pci),
687 128 * 1024, 256 * 1024);
688 }
689
690 outs = chip->has_ac97_1 &&
691 (chip->model->used_channels & OXYGEN_CHANNEL_AC97);
692 ins = (chip->model->used_channels & (OXYGEN_CHANNEL_A |
693 OXYGEN_CHANNEL_B))
694 == (OXYGEN_CHANNEL_A | OXYGEN_CHANNEL_B);
695 if (outs | ins) {
696 err = snd_pcm_new(chip->card, ins ? "Analog2" : "AC97",
697 2, outs, ins, &pcm);
698 if (err < 0)
699 return err;
700 if (outs)
701 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
702 &oxygen_ac97_ops);
703 if (ins)
704 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
705 &oxygen_rec_b_ops);
706 pcm->private_data = chip;
707 pcm->private_free = oxygen_pcm_free;
708 strcpy(pcm->name, ins ? "Analog 2" : "Front Panel");
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100709 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
710 snd_dma_pci_data(chip->pci),
711 128 * 1024, 256 * 1024);
712 }
713 return 0;
714}