blob: c166e13d17ec8b5279b73bca8c48a06812e17b0b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for C-Media's CMI8330 soundcards.
3 * Copyright (c) by George Talusan <gstalusan@uwaterloo.ca>
4 * http://www.undergrad.math.uwaterloo.ca/~gstalusa
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22/*
23 * NOTES
24 *
25 * The extended registers contain mixer settings which are largely
26 * untapped for the time being.
27 *
28 * MPU401 and SPDIF are not supported yet. I don't have the hardware
29 * to aid in coding and testing, so I won't bother.
30 *
31 * To quickly load the module,
32 *
33 * modprobe -a snd-cmi8330 sbport=0x220 sbirq=5 sbdma8=1
34 * sbdma16=5 wssport=0x530 wssirq=11 wssdma=0
35 *
36 * This card has two mixers and two PCM devices. I've cheesed it such
37 * that recording and playback can be done through the same device.
38 * The driver "magically" routes the capturing to the AD1848 codec,
39 * and playback to the SB16 codec. This allows for full-duplex mode
40 * to some extent.
41 * The utilities in alsa-utils are aware of both devices, so passing
42 * the appropriate parameters to amixer and alsactl will give you
43 * full control over both mixers.
44 */
45
46#include <sound/driver.h>
47#include <linux/init.h>
Takashi Iwaiacdcbc12005-11-17 17:11:35 +010048#include <linux/err.h>
Takashi Iwai5e24c1c2007-02-22 12:50:54 +010049#include <linux/isa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/slab.h>
51#include <linux/pnp.h>
52#include <linux/moduleparam.h>
53#include <sound/core.h>
54#include <sound/ad1848.h>
55#include <sound/sb.h>
56#include <sound/initval.h>
57
58/*
59 */
60/* #define ENABLE_SB_MIXER */
61#define PLAYBACK_ON_SB
62
63/*
64 */
65MODULE_AUTHOR("George Talusan <gstalusan@uwaterloo.ca>");
66MODULE_DESCRIPTION("C-Media CMI8330");
67MODULE_LICENSE("GPL");
68MODULE_SUPPORTED_DEVICE("{{C-Media,CMI8330,isapnp:{CMI0001,@@@0001,@X@0001}}}");
69
70static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
71static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
72static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP;
73#ifdef CONFIG_PNP
74static int isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
75#endif
76static long sbport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
77static int sbirq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
78static int sbdma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;
79static int sbdma16[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;
80static long wssport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
81static int wssirq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
82static int wssdma[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;
83
84module_param_array(index, int, NULL, 0444);
85MODULE_PARM_DESC(index, "Index value for CMI8330 soundcard.");
86module_param_array(id, charp, NULL, 0444);
87MODULE_PARM_DESC(id, "ID string for CMI8330 soundcard.");
88module_param_array(enable, bool, NULL, 0444);
89MODULE_PARM_DESC(enable, "Enable CMI8330 soundcard.");
90#ifdef CONFIG_PNP
91module_param_array(isapnp, bool, NULL, 0444);
92MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
93#endif
94
95module_param_array(sbport, long, NULL, 0444);
96MODULE_PARM_DESC(sbport, "Port # for CMI8330 SB driver.");
97module_param_array(sbirq, int, NULL, 0444);
98MODULE_PARM_DESC(sbirq, "IRQ # for CMI8330 SB driver.");
99module_param_array(sbdma8, int, NULL, 0444);
100MODULE_PARM_DESC(sbdma8, "DMA8 for CMI8330 SB driver.");
101module_param_array(sbdma16, int, NULL, 0444);
102MODULE_PARM_DESC(sbdma16, "DMA16 for CMI8330 SB driver.");
103
104module_param_array(wssport, long, NULL, 0444);
105MODULE_PARM_DESC(wssport, "Port # for CMI8330 WSS driver.");
106module_param_array(wssirq, int, NULL, 0444);
107MODULE_PARM_DESC(wssirq, "IRQ # for CMI8330 WSS driver.");
108module_param_array(wssdma, int, NULL, 0444);
109MODULE_PARM_DESC(wssdma, "DMA for CMI8330 WSS driver.");
110
Takashi Iwai59b1b342006-01-04 15:06:44 +0100111#ifdef CONFIG_PNP
Rene Herman609d7692007-05-15 11:42:56 +0200112static int isa_registered;
Clemens Ladischf7a92752005-12-07 09:13:42 +0100113static int pnp_registered;
Takashi Iwai59b1b342006-01-04 15:06:44 +0100114#endif
Clemens Ladischf7a92752005-12-07 09:13:42 +0100115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#define CMI8330_RMUX3D 16
117#define CMI8330_MUTEMUX 17
118#define CMI8330_OUTPUTVOL 18
119#define CMI8330_MASTVOL 19
120#define CMI8330_LINVOL 20
121#define CMI8330_CDINVOL 21
122#define CMI8330_WAVVOL 22
123#define CMI8330_RECMUX 23
124#define CMI8330_WAVGAIN 24
125#define CMI8330_LINGAIN 25
126#define CMI8330_CDINGAIN 26
127
128static unsigned char snd_cmi8330_image[((CMI8330_CDINGAIN)-16) + 1] =
129{
130 0x40, /* 16 - recording mux (SB-mixer-enabled) */
131#ifdef ENABLE_SB_MIXER
132 0x40, /* 17 - mute mux (Mode2) */
133#else
134 0x0, /* 17 - mute mux */
135#endif
136 0x0, /* 18 - vol */
137 0x0, /* 19 - master volume */
138 0x0, /* 20 - line-in volume */
139 0x0, /* 21 - cd-in volume */
140 0x0, /* 22 - wave volume */
141 0x0, /* 23 - mute/rec mux */
142 0x0, /* 24 - wave rec gain */
143 0x0, /* 25 - line-in rec gain */
144 0x0 /* 26 - cd-in rec gain */
145};
146
Takashi Iwaie3561702005-11-17 14:41:02 +0100147typedef int (*snd_pcm_open_callback_t)(struct snd_pcm_substream *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149struct snd_cmi8330 {
150#ifdef CONFIG_PNP
151 struct pnp_dev *cap;
152 struct pnp_dev *play;
153#endif
Takashi Iwaie3561702005-11-17 14:41:02 +0100154 struct snd_card *card;
155 struct snd_ad1848 *wss;
156 struct snd_sb *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Takashi Iwaie3561702005-11-17 14:41:02 +0100158 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 struct snd_cmi8330_stream {
Takashi Iwaie3561702005-11-17 14:41:02 +0100160 struct snd_pcm_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 snd_pcm_open_callback_t open;
162 void *private_data; /* sb or wss */
163 } streams[2];
164};
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#ifdef CONFIG_PNP
167
168static struct pnp_card_device_id snd_cmi8330_pnpids[] = {
169 { .id = "CMI0001", .devs = { { "@@@0001" }, { "@X@0001" } } },
170 { .id = "" }
171};
172
173MODULE_DEVICE_TABLE(pnp_card, snd_cmi8330_pnpids);
174
175#endif
176
177
Bjorn Helgaas07d58ad2006-03-27 01:17:11 -0800178static struct ad1848_mix_elem snd_cmi8330_controls[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179AD1848_DOUBLE("Master Playback Volume", 0, CMI8330_MASTVOL, CMI8330_MASTVOL, 4, 0, 15, 0),
180AD1848_SINGLE("Loud Playback Switch", 0, CMI8330_MUTEMUX, 6, 1, 1),
181AD1848_DOUBLE("PCM Playback Switch", 0, AD1848_LEFT_OUTPUT, AD1848_RIGHT_OUTPUT, 7, 7, 1, 1),
182AD1848_DOUBLE("PCM Playback Volume", 0, AD1848_LEFT_OUTPUT, AD1848_RIGHT_OUTPUT, 0, 0, 63, 1),
183AD1848_DOUBLE("Line Playback Switch", 0, CMI8330_MUTEMUX, CMI8330_MUTEMUX, 4, 3, 1, 0),
184AD1848_DOUBLE("Line Playback Volume", 0, CMI8330_LINVOL, CMI8330_LINVOL, 4, 0, 15, 0),
185AD1848_DOUBLE("Line Capture Switch", 0, CMI8330_RMUX3D, CMI8330_RMUX3D, 2, 1, 1, 0),
186AD1848_DOUBLE("Line Capture Volume", 0, CMI8330_LINGAIN, CMI8330_LINGAIN, 4, 0, 15, 0),
187AD1848_DOUBLE("CD Playback Switch", 0, CMI8330_MUTEMUX, CMI8330_MUTEMUX, 2, 1, 1, 0),
188AD1848_DOUBLE("CD Capture Switch", 0, CMI8330_RMUX3D, CMI8330_RMUX3D, 4, 3, 1, 0),
189AD1848_DOUBLE("CD Playback Volume", 0, CMI8330_CDINVOL, CMI8330_CDINVOL, 4, 0, 15, 0),
190AD1848_DOUBLE("CD Capture Volume", 0, CMI8330_CDINGAIN, CMI8330_CDINGAIN, 4, 0, 15, 0),
191AD1848_SINGLE("Mic Playback Switch", 0, CMI8330_MUTEMUX, 0, 1, 0),
192AD1848_SINGLE("Mic Playback Volume", 0, CMI8330_OUTPUTVOL, 0, 7, 0),
193AD1848_SINGLE("Mic Capture Switch", 0, CMI8330_RMUX3D, 0, 1, 0),
194AD1848_SINGLE("Mic Capture Volume", 0, CMI8330_OUTPUTVOL, 5, 7, 0),
195AD1848_DOUBLE("Wavetable Playback Switch", 0, CMI8330_RECMUX, CMI8330_RECMUX, 1, 0, 1, 0),
196AD1848_DOUBLE("Wavetable Playback Volume", 0, CMI8330_WAVVOL, CMI8330_WAVVOL, 4, 0, 15, 0),
197AD1848_DOUBLE("Wavetable Capture Switch", 0, CMI8330_RECMUX, CMI8330_RECMUX, 5, 4, 1, 0),
198AD1848_DOUBLE("Wavetable Capture Volume", 0, CMI8330_WAVGAIN, CMI8330_WAVGAIN, 4, 0, 15, 0),
199AD1848_SINGLE("3D Control - Switch", 0, CMI8330_RMUX3D, 5, 1, 1),
200AD1848_SINGLE("PC Speaker Playback Volume", 0, CMI8330_OUTPUTVOL, 3, 3, 0),
201AD1848_SINGLE("FM Playback Switch", 0, CMI8330_RECMUX, 3, 1, 1),
Clemens Ladisch10e8d782005-08-03 13:40:08 +0200202AD1848_SINGLE(SNDRV_CTL_NAME_IEC958("Input ",CAPTURE,SWITCH), 0, CMI8330_RMUX3D, 7, 1, 1),
203AD1848_SINGLE(SNDRV_CTL_NAME_IEC958("Input ",PLAYBACK,SWITCH), 0, CMI8330_MUTEMUX, 7, 1, 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
206#ifdef ENABLE_SB_MIXER
Bjorn Helgaas07d58ad2006-03-27 01:17:11 -0800207static struct sbmix_elem cmi8330_sb_mixers[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208SB_DOUBLE("SB Master Playback Volume", SB_DSP4_MASTER_DEV, (SB_DSP4_MASTER_DEV + 1), 3, 3, 31),
209SB_DOUBLE("Tone Control - Bass", SB_DSP4_BASS_DEV, (SB_DSP4_BASS_DEV + 1), 4, 4, 15),
210SB_DOUBLE("Tone Control - Treble", SB_DSP4_TREBLE_DEV, (SB_DSP4_TREBLE_DEV + 1), 4, 4, 15),
211SB_DOUBLE("SB PCM Playback Volume", SB_DSP4_PCM_DEV, (SB_DSP4_PCM_DEV + 1), 3, 3, 31),
212SB_DOUBLE("SB Synth Playback Volume", SB_DSP4_SYNTH_DEV, (SB_DSP4_SYNTH_DEV + 1), 3, 3, 31),
213SB_DOUBLE("SB CD Playback Switch", SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, 2, 1, 1),
214SB_DOUBLE("SB CD Playback Volume", SB_DSP4_CD_DEV, (SB_DSP4_CD_DEV + 1), 3, 3, 31),
215SB_DOUBLE("SB Line Playback Switch", SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, 4, 3, 1),
216SB_DOUBLE("SB Line Playback Volume", SB_DSP4_LINE_DEV, (SB_DSP4_LINE_DEV + 1), 3, 3, 31),
217SB_SINGLE("SB Mic Playback Switch", SB_DSP4_OUTPUT_SW, 0, 1),
218SB_SINGLE("SB Mic Playback Volume", SB_DSP4_MIC_DEV, 3, 31),
219SB_SINGLE("SB PC Speaker Volume", SB_DSP4_SPEAKER_DEV, 6, 3),
220SB_DOUBLE("SB Capture Volume", SB_DSP4_IGAIN_DEV, (SB_DSP4_IGAIN_DEV + 1), 6, 6, 3),
221SB_DOUBLE("SB Playback Volume", SB_DSP4_OGAIN_DEV, (SB_DSP4_OGAIN_DEV + 1), 6, 6, 3),
222SB_SINGLE("SB Mic Auto Gain", SB_DSP4_MIC_AGC, 0, 1),
223};
224
Bjorn Helgaas07d58ad2006-03-27 01:17:11 -0800225static unsigned char cmi8330_sb_init_values[][2] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 { SB_DSP4_MASTER_DEV + 0, 0 },
227 { SB_DSP4_MASTER_DEV + 1, 0 },
228 { SB_DSP4_PCM_DEV + 0, 0 },
229 { SB_DSP4_PCM_DEV + 1, 0 },
230 { SB_DSP4_SYNTH_DEV + 0, 0 },
231 { SB_DSP4_SYNTH_DEV + 1, 0 },
232 { SB_DSP4_INPUT_LEFT, 0 },
233 { SB_DSP4_INPUT_RIGHT, 0 },
234 { SB_DSP4_OUTPUT_SW, 0 },
235 { SB_DSP4_SPEAKER_DEV, 0 },
236};
237
238
Takashi Iwaie3561702005-11-17 14:41:02 +0100239static int __devinit cmi8330_add_sb_mixers(struct snd_sb *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 int idx, err;
242 unsigned long flags;
243
244 spin_lock_irqsave(&chip->mixer_lock, flags);
245 snd_sbmixer_write(chip, 0x00, 0x00); /* mixer reset */
246 spin_unlock_irqrestore(&chip->mixer_lock, flags);
247
248 /* mute and zero volume channels */
249 for (idx = 0; idx < ARRAY_SIZE(cmi8330_sb_init_values); idx++) {
250 spin_lock_irqsave(&chip->mixer_lock, flags);
251 snd_sbmixer_write(chip, cmi8330_sb_init_values[idx][0],
252 cmi8330_sb_init_values[idx][1]);
253 spin_unlock_irqrestore(&chip->mixer_lock, flags);
254 }
255
256 for (idx = 0; idx < ARRAY_SIZE(cmi8330_sb_mixers); idx++) {
257 if ((err = snd_sbmixer_add_ctl_elem(chip, &cmi8330_sb_mixers[idx])) < 0)
258 return err;
259 }
260 return 0;
261}
262#endif
263
Takashi Iwaie3561702005-11-17 14:41:02 +0100264static int __devinit snd_cmi8330_mixer(struct snd_card *card, struct snd_cmi8330 *acard)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 unsigned int idx;
267 int err;
268
269 strcpy(card->mixername, "CMI8330/C3D");
270
271 for (idx = 0; idx < ARRAY_SIZE(snd_cmi8330_controls); idx++) {
272 if ((err = snd_ad1848_add_ctl_elem(acard->wss, &snd_cmi8330_controls[idx])) < 0)
273 return err;
274 }
275
276#ifdef ENABLE_SB_MIXER
277 if ((err = cmi8330_add_sb_mixers(acard->sb)) < 0)
278 return err;
279#endif
280 return 0;
281}
282
283#ifdef CONFIG_PNP
284static int __devinit snd_cmi8330_pnp(int dev, struct snd_cmi8330 *acard,
285 struct pnp_card_link *card,
286 const struct pnp_card_device_id *id)
287{
288 struct pnp_dev *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 int err;
290
291 acard->cap = pnp_request_card_device(card, id->devs[0].id, NULL);
Rene Herman109c53f842007-11-30 17:59:25 +0100292 if (acard->cap == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return -EBUSY;
Rene Herman109c53f842007-11-30 17:59:25 +0100294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 acard->play = pnp_request_card_device(card, id->devs[1].id, NULL);
Rene Herman109c53f842007-11-30 17:59:25 +0100296 if (acard->play == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 pdev = acard->cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 err = pnp_activate_dev(pdev);
302 if (err < 0) {
303 snd_printk(KERN_ERR "CMI8330/C3D (AD1848) PnP configure failure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return -EBUSY;
305 }
306 wssport[dev] = pnp_port_start(pdev, 0);
307 wssdma[dev] = pnp_dma(pdev, 0);
308 wssirq[dev] = pnp_irq(pdev, 0);
309
310 /* allocate SB16 resources */
311 pdev = acard->play;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 err = pnp_activate_dev(pdev);
314 if (err < 0) {
315 snd_printk(KERN_ERR "CMI8330/C3D (SB16) PnP configure failure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return -EBUSY;
317 }
318 sbport[dev] = pnp_port_start(pdev, 0);
319 sbdma8[dev] = pnp_dma(pdev, 0);
320 sbdma16[dev] = pnp_dma(pdev, 1);
321 sbirq[dev] = pnp_irq(pdev, 0);
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return 0;
324}
325#endif
326
327/*
328 * PCM interface
329 *
330 * since we call the different chip interfaces for playback and capture
331 * directions, we need a trick.
332 *
333 * - copy the ops for each direction into a local record.
334 * - replace the open callback with the new one, which replaces the
335 * substream->private_data with the corresponding chip instance
336 * and calls again the original open callback of the chip.
337 *
338 */
339
340#ifdef PLAYBACK_ON_SB
341#define CMI_SB_STREAM SNDRV_PCM_STREAM_PLAYBACK
342#define CMI_AD_STREAM SNDRV_PCM_STREAM_CAPTURE
343#else
344#define CMI_SB_STREAM SNDRV_PCM_STREAM_CAPTURE
345#define CMI_AD_STREAM SNDRV_PCM_STREAM_PLAYBACK
346#endif
347
Takashi Iwaie3561702005-11-17 14:41:02 +0100348static int snd_cmi8330_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 struct snd_cmi8330 *chip = snd_pcm_substream_chip(substream);
351
352 /* replace the private_data and call the original open callback */
353 substream->private_data = chip->streams[SNDRV_PCM_STREAM_PLAYBACK].private_data;
354 return chip->streams[SNDRV_PCM_STREAM_PLAYBACK].open(substream);
355}
356
Takashi Iwaie3561702005-11-17 14:41:02 +0100357static int snd_cmi8330_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 struct snd_cmi8330 *chip = snd_pcm_substream_chip(substream);
360
361 /* replace the private_data and call the original open callback */
362 substream->private_data = chip->streams[SNDRV_PCM_STREAM_CAPTURE].private_data;
363 return chip->streams[SNDRV_PCM_STREAM_CAPTURE].open(substream);
364}
365
Takashi Iwaie3561702005-11-17 14:41:02 +0100366static int __devinit snd_cmi8330_pcm(struct snd_card *card, struct snd_cmi8330 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Takashi Iwaie3561702005-11-17 14:41:02 +0100368 struct snd_pcm *pcm;
369 const struct snd_pcm_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 int err;
371 static snd_pcm_open_callback_t cmi_open_callbacks[2] = {
372 snd_cmi8330_playback_open,
373 snd_cmi8330_capture_open
374 };
375
376 if ((err = snd_pcm_new(card, "CMI8330", 0, 1, 1, &pcm)) < 0)
377 return err;
378 strcpy(pcm->name, "CMI8330");
379 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 /* SB16 */
382 ops = snd_sb16dsp_get_pcm_ops(CMI_SB_STREAM);
383 chip->streams[CMI_SB_STREAM].ops = *ops;
384 chip->streams[CMI_SB_STREAM].open = ops->open;
385 chip->streams[CMI_SB_STREAM].ops.open = cmi_open_callbacks[CMI_SB_STREAM];
386 chip->streams[CMI_SB_STREAM].private_data = chip->sb;
387
388 /* AD1848 */
389 ops = snd_ad1848_get_pcm_ops(CMI_AD_STREAM);
390 chip->streams[CMI_AD_STREAM].ops = *ops;
391 chip->streams[CMI_AD_STREAM].open = ops->open;
392 chip->streams[CMI_AD_STREAM].ops.open = cmi_open_callbacks[CMI_AD_STREAM];
393 chip->streams[CMI_AD_STREAM].private_data = chip->wss;
394
395 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &chip->streams[SNDRV_PCM_STREAM_PLAYBACK].ops);
396 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &chip->streams[SNDRV_PCM_STREAM_CAPTURE].ops);
397
398 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
399 snd_dma_isa_data(),
400 64*1024, 128*1024);
401 chip->pcm = pcm;
402
403 return 0;
404}
405
406
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100407#ifdef CONFIG_PM
408static int snd_cmi8330_suspend(struct snd_card *card)
409{
410 struct snd_cmi8330 *acard = card->private_data;
411
412 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
413 snd_pcm_suspend_all(acard->pcm);
414 acard->wss->suspend(acard->wss);
415 snd_sbmixer_suspend(acard->sb);
416 return 0;
417}
418
419static int snd_cmi8330_resume(struct snd_card *card)
420{
421 struct snd_cmi8330 *acard = card->private_data;
422
423 snd_sbdsp_reset(acard->sb);
424 snd_sbmixer_suspend(acard->sb);
425 acard->wss->resume(acard->wss);
426 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
427 return 0;
428}
429#endif
430
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432/*
433 */
434
Takashi Iwai43bcd972005-09-05 17:19:20 +0200435#ifdef CONFIG_PNP
436#define is_isapnp_selected(dev) isapnp[dev]
437#else
438#define is_isapnp_selected(dev) 0
439#endif
440
441#define PFX "cmi8330: "
442
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100443static struct snd_card *snd_cmi8330_card_new(int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Takashi Iwaie3561702005-11-17 14:41:02 +0100445 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 struct snd_cmi8330 *acard;
Takashi Iwai43bcd972005-09-05 17:19:20 +0200447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 card = snd_card_new(index[dev], id[dev], THIS_MODULE,
449 sizeof(struct snd_cmi8330));
450 if (card == NULL) {
Takashi Iwai43bcd972005-09-05 17:19:20 +0200451 snd_printk(KERN_ERR PFX "could not get a new card\n");
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100452 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100454 acard = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 acard->card = card;
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100456 return card;
457}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100459static int __devinit snd_cmi8330_probe(struct snd_card *card, int dev)
460{
461 struct snd_cmi8330 *acard;
462 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100464 acard = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if ((err = snd_ad1848_create(card,
466 wssport[dev] + 4,
467 wssirq[dev],
468 wssdma[dev],
469 AD1848_HW_DETECT,
470 &acard->wss)) < 0) {
Takashi Iwai43bcd972005-09-05 17:19:20 +0200471 snd_printk(KERN_ERR PFX "(AD1848) device busy??\n");
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100472 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474 if (acard->wss->hardware != AD1848_HW_CMI8330) {
Takashi Iwai43bcd972005-09-05 17:19:20 +0200475 snd_printk(KERN_ERR PFX "(AD1848) not found during probe\n");
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100476 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478
479 if ((err = snd_sbdsp_create(card, sbport[dev],
480 sbirq[dev],
481 snd_sb16dsp_interrupt,
482 sbdma8[dev],
483 sbdma16[dev],
484 SB_HW_AUTO, &acard->sb)) < 0) {
Takashi Iwai43bcd972005-09-05 17:19:20 +0200485 snd_printk(KERN_ERR PFX "(SB16) device busy??\n");
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100486 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488 if (acard->sb->hardware != SB_HW_16) {
Takashi Iwai43bcd972005-09-05 17:19:20 +0200489 snd_printk(KERN_ERR PFX "(SB16) not found during probe\n");
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100490 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 snd_ad1848_out(acard->wss, AD1848_MISC_INFO, 0x40); /* switch on MODE2 */
494 for (i = CMI8330_RMUX3D; i <= CMI8330_CDINGAIN; i++)
495 snd_ad1848_out(acard->wss, i, snd_cmi8330_image[i - CMI8330_RMUX3D]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 if ((err = snd_cmi8330_mixer(card, acard)) < 0) {
Takashi Iwai43bcd972005-09-05 17:19:20 +0200498 snd_printk(KERN_ERR PFX "failed to create mixers\n");
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100499 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501
502 if ((err = snd_cmi8330_pcm(card, acard)) < 0) {
Takashi Iwai43bcd972005-09-05 17:19:20 +0200503 snd_printk(KERN_ERR PFX "failed to create pcms\n");
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100504 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
506
507 strcpy(card->driver, "CMI8330/C3D");
508 strcpy(card->shortname, "C-Media CMI8330/C3D");
509 sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
510 card->shortname,
511 acard->wss->port,
512 wssirq[dev],
513 wssdma[dev]);
514
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100515 return snd_card_register(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100518static int __devinit snd_cmi8330_isa_match(struct device *pdev,
519 unsigned int dev)
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100520{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100521 if (!enable[dev] || is_isapnp_selected(dev))
522 return 0;
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100523 if (wssport[dev] == SNDRV_AUTO_PORT) {
524 snd_printk(KERN_ERR PFX "specify wssport\n");
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100525 return 0;
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100526 }
527 if (sbport[dev] == SNDRV_AUTO_PORT) {
528 snd_printk(KERN_ERR PFX "specify sbport\n");
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100529 return 0;
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100530 }
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100531 return 1;
532}
533
534static int __devinit snd_cmi8330_isa_probe(struct device *pdev,
535 unsigned int dev)
536{
537 struct snd_card *card;
538 int err;
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100539
540 card = snd_cmi8330_card_new(dev);
541 if (! card)
542 return -ENOMEM;
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100543 snd_card_set_dev(card, pdev);
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100544 if ((err = snd_cmi8330_probe(card, dev)) < 0) {
545 snd_card_free(card);
546 return err;
547 }
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100548 dev_set_drvdata(pdev, card);
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100549 return 0;
550}
551
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100552static int __devexit snd_cmi8330_isa_remove(struct device *devptr,
553 unsigned int dev)
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100554{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100555 snd_card_free(dev_get_drvdata(devptr));
556 dev_set_drvdata(devptr, NULL);
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100557 return 0;
558}
559
560#ifdef CONFIG_PM
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100561static int snd_cmi8330_isa_suspend(struct device *dev, unsigned int n,
562 pm_message_t state)
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100563{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100564 return snd_cmi8330_suspend(dev_get_drvdata(dev));
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100565}
566
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100567static int snd_cmi8330_isa_resume(struct device *dev, unsigned int n)
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100568{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100569 return snd_cmi8330_resume(dev_get_drvdata(dev));
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100570}
571#endif
572
Rene Herman83c51c02007-03-20 11:33:46 +0100573#define DEV_NAME "cmi8330"
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100574
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100575static struct isa_driver snd_cmi8330_driver = {
576 .match = snd_cmi8330_isa_match,
577 .probe = snd_cmi8330_isa_probe,
578 .remove = __devexit_p(snd_cmi8330_isa_remove),
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100579#ifdef CONFIG_PM
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100580 .suspend = snd_cmi8330_isa_suspend,
581 .resume = snd_cmi8330_isa_resume,
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100582#endif
583 .driver = {
Rene Herman83c51c02007-03-20 11:33:46 +0100584 .name = DEV_NAME
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100585 },
586};
587
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589#ifdef CONFIG_PNP
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100590static int __devinit snd_cmi8330_pnp_detect(struct pnp_card_link *pcard,
591 const struct pnp_card_device_id *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 static int dev;
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100594 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 int res;
596
597 for ( ; dev < SNDRV_CARDS; dev++) {
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100598 if (enable[dev] && isapnp[dev])
599 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100601 if (dev >= SNDRV_CARDS)
602 return -ENODEV;
603
604 card = snd_cmi8330_card_new(dev);
605 if (! card)
606 return -ENOMEM;
607 if ((res = snd_cmi8330_pnp(dev, card->private_data, pcard, pid)) < 0) {
608 snd_printk(KERN_ERR PFX "PnP detection failed\n");
609 snd_card_free(card);
610 return res;
611 }
612 snd_card_set_dev(card, &pcard->card->dev);
613 if ((res = snd_cmi8330_probe(card, dev)) < 0) {
614 snd_card_free(card);
615 return res;
616 }
617 pnp_set_card_drvdata(pcard, card);
618 dev++;
619 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
622static void __devexit snd_cmi8330_pnp_remove(struct pnp_card_link * pcard)
623{
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100624 snd_card_free(pnp_get_card_drvdata(pcard));
625 pnp_set_card_drvdata(pcard, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100628#ifdef CONFIG_PM
629static int snd_cmi8330_pnp_suspend(struct pnp_card_link *pcard, pm_message_t state)
630{
631 return snd_cmi8330_suspend(pnp_get_card_drvdata(pcard));
632}
633
634static int snd_cmi8330_pnp_resume(struct pnp_card_link *pcard)
635{
636 return snd_cmi8330_resume(pnp_get_card_drvdata(pcard));
637}
638#endif
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640static struct pnp_card_driver cmi8330_pnpc_driver = {
641 .flags = PNP_DRIVER_RES_DISABLE,
642 .name = "cmi8330",
643 .id_table = snd_cmi8330_pnpids,
644 .probe = snd_cmi8330_pnp_detect,
645 .remove = __devexit_p(snd_cmi8330_pnp_remove),
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100646#ifdef CONFIG_PM
647 .suspend = snd_cmi8330_pnp_suspend,
648 .resume = snd_cmi8330_pnp_resume,
649#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650};
651#endif /* CONFIG_PNP */
652
653static int __init alsa_card_cmi8330_init(void)
654{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100655 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100657 err = isa_register_driver(&snd_cmi8330_driver, SNDRV_CARDS);
Takashi Iwai59b1b342006-01-04 15:06:44 +0100658#ifdef CONFIG_PNP
Rene Herman609d7692007-05-15 11:42:56 +0200659 if (!err)
660 isa_registered = 1;
661
Takashi Iwaiacdcbc12005-11-17 17:11:35 +0100662 err = pnp_register_card_driver(&cmi8330_pnpc_driver);
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100663 if (!err)
Clemens Ladischf7a92752005-12-07 09:13:42 +0100664 pnp_registered = 1;
Rene Herman609d7692007-05-15 11:42:56 +0200665
666 if (isa_registered)
667 err = 0;
Takashi Iwai59b1b342006-01-04 15:06:44 +0100668#endif
Rene Herman609d7692007-05-15 11:42:56 +0200669 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
672static void __exit alsa_card_cmi8330_exit(void)
673{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100674#ifdef CONFIG_PNP
675 if (pnp_registered)
676 pnp_unregister_card_driver(&cmi8330_pnpc_driver);
Rene Herman609d7692007-05-15 11:42:56 +0200677
678 if (isa_registered)
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100679#endif
Rene Herman609d7692007-05-15 11:42:56 +0200680 isa_unregister_driver(&snd_cmi8330_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
683module_init(alsa_card_cmi8330_init)
684module_exit(alsa_card_cmi8330_exit)