blob: f147f97bc696a69234d80e910096823aa4f9ff57 [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
27static struct snd_pcm_hardware oxygen_hardware[PCM_COUNT] = {
28 [PCM_A] = {
29 .info = SNDRV_PCM_INFO_MMAP |
30 SNDRV_PCM_INFO_MMAP_VALID |
31 SNDRV_PCM_INFO_INTERLEAVED |
32 SNDRV_PCM_INFO_PAUSE |
33 SNDRV_PCM_INFO_SYNC_START,
34 .formats = SNDRV_PCM_FMTBIT_S16_LE |
35 SNDRV_PCM_FMTBIT_S32_LE,
Clemens Ladisch747c6012008-01-16 08:32:53 +010036 .rates = SNDRV_PCM_RATE_32000 |
37 SNDRV_PCM_RATE_44100 |
Clemens Ladischd0ce9942007-12-23 19:50:57 +010038 SNDRV_PCM_RATE_48000 |
Clemens Ladisch747c6012008-01-16 08:32:53 +010039 SNDRV_PCM_RATE_64000 |
40 SNDRV_PCM_RATE_88200 |
Clemens Ladischd0ce9942007-12-23 19:50:57 +010041 SNDRV_PCM_RATE_96000 |
Clemens Ladisch747c6012008-01-16 08:32:53 +010042 SNDRV_PCM_RATE_176400 |
Clemens Ladischd0ce9942007-12-23 19:50:57 +010043 SNDRV_PCM_RATE_192000,
Clemens Ladisch747c6012008-01-16 08:32:53 +010044 .rate_min = 32000,
Clemens Ladischd0ce9942007-12-23 19:50:57 +010045 .rate_max = 192000,
46 .channels_min = 2,
47 .channels_max = 2,
48 .buffer_bytes_max = 256 * 1024,
49 .period_bytes_min = 128,
50 .period_bytes_max = 128 * 1024,
51 .periods_min = 2,
52 .periods_max = 2048,
53 },
54 [PCM_B] = {
55 .info = SNDRV_PCM_INFO_MMAP |
56 SNDRV_PCM_INFO_MMAP_VALID |
57 SNDRV_PCM_INFO_INTERLEAVED |
58 SNDRV_PCM_INFO_PAUSE |
59 SNDRV_PCM_INFO_SYNC_START,
60 .formats = SNDRV_PCM_FMTBIT_S16_LE |
61 SNDRV_PCM_FMTBIT_S32_LE,
62 .rates = SNDRV_PCM_RATE_32000 |
63 SNDRV_PCM_RATE_44100 |
64 SNDRV_PCM_RATE_48000 |
65 SNDRV_PCM_RATE_64000 |
66 SNDRV_PCM_RATE_88200 |
67 SNDRV_PCM_RATE_96000 |
68 SNDRV_PCM_RATE_176400 |
69 SNDRV_PCM_RATE_192000,
70 .rate_min = 32000,
71 .rate_max = 192000,
72 .channels_min = 2,
73 .channels_max = 2,
74 .buffer_bytes_max = 256 * 1024,
75 .period_bytes_min = 128,
76 .period_bytes_max = 128 * 1024,
77 .periods_min = 2,
78 .periods_max = 2048,
79 },
80 [PCM_C] = {
81 .info = SNDRV_PCM_INFO_MMAP |
82 SNDRV_PCM_INFO_MMAP_VALID |
83 SNDRV_PCM_INFO_INTERLEAVED |
84 SNDRV_PCM_INFO_PAUSE |
85 SNDRV_PCM_INFO_SYNC_START,
86 .formats = SNDRV_PCM_FMTBIT_S16_LE |
87 SNDRV_PCM_FMTBIT_S32_LE,
88 .rates = SNDRV_PCM_RATE_44100 |
89 SNDRV_PCM_RATE_48000 |
90 SNDRV_PCM_RATE_88200 |
91 SNDRV_PCM_RATE_96000,
92 .rate_min = 44100,
93 .rate_max = 96000,
94 .channels_min = 2,
95 .channels_max = 2,
96 .buffer_bytes_max = 256 * 1024,
97 .period_bytes_min = 128,
98 .period_bytes_max = 128 * 1024,
99 .periods_min = 2,
100 .periods_max = 2048,
101 },
102 [PCM_SPDIF] = {
103 .info = SNDRV_PCM_INFO_MMAP |
104 SNDRV_PCM_INFO_MMAP_VALID |
105 SNDRV_PCM_INFO_INTERLEAVED |
106 SNDRV_PCM_INFO_PAUSE |
107 SNDRV_PCM_INFO_SYNC_START,
108 .formats = SNDRV_PCM_FMTBIT_S16_LE |
109 SNDRV_PCM_FMTBIT_S32_LE,
110 .rates = SNDRV_PCM_RATE_32000 |
111 SNDRV_PCM_RATE_44100 |
112 SNDRV_PCM_RATE_48000 |
113 SNDRV_PCM_RATE_64000 |
114 SNDRV_PCM_RATE_88200 |
115 SNDRV_PCM_RATE_96000 |
116 SNDRV_PCM_RATE_176400 |
117 SNDRV_PCM_RATE_192000,
118 .rate_min = 32000,
119 .rate_max = 192000,
120 .channels_min = 2,
121 .channels_max = 2,
122 .buffer_bytes_max = 256 * 1024,
123 .period_bytes_min = 128,
124 .period_bytes_max = 128 * 1024,
125 .periods_min = 2,
126 .periods_max = 2048,
127 },
128 [PCM_MULTICH] = {
129 .info = SNDRV_PCM_INFO_MMAP |
130 SNDRV_PCM_INFO_MMAP_VALID |
131 SNDRV_PCM_INFO_INTERLEAVED |
132 SNDRV_PCM_INFO_PAUSE |
133 SNDRV_PCM_INFO_SYNC_START,
134 .formats = SNDRV_PCM_FMTBIT_S16_LE |
135 SNDRV_PCM_FMTBIT_S32_LE,
136 .rates = SNDRV_PCM_RATE_32000 |
137 SNDRV_PCM_RATE_44100 |
138 SNDRV_PCM_RATE_48000 |
139 SNDRV_PCM_RATE_64000 |
140 SNDRV_PCM_RATE_88200 |
141 SNDRV_PCM_RATE_96000 |
142 SNDRV_PCM_RATE_176400 |
143 SNDRV_PCM_RATE_192000,
144 .rate_min = 32000,
145 .rate_max = 192000,
146 .channels_min = 2,
147 .channels_max = 8,
148 .buffer_bytes_max = 2048 * 1024,
149 .period_bytes_min = 128,
150 .period_bytes_max = 256 * 1024,
151 .periods_min = 2,
152 .periods_max = 16384,
153 },
154 [PCM_AC97] = {
155 .info = SNDRV_PCM_INFO_MMAP |
156 SNDRV_PCM_INFO_MMAP_VALID |
157 SNDRV_PCM_INFO_INTERLEAVED |
158 SNDRV_PCM_INFO_PAUSE |
159 SNDRV_PCM_INFO_SYNC_START,
160 .formats = SNDRV_PCM_FMTBIT_S16_LE,
161 .rates = SNDRV_PCM_RATE_48000,
162 .rate_min = 48000,
163 .rate_max = 48000,
164 .channels_min = 2,
165 .channels_max = 2,
166 .buffer_bytes_max = 256 * 1024,
167 .period_bytes_min = 128,
168 .period_bytes_max = 128 * 1024,
169 .periods_min = 2,
170 .periods_max = 2048,
171 },
172};
173
Clemens Ladisch740eb832008-01-04 09:22:20 +0100174static inline unsigned int
175oxygen_substream_channel(struct snd_pcm_substream *substream)
176{
177 return (unsigned int)(uintptr_t)substream->runtime->private_data;
178}
179
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100180static int oxygen_open(struct snd_pcm_substream *substream,
181 unsigned int channel)
182{
183 struct oxygen *chip = snd_pcm_substream_chip(substream);
184 struct snd_pcm_runtime *runtime = substream->runtime;
185 int err;
186
Clemens Ladisch740eb832008-01-04 09:22:20 +0100187 runtime->private_data = (void *)(uintptr_t)channel;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100188 runtime->hw = oxygen_hardware[channel];
Clemens Ladisch747c6012008-01-16 08:32:53 +0100189 if (chip->model->pcm_hardware_filter)
190 chip->model->pcm_hardware_filter(channel, &runtime->hw);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100191 err = snd_pcm_hw_constraint_step(runtime, 0,
192 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
193 if (err < 0)
194 return err;
195 err = snd_pcm_hw_constraint_step(runtime, 0,
196 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
197 if (err < 0)
198 return err;
199 if (runtime->hw.formats & SNDRV_PCM_FMTBIT_S32_LE) {
200 err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
201 if (err < 0)
202 return err;
203 }
204 if (runtime->hw.channels_max > 2) {
205 err = snd_pcm_hw_constraint_step(runtime, 0,
206 SNDRV_PCM_HW_PARAM_CHANNELS,
207 2);
208 if (err < 0)
209 return err;
210 }
211 snd_pcm_set_sync(substream);
212 chip->streams[channel] = substream;
213
214 mutex_lock(&chip->mutex);
215 chip->pcm_active |= 1 << channel;
216 if (channel == PCM_SPDIF) {
217 chip->spdif_pcm_bits = chip->spdif_bits;
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100218 chip->controls[CONTROL_SPDIF_PCM]->vd[0].access &=
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100219 ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
220 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
221 SNDRV_CTL_EVENT_MASK_INFO,
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100222 &chip->controls[CONTROL_SPDIF_PCM]->id);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100223 }
224 mutex_unlock(&chip->mutex);
225
226 return 0;
227}
228
229static int oxygen_rec_a_open(struct snd_pcm_substream *substream)
230{
231 return oxygen_open(substream, PCM_A);
232}
233
234static int oxygen_rec_b_open(struct snd_pcm_substream *substream)
235{
236 return oxygen_open(substream, PCM_B);
237}
238
239static int oxygen_rec_c_open(struct snd_pcm_substream *substream)
240{
241 return oxygen_open(substream, PCM_C);
242}
243
244static int oxygen_spdif_open(struct snd_pcm_substream *substream)
245{
246 return oxygen_open(substream, PCM_SPDIF);
247}
248
249static int oxygen_multich_open(struct snd_pcm_substream *substream)
250{
251 return oxygen_open(substream, PCM_MULTICH);
252}
253
254static int oxygen_ac97_open(struct snd_pcm_substream *substream)
255{
256 return oxygen_open(substream, PCM_AC97);
257}
258
259static int oxygen_close(struct snd_pcm_substream *substream)
260{
261 struct oxygen *chip = snd_pcm_substream_chip(substream);
Clemens Ladisch740eb832008-01-04 09:22:20 +0100262 unsigned int channel = oxygen_substream_channel(substream);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100263
264 mutex_lock(&chip->mutex);
265 chip->pcm_active &= ~(1 << channel);
266 if (channel == PCM_SPDIF) {
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100267 chip->controls[CONTROL_SPDIF_PCM]->vd[0].access |=
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100268 SNDRV_CTL_ELEM_ACCESS_INACTIVE;
269 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
270 SNDRV_CTL_EVENT_MASK_INFO,
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100271 &chip->controls[CONTROL_SPDIF_PCM]->id);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100272 }
273 if (channel == PCM_SPDIF || channel == PCM_MULTICH)
274 oxygen_update_spdif_source(chip);
275 mutex_unlock(&chip->mutex);
276
277 chip->streams[channel] = NULL;
278 return 0;
279}
280
281static unsigned int oxygen_format(struct snd_pcm_hw_params *hw_params)
282{
283 if (params_format(hw_params) == SNDRV_PCM_FORMAT_S32_LE)
284 return OXYGEN_FORMAT_24;
285 else
286 return OXYGEN_FORMAT_16;
287}
288
289static unsigned int oxygen_rate(struct snd_pcm_hw_params *hw_params)
290{
291 switch (params_rate(hw_params)) {
292 case 32000:
293 return OXYGEN_RATE_32000;
294 case 44100:
295 return OXYGEN_RATE_44100;
296 default: /* 48000 */
297 return OXYGEN_RATE_48000;
298 case 64000:
299 return OXYGEN_RATE_64000;
300 case 88200:
301 return OXYGEN_RATE_88200;
302 case 96000:
303 return OXYGEN_RATE_96000;
304 case 176400:
305 return OXYGEN_RATE_176400;
306 case 192000:
307 return OXYGEN_RATE_192000;
308 }
309}
310
Clemens Ladischc2353a02008-01-18 09:17:53 +0100311static unsigned int oxygen_i2s_mclk(struct snd_pcm_hw_params *hw_params)
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100312{
Clemens Ladischc2353a02008-01-18 09:17:53 +0100313 return params_rate(hw_params) <= 96000
314 ? OXYGEN_I2S_MCLK_256 : OXYGEN_I2S_MCLK_128;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100315}
316
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100317static unsigned int oxygen_i2s_bits(struct snd_pcm_hw_params *hw_params)
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100318{
319 if (params_format(hw_params) == SNDRV_PCM_FORMAT_S32_LE)
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100320 return OXYGEN_I2S_BITS_24;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100321 else
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100322 return OXYGEN_I2S_BITS_16;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100323}
324
325static unsigned int oxygen_play_channels(struct snd_pcm_hw_params *hw_params)
326{
327 switch (params_channels(hw_params)) {
328 default: /* 2 */
329 return OXYGEN_PLAY_CHANNELS_2;
330 case 4:
331 return OXYGEN_PLAY_CHANNELS_4;
332 case 6:
333 return OXYGEN_PLAY_CHANNELS_6;
334 case 8:
335 return OXYGEN_PLAY_CHANNELS_8;
336 }
337}
338
339static const unsigned int channel_base_registers[PCM_COUNT] = {
340 [PCM_A] = OXYGEN_DMA_A_ADDRESS,
341 [PCM_B] = OXYGEN_DMA_B_ADDRESS,
342 [PCM_C] = OXYGEN_DMA_C_ADDRESS,
343 [PCM_SPDIF] = OXYGEN_DMA_SPDIF_ADDRESS,
344 [PCM_MULTICH] = OXYGEN_DMA_MULTICH_ADDRESS,
345 [PCM_AC97] = OXYGEN_DMA_AC97_ADDRESS,
346};
347
348static int oxygen_hw_params(struct snd_pcm_substream *substream,
349 struct snd_pcm_hw_params *hw_params)
350{
351 struct oxygen *chip = snd_pcm_substream_chip(substream);
Clemens Ladisch740eb832008-01-04 09:22:20 +0100352 unsigned int channel = oxygen_substream_channel(substream);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100353 int err;
354
355 err = snd_pcm_lib_malloc_pages(substream,
356 params_buffer_bytes(hw_params));
357 if (err < 0)
358 return err;
359
360 oxygen_write32(chip, channel_base_registers[channel],
361 (u32)substream->runtime->dma_addr);
362 if (channel == PCM_MULTICH) {
363 oxygen_write32(chip, OXYGEN_DMA_MULTICH_COUNT,
364 params_buffer_bytes(hw_params) / 4 - 1);
365 oxygen_write32(chip, OXYGEN_DMA_MULTICH_TCOUNT,
366 params_period_bytes(hw_params) / 4 - 1);
367 } else {
368 oxygen_write16(chip, channel_base_registers[channel] + 4,
369 params_buffer_bytes(hw_params) / 4 - 1);
370 oxygen_write16(chip, channel_base_registers[channel] + 6,
371 params_period_bytes(hw_params) / 4 - 1);
372 }
373 return 0;
374}
375
376static int oxygen_rec_a_hw_params(struct snd_pcm_substream *substream,
377 struct snd_pcm_hw_params *hw_params)
378{
379 struct oxygen *chip = snd_pcm_substream_chip(substream);
380 int err;
381
382 err = oxygen_hw_params(substream, hw_params);
383 if (err < 0)
384 return err;
385
386 spin_lock_irq(&chip->reg_lock);
387 oxygen_write8_masked(chip, OXYGEN_REC_FORMAT,
388 oxygen_format(hw_params) << OXYGEN_REC_FORMAT_A_SHIFT,
389 OXYGEN_REC_FORMAT_A_MASK);
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100390 oxygen_write16_masked(chip, OXYGEN_I2S_A_FORMAT,
391 oxygen_rate(hw_params) |
Clemens Ladischc2353a02008-01-18 09:17:53 +0100392 oxygen_i2s_mclk(hw_params) |
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100393 chip->model->adc_i2s_format |
394 oxygen_i2s_bits(hw_params),
395 OXYGEN_I2S_RATE_MASK |
396 OXYGEN_I2S_FORMAT_MASK |
Clemens Ladischc2353a02008-01-18 09:17:53 +0100397 OXYGEN_I2S_MCLK_MASK |
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100398 OXYGEN_I2S_BITS_MASK);
Clemens Ladischc9946b22008-01-21 08:44:24 +0100399 oxygen_write8_masked(chip, OXYGEN_REC_ROUTING,
400 OXYGEN_REC_A_ROUTE_I2S_ADC_1,
401 OXYGEN_REC_A_ROUTE_MASK);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100402 spin_unlock_irq(&chip->reg_lock);
403
404 mutex_lock(&chip->mutex);
405 chip->model->set_adc_params(chip, hw_params);
406 mutex_unlock(&chip->mutex);
407 return 0;
408}
409
410static int oxygen_rec_b_hw_params(struct snd_pcm_substream *substream,
411 struct snd_pcm_hw_params *hw_params)
412{
413 struct oxygen *chip = snd_pcm_substream_chip(substream);
414 int err;
415
416 err = oxygen_hw_params(substream, hw_params);
417 if (err < 0)
418 return err;
419
420 spin_lock_irq(&chip->reg_lock);
421 oxygen_write8_masked(chip, OXYGEN_REC_FORMAT,
422 oxygen_format(hw_params) << OXYGEN_REC_FORMAT_B_SHIFT,
423 OXYGEN_REC_FORMAT_B_MASK);
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100424 oxygen_write16_masked(chip, OXYGEN_I2S_B_FORMAT,
425 oxygen_rate(hw_params) |
Clemens Ladischc2353a02008-01-18 09:17:53 +0100426 oxygen_i2s_mclk(hw_params) |
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100427 chip->model->adc_i2s_format |
428 oxygen_i2s_bits(hw_params),
429 OXYGEN_I2S_RATE_MASK |
430 OXYGEN_I2S_FORMAT_MASK |
Clemens Ladischc2353a02008-01-18 09:17:53 +0100431 OXYGEN_I2S_MCLK_MASK |
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100432 OXYGEN_I2S_BITS_MASK);
Clemens Ladischc9946b22008-01-21 08:44:24 +0100433 oxygen_write8_masked(chip, OXYGEN_REC_ROUTING,
434 OXYGEN_REC_B_ROUTE_I2S_ADC_2,
435 OXYGEN_REC_B_ROUTE_MASK);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100436 spin_unlock_irq(&chip->reg_lock);
437
438 mutex_lock(&chip->mutex);
439 chip->model->set_adc_params(chip, hw_params);
440 mutex_unlock(&chip->mutex);
441 return 0;
442}
443
444static int oxygen_rec_c_hw_params(struct snd_pcm_substream *substream,
445 struct snd_pcm_hw_params *hw_params)
446{
447 struct oxygen *chip = snd_pcm_substream_chip(substream);
448 int err;
449
450 err = oxygen_hw_params(substream, hw_params);
451 if (err < 0)
452 return err;
453
454 spin_lock_irq(&chip->reg_lock);
455 oxygen_write8_masked(chip, OXYGEN_REC_FORMAT,
456 oxygen_format(hw_params) << OXYGEN_REC_FORMAT_C_SHIFT,
457 OXYGEN_REC_FORMAT_C_MASK);
Clemens Ladischc9946b22008-01-21 08:44:24 +0100458 oxygen_write8_masked(chip, OXYGEN_REC_ROUTING,
459 OXYGEN_REC_C_ROUTE_SPDIF,
460 OXYGEN_REC_C_ROUTE_MASK);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100461 spin_unlock_irq(&chip->reg_lock);
462 return 0;
463}
464
465static int oxygen_spdif_hw_params(struct snd_pcm_substream *substream,
466 struct snd_pcm_hw_params *hw_params)
467{
468 struct oxygen *chip = snd_pcm_substream_chip(substream);
469 int err;
470
471 err = oxygen_hw_params(substream, hw_params);
472 if (err < 0)
473 return err;
474
475 spin_lock_irq(&chip->reg_lock);
476 oxygen_clear_bits32(chip, OXYGEN_SPDIF_CONTROL,
477 OXYGEN_SPDIF_OUT_ENABLE);
478 oxygen_write8_masked(chip, OXYGEN_PLAY_FORMAT,
479 oxygen_format(hw_params) << OXYGEN_SPDIF_FORMAT_SHIFT,
480 OXYGEN_SPDIF_FORMAT_MASK);
481 oxygen_write32_masked(chip, OXYGEN_SPDIF_CONTROL,
482 oxygen_rate(hw_params) << OXYGEN_SPDIF_OUT_RATE_SHIFT,
483 OXYGEN_SPDIF_OUT_RATE_MASK);
484 oxygen_update_spdif_source(chip);
485 spin_unlock_irq(&chip->reg_lock);
486 return 0;
487}
488
489static int oxygen_multich_hw_params(struct snd_pcm_substream *substream,
490 struct snd_pcm_hw_params *hw_params)
491{
492 struct oxygen *chip = snd_pcm_substream_chip(substream);
493 int err;
494
495 err = oxygen_hw_params(substream, hw_params);
496 if (err < 0)
497 return err;
498
499 spin_lock_irq(&chip->reg_lock);
500 oxygen_write8_masked(chip, OXYGEN_PLAY_CHANNELS,
501 oxygen_play_channels(hw_params),
502 OXYGEN_PLAY_CHANNELS_MASK);
503 oxygen_write8_masked(chip, OXYGEN_PLAY_FORMAT,
504 oxygen_format(hw_params) << OXYGEN_MULTICH_FORMAT_SHIFT,
505 OXYGEN_MULTICH_FORMAT_MASK);
506 oxygen_write16_masked(chip, OXYGEN_I2S_MULTICH_FORMAT,
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100507 oxygen_rate(hw_params) |
508 chip->model->dac_i2s_format |
509 oxygen_i2s_bits(hw_params),
510 OXYGEN_I2S_RATE_MASK |
511 OXYGEN_I2S_FORMAT_MASK |
512 OXYGEN_I2S_BITS_MASK);
Clemens Ladischc9946b22008-01-21 08:44:24 +0100513 oxygen_write16_masked(chip, OXYGEN_PLAY_ROUTING,
514 OXYGEN_PLAY_MULTICH_I2S_DAC,
515 OXYGEN_PLAY_MUTE01 | OXYGEN_PLAY_MUTE23 |
516 OXYGEN_PLAY_MUTE45 | OXYGEN_PLAY_MUTE67 |
517 OXYGEN_PLAY_MULTICH_MASK);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100518 oxygen_update_dac_routing(chip);
519 oxygen_update_spdif_source(chip);
520 spin_unlock_irq(&chip->reg_lock);
521
522 mutex_lock(&chip->mutex);
523 chip->model->set_dac_params(chip, hw_params);
524 mutex_unlock(&chip->mutex);
525 return 0;
526}
527
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100528static int oxygen_hw_free(struct snd_pcm_substream *substream)
529{
530 struct oxygen *chip = snd_pcm_substream_chip(substream);
Clemens Ladisch740eb832008-01-04 09:22:20 +0100531 unsigned int channel = oxygen_substream_channel(substream);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100532
533 spin_lock_irq(&chip->reg_lock);
534 chip->interrupt_mask &= ~(1 << channel);
535 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, chip->interrupt_mask);
536 spin_unlock_irq(&chip->reg_lock);
537
538 return snd_pcm_lib_free_pages(substream);
539}
540
541static int oxygen_spdif_hw_free(struct snd_pcm_substream *substream)
542{
543 struct oxygen *chip = snd_pcm_substream_chip(substream);
544
545 spin_lock_irq(&chip->reg_lock);
546 oxygen_clear_bits32(chip, OXYGEN_SPDIF_CONTROL,
547 OXYGEN_SPDIF_OUT_ENABLE);
548 spin_unlock_irq(&chip->reg_lock);
549 return oxygen_hw_free(substream);
550}
551
552static int oxygen_prepare(struct snd_pcm_substream *substream)
553{
554 struct oxygen *chip = snd_pcm_substream_chip(substream);
Clemens Ladisch740eb832008-01-04 09:22:20 +0100555 unsigned int channel = oxygen_substream_channel(substream);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100556 unsigned int channel_mask = 1 << channel;
557
558 spin_lock_irq(&chip->reg_lock);
559 oxygen_set_bits8(chip, OXYGEN_DMA_FLUSH, channel_mask);
560 oxygen_clear_bits8(chip, OXYGEN_DMA_FLUSH, channel_mask);
561
562 chip->interrupt_mask |= channel_mask;
563 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, chip->interrupt_mask);
564 spin_unlock_irq(&chip->reg_lock);
565 return 0;
566}
567
568static int oxygen_trigger(struct snd_pcm_substream *substream, int cmd)
569{
570 struct oxygen *chip = snd_pcm_substream_chip(substream);
571 struct snd_pcm_substream *s;
572 unsigned int mask = 0;
573 int running;
574
575 switch (cmd) {
576 case SNDRV_PCM_TRIGGER_STOP:
577 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
578 running = 0;
579 break;
580 case SNDRV_PCM_TRIGGER_START:
581 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
582 running = 1;
583 break;
584 default:
585 return -EINVAL;
586 }
587
588 snd_pcm_group_for_each_entry(s, substream) {
589 if (snd_pcm_substream_chip(s) == chip) {
Clemens Ladisch740eb832008-01-04 09:22:20 +0100590 mask |= 1 << oxygen_substream_channel(s);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100591 snd_pcm_trigger_done(s, substream);
592 }
593 }
594
595 spin_lock(&chip->reg_lock);
596 if (running)
597 chip->pcm_running |= mask;
598 else
599 chip->pcm_running &= ~mask;
600 oxygen_write8(chip, OXYGEN_DMA_STATUS, chip->pcm_running);
601 spin_unlock(&chip->reg_lock);
602 return 0;
603}
604
605static snd_pcm_uframes_t oxygen_pointer(struct snd_pcm_substream *substream)
606{
607 struct oxygen *chip = snd_pcm_substream_chip(substream);
608 struct snd_pcm_runtime *runtime = substream->runtime;
Clemens Ladisch740eb832008-01-04 09:22:20 +0100609 unsigned int channel = oxygen_substream_channel(substream);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100610 u32 curr_addr;
611
612 /* no spinlock, this read should be atomic */
613 curr_addr = oxygen_read32(chip, channel_base_registers[channel]);
614 return bytes_to_frames(runtime, curr_addr - (u32)runtime->dma_addr);
615}
616
617static struct snd_pcm_ops oxygen_rec_a_ops = {
618 .open = oxygen_rec_a_open,
619 .close = oxygen_close,
620 .ioctl = snd_pcm_lib_ioctl,
621 .hw_params = oxygen_rec_a_hw_params,
622 .hw_free = oxygen_hw_free,
623 .prepare = oxygen_prepare,
624 .trigger = oxygen_trigger,
625 .pointer = oxygen_pointer,
626};
627
628static struct snd_pcm_ops oxygen_rec_b_ops = {
629 .open = oxygen_rec_b_open,
630 .close = oxygen_close,
631 .ioctl = snd_pcm_lib_ioctl,
632 .hw_params = oxygen_rec_b_hw_params,
633 .hw_free = oxygen_hw_free,
634 .prepare = oxygen_prepare,
635 .trigger = oxygen_trigger,
636 .pointer = oxygen_pointer,
637};
638
639static struct snd_pcm_ops oxygen_rec_c_ops = {
640 .open = oxygen_rec_c_open,
641 .close = oxygen_close,
642 .ioctl = snd_pcm_lib_ioctl,
643 .hw_params = oxygen_rec_c_hw_params,
644 .hw_free = oxygen_hw_free,
645 .prepare = oxygen_prepare,
646 .trigger = oxygen_trigger,
647 .pointer = oxygen_pointer,
648};
649
650static struct snd_pcm_ops oxygen_spdif_ops = {
651 .open = oxygen_spdif_open,
652 .close = oxygen_close,
653 .ioctl = snd_pcm_lib_ioctl,
654 .hw_params = oxygen_spdif_hw_params,
655 .hw_free = oxygen_spdif_hw_free,
656 .prepare = oxygen_prepare,
657 .trigger = oxygen_trigger,
658 .pointer = oxygen_pointer,
659};
660
661static struct snd_pcm_ops oxygen_multich_ops = {
662 .open = oxygen_multich_open,
663 .close = oxygen_close,
664 .ioctl = snd_pcm_lib_ioctl,
665 .hw_params = oxygen_multich_hw_params,
666 .hw_free = oxygen_hw_free,
667 .prepare = oxygen_prepare,
668 .trigger = oxygen_trigger,
669 .pointer = oxygen_pointer,
670};
671
672static struct snd_pcm_ops oxygen_ac97_ops = {
673 .open = oxygen_ac97_open,
674 .close = oxygen_close,
675 .ioctl = snd_pcm_lib_ioctl,
Clemens Ladischc2353a02008-01-18 09:17:53 +0100676 .hw_params = oxygen_hw_params,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100677 .hw_free = oxygen_hw_free,
678 .prepare = oxygen_prepare,
679 .trigger = oxygen_trigger,
680 .pointer = oxygen_pointer,
681};
682
683static void oxygen_pcm_free(struct snd_pcm *pcm)
684{
685 snd_pcm_lib_preallocate_free_for_all(pcm);
686}
687
688int __devinit oxygen_pcm_init(struct oxygen *chip)
689{
690 struct snd_pcm *pcm;
Clemens Ladische85e0922008-01-16 08:30:38 +0100691 int outs, ins;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100692 int err;
693
Clemens Ladische85e0922008-01-16 08:30:38 +0100694 outs = 1; /* OXYGEN_CHANNEL_MULTICH is always used */
695 ins = !!(chip->model->used_channels & (OXYGEN_CHANNEL_A |
696 OXYGEN_CHANNEL_B));
697 err = snd_pcm_new(chip->card, "Analog", 0, outs, ins, &pcm);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100698 if (err < 0)
699 return err;
700 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &oxygen_multich_ops);
Clemens Ladische85e0922008-01-16 08:30:38 +0100701 if (chip->model->used_channels & OXYGEN_CHANNEL_A)
702 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
703 &oxygen_rec_a_ops);
704 else if (chip->model->used_channels & OXYGEN_CHANNEL_B)
705 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
706 &oxygen_rec_b_ops);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100707 pcm->private_data = chip;
708 pcm->private_free = oxygen_pcm_free;
709 strcpy(pcm->name, "Analog");
710 snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream,
711 SNDRV_DMA_TYPE_DEV,
712 snd_dma_pci_data(chip->pci),
713 512 * 1024, 2048 * 1024);
Clemens Ladische85e0922008-01-16 08:30:38 +0100714 if (ins)
715 snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
716 SNDRV_DMA_TYPE_DEV,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100717 snd_dma_pci_data(chip->pci),
718 128 * 1024, 256 * 1024);
719
Clemens Ladische85e0922008-01-16 08:30:38 +0100720 outs = !!(chip->model->used_channels & OXYGEN_CHANNEL_SPDIF);
721 ins = !!(chip->model->used_channels & OXYGEN_CHANNEL_C);
722 if (outs | ins) {
723 err = snd_pcm_new(chip->card, "Digital", 1, outs, ins, &pcm);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100724 if (err < 0)
725 return err;
Clemens Ladische85e0922008-01-16 08:30:38 +0100726 if (outs)
727 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
728 &oxygen_spdif_ops);
729 if (ins)
730 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
731 &oxygen_rec_c_ops);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100732 pcm->private_data = chip;
733 pcm->private_free = oxygen_pcm_free;
Clemens Ladische85e0922008-01-16 08:30:38 +0100734 strcpy(pcm->name, "Digital");
735 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
736 snd_dma_pci_data(chip->pci),
737 128 * 1024, 256 * 1024);
738 }
739
740 outs = chip->has_ac97_1 &&
741 (chip->model->used_channels & OXYGEN_CHANNEL_AC97);
742 ins = (chip->model->used_channels & (OXYGEN_CHANNEL_A |
743 OXYGEN_CHANNEL_B))
744 == (OXYGEN_CHANNEL_A | OXYGEN_CHANNEL_B);
745 if (outs | ins) {
746 err = snd_pcm_new(chip->card, ins ? "Analog2" : "AC97",
747 2, outs, ins, &pcm);
748 if (err < 0)
749 return err;
750 if (outs)
751 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
752 &oxygen_ac97_ops);
753 if (ins)
754 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
755 &oxygen_rec_b_ops);
756 pcm->private_data = chip;
757 pcm->private_free = oxygen_pcm_free;
758 strcpy(pcm->name, ins ? "Analog 2" : "Front Panel");
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100759 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
760 snd_dma_pci_data(chip->pci),
761 128 * 1024, 256 * 1024);
762 }
763 return 0;
764}