1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) by James Courtier-Dutton <James@superbug.demon.co.uk>
4 * Driver p16v chips
5 * Version: 0.25
6 *
7 * FEATURES currently supported:
8 * Output fixed at S32_LE, 2 channel to hw:0,0
9 * Rates: 44.1, 48, 96, 192.
10 *
11 * Changelog:
12 * 0.8
13 * Use separate card based buffer for periods table.
14 * 0.9
15 * Use 2 channel output streams instead of 8 channel.
16 * (8 channel output streams might be good for ASIO type output)
17 * Corrected speaker output, so Front -> Front etc.
18 * 0.10
19 * Fixed missed interrupts.
20 * 0.11
21 * Add Sound card model number and names.
22 * Add Analog volume controls.
23 * 0.12
24 * Corrected playback interrupts. Now interrupt per period, instead of half period.
25 * 0.13
26 * Use single trigger for multichannel.
27 * 0.14
28 * Mic capture now works at fixed: S32_LE, 96000Hz, Stereo.
29 * 0.15
30 * Force buffer_size / period_size == INTEGER.
31 * 0.16
32 * Update p16v.c to work with changed alsa api.
33 * 0.17
34 * Update p16v.c to work with changed alsa api. Removed boot_devs.
35 * 0.18
36 * Merging with snd-emu10k1 driver.
37 * 0.19
38 * One stereo channel at 24bit now works.
39 * 0.20
40 * Added better register defines.
41 * 0.21
42 * Integrated with snd-emu10k1 driver.
43 * 0.22
44 * Removed #if 0 ... #endif
45 * 0.23
46 * Implement different capture rates.
47 * 0.24
48 * Implement different capture source channels.
49 * e.g. When HD Capture source is set to SPDIF,
50 * setting HD Capture channel to 0 captures from CDROM digital input.
51 * setting HD Capture channel to 1 captures from SPDIF in.
52 * 0.25
53 * Include capture buffer sizes.
54 *
55 * BUGS:
56 * Some stability problems when unloading the snd-p16v kernel module.
57 * --
58 *
59 * TODO:
60 * SPDIF out.
61 * Find out how to change capture sample rates. E.g. To record SPDIF at 48000Hz.
62 * Currently capture fixed at 48000Hz.
63 *
64 * --
65 * GENERAL INFO:
66 * Model: SB0240
67 * P16V Chip: CA0151-DBS
68 * Audigy 2 Chip: CA0102-IAT
69 * AC97 Codec: STAC 9721
70 * ADC: Philips 1361T (Stereo 24bit)
71 * DAC: CS4382-K (8-channel, 24bit, 192Khz)
72 *
73 * This code was initially based on code from ALSA's emu10k1x.c which is:
74 * Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
75 */
76 #include <linux/delay.h>
77 #include <linux/init.h>
78 #include <linux/interrupt.h>
79 #include <linux/pci.h>
80 #include <linux/slab.h>
81 #include <linux/vmalloc.h>
82 #include <linux/moduleparam.h>
83 #include <sound/core.h>
84 #include <sound/initval.h>
85 #include <sound/pcm.h>
86 #include <sound/ac97_codec.h>
87 #include <sound/info.h>
88 #include <sound/tlv.h>
89 #include <sound/emu10k1.h>
90 #include "p16v.h"
91
92 #define SET_CHANNEL 0 /* Testing channel outputs 0=Front, 1=Center/LFE, 2=Unknown, 3=Rear */
93 #define PCM_FRONT_CHANNEL 0
94 #define PCM_REAR_CHANNEL 1
95 #define PCM_CENTER_LFE_CHANNEL 2
96 #define PCM_SIDE_CHANNEL 3
97 #define CONTROL_FRONT_CHANNEL 0
98 #define CONTROL_REAR_CHANNEL 3
99 #define CONTROL_CENTER_LFE_CHANNEL 1
100 #define CONTROL_SIDE_CHANNEL 2
101
102 /* Card IDs:
103 * Class 0401: 1102:0004 (rev 04) Subsystem: 1102:2002 -> Audigy2 ZS 7.1 Model:SB0350
104 * Class 0401: 1102:0004 (rev 04) Subsystem: 1102:1007 -> Audigy2 6.1 Model:SB0240
105 * Class 0401: 1102:0004 (rev 04) Subsystem: 1102:1002 -> Audigy2 Platinum Model:SB msb0240230009266
106 * Class 0401: 1102:0004 (rev 04) Subsystem: 1102:2007 -> Audigy4 Pro Model:SB0380 M1SB0380472001901E
107 *
108 */
109
110 /* hardware definition */
111 static const struct snd_pcm_hardware snd_p16v_playback_hw = {
112 .info = SNDRV_PCM_INFO_MMAP |
113 SNDRV_PCM_INFO_INTERLEAVED |
114 SNDRV_PCM_INFO_BLOCK_TRANSFER |
115 SNDRV_PCM_INFO_RESUME |
116 SNDRV_PCM_INFO_MMAP_VALID |
117 SNDRV_PCM_INFO_SYNC_START,
118 .formats = SNDRV_PCM_FMTBIT_S32_LE, /* Only supports 24-bit samples padded to 32 bits. */
119 .rates = SNDRV_PCM_RATE_192000 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_44100,
120 .rate_min = 44100,
121 .rate_max = 192000,
122 .channels_min = 8,
123 .channels_max = 8,
124 .buffer_bytes_max = ((65536 - 64) * 8),
125 .period_bytes_min = 64,
126 .period_bytes_max = (65536 - 64),
127 .periods_min = 2,
128 .periods_max = 8,
129 .fifo_size = 0,
130 };
131
132 static const struct snd_pcm_hardware snd_p16v_capture_hw = {
133 .info = (SNDRV_PCM_INFO_MMAP |
134 SNDRV_PCM_INFO_INTERLEAVED |
135 SNDRV_PCM_INFO_BLOCK_TRANSFER |
136 SNDRV_PCM_INFO_RESUME |
137 SNDRV_PCM_INFO_MMAP_VALID),
138 .formats = SNDRV_PCM_FMTBIT_S32_LE,
139 .rates = SNDRV_PCM_RATE_192000 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_44100,
140 .rate_min = 44100,
141 .rate_max = 192000,
142 .channels_min = 2,
143 .channels_max = 2,
144 .buffer_bytes_max = (65536 - 64),
145 .period_bytes_min = 64,
146 .period_bytes_max = (65536 - 128) >> 1, /* size has to be N*64 bytes */
147 .periods_min = 2,
148 .periods_max = 2,
149 .fifo_size = 0,
150 };
151
152 /* open_playback callback */
snd_p16v_pcm_open_playback_channel(struct snd_pcm_substream * substream,int channel_id)153 static int snd_p16v_pcm_open_playback_channel(struct snd_pcm_substream *substream, int channel_id)
154 {
155 struct snd_pcm_runtime *runtime = substream->runtime;
156 int err;
157
158 /*
159 dev_dbg(emu->card->dev, "epcm device=%d, channel_id=%d\n",
160 substream->pcm->device, channel_id);
161 */
162
163 runtime->hw = snd_p16v_playback_hw;
164
165 #if 0 /* debug */
166 dev_dbg(emu->card->dev,
167 "p16v: open channel_id=%d, channel=%p, use=0x%x\n",
168 channel_id, channel, channel->use);
169 dev_dbg(emu->card->dev, "open:channel_id=%d, chip=%p, channel=%p\n",
170 channel_id, chip, channel);
171 #endif /* debug */
172 /* channel->interrupt = snd_p16v_pcm_channel_interrupt; */
173 err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
174 if (err < 0)
175 return err;
176
177 return 0;
178 }
179
180 /* open_capture callback */
snd_p16v_pcm_open_capture_channel(struct snd_pcm_substream * substream,int channel_id)181 static int snd_p16v_pcm_open_capture_channel(struct snd_pcm_substream *substream, int channel_id)
182 {
183 struct snd_pcm_runtime *runtime = substream->runtime;
184 int err;
185
186 /*
187 dev_dbg(emu->card->dev, "epcm device=%d, channel_id=%d\n",
188 substream->pcm->device, channel_id);
189 */
190
191 runtime->hw = snd_p16v_capture_hw;
192
193 err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
194 if (err < 0)
195 return err;
196
197 return 0;
198 }
199
200
201 /* close callback */
snd_p16v_pcm_close_playback(struct snd_pcm_substream * substream)202 static int snd_p16v_pcm_close_playback(struct snd_pcm_substream *substream)
203 {
204 return 0;
205 }
206
207 /* close callback */
snd_p16v_pcm_close_capture(struct snd_pcm_substream * substream)208 static int snd_p16v_pcm_close_capture(struct snd_pcm_substream *substream)
209 {
210 return 0;
211 }
212
snd_p16v_pcm_open_playback_front(struct snd_pcm_substream * substream)213 static int snd_p16v_pcm_open_playback_front(struct snd_pcm_substream *substream)
214 {
215 return snd_p16v_pcm_open_playback_channel(substream, PCM_FRONT_CHANNEL);
216 }
217
snd_p16v_pcm_open_capture(struct snd_pcm_substream * substream)218 static int snd_p16v_pcm_open_capture(struct snd_pcm_substream *substream)
219 {
220 // Only using channel 0 for now, but the card has 2 channels.
221 return snd_p16v_pcm_open_capture_channel(substream, 0);
222 }
223
snd_p16v_pcm_ioctl_playback(struct snd_pcm_substream * substream,unsigned int cmd,void * arg)224 static int snd_p16v_pcm_ioctl_playback(struct snd_pcm_substream *substream,
225 unsigned int cmd, void *arg)
226 {
227 if (cmd == SNDRV_PCM_IOCTL1_SYNC_ID) {
228 static const unsigned char id[4] = { 'P', '1', '6', 'V' };
229 snd_pcm_set_sync_per_card(substream, arg, id, 4);
230 return 0;
231 }
232 return snd_pcm_lib_ioctl(substream, cmd, arg);
233 }
234
235 /* prepare playback callback */
snd_p16v_pcm_prepare_playback(struct snd_pcm_substream * substream)236 static int snd_p16v_pcm_prepare_playback(struct snd_pcm_substream *substream)
237 {
238 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
239 struct snd_pcm_runtime *runtime = substream->runtime;
240 int channel = substream->pcm->device - emu->p16v_device_offset;
241 u32 *table_base = (u32 *)(emu->p16v_buffer->area+(8*16*channel));
242 u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
243 int i;
244 u32 tmp;
245
246 #if 0 /* debug */
247 dev_dbg(emu->card->dev,
248 "prepare:channel_number=%d, rate=%d, "
249 "format=0x%x, channels=%d, buffer_size=%ld, "
250 "period_size=%ld, periods=%u, frames_to_bytes=%d\n",
251 channel, runtime->rate, runtime->format, runtime->channels,
252 runtime->buffer_size, runtime->period_size,
253 runtime->periods, frames_to_bytes(runtime, 1));
254 dev_dbg(emu->card->dev,
255 "dma_addr=%x, dma_area=%p, table_base=%p\n",
256 runtime->dma_addr, runtime->dma_area, table_base);
257 dev_dbg(emu->card->dev,
258 "dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",
259 emu->p16v_buffer->addr, emu->p16v_buffer->area,
260 emu->p16v_buffer->bytes);
261 #endif /* debug */
262 tmp = snd_emu10k1_ptr_read(emu, A_SPDIF_SAMPLERATE, channel);
263 tmp &= ~(A_SPDIF_RATE_MASK | A_EHC_SRC48_MASK);
264 switch (runtime->rate) {
265 case 44100:
266 snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel,
267 tmp | A_SPDIF_44100 | A_EHC_SRC48_44);
268 break;
269 case 96000:
270 snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel,
271 tmp | A_SPDIF_96000 | A_EHC_SRC48_96);
272 break;
273 case 192000:
274 snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel,
275 tmp | A_SPDIF_192000 | A_EHC_SRC48_192);
276 break;
277 case 48000:
278 default:
279 snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel,
280 tmp | A_SPDIF_48000 | A_EHC_SRC48_BYPASS);
281 break;
282 }
283 /* FIXME: Check emu->buffer.size before actually writing to it. */
284 for(i = 0; i < runtime->periods; i++) {
285 table_base[i*2]=runtime->dma_addr+(i*period_size_bytes);
286 table_base[(i*2)+1]=period_size_bytes<<16;
287 }
288
289 snd_emu10k1_ptr20_write(emu, PLAYBACK_LIST_ADDR, channel, emu->p16v_buffer->addr+(8*16*channel));
290 snd_emu10k1_ptr20_write(emu, PLAYBACK_LIST_SIZE, channel, (runtime->periods - 1) << 19);
291 snd_emu10k1_ptr20_write(emu, PLAYBACK_LIST_PTR, channel, 0);
292 snd_emu10k1_ptr20_write(emu, PLAYBACK_DMA_ADDR, channel, runtime->dma_addr);
293 //snd_emu10k1_ptr20_write(emu, PLAYBACK_PERIOD_SIZE, channel, frames_to_bytes(runtime, runtime->period_size)<<16); // buffer size in bytes
294 snd_emu10k1_ptr20_write(emu, PLAYBACK_PERIOD_SIZE, channel, 0); // buffer size in bytes
295 snd_emu10k1_ptr20_write(emu, PLAYBACK_POINTER, channel, 0);
296 snd_emu10k1_ptr20_write(emu, PLAYBACK_FIFO_END_ADDRESS, channel, 0);
297 snd_emu10k1_ptr20_write(emu, PLAYBACK_FIFO_POINTER, channel, 0);
298
299 return 0;
300 }
301
302 /* prepare capture callback */
snd_p16v_pcm_prepare_capture(struct snd_pcm_substream * substream)303 static int snd_p16v_pcm_prepare_capture(struct snd_pcm_substream *substream)
304 {
305 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
306 struct snd_pcm_runtime *runtime = substream->runtime;
307 int channel = substream->pcm->device - emu->p16v_device_offset;
308
309 /*
310 dev_dbg(emu->card->dev, "prepare capture:channel_number=%d, rate=%d, "
311 "format=0x%x, channels=%d, buffer_size=%ld, period_size=%ld, "
312 "frames_to_bytes=%d\n",
313 channel, runtime->rate, runtime->format, runtime->channels,
314 runtime->buffer_size, runtime->period_size,
315 frames_to_bytes(runtime, 1));
316 */
317 switch (runtime->rate) {
318 case 44100:
319 snd_emu10k1_ptr_write(emu, A_I2S_CAPTURE_RATE, channel, A_I2S_CAPTURE_44100);
320 break;
321 case 96000:
322 snd_emu10k1_ptr_write(emu, A_I2S_CAPTURE_RATE, channel, A_I2S_CAPTURE_96000);
323 break;
324 case 192000:
325 snd_emu10k1_ptr_write(emu, A_I2S_CAPTURE_RATE, channel, A_I2S_CAPTURE_192000);
326 break;
327 case 48000:
328 default:
329 snd_emu10k1_ptr_write(emu, A_I2S_CAPTURE_RATE, channel, A_I2S_CAPTURE_48000);
330 break;
331 }
332 /* FIXME: Check emu->buffer.size before actually writing to it. */
333 snd_emu10k1_ptr20_write(emu, CAPTURE_FIFO_POINTER, channel, 0);
334 snd_emu10k1_ptr20_write(emu, CAPTURE_DMA_ADDR, channel, runtime->dma_addr);
335 snd_emu10k1_ptr20_write(emu, CAPTURE_BUFFER_SIZE, channel, frames_to_bytes(runtime, runtime->buffer_size) << 16); // buffer size in bytes
336 snd_emu10k1_ptr20_write(emu, CAPTURE_POINTER, channel, 0);
337 //snd_emu10k1_ptr20_write(emu, CAPTURE_SOURCE, 0x0, 0x333300e4); /* Select MIC or Line in */
338 //snd_emu10k1_ptr20_write(emu, EXTENDED_INT_MASK, 0, snd_emu10k1_ptr20_read(emu, EXTENDED_INT_MASK, 0) | (0x110000<<channel));
339
340 return 0;
341 }
342
snd_p16v_intr_enable(struct snd_emu10k1 * emu,unsigned int intrenb)343 static void snd_p16v_intr_enable(struct snd_emu10k1 *emu, unsigned int intrenb)
344 {
345 unsigned int enable;
346
347 guard(spinlock_irqsave)(&emu->emu_lock);
348 enable = inl(emu->port + INTE2) | intrenb;
349 outl(enable, emu->port + INTE2);
350 }
351
snd_p16v_intr_disable(struct snd_emu10k1 * emu,unsigned int intrenb)352 static void snd_p16v_intr_disable(struct snd_emu10k1 *emu, unsigned int intrenb)
353 {
354 unsigned int disable;
355
356 guard(spinlock_irqsave)(&emu->emu_lock);
357 disable = inl(emu->port + INTE2) & (~intrenb);
358 outl(disable, emu->port + INTE2);
359 }
360
snd_p16v_interrupt(struct snd_emu10k1 * emu)361 static void snd_p16v_interrupt(struct snd_emu10k1 *emu)
362 {
363 unsigned int status;
364
365 while ((status = inl(emu->port + IPR2)) != 0) {
366 u32 mask = INTE2_PLAYBACK_CH_0_LOOP; /* Full Loop */
367
368 /* dev_dbg(emu->card->dev, "p16v status=0x%x\n", status); */
369 if (status & mask) {
370 struct snd_pcm_substream *substream =
371 emu->pcm_p16v->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
372 struct snd_pcm_runtime *runtime = substream->runtime;
373
374 if (runtime && runtime->private_data) {
375 snd_pcm_period_elapsed(substream);
376 } else {
377 dev_err(emu->card->dev,
378 "p16v: status: 0x%08x, mask=0x%08x\n",
379 status, mask);
380 }
381 }
382 if (status & 0x110000) {
383 struct snd_pcm_substream *substream =
384 emu->pcm_p16v->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
385 struct snd_pcm_runtime *runtime = substream->runtime;
386
387 /* dev_info(emu->card->dev, "capture int found\n"); */
388 if (runtime && runtime->private_data) {
389 /* dev_info(emu->card->dev, "capture period_elapsed\n"); */
390 snd_pcm_period_elapsed(substream);
391 }
392 }
393 outl(status, emu->port + IPR2); /* ack all */
394 }
395 }
396
397 /* trigger_playback callback */
snd_p16v_pcm_trigger_playback(struct snd_pcm_substream * substream,int cmd)398 static int snd_p16v_pcm_trigger_playback(struct snd_pcm_substream *substream,
399 int cmd)
400 {
401 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
402 struct snd_pcm_runtime *runtime;
403 int channel;
404 int result = 0;
405 struct snd_pcm_substream *s;
406 u32 basic = 0;
407 u32 inte = 0;
408 int running = 0;
409
410 switch (cmd) {
411 case SNDRV_PCM_TRIGGER_START:
412 running=1;
413 break;
414 case SNDRV_PCM_TRIGGER_STOP:
415 default:
416 running = 0;
417 break;
418 }
419 snd_pcm_group_for_each_entry(s, substream) {
420 if (snd_pcm_substream_chip(s) != emu ||
421 s->stream != SNDRV_PCM_STREAM_PLAYBACK)
422 continue;
423 runtime = s->runtime;
424 channel = substream->pcm->device-emu->p16v_device_offset;
425 /* dev_dbg(emu->card->dev, "p16v channel=%d\n", channel); */
426 runtime->private_data = (void *)(ptrdiff_t)running;
427 basic |= (0x1<<channel);
428 inte |= (INTE2_PLAYBACK_CH_0_LOOP<<channel);
429 snd_pcm_trigger_done(s, substream);
430 }
431 /* dev_dbg(emu->card->dev, "basic=0x%x, inte=0x%x\n", basic, inte); */
432
433 switch (cmd) {
434 case SNDRV_PCM_TRIGGER_START:
435 snd_p16v_intr_enable(emu, inte);
436 snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0)| (basic));
437 break;
438 case SNDRV_PCM_TRIGGER_STOP:
439 snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0) & ~(basic));
440 snd_p16v_intr_disable(emu, inte);
441 break;
442 default:
443 result = -EINVAL;
444 break;
445 }
446 return result;
447 }
448
449 /* trigger_capture callback */
snd_p16v_pcm_trigger_capture(struct snd_pcm_substream * substream,int cmd)450 static int snd_p16v_pcm_trigger_capture(struct snd_pcm_substream *substream,
451 int cmd)
452 {
453 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
454 struct snd_pcm_runtime *runtime = substream->runtime;
455 int channel = 0;
456 int result = 0;
457 u32 inte = INTE2_CAPTURE_CH_0_LOOP | INTE2_CAPTURE_CH_0_HALF_LOOP;
458
459 switch (cmd) {
460 case SNDRV_PCM_TRIGGER_START:
461 snd_p16v_intr_enable(emu, inte);
462 snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0)|(0x100<<channel));
463 runtime->private_data = (void *)1;
464 break;
465 case SNDRV_PCM_TRIGGER_STOP:
466 snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0) & ~(0x100<<channel));
467 snd_p16v_intr_disable(emu, inte);
468 //snd_emu10k1_ptr20_write(emu, EXTENDED_INT_MASK, 0, snd_emu10k1_ptr20_read(emu, EXTENDED_INT_MASK, 0) & ~(0x110000<<channel));
469 runtime->private_data = NULL;
470 break;
471 default:
472 result = -EINVAL;
473 break;
474 }
475 return result;
476 }
477
478 /* pointer_playback callback */
479 static snd_pcm_uframes_t
snd_p16v_pcm_pointer_playback(struct snd_pcm_substream * substream)480 snd_p16v_pcm_pointer_playback(struct snd_pcm_substream *substream)
481 {
482 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
483 struct snd_pcm_runtime *runtime = substream->runtime;
484 snd_pcm_uframes_t ptr, ptr1, ptr2,ptr3,ptr4 = 0;
485 int channel = substream->pcm->device - emu->p16v_device_offset;
486
487 if (!runtime->private_data)
488 return 0;
489
490 ptr3 = snd_emu10k1_ptr20_read(emu, PLAYBACK_LIST_PTR, channel);
491 ptr1 = snd_emu10k1_ptr20_read(emu, PLAYBACK_POINTER, channel);
492 ptr4 = snd_emu10k1_ptr20_read(emu, PLAYBACK_LIST_PTR, channel);
493 if (ptr3 != ptr4) ptr1 = snd_emu10k1_ptr20_read(emu, PLAYBACK_POINTER, channel);
494 ptr2 = bytes_to_frames(runtime, ptr1);
495 ptr2+= (ptr4 >> 3) * runtime->period_size;
496 ptr=ptr2;
497 if (ptr >= runtime->buffer_size)
498 ptr -= runtime->buffer_size;
499
500 return ptr;
501 }
502
503 /* pointer_capture callback */
504 static snd_pcm_uframes_t
snd_p16v_pcm_pointer_capture(struct snd_pcm_substream * substream)505 snd_p16v_pcm_pointer_capture(struct snd_pcm_substream *substream)
506 {
507 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
508 struct snd_pcm_runtime *runtime = substream->runtime;
509 snd_pcm_uframes_t ptr, ptr1, ptr2 = 0;
510 int channel = 0;
511
512 if (!runtime->private_data)
513 return 0;
514
515 ptr1 = snd_emu10k1_ptr20_read(emu, CAPTURE_POINTER, channel);
516 ptr2 = bytes_to_frames(runtime, ptr1);
517 ptr=ptr2;
518 if (ptr >= runtime->buffer_size) {
519 ptr -= runtime->buffer_size;
520 dev_warn(emu->card->dev, "buffer capture limited!\n");
521 }
522 /*
523 dev_dbg(emu->card->dev, "ptr1 = 0x%lx, ptr2=0x%lx, ptr=0x%lx, "
524 "buffer_size = 0x%x, period_size = 0x%x, bits=%d, rate=%d\n",
525 ptr1, ptr2, ptr, (int)runtime->buffer_size,
526 (int)runtime->period_size, (int)runtime->frame_bits,
527 (int)runtime->rate);
528 */
529 return ptr;
530 }
531
532 /* operators */
533 static const struct snd_pcm_ops snd_p16v_playback_front_ops = {
534 .open = snd_p16v_pcm_open_playback_front,
535 .close = snd_p16v_pcm_close_playback,
536 .ioctl = snd_p16v_pcm_ioctl_playback,
537 .prepare = snd_p16v_pcm_prepare_playback,
538 .trigger = snd_p16v_pcm_trigger_playback,
539 .pointer = snd_p16v_pcm_pointer_playback,
540 };
541
542 static const struct snd_pcm_ops snd_p16v_capture_ops = {
543 .open = snd_p16v_pcm_open_capture,
544 .close = snd_p16v_pcm_close_capture,
545 .prepare = snd_p16v_pcm_prepare_capture,
546 .trigger = snd_p16v_pcm_trigger_capture,
547 .pointer = snd_p16v_pcm_pointer_capture,
548 };
549
snd_p16v_pcm(struct snd_emu10k1 * emu,int device)550 int snd_p16v_pcm(struct snd_emu10k1 *emu, int device)
551 {
552 struct snd_pcm *pcm;
553 struct snd_pcm_substream *substream;
554 int err;
555 int capture=1;
556
557 /* dev_dbg(emu->card->dev, "snd_p16v_pcm called. device=%d\n", device); */
558 emu->p16v_device_offset = device;
559
560 err = snd_pcm_new(emu->card, "p16v", device, 1, capture, &pcm);
561 if (err < 0)
562 return err;
563
564 pcm->private_data = emu;
565 // Single playback 8 channel device.
566 // Single capture 2 channel device.
567 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_p16v_playback_front_ops);
568 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_p16v_capture_ops);
569
570 pcm->info_flags = 0;
571 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
572 strscpy(pcm->name, "p16v");
573 emu->pcm_p16v = pcm;
574 emu->p16v_interrupt = snd_p16v_interrupt;
575
576 for(substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
577 substream;
578 substream = substream->next) {
579 snd_pcm_set_managed_buffer(substream, SNDRV_DMA_TYPE_DEV,
580 &emu->pci->dev,
581 (65536 - 64) * 8,
582 (65536 - 64) * 8);
583 /*
584 dev_dbg(emu->card->dev,
585 "preallocate playback substream: err=%d\n", err);
586 */
587 }
588
589 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
590 substream;
591 substream = substream->next) {
592 snd_pcm_set_managed_buffer(substream, SNDRV_DMA_TYPE_DEV,
593 &emu->pci->dev,
594 65536 - 64, 65536 - 64);
595 /*
596 dev_dbg(emu->card->dev,
597 "preallocate capture substream: err=%d\n", err);
598 */
599 }
600
601 return 0;
602 }
603
snd_p16v_volume_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)604 static int snd_p16v_volume_info(struct snd_kcontrol *kcontrol,
605 struct snd_ctl_elem_info *uinfo)
606 {
607 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
608 uinfo->count = 2;
609 uinfo->value.integer.min = 0;
610 uinfo->value.integer.max = 255;
611 return 0;
612 }
613
snd_p16v_volume_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)614 static int snd_p16v_volume_get(struct snd_kcontrol *kcontrol,
615 struct snd_ctl_elem_value *ucontrol)
616 {
617 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
618 int high_low = (kcontrol->private_value >> 8) & 0xff;
619 int reg = kcontrol->private_value & 0xff;
620 u32 value;
621
622 value = snd_emu10k1_ptr20_read(emu, reg, high_low);
623 if (high_low) {
624 ucontrol->value.integer.value[0] = 0xff - ((value >> 24) & 0xff); /* Left */
625 ucontrol->value.integer.value[1] = 0xff - ((value >> 16) & 0xff); /* Right */
626 } else {
627 ucontrol->value.integer.value[0] = 0xff - ((value >> 8) & 0xff); /* Left */
628 ucontrol->value.integer.value[1] = 0xff - ((value >> 0) & 0xff); /* Right */
629 }
630 return 0;
631 }
632
snd_p16v_volume_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)633 static int snd_p16v_volume_put(struct snd_kcontrol *kcontrol,
634 struct snd_ctl_elem_value *ucontrol)
635 {
636 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
637 int high_low = (kcontrol->private_value >> 8) & 0xff;
638 int reg = kcontrol->private_value & 0xff;
639 u32 value, oval;
640
641 oval = value = snd_emu10k1_ptr20_read(emu, reg, 0);
642 if (high_low == 1) {
643 value &= 0xffff;
644 value |= ((0xff - ucontrol->value.integer.value[0]) << 24) |
645 ((0xff - ucontrol->value.integer.value[1]) << 16);
646 } else {
647 value &= 0xffff0000;
648 value |= ((0xff - ucontrol->value.integer.value[0]) << 8) |
649 ((0xff - ucontrol->value.integer.value[1]) );
650 }
651 if (value != oval) {
652 snd_emu10k1_ptr20_write(emu, reg, 0, value);
653 return 1;
654 }
655 return 0;
656 }
657
snd_p16v_capture_source_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)658 static int snd_p16v_capture_source_info(struct snd_kcontrol *kcontrol,
659 struct snd_ctl_elem_info *uinfo)
660 {
661 static const char * const texts[8] = {
662 "SPDIF", "I2S", "SRC48", "SRCMulti_SPDIF", "SRCMulti_I2S",
663 "CDIF", "FX", "AC97"
664 };
665
666 return snd_ctl_enum_info(uinfo, 1, 8, texts);
667 }
668
snd_p16v_capture_source_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)669 static int snd_p16v_capture_source_get(struct snd_kcontrol *kcontrol,
670 struct snd_ctl_elem_value *ucontrol)
671 {
672 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
673
674 ucontrol->value.enumerated.item[0] = emu->p16v_capture_source;
675 return 0;
676 }
677
snd_p16v_capture_source_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)678 static int snd_p16v_capture_source_put(struct snd_kcontrol *kcontrol,
679 struct snd_ctl_elem_value *ucontrol)
680 {
681 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
682 unsigned int val;
683 int change = 0;
684 u32 mask;
685 u32 source;
686
687 val = ucontrol->value.enumerated.item[0] ;
688 if (val > 7)
689 return -EINVAL;
690 change = (emu->p16v_capture_source != val);
691 if (change) {
692 emu->p16v_capture_source = val;
693 source = (val << 28) | (val << 24) | (val << 20) | (val << 16);
694 mask = snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0) & 0xffff;
695 snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, source | mask);
696 }
697 return change;
698 }
699
snd_p16v_capture_channel_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)700 static int snd_p16v_capture_channel_info(struct snd_kcontrol *kcontrol,
701 struct snd_ctl_elem_info *uinfo)
702 {
703 static const char * const texts[4] = { "0", "1", "2", "3", };
704
705 return snd_ctl_enum_info(uinfo, 1, 4, texts);
706 }
707
snd_p16v_capture_channel_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)708 static int snd_p16v_capture_channel_get(struct snd_kcontrol *kcontrol,
709 struct snd_ctl_elem_value *ucontrol)
710 {
711 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
712
713 ucontrol->value.enumerated.item[0] = emu->p16v_capture_channel;
714 return 0;
715 }
716
snd_p16v_capture_channel_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)717 static int snd_p16v_capture_channel_put(struct snd_kcontrol *kcontrol,
718 struct snd_ctl_elem_value *ucontrol)
719 {
720 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
721 unsigned int val;
722 int change = 0;
723 u32 tmp;
724
725 val = ucontrol->value.enumerated.item[0] ;
726 if (val > 3)
727 return -EINVAL;
728 change = (emu->p16v_capture_channel != val);
729 if (change) {
730 emu->p16v_capture_channel = val;
731 tmp = snd_emu10k1_ptr20_read(emu, CAPTURE_P16V_SOURCE, 0) & 0xfffc;
732 snd_emu10k1_ptr20_write(emu, CAPTURE_P16V_SOURCE, 0, tmp | val);
733 }
734 return change;
735 }
736 static const DECLARE_TLV_DB_SCALE(snd_p16v_db_scale1, -5175, 25, 1);
737
738 #define P16V_VOL(xname,xreg,xhl) { \
739 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
740 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
741 SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
742 .info = snd_p16v_volume_info, \
743 .get = snd_p16v_volume_get, \
744 .put = snd_p16v_volume_put, \
745 .tlv = { .p = snd_p16v_db_scale1 }, \
746 .private_value = ((xreg) | ((xhl) << 8)) \
747 }
748
749 static const struct snd_kcontrol_new p16v_mixer_controls[] = {
750 P16V_VOL("HD Analog Front Playback Volume", PLAYBACK_VOLUME_MIXER9, 0),
751 P16V_VOL("HD Analog Rear Playback Volume", PLAYBACK_VOLUME_MIXER10, 1),
752 P16V_VOL("HD Analog Center/LFE Playback Volume", PLAYBACK_VOLUME_MIXER9, 1),
753 P16V_VOL("HD Analog Side Playback Volume", PLAYBACK_VOLUME_MIXER10, 0),
754 P16V_VOL("HD SPDIF Front Playback Volume", PLAYBACK_VOLUME_MIXER7, 0),
755 P16V_VOL("HD SPDIF Rear Playback Volume", PLAYBACK_VOLUME_MIXER8, 1),
756 P16V_VOL("HD SPDIF Center/LFE Playback Volume", PLAYBACK_VOLUME_MIXER7, 1),
757 P16V_VOL("HD SPDIF Side Playback Volume", PLAYBACK_VOLUME_MIXER8, 0),
758 {
759 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
760 .name = "HD source Capture",
761 .info = snd_p16v_capture_source_info,
762 .get = snd_p16v_capture_source_get,
763 .put = snd_p16v_capture_source_put
764 },
765 {
766 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
767 .name = "HD channel Capture",
768 .info = snd_p16v_capture_channel_info,
769 .get = snd_p16v_capture_channel_get,
770 .put = snd_p16v_capture_channel_put
771 },
772 };
773
774
snd_p16v_mixer(struct snd_emu10k1 * emu)775 int snd_p16v_mixer(struct snd_emu10k1 *emu)
776 {
777 int i, err;
778 struct snd_card *card = emu->card;
779
780 for (i = 0; i < ARRAY_SIZE(p16v_mixer_controls); i++) {
781 err = snd_ctl_add(card, snd_ctl_new1(&p16v_mixer_controls[i], emu));
782 if (err < 0)
783 return err;
784 }
785 return 0;
786 }
787
788 #ifdef CONFIG_PM_SLEEP
789
790 #define NUM_CHS 1 /* up to 4, but only first channel is used */
791
snd_p16v_alloc_pm_buffer(struct snd_emu10k1 * emu)792 int snd_p16v_alloc_pm_buffer(struct snd_emu10k1 *emu)
793 {
794 emu->p16v_saved = vmalloc(array_size(NUM_CHS * 4, 0x80));
795 if (! emu->p16v_saved)
796 return -ENOMEM;
797 return 0;
798 }
799
snd_p16v_free_pm_buffer(struct snd_emu10k1 * emu)800 void snd_p16v_free_pm_buffer(struct snd_emu10k1 *emu)
801 {
802 vfree(emu->p16v_saved);
803 }
804
snd_p16v_suspend(struct snd_emu10k1 * emu)805 void snd_p16v_suspend(struct snd_emu10k1 *emu)
806 {
807 int i, ch;
808 unsigned int *val;
809
810 val = emu->p16v_saved;
811 for (ch = 0; ch < NUM_CHS; ch++)
812 for (i = 0; i < 0x80; i++, val++)
813 *val = snd_emu10k1_ptr20_read(emu, i, ch);
814 }
815
snd_p16v_resume(struct snd_emu10k1 * emu)816 void snd_p16v_resume(struct snd_emu10k1 *emu)
817 {
818 int i, ch;
819 unsigned int *val;
820
821 val = emu->p16v_saved;
822 for (ch = 0; ch < NUM_CHS; ch++)
823 for (i = 0; i < 0x80; i++, val++)
824 snd_emu10k1_ptr20_write(emu, i, ch, *val);
825 }
826 #endif
827