blob: b1997216b4af678b64f594c8198abf944a2d9cae [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");
Clemens Ladischd023dc02008-05-13 09:18:27 +020035MODULE_LICENSE("GPL v2");
Clemens Ladischd0ce9942007-12-23 19:50:57 +010036
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 Ladisch1e821dd22008-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)
Clemens Ladisch7c014152008-01-28 08:36:55 +010088 schedule_work(&chip->gpio_work);
Clemens Ladischd0ce9942007-12-23 19:50:57 +010089
90 if ((status & OXYGEN_INT_MIDI) && chip->midi)
91 snd_mpu401_uart_interrupt(0, chip->midi->private_data);
92
Clemens Ladisch1e821dd22008-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
Clemens Ladisch7c014152008-01-28 08:36:55 +0100160static void oxygen_gpio_changed(struct work_struct *work)
161{
162 struct oxygen *chip = container_of(work, struct oxygen, gpio_work);
163
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200164 if (chip->model.gpio_changed)
165 chip->model.gpio_changed(chip);
Clemens Ladisch7c014152008-01-28 08:36:55 +0100166}
167
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100168#ifdef CONFIG_PROC_FS
169static void oxygen_proc_read(struct snd_info_entry *entry,
170 struct snd_info_buffer *buffer)
171{
172 struct oxygen *chip = entry->private_data;
173 int i, j;
174
175 snd_iprintf(buffer, "CMI8788\n\n");
Clemens Ladischc13650072008-05-13 09:19:53 +0200176 for (i = 0; i < OXYGEN_IO_SIZE; i += 0x10) {
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100177 snd_iprintf(buffer, "%02x:", i);
178 for (j = 0; j < 0x10; ++j)
179 snd_iprintf(buffer, " %02x", oxygen_read8(chip, i + j));
180 snd_iprintf(buffer, "\n");
181 }
182 if (mutex_lock_interruptible(&chip->mutex) < 0)
183 return;
Clemens Ladisch31c77642008-01-16 08:28:17 +0100184 if (chip->has_ac97_0) {
185 snd_iprintf(buffer, "\nAC97\n");
186 for (i = 0; i < 0x80; i += 0x10) {
187 snd_iprintf(buffer, "%02x:", i);
188 for (j = 0; j < 0x10; j += 2)
189 snd_iprintf(buffer, " %04x",
190 oxygen_read_ac97(chip, 0, i + j));
191 snd_iprintf(buffer, "\n");
192 }
193 }
194 if (chip->has_ac97_1) {
195 snd_iprintf(buffer, "\nAC97 2\n");
196 for (i = 0; i < 0x80; i += 0x10) {
197 snd_iprintf(buffer, "%02x:", i);
198 for (j = 0; j < 0x10; j += 2)
199 snd_iprintf(buffer, " %04x",
200 oxygen_read_ac97(chip, 1, i + j));
201 snd_iprintf(buffer, "\n");
202 }
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100203 }
204 mutex_unlock(&chip->mutex);
205}
206
Takashi Iwaif007dc02008-02-22 18:35:22 +0100207static void oxygen_proc_init(struct oxygen *chip)
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100208{
209 struct snd_info_entry *entry;
210
211 if (!snd_card_proc_new(chip->card, "cmi8788", &entry))
212 snd_info_set_text_ops(entry, chip, oxygen_proc_read);
213}
214#else
215#define oxygen_proc_init(chip)
216#endif
217
Takashi Iwaif007dc02008-02-22 18:35:22 +0100218static void oxygen_init(struct oxygen *chip)
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100219{
220 unsigned int i;
221
222 chip->dac_routing = 1;
223 for (i = 0; i < 8; ++i)
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200224 chip->dac_volume[i] = chip->model.dac_volume_min;
Clemens Ladische9835322008-04-16 09:14:30 +0200225 chip->dac_mute = 1;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100226 chip->spdif_playback_enable = 1;
227 chip->spdif_bits = OXYGEN_SPDIF_C | OXYGEN_SPDIF_ORIGINAL |
228 (IEC958_AES1_CON_PCM_CODER << OXYGEN_SPDIF_CATEGORY_SHIFT);
229 chip->spdif_pcm_bits = chip->spdif_bits;
230
231 if (oxygen_read8(chip, OXYGEN_REVISION) & OXYGEN_REVISION_2)
232 chip->revision = 2;
233 else
234 chip->revision = 1;
235
236 if (chip->revision == 1)
Clemens Ladischc2353a02008-01-18 09:17:53 +0100237 oxygen_set_bits8(chip, OXYGEN_MISC,
238 OXYGEN_MISC_PCI_MEM_W_1_CLOCK);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100239
Clemens Ladisch31c77642008-01-16 08:28:17 +0100240 i = oxygen_read16(chip, OXYGEN_AC97_CONTROL);
241 chip->has_ac97_0 = (i & OXYGEN_AC97_CODEC_0) != 0;
242 chip->has_ac97_1 = (i & OXYGEN_AC97_CODEC_1) != 0;
243
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100244 oxygen_write8_masked(chip, OXYGEN_FUNCTION,
Clemens Ladisch87eedd22008-03-19 08:20:13 +0100245 OXYGEN_FUNCTION_RESET_CODEC |
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200246 chip->model.function_flags,
Clemens Ladisch87eedd22008-03-19 08:20:13 +0100247 OXYGEN_FUNCTION_RESET_CODEC |
248 OXYGEN_FUNCTION_2WIRE_SPI_MASK |
249 OXYGEN_FUNCTION_ENABLE_SPI_4_5);
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100250 oxygen_write8(chip, OXYGEN_DMA_STATUS, 0);
251 oxygen_write8(chip, OXYGEN_DMA_PAUSE, 0);
252 oxygen_write8(chip, OXYGEN_PLAY_CHANNELS,
253 OXYGEN_PLAY_CHANNELS_2 |
254 OXYGEN_DMA_A_BURST_8 |
255 OXYGEN_DMA_MULTICH_BURST_8);
256 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, 0);
Clemens Ladischdb12b8e2008-03-19 08:20:59 +0100257 oxygen_write8_masked(chip, OXYGEN_MISC,
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200258 chip->model.misc_flags,
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100259 OXYGEN_MISC_WRITE_PCI_SUBID |
260 OXYGEN_MISC_REC_C_FROM_SPDIF |
261 OXYGEN_MISC_REC_B_FROM_AC97 |
Clemens Ladischdb12b8e2008-03-19 08:20:59 +0100262 OXYGEN_MISC_REC_A_FROM_MULTICH |
263 OXYGEN_MISC_MIDI);
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100264 oxygen_write8(chip, OXYGEN_REC_FORMAT,
265 (OXYGEN_FORMAT_16 << OXYGEN_REC_FORMAT_A_SHIFT) |
266 (OXYGEN_FORMAT_16 << OXYGEN_REC_FORMAT_B_SHIFT) |
267 (OXYGEN_FORMAT_16 << OXYGEN_REC_FORMAT_C_SHIFT));
268 oxygen_write8(chip, OXYGEN_PLAY_FORMAT,
269 (OXYGEN_FORMAT_16 << OXYGEN_SPDIF_FORMAT_SHIFT) |
270 (OXYGEN_FORMAT_16 << OXYGEN_MULTICH_FORMAT_SHIFT));
271 oxygen_write8(chip, OXYGEN_REC_CHANNELS, OXYGEN_REC_CHANNELS_2_2_2);
Clemens Ladischc9946b22008-01-21 08:44:24 +0100272 oxygen_write16(chip, OXYGEN_I2S_MULTICH_FORMAT,
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200273 OXYGEN_RATE_48000 | chip->model.dac_i2s_format |
Clemens Ladischfa5d8102008-03-19 08:17:33 +0100274 OXYGEN_I2S_MCLK_256 | OXYGEN_I2S_BITS_16 |
Clemens Ladischc9946b22008-01-21 08:44:24 +0100275 OXYGEN_I2S_MASTER | OXYGEN_I2S_BCLK_64);
Clemens Ladischd76596b2008-09-22 09:02:08 +0200276 if (chip->model.device_config & CAPTURE_0_FROM_I2S_1)
Clemens Ladisch43dd89c2008-03-19 08:21:32 +0100277 oxygen_write16(chip, OXYGEN_I2S_A_FORMAT,
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200278 OXYGEN_RATE_48000 | chip->model.adc_i2s_format |
Clemens Ladisch43dd89c2008-03-19 08:21:32 +0100279 OXYGEN_I2S_MCLK_256 | OXYGEN_I2S_BITS_16 |
280 OXYGEN_I2S_MASTER | OXYGEN_I2S_BCLK_64);
281 else
282 oxygen_write16(chip, OXYGEN_I2S_A_FORMAT,
283 OXYGEN_I2S_MASTER | OXYGEN_I2S_MUTE_MCLK);
Clemens Ladischd76596b2008-09-22 09:02:08 +0200284 if (chip->model.device_config & (CAPTURE_0_FROM_I2S_2 |
285 CAPTURE_2_FROM_I2S_2))
Clemens Ladisch43dd89c2008-03-19 08:21:32 +0100286 oxygen_write16(chip, OXYGEN_I2S_B_FORMAT,
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200287 OXYGEN_RATE_48000 | chip->model.adc_i2s_format |
Clemens Ladisch43dd89c2008-03-19 08:21:32 +0100288 OXYGEN_I2S_MCLK_256 | OXYGEN_I2S_BITS_16 |
289 OXYGEN_I2S_MASTER | OXYGEN_I2S_BCLK_64);
290 else
291 oxygen_write16(chip, OXYGEN_I2S_B_FORMAT,
292 OXYGEN_I2S_MASTER | OXYGEN_I2S_MUTE_MCLK);
Clemens Ladischc9946b22008-01-21 08:44:24 +0100293 oxygen_write16(chip, OXYGEN_I2S_C_FORMAT,
Clemens Ladisch43dd89c2008-03-19 08:21:32 +0100294 OXYGEN_I2S_MASTER | OXYGEN_I2S_MUTE_MCLK);
Clemens Ladisch1d98c7d2008-04-09 09:16:33 +0200295 oxygen_clear_bits32(chip, OXYGEN_SPDIF_CONTROL,
296 OXYGEN_SPDIF_OUT_ENABLE |
297 OXYGEN_SPDIF_LOOPBACK);
Clemens Ladischd76596b2008-09-22 09:02:08 +0200298 if (chip->model.device_config & CAPTURE_1_FROM_SPDIF)
Clemens Ladisch1d98c7d2008-04-09 09:16:33 +0200299 oxygen_write32_masked(chip, OXYGEN_SPDIF_CONTROL,
300 OXYGEN_SPDIF_SENSE_MASK |
301 OXYGEN_SPDIF_LOCK_MASK |
302 OXYGEN_SPDIF_RATE_MASK |
303 OXYGEN_SPDIF_LOCK_PAR |
304 OXYGEN_SPDIF_IN_CLOCK_96,
305 OXYGEN_SPDIF_SENSE_MASK |
306 OXYGEN_SPDIF_LOCK_MASK |
307 OXYGEN_SPDIF_RATE_MASK |
308 OXYGEN_SPDIF_SENSE_PAR |
309 OXYGEN_SPDIF_LOCK_PAR |
310 OXYGEN_SPDIF_IN_CLOCK_MASK);
311 else
312 oxygen_clear_bits32(chip, OXYGEN_SPDIF_CONTROL,
313 OXYGEN_SPDIF_SENSE_MASK |
314 OXYGEN_SPDIF_LOCK_MASK |
315 OXYGEN_SPDIF_RATE_MASK);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100316 oxygen_write32(chip, OXYGEN_SPDIF_OUTPUT_BITS, chip->spdif_bits);
Clemens Ladisch4a4bc532008-05-13 09:24:39 +0200317 oxygen_write16(chip, OXYGEN_2WIRE_BUS_STATUS,
318 OXYGEN_2WIRE_LENGTH_8 |
319 OXYGEN_2WIRE_INTERRUPT_MASK |
320 OXYGEN_2WIRE_SPEED_STANDARD);
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100321 oxygen_clear_bits8(chip, OXYGEN_MPU401_CONTROL, OXYGEN_MPU401_LOOPBACK);
322 oxygen_write8(chip, OXYGEN_GPI_INTERRUPT_MASK, 0);
323 oxygen_write16(chip, OXYGEN_GPIO_INTERRUPT_MASK, 0);
Clemens Ladischc9946b22008-01-21 08:44:24 +0100324 oxygen_write16(chip, OXYGEN_PLAY_ROUTING,
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100325 OXYGEN_PLAY_MULTICH_I2S_DAC |
326 OXYGEN_PLAY_SPDIF_SPDIF |
Clemens Ladischc9946b22008-01-21 08:44:24 +0100327 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
328 (1 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
329 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
330 (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT));
331 oxygen_write8(chip, OXYGEN_REC_ROUTING,
332 OXYGEN_REC_A_ROUTE_I2S_ADC_1 |
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100333 OXYGEN_REC_B_ROUTE_I2S_ADC_2 |
Clemens Ladischc9946b22008-01-21 08:44:24 +0100334 OXYGEN_REC_C_ROUTE_SPDIF);
335 oxygen_write8(chip, OXYGEN_ADC_MONITOR, 0);
336 oxygen_write8(chip, OXYGEN_A_MONITOR_ROUTING,
337 (0 << OXYGEN_A_MONITOR_ROUTE_0_SHIFT) |
338 (1 << OXYGEN_A_MONITOR_ROUTE_1_SHIFT) |
339 (2 << OXYGEN_A_MONITOR_ROUTE_2_SHIFT) |
340 (3 << OXYGEN_A_MONITOR_ROUTE_3_SHIFT));
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100341
Clemens Ladisch1d98c7d2008-04-09 09:16:33 +0200342 if (chip->has_ac97_0 | chip->has_ac97_1)
343 oxygen_write8(chip, OXYGEN_AC97_INTERRUPT_MASK,
344 OXYGEN_AC97_INT_READ_DONE |
345 OXYGEN_AC97_INT_WRITE_DONE);
346 else
347 oxygen_write8(chip, OXYGEN_AC97_INTERRUPT_MASK, 0);
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100348 oxygen_write32(chip, OXYGEN_AC97_OUT_CONFIG, 0);
349 oxygen_write32(chip, OXYGEN_AC97_IN_CONFIG, 0);
350 if (!(chip->has_ac97_0 | chip->has_ac97_1))
351 oxygen_set_bits16(chip, OXYGEN_AC97_CONTROL,
352 OXYGEN_AC97_CLOCK_DISABLE);
353 if (!chip->has_ac97_0) {
354 oxygen_set_bits16(chip, OXYGEN_AC97_CONTROL,
355 OXYGEN_AC97_NO_CODEC_0);
356 } else {
Clemens Ladisch31c77642008-01-16 08:28:17 +0100357 oxygen_write_ac97(chip, 0, AC97_RESET, 0);
358 msleep(1);
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100359 oxygen_ac97_set_bits(chip, 0, CM9780_GPIO_SETUP,
360 CM9780_GPIO0IO | CM9780_GPIO1IO);
361 oxygen_ac97_set_bits(chip, 0, CM9780_MIXER,
362 CM9780_BSTSEL | CM9780_STRO_MIC |
363 CM9780_MIX2FR | CM9780_PCBSW);
364 oxygen_ac97_set_bits(chip, 0, CM9780_JACK,
365 CM9780_RSOE | CM9780_CBOE |
366 CM9780_SSOE | CM9780_FROE |
367 CM9780_MIC2MIC | CM9780_LI2LI);
Clemens Ladisch31c77642008-01-16 08:28:17 +0100368 oxygen_write_ac97(chip, 0, AC97_MASTER, 0x0000);
369 oxygen_write_ac97(chip, 0, AC97_PC_BEEP, 0x8000);
370 oxygen_write_ac97(chip, 0, AC97_MIC, 0x8808);
371 oxygen_write_ac97(chip, 0, AC97_LINE, 0x0808);
372 oxygen_write_ac97(chip, 0, AC97_CD, 0x8808);
373 oxygen_write_ac97(chip, 0, AC97_VIDEO, 0x8808);
374 oxygen_write_ac97(chip, 0, AC97_AUX, 0x8808);
375 oxygen_write_ac97(chip, 0, AC97_REC_GAIN, 0x8000);
376 oxygen_write_ac97(chip, 0, AC97_CENTER_LFE_MASTER, 0x8080);
377 oxygen_write_ac97(chip, 0, AC97_SURROUND_MASTER, 0x8080);
Clemens Ladische97f7992008-04-01 10:02:18 +0200378 oxygen_ac97_clear_bits(chip, 0, CM9780_GPIO_STATUS,
379 CM9780_GPO0);
Clemens Ladisch31c77642008-01-16 08:28:17 +0100380 /* power down unused ADCs and DACs */
381 oxygen_ac97_set_bits(chip, 0, AC97_POWERDOWN,
382 AC97_PD_PR0 | AC97_PD_PR1);
383 oxygen_ac97_set_bits(chip, 0, AC97_EXTENDED_STATUS,
384 AC97_EA_PRI | AC97_EA_PRJ | AC97_EA_PRK);
385 }
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100386 if (chip->has_ac97_1) {
387 oxygen_set_bits32(chip, OXYGEN_AC97_OUT_CONFIG,
388 OXYGEN_AC97_CODEC1_SLOT3 |
389 OXYGEN_AC97_CODEC1_SLOT4);
390 oxygen_write_ac97(chip, 1, AC97_RESET, 0);
391 msleep(1);
392 oxygen_write_ac97(chip, 1, AC97_MASTER, 0x0000);
393 oxygen_write_ac97(chip, 1, AC97_HEADPHONE, 0x8000);
394 oxygen_write_ac97(chip, 1, AC97_PC_BEEP, 0x8000);
395 oxygen_write_ac97(chip, 1, AC97_MIC, 0x8808);
396 oxygen_write_ac97(chip, 1, AC97_LINE, 0x8808);
397 oxygen_write_ac97(chip, 1, AC97_CD, 0x8808);
398 oxygen_write_ac97(chip, 1, AC97_VIDEO, 0x8808);
399 oxygen_write_ac97(chip, 1, AC97_AUX, 0x8808);
400 oxygen_write_ac97(chip, 1, AC97_PCM, 0x0808);
401 oxygen_write_ac97(chip, 1, AC97_REC_SEL, 0x0000);
Clemens Ladischa3601562008-01-28 08:35:20 +0100402 oxygen_write_ac97(chip, 1, AC97_REC_GAIN, 0x0000);
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100403 oxygen_ac97_set_bits(chip, 1, 0x6a, 0x0040);
404 }
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100405}
406
407static void oxygen_card_free(struct snd_card *card)
408{
409 struct oxygen *chip = card->private_data;
410
411 spin_lock_irq(&chip->reg_lock);
412 chip->interrupt_mask = 0;
413 chip->pcm_running = 0;
414 oxygen_write16(chip, OXYGEN_DMA_STATUS, 0);
415 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, 0);
416 spin_unlock_irq(&chip->reg_lock);
Jeff Garzikf000fd82008-04-22 13:50:34 +0200417 if (chip->irq >= 0)
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100418 free_irq(chip->irq, chip);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100419 flush_scheduled_work();
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200420 chip->model.cleanup(chip);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100421 mutex_destroy(&chip->mutex);
422 pci_release_regions(chip->pci);
423 pci_disable_device(chip->pci);
424}
425
Takashi Iwaif007dc02008-02-22 18:35:22 +0100426int oxygen_pci_probe(struct pci_dev *pci, int index, char *id,
Clemens Ladisch568c59e2008-09-22 08:56:01 +0200427 const struct oxygen_model *model,
428 unsigned long driver_data)
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100429{
430 struct snd_card *card;
431 struct oxygen *chip;
432 int err;
433
Clemens Ladisch7ef37cd2008-01-21 08:51:55 +0100434 card = snd_card_new(index, id, model->owner,
435 sizeof *chip + model->model_data_size);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100436 if (!card)
437 return -ENOMEM;
438
439 chip = card->private_data;
440 chip->card = card;
441 chip->pci = pci;
442 chip->irq = -1;
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200443 chip->model = *model;
Clemens Ladisch7ef37cd2008-01-21 08:51:55 +0100444 chip->model_data = chip + 1;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100445 spin_lock_init(&chip->reg_lock);
446 mutex_init(&chip->mutex);
447 INIT_WORK(&chip->spdif_input_bits_work,
448 oxygen_spdif_input_bits_changed);
Clemens Ladisch7c014152008-01-28 08:36:55 +0100449 INIT_WORK(&chip->gpio_work, oxygen_gpio_changed);
Clemens Ladisch1e821dd22008-01-28 08:34:21 +0100450 init_waitqueue_head(&chip->ac97_waitqueue);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100451
452 err = pci_enable_device(pci);
453 if (err < 0)
454 goto err_card;
455
456 err = pci_request_regions(pci, model->chip);
457 if (err < 0) {
458 snd_printk(KERN_ERR "cannot reserve PCI resources\n");
459 goto err_pci_enable;
460 }
461
462 if (!(pci_resource_flags(pci, 0) & IORESOURCE_IO) ||
Clemens Ladischc13650072008-05-13 09:19:53 +0200463 pci_resource_len(pci, 0) < OXYGEN_IO_SIZE) {
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100464 snd_printk(KERN_ERR "invalid PCI I/O range\n");
465 err = -ENXIO;
466 goto err_pci_regions;
467 }
468 chip->addr = pci_resource_start(pci, 0);
469
470 pci_set_master(pci);
471 snd_card_set_dev(card, &pci->dev);
472 card->private_free = oxygen_card_free;
473
Clemens Ladisch568c59e2008-09-22 08:56:01 +0200474 if (chip->model.probe) {
475 err = chip->model.probe(chip, driver_data);
476 if (err < 0)
477 goto err_card;
478 }
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100479 oxygen_init(chip);
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200480 chip->model.init(chip);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100481
482 err = request_irq(pci->irq, oxygen_interrupt, IRQF_SHARED,
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200483 chip->model.chip, chip);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100484 if (err < 0) {
485 snd_printk(KERN_ERR "cannot grab interrupt %d\n", pci->irq);
486 goto err_card;
487 }
488 chip->irq = pci->irq;
489
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200490 strcpy(card->driver, chip->model.chip);
491 strcpy(card->shortname, chip->model.shortname);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100492 sprintf(card->longname, "%s (rev %u) at %#lx, irq %i",
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200493 chip->model.longname, chip->revision, chip->addr, chip->irq);
494 strcpy(card->mixername, chip->model.chip);
495 snd_component_add(card, chip->model.chip);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100496
497 err = oxygen_pcm_init(chip);
498 if (err < 0)
499 goto err_card;
500
501 err = oxygen_mixer_init(chip);
502 if (err < 0)
503 goto err_card;
504
Clemens Ladischdbbbd672008-09-22 09:03:42 +0200505 if (chip->model.device_config & (MIDI_OUTPUT | MIDI_INPUT)) {
506 unsigned int info_flags = MPU401_INFO_INTEGRATED;
507 if (chip->model.device_config & MIDI_OUTPUT)
508 info_flags |= MPU401_INFO_OUTPUT;
509 if (chip->model.device_config & MIDI_INPUT)
510 info_flags |= MPU401_INFO_INPUT;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100511 err = snd_mpu401_uart_new(card, 0, MPU401_HW_CMIPCI,
512 chip->addr + OXYGEN_MPU401,
Clemens Ladischdbbbd672008-09-22 09:03:42 +0200513 info_flags, 0, 0,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100514 &chip->midi);
515 if (err < 0)
516 goto err_card;
517 }
518
519 oxygen_proc_init(chip);
520
521 spin_lock_irq(&chip->reg_lock);
Clemens Ladischd76596b2008-09-22 09:02:08 +0200522 if (chip->model.device_config & CAPTURE_1_FROM_SPDIF)
Clemens Ladisch1d98c7d2008-04-09 09:16:33 +0200523 chip->interrupt_mask |= OXYGEN_INT_SPDIF_IN_DETECT;
524 if (chip->has_ac97_0 | chip->has_ac97_1)
525 chip->interrupt_mask |= OXYGEN_INT_AC97;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100526 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, chip->interrupt_mask);
527 spin_unlock_irq(&chip->reg_lock);
528
529 err = snd_card_register(card);
530 if (err < 0)
531 goto err_card;
532
533 pci_set_drvdata(pci, card);
534 return 0;
535
536err_pci_regions:
537 pci_release_regions(pci);
538err_pci_enable:
539 pci_disable_device(pci);
540err_card:
541 snd_card_free(card);
542 return err;
543}
544EXPORT_SYMBOL(oxygen_pci_probe);
545
Takashi Iwaif007dc02008-02-22 18:35:22 +0100546void oxygen_pci_remove(struct pci_dev *pci)
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100547{
548 snd_card_free(pci_get_drvdata(pci));
549 pci_set_drvdata(pci, NULL);
550}
551EXPORT_SYMBOL(oxygen_pci_remove);
Clemens Ladisch4a4bc532008-05-13 09:24:39 +0200552
553#ifdef CONFIG_PM
554int oxygen_pci_suspend(struct pci_dev *pci, pm_message_t state)
555{
556 struct snd_card *card = pci_get_drvdata(pci);
557 struct oxygen *chip = card->private_data;
558 unsigned int i, saved_interrupt_mask;
559
560 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
561
562 for (i = 0; i < PCM_COUNT; ++i)
563 if (chip->streams[i])
564 snd_pcm_suspend(chip->streams[i]);
565
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200566 if (chip->model.suspend)
567 chip->model.suspend(chip);
Clemens Ladisch4a4bc532008-05-13 09:24:39 +0200568
569 spin_lock_irq(&chip->reg_lock);
570 saved_interrupt_mask = chip->interrupt_mask;
571 chip->interrupt_mask = 0;
572 oxygen_write16(chip, OXYGEN_DMA_STATUS, 0);
573 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, 0);
574 spin_unlock_irq(&chip->reg_lock);
575
576 synchronize_irq(chip->irq);
577 flush_scheduled_work();
578 chip->interrupt_mask = saved_interrupt_mask;
579
580 pci_disable_device(pci);
581 pci_save_state(pci);
582 pci_set_power_state(pci, pci_choose_state(pci, state));
583 return 0;
584}
585EXPORT_SYMBOL(oxygen_pci_suspend);
586
587static const u32 registers_to_restore[OXYGEN_IO_SIZE / 32] = {
588 0xffffffff, 0x00ff077f, 0x00011d08, 0x007f00ff,
589 0x00300000, 0x00000fe4, 0x0ff7001f, 0x00000000
590};
591static const u32 ac97_registers_to_restore[2][0x40 / 32] = {
592 { 0x18284fa2, 0x03060000 },
593 { 0x00007fa6, 0x00200000 }
594};
595
596static inline int is_bit_set(const u32 *bitmap, unsigned int bit)
597{
598 return bitmap[bit / 32] & (1 << (bit & 31));
599}
600
601static void oxygen_restore_ac97(struct oxygen *chip, unsigned int codec)
602{
603 unsigned int i;
604
605 oxygen_write_ac97(chip, codec, AC97_RESET, 0);
606 msleep(1);
607 for (i = 1; i < 0x40; ++i)
608 if (is_bit_set(ac97_registers_to_restore[codec], i))
609 oxygen_write_ac97(chip, codec, i * 2,
610 chip->saved_ac97_registers[codec][i]);
611}
612
613int oxygen_pci_resume(struct pci_dev *pci)
614{
615 struct snd_card *card = pci_get_drvdata(pci);
616 struct oxygen *chip = card->private_data;
617 unsigned int i;
618
619 pci_set_power_state(pci, PCI_D0);
620 pci_restore_state(pci);
621 if (pci_enable_device(pci) < 0) {
622 snd_printk(KERN_ERR "cannot reenable device");
623 snd_card_disconnect(card);
624 return -EIO;
625 }
626 pci_set_master(pci);
627
628 oxygen_write16(chip, OXYGEN_DMA_STATUS, 0);
629 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, 0);
630 for (i = 0; i < OXYGEN_IO_SIZE; ++i)
631 if (is_bit_set(registers_to_restore, i))
632 oxygen_write8(chip, i, chip->saved_registers._8[i]);
633 if (chip->has_ac97_0)
634 oxygen_restore_ac97(chip, 0);
635 if (chip->has_ac97_1)
636 oxygen_restore_ac97(chip, 1);
637
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200638 if (chip->model.resume)
639 chip->model.resume(chip);
Clemens Ladisch4a4bc532008-05-13 09:24:39 +0200640
641 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, chip->interrupt_mask);
642
643 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
644 return 0;
645}
646EXPORT_SYMBOL(oxygen_pci_resume);
647#endif /* CONFIG_PM */