blob: 09a16e459de991cec88675e18afb0add98847312 [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 Ladischd55d7a12008-05-13 09:25:39 +020027/* most DMA channels have a 16-bit counter for 32-bit words */
28#define BUFFER_BYTES_MAX ((1 << 16) * 4)
29/* the multichannel DMA channel has a 24-bit counter */
30#define BUFFER_BYTES_MAX_MULTICH ((1 << 24) * 4)
31
32#define PERIOD_BYTES_MIN 64
33
34#define DEFAULT_BUFFER_BYTES (BUFFER_BYTES_MAX / 2)
35#define DEFAULT_BUFFER_BYTES_MULTICH (1024 * 1024)
36
Clemens Ladischc57cccc2008-01-21 08:54:06 +010037static const struct snd_pcm_hardware oxygen_stereo_hardware = {
38 .info = SNDRV_PCM_INFO_MMAP |
39 SNDRV_PCM_INFO_MMAP_VALID |
40 SNDRV_PCM_INFO_INTERLEAVED |
41 SNDRV_PCM_INFO_PAUSE |
42 SNDRV_PCM_INFO_SYNC_START,
43 .formats = SNDRV_PCM_FMTBIT_S16_LE |
44 SNDRV_PCM_FMTBIT_S32_LE,
45 .rates = SNDRV_PCM_RATE_32000 |
46 SNDRV_PCM_RATE_44100 |
47 SNDRV_PCM_RATE_48000 |
48 SNDRV_PCM_RATE_64000 |
49 SNDRV_PCM_RATE_88200 |
50 SNDRV_PCM_RATE_96000 |
51 SNDRV_PCM_RATE_176400 |
52 SNDRV_PCM_RATE_192000,
53 .rate_min = 32000,
54 .rate_max = 192000,
55 .channels_min = 2,
56 .channels_max = 2,
Clemens Ladischd55d7a12008-05-13 09:25:39 +020057 .buffer_bytes_max = BUFFER_BYTES_MAX,
58 .period_bytes_min = PERIOD_BYTES_MIN,
59 .period_bytes_max = BUFFER_BYTES_MAX / 2,
Clemens Ladischc57cccc2008-01-21 08:54:06 +010060 .periods_min = 2,
Clemens Ladischd55d7a12008-05-13 09:25:39 +020061 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
Clemens Ladischc57cccc2008-01-21 08:54:06 +010062};
63static const struct snd_pcm_hardware oxygen_multichannel_hardware = {
64 .info = SNDRV_PCM_INFO_MMAP |
65 SNDRV_PCM_INFO_MMAP_VALID |
66 SNDRV_PCM_INFO_INTERLEAVED |
67 SNDRV_PCM_INFO_PAUSE |
68 SNDRV_PCM_INFO_SYNC_START,
69 .formats = SNDRV_PCM_FMTBIT_S16_LE |
70 SNDRV_PCM_FMTBIT_S32_LE,
71 .rates = SNDRV_PCM_RATE_32000 |
72 SNDRV_PCM_RATE_44100 |
73 SNDRV_PCM_RATE_48000 |
74 SNDRV_PCM_RATE_64000 |
75 SNDRV_PCM_RATE_88200 |
76 SNDRV_PCM_RATE_96000 |
77 SNDRV_PCM_RATE_176400 |
78 SNDRV_PCM_RATE_192000,
79 .rate_min = 32000,
80 .rate_max = 192000,
81 .channels_min = 2,
82 .channels_max = 8,
Clemens Ladischd55d7a12008-05-13 09:25:39 +020083 .buffer_bytes_max = BUFFER_BYTES_MAX_MULTICH,
84 .period_bytes_min = PERIOD_BYTES_MIN,
85 .period_bytes_max = BUFFER_BYTES_MAX_MULTICH / 2,
Clemens Ladischc57cccc2008-01-21 08:54:06 +010086 .periods_min = 2,
Clemens Ladischd55d7a12008-05-13 09:25:39 +020087 .periods_max = BUFFER_BYTES_MAX_MULTICH / PERIOD_BYTES_MIN,
Clemens Ladischc57cccc2008-01-21 08:54:06 +010088};
89static const struct snd_pcm_hardware oxygen_ac97_hardware = {
90 .info = SNDRV_PCM_INFO_MMAP |
91 SNDRV_PCM_INFO_MMAP_VALID |
92 SNDRV_PCM_INFO_INTERLEAVED |
93 SNDRV_PCM_INFO_PAUSE |
94 SNDRV_PCM_INFO_SYNC_START,
95 .formats = SNDRV_PCM_FMTBIT_S16_LE,
96 .rates = SNDRV_PCM_RATE_48000,
97 .rate_min = 48000,
98 .rate_max = 48000,
99 .channels_min = 2,
100 .channels_max = 2,
Clemens Ladischd55d7a12008-05-13 09:25:39 +0200101 .buffer_bytes_max = BUFFER_BYTES_MAX,
102 .period_bytes_min = PERIOD_BYTES_MIN,
103 .period_bytes_max = BUFFER_BYTES_MAX / 2,
Clemens Ladischc57cccc2008-01-21 08:54:06 +0100104 .periods_min = 2,
Clemens Ladischd55d7a12008-05-13 09:25:39 +0200105 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
Clemens Ladischc57cccc2008-01-21 08:54:06 +0100106};
107
108static const struct snd_pcm_hardware *const oxygen_hardware[PCM_COUNT] = {
109 [PCM_A] = &oxygen_stereo_hardware,
110 [PCM_B] = &oxygen_stereo_hardware,
111 [PCM_C] = &oxygen_stereo_hardware,
112 [PCM_SPDIF] = &oxygen_stereo_hardware,
113 [PCM_MULTICH] = &oxygen_multichannel_hardware,
114 [PCM_AC97] = &oxygen_ac97_hardware,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100115};
116
Clemens Ladisch740eb832008-01-04 09:22:20 +0100117static inline unsigned int
118oxygen_substream_channel(struct snd_pcm_substream *substream)
119{
120 return (unsigned int)(uintptr_t)substream->runtime->private_data;
121}
122
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100123static int oxygen_open(struct snd_pcm_substream *substream,
124 unsigned int channel)
125{
126 struct oxygen *chip = snd_pcm_substream_chip(substream);
127 struct snd_pcm_runtime *runtime = substream->runtime;
128 int err;
129
Clemens Ladisch740eb832008-01-04 09:22:20 +0100130 runtime->private_data = (void *)(uintptr_t)channel;
Clemens Ladisch5f7b9b42008-01-28 08:35:47 +0100131 if (channel == PCM_B && chip->has_ac97_1 &&
Clemens Ladischf009ad92008-03-19 08:19:41 +0100132 (chip->model->pcm_dev_cfg & CAPTURE_2_FROM_AC97_1))
Clemens Ladisch5f7b9b42008-01-28 08:35:47 +0100133 runtime->hw = oxygen_ac97_hardware;
134 else
135 runtime->hw = *oxygen_hardware[channel];
Clemens Ladisch976cd622008-01-25 08:37:49 +0100136 switch (channel) {
137 case PCM_C:
Clemens Ladisch33c646e2008-01-24 08:43:16 +0100138 runtime->hw.rates &= ~(SNDRV_PCM_RATE_32000 |
139 SNDRV_PCM_RATE_64000);
140 runtime->hw.rate_min = 44100;
Clemens Ladisch976cd622008-01-25 08:37:49 +0100141 break;
142 case PCM_MULTICH:
143 runtime->hw.channels_max = chip->model->dac_channels;
144 break;
Clemens Ladisch33c646e2008-01-24 08:43:16 +0100145 }
Clemens Ladisch747c6012008-01-16 08:32:53 +0100146 if (chip->model->pcm_hardware_filter)
147 chip->model->pcm_hardware_filter(channel, &runtime->hw);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100148 err = snd_pcm_hw_constraint_step(runtime, 0,
149 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
150 if (err < 0)
151 return err;
152 err = snd_pcm_hw_constraint_step(runtime, 0,
153 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
154 if (err < 0)
155 return err;
156 if (runtime->hw.formats & SNDRV_PCM_FMTBIT_S32_LE) {
157 err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
158 if (err < 0)
159 return err;
160 }
161 if (runtime->hw.channels_max > 2) {
162 err = snd_pcm_hw_constraint_step(runtime, 0,
163 SNDRV_PCM_HW_PARAM_CHANNELS,
164 2);
165 if (err < 0)
166 return err;
167 }
168 snd_pcm_set_sync(substream);
169 chip->streams[channel] = substream;
170
171 mutex_lock(&chip->mutex);
172 chip->pcm_active |= 1 << channel;
173 if (channel == PCM_SPDIF) {
174 chip->spdif_pcm_bits = chip->spdif_bits;
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100175 chip->controls[CONTROL_SPDIF_PCM]->vd[0].access &=
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100176 ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
177 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
178 SNDRV_CTL_EVENT_MASK_INFO,
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100179 &chip->controls[CONTROL_SPDIF_PCM]->id);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100180 }
181 mutex_unlock(&chip->mutex);
182
183 return 0;
184}
185
186static int oxygen_rec_a_open(struct snd_pcm_substream *substream)
187{
188 return oxygen_open(substream, PCM_A);
189}
190
191static int oxygen_rec_b_open(struct snd_pcm_substream *substream)
192{
193 return oxygen_open(substream, PCM_B);
194}
195
196static int oxygen_rec_c_open(struct snd_pcm_substream *substream)
197{
198 return oxygen_open(substream, PCM_C);
199}
200
201static int oxygen_spdif_open(struct snd_pcm_substream *substream)
202{
203 return oxygen_open(substream, PCM_SPDIF);
204}
205
206static int oxygen_multich_open(struct snd_pcm_substream *substream)
207{
208 return oxygen_open(substream, PCM_MULTICH);
209}
210
211static int oxygen_ac97_open(struct snd_pcm_substream *substream)
212{
213 return oxygen_open(substream, PCM_AC97);
214}
215
216static int oxygen_close(struct snd_pcm_substream *substream)
217{
218 struct oxygen *chip = snd_pcm_substream_chip(substream);
Clemens Ladisch740eb832008-01-04 09:22:20 +0100219 unsigned int channel = oxygen_substream_channel(substream);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100220
221 mutex_lock(&chip->mutex);
222 chip->pcm_active &= ~(1 << channel);
223 if (channel == PCM_SPDIF) {
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100224 chip->controls[CONTROL_SPDIF_PCM]->vd[0].access |=
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100225 SNDRV_CTL_ELEM_ACCESS_INACTIVE;
226 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
227 SNDRV_CTL_EVENT_MASK_INFO,
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100228 &chip->controls[CONTROL_SPDIF_PCM]->id);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100229 }
230 if (channel == PCM_SPDIF || channel == PCM_MULTICH)
231 oxygen_update_spdif_source(chip);
232 mutex_unlock(&chip->mutex);
233
234 chip->streams[channel] = NULL;
235 return 0;
236}
237
238static unsigned int oxygen_format(struct snd_pcm_hw_params *hw_params)
239{
240 if (params_format(hw_params) == SNDRV_PCM_FORMAT_S32_LE)
241 return OXYGEN_FORMAT_24;
242 else
243 return OXYGEN_FORMAT_16;
244}
245
246static unsigned int oxygen_rate(struct snd_pcm_hw_params *hw_params)
247{
248 switch (params_rate(hw_params)) {
249 case 32000:
250 return OXYGEN_RATE_32000;
251 case 44100:
252 return OXYGEN_RATE_44100;
253 default: /* 48000 */
254 return OXYGEN_RATE_48000;
255 case 64000:
256 return OXYGEN_RATE_64000;
257 case 88200:
258 return OXYGEN_RATE_88200;
259 case 96000:
260 return OXYGEN_RATE_96000;
261 case 176400:
262 return OXYGEN_RATE_176400;
263 case 192000:
264 return OXYGEN_RATE_192000;
265 }
266}
267
Clemens Ladischc2353a02008-01-18 09:17:53 +0100268static unsigned int oxygen_i2s_mclk(struct snd_pcm_hw_params *hw_params)
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100269{
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100270 if (params_rate(hw_params) <= 96000)
271 return OXYGEN_I2S_MCLK_256;
272 else
273 return OXYGEN_I2S_MCLK_128;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100274}
275
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100276static unsigned int oxygen_i2s_bits(struct snd_pcm_hw_params *hw_params)
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100277{
278 if (params_format(hw_params) == SNDRV_PCM_FORMAT_S32_LE)
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100279 return OXYGEN_I2S_BITS_24;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100280 else
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100281 return OXYGEN_I2S_BITS_16;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100282}
283
284static unsigned int oxygen_play_channels(struct snd_pcm_hw_params *hw_params)
285{
286 switch (params_channels(hw_params)) {
287 default: /* 2 */
288 return OXYGEN_PLAY_CHANNELS_2;
289 case 4:
290 return OXYGEN_PLAY_CHANNELS_4;
291 case 6:
292 return OXYGEN_PLAY_CHANNELS_6;
293 case 8:
294 return OXYGEN_PLAY_CHANNELS_8;
295 }
296}
297
298static const unsigned int channel_base_registers[PCM_COUNT] = {
299 [PCM_A] = OXYGEN_DMA_A_ADDRESS,
300 [PCM_B] = OXYGEN_DMA_B_ADDRESS,
301 [PCM_C] = OXYGEN_DMA_C_ADDRESS,
302 [PCM_SPDIF] = OXYGEN_DMA_SPDIF_ADDRESS,
303 [PCM_MULTICH] = OXYGEN_DMA_MULTICH_ADDRESS,
304 [PCM_AC97] = OXYGEN_DMA_AC97_ADDRESS,
305};
306
307static int oxygen_hw_params(struct snd_pcm_substream *substream,
308 struct snd_pcm_hw_params *hw_params)
309{
310 struct oxygen *chip = snd_pcm_substream_chip(substream);
Clemens Ladisch740eb832008-01-04 09:22:20 +0100311 unsigned int channel = oxygen_substream_channel(substream);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100312 int err;
313
314 err = snd_pcm_lib_malloc_pages(substream,
315 params_buffer_bytes(hw_params));
316 if (err < 0)
317 return err;
318
319 oxygen_write32(chip, channel_base_registers[channel],
320 (u32)substream->runtime->dma_addr);
321 if (channel == PCM_MULTICH) {
322 oxygen_write32(chip, OXYGEN_DMA_MULTICH_COUNT,
323 params_buffer_bytes(hw_params) / 4 - 1);
324 oxygen_write32(chip, OXYGEN_DMA_MULTICH_TCOUNT,
325 params_period_bytes(hw_params) / 4 - 1);
326 } else {
327 oxygen_write16(chip, channel_base_registers[channel] + 4,
328 params_buffer_bytes(hw_params) / 4 - 1);
329 oxygen_write16(chip, channel_base_registers[channel] + 6,
330 params_period_bytes(hw_params) / 4 - 1);
331 }
332 return 0;
333}
334
335static int oxygen_rec_a_hw_params(struct snd_pcm_substream *substream,
336 struct snd_pcm_hw_params *hw_params)
337{
338 struct oxygen *chip = snd_pcm_substream_chip(substream);
339 int err;
340
341 err = oxygen_hw_params(substream, hw_params);
342 if (err < 0)
343 return err;
344
345 spin_lock_irq(&chip->reg_lock);
346 oxygen_write8_masked(chip, OXYGEN_REC_FORMAT,
347 oxygen_format(hw_params) << OXYGEN_REC_FORMAT_A_SHIFT,
348 OXYGEN_REC_FORMAT_A_MASK);
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100349 oxygen_write16_masked(chip, OXYGEN_I2S_A_FORMAT,
350 oxygen_rate(hw_params) |
Clemens Ladischc2353a02008-01-18 09:17:53 +0100351 oxygen_i2s_mclk(hw_params) |
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100352 chip->model->adc_i2s_format |
353 oxygen_i2s_bits(hw_params),
354 OXYGEN_I2S_RATE_MASK |
355 OXYGEN_I2S_FORMAT_MASK |
Clemens Ladischc2353a02008-01-18 09:17:53 +0100356 OXYGEN_I2S_MCLK_MASK |
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100357 OXYGEN_I2S_BITS_MASK);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100358 spin_unlock_irq(&chip->reg_lock);
359
360 mutex_lock(&chip->mutex);
361 chip->model->set_adc_params(chip, hw_params);
362 mutex_unlock(&chip->mutex);
363 return 0;
364}
365
366static int oxygen_rec_b_hw_params(struct snd_pcm_substream *substream,
367 struct snd_pcm_hw_params *hw_params)
368{
369 struct oxygen *chip = snd_pcm_substream_chip(substream);
Clemens Ladisch5f7b9b42008-01-28 08:35:47 +0100370 int is_ac97;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100371 int err;
372
373 err = oxygen_hw_params(substream, hw_params);
374 if (err < 0)
375 return err;
376
Clemens Ladisch5f7b9b42008-01-28 08:35:47 +0100377 is_ac97 = chip->has_ac97_1 &&
Clemens Ladischf009ad92008-03-19 08:19:41 +0100378 (chip->model->pcm_dev_cfg & CAPTURE_2_FROM_AC97_1);
Clemens Ladisch5f7b9b42008-01-28 08:35:47 +0100379
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100380 spin_lock_irq(&chip->reg_lock);
381 oxygen_write8_masked(chip, OXYGEN_REC_FORMAT,
382 oxygen_format(hw_params) << OXYGEN_REC_FORMAT_B_SHIFT,
383 OXYGEN_REC_FORMAT_B_MASK);
Clemens Ladisch5f7b9b42008-01-28 08:35:47 +0100384 if (!is_ac97)
385 oxygen_write16_masked(chip, OXYGEN_I2S_B_FORMAT,
386 oxygen_rate(hw_params) |
387 oxygen_i2s_mclk(hw_params) |
388 chip->model->adc_i2s_format |
389 oxygen_i2s_bits(hw_params),
390 OXYGEN_I2S_RATE_MASK |
391 OXYGEN_I2S_FORMAT_MASK |
392 OXYGEN_I2S_MCLK_MASK |
393 OXYGEN_I2S_BITS_MASK);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100394 spin_unlock_irq(&chip->reg_lock);
395
Clemens Ladisch5f7b9b42008-01-28 08:35:47 +0100396 if (!is_ac97) {
397 mutex_lock(&chip->mutex);
398 chip->model->set_adc_params(chip, hw_params);
399 mutex_unlock(&chip->mutex);
400 }
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100401 return 0;
402}
403
404static int oxygen_rec_c_hw_params(struct snd_pcm_substream *substream,
405 struct snd_pcm_hw_params *hw_params)
406{
407 struct oxygen *chip = snd_pcm_substream_chip(substream);
408 int err;
409
410 err = oxygen_hw_params(substream, hw_params);
411 if (err < 0)
412 return err;
413
414 spin_lock_irq(&chip->reg_lock);
415 oxygen_write8_masked(chip, OXYGEN_REC_FORMAT,
416 oxygen_format(hw_params) << OXYGEN_REC_FORMAT_C_SHIFT,
417 OXYGEN_REC_FORMAT_C_MASK);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100418 spin_unlock_irq(&chip->reg_lock);
419 return 0;
420}
421
422static int oxygen_spdif_hw_params(struct snd_pcm_substream *substream,
423 struct snd_pcm_hw_params *hw_params)
424{
425 struct oxygen *chip = snd_pcm_substream_chip(substream);
426 int err;
427
428 err = oxygen_hw_params(substream, hw_params);
429 if (err < 0)
430 return err;
431
432 spin_lock_irq(&chip->reg_lock);
433 oxygen_clear_bits32(chip, OXYGEN_SPDIF_CONTROL,
434 OXYGEN_SPDIF_OUT_ENABLE);
435 oxygen_write8_masked(chip, OXYGEN_PLAY_FORMAT,
436 oxygen_format(hw_params) << OXYGEN_SPDIF_FORMAT_SHIFT,
437 OXYGEN_SPDIF_FORMAT_MASK);
438 oxygen_write32_masked(chip, OXYGEN_SPDIF_CONTROL,
439 oxygen_rate(hw_params) << OXYGEN_SPDIF_OUT_RATE_SHIFT,
440 OXYGEN_SPDIF_OUT_RATE_MASK);
441 oxygen_update_spdif_source(chip);
442 spin_unlock_irq(&chip->reg_lock);
443 return 0;
444}
445
446static int oxygen_multich_hw_params(struct snd_pcm_substream *substream,
447 struct snd_pcm_hw_params *hw_params)
448{
449 struct oxygen *chip = snd_pcm_substream_chip(substream);
450 int err;
451
452 err = oxygen_hw_params(substream, hw_params);
453 if (err < 0)
454 return err;
455
456 spin_lock_irq(&chip->reg_lock);
457 oxygen_write8_masked(chip, OXYGEN_PLAY_CHANNELS,
458 oxygen_play_channels(hw_params),
459 OXYGEN_PLAY_CHANNELS_MASK);
460 oxygen_write8_masked(chip, OXYGEN_PLAY_FORMAT,
461 oxygen_format(hw_params) << OXYGEN_MULTICH_FORMAT_SHIFT,
462 OXYGEN_MULTICH_FORMAT_MASK);
463 oxygen_write16_masked(chip, OXYGEN_I2S_MULTICH_FORMAT,
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100464 oxygen_rate(hw_params) |
465 chip->model->dac_i2s_format |
466 oxygen_i2s_bits(hw_params),
467 OXYGEN_I2S_RATE_MASK |
468 OXYGEN_I2S_FORMAT_MASK |
469 OXYGEN_I2S_BITS_MASK);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100470 oxygen_update_dac_routing(chip);
471 oxygen_update_spdif_source(chip);
472 spin_unlock_irq(&chip->reg_lock);
473
474 mutex_lock(&chip->mutex);
475 chip->model->set_dac_params(chip, hw_params);
476 mutex_unlock(&chip->mutex);
477 return 0;
478}
479
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100480static int oxygen_hw_free(struct snd_pcm_substream *substream)
481{
482 struct oxygen *chip = snd_pcm_substream_chip(substream);
Clemens Ladisch740eb832008-01-04 09:22:20 +0100483 unsigned int channel = oxygen_substream_channel(substream);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100484
485 spin_lock_irq(&chip->reg_lock);
486 chip->interrupt_mask &= ~(1 << channel);
487 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, chip->interrupt_mask);
488 spin_unlock_irq(&chip->reg_lock);
489
490 return snd_pcm_lib_free_pages(substream);
491}
492
493static int oxygen_spdif_hw_free(struct snd_pcm_substream *substream)
494{
495 struct oxygen *chip = snd_pcm_substream_chip(substream);
496
497 spin_lock_irq(&chip->reg_lock);
498 oxygen_clear_bits32(chip, OXYGEN_SPDIF_CONTROL,
499 OXYGEN_SPDIF_OUT_ENABLE);
500 spin_unlock_irq(&chip->reg_lock);
501 return oxygen_hw_free(substream);
502}
503
504static int oxygen_prepare(struct snd_pcm_substream *substream)
505{
506 struct oxygen *chip = snd_pcm_substream_chip(substream);
Clemens Ladisch740eb832008-01-04 09:22:20 +0100507 unsigned int channel = oxygen_substream_channel(substream);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100508 unsigned int channel_mask = 1 << channel;
509
510 spin_lock_irq(&chip->reg_lock);
511 oxygen_set_bits8(chip, OXYGEN_DMA_FLUSH, channel_mask);
512 oxygen_clear_bits8(chip, OXYGEN_DMA_FLUSH, channel_mask);
513
514 chip->interrupt_mask |= channel_mask;
515 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, chip->interrupt_mask);
516 spin_unlock_irq(&chip->reg_lock);
517 return 0;
518}
519
520static int oxygen_trigger(struct snd_pcm_substream *substream, int cmd)
521{
522 struct oxygen *chip = snd_pcm_substream_chip(substream);
523 struct snd_pcm_substream *s;
524 unsigned int mask = 0;
Clemens Ladischdb2396d2008-01-21 08:44:52 +0100525 int pausing;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100526
527 switch (cmd) {
528 case SNDRV_PCM_TRIGGER_STOP:
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100529 case SNDRV_PCM_TRIGGER_START:
Clemens Ladisch4a4bc532008-05-13 09:24:39 +0200530 case SNDRV_PCM_TRIGGER_SUSPEND:
Clemens Ladischdb2396d2008-01-21 08:44:52 +0100531 pausing = 0;
532 break;
533 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100534 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Clemens Ladischdb2396d2008-01-21 08:44:52 +0100535 pausing = 1;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100536 break;
537 default:
538 return -EINVAL;
539 }
540
541 snd_pcm_group_for_each_entry(s, substream) {
542 if (snd_pcm_substream_chip(s) == chip) {
Clemens Ladisch740eb832008-01-04 09:22:20 +0100543 mask |= 1 << oxygen_substream_channel(s);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100544 snd_pcm_trigger_done(s, substream);
545 }
546 }
547
548 spin_lock(&chip->reg_lock);
Clemens Ladischdb2396d2008-01-21 08:44:52 +0100549 if (!pausing) {
550 if (cmd == SNDRV_PCM_TRIGGER_START)
551 chip->pcm_running |= mask;
552 else
553 chip->pcm_running &= ~mask;
554 oxygen_write8(chip, OXYGEN_DMA_STATUS, chip->pcm_running);
555 } else {
556 if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH)
557 oxygen_set_bits8(chip, OXYGEN_DMA_PAUSE, mask);
558 else
559 oxygen_clear_bits8(chip, OXYGEN_DMA_PAUSE, mask);
560 }
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100561 spin_unlock(&chip->reg_lock);
562 return 0;
563}
564
565static snd_pcm_uframes_t oxygen_pointer(struct snd_pcm_substream *substream)
566{
567 struct oxygen *chip = snd_pcm_substream_chip(substream);
568 struct snd_pcm_runtime *runtime = substream->runtime;
Clemens Ladisch740eb832008-01-04 09:22:20 +0100569 unsigned int channel = oxygen_substream_channel(substream);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100570 u32 curr_addr;
571
572 /* no spinlock, this read should be atomic */
573 curr_addr = oxygen_read32(chip, channel_base_registers[channel]);
574 return bytes_to_frames(runtime, curr_addr - (u32)runtime->dma_addr);
575}
576
577static struct snd_pcm_ops oxygen_rec_a_ops = {
578 .open = oxygen_rec_a_open,
579 .close = oxygen_close,
580 .ioctl = snd_pcm_lib_ioctl,
581 .hw_params = oxygen_rec_a_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_rec_b_ops = {
589 .open = oxygen_rec_b_open,
590 .close = oxygen_close,
591 .ioctl = snd_pcm_lib_ioctl,
592 .hw_params = oxygen_rec_b_hw_params,
593 .hw_free = oxygen_hw_free,
594 .prepare = oxygen_prepare,
595 .trigger = oxygen_trigger,
596 .pointer = oxygen_pointer,
597};
598
599static struct snd_pcm_ops oxygen_rec_c_ops = {
600 .open = oxygen_rec_c_open,
601 .close = oxygen_close,
602 .ioctl = snd_pcm_lib_ioctl,
603 .hw_params = oxygen_rec_c_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_spdif_ops = {
611 .open = oxygen_spdif_open,
612 .close = oxygen_close,
613 .ioctl = snd_pcm_lib_ioctl,
614 .hw_params = oxygen_spdif_hw_params,
615 .hw_free = oxygen_spdif_hw_free,
616 .prepare = oxygen_prepare,
617 .trigger = oxygen_trigger,
618 .pointer = oxygen_pointer,
619};
620
621static struct snd_pcm_ops oxygen_multich_ops = {
622 .open = oxygen_multich_open,
623 .close = oxygen_close,
624 .ioctl = snd_pcm_lib_ioctl,
625 .hw_params = oxygen_multich_hw_params,
626 .hw_free = oxygen_hw_free,
627 .prepare = oxygen_prepare,
628 .trigger = oxygen_trigger,
629 .pointer = oxygen_pointer,
630};
631
632static struct snd_pcm_ops oxygen_ac97_ops = {
633 .open = oxygen_ac97_open,
634 .close = oxygen_close,
635 .ioctl = snd_pcm_lib_ioctl,
Clemens Ladischc2353a02008-01-18 09:17:53 +0100636 .hw_params = oxygen_hw_params,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100637 .hw_free = oxygen_hw_free,
638 .prepare = oxygen_prepare,
639 .trigger = oxygen_trigger,
640 .pointer = oxygen_pointer,
641};
642
643static void oxygen_pcm_free(struct snd_pcm *pcm)
644{
645 snd_pcm_lib_preallocate_free_for_all(pcm);
646}
647
Takashi Iwaif007dc02008-02-22 18:35:22 +0100648int oxygen_pcm_init(struct oxygen *chip)
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100649{
650 struct snd_pcm *pcm;
Clemens Ladische85e0922008-01-16 08:30:38 +0100651 int outs, ins;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100652 int err;
653
Clemens Ladischf009ad92008-03-19 08:19:41 +0100654 outs = !!(chip->model->pcm_dev_cfg & PLAYBACK_0_TO_I2S);
655 ins = !!(chip->model->pcm_dev_cfg & (CAPTURE_0_FROM_I2S_1 |
656 CAPTURE_0_FROM_I2S_2));
657 if (outs | ins) {
658 err = snd_pcm_new(chip->card, "Analog", 0, outs, ins, &pcm);
659 if (err < 0)
660 return err;
661 if (outs)
662 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
663 &oxygen_multich_ops);
664 if (chip->model->pcm_dev_cfg & CAPTURE_0_FROM_I2S_1)
665 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
666 &oxygen_rec_a_ops);
667 else if (chip->model->pcm_dev_cfg & CAPTURE_0_FROM_I2S_2)
668 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
669 &oxygen_rec_b_ops);
670 pcm->private_data = chip;
671 pcm->private_free = oxygen_pcm_free;
672 strcpy(pcm->name, "Analog");
673 if (outs)
674 snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream,
675 SNDRV_DMA_TYPE_DEV,
676 snd_dma_pci_data(chip->pci),
Clemens Ladischd55d7a12008-05-13 09:25:39 +0200677 DEFAULT_BUFFER_BYTES_MULTICH,
678 BUFFER_BYTES_MAX_MULTICH);
Clemens Ladischf009ad92008-03-19 08:19:41 +0100679 if (ins)
680 snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
681 SNDRV_DMA_TYPE_DEV,
682 snd_dma_pci_data(chip->pci),
Clemens Ladischd55d7a12008-05-13 09:25:39 +0200683 DEFAULT_BUFFER_BYTES,
684 BUFFER_BYTES_MAX);
Clemens Ladischf009ad92008-03-19 08:19:41 +0100685 }
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100686
Clemens Ladischf009ad92008-03-19 08:19:41 +0100687 outs = !!(chip->model->pcm_dev_cfg & PLAYBACK_1_TO_SPDIF);
688 ins = !!(chip->model->pcm_dev_cfg & CAPTURE_1_FROM_SPDIF);
Clemens Ladische85e0922008-01-16 08:30:38 +0100689 if (outs | ins) {
690 err = snd_pcm_new(chip->card, "Digital", 1, outs, ins, &pcm);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100691 if (err < 0)
692 return err;
Clemens Ladische85e0922008-01-16 08:30:38 +0100693 if (outs)
694 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
695 &oxygen_spdif_ops);
696 if (ins)
697 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
698 &oxygen_rec_c_ops);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100699 pcm->private_data = chip;
700 pcm->private_free = oxygen_pcm_free;
Clemens Ladische85e0922008-01-16 08:30:38 +0100701 strcpy(pcm->name, "Digital");
702 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
703 snd_dma_pci_data(chip->pci),
Clemens Ladischd55d7a12008-05-13 09:25:39 +0200704 DEFAULT_BUFFER_BYTES,
705 BUFFER_BYTES_MAX);
Clemens Ladische85e0922008-01-16 08:30:38 +0100706 }
707
Clemens Ladischf009ad92008-03-19 08:19:41 +0100708 if (chip->has_ac97_1) {
709 outs = !!(chip->model->pcm_dev_cfg & PLAYBACK_2_TO_AC97_1);
710 ins = !!(chip->model->pcm_dev_cfg & CAPTURE_2_FROM_AC97_1);
711 } else {
712 outs = 0;
713 ins = !!(chip->model->pcm_dev_cfg & CAPTURE_2_FROM_I2S_2);
714 }
Clemens Ladische85e0922008-01-16 08:30:38 +0100715 if (outs | ins) {
Clemens Ladisch5f7b9b42008-01-28 08:35:47 +0100716 err = snd_pcm_new(chip->card, outs ? "AC97" : "Analog2",
Clemens Ladische85e0922008-01-16 08:30:38 +0100717 2, outs, ins, &pcm);
718 if (err < 0)
719 return err;
Clemens Ladisch5f7b9b42008-01-28 08:35:47 +0100720 if (outs) {
Clemens Ladische85e0922008-01-16 08:30:38 +0100721 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
722 &oxygen_ac97_ops);
Clemens Ladisch5f7b9b42008-01-28 08:35:47 +0100723 oxygen_write8_masked(chip, OXYGEN_REC_ROUTING,
724 OXYGEN_REC_B_ROUTE_AC97_1,
725 OXYGEN_REC_B_ROUTE_MASK);
726 }
Clemens Ladische85e0922008-01-16 08:30:38 +0100727 if (ins)
728 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
729 &oxygen_rec_b_ops);
730 pcm->private_data = chip;
731 pcm->private_free = oxygen_pcm_free;
Clemens Ladisch5f7b9b42008-01-28 08:35:47 +0100732 strcpy(pcm->name, outs ? "Front Panel" : "Analog 2");
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100733 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
734 snd_dma_pci_data(chip->pci),
Clemens Ladischd55d7a12008-05-13 09:25:39 +0200735 DEFAULT_BUFFER_BYTES,
736 BUFFER_BYTES_MAX);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100737 }
738 return 0;
739}