blob: 2f1a85185a16afc618f96273223f80cda384f738 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ALSA driver for RME Digi96, Digi96/8 and Digi96/8 PRO/PAD/PST audio
3 * interfaces
4 *
5 * Copyright (c) 2000, 2001 Anders Torger <torger@ludd.luth.se>
6 *
7 * Thanks to Henk Hesselink <henk@anda.nl> for the analog volume control
8 * code.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 */
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/delay.h>
27#include <linux/init.h>
28#include <linux/interrupt.h>
29#include <linux/pci.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040030#include <linux/module.h>
Knut Petersena8cc2092013-08-22 14:36:16 +020031#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <sound/core.h>
34#include <sound/info.h>
35#include <sound/control.h>
36#include <sound/pcm.h>
37#include <sound/pcm_params.h>
38#include <sound/asoundef.h>
39#include <sound/initval.h>
40
41#include <asm/io.h>
42
43/* note, two last pcis should be equal, it is not a bug */
44
45MODULE_AUTHOR("Anders Torger <torger@ludd.luth.se>");
46MODULE_DESCRIPTION("RME Digi96, Digi96/8, Digi96/8 PRO, Digi96/8 PST, "
47 "Digi96/8 PAD");
48MODULE_LICENSE("GPL");
49MODULE_SUPPORTED_DEVICE("{{RME,Digi96},"
50 "{RME,Digi96/8},"
51 "{RME,Digi96/8 PRO},"
52 "{RME,Digi96/8 PST},"
53 "{RME,Digi96/8 PAD}}");
54
55static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
56static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103057static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59module_param_array(index, int, NULL, 0444);
60MODULE_PARM_DESC(index, "Index value for RME Digi96 soundcard.");
61module_param_array(id, charp, NULL, 0444);
62MODULE_PARM_DESC(id, "ID string for RME Digi96 soundcard.");
63module_param_array(enable, bool, NULL, 0444);
64MODULE_PARM_DESC(enable, "Enable RME Digi96 soundcard.");
65
66/*
67 * Defines for RME Digi96 series, from internal RME reference documents
68 * dated 12.01.00
69 */
70
71#define RME96_SPDIF_NCHANNELS 2
72
73/* Playback and capture buffer size */
74#define RME96_BUFFER_SIZE 0x10000
75
76/* IO area size */
77#define RME96_IO_SIZE 0x60000
78
79/* IO area offsets */
80#define RME96_IO_PLAY_BUFFER 0x0
81#define RME96_IO_REC_BUFFER 0x10000
82#define RME96_IO_CONTROL_REGISTER 0x20000
83#define RME96_IO_ADDITIONAL_REG 0x20004
84#define RME96_IO_CONFIRM_PLAY_IRQ 0x20008
85#define RME96_IO_CONFIRM_REC_IRQ 0x2000C
86#define RME96_IO_SET_PLAY_POS 0x40000
87#define RME96_IO_RESET_PLAY_POS 0x4FFFC
88#define RME96_IO_SET_REC_POS 0x50000
89#define RME96_IO_RESET_REC_POS 0x5FFFC
90#define RME96_IO_GET_PLAY_POS 0x20000
91#define RME96_IO_GET_REC_POS 0x30000
92
93/* Write control register bits */
94#define RME96_WCR_START (1 << 0)
95#define RME96_WCR_START_2 (1 << 1)
96#define RME96_WCR_GAIN_0 (1 << 2)
97#define RME96_WCR_GAIN_1 (1 << 3)
98#define RME96_WCR_MODE24 (1 << 4)
99#define RME96_WCR_MODE24_2 (1 << 5)
100#define RME96_WCR_BM (1 << 6)
101#define RME96_WCR_BM_2 (1 << 7)
102#define RME96_WCR_ADAT (1 << 8)
103#define RME96_WCR_FREQ_0 (1 << 9)
104#define RME96_WCR_FREQ_1 (1 << 10)
105#define RME96_WCR_DS (1 << 11)
106#define RME96_WCR_PRO (1 << 12)
107#define RME96_WCR_EMP (1 << 13)
108#define RME96_WCR_SEL (1 << 14)
109#define RME96_WCR_MASTER (1 << 15)
110#define RME96_WCR_PD (1 << 16)
111#define RME96_WCR_INP_0 (1 << 17)
112#define RME96_WCR_INP_1 (1 << 18)
113#define RME96_WCR_THRU_0 (1 << 19)
114#define RME96_WCR_THRU_1 (1 << 20)
115#define RME96_WCR_THRU_2 (1 << 21)
116#define RME96_WCR_THRU_3 (1 << 22)
117#define RME96_WCR_THRU_4 (1 << 23)
118#define RME96_WCR_THRU_5 (1 << 24)
119#define RME96_WCR_THRU_6 (1 << 25)
120#define RME96_WCR_THRU_7 (1 << 26)
121#define RME96_WCR_DOLBY (1 << 27)
122#define RME96_WCR_MONITOR_0 (1 << 28)
123#define RME96_WCR_MONITOR_1 (1 << 29)
124#define RME96_WCR_ISEL (1 << 30)
125#define RME96_WCR_IDIS (1 << 31)
126
127#define RME96_WCR_BITPOS_GAIN_0 2
128#define RME96_WCR_BITPOS_GAIN_1 3
129#define RME96_WCR_BITPOS_FREQ_0 9
130#define RME96_WCR_BITPOS_FREQ_1 10
131#define RME96_WCR_BITPOS_INP_0 17
132#define RME96_WCR_BITPOS_INP_1 18
133#define RME96_WCR_BITPOS_MONITOR_0 28
134#define RME96_WCR_BITPOS_MONITOR_1 29
135
136/* Read control register bits */
137#define RME96_RCR_AUDIO_ADDR_MASK 0xFFFF
138#define RME96_RCR_IRQ_2 (1 << 16)
139#define RME96_RCR_T_OUT (1 << 17)
140#define RME96_RCR_DEV_ID_0 (1 << 21)
141#define RME96_RCR_DEV_ID_1 (1 << 22)
142#define RME96_RCR_LOCK (1 << 23)
143#define RME96_RCR_VERF (1 << 26)
144#define RME96_RCR_F0 (1 << 27)
145#define RME96_RCR_F1 (1 << 28)
146#define RME96_RCR_F2 (1 << 29)
147#define RME96_RCR_AUTOSYNC (1 << 30)
148#define RME96_RCR_IRQ (1 << 31)
149
150#define RME96_RCR_BITPOS_F0 27
151#define RME96_RCR_BITPOS_F1 28
152#define RME96_RCR_BITPOS_F2 29
153
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300154/* Additional register bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#define RME96_AR_WSEL (1 << 0)
156#define RME96_AR_ANALOG (1 << 1)
157#define RME96_AR_FREQPAD_0 (1 << 2)
158#define RME96_AR_FREQPAD_1 (1 << 3)
159#define RME96_AR_FREQPAD_2 (1 << 4)
160#define RME96_AR_PD2 (1 << 5)
161#define RME96_AR_DAC_EN (1 << 6)
162#define RME96_AR_CLATCH (1 << 7)
163#define RME96_AR_CCLK (1 << 8)
164#define RME96_AR_CDATA (1 << 9)
165
166#define RME96_AR_BITPOS_F0 2
167#define RME96_AR_BITPOS_F1 3
168#define RME96_AR_BITPOS_F2 4
169
170/* Monitor tracks */
171#define RME96_MONITOR_TRACKS_1_2 0
172#define RME96_MONITOR_TRACKS_3_4 1
173#define RME96_MONITOR_TRACKS_5_6 2
174#define RME96_MONITOR_TRACKS_7_8 3
175
176/* Attenuation */
177#define RME96_ATTENUATION_0 0
178#define RME96_ATTENUATION_6 1
179#define RME96_ATTENUATION_12 2
180#define RME96_ATTENUATION_18 3
181
182/* Input types */
183#define RME96_INPUT_OPTICAL 0
184#define RME96_INPUT_COAXIAL 1
185#define RME96_INPUT_INTERNAL 2
186#define RME96_INPUT_XLR 3
187#define RME96_INPUT_ANALOG 4
188
189/* Clock modes */
190#define RME96_CLOCKMODE_SLAVE 0
191#define RME96_CLOCKMODE_MASTER 1
192#define RME96_CLOCKMODE_WORDCLOCK 2
193
194/* Block sizes in bytes */
195#define RME96_SMALL_BLOCK_SIZE 2048
196#define RME96_LARGE_BLOCK_SIZE 8192
197
198/* Volume control */
199#define RME96_AD1852_VOL_BITS 14
200#define RME96_AD1855_VOL_BITS 10
201
Knut Petersenb892ca12013-08-13 21:18:12 +0200202/* Defines for snd_rme96_trigger */
203#define RME96_TB_START_PLAYBACK 1
204#define RME96_TB_START_CAPTURE 2
205#define RME96_TB_STOP_PLAYBACK 4
206#define RME96_TB_STOP_CAPTURE 8
207#define RME96_TB_RESET_PLAYPOS 16
208#define RME96_TB_RESET_CAPTUREPOS 32
209#define RME96_TB_CLEAR_PLAYBACK_IRQ 64
210#define RME96_TB_CLEAR_CAPTURE_IRQ 128
211#define RME96_RESUME_PLAYBACK (RME96_TB_START_PLAYBACK)
212#define RME96_RESUME_CAPTURE (RME96_TB_START_CAPTURE)
213#define RME96_RESUME_BOTH (RME96_RESUME_PLAYBACK \
214 | RME96_RESUME_CAPTURE)
215#define RME96_START_PLAYBACK (RME96_TB_START_PLAYBACK \
216 | RME96_TB_RESET_PLAYPOS)
217#define RME96_START_CAPTURE (RME96_TB_START_CAPTURE \
218 | RME96_TB_RESET_CAPTUREPOS)
219#define RME96_START_BOTH (RME96_START_PLAYBACK \
220 | RME96_START_CAPTURE)
221#define RME96_STOP_PLAYBACK (RME96_TB_STOP_PLAYBACK \
222 | RME96_TB_CLEAR_PLAYBACK_IRQ)
223#define RME96_STOP_CAPTURE (RME96_TB_STOP_CAPTURE \
224 | RME96_TB_CLEAR_CAPTURE_IRQ)
225#define RME96_STOP_BOTH (RME96_STOP_PLAYBACK \
226 | RME96_STOP_CAPTURE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100228struct rme96 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 spinlock_t lock;
230 int irq;
231 unsigned long port;
232 void __iomem *iobase;
233
234 u32 wcreg; /* cached write control register value */
235 u32 wcreg_spdif; /* S/PDIF setup */
236 u32 wcreg_spdif_stream; /* S/PDIF setup (temporary) */
237 u32 rcreg; /* cached read control register value */
238 u32 areg; /* cached additional register value */
239 u16 vol[2]; /* cached volume of analog output */
240
241 u8 rev; /* card revision number */
242
Takashi Iwaia932be92014-02-25 15:01:15 +0100243#ifdef CONFIG_PM_SLEEP
Knut Petersen528ba522013-08-21 09:18:54 +0200244 u32 playback_pointer;
245 u32 capture_pointer;
246 void *playback_suspend_buffer;
247 void *capture_suspend_buffer;
248#endif
249
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100250 struct snd_pcm_substream *playback_substream;
251 struct snd_pcm_substream *capture_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 int playback_frlog; /* log2 of framesize */
254 int capture_frlog;
255
256 size_t playback_periodsize; /* in bytes, zero if not used */
257 size_t capture_periodsize; /* in bytes, zero if not used */
258
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100259 struct snd_card *card;
260 struct snd_pcm *spdif_pcm;
261 struct snd_pcm *adat_pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 struct pci_dev *pci;
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100263 struct snd_kcontrol *spdif_ctl;
264};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Benoit Taine9baa3c32014-08-08 15:56:03 +0200266static const struct pci_device_id snd_rme96_ids[] = {
Joe Perches28d27aa2009-06-24 22:13:35 -0700267 { PCI_VDEVICE(XILINX, PCI_DEVICE_ID_RME_DIGI96), 0, },
268 { PCI_VDEVICE(XILINX, PCI_DEVICE_ID_RME_DIGI96_8), 0, },
269 { PCI_VDEVICE(XILINX, PCI_DEVICE_ID_RME_DIGI96_8_PRO), 0, },
270 { PCI_VDEVICE(XILINX, PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST), 0, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 { 0, }
272};
273
274MODULE_DEVICE_TABLE(pci, snd_rme96_ids);
275
276#define RME96_ISPLAYING(rme96) ((rme96)->wcreg & RME96_WCR_START)
277#define RME96_ISRECORDING(rme96) ((rme96)->wcreg & RME96_WCR_START_2)
Roland Dreier8b7fc422005-09-14 14:19:17 -0700278#define RME96_HAS_ANALOG_IN(rme96) ((rme96)->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST)
279#define RME96_HAS_ANALOG_OUT(rme96) ((rme96)->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PRO || \
280 (rme96)->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281#define RME96_DAC_IS_1852(rme96) (RME96_HAS_ANALOG_OUT(rme96) && (rme96)->rev >= 4)
Roland Dreier8b7fc422005-09-14 14:19:17 -0700282#define RME96_DAC_IS_1855(rme96) (((rme96)->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST && (rme96)->rev < 4) || \
283 ((rme96)->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PRO && (rme96)->rev == 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284#define RME96_185X_MAX_OUT(rme96) ((1 << (RME96_DAC_IS_1852(rme96) ? RME96_AD1852_VOL_BITS : RME96_AD1855_VOL_BITS)) - 1)
285
286static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100287snd_rme96_playback_prepare(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100290snd_rme96_capture_prepare(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100293snd_rme96_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 int cmd);
295
296static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100297snd_rme96_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 int cmd);
299
300static snd_pcm_uframes_t
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100301snd_rme96_playback_pointer(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303static snd_pcm_uframes_t
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100304snd_rme96_capture_pointer(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Bill Pembertone23e7a12012-12-06 12:35:10 -0500306static void snd_rme96_proc_init(struct rme96 *rme96);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100309snd_rme96_create_switches(struct snd_card *card,
310 struct rme96 *rme96);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100313snd_rme96_getinputtype(struct rme96 *rme96);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315static inline unsigned int
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100316snd_rme96_playback_ptr(struct rme96 *rme96)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
318 return (readl(rme96->iobase + RME96_IO_GET_PLAY_POS)
319 & RME96_RCR_AUDIO_ADDR_MASK) >> rme96->playback_frlog;
320}
321
322static inline unsigned int
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100323snd_rme96_capture_ptr(struct rme96 *rme96)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 return (readl(rme96->iobase + RME96_IO_GET_REC_POS)
326 & RME96_RCR_AUDIO_ADDR_MASK) >> rme96->capture_frlog;
327}
328
329static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100330snd_rme96_playback_silence(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 int channel, /* not used (interleaved data) */
332 snd_pcm_uframes_t pos,
333 snd_pcm_uframes_t count)
334{
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100335 struct rme96 *rme96 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 count <<= rme96->playback_frlog;
337 pos <<= rme96->playback_frlog;
338 memset_io(rme96->iobase + RME96_IO_PLAY_BUFFER + pos,
339 0, count);
340 return 0;
341}
342
343static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100344snd_rme96_playback_copy(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 int channel, /* not used (interleaved data) */
346 snd_pcm_uframes_t pos,
347 void __user *src,
348 snd_pcm_uframes_t count)
349{
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100350 struct rme96 *rme96 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 count <<= rme96->playback_frlog;
352 pos <<= rme96->playback_frlog;
Takashi Iwai2026d242013-10-29 16:04:37 +0100353 return copy_from_user_toio(rme96->iobase + RME96_IO_PLAY_BUFFER + pos, src,
354 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100358snd_rme96_capture_copy(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 int channel, /* not used (interleaved data) */
360 snd_pcm_uframes_t pos,
361 void __user *dst,
362 snd_pcm_uframes_t count)
363{
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100364 struct rme96 *rme96 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 count <<= rme96->capture_frlog;
366 pos <<= rme96->capture_frlog;
Takashi Iwai2026d242013-10-29 16:04:37 +0100367 return copy_to_user_fromio(dst, rme96->iobase + RME96_IO_REC_BUFFER + pos,
368 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
371/*
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -0800372 * Digital output capabilities (S/PDIF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 */
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100374static struct snd_pcm_hardware snd_rme96_playback_spdif_info =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 .info = (SNDRV_PCM_INFO_MMAP_IOMEM |
377 SNDRV_PCM_INFO_MMAP_VALID |
Knut Petersenb892ca12013-08-13 21:18:12 +0200378 SNDRV_PCM_INFO_SYNC_START |
Knut Petersen528ba522013-08-21 09:18:54 +0200379 SNDRV_PCM_INFO_RESUME |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 SNDRV_PCM_INFO_INTERLEAVED |
381 SNDRV_PCM_INFO_PAUSE),
382 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
383 SNDRV_PCM_FMTBIT_S32_LE),
384 .rates = (SNDRV_PCM_RATE_32000 |
385 SNDRV_PCM_RATE_44100 |
386 SNDRV_PCM_RATE_48000 |
387 SNDRV_PCM_RATE_64000 |
388 SNDRV_PCM_RATE_88200 |
389 SNDRV_PCM_RATE_96000),
390 .rate_min = 32000,
391 .rate_max = 96000,
392 .channels_min = 2,
393 .channels_max = 2,
394 .buffer_bytes_max = RME96_BUFFER_SIZE,
395 .period_bytes_min = RME96_SMALL_BLOCK_SIZE,
396 .period_bytes_max = RME96_LARGE_BLOCK_SIZE,
397 .periods_min = RME96_BUFFER_SIZE / RME96_LARGE_BLOCK_SIZE,
398 .periods_max = RME96_BUFFER_SIZE / RME96_SMALL_BLOCK_SIZE,
399 .fifo_size = 0,
400};
401
402/*
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -0800403 * Digital input capabilities (S/PDIF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 */
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100405static struct snd_pcm_hardware snd_rme96_capture_spdif_info =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 .info = (SNDRV_PCM_INFO_MMAP_IOMEM |
408 SNDRV_PCM_INFO_MMAP_VALID |
Knut Petersenb892ca12013-08-13 21:18:12 +0200409 SNDRV_PCM_INFO_SYNC_START |
Knut Petersen528ba522013-08-21 09:18:54 +0200410 SNDRV_PCM_INFO_RESUME |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 SNDRV_PCM_INFO_INTERLEAVED |
412 SNDRV_PCM_INFO_PAUSE),
413 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
414 SNDRV_PCM_FMTBIT_S32_LE),
415 .rates = (SNDRV_PCM_RATE_32000 |
416 SNDRV_PCM_RATE_44100 |
417 SNDRV_PCM_RATE_48000 |
418 SNDRV_PCM_RATE_64000 |
419 SNDRV_PCM_RATE_88200 |
420 SNDRV_PCM_RATE_96000),
421 .rate_min = 32000,
422 .rate_max = 96000,
423 .channels_min = 2,
424 .channels_max = 2,
425 .buffer_bytes_max = RME96_BUFFER_SIZE,
426 .period_bytes_min = RME96_SMALL_BLOCK_SIZE,
427 .period_bytes_max = RME96_LARGE_BLOCK_SIZE,
428 .periods_min = RME96_BUFFER_SIZE / RME96_LARGE_BLOCK_SIZE,
429 .periods_max = RME96_BUFFER_SIZE / RME96_SMALL_BLOCK_SIZE,
430 .fifo_size = 0,
431};
432
433/*
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -0800434 * Digital output capabilities (ADAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 */
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100436static struct snd_pcm_hardware snd_rme96_playback_adat_info =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 .info = (SNDRV_PCM_INFO_MMAP_IOMEM |
439 SNDRV_PCM_INFO_MMAP_VALID |
Knut Petersenb892ca12013-08-13 21:18:12 +0200440 SNDRV_PCM_INFO_SYNC_START |
Knut Petersen528ba522013-08-21 09:18:54 +0200441 SNDRV_PCM_INFO_RESUME |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 SNDRV_PCM_INFO_INTERLEAVED |
443 SNDRV_PCM_INFO_PAUSE),
444 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
445 SNDRV_PCM_FMTBIT_S32_LE),
446 .rates = (SNDRV_PCM_RATE_44100 |
447 SNDRV_PCM_RATE_48000),
448 .rate_min = 44100,
449 .rate_max = 48000,
450 .channels_min = 8,
451 .channels_max = 8,
452 .buffer_bytes_max = RME96_BUFFER_SIZE,
453 .period_bytes_min = RME96_SMALL_BLOCK_SIZE,
454 .period_bytes_max = RME96_LARGE_BLOCK_SIZE,
455 .periods_min = RME96_BUFFER_SIZE / RME96_LARGE_BLOCK_SIZE,
456 .periods_max = RME96_BUFFER_SIZE / RME96_SMALL_BLOCK_SIZE,
457 .fifo_size = 0,
458};
459
460/*
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -0800461 * Digital input capabilities (ADAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 */
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100463static struct snd_pcm_hardware snd_rme96_capture_adat_info =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 .info = (SNDRV_PCM_INFO_MMAP_IOMEM |
466 SNDRV_PCM_INFO_MMAP_VALID |
Knut Petersenb892ca12013-08-13 21:18:12 +0200467 SNDRV_PCM_INFO_SYNC_START |
Knut Petersen528ba522013-08-21 09:18:54 +0200468 SNDRV_PCM_INFO_RESUME |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 SNDRV_PCM_INFO_INTERLEAVED |
470 SNDRV_PCM_INFO_PAUSE),
471 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
472 SNDRV_PCM_FMTBIT_S32_LE),
473 .rates = (SNDRV_PCM_RATE_44100 |
474 SNDRV_PCM_RATE_48000),
475 .rate_min = 44100,
476 .rate_max = 48000,
477 .channels_min = 8,
478 .channels_max = 8,
479 .buffer_bytes_max = RME96_BUFFER_SIZE,
480 .period_bytes_min = RME96_SMALL_BLOCK_SIZE,
481 .period_bytes_max = RME96_LARGE_BLOCK_SIZE,
482 .periods_min = RME96_BUFFER_SIZE / RME96_LARGE_BLOCK_SIZE,
483 .periods_max = RME96_BUFFER_SIZE / RME96_SMALL_BLOCK_SIZE,
484 .fifo_size = 0,
485};
486
487/*
488 * The CDATA, CCLK and CLATCH bits can be used to write to the SPI interface
489 * of the AD1852 or AD1852 D/A converter on the board. CDATA must be set up
490 * on the falling edge of CCLK and be stable on the rising edge. The rising
491 * edge of CLATCH after the last data bit clocks in the whole data word.
492 * A fast processor could probably drive the SPI interface faster than the
493 * DAC can handle (3MHz for the 1855, unknown for the 1852). The udelay(1)
494 * limits the data rate to 500KHz and only causes a delay of 33 microsecs.
495 *
496 * NOTE: increased delay from 1 to 10, since there where problems setting
497 * the volume.
498 */
499static void
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100500snd_rme96_write_SPI(struct rme96 *rme96, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 int i;
503
504 for (i = 0; i < 16; i++) {
505 if (val & 0x8000) {
506 rme96->areg |= RME96_AR_CDATA;
507 } else {
508 rme96->areg &= ~RME96_AR_CDATA;
509 }
510 rme96->areg &= ~(RME96_AR_CCLK | RME96_AR_CLATCH);
511 writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
512 udelay(10);
513 rme96->areg |= RME96_AR_CCLK;
514 writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
515 udelay(10);
516 val <<= 1;
517 }
518 rme96->areg &= ~(RME96_AR_CCLK | RME96_AR_CDATA);
519 rme96->areg |= RME96_AR_CLATCH;
520 writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
521 udelay(10);
522 rme96->areg &= ~RME96_AR_CLATCH;
523 writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
524}
525
526static void
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100527snd_rme96_apply_dac_volume(struct rme96 *rme96)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 if (RME96_DAC_IS_1852(rme96)) {
530 snd_rme96_write_SPI(rme96, (rme96->vol[0] << 2) | 0x0);
531 snd_rme96_write_SPI(rme96, (rme96->vol[1] << 2) | 0x2);
532 } else if (RME96_DAC_IS_1855(rme96)) {
533 snd_rme96_write_SPI(rme96, (rme96->vol[0] & 0x3FF) | 0x000);
534 snd_rme96_write_SPI(rme96, (rme96->vol[1] & 0x3FF) | 0x400);
535 }
536}
537
538static void
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100539snd_rme96_reset_dac(struct rme96 *rme96)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 writel(rme96->wcreg | RME96_WCR_PD,
542 rme96->iobase + RME96_IO_CONTROL_REGISTER);
543 writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
544}
545
546static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100547snd_rme96_getmontracks(struct rme96 *rme96)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549 return ((rme96->wcreg >> RME96_WCR_BITPOS_MONITOR_0) & 1) +
550 (((rme96->wcreg >> RME96_WCR_BITPOS_MONITOR_1) & 1) << 1);
551}
552
553static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100554snd_rme96_setmontracks(struct rme96 *rme96,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 int montracks)
556{
557 if (montracks & 1) {
558 rme96->wcreg |= RME96_WCR_MONITOR_0;
559 } else {
560 rme96->wcreg &= ~RME96_WCR_MONITOR_0;
561 }
562 if (montracks & 2) {
563 rme96->wcreg |= RME96_WCR_MONITOR_1;
564 } else {
565 rme96->wcreg &= ~RME96_WCR_MONITOR_1;
566 }
567 writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
568 return 0;
569}
570
571static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100572snd_rme96_getattenuation(struct rme96 *rme96)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574 return ((rme96->wcreg >> RME96_WCR_BITPOS_GAIN_0) & 1) +
575 (((rme96->wcreg >> RME96_WCR_BITPOS_GAIN_1) & 1) << 1);
576}
577
578static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100579snd_rme96_setattenuation(struct rme96 *rme96,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 int attenuation)
581{
582 switch (attenuation) {
583 case 0:
584 rme96->wcreg = (rme96->wcreg & ~RME96_WCR_GAIN_0) &
585 ~RME96_WCR_GAIN_1;
586 break;
587 case 1:
588 rme96->wcreg = (rme96->wcreg | RME96_WCR_GAIN_0) &
589 ~RME96_WCR_GAIN_1;
590 break;
591 case 2:
592 rme96->wcreg = (rme96->wcreg & ~RME96_WCR_GAIN_0) |
593 RME96_WCR_GAIN_1;
594 break;
595 case 3:
596 rme96->wcreg = (rme96->wcreg | RME96_WCR_GAIN_0) |
597 RME96_WCR_GAIN_1;
598 break;
599 default:
600 return -EINVAL;
601 }
602 writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
603 return 0;
604}
605
606static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100607snd_rme96_capture_getrate(struct rme96 *rme96,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 int *is_adat)
609{
610 int n, rate;
611
612 *is_adat = 0;
613 if (rme96->areg & RME96_AR_ANALOG) {
614 /* Analog input, overrides S/PDIF setting */
615 n = ((rme96->areg >> RME96_AR_BITPOS_F0) & 1) +
616 (((rme96->areg >> RME96_AR_BITPOS_F1) & 1) << 1);
617 switch (n) {
618 case 1:
619 rate = 32000;
620 break;
621 case 2:
622 rate = 44100;
623 break;
624 case 3:
625 rate = 48000;
626 break;
627 default:
628 return -1;
629 }
630 return (rme96->areg & RME96_AR_BITPOS_F2) ? rate << 1 : rate;
631 }
632
633 rme96->rcreg = readl(rme96->iobase + RME96_IO_CONTROL_REGISTER);
634 if (rme96->rcreg & RME96_RCR_LOCK) {
635 /* ADAT rate */
636 *is_adat = 1;
637 if (rme96->rcreg & RME96_RCR_T_OUT) {
638 return 48000;
639 }
640 return 44100;
641 }
642
643 if (rme96->rcreg & RME96_RCR_VERF) {
644 return -1;
645 }
646
647 /* S/PDIF rate */
648 n = ((rme96->rcreg >> RME96_RCR_BITPOS_F0) & 1) +
649 (((rme96->rcreg >> RME96_RCR_BITPOS_F1) & 1) << 1) +
650 (((rme96->rcreg >> RME96_RCR_BITPOS_F2) & 1) << 2);
651
652 switch (n) {
653 case 0:
654 if (rme96->rcreg & RME96_RCR_T_OUT) {
655 return 64000;
656 }
657 return -1;
658 case 3: return 96000;
659 case 4: return 88200;
660 case 5: return 48000;
661 case 6: return 44100;
662 case 7: return 32000;
663 default:
664 break;
665 }
666 return -1;
667}
668
669static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100670snd_rme96_playback_getrate(struct rme96 *rme96)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 int rate, dummy;
673
674 if (!(rme96->wcreg & RME96_WCR_MASTER) &&
675 snd_rme96_getinputtype(rme96) != RME96_INPUT_ANALOG &&
676 (rate = snd_rme96_capture_getrate(rme96, &dummy)) > 0)
677 {
678 /* slave clock */
679 return rate;
680 }
681 rate = ((rme96->wcreg >> RME96_WCR_BITPOS_FREQ_0) & 1) +
682 (((rme96->wcreg >> RME96_WCR_BITPOS_FREQ_1) & 1) << 1);
683 switch (rate) {
684 case 1:
685 rate = 32000;
686 break;
687 case 2:
688 rate = 44100;
689 break;
690 case 3:
691 rate = 48000;
692 break;
693 default:
694 return -1;
695 }
696 return (rme96->wcreg & RME96_WCR_DS) ? rate << 1 : rate;
697}
698
699static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100700snd_rme96_playback_setrate(struct rme96 *rme96,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 int rate)
702{
703 int ds;
704
705 ds = rme96->wcreg & RME96_WCR_DS;
706 switch (rate) {
707 case 32000:
708 rme96->wcreg &= ~RME96_WCR_DS;
709 rme96->wcreg = (rme96->wcreg | RME96_WCR_FREQ_0) &
710 ~RME96_WCR_FREQ_1;
711 break;
712 case 44100:
713 rme96->wcreg &= ~RME96_WCR_DS;
714 rme96->wcreg = (rme96->wcreg | RME96_WCR_FREQ_1) &
715 ~RME96_WCR_FREQ_0;
716 break;
717 case 48000:
718 rme96->wcreg &= ~RME96_WCR_DS;
719 rme96->wcreg = (rme96->wcreg | RME96_WCR_FREQ_0) |
720 RME96_WCR_FREQ_1;
721 break;
722 case 64000:
723 rme96->wcreg |= RME96_WCR_DS;
724 rme96->wcreg = (rme96->wcreg | RME96_WCR_FREQ_0) &
725 ~RME96_WCR_FREQ_1;
726 break;
727 case 88200:
728 rme96->wcreg |= RME96_WCR_DS;
729 rme96->wcreg = (rme96->wcreg | RME96_WCR_FREQ_1) &
730 ~RME96_WCR_FREQ_0;
731 break;
732 case 96000:
733 rme96->wcreg |= RME96_WCR_DS;
734 rme96->wcreg = (rme96->wcreg | RME96_WCR_FREQ_0) |
735 RME96_WCR_FREQ_1;
736 break;
737 default:
738 return -EINVAL;
739 }
740 if ((!ds && rme96->wcreg & RME96_WCR_DS) ||
741 (ds && !(rme96->wcreg & RME96_WCR_DS)))
742 {
743 /* change to/from double-speed: reset the DAC (if available) */
744 snd_rme96_reset_dac(rme96);
745 } else {
746 writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
747 }
748 return 0;
749}
750
751static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100752snd_rme96_capture_analog_setrate(struct rme96 *rme96,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 int rate)
754{
755 switch (rate) {
756 case 32000:
757 rme96->areg = ((rme96->areg | RME96_AR_FREQPAD_0) &
758 ~RME96_AR_FREQPAD_1) & ~RME96_AR_FREQPAD_2;
759 break;
760 case 44100:
761 rme96->areg = ((rme96->areg & ~RME96_AR_FREQPAD_0) |
762 RME96_AR_FREQPAD_1) & ~RME96_AR_FREQPAD_2;
763 break;
764 case 48000:
765 rme96->areg = ((rme96->areg | RME96_AR_FREQPAD_0) |
766 RME96_AR_FREQPAD_1) & ~RME96_AR_FREQPAD_2;
767 break;
768 case 64000:
769 if (rme96->rev < 4) {
770 return -EINVAL;
771 }
772 rme96->areg = ((rme96->areg | RME96_AR_FREQPAD_0) &
773 ~RME96_AR_FREQPAD_1) | RME96_AR_FREQPAD_2;
774 break;
775 case 88200:
776 if (rme96->rev < 4) {
777 return -EINVAL;
778 }
779 rme96->areg = ((rme96->areg & ~RME96_AR_FREQPAD_0) |
780 RME96_AR_FREQPAD_1) | RME96_AR_FREQPAD_2;
781 break;
782 case 96000:
783 rme96->areg = ((rme96->areg | RME96_AR_FREQPAD_0) |
784 RME96_AR_FREQPAD_1) | RME96_AR_FREQPAD_2;
785 break;
786 default:
787 return -EINVAL;
788 }
789 writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
790 return 0;
791}
792
793static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100794snd_rme96_setclockmode(struct rme96 *rme96,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 int mode)
796{
797 switch (mode) {
798 case RME96_CLOCKMODE_SLAVE:
799 /* AutoSync */
800 rme96->wcreg &= ~RME96_WCR_MASTER;
801 rme96->areg &= ~RME96_AR_WSEL;
802 break;
803 case RME96_CLOCKMODE_MASTER:
804 /* Internal */
805 rme96->wcreg |= RME96_WCR_MASTER;
806 rme96->areg &= ~RME96_AR_WSEL;
807 break;
808 case RME96_CLOCKMODE_WORDCLOCK:
809 /* Word clock is a master mode */
810 rme96->wcreg |= RME96_WCR_MASTER;
811 rme96->areg |= RME96_AR_WSEL;
812 break;
813 default:
814 return -EINVAL;
815 }
816 writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
817 writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
818 return 0;
819}
820
821static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100822snd_rme96_getclockmode(struct rme96 *rme96)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
824 if (rme96->areg & RME96_AR_WSEL) {
825 return RME96_CLOCKMODE_WORDCLOCK;
826 }
827 return (rme96->wcreg & RME96_WCR_MASTER) ? RME96_CLOCKMODE_MASTER :
828 RME96_CLOCKMODE_SLAVE;
829}
830
831static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100832snd_rme96_setinputtype(struct rme96 *rme96,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 int type)
834{
835 int n;
836
837 switch (type) {
838 case RME96_INPUT_OPTICAL:
839 rme96->wcreg = (rme96->wcreg & ~RME96_WCR_INP_0) &
840 ~RME96_WCR_INP_1;
841 break;
842 case RME96_INPUT_COAXIAL:
843 rme96->wcreg = (rme96->wcreg | RME96_WCR_INP_0) &
844 ~RME96_WCR_INP_1;
845 break;
846 case RME96_INPUT_INTERNAL:
847 rme96->wcreg = (rme96->wcreg & ~RME96_WCR_INP_0) |
848 RME96_WCR_INP_1;
849 break;
850 case RME96_INPUT_XLR:
Roland Dreier8b7fc422005-09-14 14:19:17 -0700851 if ((rme96->pci->device != PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST &&
852 rme96->pci->device != PCI_DEVICE_ID_RME_DIGI96_8_PRO) ||
853 (rme96->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 rme96->rev > 4))
855 {
856 /* Only Digi96/8 PRO and Digi96/8 PAD supports XLR */
857 return -EINVAL;
858 }
859 rme96->wcreg = (rme96->wcreg | RME96_WCR_INP_0) |
860 RME96_WCR_INP_1;
861 break;
862 case RME96_INPUT_ANALOG:
863 if (!RME96_HAS_ANALOG_IN(rme96)) {
864 return -EINVAL;
865 }
866 rme96->areg |= RME96_AR_ANALOG;
867 writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
868 if (rme96->rev < 4) {
869 /*
870 * Revision less than 004 does not support 64 and
871 * 88.2 kHz
872 */
873 if (snd_rme96_capture_getrate(rme96, &n) == 88200) {
874 snd_rme96_capture_analog_setrate(rme96, 44100);
875 }
876 if (snd_rme96_capture_getrate(rme96, &n) == 64000) {
877 snd_rme96_capture_analog_setrate(rme96, 32000);
878 }
879 }
880 return 0;
881 default:
882 return -EINVAL;
883 }
884 if (type != RME96_INPUT_ANALOG && RME96_HAS_ANALOG_IN(rme96)) {
885 rme96->areg &= ~RME96_AR_ANALOG;
886 writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
887 }
888 writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
889 return 0;
890}
891
892static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100893snd_rme96_getinputtype(struct rme96 *rme96)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
895 if (rme96->areg & RME96_AR_ANALOG) {
896 return RME96_INPUT_ANALOG;
897 }
898 return ((rme96->wcreg >> RME96_WCR_BITPOS_INP_0) & 1) +
899 (((rme96->wcreg >> RME96_WCR_BITPOS_INP_1) & 1) << 1);
900}
901
902static void
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100903snd_rme96_setframelog(struct rme96 *rme96,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 int n_channels,
905 int is_playback)
906{
907 int frlog;
908
909 if (n_channels == 2) {
910 frlog = 1;
911 } else {
912 /* assume 8 channels */
913 frlog = 3;
914 }
915 if (is_playback) {
916 frlog += (rme96->wcreg & RME96_WCR_MODE24) ? 2 : 1;
917 rme96->playback_frlog = frlog;
918 } else {
919 frlog += (rme96->wcreg & RME96_WCR_MODE24_2) ? 2 : 1;
920 rme96->capture_frlog = frlog;
921 }
922}
923
924static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100925snd_rme96_playback_setformat(struct rme96 *rme96,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 int format)
927{
928 switch (format) {
929 case SNDRV_PCM_FORMAT_S16_LE:
930 rme96->wcreg &= ~RME96_WCR_MODE24;
931 break;
932 case SNDRV_PCM_FORMAT_S32_LE:
933 rme96->wcreg |= RME96_WCR_MODE24;
934 break;
935 default:
936 return -EINVAL;
937 }
938 writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
939 return 0;
940}
941
942static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100943snd_rme96_capture_setformat(struct rme96 *rme96,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 int format)
945{
946 switch (format) {
947 case SNDRV_PCM_FORMAT_S16_LE:
948 rme96->wcreg &= ~RME96_WCR_MODE24_2;
949 break;
950 case SNDRV_PCM_FORMAT_S32_LE:
951 rme96->wcreg |= RME96_WCR_MODE24_2;
952 break;
953 default:
954 return -EINVAL;
955 }
956 writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
957 return 0;
958}
959
960static void
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100961snd_rme96_set_period_properties(struct rme96 *rme96,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 size_t period_bytes)
963{
964 switch (period_bytes) {
965 case RME96_LARGE_BLOCK_SIZE:
966 rme96->wcreg &= ~RME96_WCR_ISEL;
967 break;
968 case RME96_SMALL_BLOCK_SIZE:
969 rme96->wcreg |= RME96_WCR_ISEL;
970 break;
971 default:
972 snd_BUG();
973 break;
974 }
975 rme96->wcreg &= ~RME96_WCR_IDIS;
976 writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
977}
978
979static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100980snd_rme96_playback_hw_params(struct snd_pcm_substream *substream,
981 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Takashi Iwaia3aefd82005-11-17 15:05:37 +0100983 struct rme96 *rme96 = snd_pcm_substream_chip(substream);
984 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 int err, rate, dummy;
986
Clemens Ladisch4d233592005-09-05 10:35:20 +0200987 runtime->dma_area = (void __force *)(rme96->iobase +
988 RME96_IO_PLAY_BUFFER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 runtime->dma_addr = rme96->port + RME96_IO_PLAY_BUFFER;
990 runtime->dma_bytes = RME96_BUFFER_SIZE;
991
992 spin_lock_irq(&rme96->lock);
993 if (!(rme96->wcreg & RME96_WCR_MASTER) &&
994 snd_rme96_getinputtype(rme96) != RME96_INPUT_ANALOG &&
995 (rate = snd_rme96_capture_getrate(rme96, &dummy)) > 0)
996 {
997 /* slave clock */
998 if ((int)params_rate(params) != rate) {
999 spin_unlock_irq(&rme96->lock);
1000 return -EIO;
1001 }
1002 } else if ((err = snd_rme96_playback_setrate(rme96, params_rate(params))) < 0) {
1003 spin_unlock_irq(&rme96->lock);
1004 return err;
1005 }
1006 if ((err = snd_rme96_playback_setformat(rme96, params_format(params))) < 0) {
1007 spin_unlock_irq(&rme96->lock);
1008 return err;
1009 }
1010 snd_rme96_setframelog(rme96, params_channels(params), 1);
1011 if (rme96->capture_periodsize != 0) {
1012 if (params_period_size(params) << rme96->playback_frlog !=
1013 rme96->capture_periodsize)
1014 {
1015 spin_unlock_irq(&rme96->lock);
1016 return -EBUSY;
1017 }
1018 }
1019 rme96->playback_periodsize =
1020 params_period_size(params) << rme96->playback_frlog;
1021 snd_rme96_set_period_properties(rme96, rme96->playback_periodsize);
1022 /* S/PDIF setup */
1023 if ((rme96->wcreg & RME96_WCR_ADAT) == 0) {
1024 rme96->wcreg &= ~(RME96_WCR_PRO | RME96_WCR_DOLBY | RME96_WCR_EMP);
1025 writel(rme96->wcreg |= rme96->wcreg_spdif_stream, rme96->iobase + RME96_IO_CONTROL_REGISTER);
1026 }
1027 spin_unlock_irq(&rme96->lock);
1028
1029 return 0;
1030}
1031
1032static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001033snd_rme96_capture_hw_params(struct snd_pcm_substream *substream,
1034 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001036 struct rme96 *rme96 = snd_pcm_substream_chip(substream);
1037 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 int err, isadat, rate;
1039
Clemens Ladisch4d233592005-09-05 10:35:20 +02001040 runtime->dma_area = (void __force *)(rme96->iobase +
1041 RME96_IO_REC_BUFFER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 runtime->dma_addr = rme96->port + RME96_IO_REC_BUFFER;
1043 runtime->dma_bytes = RME96_BUFFER_SIZE;
1044
1045 spin_lock_irq(&rme96->lock);
1046 if ((err = snd_rme96_capture_setformat(rme96, params_format(params))) < 0) {
1047 spin_unlock_irq(&rme96->lock);
1048 return err;
1049 }
1050 if (snd_rme96_getinputtype(rme96) == RME96_INPUT_ANALOG) {
1051 if ((err = snd_rme96_capture_analog_setrate(rme96,
1052 params_rate(params))) < 0)
1053 {
1054 spin_unlock_irq(&rme96->lock);
1055 return err;
1056 }
1057 } else if ((rate = snd_rme96_capture_getrate(rme96, &isadat)) > 0) {
1058 if ((int)params_rate(params) != rate) {
1059 spin_unlock_irq(&rme96->lock);
1060 return -EIO;
1061 }
1062 if ((isadat && runtime->hw.channels_min == 2) ||
1063 (!isadat && runtime->hw.channels_min == 8))
1064 {
1065 spin_unlock_irq(&rme96->lock);
1066 return -EIO;
1067 }
1068 }
1069 snd_rme96_setframelog(rme96, params_channels(params), 0);
1070 if (rme96->playback_periodsize != 0) {
1071 if (params_period_size(params) << rme96->capture_frlog !=
1072 rme96->playback_periodsize)
1073 {
1074 spin_unlock_irq(&rme96->lock);
1075 return -EBUSY;
1076 }
1077 }
1078 rme96->capture_periodsize =
1079 params_period_size(params) << rme96->capture_frlog;
1080 snd_rme96_set_period_properties(rme96, rme96->capture_periodsize);
1081 spin_unlock_irq(&rme96->lock);
1082
1083 return 0;
1084}
1085
1086static void
Knut Petersenb892ca12013-08-13 21:18:12 +02001087snd_rme96_trigger(struct rme96 *rme96,
1088 int op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Knut Petersenb892ca12013-08-13 21:18:12 +02001090 if (op & RME96_TB_RESET_PLAYPOS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 writel(0, rme96->iobase + RME96_IO_RESET_PLAY_POS);
Knut Petersenb892ca12013-08-13 21:18:12 +02001092 if (op & RME96_TB_RESET_CAPTUREPOS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 writel(0, rme96->iobase + RME96_IO_RESET_REC_POS);
Knut Petersenb892ca12013-08-13 21:18:12 +02001094 if (op & RME96_TB_CLEAR_PLAYBACK_IRQ) {
1095 rme96->rcreg = readl(rme96->iobase + RME96_IO_CONTROL_REGISTER);
1096 if (rme96->rcreg & RME96_RCR_IRQ)
1097 writel(0, rme96->iobase + RME96_IO_CONFIRM_PLAY_IRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 }
Knut Petersenb892ca12013-08-13 21:18:12 +02001099 if (op & RME96_TB_CLEAR_CAPTURE_IRQ) {
1100 rme96->rcreg = readl(rme96->iobase + RME96_IO_CONTROL_REGISTER);
1101 if (rme96->rcreg & RME96_RCR_IRQ_2)
1102 writel(0, rme96->iobase + RME96_IO_CONFIRM_REC_IRQ);
1103 }
1104 if (op & RME96_TB_START_PLAYBACK)
1105 rme96->wcreg |= RME96_WCR_START;
1106 if (op & RME96_TB_STOP_PLAYBACK)
1107 rme96->wcreg &= ~RME96_WCR_START;
1108 if (op & RME96_TB_START_CAPTURE)
1109 rme96->wcreg |= RME96_WCR_START_2;
1110 if (op & RME96_TB_STOP_CAPTURE)
1111 rme96->wcreg &= ~RME96_WCR_START_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
1113}
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117static irqreturn_t
1118snd_rme96_interrupt(int irq,
David Howells7d12e782006-10-05 14:55:46 +01001119 void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001121 struct rme96 *rme96 = (struct rme96 *)dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 rme96->rcreg = readl(rme96->iobase + RME96_IO_CONTROL_REGISTER);
1124 /* fastpath out, to ease interrupt sharing */
1125 if (!((rme96->rcreg & RME96_RCR_IRQ) ||
1126 (rme96->rcreg & RME96_RCR_IRQ_2)))
1127 {
1128 return IRQ_NONE;
1129 }
1130
1131 if (rme96->rcreg & RME96_RCR_IRQ) {
1132 /* playback */
1133 snd_pcm_period_elapsed(rme96->playback_substream);
1134 writel(0, rme96->iobase + RME96_IO_CONFIRM_PLAY_IRQ);
1135 }
1136 if (rme96->rcreg & RME96_RCR_IRQ_2) {
1137 /* capture */
1138 snd_pcm_period_elapsed(rme96->capture_substream);
1139 writel(0, rme96->iobase + RME96_IO_CONFIRM_REC_IRQ);
1140 }
1141 return IRQ_HANDLED;
1142}
1143
1144static unsigned int period_bytes[] = { RME96_SMALL_BLOCK_SIZE, RME96_LARGE_BLOCK_SIZE };
1145
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001146static struct snd_pcm_hw_constraint_list hw_constraints_period_bytes = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 .count = ARRAY_SIZE(period_bytes),
1148 .list = period_bytes,
1149 .mask = 0
1150};
1151
Takashi Iwai2ce7fb52006-05-10 16:24:42 +02001152static void
1153rme96_set_buffer_size_constraint(struct rme96 *rme96,
1154 struct snd_pcm_runtime *runtime)
1155{
1156 unsigned int size;
1157
1158 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1159 RME96_BUFFER_SIZE, RME96_BUFFER_SIZE);
1160 if ((size = rme96->playback_periodsize) != 0 ||
1161 (size = rme96->capture_periodsize) != 0)
1162 snd_pcm_hw_constraint_minmax(runtime,
1163 SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1164 size, size);
1165 else
1166 snd_pcm_hw_constraint_list(runtime, 0,
1167 SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1168 &hw_constraints_period_bytes);
1169}
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001172snd_rme96_playback_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
1174 int rate, dummy;
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001175 struct rme96 *rme96 = snd_pcm_substream_chip(substream);
1176 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Knut Petersenb892ca12013-08-13 21:18:12 +02001178 snd_pcm_set_sync(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 spin_lock_irq(&rme96->lock);
1180 if (rme96->playback_substream != NULL) {
1181 spin_unlock_irq(&rme96->lock);
1182 return -EBUSY;
1183 }
1184 rme96->wcreg &= ~RME96_WCR_ADAT;
1185 writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
1186 rme96->playback_substream = substream;
1187 spin_unlock_irq(&rme96->lock);
1188
1189 runtime->hw = snd_rme96_playback_spdif_info;
1190 if (!(rme96->wcreg & RME96_WCR_MASTER) &&
1191 snd_rme96_getinputtype(rme96) != RME96_INPUT_ANALOG &&
1192 (rate = snd_rme96_capture_getrate(rme96, &dummy)) > 0)
1193 {
1194 /* slave clock */
Clemens Ladisch918f3a02007-08-13 17:40:54 +02001195 runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 runtime->hw.rate_min = rate;
1197 runtime->hw.rate_max = rate;
1198 }
Takashi Iwai2ce7fb52006-05-10 16:24:42 +02001199 rme96_set_buffer_size_constraint(rme96, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 rme96->wcreg_spdif_stream = rme96->wcreg_spdif;
1202 rme96->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1203 snd_ctl_notify(rme96->card, SNDRV_CTL_EVENT_MASK_VALUE |
1204 SNDRV_CTL_EVENT_MASK_INFO, &rme96->spdif_ctl->id);
1205 return 0;
1206}
1207
1208static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001209snd_rme96_capture_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
1211 int isadat, rate;
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001212 struct rme96 *rme96 = snd_pcm_substream_chip(substream);
1213 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Knut Petersenb892ca12013-08-13 21:18:12 +02001215 snd_pcm_set_sync(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 runtime->hw = snd_rme96_capture_spdif_info;
1217 if (snd_rme96_getinputtype(rme96) != RME96_INPUT_ANALOG &&
1218 (rate = snd_rme96_capture_getrate(rme96, &isadat)) > 0)
1219 {
1220 if (isadat) {
1221 return -EIO;
1222 }
Clemens Ladisch918f3a02007-08-13 17:40:54 +02001223 runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 runtime->hw.rate_min = rate;
1225 runtime->hw.rate_max = rate;
1226 }
1227
1228 spin_lock_irq(&rme96->lock);
1229 if (rme96->capture_substream != NULL) {
1230 spin_unlock_irq(&rme96->lock);
1231 return -EBUSY;
1232 }
1233 rme96->capture_substream = substream;
1234 spin_unlock_irq(&rme96->lock);
1235
Takashi Iwai2ce7fb52006-05-10 16:24:42 +02001236 rme96_set_buffer_size_constraint(rme96, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return 0;
1238}
1239
1240static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001241snd_rme96_playback_adat_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
1243 int rate, dummy;
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001244 struct rme96 *rme96 = snd_pcm_substream_chip(substream);
1245 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Knut Petersenb892ca12013-08-13 21:18:12 +02001247 snd_pcm_set_sync(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 spin_lock_irq(&rme96->lock);
1249 if (rme96->playback_substream != NULL) {
1250 spin_unlock_irq(&rme96->lock);
1251 return -EBUSY;
1252 }
1253 rme96->wcreg |= RME96_WCR_ADAT;
1254 writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
1255 rme96->playback_substream = substream;
1256 spin_unlock_irq(&rme96->lock);
1257
1258 runtime->hw = snd_rme96_playback_adat_info;
1259 if (!(rme96->wcreg & RME96_WCR_MASTER) &&
1260 snd_rme96_getinputtype(rme96) != RME96_INPUT_ANALOG &&
1261 (rate = snd_rme96_capture_getrate(rme96, &dummy)) > 0)
1262 {
1263 /* slave clock */
Clemens Ladisch918f3a02007-08-13 17:40:54 +02001264 runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 runtime->hw.rate_min = rate;
1266 runtime->hw.rate_max = rate;
1267 }
Takashi Iwai2ce7fb52006-05-10 16:24:42 +02001268 rme96_set_buffer_size_constraint(rme96, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return 0;
1270}
1271
1272static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001273snd_rme96_capture_adat_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
1275 int isadat, rate;
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001276 struct rme96 *rme96 = snd_pcm_substream_chip(substream);
1277 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Knut Petersenb892ca12013-08-13 21:18:12 +02001279 snd_pcm_set_sync(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 runtime->hw = snd_rme96_capture_adat_info;
1281 if (snd_rme96_getinputtype(rme96) == RME96_INPUT_ANALOG) {
1282 /* makes no sense to use analog input. Note that analog
1283 expension cards AEB4/8-I are RME96_INPUT_INTERNAL */
1284 return -EIO;
1285 }
1286 if ((rate = snd_rme96_capture_getrate(rme96, &isadat)) > 0) {
1287 if (!isadat) {
1288 return -EIO;
1289 }
Clemens Ladisch918f3a02007-08-13 17:40:54 +02001290 runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 runtime->hw.rate_min = rate;
1292 runtime->hw.rate_max = rate;
1293 }
1294
1295 spin_lock_irq(&rme96->lock);
1296 if (rme96->capture_substream != NULL) {
1297 spin_unlock_irq(&rme96->lock);
1298 return -EBUSY;
1299 }
1300 rme96->capture_substream = substream;
1301 spin_unlock_irq(&rme96->lock);
1302
Takashi Iwai2ce7fb52006-05-10 16:24:42 +02001303 rme96_set_buffer_size_constraint(rme96, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return 0;
1305}
1306
1307static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001308snd_rme96_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001310 struct rme96 *rme96 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 int spdif = 0;
1312
1313 spin_lock_irq(&rme96->lock);
1314 if (RME96_ISPLAYING(rme96)) {
Knut Petersenb892ca12013-08-13 21:18:12 +02001315 snd_rme96_trigger(rme96, RME96_STOP_PLAYBACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
1317 rme96->playback_substream = NULL;
1318 rme96->playback_periodsize = 0;
1319 spdif = (rme96->wcreg & RME96_WCR_ADAT) == 0;
1320 spin_unlock_irq(&rme96->lock);
1321 if (spdif) {
1322 rme96->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1323 snd_ctl_notify(rme96->card, SNDRV_CTL_EVENT_MASK_VALUE |
1324 SNDRV_CTL_EVENT_MASK_INFO, &rme96->spdif_ctl->id);
1325 }
1326 return 0;
1327}
1328
1329static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001330snd_rme96_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001332 struct rme96 *rme96 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 spin_lock_irq(&rme96->lock);
1335 if (RME96_ISRECORDING(rme96)) {
Knut Petersenb892ca12013-08-13 21:18:12 +02001336 snd_rme96_trigger(rme96, RME96_STOP_CAPTURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
1338 rme96->capture_substream = NULL;
1339 rme96->capture_periodsize = 0;
1340 spin_unlock_irq(&rme96->lock);
1341 return 0;
1342}
1343
1344static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001345snd_rme96_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001347 struct rme96 *rme96 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 spin_lock_irq(&rme96->lock);
1350 if (RME96_ISPLAYING(rme96)) {
Knut Petersenb892ca12013-08-13 21:18:12 +02001351 snd_rme96_trigger(rme96, RME96_STOP_PLAYBACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
1353 writel(0, rme96->iobase + RME96_IO_RESET_PLAY_POS);
1354 spin_unlock_irq(&rme96->lock);
1355 return 0;
1356}
1357
1358static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001359snd_rme96_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001361 struct rme96 *rme96 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 spin_lock_irq(&rme96->lock);
1364 if (RME96_ISRECORDING(rme96)) {
Knut Petersenb892ca12013-08-13 21:18:12 +02001365 snd_rme96_trigger(rme96, RME96_STOP_CAPTURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
1367 writel(0, rme96->iobase + RME96_IO_RESET_REC_POS);
1368 spin_unlock_irq(&rme96->lock);
1369 return 0;
1370}
1371
1372static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001373snd_rme96_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 int cmd)
1375{
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001376 struct rme96 *rme96 = snd_pcm_substream_chip(substream);
Knut Petersenb892ca12013-08-13 21:18:12 +02001377 struct snd_pcm_substream *s;
1378 bool sync;
1379
1380 snd_pcm_group_for_each_entry(s, substream) {
1381 if (snd_pcm_substream_chip(s) == rme96)
1382 snd_pcm_trigger_done(s, substream);
1383 }
1384
1385 sync = (rme96->playback_substream && rme96->capture_substream) &&
1386 (rme96->playback_substream->group ==
1387 rme96->capture_substream->group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 switch (cmd) {
1390 case SNDRV_PCM_TRIGGER_START:
1391 if (!RME96_ISPLAYING(rme96)) {
Knut Petersenb892ca12013-08-13 21:18:12 +02001392 if (substream != rme96->playback_substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return -EBUSY;
Knut Petersenb892ca12013-08-13 21:18:12 +02001394 snd_rme96_trigger(rme96, sync ? RME96_START_BOTH
1395 : RME96_START_PLAYBACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
1397 break;
1398
Knut Petersen528ba522013-08-21 09:18:54 +02001399 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 case SNDRV_PCM_TRIGGER_STOP:
1401 if (RME96_ISPLAYING(rme96)) {
Knut Petersenb892ca12013-08-13 21:18:12 +02001402 if (substream != rme96->playback_substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 return -EBUSY;
Knut Petersenb892ca12013-08-13 21:18:12 +02001404 snd_rme96_trigger(rme96, sync ? RME96_STOP_BOTH
1405 : RME96_STOP_PLAYBACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
1407 break;
1408
1409 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Knut Petersenb892ca12013-08-13 21:18:12 +02001410 if (RME96_ISPLAYING(rme96))
1411 snd_rme96_trigger(rme96, sync ? RME96_STOP_BOTH
1412 : RME96_STOP_PLAYBACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 break;
1414
Knut Petersen528ba522013-08-21 09:18:54 +02001415 case SNDRV_PCM_TRIGGER_RESUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Knut Petersenb892ca12013-08-13 21:18:12 +02001417 if (!RME96_ISPLAYING(rme96))
1418 snd_rme96_trigger(rme96, sync ? RME96_RESUME_BOTH
1419 : RME96_RESUME_PLAYBACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 break;
Knut Petersenb892ca12013-08-13 21:18:12 +02001421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 default:
1423 return -EINVAL;
1424 }
Knut Petersenb892ca12013-08-13 21:18:12 +02001425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return 0;
1427}
1428
1429static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001430snd_rme96_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 int cmd)
1432{
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001433 struct rme96 *rme96 = snd_pcm_substream_chip(substream);
Knut Petersenb892ca12013-08-13 21:18:12 +02001434 struct snd_pcm_substream *s;
1435 bool sync;
1436
1437 snd_pcm_group_for_each_entry(s, substream) {
1438 if (snd_pcm_substream_chip(s) == rme96)
1439 snd_pcm_trigger_done(s, substream);
1440 }
1441
1442 sync = (rme96->playback_substream && rme96->capture_substream) &&
1443 (rme96->playback_substream->group ==
1444 rme96->capture_substream->group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 switch (cmd) {
1447 case SNDRV_PCM_TRIGGER_START:
1448 if (!RME96_ISRECORDING(rme96)) {
Knut Petersenb892ca12013-08-13 21:18:12 +02001449 if (substream != rme96->capture_substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return -EBUSY;
Knut Petersenb892ca12013-08-13 21:18:12 +02001451 snd_rme96_trigger(rme96, sync ? RME96_START_BOTH
1452 : RME96_START_CAPTURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 }
1454 break;
1455
Knut Petersen528ba522013-08-21 09:18:54 +02001456 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 case SNDRV_PCM_TRIGGER_STOP:
1458 if (RME96_ISRECORDING(rme96)) {
Knut Petersenb892ca12013-08-13 21:18:12 +02001459 if (substream != rme96->capture_substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return -EBUSY;
Knut Petersenb892ca12013-08-13 21:18:12 +02001461 snd_rme96_trigger(rme96, sync ? RME96_STOP_BOTH
1462 : RME96_STOP_CAPTURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
1464 break;
1465
1466 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Knut Petersenb892ca12013-08-13 21:18:12 +02001467 if (RME96_ISRECORDING(rme96))
1468 snd_rme96_trigger(rme96, sync ? RME96_STOP_BOTH
1469 : RME96_STOP_CAPTURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 break;
1471
Knut Petersen528ba522013-08-21 09:18:54 +02001472 case SNDRV_PCM_TRIGGER_RESUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Knut Petersenb892ca12013-08-13 21:18:12 +02001474 if (!RME96_ISRECORDING(rme96))
1475 snd_rme96_trigger(rme96, sync ? RME96_RESUME_BOTH
1476 : RME96_RESUME_CAPTURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 break;
Knut Petersenb892ca12013-08-13 21:18:12 +02001478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 default:
1480 return -EINVAL;
1481 }
1482
1483 return 0;
1484}
1485
1486static snd_pcm_uframes_t
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001487snd_rme96_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001489 struct rme96 *rme96 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 return snd_rme96_playback_ptr(rme96);
1491}
1492
1493static snd_pcm_uframes_t
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001494snd_rme96_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001496 struct rme96 *rme96 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return snd_rme96_capture_ptr(rme96);
1498}
1499
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001500static struct snd_pcm_ops snd_rme96_playback_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 .open = snd_rme96_playback_spdif_open,
1502 .close = snd_rme96_playback_close,
1503 .ioctl = snd_pcm_lib_ioctl,
1504 .hw_params = snd_rme96_playback_hw_params,
1505 .prepare = snd_rme96_playback_prepare,
1506 .trigger = snd_rme96_playback_trigger,
1507 .pointer = snd_rme96_playback_pointer,
1508 .copy = snd_rme96_playback_copy,
1509 .silence = snd_rme96_playback_silence,
1510 .mmap = snd_pcm_lib_mmap_iomem,
1511};
1512
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001513static struct snd_pcm_ops snd_rme96_capture_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 .open = snd_rme96_capture_spdif_open,
1515 .close = snd_rme96_capture_close,
1516 .ioctl = snd_pcm_lib_ioctl,
1517 .hw_params = snd_rme96_capture_hw_params,
1518 .prepare = snd_rme96_capture_prepare,
1519 .trigger = snd_rme96_capture_trigger,
1520 .pointer = snd_rme96_capture_pointer,
1521 .copy = snd_rme96_capture_copy,
1522 .mmap = snd_pcm_lib_mmap_iomem,
1523};
1524
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001525static struct snd_pcm_ops snd_rme96_playback_adat_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 .open = snd_rme96_playback_adat_open,
1527 .close = snd_rme96_playback_close,
1528 .ioctl = snd_pcm_lib_ioctl,
1529 .hw_params = snd_rme96_playback_hw_params,
1530 .prepare = snd_rme96_playback_prepare,
1531 .trigger = snd_rme96_playback_trigger,
1532 .pointer = snd_rme96_playback_pointer,
1533 .copy = snd_rme96_playback_copy,
1534 .silence = snd_rme96_playback_silence,
1535 .mmap = snd_pcm_lib_mmap_iomem,
1536};
1537
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001538static struct snd_pcm_ops snd_rme96_capture_adat_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 .open = snd_rme96_capture_adat_open,
1540 .close = snd_rme96_capture_close,
1541 .ioctl = snd_pcm_lib_ioctl,
1542 .hw_params = snd_rme96_capture_hw_params,
1543 .prepare = snd_rme96_capture_prepare,
1544 .trigger = snd_rme96_capture_trigger,
1545 .pointer = snd_rme96_capture_pointer,
1546 .copy = snd_rme96_capture_copy,
1547 .mmap = snd_pcm_lib_mmap_iomem,
1548};
1549
1550static void
1551snd_rme96_free(void *private_data)
1552{
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001553 struct rme96 *rme96 = (struct rme96 *)private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
1555 if (rme96 == NULL) {
1556 return;
1557 }
1558 if (rme96->irq >= 0) {
Knut Petersenb892ca12013-08-13 21:18:12 +02001559 snd_rme96_trigger(rme96, RME96_STOP_BOTH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 rme96->areg &= ~RME96_AR_DAC_EN;
1561 writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
1562 free_irq(rme96->irq, (void *)rme96);
1563 rme96->irq = -1;
1564 }
1565 if (rme96->iobase) {
1566 iounmap(rme96->iobase);
1567 rme96->iobase = NULL;
1568 }
1569 if (rme96->port) {
1570 pci_release_regions(rme96->pci);
1571 rme96->port = 0;
1572 }
Takashi Iwaia932be92014-02-25 15:01:15 +01001573#ifdef CONFIG_PM_SLEEP
Knut Petersen528ba522013-08-21 09:18:54 +02001574 vfree(rme96->playback_suspend_buffer);
1575 vfree(rme96->capture_suspend_buffer);
1576#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 pci_disable_device(rme96->pci);
1578}
1579
1580static void
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001581snd_rme96_free_spdif_pcm(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
Joe Perches9fe856e2010-09-04 18:52:54 -07001583 struct rme96 *rme96 = pcm->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 rme96->spdif_pcm = NULL;
1585}
1586
1587static void
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001588snd_rme96_free_adat_pcm(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
Joe Perches9fe856e2010-09-04 18:52:54 -07001590 struct rme96 *rme96 = pcm->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 rme96->adat_pcm = NULL;
1592}
1593
Bill Pembertone23e7a12012-12-06 12:35:10 -05001594static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001595snd_rme96_create(struct rme96 *rme96)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
1597 struct pci_dev *pci = rme96->pci;
1598 int err;
1599
1600 rme96->irq = -1;
1601 spin_lock_init(&rme96->lock);
1602
1603 if ((err = pci_enable_device(pci)) < 0)
1604 return err;
1605
1606 if ((err = pci_request_regions(pci, "RME96")) < 0)
1607 return err;
1608 rme96->port = pci_resource_start(rme96->pci, 0);
1609
Harvey Harrison44977b712008-02-28 11:57:47 +01001610 rme96->iobase = ioremap_nocache(rme96->port, RME96_IO_SIZE);
1611 if (!rme96->iobase) {
Takashi Iwai342cd9342014-02-25 15:03:18 +01001612 dev_err(rme96->card->dev,
1613 "unable to remap memory region 0x%lx-0x%lx\n",
1614 rme96->port, rme96->port + RME96_IO_SIZE - 1);
Takashi Iwai688956f22006-06-06 15:44:34 +02001615 return -ENOMEM;
1616 }
1617
Takashi Iwai437a5a42006-11-21 12:14:23 +01001618 if (request_irq(pci->irq, snd_rme96_interrupt, IRQF_SHARED,
Takashi Iwai934c2b62011-06-10 16:36:37 +02001619 KBUILD_MODNAME, rme96)) {
Takashi Iwai342cd9342014-02-25 15:03:18 +01001620 dev_err(rme96->card->dev, "unable to grab IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 return -EBUSY;
1622 }
1623 rme96->irq = pci->irq;
1624
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 /* read the card's revision number */
1626 pci_read_config_byte(pci, 8, &rme96->rev);
1627
1628 /* set up ALSA pcm device for S/PDIF */
1629 if ((err = snd_pcm_new(rme96->card, "Digi96 IEC958", 0,
1630 1, 1, &rme96->spdif_pcm)) < 0)
1631 {
1632 return err;
1633 }
1634 rme96->spdif_pcm->private_data = rme96;
1635 rme96->spdif_pcm->private_free = snd_rme96_free_spdif_pcm;
1636 strcpy(rme96->spdif_pcm->name, "Digi96 IEC958");
1637 snd_pcm_set_ops(rme96->spdif_pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_rme96_playback_spdif_ops);
1638 snd_pcm_set_ops(rme96->spdif_pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_rme96_capture_spdif_ops);
1639
1640 rme96->spdif_pcm->info_flags = 0;
1641
1642 /* set up ALSA pcm device for ADAT */
Roland Dreier8b7fc422005-09-14 14:19:17 -07001643 if (pci->device == PCI_DEVICE_ID_RME_DIGI96) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 /* ADAT is not available on the base model */
1645 rme96->adat_pcm = NULL;
1646 } else {
1647 if ((err = snd_pcm_new(rme96->card, "Digi96 ADAT", 1,
1648 1, 1, &rme96->adat_pcm)) < 0)
1649 {
1650 return err;
1651 }
1652 rme96->adat_pcm->private_data = rme96;
1653 rme96->adat_pcm->private_free = snd_rme96_free_adat_pcm;
1654 strcpy(rme96->adat_pcm->name, "Digi96 ADAT");
1655 snd_pcm_set_ops(rme96->adat_pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_rme96_playback_adat_ops);
1656 snd_pcm_set_ops(rme96->adat_pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_rme96_capture_adat_ops);
1657
1658 rme96->adat_pcm->info_flags = 0;
1659 }
1660
1661 rme96->playback_periodsize = 0;
1662 rme96->capture_periodsize = 0;
1663
1664 /* make sure playback/capture is stopped, if by some reason active */
Knut Petersenb892ca12013-08-13 21:18:12 +02001665 snd_rme96_trigger(rme96, RME96_STOP_BOTH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 /* set default values in registers */
1668 rme96->wcreg =
1669 RME96_WCR_FREQ_1 | /* set 44.1 kHz playback */
1670 RME96_WCR_SEL | /* normal playback */
1671 RME96_WCR_MASTER | /* set to master clock mode */
1672 RME96_WCR_INP_0; /* set coaxial input */
1673
1674 rme96->areg = RME96_AR_FREQPAD_1; /* set 44.1 kHz analog capture */
1675
1676 writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
1677 writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
1678
1679 /* reset the ADC */
1680 writel(rme96->areg | RME96_AR_PD2,
1681 rme96->iobase + RME96_IO_ADDITIONAL_REG);
1682 writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
1683
1684 /* reset and enable the DAC (order is important). */
1685 snd_rme96_reset_dac(rme96);
1686 rme96->areg |= RME96_AR_DAC_EN;
1687 writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
1688
1689 /* reset playback and record buffer pointers */
1690 writel(0, rme96->iobase + RME96_IO_RESET_PLAY_POS);
1691 writel(0, rme96->iobase + RME96_IO_RESET_REC_POS);
1692
1693 /* reset volume */
1694 rme96->vol[0] = rme96->vol[1] = 0;
1695 if (RME96_HAS_ANALOG_OUT(rme96)) {
1696 snd_rme96_apply_dac_volume(rme96);
1697 }
1698
1699 /* init switch interface */
1700 if ((err = snd_rme96_create_switches(rme96->card, rme96)) < 0) {
1701 return err;
1702 }
1703
1704 /* init proc interface */
1705 snd_rme96_proc_init(rme96);
1706
1707 return 0;
1708}
1709
1710/*
1711 * proc interface
1712 */
1713
1714static void
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001715snd_rme96_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
1717 int n;
Joe Perches9fe856e2010-09-04 18:52:54 -07001718 struct rme96 *rme96 = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 rme96->rcreg = readl(rme96->iobase + RME96_IO_CONTROL_REGISTER);
1721
1722 snd_iprintf(buffer, rme96->card->longname);
1723 snd_iprintf(buffer, " (index #%d)\n", rme96->card->number + 1);
1724
1725 snd_iprintf(buffer, "\nGeneral settings\n");
1726 if (rme96->wcreg & RME96_WCR_IDIS) {
1727 snd_iprintf(buffer, " period size: N/A (interrupts "
1728 "disabled)\n");
1729 } else if (rme96->wcreg & RME96_WCR_ISEL) {
1730 snd_iprintf(buffer, " period size: 2048 bytes\n");
1731 } else {
1732 snd_iprintf(buffer, " period size: 8192 bytes\n");
1733 }
1734 snd_iprintf(buffer, "\nInput settings\n");
1735 switch (snd_rme96_getinputtype(rme96)) {
1736 case RME96_INPUT_OPTICAL:
1737 snd_iprintf(buffer, " input: optical");
1738 break;
1739 case RME96_INPUT_COAXIAL:
1740 snd_iprintf(buffer, " input: coaxial");
1741 break;
1742 case RME96_INPUT_INTERNAL:
1743 snd_iprintf(buffer, " input: internal");
1744 break;
1745 case RME96_INPUT_XLR:
1746 snd_iprintf(buffer, " input: XLR");
1747 break;
1748 case RME96_INPUT_ANALOG:
1749 snd_iprintf(buffer, " input: analog");
1750 break;
1751 }
1752 if (snd_rme96_capture_getrate(rme96, &n) < 0) {
1753 snd_iprintf(buffer, "\n sample rate: no valid signal\n");
1754 } else {
1755 if (n) {
1756 snd_iprintf(buffer, " (8 channels)\n");
1757 } else {
1758 snd_iprintf(buffer, " (2 channels)\n");
1759 }
1760 snd_iprintf(buffer, " sample rate: %d Hz\n",
1761 snd_rme96_capture_getrate(rme96, &n));
1762 }
1763 if (rme96->wcreg & RME96_WCR_MODE24_2) {
1764 snd_iprintf(buffer, " sample format: 24 bit\n");
1765 } else {
1766 snd_iprintf(buffer, " sample format: 16 bit\n");
1767 }
1768
1769 snd_iprintf(buffer, "\nOutput settings\n");
1770 if (rme96->wcreg & RME96_WCR_SEL) {
1771 snd_iprintf(buffer, " output signal: normal playback\n");
1772 } else {
1773 snd_iprintf(buffer, " output signal: same as input\n");
1774 }
1775 snd_iprintf(buffer, " sample rate: %d Hz\n",
1776 snd_rme96_playback_getrate(rme96));
1777 if (rme96->wcreg & RME96_WCR_MODE24) {
1778 snd_iprintf(buffer, " sample format: 24 bit\n");
1779 } else {
1780 snd_iprintf(buffer, " sample format: 16 bit\n");
1781 }
1782 if (rme96->areg & RME96_AR_WSEL) {
1783 snd_iprintf(buffer, " sample clock source: word clock\n");
1784 } else if (rme96->wcreg & RME96_WCR_MASTER) {
1785 snd_iprintf(buffer, " sample clock source: internal\n");
1786 } else if (snd_rme96_getinputtype(rme96) == RME96_INPUT_ANALOG) {
1787 snd_iprintf(buffer, " sample clock source: autosync (internal anyway due to analog input setting)\n");
1788 } else if (snd_rme96_capture_getrate(rme96, &n) < 0) {
1789 snd_iprintf(buffer, " sample clock source: autosync (internal anyway due to no valid signal)\n");
1790 } else {
1791 snd_iprintf(buffer, " sample clock source: autosync\n");
1792 }
1793 if (rme96->wcreg & RME96_WCR_PRO) {
1794 snd_iprintf(buffer, " format: AES/EBU (professional)\n");
1795 } else {
1796 snd_iprintf(buffer, " format: IEC958 (consumer)\n");
1797 }
1798 if (rme96->wcreg & RME96_WCR_EMP) {
1799 snd_iprintf(buffer, " emphasis: on\n");
1800 } else {
1801 snd_iprintf(buffer, " emphasis: off\n");
1802 }
1803 if (rme96->wcreg & RME96_WCR_DOLBY) {
1804 snd_iprintf(buffer, " non-audio (dolby): on\n");
1805 } else {
1806 snd_iprintf(buffer, " non-audio (dolby): off\n");
1807 }
1808 if (RME96_HAS_ANALOG_IN(rme96)) {
1809 snd_iprintf(buffer, "\nAnalog output settings\n");
1810 switch (snd_rme96_getmontracks(rme96)) {
1811 case RME96_MONITOR_TRACKS_1_2:
1812 snd_iprintf(buffer, " monitored ADAT tracks: 1+2\n");
1813 break;
1814 case RME96_MONITOR_TRACKS_3_4:
1815 snd_iprintf(buffer, " monitored ADAT tracks: 3+4\n");
1816 break;
1817 case RME96_MONITOR_TRACKS_5_6:
1818 snd_iprintf(buffer, " monitored ADAT tracks: 5+6\n");
1819 break;
1820 case RME96_MONITOR_TRACKS_7_8:
1821 snd_iprintf(buffer, " monitored ADAT tracks: 7+8\n");
1822 break;
1823 }
1824 switch (snd_rme96_getattenuation(rme96)) {
1825 case RME96_ATTENUATION_0:
1826 snd_iprintf(buffer, " attenuation: 0 dB\n");
1827 break;
1828 case RME96_ATTENUATION_6:
1829 snd_iprintf(buffer, " attenuation: -6 dB\n");
1830 break;
1831 case RME96_ATTENUATION_12:
1832 snd_iprintf(buffer, " attenuation: -12 dB\n");
1833 break;
1834 case RME96_ATTENUATION_18:
1835 snd_iprintf(buffer, " attenuation: -18 dB\n");
1836 break;
1837 }
1838 snd_iprintf(buffer, " volume left: %u\n", rme96->vol[0]);
1839 snd_iprintf(buffer, " volume right: %u\n", rme96->vol[1]);
1840 }
1841}
1842
Bill Pembertone23e7a12012-12-06 12:35:10 -05001843static void snd_rme96_proc_init(struct rme96 *rme96)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001845 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
1847 if (! snd_card_proc_new(rme96->card, "rme96", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02001848 snd_info_set_text_ops(entry, rme96, snd_rme96_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849}
1850
1851/*
1852 * control interface
1853 */
1854
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001855#define snd_rme96_info_loopback_control snd_ctl_boolean_mono_info
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001858snd_rme96_get_loopback_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001860 struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862 spin_lock_irq(&rme96->lock);
1863 ucontrol->value.integer.value[0] = rme96->wcreg & RME96_WCR_SEL ? 0 : 1;
1864 spin_unlock_irq(&rme96->lock);
1865 return 0;
1866}
1867static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001868snd_rme96_put_loopback_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869{
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001870 struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 unsigned int val;
1872 int change;
1873
1874 val = ucontrol->value.integer.value[0] ? 0 : RME96_WCR_SEL;
1875 spin_lock_irq(&rme96->lock);
1876 val = (rme96->wcreg & ~RME96_WCR_SEL) | val;
1877 change = val != rme96->wcreg;
1878 rme96->wcreg = val;
1879 writel(val, rme96->iobase + RME96_IO_CONTROL_REGISTER);
1880 spin_unlock_irq(&rme96->lock);
1881 return change;
1882}
1883
1884static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001885snd_rme96_info_inputtype_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
Takashi Iwai9c30d462014-10-20 18:19:23 +02001887 static const char * const _texts[5] = {
1888 "Optical", "Coaxial", "Internal", "XLR", "Analog"
1889 };
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001890 struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
Takashi Iwai9c30d462014-10-20 18:19:23 +02001891 const char *texts[5] = {
1892 _texts[0], _texts[1], _texts[2], _texts[3], _texts[4]
1893 };
1894 int num_items;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 switch (rme96->pci->device) {
Roland Dreier8b7fc422005-09-14 14:19:17 -07001897 case PCI_DEVICE_ID_RME_DIGI96:
1898 case PCI_DEVICE_ID_RME_DIGI96_8:
Takashi Iwai9c30d462014-10-20 18:19:23 +02001899 num_items = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 break;
Roland Dreier8b7fc422005-09-14 14:19:17 -07001901 case PCI_DEVICE_ID_RME_DIGI96_8_PRO:
Takashi Iwai9c30d462014-10-20 18:19:23 +02001902 num_items = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 break;
Roland Dreier8b7fc422005-09-14 14:19:17 -07001904 case PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (rme96->rev > 4) {
1906 /* PST */
Takashi Iwai9c30d462014-10-20 18:19:23 +02001907 num_items = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 texts[3] = _texts[4]; /* Analog instead of XLR */
1909 } else {
1910 /* PAD */
Takashi Iwai9c30d462014-10-20 18:19:23 +02001911 num_items = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 }
1913 break;
1914 default:
1915 snd_BUG();
Takashi Iwai9c30d462014-10-20 18:19:23 +02001916 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 }
Takashi Iwai9c30d462014-10-20 18:19:23 +02001918 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919}
1920static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001921snd_rme96_get_inputtype_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001923 struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 unsigned int items = 3;
1925
1926 spin_lock_irq(&rme96->lock);
1927 ucontrol->value.enumerated.item[0] = snd_rme96_getinputtype(rme96);
1928
1929 switch (rme96->pci->device) {
Roland Dreier8b7fc422005-09-14 14:19:17 -07001930 case PCI_DEVICE_ID_RME_DIGI96:
1931 case PCI_DEVICE_ID_RME_DIGI96_8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 items = 3;
1933 break;
Roland Dreier8b7fc422005-09-14 14:19:17 -07001934 case PCI_DEVICE_ID_RME_DIGI96_8_PRO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 items = 4;
1936 break;
Roland Dreier8b7fc422005-09-14 14:19:17 -07001937 case PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 if (rme96->rev > 4) {
1939 /* for handling PST case, (INPUT_ANALOG is moved to INPUT_XLR */
1940 if (ucontrol->value.enumerated.item[0] == RME96_INPUT_ANALOG) {
1941 ucontrol->value.enumerated.item[0] = RME96_INPUT_XLR;
1942 }
1943 items = 4;
1944 } else {
1945 items = 5;
1946 }
1947 break;
1948 default:
1949 snd_BUG();
1950 break;
1951 }
1952 if (ucontrol->value.enumerated.item[0] >= items) {
1953 ucontrol->value.enumerated.item[0] = items - 1;
1954 }
1955
1956 spin_unlock_irq(&rme96->lock);
1957 return 0;
1958}
1959static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001960snd_rme96_put_inputtype_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961{
Takashi Iwaia3aefd82005-11-17 15:05:37 +01001962 struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 unsigned int val;
1964 int change, items = 3;
1965
1966 switch (rme96->pci->device) {
Roland Dreier8b7fc422005-09-14 14:19:17 -07001967 case PCI_DEVICE_ID_RME_DIGI96:
1968 case PCI_DEVICE_ID_RME_DIGI96_8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 items = 3;
1970 break;
Roland Dreier8b7fc422005-09-14 14:19:17 -07001971 case PCI_DEVICE_ID_RME_DIGI96_8_PRO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 items = 4;
1973 break;
Roland Dreier8b7fc422005-09-14 14:19:17 -07001974 case PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 if (rme96->rev > 4) {
1976 items = 4;
1977 } else {
1978 items = 5;
1979 }
1980 break;
1981 default:
1982 snd_BUG();
1983 break;
1984 }
1985 val = ucontrol->value.enumerated.item[0] % items;
1986
1987 /* special case for PST */
Roland Dreier8b7fc422005-09-14 14:19:17 -07001988 if (rme96->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST && rme96->rev > 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 if (val == RME96_INPUT_XLR) {
1990 val = RME96_INPUT_ANALOG;
1991 }
1992 }
1993
1994 spin_lock_irq(&rme96->lock);
1995 change = (int)val != snd_rme96_getinputtype(rme96);
1996 snd_rme96_setinputtype(rme96, val);
1997 spin_unlock_irq(&rme96->lock);
1998 return change;
1999}
2000
2001static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002002snd_rme96_info_clockmode_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
Takashi Iwai9c30d462014-10-20 18:19:23 +02002004 static const char * const texts[3] = { "AutoSync", "Internal", "Word" };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Takashi Iwai9c30d462014-10-20 18:19:23 +02002006 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007}
2008static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002009snd_rme96_get_clockmode_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002011 struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 spin_lock_irq(&rme96->lock);
2014 ucontrol->value.enumerated.item[0] = snd_rme96_getclockmode(rme96);
2015 spin_unlock_irq(&rme96->lock);
2016 return 0;
2017}
2018static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002019snd_rme96_put_clockmode_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002021 struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 unsigned int val;
2023 int change;
2024
2025 val = ucontrol->value.enumerated.item[0] % 3;
2026 spin_lock_irq(&rme96->lock);
2027 change = (int)val != snd_rme96_getclockmode(rme96);
2028 snd_rme96_setclockmode(rme96, val);
2029 spin_unlock_irq(&rme96->lock);
2030 return change;
2031}
2032
2033static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002034snd_rme96_info_attenuation_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
Takashi Iwai9c30d462014-10-20 18:19:23 +02002036 static const char * const texts[4] = {
2037 "0 dB", "-6 dB", "-12 dB", "-18 dB"
2038 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Takashi Iwai9c30d462014-10-20 18:19:23 +02002040 return snd_ctl_enum_info(uinfo, 1, 4, texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041}
2042static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002043snd_rme96_get_attenuation_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002045 struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
2047 spin_lock_irq(&rme96->lock);
2048 ucontrol->value.enumerated.item[0] = snd_rme96_getattenuation(rme96);
2049 spin_unlock_irq(&rme96->lock);
2050 return 0;
2051}
2052static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002053snd_rme96_put_attenuation_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002055 struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 unsigned int val;
2057 int change;
2058
2059 val = ucontrol->value.enumerated.item[0] % 4;
2060 spin_lock_irq(&rme96->lock);
2061
2062 change = (int)val != snd_rme96_getattenuation(rme96);
2063 snd_rme96_setattenuation(rme96, val);
2064 spin_unlock_irq(&rme96->lock);
2065 return change;
2066}
2067
2068static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002069snd_rme96_info_montracks_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
Takashi Iwai9c30d462014-10-20 18:19:23 +02002071 static const char * const texts[4] = { "1+2", "3+4", "5+6", "7+8" };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Takashi Iwai9c30d462014-10-20 18:19:23 +02002073 return snd_ctl_enum_info(uinfo, 1, 4, texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074}
2075static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002076snd_rme96_get_montracks_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002078 struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080 spin_lock_irq(&rme96->lock);
2081 ucontrol->value.enumerated.item[0] = snd_rme96_getmontracks(rme96);
2082 spin_unlock_irq(&rme96->lock);
2083 return 0;
2084}
2085static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002086snd_rme96_put_montracks_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002088 struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 unsigned int val;
2090 int change;
2091
2092 val = ucontrol->value.enumerated.item[0] % 4;
2093 spin_lock_irq(&rme96->lock);
2094 change = (int)val != snd_rme96_getmontracks(rme96);
2095 snd_rme96_setmontracks(rme96, val);
2096 spin_unlock_irq(&rme96->lock);
2097 return change;
2098}
2099
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002100static u32 snd_rme96_convert_from_aes(struct snd_aes_iec958 *aes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101{
2102 u32 val = 0;
2103 val |= (aes->status[0] & IEC958_AES0_PROFESSIONAL) ? RME96_WCR_PRO : 0;
2104 val |= (aes->status[0] & IEC958_AES0_NONAUDIO) ? RME96_WCR_DOLBY : 0;
2105 if (val & RME96_WCR_PRO)
2106 val |= (aes->status[0] & IEC958_AES0_PRO_EMPHASIS_5015) ? RME96_WCR_EMP : 0;
2107 else
2108 val |= (aes->status[0] & IEC958_AES0_CON_EMPHASIS_5015) ? RME96_WCR_EMP : 0;
2109 return val;
2110}
2111
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002112static void snd_rme96_convert_to_aes(struct snd_aes_iec958 *aes, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
2114 aes->status[0] = ((val & RME96_WCR_PRO) ? IEC958_AES0_PROFESSIONAL : 0) |
2115 ((val & RME96_WCR_DOLBY) ? IEC958_AES0_NONAUDIO : 0);
2116 if (val & RME96_WCR_PRO)
2117 aes->status[0] |= (val & RME96_WCR_EMP) ? IEC958_AES0_PRO_EMPHASIS_5015 : 0;
2118 else
2119 aes->status[0] |= (val & RME96_WCR_EMP) ? IEC958_AES0_CON_EMPHASIS_5015 : 0;
2120}
2121
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002122static int snd_rme96_control_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123{
2124 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2125 uinfo->count = 1;
2126 return 0;
2127}
2128
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002129static int snd_rme96_control_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002131 struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
2133 snd_rme96_convert_to_aes(&ucontrol->value.iec958, rme96->wcreg_spdif);
2134 return 0;
2135}
2136
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002137static int snd_rme96_control_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002139 struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 int change;
2141 u32 val;
2142
2143 val = snd_rme96_convert_from_aes(&ucontrol->value.iec958);
2144 spin_lock_irq(&rme96->lock);
2145 change = val != rme96->wcreg_spdif;
2146 rme96->wcreg_spdif = val;
2147 spin_unlock_irq(&rme96->lock);
2148 return change;
2149}
2150
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002151static int snd_rme96_control_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
2153 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2154 uinfo->count = 1;
2155 return 0;
2156}
2157
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002158static int snd_rme96_control_spdif_stream_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002160 struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
2162 snd_rme96_convert_to_aes(&ucontrol->value.iec958, rme96->wcreg_spdif_stream);
2163 return 0;
2164}
2165
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002166static int snd_rme96_control_spdif_stream_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002168 struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 int change;
2170 u32 val;
2171
2172 val = snd_rme96_convert_from_aes(&ucontrol->value.iec958);
2173 spin_lock_irq(&rme96->lock);
2174 change = val != rme96->wcreg_spdif_stream;
2175 rme96->wcreg_spdif_stream = val;
2176 rme96->wcreg &= ~(RME96_WCR_PRO | RME96_WCR_DOLBY | RME96_WCR_EMP);
2177 rme96->wcreg |= val;
2178 writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
2179 spin_unlock_irq(&rme96->lock);
2180 return change;
2181}
2182
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002183static int snd_rme96_control_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
2185 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2186 uinfo->count = 1;
2187 return 0;
2188}
2189
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002190static int snd_rme96_control_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
2192 ucontrol->value.iec958.status[0] = kcontrol->private_value;
2193 return 0;
2194}
2195
2196static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002197snd_rme96_dac_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198{
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002199 struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
2201 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2202 uinfo->count = 2;
2203 uinfo->value.integer.min = 0;
2204 uinfo->value.integer.max = RME96_185X_MAX_OUT(rme96);
2205 return 0;
2206}
2207
2208static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002209snd_rme96_dac_volume_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210{
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002211 struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
2213 spin_lock_irq(&rme96->lock);
2214 u->value.integer.value[0] = rme96->vol[0];
2215 u->value.integer.value[1] = rme96->vol[1];
2216 spin_unlock_irq(&rme96->lock);
2217
2218 return 0;
2219}
2220
2221static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002222snd_rme96_dac_volume_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002224 struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 int change = 0;
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01002226 unsigned int vol, maxvol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01002228
2229 if (!RME96_HAS_ANALOG_OUT(rme96))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 return -EINVAL;
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01002231 maxvol = RME96_185X_MAX_OUT(rme96);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 spin_lock_irq(&rme96->lock);
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01002233 vol = u->value.integer.value[0];
2234 if (vol != rme96->vol[0] && vol <= maxvol) {
2235 rme96->vol[0] = vol;
2236 change = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 }
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01002238 vol = u->value.integer.value[1];
2239 if (vol != rme96->vol[1] && vol <= maxvol) {
2240 rme96->vol[1] = vol;
2241 change = 1;
2242 }
2243 if (change)
2244 snd_rme96_apply_dac_volume(rme96);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 spin_unlock_irq(&rme96->lock);
2246
2247 return change;
2248}
2249
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002250static struct snd_kcontrol_new snd_rme96_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
2252 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2253 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
2254 .info = snd_rme96_control_spdif_info,
2255 .get = snd_rme96_control_spdif_get,
2256 .put = snd_rme96_control_spdif_put
2257},
2258{
2259 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2260 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2261 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
2262 .info = snd_rme96_control_spdif_stream_info,
2263 .get = snd_rme96_control_spdif_stream_get,
2264 .put = snd_rme96_control_spdif_stream_put
2265},
2266{
2267 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002268 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
2270 .info = snd_rme96_control_spdif_mask_info,
2271 .get = snd_rme96_control_spdif_mask_get,
2272 .private_value = IEC958_AES0_NONAUDIO |
2273 IEC958_AES0_PROFESSIONAL |
2274 IEC958_AES0_CON_EMPHASIS
2275},
2276{
2277 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002278 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
2280 .info = snd_rme96_control_spdif_mask_info,
2281 .get = snd_rme96_control_spdif_mask_get,
2282 .private_value = IEC958_AES0_NONAUDIO |
2283 IEC958_AES0_PROFESSIONAL |
2284 IEC958_AES0_PRO_EMPHASIS
2285},
2286{
2287 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2288 .name = "Input Connector",
2289 .info = snd_rme96_info_inputtype_control,
2290 .get = snd_rme96_get_inputtype_control,
2291 .put = snd_rme96_put_inputtype_control
2292},
2293{
2294 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2295 .name = "Loopback Input",
2296 .info = snd_rme96_info_loopback_control,
2297 .get = snd_rme96_get_loopback_control,
2298 .put = snd_rme96_put_loopback_control
2299},
2300{
2301 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2302 .name = "Sample Clock Source",
2303 .info = snd_rme96_info_clockmode_control,
2304 .get = snd_rme96_get_clockmode_control,
2305 .put = snd_rme96_put_clockmode_control
2306},
2307{
2308 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2309 .name = "Monitor Tracks",
2310 .info = snd_rme96_info_montracks_control,
2311 .get = snd_rme96_get_montracks_control,
2312 .put = snd_rme96_put_montracks_control
2313},
2314{
2315 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2316 .name = "Attenuation",
2317 .info = snd_rme96_info_attenuation_control,
2318 .get = snd_rme96_get_attenuation_control,
2319 .put = snd_rme96_put_attenuation_control
2320},
2321{
2322 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2323 .name = "DAC Playback Volume",
2324 .info = snd_rme96_dac_volume_info,
2325 .get = snd_rme96_dac_volume_get,
2326 .put = snd_rme96_dac_volume_put
2327}
2328};
2329
2330static int
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002331snd_rme96_create_switches(struct snd_card *card,
2332 struct rme96 *rme96)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
2334 int idx, err;
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002335 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
2337 for (idx = 0; idx < 7; idx++) {
2338 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_rme96_controls[idx], rme96))) < 0)
2339 return err;
2340 if (idx == 1) /* IEC958 (S/PDIF) Stream */
2341 rme96->spdif_ctl = kctl;
2342 }
2343
2344 if (RME96_HAS_ANALOG_OUT(rme96)) {
2345 for (idx = 7; idx < 10; idx++)
2346 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_rme96_controls[idx], rme96))) < 0)
2347 return err;
2348 }
2349
2350 return 0;
2351}
2352
2353/*
2354 * Card initialisation
2355 */
2356
Takashi Iwaia932be92014-02-25 15:01:15 +01002357#ifdef CONFIG_PM_SLEEP
Knut Petersen528ba522013-08-21 09:18:54 +02002358
Takashi Iwaia932be92014-02-25 15:01:15 +01002359static int rme96_suspend(struct device *dev)
Knut Petersen528ba522013-08-21 09:18:54 +02002360{
Takashi Iwaia932be92014-02-25 15:01:15 +01002361 struct pci_dev *pci = to_pci_dev(dev);
2362 struct snd_card *card = dev_get_drvdata(dev);
Knut Petersen528ba522013-08-21 09:18:54 +02002363 struct rme96 *rme96 = card->private_data;
2364
2365 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
2366 snd_pcm_suspend(rme96->playback_substream);
2367 snd_pcm_suspend(rme96->capture_substream);
2368
2369 /* save capture & playback pointers */
2370 rme96->playback_pointer = readl(rme96->iobase + RME96_IO_GET_PLAY_POS)
2371 & RME96_RCR_AUDIO_ADDR_MASK;
2372 rme96->capture_pointer = readl(rme96->iobase + RME96_IO_GET_REC_POS)
2373 & RME96_RCR_AUDIO_ADDR_MASK;
2374
2375 /* save playback and capture buffers */
2376 memcpy_fromio(rme96->playback_suspend_buffer,
2377 rme96->iobase + RME96_IO_PLAY_BUFFER, RME96_BUFFER_SIZE);
2378 memcpy_fromio(rme96->capture_suspend_buffer,
2379 rme96->iobase + RME96_IO_REC_BUFFER, RME96_BUFFER_SIZE);
2380
2381 /* disable the DAC */
2382 rme96->areg &= ~RME96_AR_DAC_EN;
2383 writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
2384
2385 pci_disable_device(pci);
2386 pci_save_state(pci);
2387
2388 return 0;
2389}
2390
Takashi Iwaia932be92014-02-25 15:01:15 +01002391static int rme96_resume(struct device *dev)
Knut Petersen528ba522013-08-21 09:18:54 +02002392{
Takashi Iwaia932be92014-02-25 15:01:15 +01002393 struct pci_dev *pci = to_pci_dev(dev);
2394 struct snd_card *card = dev_get_drvdata(dev);
Knut Petersen528ba522013-08-21 09:18:54 +02002395 struct rme96 *rme96 = card->private_data;
2396
2397 pci_restore_state(pci);
Takashi Iwaie58a2442013-08-22 12:02:31 +02002398 if (pci_enable_device(pci) < 0) {
Takashi Iwai342cd9342014-02-25 15:03:18 +01002399 dev_err(dev, "pci_enable_device failed, disabling device\n");
Takashi Iwaie58a2442013-08-22 12:02:31 +02002400 snd_card_disconnect(card);
2401 return -EIO;
2402 }
Knut Petersen528ba522013-08-21 09:18:54 +02002403
2404 /* reset playback and record buffer pointers */
2405 writel(0, rme96->iobase + RME96_IO_SET_PLAY_POS
2406 + rme96->playback_pointer);
2407 writel(0, rme96->iobase + RME96_IO_SET_REC_POS
2408 + rme96->capture_pointer);
2409
2410 /* restore playback and capture buffers */
2411 memcpy_toio(rme96->iobase + RME96_IO_PLAY_BUFFER,
2412 rme96->playback_suspend_buffer, RME96_BUFFER_SIZE);
2413 memcpy_toio(rme96->iobase + RME96_IO_REC_BUFFER,
2414 rme96->capture_suspend_buffer, RME96_BUFFER_SIZE);
2415
2416 /* reset the ADC */
2417 writel(rme96->areg | RME96_AR_PD2,
2418 rme96->iobase + RME96_IO_ADDITIONAL_REG);
2419 writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
2420
2421 /* reset and enable DAC, restore analog volume */
2422 snd_rme96_reset_dac(rme96);
2423 rme96->areg |= RME96_AR_DAC_EN;
2424 writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
2425 if (RME96_HAS_ANALOG_OUT(rme96)) {
2426 usleep_range(3000, 10000);
2427 snd_rme96_apply_dac_volume(rme96);
2428 }
2429
2430 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2431
2432 return 0;
2433}
2434
Takashi Iwaia932be92014-02-25 15:01:15 +01002435static SIMPLE_DEV_PM_OPS(rme96_pm, rme96_suspend, rme96_resume);
2436#define RME96_PM_OPS &rme96_pm
2437#else
2438#define RME96_PM_OPS NULL
2439#endif /* CONFIG_PM_SLEEP */
Knut Petersen528ba522013-08-21 09:18:54 +02002440
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002441static void snd_rme96_card_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442{
2443 snd_rme96_free(card->private_data);
2444}
2445
Bill Pembertone23e7a12012-12-06 12:35:10 -05002446static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447snd_rme96_probe(struct pci_dev *pci,
2448 const struct pci_device_id *pci_id)
2449{
2450 static int dev;
Takashi Iwaia3aefd82005-11-17 15:05:37 +01002451 struct rme96 *rme96;
2452 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 int err;
2454 u8 val;
2455
2456 if (dev >= SNDRV_CARDS) {
2457 return -ENODEV;
2458 }
2459 if (!enable[dev]) {
2460 dev++;
2461 return -ENOENT;
2462 }
Takashi Iwai60c57722014-01-29 14:20:19 +01002463 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
2464 sizeof(struct rme96), &card);
Takashi Iwaie58de7b2008-12-28 16:44:30 +01002465 if (err < 0)
2466 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 card->private_free = snd_rme96_card_free;
Joe Perches9fe856e2010-09-04 18:52:54 -07002468 rme96 = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 rme96->card = card;
2470 rme96->pci = pci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 if ((err = snd_rme96_create(rme96)) < 0) {
2472 snd_card_free(card);
2473 return err;
2474 }
2475
Takashi Iwaia932be92014-02-25 15:01:15 +01002476#ifdef CONFIG_PM_SLEEP
Knut Petersen528ba522013-08-21 09:18:54 +02002477 rme96->playback_suspend_buffer = vmalloc(RME96_BUFFER_SIZE);
2478 if (!rme96->playback_suspend_buffer) {
Takashi Iwai342cd9342014-02-25 15:03:18 +01002479 dev_err(card->dev,
Knut Petersen528ba522013-08-21 09:18:54 +02002480 "Failed to allocate playback suspend buffer!\n");
2481 snd_card_free(card);
2482 return -ENOMEM;
2483 }
2484 rme96->capture_suspend_buffer = vmalloc(RME96_BUFFER_SIZE);
2485 if (!rme96->capture_suspend_buffer) {
Takashi Iwai342cd9342014-02-25 15:03:18 +01002486 dev_err(card->dev,
Knut Petersen528ba522013-08-21 09:18:54 +02002487 "Failed to allocate capture suspend buffer!\n");
2488 snd_card_free(card);
2489 return -ENOMEM;
2490 }
2491#endif
2492
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 strcpy(card->driver, "Digi96");
2494 switch (rme96->pci->device) {
Roland Dreier8b7fc422005-09-14 14:19:17 -07002495 case PCI_DEVICE_ID_RME_DIGI96:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 strcpy(card->shortname, "RME Digi96");
2497 break;
Roland Dreier8b7fc422005-09-14 14:19:17 -07002498 case PCI_DEVICE_ID_RME_DIGI96_8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 strcpy(card->shortname, "RME Digi96/8");
2500 break;
Roland Dreier8b7fc422005-09-14 14:19:17 -07002501 case PCI_DEVICE_ID_RME_DIGI96_8_PRO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 strcpy(card->shortname, "RME Digi96/8 PRO");
2503 break;
Roland Dreier8b7fc422005-09-14 14:19:17 -07002504 case PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 pci_read_config_byte(rme96->pci, 8, &val);
2506 if (val < 5) {
2507 strcpy(card->shortname, "RME Digi96/8 PAD");
2508 } else {
2509 strcpy(card->shortname, "RME Digi96/8 PST");
2510 }
2511 break;
2512 }
2513 sprintf(card->longname, "%s at 0x%lx, irq %d", card->shortname,
2514 rme96->port, rme96->irq);
2515
2516 if ((err = snd_card_register(card)) < 0) {
2517 snd_card_free(card);
2518 return err;
2519 }
2520 pci_set_drvdata(pci, card);
2521 dev++;
2522 return 0;
2523}
2524
Bill Pembertone23e7a12012-12-06 12:35:10 -05002525static void snd_rme96_remove(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526{
2527 snd_card_free(pci_get_drvdata(pci));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528}
2529
Takashi Iwaie9f66d92012-04-24 12:25:00 +02002530static struct pci_driver rme96_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02002531 .name = KBUILD_MODNAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 .id_table = snd_rme96_ids,
2533 .probe = snd_rme96_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05002534 .remove = snd_rme96_remove,
Takashi Iwaia932be92014-02-25 15:01:15 +01002535 .driver = {
2536 .pm = RME96_PM_OPS,
2537 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538};
2539
Takashi Iwaie9f66d92012-04-24 12:25:00 +02002540module_pci_driver(rme96_driver);