blob: 228f30800fd90f76b0d6e11ea713505698093f6d [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
Clemens Ladisch397b1dc2008-09-22 09:04:43 +020038static inline int oxygen_uart_input_ready(struct oxygen *chip)
39{
40 return !(oxygen_read8(chip, OXYGEN_MPU401 + 1) & MPU401_RX_EMPTY);
41}
42
43static void oxygen_read_uart(struct oxygen *chip)
44{
45 if (unlikely(!oxygen_uart_input_ready(chip))) {
46 /* no data, but read it anyway to clear the interrupt */
47 oxygen_read8(chip, OXYGEN_MPU401);
48 return;
49 }
50 do {
51 u8 data = oxygen_read8(chip, OXYGEN_MPU401);
52 if (data == MPU401_ACK)
53 continue;
54 if (chip->uart_input_count >= ARRAY_SIZE(chip->uart_input))
55 chip->uart_input_count = 0;
56 chip->uart_input[chip->uart_input_count++] = data;
57 } while (oxygen_uart_input_ready(chip));
58 if (chip->model.uart_input)
59 chip->model.uart_input(chip);
60}
61
Clemens Ladischd0ce9942007-12-23 19:50:57 +010062static irqreturn_t oxygen_interrupt(int dummy, void *dev_id)
63{
64 struct oxygen *chip = dev_id;
65 unsigned int status, clear, elapsed_streams, i;
66
67 status = oxygen_read16(chip, OXYGEN_INTERRUPT_STATUS);
68 if (!status)
69 return IRQ_NONE;
70
71 spin_lock(&chip->reg_lock);
72
73 clear = status & (OXYGEN_CHANNEL_A |
74 OXYGEN_CHANNEL_B |
75 OXYGEN_CHANNEL_C |
76 OXYGEN_CHANNEL_SPDIF |
77 OXYGEN_CHANNEL_MULTICH |
78 OXYGEN_CHANNEL_AC97 |
Clemens Ladischc2353a02008-01-18 09:17:53 +010079 OXYGEN_INT_SPDIF_IN_DETECT |
Clemens Ladisch1e821dd2008-01-28 08:34:21 +010080 OXYGEN_INT_GPIO |
81 OXYGEN_INT_AC97);
Clemens Ladischd0ce9942007-12-23 19:50:57 +010082 if (clear) {
Clemens Ladischc2353a02008-01-18 09:17:53 +010083 if (clear & OXYGEN_INT_SPDIF_IN_DETECT)
84 chip->interrupt_mask &= ~OXYGEN_INT_SPDIF_IN_DETECT;
Clemens Ladischd0ce9942007-12-23 19:50:57 +010085 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK,
86 chip->interrupt_mask & ~clear);
87 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK,
88 chip->interrupt_mask);
89 }
90
91 elapsed_streams = status & chip->pcm_running;
92
93 spin_unlock(&chip->reg_lock);
94
95 for (i = 0; i < PCM_COUNT; ++i)
96 if ((elapsed_streams & (1 << i)) && chip->streams[i])
97 snd_pcm_period_elapsed(chip->streams[i]);
98
Clemens Ladischc2353a02008-01-18 09:17:53 +010099 if (status & OXYGEN_INT_SPDIF_IN_DETECT) {
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100100 spin_lock(&chip->reg_lock);
101 i = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL);
Clemens Ladisch7f0b8942008-01-21 08:53:30 +0100102 if (i & (OXYGEN_SPDIF_SENSE_INT | OXYGEN_SPDIF_LOCK_INT |
103 OXYGEN_SPDIF_RATE_INT)) {
104 /* write the interrupt bit(s) to clear */
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100105 oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, i);
106 schedule_work(&chip->spdif_input_bits_work);
107 }
108 spin_unlock(&chip->reg_lock);
109 }
110
111 if (status & OXYGEN_INT_GPIO)
Clemens Ladisch7c014152008-01-28 08:36:55 +0100112 schedule_work(&chip->gpio_work);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100113
Clemens Ladisch397b1dc2008-09-22 09:04:43 +0200114 if (status & OXYGEN_INT_MIDI) {
115 if (chip->midi)
116 snd_mpu401_uart_interrupt(0, chip->midi->private_data);
117 else
118 oxygen_read_uart(chip);
119 }
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100120
Clemens Ladisch1e821dd2008-01-28 08:34:21 +0100121 if (status & OXYGEN_INT_AC97)
122 wake_up(&chip->ac97_waitqueue);
123
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100124 return IRQ_HANDLED;
125}
126
127static void oxygen_spdif_input_bits_changed(struct work_struct *work)
128{
129 struct oxygen *chip = container_of(work, struct oxygen,
130 spdif_input_bits_work);
Clemens Ladisch7f0b8942008-01-21 08:53:30 +0100131 u32 reg;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100132
Clemens Ladisch7f0b8942008-01-21 08:53:30 +0100133 /*
134 * This function gets called when there is new activity on the SPDIF
135 * input, or when we lose lock on the input signal, or when the rate
136 * changes.
137 */
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100138 msleep(1);
Clemens Ladisch7f0b8942008-01-21 08:53:30 +0100139 spin_lock_irq(&chip->reg_lock);
140 reg = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL);
141 if ((reg & (OXYGEN_SPDIF_SENSE_STATUS |
142 OXYGEN_SPDIF_LOCK_STATUS))
143 == OXYGEN_SPDIF_SENSE_STATUS) {
144 /*
145 * If we detect activity on the SPDIF input but cannot lock to
146 * a signal, the clock bit is likely to be wrong.
147 */
148 reg ^= OXYGEN_SPDIF_IN_CLOCK_MASK;
149 oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, reg);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100150 spin_unlock_irq(&chip->reg_lock);
151 msleep(1);
Clemens Ladisch7f0b8942008-01-21 08:53:30 +0100152 spin_lock_irq(&chip->reg_lock);
153 reg = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL);
154 if ((reg & (OXYGEN_SPDIF_SENSE_STATUS |
155 OXYGEN_SPDIF_LOCK_STATUS))
156 == OXYGEN_SPDIF_SENSE_STATUS) {
157 /* nothing detected with either clock; give up */
158 if ((reg & OXYGEN_SPDIF_IN_CLOCK_MASK)
159 == OXYGEN_SPDIF_IN_CLOCK_192) {
160 /*
161 * Reset clock to <= 96 kHz because this is
162 * more likely to be received next time.
163 */
164 reg &= ~OXYGEN_SPDIF_IN_CLOCK_MASK;
165 reg |= OXYGEN_SPDIF_IN_CLOCK_96;
166 oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, reg);
167 }
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100168 }
169 }
Clemens Ladisch7f0b8942008-01-21 08:53:30 +0100170 spin_unlock_irq(&chip->reg_lock);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100171
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100172 if (chip->controls[CONTROL_SPDIF_INPUT_BITS]) {
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100173 spin_lock_irq(&chip->reg_lock);
Clemens Ladischc2353a02008-01-18 09:17:53 +0100174 chip->interrupt_mask |= OXYGEN_INT_SPDIF_IN_DETECT;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100175 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK,
176 chip->interrupt_mask);
177 spin_unlock_irq(&chip->reg_lock);
178
Clemens Ladisch7f0b8942008-01-21 08:53:30 +0100179 /*
180 * We don't actually know that any channel status bits have
181 * changed, but let's send a notification just to be sure.
182 */
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100183 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
Clemens Ladisch01a3aff2008-01-14 08:56:01 +0100184 &chip->controls[CONTROL_SPDIF_INPUT_BITS]->id);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100185 }
186}
187
Clemens Ladisch7c014152008-01-28 08:36:55 +0100188static void oxygen_gpio_changed(struct work_struct *work)
189{
190 struct oxygen *chip = container_of(work, struct oxygen, gpio_work);
191
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200192 if (chip->model.gpio_changed)
193 chip->model.gpio_changed(chip);
Clemens Ladisch7c014152008-01-28 08:36:55 +0100194}
195
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100196#ifdef CONFIG_PROC_FS
197static void oxygen_proc_read(struct snd_info_entry *entry,
198 struct snd_info_buffer *buffer)
199{
200 struct oxygen *chip = entry->private_data;
201 int i, j;
202
203 snd_iprintf(buffer, "CMI8788\n\n");
Clemens Ladischc1365002008-05-13 09:19:53 +0200204 for (i = 0; i < OXYGEN_IO_SIZE; i += 0x10) {
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100205 snd_iprintf(buffer, "%02x:", i);
206 for (j = 0; j < 0x10; ++j)
207 snd_iprintf(buffer, " %02x", oxygen_read8(chip, i + j));
208 snd_iprintf(buffer, "\n");
209 }
210 if (mutex_lock_interruptible(&chip->mutex) < 0)
211 return;
Clemens Ladisch31c77642008-01-16 08:28:17 +0100212 if (chip->has_ac97_0) {
213 snd_iprintf(buffer, "\nAC97\n");
214 for (i = 0; i < 0x80; i += 0x10) {
215 snd_iprintf(buffer, "%02x:", i);
216 for (j = 0; j < 0x10; j += 2)
217 snd_iprintf(buffer, " %04x",
218 oxygen_read_ac97(chip, 0, i + j));
219 snd_iprintf(buffer, "\n");
220 }
221 }
222 if (chip->has_ac97_1) {
223 snd_iprintf(buffer, "\nAC97 2\n");
224 for (i = 0; i < 0x80; i += 0x10) {
225 snd_iprintf(buffer, "%02x:", i);
226 for (j = 0; j < 0x10; j += 2)
227 snd_iprintf(buffer, " %04x",
228 oxygen_read_ac97(chip, 1, i + j));
229 snd_iprintf(buffer, "\n");
230 }
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100231 }
232 mutex_unlock(&chip->mutex);
233}
234
Takashi Iwaif007dc02008-02-22 18:35:22 +0100235static void oxygen_proc_init(struct oxygen *chip)
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100236{
237 struct snd_info_entry *entry;
238
239 if (!snd_card_proc_new(chip->card, "cmi8788", &entry))
240 snd_info_set_text_ops(entry, chip, oxygen_proc_read);
241}
242#else
243#define oxygen_proc_init(chip)
244#endif
245
Takashi Iwaif007dc02008-02-22 18:35:22 +0100246static void oxygen_init(struct oxygen *chip)
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100247{
248 unsigned int i;
249
250 chip->dac_routing = 1;
251 for (i = 0; i < 8; ++i)
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200252 chip->dac_volume[i] = chip->model.dac_volume_min;
Clemens Ladische9835322008-04-16 09:14:30 +0200253 chip->dac_mute = 1;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100254 chip->spdif_playback_enable = 1;
255 chip->spdif_bits = OXYGEN_SPDIF_C | OXYGEN_SPDIF_ORIGINAL |
256 (IEC958_AES1_CON_PCM_CODER << OXYGEN_SPDIF_CATEGORY_SHIFT);
257 chip->spdif_pcm_bits = chip->spdif_bits;
258
259 if (oxygen_read8(chip, OXYGEN_REVISION) & OXYGEN_REVISION_2)
260 chip->revision = 2;
261 else
262 chip->revision = 1;
263
264 if (chip->revision == 1)
Clemens Ladischc2353a02008-01-18 09:17:53 +0100265 oxygen_set_bits8(chip, OXYGEN_MISC,
266 OXYGEN_MISC_PCI_MEM_W_1_CLOCK);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100267
Clemens Ladisch31c77642008-01-16 08:28:17 +0100268 i = oxygen_read16(chip, OXYGEN_AC97_CONTROL);
269 chip->has_ac97_0 = (i & OXYGEN_AC97_CODEC_0) != 0;
270 chip->has_ac97_1 = (i & OXYGEN_AC97_CODEC_1) != 0;
271
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100272 oxygen_write8_masked(chip, OXYGEN_FUNCTION,
Clemens Ladisch87eedd22008-03-19 08:20:13 +0100273 OXYGEN_FUNCTION_RESET_CODEC |
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200274 chip->model.function_flags,
Clemens Ladisch87eedd22008-03-19 08:20:13 +0100275 OXYGEN_FUNCTION_RESET_CODEC |
276 OXYGEN_FUNCTION_2WIRE_SPI_MASK |
277 OXYGEN_FUNCTION_ENABLE_SPI_4_5);
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100278 oxygen_write8(chip, OXYGEN_DMA_STATUS, 0);
279 oxygen_write8(chip, OXYGEN_DMA_PAUSE, 0);
280 oxygen_write8(chip, OXYGEN_PLAY_CHANNELS,
281 OXYGEN_PLAY_CHANNELS_2 |
282 OXYGEN_DMA_A_BURST_8 |
283 OXYGEN_DMA_MULTICH_BURST_8);
284 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, 0);
Clemens Ladischdb12b8e2008-03-19 08:20:59 +0100285 oxygen_write8_masked(chip, OXYGEN_MISC,
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200286 chip->model.misc_flags,
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100287 OXYGEN_MISC_WRITE_PCI_SUBID |
288 OXYGEN_MISC_REC_C_FROM_SPDIF |
289 OXYGEN_MISC_REC_B_FROM_AC97 |
Clemens Ladischdb12b8e2008-03-19 08:20:59 +0100290 OXYGEN_MISC_REC_A_FROM_MULTICH |
291 OXYGEN_MISC_MIDI);
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100292 oxygen_write8(chip, OXYGEN_REC_FORMAT,
293 (OXYGEN_FORMAT_16 << OXYGEN_REC_FORMAT_A_SHIFT) |
294 (OXYGEN_FORMAT_16 << OXYGEN_REC_FORMAT_B_SHIFT) |
295 (OXYGEN_FORMAT_16 << OXYGEN_REC_FORMAT_C_SHIFT));
296 oxygen_write8(chip, OXYGEN_PLAY_FORMAT,
297 (OXYGEN_FORMAT_16 << OXYGEN_SPDIF_FORMAT_SHIFT) |
298 (OXYGEN_FORMAT_16 << OXYGEN_MULTICH_FORMAT_SHIFT));
299 oxygen_write8(chip, OXYGEN_REC_CHANNELS, OXYGEN_REC_CHANNELS_2_2_2);
Clemens Ladischc9946b22008-01-21 08:44:24 +0100300 oxygen_write16(chip, OXYGEN_I2S_MULTICH_FORMAT,
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200301 OXYGEN_RATE_48000 | chip->model.dac_i2s_format |
Clemens Ladischfa5d8102008-03-19 08:17:33 +0100302 OXYGEN_I2S_MCLK_256 | OXYGEN_I2S_BITS_16 |
Clemens Ladischc9946b22008-01-21 08:44:24 +0100303 OXYGEN_I2S_MASTER | OXYGEN_I2S_BCLK_64);
Clemens Ladischd76596b2008-09-22 09:02:08 +0200304 if (chip->model.device_config & CAPTURE_0_FROM_I2S_1)
Clemens Ladisch43dd89c2008-03-19 08:21:32 +0100305 oxygen_write16(chip, OXYGEN_I2S_A_FORMAT,
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200306 OXYGEN_RATE_48000 | chip->model.adc_i2s_format |
Clemens Ladisch43dd89c2008-03-19 08:21:32 +0100307 OXYGEN_I2S_MCLK_256 | OXYGEN_I2S_BITS_16 |
308 OXYGEN_I2S_MASTER | OXYGEN_I2S_BCLK_64);
309 else
310 oxygen_write16(chip, OXYGEN_I2S_A_FORMAT,
311 OXYGEN_I2S_MASTER | OXYGEN_I2S_MUTE_MCLK);
Clemens Ladischd76596b2008-09-22 09:02:08 +0200312 if (chip->model.device_config & (CAPTURE_0_FROM_I2S_2 |
313 CAPTURE_2_FROM_I2S_2))
Clemens Ladisch43dd89c2008-03-19 08:21:32 +0100314 oxygen_write16(chip, OXYGEN_I2S_B_FORMAT,
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200315 OXYGEN_RATE_48000 | chip->model.adc_i2s_format |
Clemens Ladisch43dd89c2008-03-19 08:21:32 +0100316 OXYGEN_I2S_MCLK_256 | OXYGEN_I2S_BITS_16 |
317 OXYGEN_I2S_MASTER | OXYGEN_I2S_BCLK_64);
318 else
319 oxygen_write16(chip, OXYGEN_I2S_B_FORMAT,
320 OXYGEN_I2S_MASTER | OXYGEN_I2S_MUTE_MCLK);
Clemens Ladischc9946b22008-01-21 08:44:24 +0100321 oxygen_write16(chip, OXYGEN_I2S_C_FORMAT,
Clemens Ladisch43dd89c2008-03-19 08:21:32 +0100322 OXYGEN_I2S_MASTER | OXYGEN_I2S_MUTE_MCLK);
Clemens Ladisch1d98c7d2008-04-09 09:16:33 +0200323 oxygen_clear_bits32(chip, OXYGEN_SPDIF_CONTROL,
324 OXYGEN_SPDIF_OUT_ENABLE |
325 OXYGEN_SPDIF_LOOPBACK);
Clemens Ladischd76596b2008-09-22 09:02:08 +0200326 if (chip->model.device_config & CAPTURE_1_FROM_SPDIF)
Clemens Ladisch1d98c7d2008-04-09 09:16:33 +0200327 oxygen_write32_masked(chip, OXYGEN_SPDIF_CONTROL,
328 OXYGEN_SPDIF_SENSE_MASK |
329 OXYGEN_SPDIF_LOCK_MASK |
330 OXYGEN_SPDIF_RATE_MASK |
331 OXYGEN_SPDIF_LOCK_PAR |
332 OXYGEN_SPDIF_IN_CLOCK_96,
333 OXYGEN_SPDIF_SENSE_MASK |
334 OXYGEN_SPDIF_LOCK_MASK |
335 OXYGEN_SPDIF_RATE_MASK |
336 OXYGEN_SPDIF_SENSE_PAR |
337 OXYGEN_SPDIF_LOCK_PAR |
338 OXYGEN_SPDIF_IN_CLOCK_MASK);
339 else
340 oxygen_clear_bits32(chip, OXYGEN_SPDIF_CONTROL,
341 OXYGEN_SPDIF_SENSE_MASK |
342 OXYGEN_SPDIF_LOCK_MASK |
343 OXYGEN_SPDIF_RATE_MASK);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100344 oxygen_write32(chip, OXYGEN_SPDIF_OUTPUT_BITS, chip->spdif_bits);
Clemens Ladisch4a4bc532008-05-13 09:24:39 +0200345 oxygen_write16(chip, OXYGEN_2WIRE_BUS_STATUS,
346 OXYGEN_2WIRE_LENGTH_8 |
347 OXYGEN_2WIRE_INTERRUPT_MASK |
348 OXYGEN_2WIRE_SPEED_STANDARD);
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100349 oxygen_clear_bits8(chip, OXYGEN_MPU401_CONTROL, OXYGEN_MPU401_LOOPBACK);
350 oxygen_write8(chip, OXYGEN_GPI_INTERRUPT_MASK, 0);
351 oxygen_write16(chip, OXYGEN_GPIO_INTERRUPT_MASK, 0);
Clemens Ladischc9946b22008-01-21 08:44:24 +0100352 oxygen_write16(chip, OXYGEN_PLAY_ROUTING,
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100353 OXYGEN_PLAY_MULTICH_I2S_DAC |
354 OXYGEN_PLAY_SPDIF_SPDIF |
Clemens Ladischc9946b22008-01-21 08:44:24 +0100355 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
356 (1 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
357 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
358 (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT));
359 oxygen_write8(chip, OXYGEN_REC_ROUTING,
360 OXYGEN_REC_A_ROUTE_I2S_ADC_1 |
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100361 OXYGEN_REC_B_ROUTE_I2S_ADC_2 |
Clemens Ladischc9946b22008-01-21 08:44:24 +0100362 OXYGEN_REC_C_ROUTE_SPDIF);
363 oxygen_write8(chip, OXYGEN_ADC_MONITOR, 0);
364 oxygen_write8(chip, OXYGEN_A_MONITOR_ROUTING,
365 (0 << OXYGEN_A_MONITOR_ROUTE_0_SHIFT) |
366 (1 << OXYGEN_A_MONITOR_ROUTE_1_SHIFT) |
367 (2 << OXYGEN_A_MONITOR_ROUTE_2_SHIFT) |
368 (3 << OXYGEN_A_MONITOR_ROUTE_3_SHIFT));
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100369
Clemens Ladisch1d98c7d2008-04-09 09:16:33 +0200370 if (chip->has_ac97_0 | chip->has_ac97_1)
371 oxygen_write8(chip, OXYGEN_AC97_INTERRUPT_MASK,
372 OXYGEN_AC97_INT_READ_DONE |
373 OXYGEN_AC97_INT_WRITE_DONE);
374 else
375 oxygen_write8(chip, OXYGEN_AC97_INTERRUPT_MASK, 0);
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100376 oxygen_write32(chip, OXYGEN_AC97_OUT_CONFIG, 0);
377 oxygen_write32(chip, OXYGEN_AC97_IN_CONFIG, 0);
378 if (!(chip->has_ac97_0 | chip->has_ac97_1))
379 oxygen_set_bits16(chip, OXYGEN_AC97_CONTROL,
380 OXYGEN_AC97_CLOCK_DISABLE);
381 if (!chip->has_ac97_0) {
382 oxygen_set_bits16(chip, OXYGEN_AC97_CONTROL,
383 OXYGEN_AC97_NO_CODEC_0);
384 } else {
Clemens Ladisch31c77642008-01-16 08:28:17 +0100385 oxygen_write_ac97(chip, 0, AC97_RESET, 0);
386 msleep(1);
Clemens Ladisch878ac3e2008-01-21 08:50:19 +0100387 oxygen_ac97_set_bits(chip, 0, CM9780_GPIO_SETUP,
388 CM9780_GPIO0IO | CM9780_GPIO1IO);
389 oxygen_ac97_set_bits(chip, 0, CM9780_MIXER,
390 CM9780_BSTSEL | CM9780_STRO_MIC |
391 CM9780_MIX2FR | CM9780_PCBSW);
392 oxygen_ac97_set_bits(chip, 0, CM9780_JACK,
393 CM9780_RSOE | CM9780_CBOE |
394 CM9780_SSOE | CM9780_FROE |
395 CM9780_MIC2MIC | CM9780_LI2LI);
Clemens Ladisch31c77642008-01-16 08:28:17 +0100396 oxygen_write_ac97(chip, 0, AC97_MASTER, 0x0000);
397 oxygen_write_ac97(chip, 0, AC97_PC_BEEP, 0x8000);
398 oxygen_write_ac97(chip, 0, AC97_MIC, 0x8808);
399 oxygen_write_ac97(chip, 0, AC97_LINE, 0x0808);
400 oxygen_write_ac97(chip, 0, AC97_CD, 0x8808);
401 oxygen_write_ac97(chip, 0, AC97_VIDEO, 0x8808);
402 oxygen_write_ac97(chip, 0, AC97_AUX, 0x8808);
403 oxygen_write_ac97(chip, 0, AC97_REC_GAIN, 0x8000);
404 oxygen_write_ac97(chip, 0, AC97_CENTER_LFE_MASTER, 0x8080);
405 oxygen_write_ac97(chip, 0, AC97_SURROUND_MASTER, 0x8080);
Clemens Ladische97f7992008-04-01 10:02:18 +0200406 oxygen_ac97_clear_bits(chip, 0, CM9780_GPIO_STATUS,
407 CM9780_GPO0);
Clemens Ladisch31c77642008-01-16 08:28:17 +0100408 /* power down unused ADCs and DACs */
409 oxygen_ac97_set_bits(chip, 0, AC97_POWERDOWN,
410 AC97_PD_PR0 | AC97_PD_PR1);
411 oxygen_ac97_set_bits(chip, 0, AC97_EXTENDED_STATUS,
412 AC97_EA_PRI | AC97_EA_PRJ | AC97_EA_PRK);
413 }
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100414 if (chip->has_ac97_1) {
415 oxygen_set_bits32(chip, OXYGEN_AC97_OUT_CONFIG,
416 OXYGEN_AC97_CODEC1_SLOT3 |
417 OXYGEN_AC97_CODEC1_SLOT4);
418 oxygen_write_ac97(chip, 1, AC97_RESET, 0);
419 msleep(1);
420 oxygen_write_ac97(chip, 1, AC97_MASTER, 0x0000);
421 oxygen_write_ac97(chip, 1, AC97_HEADPHONE, 0x8000);
422 oxygen_write_ac97(chip, 1, AC97_PC_BEEP, 0x8000);
423 oxygen_write_ac97(chip, 1, AC97_MIC, 0x8808);
424 oxygen_write_ac97(chip, 1, AC97_LINE, 0x8808);
425 oxygen_write_ac97(chip, 1, AC97_CD, 0x8808);
426 oxygen_write_ac97(chip, 1, AC97_VIDEO, 0x8808);
427 oxygen_write_ac97(chip, 1, AC97_AUX, 0x8808);
428 oxygen_write_ac97(chip, 1, AC97_PCM, 0x0808);
429 oxygen_write_ac97(chip, 1, AC97_REC_SEL, 0x0000);
Clemens Ladischa3601562008-01-28 08:35:20 +0100430 oxygen_write_ac97(chip, 1, AC97_REC_GAIN, 0x0000);
Clemens Ladischb78e3db2008-01-25 08:39:26 +0100431 oxygen_ac97_set_bits(chip, 1, 0x6a, 0x0040);
432 }
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100433}
434
435static void oxygen_card_free(struct snd_card *card)
436{
437 struct oxygen *chip = card->private_data;
438
439 spin_lock_irq(&chip->reg_lock);
440 chip->interrupt_mask = 0;
441 chip->pcm_running = 0;
442 oxygen_write16(chip, OXYGEN_DMA_STATUS, 0);
443 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, 0);
444 spin_unlock_irq(&chip->reg_lock);
Jeff Garzikf000fd82008-04-22 13:50:34 +0200445 if (chip->irq >= 0)
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100446 free_irq(chip->irq, chip);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100447 flush_scheduled_work();
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200448 chip->model.cleanup(chip);
Clemens Ladisch6ed91152009-02-19 08:38:25 +0100449 kfree(chip->model_data);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100450 mutex_destroy(&chip->mutex);
451 pci_release_regions(chip->pci);
452 pci_disable_device(chip->pci);
453}
454
Takashi Iwaif007dc02008-02-22 18:35:22 +0100455int oxygen_pci_probe(struct pci_dev *pci, int index, char *id,
Clemens Ladischbb718582009-02-19 08:37:13 +0100456 struct module *owner,
Clemens Ladisch568c59e2008-09-22 08:56:01 +0200457 const struct oxygen_model *model,
458 unsigned long driver_data)
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100459{
460 struct snd_card *card;
461 struct oxygen *chip;
462 int err;
463
Clemens Ladisch6ed91152009-02-19 08:38:25 +0100464 err = snd_card_create(index, id, owner, sizeof(*chip), &card);
Takashi Iwaie58de7b2008-12-28 16:44:30 +0100465 if (err < 0)
466 return err;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100467
468 chip = card->private_data;
469 chip->card = card;
470 chip->pci = pci;
471 chip->irq = -1;
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200472 chip->model = *model;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100473 spin_lock_init(&chip->reg_lock);
474 mutex_init(&chip->mutex);
475 INIT_WORK(&chip->spdif_input_bits_work,
476 oxygen_spdif_input_bits_changed);
Clemens Ladisch7c014152008-01-28 08:36:55 +0100477 INIT_WORK(&chip->gpio_work, oxygen_gpio_changed);
Clemens Ladisch1e821dd2008-01-28 08:34:21 +0100478 init_waitqueue_head(&chip->ac97_waitqueue);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100479
480 err = pci_enable_device(pci);
481 if (err < 0)
482 goto err_card;
483
484 err = pci_request_regions(pci, model->chip);
485 if (err < 0) {
486 snd_printk(KERN_ERR "cannot reserve PCI resources\n");
487 goto err_pci_enable;
488 }
489
490 if (!(pci_resource_flags(pci, 0) & IORESOURCE_IO) ||
Clemens Ladischc1365002008-05-13 09:19:53 +0200491 pci_resource_len(pci, 0) < OXYGEN_IO_SIZE) {
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100492 snd_printk(KERN_ERR "invalid PCI I/O range\n");
493 err = -ENXIO;
494 goto err_pci_regions;
495 }
496 chip->addr = pci_resource_start(pci, 0);
497
Clemens Ladisch6ed91152009-02-19 08:38:25 +0100498 if (chip->model.model_data_size) {
499 chip->model_data = kmalloc(chip->model.model_data_size,
500 GFP_KERNEL);
501 if (!chip->model_data) {
502 err = -ENOMEM;
503 goto err_pci_regions;
504 }
505 }
506
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100507 pci_set_master(pci);
508 snd_card_set_dev(card, &pci->dev);
509 card->private_free = oxygen_card_free;
510
Clemens Ladisch568c59e2008-09-22 08:56:01 +0200511 if (chip->model.probe) {
512 err = chip->model.probe(chip, driver_data);
513 if (err < 0)
514 goto err_card;
515 }
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100516 oxygen_init(chip);
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200517 chip->model.init(chip);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100518
519 err = request_irq(pci->irq, oxygen_interrupt, IRQF_SHARED,
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200520 chip->model.chip, chip);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100521 if (err < 0) {
522 snd_printk(KERN_ERR "cannot grab interrupt %d\n", pci->irq);
523 goto err_card;
524 }
525 chip->irq = pci->irq;
526
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200527 strcpy(card->driver, chip->model.chip);
528 strcpy(card->shortname, chip->model.shortname);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100529 sprintf(card->longname, "%s (rev %u) at %#lx, irq %i",
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200530 chip->model.longname, chip->revision, chip->addr, chip->irq);
531 strcpy(card->mixername, chip->model.chip);
532 snd_component_add(card, chip->model.chip);
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100533
534 err = oxygen_pcm_init(chip);
535 if (err < 0)
536 goto err_card;
537
538 err = oxygen_mixer_init(chip);
539 if (err < 0)
540 goto err_card;
541
Clemens Ladischdbbbd672008-09-22 09:03:42 +0200542 if (chip->model.device_config & (MIDI_OUTPUT | MIDI_INPUT)) {
543 unsigned int info_flags = MPU401_INFO_INTEGRATED;
544 if (chip->model.device_config & MIDI_OUTPUT)
545 info_flags |= MPU401_INFO_OUTPUT;
546 if (chip->model.device_config & MIDI_INPUT)
547 info_flags |= MPU401_INFO_INPUT;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100548 err = snd_mpu401_uart_new(card, 0, MPU401_HW_CMIPCI,
549 chip->addr + OXYGEN_MPU401,
Clemens Ladischdbbbd672008-09-22 09:03:42 +0200550 info_flags, 0, 0,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100551 &chip->midi);
552 if (err < 0)
553 goto err_card;
554 }
555
556 oxygen_proc_init(chip);
557
558 spin_lock_irq(&chip->reg_lock);
Clemens Ladischd76596b2008-09-22 09:02:08 +0200559 if (chip->model.device_config & CAPTURE_1_FROM_SPDIF)
Clemens Ladisch1d98c7d2008-04-09 09:16:33 +0200560 chip->interrupt_mask |= OXYGEN_INT_SPDIF_IN_DETECT;
561 if (chip->has_ac97_0 | chip->has_ac97_1)
562 chip->interrupt_mask |= OXYGEN_INT_AC97;
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100563 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, chip->interrupt_mask);
564 spin_unlock_irq(&chip->reg_lock);
565
566 err = snd_card_register(card);
567 if (err < 0)
568 goto err_card;
569
570 pci_set_drvdata(pci, card);
571 return 0;
572
573err_pci_regions:
574 pci_release_regions(pci);
575err_pci_enable:
576 pci_disable_device(pci);
577err_card:
578 snd_card_free(card);
579 return err;
580}
581EXPORT_SYMBOL(oxygen_pci_probe);
582
Takashi Iwaif007dc02008-02-22 18:35:22 +0100583void oxygen_pci_remove(struct pci_dev *pci)
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100584{
585 snd_card_free(pci_get_drvdata(pci));
586 pci_set_drvdata(pci, NULL);
587}
588EXPORT_SYMBOL(oxygen_pci_remove);
Clemens Ladisch4a4bc532008-05-13 09:24:39 +0200589
590#ifdef CONFIG_PM
591int oxygen_pci_suspend(struct pci_dev *pci, pm_message_t state)
592{
593 struct snd_card *card = pci_get_drvdata(pci);
594 struct oxygen *chip = card->private_data;
595 unsigned int i, saved_interrupt_mask;
596
597 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
598
599 for (i = 0; i < PCM_COUNT; ++i)
600 if (chip->streams[i])
601 snd_pcm_suspend(chip->streams[i]);
602
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200603 if (chip->model.suspend)
604 chip->model.suspend(chip);
Clemens Ladisch4a4bc532008-05-13 09:24:39 +0200605
606 spin_lock_irq(&chip->reg_lock);
607 saved_interrupt_mask = chip->interrupt_mask;
608 chip->interrupt_mask = 0;
609 oxygen_write16(chip, OXYGEN_DMA_STATUS, 0);
610 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, 0);
611 spin_unlock_irq(&chip->reg_lock);
612
613 synchronize_irq(chip->irq);
614 flush_scheduled_work();
615 chip->interrupt_mask = saved_interrupt_mask;
616
617 pci_disable_device(pci);
618 pci_save_state(pci);
619 pci_set_power_state(pci, pci_choose_state(pci, state));
620 return 0;
621}
622EXPORT_SYMBOL(oxygen_pci_suspend);
623
624static const u32 registers_to_restore[OXYGEN_IO_SIZE / 32] = {
625 0xffffffff, 0x00ff077f, 0x00011d08, 0x007f00ff,
626 0x00300000, 0x00000fe4, 0x0ff7001f, 0x00000000
627};
628static const u32 ac97_registers_to_restore[2][0x40 / 32] = {
629 { 0x18284fa2, 0x03060000 },
630 { 0x00007fa6, 0x00200000 }
631};
632
633static inline int is_bit_set(const u32 *bitmap, unsigned int bit)
634{
635 return bitmap[bit / 32] & (1 << (bit & 31));
636}
637
638static void oxygen_restore_ac97(struct oxygen *chip, unsigned int codec)
639{
640 unsigned int i;
641
642 oxygen_write_ac97(chip, codec, AC97_RESET, 0);
643 msleep(1);
644 for (i = 1; i < 0x40; ++i)
645 if (is_bit_set(ac97_registers_to_restore[codec], i))
646 oxygen_write_ac97(chip, codec, i * 2,
647 chip->saved_ac97_registers[codec][i]);
648}
649
650int oxygen_pci_resume(struct pci_dev *pci)
651{
652 struct snd_card *card = pci_get_drvdata(pci);
653 struct oxygen *chip = card->private_data;
654 unsigned int i;
655
656 pci_set_power_state(pci, PCI_D0);
657 pci_restore_state(pci);
658 if (pci_enable_device(pci) < 0) {
659 snd_printk(KERN_ERR "cannot reenable device");
660 snd_card_disconnect(card);
661 return -EIO;
662 }
663 pci_set_master(pci);
664
665 oxygen_write16(chip, OXYGEN_DMA_STATUS, 0);
666 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, 0);
667 for (i = 0; i < OXYGEN_IO_SIZE; ++i)
668 if (is_bit_set(registers_to_restore, i))
669 oxygen_write8(chip, i, chip->saved_registers._8[i]);
670 if (chip->has_ac97_0)
671 oxygen_restore_ac97(chip, 0);
672 if (chip->has_ac97_1)
673 oxygen_restore_ac97(chip, 1);
674
Clemens Ladisch9bd6a732008-09-22 08:55:19 +0200675 if (chip->model.resume)
676 chip->model.resume(chip);
Clemens Ladisch4a4bc532008-05-13 09:24:39 +0200677
678 oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, chip->interrupt_mask);
679
680 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
681 return 0;
682}
683EXPORT_SYMBOL(oxygen_pci_resume);
684#endif /* CONFIG_PM */