blob: 06394e4409b33d4e8922ddde0fac0d163e651cdd [file] [log] [blame]
Clemens Ladischd0ce9942007-12-23 19:50:57 +01001/*
2 * C-Media CMI8788 driver - main driver module
3 *
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License, version 2.
9 *
10 * This driver is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this driver; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
Clemens Ladischd0ce9942007-12-23 19:50:57 +010020#include <linux/delay.h>
21#include <linux/interrupt.h>
22#include <linux/mutex.h>
23#include <linux/pci.h>
24#include <sound/ac97_codec.h>
25#include <sound/asoundef.h>
26#include <sound/core.h>
27#include <sound/info.h>
28#include <sound/mpu401.h>
29#include <sound/pcm.h>
30#include "oxygen.h"
Clemens Ladisch878ac3e2008-01-21 08:50:19 +010031#include "cm9780.h"
Clemens Ladischd0ce9942007-12-23 19:50:57 +010032
33MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
34MODULE_DESCRIPTION("C-Media CMI8788 helper library");
35MODULE_LICENSE("GPL");
36
37
38static irqreturn_t oxygen_interrupt(int dummy, void *dev_id)
39{
40 struct oxygen *chip = dev_id;
41 unsigned int status, clear, elapsed_streams, i;
42
43 status = oxygen_read16(chip, OXYGEN_INTERRUPT_STATUS);
44 if (!status)
45 return IRQ_NONE;
46
47 spin_lock(&chip->reg_lock);
48
49 clear = status & (OXYGEN_CHANNEL_A |
50 OXYGEN_CHANNEL_B |
51 OXYGEN_CHANNEL_C |
52 OXYGEN_CHANNEL_SPDIF |
53 OXYGEN_CHANNEL_MULTICH |
54 OXYGEN_CHANNEL_AC97 |
Clemens Ladischc2353a02008-01-18 09:17:53 +010055 OXYGEN_INT_SPDIF_IN_DETECT |
Clemens Ladischd0ce9942007-12-23 19:50:57 +010056 OXYGEN_INT_GPIO);
57 if (clear) {
Clemens Ladischc2353a02008-01-18 09:17:53 +010058 if (clear & OXYGEN_INT_SPDIF_IN_DETECT)
59 chip->interrupt_mask &= ~OXYGEN_INT_SPDIF_IN_DETECT;
Clemens Ladischd0ce9942007-12-23 19:50:57 +010060 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK,
61 chip->interrupt_mask & ~clear);
62 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK,
63 chip->interrupt_mask);
64 }
65
66 elapsed_streams = status & chip->pcm_running;
67
68 spin_unlock(&chip->reg_lock);
69
70 for (i = 0; i < PCM_COUNT; ++i)
71 if ((elapsed_streams & (1 << i)) && chip->streams[i])
72 snd_pcm_period_elapsed(chip->streams[i]);
73
Clemens Ladischc2353a02008-01-18 09:17:53 +010074 if (status & OXYGEN_INT_SPDIF_IN_DETECT) {
Clemens Ladischd0ce9942007-12-23 19:50:57 +010075 spin_lock(&chip->reg_lock);
76 i = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL);
Clemens Ladisch7f0b8942008-01-21 08:53:30 +010077 if (i & (OXYGEN_SPDIF_SENSE_INT | OXYGEN_SPDIF_LOCK_INT |
78 OXYGEN_SPDIF_RATE_INT)) {
79 /* write the interrupt bit(s) to clear */
Clemens Ladischd0ce9942007-12-23 19:50:57 +010080 oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, i);
81 schedule_work(&chip->spdif_input_bits_work);
82 }
83 spin_unlock(&chip->reg_lock);
84 }
85
86 if (status & OXYGEN_INT_GPIO)
87 ;
88
89 if ((status & OXYGEN_INT_MIDI) && chip->midi)
90 snd_mpu401_uart_interrupt(0, chip->midi->private_data);
91
92 return IRQ_HANDLED;
93}
94
95static void oxygen_spdif_input_bits_changed(struct work_struct *work)
96{
97 struct oxygen *chip = container_of(work, struct oxygen,
98 spdif_input_bits_work);
Clemens Ladisch7f0b8942008-01-21 08:53:30 +010099 u32 reg;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100100
Clemens Ladisch7f0b8942008-01-21 08:53:30 +0100101 /*
102 * This function gets called when there is new activity on the SPDIF
103 * input, or when we lose lock on the input signal, or when the rate
104 * changes.
105 */
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100106 msleep(1);
Clemens Ladisch7f0b8942008-01-21 08:53:30 +0100107 spin_lock_irq(&chip->reg_lock);
108 reg = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL);
109 if ((reg & (OXYGEN_SPDIF_SENSE_STATUS |
110 OXYGEN_SPDIF_LOCK_STATUS))
111 == OXYGEN_SPDIF_SENSE_STATUS) {
112 /*
113 * If we detect activity on the SPDIF input but cannot lock to
114 * a signal, the clock bit is likely to be wrong.
115 */
116 reg ^= OXYGEN_SPDIF_IN_CLOCK_MASK;
117 oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, reg);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100118 spin_unlock_irq(&chip->reg_lock);
119 msleep(1);
Clemens Ladisch7f0b8942008-01-21 08:53:30 +0100120 spin_lock_irq(&chip->reg_lock);
121 reg = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL);
122 if ((reg & (OXYGEN_SPDIF_SENSE_STATUS |
123 OXYGEN_SPDIF_LOCK_STATUS))
124 == OXYGEN_SPDIF_SENSE_STATUS) {
125 /* nothing detected with either clock; give up */
126 if ((reg & OXYGEN_SPDIF_IN_CLOCK_MASK)
127 == OXYGEN_SPDIF_IN_CLOCK_192) {
128 /*
129 * Reset clock to <= 96 kHz because this is
130 * more likely to be received next time.
131 */
132 reg &= ~OXYGEN_SPDIF_IN_CLOCK_MASK;
133 reg |= OXYGEN_SPDIF_IN_CLOCK_96;
134 oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, reg);
135 }
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100136 }
137 }
Clemens Ladisch7f0b8942008-01-21 08:53:30 +0100138 spin_unlock_irq(&chip->reg_lock);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100139
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100140 if (chip->controls[CONTROL_SPDIF_INPUT_BITS]) {
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100141 spin_lock_irq(&chip->reg_lock);
Clemens Ladischc2353a02008-01-18 09:17:53 +0100142 chip->interrupt_mask |= OXYGEN_INT_SPDIF_IN_DETECT;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100143 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK,
144 chip->interrupt_mask);
145 spin_unlock_irq(&chip->reg_lock);
146
Clemens Ladisch7f0b8942008-01-21 08:53:30 +0100147 /*
148 * We don't actually know that any channel status bits have
149 * changed, but let's send a notification just to be sure.
150 */
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100151 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100152 &chip->controls[CONTROL_SPDIF_INPUT_BITS]->id);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100153 }
154}
155
156#ifdef CONFIG_PROC_FS
157static void oxygen_proc_read(struct snd_info_entry *entry,
158 struct snd_info_buffer *buffer)
159{
160 struct oxygen *chip = entry->private_data;
161 int i, j;
162
163 snd_iprintf(buffer, "CMI8788\n\n");
164 for (i = 0; i < 0x100; i += 0x10) {
165 snd_iprintf(buffer, "%02x:", i);
166 for (j = 0; j < 0x10; ++j)
167 snd_iprintf(buffer, " %02x", oxygen_read8(chip, i + j));
168 snd_iprintf(buffer, "\n");
169 }
170 if (mutex_lock_interruptible(&chip->mutex) < 0)
171 return;
Clemens Ladisch31c77642008-01-16 08:28:17 +0100172 if (chip->has_ac97_0) {
173 snd_iprintf(buffer, "\nAC97\n");
174 for (i = 0; i < 0x80; i += 0x10) {
175 snd_iprintf(buffer, "%02x:", i);
176 for (j = 0; j < 0x10; j += 2)
177 snd_iprintf(buffer, " %04x",
178 oxygen_read_ac97(chip, 0, i + j));
179 snd_iprintf(buffer, "\n");
180 }
181 }
182 if (chip->has_ac97_1) {
183 snd_iprintf(buffer, "\nAC97 2\n");
184 for (i = 0; i < 0x80; i += 0x10) {
185 snd_iprintf(buffer, "%02x:", i);
186 for (j = 0; j < 0x10; j += 2)
187 snd_iprintf(buffer, " %04x",
188 oxygen_read_ac97(chip, 1, i + j));
189 snd_iprintf(buffer, "\n");
190 }
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100191 }
192 mutex_unlock(&chip->mutex);
193}
194
195static void __devinit oxygen_proc_init(struct oxygen *chip)
196{
197 struct snd_info_entry *entry;
198
199 if (!snd_card_proc_new(chip->card, "cmi8788", &entry))
200 snd_info_set_text_ops(entry, chip, oxygen_proc_read);
201}
202#else
203#define oxygen_proc_init(chip)
204#endif
205
206static void __devinit oxygen_init(struct oxygen *chip)
207{
208 unsigned int i;
209
210 chip->dac_routing = 1;
211 for (i = 0; i < 8; ++i)
212 chip->dac_volume[i] = 0xff;
213 chip->spdif_playback_enable = 1;
214 chip->spdif_bits = OXYGEN_SPDIF_C | OXYGEN_SPDIF_ORIGINAL |
215 (IEC958_AES1_CON_PCM_CODER << OXYGEN_SPDIF_CATEGORY_SHIFT);
216 chip->spdif_pcm_bits = chip->spdif_bits;
217
218 if (oxygen_read8(chip, OXYGEN_REVISION) & OXYGEN_REVISION_2)
219 chip->revision = 2;
220 else
221 chip->revision = 1;
222
223 if (chip->revision == 1)
Clemens Ladischc2353a02008-01-18 09:17:53 +0100224 oxygen_set_bits8(chip, OXYGEN_MISC,
225 OXYGEN_MISC_PCI_MEM_W_1_CLOCK);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100226
Clemens Ladisch31c77642008-01-16 08:28:17 +0100227 i = oxygen_read16(chip, OXYGEN_AC97_CONTROL);
228 chip->has_ac97_0 = (i & OXYGEN_AC97_CODEC_0) != 0;
229 chip->has_ac97_1 = (i & OXYGEN_AC97_CODEC_1) != 0;
230
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100231 oxygen_set_bits8(chip, OXYGEN_FUNCTION,
232 OXYGEN_FUNCTION_RESET_CODEC |
Clemens Ladisch84aa6b72008-01-16 08:28:54 +0100233 chip->model->function_flags);
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100234 oxygen_write8_masked(chip, OXYGEN_FUNCTION,
235 OXYGEN_FUNCTION_SPI,
236 OXYGEN_FUNCTION_2WIRE_SPI_MASK);
237 oxygen_write8(chip, OXYGEN_DMA_STATUS, 0);
238 oxygen_write8(chip, OXYGEN_DMA_PAUSE, 0);
239 oxygen_write8(chip, OXYGEN_PLAY_CHANNELS,
240 OXYGEN_PLAY_CHANNELS_2 |
241 OXYGEN_DMA_A_BURST_8 |
242 OXYGEN_DMA_MULTICH_BURST_8);
243 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, 0);
244 oxygen_write8_masked(chip, OXYGEN_MISC, 0,
245 OXYGEN_MISC_WRITE_PCI_SUBID |
246 OXYGEN_MISC_REC_C_FROM_SPDIF |
247 OXYGEN_MISC_REC_B_FROM_AC97 |
248 OXYGEN_MISC_REC_A_FROM_MULTICH);
249 oxygen_write8(chip, OXYGEN_REC_FORMAT,
250 (OXYGEN_FORMAT_16 << OXYGEN_REC_FORMAT_A_SHIFT) |
251 (OXYGEN_FORMAT_16 << OXYGEN_REC_FORMAT_B_SHIFT) |
252 (OXYGEN_FORMAT_16 << OXYGEN_REC_FORMAT_C_SHIFT));
253 oxygen_write8(chip, OXYGEN_PLAY_FORMAT,
254 (OXYGEN_FORMAT_16 << OXYGEN_SPDIF_FORMAT_SHIFT) |
255 (OXYGEN_FORMAT_16 << OXYGEN_MULTICH_FORMAT_SHIFT));
256 oxygen_write8(chip, OXYGEN_REC_CHANNELS, OXYGEN_REC_CHANNELS_2_2_2);
Clemens Ladischc9946b22008-01-21 08:44:24 +0100257 oxygen_write16(chip, OXYGEN_I2S_MULTICH_FORMAT,
258 OXYGEN_RATE_48000 | OXYGEN_I2S_FORMAT_LJUST |
259 OXYGEN_I2S_MCLK_128 | OXYGEN_I2S_BITS_16 |
260 OXYGEN_I2S_MASTER | OXYGEN_I2S_BCLK_64);
261 oxygen_write16(chip, OXYGEN_I2S_A_FORMAT,
262 OXYGEN_RATE_48000 | OXYGEN_I2S_FORMAT_LJUST |
263 OXYGEN_I2S_MCLK_128 | OXYGEN_I2S_BITS_16 |
264 OXYGEN_I2S_MASTER | OXYGEN_I2S_BCLK_64);
265 oxygen_write16(chip, OXYGEN_I2S_B_FORMAT,
266 OXYGEN_RATE_48000 | OXYGEN_I2S_FORMAT_LJUST |
267 OXYGEN_I2S_MCLK_128 | OXYGEN_I2S_BITS_16 |
268 OXYGEN_I2S_MASTER | OXYGEN_I2S_BCLK_64);
269 oxygen_write16(chip, OXYGEN_I2S_C_FORMAT,
270 OXYGEN_RATE_48000 | OXYGEN_I2S_FORMAT_LJUST |
271 OXYGEN_I2S_MCLK_128 | OXYGEN_I2S_BITS_16 |
272 OXYGEN_I2S_MASTER | OXYGEN_I2S_BCLK_64);
Clemens Ladisch7f0b8942008-01-21 08:53:30 +0100273 oxygen_write32_masked(chip, OXYGEN_SPDIF_CONTROL,
274 OXYGEN_SPDIF_SENSE_MASK |
275 OXYGEN_SPDIF_LOCK_MASK |
276 OXYGEN_SPDIF_RATE_MASK |
277 OXYGEN_SPDIF_LOCK_PAR |
278 OXYGEN_SPDIF_IN_CLOCK_96,
279 OXYGEN_SPDIF_OUT_ENABLE |
280 OXYGEN_SPDIF_LOOPBACK |
281 OXYGEN_SPDIF_SENSE_MASK |
282 OXYGEN_SPDIF_LOCK_MASK |
283 OXYGEN_SPDIF_RATE_MASK |
284 OXYGEN_SPDIF_SENSE_PAR |
285 OXYGEN_SPDIF_LOCK_PAR |
286 OXYGEN_SPDIF_IN_CLOCK_MASK);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100287 oxygen_write32(chip, OXYGEN_SPDIF_OUTPUT_BITS, chip->spdif_bits);
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100288 oxygen_clear_bits8(chip, OXYGEN_MPU401_CONTROL, OXYGEN_MPU401_LOOPBACK);
289 oxygen_write8(chip, OXYGEN_GPI_INTERRUPT_MASK, 0);
290 oxygen_write16(chip, OXYGEN_GPIO_INTERRUPT_MASK, 0);
Clemens Ladischc9946b22008-01-21 08:44:24 +0100291 oxygen_write16(chip, OXYGEN_PLAY_ROUTING,
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100292 OXYGEN_PLAY_MULTICH_I2S_DAC |
293 OXYGEN_PLAY_SPDIF_SPDIF |
Clemens Ladischc9946b22008-01-21 08:44:24 +0100294 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
295 (1 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
296 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
297 (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT));
298 oxygen_write8(chip, OXYGEN_REC_ROUTING,
299 OXYGEN_REC_A_ROUTE_I2S_ADC_1 |
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100300 OXYGEN_REC_B_ROUTE_I2S_ADC_2 |
Clemens Ladischc9946b22008-01-21 08:44:24 +0100301 OXYGEN_REC_C_ROUTE_SPDIF);
302 oxygen_write8(chip, OXYGEN_ADC_MONITOR, 0);
303 oxygen_write8(chip, OXYGEN_A_MONITOR_ROUTING,
304 (0 << OXYGEN_A_MONITOR_ROUTE_0_SHIFT) |
305 (1 << OXYGEN_A_MONITOR_ROUTE_1_SHIFT) |
306 (2 << OXYGEN_A_MONITOR_ROUTE_2_SHIFT) |
307 (3 << OXYGEN_A_MONITOR_ROUTE_3_SHIFT));
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100308
Clemens Ladischc9946b22008-01-21 08:44:24 +0100309 oxygen_write8(chip, OXYGEN_AC97_INTERRUPT_MASK, 0);
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100310 oxygen_write32(chip, OXYGEN_AC97_OUT_CONFIG, 0);
311 oxygen_write32(chip, OXYGEN_AC97_IN_CONFIG, 0);
312 if (!(chip->has_ac97_0 | chip->has_ac97_1))
313 oxygen_set_bits16(chip, OXYGEN_AC97_CONTROL,
314 OXYGEN_AC97_CLOCK_DISABLE);
315 if (!chip->has_ac97_0) {
316 oxygen_set_bits16(chip, OXYGEN_AC97_CONTROL,
317 OXYGEN_AC97_NO_CODEC_0);
318 } else {
Clemens Ladisch31c77642008-01-16 08:28:17 +0100319 oxygen_write_ac97(chip, 0, AC97_RESET, 0);
320 msleep(1);
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100321 oxygen_ac97_set_bits(chip, 0, CM9780_GPIO_SETUP,
322 CM9780_GPIO0IO | CM9780_GPIO1IO);
323 oxygen_ac97_set_bits(chip, 0, CM9780_MIXER,
324 CM9780_BSTSEL | CM9780_STRO_MIC |
325 CM9780_MIX2FR | CM9780_PCBSW);
326 oxygen_ac97_set_bits(chip, 0, CM9780_JACK,
327 CM9780_RSOE | CM9780_CBOE |
328 CM9780_SSOE | CM9780_FROE |
329 CM9780_MIC2MIC | CM9780_LI2LI);
Clemens Ladisch31c77642008-01-16 08:28:17 +0100330 oxygen_write_ac97(chip, 0, AC97_MASTER, 0x0000);
331 oxygen_write_ac97(chip, 0, AC97_PC_BEEP, 0x8000);
332 oxygen_write_ac97(chip, 0, AC97_MIC, 0x8808);
333 oxygen_write_ac97(chip, 0, AC97_LINE, 0x0808);
334 oxygen_write_ac97(chip, 0, AC97_CD, 0x8808);
335 oxygen_write_ac97(chip, 0, AC97_VIDEO, 0x8808);
336 oxygen_write_ac97(chip, 0, AC97_AUX, 0x8808);
337 oxygen_write_ac97(chip, 0, AC97_REC_GAIN, 0x8000);
338 oxygen_write_ac97(chip, 0, AC97_CENTER_LFE_MASTER, 0x8080);
339 oxygen_write_ac97(chip, 0, AC97_SURROUND_MASTER, 0x8080);
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100340 oxygen_ac97_clear_bits(chip, 0,
341 CM9780_GPIO_STATUS, CM9780_GPO0);
Clemens Ladisch31c77642008-01-16 08:28:17 +0100342 /* power down unused ADCs and DACs */
343 oxygen_ac97_set_bits(chip, 0, AC97_POWERDOWN,
344 AC97_PD_PR0 | AC97_PD_PR1);
345 oxygen_ac97_set_bits(chip, 0, AC97_EXTENDED_STATUS,
346 AC97_EA_PRI | AC97_EA_PRJ | AC97_EA_PRK);
347 }
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100348 if (chip->has_ac97_1) {
349 oxygen_set_bits32(chip, OXYGEN_AC97_OUT_CONFIG,
350 OXYGEN_AC97_CODEC1_SLOT3 |
351 OXYGEN_AC97_CODEC1_SLOT4);
352 oxygen_write_ac97(chip, 1, AC97_RESET, 0);
353 msleep(1);
354 oxygen_write_ac97(chip, 1, AC97_MASTER, 0x0000);
355 oxygen_write_ac97(chip, 1, AC97_HEADPHONE, 0x8000);
356 oxygen_write_ac97(chip, 1, AC97_PC_BEEP, 0x8000);
357 oxygen_write_ac97(chip, 1, AC97_MIC, 0x8808);
358 oxygen_write_ac97(chip, 1, AC97_LINE, 0x8808);
359 oxygen_write_ac97(chip, 1, AC97_CD, 0x8808);
360 oxygen_write_ac97(chip, 1, AC97_VIDEO, 0x8808);
361 oxygen_write_ac97(chip, 1, AC97_AUX, 0x8808);
362 oxygen_write_ac97(chip, 1, AC97_PCM, 0x0808);
363 oxygen_write_ac97(chip, 1, AC97_REC_SEL, 0x0000);
364 oxygen_write_ac97(chip, 1, AC97_REC_GAIN, 0x8000);
365 oxygen_ac97_clear_bits(chip, 1, AC97_REC_GAIN, 0x1c00);
366 oxygen_ac97_set_bits(chip, 1, 0x6a, 0x0040);
367 }
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100368}
369
370static void oxygen_card_free(struct snd_card *card)
371{
372 struct oxygen *chip = card->private_data;
373
374 spin_lock_irq(&chip->reg_lock);
375 chip->interrupt_mask = 0;
376 chip->pcm_running = 0;
377 oxygen_write16(chip, OXYGEN_DMA_STATUS, 0);
378 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, 0);
379 spin_unlock_irq(&chip->reg_lock);
380 if (chip->irq >= 0) {
381 free_irq(chip->irq, chip);
382 synchronize_irq(chip->irq);
383 }
384 flush_scheduled_work();
385 chip->model->cleanup(chip);
386 mutex_destroy(&chip->mutex);
387 pci_release_regions(chip->pci);
388 pci_disable_device(chip->pci);
389}
390
391int __devinit oxygen_pci_probe(struct pci_dev *pci, int index, char *id,
Clemens Ladisch44fb7aa2008-01-21 08:45:37 +0100392 int midi, const struct oxygen_model *model)
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100393{
394 struct snd_card *card;
395 struct oxygen *chip;
396 int err;
397
Clemens Ladisch7ef37cd2008-01-21 08:51:55 +0100398 card = snd_card_new(index, id, model->owner,
399 sizeof *chip + model->model_data_size);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100400 if (!card)
401 return -ENOMEM;
402
403 chip = card->private_data;
404 chip->card = card;
405 chip->pci = pci;
406 chip->irq = -1;
407 chip->model = model;
Clemens Ladisch7ef37cd2008-01-21 08:51:55 +0100408 chip->model_data = chip + 1;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100409 spin_lock_init(&chip->reg_lock);
410 mutex_init(&chip->mutex);
411 INIT_WORK(&chip->spdif_input_bits_work,
412 oxygen_spdif_input_bits_changed);
413
414 err = pci_enable_device(pci);
415 if (err < 0)
416 goto err_card;
417
418 err = pci_request_regions(pci, model->chip);
419 if (err < 0) {
420 snd_printk(KERN_ERR "cannot reserve PCI resources\n");
421 goto err_pci_enable;
422 }
423
424 if (!(pci_resource_flags(pci, 0) & IORESOURCE_IO) ||
425 pci_resource_len(pci, 0) < 0x100) {
426 snd_printk(KERN_ERR "invalid PCI I/O range\n");
427 err = -ENXIO;
428 goto err_pci_regions;
429 }
430 chip->addr = pci_resource_start(pci, 0);
431
432 pci_set_master(pci);
433 snd_card_set_dev(card, &pci->dev);
434 card->private_free = oxygen_card_free;
435
436 oxygen_init(chip);
437 model->init(chip);
438
439 err = request_irq(pci->irq, oxygen_interrupt, IRQF_SHARED,
440 model->chip, chip);
441 if (err < 0) {
442 snd_printk(KERN_ERR "cannot grab interrupt %d\n", pci->irq);
443 goto err_card;
444 }
445 chip->irq = pci->irq;
446
447 strcpy(card->driver, model->chip);
448 strcpy(card->shortname, model->shortname);
449 sprintf(card->longname, "%s (rev %u) at %#lx, irq %i",
450 model->longname, chip->revision, chip->addr, chip->irq);
451 strcpy(card->mixername, model->chip);
452 snd_component_add(card, model->chip);
453
454 err = oxygen_pcm_init(chip);
455 if (err < 0)
456 goto err_card;
457
458 err = oxygen_mixer_init(chip);
459 if (err < 0)
460 goto err_card;
461
Clemens Ladisch44fb7aa2008-01-21 08:45:37 +0100462 oxygen_write8_masked(chip, OXYGEN_MISC,
463 midi ? OXYGEN_MISC_MIDI : 0, OXYGEN_MISC_MIDI);
464 if (midi) {
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100465 err = snd_mpu401_uart_new(card, 0, MPU401_HW_CMIPCI,
466 chip->addr + OXYGEN_MPU401,
467 MPU401_INFO_INTEGRATED, 0, 0,
468 &chip->midi);
469 if (err < 0)
470 goto err_card;
471 }
472
473 oxygen_proc_init(chip);
474
475 spin_lock_irq(&chip->reg_lock);
Clemens Ladischc2353a02008-01-18 09:17:53 +0100476 chip->interrupt_mask |= OXYGEN_INT_SPDIF_IN_DETECT;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100477 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, chip->interrupt_mask);
478 spin_unlock_irq(&chip->reg_lock);
479
480 err = snd_card_register(card);
481 if (err < 0)
482 goto err_card;
483
484 pci_set_drvdata(pci, card);
485 return 0;
486
487err_pci_regions:
488 pci_release_regions(pci);
489err_pci_enable:
490 pci_disable_device(pci);
491err_card:
492 snd_card_free(card);
493 return err;
494}
495EXPORT_SYMBOL(oxygen_pci_probe);
496
497void __devexit oxygen_pci_remove(struct pci_dev *pci)
498{
499 snd_card_free(pci_get_drvdata(pci));
500 pci_set_drvdata(pci, NULL);
501}
502EXPORT_SYMBOL(oxygen_pci_remove);