blob: eeaed555185c35f529896366b9d115b3722daf96 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Copyright (c) by James Courtier-Dutton <James@superbug.demon.co.uk>
4 * Driver p16v chips
James Courtier-Dutton22bc30c2005-05-07 16:13:40 +02005 * Version: 0.25
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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
James Courtier-Duttonf927c8f2005-05-07 15:34:13 +020045 * 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.
James Courtier-Dutton22bc30c2005-05-07 16:13:40 +020052 * 0.25
53 * Include capture buffer sizes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 *
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 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030073 * This code was initially based on code from ALSA's emu10k1x.c which is:
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 * Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/delay.h>
77#include <linux/init.h>
78#include <linux/interrupt.h>
79#include <linux/pci.h>
80#include <linux/slab.h>
Andreas Schwabbd01e7b2005-12-05 15:12:20 +010081#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#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>
James Courtier-Dutton31508f82006-07-22 17:02:10 +010088#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#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
Takashi Iwaieff49132005-12-22 17:01:20 +010096#define PCM_SIDE_CHANNEL 3
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#define CONTROL_FRONT_CHANNEL 0
98#define CONTROL_REAR_CHANNEL 3
99#define CONTROL_CENTER_LFE_CHANNEL 1
Takashi Iwaieff49132005-12-22 17:01:20 +0100100#define CONTROL_SIDE_CHANNEL 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
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 */
Bhumika Goyal7c0ddf02017-08-12 21:01:18 +0530111static const struct snd_pcm_hardware snd_p16v_playback_hw = {
Clemens Ladischb83f3462007-08-13 17:37:55 +0200112 .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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 .formats = SNDRV_PCM_FMTBIT_S32_LE, /* Only supports 24-bit samples padded to 32 bits. */
James Courtier-Dutton001f7582005-04-09 23:38:25 +0200119 .rates = SNDRV_PCM_RATE_192000 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_44100,
120 .rate_min = 44100,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .rate_max = 192000,
122 .channels_min = 8,
123 .channels_max = 8,
James Courtier-Dutton310bacd2005-04-10 00:00:24 +0200124 .buffer_bytes_max = ((65536 - 64) * 8),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 .period_bytes_min = 64,
James Courtier-Dutton310bacd2005-04-10 00:00:24 +0200126 .period_bytes_max = (65536 - 64),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 .periods_min = 2,
128 .periods_max = 8,
129 .fifo_size = 0,
130};
131
Bhumika Goyal7c0ddf02017-08-12 21:01:18 +0530132static const struct snd_pcm_hardware snd_p16v_capture_hw = {
James Courtier-Duttonc7025632005-05-04 17:26:28 +0200133 .info = (SNDRV_PCM_INFO_MMAP |
134 SNDRV_PCM_INFO_INTERLEAVED |
135 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Takashi Iwai09668b42005-11-17 16:14:10 +0100136 SNDRV_PCM_INFO_RESUME |
James Courtier-Duttonc7025632005-05-04 17:26:28 +0200137 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,
James Courtier-Dutton22bc30c2005-05-07 16:13:40 +0200144 .buffer_bytes_max = (65536 - 64),
James Courtier-Duttonc7025632005-05-04 17:26:28 +0200145 .period_bytes_min = 64,
James Courtier-Dutton22bc30c2005-05-07 16:13:40 +0200146 .period_bytes_max = (65536 - 128) >> 1, /* size has to be N*64 bytes */
James Courtier-Duttonc7025632005-05-04 17:26:28 +0200147 .periods_min = 2,
148 .periods_max = 2,
149 .fifo_size = 0,
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100150};
151
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100152static void snd_p16v_pcm_free_substream(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100154 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Markus Elfringa8279772015-01-03 17:06:04 +0100155
156 kfree(epcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
159/* open_playback callback */
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100160static int snd_p16v_pcm_open_playback_channel(struct snd_pcm_substream *substream, int channel_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100162 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
163 struct snd_emu10k1_voice *channel = &(emu->p16v_voices[channel_id]);
164 struct snd_emu10k1_pcm *epcm;
165 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 int err;
167
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200168 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
Takashi Iwai6f002b02014-02-25 17:02:09 +0100169 /* dev_dbg(emu->card->dev, "epcm kcalloc: %p\n", epcm); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 if (epcm == NULL)
172 return -ENOMEM;
173 epcm->emu = emu;
174 epcm->substream = substream;
Takashi Iwai28a97c12009-02-05 16:08:14 +0100175 /*
Takashi Iwai6f002b02014-02-25 17:02:09 +0100176 dev_dbg(emu->card->dev, "epcm device=%d, channel_id=%d\n",
Takashi Iwai28a97c12009-02-05 16:08:14 +0100177 substream->pcm->device, channel_id);
178 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 runtime->private_data = epcm;
180 runtime->private_free = snd_p16v_pcm_free_substream;
181
182 runtime->hw = snd_p16v_playback_hw;
183
184 channel->emu = emu;
185 channel->number = channel_id;
186
187 channel->use=1;
Takashi Iwai28a97c12009-02-05 16:08:14 +0100188#if 0 /* debug */
Takashi Iwai6f002b02014-02-25 17:02:09 +0100189 dev_dbg(emu->card->dev,
Takashi Iwai28a97c12009-02-05 16:08:14 +0100190 "p16v: open channel_id=%d, channel=%p, use=0x%x\n",
191 channel_id, channel, channel->use);
Takashi Iwai6f002b02014-02-25 17:02:09 +0100192 dev_dbg(emu->card->dev, "open:channel_id=%d, chip=%p, channel=%p\n",
Takashi Iwai28a97c12009-02-05 16:08:14 +0100193 channel_id, chip, channel);
194#endif /* debug */
195 /* channel->interrupt = snd_p16v_pcm_channel_interrupt; */
196 channel->epcm = epcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
198 return err;
199
Clemens Ladischb83f3462007-08-13 17:37:55 +0200200 runtime->sync.id32[0] = substream->pcm->card->number;
201 runtime->sync.id32[1] = 'P';
202 runtime->sync.id32[2] = 16;
203 runtime->sync.id32[3] = 'V';
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return 0;
206}
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100207/* open_capture callback */
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100208static int snd_p16v_pcm_open_capture_channel(struct snd_pcm_substream *substream, int channel_id)
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100209{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100210 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
211 struct snd_emu10k1_voice *channel = &(emu->p16v_capture_voice);
212 struct snd_emu10k1_pcm *epcm;
213 struct snd_pcm_runtime *runtime = substream->runtime;
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100214 int err;
215
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200216 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
Takashi Iwai6f002b02014-02-25 17:02:09 +0100217 /* dev_dbg(emu->card->dev, "epcm kcalloc: %p\n", epcm); */
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100218
219 if (epcm == NULL)
220 return -ENOMEM;
221 epcm->emu = emu;
222 epcm->substream = substream;
Takashi Iwai28a97c12009-02-05 16:08:14 +0100223 /*
Takashi Iwai6f002b02014-02-25 17:02:09 +0100224 dev_dbg(emu->card->dev, "epcm device=%d, channel_id=%d\n",
Takashi Iwai28a97c12009-02-05 16:08:14 +0100225 substream->pcm->device, channel_id);
226 */
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100227 runtime->private_data = epcm;
228 runtime->private_free = snd_p16v_pcm_free_substream;
229
230 runtime->hw = snd_p16v_capture_hw;
231
232 channel->emu = emu;
233 channel->number = channel_id;
234
235 channel->use=1;
Takashi Iwai28a97c12009-02-05 16:08:14 +0100236#if 0 /* debug */
Takashi Iwai6f002b02014-02-25 17:02:09 +0100237 dev_dbg(emu->card->dev,
Takashi Iwai28a97c12009-02-05 16:08:14 +0100238 "p16v: open channel_id=%d, channel=%p, use=0x%x\n",
239 channel_id, channel, channel->use);
Takashi Iwai6f002b02014-02-25 17:02:09 +0100240 dev_dbg(emu->card->dev, "open:channel_id=%d, chip=%p, channel=%p\n",
Takashi Iwai28a97c12009-02-05 16:08:14 +0100241 channel_id, chip, channel);
242#endif /* debug */
243 /* channel->interrupt = snd_p16v_pcm_channel_interrupt; */
244 channel->epcm = epcm;
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100245 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
246 return err;
247
248 return 0;
249}
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252/* close callback */
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100253static int snd_p16v_pcm_close_playback(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100255 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
256 //struct snd_pcm_runtime *runtime = substream->runtime;
257 //struct snd_emu10k1_pcm *epcm = runtime->private_data;
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100258 emu->p16v_voices[substream->pcm->device - emu->p16v_device_offset].use = 0;
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100259 /* FIXME: maybe zero others */
260 return 0;
261}
262
263/* close callback */
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100264static int snd_p16v_pcm_close_capture(struct snd_pcm_substream *substream)
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100265{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100266 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
267 //struct snd_pcm_runtime *runtime = substream->runtime;
268 //struct snd_emu10k1_pcm *epcm = runtime->private_data;
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100269 emu->p16v_capture_voice.use = 0;
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100270 /* FIXME: maybe zero others */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return 0;
272}
273
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100274static int snd_p16v_pcm_open_playback_front(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 return snd_p16v_pcm_open_playback_channel(substream, PCM_FRONT_CHANNEL);
277}
278
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100279static int snd_p16v_pcm_open_capture(struct snd_pcm_substream *substream)
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100280{
281 // Only using channel 0 for now, but the card has 2 channels.
282 return snd_p16v_pcm_open_capture_channel(substream, 0);
283}
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285/* hw_params callback */
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100286static int snd_p16v_pcm_hw_params_playback(struct snd_pcm_substream *substream,
287 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Masahiro Yamada44cc4a02016-09-06 20:41:19 +0900289 return snd_pcm_lib_malloc_pages(substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 params_buffer_bytes(hw_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100293/* hw_params callback */
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100294static int snd_p16v_pcm_hw_params_capture(struct snd_pcm_substream *substream,
295 struct snd_pcm_hw_params *hw_params)
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100296{
Masahiro Yamada44cc4a02016-09-06 20:41:19 +0900297 return snd_pcm_lib_malloc_pages(substream,
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100298 params_buffer_bytes(hw_params));
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100299}
300
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302/* hw_free callback */
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100303static int snd_p16v_pcm_hw_free_playback(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Masahiro Yamada44cc4a02016-09-06 20:41:19 +0900305 return snd_pcm_lib_free_pages(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100308/* hw_free callback */
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100309static int snd_p16v_pcm_hw_free_capture(struct snd_pcm_substream *substream)
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100310{
Masahiro Yamada44cc4a02016-09-06 20:41:19 +0900311 return snd_pcm_lib_free_pages(substream);
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100312}
313
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315/* prepare playback callback */
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100316static int snd_p16v_pcm_prepare_playback(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100318 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
319 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 int channel = substream->pcm->device - emu->p16v_device_offset;
321 u32 *table_base = (u32 *)(emu->p16v_buffer.area+(8*16*channel));
322 u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
323 int i;
324 u32 tmp;
325
Takashi Iwai28a97c12009-02-05 16:08:14 +0100326#if 0 /* debug */
Takashi Iwai6f002b02014-02-25 17:02:09 +0100327 dev_dbg(emu->card->dev,
328 "prepare:channel_number=%d, rate=%d, "
Takashi Iwai28a97c12009-02-05 16:08:14 +0100329 "format=0x%x, channels=%d, buffer_size=%ld, "
330 "period_size=%ld, periods=%u, frames_to_bytes=%d\n",
331 channel, runtime->rate, runtime->format, runtime->channels,
332 runtime->buffer_size, runtime->period_size,
333 runtime->periods, frames_to_bytes(runtime, 1));
Takashi Iwai6f002b02014-02-25 17:02:09 +0100334 dev_dbg(emu->card->dev,
335 "dma_addr=%x, dma_area=%p, table_base=%p\n",
Takashi Iwai28a97c12009-02-05 16:08:14 +0100336 runtime->dma_addr, runtime->dma_area, table_base);
Takashi Iwai6f002b02014-02-25 17:02:09 +0100337 dev_dbg(emu->card->dev,
338 "dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",
Takashi Iwai28a97c12009-02-05 16:08:14 +0100339 emu->p16v_buffer.addr, emu->p16v_buffer.area,
340 emu->p16v_buffer.bytes);
341#endif /* debug */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 tmp = snd_emu10k1_ptr_read(emu, A_SPDIF_SAMPLERATE, channel);
343 switch (runtime->rate) {
344 case 44100:
James Courtier-Dutton001f7582005-04-09 23:38:25 +0200345 snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0xe0e0) | 0x8080);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 break;
347 case 96000:
James Courtier-Dutton001f7582005-04-09 23:38:25 +0200348 snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0xe0e0) | 0x4040);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 break;
350 case 192000:
James Courtier-Dutton001f7582005-04-09 23:38:25 +0200351 snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0xe0e0) | 0x2020);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 break;
James Courtier-Dutton001f7582005-04-09 23:38:25 +0200353 case 48000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 default:
James Courtier-Dutton001f7582005-04-09 23:38:25 +0200355 snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0xe0e0) | 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 break;
357 }
358 /* FIXME: Check emu->buffer.size before actually writing to it. */
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100359 for(i = 0; i < runtime->periods; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 table_base[i*2]=runtime->dma_addr+(i*period_size_bytes);
361 table_base[(i*2)+1]=period_size_bytes<<16;
362 }
363
364 snd_emu10k1_ptr20_write(emu, PLAYBACK_LIST_ADDR, channel, emu->p16v_buffer.addr+(8*16*channel));
365 snd_emu10k1_ptr20_write(emu, PLAYBACK_LIST_SIZE, channel, (runtime->periods - 1) << 19);
366 snd_emu10k1_ptr20_write(emu, PLAYBACK_LIST_PTR, channel, 0);
367 snd_emu10k1_ptr20_write(emu, PLAYBACK_DMA_ADDR, channel, runtime->dma_addr);
James Courtier-Dutton310bacd2005-04-10 00:00:24 +0200368 //snd_emu10k1_ptr20_write(emu, PLAYBACK_PERIOD_SIZE, channel, frames_to_bytes(runtime, runtime->period_size)<<16); // buffer size in bytes
369 snd_emu10k1_ptr20_write(emu, PLAYBACK_PERIOD_SIZE, channel, 0); // buffer size in bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 snd_emu10k1_ptr20_write(emu, PLAYBACK_POINTER, channel, 0);
371 snd_emu10k1_ptr20_write(emu, 0x07, channel, 0x0);
372 snd_emu10k1_ptr20_write(emu, 0x08, channel, 0);
373
374 return 0;
375}
376
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100377/* prepare capture callback */
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100378static int snd_p16v_pcm_prepare_capture(struct snd_pcm_substream *substream)
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100379{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100380 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
381 struct snd_pcm_runtime *runtime = substream->runtime;
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100382 int channel = substream->pcm->device - emu->p16v_device_offset;
James Courtier-Duttonc7025632005-05-04 17:26:28 +0200383 u32 tmp;
Takashi Iwai28a97c12009-02-05 16:08:14 +0100384
385 /*
Takashi Iwai6f002b02014-02-25 17:02:09 +0100386 dev_dbg(emu->card->dev, "prepare capture:channel_number=%d, rate=%d, "
Takashi Iwai28a97c12009-02-05 16:08:14 +0100387 "format=0x%x, channels=%d, buffer_size=%ld, period_size=%ld, "
388 "frames_to_bytes=%d\n",
389 channel, runtime->rate, runtime->format, runtime->channels,
390 runtime->buffer_size, runtime->period_size,
391 frames_to_bytes(runtime, 1));
392 */
James Courtier-Duttonc7025632005-05-04 17:26:28 +0200393 tmp = snd_emu10k1_ptr_read(emu, A_SPDIF_SAMPLERATE, channel);
394 switch (runtime->rate) {
395 case 44100:
396 snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0x0e00) | 0x0800);
397 break;
398 case 96000:
399 snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0x0e00) | 0x0400);
400 break;
401 case 192000:
402 snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0x0e00) | 0x0200);
403 break;
404 case 48000:
405 default:
406 snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0x0e00) | 0x0000);
407 break;
408 }
409 /* FIXME: Check emu->buffer.size before actually writing to it. */
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100410 snd_emu10k1_ptr20_write(emu, 0x13, channel, 0);
411 snd_emu10k1_ptr20_write(emu, CAPTURE_DMA_ADDR, channel, runtime->dma_addr);
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100412 snd_emu10k1_ptr20_write(emu, CAPTURE_BUFFER_SIZE, channel, frames_to_bytes(runtime, runtime->buffer_size) << 16); // buffer size in bytes
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100413 snd_emu10k1_ptr20_write(emu, CAPTURE_POINTER, channel, 0);
414 //snd_emu10k1_ptr20_write(emu, CAPTURE_SOURCE, 0x0, 0x333300e4); /* Select MIC or Line in */
415 //snd_emu10k1_ptr20_write(emu, EXTENDED_INT_MASK, 0, snd_emu10k1_ptr20_read(emu, EXTENDED_INT_MASK, 0) | (0x110000<<channel));
416
417 return 0;
418}
419
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100420static void snd_p16v_intr_enable(struct snd_emu10k1 *emu, unsigned int intrenb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 unsigned long flags;
423 unsigned int enable;
424
425 spin_lock_irqsave(&emu->emu_lock, flags);
426 enable = inl(emu->port + INTE2) | intrenb;
427 outl(enable, emu->port + INTE2);
428 spin_unlock_irqrestore(&emu->emu_lock, flags);
429}
430
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100431static void snd_p16v_intr_disable(struct snd_emu10k1 *emu, unsigned int intrenb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 unsigned long flags;
434 unsigned int disable;
435
436 spin_lock_irqsave(&emu->emu_lock, flags);
437 disable = inl(emu->port + INTE2) & (~intrenb);
438 outl(disable, emu->port + INTE2);
439 spin_unlock_irqrestore(&emu->emu_lock, flags);
440}
441
442/* trigger_playback callback */
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100443static int snd_p16v_pcm_trigger_playback(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 int cmd)
445{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100446 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
447 struct snd_pcm_runtime *runtime;
448 struct snd_emu10k1_pcm *epcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 int channel;
450 int result = 0;
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100451 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 u32 basic = 0;
453 u32 inte = 0;
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100454 int running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 switch (cmd) {
457 case SNDRV_PCM_TRIGGER_START:
458 running=1;
459 break;
460 case SNDRV_PCM_TRIGGER_STOP:
461 default:
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100462 running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 break;
464 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100465 snd_pcm_group_for_each_entry(s, substream) {
Clemens Ladisch29998d22007-07-30 08:14:31 +0200466 if (snd_pcm_substream_chip(s) != emu ||
467 s->stream != SNDRV_PCM_STREAM_PLAYBACK)
468 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 runtime = s->runtime;
470 epcm = runtime->private_data;
471 channel = substream->pcm->device-emu->p16v_device_offset;
Takashi Iwai6f002b02014-02-25 17:02:09 +0100472 /* dev_dbg(emu->card->dev, "p16v channel=%d\n", channel); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 epcm->running = running;
474 basic |= (0x1<<channel);
475 inte |= (INTE2_PLAYBACK_CH_0_LOOP<<channel);
476 snd_pcm_trigger_done(s, substream);
477 }
Takashi Iwai6f002b02014-02-25 17:02:09 +0100478 /* dev_dbg(emu->card->dev, "basic=0x%x, inte=0x%x\n", basic, inte); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 switch (cmd) {
481 case SNDRV_PCM_TRIGGER_START:
482 snd_p16v_intr_enable(emu, inte);
483 snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0)| (basic));
484 break;
485 case SNDRV_PCM_TRIGGER_STOP:
486 snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0) & ~(basic));
487 snd_p16v_intr_disable(emu, inte);
488 break;
489 default:
490 result = -EINVAL;
491 break;
492 }
493 return result;
494}
495
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100496/* trigger_capture callback */
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100497static int snd_p16v_pcm_trigger_capture(struct snd_pcm_substream *substream,
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100498 int cmd)
499{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100500 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
501 struct snd_pcm_runtime *runtime = substream->runtime;
502 struct snd_emu10k1_pcm *epcm = runtime->private_data;
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100503 int channel = 0;
504 int result = 0;
505 u32 inte = INTE2_CAPTURE_CH_0_LOOP | INTE2_CAPTURE_CH_0_HALF_LOOP;
506
507 switch (cmd) {
508 case SNDRV_PCM_TRIGGER_START:
509 snd_p16v_intr_enable(emu, inte);
510 snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0)|(0x100<<channel));
511 epcm->running = 1;
512 break;
513 case SNDRV_PCM_TRIGGER_STOP:
514 snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0) & ~(0x100<<channel));
515 snd_p16v_intr_disable(emu, inte);
516 //snd_emu10k1_ptr20_write(emu, EXTENDED_INT_MASK, 0, snd_emu10k1_ptr20_read(emu, EXTENDED_INT_MASK, 0) & ~(0x110000<<channel));
517 epcm->running = 0;
518 break;
519 default:
520 result = -EINVAL;
521 break;
522 }
523 return result;
524}
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526/* pointer_playback callback */
527static snd_pcm_uframes_t
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100528snd_p16v_pcm_pointer_playback(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100530 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
531 struct snd_pcm_runtime *runtime = substream->runtime;
532 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 snd_pcm_uframes_t ptr, ptr1, ptr2,ptr3,ptr4 = 0;
534 int channel = substream->pcm->device - emu->p16v_device_offset;
535 if (!epcm->running)
536 return 0;
537
538 ptr3 = snd_emu10k1_ptr20_read(emu, PLAYBACK_LIST_PTR, channel);
539 ptr1 = snd_emu10k1_ptr20_read(emu, PLAYBACK_POINTER, channel);
540 ptr4 = snd_emu10k1_ptr20_read(emu, PLAYBACK_LIST_PTR, channel);
541 if (ptr3 != ptr4) ptr1 = snd_emu10k1_ptr20_read(emu, PLAYBACK_POINTER, channel);
542 ptr2 = bytes_to_frames(runtime, ptr1);
543 ptr2+= (ptr4 >> 3) * runtime->period_size;
544 ptr=ptr2;
545 if (ptr >= runtime->buffer_size)
546 ptr -= runtime->buffer_size;
547
548 return ptr;
549}
550
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100551/* pointer_capture callback */
552static snd_pcm_uframes_t
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100553snd_p16v_pcm_pointer_capture(struct snd_pcm_substream *substream)
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100554{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100555 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
556 struct snd_pcm_runtime *runtime = substream->runtime;
557 struct snd_emu10k1_pcm *epcm = runtime->private_data;
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100558 snd_pcm_uframes_t ptr, ptr1, ptr2 = 0;
559 int channel = 0;
560
561 if (!epcm->running)
562 return 0;
563
564 ptr1 = snd_emu10k1_ptr20_read(emu, CAPTURE_POINTER, channel);
565 ptr2 = bytes_to_frames(runtime, ptr1);
566 ptr=ptr2;
567 if (ptr >= runtime->buffer_size) {
568 ptr -= runtime->buffer_size;
Takashi Iwai6f002b02014-02-25 17:02:09 +0100569 dev_warn(emu->card->dev, "buffer capture limited!\n");
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100570 }
Takashi Iwai28a97c12009-02-05 16:08:14 +0100571 /*
Takashi Iwai6f002b02014-02-25 17:02:09 +0100572 dev_dbg(emu->card->dev, "ptr1 = 0x%lx, ptr2=0x%lx, ptr=0x%lx, "
Takashi Iwai28a97c12009-02-05 16:08:14 +0100573 "buffer_size = 0x%x, period_size = 0x%x, bits=%d, rate=%d\n",
574 ptr1, ptr2, ptr, (int)runtime->buffer_size,
575 (int)runtime->period_size, (int)runtime->frame_bits,
576 (int)runtime->rate);
577 */
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100578 return ptr;
579}
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581/* operators */
Julia Lawall6769e9882016-09-02 00:13:10 +0200582static const struct snd_pcm_ops snd_p16v_playback_front_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 .open = snd_p16v_pcm_open_playback_front,
584 .close = snd_p16v_pcm_close_playback,
585 .ioctl = snd_pcm_lib_ioctl,
586 .hw_params = snd_p16v_pcm_hw_params_playback,
587 .hw_free = snd_p16v_pcm_hw_free_playback,
588 .prepare = snd_p16v_pcm_prepare_playback,
589 .trigger = snd_p16v_pcm_trigger_playback,
590 .pointer = snd_p16v_pcm_pointer_playback,
591};
592
Julia Lawall6769e9882016-09-02 00:13:10 +0200593static const struct snd_pcm_ops snd_p16v_capture_ops = {
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100594 .open = snd_p16v_pcm_open_capture,
595 .close = snd_p16v_pcm_close_capture,
596 .ioctl = snd_pcm_lib_ioctl,
597 .hw_params = snd_p16v_pcm_hw_params_capture,
598 .hw_free = snd_p16v_pcm_hw_free_capture,
599 .prepare = snd_p16v_pcm_prepare_capture,
600 .trigger = snd_p16v_pcm_trigger_capture,
601 .pointer = snd_p16v_pcm_pointer_capture,
602};
603
604
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100605int snd_p16v_free(struct snd_emu10k1 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 // release the data
608 if (chip->p16v_buffer.area) {
609 snd_dma_free_pages(&chip->p16v_buffer);
Takashi Iwai28a97c12009-02-05 16:08:14 +0100610 /*
Takashi Iwai6f002b02014-02-25 17:02:09 +0100611 dev_dbg(chip->card->dev, "period lables free: %p\n",
Takashi Iwai28a97c12009-02-05 16:08:14 +0100612 &chip->p16v_buffer);
613 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615 return 0;
616}
617
Lars-Peter Clausenbb814c32015-01-02 12:24:49 +0100618int snd_p16v_pcm(struct snd_emu10k1 *emu, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100620 struct snd_pcm *pcm;
621 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 int err;
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100623 int capture=1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Takashi Iwai6f002b02014-02-25 17:02:09 +0100625 /* dev_dbg(emu->card->dev, "snd_p16v_pcm called. device=%d\n", device); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 emu->p16v_device_offset = device;
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if ((err = snd_pcm_new(emu->card, "p16v", device, 1, capture, &pcm)) < 0)
629 return err;
630
631 pcm->private_data = emu;
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100632 // Single playback 8 channel device.
633 // Single capture 2 channel device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_p16v_playback_front_ops);
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100635 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_p16v_capture_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 pcm->info_flags = 0;
638 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
639 strcpy(pcm->name, "p16v");
Takashi Iwai09668b42005-11-17 16:14:10 +0100640 emu->pcm_p16v = pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 for(substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
643 substream;
644 substream = substream->next) {
Takashi Iwai5116b942019-02-04 16:32:09 +0100645 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV,
646 snd_dma_pci_data(emu->pci),
647 (65536 - 64) * 8,
648 (65536 - 64) * 8);
Takashi Iwai28a97c12009-02-05 16:08:14 +0100649 /*
Takashi Iwai6f002b02014-02-25 17:02:09 +0100650 dev_dbg(emu->card->dev,
Takashi Iwai28a97c12009-02-05 16:08:14 +0100651 "preallocate playback substream: err=%d\n", err);
652 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654
655 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
656 substream;
657 substream = substream->next) {
Takashi Iwai5116b942019-02-04 16:32:09 +0100658 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV,
659 snd_dma_pci_data(emu->pci),
660 65536 - 64, 65536 - 64);
Takashi Iwai28a97c12009-02-05 16:08:14 +0100661 /*
Takashi Iwai6f002b02014-02-25 17:02:09 +0100662 dev_dbg(emu->card->dev,
Takashi Iwai28a97c12009-02-05 16:08:14 +0100663 "preallocate capture substream: err=%d\n", err);
664 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return 0;
668}
669
Takashi Iwaieff49132005-12-22 17:01:20 +0100670static int snd_p16v_volume_info(struct snd_kcontrol *kcontrol,
671 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
674 uinfo->count = 2;
675 uinfo->value.integer.min = 0;
676 uinfo->value.integer.max = 255;
677 return 0;
678}
679
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100680static int snd_p16v_volume_get(struct snd_kcontrol *kcontrol,
Takashi Iwaieff49132005-12-22 17:01:20 +0100681 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100683 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
Takashi Iwaieff49132005-12-22 17:01:20 +0100684 int high_low = (kcontrol->private_value >> 8) & 0xff;
685 int reg = kcontrol->private_value & 0xff;
686 u32 value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Takashi Iwaieff49132005-12-22 17:01:20 +0100688 value = snd_emu10k1_ptr20_read(emu, reg, high_low);
689 if (high_low) {
690 ucontrol->value.integer.value[0] = 0xff - ((value >> 24) & 0xff); /* Left */
691 ucontrol->value.integer.value[1] = 0xff - ((value >> 16) & 0xff); /* Right */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 } else {
Takashi Iwaieff49132005-12-22 17:01:20 +0100693 ucontrol->value.integer.value[0] = 0xff - ((value >> 8) & 0xff); /* Left */
694 ucontrol->value.integer.value[1] = 0xff - ((value >> 0) & 0xff); /* Right */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
Takashi Iwaieff49132005-12-22 17:01:20 +0100696 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100699static int snd_p16v_volume_put(struct snd_kcontrol *kcontrol,
Takashi Iwaieff49132005-12-22 17:01:20 +0100700 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100702 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
Takashi Iwaieff49132005-12-22 17:01:20 +0100703 int high_low = (kcontrol->private_value >> 8) & 0xff;
704 int reg = kcontrol->private_value & 0xff;
705 u32 value, oval;
706
707 oval = value = snd_emu10k1_ptr20_read(emu, reg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (high_low == 1) {
709 value &= 0xffff;
Takashi Iwaieff49132005-12-22 17:01:20 +0100710 value |= ((0xff - ucontrol->value.integer.value[0]) << 24) |
711 ((0xff - ucontrol->value.integer.value[1]) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 } else {
713 value &= 0xffff0000;
Takashi Iwaieff49132005-12-22 17:01:20 +0100714 value |= ((0xff - ucontrol->value.integer.value[0]) << 8) |
715 ((0xff - ucontrol->value.integer.value[1]) );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
Takashi Iwaieff49132005-12-22 17:01:20 +0100717 if (value != oval) {
718 snd_emu10k1_ptr20_write(emu, reg, 0, value);
719 return 1;
720 }
721 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
Takashi Iwaieff49132005-12-22 17:01:20 +0100724static int snd_p16v_capture_source_info(struct snd_kcontrol *kcontrol,
725 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Takashi Iwai1541c662014-10-20 18:16:48 +0200727 static const char * const texts[8] = {
Takashi Iwaieff49132005-12-22 17:01:20 +0100728 "SPDIF", "I2S", "SRC48", "SRCMulti_SPDIF", "SRCMulti_I2S",
729 "CDIF", "FX", "AC97"
730 };
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100731
Takashi Iwai1541c662014-10-20 18:16:48 +0200732 return snd_ctl_enum_info(uinfo, 1, 8, texts);
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100733}
734
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100735static int snd_p16v_capture_source_get(struct snd_kcontrol *kcontrol,
736 struct snd_ctl_elem_value *ucontrol)
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100737{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100738 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100739
740 ucontrol->value.enumerated.item[0] = emu->p16v_capture_source;
741 return 0;
742}
743
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100744static int snd_p16v_capture_source_put(struct snd_kcontrol *kcontrol,
745 struct snd_ctl_elem_value *ucontrol)
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100746{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100747 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100748 unsigned int val;
749 int change = 0;
750 u32 mask;
751 u32 source;
752
753 val = ucontrol->value.enumerated.item[0] ;
Takashi Iwaiaa299d02007-11-15 13:16:02 +0100754 if (val > 7)
755 return -EINVAL;
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100756 change = (emu->p16v_capture_source != val);
757 if (change) {
758 emu->p16v_capture_source = val;
759 source = (val << 28) | (val << 24) | (val << 20) | (val << 16);
760 mask = snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0) & 0xffff;
761 snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, source | mask);
762 }
763 return change;
764}
765
Takashi Iwaieff49132005-12-22 17:01:20 +0100766static int snd_p16v_capture_channel_info(struct snd_kcontrol *kcontrol,
767 struct snd_ctl_elem_info *uinfo)
James Courtier-Duttonf927c8f2005-05-07 15:34:13 +0200768{
Takashi Iwai1541c662014-10-20 18:16:48 +0200769 static const char * const texts[4] = { "0", "1", "2", "3", };
James Courtier-Duttonf927c8f2005-05-07 15:34:13 +0200770
Takashi Iwai1541c662014-10-20 18:16:48 +0200771 return snd_ctl_enum_info(uinfo, 1, 4, texts);
James Courtier-Duttonf927c8f2005-05-07 15:34:13 +0200772}
773
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100774static int snd_p16v_capture_channel_get(struct snd_kcontrol *kcontrol,
775 struct snd_ctl_elem_value *ucontrol)
James Courtier-Duttonf927c8f2005-05-07 15:34:13 +0200776{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100777 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
James Courtier-Duttonf927c8f2005-05-07 15:34:13 +0200778
779 ucontrol->value.enumerated.item[0] = emu->p16v_capture_channel;
780 return 0;
781}
782
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100783static int snd_p16v_capture_channel_put(struct snd_kcontrol *kcontrol,
784 struct snd_ctl_elem_value *ucontrol)
James Courtier-Duttonf927c8f2005-05-07 15:34:13 +0200785{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100786 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
James Courtier-Duttonf927c8f2005-05-07 15:34:13 +0200787 unsigned int val;
788 int change = 0;
789 u32 tmp;
790
791 val = ucontrol->value.enumerated.item[0] ;
Takashi Iwaiaa299d02007-11-15 13:16:02 +0100792 if (val > 3)
793 return -EINVAL;
James Courtier-Duttonf927c8f2005-05-07 15:34:13 +0200794 change = (emu->p16v_capture_channel != val);
795 if (change) {
796 emu->p16v_capture_channel = val;
797 tmp = snd_emu10k1_ptr20_read(emu, CAPTURE_P16V_SOURCE, 0) & 0xfffc;
798 snd_emu10k1_ptr20_write(emu, CAPTURE_P16V_SOURCE, 0, tmp | val);
799 }
800 return change;
801}
Takashi Iwai0cb29ea2007-01-29 15:33:49 +0100802static const DECLARE_TLV_DB_SCALE(snd_p16v_db_scale1, -5175, 25, 1);
James Courtier-Duttonf927c8f2005-05-07 15:34:13 +0200803
Takashi Iwaieff49132005-12-22 17:01:20 +0100804#define P16V_VOL(xname,xreg,xhl) { \
805 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
James Courtier-Dutton31508f82006-07-22 17:02:10 +0100806 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
807 SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
Takashi Iwaieff49132005-12-22 17:01:20 +0100808 .info = snd_p16v_volume_info, \
809 .get = snd_p16v_volume_get, \
810 .put = snd_p16v_volume_put, \
Takashi Iwai7cf0a952006-08-17 16:23:07 +0200811 .tlv = { .p = snd_p16v_db_scale1 }, \
Takashi Iwaieff49132005-12-22 17:01:20 +0100812 .private_value = ((xreg) | ((xhl) << 8)) \
813}
814
Bill Pembertone23e7a12012-12-06 12:35:10 -0500815static struct snd_kcontrol_new p16v_mixer_controls[] = {
Takashi Iwaieff49132005-12-22 17:01:20 +0100816 P16V_VOL("HD Analog Front Playback Volume", PLAYBACK_VOLUME_MIXER9, 0),
817 P16V_VOL("HD Analog Rear Playback Volume", PLAYBACK_VOLUME_MIXER10, 1),
818 P16V_VOL("HD Analog Center/LFE Playback Volume", PLAYBACK_VOLUME_MIXER9, 1),
819 P16V_VOL("HD Analog Side Playback Volume", PLAYBACK_VOLUME_MIXER10, 0),
820 P16V_VOL("HD SPDIF Front Playback Volume", PLAYBACK_VOLUME_MIXER7, 0),
821 P16V_VOL("HD SPDIF Rear Playback Volume", PLAYBACK_VOLUME_MIXER8, 1),
822 P16V_VOL("HD SPDIF Center/LFE Playback Volume", PLAYBACK_VOLUME_MIXER7, 1),
823 P16V_VOL("HD SPDIF Side Playback Volume", PLAYBACK_VOLUME_MIXER8, 0),
824 {
825 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
826 .name = "HD source Capture",
827 .info = snd_p16v_capture_source_info,
828 .get = snd_p16v_capture_source_get,
829 .put = snd_p16v_capture_source_put
830 },
831 {
832 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
833 .name = "HD channel Capture",
834 .info = snd_p16v_capture_channel_info,
835 .get = snd_p16v_capture_channel_get,
836 .put = snd_p16v_capture_channel_put
837 },
James Courtier-Duttonf927c8f2005-05-07 15:34:13 +0200838};
839
Takashi Iwaieff49132005-12-22 17:01:20 +0100840
Bill Pembertone23e7a12012-12-06 12:35:10 -0500841int snd_p16v_mixer(struct snd_emu10k1 *emu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Takashi Iwaieff49132005-12-22 17:01:20 +0100843 int i, err;
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100844 struct snd_card *card = emu->card;
Takashi Iwaieff49132005-12-22 17:01:20 +0100845
846 for (i = 0; i < ARRAY_SIZE(p16v_mixer_controls); i++) {
847 if ((err = snd_ctl_add(card, snd_ctl_new1(&p16v_mixer_controls[i],
848 emu))) < 0)
849 return err;
850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return 0;
852}
853
Takashi Iwaic7561cd2012-08-14 18:12:04 +0200854#ifdef CONFIG_PM_SLEEP
Takashi Iwai09668b42005-11-17 16:14:10 +0100855
856#define NUM_CHS 1 /* up to 4, but only first channel is used */
857
Bill Pembertone23e7a12012-12-06 12:35:10 -0500858int snd_p16v_alloc_pm_buffer(struct snd_emu10k1 *emu)
Takashi Iwai09668b42005-11-17 16:14:10 +0100859{
Kees Cook42bc47b2018-06-12 14:27:11 -0700860 emu->p16v_saved = vmalloc(array_size(NUM_CHS * 4, 0x80));
Takashi Iwai09668b42005-11-17 16:14:10 +0100861 if (! emu->p16v_saved)
862 return -ENOMEM;
863 return 0;
864}
865
866void snd_p16v_free_pm_buffer(struct snd_emu10k1 *emu)
867{
868 vfree(emu->p16v_saved);
869}
870
871void snd_p16v_suspend(struct snd_emu10k1 *emu)
872{
873 int i, ch;
874 unsigned int *val;
875
876 val = emu->p16v_saved;
877 for (ch = 0; ch < NUM_CHS; ch++)
878 for (i = 0; i < 0x80; i++, val++)
879 *val = snd_emu10k1_ptr20_read(emu, i, ch);
880}
881
882void snd_p16v_resume(struct snd_emu10k1 *emu)
883{
884 int i, ch;
885 unsigned int *val;
886
887 val = emu->p16v_saved;
888 for (ch = 0; ch < NUM_CHS; ch++)
889 for (i = 0; i < 0x80; i++, val++)
890 snd_emu10k1_ptr20_write(emu, i, ch, *val);
891}
892#endif