blob: de6bf41c3e961ff69c391b46d5bdec09a3bdf6ad [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 Ladisch1e821dd2008-01-28 08:34:21 +010056 OXYGEN_INT_GPIO |
57 OXYGEN_INT_AC97);
Clemens Ladischd0ce9942007-12-23 19:50:57 +010058 if (clear) {
Clemens Ladischc2353a02008-01-18 09:17:53 +010059 if (clear & OXYGEN_INT_SPDIF_IN_DETECT)
60 chip->interrupt_mask &= ~OXYGEN_INT_SPDIF_IN_DETECT;
Clemens Ladischd0ce9942007-12-23 19:50:57 +010061 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK,
62 chip->interrupt_mask & ~clear);
63 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK,
64 chip->interrupt_mask);
65 }
66
67 elapsed_streams = status & chip->pcm_running;
68
69 spin_unlock(&chip->reg_lock);
70
71 for (i = 0; i < PCM_COUNT; ++i)
72 if ((elapsed_streams & (1 << i)) && chip->streams[i])
73 snd_pcm_period_elapsed(chip->streams[i]);
74
Clemens Ladischc2353a02008-01-18 09:17:53 +010075 if (status & OXYGEN_INT_SPDIF_IN_DETECT) {
Clemens Ladischd0ce9942007-12-23 19:50:57 +010076 spin_lock(&chip->reg_lock);
77 i = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL);
Clemens Ladisch7f0b8942008-01-21 08:53:30 +010078 if (i & (OXYGEN_SPDIF_SENSE_INT | OXYGEN_SPDIF_LOCK_INT |
79 OXYGEN_SPDIF_RATE_INT)) {
80 /* write the interrupt bit(s) to clear */
Clemens Ladischd0ce9942007-12-23 19:50:57 +010081 oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, i);
82 schedule_work(&chip->spdif_input_bits_work);
83 }
84 spin_unlock(&chip->reg_lock);
85 }
86
87 if (status & OXYGEN_INT_GPIO)
88 ;
89
90 if ((status & OXYGEN_INT_MIDI) && chip->midi)
91 snd_mpu401_uart_interrupt(0, chip->midi->private_data);
92
Clemens Ladisch1e821dd2008-01-28 08:34:21 +010093 if (status & OXYGEN_INT_AC97)
94 wake_up(&chip->ac97_waitqueue);
95
Clemens Ladischd0ce9942007-12-23 19:50:57 +010096 return IRQ_HANDLED;
97}
98
99static void oxygen_spdif_input_bits_changed(struct work_struct *work)
100{
101 struct oxygen *chip = container_of(work, struct oxygen,
102 spdif_input_bits_work);
Clemens Ladisch7f0b8942008-01-21 08:53:30 +0100103 u32 reg;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100104
Clemens Ladisch7f0b8942008-01-21 08:53:30 +0100105 /*
106 * This function gets called when there is new activity on the SPDIF
107 * input, or when we lose lock on the input signal, or when the rate
108 * changes.
109 */
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100110 msleep(1);
Clemens Ladisch7f0b8942008-01-21 08:53:30 +0100111 spin_lock_irq(&chip->reg_lock);
112 reg = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL);
113 if ((reg & (OXYGEN_SPDIF_SENSE_STATUS |
114 OXYGEN_SPDIF_LOCK_STATUS))
115 == OXYGEN_SPDIF_SENSE_STATUS) {
116 /*
117 * If we detect activity on the SPDIF input but cannot lock to
118 * a signal, the clock bit is likely to be wrong.
119 */
120 reg ^= OXYGEN_SPDIF_IN_CLOCK_MASK;
121 oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, reg);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100122 spin_unlock_irq(&chip->reg_lock);
123 msleep(1);
Clemens Ladisch7f0b8942008-01-21 08:53:30 +0100124 spin_lock_irq(&chip->reg_lock);
125 reg = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL);
126 if ((reg & (OXYGEN_SPDIF_SENSE_STATUS |
127 OXYGEN_SPDIF_LOCK_STATUS))
128 == OXYGEN_SPDIF_SENSE_STATUS) {
129 /* nothing detected with either clock; give up */
130 if ((reg & OXYGEN_SPDIF_IN_CLOCK_MASK)
131 == OXYGEN_SPDIF_IN_CLOCK_192) {
132 /*
133 * Reset clock to <= 96 kHz because this is
134 * more likely to be received next time.
135 */
136 reg &= ~OXYGEN_SPDIF_IN_CLOCK_MASK;
137 reg |= OXYGEN_SPDIF_IN_CLOCK_96;
138 oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, reg);
139 }
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100140 }
141 }
Clemens Ladisch7f0b8942008-01-21 08:53:30 +0100142 spin_unlock_irq(&chip->reg_lock);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100143
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100144 if (chip->controls[CONTROL_SPDIF_INPUT_BITS]) {
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100145 spin_lock_irq(&chip->reg_lock);
Clemens Ladischc2353a02008-01-18 09:17:53 +0100146 chip->interrupt_mask |= OXYGEN_INT_SPDIF_IN_DETECT;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100147 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK,
148 chip->interrupt_mask);
149 spin_unlock_irq(&chip->reg_lock);
150
Clemens Ladisch7f0b8942008-01-21 08:53:30 +0100151 /*
152 * We don't actually know that any channel status bits have
153 * changed, but let's send a notification just to be sure.
154 */
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100155 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100156 &chip->controls[CONTROL_SPDIF_INPUT_BITS]->id);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100157 }
158}
159
160#ifdef CONFIG_PROC_FS
161static void oxygen_proc_read(struct snd_info_entry *entry,
162 struct snd_info_buffer *buffer)
163{
164 struct oxygen *chip = entry->private_data;
165 int i, j;
166
167 snd_iprintf(buffer, "CMI8788\n\n");
168 for (i = 0; i < 0x100; i += 0x10) {
169 snd_iprintf(buffer, "%02x:", i);
170 for (j = 0; j < 0x10; ++j)
171 snd_iprintf(buffer, " %02x", oxygen_read8(chip, i + j));
172 snd_iprintf(buffer, "\n");
173 }
174 if (mutex_lock_interruptible(&chip->mutex) < 0)
175 return;
Clemens Ladisch31c77642008-01-16 08:28:17 +0100176 if (chip->has_ac97_0) {
177 snd_iprintf(buffer, "\nAC97\n");
178 for (i = 0; i < 0x80; i += 0x10) {
179 snd_iprintf(buffer, "%02x:", i);
180 for (j = 0; j < 0x10; j += 2)
181 snd_iprintf(buffer, " %04x",
182 oxygen_read_ac97(chip, 0, i + j));
183 snd_iprintf(buffer, "\n");
184 }
185 }
186 if (chip->has_ac97_1) {
187 snd_iprintf(buffer, "\nAC97 2\n");
188 for (i = 0; i < 0x80; i += 0x10) {
189 snd_iprintf(buffer, "%02x:", i);
190 for (j = 0; j < 0x10; j += 2)
191 snd_iprintf(buffer, " %04x",
192 oxygen_read_ac97(chip, 1, i + j));
193 snd_iprintf(buffer, "\n");
194 }
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100195 }
196 mutex_unlock(&chip->mutex);
197}
198
199static void __devinit oxygen_proc_init(struct oxygen *chip)
200{
201 struct snd_info_entry *entry;
202
203 if (!snd_card_proc_new(chip->card, "cmi8788", &entry))
204 snd_info_set_text_ops(entry, chip, oxygen_proc_read);
205}
206#else
207#define oxygen_proc_init(chip)
208#endif
209
210static void __devinit oxygen_init(struct oxygen *chip)
211{
212 unsigned int i;
213
214 chip->dac_routing = 1;
215 for (i = 0; i < 8; ++i)
216 chip->dac_volume[i] = 0xff;
217 chip->spdif_playback_enable = 1;
218 chip->spdif_bits = OXYGEN_SPDIF_C | OXYGEN_SPDIF_ORIGINAL |
219 (IEC958_AES1_CON_PCM_CODER << OXYGEN_SPDIF_CATEGORY_SHIFT);
220 chip->spdif_pcm_bits = chip->spdif_bits;
221
222 if (oxygen_read8(chip, OXYGEN_REVISION) & OXYGEN_REVISION_2)
223 chip->revision = 2;
224 else
225 chip->revision = 1;
226
227 if (chip->revision == 1)
Clemens Ladischc2353a02008-01-18 09:17:53 +0100228 oxygen_set_bits8(chip, OXYGEN_MISC,
229 OXYGEN_MISC_PCI_MEM_W_1_CLOCK);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100230
Clemens Ladisch31c77642008-01-16 08:28:17 +0100231 i = oxygen_read16(chip, OXYGEN_AC97_CONTROL);
232 chip->has_ac97_0 = (i & OXYGEN_AC97_CODEC_0) != 0;
233 chip->has_ac97_1 = (i & OXYGEN_AC97_CODEC_1) != 0;
234
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100235 oxygen_set_bits8(chip, OXYGEN_FUNCTION,
236 OXYGEN_FUNCTION_RESET_CODEC |
Clemens Ladisch84aa6b72008-01-16 08:28:54 +0100237 chip->model->function_flags);
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100238 oxygen_write8_masked(chip, OXYGEN_FUNCTION,
239 OXYGEN_FUNCTION_SPI,
240 OXYGEN_FUNCTION_2WIRE_SPI_MASK);
241 oxygen_write8(chip, OXYGEN_DMA_STATUS, 0);
242 oxygen_write8(chip, OXYGEN_DMA_PAUSE, 0);
243 oxygen_write8(chip, OXYGEN_PLAY_CHANNELS,
244 OXYGEN_PLAY_CHANNELS_2 |
245 OXYGEN_DMA_A_BURST_8 |
246 OXYGEN_DMA_MULTICH_BURST_8);
247 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, 0);
248 oxygen_write8_masked(chip, OXYGEN_MISC, 0,
249 OXYGEN_MISC_WRITE_PCI_SUBID |
250 OXYGEN_MISC_REC_C_FROM_SPDIF |
251 OXYGEN_MISC_REC_B_FROM_AC97 |
252 OXYGEN_MISC_REC_A_FROM_MULTICH);
253 oxygen_write8(chip, OXYGEN_REC_FORMAT,
254 (OXYGEN_FORMAT_16 << OXYGEN_REC_FORMAT_A_SHIFT) |
255 (OXYGEN_FORMAT_16 << OXYGEN_REC_FORMAT_B_SHIFT) |
256 (OXYGEN_FORMAT_16 << OXYGEN_REC_FORMAT_C_SHIFT));
257 oxygen_write8(chip, OXYGEN_PLAY_FORMAT,
258 (OXYGEN_FORMAT_16 << OXYGEN_SPDIF_FORMAT_SHIFT) |
259 (OXYGEN_FORMAT_16 << OXYGEN_MULTICH_FORMAT_SHIFT));
260 oxygen_write8(chip, OXYGEN_REC_CHANNELS, OXYGEN_REC_CHANNELS_2_2_2);
Clemens Ladischc9946b22008-01-21 08:44:24 +0100261 oxygen_write16(chip, OXYGEN_I2S_MULTICH_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_A_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_B_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);
273 oxygen_write16(chip, OXYGEN_I2S_C_FORMAT,
274 OXYGEN_RATE_48000 | OXYGEN_I2S_FORMAT_LJUST |
275 OXYGEN_I2S_MCLK_128 | OXYGEN_I2S_BITS_16 |
276 OXYGEN_I2S_MASTER | OXYGEN_I2S_BCLK_64);
Clemens Ladisch7f0b8942008-01-21 08:53:30 +0100277 oxygen_write32_masked(chip, OXYGEN_SPDIF_CONTROL,
278 OXYGEN_SPDIF_SENSE_MASK |
279 OXYGEN_SPDIF_LOCK_MASK |
280 OXYGEN_SPDIF_RATE_MASK |
281 OXYGEN_SPDIF_LOCK_PAR |
282 OXYGEN_SPDIF_IN_CLOCK_96,
283 OXYGEN_SPDIF_OUT_ENABLE |
284 OXYGEN_SPDIF_LOOPBACK |
285 OXYGEN_SPDIF_SENSE_MASK |
286 OXYGEN_SPDIF_LOCK_MASK |
287 OXYGEN_SPDIF_RATE_MASK |
288 OXYGEN_SPDIF_SENSE_PAR |
289 OXYGEN_SPDIF_LOCK_PAR |
290 OXYGEN_SPDIF_IN_CLOCK_MASK);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100291 oxygen_write32(chip, OXYGEN_SPDIF_OUTPUT_BITS, chip->spdif_bits);
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100292 oxygen_clear_bits8(chip, OXYGEN_MPU401_CONTROL, OXYGEN_MPU401_LOOPBACK);
293 oxygen_write8(chip, OXYGEN_GPI_INTERRUPT_MASK, 0);
294 oxygen_write16(chip, OXYGEN_GPIO_INTERRUPT_MASK, 0);
Clemens Ladischc9946b22008-01-21 08:44:24 +0100295 oxygen_write16(chip, OXYGEN_PLAY_ROUTING,
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100296 OXYGEN_PLAY_MULTICH_I2S_DAC |
297 OXYGEN_PLAY_SPDIF_SPDIF |
Clemens Ladischc9946b22008-01-21 08:44:24 +0100298 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
299 (1 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
300 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
301 (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT));
302 oxygen_write8(chip, OXYGEN_REC_ROUTING,
303 OXYGEN_REC_A_ROUTE_I2S_ADC_1 |
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100304 OXYGEN_REC_B_ROUTE_I2S_ADC_2 |
Clemens Ladischc9946b22008-01-21 08:44:24 +0100305 OXYGEN_REC_C_ROUTE_SPDIF);
306 oxygen_write8(chip, OXYGEN_ADC_MONITOR, 0);
307 oxygen_write8(chip, OXYGEN_A_MONITOR_ROUTING,
308 (0 << OXYGEN_A_MONITOR_ROUTE_0_SHIFT) |
309 (1 << OXYGEN_A_MONITOR_ROUTE_1_SHIFT) |
310 (2 << OXYGEN_A_MONITOR_ROUTE_2_SHIFT) |
311 (3 << OXYGEN_A_MONITOR_ROUTE_3_SHIFT));
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100312
Clemens Ladisch1e821dd2008-01-28 08:34:21 +0100313 oxygen_write8(chip, OXYGEN_AC97_INTERRUPT_MASK,
314 OXYGEN_AC97_INT_READ_DONE |
315 OXYGEN_AC97_INT_WRITE_DONE);
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100316 oxygen_write32(chip, OXYGEN_AC97_OUT_CONFIG, 0);
317 oxygen_write32(chip, OXYGEN_AC97_IN_CONFIG, 0);
318 if (!(chip->has_ac97_0 | chip->has_ac97_1))
319 oxygen_set_bits16(chip, OXYGEN_AC97_CONTROL,
320 OXYGEN_AC97_CLOCK_DISABLE);
321 if (!chip->has_ac97_0) {
322 oxygen_set_bits16(chip, OXYGEN_AC97_CONTROL,
323 OXYGEN_AC97_NO_CODEC_0);
324 } else {
Clemens Ladisch31c77642008-01-16 08:28:17 +0100325 oxygen_write_ac97(chip, 0, AC97_RESET, 0);
326 msleep(1);
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100327 oxygen_ac97_set_bits(chip, 0, CM9780_GPIO_SETUP,
328 CM9780_GPIO0IO | CM9780_GPIO1IO);
329 oxygen_ac97_set_bits(chip, 0, CM9780_MIXER,
330 CM9780_BSTSEL | CM9780_STRO_MIC |
331 CM9780_MIX2FR | CM9780_PCBSW);
332 oxygen_ac97_set_bits(chip, 0, CM9780_JACK,
333 CM9780_RSOE | CM9780_CBOE |
334 CM9780_SSOE | CM9780_FROE |
335 CM9780_MIC2MIC | CM9780_LI2LI);
Clemens Ladisch31c77642008-01-16 08:28:17 +0100336 oxygen_write_ac97(chip, 0, AC97_MASTER, 0x0000);
337 oxygen_write_ac97(chip, 0, AC97_PC_BEEP, 0x8000);
338 oxygen_write_ac97(chip, 0, AC97_MIC, 0x8808);
339 oxygen_write_ac97(chip, 0, AC97_LINE, 0x0808);
340 oxygen_write_ac97(chip, 0, AC97_CD, 0x8808);
341 oxygen_write_ac97(chip, 0, AC97_VIDEO, 0x8808);
342 oxygen_write_ac97(chip, 0, AC97_AUX, 0x8808);
343 oxygen_write_ac97(chip, 0, AC97_REC_GAIN, 0x8000);
344 oxygen_write_ac97(chip, 0, AC97_CENTER_LFE_MASTER, 0x8080);
345 oxygen_write_ac97(chip, 0, AC97_SURROUND_MASTER, 0x8080);
Clemens Ladisch31c77642008-01-16 08:28:17 +0100346 /* power down unused ADCs and DACs */
347 oxygen_ac97_set_bits(chip, 0, AC97_POWERDOWN,
348 AC97_PD_PR0 | AC97_PD_PR1);
349 oxygen_ac97_set_bits(chip, 0, AC97_EXTENDED_STATUS,
350 AC97_EA_PRI | AC97_EA_PRJ | AC97_EA_PRK);
351 }
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100352 if (chip->has_ac97_1) {
353 oxygen_set_bits32(chip, OXYGEN_AC97_OUT_CONFIG,
354 OXYGEN_AC97_CODEC1_SLOT3 |
355 OXYGEN_AC97_CODEC1_SLOT4);
356 oxygen_write_ac97(chip, 1, AC97_RESET, 0);
357 msleep(1);
358 oxygen_write_ac97(chip, 1, AC97_MASTER, 0x0000);
359 oxygen_write_ac97(chip, 1, AC97_HEADPHONE, 0x8000);
360 oxygen_write_ac97(chip, 1, AC97_PC_BEEP, 0x8000);
361 oxygen_write_ac97(chip, 1, AC97_MIC, 0x8808);
362 oxygen_write_ac97(chip, 1, AC97_LINE, 0x8808);
363 oxygen_write_ac97(chip, 1, AC97_CD, 0x8808);
364 oxygen_write_ac97(chip, 1, AC97_VIDEO, 0x8808);
365 oxygen_write_ac97(chip, 1, AC97_AUX, 0x8808);
366 oxygen_write_ac97(chip, 1, AC97_PCM, 0x0808);
367 oxygen_write_ac97(chip, 1, AC97_REC_SEL, 0x0000);
368 oxygen_write_ac97(chip, 1, AC97_REC_GAIN, 0x8000);
369 oxygen_ac97_clear_bits(chip, 1, AC97_REC_GAIN, 0x1c00);
370 oxygen_ac97_set_bits(chip, 1, 0x6a, 0x0040);
371 }
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100372}
373
374static void oxygen_card_free(struct snd_card *card)
375{
376 struct oxygen *chip = card->private_data;
377
378 spin_lock_irq(&chip->reg_lock);
379 chip->interrupt_mask = 0;
380 chip->pcm_running = 0;
381 oxygen_write16(chip, OXYGEN_DMA_STATUS, 0);
382 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, 0);
383 spin_unlock_irq(&chip->reg_lock);
384 if (chip->irq >= 0) {
385 free_irq(chip->irq, chip);
386 synchronize_irq(chip->irq);
387 }
388 flush_scheduled_work();
389 chip->model->cleanup(chip);
390 mutex_destroy(&chip->mutex);
391 pci_release_regions(chip->pci);
392 pci_disable_device(chip->pci);
393}
394
395int __devinit oxygen_pci_probe(struct pci_dev *pci, int index, char *id,
Clemens Ladisch44fb7aa2008-01-21 08:45:37 +0100396 int midi, const struct oxygen_model *model)
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100397{
398 struct snd_card *card;
399 struct oxygen *chip;
400 int err;
401
Clemens Ladisch7ef37cd2008-01-21 08:51:55 +0100402 card = snd_card_new(index, id, model->owner,
403 sizeof *chip + model->model_data_size);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100404 if (!card)
405 return -ENOMEM;
406
407 chip = card->private_data;
408 chip->card = card;
409 chip->pci = pci;
410 chip->irq = -1;
411 chip->model = model;
Clemens Ladisch7ef37cd2008-01-21 08:51:55 +0100412 chip->model_data = chip + 1;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100413 spin_lock_init(&chip->reg_lock);
414 mutex_init(&chip->mutex);
415 INIT_WORK(&chip->spdif_input_bits_work,
416 oxygen_spdif_input_bits_changed);
Clemens Ladisch1e821dd2008-01-28 08:34:21 +0100417 init_waitqueue_head(&chip->ac97_waitqueue);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100418
419 err = pci_enable_device(pci);
420 if (err < 0)
421 goto err_card;
422
423 err = pci_request_regions(pci, model->chip);
424 if (err < 0) {
425 snd_printk(KERN_ERR "cannot reserve PCI resources\n");
426 goto err_pci_enable;
427 }
428
429 if (!(pci_resource_flags(pci, 0) & IORESOURCE_IO) ||
430 pci_resource_len(pci, 0) < 0x100) {
431 snd_printk(KERN_ERR "invalid PCI I/O range\n");
432 err = -ENXIO;
433 goto err_pci_regions;
434 }
435 chip->addr = pci_resource_start(pci, 0);
436
437 pci_set_master(pci);
438 snd_card_set_dev(card, &pci->dev);
439 card->private_free = oxygen_card_free;
440
441 oxygen_init(chip);
442 model->init(chip);
443
444 err = request_irq(pci->irq, oxygen_interrupt, IRQF_SHARED,
445 model->chip, chip);
446 if (err < 0) {
447 snd_printk(KERN_ERR "cannot grab interrupt %d\n", pci->irq);
448 goto err_card;
449 }
450 chip->irq = pci->irq;
451
452 strcpy(card->driver, model->chip);
453 strcpy(card->shortname, model->shortname);
454 sprintf(card->longname, "%s (rev %u) at %#lx, irq %i",
455 model->longname, chip->revision, chip->addr, chip->irq);
456 strcpy(card->mixername, model->chip);
457 snd_component_add(card, model->chip);
458
459 err = oxygen_pcm_init(chip);
460 if (err < 0)
461 goto err_card;
462
463 err = oxygen_mixer_init(chip);
464 if (err < 0)
465 goto err_card;
466
Clemens Ladisch44fb7aa2008-01-21 08:45:37 +0100467 oxygen_write8_masked(chip, OXYGEN_MISC,
468 midi ? OXYGEN_MISC_MIDI : 0, OXYGEN_MISC_MIDI);
469 if (midi) {
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100470 err = snd_mpu401_uart_new(card, 0, MPU401_HW_CMIPCI,
471 chip->addr + OXYGEN_MPU401,
472 MPU401_INFO_INTEGRATED, 0, 0,
473 &chip->midi);
474 if (err < 0)
475 goto err_card;
476 }
477
478 oxygen_proc_init(chip);
479
480 spin_lock_irq(&chip->reg_lock);
Clemens Ladisch1e821dd2008-01-28 08:34:21 +0100481 chip->interrupt_mask |= OXYGEN_INT_SPDIF_IN_DETECT | OXYGEN_INT_AC97;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100482 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, chip->interrupt_mask);
483 spin_unlock_irq(&chip->reg_lock);
484
485 err = snd_card_register(card);
486 if (err < 0)
487 goto err_card;
488
489 pci_set_drvdata(pci, card);
490 return 0;
491
492err_pci_regions:
493 pci_release_regions(pci);
494err_pci_enable:
495 pci_disable_device(pci);
496err_card:
497 snd_card_free(card);
498 return err;
499}
500EXPORT_SYMBOL(oxygen_pci_probe);
501
502void __devexit oxygen_pci_remove(struct pci_dev *pci)
503{
504 snd_card_free(pci_get_drvdata(pci));
505 pci_set_drvdata(pci, NULL);
506}
507EXPORT_SYMBOL(oxygen_pci_remove);